int rpctest_nodeid_cb (flux_t h, int type, zmsg_t **zmsg, void *arg) { int errnum = 0; uint32_t nodeid; JSON o = NULL; int flags; if (flux_request_decode (*zmsg, NULL, NULL) < 0 || flux_msg_get_nodeid (*zmsg, &nodeid, &flags) < 0) { errnum = errno; goto done; } if (nodeid == nodeid_fake_error) { nodeid_fake_error = -1; errnum = EPERM; /* an error not likely to be seen */ goto done; } o = Jnew (); Jadd_int (o, "nodeid", nodeid); Jadd_int (o, "flags", flags); done: (void)flux_respond (h, *zmsg, errnum, Jtostr (o)); Jput (o); zmsg_destroy (zmsg); return 0; }
static inline void create_req4allsocks (json_t *reqobj) { json_t *req1; Jadd_str (reqobj, "type", "socket"); Jadd_int (reqobj, "req_qty", 1); req1 = Jnew (); Jadd_str (req1, "type", "core"); Jadd_int (req1, "req_qty", 1); json_object_set_new (reqobj, "req_child", req1); }
void test_nsrc (flux_t *h, uint32_t nodeid) { flux_future_t *f; const int count = 10000; json_object *in = Jnew (); const char *json_str; json_object *out = NULL; int i, seq; Jadd_int (in, "count", count); if (!(f = flux_rpc (h, "req.nsrc", Jtostr (in), FLUX_NODEID_ANY, FLUX_RPC_NORESPONSE))) log_err_exit ("%s", __FUNCTION__); flux_future_destroy (f); for (i = 0; i < count; i++) { flux_msg_t *msg = flux_recv (h, FLUX_MATCH_ANY, 0); if (!msg) log_err_exit ("%s", __FUNCTION__); if (flux_response_decode (msg, NULL, &json_str) < 0) log_msg_exit ("%s: decode %d", __FUNCTION__, i); if (!json_str || !(out = Jfromstr (json_str)) || !Jget_int (out, "seq", &seq)) log_msg_exit ("%s: decode %d payload", __FUNCTION__, i); if (seq != i) log_msg_exit ("%s: decode %d - seq mismatch %d", __FUNCTION__, i, seq); Jput (out); flux_msg_destroy (msg); } Jput (in); }
static int output_handler (zio_t *z, const char *json_str, int len, void *arg) { struct subprocess *p = (struct subprocess *) arg; json_object *o; if (p->io_cb) { if (!(o = json_tokener_parse (json_str))) { errno = EINVAL; return -1; } Jadd_int (o, "pid", subprocess_pid (p)); Jadd_str (o, "type", "io"); Jadd_str (o, "name", zio_name (z)); p->io_cb (p, json_object_to_json_string (o)); json_object_put (o); } else send_output_to_stream (zio_name (z), json_str); /* * Check for process completion in case EOF from I/O stream and process * already registered exit */ check_completion (p); return (0); }
/* Return 'n' sequenced responses. */ void nsrc_request_cb (flux_t h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { const char *json_str; int saved_errno; JSON o = Jnew (); int i, count; int rc = -1; if (flux_request_decode (msg, NULL, &json_str) < 0) { saved_errno = errno; goto done; } if (!(o = Jfromstr (json_str)) || !Jget_int (o, "count", &count)) { saved_errno = errno = EPROTO; goto done; } for (i = 0; i < count; i++) { Jadd_int (o, "seq", i); if (flux_respond (h, msg, 0, Jtostr (o)) < 0) { saved_errno = errno; flux_log_error (h, "%s: flux_respond", __FUNCTION__); goto done; } } rc = 0; done: if (rc < 0) { if (flux_respond (h, msg, saved_errno, NULL) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); } Jput (o); }
void send_ping (struct ping_ctx *ctx) { struct timespec t0; json_object *in = Jnew (); flux_rpc_t *rpc; struct ping_data *pdata = xzmalloc (sizeof (*pdata)); pdata->tstat = xzmalloc (sizeof (*(pdata->tstat))); pdata->seq = 0; pdata->route = NULL; pdata->rpc_count = 0; Jadd_int (in, "seq", ctx->send_count); monotime (&t0); Jadd_int64 (in, "time.tv_sec", t0.tv_sec); Jadd_int64 (in, "time.tv_nsec", t0.tv_nsec); Jadd_str (in, "pad", ctx->pad); if (ctx->rank) rpc = flux_rpc_multi (ctx->h, ctx->topic, Jtostr (in), ctx->rank, 0); else rpc = flux_rpc (ctx->h, ctx->topic, Jtostr (in), ctx->nodeid, 0); if (!rpc) log_err_exit ("flux_rpc"); flux_rpc_aux_set (rpc, pdata, ping_data_free); if (flux_rpc_then (rpc, ping_continuation, ctx) < 0) log_err_exit ("flux_rpc_then"); Jput (in); ctx->send_count++; }
/* Return 'n' sequenced responses. */ static int nsrc_request_cb (flux_t h, int typemask, zmsg_t **zmsg, void *arg) { JSON o = Jnew (); int i, count; if (flux_json_request_decode (*zmsg, &o) < 0) { if (flux_err_respond (h, errno, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_err_respond: %s", __FUNCTION__, strerror (errno)); goto done; } if (!Jget_int (o, "count", &count)) { if (flux_err_respond (h, EPROTO, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_err_respond: %s", __FUNCTION__, strerror (errno)); goto done; } for (i = 0; i < count; i++) { zmsg_t *cpy = zmsg_dup (*zmsg); if (!cpy) oom (); Jadd_int (o, "seq", i); if (flux_json_respond (h, o, &cpy) < 0) flux_log (h, LOG_ERR, "%s: flux_json_respond: %s", __FUNCTION__, strerror (errno)); zmsg_destroy (&cpy); } zmsg_destroy (zmsg); done: Jput (o); return 0; }
flux_msg_t *shutdown_vencode (double grace, int exitcode, int rank, const char *fmt, va_list ap) { flux_msg_t *msg; JSON out = Jnew (); char reason[REASON_MAX]; vsnprintf (reason, sizeof (reason), fmt, ap); Jadd_str (out, "reason", reason); Jadd_double (out, "grace", grace); Jadd_int (out, "rank", rank); Jadd_int (out, "exitcode", exitcode); msg = flux_event_encode ("shutdown", Jtostr (out)); Jput (out); return msg; }
/* Return a fixed json payload */ void src_request_cb (flux_t h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { JSON o = Jnew (); Jadd_int (o, "wormz", 42); if (flux_respond (h, msg, 0, Jtostr (o)) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); Jput (o); }
static flux_rpc_t *dmesg_rpc (flux_t h, int seq, bool follow) { flux_rpc_t *rpc; JSON o = Jnew (); Jadd_int (o, "seq", seq); Jadd_bool (o, "follow", follow); rpc = flux_rpc (h, "cmb.dmesg", Jtostr (o), FLUX_NODEID_ANY, 0); Jput (o); return rpc; }
flux_msg_t *flux_heartbeat_encode (int epoch) { flux_msg_t *msg; JSON o = Jnew (); Jadd_int (o, "epoch", epoch); msg = flux_event_encode ("hb", Jtostr (o)); Jput (o); return msg; }
/* This test is to make sure that deferred responses are handled in order. * Arrange for module to source 10K sequenced responses. Messages 5000-5499 * are "put back" on the handle using flux_putmsg(). We ensure that * the 10K messages are nonetheless received in order. */ void test_putmsg (flux_t *h, uint32_t nodeid) { flux_future_t *f; const char *json_str; const int count = 10000; const int defer_start = 5000; const int defer_count = 500; json_object *in = Jnew (); json_object *out = NULL; int seq, myseq = 0; zlist_t *defer = zlist_new (); bool popped = false; flux_msg_t *z; if (!defer) oom (); Jadd_int (in, "count", count); if (!(f = flux_rpc (h, "req.nsrc", Jtostr (in), FLUX_NODEID_ANY, FLUX_RPC_NORESPONSE))) log_err_exit ("%s", __FUNCTION__); flux_future_destroy (f); do { flux_msg_t *msg = flux_recv (h, FLUX_MATCH_ANY, 0); if (!msg) log_err_exit ("%s", __FUNCTION__); if (flux_response_decode (msg, NULL, &json_str) < 0) log_msg_exit ("%s: decode", __FUNCTION__); if (!json_str || !(out = Jfromstr (json_str)) || !Jget_int (out, "seq", &seq)) log_msg_exit ("%s: decode - payload", __FUNCTION__); Jput (out); if (seq >= defer_start && seq < defer_start + defer_count && !popped) { if (zlist_append (defer, msg) < 0) oom (); if (seq == defer_start + defer_count - 1) { while ((z = zlist_pop (defer))) { if (flux_requeue (h, z, FLUX_RQ_TAIL) < 0) log_err_exit ("%s: flux_requeue", __FUNCTION__); flux_msg_destroy (z); } popped = true; } continue; } if (seq != myseq) log_msg_exit ("%s: expected %d got %d", __FUNCTION__, myseq, seq); myseq++; flux_msg_destroy (msg); } while (myseq < count); zlist_destroy (&defer); Jput (in); }
/* request nodeid and flags returned in response */ void rpctest_nodeid_cb (flux_t h, flux_msg_watcher_t *w, const flux_msg_t *msg, void *arg) { int errnum = 0; uint32_t nodeid; JSON o = NULL; int flags; if (flux_request_decode (msg, NULL, NULL) < 0 || flux_msg_get_nodeid (msg, &nodeid, &flags) < 0) { errnum = errno; goto done; } o = Jnew (); Jadd_int (o, "nodeid", nodeid); Jadd_int (o, "flags", flags); done: (void)flux_respond (h, msg, errnum, Jtostr (o)); Jput (o); }
/* Return a fixed json payload */ static int src_request_cb (flux_t h, int typemask, zmsg_t **zmsg, void *arg) { JSON o = Jnew (); Jadd_int (o, "wormz", 42); if (flux_json_respond (h, o, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_json_respond: %s", __FUNCTION__, strerror (errno)); Jput (o); return 0; }
static void send_enter_request (ctx_t *ctx, barrier_t *b) { json_object *o = Jnew (); flux_rpc_t *rpc; Jadd_str (o, "name", b->name); Jadd_int (o, "count", b->count); Jadd_int (o, "nprocs", b->nprocs); Jadd_int (o, "hopcount", 1); if (!(rpc = flux_rpc (ctx->h, "barrier.enter", Jtostr (o), FLUX_NODEID_UPSTREAM, FLUX_RPC_NORESPONSE))) { flux_log_error (ctx->h, "sending barrier.enter request"); goto done; } done: if (rpc) flux_rpc_destroy (rpc); json_object_put (o); }
/* Return number of queued clog requests */ void count_request_cb (flux_t h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { ctx_t *ctx = getctx (h); JSON o = Jnew (); Jadd_int (o, "count", zlist_size (ctx->clog_requests)); if (flux_respond (h, msg, 0, Jtostr (o)) < 0) flux_log_error (h, "%s: flux_json_respond", __FUNCTION__); Jput (o); }
/* Return number of queued clog requests */ static int count_request_cb (flux_t h, int typemask, zmsg_t **zmsg, void *arg) { ctx_t *ctx = getctx (h); JSON o = Jnew (); Jadd_int (o, "count", zlist_size (ctx->clog_requests)); if (flux_json_respond (h, o, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_json_respond: %s", __FUNCTION__, strerror (errno)); Jput (o); return 0; }
/* Proxy ping. */ void xping_request_cb (flux_t h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { ctx_t *ctx = arg; const char *json_str; int saved_errno; int rank, seq = ctx->ping_seq++; const char *service; char *hashkey = NULL; JSON in = Jnew (); JSON o = NULL; flux_msg_t *cpy; if (flux_request_decode (msg, NULL, &json_str) < 0) { saved_errno = errno; goto error; } if (!(o = Jfromstr (json_str)) || !Jget_int (o, "rank", &rank) || !Jget_str (o, "service", &service)) { saved_errno = errno = EPROTO; goto error; } flux_log (h, LOG_DEBUG, "Rxping rank=%d service=%s", rank, service); Jadd_int (in, "seq", seq); flux_log (h, LOG_DEBUG, "Tping seq=%d %d!%s", seq, rank, service); flux_rpc_t *rpc; if (!(rpc = flux_rpc (h, service, Jtostr (in), rank, FLUX_RPC_NORESPONSE))) { saved_errno = errno; goto error; } flux_rpc_destroy (rpc); if (!(cpy = flux_msg_copy (msg, true))) { saved_errno = errno; goto error; } hashkey = xasprintf ("%d", seq); zhash_update (ctx->ping_requests, hashkey, cpy); zhash_freefn (ctx->ping_requests, hashkey, (zhash_free_fn *)flux_msg_destroy); Jput (o); Jput (in); if (hashkey) free (hashkey); return; error: if (flux_respond (h, msg, saved_errno, NULL) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); Jput (o); Jput (in); }
JSON kp_tcommit_enc (const char *sender, JSON ops, const char *fence, int nprocs) { JSON o = Jnew (); Jadd_obj (o, "ops", ops); /* takes a ref on ops */ if (sender) Jadd_str (o, ".arg_sender", sender); if (fence) { Jadd_str (o, ".arg_fence", fence); Jadd_int (o, ".arg_nprocs", nprocs); } return o; }
JSON kp_rgetroot_enc (int rootseq, const char *rootdir) { JSON o = NULL; if (!rootdir) { errno = EINVAL; goto done; } o = Jnew (); Jadd_int (o, "rootseq", rootseq); Jadd_str (o, "rootdir", rootdir); done: return o; }
JSON kp_rcommit_enc (int rootseq, const char *rootdir, const char *sender) { JSON o = NULL; if (!rootdir || !sender) { errno = EINVAL; goto done; } o = Jnew (); Jadd_int (o, "rootseq", rootseq); Jadd_str (o, "rootdir", rootdir); Jadd_str (o, "sender", sender); done: return o; }
flux_msg_t *hello_encode (int rank) { flux_msg_t *msg = NULL; JSON out = Jnew (); Jadd_int (out, "rank", rank); if (!(msg = flux_request_encode ("cmb.hello", Jtostr (out)))) goto error; if (flux_msg_set_nodeid (msg, 0, 0) < 0) goto error; Jput (out); return msg; error: flux_msg_destroy (msg); Jput (out); return NULL; }
static int exit_event_send (flux_t *h, const char *name, int errnum) { json_object *o = Jnew (); flux_msg_t *msg = NULL; int rc = -1; Jadd_str (o, "name", name); Jadd_int (o, "errnum", errnum); if (!(msg = flux_event_encode ("barrier.exit", Jtostr (o)))) goto done; if (flux_send (h, msg, 0) < 0) goto done; rc = 0; done: Jput (o); flux_msg_destroy (msg); return rc; }
static int dmesg_clear (flux_t h, int seq) { flux_rpc_t *rpc; JSON o = Jnew (); int rc = -1; Jadd_int (o, "seq", seq); if (!(rpc = flux_rpc (h, "cmb.dmesg.clear", Jtostr (o), FLUX_NODEID_ANY, 0))) goto done; if (flux_rpc_get (rpc, NULL, NULL) < 0) goto done; rc = 0; done: flux_rpc_destroy (rpc); Jput (o); return rc; }
static int exit_event_send (flux_t h, const char *name, int errnum) { JSON o = Jnew (); zmsg_t *zmsg = NULL; int rc = -1; Jadd_str (o, "name", name); Jadd_int (o, "errnum", errnum); if (!(zmsg = flux_event_encode ("barrier.exit", Jtostr (o)))) goto done; if (flux_sendmsg (h, &zmsg) < 0) goto done; rc = 0; done: Jput (o); zmsg_destroy (&zmsg); return rc; }
static void stats_get_cb (flux_t h, flux_msg_handler_t *w, const flux_msg_t *msg, void *arg) { flux_msgcounters_t mcs; JSON out = Jnew (); flux_get_msgcounters (h, &mcs); Jadd_int (out, "#request (tx)", mcs.request_tx); Jadd_int (out, "#request (rx)", mcs.request_rx); Jadd_int (out, "#response (tx)", mcs.response_tx); Jadd_int (out, "#response (rx)", mcs.response_rx); Jadd_int (out, "#event (tx)", mcs.event_tx); Jadd_int (out, "#event (rx)", mcs.event_rx); Jadd_int (out, "#keepalive (tx)", mcs.keepalive_tx); Jadd_int (out, "#keepalive (rx)", mcs.keepalive_rx); if (flux_respond (h, msg, 0, Jtostr (out)) < 0) FLUX_LOG_ERROR (h); Jput (out); }
JSON kp_tsetroot_enc (int rootseq, const char *rootdir, JSON root, const char *fence) { JSON o = NULL; if (!rootdir) { errno = EINVAL; goto done; } o = Jnew (); Jadd_int (o, "rootseq", rootseq); Jadd_str (o, "rootdir", rootdir); if (fence) Jadd_str (o, "fence", fence); if (root) Jadd_obj (o, "rootdirval", root); /* takes a ref */ done: return o; }
/* Proxy ping. */ static int xping_request_cb (flux_t h, int typemask, zmsg_t **zmsg, void *arg) { ctx_t *ctx = arg; int rank, seq = ctx->ping_seq++; const char *service; char *hashkey = NULL; JSON in = Jnew (); JSON o = NULL; if (flux_json_request_decode (*zmsg, &o) < 0) { if (flux_err_respond (h, errno, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_err_respond: %s", __FUNCTION__, strerror (errno)); goto done; } if (!Jget_int (o, "rank", &rank) || !Jget_str (o, "service", &service)) { if (flux_err_respond (h, EPROTO, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_err_respond: %s", __FUNCTION__, strerror (errno)); goto done; } flux_log (h, LOG_DEBUG, "Rxping rank=%d service=%s", rank, service); Jadd_int (in, "seq", seq); flux_log (h, LOG_DEBUG, "Tping seq=%d %d!%s", seq, rank, service); if (flux_json_request (h, rank, 0, service, in) < 0) { if (flux_err_respond (h, errno, zmsg) < 0) flux_log (h, LOG_ERR, "%s: flux_err_respond: %s", __FUNCTION__, strerror (errno)); goto done; } hashkey = xasprintf ("%d", seq); zhash_update (ctx->ping_requests, hashkey, *zmsg); *zmsg = NULL; done: Jput (o); Jput (in); if (hashkey) free (hashkey); return 0; }
static void xping (flux_t *h, uint32_t nodeid, uint32_t xnodeid, const char *svc) { flux_future_t *f; const char *json_str; json_object *in = Jnew (); json_object *out = NULL; const char *route; Jadd_int (in, "rank", xnodeid); Jadd_str (in, "service", svc); if (!(f = flux_rpc (h, "req.xping", Jtostr (in), nodeid, 0)) || flux_rpc_get (f, &json_str) < 0) log_err_exit ("req.xping"); if (!json_str || !(out = Jfromstr (json_str)) || !Jget_str (out, "route", &route)) log_errn_exit (EPROTO, "req.xping"); printf ("hops=%d\n", count_hops (route)); Jput (out); Jput (in); flux_future_destroy (f); }
void test_put (void) { JSON o; bool dir = false; bool link = false; const char *key = NULL; JSON val = NULL; int i; val = Jnew (); Jadd_int (val, "i", 2); o = kp_tput_enc ("a", val, false, true); ok (o != NULL, "kp_tput_snec works"); val = NULL; ok (kp_tput_dec (o, &key, &val, &dir, &link) == 0 && !dir && link, "kp_tput_dec works"); ok (val && Jget_int (val, "i", &i) && i == 2, "kp_tput_dec returned encoded object"); Jput (o); /* owns val */ }