libmobject-store.c 6.28 KB
Newer Older
1 2 3 4 5 6
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7
#include "mobject-store-config.h"
8 9

#include <stdio.h>
10
#include <stdlib.h>
11 12
#include <assert.h>

13 14 15
#include <margo.h>
#include <ssg.h>

16
#include "libmobject-store.h"
17 18 19 20 21 22
#include "src/rpc-types/write-op.h"
#include "src/rpc-types/read-op.h"
#include "src/rpc-types/read-op.h"
#include "src/client/io-context.h"
#include "src/io-chain/prepare-read-op.h"
#include "src/io-chain/prepare-write-op.h"
23

24 25
#define MOBJECT_CLUSTER_FILE_ENV "MOBJECT_CLUSTER_FILE"

26 27 28 29 30 31

// global variables for RPC ids
hg_id_t mobject_write_op_rpc_id;
hg_id_t mobject_read_op_rpc_id;
hg_id_t mobject_shutdown_rpc_id;

32
typedef struct mobject_store_handle
33
{
34
    margo_instance_id mid;
35
    ssg_group_id_t gid;
36
    int connected;
37 38
} mobject_store_handle_t;

39

40
int mobject_store_create(mobject_store_t *cluster, const char * const id)
41
{
42 43 44 45
    mobject_store_handle_t *cluster_handle;
    char *cluster_file;
    int ret;

46 47 48
    (void)id; /* XXX: id unused in mobject */

    /* allocate a new cluster handle and set some fields */
Matthieu Dorier's avatar
Matthieu Dorier committed
49
    cluster_handle = (mobject_store_handle_t*)calloc(1,sizeof(*cluster_handle));
50
    if (!cluster_handle)
51 52 53 54 55 56 57 58 59
        return -1;

    /* use env variable to determine how to connect to the cluster */
    /* NOTE: this is the _only_ method for specifying a cluster for now... */
    cluster_file = getenv(MOBJECT_CLUSTER_FILE_ENV);
    if (!cluster_file)
    {
        fprintf(stderr, "Error: %s env variable must point to mobject cluster file\n",
            MOBJECT_CLUSTER_FILE_ENV);
60
        free(cluster_handle);
61 62 63 64 65 66 67 68
        return -1;
    }

    ret = ssg_group_id_load(cluster_file, &cluster_handle->gid);
    if (ret != 0)
    {
        fprintf(stderr, "Error: Unable to load mobject cluster info from file %s\n",
            cluster_file);
69
        free(cluster_handle);
70 71
        return -1;
    }
72 73 74

    /* set the returned cluster handle */
    *cluster = cluster_handle;
75

76
    return 0;
77 78 79 80
}

int mobject_store_connect(mobject_store_t cluster)
{
81 82 83 84 85 86 87 88 89
    mobject_store_handle_t *cluster_handle = (mobject_store_handle_t *)cluster;
    char *srv_addr;
    char proto[24] = {0};
    int i;
    int ret;

    if (cluster_handle->connected)
        return 0;

Shane Snyder's avatar
Shane Snyder committed
90
    /* figure out protocol to connect with using address information 
91 92 93 94 95 96 97 98 99 100
     * associated with the SSG group ID
     */
    srv_addr = ssg_group_id_get_addr_str(cluster_handle->gid);
    if (!srv_addr)
    {
        fprintf(stderr, "Error: Unable to obtain cluster group server address\n");
        ssg_group_id_free(cluster_handle->gid);
        free(cluster_handle);
        return -1;
    }
Shane Snyder's avatar
Shane Snyder committed
101
    /* we only need to the proto portion of the address to initialize */
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
    for(i=0; i<24 && srv_addr[i] != '\0' && srv_addr[i] != ':'; i++)
        proto[i] = srv_addr[i];

    /* intialize margo */
    /* XXX: probably want to expose some way of tweaking threading parameters */
    cluster_handle->mid = margo_init(proto, MARGO_CLIENT_MODE, 0, -1);
    if (cluster_handle->mid == MARGO_INSTANCE_NULL)
    {
        fprintf(stderr, "Error: Unable to initialize margo\n");
        free(srv_addr);
        ssg_group_id_free(cluster_handle->gid);
        free(cluster_handle);
        return -1;
    }

    /* initialize ssg */
    ret = ssg_init(cluster_handle->mid);
    if (ret != SSG_SUCCESS)
    {
        fprintf(stderr, "Error: Unable to initialize SSG\n");
        margo_finalize(cluster_handle->mid);
        free(srv_addr);
        ssg_group_id_free(cluster_handle->gid);
        free(cluster_handle);
        return -1;
    }

    /* attach to the cluster group */
    ret = ssg_group_attach(cluster_handle->gid);
    if (ret != SSG_SUCCESS)
    {
        fprintf(stderr, "Error: Unable to attach to the mobject cluster group\n");
        ssg_finalize();
        margo_finalize(cluster_handle->mid);
        free(srv_addr);
        ssg_group_id_free(cluster_handle->gid);
        free(cluster_handle);
        return -1;
    }
    cluster_handle->connected = 1;

    free(srv_addr);
144 145

    return 0;
146 147
}

148
void mobject_store_shutdown(mobject_store_t cluster)
149
{
150 151
    mobject_store_handle_t *cluster_handle =
        (mobject_store_handle_t *)cluster;
152 153
    assert(cluster_handle != NULL);

154 155
    if (!cluster_handle->connected)
        return;
156

157 158 159 160
    ssg_group_detach(cluster_handle->gid);
    ssg_finalize();
    margo_finalize(cluster_handle->mid);
    ssg_group_id_free(cluster_handle->gid);
161
    free(cluster_handle);
162

163
    return;
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

void mobject_store_register(margo_instance_id mid)
{
	static int registered = 0;
	
	if(!registered) {
		mobject_write_op_rpc_id = 
		MARGO_REGISTER(mid, "mobject_write_op", write_op_in_t, write_op_out_t, NULL);
		mobject_read_op_rpc_id = 
		MARGO_REGISTER(mid, "mobject_read_op",  read_op_in_t,  read_op_out_t, NULL);
		mobject_shutdown_rpc_id =
		MARGO_REGISTER(mid, "mobject_shutdown", void, void, NULL);
		registered = 1;
	}
}

int mobject_store_write_op_operate(mobject_store_write_op_t write_op,
                                   mobject_store_ioctx_t io,
                                   const char *oid,
                                   time_t *mtime,
                                   int flags) 
{
    write_op_in_t in;
    in.object_name = oid;
    in.pool_name   = io->pool_name;
    in.write_op    = write_op;

    prepare_write_op(io->mid, write_op);

    hg_handle_t h;
    margo_create(io->mid, io->svr_addr, mobject_write_op_rpc_id, &h);
    margo_forward(h, &in);

    write_op_out_t resp;
    margo_get_output(h, &resp);

    margo_free_output(h,&resp);
    margo_destroy(h);
Rob Latham's avatar
Rob Latham committed
203
    return 0;
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
}

int mobject_store_read_op_operate(mobject_store_read_op_t read_op,
                                  mobject_store_ioctx_t ioctx,
                                  const char *oid,
                                  int flags)
{   
    read_op_in_t in; 
    in.object_name = oid;
    in.pool_name   = ioctx->pool_name;
    in.read_op     = read_op;
    
    prepare_read_op(ioctx->mid, read_op);
    
    // TODO: svr_addr should be computed based on the pool name, object name,
    // and SSG structures accessible via the io context
    hg_handle_t h;
    margo_create(ioctx->mid, ioctx->svr_addr, mobject_read_op_rpc_id, &h);
    margo_forward(h, &in);
    
    read_op_out_t resp; 
    margo_get_output(h, &resp);
    
    feed_read_op_pointers_from_response(read_op, resp.responses);
    
    margo_free_output(h,&resp);
    margo_destroy(h);
    
    return 0;
}