1 /* Copyright (c) 2009, 2010 Nicira Networks.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "ovsdb-idl.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
31 #include "ovsdb-data.h"
32 #include "ovsdb-error.h"
33 #include "ovsdb-idl-provider.h"
34 #include "poll-loop.h"
38 #define THIS_MODULE VLM_ovsdb_idl
41 /* An arc from one idl_row to another. When row A contains a UUID that
42 * references row B, this is represented by an arc from A (the source) to B
45 * Arcs from a row to itself are omitted, that is, src and dst are always
48 * Arcs are never duplicated, that is, even if there are multiple references
49 * from A to B, there is only a single arc from A to B.
51 * Arcs are directed: an arc from A to B is the converse of an an arc from B to
52 * A. Both an arc and its converse may both be present, if each row refers
53 * to the other circularly.
55 * The source and destination row may be in the same table or in different
58 struct ovsdb_idl_arc {
59 struct list src_node; /* In src->src_arcs list. */
60 struct list dst_node; /* In dst->dst_arcs list. */
61 struct ovsdb_idl_row *src; /* Source row. */
62 struct ovsdb_idl_row *dst; /* Destination row. */
66 const struct ovsdb_idl_class *class;
67 struct jsonrpc_session *session;
68 struct shash table_by_name;
69 struct ovsdb_idl_table *tables;
70 struct json *monitor_request_id;
71 unsigned int last_monitor_request_seqno;
72 unsigned int change_seqno;
74 /* Transaction support. */
75 struct ovsdb_idl_txn *txn;
76 struct hmap outstanding_txns;
79 struct ovsdb_idl_txn {
80 struct hmap_node hmap_node;
81 struct json *request_id;
82 struct ovsdb_idl *idl;
84 enum ovsdb_idl_txn_status status;
92 struct json *inc_where;
93 unsigned int inc_index;
94 int64_t inc_new_value;
97 struct hmap inserted_rows;
100 struct ovsdb_idl_txn_insert {
101 struct hmap_node hmap_node; /* In struct ovsdb_idl_txn's inserted_rows. */
102 struct uuid dummy; /* Dummy UUID used locally. */
103 int op_index; /* Index into transaction's operation array. */
104 struct uuid real; /* Real UUID used by database server. */
107 static struct vlog_rate_limit syntax_rl = VLOG_RATE_LIMIT_INIT(1, 5);
108 static struct vlog_rate_limit semantic_rl = VLOG_RATE_LIMIT_INIT(1, 5);
110 static void ovsdb_idl_clear(struct ovsdb_idl *);
111 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl *);
112 static void ovsdb_idl_parse_update(struct ovsdb_idl *, const struct json *);
113 static struct ovsdb_error *ovsdb_idl_parse_update__(struct ovsdb_idl *,
114 const struct json *);
115 static void ovsdb_idl_process_update(struct ovsdb_idl_table *,
117 const struct json *old,
118 const struct json *new);
119 static void ovsdb_idl_insert_row(struct ovsdb_idl_row *, const struct json *);
120 static void ovsdb_idl_delete_row(struct ovsdb_idl_row *);
121 static void ovsdb_idl_modify_row(struct ovsdb_idl_row *, const struct json *);
123 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *);
124 static struct ovsdb_idl_row *ovsdb_idl_row_create__(
125 const struct ovsdb_idl_table_class *);
126 static struct ovsdb_idl_row *ovsdb_idl_row_create(struct ovsdb_idl_table *,
127 const struct uuid *);
128 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row *);
130 static void ovsdb_idl_row_parse(struct ovsdb_idl_row *);
131 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row *);
132 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row *);
133 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row *);
135 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl *);
136 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl *,
137 const struct jsonrpc_msg *msg);
140 ovsdb_idl_create(const char *remote, const struct ovsdb_idl_class *class)
142 struct ovsdb_idl *idl;
145 idl = xzalloc(sizeof *idl);
147 idl->session = jsonrpc_session_open(remote);
148 shash_init(&idl->table_by_name);
149 idl->tables = xmalloc(class->n_tables * sizeof *idl->tables);
150 for (i = 0; i < class->n_tables; i++) {
151 const struct ovsdb_idl_table_class *tc = &class->tables[i];
152 struct ovsdb_idl_table *table = &idl->tables[i];
155 assert(!shash_find(&idl->table_by_name, tc->name));
156 shash_add(&idl->table_by_name, tc->name, table);
158 shash_init(&table->columns);
159 for (j = 0; j < tc->n_columns; j++) {
160 const struct ovsdb_idl_column *column = &tc->columns[j];
162 assert(!shash_find(&table->columns, column->name));
163 shash_add(&table->columns, column->name, column);
165 hmap_init(&table->rows);
168 idl->last_monitor_request_seqno = UINT_MAX;
169 hmap_init(&idl->outstanding_txns);
175 ovsdb_idl_destroy(struct ovsdb_idl *idl)
181 ovsdb_idl_clear(idl);
182 jsonrpc_session_close(idl->session);
184 for (i = 0; i < idl->class->n_tables; i++) {
185 struct ovsdb_idl_table *table = &idl->tables[i];
186 shash_destroy(&table->columns);
187 hmap_destroy(&table->rows);
189 shash_destroy(&idl->table_by_name);
191 json_destroy(idl->monitor_request_id);
197 ovsdb_idl_clear(struct ovsdb_idl *idl)
199 bool changed = false;
202 for (i = 0; i < idl->class->n_tables; i++) {
203 struct ovsdb_idl_table *table = &idl->tables[i];
204 struct ovsdb_idl_row *row, *next_row;
206 if (hmap_is_empty(&table->rows)) {
211 HMAP_FOR_EACH_SAFE (row, next_row, struct ovsdb_idl_row, hmap_node,
213 struct ovsdb_idl_arc *arc, *next_arc;
215 if (!ovsdb_idl_row_is_orphan(row)) {
216 ovsdb_idl_row_unparse(row);
218 LIST_FOR_EACH_SAFE (arc, next_arc, struct ovsdb_idl_arc, src_node,
222 /* No need to do anything with dst_arcs: some node has those arcs
223 * as forward arcs and will destroy them itself. */
225 ovsdb_idl_row_destroy(row);
235 ovsdb_idl_run(struct ovsdb_idl *idl)
237 unsigned int initial_change_seqno = idl->change_seqno;
241 jsonrpc_session_run(idl->session);
242 for (i = 0; jsonrpc_session_is_connected(idl->session) && i < 50; i++) {
243 struct jsonrpc_msg *msg, *reply;
246 seqno = jsonrpc_session_get_seqno(idl->session);
247 if (idl->last_monitor_request_seqno != seqno) {
248 idl->last_monitor_request_seqno = seqno;
249 ovsdb_idl_txn_abort_all(idl);
250 ovsdb_idl_send_monitor_request(idl);
254 msg = jsonrpc_session_recv(idl->session);
260 if (msg->type == JSONRPC_NOTIFY
261 && !strcmp(msg->method, "update")
262 && msg->params->type == JSON_ARRAY
263 && msg->params->u.array.n == 2
264 && msg->params->u.array.elems[0]->type == JSON_NULL) {
265 ovsdb_idl_parse_update(idl, msg->params->u.array.elems[1]);
266 } else if (msg->type == JSONRPC_REPLY
267 && idl->monitor_request_id
268 && json_equal(idl->monitor_request_id, msg->id)) {
269 json_destroy(idl->monitor_request_id);
270 idl->monitor_request_id = NULL;
271 ovsdb_idl_clear(idl);
272 ovsdb_idl_parse_update(idl, msg->result);
273 } else if (msg->type == JSONRPC_REPLY
274 && msg->id && msg->id->type == JSON_STRING
275 && !strcmp(msg->id->u.string, "echo")) {
276 /* It's a reply to our echo request. Ignore it. */
277 } else if ((msg->type == JSONRPC_ERROR
278 || msg->type == JSONRPC_REPLY)
279 && ovsdb_idl_txn_process_reply(idl, msg)) {
280 /* ovsdb_idl_txn_process_reply() did everything needful. */
282 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
283 * a transaction before we receive the reply, so keep the log level
285 VLOG_DBG("%s: received unexpected %s message",
286 jsonrpc_session_get_name(idl->session),
287 jsonrpc_msg_type_to_string(msg->type));
290 jsonrpc_session_send(idl->session, reply);
292 jsonrpc_msg_destroy(msg);
295 return initial_change_seqno != idl->change_seqno;
299 ovsdb_idl_wait(struct ovsdb_idl *idl)
301 jsonrpc_session_wait(idl->session);
302 jsonrpc_session_recv_wait(idl->session);
306 ovsdb_idl_get_seqno(const struct ovsdb_idl *idl)
308 return idl->change_seqno;
312 ovsdb_idl_has_ever_connected(const struct ovsdb_idl *idl)
314 return ovsdb_idl_get_seqno(idl) != 0;
318 ovsdb_idl_force_reconnect(struct ovsdb_idl *idl)
320 jsonrpc_session_force_reconnect(idl->session);
324 ovsdb_idl_send_monitor_request(struct ovsdb_idl *idl)
326 struct json *monitor_requests;
327 struct jsonrpc_msg *msg;
330 monitor_requests = json_object_create();
331 for (i = 0; i < idl->class->n_tables; i++) {
332 const struct ovsdb_idl_table *table = &idl->tables[i];
333 const struct ovsdb_idl_table_class *tc = table->class;
334 struct json *monitor_request, *columns;
337 monitor_request = json_object_create();
338 columns = json_array_create_empty();
339 for (i = 0; i < tc->n_columns; i++) {
340 const struct ovsdb_idl_column *column = &tc->columns[i];
341 json_array_add(columns, json_string_create(column->name));
343 json_object_put(monitor_request, "columns", columns);
344 json_object_put(monitor_requests, tc->name, monitor_request);
347 json_destroy(idl->monitor_request_id);
348 msg = jsonrpc_create_request(
350 json_array_create_3(json_string_create(idl->class->database),
351 json_null_create(), monitor_requests),
352 &idl->monitor_request_id);
353 jsonrpc_session_send(idl->session, msg);
357 ovsdb_idl_parse_update(struct ovsdb_idl *idl, const struct json *table_updates)
359 struct ovsdb_error *error;
363 error = ovsdb_idl_parse_update__(idl, table_updates);
365 if (!VLOG_DROP_WARN(&syntax_rl)) {
366 char *s = ovsdb_error_to_string(error);
367 VLOG_WARN_RL(&syntax_rl, "%s", s);
370 ovsdb_error_destroy(error);
374 static struct ovsdb_error *
375 ovsdb_idl_parse_update__(struct ovsdb_idl *idl,
376 const struct json *table_updates)
378 const struct shash_node *tables_node;
380 if (table_updates->type != JSON_OBJECT) {
381 return ovsdb_syntax_error(table_updates, NULL,
382 "<table-updates> is not an object");
384 SHASH_FOR_EACH (tables_node, json_object(table_updates)) {
385 const struct json *table_update = tables_node->data;
386 const struct shash_node *table_node;
387 struct ovsdb_idl_table *table;
389 table = shash_find_data(&idl->table_by_name, tables_node->name);
391 return ovsdb_syntax_error(
393 "<table-updates> includes unknown table \"%s\"",
397 if (table_update->type != JSON_OBJECT) {
398 return ovsdb_syntax_error(table_update, NULL,
399 "<table-update> for table \"%s\" is "
400 "not an object", table->class->name);
402 SHASH_FOR_EACH (table_node, json_object(table_update)) {
403 const struct json *row_update = table_node->data;
404 const struct json *old_json, *new_json;
407 if (!uuid_from_string(&uuid, table_node->name)) {
408 return ovsdb_syntax_error(table_update, NULL,
409 "<table-update> for table \"%s\" "
411 "\"%s\" as member name",
415 if (row_update->type != JSON_OBJECT) {
416 return ovsdb_syntax_error(row_update, NULL,
417 "<table-update> for table \"%s\" "
418 "contains <row-update> for %s that "
424 old_json = shash_find_data(json_object(row_update), "old");
425 new_json = shash_find_data(json_object(row_update), "new");
426 if (old_json && old_json->type != JSON_OBJECT) {
427 return ovsdb_syntax_error(old_json, NULL,
428 "\"old\" <row> is not object");
429 } else if (new_json && new_json->type != JSON_OBJECT) {
430 return ovsdb_syntax_error(new_json, NULL,
431 "\"new\" <row> is not object");
432 } else if ((old_json != NULL) + (new_json != NULL)
433 != shash_count(json_object(row_update))) {
434 return ovsdb_syntax_error(row_update, NULL,
435 "<row-update> contains unexpected "
437 } else if (!old_json && !new_json) {
438 return ovsdb_syntax_error(row_update, NULL,
439 "<row-update> missing \"old\" "
440 "and \"new\" members");
443 ovsdb_idl_process_update(table, &uuid, old_json, new_json);
450 static struct ovsdb_idl_row *
451 ovsdb_idl_get_row(struct ovsdb_idl_table *table, const struct uuid *uuid)
453 struct ovsdb_idl_row *row;
455 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, hmap_node,
456 uuid_hash(uuid), &table->rows) {
457 if (uuid_equals(&row->uuid, uuid)) {
465 ovsdb_idl_process_update(struct ovsdb_idl_table *table,
466 const struct uuid *uuid, const struct json *old,
467 const struct json *new)
469 struct ovsdb_idl_row *row;
471 row = ovsdb_idl_get_row(table, uuid);
474 if (row && !ovsdb_idl_row_is_orphan(row)) {
475 /* XXX perhaps we should check the 'old' values? */
476 ovsdb_idl_delete_row(row);
478 VLOG_WARN_RL(&semantic_rl, "cannot delete missing row "UUID_FMT" "
480 UUID_ARGS(uuid), table->class->name);
485 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
486 } else if (ovsdb_idl_row_is_orphan(row)) {
487 ovsdb_idl_insert_row(row, new);
489 VLOG_WARN_RL(&semantic_rl, "cannot add existing row "UUID_FMT" to "
490 "table %s", UUID_ARGS(uuid), table->class->name);
491 ovsdb_idl_modify_row(row, new);
496 /* XXX perhaps we should check the 'old' values? */
497 if (!ovsdb_idl_row_is_orphan(row)) {
498 ovsdb_idl_modify_row(row, new);
500 VLOG_WARN_RL(&semantic_rl, "cannot modify missing but "
501 "referenced row "UUID_FMT" in table %s",
502 UUID_ARGS(uuid), table->class->name);
503 ovsdb_idl_insert_row(row, new);
506 VLOG_WARN_RL(&semantic_rl, "cannot modify missing row "UUID_FMT" "
507 "in table %s", UUID_ARGS(uuid), table->class->name);
508 ovsdb_idl_insert_row(ovsdb_idl_row_create(table, uuid), new);
514 ovsdb_idl_row_update(struct ovsdb_idl_row *row, const struct json *row_json)
516 struct ovsdb_idl_table *table = row->table;
517 struct shash_node *node;
519 SHASH_FOR_EACH (node, json_object(row_json)) {
520 const char *column_name = node->name;
521 const struct ovsdb_idl_column *column;
522 struct ovsdb_datum datum;
523 struct ovsdb_error *error;
525 column = shash_find_data(&table->columns, column_name);
527 VLOG_WARN_RL(&syntax_rl, "unknown column %s updating row "UUID_FMT,
528 column_name, UUID_ARGS(&row->uuid));
532 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
534 ovsdb_datum_swap(&row->old[column - table->class->columns],
536 ovsdb_datum_destroy(&datum, &column->type);
538 char *s = ovsdb_error_to_string(error);
539 VLOG_WARN_RL(&syntax_rl, "error parsing column %s in row "UUID_FMT
540 " in table %s: %s", column_name,
541 UUID_ARGS(&row->uuid), table->class->name, s);
543 ovsdb_error_destroy(error);
548 /* When a row A refers to row B through a column with a "refTable" constraint,
549 * but row B does not exist, row B is called an "orphan row". Orphan rows
550 * should not persist, because the database enforces referential integrity, but
551 * they can appear transiently as changes from the database are received (the
552 * database doesn't try to topologically sort them and circular references mean
553 * it isn't always possible anyhow).
555 * This function returns true if 'row' is an orphan row, otherwise false.
558 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row *row)
560 return !row->old && !row->new;
563 /* Returns true if 'row' is conceptually part of the database as modified by
564 * the current transaction (if any), false otherwise.
566 * This function will return true if 'row' is not an orphan (see the comment on
567 * ovsdb_idl_row_is_orphan()) and:
569 * - 'row' exists in the database and has not been deleted within the
570 * current transaction (if any).
572 * - 'row' was inserted within the current transaction and has not been
573 * deleted. (In the latter case you should not have passed 'row' in at
574 * all, because ovsdb_idl_txn_delete() freed it.)
576 * This function will return false if 'row' is an orphan or if 'row' was
577 * deleted within the current transaction.
580 ovsdb_idl_row_exists(const struct ovsdb_idl_row *row)
582 return row->new != NULL;
586 ovsdb_idl_row_parse(struct ovsdb_idl_row *row)
588 const struct ovsdb_idl_table_class *class = row->table->class;
591 for (i = 0; i < class->n_columns; i++) {
592 const struct ovsdb_idl_column *c = &class->columns[i];
593 (c->parse)(row, &row->old[i]);
598 ovsdb_idl_row_unparse(struct ovsdb_idl_row *row)
600 const struct ovsdb_idl_table_class *class = row->table->class;
603 for (i = 0; i < class->n_columns; i++) {
604 const struct ovsdb_idl_column *c = &class->columns[i];
610 ovsdb_idl_row_clear_old(struct ovsdb_idl_row *row)
612 assert(row->old == row->new);
613 if (!ovsdb_idl_row_is_orphan(row)) {
614 const struct ovsdb_idl_table_class *class = row->table->class;
617 for (i = 0; i < class->n_columns; i++) {
618 ovsdb_datum_destroy(&row->old[i], &class->columns[i].type);
621 row->old = row->new = NULL;
626 ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
628 if (row->old != row->new) {
630 const struct ovsdb_idl_table_class *class = row->table->class;
633 BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
634 ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
645 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row *row, bool destroy_dsts)
647 struct ovsdb_idl_arc *arc, *next;
649 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
650 * that this causes to be unreferenced. */
651 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, src_node,
653 list_remove(&arc->dst_node);
655 && ovsdb_idl_row_is_orphan(arc->dst)
656 && list_is_empty(&arc->dst->dst_arcs)) {
657 ovsdb_idl_row_destroy(arc->dst);
661 list_init(&row->src_arcs);
664 /* Force nodes that reference 'row' to reparse. */
666 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row *row)
668 struct ovsdb_idl_arc *arc, *next;
670 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
671 * 'arc', so we need to use the "safe" variant of list traversal. However,
672 * calling an ovsdb_idl_column's 'parse' function will add an arc
673 * equivalent to 'arc' to row->arcs. That could be a problem for
674 * traversal, but it adds it at the beginning of the list to prevent us
675 * from stumbling upon it again.
677 * (If duplicate arcs were possible then we would need to make sure that
678 * 'next' didn't also point into 'arc''s destination, but we forbid
679 * duplicate arcs.) */
680 LIST_FOR_EACH_SAFE (arc, next, struct ovsdb_idl_arc, dst_node,
682 struct ovsdb_idl_row *ref = arc->src;
684 ovsdb_idl_row_unparse(ref);
685 ovsdb_idl_row_clear_arcs(ref, false);
686 ovsdb_idl_row_parse(ref);
690 static struct ovsdb_idl_row *
691 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class *class)
693 struct ovsdb_idl_row *row = xzalloc(class->allocation_size);
694 list_init(&row->src_arcs);
695 list_init(&row->dst_arcs);
696 hmap_node_nullify(&row->txn_node);
700 static struct ovsdb_idl_row *
701 ovsdb_idl_row_create(struct ovsdb_idl_table *table, const struct uuid *uuid)
703 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(table->class);
704 hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
711 ovsdb_idl_row_destroy(struct ovsdb_idl_row *row)
714 ovsdb_idl_row_clear_old(row);
715 hmap_remove(&row->table->rows, &row->hmap_node);
721 ovsdb_idl_insert_row(struct ovsdb_idl_row *row, const struct json *row_json)
723 const struct ovsdb_idl_table_class *class = row->table->class;
726 assert(!row->old && !row->new);
727 row->old = row->new = xmalloc(class->n_columns * sizeof *row->old);
728 for (i = 0; i < class->n_columns; i++) {
729 ovsdb_datum_init_default(&row->old[i], &class->columns[i].type);
731 ovsdb_idl_row_update(row, row_json);
732 ovsdb_idl_row_parse(row);
734 ovsdb_idl_row_reparse_backrefs(row);
738 ovsdb_idl_delete_row(struct ovsdb_idl_row *row)
740 ovsdb_idl_row_unparse(row);
741 ovsdb_idl_row_clear_arcs(row, true);
742 ovsdb_idl_row_clear_old(row);
743 if (list_is_empty(&row->dst_arcs)) {
744 ovsdb_idl_row_destroy(row);
746 ovsdb_idl_row_reparse_backrefs(row);
751 ovsdb_idl_modify_row(struct ovsdb_idl_row *row, const struct json *row_json)
753 ovsdb_idl_row_unparse(row);
754 ovsdb_idl_row_clear_arcs(row, true);
755 ovsdb_idl_row_update(row, row_json);
756 ovsdb_idl_row_parse(row);
760 may_add_arc(const struct ovsdb_idl_row *src, const struct ovsdb_idl_row *dst)
762 const struct ovsdb_idl_arc *arc;
769 /* No duplicate arcs.
771 * We only need to test whether the first arc in dst->dst_arcs originates
772 * at 'src', since we add all of the arcs from a given source in a clump
773 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
774 * added at the front of the dst_arcs list. */
775 if (list_is_empty(&dst->dst_arcs)) {
778 arc = CONTAINER_OF(dst->dst_arcs.next, struct ovsdb_idl_arc, dst_node);
779 return arc->src != src;
782 static struct ovsdb_idl_table *
783 ovsdb_idl_table_from_class(const struct ovsdb_idl *idl,
784 const struct ovsdb_idl_table_class *table_class)
786 return &idl->tables[table_class - idl->class->tables];
789 struct ovsdb_idl_row *
790 ovsdb_idl_get_row_arc(struct ovsdb_idl_row *src,
791 struct ovsdb_idl_table_class *dst_table_class,
792 const struct uuid *dst_uuid)
794 struct ovsdb_idl *idl = src->table->idl;
795 struct ovsdb_idl_table *dst_table;
796 struct ovsdb_idl_arc *arc;
797 struct ovsdb_idl_row *dst;
799 dst_table = ovsdb_idl_table_from_class(idl, dst_table_class);
800 dst = ovsdb_idl_get_row(dst_table, dst_uuid);
802 /* We're being called from ovsdb_idl_txn_write(). We must not update
803 * any arcs, because the transaction will be backed out at commit or
804 * abort time and we don't want our graph screwed up.
806 * Just return the destination row, if there is one and it has not been
808 if (dst && (hmap_node_is_null(&dst->txn_node) || dst->new)) {
813 /* We're being called from some other context. Update the graph. */
815 dst = ovsdb_idl_row_create(dst_table, dst_uuid);
818 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
819 if (may_add_arc(src, dst)) {
820 /* The arc *must* be added at the front of the dst_arcs list. See
821 * ovsdb_idl_row_reparse_backrefs() for details. */
822 arc = xmalloc(sizeof *arc);
823 list_push_front(&src->src_arcs, &arc->src_node);
824 list_push_front(&dst->dst_arcs, &arc->dst_node);
829 return !ovsdb_idl_row_is_orphan(dst) ? dst : NULL;
833 const struct ovsdb_idl_row *
834 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl *idl,
835 const struct ovsdb_idl_table_class *tc,
836 const struct uuid *uuid)
838 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl, tc), uuid);
841 static struct ovsdb_idl_row *
842 next_real_row(struct ovsdb_idl_table *table, struct hmap_node *node)
844 for (; node; node = hmap_next(&table->rows, node)) {
845 struct ovsdb_idl_row *row;
847 row = CONTAINER_OF(node, struct ovsdb_idl_row, hmap_node);
848 if (ovsdb_idl_row_exists(row)) {
855 const struct ovsdb_idl_row *
856 ovsdb_idl_first_row(const struct ovsdb_idl *idl,
857 const struct ovsdb_idl_table_class *table_class)
859 struct ovsdb_idl_table *table
860 = ovsdb_idl_table_from_class(idl, table_class);
861 return next_real_row(table, hmap_first(&table->rows));
864 const struct ovsdb_idl_row *
865 ovsdb_idl_next_row(const struct ovsdb_idl_row *row)
867 struct ovsdb_idl_table *table = row->table;
869 return next_real_row(table, hmap_next(&table->rows, &row->hmap_node));
874 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
875 enum ovsdb_idl_txn_status);
878 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status)
897 struct ovsdb_idl_txn *
898 ovsdb_idl_txn_create(struct ovsdb_idl *idl)
900 struct ovsdb_idl_txn *txn;
903 idl->txn = txn = xmalloc(sizeof *txn);
904 txn->request_id = NULL;
906 hmap_init(&txn->txn_rows);
907 txn->status = TXN_INCOMPLETE;
909 txn->dry_run = false;
910 ds_init(&txn->comment);
912 txn->inc_table = NULL;
913 txn->inc_column = NULL;
914 txn->inc_where = NULL;
916 hmap_init(&txn->inserted_rows);
921 /* Appends 's', which is treated as a printf()-type format string, to the
922 * comments that will be passed to the OVSDB server when 'txn' is committed.
923 * (The comment will be committed to the OVSDB log, which "ovsdb-tool
924 * show-log" can print in a relatively human-readable form.) */
926 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn *txn, const char *s, ...)
930 if (txn->comment.length) {
931 ds_put_char(&txn->comment, '\n');
935 ds_put_format_valist(&txn->comment, s, args);
940 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn *txn)
946 ovsdb_idl_txn_increment(struct ovsdb_idl_txn *txn, const char *table,
947 const char *column, const struct json *where)
949 assert(!txn->inc_table);
950 txn->inc_table = xstrdup(table);
951 txn->inc_column = xstrdup(column);
952 txn->inc_where = where ? json_clone(where) : json_array_create_empty();
956 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn *txn)
958 struct ovsdb_idl_txn_insert *insert, *next;
960 json_destroy(txn->request_id);
961 if (txn->status == TXN_INCOMPLETE) {
962 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
964 ovsdb_idl_txn_abort(txn);
965 ds_destroy(&txn->comment);
967 free(txn->inc_table);
968 free(txn->inc_column);
969 json_destroy(txn->inc_where);
970 HMAP_FOR_EACH_SAFE (insert, next, struct ovsdb_idl_txn_insert, hmap_node,
971 &txn->inserted_rows) {
974 hmap_destroy(&txn->inserted_rows);
979 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn *txn)
981 if (txn->status != TXN_INCOMPLETE) {
982 poll_immediate_wake();
987 where_uuid_equals(const struct uuid *uuid)
992 json_string_create("_uuid"),
993 json_string_create("=="),
995 json_string_create("uuid"),
996 json_string_create_nocopy(
997 xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
1001 uuid_name_from_uuid(const struct uuid *uuid)
1006 name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
1007 for (p = name; *p != '\0'; p++) {
1016 static const struct ovsdb_idl_row *
1017 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn *txn, const struct uuid *uuid)
1019 const struct ovsdb_idl_row *row;
1021 HMAP_FOR_EACH_WITH_HASH (row, struct ovsdb_idl_row, txn_node,
1022 uuid_hash(uuid), &txn->txn_rows) {
1023 if (uuid_equals(&row->uuid, uuid)) {
1030 /* XXX there must be a cleaner way to do this */
1031 static struct json *
1032 substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
1034 if (json->type == JSON_ARRAY) {
1038 if (json->u.array.n == 2
1039 && json->u.array.elems[0]->type == JSON_STRING
1040 && json->u.array.elems[1]->type == JSON_STRING
1041 && !strcmp(json->u.array.elems[0]->u.string, "uuid")
1042 && uuid_from_string(&uuid, json->u.array.elems[1]->u.string)) {
1043 const struct ovsdb_idl_row *row;
1045 row = ovsdb_idl_txn_get_row(txn, &uuid);
1046 if (row && !row->old && row->new) {
1049 return json_array_create_2(
1050 json_string_create("named-uuid"),
1051 json_string_create_nocopy(uuid_name_from_uuid(&uuid)));
1055 for (i = 0; i < json->u.array.n; i++) {
1056 json->u.array.elems[i] = substitute_uuids(json->u.array.elems[i],
1059 } else if (json->type == JSON_OBJECT) {
1060 struct shash_node *node;
1062 SHASH_FOR_EACH (node, json_object(json)) {
1063 node->data = substitute_uuids(node->data, txn);
1070 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn *txn)
1072 struct ovsdb_idl_row *row, *next;
1074 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1075 * ovsdb_idl_column's 'parse' function, which will call
1076 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1077 * transaction and fail to update the graph. */
1078 txn->idl->txn = NULL;
1080 HMAP_FOR_EACH_SAFE (row, next, struct ovsdb_idl_row, txn_node,
1084 ovsdb_idl_row_unparse(row);
1085 ovsdb_idl_row_clear_arcs(row, false);
1086 ovsdb_idl_row_parse(row);
1089 ovsdb_idl_row_unparse(row);
1091 ovsdb_idl_row_clear_new(row);
1094 row->prereqs = NULL;
1097 row->written = NULL;
1099 hmap_remove(&txn->txn_rows, &row->txn_node);
1100 hmap_node_nullify(&row->txn_node);
1102 hmap_remove(&row->table->rows, &row->hmap_node);
1106 hmap_destroy(&txn->txn_rows);
1107 hmap_init(&txn->txn_rows);
1110 enum ovsdb_idl_txn_status
1111 ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
1113 struct ovsdb_idl_row *row;
1114 struct json *operations;
1117 if (txn != txn->idl->txn) {
1121 operations = json_array_create_1(
1122 json_string_create(txn->idl->class->database));
1124 /* Add prerequisites and declarations of new rows. */
1125 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1126 /* XXX check that deleted rows exist even if no prereqs? */
1128 const struct ovsdb_idl_table_class *class = row->table->class;
1129 size_t n_columns = class->n_columns;
1130 struct json *op, *columns, *row_json;
1133 op = json_object_create();
1134 json_array_add(operations, op);
1135 json_object_put_string(op, "op", "wait");
1136 json_object_put_string(op, "table", class->name);
1137 json_object_put(op, "timeout", json_integer_create(0));
1138 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1139 json_object_put_string(op, "until", "==");
1140 columns = json_array_create_empty();
1141 json_object_put(op, "columns", columns);
1142 row_json = json_object_create();
1143 json_object_put(op, "rows", json_array_create_1(row_json));
1145 BITMAP_FOR_EACH_1 (idx, n_columns, row->prereqs) {
1146 const struct ovsdb_idl_column *column = &class->columns[idx];
1147 json_array_add(columns, json_string_create(column->name));
1148 json_object_put(row_json, column->name,
1149 ovsdb_datum_to_json(&row->old[idx],
1156 any_updates = false;
1157 HMAP_FOR_EACH (row, struct ovsdb_idl_row, txn_node, &txn->txn_rows) {
1158 const struct ovsdb_idl_table_class *class = row->table->class;
1160 if (row->old == row->new) {
1162 } else if (!row->new) {
1163 struct json *op = json_object_create();
1164 json_object_put_string(op, "op", "delete");
1165 json_object_put_string(op, "table", class->name);
1166 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1167 json_array_add(operations, op);
1170 struct json *row_json;
1174 op = json_object_create();
1175 json_object_put_string(op, "op", row->old ? "update" : "insert");
1176 json_object_put_string(op, "table", class->name);
1178 json_object_put(op, "where", where_uuid_equals(&row->uuid));
1180 struct ovsdb_idl_txn_insert *insert;
1182 json_object_put(op, "uuid-name",
1183 json_string_create_nocopy(
1184 uuid_name_from_uuid(&row->uuid)));
1186 insert = xmalloc(sizeof *insert);
1187 insert->dummy = row->uuid;
1188 insert->op_index = operations->u.array.n - 1;
1189 uuid_zero(&insert->real);
1190 hmap_insert(&txn->inserted_rows, &insert->hmap_node,
1191 uuid_hash(&insert->dummy));
1193 row_json = json_object_create();
1194 json_object_put(op, "row", row_json);
1196 BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
1197 const struct ovsdb_idl_column *column = &class->columns[idx];
1200 ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
1202 : !ovsdb_datum_is_default(&row->new[idx], &column->type)) {
1203 json_object_put(row_json, column->name,
1205 ovsdb_datum_to_json(&row->new[idx],
1211 if (!row->old || !shash_is_empty(json_object(row_json))) {
1212 json_array_add(operations, op);
1220 /* Add increment. */
1221 if (txn->inc_table && any_updates) {
1224 txn->inc_index = operations->u.array.n - 1;
1226 op = json_object_create();
1227 json_object_put_string(op, "op", "mutate");
1228 json_object_put_string(op, "table", txn->inc_table);
1229 json_object_put(op, "where",
1230 substitute_uuids(json_clone(txn->inc_where), txn));
1231 json_object_put(op, "mutations",
1232 json_array_create_1(
1233 json_array_create_3(
1234 json_string_create(txn->inc_column),
1235 json_string_create("+="),
1236 json_integer_create(1))));
1237 json_array_add(operations, op);
1239 op = json_object_create();
1240 json_object_put_string(op, "op", "select");
1241 json_object_put_string(op, "table", txn->inc_table);
1242 json_object_put(op, "where",
1243 substitute_uuids(json_clone(txn->inc_where), txn));
1244 json_object_put(op, "columns",
1245 json_array_create_1(json_string_create(
1247 json_array_add(operations, op);
1250 if (txn->comment.length) {
1251 struct json *op = json_object_create();
1252 json_object_put_string(op, "op", "comment");
1253 json_object_put_string(op, "comment", ds_cstr(&txn->comment));
1254 json_array_add(operations, op);
1258 struct json *op = json_object_create();
1259 json_object_put_string(op, "op", "abort");
1260 json_array_add(operations, op);
1264 txn->status = TXN_UNCHANGED;
1265 json_destroy(operations);
1266 } else if (!jsonrpc_session_send(
1268 jsonrpc_create_request(
1269 "transact", operations, &txn->request_id))) {
1270 hmap_insert(&txn->idl->outstanding_txns, &txn->hmap_node,
1271 json_hash(txn->request_id, 0));
1273 txn->status = TXN_TRY_AGAIN;
1276 ovsdb_idl_txn_disassemble(txn);
1280 /* Attempts to commit 'txn', blocking until the commit either succeeds or
1281 * fails. Returns the final commit status, which may be any TXN_* value other
1282 * than TXN_INCOMPLETE. */
1283 enum ovsdb_idl_txn_status
1284 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn *txn)
1286 enum ovsdb_idl_txn_status status;
1289 while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
1290 ovsdb_idl_run(txn->idl);
1291 ovsdb_idl_wait(txn->idl);
1292 ovsdb_idl_txn_wait(txn);
1299 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn *txn)
1301 assert(txn->status == TXN_SUCCESS);
1302 return txn->inc_new_value;
1306 ovsdb_idl_txn_abort(struct ovsdb_idl_txn *txn)
1308 ovsdb_idl_txn_disassemble(txn);
1309 if (txn->status == TXN_INCOMPLETE) {
1310 txn->status = TXN_ABORTED;
1315 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn *txn)
1317 if (txn->status != TXN_ERROR) {
1318 return ovsdb_idl_txn_status_to_string(txn->status);
1319 } else if (txn->error) {
1322 return "no error details available";
1327 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn *txn,
1328 const struct json *json)
1330 if (txn->error == NULL) {
1331 txn->error = json_to_string(json, JSSF_SORT);
1335 /* For transaction 'txn' that completed successfully, finds and returns the
1336 * permanent UUID that the database assigned to a newly inserted row, given the
1337 * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
1339 * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
1340 * if it was assigned by that function and then deleted by
1341 * ovsdb_idl_txn_delete() within the same transaction. (Rows that are inserted
1342 * and then deleted within a single transaction are never sent to the database
1343 * server, so it never assigns them a permanent UUID.) */
1345 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn *txn,
1346 const struct uuid *uuid)
1348 const struct ovsdb_idl_txn_insert *insert;
1350 assert(txn->status == TXN_SUCCESS || txn->status == TXN_UNCHANGED);
1351 HMAP_FOR_EACH_IN_BUCKET (insert, struct ovsdb_idl_txn_insert, hmap_node,
1352 uuid_hash(uuid), &txn->inserted_rows) {
1353 if (uuid_equals(uuid, &insert->dummy)) {
1354 return &insert->real;
1361 ovsdb_idl_txn_complete(struct ovsdb_idl_txn *txn,
1362 enum ovsdb_idl_txn_status status)
1364 txn->status = status;
1365 hmap_remove(&txn->idl->outstanding_txns, &txn->hmap_node);
1369 ovsdb_idl_txn_read(const struct ovsdb_idl_row *row,
1370 const struct ovsdb_idl_column *column,
1371 struct ovsdb_datum *datum)
1373 const struct ovsdb_idl_table_class *class = row->table->class;
1374 size_t column_idx = column - class->columns;
1376 assert(row->new != NULL);
1377 if (row->written && bitmap_is_set(row->written, column_idx)) {
1378 ovsdb_datum_clone(datum, &row->new[column_idx], &column->type);
1379 } else if (row->old) {
1380 ovsdb_datum_clone(datum, &row->old[column_idx], &column->type);
1382 ovsdb_datum_init_default(datum, &column->type);
1387 ovsdb_idl_txn_write(const struct ovsdb_idl_row *row_,
1388 const struct ovsdb_idl_column *column,
1389 struct ovsdb_datum *datum)
1391 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1392 const struct ovsdb_idl_table_class *class = row->table->class;
1393 size_t column_idx = column - class->columns;
1395 assert(row->new != NULL);
1396 assert(column_idx < class->n_columns);
1397 if (hmap_node_is_null(&row->txn_node)) {
1398 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1399 uuid_hash(&row->uuid));
1401 if (row->old == row->new) {
1402 row->new = xmalloc(class->n_columns * sizeof *row->new);
1404 if (!row->written) {
1405 row->written = bitmap_allocate(class->n_columns);
1407 if (bitmap_is_set(row->written, column_idx)) {
1408 ovsdb_datum_destroy(&row->new[column_idx], &column->type);
1410 bitmap_set1(row->written, column_idx);
1412 row->new[column_idx] = *datum;
1413 (column->unparse)(row);
1414 (column->parse)(row, &row->new[column_idx]);
1418 ovsdb_idl_txn_verify(const struct ovsdb_idl_row *row_,
1419 const struct ovsdb_idl_column *column)
1421 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1422 const struct ovsdb_idl_table_class *class = row->table->class;
1423 size_t column_idx = column - class->columns;
1425 assert(row->new != NULL);
1427 || (row->written && bitmap_is_set(row->written, column_idx))) {
1431 if (hmap_node_is_null(&row->txn_node)) {
1432 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1433 uuid_hash(&row->uuid));
1435 if (!row->prereqs) {
1436 row->prereqs = bitmap_allocate(class->n_columns);
1438 bitmap_set1(row->prereqs, column_idx);
1442 ovsdb_idl_txn_delete(const struct ovsdb_idl_row *row_)
1444 struct ovsdb_idl_row *row = (struct ovsdb_idl_row *) row_;
1446 assert(row->new != NULL);
1448 ovsdb_idl_row_unparse(row);
1449 ovsdb_idl_row_clear_new(row);
1450 assert(!row->prereqs);
1451 hmap_remove(&row->table->rows, &row->hmap_node);
1452 hmap_remove(&row->table->idl->txn->txn_rows, &row->txn_node);
1456 if (hmap_node_is_null(&row->txn_node)) {
1457 hmap_insert(&row->table->idl->txn->txn_rows, &row->txn_node,
1458 uuid_hash(&row->uuid));
1460 ovsdb_idl_row_clear_new(row);
1464 const struct ovsdb_idl_row *
1465 ovsdb_idl_txn_insert(struct ovsdb_idl_txn *txn,
1466 const struct ovsdb_idl_table_class *class,
1467 const struct uuid *uuid)
1469 struct ovsdb_idl_row *row = ovsdb_idl_row_create__(class);
1472 assert(!ovsdb_idl_txn_get_row(txn, uuid));
1475 uuid_generate(&row->uuid);
1478 row->table = ovsdb_idl_table_from_class(txn->idl, class);
1479 row->new = xmalloc(class->n_columns * sizeof *row->new);
1480 row->written = bitmap_allocate(class->n_columns);
1481 hmap_insert(&row->table->rows, &row->hmap_node, uuid_hash(&row->uuid));
1482 hmap_insert(&txn->txn_rows, &row->txn_node, uuid_hash(&row->uuid));
1487 ovsdb_idl_txn_abort_all(struct ovsdb_idl *idl)
1489 struct ovsdb_idl_txn *txn;
1491 HMAP_FOR_EACH (txn, struct ovsdb_idl_txn, hmap_node,
1492 &idl->outstanding_txns) {
1493 ovsdb_idl_txn_complete(txn, TXN_TRY_AGAIN);
1497 static struct ovsdb_idl_txn *
1498 ovsdb_idl_txn_find(struct ovsdb_idl *idl, const struct json *id)
1500 struct ovsdb_idl_txn *txn;
1502 HMAP_FOR_EACH_WITH_HASH (txn, struct ovsdb_idl_txn, hmap_node,
1503 json_hash(id, 0), &idl->outstanding_txns) {
1504 if (json_equal(id, txn->request_id)) {
1512 check_json_type(const struct json *json, enum json_type type, const char *name)
1515 VLOG_WARN_RL(&syntax_rl, "%s is missing", name);
1517 } else if (json->type != type) {
1518 VLOG_WARN_RL(&syntax_rl, "%s is %s instead of %s",
1519 name, json_type_to_string(json->type),
1520 json_type_to_string(type));
1528 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn *txn,
1529 const struct json_array *results)
1531 struct json *count, *rows, *row, *column;
1532 struct shash *mutate, *select;
1534 if (txn->inc_index + 2 > results->n) {
1535 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1536 "for increment (has %zu, needs %u)",
1537 results->n, txn->inc_index + 2);
1541 /* We know that this is a JSON object because the loop in
1542 * ovsdb_idl_txn_process_reply() checked. */
1543 mutate = json_object(results->elems[txn->inc_index]);
1544 count = shash_find_data(mutate, "count");
1545 if (!check_json_type(count, JSON_INTEGER, "\"mutate\" reply \"count\"")) {
1548 if (count->u.integer != 1) {
1549 VLOG_WARN_RL(&syntax_rl,
1550 "\"mutate\" reply \"count\" is %lld instead of 1",
1555 select = json_object(results->elems[txn->inc_index + 1]);
1556 rows = shash_find_data(select, "rows");
1557 if (!check_json_type(rows, JSON_ARRAY, "\"select\" reply \"rows\"")) {
1560 if (rows->u.array.n != 1) {
1561 VLOG_WARN_RL(&syntax_rl, "\"select\" reply \"rows\" has %zu elements "
1566 row = rows->u.array.elems[0];
1567 if (!check_json_type(row, JSON_OBJECT, "\"select\" reply row")) {
1570 column = shash_find_data(json_object(row), txn->inc_column);
1571 if (!check_json_type(column, JSON_INTEGER,
1572 "\"select\" reply inc column")) {
1575 txn->inc_new_value = column->u.integer;
1580 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert *insert,
1581 const struct json_array *results)
1583 static const struct ovsdb_base_type uuid_type = OVSDB_BASE_UUID_INIT;
1584 struct ovsdb_error *error;
1585 struct json *json_uuid;
1586 union ovsdb_atom uuid;
1587 struct shash *reply;
1589 if (insert->op_index >= results->n) {
1590 VLOG_WARN_RL(&syntax_rl, "reply does not contain enough operations "
1591 "for insert (has %zu, needs %u)",
1592 results->n, insert->op_index);
1596 /* We know that this is a JSON object because the loop in
1597 * ovsdb_idl_txn_process_reply() checked. */
1598 reply = json_object(results->elems[insert->op_index]);
1599 json_uuid = shash_find_data(reply, "uuid");
1600 if (!check_json_type(json_uuid, JSON_ARRAY, "\"insert\" reply \"uuid\"")) {
1604 error = ovsdb_atom_from_json(&uuid, &uuid_type, json_uuid, NULL);
1606 char *s = ovsdb_error_to_string(error);
1607 VLOG_WARN_RL(&syntax_rl, "\"insert\" reply \"uuid\" is not a JSON "
1613 insert->real = uuid.uuid;
1619 ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
1620 const struct jsonrpc_msg *msg)
1622 struct ovsdb_idl_txn *txn;
1623 enum ovsdb_idl_txn_status status;
1625 txn = ovsdb_idl_txn_find(idl, msg->id);
1630 if (msg->type == JSONRPC_ERROR) {
1632 } else if (msg->result->type != JSON_ARRAY) {
1633 VLOG_WARN_RL(&syntax_rl, "reply to \"transact\" is not JSON array");
1636 struct json_array *ops = &msg->result->u.array;
1637 int hard_errors = 0;
1638 int soft_errors = 0;
1641 for (i = 0; i < ops->n; i++) {
1642 struct json *op = ops->elems[i];
1644 if (op->type == JSON_NULL) {
1645 /* This isn't an error in itself but indicates that some prior
1646 * operation failed, so make sure that we know about it. */
1648 } else if (op->type == JSON_OBJECT) {
1651 error = shash_find_data(json_object(op), "error");
1653 if (error->type == JSON_STRING) {
1654 if (!strcmp(error->u.string, "timed out")) {
1656 } else if (strcmp(error->u.string, "aborted")) {
1658 ovsdb_idl_txn_set_error_json(txn, op);
1662 ovsdb_idl_txn_set_error_json(txn, op);
1663 VLOG_WARN_RL(&syntax_rl,
1664 "\"error\" in reply is not JSON string");
1669 ovsdb_idl_txn_set_error_json(txn, op);
1670 VLOG_WARN_RL(&syntax_rl,
1671 "operation reply is not JSON null or object");
1675 if (!soft_errors && !hard_errors) {
1676 struct ovsdb_idl_txn_insert *insert;
1678 if (txn->inc_table && !ovsdb_idl_txn_process_inc_reply(txn, ops)) {
1682 HMAP_FOR_EACH (insert, struct ovsdb_idl_txn_insert, hmap_node,
1683 &txn->inserted_rows) {
1684 if (!ovsdb_idl_txn_process_insert_reply(insert, ops)) {
1690 status = (hard_errors ? TXN_ERROR
1691 : soft_errors ? TXN_TRY_AGAIN
1695 ovsdb_idl_txn_complete(txn, status);
1699 struct ovsdb_idl_txn *
1700 ovsdb_idl_txn_get(const struct ovsdb_idl_row *row)
1702 struct ovsdb_idl_txn *txn = row->table->idl->txn;
1703 assert(txn != NULL);
1708 ovsdb_idl_txn_get_idl (struct ovsdb_idl_txn *txn)