From 989b42a7f491451a776c0d4059afde2b4bcf1070 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Mon, 30 Apr 2018 17:25:21 -0700 Subject: [PATCH 01/15] test/rpc: stop using json-c Problem: t/rpc/rpc.c still uses json-c. Convert to jansson. --- t/rpc/rpc.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/t/rpc/rpc.c b/t/rpc/rpc.c index 3c1be96fc0ff..dc069b40103d 100644 --- a/t/rpc/rpc.c +++ b/t/rpc/rpc.c @@ -1,7 +1,7 @@ #include #include +#include -#include "src/common/libutil/shortjson.h" #include "src/common/libtap/tap.h" #include "util.h" @@ -21,22 +21,21 @@ void rpctest_incr_cb (flux_t *h, flux_msg_handler_t *mh, void rpctest_nodeid_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { - int errnum = 0; uint32_t nodeid; - json_object *o = NULL; int flags; if (flux_request_decode (msg, NULL, NULL) < 0 || flux_msg_get_nodeid (msg, &nodeid, &flags) < 0) { - errnum = errno; - goto done; + goto error; } - o = Jnew (); - Jadd_int (o, "nodeid", nodeid); - Jadd_int (o, "flags", flags); -done: - (void)flux_respond (h, msg, errnum, Jtostr (o)); - Jput (o); + if (flux_respond_pack (h, msg, "s:i s:i", + "nodeid", (int)nodeid, + "flags", flags) < 0) + diag ("%s: flux_respond_pack: %s", __FUNCTION__, strerror (errno)); + return; +error: + if (flux_respond (h, msg, errno, NULL) < 0) + diag ("%s: flux_respond: %s", __FUNCTION__, strerror (errno)); } /* request payload echoed in response */ @@ -206,7 +205,8 @@ void test_basic (flux_t *h) void test_encoding (flux_t *h) { - json_object *o; + json_t *o; + char *s; const char *json_str; flux_future_t *r; @@ -240,16 +240,18 @@ void test_encoding (flux_t *h) /* flux_rpc_get is ok if user doesn't desire response payload */ errno = 0; - o = Jnew (); - Jadd_int (o, "foo", 42); - json_str = Jtostr (o); - ok ((r = flux_rpc (h, "rpctest.echo", json_str, FLUX_NODEID_ANY, 0)) != NULL, + if (!(o = json_pack ("{s:i}", "foo", 42))) + BAIL_OUT ("json_pack failed"); + if (!(s = json_dumps (o, JSON_COMPACT))) + BAIL_OUT ("json_dumps failed"); + ok ((r = flux_rpc (h, "rpctest.echo", s, FLUX_NODEID_ANY, 0)) != NULL, "flux_rpc with payload works"); + free (s); errno = 0; ok (flux_rpc_get (r, NULL) == 0, "flux_rpc_get is ok if user doesn't desire response payload"); flux_future_destroy (r); - Jput (o); + json_decref (o); /* working with-payload RPC */ ok ((r = flux_rpc (h, "rpctest.echo", "{}", FLUX_NODEID_ANY, 0)) != NULL, From cfb77f94c39759e9c8cc9db98715a8d2ee3d7e42 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 10:29:46 -0700 Subject: [PATCH 02/15] test/mrpc: stop using json-c Problem: t/rpc/mrpc.c still uses json-c. Use flux_respond_pack() in that one instance. --- t/rpc/mrpc.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/t/rpc/mrpc.c b/t/rpc/mrpc.c index 4dabc15806f5..284de09a8a5e 100644 --- a/t/rpc/mrpc.c +++ b/t/rpc/mrpc.c @@ -2,7 +2,6 @@ #include #include -#include "src/common/libutil/shortjson.h" #include "src/common/libutil/nodeset.h" #include "src/common/libtap/tap.h" @@ -16,27 +15,25 @@ static int nodeid_fake_error = -1; void rpctest_nodeid_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { - int errnum = 0; uint32_t nodeid; - json_object *o = NULL; int flags; if (flux_request_decode (msg, NULL, NULL) < 0 || flux_msg_get_nodeid (msg, &nodeid, &flags) < 0) { - errnum = errno; - goto done; + goto error; } if (nodeid == nodeid_fake_error) { nodeid_fake_error = -1; - errnum = EPERM; /* an error not likely to be seen */ - goto done; + errno = EPERM; /* an error not likely to be seen */ + goto error; } - o = Jnew (); - Jadd_int (o, "nodeid", nodeid); - Jadd_int (o, "flags", flags); -done: - (void)flux_respond (h, msg, errnum, Jtostr (o)); - Jput (o); + if (flux_respond_pack (h, msg, "{s:i s:i}", "nodeid", nodeid, + "flags", flags) < 0) + diag ("%s: flux_respond_pack: %s", __FUNCTION__, strerror (errno)); + return; +error: + if (flux_respond (h, msg, errno, NULL) < 0) + diag ("%s: flux_respond: %s", __FUNCTION__, strerror (errno)); } void rpcftest_nodeid_cb (flux_t *h, flux_msg_handler_t *mh, From c2d9f9317c62fdd3183d5fb80fac8f09a32e5c2a Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 10:32:21 -0700 Subject: [PATCH 03/15] test/request: [cleanup] fix trailing whitespace Problem: t/request/req.c conntains lines with trailing white space which makes vim/git sad. Delete trailing white space. --- t/request/req.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/request/req.c b/t/request/req.c index c65595204389..1f6614eeb735 100644 --- a/t/request/req.c +++ b/t/request/req.c @@ -44,7 +44,7 @@ static t_req_ctx_t *getctx (flux_t *h) ctx->clog_requests = zlist_new (); if (!ctx->clog_requests || !ctx->ping_requests) { saved_errno = ENOMEM; - goto error; + goto error; } if (flux_get_rank (h, &ctx->rank) < 0) { saved_errno = errno; From 3b6d135bd76d3f80da0891f731adcdef0ec8ad44 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 11:01:33 -0700 Subject: [PATCH 04/15] test/request: stop using json-c (server) Problem: t/request/req.c still uses json-c. Convert to jansson and the "pack" RPC functions. --- t/request/req.c | 147 ++++++++++++++---------------------------------- 1 file changed, 43 insertions(+), 104 deletions(-) diff --git a/t/request/req.c b/t/request/req.c index 1f6614eeb735..95e1c0eeac5c 100644 --- a/t/request/req.c +++ b/t/request/req.c @@ -3,10 +3,11 @@ #endif #include #include +#include +#include "src/common/libutil/oom.h" #include "src/common/libutil/xzmalloc.h" #include "src/common/libutil/log.h" -#include "src/common/libutil/shortjson.h" typedef struct { flux_t *h; @@ -65,12 +66,10 @@ void count_request_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { t_req_ctx_t *ctx = getctx (h); - json_object *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_respond", __FUNCTION__); - Jput (o); + if (flux_respond_pack (h, msg, "{s:i}", + "count", zlist_size (ctx->clog_requests)) < 0) + flux_log_error (h, "%s: flux_respond_pack", __FUNCTION__); } /* Don't reply to request - just queue it for later. @@ -109,28 +108,20 @@ void flush_request_cb (flux_t *h, flux_msg_handler_t *mh, void sink_request_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { - const char *json_str; - int saved_errno; - json_object *o = NULL; double d; - int rc = -1; - if (flux_request_decode (msg, NULL, &json_str) < 0) { - saved_errno = errno; - goto done; - } - if (!json_str - || !(o = Jfromstr (json_str)) - || !Jget_double (o, "pi", &d) - || d != 3.14) { - saved_errno = errno = EPROTO; - goto done; + if (flux_request_unpack (msg, NULL, "{s:f}", "pi", &d) < 0) + goto error; + if (d != 3.14) { + errno = EPROTO; + goto error; } - rc = 0; -done: - if (flux_respond (h, msg, rc < 0 ? saved_errno : 0, NULL) < 0) + if (flux_respond (h, msg, 0, NULL) < 0) + flux_log_error (h, "%s: flux_respond", __FUNCTION__); + return; +error: + if (flux_respond (h, msg, errno, NULL) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); - Jput (o); } /* Return a fixed json payload @@ -138,12 +129,8 @@ void sink_request_cb (flux_t *h, flux_msg_handler_t *mh, void src_request_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { - json_object *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); + if (flux_respond_pack (h, msg, "{s:i}", "wormz", 42) < 0) + flux_log_error (h, "%s: flux_respond_pack", __FUNCTION__); } /* Return 'n' sequenced responses. @@ -151,37 +138,18 @@ void src_request_cb (flux_t *h, flux_msg_handler_t *mh, void nsrc_request_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { - const char *json_str; - int saved_errno; - json_object *o = Jnew (); int i, count; - int rc = -1; - if (flux_request_decode (msg, NULL, &json_str) < 0) { - saved_errno = errno; - goto done; - } - if (!json_str - || !(o = Jfromstr (json_str)) - || !Jget_int (o, "count", &count)) { - saved_errno = errno = EPROTO; - goto done; - } + if (flux_request_unpack (msg, NULL, "{s:i}", "count", &count) < 0) + goto error; 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) + if (flux_respond_pack (h, msg, "{s:i}", "seq", i) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); } - Jput (o); + return; +error: + if (flux_respond (h, msg, errno, NULL) < 0) + flux_log_error (h, "%s: flux_respond", __FUNCTION__); } /* Always return an error 42 @@ -223,55 +191,33 @@ void xping_request_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { t_req_ctx_t *ctx = arg; - const char *json_str; - int saved_errno; int rank, seq = ctx->ping_seq++; const char *service; char *hashkey = NULL; - json_object *in = Jnew (); - json_object *o = NULL; flux_msg_t *cpy; - if (flux_request_decode (msg, NULL, &json_str) < 0) { - saved_errno = errno; + if (flux_request_unpack (msg, NULL, "{s:i s:s}", "rank", &rank, + "service", &service) < 0) goto error; - } - if (!json_str - || !(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_future_t *f; - if (!(f = flux_rpc (h, service, Jtostr (in), rank, - FLUX_RPC_NORESPONSE))) { - saved_errno = errno; + if (!(f = flux_rpc_pack (h, service, rank, FLUX_RPC_NORESPONSE, + "{s:i}", "seq", seq))) goto error; - } flux_future_destroy (f); - if (!(cpy = flux_msg_copy (msg, true))) { - saved_errno = errno; + if (!(cpy = flux_msg_copy (msg, true))) 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); + free (hashkey); return; error: - if (flux_respond (h, msg, saved_errno, NULL) < 0) + if (flux_respond (h, msg, errno, NULL) < 0) flux_log_error (h, "%s: flux_respond", __FUNCTION__); - Jput (o); - Jput (in); } /* Handle ping response for proxy ping. @@ -281,42 +227,35 @@ void ping_response_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { t_req_ctx_t *ctx = arg; - const char *json_str; - json_object *o = NULL; - json_object *out = Jnew ();; int seq; const char *route; flux_msg_t *req = NULL; - char *hashkey = NULL; + char hashkey[16]; + const char *json_str; + json_t *o = NULL; if (flux_response_decode (msg, NULL, &json_str) < 0) { flux_log_error (h, "%s: flux_response_decode", __FUNCTION__); - goto done; + return; } - if (!json_str - || !(o = Jfromstr (json_str)) - || !Jget_int (o, "seq", &seq) - || !Jget_str (o, "route", &route)) { - errno = EPROTO; - flux_log_error (h, "%s: payload", __FUNCTION__); + if (!json_str || !(o = json_loads (json_str, 0, NULL)) + || json_unpack (o, "{s:i s:s}", "seq", &seq, + "route", &route) < 0) { + flux_log (h, LOG_ERR, "%s: error decoding payload", __FUNCTION__); goto done; } flux_log (h, LOG_DEBUG, "Rping seq=%d %s", seq, route); - hashkey = xasprintf ("%d", seq); + snprintf (hashkey, sizeof (hashkey), "%d", seq); if (!(req = zhash_lookup (ctx->ping_requests, hashkey))) { flux_log_error (h, "%s: unsolicited ping response", __FUNCTION__); goto done; } flux_log (h, LOG_DEBUG, "Txping seq=%d %s", seq, route); - Jadd_str (out, "route", route); - if (flux_respond (h, req, 0, Jtostr (out)) < 0) - flux_log_error (h, "%s: flux_respond", __FUNCTION__); + if (flux_respond_pack (h, req, "{s:s}", "route", route) < 0) + flux_log_error (h, "%s: flux_respond_pack", __FUNCTION__); zhash_delete (ctx->ping_requests, hashkey); done: - if (hashkey) - free (hashkey); - Jput (o); - Jput (out); + json_decref (o); } /* Handle the simplest possible request. From ae94067dbd0a7ef5b51b3a160c6a2e234fe1d3cd Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 11:26:19 -0700 Subject: [PATCH 05/15] test/request: stop using json-c (client) Problem: t/request/treq.c is still using jsonc-c. Convert to jansson. --- t/request/treq.c | 92 ++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 62 deletions(-) diff --git a/t/request/treq.c b/t/request/treq.c index 7eeee734c350..ee2e8e5f4ca0 100644 --- a/t/request/treq.c +++ b/t/request/treq.c @@ -32,11 +32,12 @@ #include #include #include +#include #include "src/common/libutil/log.h" +#include "src/common/libutil/oom.h" #include "src/common/libutil/monotime.h" #include "src/common/libutil/xzmalloc.h" -#include "src/common/libutil/shortjson.h" void test_null (flux_t *h, uint32_t nodeid); void test_echo (flux_t *h, uint32_t nodeid); @@ -145,23 +146,15 @@ void test_null (flux_t *h, uint32_t nodeid) void test_echo (flux_t *h, uint32_t nodeid) { - json_object *in = Jnew (); - json_object *out = NULL; - const char *json_str; const char *s; flux_future_t *f; - Jadd_str (in, "mumble", "burble"); - if (!(f = flux_rpc (h, "req.echo", Jtostr (in), nodeid, 0)) - || flux_rpc_get (f, &json_str) < 0) + if (!(f = flux_rpc_pack (h, "req.echo", nodeid, 0, + "{s:s}", "mumble", "burble")) + || flux_rpc_get_unpack (f, "{s:s}", "mumble", &s) < 0) log_err_exit ("%s", __FUNCTION__); - if (!json_str - || !(out = Jfromstr (json_str)) - || !Jget_str (out, "mumble", &s) - || strcmp (s, "burble") != 0) + if (strcmp (s, "burble") != 0) log_msg_exit ("%s: returned payload wasn't an echo", __FUNCTION__); - Jput (in); - Jput (out); flux_future_destroy (f); } @@ -181,65 +174,53 @@ void test_err (flux_t *h, uint32_t nodeid) void test_src (flux_t *h, uint32_t nodeid) { flux_future_t *f; - const char *json_str; - json_object *out = NULL; int i; if (!(f = flux_rpc (h, "req.src", NULL, nodeid, 0)) - || flux_rpc_get (f, &json_str) < 0) + || flux_rpc_get_unpack (f, "{s:i}", "wormz", &i) < 0) log_err_exit ("%s", __FUNCTION__); - if (!json_str - || !(out = Jfromstr (json_str)) - || !Jget_int (out, "wormz", &i) - || i != 42) + if (i != 42) log_msg_exit ("%s: didn't get expected payload", __FUNCTION__); - Jput (out); flux_future_destroy (f); } void test_sink (flux_t *h, uint32_t nodeid) { flux_future_t *f; - json_object *in = Jnew(); - Jadd_double (in, "pi", 3.14); - if (!(f = flux_rpc (h, "req.sink", Jtostr (in), nodeid, 0)) + if (!(f = flux_rpc_pack (h, "req.sink", nodeid, 0, "{s:f}", "pi", 3.14)) || flux_future_get (f, NULL) < 0) log_err_exit ("%s", __FUNCTION__); - Jput (in); flux_future_destroy (f); } 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; + const int count = 10000; + int i, seq = -1; + json_t *o; - Jadd_int (in, "count", count); - if (!(f = flux_rpc (h, "req.nsrc", Jtostr (in), FLUX_NODEID_ANY, - FLUX_RPC_NORESPONSE))) + if (!(f = flux_rpc_pack (h, "req.nsrc", + FLUX_NODEID_ANY, FLUX_RPC_NORESPONSE, + "{s:i}", "count", count))) 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) + flux_msg_t *msg; + if (!(msg = flux_recv (h, FLUX_MATCH_ANY, 0))) 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)) + if (!json_str || !(o = json_loads (json_str, 0, NULL)) + || json_unpack (o, "{s:i}", "seq", &seq) < 0) 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); + json_decref (o); flux_msg_destroy (msg); } - Jput (in); } /* This test is to make sure that deferred responses are handled in order. @@ -254,19 +235,18 @@ void test_putmsg (flux_t *h, uint32_t nodeid) 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; + json_t *o; if (!defer) oom (); - Jadd_int (in, "count", count); - if (!(f = flux_rpc (h, "req.nsrc", Jtostr (in), FLUX_NODEID_ANY, - FLUX_RPC_NORESPONSE))) + if (!(f = flux_rpc_pack (h, "req.nsrc", + FLUX_NODEID_ANY, FLUX_RPC_NORESPONSE, + "{s:i}", "count", count))) log_err_exit ("%s", __FUNCTION__); flux_future_destroy (f); do { @@ -275,11 +255,10 @@ void test_putmsg (flux_t *h, uint32_t nodeid) 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)) + if (!json_str || !(o = json_loads (json_str, 0, NULL)) + || json_unpack (o, "{s:i}", "seq", &seq) < 0) log_msg_exit ("%s: decode - payload", __FUNCTION__); - Jput (out); + json_decref (o); if (seq >= defer_start && seq < defer_start + defer_count && !popped) { if (zlist_append (defer, msg) < 0) oom (); @@ -299,7 +278,6 @@ void test_putmsg (flux_t *h, uint32_t nodeid) flux_msg_destroy (msg); } while (myseq < count); zlist_destroy (&defer); - Jput (in); } static int count_hops (const char *s) @@ -318,23 +296,13 @@ static int count_hops (const char *s) 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) + if (!(f = flux_rpc_pack (h, "req.xping", nodeid, 0, + "{s:i s:s}", "rank", xnodeid, "service", svc)) + || flux_rpc_get_unpack (f, "{s:s}", "route", &route) < 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); } From 711d665a7c667e60045e4637b08c28ff932e9d23 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 13:51:09 -0700 Subject: [PATCH 06/15] cmd/flux: drop shortjson.h Problem: cmd/flux includes shortjson.h but doesn't use it. Drop the extraneous include. --- src/cmd/flux.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd/flux.c b/src/cmd/flux.c index 746eaeb8c612..d7a1a2809264 100644 --- a/src/cmd/flux.c +++ b/src/cmd/flux.c @@ -40,7 +40,6 @@ #include "src/common/libutil/log.h" #include "src/common/libutil/xzmalloc.h" -#include "src/common/libutil/shortjson.h" #include "src/common/libutil/environment.h" #include "cmdhelp.h" From 4288fc310c9ce37ba7f6ee6a57527397d0c711f5 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 13:56:25 -0700 Subject: [PATCH 07/15] libutil/tstat: drop shortjson.h Problem: libutil/tstat includes shortjson.h but doesn't use it. Drop the extraneous include. --- src/common/libutil/tstat.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/libutil/tstat.c b/src/common/libutil/tstat.c index 143297fc1af4..ad645e98b8a8 100644 --- a/src/common/libutil/tstat.c +++ b/src/common/libutil/tstat.c @@ -27,7 +27,6 @@ #endif #include -#include "shortjson.h" #include "tstat.h" #include "log.h" From 09bb40cbb16236df935a40b6d36d6700f79c5eb0 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 13:57:20 -0700 Subject: [PATCH 08/15] libflux/test: drop shortjson.h from module test Problem: module test includes shortjson.h but doesn't use it. Drop extraneous include --- src/common/libflux/test/module.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/libflux/test/module.c b/src/common/libflux/test/module.c index b3618469fb23..616396409fd8 100644 --- a/src/common/libflux/test/module.c +++ b/src/common/libflux/test/module.c @@ -1,7 +1,6 @@ #include #include -#include "src/common/libutil/shortjson.h" #include "src/common/libutil/xzmalloc.h" #include "src/common/libtap/tap.h" From 33b8bf29278b36af30f708f6ae1f2358d01b4d73 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 14:01:03 -0700 Subject: [PATCH 09/15] man/test: stop using json-c in treduce.c Problem: doc/man3/treduce.c is still using json-c. Convert to rpc pack functions. --- doc/man3/treduce.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/doc/man3/treduce.c b/doc/man3/treduce.c index 2d8e93438cf5..303f0aef539d 100644 --- a/doc/man3/treduce.c +++ b/doc/man3/treduce.c @@ -1,7 +1,7 @@ #include #include #include -#include "src/common/libutil/shortjson.h" +#include #include "src/common/libutil/nodeset.h" #include "src/common/libutil/xzmalloc.h" @@ -41,13 +41,11 @@ void forward (flux_reduce_t *r, int batchnum, void *arg) flux_future_t *f; while ((item = flux_reduce_pop (r))) { - json_object *out = Jnew (); - Jadd_int (out, "batchnum", batchnum); - Jadd_str (out, "nodeset", item); - f = flux_rpc (ctx->h, "treduce.forward", Jtostr (out), - FLUX_NODEID_UPSTREAM, FLUX_RPC_NORESPONSE); + f = flux_rpc_pack (ctx->h, "treduce.forward", + FLUX_NODEID_UPSTREAM, FLUX_RPC_NORESPONSE, + "{s:i s:s}", "batchnum", batchnum, + "nodeset", item); flux_future_destroy (f); - Jput (out); free (item); } } @@ -77,21 +75,17 @@ void forward_cb (flux_t *h, flux_msg_handler_t *mh, const flux_msg_t *msg, void *arg) { struct context *ctx = arg; - const char *json_str, *nodeset_str; - json_object *in = NULL; + const char *nodeset_str; int batchnum; char *item; - if (flux_request_decode (msg, NULL, &json_str) < 0 - || !json_str - || !(in = Jfromstr (json_str)) - || !Jget_int (in, "batchnum", &batchnum) - || !Jget_str (in, "nodeset", &nodeset_str)) + if (flux_request_unpack (msg, NULL, "{s:i s:s}", + "batchnum", &batchnum, + "nodeset", &nodeset_str) < 0) return; item = xstrdup (nodeset_str); if (flux_reduce_append (ctx->r, item, batchnum) < 0) free (item); - Jput (in); } void heartbeat_cb (flux_t *h, flux_msg_handler_t *mh, From 594d9cc15934fd66e5691650d4ea761fcff95c0b Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 14:05:16 -0700 Subject: [PATCH 10/15] man/test: stop using json-c in tmrpc_then.c Problem: doc/man3/tmrpc_then.c is still using json-c. --- doc/man3/tmrpc_then.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/doc/man3/tmrpc_then.c b/doc/man3/tmrpc_then.c index e7bf79036bdc..662fdcae12e6 100644 --- a/doc/man3/tmrpc_then.c +++ b/doc/man3/tmrpc_then.c @@ -1,36 +1,28 @@ +#include #include -#include "src/common/libutil/shortjson.h" #include "src/common/libutil/log.h" void get_rank (flux_mrpc_t *mrpc, void *arg) { - const char *json_str; - json_object *o; const char *rank; uint32_t nodeid; if (flux_mrpc_get_nodeid (mrpc, &nodeid) < 0) log_err_exit ("flux_mrpc_get_nodeid"); - if (flux_mrpc_get (mrpc, &json_str) < 0) + if (flux_mrpc_get_unpack (mrpc, "{s:s}", "value", &rank) < 0) log_err_exit ("flux_mrpc_get"); - if (!json_str - || !(o = Jfromstr (json_str)) - || !Jget_str (o, "value", &rank)) - log_msg_exit ("response protocol error"); printf ("[%" PRIu32 "] rank is %s\n", nodeid, rank); - Jput (o); } int main (int argc, char **argv) { flux_t *h; flux_mrpc_t *mrpc; - json_object *o = Jnew(); - Jadd_str (o, "name", "rank"); if (!(h = flux_open (NULL, 0))) log_err_exit ("flux_open"); - if (!(mrpc = flux_mrpc (h, "attr.get", Jtostr (o), "all", 0))) + if (!(mrpc = flux_mrpc_pack (h, "attr.get", "all", 0, + "{s:s}", "name", "rank"))) log_err_exit ("flux_mrpc"); if (flux_mrpc_then (mrpc, get_rank, NULL) < 0) log_err_exit ("flux_mrpc_then"); @@ -39,6 +31,5 @@ int main (int argc, char **argv) flux_mrpc_destroy (mrpc); flux_close (h); - Jput (o); return (0); } From 9f13d005a10d811349b7d6e6cbe670fd88bdecc1 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 14:18:26 -0700 Subject: [PATCH 11/15] cmd/cmdhelp: stop using json-c Problem: cmdhelp is still using json-c. Convert to jansson. --- src/cmd/cmdhelp.c | 72 +++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 49 deletions(-) diff --git a/src/cmd/cmdhelp.c b/src/cmd/cmdhelp.c index 2e0a9be53205..64844c11e9d6 100644 --- a/src/cmd/cmdhelp.c +++ b/src/cmd/cmdhelp.c @@ -27,11 +27,11 @@ #include #include #include +#include #include "src/common/libutil/log.h" #include "src/common/libutil/xzmalloc.h" #include "src/common/libutil/sds.h" -#include "src/common/libutil/shortjson.h" #include "cmdhelp.h" @@ -74,44 +74,18 @@ static void cmd_list_destroy (zlist_t *zl) zlist_destroy (&zl); } -static json_object *command_list_file_read (const char *path) +static json_t *command_list_file_read (const char *path) { - FILE *fp = NULL; - json_object *o = NULL; - enum json_tokener_error e = json_tokener_success; - json_tokener *tok = json_tokener_new (); + json_t *o; + json_error_t error; - if (tok == NULL) { - log_msg ("json_tokener_new: Out of memory"); - return (NULL); - } - - if (!(fp = fopen (path, "r"))) { - log_err ("%s", path); - return (NULL); - } - - do { - int len; - char buf [4096]; - char *s; - if (!(s = fgets (buf, sizeof (buf), fp))) - break; - len = strlen (s); - o = json_tokener_parse_ex (tok, s, len); - } while ((e = json_tokener_get_error(tok)) == json_tokener_continue); - - fclose (fp); - json_tokener_free (tok); - - if (!o || e != json_tokener_success) { - log_msg ("%s: %s", path, o ? json_tokener_error_desc (e) : "premature EOF"); - return (NULL); + if (!(o = json_load_file (path, 0, &error))) { + log_msg ("%s::%d: %s", path, error.line, error.text); + return NULL; } - - if (json_object_get_type (o) != json_type_array) { + if (!json_is_array (o)) { log_msg ("%s: not a JSON array", path); - json_object_put (o); + json_decref (o); return (NULL); } @@ -123,26 +97,27 @@ static int command_list_read (zhash_t *h, const char *path) int i; int rc = -1; int n = 0; - json_object *o = NULL; - struct array_list *l; - - o = command_list_file_read (path); + json_t *o = NULL; - if (!(l = json_object_get_array (o))) { - log_msg ("%s: failed to get array list from JSON", path); + if (!(o = command_list_file_read (path))) goto out; - } - n = array_list_length (l); + n = json_array_size (o); for (i = 0; i < n; i++) { - json_object *entry = array_list_get_idx (l, i); const char *category; const char *command; const char *description; + json_t *entry; zlist_t *zl; - if (!Jget_str (entry, "category", &category) - || !Jget_str (entry, "command", &command) - || !Jget_str (entry, "description", &description)) { + + if (!(entry = json_array_get (o, i))) { + log_msg ("%s: entry %d is not an object", path, i); + goto out; + } + if (json_unpack (entry, "{s:s s:s s:s}", + "category", &category, + "command", &command, + "description", &description) < 0) { log_msg ("%s: Missing element in JSON entry %d", path, i); goto out; } @@ -161,8 +136,7 @@ static int command_list_read (zhash_t *h, const char *path) rc = 0; out: - if (o) - json_object_put (o); + json_decref (o); return (rc); } From 8586c5547c8a6e65dc0ddcb93c13a6854f2a25da Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 14:19:26 -0700 Subject: [PATCH 12/15] cmd/flux-comms: drop shortjson.h Problem: flux-comms was including shortjson.h but not using it. Drop extraneous include. --- src/cmd/flux-comms.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd/flux-comms.c b/src/cmd/flux-comms.c index c4195517f955..a84ccb31e5fd 100644 --- a/src/cmd/flux-comms.c +++ b/src/cmd/flux-comms.c @@ -33,7 +33,6 @@ #include #include "src/common/libutil/log.h" -#include "src/common/libutil/shortjson.h" #define OPTIONS "+hr:" From 5e7726e9840ebcb574bf4676028c2366328c3908 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 14:52:20 -0700 Subject: [PATCH 13/15] cmd/flux-jstat: stop using json-c Problem: flux-jstat is still using json-c. Convert to jansson. --- src/cmd/flux-jstat.c | 67 ++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/src/cmd/flux-jstat.c b/src/cmd/flux-jstat.c index 63fb35775844..b12812532c61 100644 --- a/src/cmd/flux-jstat.c +++ b/src/cmd/flux-jstat.c @@ -31,11 +31,12 @@ #include #include #include +#include +#include #include #include "src/common/libutil/log.h" #include "src/common/libutil/xzmalloc.h" -#include "src/common/libutil/shortjson.h" /****************************************************************************** @@ -111,20 +112,6 @@ static FILE *open_test_outfile (const char *fn) return fp; } -static inline void get_jobid (json_object *jcb, int64_t *j) -{ - Jget_int64 (jcb, JSC_JOBID, j); -} - -static inline void get_states (json_object *jcb, int64_t *os, int64_t *ns) -{ - json_object *o = NULL; - Jget_obj (jcb, JSC_STATE_PAIR, &o); - Jget_int64 (o, JSC_STATE_PAIR_OSTATE, os); - Jget_int64 (o, JSC_STATE_PAIR_NSTATE, ns); -} - - /****************************************************************************** * * * Async notification callback * @@ -133,12 +120,11 @@ static inline void get_states (json_object *jcb, int64_t *os, int64_t *ns) static int job_status_cb (const char *jcbstr, void *arg, int errnum) { - int64_t os = 0; - int64_t ns = 0; - int64_t j = 0; + int os; + int ns; jstatctx_t *ctx = NULL; flux_t *h = (flux_t *)arg; - json_object *jcb = NULL; + json_t *jcb = NULL; ctx = getctx (h); if (errnum > 0) { @@ -146,19 +132,19 @@ static int job_status_cb (const char *jcbstr, void *arg, int errnum) return -1; } - if (!(jcb = Jfromstr (jcbstr))) { + if (!(jcb = json_loads (jcbstr, 0, NULL)) + || json_unpack (jcb, "{s:{s:i s:i}}", JSC_STATE_PAIR, + JSC_STATE_PAIR_OSTATE, &os, + JSC_STATE_PAIR_NSTATE, &ns) < 0) { flux_log (ctx->h, LOG_ERR, "job_status_cb: error parsing JSON string"); + json_decref (jcb); return -1; } - get_jobid (jcb, &j); - get_states (jcb, &os, &ns); - Jput (jcb); - fprintf (ctx->op, "%s->%s\n", - jsc_job_num2state ((job_state_t)os), - jsc_job_num2state ((job_state_t)ns)); + fprintf (ctx->op, "%s->%s\n", jsc_job_num2state (os), + jsc_job_num2state (ns)); fflush (ctx->op); - + json_decref (jcb); return 0; } @@ -192,21 +178,30 @@ static int handle_notify_req (flux_t *h, const char *ofn) static int handle_query_req (flux_t *h, int64_t j, const char *k, const char *n) { - json_object *jcb = NULL; - jstatctx_t *ctx = NULL; - char *jcbstr; + jstatctx_t *ctx = getctx (h); + json_t *jcb = NULL; + char *jcbstr = NULL; + char *jcbstr_pretty = NULL; - ctx = getctx (h); - ctx->op = n? open_test_outfile (n) : stdout; + ctx->op = n ? open_test_outfile (n) : stdout; if (jsc_query_jcb (h, j, k, &jcbstr) != 0) { flux_log (h, LOG_ERR, "jsc_query_jcb reported an error"); return -1; } - jcb = Jfromstr (jcbstr); + if (!(jcb = json_loads (jcbstr, 0, NULL))) { + flux_log (h, LOG_ERR, "error loading jcbstr"); + goto done; + } + if (!(jcbstr_pretty = json_dumps (jcb, JSON_INDENT(2)))) { + flux_log (h, LOG_ERR, "error dumping jcbstr"); + goto done; + } + fprintf (ctx->op, "Job Control Block: attribute %s for job %"PRIi64"\n", k, j); - fprintf (ctx->op, "%s\n", jcb == NULL ? jcbstr : - json_object_to_json_string_ext (jcb, JSON_C_TO_STRING_PRETTY)); - Jput (jcb); + fprintf (ctx->op, "%s\n", jcbstr_pretty); +done: + free (jcbstr_pretty); + json_decref (jcb); free (jcbstr); return 0; } From 9def9f77e88c558eaa59a12812e18aaba7672874 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 15:11:58 -0700 Subject: [PATCH 14/15] libjsc: use rpc_pack for kvspath RPC Simplify code in lwj_kvs_path() by using flux_rpc_pack() and flux_rpc_get_unpack() instead of "hand parsing" the JSON payloads. --- src/common/libjsc/jstatctl.c | 47 ++---------------------------------- 1 file changed, 2 insertions(+), 45 deletions(-) diff --git a/src/common/libjsc/jstatctl.c b/src/common/libjsc/jstatctl.c index a0460eeda690..bced895871fd 100644 --- a/src/common/libjsc/jstatctl.c +++ b/src/common/libjsc/jstatctl.c @@ -142,60 +142,18 @@ static jscctx_t *getctx (flux_t *h) return ctx; } -json_object * kvspath_request_json (int64_t id) -{ - json_object *o = json_object_new_object (); - json_object *ar = json_object_new_array (); - json_object *v = json_object_new_int64 (id); - - if (!o || !ar || !v) { - Jput (o); - Jput (ar); - Jput (v); - return (NULL); - } - json_object_array_add (ar, v); - json_object_object_add (o, "ids", ar); - return (o); -} - -const char * kvs_path_json_get (json_object *o) -{ - const char *p; - json_object *ar; - if (!Jget_obj (o, "paths", &ar) || !Jget_ar_str (ar, 0, &p)) - return (NULL); - return (p); -} - static int lwj_kvs_path (flux_t *h, int64_t id, char **pathp) { int rc = -1; const char *path; - const char *json_str; flux_future_t *f; uint32_t nodeid = FLUX_NODEID_ANY; - json_object *o = kvspath_request_json (id); - if (!(f = flux_rpc (h, "job.kvspath", Jtostr (o), nodeid, 0)) - || (flux_rpc_get (f, &json_str) < 0)) { + if (!(f = flux_rpc_pack (h, "job.kvspath", nodeid, 0, "{s:[I]}", "ids", id)) + || flux_rpc_get_unpack (f, "{s:[s]}", "paths", &path) < 0) { flux_log_error (h, "flux_rpc (job.kvspath)"); goto out; } - Jput (o); - o = NULL; - if (!json_str) { - flux_log (h, LOG_ERR, "flux_rpc (job.kvspath): empty payload"); - goto out; - } - if (!(o = Jfromstr (json_str))) { - flux_log_error (h, "flux_rpc (job.kvspath): failed to parse json"); - goto out; - } - if (!(path = kvs_path_json_get (o))) { - flux_log_error (h, "flux_rpc (job.kvspath): failed to get path"); - goto out; - } if (!(*pathp = strdup (path))) { flux_log_error (h, "flux_rpc (job.kvspath): strdup"); goto out; @@ -203,7 +161,6 @@ static int lwj_kvs_path (flux_t *h, int64_t id, char **pathp) rc = 0; out: flux_future_destroy (f); - Jput (o); return (rc); } From 852d53ba11688bc1c50051ddef95d66c1caace42 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 2 May 2018 17:07:13 -0700 Subject: [PATCH 15/15] libsubprocess: stop using json-c Problem: libsubprocess library still uses json-c. Convert to jansson. Drop extraneous json-c includes from subprocess/zio unit tests as well. --- src/common/libsubprocess/subprocess.c | 31 ++++++++++++++++------ src/common/libsubprocess/test/loop.c | 2 -- src/common/libsubprocess/test/socketpair.c | 1 - src/common/libsubprocess/test/subprocess.c | 1 - src/common/libsubprocess/test/zio.c | 1 - 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/common/libsubprocess/subprocess.c b/src/common/libsubprocess/subprocess.c index 01a5d5eba27d..798027342bfa 100644 --- a/src/common/libsubprocess/subprocess.c +++ b/src/common/libsubprocess/subprocess.c @@ -35,10 +35,10 @@ #include #include #include +#include #include "src/common/libutil/log.h" #include "src/common/libutil/xzmalloc.h" -#include "src/common/libutil/shortjson.h" #include "src/common/libutil/fdwalk.h" #include "zio.h" #include "subprocess.h" @@ -212,18 +212,29 @@ static int check_completion (struct subprocess *p) static int output_handler (zio_t *z, const char *json_str, int len, void *arg) { struct subprocess *p = (struct subprocess *) arg; - json_object *o; + json_t *o = NULL; + json_t *new_o; + char *json_str_amended; if (p->io_cb) { - if (!(o = json_tokener_parse (json_str))) { + if (!(o = json_loads (json_str, 0, NULL))) { 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); + if (!(new_o = json_pack ("{s:i s:s s:s}", "pid", subprocess_pid (p), + "type", "io", + "name", zio_name (z)))) + goto error; + if (json_object_update (o, new_o) < 0) { + json_decref (new_o); + goto error; + } + json_decref (new_o); + if (!(json_str_amended = json_dumps (o, JSON_COMPACT))) + goto error; + p->io_cb (p, json_str_amended); + free (json_str_amended); + json_decref (o); } else send_output_to_stream (zio_name (z), json_str); @@ -234,6 +245,10 @@ static int output_handler (zio_t *z, const char *json_str, int len, void *arg) */ check_completion (p); return (0); +error: + json_decref (o); + errno = EINVAL; + return -1; } static int hooks_table_init (struct subprocess *p) diff --git a/src/common/libsubprocess/test/loop.c b/src/common/libsubprocess/test/loop.c index df09deec52a4..f59d7bd32b35 100644 --- a/src/common/libsubprocess/test/loop.c +++ b/src/common/libsubprocess/test/loop.c @@ -29,8 +29,6 @@ #include #include -#include "src/common/libjson-c/json.h" - #include "tap.h" #include "subprocess.h" diff --git a/src/common/libsubprocess/test/socketpair.c b/src/common/libsubprocess/test/socketpair.c index 9b24a169bcba..450505697cb5 100644 --- a/src/common/libsubprocess/test/socketpair.c +++ b/src/common/libsubprocess/test/socketpair.c @@ -29,7 +29,6 @@ #include #include -#include "src/common/libjson-c/json.h" #include "tap.h" #include "subprocess.h" diff --git a/src/common/libsubprocess/test/subprocess.c b/src/common/libsubprocess/test/subprocess.c index a801a20d4d53..d7b29083b287 100644 --- a/src/common/libsubprocess/test/subprocess.c +++ b/src/common/libsubprocess/test/subprocess.c @@ -25,7 +25,6 @@ #include #include #include -#include "src/common/libjson-c/json.h" #include "tap.h" #include "subprocess.h" diff --git a/src/common/libsubprocess/test/zio.c b/src/common/libsubprocess/test/zio.c index ac89855f157f..85671a04f193 100644 --- a/src/common/libsubprocess/test/zio.c +++ b/src/common/libsubprocess/test/zio.c @@ -32,7 +32,6 @@ #include #include "src/common/libtap/tap.h" -#include "src/common/libjson-c/json.h" #include "zio.h"