core-read-op.cpp 18 KB
Newer Older
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6 7 8
#include <map>
#include <string>
#include <iostream>
9
#include <algorithm>
Matthieu Dorier's avatar
Matthieu Dorier committed
10
#include <vector>
11
#include <list>
Matthieu Dorier's avatar
Matthieu Dorier committed
12
#include <cinttypes>
13
#include <bake-client.h>
14 15 16 17 18
#include "src/server/core/core-read-op.h"
#include "src/server/visitor-args.h"
#include "src/io-chain/read-op-visitor.h"
#include "src/io-chain/read-resp-impl.h"
#include "src/omap-iter/omap-iter-impl.h"
19
#include "src/server/core/key-types.h"
20
#include "src/server/core/covermap.hpp"
21

22
static int tabs = 0;
23
/*
24 25 26
#define ENTERING {for(int i=0; i<tabs; i++) fprintf(stderr," "); fprintf(stderr,"[ENTERING]>> %s\n",__FUNCTION__); tabs += 1;}
#define LEAVING  {tabs -= 1; for(int i=0; i<tabs; i++) fprintf(stderr," "); fprintf(stderr,"[LEAVING]<<< %s\n",__FUNCTION__); }
#define ERROR    {for(int i=0; i<(tabs+1); i++) fprintf(stderr, " "); fprintf(stderr,"[ERROR] "); }
27 28 29 30
*/
#define ENTERING
#define LEAVING
#define ERROR
31

32 33 34 35 36 37 38 39
static void read_op_exec_begin(void*);
static void read_op_exec_stat(void*, uint64_t*, time_t*, int*);
static void read_op_exec_read(void*, uint64_t, size_t, buffer_u, size_t*, int*);
static void read_op_exec_omap_get_keys(void*, const char*, uint64_t, mobject_store_omap_iter_t*, int*);
static void read_op_exec_omap_get_vals(void*, const char*, const char*, uint64_t, mobject_store_omap_iter_t*, int*);
static void read_op_exec_omap_get_vals_by_keys(void*, char const* const*, size_t, mobject_store_omap_iter_t*, int*);
static void read_op_exec_end(void*);

40 41 42 43 44 45
/* defined in core-write-op.cpp */
extern uint64_t mobject_compute_object_size(
        sdskv_provider_handle_t ph,
        sdskv_database_id_t seg_db_id,
        oid_t oid, double ts);

46 47 48 49 50
static oid_t get_oid_from_name(
        sdskv_provider_handle_t ph,
        sdskv_database_id_t name_db_id,
        const char* name);

51 52 53 54 55 56 57
struct read_request_t {
    double timestamp;              // timestamp at which the segment was created
    uint64_t absolute_start_index; // start index within the object
    uint64_t absolute_end_index;   // end index within the object
    uint64_t region_start_index;   // where to start within the region
    uint64_t region_end_index;     // where to end within the region
    uint64_t client_offset;        // offset within the client's buffer
58
    bake_region_id_t region;  // region id
59 60
};

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static struct read_op_visitor read_op_exec = {
	.visit_begin                 = read_op_exec_begin,
	.visit_stat                  = read_op_exec_stat,
	.visit_read                  = read_op_exec_read,
	.visit_omap_get_keys         = read_op_exec_omap_get_keys,
	.visit_omap_get_vals         = read_op_exec_omap_get_vals,
	.visit_omap_get_vals_by_keys = read_op_exec_omap_get_vals_by_keys,
	.visit_end                   = read_op_exec_end
};

extern "C" void core_read_op(mobject_store_read_op_t read_op, server_visitor_args_t vargs)
{
	execute_read_op_visitor(&read_op_exec, read_op, (void*)vargs);
}

void read_op_exec_begin(void* u)
{
78
    ENTERING;
79
    auto vargs = static_cast<server_visitor_args_t>(u);
80 81 82 83 84 85 86 87 88 89
    // find oid
    const char* object_name = vargs->object_name;
    oid_t oid = vargs->oid;
    if(oid == 0) {
        sdskv_provider_handle_t sdskv_ph = vargs->srv_ctx->sdskv_ph;
        sdskv_database_id_t name_db_id = vargs->srv_ctx->name_db_id;
        oid = get_oid_from_name(sdskv_ph, name_db_id, object_name);
        vargs->oid = oid;
    }
    LEAVING
90 91 92 93
}

void read_op_exec_stat(void* u, uint64_t* psize, time_t* pmtime, int* prval)
{
94
    ENTERING;
95
    auto vargs = static_cast<server_visitor_args_t>(u);
96 97 98 99 100 101 102 103 104 105 106 107 108
    sdskv_provider_handle_t sdskv_ph = vargs->srv_ctx->sdskv_ph;
    sdskv_database_id_t seg_db_id = vargs->srv_ctx->segment_db_id;
    // find oid
    oid_t oid = vargs->oid;
    if(oid == 0) {
        *prval = -1;
        LEAVING;
        return;
    }
    
    double ts = ABT_get_wtime();
    *psize = mobject_compute_object_size(sdskv_ph, seg_db_id, oid, ts);

109
    LEAVING;
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
void read_op_exec_read(void* u, uint64_t offset, size_t len, buffer_u buf, size_t* bytes_read, int* prval)
{
    ENTERING;
    auto vargs = static_cast<server_visitor_args_t>(u);
    bake_provider_handle_t bph = vargs->srv_ctx->bake_ph;
    bake_target_id_t bti = vargs->srv_ctx->bake_tid;
    bake_region_id_t rid;
    hg_bulk_t remote_bulk = vargs->bulk_handle;
    const char* remote_addr_str = vargs->client_addr_str;
    hg_addr_t   remote_addr     = vargs->client_addr;
    sdskv_provider_handle_t sdskv_ph = vargs->srv_ctx->sdskv_ph;
    sdskv_database_id_t seg_db_id = vargs->srv_ctx->segment_db_id;
    int ret;

    uint64_t client_start_index = offset;
    uint64_t client_end_index = offset+len;

    *prval = 0;

    // find oid
    oid_t oid = vargs->oid;
    if(oid == 0) {
        *prval = -1;
        ERROR fprintf(stderr,"oid == 0\n");
        LEAVING;
        return;
    }

    segment_key_t lb;
    lb.oid = oid;
    lb.timestamp = ABT_get_wtime();

    covermap<uint64_t> coverage(offset, offset+len);

146
    size_t max_segments = 128; // XXX this is a pretty arbitrary number
147 148 149 150 151 152 153 154 155 156 157 158 159 160
    segment_key_t       segment_keys[max_segments];
    void*               segment_keys_addrs[max_segments];
    hg_size_t           segment_keys_size[max_segments];
    bake_region_id_t    segment_data[max_segments];
    void*               segment_data_addrs[max_segments];
    hg_size_t           segment_data_size[max_segments];
    for(auto i = 0 ; i < max_segments; i++) {
        segment_keys_addrs[i] = (void*)(&segment_keys[i]);
        segment_keys_size[i]  = sizeof(segment_key_t);
        segment_data_addrs[i] = (void*)(&segment_data[i]);
        segment_data_size[i]  = sizeof(bake_region_id_t);
    }

    bool done = false;
161
    int seg_start_ndx =  0;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    while(!coverage.full() && !done) {

        // get the next max_segments segments
        size_t num_segments = max_segments;
        ret = sdskv_list_keyvals(sdskv_ph, seg_db_id,
                    (const void*)&lb, sizeof(lb),
                    segment_keys_addrs, segment_keys_size,
                    segment_data_addrs, segment_data_size,
                    &num_segments);

        if(ret != SDSKV_SUCCESS) {
            ERROR fprintf(stderr, "sdskv_list_keyvals returned %d\n", ret);
            *prval = -1;
            LEAVING;
            return;
        }

        size_t i;
180
        for(i=seg_start_ndx; i < num_segments; i++) {
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

            const segment_key_t&    seg    = segment_keys[i];
            const bake_region_id_t& region = segment_data[i];
       
            if(seg.oid != oid || coverage.full()) {
                done = true;
                break;
            }

            switch(seg.type) {

                case seg_type_t::ZERO:
                    coverage.set(seg.start_index, seg.end_index);
                    break;

                case seg_type_t::TOMBSTONE:
                    coverage.set(seg.start_index, seg.end_index);
                    break;

                case seg_type_t::BAKE_REGION: {
                    auto ranges = coverage.set(seg.start_index, seg.end_index);
                    for(auto r : ranges) {
                        uint64_t segment_size  = r.end - r.start;
                        uint64_t region_offset = r.start - seg.start_index;
                        uint64_t remote_offset = r.start - offset;
206
                        uint64_t bytes_read = 0;
207
                        ret = bake_proxy_read(bph, region, region_offset, remote_bulk,
208
                                remote_offset, remote_addr_str, segment_size, &bytes_read);
209 210 211
                        if(ret != 0) {
                            *prval = -1;
                            ERROR fprintf(stderr,"bake_proxy_read returned %d\n", ret);
212 213 214 215 216 217 218
                            LEAVING;
                            return;
                        }
                        else if (bytes_read != segment_size) {
                            *prval = -1;
                            ERROR fprintf(stderr,"bake_proxy_read invalid read of %" PRIu64 \
                                                 " (requested=%" PRIu64 ")\n", bytes_read, segment_size);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                            LEAVING;
                            return;
                        }
                    }
                    break;
                } // end case seg_type_t::BAKE_REGION

                case seg_type_t::SMALL_REGION: {
                    auto ranges = coverage.set(seg.start_index, seg.end_index);
                    const char* base = static_cast<const char*>((void*)(&region));
                    margo_instance_id mid = vargs->srv_ctx->mid;
                    for(auto r : ranges) {
                        uint64_t segment_size  = r.end - r.start;
                        uint64_t region_offset = r.start - seg.start_index;
                        uint64_t remote_offset = r.start - offset;
                        void* buf_ptrs[1] = { const_cast<char*>(base + region_offset) };
                        hg_size_t buf_sizes[1] = { segment_size };
                        hg_bulk_t handle;
                        ret = margo_bulk_create(mid,1, buf_ptrs, buf_sizes, HG_BULK_READ_ONLY, &handle);
                        if(ret != HG_SUCCESS) {
                            ERROR fprintf(stderr,"margo_bulk_create returned %d\n", ret);
                            LEAVING;
                            *prval = -1;
                            return;
                        } // end if
                        ret = margo_bulk_transfer(mid, HG_BULK_PUSH, 
                                remote_addr, remote_bulk, 
                                buf.as_offset+remote_offset, 
                                handle, 0, segment_size);
                        if(ret != HG_SUCCESS) {
                            ERROR fprintf(stderr,"margo_bulk_transfer returned %d\n", ret);
                            *prval = -1;
                            LEAVING;
                            return;
                        } // end if
                        ret = margo_bulk_free(handle);
                        if(ret != HG_SUCCESS) {
                            ERROR fprintf(stderr,"margo_bulk_free returned %d\n", ret);
                            *prval = -1;
                            LEAVING;
                            return;
                        } // end if
                    } // end for
                } // end case seg_type_t::SMALL_REGION

            } // end switch
265 266
            // update the start key timestamp to that of the last processed segment
            lb.timestamp = seg.timestamp;
267 268
        } // end for

269
        seg_start_ndx = 1;
270 271
        if(num_segments != max_segments) done = true;
    }
272
    *bytes_read = coverage.bytes_read();
273 274
    LEAVING;
}
275 276 277 278

void read_op_exec_omap_get_keys(void* u, const char* start_after, uint64_t max_return, 
				mobject_store_omap_iter_t* iter, int* prval)
{
279
    ENTERING;
280
    auto vargs = static_cast<server_visitor_args_t>(u);
281
    const char* object_name = vargs->object_name;
282 283 284
    sdskv_provider_handle_t sdskv_ph = vargs->srv_ctx->sdskv_ph;
    sdskv_database_id_t omap_db_id = vargs->srv_ctx->omap_db_id;
    int ret;
285 286
    *prval = 0;

287 288
    oid_t oid = vargs->oid;
    if(oid == 0) {
289
        *prval = -1;
290 291
        ERROR fprintf(stderr, "oid == 0\n");
        LEAVING;
292 293
        return;
    }
294
    
295
    omap_iter_create(iter);
296
    size_t lb_size = sizeof(omap_key_t)+MAX_OMAP_KEY_SIZE;
297 298 299
    omap_key_t* lb = (omap_key_t*)calloc(1, lb_size);
    lb->oid = oid;
    strcpy(lb->key, start_after);
300

301
    hg_size_t max_keys = 10;
302
    hg_size_t key_len  = MAX_OMAP_KEY_SIZE+sizeof(omap_key_t); 
303 304 305 306
    std::vector<void*> keys(max_keys);
    std::vector<hg_size_t> ksizes(max_keys, key_len);
    std::vector<std::vector<char>> buffers(max_keys, std::vector<char>(key_len));
    for(auto i=0; i < max_keys; i++) keys[i] = (void*)buffers[i].data();
307

308 309 310 311 312 313 314 315 316
    hg_size_t keys_retrieved = max_keys;
    hg_size_t count = 0;
    do {
        ret = sdskv_list_keys(sdskv_ph, omap_db_id,
                (const void*)lb, lb_size,
                keys.data(), ksizes.data(),
                &keys_retrieved);
        if(ret != SDSKV_SUCCESS) {
            *prval = -1;
317
            ERROR fprintf(stderr, "sdskv_list_keys returned %d\n", ret);
318 319
            break;
        }
320
        const char* k = NULL;
321 322 323 324 325 326 327
        for(auto i = 0; i < keys_retrieved && count < max_return; i++, count++) {
            // extract the actual key part, without the oid
            k = ((omap_key_t*)keys[i])->key;
            /* this key is not part of the same object, we should leave the loop */
            if(((omap_key_t*)keys[i])->oid != oid) goto out; /* ugly way of leaving the loop, I know ... */
            omap_iter_append(*iter, k, nullptr, 0);
        }
328 329 330 331
        if(k != NULL) {
            strcpy(lb->key, k);
            lb_size = strlen(k) + sizeof(omap_key_t);
        }
332 333 334 335
    } while(keys_retrieved == max_keys && count < max_return);

out:
    free(lb);
336
    LEAVING;
337 338 339 340
}

void read_op_exec_omap_get_vals(void* u, const char* start_after, const char* filter_prefix, uint64_t max_return, mobject_store_omap_iter_t* iter, int* prval)
{
341
    ENTERING;
342
    auto vargs = static_cast<server_visitor_args_t>(u);
343
    const char* object_name = vargs->object_name;
344 345 346
    sdskv_provider_handle_t sdskv_ph = vargs->srv_ctx->sdskv_ph;
    sdskv_database_id_t omap_db_id = vargs->srv_ctx->omap_db_id;
    int ret;
347 348
    *prval = 0;

349 350
    oid_t oid = vargs->oid;
    if(oid == 0) {
351
        *prval = -1;
352 353
        ERROR fprintf(stderr, "oid == 0\n");
        LEAVING;
354 355 356
        return;
    }

357 358 359
    hg_size_t max_items = std::min(max_return, (decltype(max_return))10);
    hg_size_t key_len  = MAX_OMAP_KEY_SIZE + sizeof(omap_key_t);
    hg_size_t val_len  = MAX_OMAP_VAL_SIZE;
360

361 362
    omap_iter_create(iter);

363
    /* omap_key_t equivalent of start_key */
364
    hg_size_t lb_size = key_len;
365 366 367
    omap_key_t* lb = (omap_key_t*)calloc(1, lb_size);
    lb->oid = oid;
    strcpy(lb->key, start_after);
368

369 370 371 372 373
    /* omap_key_t equivalent of the filter_prefix */
    hg_size_t prefix_size = sizeof(omap_key_t)+strlen(filter_prefix);
    omap_key_t* prefix = (omap_key_t*)calloc(1, prefix_size);
    prefix->oid = oid;
    strcpy(prefix->key, filter_prefix);
374 375
    hg_size_t prefix_actual_size = offsetof(omap_key_t, key)+strlen(filter_prefix);
    /* we need the above because the prefix in sdskv is not considered a string */
376 377 378 379 380 381 382 383 384 385 386

    /* initialize structures to pass to SDSKV functions */
    std::vector<void*> keys(max_items);
    std::vector<void*> vals(max_items);
    std::vector<hg_size_t> ksizes(max_items, key_len);
    std::vector<hg_size_t> vsizes(max_items, val_len);
    std::vector<std::vector<char>> key_buffers(max_items, std::vector<char>(key_len));
    std::vector<std::vector<char>> val_buffers(max_items, std::vector<char>(val_len));
    for(auto i=0; i < max_items; i++) {
        keys[i] = (void*)key_buffers[i].data();
        vals[i] = (void*)val_buffers[i].data();
387
    }
388 389 390 391 392 393 394

    hg_size_t items_retrieved = max_items;
    hg_size_t count = 0;
    do {
        ret = sdskv_list_keyvals_with_prefix(
                sdskv_ph, omap_db_id,
                (const void*)lb, lb_size,
395
                (const void*)prefix, prefix_actual_size,
396 397 398 399 400
                keys.data(), ksizes.data(),
                vals.data(), vsizes.data(),
                &items_retrieved);
        if(ret != SDSKV_SUCCESS) {
            *prval = -1;
401
            ERROR fprintf(stderr, "sdskv_list_keyvals_with_prefix returned %d\n", ret);
402 403 404 405 406 407 408 409 410 411 412
            break;
        }
        const char* k;
        for(auto i = 0; i < items_retrieved && count < max_return; i++, count++) {
            // extract the actual key part, without the oid
            k = ((omap_key_t*)keys[i])->key;
            /* this key is not part of the same object, we should leave the loop */
            if(((omap_key_t*)keys[i])->oid != oid) goto out; /* ugly way of leaving the loop, I know ... */

            omap_iter_append(*iter, k, (const char*)vals[i], vsizes[i]);
        }
413 414
        memset(lb, 0, lb_size);
        lb->oid = oid;
415 416 417 418 419 420
        strcpy(lb->key, k);

    } while(items_retrieved == max_items && count < max_return);

out:
    free(lb);
421
    LEAVING;
422 423 424 425
}

void read_op_exec_omap_get_vals_by_keys(void* u, char const* const* keys, size_t num_keys, mobject_store_omap_iter_t* iter, int* prval)
{
426
    ENTERING;
427
    auto vargs = static_cast<server_visitor_args_t>(u);
428
    const char* object_name = vargs->object_name;
429 430 431
    sdskv_provider_handle_t sdskv_ph = vargs->srv_ctx->sdskv_ph;
    sdskv_database_id_t omap_db_id = vargs->srv_ctx->omap_db_id;
    int ret;
432 433
    *prval = 0;

434 435
    oid_t oid = vargs->oid;
    if(oid == 0) {
436
        *prval = -1;
437 438
        ERROR fprintf(stderr, "oid == 0\n");
        LEAVING;
439 440
        return;
    }
441
    
442 443
    omap_iter_create(iter);

444 445 446 447
    // figure out key sizes
    std::vector<hg_size_t> ksizes(num_keys);
    hg_size_t max_ksize = 0;
    for(auto i=0; i < num_keys; i++) {
448
        hg_size_t s = offsetof(omap_key_t,key)+strlen(keys[i])+1;
449 450 451
        if(s > max_ksize) max_ksize = s;
        ksizes[i] = s;
    }
452
    max_ksize += sizeof(omap_key_t);
453

454
    omap_key_t* key = (omap_key_t*)calloc(1, max_ksize);
455
    for(size_t i=0; i < num_keys; i++) {
456 457
        memset(key, 0, max_ksize);
        key->oid = oid;
458 459 460 461 462 463 464
        strcpy(key->key, keys[i]);
        // get length of the value
        hg_size_t vsize;
        ret = sdskv_length(sdskv_ph, omap_db_id, 
                (const void*)key, ksizes[i], &vsize);
        if(ret != SDSKV_SUCCESS) {
            *prval = -1;
465
            ERROR fprintf(stderr, "sdskv_length returned %d\n", ret);
466 467 468 469 470 471 472 473
            break;
        }
        std::vector<char> value(vsize);
        ret = sdskv_get(sdskv_ph, omap_db_id,
                (const void*)key, ksizes[i],
                (void*)value.data(), &vsize);
        if(ret != SDSKV_SUCCESS) {
            *prval = -1;
474
            ERROR fprintf(stderr, "sdskv_get returned %d\n", ret);
475
            break;
476
        }
477
        omap_iter_append(*iter, keys[i], value.data(), vsize);
478
    }
479
    LEAVING;
480 481 482 483 484 485
}

void read_op_exec_end(void* u)
{
    auto vargs = static_cast<server_visitor_args_t>(u);
}
486 487 488 489 490 491

static oid_t get_oid_from_name( 
        sdskv_provider_handle_t ph,
        sdskv_database_id_t name_db_id,
        const char* name)
{
492
    ENTERING;
493 494
    oid_t result = 0;
    hg_size_t oid_size = sizeof(result);
495 496 497
    int ret = sdskv_get(ph, name_db_id, (const void*)name, strlen(name)+1, (void*)&result, &oid_size);
    if(ret != SDSKV_SUCCESS) result = 0;
    LEAVING;
498 499
    return result;
}