sdskv-server.cc 53.4 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <map>
Matthieu Dorier's avatar
Matthieu Dorier committed
8
#include <iostream>
Matthieu Dorier's avatar
Matthieu Dorier committed
9 10 11 12 13 14 15 16 17 18 19
#include <unordered_map>
#define SDSKV
#include "datastore/datastore_factory.h"
#include "sdskv-rpc-types.h"
#include "sdskv-server.h"

struct sdskv_server_context_t
{
    std::unordered_map<sdskv_database_id_t, AbstractDataStore*> databases;
    std::map<std::string, sdskv_database_id_t> name2id;
    std::map<sdskv_database_id_t, std::string> id2name;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    hg_id_t sdskv_put_id;
    hg_id_t sdskv_bulk_put_id;
    hg_id_t sdskv_get_id;
    hg_id_t sdskv_exists_id;
    hg_id_t sdskv_erase_id;
    hg_id_t sdskv_length_id;
    hg_id_t sdskv_bulk_get_id;
    hg_id_t sdskv_open_id;
    hg_id_t sdskv_list_keys_id;
    hg_id_t sdskv_list_keyvals_id;
    /* migration */
    hg_id_t sdskv_migrate_keys_id;
    hg_id_t sdskv_migrate_key_range_id;
    hg_id_t sdskv_migrate_keys_prefixed_id;
    hg_id_t sdskv_migrate_all_keys_id;
    hg_id_t sdskv_migrate_database_id;
};

template<typename F>
struct scoped_call {
    F _f;
    scoped_call(const F& f) : _f(f) {}
    ~scoped_call() { _f(); }
Matthieu Dorier's avatar
Matthieu Dorier committed
44 45
};

46 47 48 49 50
template<typename F>
inline scoped_call<F> at_exit(F&& f) {
    return scoped_call<F>(std::forward<F>(f));
}

Matthieu Dorier's avatar
Matthieu Dorier committed
51 52 53 54 55 56
DECLARE_MARGO_RPC_HANDLER(sdskv_put_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_length_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_get_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_open_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_bulk_put_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_bulk_get_ult)
57
DECLARE_MARGO_RPC_HANDLER(sdskv_list_keys_ult)
58
DECLARE_MARGO_RPC_HANDLER(sdskv_list_keyvals_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
59
DECLARE_MARGO_RPC_HANDLER(sdskv_erase_ult)
60
DECLARE_MARGO_RPC_HANDLER(sdskv_exists_ult)
61 62 63 64 65 66 67 68 69 70 71 72
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_keys_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_key_range_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_keys_prefixed_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_all_keys_ult)
DECLARE_MARGO_RPC_HANDLER(sdskv_migrate_database_ult)
/*
DECLARE_MARGO_RPC_HANDLER(__migrate_keys_ult)
DECLARE_MARGO_RPC_HANDLER(__migrate_key_range_ult)
DECLARE_MARGO_RPC_HANDLER(__migrate_keys_prefixed_ult)
DECLARE_MARGO_RPC_HANDLER(__migrate_all_keys_ult)
DECLARE_MARGO_RPC_HANDLER(__migrate_database_ult)
*/
Matthieu Dorier's avatar
Matthieu Dorier committed
73 74 75 76
static void sdskv_server_finalize_cb(void *data);

extern "C" int sdskv_provider_register(
        margo_instance_id mid,
77
        uint16_t provider_id,
Matthieu Dorier's avatar
Matthieu Dorier committed
78 79 80 81 82 83 84 85 86
        ABT_pool abt_pool,
        sdskv_provider_t* provider)
{
    sdskv_server_context_t *tmp_svr_ctx;

    /* check if a provider with the same multiplex id already exists */
    {
        hg_id_t id;
        hg_bool_t flag;
87
        margo_provider_registered_name(mid, "sdskv_put_rpc", provider_id, &id, &flag);
Matthieu Dorier's avatar
Matthieu Dorier committed
88
        if(flag == HG_TRUE) {
89
            fprintf(stderr, "sdskv_provider_register(): a provider with the same provider id (%d) already exists\n", provider_id);
90
            return SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
91 92 93 94
        }
    }

    /* allocate the resulting structure */    
Matthieu Dorier's avatar
Matthieu Dorier committed
95
    tmp_svr_ctx = new sdskv_server_context_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
96
    if(!tmp_svr_ctx)
97
        return SDSKV_ERR_ALLOCATION;
Matthieu Dorier's avatar
Matthieu Dorier committed
98 99 100

    /* register RPCs */
    hg_id_t rpc_id;
101
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_put_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
102
            put_in_t, put_out_t,
103
            sdskv_put_ult, provider_id, abt_pool);
104
    tmp_svr_ctx->sdskv_put_id = rpc_id;
105 106
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_bulk_put_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
107
            bulk_put_in_t, bulk_put_out_t,
108
            sdskv_bulk_put_ult, provider_id, abt_pool);
109
    tmp_svr_ctx->sdskv_bulk_put_id = rpc_id;
110 111
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_get_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
112
            get_in_t, get_out_t,
113
            sdskv_get_ult, provider_id, abt_pool);
114
    tmp_svr_ctx->sdskv_get_id = rpc_id;
115 116
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_length_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
117
            length_in_t, length_out_t,
118
            sdskv_length_ult, provider_id, abt_pool);
119
    tmp_svr_ctx->sdskv_length_id = rpc_id;
120
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
121 122 123
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_exists_rpc",
            exists_in_t, exists_out_t,
            sdskv_exists_ult, provider_id, abt_pool);
124
    tmp_svr_ctx->sdskv_exists_id = rpc_id;
125
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
126
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_bulk_get_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
127
            bulk_get_in_t, bulk_get_out_t,
128
            sdskv_bulk_get_ult, provider_id, abt_pool);
129
    tmp_svr_ctx->sdskv_bulk_get_id = rpc_id;
130 131
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_open_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
132
            open_in_t, open_out_t,
133
            sdskv_open_ult, provider_id, abt_pool);
134
    tmp_svr_ctx->sdskv_open_id = rpc_id;
135 136
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_list_keys_rpc",
137
            list_keys_in_t, list_keys_out_t,
138
            sdskv_list_keys_ult, provider_id, abt_pool);
139
    tmp_svr_ctx->sdskv_list_keys_id = rpc_id;
140 141
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_list_keyvals_rpc",
142
            list_keyvals_in_t, list_keyvals_out_t,
143
            sdskv_list_keyvals_ult, provider_id, abt_pool);
144
    tmp_svr_ctx->sdskv_list_keyvals_id = rpc_id;
145 146
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_erase_rpc",
Matthieu Dorier's avatar
Matthieu Dorier committed
147
            erase_in_t, erase_out_t,
148
            sdskv_erase_ult, provider_id, abt_pool);
149
    tmp_svr_ctx->sdskv_erase_id = rpc_id;
150
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    /* migration RPC */
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_keys_rpc",
            migrate_keys_in_t, migrate_keys_out_t,
            sdskv_migrate_keys_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_keys_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_key_range_rpc",
            migrate_key_range_in_t, migrate_keys_out_t,
            sdskv_migrate_key_range_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_key_range_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_keys_prefixed_rpc",
            migrate_keys_prefixed_in_t, migrate_keys_out_t,
            sdskv_migrate_keys_prefixed_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_keys_prefixed_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_all_keys_rpc",
            migrate_all_keys_in_t, migrate_keys_out_t,
            sdskv_migrate_all_keys_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_all_keys_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "sdskv_migrate_database_rpc",
            migrate_database_in_t, migrate_database_out_t,
            sdskv_migrate_database_ult, provider_id, abt_pool);
    tmp_svr_ctx->sdskv_migrate_database_id = rpc_id;
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    /*
    rpc_id = margo_register_provider(mid, "__migrate_keys_rpc",
            __migrate_keys_in_t, __migrate_keys_out_t,
            sdskv_migrate_keys_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = margo_register_provider(mid, "sdskv_migrate_key_range_rpc",
            migrate_key_range_in_t, migrate_keys_out_t,
            sdskv_migrate_key_range_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = margo_register_provider(mid, "sdskv_migrate_keys_prefixed_rpc",
            migrate_prefixed_keys_in_t, migrate_keys_out_t,
            sdskv_migrate_prefixed_keys_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = margo_register_provider(mid, "sdskv_migrate_all_keys_rpc",
            migrate_all_keys_in_t, migrate_keys_out_t,
            sdskv_migrate_all_keys_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = margo_register_provider(mid, "sdskv_migrate_database_rpc",
            migrate_database_in_t, migrate_database_out_t,
            sdskv_migrate_database_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    */
Matthieu Dorier's avatar
Matthieu Dorier committed
199 200 201 202 203 204
    /* install the bake server finalize callback */
    margo_push_finalize_callback(mid, &sdskv_server_finalize_cb, tmp_svr_ctx);

    if(provider != SDSKV_PROVIDER_IGNORE)
        *provider = tmp_svr_ctx;

205
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
206 207
}

208
extern "C" int sdskv_provider_attach_database(
Matthieu Dorier's avatar
Matthieu Dorier committed
209
        sdskv_provider_t provider,
210
        const sdskv_config_t* config,
Matthieu Dorier's avatar
Matthieu Dorier committed
211 212
        sdskv_database_id_t* db_id)
{
213 214
    auto db = datastore_factory::open_datastore(config->db_type, 
            std::string(config->db_name), std::string(config->db_path));
215
    if(db == nullptr) return SDSKV_ERR_DB_CREATE;
216
    db->set_comparison_function(config->db_comparison_fn);
Matthieu Dorier's avatar
Matthieu Dorier committed
217
    sdskv_database_id_t id = (sdskv_database_id_t)(db);
218 219 220
    if(config->db_no_overwrite) {
        db->set_no_overwrite();
    }
Matthieu Dorier's avatar
Matthieu Dorier committed
221

222 223
    provider->name2id[std::string(config->db_name)] = id;
    provider->id2name[id] = std::string(config->db_name);
Matthieu Dorier's avatar
Matthieu Dorier committed
224 225
    provider->databases[id] = db;

226 227
    *db_id = id;

228
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
229 230
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
extern "C" int sdskv_provider_add_database(
        sdskv_provider_t provider,
        const char* db_name,
        const char* db_path,
        sdskv_db_type_t db_type,
        sdskv_compare_fn comp_fn,
        sdskv_database_id_t* db_id) {
    sdskv_config_t config = SDSKV_CONFIG_DEFAULT;
    config.db_name = db_name;
    config.db_path = db_path;
    config.db_type = db_type;
    config.db_comparison_fn = comp_fn;
    return sdskv_provider_attach_database(
            provider, &config, db_id);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
247 248 249 250 251 252 253 254 255 256 257
extern "C" int sdskv_provider_remove_database(
        sdskv_provider_t provider,
        sdskv_database_id_t db_id)
{
    if(provider->databases.count(db_id)) {
        auto dbname = provider->id2name[db_id];
        provider->id2name.erase(db_id);
        provider->name2id.erase(dbname);
        auto db = provider->databases[db_id];
        delete db;
        provider->databases.erase(db_id);
258
        return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
259
    } else {
260
        return SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
261 262 263 264 265 266 267 268 269 270 271 272 273
    }
}

extern "C" int sdskv_provider_remove_all_databases(
        sdskv_provider_t provider)
{
    for(auto db : provider->databases) {
        delete db.second;
    }
    provider->databases.clear();
    provider->name2id.clear();
    provider->id2name.clear();

274
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
275 276 277 278 279 280 281
}

extern "C" int sdskv_provider_count_databases(
        sdskv_provider_t provider,
        uint64_t* num_db)
{
    *num_db = provider->databases.size();
282
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
283 284 285 286 287 288 289 290 291 292 293
}

extern "C" int sdskv_provider_list_databases(
        sdskv_provider_t provider,
        sdskv_database_id_t* targets)
{
    unsigned i = 0;
    for(auto p : provider->name2id) {
        targets[i] = p.second;
        i++;
    }
294
    return SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
295 296 297 298 299 300 301 302 303 304 305 306
}

static void sdskv_put_ult(hg_handle_t handle)
{
    hg_return_t hret;
    put_in_t in;
    put_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
307
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
308 309
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
310
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
311 312 313 314 315 316 317
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
    
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
318
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
319 320 321 322 323 324 325
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
326
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
327 328 329 330 331 332
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

333 334
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
    ds_bulk_t vdata(in.value.data, in.value.data+in.value.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
335 336

    if(it->second->put(kdata, vdata)) {
337
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
338
    } else {
339
        out.ret = SDSKV_ERR_PUT;
Matthieu Dorier's avatar
Matthieu Dorier committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);
}
DEFINE_MARGO_RPC_HANDLER(sdskv_put_ult)

static void sdskv_length_ult(hg_handle_t handle)
{

    hg_return_t hret;
    length_in_t in;
    length_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
359
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
360 361
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
362
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
363 364 365 366 367 368 369
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
370
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
371 372 373 374 375 376 377
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
378
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
379 380 381 382 383 384
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
385
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
386 387 388 389

    ds_bulk_t vdata;
    if(it->second->get(kdata, vdata)) {
        out.size = vdata.size();
390
        out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
391 392
    } else {
        out.size = 0;
393
        out.ret  = SDSKV_ERR_UNKNOWN_KEY;
Matthieu Dorier's avatar
Matthieu Dorier committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_length_ult)

static void sdskv_get_ult(hg_handle_t handle)
{

    hg_return_t hret;
    get_in_t in;
    get_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
415
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
416 417
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
418
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
419 420 421 422 423 424 425
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
426
        out.ret = SDSKV_ERR_MERCURY;
427 428
        out.value.data = nullptr;
        out.value.size = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
429 430 431 432 433 434 435
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
436
        out.ret = SDSKV_ERR_UNKNOWN_DB;
437 438
        out.value.data = nullptr;
        out.value.size = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
439 440 441 442 443 444
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
445
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
446 447 448 449

    ds_bulk_t vdata;
    if(it->second->get(kdata, vdata)) {
        if(vdata.size() <= in.vsize) {
450 451
            out.value.size = vdata.size();
            out.value.data = vdata.data();
452
            out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
453
        } else {
454 455
            out.value.size = 0;
            out.value.data = nullptr;
456
            out.ret = SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
457 458
        }
    } else {
459 460
        out.value.size = 0;
        out.value.data = nullptr;
461
        out.ret   = SDSKV_ERR_UNKNOWN_KEY;
Matthieu Dorier's avatar
Matthieu Dorier committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_get_ult)

static void sdskv_open_ult(hg_handle_t handle)
{

    hg_return_t hret;
    open_in_t in;
    open_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
483
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
484 485
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
486
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
487 488 489 490 491 492 493
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
494
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
495 496 497 498 499 500 501
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->name2id.find(std::string(in.name));
    if(it == svr_ctx->name2id.end()) {
502
        out.ret = SDSKV_ERR_DB_NAME;
Matthieu Dorier's avatar
Matthieu Dorier committed
503 504 505 506 507 508 509
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

    out.db_id = it->second;
510
    out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_open_ult)

static void sdskv_bulk_put_ult(hg_handle_t handle)
{

    hg_return_t hret;
    bulk_put_in_t in;
    bulk_put_out_t out;
    hg_bulk_t bulk_handle;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
531
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
532 533
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
534
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
535 536 537 538 539 540 541
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
542
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
543 544 545 546 547
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

548
    auto it = svr_ctx->databases.find(in.db_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
549
    if(it == svr_ctx->databases.end()) {
550
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
551 552 553 554 555
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
556

557
    ds_bulk_t vdata(in.vsize);
Matthieu Dorier's avatar
Matthieu Dorier committed
558

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    if(in.vsize > 0) {

        void *buffer = (void*)vdata.data();
        hg_size_t size = vdata.size();
        hret = margo_bulk_create(mid, 1, (void**)&buffer, &size,
                HG_BULK_WRITE_ONLY, &bulk_handle);
        if(hret != HG_SUCCESS) {
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_destroy(handle);
            return;
        }

        hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.handle, 0,
                bulk_handle, 0, vdata.size());
        if(hret != HG_SUCCESS) {
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_bulk_free(bulk_handle);
            margo_destroy(handle);
            return;
        }

Matthieu Dorier's avatar
Matthieu Dorier committed
584
        margo_bulk_free(bulk_handle);
585

Matthieu Dorier's avatar
Matthieu Dorier committed
586 587
    }

588
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
589 590 591 592

    auto b = it->second->put(kdata, vdata);

    if(b) {
593
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
594
    } else {
595
        out.ret = SDSKV_ERR_PUT;
Matthieu Dorier's avatar
Matthieu Dorier committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_bulk_put_ult)

static void sdskv_bulk_get_ult(hg_handle_t handle)
{

    hg_return_t hret;
    bulk_get_in_t in;
    bulk_get_out_t out;
    hg_bulk_t bulk_handle;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
618
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
619 620
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
621
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
622 623 624 625 626 627 628
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
629
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
630 631 632 633 634
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

635
    auto it = svr_ctx->databases.find(in.db_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
636
    if(it == svr_ctx->databases.end()) {
637
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
638 639 640 641 642 643
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
644
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
645 646 647 648

    ds_bulk_t vdata;
    auto b = it->second->get(kdata, vdata);

649
    if(!b || vdata.size() > in.vsize) {
Matthieu Dorier's avatar
Matthieu Dorier committed
650
        out.size = 0;
651
        out.ret = SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
652 653 654 655 656 657 658 659
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }

    void *buffer = (void*)vdata.data();
    hg_size_t size = vdata.size();
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    if(size > 0) {
        hret = margo_bulk_create(mid, 1, (void**)&buffer, &size,
                HG_BULK_READ_ONLY, &bulk_handle);
        if(hret != HG_SUCCESS) {
            out.size = 0;
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_destroy(handle);
            return;
        }

        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, info->addr, in.handle, 0,
                bulk_handle, 0, vdata.size());
        if(hret != HG_SUCCESS) {
            out.size = 0;
            out.ret = SDSKV_ERR_MERCURY;
            margo_respond(handle, &out);
            margo_free_input(handle, &in);
            margo_bulk_free(bulk_handle);
            margo_destroy(handle);
            return;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
683 684 685 686

        margo_bulk_free(bulk_handle);
    }

687
    out.size = size;
688
    out.ret  = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
689 690 691 692 693 694 695 696 697

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle);

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_bulk_get_ult)

Matthieu Dorier's avatar
Matthieu Dorier committed
698 699 700 701 702 703 704 705 706 707 708
static void sdskv_erase_ult(hg_handle_t handle)
{

    hg_return_t hret;
    erase_in_t in;
    erase_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
709
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
710 711
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
712
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
713 714 715 716 717 718 719
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
720
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
721 722 723 724 725 726 727
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
728
        out.ret = SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
729 730 731 732 733 734
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
735
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);
Matthieu Dorier's avatar
Matthieu Dorier committed
736 737

    if(it->second->erase(kdata)) {
738
        out.ret   = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
739
    } else {
740
        out.ret   = SDSKV_ERR_ERASE;
Matthieu Dorier's avatar
Matthieu Dorier committed
741 742 743 744 745 746 747 748 749 750
    }

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_erase_ult)

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
static void sdskv_exists_ult(hg_handle_t handle)
{

    hg_return_t hret;
    exists_in_t in;
    exists_out_t out;

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!svr_ctx) {
        fprintf(stderr, "Error: SDSKV could not find provider\n"); 
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    auto it = svr_ctx->databases.find(in.db_id);
    if(it == svr_ctx->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        margo_respond(handle, &out);
        margo_free_input(handle, &in);
        margo_destroy(handle);
        return;
    }
    
    ds_bulk_t kdata(in.key.data, in.key.data+in.key.size);

    out.flag = it->second->exists(kdata) ? 1 : 0;
    out.ret  = SDSKV_SUCCESS;

    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 
    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_exists_ult)

800
static void sdskv_list_keys_ult(hg_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
801 802 803
{

    hg_return_t hret;
804 805
    list_keys_in_t in;
    list_keys_out_t out;
Matthieu Dorier's avatar
Matthieu Dorier committed
806 807
    hg_bulk_t ksizes_local_bulk = HG_BULK_NULL;
    hg_bulk_t keys_local_bulk   = HG_BULK_NULL;
Matthieu Dorier's avatar
Matthieu Dorier committed
808

809
    out.ret     = SDSKV_SUCCESS;
810 811
    out.nkeys   = 0;

Matthieu Dorier's avatar
Matthieu Dorier committed
812
    /* get the provider handling this request */
Matthieu Dorier's avatar
Matthieu Dorier committed
813 814 815 816
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
817
        (sdskv_provider_t)margo_registered_data(mid, info->id);
Matthieu Dorier's avatar
Matthieu Dorier committed
818
    if(!svr_ctx) {
819 820
        std::cerr << "Error: SDSKV list_keys could not find provider" << std::endl;
        out.ret = SDSKV_ERR_UNKNOWN_PR;
Matthieu Dorier's avatar
Matthieu Dorier committed
821 822 823 824 825
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
826
    /* get the input */
Matthieu Dorier's avatar
Matthieu Dorier committed
827 828
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
829
        std::cerr << "Error: SDSKV list_keys could not get RPC input" << std::endl;
830
        out.ret = SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
831 832 833 834 835
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
836 837 838 839 840 841
    try {

        /* find the database targeted */
        auto it = svr_ctx->databases.find(in.db_id);
        if(it == svr_ctx->databases.end()) {
            std::cerr << "Error: SDSKV list_keys could not get database with id " << in.db_id << std::endl;
842
            throw SDSKV_ERR_UNKNOWN_DB;
Matthieu Dorier's avatar
Matthieu Dorier committed
843 844 845 846 847 848 849 850 851 852 853 854
        }
        auto db = it->second;

        /* create a bulk handle to receive and send key sizes from client */
        std::vector<hg_size_t> ksizes(in.max_keys);
        std::vector<void*> ksizes_addr(1);
        ksizes_addr[0] = (void*)ksizes.data();
        hg_size_t ksizes_bulk_size = ksizes.size()*sizeof(hg_size_t);
        hret = margo_bulk_create(mid, 1, ksizes_addr.data(), 
                &ksizes_bulk_size, HG_BULK_READWRITE, &ksizes_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not create bulk handle (ksizes_local_bulk)" << std::endl;
855
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
856 857
        }

858

Matthieu Dorier's avatar
Matthieu Dorier committed
859 860 861 862 863 864 865
        /* receive the key sizes from the client */
        hg_addr_t origin_addr = info->addr;
        hret = margo_bulk_transfer(mid, HG_BULK_PULL, origin_addr,
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not issue bulk transfer " 
                << "(pull from in.ksizes_bulk_handle to ksizes_local_bulk)" << std::endl;
866
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
867 868
        }

869 870 871
        /* make a copy of the remote key sizes */
        std::vector<hg_size_t> remote_ksizes(ksizes.begin(), ksizes.end());

Matthieu Dorier's avatar
Matthieu Dorier committed
872
        /* get the keys from the underlying database */    
873
        ds_bulk_t start_kdata(in.start_key.data, in.start_key.data+in.start_key.size);
874 875
        ds_bulk_t prefix(in.prefix.data, in.prefix.data+in.prefix.size);
        auto keys = db->list_keys(start_kdata, in.max_keys, prefix);
Matthieu Dorier's avatar
Matthieu Dorier committed
876 877
        hg_size_t num_keys = std::min(keys.size(), in.max_keys);

878 879
        if(num_keys == 0) throw SDSKV_SUCCESS;

Matthieu Dorier's avatar
Matthieu Dorier committed
880 881 882 883 884 885 886
        /* create the array of actual sizes */
        std::vector<hg_size_t> true_ksizes(num_keys);
        hg_size_t keys_bulk_size = 0;
        for(unsigned i = 0; i < num_keys; i++) {
            true_ksizes[i] = keys[i].size();
            if(true_ksizes[i] > ksizes[i]) {
                // this key has a size that exceeds the allocated size on client
887
                throw SDSKV_ERR_SIZE;
Matthieu Dorier's avatar
Matthieu Dorier committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
            } else {
                ksizes[i] = true_ksizes[i];
                keys_bulk_size += ksizes[i];
            }
        }

        /* create an array of addresses pointing to keys */
        std::vector<void*> keys_addr(num_keys);
        for(unsigned i=0; i < num_keys; i++) {
            keys_addr[i] = (void*)(keys[i].data());
        }

        /* expose the keys for bulk transfer */
        hret = margo_bulk_create(mid, num_keys, keys_addr.data(),
                true_ksizes.data(), HG_BULK_READ_ONLY, &keys_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not create bulk handle (keys_local_bulk)" << std::endl;
905
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
906 907 908 909 910 911 912 913
        }

        /* transfer the ksizes back to the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr, 
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keys could not issue bulk transfer "
                << "(push from ksizes_local_bulk to in.ksizes_bulk_handle)" << std::endl;
914
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
915 916 917
        }

        /* transfer the keys to the client */
918 919 920 921
        uint64_t remote_offset = 0;
        uint64_t local_offset  = 0;
        for(unsigned i = 0; i < num_keys; i++) {

922 923 924 925 926 927 928
            if(true_ksizes[i] > 0) {
                hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr,
                        in.keys_bulk_handle, remote_offset, keys_local_bulk, local_offset, true_ksizes[i]);
                if(hret != HG_SUCCESS) {
                    std::cerr << "Error: SDSKV list_keys could not issue bulk transfer (keys_local_bulk)" << std::endl;
                    throw SDSKV_ERR_MERCURY;
                }
929 930 931 932 933 934 935
            }

            remote_offset += remote_ksizes[i];
            local_offset  += true_ksizes[i];
        }

        out.nkeys = num_keys;
936
        out.ret = SDSKV_SUCCESS;
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962

    } catch(int exc_no) {
        out.ret = exc_no;
    }

    margo_bulk_free(ksizes_local_bulk);
    margo_bulk_free(keys_local_bulk);
    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
DEFINE_MARGO_RPC_HANDLER(sdskv_list_keys_ult)

static void sdskv_list_keyvals_ult(hg_handle_t handle)
{

    hg_return_t hret;
    list_keyvals_in_t in;
    list_keyvals_out_t out;
    hg_bulk_t ksizes_local_bulk = HG_BULK_NULL;
    hg_bulk_t keys_local_bulk   = HG_BULK_NULL;
    hg_bulk_t vsizes_local_bulk = HG_BULK_NULL;
    hg_bulk_t vals_local_bulk   = HG_BULK_NULL;

963
    out.ret     = SDSKV_SUCCESS;
964 965 966 967 968 969 970
    out.nkeys   = 0;

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t svr_ctx = 
971
        (sdskv_provider_t)margo_registered_data(mid, info->id);
972
    if(!svr_ctx) {
973 974
        std::cerr << "Error: SDSKV list_keyvals could not find provider" << std::endl;
        out.ret = SDSKV_ERR_UNKNOWN_PR;
975 976 977 978 979 980 981 982 983
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        std::cerr << "Error: SDSKV list_keyvals could not get RPC input" << std::endl;
984
        out.ret = SDSKV_ERR_MERCURY;
985 986 987 988 989 990 991 992 993 994 995
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    try {

        /* find the database targeted */
        auto it = svr_ctx->databases.find(in.db_id);
        if(it == svr_ctx->databases.end()) {
            std::cerr << "Error: SDSKV list_keyvals could not get database with id " << in.db_id << std::endl;
996
            throw SDSKV_ERR_UNKNOWN_DB;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
        }
        auto db = it->second;

        /* create a bulk handle to receive and send key sizes from client */
        std::vector<hg_size_t> ksizes(in.max_keys);
        std::vector<void*> ksizes_addr(1);
        ksizes_addr[0] = (void*)ksizes.data();
        hg_size_t ksizes_bulk_size = ksizes.size()*sizeof(hg_size_t);
        hret = margo_bulk_create(mid, 1, ksizes_addr.data(), 
                &ksizes_bulk_size, HG_BULK_READWRITE, &ksizes_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (ksizes_local_bulk)" << std::endl;
1009
            throw SDSKV_ERR_MERCURY;
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
        }

        /* create a bulk handle to receive and send value sizes from client */
        std::vector<hg_size_t> vsizes(in.max_keys);
        std::vector<void*> vsizes_addr(1);
        vsizes_addr[0] = (void*)vsizes.data();
        hg_size_t vsizes_bulk_size = vsizes.size()*sizeof(hg_size_t);
        hret = margo_bulk_create(mid, 1, vsizes_addr.data(), 
                &vsizes_bulk_size, HG_BULK_READWRITE, &vsizes_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (vsizes_local_bulk)" << std::endl;
1021
            throw SDSKV_ERR_MERCURY;
1022 1023 1024 1025 1026 1027 1028 1029 1030
        }

        /* receive the key sizes from the client */
        hg_addr_t origin_addr = info->addr;
        hret = margo_bulk_transfer(mid, HG_BULK_PULL, origin_addr,
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer " 
                << "(pull from in.ksizes_bulk_handle to ksizes_local_bulk)" << std::endl;
1031
            throw SDSKV_ERR_MERCURY;
1032 1033 1034 1035 1036
        }

        /* receive the values sizes from the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PULL, origin_addr,
                in.vsizes_bulk_handle, 0, vsizes_local_bulk, 0, vsizes_bulk_size);
Matthieu Dorier's avatar
Matthieu Dorier committed
1037
        if(hret != HG_SUCCESS) {
1038 1039
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer " 
                << "(pull from in.vsizes_bulk_handle to vsizes_local_bulk)" << std::endl;
1040
            throw SDSKV_ERR_MERCURY;
Matthieu Dorier's avatar
Matthieu Dorier committed
1041 1042
        }

1043 1044 1045 1046 1047
        /* make a copy of the remote key sizes and value sizes */
        std::vector<hg_size_t> remote_ksizes(ksizes.begin(), ksizes.end());
        std::vector<hg_size_t> remote_vsizes(vsizes.begin(), vsizes.end());

        /* get the keys and values from the underlying database */    
1048
        ds_bulk_t start_kdata(in.start_key.data, in.start_key.data+in.start_key.size);
1049 1050
        ds_bulk_t prefix(in.prefix.data, in.prefix.data+in.prefix.size);
        auto keyvals = db->list_keyvals(start_kdata, in.max_keys, prefix);
1051 1052
        hg_size_t num_keys = std::min(keyvals.size(), in.max_keys);

1053 1054
        if(num_keys == 0) throw SDSKV_SUCCESS;

1055 1056 1057 1058 1059 1060 1061
        /* create the array of actual key sizes */
        std::vector<hg_size_t> true_ksizes(num_keys);
        hg_size_t keys_bulk_size = 0;
        for(unsigned i = 0; i < num_keys; i++) {
            true_ksizes[i] = keyvals[i].first.size();
            if(true_ksizes[i] > ksizes[i]) {
                // this key has a size that exceeds the allocated size on client
1062
                throw SDSKV_ERR_SIZE;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
            } else {
                ksizes[i] = true_ksizes[i];
                keys_bulk_size += ksizes[i];
            }
        }

        /* create the array of actual value sizes */
        std::vector<hg_size_t> true_vsizes(num_keys);
        hg_size_t vals_bulk_size = 0;
        for(unsigned i = 0; i < num_keys; i++) {
            true_vsizes[i] = keyvals[i].second.size();
            if(true_vsizes[i] > vsizes[i]) {
                // this value has a size that exceeds the allocated size on client
1076
                throw SDSKV_ERR_SIZE;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
            } else {
                vsizes[i] = true_vsizes[i];
                vals_bulk_size += vsizes[i];
            }
        }

        /* create an array of addresses pointing to keys */
        std::vector<void*> keys_addr(num_keys);
        for(unsigned i=0; i < num_keys; i++) {
            keys_addr[i] = (void*)(keyvals[i].first.data());
        }

        /* create an array of addresses pointing to values */
        std::vector<void*> vals_addr(num_keys);
        for(unsigned i=0; i < num_keys; i++) {
            vals_addr[i] = (void*)(keyvals[i].second.data());
        }

        /* expose the keys for bulk transfer */
        hret = margo_bulk_create(mid, num_keys, keys_addr.data(),
                true_ksizes.data(), HG_BULK_READ_ONLY, &keys_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (keys_local_bulk)" << std::endl;
1100
            throw SDSKV_ERR_MERCURY;
1101 1102 1103 1104 1105 1106 1107
        }

        /* expose the values for bulk transfer */
        hret = margo_bulk_create(mid, num_keys, vals_addr.data(),
                true_vsizes.data(), HG_BULK_READ_ONLY, &vals_local_bulk);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not create bulk handle (vals_local_bulk)" << std::endl;
1108
            throw SDSKV_ERR_MERCURY;
1109 1110 1111 1112 1113 1114 1115 1116
        }

        /* transfer the ksizes back to the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr, 
                in.ksizes_bulk_handle, 0, ksizes_local_bulk, 0, ksizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer "
                << "(push from ksizes_local_bulk to in.ksizes_bulk_handle)" << std::endl;
1117
            throw SDSKV_ERR_MERCURY;
1118 1119 1120 1121 1122 1123 1124 1125
        }

        /* transfer the vsizes back to the client */
        hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr, 
                in.vsizes_bulk_handle, 0, vsizes_local_bulk, 0, vsizes_bulk_size);
        if(hret != HG_SUCCESS) {
            std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer "
                << "(push from vsizes_local_bulk to in.vsizes_bulk_handle)" << std::endl;
1126
            throw SDSKV_ERR_MERCURY;
1127 1128 1129 1130 1131 1132 1133
        }

        uint64_t remote_offset = 0;
        uint64_t local_offset  = 0;

        /* transfer the keys to the client */
        for(unsigned i=0; i < num_keys; i++) {
1134 1135 1136 1137 1138 1139 1140
            if(true_ksizes[i] > 0) {
                hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr,
                        in.keys_bulk_handle, remote_offset, keys_local_bulk, local_offset, true_ksizes[i]);
                if(hret != HG_SUCCESS) {
                    std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer (keys_local_bulk)" << std::endl;
                    throw SDSKV_ERR_MERCURY;
                }
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
            }
            remote_offset += remote_ksizes[i];
            local_offset  += true_ksizes[i];
        }

        remote_offset = 0;
        local_offset  = 0;

        /* transfer the values to the client */
        for(unsigned i=0; i < num_keys; i++) {
1151 1152 1153 1154 1155 1156 1157
            if(true_vsizes[i] > 0) {
                hret = margo_bulk_transfer(mid, HG_BULK_PUSH, origin_addr,
                        in.vals_bulk_handle, remote_offset, vals_local_bulk, local_offset, true_vsizes[i]);
                if(hret != HG_SUCCESS) {
                    std::cerr << "Error: SDSKV list_keyvals could not issue bulk transfer (vals_local_bulk)" << std::endl;
                    throw SDSKV_ERR_MERCURY;
                }
1158 1159 1160 1161 1162
            }
            remote_offset += remote_vsizes[i];
            local_offset  += true_vsizes[i];
        }

Matthieu Dorier's avatar
Matthieu Dorier committed
1163
        out.nkeys = num_keys;
1164
        out.ret = SDSKV_SUCCESS;
Matthieu Dorier's avatar
Matthieu Dorier committed
1165 1166 1167

    } catch(int exc_no) {
        out.ret = exc_no;
Matthieu Dorier's avatar
Matthieu Dorier committed
1168 1169
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
1170 1171
    margo_bulk_free(ksizes_local_bulk);
    margo_bulk_free(keys_local_bulk);
1172 1173
    margo_bulk_free(vsizes_local_bulk);
    margo_bulk_free(vals_local_bulk);
Matthieu Dorier's avatar
Matthieu Dorier committed
1174 1175 1176 1177 1178 1179
    margo_respond(handle, &out);
    margo_free_input(handle, &in);
    margo_destroy(handle); 

    return;
}
1180
DEFINE_MARGO_RPC_HANDLER(sdskv_list_keyvals_ult)
Matthieu Dorier's avatar
Matthieu Dorier committed
1181

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
static void sdskv_migrate_keys_ult(hg_handle_t handle)
{
    hg_return_t hret;
    migrate_keys_in_t in;
    migrate_keys_out_t out;
    out.ret = SDSKV_SUCCESS;

    auto r0 = at_exit([&handle]() { margo_destroy(handle); });
    auto r1 = at_exit([&handle,&out]() { margo_respond(handle, &out); });

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t provider = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!provider) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }
    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r2 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });
    /* find the source database */
    auto it = provider->databases.find(in.source_db_id);
    if(it == provider->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }
    auto database = it->second;
    /* lookup the address of the target provider */
    hg_addr_t target_addr = HG_ADDR_NULL;
    hret = margo_addr_lookup(mid, in.target_addr, &target_addr);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* create the bulk buffer to receive the keys */
    char *buffer = (char*)malloc(in.bulk_size);
    auto r4 = at_exit([&buffer]() { free(buffer); });
    hg_size_t bulk_size = in.bulk_size;
    hg_bulk_t bulk_handle = HG_BULK_NULL;
    hret = margo_bulk_create(mid, 1, (void**)&buffer, &bulk_size,
            HG_BULK_WRITE_ONLY, &bulk_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r5 = at_exit([&bulk_handle]() { margo_bulk_free(bulk_handle); });

    /* issue a bulk pull */
    hret = margo_bulk_transfer(mid, HG_BULK_PULL, info->addr, in.keys_bulk, 0,
            bulk_handle, 0, in.bulk_size);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }

    /* remap the keys from the void* buffer */
    hg_size_t* seg_sizes = (hg_size_t*)buffer;
    char* packed_keys = buffer + in.num_keys*sizeof(hg_size_t);

    /* create a handle for a "put" RPC */
    hg_handle_t put_handle;
    hret = margo_create(mid, target_addr, provider->sdskv_put_id, &put_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    auto r6 = at_exit([&put_handle]() { margo_destroy(put_handle); });
    /* iterate over the keys */
    size_t offset = 0;
    put_in_t put_in;
    put_out_t put_out;
    for(unsigned i=0; i < in.num_keys; i++) {
        /* find the key */
        char* key = packed_keys + offset;
        size_t size = seg_sizes[i]; 
        offset += size;

        ds_bulk_t kdata(key, key+size);
        ds_bulk_t vdata;
        auto b = database->get(kdata, vdata);
        if(!b) continue;

        /* issue a "put" for that key */
        put_in.db_id      = in.target_db_id;
        put_in.key.data   = (kv_ptr_t)kdata.data();
        put_in.key.size   = kdata.size();
        put_in.value.data = (kv_ptr_t)vdata.data();
        put_in.value.size = vdata.size();
        /* forward put call */
        hret = margo_provider_forward(in.target_provider_id, put_handle, &put_in);
        if(hret != HG_SUCCESS) {
            out.ret = SDSKV_ERR_MIGRATION;
            return;
        }
        /* get output of the put call */
        hret = margo_get_output(put_handle, &put_out);
        if(hret != HG_SUCCESS || put_out.ret != SDSKV_SUCCESS) {
            out.ret = SDSKV_ERR_MIGRATION;
            return;
        }
        margo_free_output(put_handle, &out);
        /* remove the key if needed */
        if(in.flag == SDSKV_REMOVE_ORIGINAL) {
            database->erase(kdata);
        }
    }
}
DEFINE_MARGO_RPC_HANDLER(sdskv_migrate_keys_ult)

static void sdskv_migrate_key_range_ult(hg_handle_t handle)
{
    hg_return_t hret;
    migrate_key_range_in_t in;
    migrate_keys_out_t out;

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t provider = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!provider) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    // TODO implement this operation
    out.ret = SDSKV_OP_NOT_IMPL;
    margo_respond(handle, &out);

    margo_free_input(handle, &in);
    margo_destroy(handle);
}
DEFINE_MARGO_RPC_HANDLER(sdskv_migrate_key_range_ult)

static void sdskv_migrate_keys_prefixed_ult(hg_handle_t handle)
{
    hg_return_t hret;
    migrate_keys_prefixed_in_t in;
    migrate_keys_out_t out;

    /* need to destroy the handle at exit */
    auto r0 = at_exit([&handle]() { margo_destroy(handle); });
    /* need to respond at exit */
    auto r1 = at_exit([&handle,&out]() { margo_respond(handle, &out); });

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t provider = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!provider) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }
    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    /* need to destroy the input at exit */
    auto r2 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });
    /* find the source database */
    auto it = provider->databases.find(in.source_db_id);
    if(it == provider->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }
    auto database = it->second;
    /* lookup the address of the target provider */
    hg_addr_t target_addr = HG_ADDR_NULL;
    hret = margo_addr_lookup(mid, in.target_addr, &target_addr);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    /* need to destroy the address at exit */
    auto r3 = at_exit([&mid,&target_addr]() { margo_addr_free(mid, target_addr); });
    /* create a handle for a "put" RPC */
    hg_handle_t put_handle;
    hret = margo_create(mid, target_addr, provider->sdskv_put_id, &put_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    /* need to destroy the handle at exist */
    auto r4 = at_exit([&put_handle]() { margo_destroy(put_handle); });
    /* iterate over the keys by packets of 64 */
    /* XXX make this number configurable */
    std::vector<std::pair<ds_bulk_t,ds_bulk_t>> batch;
    ds_bulk_t start_key;
    ds_bulk_t prefix(in.key_prefix.data, in.key_prefix.data + in.key_prefix.size);
    do {
        try {
            batch = database->list_keyvals(start_key, 64, prefix);
        } catch(int err) {
            out.ret = err;
            return;
        }
        if(batch.size() == 0) 
            break;
        /* issue a put for all the keys in this batch */
        put_in_t put_in;
        put_out_t put_out;
        for(auto& kv : batch) {
            put_in.db_id  = in.target_db_id;
            put_in.key.data   = (kv_ptr_t)kv.first.data();
            put_in.key.size   = kv.first.size();
            put_in.value.data = (kv_ptr_t)kv.second.data();
            put_in.value.size = kv.second.size();
            /* forward put call */
            hret = margo_provider_forward(in.target_provider_id, put_handle, &put_in);
            if(hret != HG_SUCCESS) {
                out.ret = SDSKV_ERR_MIGRATION;
                return;
            }
            /* get output of the put call */
            hret = margo_get_output(put_handle, &put_out);
            if(hret != HG_SUCCESS || put_out.ret != SDSKV_SUCCESS) {
                out.ret = SDSKV_ERR_MIGRATION;
                return;
            }
            margo_free_output(put_handle, &out);
            /* remove the key if needed */
            if(in.flag == SDSKV_REMOVE_ORIGINAL) {
                database->erase(kv.first);
            }
        }
        /* if original is removed, start_key can stay empty since we
           keep taking the beginning of the container, otherwise
           we need to update start_key. */
        if(in.flag != SDSKV_REMOVE_ORIGINAL) {
            start_key = std::move(batch.rbegin()->first);
        }
    } while(batch.size() == 64);
}
DEFINE_MARGO_RPC_HANDLER(sdskv_migrate_keys_prefixed_ult)

static void sdskv_migrate_all_keys_ult(hg_handle_t handle)
{
    hg_return_t hret;
    migrate_all_keys_in_t in;
    migrate_keys_out_t out;
    
    /* need to destroy the handle at exit */
    auto r0 = at_exit([&handle]() { margo_destroy(handle); });
    /* need to respond at exit */
    auto r1 = at_exit([&handle,&out]() { margo_respond(handle, &out); });

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t provider = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!provider) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        return;
    }
    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    /* need to destroy the input at exit */
    auto r2 = at_exit([&handle,&in]() { margo_free_input(handle, &in); });
    /* find the source database */
    auto it = provider->databases.find(in.source_db_id);
    if(it == provider->databases.end()) {
        out.ret = SDSKV_ERR_UNKNOWN_DB;
        return;
    }
    auto database = it->second;
    /* lookup the address of the target provider */
    hg_addr_t target_addr = HG_ADDR_NULL;
    hret = margo_addr_lookup(mid, in.target_addr, &target_addr);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    /* need to destroy the address at exit */
    auto r3 = at_exit([&mid,&target_addr]() { margo_addr_free(mid, target_addr); });
    /* create a handle for a "put" RPC */
    hg_handle_t put_handle;
    hret = margo_create(mid, target_addr, provider->sdskv_put_id, &put_handle);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        return;
    }
    /* need to destroy the handle at exist */
    auto r4 = at_exit([&put_handle]() { margo_destroy(put_handle); });
    /* iterate over the keys by packets of 64 */
    /* XXX make this number configurable */
    std::vector<std::pair<ds_bulk_t,ds_bulk_t>> batch;
    ds_bulk_t start_key;
    do {
        try {
            batch = database->list_keyvals(start_key, 64);
        } catch(int err) {
            out.ret = err;
            return;
        }
        if(batch.size() == 0) 
            break;
        /* issue a put for all the keys in this batch */
        put_in_t put_in;
        put_out_t put_out;
        for(auto& kv : batch) {
            put_in.db_id  = in.target_db_id;
            put_in.key.data   = (kv_ptr_t)kv.first.data();
            put_in.key.size   = kv.first.size();
            put_in.value.data = (kv_ptr_t)kv.second.data();
            put_in.value.size = kv.second.size();
            /* forward put call */
            hret = margo_provider_forward(in.target_provider_id, put_handle, &put_in);
            if(hret != HG_SUCCESS) {
                out.ret = SDSKV_ERR_MIGRATION;
                return;
            }
            /* get output of the put call */
            hret = margo_get_output(put_handle, &put_out);
            if(hret != HG_SUCCESS || put_out.ret != SDSKV_SUCCESS) {
                out.ret = SDSKV_ERR_MIGRATION;
                return;
            }
            margo_free_output(put_handle, &out);
            /* remove the key if needed */
            if(in.flag == SDSKV_REMOVE_ORIGINAL) {
                database->erase(kv.first);
            }
        }
        /* if original is removed, start_key can stay empty since we
           keep taking the beginning of the container, otherwise
           we need to update start_key. */
        if(in.flag != SDSKV_REMOVE_ORIGINAL) {
            start_key = std::move(batch.rbegin()->first);
        }
    } while(batch.size() == 64);

}
DEFINE_MARGO_RPC_HANDLER(sdskv_migrate_all_keys_ult)

static void sdskv_migrate_database_ult(hg_handle_t handle)
{
    hg_return_t hret;
    migrate_database_in_t in;
    migrate_database_out_t out;

    /* get the provider handling this request */
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    const struct hg_info* info = margo_get_info(handle);
    sdskv_provider_t provider = 
        (sdskv_provider_t)margo_registered_data(mid, info->id);
    if(!provider) {
        out.ret = SDSKV_ERR_UNKNOWN_PR;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
    /* get the input */
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS) {
        out.ret = SDSKV_ERR_MERCURY;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
    
    // TODO implement this operation
    out.ret = SDSKV_OP_NOT_IMPL;
    out.db_id = 0;
    margo_respond(handle, &out);

    margo_free_input(handle, &in);
    margo_destroy(handle);
}
DEFINE_MARGO_RPC_HANDLER(sdskv_migrate_database_ult)

#if 0
static void __migrate_keys_ult(hg_handle_t handle)
{
    // TODO
}
DEFINE_MARGO_RPC_HANDLER(__migrate_keys_ult)

static void __migrate_key_range_ult(hg_handle_t handle)
{
    // TODO
}
DEFINE_MARGO_RPC_HANDLER(__migrate_key_range_ult)

static void __migrate_keys_prefixed_ult(hg_handle_t handle)
{
    // TODO
}
DEFINE_MARGO_RPC_HANDLER(__migrate_keys_prefixed_ult)

static void __migrate_all_keys_ult(hg_handle_t handle)
{
    // TODO
}
DEFINE_MARGO_RPC_HANDLER(__migrate_all_keys_ult)

static void __migrate_database_ult(hg_handle_t handle)
{
    // TODO
}
DEFINE_MARGO_RPC_HANDLER(__migrate_database_ult)
#endif

Matthieu Dorier's avatar
Matthieu Dorier committed
1609 1610 1611 1612 1613 1614 1615
static void sdskv_server_finalize_cb(void *data)
{
    sdskv_provider_t svr_ctx = (sdskv_provider_t)data;
    assert(svr_ctx);

    sdskv_provider_remove_all_databases(svr_ctx);

Matthieu Dorier's avatar
Matthieu Dorier committed
1616
    delete svr_ctx;
Matthieu Dorier's avatar
Matthieu Dorier committed
1617 1618 1619 1620

    return;
}