Commit 3394c550 authored by Philip Carns's avatar Philip Carns

refactor mercury init to share across targets

parent b0bf341a
Pipeline #976 skipped
...@@ -4,46 +4,55 @@ ...@@ -4,46 +4,55 @@
* See COPYRIGHT in top-level directory. * See COPYRIGHT in top-level directory.
*/ */
#include <assert.h>
#include <bake-bulk.h> #include <bake-bulk.h>
#include <margo.h> #include <margo.h>
#include "bake-bulk-rpc.h" #include "bake-bulk-rpc.h"
/* Refers to a single Mercury/Margo initialization, for now this is shared by
* all remote targets. In the future we probably need to support multiple in
* case we run atop more than one transport at a time.
*/
struct hg_instance
{
margo_instance_id mid;
hg_class_t *hg_class;
hg_context_t *hg_context;
int refct;
hg_id_t bake_bulk_shutdown_id;
hg_id_t bake_bulk_create_id;
hg_id_t bake_bulk_write_id;
hg_id_t bake_bulk_persist_id;
hg_id_t bake_bulk_get_size_id;
hg_id_t bake_bulk_read_id;
};
/* Refers to an instance connected to a specific target */
struct bake_instance struct bake_instance
{ {
bake_target_id_t bti; /* persistent identifier for this target */ bake_target_id_t bti; /* persistent identifier for this target */
hg_addr_t dest; /* remote Mercury address */ hg_addr_t dest; /* remote Mercury address */
/* TODO: stuff will probably be split out into an hg instance */
margo_instance_id mid; /* Margo instance */
hg_class_t *hg_class;
hg_context_t *hg_context;
}; };
/* TODO: replace later, hard coded global instance */ /* TODO: replace later, hard coded global instance */
struct bake_instance g_binst; struct bake_instance g_binst = {
.bti = 0,
.dest = HG_ADDR_NULL,
};
/* TODO: these IDs are probably not global in the long run either */ struct hg_instance g_hginst = {
static hg_id_t g_bake_bulk_shutdown_id; .mid = MARGO_INSTANCE_NULL,
static hg_id_t g_bake_bulk_create_id; .hg_class = NULL,
static hg_id_t g_bake_bulk_write_id; .hg_context = NULL,
static hg_id_t g_bake_bulk_persist_id; .refct = 0,
static hg_id_t g_bake_bulk_get_size_id; };
static hg_id_t g_bake_bulk_read_id;
int bake_probe_instance( static int hg_instance_init(const char *mercury_dest)
const char *mercury_dest,
bake_target_id_t *bti)
{ {
*bti = 0; /* TODO: use a real id eventually, just a placeholder for now */ /* have we already started a Mercury instance? */
hg_return_t hret; if(g_hginst.refct > 0)
return(0);
memset(&g_binst, 0, sizeof(g_binst));
/* TODO: eventually we will not init HG on every target probe, probably
* separate step so hg instance can be shared. Simple test case is to
* build a single client application that communicates with two distinct
* targets (on different servers) simultaneously.
*/
/* boilerplate HG initialization steps */ /* boilerplate HG initialization steps */
/***************************************/ /***************************************/
...@@ -51,68 +60,96 @@ int bake_probe_instance( ...@@ -51,68 +60,96 @@ int bake_probe_instance(
* na_listen flag is false); but we pass in the *target* server address * na_listen flag is false); but we pass in the *target* server address
* here to make sure that Mercury starts up the correct transport * here to make sure that Mercury starts up the correct transport
*/ */
g_binst.hg_class = HG_Init(mercury_dest, HG_FALSE); g_hginst.hg_class = HG_Init(mercury_dest, HG_FALSE);
if(!g_binst.hg_class) if(!g_hginst.hg_class)
{ {
return(-1); return(-1);
} }
g_binst.hg_context = HG_Context_create(g_binst.hg_class); g_hginst.hg_context = HG_Context_create(g_hginst.hg_class);
if(!g_binst.hg_context) if(!g_hginst.hg_context)
{ {
HG_Finalize(g_binst.hg_class); HG_Finalize(g_hginst.hg_class);
return(-1); return(-1);
} }
/* register RPCs */ /* register RPCs */
g_bake_bulk_shutdown_id = g_hginst.bake_bulk_shutdown_id =
MERCURY_REGISTER(g_binst.hg_class, MERCURY_REGISTER(g_hginst.hg_class,
"bake_bulk_shutdown_rpc", void, void, "bake_bulk_shutdown_rpc", void, void,
NULL); NULL);
g_bake_bulk_create_id = g_hginst.bake_bulk_create_id =
MERCURY_REGISTER(g_binst.hg_class, MERCURY_REGISTER(g_hginst.hg_class,
"bake_bulk_create_rpc", "bake_bulk_create_rpc",
bake_bulk_create_in_t, bake_bulk_create_in_t,
bake_bulk_create_out_t, bake_bulk_create_out_t,
NULL); NULL);
g_bake_bulk_write_id = g_hginst.bake_bulk_write_id =
MERCURY_REGISTER(g_binst.hg_class, MERCURY_REGISTER(g_hginst.hg_class,
"bake_bulk_write_rpc", "bake_bulk_write_rpc",
bake_bulk_write_in_t, bake_bulk_write_in_t,
bake_bulk_write_out_t, bake_bulk_write_out_t,
NULL); NULL);
g_bake_bulk_persist_id = g_hginst.bake_bulk_persist_id =
MERCURY_REGISTER(g_binst.hg_class, MERCURY_REGISTER(g_hginst.hg_class,
"bake_bulk_persist_rpc", "bake_bulk_persist_rpc",
bake_bulk_persist_in_t, bake_bulk_persist_in_t,
bake_bulk_persist_out_t, bake_bulk_persist_out_t,
NULL); NULL);
g_bake_bulk_get_size_id = g_hginst.bake_bulk_get_size_id =
MERCURY_REGISTER(g_binst.hg_class, MERCURY_REGISTER(g_hginst.hg_class,
"bake_bulk_get_size_rpc", "bake_bulk_get_size_rpc",
bake_bulk_get_size_in_t, bake_bulk_get_size_in_t,
bake_bulk_get_size_out_t, bake_bulk_get_size_out_t,
NULL); NULL);
g_bake_bulk_read_id = g_hginst.bake_bulk_read_id =
MERCURY_REGISTER(g_binst.hg_class, MERCURY_REGISTER(g_hginst.hg_class,
"bake_bulk_read_rpc", "bake_bulk_read_rpc",
bake_bulk_read_in_t, bake_bulk_read_in_t,
bake_bulk_read_out_t, bake_bulk_read_out_t,
NULL); NULL);
g_binst.mid = margo_init(0, 0, g_binst.hg_context); g_hginst.mid = margo_init(0, 0, g_hginst.hg_context);
if(!g_binst.mid) if(!g_hginst.mid)
{ {
HG_Context_destroy(g_binst.hg_context); HG_Context_destroy(g_hginst.hg_context);
HG_Finalize(g_binst.hg_class); HG_Finalize(g_hginst.hg_class);
return(-1); return(-1);
} }
g_hginst.refct = 1;
return(0);
}
void hg_instance_finalize(void)
{
g_hginst.refct--;
assert(g_hginst.refct > -1);
if(g_hginst.refct == 0)
{
margo_finalize(g_hginst.mid);
HG_Context_destroy(g_hginst.hg_context);
HG_Finalize(g_hginst.hg_class);
}
}
int bake_probe_instance(
const char *mercury_dest,
bake_target_id_t *bti)
{
hg_return_t hret;
*bti = 0; /* TODO: use a real id eventually, just a placeholder for now */
int ret;
ret = hg_instance_init(mercury_dest);
if(ret < 0)
return(ret);
hret = margo_addr_lookup(g_binst.mid, mercury_dest, &g_binst.dest); hret = margo_addr_lookup(g_hginst.mid, mercury_dest, &g_binst.dest);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
margo_finalize(g_binst.mid); hg_instance_finalize();
HG_Context_destroy(g_binst.hg_context);
HG_Finalize(g_binst.hg_class);
return(-1); return(-1);
} }
...@@ -122,10 +159,8 @@ int bake_probe_instance( ...@@ -122,10 +159,8 @@ int bake_probe_instance(
void bake_release_instance( void bake_release_instance(
bake_target_id_t bti) bake_target_id_t bti)
{ {
HG_Addr_free(g_binst.hg_class, g_binst.dest); HG_Addr_free(g_hginst.hg_class, g_binst.dest);
margo_finalize(g_binst.mid); hg_instance_finalize();
HG_Context_destroy(g_binst.hg_context);
HG_Finalize(g_binst.hg_class);
memset(&g_binst, 0, sizeof(g_binst)); memset(&g_binst, 0, sizeof(g_binst));
return; return;
} }
...@@ -136,14 +171,14 @@ int bake_shutdown_service(bake_target_id_t bti) ...@@ -136,14 +171,14 @@ int bake_shutdown_service(bake_target_id_t bti)
hg_handle_t handle; hg_handle_t handle;
/* create handle */ /* create handle */
hret = HG_Create(g_binst.hg_context, g_binst.dest, hret = HG_Create(g_hginst.hg_context, g_binst.dest,
g_bake_bulk_shutdown_id, &handle); g_hginst.bake_bulk_shutdown_id, &handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
return(-1); return(-1);
} }
hret = margo_forward(g_binst.mid, handle, NULL); hret = margo_forward(g_hginst.mid, handle, NULL);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Destroy(handle); HG_Destroy(handle);
...@@ -171,7 +206,7 @@ int bake_bulk_write( ...@@ -171,7 +206,7 @@ int bake_bulk_write(
in.rid = rid; in.rid = rid;
in.region_offset = region_offset; in.region_offset = region_offset;
hret = HG_Bulk_create(g_binst.hg_class, 1, (void**)(&buf), &buf_size, hret = HG_Bulk_create(g_hginst.hg_class, 1, (void**)(&buf), &buf_size,
HG_BULK_READ_ONLY, &in.bulk_handle); HG_BULK_READ_ONLY, &in.bulk_handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
...@@ -179,15 +214,15 @@ int bake_bulk_write( ...@@ -179,15 +214,15 @@ int bake_bulk_write(
} }
/* create handle */ /* create handle */
hret = HG_Create(g_binst.hg_context, g_binst.dest, hret = HG_Create(g_hginst.hg_context, g_binst.dest,
g_bake_bulk_write_id, &handle); g_hginst.bake_bulk_write_id, &handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Bulk_free(in.bulk_handle); HG_Bulk_free(in.bulk_handle);
return(-1); return(-1);
} }
hret = margo_forward(g_binst.mid, handle, &in); hret = margo_forward(g_hginst.mid, handle, &in);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Destroy(handle); HG_Destroy(handle);
...@@ -226,14 +261,14 @@ int bake_bulk_create( ...@@ -226,14 +261,14 @@ int bake_bulk_create(
in.region_size = region_size; in.region_size = region_size;
/* create handle */ /* create handle */
hret = HG_Create(g_binst.hg_context, g_binst.dest, hret = HG_Create(g_hginst.hg_context, g_binst.dest,
g_bake_bulk_create_id, &handle); g_hginst.bake_bulk_create_id, &handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
return(-1); return(-1);
} }
hret = margo_forward(g_binst.mid, handle, &in); hret = margo_forward(g_hginst.mid, handle, &in);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Destroy(handle); HG_Destroy(handle);
...@@ -270,14 +305,14 @@ int bake_bulk_persist( ...@@ -270,14 +305,14 @@ int bake_bulk_persist(
in.rid = rid; in.rid = rid;
/* create handle */ /* create handle */
hret = HG_Create(g_binst.hg_context, g_binst.dest, hret = HG_Create(g_hginst.hg_context, g_binst.dest,
g_bake_bulk_persist_id, &handle); g_hginst.bake_bulk_persist_id, &handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
return(-1); return(-1);
} }
hret = margo_forward(g_binst.mid, handle, &in); hret = margo_forward(g_hginst.mid, handle, &in);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Destroy(handle); HG_Destroy(handle);
...@@ -313,14 +348,14 @@ int bake_bulk_get_size( ...@@ -313,14 +348,14 @@ int bake_bulk_get_size(
in.rid = rid; in.rid = rid;
/* create handle */ /* create handle */
hret = HG_Create(g_binst.hg_context, g_binst.dest, hret = HG_Create(g_hginst.hg_context, g_binst.dest,
g_bake_bulk_get_size_id, &handle); g_hginst.bake_bulk_get_size_id, &handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
return(-1); return(-1);
} }
hret = margo_forward(g_binst.mid, handle, &in); hret = margo_forward(g_hginst.mid, handle, &in);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Destroy(handle); HG_Destroy(handle);
...@@ -359,7 +394,7 @@ int bake_bulk_read( ...@@ -359,7 +394,7 @@ int bake_bulk_read(
in.rid = rid; in.rid = rid;
in.region_offset = region_offset; in.region_offset = region_offset;
hret = HG_Bulk_create(g_binst.hg_class, 1, (void**)(&buf), &buf_size, hret = HG_Bulk_create(g_hginst.hg_class, 1, (void**)(&buf), &buf_size,
HG_BULK_WRITE_ONLY, &in.bulk_handle); HG_BULK_WRITE_ONLY, &in.bulk_handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
...@@ -367,15 +402,15 @@ int bake_bulk_read( ...@@ -367,15 +402,15 @@ int bake_bulk_read(
} }
/* create handle */ /* create handle */
hret = HG_Create(g_binst.hg_context, g_binst.dest, hret = HG_Create(g_hginst.hg_context, g_binst.dest,
g_bake_bulk_read_id, &handle); g_hginst.bake_bulk_read_id, &handle);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Bulk_free(in.bulk_handle); HG_Bulk_free(in.bulk_handle);
return(-1); return(-1);
} }
hret = margo_forward(g_binst.mid, handle, &in); hret = margo_forward(g_hginst.mid, handle, &in);
if(hret != HG_SUCCESS) if(hret != HG_SUCCESS)
{ {
HG_Destroy(handle); HG_Destroy(handle);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment