swim-fd-ping.c 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*
 * (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"


MERCURY_GEN_PROC(swim_member_update_t, \
    ((uint32_t) (rank)) \
    ((uint8_t)  (susp_level)) \
    ((uint32_t) (inc_nr)));

/* a swim message is the membership information piggybacked (gossiped)
 * on the ping and ack messages generated by the protocol
 */
typedef struct swim_message_s
{
    uint32_t                source_rank;
    uint32_t                source_inc_nr;
    swim_member_update_t    pb_buf[SWIM_MAX_PIGGYBACK_ENTRIES]; //TODO: can we do dynamic array instead?
} 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, \
    ((uint32_t)       (target_member)) \
    ((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);

    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);

    HG_Register_data(hg_cls, dping_rpc_id, s, NULL);
    HG_Register_data(hg_cls, iping_rpc_id, s, NULL);

    return;
}

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

static int swim_send_dping(ssg_t s, int target);

void swim_dping_send_ult(
    void *t_arg)
{
    ssg_t s = (ssg_t)t_arg;
    swim_context_t *swim_ctx;
    int target;
    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)
    {
        // TODO is this if check necessary?
        /* 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;
}

static int swim_send_dping(ssg_t s, int target)
{
    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);

    SSG_DEBUG(s, "send dping req to %d\n", target);

    /* 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);

        SSG_DEBUG(s, "recv dping ack from %d\n", dping_resp.msg.source_rank);
144
        assert((int)dping_resp.msg.source_rank == target);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

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

        ret = 0;
    }
    else if(hret != HG_TIMEOUT)
    {
        SSG_DEBUG(s, "dping req error from %d, err=%d\n", target, hret);
    }

    HG_Destroy(handle);
    return(ret);
}

static void swim_dping_recv_ult(hg_handle_t handle)
{
    ssg_t s;
    swim_context_t *swim_ctx;
    struct hg_info *info;
    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);

    hret = HG_Get_input(handle, &dping_req);
    if(hret != HG_SUCCESS)
        return;

    SSG_DEBUG(s, "recv dping req from %d\n", dping_req.msg.source_rank);

    /* 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));

    SSG_DEBUG(s, "send dping ack to %d\n", dping_req.msg.source_rank);

    /* 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;
    int my_subgroup_member = SSG_MEMBER_RANK_UNKNOWN;
    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;

    SSG_DEBUG(s, "send iping req to %d, target=%d\n",
        my_subgroup_member, swim_ctx->ping_target);

    /* fill the indirect ping request with target member and current
     * membership state
     */
    iping_req.target_member = swim_ctx->ping_target;
    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;

        SSG_DEBUG(s, "recv iping ack from %d, target=%d\n",
            iping_resp.msg.source_rank, swim_ctx->ping_target);

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

        // TODO is this if check necessary?
        /* 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
         */
        if(swim_ctx->ping_target == (int)iping_req.target_member)
            swim_ctx->ping_target_acked = 1;
    }
    else if(hret != HG_TIMEOUT)
    {
        SSG_DEBUG(s, "iping req error from %d, err=%d, target=%d\n",
            my_subgroup_member, hret, swim_ctx->ping_target);
    }

    HG_Destroy(handle);
    return;
}

static void swim_iping_recv_ult(hg_handle_t handle)
{
    ssg_t s;
    swim_context_t *swim_ctx;
    struct hg_info *info;
    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);

    hret = HG_Get_input(handle, &iping_req);
    if(hret != HG_SUCCESS)
        return;

    SSG_DEBUG(s, "recv iping req from %d, target=%d\n",
        iping_req.msg.source_rank, iping_req.target_member);

    /* 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 */
    ret = swim_send_dping(s, iping_req.target_member);
    if(ret == 0)
    {
        /* if the dping req succeeds, fill the indirect ping
         * response with current membership state
         */
        swim_pack_message(s, &(iping_resp.msg));

        SSG_DEBUG(s, "send iping ack to %d, target=%d\n",
            iping_req.msg.source_rank, iping_req.target_member);

        /* 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)
{
    memset(msg, 0, sizeof(*msg));

    /* fill in self information */
    msg->source_rank = s->view.self_rank;
    msg->source_inc_nr = s->view.member_states[s->view.self_rank].swim_inc_nr;

#if 0
    /* piggyback a set of membership states on this message */
    swim_retrieve_membership_updates(swim_ctx, msg->pb_buf,
        SWIM_MAX_PIGGYBACK_ENTRIES);
#endif

    return;
}

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

    /* TODO: use unsuspect_member? -- update state of the sender of this message */
    sender_update.rank = msg->source_rank;
    sender_update.susp_level = 0;
    sender_update.inc_nr = msg->source_inc_nr;
#if 0
    swim_apply_membership_updates(swim_ctx, &sender_state, 1);

    /* update membership status using piggybacked membership states */
    swim_apply_membership_updates(swim_ctx, msg->pb_buf,
        SWIM_MAX_PIGGYBACK_ENTRIES);
#endif

    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:
            hret = hg_proc_int32_t(proc, &(msg->source_rank));
            if(hret != HG_SUCCESS)
            {
                hret = HG_PROTOCOL_ERROR;
                return hret;
            }
            hret = hg_proc_uint32_t(proc, &(msg->source_inc_nr));
            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:
            hret = hg_proc_int32_t(proc, &(msg->source_rank));
            if(hret != HG_SUCCESS)
            {
                hret = HG_PROTOCOL_ERROR;
                return hret;
            }
            hret = hg_proc_uint32_t(proc, &(msg->source_inc_nr));
            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);
}