mobject-client.c 7.63 KB
Newer Older
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 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include "mobject-store-config.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <margo.h>
#include <ssg.h>

#include "mobject-client.h"
#include "src/io-chain/prepare-write-op.h"
#include "src/io-chain/prepare-read-op.h"
#include "src/rpc-types/write-op.h"
#include "src/rpc-types/read-op.h"
#include "src/util/log.h"

struct mobject_client {

    margo_instance_id mid;

    char* client_addr;

    hg_id_t mobject_write_op_rpc_id;
    hg_id_t mobject_read_op_rpc_id;
    hg_id_t mobject_shutdown_rpc_id;

    uint64_t num_provider_handles;
};

struct mobject_provider_handle {
    mobject_client_t client;
    hg_addr_t        addr;
    uint8_t          mplex_id;
    uint64_t         refcount;
};

static int mobject_client_register(mobject_client_t client, margo_instance_id mid) 
{
    int ret;
    hg_addr_t client_addr = HG_ADDR_NULL;
    char      client_addr_str[128];
    hg_size_t client_addr_len = 128;

    ret = margo_addr_self(mid, &client_addr); 
    if(ret != HG_SUCCESS) return -1;

    ret = margo_addr_to_string(mid, client_addr_str, &client_addr_len, client_addr);
    if(ret != HG_SUCCESS) {
        margo_addr_free(mid, client_addr);
        return -1;
    }

    margo_addr_free(mid, client_addr);

    client->mid = mid;
    client->client_addr = strdup(client_addr_str);

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

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

        margo_registered_name(mid, "mobject_write_op", &client->mobject_write_op_rpc_id, &flag);
        margo_registered_name(mid, "mobject_read_op",  &client->mobject_read_op_rpc_id,  &flag);
        margo_registered_name(mid, "mobject_shutdown", &client->mobject_shutdown_rpc_id, &flag);

    } else {
        
        client->mobject_write_op_rpc_id =
            MARGO_REGISTER(mid, "mobject_write_op", write_op_in_t, write_op_out_t, NULL);
        client->mobject_read_op_rpc_id = 
            MARGO_REGISTER(mid, "mobject_read_op",  read_op_in_t,  read_op_out_t, NULL);
        client->mobject_shutdown_rpc_id =
            MARGO_REGISTER(mid, "mobject_shutdown", void, void, NULL);
    }

    return 0;
}

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

    c->num_provider_handles = 0;

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

    *client = c;
    return 0;
}

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

int mobject_provider_handle_create(
        mobject_client_t client,
        hg_addr_t addr,
        uint8_t mplex_id,
        mobject_provider_handle_t* handle)
{
    if(client == MOBJECT_CLIENT_NULL) return -1;

    mobject_provider_handle_t provider =
        (mobject_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 mobject_provider_handle_ref_incr(mobject_provider_handle_t handle)
{
    if(handle == MOBJECT_PROVIDER_HANDLE_NULL) return -1;
    handle->refcount += 1;
    return 0;
}

int mobject_provider_handle_release(mobject_provider_handle_t handle)
{
    if(handle == MOBJECT_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 mobject_shutdown(mobject_client_t client, hg_addr_t addr)
{
    hg_return_t hret;
    hg_handle_t handle;

    hret = margo_create(client->mid, addr,
            client->mobject_shutdown_rpc_id, &handle);

    if(hret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_create() failed in mobject_shutdown()\n");
        return -1;
    }

    hret = margo_forward(handle, NULL);
    if(hret != HG_SUCCESS)
    {
        fprintf(stderr, "[MOBJECT] margo_forward() failed in mobject_shutdown()\n");
        margo_destroy(handle);
        return -1;
    }

    margo_destroy(handle);
    return 0;
}

int mobject_write_op_operate(
        mobject_provider_handle_t mph,
        mobject_store_write_op_t write_op,
        const char* pool_name,
        const char *oid,
        time_t *mtime,
        int flags)
{
    hg_return_t ret;

    write_op_in_t in;
    in.object_name = oid;
    in.pool_name   = pool_name;
    in.write_op    = write_op;
    in.client_addr = mph->client->client_addr;
    // TODO take mtime into account

    prepare_write_op(mph->client->mid, write_op);

    hg_addr_t svr_addr = mph->addr;
    if(svr_addr == HG_ADDR_NULL) {
        fprintf(stderr, "[MOBJECT] NULL provider address passed to mobject_write_op_operate\n");
        return -1;
    }

    hg_handle_t h;
    ret = margo_create(mph->client->mid, svr_addr, mph->client->mobject_write_op_rpc_id, &h);
    if(ret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_create() failed in mobject_write_op_operate()\n");
        return -1;
    }

    margo_set_target_id(h, mph->mplex_id);

    ret = margo_forward(h, &in);
    if(ret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_forward() failed in mobject_write_op_operate()\n");
        margo_destroy(h);
        return -1;
    }

    write_op_out_t resp;
    ret = margo_get_output(h, &resp); 
    if(ret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_get_output() failed in mobject_write_op_operate()\n");
        margo_destroy(h);
        return -1;
    }

    margo_free_output(h,&resp); 

    margo_destroy(h);

    return 0;
}

int mobject_read_op_operate(
        mobject_provider_handle_t mph,
        mobject_store_read_op_t read_op,
        const char* pool_name,
        const char *oid,
        int flags)
{
    hg_return_t ret;

    read_op_in_t in; 
    in.object_name = oid;
    in.pool_name   = pool_name;
    in.read_op     = read_op;
    in.client_addr = mph->client->client_addr;;

    prepare_read_op(mph->client->mid, read_op);

    hg_addr_t svr_addr = mph->addr;

    if(svr_addr == HG_ADDR_NULL) {
        fprintf(stderr, "[MOBJECT] NULL provider address passed to mobject_write_op_operate\n");
        return -1;
    }

    hg_handle_t h;
    ret = margo_create(mph->client->mid, svr_addr, mph->client->mobject_read_op_rpc_id, &h);
    if(ret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_create() failed in mobject_read_op_operate()\n");
        return -1;
    }

    margo_set_target_id(h, mph->mplex_id);

    ret = margo_forward(h, &in);
    if(ret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_forward() failed in mobject_read_op_operate()\n");
        margo_destroy(h);
        return -1;
    }

    read_op_out_t resp; 
    ret = margo_get_output(h, &resp); 
    if(ret != HG_SUCCESS) {
        fprintf(stderr, "[MOBJECT] margo_get_output() failed in mobject_read_op_operate()\n");
        margo_destroy(h);
        return -1;
    }

    feed_read_op_pointers_from_response(read_op, resp.responses);

    margo_free_output(h,&resp); 
    margo_destroy(h);

    return 0;
}