sdskv-client.c 15 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include "sdskv-client.h"
#include "sdskv-rpc-types.h"

#define MAX_RPC_MESSAGE_SIZE 4000 // in bytes

struct sdskv_client {
    margo_instance_id mid;

    hg_id_t sdskv_put_id;
    hg_id_t sdskv_bulk_put_id;
    hg_id_t sdskv_get_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_id;

    uint64_t num_provider_handles;
};

struct sdskv_provider_handle {
    sdskv_client_t client;
    hg_addr_t      addr;
    uint8_t        mplex_id;
    uint64_t       refcount;
};

static int sdskv_client_register(sdskv_client_t client, margo_instance_id mid)
{
    client->mid = mid;

    /* check if RPCs have already been registered */
    hg_bool_t flag;
    hg_id_t id;
    margo_registered_name(mid, "sdskv_put_rpc", &id, &flag);

    if(flag == HG_TRUE) { /* RPCs already registered */

        margo_registered_name(mid, "sdskv_put_rpc",      &client->sdskv_put_id,      &flag);
        margo_registered_name(mid, "sdskv_bulk_put_rpc", &client->sdskv_bulk_put_id, &flag);
        margo_registered_name(mid, "sdskv_get_rpc",      &client->sdskv_get_id,      &flag);
        margo_registered_name(mid, "sdskv_length_rpc",   &client->sdskv_length_id,   &flag);
        margo_registered_name(mid, "sdskv_bulk_get_rpc", &client->sdskv_bulk_get_id, &flag);
        margo_registered_name(mid, "sdskv_open_rpc",     &client->sdskv_open_id,     &flag);
        margo_registered_name(mid, "sdskv_list_rpc",     &client->sdskv_list_id,     &flag);

    } else {

        client->sdskv_put_id =
            MARGO_REGISTER(mid, "sdskv_put_rpc", put_in_t, put_out_t, NULL);
        client->sdskv_bulk_put_id =
            MARGO_REGISTER(mid, "sdskv_bulk_put_rpc", bulk_put_in_t, bulk_put_out_t, NULL);
        client->sdskv_get_id =
            MARGO_REGISTER(mid, "sdskv_get_rpc", get_in_t, get_out_t, NULL);
        client->sdskv_length_id =
            MARGO_REGISTER(mid, "sdskv_length_rpc", length_in_t, length_out_t, NULL);
        client->sdskv_bulk_get_id =
            MARGO_REGISTER(mid, "sdskv_bulk_get_rpc", bulk_get_in_t, bulk_get_out_t, NULL);
        client->sdskv_open_id =
            MARGO_REGISTER(mid, "sdskv_open_rpc", open_in_t, open_out_t, NULL);
        client->sdskv_list_id =
            MARGO_REGISTER(mid, "sdskv_list_rpc", list_in_t, list_out_t, NULL);
    }

    return 0;
}

int sdskv_client_init(margo_instance_id mid, sdskv_client_t* client)
{
    sdskv_client_t c = (sdskv_client_t)calloc(1, sizeof(*c));
    if(!c) return -1;

    c->num_provider_handles = 0;

    int ret = sdskv_client_register(c, mid);
    if(ret != 0) return ret;

    *client = c;
    return 0;
}

int sdskv_client_finalize(sdskv_client_t client)
{
    if(client->num_provider_handles != 0) {
        fprintf(stderr,
                "[SDSKV] Warning: %d provider handles not released before sdskv_client_finalize was called\n",
                client->num_provider_handles);
    }
    free(client);
    return 0;
}

int sdskv_provider_handle_create(
        sdskv_client_t client,
        hg_addr_t addr,
        uint8_t mplex_id,
        sdskv_provider_handle_t* handle)
{
    if(client == SDSKV_CLIENT_NULL) return -1;

    sdskv_provider_handle_t provider =
        (sdskv_provider_handle_t)calloc(1, sizeof(*provider));

    if(!provider) return -1;

    hg_return_t ret = margo_addr_dup(client->mid, addr, &(provider->addr));
    if(ret != HG_SUCCESS) {
        free(provider);
        return -1;
    }

    provider->client   = client;
    provider->mplex_id = mplex_id;
    provider->refcount = 1;

    client->num_provider_handles += 1;

    *handle = provider;
    return 0;
}

int sdskv_provider_handle_ref_incr(
        sdskv_provider_handle_t handle)
{
    if(handle == SDSKV_PROVIDER_HANDLE_NULL) return -1;
    handle->refcount += 1;
    return 0;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
129
int sdskv_provider_handle_release(sdskv_provider_handle_t handle)
Matthieu Dorier's avatar
Matthieu Dorier committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
{
    if(handle == SDSKV_PROVIDER_HANDLE_NULL) return -1;
    handle->refcount -= 1;
    if(handle->refcount == 0) {
        margo_addr_free(handle->client->mid, handle->addr);
        handle->client->num_provider_handles -= 1;
        free(handle);
    }
    return 0;
}

int sdskv_open(
        sdskv_provider_handle_t provider,
        const char* db_name,
        sdskv_database_id_t* db_id)
{
    hg_return_t hret;
    int ret;
    open_in_t in;
    open_out_t out;
    hg_handle_t handle;

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_open_id,
            &handle);
    if(hret != HG_SUCCESS) return -1;

    hret = margo_set_target_id(handle, provider->mplex_id);

    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
167 168
    in.name = (char*)db_name;

Matthieu Dorier's avatar
Matthieu Dorier committed
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    ret = out.ret;
    *db_id = out.db_id;

    margo_free_output(handle, &out);
    margo_destroy(handle);

    return ret;
}

int sdskv_put(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id,
        const void *key, hg_size_t ksize,
        const void *value, hg_size_t vsize)
{
    hg_return_t hret;
    int ret;
    hg_handle_t handle;

    hg_size_t msize = ksize + vsize + 2*sizeof(hg_size_t);

    if(msize <= MAX_RPC_MESSAGE_SIZE) {

        put_in_t in;
        put_out_t out;

        in.db_id = db_id;
        in.key   = (kv_data_t)key;
        in.ksize = ksize;
        in.value = (kv_data_t)value;
        in.vsize = vsize;

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_put_id,
                &handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
218 219 220 221
        if(hret != HG_SUCCESS) {
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
            return -1;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
222 223 224 225

        hret = margo_set_target_id(handle, provider->mplex_id);

        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
226
            fprintf(stderr,"[SDSKV] margo_set_target_id() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
227 228 229 230 231 232
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
233
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
234 235 236 237 238 239
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
240
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;

        margo_free_output(handle, &out);

    } else {

        bulk_put_in_t in;
        bulk_put_out_t out;

        in.bulk.db_id = db_id;
        in.bulk.key   = (kv_data_t)key;
        in.bulk.ksize = ksize;
        in.bulk.vsize = vsize;

        hret = margo_bulk_create(provider->client->mid, 1, (void**)(&value), &in.bulk.vsize,
                                HG_BULK_READ_ONLY, &in.bulk.handle);
Matthieu Dorier's avatar
Matthieu Dorier committed
261 262 263 264
        if(ret != HG_SUCCESS) {
            fprintf(stderr,"[SDSKV] margo_bulk_create() failed in sdskv_put()\n");
            return -1;
        }
Matthieu Dorier's avatar
Matthieu Dorier committed
265 266 267 268 269 270 271 272

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_bulk_put_id,
                &handle);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
273
            fprintf(stderr,"[SDSKV] margo_create() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
274 275 276 277 278 279 280
            margo_bulk_free(in.bulk.handle);
            return -1;
        }

        hret = margo_set_target_id(handle, provider->mplex_id);

        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
281
            fprintf(stderr,"[SDSKV] margo_set_target_id() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
282 283 284 285 286 287 288
            margo_bulk_free(in.bulk.handle);
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
289
            fprintf(stderr,"[SDSKV] margo_forward() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
290 291 292 293 294 295 296
            margo_bulk_free(in.bulk.handle);
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
Matthieu Dorier's avatar
Matthieu Dorier committed
297
            fprintf(stderr,"[SDSKV] margo_get_output() failed in sdskv_put()\n");
Matthieu Dorier's avatar
Matthieu Dorier committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
            margo_bulk_free(in.bulk.handle);
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;
        margo_free_output(handle, &out);
        margo_bulk_free(in.bulk.handle);
    }

    margo_destroy(handle);
    return ret;
}

int sdskv_get(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id, 
        const void *key, hg_size_t ksize,
        void *value, hg_size_t* vsize)
{
    hg_return_t hret;
    hg_size_t size;
    hg_size_t msize;
    int ret;
    hg_handle_t handle;

    size = *(hg_size_t*)vsize;
    msize = size + sizeof(hg_size_t) + sizeof(hg_return_t);

    if (msize <= MAX_RPC_MESSAGE_SIZE) {

        get_in_t in;
        get_out_t out;

        in.db_id = db_id;
        in.key = (kv_data_t)key;
        in.ksize = ksize;
        in.vsize = size;

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_get_id,
                &handle);
        if(hret != HG_SUCCESS) return -1;

        hret = margo_set_target_id(handle, provider->mplex_id);

        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;
        *vsize = (hg_size_t)out.vsize;

        if (out.vsize > 0) {
            memcpy(value, out.value, out.vsize);
        }

        margo_free_output(handle, &out);

    } else {

        bulk_get_in_t in;
        bulk_get_out_t out;

        in.bulk.db_id = db_id;
        in.bulk.key = (kv_data_t)key;
        in.bulk.ksize = ksize;
        in.bulk.vsize = size;

        hret = margo_bulk_create(provider->client->mid, 1, &value, &in.bulk.vsize,
                                HG_BULK_WRITE_ONLY, &in.bulk.handle);
        if(hret != HG_SUCCESS) return -1;

        /* create handle */
        hret = margo_create(
                provider->client->mid,
                provider->addr,
                provider->client->sdskv_bulk_get_id,
                &handle);
        if(hret != HG_SUCCESS) {
            margo_bulk_free(in.bulk.handle);
            return -1;
        }

        hret = margo_set_target_id(handle, provider->mplex_id);
        if(hret != HG_SUCCESS) {
            margo_bulk_free(in.bulk.handle);
            margo_destroy(handle);
            return -1;
        }

        hret = margo_forward(handle, &in);
        if(hret != HG_SUCCESS) {
            margo_bulk_free(in.bulk.handle);
            margo_destroy(handle);
            return -1;
        }

        hret = margo_get_output(handle, &out);
        if(hret != HG_SUCCESS) {
            margo_bulk_free(in.bulk.handle);
            margo_destroy(handle);
            return -1;
        }

        ret = out.ret;
        *vsize = (hg_size_t)out.size;

        margo_free_output(handle, &out);
        margo_bulk_free(in.bulk.handle);
    }

    margo_destroy(handle);
    return ret;
}

int sdskv_length(sdskv_provider_handle_t provider, 
        sdskv_database_id_t db_id, const void *key, 
        hg_size_t ksize, hg_size_t* vsize)
{
    hg_return_t hret;
    int ret;
    hg_handle_t handle;

    length_in_t in;
    length_out_t out;

    in.db_id = db_id;
    in.key   = (kv_data_t)key;
    in.ksize = ksize;

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_length_id,
            &handle);
    if(hret != HG_SUCCESS) return -1;

    hret = margo_set_target_id(handle, provider->mplex_id);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    ret = out.ret;
    if(ret == 0) *vsize = out.size;

    margo_free_output(handle, &out);
    margo_destroy(handle);
    return ret;
}

int sdskv_list_keys(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id,  // db instance
        const void *start_key,  // we want keys strictly after this start_key
        hg_size_t start_ksize,  // size of the start_key
        void **keys,            // pointer to an array of void* pointers,
        //     this array has size *max_keys
        hg_size_t* ksizes,      // pointer to an array of hg_size_t sizes
        //    representing sizes allocated in
        //     keys for each key
        hg_size_t* max_keys)   // maximum number of keys requested
{
    list_in_t  in;
    list_out_t out;
    hg_return_t hret = HG_SUCCESS;
    hg_handle_t handle;
    int ret;
    int i;

    in.db_id = db_id;
    in.start_key = (kv_data_t) start_key;
    in.start_ksize = start_ksize;
    in.max_keys = *max_keys;

    /* create handle */
    hret = margo_create(
            provider->client->mid,
            provider->addr,
            provider->client->sdskv_list_id,
            &handle);
    if(hret != HG_SUCCESS) return -1;

    hret = margo_set_target_id(handle, provider->mplex_id);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_forward(handle, &in);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    hret = margo_get_output(handle, &out);
    if(hret != HG_SUCCESS) {
        margo_destroy(handle);
        return -1;
    }

    *max_keys = out.nkeys;
    ret = out.ret;

    for (i=0; i < out.nkeys; i++) {
        ksizes[i] = out.ksizes[i];
        memcpy(keys[i], out.keys[i], out.ksizes[i]);
    }
    margo_free_output(handle, &out);

    margo_destroy(handle);

    return ret;
}

int sdskv_list_keys_with_prefix(sdskv_provider_handle_t provider,
        sdskv_database_id_t db_id,  // db instance
        const void *start_key,  // we want keys strictly after this start_key
        hg_size_t start_ksize,  // size of the start_key
        const void *prefix,     // return only keys that begin with 'prefix'
        hg_size_t prefix_size,
        void **keys,            // pointer to an array of void* pointers,
        // this array has size *max_keys
        hg_size_t* ksizes,      // pointer to an array of hg_size_t sizes 
        // representing sizes allocated in
        // keys for each key
        hg_size_t* max_keys)   // maximum number of keys requested
{
    // TODO
}

Matthieu Dorier's avatar
Matthieu Dorier committed
557 558 559 560
int sdskv_shutdown_service(sdskv_client_t client, hg_addr_t addr)
{
    return margo_shutdown_remote_instance(client->mid, addr);
}