swim-fd-ping.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * (C) 2016 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include <ssg-config.h>

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

#include <mercury.h>
#include <margo.h>
#include <ssg.h>
#include "ssg-internal.h"
#include "swim-fd.h"
#include "swim-fd-internal.h"

Shane Snyder's avatar
Shane Snyder committed
20 21 22 23 24 25
/* NOTE these defines must be kept in sync with typedefs in
 * swim-internal.h
 */
#define hg_proc_swim_member_id_t hg_proc_int64_t
#define hg_proc_swim_member_status_t hg_proc_uint8_t
#define hg_proc_swim_member_inc_nr_t hg_proc_uint32_t
26

Shane Snyder's avatar
Shane Snyder committed
27 28 29 30
MERCURY_GEN_STRUCT_PROC(swim_member_update_t, \
    ((swim_member_id_t) (id)) \
    ((swim_member_status_t) (status)) \
    ((swim_member_inc_nr_t) (inc_nr)));
31 32 33 34 35 36

/* a swim message is the membership information piggybacked (gossiped)
 * on the ping and ack messages generated by the protocol
 */
typedef struct swim_message_s
{
Shane Snyder's avatar
Shane Snyder committed
37 38 39
    swim_member_id_t source_id;
    swim_member_inc_nr_t source_inc_nr;
    swim_member_update_t pb_buf[SWIM_MAX_PIGGYBACK_ENTRIES]; //TODO: can we do dynamic array instead?
40 41 42 43 44 45 46 47 48 49 50
} swim_message_t;

static hg_return_t hg_proc_swim_message_t(hg_proc_t proc, void *data);

MERCURY_GEN_PROC(swim_dping_req_t, \
    ((swim_message_t) (msg)));

MERCURY_GEN_PROC(swim_dping_resp_t, \
    ((swim_message_t) (msg)));

MERCURY_GEN_PROC(swim_iping_req_t, \
Shane Snyder's avatar
Shane Snyder committed
51
    ((swim_member_id_t) (target_id)) \
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    ((swim_message_t) (msg)));

MERCURY_GEN_PROC(swim_iping_resp_t, \
    ((swim_message_t) (msg)));

DECLARE_MARGO_RPC_HANDLER(swim_dping_recv_ult)
DECLARE_MARGO_RPC_HANDLER(swim_iping_recv_ult)

static void swim_pack_message(ssg_t s, swim_message_t *msg);
static void swim_unpack_message(ssg_t s, swim_message_t *msg);

static hg_id_t dping_rpc_id;
static hg_id_t iping_rpc_id;

void swim_register_ping_rpcs(
    ssg_t s)
{
    hg_class_t *hg_cls = margo_get_class(s->mid);

71
    /* register RPC handlers for SWIM pings */
72 73 74 75 76
    dping_rpc_id = MERCURY_REGISTER(hg_cls, "dping", swim_dping_req_t,
        swim_dping_resp_t, swim_dping_recv_ult_handler);
    iping_rpc_id = MERCURY_REGISTER(hg_cls, "iping", swim_iping_req_t,
        swim_iping_resp_t, swim_iping_recv_ult_handler);

77 78 79 80 81
    /* TODO: disable responses for RPCs to make them one-way operations */
    //HG_Registered_disable_response(hg_cls, dping_rpc_id, HG_TRUE);
    //HG_Registered_disable_response(hg_cls, iping_rpc_id, HG_TRUE);

    /* register swim context data structure with each RPC type */
82 83 84 85 86 87 88 89 90 91
    HG_Register_data(hg_cls, dping_rpc_id, s, NULL);
    HG_Register_data(hg_cls, iping_rpc_id, s, NULL);

    return;
}

/********************************
 *       SWIM direct pings      *
 ********************************/

Shane Snyder's avatar
Shane Snyder committed
92 93
static int swim_send_dping(
    ssg_t s, swim_member_id_t target);
94 95 96 97 98 99

void swim_dping_send_ult(
    void *t_arg)
{
    ssg_t s = (ssg_t)t_arg;
    swim_context_t *swim_ctx;
Shane Snyder's avatar
Shane Snyder committed
100
    swim_member_id_t target;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    int ret;

    assert(s != SSG_NULL);
    swim_ctx = s->swim_ctx;
    assert(swim_ctx != NULL);

    target = swim_ctx->ping_target;
    ret = swim_send_dping(s, target);
    if (ret == 0)
    {
        /* mark this dping req as acked, double checking to make
         * sure we aren't inadvertently ack'ing a ping request
         * for a more recent tick of the protocol
         */
        if(swim_ctx->ping_target == target)
            swim_ctx->ping_target_acked = 1;
    }

    return;
}

Shane Snyder's avatar
Shane Snyder committed
122 123
static int swim_send_dping(
    ssg_t s, swim_member_id_t target)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
{
    swim_context_t *swim_ctx = s->swim_ctx;
    hg_addr_t target_addr = HG_ADDR_NULL;
    hg_handle_t handle;
    swim_dping_req_t dping_req;
    swim_dping_resp_t dping_resp;
    hg_return_t hret;
    int ret = -1;

    target_addr = s->view.member_states[target].addr;
    if(target_addr == HG_ADDR_NULL)
        return(ret);

    hret = HG_Create(margo_get_context(s->mid), target_addr, dping_rpc_id,
        &handle);
    if(hret != HG_SUCCESS)
        return(ret);

Shane Snyder's avatar
Shane Snyder committed
142
    SSG_DEBUG(s, "SWIM: send dping req to %d\n", (int)target);
143 144 145 146 147 148 149 150 151 152 153 154 155

    /* fill the direct ping request with current membership state */
    swim_pack_message(s, &(dping_req.msg));

    /* send a direct ping that expires at the end of the protocol period */
    hret = margo_forward_timed(s->mid, handle, &dping_req,
        swim_ctx->prot_period_len);
    if (hret == HG_SUCCESS)
    {
        hret = HG_Get_output(handle, &dping_resp);
        if(hret != HG_SUCCESS)
            return(ret);

Shane Snyder's avatar
Shane Snyder committed
156
        SSG_DEBUG(s, "SWIM: recv dping ack from %d\n", (int)dping_resp.msg.source_id);
Shane Snyder's avatar
Shane Snyder committed
157
        assert(dping_resp.msg.source_id == target);
158 159 160 161 162 163 164 165

        /* extract target's membership state from response */
        swim_unpack_message(s, &(dping_resp.msg));

        ret = 0;
    }
    else if(hret != HG_TIMEOUT)
    {
Shane Snyder's avatar
Shane Snyder committed
166
        SSG_DEBUG(s, "SWIM: dping req error from %d (err=%d)\n", (int)target, hret);
167 168 169 170 171 172 173 174 175 176
    }

    HG_Destroy(handle);
    return(ret);
}

static void swim_dping_recv_ult(hg_handle_t handle)
{
    ssg_t s;
    swim_context_t *swim_ctx;
177
    const struct hg_info *info;
178 179 180 181 182 183 184 185 186 187 188 189 190
    swim_dping_req_t dping_req;
    swim_dping_resp_t dping_resp;
    hg_return_t hret;

    /* get ssg & swim state */
    info = HG_Get_info(handle);
    if(info == NULL)
        return;
    s = (ssg_t)HG_Registered_data(info->hg_class, dping_rpc_id);
    assert(s != SSG_NULL);
    swim_ctx = s->swim_ctx;
    assert(swim_ctx != NULL);

191 192 193 194 195 196 197 198 199
    /* XXX: make rank 1 unresponsive */
    //int drop = rand() % 2;
    int drop = 1;
    if(s->view.self_rank == 1 && drop)
    {
        HG_Destroy(handle);
        return;
    }

200 201 202 203
    hret = HG_Get_input(handle, &dping_req);
    if(hret != HG_SUCCESS)
        return;

Shane Snyder's avatar
Shane Snyder committed
204
    SSG_DEBUG(s, "SWIM: recv dping req from %d\n", (int)dping_req.msg.source_id);
205 206 207 208 209 210 211

    /* extract sender's membership state from request */
    swim_unpack_message(s, &(dping_req.msg));

    /* fill the direct ping response with current membership state */
    swim_pack_message(s, &(dping_resp.msg));

Shane Snyder's avatar
Shane Snyder committed
212
    SSG_DEBUG(s, "SWIM: send dping ack to %d\n", (int)dping_req.msg.source_id);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    /* respond to sender of the dping req */
    margo_respond(s->mid, handle, &dping_resp);

    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(swim_dping_recv_ult)

/********************************
 *     SWIM indirect pings      *
 ********************************/

void swim_iping_send_ult(
    void *t_arg)
{
    ssg_t s = (ssg_t)t_arg;
    swim_context_t *swim_ctx;
    int i;
Shane Snyder's avatar
Shane Snyder committed
232
    swim_member_id_t my_subgroup_member = SSG_MEMBER_RANK_UNKNOWN;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    hg_addr_t target_addr = HG_ADDR_NULL;
    hg_handle_t handle;
    swim_iping_req_t iping_req;
    swim_iping_resp_t iping_resp;
    hg_return_t hret;

    assert(s != SSG_NULL);
    swim_ctx = s->swim_ctx;
    assert(swim_ctx != NULL);

    for(i = 0; i < swim_ctx->prot_subgroup_sz; i++)
    {
        if(swim_ctx->subgroup_members[i] != SSG_MEMBER_RANK_UNKNOWN)
        {
            my_subgroup_member = swim_ctx->subgroup_members[i];
            swim_ctx->subgroup_members[i] = SSG_MEMBER_RANK_UNKNOWN;
            break;
        }
    }
    assert(my_subgroup_member != SSG_MEMBER_RANK_UNKNOWN);

    target_addr = s->view.member_states[my_subgroup_member].addr;
    if(target_addr == HG_ADDR_NULL)
        return;

    hret = HG_Create(margo_get_context(s->mid), target_addr, iping_rpc_id,
        &handle);
    if(hret != HG_SUCCESS)
        return;

Shane Snyder's avatar
Shane Snyder committed
263
    SSG_DEBUG(s, "SWIM: send iping req to %d (target=%d)\n",
Shane Snyder's avatar
Shane Snyder committed
264
        (int)my_subgroup_member, (int)swim_ctx->ping_target);
265 266 267 268

    /* fill the indirect ping request with target member and current
     * membership state
     */
Shane Snyder's avatar
Shane Snyder committed
269
    iping_req.target_id = swim_ctx->ping_target;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    swim_pack_message(s, &(iping_req.msg));

    /* send this indirect ping */
    /* NOTE: the timeout is just the protocol period length minus
     * the dping timeout, which should cause this iping to timeout
     * right at the end of the current protocol period.
     */
    hret = margo_forward_timed(s->mid, handle, &iping_req,
        (swim_ctx->prot_period_len - swim_ctx->dping_timeout));
    if (hret == HG_SUCCESS)
    {
        hret = HG_Get_output(handle, &iping_resp);
        if(hret != HG_SUCCESS)
            return;

Shane Snyder's avatar
Shane Snyder committed
285
        SSG_DEBUG(s, "SWIM: recv iping ack from %d (target=%d)\n",
Shane Snyder's avatar
Shane Snyder committed
286
            (int)iping_resp.msg.source_id, (int)swim_ctx->ping_target);
287 288 289 290 291 292 293 294

        /* extract target's membership state from response */
        swim_unpack_message(s, &(iping_resp.msg));

        /* mark this iping req as acked, double checking to make
         * sure we aren't inadvertently ack'ing a ping request
         * for a more recent tick of the protocol
         */
Shane Snyder's avatar
Shane Snyder committed
295
        if(swim_ctx->ping_target == iping_req.target_id)
296 297 298 299
            swim_ctx->ping_target_acked = 1;
    }
    else if(hret != HG_TIMEOUT)
    {
Shane Snyder's avatar
Shane Snyder committed
300
        SSG_DEBUG(s, "SWIM: iping req error from %d (target=%d, err=%d)\n",
Shane Snyder's avatar
Shane Snyder committed
301
            (int)my_subgroup_member, hret, (int)swim_ctx->ping_target);
302 303 304 305 306 307 308 309 310 311
    }

    HG_Destroy(handle);
    return;
}

static void swim_iping_recv_ult(hg_handle_t handle)
{
    ssg_t s;
    swim_context_t *swim_ctx;
312
    const struct hg_info *info;
313 314 315 316 317 318 319 320 321 322 323 324 325 326
    swim_iping_req_t iping_req;
    swim_iping_resp_t iping_resp;
    hg_return_t hret;
    int ret;

    /* get the swim state */
    info = HG_Get_info(handle);
    if(info == NULL)
        return;
    s = (ssg_t)HG_Registered_data(info->hg_class, dping_rpc_id);
    assert(s != SSG_NULL);
    swim_ctx = s->swim_ctx;
    assert(swim_ctx != NULL);

327 328 329 330 331 332 333
    /* XXX: make rank 1 unresponsive */
    if(s->view.self_rank == 1)
    {
        HG_Destroy(handle);
        return;
    }

334 335 336 337
    hret = HG_Get_input(handle, &iping_req);
    if(hret != HG_SUCCESS)
        return;

Shane Snyder's avatar
Shane Snyder committed
338
    SSG_DEBUG(s, "SWIM: recv iping req from %d (target=%d)\n",
Shane Snyder's avatar
Shane Snyder committed
339
        (int)iping_req.msg.source_id, (int)iping_req.target_id);
340 341 342 343 344

    /* extract sender's membership state from request */
    swim_unpack_message(s, &(iping_req.msg));

    /* send direct ping to target on behalf of who sent iping req */
Shane Snyder's avatar
Shane Snyder committed
345
    ret = swim_send_dping(s, iping_req.target_id);
346 347 348 349 350 351 352
    if(ret == 0)
    {
        /* if the dping req succeeds, fill the indirect ping
         * response with current membership state
         */
        swim_pack_message(s, &(iping_resp.msg));

Shane Snyder's avatar
Shane Snyder committed
353
        SSG_DEBUG(s, "SWIM: send iping ack to %d (target=%d)\n",
Shane Snyder's avatar
Shane Snyder committed
354
            (int)iping_req.msg.source_id, (int)iping_req.target_id);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

        /* respond to sender of the iping req */
        margo_respond(s->mid, handle, &iping_resp);
    }

    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(swim_iping_recv_ult)

/********************************
 *      SWIM ping helpers       *
 ********************************/

static void swim_pack_message(ssg_t s, swim_message_t *msg)
{
371 372
    swim_context_t *swim_ctx = s->swim_ctx;

373 374 375
    memset(msg, 0, sizeof(*msg));

    /* fill in self information */
Shane Snyder's avatar
Shane Snyder committed
376
    msg->source_id = s->view.self_rank;
377
    msg->source_inc_nr = swim_ctx->member_inc_nrs[s->view.self_rank];
378 379

    /* piggyback a set of membership states on this message */
Shane Snyder's avatar
Shane Snyder committed
380
    swim_retrieve_membership_updates(s, msg->pb_buf, SWIM_MAX_PIGGYBACK_ENTRIES);
381 382 383 384 385 386 387 388

    return;
}

static void swim_unpack_message(ssg_t s, swim_message_t *msg)
{
    swim_member_update_t sender_update;

Shane Snyder's avatar
Shane Snyder committed
389 390 391
    /* apply (implicit) sender update */
    sender_update.id = msg->source_id;
    sender_update.status = SWIM_MEMBER_ALIVE;
392
    sender_update.inc_nr = msg->source_inc_nr;
Shane Snyder's avatar
Shane Snyder committed
393
    swim_apply_membership_updates(s, &sender_update, 1);
394

Shane Snyder's avatar
Shane Snyder committed
395 396
    /* update membership status using piggybacked membership updates */
    swim_apply_membership_updates(s, msg->pb_buf, SWIM_MAX_PIGGYBACK_ENTRIES);
397 398 399 400 401 402 403 404 405 406 407 408 409 410

    return;
}

/* manual serialization/deserialization routine for swim messages */
static hg_return_t hg_proc_swim_message_t(hg_proc_t proc, void *data)
{
    swim_message_t *msg = (swim_message_t *)data;
    hg_return_t hret = HG_PROTOCOL_ERROR;
    int i;

    switch(hg_proc_get_op(proc))
    {
        case HG_ENCODE:
Shane Snyder's avatar
Shane Snyder committed
411
            hret = hg_proc_swim_member_id_t(proc, &(msg->source_id));
412 413 414 415 416
            if(hret != HG_SUCCESS)
            {
                hret = HG_PROTOCOL_ERROR;
                return hret;
            }
Shane Snyder's avatar
Shane Snyder committed
417
            hret = hg_proc_swim_member_inc_nr_t(proc, &(msg->source_inc_nr));
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
            if(hret != HG_SUCCESS)
            {
                hret = HG_PROTOCOL_ERROR;
                return hret;
            }
            for(i = 0; i < SWIM_MAX_PIGGYBACK_ENTRIES; i++)
            {
                hret = hg_proc_swim_member_update_t(proc, &(msg->pb_buf[i]));
                if(hret != HG_SUCCESS)
                {
                    hret = HG_PROTOCOL_ERROR;
                    return hret;
                }
            }
            break;
        case HG_DECODE:
Shane Snyder's avatar
Shane Snyder committed
434
            hret = hg_proc_swim_member_id_t(proc, &(msg->source_id));
435 436 437 438 439
            if(hret != HG_SUCCESS)
            {
                hret = HG_PROTOCOL_ERROR;
                return hret;
            }
Shane Snyder's avatar
Shane Snyder committed
440
            hret = hg_proc_swim_member_inc_nr_t(proc, &(msg->source_inc_nr));
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
            if(hret != HG_SUCCESS)
            {
                hret = HG_PROTOCOL_ERROR;
                return hret;
            }
            for(i = 0; i < SWIM_MAX_PIGGYBACK_ENTRIES; i++)
            {
                hret = hg_proc_swim_member_update_t(proc, &(msg->pb_buf[i]));
                if(hret != HG_SUCCESS)
                {
                    hret = HG_PROTOCOL_ERROR;
                    return hret;
                }
            }
            break;
        case HG_FREE:
            /* do nothing */
            hret = HG_SUCCESS;
            break;
        default:
            break;
    }

    return(hret);
}