ssg.c 45.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>
17
#include <linux/limits.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
18
#include <assert.h>
Shane Snyder's avatar
Shane Snyder committed
19
#ifdef SSG_HAVE_MPI
20 21
#include <mpi.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
22

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

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

33 34 35
/* arguments for group lookup ULTs */
struct ssg_group_lookup_ult_args
{
36
    const char *addr_str;
37
    ssg_group_view_t *view;
Shane Snyder's avatar
Shane Snyder committed
38
    ABT_rwlock lock;
39
    int out;
40 41 42
};
static void ssg_group_lookup_ult(void * arg);

43
/* SSG helper routine prototypes */
Shane Snyder's avatar
Shane Snyder committed
44 45 46 47 48 49 50
static ssg_group_t * ssg_group_create_internal(
    const char * group_name, const char * const group_addr_strs[],
    int group_size, ssg_membership_update_cb update_cb, void *update_cb_dat);
static int ssg_group_view_create(
    const char * const group_addr_strs[], int group_size,
    const char * self_addr_str, ABT_rwlock view_lock,
    ssg_group_view_t * view, ssg_member_id_t * self_id);
51 52
static ssg_member_state_t * ssg_group_view_add_member(
    const char * addr_str, ssg_group_view_t * view, ABT_rwlock lock);
53
static ssg_group_descriptor_t * ssg_group_descriptor_create(
54
    uint64_t name_hash, const char * leader_addr_str, int owner_status);
55 56 57 58 59 60
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor);
static void ssg_group_destroy_internal(
    ssg_group_t * g);
static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag);
Shane Snyder's avatar
Shane Snyder committed
61 62 63 64 65 66
static void ssg_group_view_destroy(
    ssg_group_view_t * view);
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor);
static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str);
67 68
static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs);
Shane Snyder's avatar
Shane Snyder committed
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
static void ssg_shuffle_member_list(
    ssg_group_target_list_t *list);

/* SWIM group management routine prototypes */
static int ssg_get_swim_dping_target(
    void *group_data,
    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 dping_target_id,
    int *num_targets,
    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,
    swim_member_state_t **state);
static void ssg_apply_swim_member_update(
    void *group_data,
    swim_member_update_t update);
95

96 97
/* XXX: i think we ultimately need per-mid ssg instances rather than 1 global? */
ssg_instance_t *ssg_inst = NULL;
98

99 100 101
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
102

103 104
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
105
{
106 107
    struct timespec ts;

108 109 110 111 112 113 114 115 116 117
    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;

118
    ssg_register_rpcs();
119

120 121 122 123
    /* seed RNG */
    clock_gettime(CLOCK_MONOTONIC, &ts);
    srand(ts.tv_nsec + getpid());

124 125
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
126

127
int ssg_finalize()
128
{
129 130
    ssg_group_t *g, *g_tmp;
    ssg_attached_group_t *ag, *ag_tmp;
131 132 133 134 135

    if (!ssg_inst)
        return SSG_FAILURE;

    /* destroy all active groups */
136
    HASH_ITER(hh, ssg_inst->group_table, g, g_tmp)
137 138 139 140 141
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ssg_group_destroy_internal(g);
    }

142 143 144 145 146 147 148
    /* 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);
    }

149 150 151
    free(ssg_inst);
    ssg_inst = NULL;

152
    return SSG_SUCCESS;
153
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
154

155 156 157
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
158

Shane Snyder's avatar
Shane Snyder committed
159 160 161 162 163 164
ssg_group_id_t ssg_group_create(
    const char * group_name,
    const char * const group_addr_strs[],
    int group_size,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
165
{
Shane Snyder's avatar
Shane Snyder committed
166 167
    ssg_group_t *g;
    ssg_group_id_t g_id = SSG_GROUP_ID_NULL;
168

Shane Snyder's avatar
Shane Snyder committed
169 170 171
    g = ssg_group_create_internal(group_name, group_addr_strs,
            group_size, update_cb, update_cb_dat);
    if (g)
172
    {
Shane Snyder's avatar
Shane Snyder committed
173 174 175 176 177 178
        /* on successful creation, dup the group descriptor and return
         * it for the caller to hold on to
         */
        g_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
        if (g_id == SSG_GROUP_ID_NULL)
            ssg_group_destroy_internal(g);
179
    }
Shane Snyder's avatar
Shane Snyder committed
180 181

    return g_id;
182
}
Shane Snyder's avatar
Shane Snyder committed
183

Shane Snyder's avatar
Shane Snyder committed
184 185 186 187 188
ssg_group_id_t ssg_group_create_config(
    const char * group_name,
    const char * file_name,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
189
{
Shane Snyder's avatar
Shane Snyder committed
190 191 192 193 194 195 196 197 198 199
    int fd;
    struct stat st;
    char *rd_buf = NULL;
    ssize_t rd_buf_size;
    char *tok;
    void *addr_str_buf = NULL;
    int addr_str_buf_len = 0, num_addrs = 0;
    int ret;
    const char **addr_strs = NULL;
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
200

Shane Snyder's avatar
Shane Snyder committed
201 202 203 204 205 206 207 208
    /* 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;
    }
209

Shane Snyder's avatar
Shane Snyder committed
210 211 212
    /* get file size and allocate a buffer to store it */
    ret = fstat(fd, &st);
    if (ret == -1)
Shane Snyder's avatar
Shane Snyder committed
213
    {
Shane Snyder's avatar
Shane Snyder committed
214 215 216
        fprintf(stderr, "Error: SSG unable to stat config file %s for group %s\n",
            file_name, group_name);
        goto fini;
Shane Snyder's avatar
Shane Snyder committed
217
    }
Shane Snyder's avatar
Shane Snyder committed
218 219
    rd_buf = malloc(st.st_size+1);
    if (rd_buf == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
220

Shane Snyder's avatar
Shane Snyder committed
221 222 223
    /* load it all in one fell swoop */
    rd_buf_size = read(fd, rd_buf, st.st_size);
    if (rd_buf_size != st.st_size)
Shane Snyder's avatar
Shane Snyder committed
224
    {
Shane Snyder's avatar
Shane Snyder committed
225 226 227
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
Shane Snyder's avatar
Shane Snyder committed
228
    }
Shane Snyder's avatar
Shane Snyder committed
229 230 231 232 233 234 235
    rd_buf[rd_buf_size]='\0';

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

Shane Snyder's avatar
Shane Snyder committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    /* build up the address buffer */
    addr_str_buf = malloc(rd_buf_size);
    if (addr_str_buf == NULL) goto fini;
    do
    {
        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;
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
    if (addr_str_buf_len != rd_buf_size)
    {
        /* adjust buffer size if our initial guess was wrong */
        void *tmp = realloc(addr_str_buf, addr_str_buf_len);
        if (tmp == NULL) goto fini;
        addr_str_buf = tmp;
    }
Shane Snyder's avatar
Shane Snyder committed
255

Shane Snyder's avatar
Shane Snyder committed
256 257 258
    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, num_addrs);
    if (!addr_strs) goto fini;
Shane Snyder's avatar
Shane Snyder committed
259

Shane Snyder's avatar
Shane Snyder committed
260 261 262
    /* invoke the generic group create routine using our list of addrs */
    group_id = ssg_group_create(group_name, addr_strs, num_addrs,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
263

Shane Snyder's avatar
Shane Snyder committed
264 265 266 267 268 269
fini:
    /* cleanup before returning */
    if (fd != -1) close(fd);
    free(rd_buf);
    free(addr_str_buf);
    free(addr_strs);
Shane Snyder's avatar
Shane Snyder committed
270

Shane Snyder's avatar
Shane Snyder committed
271
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
272 273
}

Shane Snyder's avatar
Shane Snyder committed
274 275 276 277 278 279
#ifdef SSG_HAVE_MPI
ssg_group_id_t ssg_group_create_mpi(
    const char * group_name,
    MPI_Comm comm,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
Shane Snyder's avatar
Shane Snyder committed
280
{
Shane Snyder's avatar
Shane Snyder committed
281 282
    int i;
    char *self_addr_str = NULL;
283
    int self_addr_str_size = 0;
Shane Snyder's avatar
Shane Snyder committed
284 285 286 287 288 289
    char *addr_str_buf = NULL;
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
Shane Snyder's avatar
Shane Snyder committed
290

Shane Snyder's avatar
Shane Snyder committed
291
    if (!ssg_inst) goto fini;
Shane Snyder's avatar
Shane Snyder committed
292

Shane Snyder's avatar
Shane Snyder committed
293
    /* get my address */
294
    SSG_GET_SELF_ADDR_STR(ssg_inst->mid, self_addr_str);
Shane Snyder's avatar
Shane Snyder committed
295
    if (self_addr_str == NULL) goto fini;
296
    self_addr_str_size = (int)strlen(self_addr_str) + 1;
297

Shane Snyder's avatar
Shane Snyder committed
298 299 300 301 302
    /* gather the buffer sizes */
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
303
    sizes[comm_rank] = self_addr_str_size;
Shane Snyder's avatar
Shane Snyder committed
304
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);
305

Shane Snyder's avatar
Shane Snyder committed
306 307
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
308
     */
Shane Snyder's avatar
Shane Snyder committed
309 310 311 312 313
    sizes_psum = malloc((comm_size+1) * sizeof(*sizes_psum));
    if (sizes_psum == NULL) goto fini;
    sizes_psum[0] = 0;
    for (i = 1; i < comm_size+1; i++)
        sizes_psum[i] = sizes_psum[i-1] + sizes[i-1];
314

Shane Snyder's avatar
Shane Snyder committed
315 316 317
    /* allgather the addresses */
    addr_str_buf = malloc(sizes_psum[comm_size]);
    if (addr_str_buf == NULL) goto fini;
318
    MPI_Allgatherv(self_addr_str, self_addr_str_size, MPI_BYTE,
Shane Snyder's avatar
Shane Snyder committed
319
            addr_str_buf, sizes, sizes_psum, MPI_BYTE, comm);
Shane Snyder's avatar
Shane Snyder committed
320

Shane Snyder's avatar
Shane Snyder committed
321 322 323
    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, comm_size);
    if (!addr_strs) goto fini;
324

Shane Snyder's avatar
Shane Snyder committed
325 326 327
    /* invoke the generic group create routine using our list of addrs */
    group_id = ssg_group_create(group_name, addr_strs, comm_size,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
328

Shane Snyder's avatar
Shane Snyder committed
329 330 331 332 333 334 335
fini:
    /* cleanup before returning */
    free(self_addr_str);
    free(sizes);
    free(sizes_psum);
    free(addr_str_buf);
    free(addr_strs);
Shane Snyder's avatar
Shane Snyder committed
336

Shane Snyder's avatar
Shane Snyder committed
337
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
338
}
Shane Snyder's avatar
Shane Snyder committed
339
#endif
Shane Snyder's avatar
Shane Snyder committed
340

Shane Snyder's avatar
Shane Snyder committed
341 342
int ssg_group_destroy(
    ssg_group_id_t group_id)
343
{
Shane Snyder's avatar
Shane Snyder committed
344 345
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_group_t *g;
346

Shane Snyder's avatar
Shane Snyder committed
347
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_FAILURE;
348

Shane Snyder's avatar
Shane Snyder committed
349
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
350
    {
Shane Snyder's avatar
Shane Snyder committed
351 352
        fprintf(stderr, "Error: SSG unable to destroy a group it is not a member of\n");
        return SSG_FAILURE;
353
    }
354

Shane Snyder's avatar
Shane Snyder committed
355 356 357 358
    /* find the group structure and destroy it */
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
    if (!g)
359
    {
Shane Snyder's avatar
Shane Snyder committed
360 361
        fprintf(stderr, "Error: SSG unable to find expected group reference\n");
        return SSG_FAILURE;
362
    }
Shane Snyder's avatar
Shane Snyder committed
363
    ssg_group_destroy_internal(g);
364

Shane Snyder's avatar
Shane Snyder committed
365
    return SSG_SUCCESS;
366 367
}

Shane Snyder's avatar
Shane Snyder committed
368 369
ssg_group_id_t ssg_group_join(
    ssg_group_id_t in_group_id,
370 371
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
372
{
Shane Snyder's avatar
Shane Snyder committed
373 374 375 376 377 378
    ssg_group_descriptor_t *in_group_descriptor = (ssg_group_descriptor_t *)in_group_id;
    char *self_addr_str = NULL;
    char *group_name = NULL;
    int group_size;
    void *view_buf = NULL;
    const char **addr_strs = NULL;
379
    int sret;
Shane Snyder's avatar
Shane Snyder committed
380 381
    ssg_group_t *g = NULL;
    ssg_group_id_t g_id = SSG_GROUP_ID_NULL;
382

Shane Snyder's avatar
Shane Snyder committed
383
    if (!ssg_inst || in_group_id == SSG_GROUP_ID_NULL) goto fini;
384

Shane Snyder's avatar
Shane Snyder committed
385
    if (in_group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
386
    {
Shane Snyder's avatar
Shane Snyder committed
387
        fprintf(stderr, "Error: SSG unable to join a group it is already a member of\n");
388 389
        goto fini;
    }
Shane Snyder's avatar
Shane Snyder committed
390
    else if (in_group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
391
    {
Shane Snyder's avatar
Shane Snyder committed
392
        fprintf(stderr, "Error: SSG unable to join a group it is attached to\n");
393 394
        goto fini;
    }
395

Shane Snyder's avatar
Shane Snyder committed
396
    /* get my address string */
397
    SSG_GET_SELF_ADDR_STR(ssg_inst->mid, self_addr_str);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
398 399
    if (self_addr_str == NULL) goto fini;

Shane Snyder's avatar
Shane Snyder committed
400 401 402
    sret = ssg_group_join_send(in_group_descriptor, &group_name,
        &group_size, &view_buf);
    if (sret != SSG_SUCCESS || !group_name || !view_buf) goto fini;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
403

Shane Snyder's avatar
Shane Snyder committed
404 405
    /* set up address string array for all group members */
    addr_strs = ssg_addr_str_buf_to_list(view_buf, group_size);
406 407
    if (!addr_strs) goto fini;

Shane Snyder's avatar
Shane Snyder committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    /* append self address string to list of group member address strings */
    addr_strs = realloc(addr_strs, (group_size+1)*sizeof(char *));
    if(!addr_strs) goto fini;
    addr_strs[group_size++] = self_addr_str;

    g = ssg_group_create_internal(group_name, addr_strs, group_size,
            update_cb, update_cb_dat);
    if (g)
    {
        /* on successful creation, dup the group descriptor and return
         * it for the caller to hold on to
         */
        g_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
        if (g_id == SSG_GROUP_ID_NULL) goto fini;
    }
Shane Snyder's avatar
Shane Snyder committed
423

Shane Snyder's avatar
Shane Snyder committed
424 425 426
    /* don't free on success */
    group_name = NULL;
    g = NULL;
Shane Snyder's avatar
Shane Snyder committed
427
fini:
Shane Snyder's avatar
Shane Snyder committed
428 429 430 431
    if (g) ssg_group_destroy_internal(g);
    free(addr_strs);
    free(view_buf);
    free(group_name);
432
    free(self_addr_str);
433

Shane Snyder's avatar
Shane Snyder committed
434
    return g_id;
Shane Snyder's avatar
Shane Snyder committed
435 436
}

Shane Snyder's avatar
Shane Snyder committed
437
int ssg_group_leave(
438
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
439
{
Shane Snyder's avatar
Shane Snyder committed
440
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
441

Shane Snyder's avatar
Shane Snyder committed
442
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_FAILURE;
443

444 445
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
Shane Snyder's avatar
Shane Snyder committed
446
        fprintf(stderr, "Error: SSG unable to leave group it is not a member of\n");
447 448 449 450
        return SSG_FAILURE;
    }

    return SSG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
451 452
}

453 454 455
int ssg_group_attach(
    ssg_group_id_t group_id)
{
Shane Snyder's avatar
Shane Snyder committed
456
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
457 458 459 460 461 462
    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;
463

464
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
465

466 467 468 469 470 471 472 473 474 475 476
    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;
    }
477

478 479 480 481 482 483 484
    /* 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;

Shane Snyder's avatar
Shane Snyder committed
485
    /* set up address string array for all group members */
486 487 488 489 490 491 492
    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));
Shane Snyder's avatar
Shane Snyder committed
493
    ag->name = strdup(group_name);
494 495 496 497 498
    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 */
Shane Snyder's avatar
Shane Snyder committed
499
    sret = ssg_group_view_create(addr_strs, group_size, NULL, ag->lock, &ag->view, NULL);
500 501 502 503 504 505 506 507 508
    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 */
Shane Snyder's avatar
Shane Snyder committed
509
    group_name = NULL;
510 511
    ag = NULL;
fini:
Shane Snyder's avatar
Shane Snyder committed
512
    if (ag) ssg_attached_group_destroy(ag);
513
    free(addr_strs);
Shane Snyder's avatar
Shane Snyder committed
514 515
    free(view_buf);
    free(group_name);
516 517

    return sret;
518 519 520 521 522
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
523 524 525
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_attached_group_t *ag;

Shane Snyder's avatar
Shane Snyder committed
526 527
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_FAILURE;

528 529 530 531 532 533 534 535 536 537 538 539
    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)
    {
Shane Snyder's avatar
Shane Snyder committed
540
        fprintf(stderr, "Error: SSG unable to find expected group attached\n");
541 542 543 544 545
        return SSG_FAILURE;
    }
    HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
    ssg_attached_group_destroy(ag);

546 547 548
    return SSG_SUCCESS;
}

549 550 551
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
552

553
ssg_member_id_t ssg_get_group_self_id(
554
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
555
{
Shane Snyder's avatar
Shane Snyder committed
556
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
557 558
    ssg_group_t *g;

Shane Snyder's avatar
Shane Snyder committed
559
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return SSG_MEMBER_ID_INVALID;
560

561 562 563 564
    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");
565
        return SSG_MEMBER_ID_INVALID;
566 567
    }

Shane Snyder's avatar
Shane Snyder committed
568 569
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
Shane Snyder's avatar
Shane Snyder committed
570
    if (!g) return SSG_MEMBER_ID_INVALID;
571 572

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
573 574
}

575 576
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
577
{
Shane Snyder's avatar
Shane Snyder committed
578
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
Shane Snyder's avatar
Shane Snyder committed
579
    int group_size = 0;
580

Shane Snyder's avatar
Shane Snyder committed
581
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) return 0;
582

583 584 585 586 587 588 589
    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)
Shane Snyder's avatar
Shane Snyder committed
590 591 592 593 594
        {
            ABT_rwlock_rdlock(g->lock);
            group_size = g->view.size + 1; /* add ourself to view size */
            ABT_rwlock_unlock(g->lock);
        }
595 596 597 598 599 600 601 602
    }
    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)
Shane Snyder's avatar
Shane Snyder committed
603 604 605 606 607
        {
            ABT_rwlock_rdlock(ag->lock);
            group_size = ag->view.size;
            ABT_rwlock_unlock(ag->lock);
        }
608 609 610 611 612
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain size of groups that the caller" \
            " is a member of or an attacher of\n");
613
        return 0;
614
    }
615

Shane Snyder's avatar
Shane Snyder committed
616
    return group_size;
Shane Snyder's avatar
Shane Snyder committed
617 618
}

619 620 621
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
622
{
Shane Snyder's avatar
Shane Snyder committed
623
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
624
    ssg_member_state_t *member_state;
Shane Snyder's avatar
Shane Snyder committed
625
    hg_addr_t member_addr = HG_ADDR_NULL;
626

627 628
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL ||
            member_id == SSG_MEMBER_ID_INVALID)
629 630
        return HG_ADDR_NULL;

631 632 633 634 635 636 637
    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)
Shane Snyder's avatar
Shane Snyder committed
638 639 640 641 642 643 644 645
        {
            ABT_rwlock_rdlock(g->lock);
            HASH_FIND(hh, g->view.member_map, &member_id, sizeof(ssg_member_id_t),
                member_state);
            if (member_state) 
                member_addr = member_state->addr;
            ABT_rwlock_unlock(g->lock);
        }
646 647 648 649 650 651 652 653
    }
    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)
Shane Snyder's avatar
Shane Snyder committed
654 655 656 657 658 659 660 661
        {
            ABT_rwlock_rdlock(ag->lock);
            HASH_FIND(hh, ag->view.member_map, &member_id, sizeof(ssg_member_id_t),
                member_state);
            if (member_state) 
                member_addr = member_state->addr;
            ABT_rwlock_unlock(ag->lock);
        }
662 663 664 665 666 667 668 669
    }
    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;
    }

Shane Snyder's avatar
Shane Snyder committed
670
    return member_addr;
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
}

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
688
    descriptor = SSG_GROUP_ID_NULL;
689 690 691
    return;
}

692 693 694 695 696 697 698 699
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);
}

700 701 702 703 704 705 706
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;
707
    char *gid_buf, *p; 
708 709 710 711 712 713 714 715 716 717 718 719 720

    *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 */
721 722 723 724 725 726
    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);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 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 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 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
    /* 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;
}

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
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");
902
            sprintf(group_self_id, "%lu", g->self_id);
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
        }
    }
    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)
    {
927
        ssg_member_state_t *member_state, *tmp_ms;
928 929 930 931 932

        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);
933
        printf("\tsize: %d\n", group_view->size+1);
934
        printf("\tview:\n");
935
        HASH_ITER(hh, group_view->member_map, member_state, tmp_ms)
936
        {
Shane Snyder's avatar
Shane Snyder committed
937
            printf("\t\tid: %20lu\taddr: %s\n", member_state->id,
938
                member_state->addr_str);
939
        }
940 941 942 943
    }
    else
        fprintf(stderr, "Error: SSG unable to find group view associated" \
            " with the given group ID\n");
944

945
    return;
Shane Snyder's avatar
Shane Snyder committed
946 947
}

948 949 950
/************************************
 *** SSG internal helper routines ***
 ************************************/
951

Shane Snyder's avatar
Shane Snyder committed
952 953 954
static ssg_group_t * ssg_group_create_internal(
    const char * group_name, const char * const group_addr_strs[],
    int group_size, ssg_membership_update_cb update_cb, void *update_cb_dat)
955
{
Shane Snyder's avatar
Shane Snyder committed
956 957 958 959 960 961 962
    uint64_t name_hash;
    char *self_addr_str = NULL;
    ssg_member_state_t *ms, *tmp_ms;
    unsigned int i = 0;
    int sret;
    int success = 0;
    ssg_group_t *g = NULL;
963

Shane Snyder's avatar
Shane Snyder committed
964
    if (!ssg_inst) return NULL;
Shane Snyder's avatar
Shane Snyder committed
965

Shane Snyder's avatar
Shane Snyder committed
966 967 968 969 970 971 972
    name_hash = ssg_hash64_str(group_name);

    /* make sure we aren't re-creating an existing group */
    HASH_FIND(hh, ssg_inst->group_table, &name_hash, sizeof(uint64_t), g);
    if (g) return NULL;

    /* get my address string */
973
    SSG_GET_SELF_ADDR_STR(ssg_inst->mid, self_addr_str);
Shane Snyder's avatar
Shane Snyder committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
    if (self_addr_str == NULL) goto fini;

    /* 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->update_cb = update_cb;
    g->update_cb_dat = update_cb_dat;
    ABT_rwlock_create(&g->lock);

    /* generate unique descriptor for this group  */
    g->descriptor = ssg_group_descriptor_create(name_hash, self_addr_str,
        SSG_OWNER_IS_MEMBER);
    if (g->descriptor == NULL) goto fini;

    /* initialize the group view */
    sret = ssg_group_view_create(group_addr_strs, group_size, self_addr_str,
        g->lock, &g->view, &g->self_id);
    if (sret != SSG_SUCCESS) goto fini;
    if (g->self_id == SSG_MEMBER_ID_INVALID)
Shane Snyder's avatar
Shane Snyder committed
996
    {
Shane Snyder's avatar
Shane Snyder committed
997 998 999 1000
        /* if unable to resolve my rank within the group, error out */
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
Shane Snyder's avatar
Shane Snyder committed
1001
    }
1002

Shane Snyder's avatar
Shane Snyder committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    /* create a list of all target member states and shuffle it */
    g->target_list.targets = malloc(g->view.size * sizeof(*g->target_list.targets));
    if (g->target_list.targets == NULL) goto fini;
    g->target_list.nslots = g->target_list.len = g->view.size;
    g->target_list.dping_ndx = 0;
    HASH_ITER(hh, g->view.member_map, ms, tmp_ms)
    {
        g->target_list.targets[i] = ms;
        i++;
    }
1013

Shane Snyder's avatar
Shane Snyder committed
1014 1015 1016 1017 1018 1019 1020 1021 1022
    /* initialize swim failure detector */
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
    swim_group_mgmt_callbacks_t swim_callbacks = {
        .get_dping_target = &ssg_get_swim_dping_target,
        .get_iping_targets = &ssg_get_swim_iping_targets,
        .get_member_addr = ssg_get_swim_member_addr,
        .get_member_state = ssg_get_swim_member_state,
        .apply_member_update = ssg_apply_swim_member_update,
1023
        .apply_user_updates = ssg_apply_swim_user_updates,
Shane Snyder's avatar
Shane Snyder committed
1024 1025 1026 1027 1028 1029 1030 1031 1032
    };
    g->swim_ctx = swim_init(ssg_inst->mid, g, (swim_member_id_t)g->self_id,
        swim_callbacks, 1);
    if (g->swim_ctx == NULL) goto fini;

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

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
#ifdef DEBUG
    /* set debug output pointer */
    char *dbg_log_dir = getenv("SSG_DEBUG_LOGDIR");
    if (dbg_log_dir)
    {
        char dbg_log_path[PATH_MAX];
        snprintf(dbg_log_path, PATH_MAX, "%s/ssg-%s-%lu.log",
            dbg_log_dir, g->name, g->self_id);
        g->dbg_log = fopen(dbg_log_path, "a");
        if (!g->dbg_log) goto fini;
    }
    else
    {
        g->dbg_log = stdout;
    }
#endif

Shane Snyder's avatar
Shane Snyder committed
1050 1051 1052 1053
    SSG_DEBUG(g, "group create successful (size=%d)\n", group_size);
    success = 1;
fini:
    if (!success && g)
1054
    {
Shane Snyder's avatar
Shane Snyder committed
1055 1056 1057 1058 1059 1060
        if (g->descriptor) ssg_group_descriptor_free(g->descriptor);
        ssg_group_view_destroy(&g->view);
        free(g->target_list.targets);
        free(g->name);
        free(g);
        g = NULL;
1061
    }
Shane Snyder's avatar
Shane Snyder committed
1062 1063 1064
    free(self_addr_str);

    return g;
1065
}
1066

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
int ssg_group_add_member(
    ssg_group_t *g, const char * addr_str)
{
    ssg_member_state_t *new_ms;

    /* group view add member */
    new_ms = ssg_group_view_add_member(addr_str, &g->view, g->lock);
    if (new_ms == NULL) return SSG_FAILURE;

    ABT_rwlock_wrlock(g->lock);

    /* add to target list */
    if (g->target_list.len == g->target_list.nslots)
    {
        /* realloc target list, use fixed incr for now */
        /* XXX constants bad... */
        g->target_list.targets = realloc(g->target_list.targets, 
            (g->target_list.len + 10) * sizeof(*g->target_list.targets));
        if (!g->target_list.targets) return SSG_FAILURE;
        g->target_list.nslots += 10;
    }
    g->target_list.targets[g->target_list.len++] = new_ms;

    SSG_DEBUG(g, "successfully added joining member %lu\n", new_ms->id);

    ABT_rwlock_unlock(g->lock);

    return SSG_SUCCESS;
}

1097
static int ssg_group_view_create(
1098
    const char * const group_addr_strs[], int group_size,
Shane Snyder's avatar
Shane Snyder committed
1099 1100
    const char * self_addr_str, ABT_rwlock view_lock,
    ssg_group_view_t * view, ssg_member_id_t * self_id)
1101 1102
{
    int i, j, r;
1103 1104
    ABT_thread *lookup_ults = NULL;
    struct ssg_group_lookup_ult_args *lookup_ult_args = NULL;
1105 1106 1107
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
    int aret;
1108
    int sret = SSG_FAILURE;
1109

1110 1111 1112
    if (self_id)
        *self_id = SSG_MEMBER_ID_INVALID;
        
Shane Snyder's avatar
Shane Snyder committed
1113
    if ((self_id != NULL && self_addr_str == NULL) || !view) goto fini;
Shane Snyder's avatar
Shane Snyder committed
1114

1115 1116
    /* allocate lookup ULTs */
    lookup_ults = malloc(group_size * sizeof(*lookup_ults));
1117 1118
    if (lookup_ults == NULL) goto fini;
    for (i = 0; i < group_size; i++) lookup_ults[i] = ABT_THREAD_NULL;
1119
    lookup_ult_args = malloc(group_size * sizeof(*lookup_ult_args));
1120
    if (lookup_ult_args == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
1121

Shane Snyder's avatar
Shane Snyder committed
1122
    if(self_addr_str)
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    {
        /* strstr is used here b/c there may be inconsistencies in whether the class
         * is included in the address or not (it should not be in HG_Addr_to_string,
         * but it's possible that it is in the list of group address strings)
         */
        self_addr_substr = strstr(self_addr_str, "://");
        if (self_addr_substr == NULL)
            self_addr_substr = self_addr_str;
        else
            self_addr_substr += 3;
    }

1135
    /* construct view using ULTs to lookup the address of each group member */
Shane Snyder's avatar
Shane Snyder committed
1136
    r = rand() % group_size;
1137 1138 1139 1140 1141
    for (i = 0; i < group_size; i++)
    {
        /* randomize our starting index so all group members aren't looking
         * up other group members in the same order
         */
Shane Snyder's avatar
Shane Snyder committed
1142
        j = (r + i) % group_size;
1143

1144 1145
        if (group_addr_strs[j] == NULL || strlen(group_addr_strs[j]) == 0) continue;

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
        /* resolve self id in group if caller asked for it */
        if (self_addr_substr)
        {
            addr_substr = strstr(group_addr_strs[j], "://");
            if (addr_substr == NULL)
                addr_substr = group_addr_strs[j];
            else
                addr_substr += 3;

            if (strcmp(self_addr_substr, addr_substr) == 0)
            {
1157
                if (self_id)               
1158
                    *self_id = ssg_gen_member_id(group_addr_strs[j]);
1159 1160 1161

                /* don't look up our own address, we already know it */
                continue;
1162 1163 1164 1165
            }
        }

        /* XXX limit outstanding lookups to some max */
1166
        lookup_ult_args[j].addr_str = group_addr_strs[j];
1167
        lookup_ult_args[j].view = view;
Shane Snyder's avatar
Shane Snyder committed
1168
        lookup_ult_args[j].lock = view_lock;
1169 1170
        ABT_pool pool;
        margo_get_handler_pool(ssg_inst->mid, &pool);
1171 1172
        aret = ABT_thread_create(pool, &ssg_group_lookup_ult,
            &lookup_ult_args[j], ABT_THREAD_ATTR_NULL,
1173
            &lookup_ults[j]);
1174
        if (aret != ABT_SUCCESS) goto fini;
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    }

    /* wait on all lookup ULTs to terminate */
    for (i = 0; i < group_size; i++)
    {
        if (lookup_ults[i] == ABT_THREAD_NULL) continue;

        aret = ABT_thread_join(lookup_ults[i]);
        ABT_thread_free(&lookup_ults[i]);
        lookup_ults[i] = ABT_THREAD_NULL;
1185
        if (aret != ABT_SUCCESS) goto fini;
1186
        else if (lookup_ult_args[i].out != SSG_SUCCESS)
1187
        {
1188 1189
            fprintf(stderr, "Error: SSG unable to lookup HG address %s\n",
                lookup_ult_args[i].addr_str);
1190
            goto fini;
1191 1192 1193
        }
    }

1194 1195 1196
    /* clean exit */
    sret = SSG_SUCCESS;

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
fini:
    if (sret != SSG_SUCCESS)
    {
        for (i = 0; i < group_size; i++)
        {
            if (lookup_ults[i] != ABT_THREAD_NULL)
            {
                ABT_thread_cancel(lookup_ults[i]);
                ABT_thread_free(&lookup_ults[i]);
            }
        }
        ssg_group_view_destroy(view);
    }
    free(lookup_ults);
    free(lookup_ult_args);

    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
1214
}
1215

1216 1217 1218 1219 1220
static void ssg_group_lookup_ult(
    void * arg)
{
    struct ssg_group_lookup_ult_args *l = arg;

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
    if (ssg_group_view_add_member(l->addr_str, l->view, l->lock) != NULL)
        l->out = SSG_SUCCESS;
    else
        l->out = SSG_FAILURE;

    return;
}

static ssg_member_state_t * ssg_group_view_add_member(
    const char * addr_str,
    ssg_group_view_t * view,
    ABT_rwlock lock)
{
    ssg_member_state_t *ms;
    hg_return_t hret;

    ms = calloc(1, sizeof(*ms));
    if (!ms) return NULL;
    ms->addr_str = strdup(addr_str);
    if (!ms->addr_str)
1241
    {
1242 1243
        free(ms);
        return NULL;
1244
    }
1245 1246 1247 1248 1249
    ms->id = ssg_gen_member_id(addr_str);
    SWIM_MEMBER_STATE_INIT(ms->swim_state);

    hret = margo_addr_lookup(ssg_inst->mid, addr_str, &ms->addr);
    if (hret != HG_SUCCESS)
1250
    {
1251 1252 1253
        free(ms->addr_str);
        free(ms);
        return NULL;
1254
    }
1255

1256 1257 1258 1259 1260 1261
    ABT_rwlock_wrlock(lock);
    HASH_ADD(hh, view->member_map, id, sizeof(ssg_member_id_t), ms);
    view->size++;
    ABT_rwlock_unlock(lock);

    return ms;
1262 1263
}

Shane Snyder's avatar
Shane Snyder committed
1264 1265
static ssg_group_descriptor_t * ssg_group_descriptor_create(
    uint64_t name_hash, const char * leader_addr_str, int owner_status)
1266
{
Shane Snyder's avatar
Shane Snyder committed
1267
    ssg_group_descriptor_t *descriptor;
1268

Shane Snyder's avatar
Shane Snyder committed
1269 1270 1271 1272 1273 1274 1275 1276
    descriptor = malloc(sizeof(*descriptor));
    if (!descriptor) return NULL;

    /* hash the group name to obtain an 64-bit unique ID */
    descriptor->magic_nr = SSG_MAGIC_NR;
    descriptor->name_hash = name_hash;
    descriptor->addr_str = strdup(leader_addr_str);
    if (!descriptor->addr_str)
1277
    {
Shane Snyder's avatar
Shane Snyder committed
1278 1279
        free(descriptor);
        return NULL;
1280
    }
Shane Snyder's avatar
Shane Snyder committed
1281 1282 1283 1284
    descriptor->owner_status = owner_status;
    descriptor->ref_count = 1;
    return descriptor;
}
1285

Shane Snyder's avatar
Shane Snyder committed
1286 1287 1288 1289 1290
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor)
{
    descriptor->ref_count++;
    return descriptor;
1291 1292 1293 1294 1295
}

static void ssg_group_destroy_internal(
    ssg_group_t * g)
{
Shane Snyder's avatar
Shane Snyder committed
1296 1297 1298
    /* free up SWIM state */
    if(g->swim_ctx)
        swim_finalize(g->swim_ctx);
1299

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    /* XXX LOCK */

#ifdef DEBUG
    fflush(g->dbg_log);

    char *dbg_log_dir = getenv("SSG_DEBUG_LOGDIR");
    if (dbg_log_dir)
        fclose(g->dbg_log);
#endif

1310
    /* destroy group state */
Shane Snyder's avatar
Shane Snyder committed
1311
    HASH_DELETE(hh, ssg_inst->group_table, g);
1312
    ssg_group_view_destroy(&g->view);
Shane Snyder's avatar
Shane Snyder committed
1313
    g->descriptor->owner_status = SSG_OWNER_IS_UNASSOCIATED;
1314
    ssg_group_descriptor_free(g->descriptor);
Shane Snyder's avatar
Shane Snyder committed
1315
    ABT_rwlock_free(&g->lock);
1316
    free(g->name);
1317 1318
    free(g);

1319 1320 1321 1322 1323 1324
    return;
}

static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag)
{
Shane Snyder's avatar
Shane Snyder committed
1325 1326
    ssg_group_view_destroy(&ag->view);
    ag->descriptor->owner_status = SSG_OWNER_IS_UNASSOCIATED;
1327 1328 1329 1330 1331 1332
    ssg_group_descriptor_free(ag->descriptor);
    free(ag->name);
    free(ag);
    return;
}

Shane Snyder's avatar
Shane Snyder committed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
static void ssg_group_view_destroy(
    ssg_group_view_t * view)
{
    ssg_member_state_t *state, *tmp;

    /* destroy state for all group members */
    HASH_ITER(hh, view->member_map, state, tmp)
    {
        HASH_DEL(view->member_map, state);
        free(state->addr_str);
1343
        margo_addr_free(ssg_inst->mid, state->addr);
Shane Snyder's avatar
Shane Snyder committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
        free(state);
    }
    view->member_map = NULL;

    return;
}

static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor)
{
    if (descriptor)
    {
        if(descriptor->ref_count == 1)
        {
            free(descriptor->addr_str);
            free(descriptor);
        }
        else
        {
            descriptor->ref_count--;
        }
    }
    return;
}

static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str)
{
    char tmp[64] = {0};
    ssg_member_id_t id = (ssg_member_id_t)ssg_hash64_str(addr_str);
    while (id == SSG_MEMBER_ID_INVALID)
    {
        if (tmp[0] == 0) strncpy(tmp, addr_str, 63);
        tmp[0]++;
        id = (ssg_member_id_t)ssg_hash64_str(tmp);
    }
    return id;
}

1383 1384 1385
static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs)
{
1386
    int i;
1387 1388 1389 1390
    const char **ret = malloc(num_addrs * sizeof(*ret));
    if (ret == NULL) return NULL;

    ret[0] = buf;
1391
    for (i = 1; i < num_addrs; i++)
1392 1393 1394 1395 1396
    {
        const char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
    }
    return ret;