core-read-op.cpp 17.5 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>
12
#include <bake-client.h>
13 14 15 16 17
#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"
18
#include "src/server/core/key-types.h"
19
#include "src/server/core/covermap.hpp"
20

21
static int tabs = 0;
22
/*
23 24 25
#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] "); }
26 27 28 29
*/
#define ENTERING
#define LEAVING
#define ERROR
30

31 32 33 34 35 36 37 38
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*);

Matthieu Dorier's avatar
Matthieu Dorier committed
39 40 41 42 43 44
/* 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);

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

50 51 52 53 54 55 56
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
57
    bake_region_id_t region;  // region id
58 59
};

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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)
{
77
    ENTERING;
78
    auto vargs = static_cast<server_visitor_args_t>(u);
Matthieu Dorier's avatar
Matthieu Dorier committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    // 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;
    }
    if(oid == 0) {
        ERROR fprintf(stderr,"oid == 0\n");
        LEAVING;
        return;
    }
    LEAVING
94 95 96 97
}

void read_op_exec_stat(void* u, uint64_t* psize, time_t* pmtime, int* prval)
{
98
    ENTERING;
99
    auto vargs = static_cast<server_visitor_args_t>(u);
Matthieu Dorier's avatar
Matthieu Dorier committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    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;
        ERROR fprintf(stderr,"oid == 0\n");
        LEAVING;
        return;
    }
    
    double ts = ABT_get_wtime();
    *psize = mobject_compute_object_size(sdskv_ph, seg_db_id, oid, ts);

114
    LEAVING;
115 116
}

Matthieu Dorier's avatar
Matthieu Dorier committed
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 146 147 148 149 150 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 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 265
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);

    size_t max_segments = 10; // XXX this is a pretty arbitrary number
    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;

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

            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;
                        ret = bake_proxy_read(bph, region, region_offset, remote_bulk,
                                remote_offset, remote_addr_str, segment_size);
                        if(ret != 0) {
                            *prval = -1;
                            ERROR fprintf(stderr,"bake_proxy_read returned %d\n", ret);
                            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
        } // end for

        if(num_segments != max_segments) done = true;
    }
266
    *bytes_read = coverage.bytes_read();
Matthieu Dorier's avatar
Matthieu Dorier committed
267 268
    LEAVING;
}
269 270 271 272

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)
{
273
    ENTERING;
274
    auto vargs = static_cast<server_visitor_args_t>(u);
275
    const char* object_name = vargs->object_name;
276 277 278
    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;
279 280
    *prval = 0;

281 282
    oid_t oid = vargs->oid;
    if(oid == 0) {
283
        *prval = -1;
284 285
        ERROR fprintf(stderr, "oid == 0\n");
        LEAVING;
286 287
        return;
    }
288
    
289
    omap_iter_create(iter);
290
    size_t lb_size = sizeof(omap_key_t)+MAX_OMAP_KEY_SIZE;
291 292 293
    omap_key_t* lb = (omap_key_t*)calloc(1, lb_size);
    lb->oid = oid;
    strcpy(lb->key, start_after);
294

295
    hg_size_t max_keys = 10;
296
    hg_size_t key_len  = MAX_OMAP_KEY_SIZE+sizeof(omap_key_t); 
297 298 299 300
    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();
301

302 303 304 305 306 307 308 309 310
    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;
311
            ERROR fprintf(stderr, "sdskv_list_keys returned %d\n", ret);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
            break;
        }
        const char* k;
        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);
        }
        strcpy(lb->key, k);
        lb_size = strlen(k) + sizeof(omap_key_t);

    } while(keys_retrieved == max_keys && count < max_return);

out:
    free(lb);
330
    LEAVING;
331 332 333 334
}

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)
{
335
    ENTERING;
336
    auto vargs = static_cast<server_visitor_args_t>(u);
337
    const char* object_name = vargs->object_name;
338 339 340
    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;
341 342
    *prval = 0;

343 344
    oid_t oid = vargs->oid;
    if(oid == 0) {
345
        *prval = -1;
346 347
        ERROR fprintf(stderr, "oid == 0\n");
        LEAVING;
348 349 350
        return;
    }

351 352 353
    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;
354

355 356
    omap_iter_create(iter);

357
    /* omap_key_t equivalent of start_key */
358
    hg_size_t lb_size = key_len;
359 360 361
    omap_key_t* lb = (omap_key_t*)calloc(1, lb_size);
    lb->oid = oid;
    strcpy(lb->key, start_after);
362

363 364 365 366 367
    /* 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);
368 369
    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 */
370 371 372 373 374 375 376 377 378 379 380

    /* 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();
381
    }
382 383 384 385 386 387 388

    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,
389
                (const void*)prefix, prefix_actual_size,
390 391 392 393 394
                keys.data(), ksizes.data(),
                vals.data(), vsizes.data(),
                &items_retrieved);
        if(ret != SDSKV_SUCCESS) {
            *prval = -1;
395
            ERROR fprintf(stderr, "sdskv_list_keyvals_with_prefix returned %d\n", ret);
396 397 398 399 400 401 402 403 404 405 406
            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]);
        }
407 408
        memset(lb, 0, lb_size);
        lb->oid = oid;
409 410 411 412 413 414
        strcpy(lb->key, k);

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

out:
    free(lb);
415
    LEAVING;
416 417 418 419
}

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)
{
420
    ENTERING;
421
    auto vargs = static_cast<server_visitor_args_t>(u);
422
    const char* object_name = vargs->object_name;
423 424 425
    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;
426 427
    *prval = 0;

428 429
    oid_t oid = vargs->oid;
    if(oid == 0) {
430
        *prval = -1;
431 432
        ERROR fprintf(stderr, "oid == 0\n");
        LEAVING;
433 434
        return;
    }
435
    
436 437
    omap_iter_create(iter);

438 439 440 441
    // 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++) {
442
        hg_size_t s = offsetof(omap_key_t,key)+strlen(keys[i])+1;
443 444 445
        if(s > max_ksize) max_ksize = s;
        ksizes[i] = s;
    }
446
    max_ksize += sizeof(omap_key_t);
447

448
    omap_key_t* key = (omap_key_t*)calloc(1, max_ksize);
449
    for(size_t i=0; i < num_keys; i++) {
450 451
        memset(key, 0, max_ksize);
        key->oid = oid;
452 453 454 455 456 457 458
        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;
459
            ERROR fprintf(stderr, "sdskv_length returned %d\n", ret);
460 461 462 463 464 465 466 467
            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;
468
            ERROR fprintf(stderr, "sdskv_get returned %d\n", ret);
469
            break;
470
        }
471
        omap_iter_append(*iter, keys[i], value.data(), vsize);
472
    }
473
    LEAVING;
474 475 476 477 478 479
}

void read_op_exec_end(void* u)
{
    auto vargs = static_cast<server_visitor_args_t>(u);
}
480 481 482 483 484 485

static oid_t get_oid_from_name( 
        sdskv_provider_handle_t ph,
        sdskv_database_id_t name_db_id,
        const char* name)
{
486
    ENTERING;
487 488
    oid_t result = 0;
    hg_size_t oid_size = sizeof(result);
489 490 491 492 493 494
    int ret = sdskv_get(ph, name_db_id, (const void*)name, strlen(name)+1, (void*)&result, &oid_size);
    if(ret != SDSKV_SUCCESS) result = 0;
    if(result == 0) {
        ERROR fprintf(stderr,"oid == 0\n");
    }
    LEAVING;
495 496
    return result;
}