HEPnOSService.cpp 3.18 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1
2
3
4
5
6
7
8
9
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
10
#include <cstring>
Matthieu Dorier's avatar
Matthieu Dorier committed
11
12
13
#include <unistd.h>
#include <mpi.h>
#include <margo.h>
14
#include <sdskv-server.hpp>
15
#include "ServiceConfig.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
16
#include "ConnectionInfoGenerator.hpp"
17
#include "hepnos-service.h"
Matthieu Dorier's avatar
Matthieu Dorier committed
18
19
20

#define ASSERT(__cond, __msg, ...) { if(!(__cond)) { fprintf(stderr, "[%s:%d] " __msg, __FILE__, __LINE__, __VA_ARGS__); exit(-1); } }

21
void hepnos_run_service(MPI_Comm comm, const char* config_file, const char* connection_file)
Matthieu Dorier's avatar
Matthieu Dorier committed
22
23
24
25
{
    margo_instance_id mid;
    int ret;
    int rank;
26
27

    MPI_Comm_rank(comm, &rank);
Matthieu Dorier's avatar
Matthieu Dorier committed
28

29
30
31
32
33
34
35
36
37
38
39
40
    /* load configuration */
    std::unique_ptr<hepnos::ServiceConfig> config;
    try {
        config = std::make_unique<hepnos::ServiceConfig>(config_file, rank);
    } catch(const std::exception& e) {
        std::cerr << "Error: when reading configuration:" << std::endl;
        std::cerr << "  " << e.what() << std::endl;
        std::cerr << "Aborting." << std::endl;
        MPI_Abort(MPI_COMM_WORLD, -1);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
41
    /* Margo initialization */
42
    mid = margo_init(config->getAddress().c_str(), MARGO_SERVER_MODE, 0, config->getNumThreads()-1);
Matthieu Dorier's avatar
Matthieu Dorier committed
43
44
    if (mid == MARGO_INSTANCE_NULL)
    {
45
46
        std::cerr << "Error: unable to initialize margo" << std::endl;
        std::cerr << "Aborting." << std::endl;
47
48
        MPI_Abort(MPI_COMM_WORLD, -1);
        return;
Matthieu Dorier's avatar
Matthieu Dorier committed
49
50
51
52
53
54
55
56
57
58
    }
    margo_enable_remote_shutdown(mid);

    /* Get self address as string */
    hg_addr_t self_addr;
    margo_addr_self(mid, &self_addr);
    char self_addr_str[128];
    hg_size_t self_addr_str_size = 128;
    margo_addr_to_string(mid, self_addr_str, &self_addr_str_size, self_addr);

59
60
    if(config->hasDatabase()) {
        /* SDSKV provider initialization */
61
        for(auto sdskv_provider_id = 0; sdskv_provider_id < config->getNumDatabaseProviders(); sdskv_provider_id++) {
62
63

            sdskv::provider* provider = sdskv::provider::create(mid, sdskv_provider_id, SDSKV_ABT_POOL_DEFAULT);
64

65
66
67
68
69
70
71
72
73
74
75
76
77
            for(unsigned i=0 ; i < config->getNumDatabaseTargets(); i++)  {
                auto db_path = config->getDatabasePath(rank, sdskv_provider_id, i);
                auto db_name = config->getDatabaseName(rank, sdskv_provider_id, i);
                sdskv_db_type_t db_type;
                if(config->getDatabaseType() == "map") db_type = KVDB_MAP;
                if(config->getDatabaseType() == "ldb") db_type = KVDB_LEVELDB;
                if(config->getDatabaseType() == "bdb") db_type = KVDB_BERKELEYDB;
                sdskv_database_id_t db_id;
                sdskv_config_t config;
                std::memset(&config, 0, sizeof(config));
                config.db_name = db_name.c_str();
                config.db_path = db_path.c_str();
                config.db_type = db_type;
78
                db_id = provider->attach_database(config);
79
80
            }
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
81
82
83
84
    }

    margo_addr_free(mid, self_addr);

85
86
87
    hepnos::ConnectionInfoGenerator fileGen(self_addr_str, 
            config->getNumDatabaseProviders(),
            config->getNumStorageProviders());
Matthieu Dorier's avatar
Matthieu Dorier committed
88
    fileGen.generateFile(MPI_COMM_WORLD, connection_file);
Matthieu Dorier's avatar
Matthieu Dorier committed
89

90
    margo_wait_for_finalize(mid);
Matthieu Dorier's avatar
Matthieu Dorier committed
91
}