ssg.c 37.5 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (c) 2016 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */

7
#include "ssg-config.h"
Shane Snyder's avatar
Shane Snyder committed
8

Jonathan Jenkins's avatar
Jonathan Jenkins committed
9 10 11 12
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
13
#include <errno.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
14 15
#include <stdlib.h>
#include <string.h>
16
#include <time.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
17
#include <assert.h>
Shane Snyder's avatar
Shane Snyder committed
18
#ifdef SSG_HAVE_MPI
19 20
#include <mpi.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
21

Shane Snyder's avatar
Shane Snyder committed
22
#include <mercury.h>
23
#include <abt.h>
Shane Snyder's avatar
Shane Snyder committed
24
#include <margo.h>
25

26
#include "ssg.h"
Shane Snyder's avatar
Shane Snyder committed
27 28 29
#ifdef SSG_HAVE_MPI
#include "ssg-mpi.h"
#endif
30
#include "ssg-internal.h"
31

32 33 34
/* arguments for group lookup ULTs */
struct ssg_group_lookup_ult_args
{
35
    ssg_group_view_t *view;
36 37 38 39 40
    ssg_member_state_t *member_state;
    hg_return_t out;
};
static void ssg_group_lookup_ult(void * arg);

41
/* SSG helper routine prototypes */
42
static ssg_group_descriptor_t * ssg_group_descriptor_create(
43
    uint64_t name_hash, const char * leader_addr_str, int owner_status);
44 45 46 47 48
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor);
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor);
static int ssg_group_view_create(
49 50 51
    const char * const group_addr_strs[], int group_size,
    hg_addr_t self_addr, ssg_member_id_t * self_id,
    ssg_group_view_t * view);
52 53
static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str);
54 55 56 57 58 59 60 61
static void ssg_group_view_destroy(
    ssg_group_view_t * view);
static void ssg_group_destroy_internal(
    ssg_group_t * g);
static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag);
static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs);
62

63 64
/* XXX: i think we ultimately need per-mid ssg instances rather than 1 global? */
ssg_instance_t *ssg_inst = NULL;
Shane Snyder's avatar
Shane Snyder committed
65

66 67 68
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
69

70 71
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
72
{
73 74
    struct timespec ts;

75 76 77 78 79 80 81 82 83 84
    if (ssg_inst)
        return SSG_FAILURE;

    /* initialize an SSG instance for this margo instance */
    ssg_inst = malloc(sizeof(*ssg_inst));
    if (!ssg_inst)
        return SSG_FAILURE;
    memset(ssg_inst, 0, sizeof(*ssg_inst));
    ssg_inst->mid = mid;

85
    ssg_register_rpcs();
Shane Snyder's avatar
Shane Snyder committed
86

87 88 89 90
    /* seed RNG */
    clock_gettime(CLOCK_MONOTONIC, &ts);
    srand(ts.tv_nsec + getpid());

91 92
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
93

94
int ssg_finalize()
95
{
96 97
    ssg_group_t *g, *g_tmp;
    ssg_attached_group_t *ag, *ag_tmp;
98 99 100 101 102

    if (!ssg_inst)
        return SSG_FAILURE;

    /* destroy all active groups */
103
    HASH_ITER(hh, ssg_inst->group_table, g, g_tmp)
104 105 106 107 108
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ssg_group_destroy_internal(g);
    }

109 110 111 112 113 114 115
    /* detach from all attached groups */
    HASH_ITER(hh, ssg_inst->attached_group_table, ag, ag_tmp)
    {
        HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
        ssg_attached_group_destroy(ag);
    }

116 117 118
    free(ssg_inst);
    ssg_inst = NULL;

119
    return SSG_SUCCESS;
120
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
121

122 123 124
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
125

126 127
static int ssg_get_swim_dping_target(
    void *group_data,
Shane Snyder's avatar
Shane Snyder committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141
    swim_member_id_t *target_id,
    swim_member_inc_nr_t *target_inc_nr,
    hg_addr_t *target_addr);
static int ssg_get_swim_iping_targets(
    void *group_data,
    swim_member_id_t *target_ids,
    hg_addr_t *target_addrs);
static void ssg_get_swim_member_addr(
    void *group_data,
    swim_member_id_t id,
    hg_addr_t *target_addr);
static void ssg_get_swim_member_state(
    void *group_data,
    swim_member_id_t id,
142
    swim_member_state_t **state);
143 144 145
static void ssg_apply_swim_member_update(
    void *group_data,
    swim_member_update_t update);
Shane Snyder's avatar
Shane Snyder committed
146

Shane Snyder's avatar
Shane Snyder committed
147 148 149 150 151
static void ssg_gen_rand_member_list(
    ssg_group_t *g);

static int ssg_get_swim_dping_target(
    void *group_data,
Shane Snyder's avatar
Shane Snyder committed
152 153 154
    swim_member_id_t *target_id,
    swim_member_inc_nr_t *target_inc_nr,
    hg_addr_t *target_addr)
155 156
{
    ssg_group_t *g = (ssg_group_t *)group_data;
Shane Snyder's avatar
Shane Snyder committed
157
    ssg_member_state_t *target_ms;
158 159 160

    assert(g != NULL);

Shane Snyder's avatar
Shane Snyder committed
161 162 163 164 165 166 167
    /* if current member list is empty, generate a new random one */
    if (g->member_list == NULL)
        ssg_gen_rand_member_list(g);

    /* pull random member off head of list and return addr */
    target_ms = g->member_list;
    LL_DELETE(g->member_list, target_ms);
Shane Snyder's avatar
Shane Snyder committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    *target_id = (swim_member_id_t)target_ms->id;
    *target_inc_nr = target_ms->swim_state.inc_nr;
    *target_addr = target_ms->addr;

    return 0;
}

static int ssg_get_swim_iping_targets(
    void *group_data,
    swim_member_id_t *target_id,
    hg_addr_t *target_addrs)
{
    ssg_group_t *g = (ssg_group_t *)group_data;

    assert(g != NULL);
183 184 185 186

    return 0;
}

Shane Snyder's avatar
Shane Snyder committed
187 188 189 190 191 192 193 194 195 196 197 198 199
static void ssg_gen_rand_member_list(ssg_group_t *g)
{
    ssg_member_state_t *ms, *tmp;

    /* XXX generate random LL permutation from membership map */
    HASH_ITER(hh, g->view.member_map, ms, tmp)
    {
        LL_APPEND(g->member_list, ms);
    }

    return;
}

Shane Snyder's avatar
Shane Snyder committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
static void ssg_get_swim_member_addr(
    void *group_data,
    swim_member_id_t id,
    hg_addr_t *addr)
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    ssg_member_id_t ssg_id = (ssg_member_id_t)id;
    ssg_member_state_t *ms;

    assert(g != NULL);

    HASH_FIND(hh, g->view.member_map, &ssg_id, sizeof(ssg_member_id_t), ms);
    /* XXX ASSERT */

    *addr = ms->addr;

    return;
}

static void ssg_get_swim_member_state(
    void *group_data,
    swim_member_id_t id,
222
    swim_member_state_t **state)
Shane Snyder's avatar
Shane Snyder committed
223 224 225 226 227 228 229 230 231 232
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    ssg_member_id_t ssg_id = (ssg_member_id_t)id;
    ssg_member_state_t *ms;

    assert(g != NULL);

    HASH_FIND(hh, g->view.member_map, &ssg_id, sizeof(ssg_member_id_t), ms);
    /* XXX ASSERT */

233
    *state = &ms->swim_state;
Shane Snyder's avatar
Shane Snyder committed
234 235 236 237

    return;
}

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
static void ssg_apply_swim_member_update(
    void *group_data,
    swim_member_update_t update)
{
    ssg_group_t *g = (ssg_group_t *)group_data;
    ssg_member_id_t ssg_id = (ssg_member_id_t)update.id;
    ssg_member_state_t *ms;
    ssg_member_update_t ssg_update;

    assert(g != NULL);

    if (update.state.status == SWIM_MEMBER_DEAD)
    {
        HASH_FIND(hh, g->view.member_map, &ssg_id, sizeof(ssg_member_id_t), ms);
        if (ms)
        {
            /* update group, but don't completely remove state */
            LL_DELETE(g->member_list, ms);
            margo_addr_free(ssg_inst->mid, ms->addr);
            g->view.size--;
            ssg_update.id = ssg_id;
            ssg_update.type = SSG_MEMBER_REMOVE;
        }
    }
    else
    {
        assert(0); /* XXX: dynamic group joins aren't possible yet */
    }

    /* execute user-supplied membership update callback, if given */
    if (g->update_cb)
        g->update_cb(ssg_update, g->update_cb_dat);

    return;
}

Shane Snyder's avatar
Shane Snyder committed
274
ssg_group_id_t ssg_group_create(
275 276
    const char * group_name,
    const char * const group_addr_strs[],
277 278 279
    int group_size,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
280
{
281
    uint64_t name_hash;
Shane Snyder's avatar
Shane Snyder committed
282
    hg_addr_t self_addr = HG_ADDR_NULL;
283
    ssg_group_descriptor_t *tmp_descriptor;
284
    ssg_group_t *g = NULL;
285
    hg_return_t hret;
286
    int sret;
Shane Snyder's avatar
Shane Snyder committed
287
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
288

289
    if (!ssg_inst) return group_id;
290

291
    name_hash = ssg_hash64_str(group_name);
292

293
    /* generate a unique ID for this group  */
294 295
    tmp_descriptor = ssg_group_descriptor_create(name_hash, group_addr_strs[0],
        SSG_OWNER_IS_MEMBER);
296
    if (tmp_descriptor == NULL) return group_id;
297

Shane Snyder's avatar
Shane Snyder committed
298
    /* make sure we aren't re-creating an existing group */
299
    HASH_FIND(hh, ssg_inst->group_table, &tmp_descriptor->name_hash,
Shane Snyder's avatar
Shane Snyder committed
300
        sizeof(uint64_t), g);
301 302 303 304 305
    if (g)
    {
        g = NULL;
        goto fini;
    }
306

307 308 309 310 311 312 313
    /* allocate an SSG group data structure and initialize some of it */
    g = malloc(sizeof(*g));
    if (!g) goto fini;
    memset(g, 0, sizeof(*g));
    g->name = strdup(group_name);
    if (!g->name) goto fini;
    g->descriptor = tmp_descriptor;
314 315
    g->update_cb = update_cb;
    g->update_cb_dat = update_cb_dat;
316

317 318 319 320
    /* get my address */
    hret = margo_addr_self(ssg_inst->mid, &self_addr);
    if (hret != HG_SUCCESS) goto fini;

321
    /* initialize the group view */
322 323
    sret = ssg_group_view_create(group_addr_strs, group_size, self_addr,
        &g->self_id, &g->view);
324
    if (sret != SSG_SUCCESS) goto fini;
325
    if (g->self_id == SSG_MEMBER_ID_INVALID)
326
    {
327
        /* if unable to resolve my rank within the group, error out */
328 329 330 331 332
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }

Shane Snyder's avatar
Shane Snyder committed
333 334 335
    /* generate random list permutation of the member map */
    ssg_gen_rand_member_list(g);

336
    /* initialize swim failure detector */
337 338
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
339 340
    swim_group_mgmt_callbacks_t swim_callbacks = {
        .get_dping_target = &ssg_get_swim_dping_target,
Shane Snyder's avatar
Shane Snyder committed
341 342 343
        .get_iping_targets = &ssg_get_swim_iping_targets,
        .get_member_addr = ssg_get_swim_member_addr,
        .get_member_state = ssg_get_swim_member_state,
344
        .apply_member_update = ssg_apply_swim_member_update,
345
    };
346 347
    g->swim_ctx = swim_init(ssg_inst->mid, g, (swim_member_id_t)g->self_id,
        swim_callbacks, 1);
Shane Snyder's avatar
Shane Snyder committed
348
    if (g->swim_ctx == NULL) goto fini;
349

Shane Snyder's avatar
Shane Snyder committed
350 351 352
    /* everything successful -- set the output group identifier, which is just
     * an opaque pointer to the group descriptor structure
     */
353 354
    group_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
    if (group_id == SSG_GROUP_ID_NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
355

356 357 358 359
    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->group_table, descriptor->name_hash,
        sizeof(uint64_t), g);

Shane Snyder's avatar
Shane Snyder committed
360
    SSG_DEBUG(g, "group create successful (size=%d)\n", group_size);
361

362
    /* don't free group pointer on success */
363 364
    g = NULL;
fini:
365
    if (self_addr != HG_ADDR_NULL) margo_addr_free(ssg_inst->mid, self_addr);
366 367
    if (g)
    {
368
        ssg_group_view_destroy(&g->view);
369
        free(g->name);
370 371
        free(g);
    }
372 373
    if (group_id == SSG_GROUP_ID_NULL)
        ssg_group_descriptor_free(tmp_descriptor);
374

Shane Snyder's avatar
Shane Snyder committed
375
    return group_id;
376 377
}

Shane Snyder's avatar
Shane Snyder committed
378
ssg_group_id_t ssg_group_create_config(
379
    const char * group_name,
380 381 382
    const char * file_name,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
383 384 385 386
{
    int fd;
    struct stat st;
    char *rd_buf = NULL;
387
    ssize_t rd_buf_size;
388
    char *tok;
389 390
    void *addr_str_buf = NULL;
    int addr_str_buf_len = 0, num_addrs = 0;
391 392
    int ret;
    const char **addr_strs = NULL;
Shane Snyder's avatar
Shane Snyder committed
393
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

    /* open config file for reading */
    fd = open(file_name, O_RDONLY);
    if (fd == -1)
    {
        fprintf(stderr, "Error: SSG unable to open config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }

    /* get file size and allocate a buffer to store it */
    ret = fstat(fd, &st);
    if (ret == -1)
    {
        fprintf(stderr, "Error: SSG unable to stat config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
    rd_buf = malloc(st.st_size+1);
    if (rd_buf == NULL) goto fini;

    /* load it all in one fell swoop */
416 417
    rd_buf_size = read(fd, rd_buf, st.st_size);
    if (rd_buf_size != st.st_size)
418 419 420 421 422
    {
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
423
    rd_buf[rd_buf_size]='\0';
424 425 426 427 428

    /* strtok the result - each space-delimited address is assumed to be
     * a unique mercury address
     */
    tok = strtok(rd_buf, "\r\n\t ");
Jonathan Jenkins's avatar
Jonathan Jenkins committed
429 430
    if (tok == NULL) goto fini;

431
    /* build up the address buffer */
432 433
    addr_str_buf = malloc(rd_buf_size);
    if (addr_str_buf == NULL) goto fini;
434 435
    do
    {
436 437 438
        int tok_size = strlen(tok);
        memcpy((char*)addr_str_buf + addr_str_buf_len, tok, tok_size+1);
        addr_str_buf_len += tok_size+1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
439 440 441
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
442
    if (addr_str_buf_len != rd_buf_size)
443 444
    {
        /* adjust buffer size if our initial guess was wrong */
445
        void *tmp = realloc(addr_str_buf, addr_str_buf_len);
446
        if (tmp == NULL) goto fini;
447
        addr_str_buf = tmp;
448
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
449

450
    /* set up address string array for group members */
451
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, num_addrs);
452
    if (!addr_strs) goto fini;
453

454
    /* invoke the generic group create routine using our list of addrs */
455 456
    group_id = ssg_group_create(group_name, addr_strs, num_addrs,
        update_cb, update_cb_dat);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
457 458

fini:
459
    /* cleanup before returning */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
460
    if (fd != -1) close(fd);
461
    free(rd_buf);
462
    free(addr_str_buf);
463 464
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
465
    return group_id;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
466 467
}

Shane Snyder's avatar
Shane Snyder committed
468
#ifdef SSG_HAVE_MPI
Shane Snyder's avatar
Shane Snyder committed
469
ssg_group_id_t ssg_group_create_mpi(
470
    const char * group_name,
471 472 473
    MPI_Comm comm,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
474
{
475
    int i;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
476
    hg_addr_t self_addr = HG_ADDR_NULL;
477
    char *self_addr_str = NULL;
478 479
    hg_size_t self_addr_str_size = 0;
    int self_addr_str_size_int = 0; /* for mpi-friendly conversion */
480
    char *addr_str_buf = NULL;
481 482 483 484
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
485
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
486
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
487

488 489
    if (!ssg_inst) goto fini;

490
    /* get my address */
491
    hret = margo_addr_self(ssg_inst->mid, &self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
492
    if (hret != HG_SUCCESS) goto fini;
493
    hret = margo_addr_to_string(ssg_inst->mid, NULL, &self_addr_str_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
494
    if (hret != HG_SUCCESS) goto fini;
495
    self_addr_str = malloc(self_addr_str_size);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
496
    if (self_addr_str == NULL) goto fini;
497
    hret = margo_addr_to_string(ssg_inst->mid, self_addr_str, &self_addr_str_size, self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
498
    if (hret != HG_SUCCESS) goto fini;
499
    self_addr_str_size_int = (int)self_addr_str_size; /* null char included in call */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
500

501
    /* gather the buffer sizes */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
502 503 504 505
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
506
    sizes[comm_rank] = self_addr_str_size_int;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
507 508
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

509 510 511
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
     */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
512 513 514
    sizes_psum = malloc((comm_size+1) * sizeof(*sizes_psum));
    if (sizes_psum == NULL) goto fini;
    sizes_psum[0] = 0;
515
    for (i = 1; i < comm_size+1; i++)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
516 517
        sizes_psum[i] = sizes_psum[i-1] + sizes[i-1];

518
    /* allgather the addresses */
519 520
    addr_str_buf = malloc(sizes_psum[comm_size]);
    if (addr_str_buf == NULL) goto fini;
521
    MPI_Allgatherv(self_addr_str, self_addr_str_size_int, MPI_BYTE,
522
            addr_str_buf, sizes, sizes_psum, MPI_BYTE, comm);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
523

524
    /* set up address string array for group members */
525
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, comm_size);
526 527 528
    if (!addr_strs) goto fini;

    /* invoke the generic group create routine using our list of addrs */
529 530
    group_id = ssg_group_create(group_name, addr_strs, comm_size,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
531 532

fini:
533
    /* cleanup before returning */
534
    if (self_addr != HG_ADDR_NULL) margo_addr_free(ssg_inst->mid, self_addr);
535
    free(self_addr_str);
536 537 538
    free(sizes);
    free(sizes_psum);
    free(addr_str_buf);
539 540
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
541
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
542 543 544
}
#endif

545 546
int ssg_group_destroy(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
547
{
Shane Snyder's avatar
Shane Snyder committed
548
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
549
    ssg_group_t *g;
Shane Snyder's avatar
Shane Snyder committed
550

Shane Snyder's avatar
Shane Snyder committed
551
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
552
        return SSG_FAILURE;
Shane Snyder's avatar
Shane Snyder committed
553

554 555 556 557 558 559
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG unable to destroy a group it is not a member of\n");
        return SSG_FAILURE;
    }

560
    /* find the group structure and destroy it */
Shane Snyder's avatar
Shane Snyder committed
561 562
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
563 564 565 566 567
    if (!g)
    {
        fprintf(stderr, "Error: SSG unable to find expected group reference\n");
        return SSG_FAILURE;
    }
568
    HASH_DELETE(hh, ssg_inst->group_table, g);
569
    ssg_group_destroy_internal(g);
Shane Snyder's avatar
Shane Snyder committed
570

571
    return SSG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
572 573
}

574 575 576
int ssg_group_attach(
    ssg_group_id_t group_id)
{
Shane Snyder's avatar
Shane Snyder committed
577
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
578 579 580 581 582 583
    ssg_attached_group_t *ag = NULL;
    char *group_name = NULL;
    int group_size;
    void *view_buf = NULL;
    const char **addr_strs = NULL;
    int sret = SSG_FAILURE;
Shane Snyder's avatar
Shane Snyder committed
584

585
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
586

587 588 589 590 591 592 593 594 595 596 597
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG unable to attach a group it is a member of\n");
        goto fini;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        fprintf(stderr, "Error: SSG unable to attach a group it is" \
            " already attached to\n");
        goto fini;
    }
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    /* send the attach request to a group member to initiate a bulk transfer
     * of the group's membership view
     */
    sret = ssg_group_attach_send(group_descriptor, &group_name,
        &group_size, &view_buf);
    if (sret != SSG_SUCCESS || !group_name || !view_buf) goto fini;

    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(view_buf, group_size);
    if (!addr_strs) goto fini;

    /* allocate an SSG attached group data structure and initialize some of it */
    ag = malloc(sizeof(*ag));
    if (!ag) goto fini;
    memset(ag, 0, sizeof(*ag));
    ag->name = group_name;
    ag->descriptor = ssg_group_descriptor_dup(group_descriptor);
    if (!ag->descriptor) goto fini;
    ag->descriptor->owner_status = SSG_OWNER_IS_ATTACHER;

    /* create the view for the group */
620 621
    sret = ssg_group_view_create(addr_strs, group_size, HG_ADDR_NULL,
        NULL, &ag->view);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    if (sret != SSG_SUCCESS) goto fini;

    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->attached_group_table, descriptor->name_hash,
        sizeof(uint64_t), ag);

    sret = SSG_SUCCESS;

    /* don't free on success */
    ag = NULL;
fini:
    free(view_buf);
    free(addr_strs);
    if (ag)
    {
637
        ssg_group_view_destroy(&ag->view);
638 639
        free(ag->name);
        free(ag);
640
        ssg_group_descriptor_free(ag->descriptor);
641 642 643
    }

    return sret;
644 645 646 647 648
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_attached_group_t *ag;

    if (group_descriptor->owner_status != SSG_OWNER_IS_ATTACHER)
    {
        fprintf(stderr, "Error: SSG unable to detach from group that" \
            " was never attached\n");
        return SSG_FAILURE;
    }

    /* find the attached group structure and destroy it */
    HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), ag);
    if (!ag)
    {
        fprintf(stderr, "Error: SSG unable to find expected attached group reference\n");
        return SSG_FAILURE;
    }
    HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
    ssg_attached_group_destroy(ag);

670 671 672
    return SSG_SUCCESS;
}

673 674 675
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
676

Shane Snyder's avatar
Shane Snyder committed
677
ssg_member_id_t ssg_get_group_self_id(
678
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
679
{
Shane Snyder's avatar
Shane Snyder committed
680
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
Shane Snyder's avatar
Shane Snyder committed
681 682
    ssg_group_t *g;

Shane Snyder's avatar
Shane Snyder committed
683
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
684
        return SSG_MEMBER_ID_INVALID;
Shane Snyder's avatar
Shane Snyder committed
685

686 687 688 689
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG can only obtain a self ID from a group the" \
            " caller is a member of\n");
690
        return SSG_MEMBER_ID_INVALID;
691 692
    }

Shane Snyder's avatar
Shane Snyder committed
693 694
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
Shane Snyder's avatar
Shane Snyder committed
695
    if (!g)
696
        return SSG_MEMBER_ID_INVALID;
Shane Snyder's avatar
Shane Snyder committed
697 698

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
699 700
}

701 702
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
703
{
Shane Snyder's avatar
Shane Snyder committed
704
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
705
    ssg_group_view_t *group_view = NULL;
Shane Snyder's avatar
Shane Snyder committed
706

Shane Snyder's avatar
Shane Snyder committed
707
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
Shane Snyder's avatar
Shane Snyder committed
708 709
        return 0;

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
            group_view = &g->view;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
            group_view = &ag->view;
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain size of groups that the caller" \
            " is a member of or an attacher of\n");
Shane Snyder's avatar
Shane Snyder committed
732
        return 0;
733
    }
Shane Snyder's avatar
Shane Snyder committed
734

735 736 737 738
    if (group_view)
        return group_view->size;
    else
        return 0;
Shane Snyder's avatar
Shane Snyder committed
739 740
}

741 742 743
hg_addr_t ssg_get_addr(
    ssg_group_id_t group_id,
    ssg_member_id_t member_id)
Shane Snyder's avatar
Shane Snyder committed
744
{
Shane Snyder's avatar
Shane Snyder committed
745
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
746
    ssg_group_view_t *group_view = NULL;
747
    ssg_member_state_t *member_state;
Shane Snyder's avatar
Shane Snyder committed
748

749 750
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL ||
            member_id == SSG_MEMBER_ID_INVALID)
Shane Snyder's avatar
Shane Snyder committed
751 752
        return HG_ADDR_NULL;

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
            group_view = &g->view;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
            group_view = &ag->view;
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain member addresses of groups" \
            " that the caller is a member of or an attacher of\n");
        return HG_ADDR_NULL;
    }

    if (group_view)
    {
780 781 782 783 784
        HASH_FIND(hh, group_view->member_map, &member_id, sizeof(ssg_member_id_t),
            member_state);
        if (member_state)
            return member_state->addr;
        else
785 786 787
            return HG_ADDR_NULL;
    }
    else
Shane Snyder's avatar
Shane Snyder committed
788
        return HG_ADDR_NULL;
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
}

ssg_group_id_t ssg_group_id_dup(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *dup;

    dup = ssg_group_descriptor_dup((ssg_group_descriptor_t *)group_id);
    return (ssg_group_id_t)dup;
}

void ssg_group_id_free(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *descriptor = (ssg_group_descriptor_t *)group_id;

    ssg_group_descriptor_free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
806
    descriptor = SSG_GROUP_ID_NULL;
807 808 809
    return;
}

810 811 812 813 814 815 816 817 818
char *ssg_group_id_get_addr_str(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;

    return strdup(group_descriptor->addr_str);
}


819 820 821 822 823 824 825
void ssg_group_id_serialize(
    ssg_group_id_t group_id,
    char ** buf_p,
    size_t * buf_size_p)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    size_t alloc_size;
826
    char *gid_buf, *p; 
827 828 829 830 831 832 833 834 835 836 837 838 839

    *buf_p = NULL;
    *buf_size_p = 0;

    /* determine needed buffer size */
    alloc_size = (sizeof(group_descriptor->magic_nr) + sizeof(group_descriptor->name_hash) +
        strlen(group_descriptor->addr_str) + 1);

    gid_buf = malloc(alloc_size);
    if (!gid_buf)
        return;

    /* serialize */
840 841 842 843 844 845
    p = gid_buf;
    *(uint64_t *)p = group_descriptor->magic_nr;
    p += sizeof(uint64_t);
    *(uint64_t *)p = group_descriptor->name_hash;
    p += sizeof(uint64_t);
    strcpy(p, group_descriptor->addr_str);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    /* the rest of the descriptor is stateful and not appropriate for serializing... */

    *buf_p = gid_buf;
    *buf_size_p = alloc_size;

    return;
}

void ssg_group_id_deserialize(
    const char * buf,
    size_t buf_size,
    ssg_group_id_t * group_id_p)
{
    size_t min_buf_size;
    uint64_t magic_nr;
    uint64_t name_hash;
    const char *addr_str;
    ssg_group_descriptor_t *group_descriptor;

    *group_id_p = SSG_GROUP_ID_NULL;

    /* check to ensure the buffer contains enough data to make a group ID */
    min_buf_size = (sizeof(group_descriptor->magic_nr) +
        sizeof(group_descriptor->name_hash) + 1);
    if (buf_size < min_buf_size)
    {
        fprintf(stderr, "Error: Serialized buffer does not contain a valid SSG group ID\n");
        return;
    }

    /* deserialize */
    magic_nr = *(uint64_t *)buf;
    if (magic_nr != SSG_MAGIC_NR)
    {
        fprintf(stderr, "Error: Magic number mismatch when deserializing SSG group ID\n");
        return;
    }
    buf += sizeof(uint64_t);
    name_hash = *(uint64_t *)buf;
    buf += sizeof(uint64_t);
    addr_str = buf;

    group_descriptor = ssg_group_descriptor_create(name_hash, addr_str,
        SSG_OWNER_IS_UNASSOCIATED);
    if (!group_descriptor)
        return;

    *group_id_p = (ssg_group_id_t)group_descriptor;

    return;
}

int ssg_group_id_store(
    const char * file_name,
    ssg_group_id_t group_id)
{
    int fd;
    char *buf;
    size_t buf_size;
    ssize_t bytes_written;

    fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0)
    {
        fprintf(stderr, "Error: Unable to open file %s for storing SSG group ID\n",
            file_name);
        return SSG_FAILURE;
    }

    ssg_group_id_serialize(group_id, &buf, &buf_size);
    if (buf == NULL)
    {
        fprintf(stderr, "Error: Unable to serialize SSG group ID.\n");
        close(fd);
        return SSG_FAILURE;
    }

    bytes_written = write(fd, buf, buf_size);
    if (bytes_written != (ssize_t)buf_size)
    {
        fprintf(stderr, "Error: Unable to write SSG group ID to file %s\n", file_name);
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    close(fd);
    free(buf);
    return SSG_SUCCESS;
}


int ssg_group_id_load(
    const char * file_name,
    ssg_group_id_t * group_id_p)
{
    int fd;
    struct stat fstats;
    char *buf;
    ssize_t bytes_read;
    int ret;

    *group_id_p = SSG_GROUP_ID_NULL;

    fd = open(file_name, O_RDONLY);
    if (fd < 0)
    {
        fprintf(stderr, "Error: Unable to open file %s for loading SSG group ID\n",
            file_name);
        return SSG_FAILURE;
    }

    ret = fstat(fd, &fstats);
    if (ret != 0)
    {
        fprintf(stderr, "Error: Unable to stat file %s\n", file_name);
        close(fd);
        return SSG_FAILURE;
    }
    if (fstats.st_size == 0)
    {
        fprintf(stderr, "Error: SSG group ID file %s is empty\n", file_name);
        close(fd);
        return SSG_FAILURE;
    }

    buf = malloc(fstats.st_size);
    if (buf == NULL)
    {
        close(fd);
        return SSG_FAILURE;
    }

    bytes_read = read(fd, buf, fstats.st_size);
    if (bytes_read != (ssize_t)fstats.st_size)
    {
        fprintf(stderr, "Error: Unable to read SSG group ID from file %s\n", file_name);
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    ssg_group_id_deserialize(buf, (size_t)bytes_read, group_id_p);
    if (*group_id_p == SSG_GROUP_ID_NULL)
    {
        fprintf(stderr, "Error: Unable to deserialize SSG group ID\n");
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    close(fd);
    free(buf);
    return SSG_SUCCESS;
}

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
void ssg_group_dump(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_group_view_t *group_view = NULL;
    char *group_name = NULL;
    char group_role[32];
    char group_self_id[32];

    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
        {
            group_view = &g->view;
            group_name = g->name;
            strcpy(group_role, "member");
1022
            sprintf(group_self_id, "%lu", g->self_id);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        }
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
        {
            group_view = &ag->view;
            group_name = ag->name;
            strcpy(group_role, "attacher");
        }
    }
    else
    {
        fprintf(stderr, "Error: SSG can only dump membership information for" \
            " groups that the caller is a member of or an attacher of\n");
        return;
    }

    if (group_view)
    {
1047
        ssg_member_state_t *member_state, *tmp_ms;
1048 1049 1050 1051 1052

        printf("SSG membership information for group '%s':\n", group_name);
        printf("\trole: '%s'\n", group_role);
        if (strcmp(group_role, "member") == 0)
            printf("\tself_id: %s\n", group_self_id);
Shane Snyder's avatar
Shane Snyder committed
1053
        printf("\tsize: %d\n", group_view->size);
1054
        printf("\tview:\n");
1055
        HASH_ITER(hh, group_view->member_map, member_state, tmp_ms)
1056
        {
Shane Snyder's avatar
Shane Snyder committed
1057
            printf("\t\tid: %20lu\taddr: %s\n", member_state->id,
1058
                member_state->addr_str);
1059
        }
1060 1061 1062 1063
    }
    else
        fprintf(stderr, "Error: SSG unable to find group view associated" \
            " with the given group ID\n");
Shane Snyder's avatar
Shane Snyder committed
1064

1065
    return;
Shane Snyder's avatar
Shane Snyder committed
1066 1067
}

1068 1069 1070
/************************************
 *** SSG internal helper routines ***
 ************************************/
1071

1072
static ssg_group_descriptor_t * ssg_group_descriptor_create(
1073
    uint64_t name_hash, const char * leader_addr_str, int owner_status)
1074
{
1075
    ssg_group_descriptor_t *descriptor;
1076

1077 1078
    descriptor = malloc(sizeof(*descriptor));
    if (!descriptor) return NULL;
Shane Snyder's avatar
Shane Snyder committed
1079

1080
    /* hash the group name to obtain an 64-bit unique ID */
1081
    descriptor->magic_nr = SSG_MAGIC_NR;
1082
    descriptor->name_hash = name_hash;
1083 1084
    descriptor->addr_str = strdup(leader_addr_str);
    if (!descriptor->addr_str)
Shane Snyder's avatar
Shane Snyder committed
1085
    {
1086
        free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
1087 1088
        return NULL;
    }
1089
    descriptor->owner_status = owner_status;
1090
    descriptor->ref_count = 1;
1091 1092
    return descriptor;
}
1093