Skip to content

Commit

Permalink
jsonrpc: Don't swallow errors in jsonrpc_transact_block().
Browse files Browse the repository at this point in the history
If a server returned an error in response to a request,
jsonrpc_transact_block() would ignore it.  This patch changes the
behavior and updates its callers to gracefully handle the
possibility.

Signed-off-by: Ethan Jackson <[email protected]>
  • Loading branch information
ejj committed Feb 21, 2012
1 parent 491aa88 commit d35f8e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
8 changes: 5 additions & 3 deletions lib/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,11 @@ jsonrpc_transact_block(struct jsonrpc *rpc, struct jsonrpc_msg *request,
if (!error) {
for (;;) {
error = jsonrpc_recv_block(rpc, &reply);
if (error
|| (reply->type == JSONRPC_REPLY
&& json_equal(id, reply->id))) {
if (error) {
break;
}
if ((reply->type == JSONRPC_REPLY || reply->type == JSONRPC_ERROR)
&& json_equal(id, reply->id)) {
break;
}
jsonrpc_msg_destroy(reply);
Expand Down
43 changes: 19 additions & 24 deletions ovsdb/ovsdb-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ usage(void)
exit(EXIT_SUCCESS);
}

static void
check_txn(int error, struct jsonrpc_msg **reply_)
{
struct jsonrpc_msg *reply = *reply_;

if (error) {
ovs_fatal(error, "transaction failed");
}

if (reply->error) {
ovs_fatal(error, "transaction returned error: %s",
json_to_string(reply->error, table_style.json_flags));
}
}

static struct json *
parse_json(const char *s)
{
Expand Down Expand Up @@ -342,16 +357,12 @@ fetch_schema(struct jsonrpc *rpc, const char *database)
{
struct jsonrpc_msg *request, *reply;
struct ovsdb_schema *schema;
int error;

request = jsonrpc_create_request("get_schema",
json_array_create_1(
json_string_create(database)),
NULL);
error = jsonrpc_transact_block(rpc, request, &reply);
if (error) {
ovs_fatal(error, "transaction failed");
}
check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
jsonrpc_msg_destroy(reply);

Expand All @@ -362,16 +373,12 @@ static void
fetch_dbs(struct jsonrpc *rpc, struct sset *dbs)
{
struct jsonrpc_msg *request, *reply;
int error;
size_t i;

request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
NULL);
error = jsonrpc_transact_block(rpc, request, &reply);
if (error) {
ovs_fatal(error, "transaction failed");
}

check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
if (reply->result->type != JSON_ARRAY) {
ovs_fatal(0, "list_dbs response is not array");
}
Expand Down Expand Up @@ -485,19 +492,11 @@ do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
{
struct jsonrpc_msg *request, *reply;
struct json *transaction;
int error;

transaction = parse_json(argv[0]);

request = jsonrpc_create_request("transact", transaction, NULL);
error = jsonrpc_transact_block(rpc, request, &reply);
if (error) {
ovs_fatal(error, "transaction failed");
}
if (reply->error) {
ovs_fatal(error, "transaction returned error: %s",
json_to_string(reply->error, table_style.json_flags));
}
check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
print_json(reply->result);
putchar('\n');
jsonrpc_msg_destroy(reply);
Expand Down Expand Up @@ -898,7 +897,6 @@ do_dump(struct jsonrpc *rpc, const char *database,
struct jsonrpc_msg *request, *reply;
struct ovsdb_schema *schema;
struct json *transaction;
int error;

const struct shash_node **tables;
size_t n_tables;
Expand Down Expand Up @@ -935,10 +933,7 @@ do_dump(struct jsonrpc *rpc, const char *database,

/* Send request, get reply. */
request = jsonrpc_create_request("transact", transaction, NULL);
error = jsonrpc_transact_block(rpc, request, &reply);
if (error) {
ovs_fatal(error, "transaction failed");
}
check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);

/* Print database contents. */
if (reply->result->type != JSON_ARRAY
Expand Down
2 changes: 1 addition & 1 deletion tests/test-ovsdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ do_idl(int argc, char *argv[])
substitute_uuids(json, symtab);
request = jsonrpc_create_request("transact", json, NULL);
error = jsonrpc_transact_block(rpc, request, &reply);
if (error) {
if (error || reply->error) {
ovs_fatal(error, "jsonrpc transaction failed");
}
printf("%03d: ", step++);
Expand Down

0 comments on commit d35f8e7

Please sign in to comment.