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"
30 #include "ovsdb-data.h"
31 #include "ovsdb-error.h"
32 #include "ovsdb-idl-provider.h"
33 #include "poll-loop.h"
37 #define THIS_MODULE VLM_ovsdb_idl
40 /* An arc from one idl_row to another. When row A contains a UUID that
41 * references row B, this is represented by an arc from A (the source) to B
44 * Arcs from a row to itself are omitted, that is, src and dst are always
47 * Arcs are never duplicated, that is, even if there are multiple references
48 * from A to B, there is only a single arc from A to B.
50 * Arcs are directed: an arc from A to B is the converse of an an arc from B to
51 * A. Both an arc and its converse may both be present, if each row refers
52 * to the other circularly.
54 * The source and destination row may be in the same table or in different
57 struct ovsdb_idl_arc
{
58 struct list src_node
; /* In src->src_arcs list. */
59 struct list dst_node
; /* In dst->dst_arcs list. */
60 struct ovsdb_idl_row
*src
; /* Source row. */
61 struct ovsdb_idl_row
*dst
; /* Destination row. */
65 const struct ovsdb_idl_class
*class;
66 struct jsonrpc_session
*session
;
67 struct shash table_by_name
;
68 struct ovsdb_idl_table
*tables
;
69 struct json
*monitor_request_id
;
70 unsigned int last_monitor_request_seqno
;
71 unsigned int change_seqno
;
73 /* Transaction support. */
74 struct ovsdb_idl_txn
*txn
;
75 struct hmap outstanding_txns
;
78 struct ovsdb_idl_txn
{
79 struct hmap_node hmap_node
;
80 struct json
*request_id
;
81 struct ovsdb_idl
*idl
;
83 enum ovsdb_idl_txn_status status
;
91 struct json
*inc_where
;
92 unsigned int inc_index
;
93 int64_t inc_new_value
;
96 struct hmap inserted_rows
;
99 struct ovsdb_idl_txn_insert
{
100 struct hmap_node hmap_node
; /* In struct ovsdb_idl_txn's inserted_rows. */
101 struct uuid dummy
; /* Dummy UUID used locally. */
102 int op_index
; /* Index into transaction's operation array. */
103 struct uuid real
; /* Real UUID used by database server. */
106 static struct vlog_rate_limit syntax_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
107 static struct vlog_rate_limit semantic_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
109 static void ovsdb_idl_clear(struct ovsdb_idl
*);
110 static void ovsdb_idl_send_monitor_request(struct ovsdb_idl
*);
111 static void ovsdb_idl_parse_update(struct ovsdb_idl
*, const struct json
*);
112 static struct ovsdb_error
*ovsdb_idl_parse_update__(struct ovsdb_idl
*,
113 const struct json
*);
114 static void ovsdb_idl_process_update(struct ovsdb_idl_table
*,
116 const struct json
*old
,
117 const struct json
*new);
118 static void ovsdb_idl_insert_row(struct ovsdb_idl_row
*, const struct json
*);
119 static void ovsdb_idl_delete_row(struct ovsdb_idl_row
*);
120 static void ovsdb_idl_modify_row(struct ovsdb_idl_row
*, const struct json
*);
122 static bool ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row
*);
123 static struct ovsdb_idl_row
*ovsdb_idl_row_create__(
124 const struct ovsdb_idl_table_class
*);
125 static struct ovsdb_idl_row
*ovsdb_idl_row_create(struct ovsdb_idl_table
*,
126 const struct uuid
*);
127 static void ovsdb_idl_row_destroy(struct ovsdb_idl_row
*);
129 static void ovsdb_idl_row_parse(struct ovsdb_idl_row
*);
130 static void ovsdb_idl_row_unparse(struct ovsdb_idl_row
*);
131 static void ovsdb_idl_row_clear_old(struct ovsdb_idl_row
*);
132 static void ovsdb_idl_row_clear_new(struct ovsdb_idl_row
*);
134 static void ovsdb_idl_txn_abort_all(struct ovsdb_idl
*);
135 static bool ovsdb_idl_txn_process_reply(struct ovsdb_idl
*,
136 const struct jsonrpc_msg
*msg
);
139 ovsdb_idl_create(const char *remote
, const struct ovsdb_idl_class
*class)
141 struct ovsdb_idl
*idl
;
144 idl
= xzalloc(sizeof *idl
);
146 idl
->session
= jsonrpc_session_open(remote
);
147 shash_init(&idl
->table_by_name
);
148 idl
->tables
= xmalloc(class->n_tables
* sizeof *idl
->tables
);
149 for (i
= 0; i
< class->n_tables
; i
++) {
150 const struct ovsdb_idl_table_class
*tc
= &class->tables
[i
];
151 struct ovsdb_idl_table
*table
= &idl
->tables
[i
];
154 assert(!shash_find(&idl
->table_by_name
, tc
->name
));
155 shash_add(&idl
->table_by_name
, tc
->name
, table
);
157 shash_init(&table
->columns
);
158 for (j
= 0; j
< tc
->n_columns
; j
++) {
159 const struct ovsdb_idl_column
*column
= &tc
->columns
[j
];
161 assert(!shash_find(&table
->columns
, column
->name
));
162 shash_add(&table
->columns
, column
->name
, column
);
164 hmap_init(&table
->rows
);
167 idl
->last_monitor_request_seqno
= UINT_MAX
;
168 hmap_init(&idl
->outstanding_txns
);
174 ovsdb_idl_destroy(struct ovsdb_idl
*idl
)
180 ovsdb_idl_clear(idl
);
181 jsonrpc_session_close(idl
->session
);
183 for (i
= 0; i
< idl
->class->n_tables
; i
++) {
184 struct ovsdb_idl_table
*table
= &idl
->tables
[i
];
185 shash_destroy(&table
->columns
);
186 hmap_destroy(&table
->rows
);
188 shash_destroy(&idl
->table_by_name
);
190 json_destroy(idl
->monitor_request_id
);
196 ovsdb_idl_clear(struct ovsdb_idl
*idl
)
198 bool changed
= false;
201 for (i
= 0; i
< idl
->class->n_tables
; i
++) {
202 struct ovsdb_idl_table
*table
= &idl
->tables
[i
];
203 struct ovsdb_idl_row
*row
, *next_row
;
205 if (hmap_is_empty(&table
->rows
)) {
210 HMAP_FOR_EACH_SAFE (row
, next_row
, struct ovsdb_idl_row
, hmap_node
,
212 struct ovsdb_idl_arc
*arc
, *next_arc
;
214 if (!ovsdb_idl_row_is_orphan(row
)) {
215 ovsdb_idl_row_unparse(row
);
217 LIST_FOR_EACH_SAFE (arc
, next_arc
, struct ovsdb_idl_arc
, src_node
,
221 /* No need to do anything with dst_arcs: some node has those arcs
222 * as forward arcs and will destroy them itself. */
224 ovsdb_idl_row_destroy(row
);
234 ovsdb_idl_run(struct ovsdb_idl
*idl
)
239 jsonrpc_session_run(idl
->session
);
240 for (i
= 0; jsonrpc_session_is_connected(idl
->session
) && i
< 50; i
++) {
241 struct jsonrpc_msg
*msg
, *reply
;
244 seqno
= jsonrpc_session_get_seqno(idl
->session
);
245 if (idl
->last_monitor_request_seqno
!= seqno
) {
246 idl
->last_monitor_request_seqno
= seqno
;
247 ovsdb_idl_txn_abort_all(idl
);
248 ovsdb_idl_send_monitor_request(idl
);
252 msg
= jsonrpc_session_recv(idl
->session
);
258 if (msg
->type
== JSONRPC_NOTIFY
259 && !strcmp(msg
->method
, "update")
260 && msg
->params
->type
== JSON_ARRAY
261 && msg
->params
->u
.array
.n
== 2
262 && msg
->params
->u
.array
.elems
[0]->type
== JSON_NULL
) {
263 ovsdb_idl_parse_update(idl
, msg
->params
->u
.array
.elems
[1]);
264 } else if (msg
->type
== JSONRPC_REPLY
265 && idl
->monitor_request_id
266 && json_equal(idl
->monitor_request_id
, msg
->id
)) {
267 json_destroy(idl
->monitor_request_id
);
268 idl
->monitor_request_id
= NULL
;
269 ovsdb_idl_clear(idl
);
270 ovsdb_idl_parse_update(idl
, msg
->result
);
271 } else if (msg
->type
== JSONRPC_REPLY
272 && msg
->id
&& msg
->id
->type
== JSON_STRING
273 && !strcmp(msg
->id
->u
.string
, "echo")) {
274 /* It's a reply to our echo request. Ignore it. */
275 } else if ((msg
->type
== JSONRPC_ERROR
276 || msg
->type
== JSONRPC_REPLY
)
277 && ovsdb_idl_txn_process_reply(idl
, msg
)) {
278 /* ovsdb_idl_txn_process_reply() did everything needful. */
280 /* This can happen if ovsdb_idl_txn_destroy() is called to destroy
281 * a transaction before we receive the reply, so keep the log level
283 VLOG_DBG("%s: received unexpected %s message",
284 jsonrpc_session_get_name(idl
->session
),
285 jsonrpc_msg_type_to_string(msg
->type
));
288 jsonrpc_session_send(idl
->session
, reply
);
290 jsonrpc_msg_destroy(msg
);
295 ovsdb_idl_wait(struct ovsdb_idl
*idl
)
297 jsonrpc_session_wait(idl
->session
);
298 jsonrpc_session_recv_wait(idl
->session
);
302 ovsdb_idl_get_seqno(const struct ovsdb_idl
*idl
)
304 return idl
->change_seqno
;
308 ovsdb_idl_has_ever_connected(const struct ovsdb_idl
*idl
)
310 return ovsdb_idl_get_seqno(idl
) != 0;
314 ovsdb_idl_force_reconnect(struct ovsdb_idl
*idl
)
316 jsonrpc_session_force_reconnect(idl
->session
);
320 ovsdb_idl_send_monitor_request(struct ovsdb_idl
*idl
)
322 struct json
*monitor_requests
;
323 struct jsonrpc_msg
*msg
;
326 monitor_requests
= json_object_create();
327 for (i
= 0; i
< idl
->class->n_tables
; i
++) {
328 const struct ovsdb_idl_table
*table
= &idl
->tables
[i
];
329 const struct ovsdb_idl_table_class
*tc
= table
->class;
330 struct json
*monitor_request
, *columns
;
333 monitor_request
= json_object_create();
334 columns
= json_array_create_empty();
335 for (i
= 0; i
< tc
->n_columns
; i
++) {
336 const struct ovsdb_idl_column
*column
= &tc
->columns
[i
];
337 json_array_add(columns
, json_string_create(column
->name
));
339 json_object_put(monitor_request
, "columns", columns
);
340 json_object_put(monitor_requests
, tc
->name
, monitor_request
);
343 json_destroy(idl
->monitor_request_id
);
344 msg
= jsonrpc_create_request(
346 json_array_create_3(json_string_create(idl
->class->database
),
347 json_null_create(), monitor_requests
),
348 &idl
->monitor_request_id
);
349 jsonrpc_session_send(idl
->session
, msg
);
353 ovsdb_idl_parse_update(struct ovsdb_idl
*idl
, const struct json
*table_updates
)
355 struct ovsdb_error
*error
;
359 error
= ovsdb_idl_parse_update__(idl
, table_updates
);
361 if (!VLOG_DROP_WARN(&syntax_rl
)) {
362 char *s
= ovsdb_error_to_string(error
);
363 VLOG_WARN_RL(&syntax_rl
, "%s", s
);
366 ovsdb_error_destroy(error
);
370 static struct ovsdb_error
*
371 ovsdb_idl_parse_update__(struct ovsdb_idl
*idl
,
372 const struct json
*table_updates
)
374 const struct shash_node
*tables_node
;
376 if (table_updates
->type
!= JSON_OBJECT
) {
377 return ovsdb_syntax_error(table_updates
, NULL
,
378 "<table-updates> is not an object");
380 SHASH_FOR_EACH (tables_node
, json_object(table_updates
)) {
381 const struct json
*table_update
= tables_node
->data
;
382 const struct shash_node
*table_node
;
383 struct ovsdb_idl_table
*table
;
385 table
= shash_find_data(&idl
->table_by_name
, tables_node
->name
);
387 return ovsdb_syntax_error(
389 "<table-updates> includes unknown table \"%s\"",
393 if (table_update
->type
!= JSON_OBJECT
) {
394 return ovsdb_syntax_error(table_update
, NULL
,
395 "<table-update> for table \"%s\" is "
396 "not an object", table
->class->name
);
398 SHASH_FOR_EACH (table_node
, json_object(table_update
)) {
399 const struct json
*row_update
= table_node
->data
;
400 const struct json
*old_json
, *new_json
;
403 if (!uuid_from_string(&uuid
, table_node
->name
)) {
404 return ovsdb_syntax_error(table_update
, NULL
,
405 "<table-update> for table \"%s\" "
407 "\"%s\" as member name",
411 if (row_update
->type
!= JSON_OBJECT
) {
412 return ovsdb_syntax_error(row_update
, NULL
,
413 "<table-update> for table \"%s\" "
414 "contains <row-update> for %s that "
420 old_json
= shash_find_data(json_object(row_update
), "old");
421 new_json
= shash_find_data(json_object(row_update
), "new");
422 if (old_json
&& old_json
->type
!= JSON_OBJECT
) {
423 return ovsdb_syntax_error(old_json
, NULL
,
424 "\"old\" <row> is not object");
425 } else if (new_json
&& new_json
->type
!= JSON_OBJECT
) {
426 return ovsdb_syntax_error(new_json
, NULL
,
427 "\"new\" <row> is not object");
428 } else if ((old_json
!= NULL
) + (new_json
!= NULL
)
429 != shash_count(json_object(row_update
))) {
430 return ovsdb_syntax_error(row_update
, NULL
,
431 "<row-update> contains unexpected "
433 } else if (!old_json
&& !new_json
) {
434 return ovsdb_syntax_error(row_update
, NULL
,
435 "<row-update> missing \"old\" "
436 "and \"new\" members");
439 ovsdb_idl_process_update(table
, &uuid
, old_json
, new_json
);
446 static struct ovsdb_idl_row
*
447 ovsdb_idl_get_row(struct ovsdb_idl_table
*table
, const struct uuid
*uuid
)
449 struct ovsdb_idl_row
*row
;
451 HMAP_FOR_EACH_WITH_HASH (row
, struct ovsdb_idl_row
, hmap_node
,
452 uuid_hash(uuid
), &table
->rows
) {
453 if (uuid_equals(&row
->uuid
, uuid
)) {
461 ovsdb_idl_process_update(struct ovsdb_idl_table
*table
,
462 const struct uuid
*uuid
, const struct json
*old
,
463 const struct json
*new)
465 struct ovsdb_idl_row
*row
;
467 row
= ovsdb_idl_get_row(table
, uuid
);
470 if (row
&& !ovsdb_idl_row_is_orphan(row
)) {
471 /* XXX perhaps we should check the 'old' values? */
472 ovsdb_idl_delete_row(row
);
474 VLOG_WARN_RL(&semantic_rl
, "cannot delete missing row "UUID_FMT
" "
476 UUID_ARGS(uuid
), table
->class->name
);
481 ovsdb_idl_insert_row(ovsdb_idl_row_create(table
, uuid
), new);
482 } else if (ovsdb_idl_row_is_orphan(row
)) {
483 ovsdb_idl_insert_row(row
, new);
485 VLOG_WARN_RL(&semantic_rl
, "cannot add existing row "UUID_FMT
" to "
486 "table %s", UUID_ARGS(uuid
), table
->class->name
);
487 ovsdb_idl_modify_row(row
, new);
492 /* XXX perhaps we should check the 'old' values? */
493 if (!ovsdb_idl_row_is_orphan(row
)) {
494 ovsdb_idl_modify_row(row
, new);
496 VLOG_WARN_RL(&semantic_rl
, "cannot modify missing but "
497 "referenced row "UUID_FMT
" in table %s",
498 UUID_ARGS(uuid
), table
->class->name
);
499 ovsdb_idl_insert_row(row
, new);
502 VLOG_WARN_RL(&semantic_rl
, "cannot modify missing row "UUID_FMT
" "
503 "in table %s", UUID_ARGS(uuid
), table
->class->name
);
504 ovsdb_idl_insert_row(ovsdb_idl_row_create(table
, uuid
), new);
510 ovsdb_idl_row_update(struct ovsdb_idl_row
*row
, const struct json
*row_json
)
512 struct ovsdb_idl_table
*table
= row
->table
;
513 struct shash_node
*node
;
515 SHASH_FOR_EACH (node
, json_object(row_json
)) {
516 const char *column_name
= node
->name
;
517 const struct ovsdb_idl_column
*column
;
518 struct ovsdb_datum datum
;
519 struct ovsdb_error
*error
;
521 column
= shash_find_data(&table
->columns
, column_name
);
523 VLOG_WARN_RL(&syntax_rl
, "unknown column %s updating row "UUID_FMT
,
524 column_name
, UUID_ARGS(&row
->uuid
));
528 error
= ovsdb_datum_from_json(&datum
, &column
->type
, node
->data
, NULL
);
530 ovsdb_datum_swap(&row
->old
[column
- table
->class->columns
],
532 ovsdb_datum_destroy(&datum
, &column
->type
);
534 char *s
= ovsdb_error_to_string(error
);
535 VLOG_WARN_RL(&syntax_rl
, "error parsing column %s in row "UUID_FMT
536 " in table %s: %s", column_name
,
537 UUID_ARGS(&row
->uuid
), table
->class->name
, s
);
539 ovsdb_error_destroy(error
);
544 /* When a row A refers to row B through a column with a "refTable" constraint,
545 * but row B does not exist, row B is called an "orphan row". Orphan rows
546 * should not persist, because the database enforces referential integrity, but
547 * they can appear transiently as changes from the database are received (the
548 * database doesn't try to topologically sort them and circular references mean
549 * it isn't always possible anyhow).
551 * This function returns true if 'row' is an orphan row, otherwise false.
554 ovsdb_idl_row_is_orphan(const struct ovsdb_idl_row
*row
)
556 return !row
->old
&& !row
->new;
559 /* Returns true if 'row' is conceptually part of the database as modified by
560 * the current transaction (if any), false otherwise.
562 * This function will return true if 'row' is not an orphan (see the comment on
563 * ovsdb_idl_row_is_orphan()) and:
565 * - 'row' exists in the database and has not been deleted within the
566 * current transaction (if any).
568 * - 'row' was inserted within the current transaction and has not been
569 * deleted. (In the latter case you should not have passed 'row' in at
570 * all, because ovsdb_idl_txn_delete() freed it.)
572 * This function will return false if 'row' is an orphan or if 'row' was
573 * deleted within the current transaction.
576 ovsdb_idl_row_exists(const struct ovsdb_idl_row
*row
)
578 return row
->new != NULL
;
582 ovsdb_idl_row_parse(struct ovsdb_idl_row
*row
)
584 const struct ovsdb_idl_table_class
*class = row
->table
->class;
587 for (i
= 0; i
< class->n_columns
; i
++) {
588 const struct ovsdb_idl_column
*c
= &class->columns
[i
];
589 (c
->parse
)(row
, &row
->old
[i
]);
594 ovsdb_idl_row_unparse(struct ovsdb_idl_row
*row
)
596 const struct ovsdb_idl_table_class
*class = row
->table
->class;
599 for (i
= 0; i
< class->n_columns
; i
++) {
600 const struct ovsdb_idl_column
*c
= &class->columns
[i
];
606 ovsdb_idl_row_clear_old(struct ovsdb_idl_row
*row
)
608 assert(row
->old
== row
->new);
609 if (!ovsdb_idl_row_is_orphan(row
)) {
610 const struct ovsdb_idl_table_class
*class = row
->table
->class;
613 for (i
= 0; i
< class->n_columns
; i
++) {
614 ovsdb_datum_destroy(&row
->old
[i
], &class->columns
[i
].type
);
617 row
->old
= row
->new = NULL
;
622 ovsdb_idl_row_clear_new(struct ovsdb_idl_row
*row
)
624 if (row
->old
!= row
->new) {
626 const struct ovsdb_idl_table_class
*class = row
->table
->class;
629 BITMAP_FOR_EACH_1 (i
, class->n_columns
, row
->written
) {
630 ovsdb_datum_destroy(&row
->new[i
], &class->columns
[i
].type
);
641 ovsdb_idl_row_clear_arcs(struct ovsdb_idl_row
*row
, bool destroy_dsts
)
643 struct ovsdb_idl_arc
*arc
, *next
;
645 /* Delete all forward arcs. If 'destroy_dsts', destroy any orphaned rows
646 * that this causes to be unreferenced. */
647 LIST_FOR_EACH_SAFE (arc
, next
, struct ovsdb_idl_arc
, src_node
,
649 list_remove(&arc
->dst_node
);
651 && ovsdb_idl_row_is_orphan(arc
->dst
)
652 && list_is_empty(&arc
->dst
->dst_arcs
)) {
653 ovsdb_idl_row_destroy(arc
->dst
);
657 list_init(&row
->src_arcs
);
660 /* Force nodes that reference 'row' to reparse. */
662 ovsdb_idl_row_reparse_backrefs(struct ovsdb_idl_row
*row
)
664 struct ovsdb_idl_arc
*arc
, *next
;
666 /* This is trickier than it looks. ovsdb_idl_row_clear_arcs() will destroy
667 * 'arc', so we need to use the "safe" variant of list traversal. However,
668 * calling an ovsdb_idl_column's 'parse' function will add an arc
669 * equivalent to 'arc' to row->arcs. That could be a problem for
670 * traversal, but it adds it at the beginning of the list to prevent us
671 * from stumbling upon it again.
673 * (If duplicate arcs were possible then we would need to make sure that
674 * 'next' didn't also point into 'arc''s destination, but we forbid
675 * duplicate arcs.) */
676 LIST_FOR_EACH_SAFE (arc
, next
, struct ovsdb_idl_arc
, dst_node
,
678 struct ovsdb_idl_row
*ref
= arc
->src
;
680 ovsdb_idl_row_unparse(ref
);
681 ovsdb_idl_row_clear_arcs(ref
, false);
682 ovsdb_idl_row_parse(ref
);
686 static struct ovsdb_idl_row
*
687 ovsdb_idl_row_create__(const struct ovsdb_idl_table_class
*class)
689 struct ovsdb_idl_row
*row
= xzalloc(class->allocation_size
);
690 list_init(&row
->src_arcs
);
691 list_init(&row
->dst_arcs
);
692 hmap_node_nullify(&row
->txn_node
);
696 static struct ovsdb_idl_row
*
697 ovsdb_idl_row_create(struct ovsdb_idl_table
*table
, const struct uuid
*uuid
)
699 struct ovsdb_idl_row
*row
= ovsdb_idl_row_create__(table
->class);
700 hmap_insert(&table
->rows
, &row
->hmap_node
, uuid_hash(uuid
));
707 ovsdb_idl_row_destroy(struct ovsdb_idl_row
*row
)
710 ovsdb_idl_row_clear_old(row
);
711 hmap_remove(&row
->table
->rows
, &row
->hmap_node
);
717 ovsdb_idl_insert_row(struct ovsdb_idl_row
*row
, const struct json
*row_json
)
719 const struct ovsdb_idl_table_class
*class = row
->table
->class;
722 assert(!row
->old
&& !row
->new);
723 row
->old
= row
->new = xmalloc(class->n_columns
* sizeof *row
->old
);
724 for (i
= 0; i
< class->n_columns
; i
++) {
725 ovsdb_datum_init_default(&row
->old
[i
], &class->columns
[i
].type
);
727 ovsdb_idl_row_update(row
, row_json
);
728 ovsdb_idl_row_parse(row
);
730 ovsdb_idl_row_reparse_backrefs(row
);
734 ovsdb_idl_delete_row(struct ovsdb_idl_row
*row
)
736 ovsdb_idl_row_unparse(row
);
737 ovsdb_idl_row_clear_arcs(row
, true);
738 ovsdb_idl_row_clear_old(row
);
739 if (list_is_empty(&row
->dst_arcs
)) {
740 ovsdb_idl_row_destroy(row
);
742 ovsdb_idl_row_reparse_backrefs(row
);
747 ovsdb_idl_modify_row(struct ovsdb_idl_row
*row
, const struct json
*row_json
)
749 ovsdb_idl_row_unparse(row
);
750 ovsdb_idl_row_clear_arcs(row
, true);
751 ovsdb_idl_row_update(row
, row_json
);
752 ovsdb_idl_row_parse(row
);
756 may_add_arc(const struct ovsdb_idl_row
*src
, const struct ovsdb_idl_row
*dst
)
758 const struct ovsdb_idl_arc
*arc
;
765 /* No duplicate arcs.
767 * We only need to test whether the first arc in dst->dst_arcs originates
768 * at 'src', since we add all of the arcs from a given source in a clump
769 * (in a single call to ovsdb_idl_row_parse()) and new arcs are always
770 * added at the front of the dst_arcs list. */
771 if (list_is_empty(&dst
->dst_arcs
)) {
774 arc
= CONTAINER_OF(dst
->dst_arcs
.next
, struct ovsdb_idl_arc
, dst_node
);
775 return arc
->src
!= src
;
778 static struct ovsdb_idl_table
*
779 ovsdb_idl_table_from_class(const struct ovsdb_idl
*idl
,
780 const struct ovsdb_idl_table_class
*table_class
)
782 return &idl
->tables
[table_class
- idl
->class->tables
];
785 struct ovsdb_idl_row
*
786 ovsdb_idl_get_row_arc(struct ovsdb_idl_row
*src
,
787 struct ovsdb_idl_table_class
*dst_table_class
,
788 const struct uuid
*dst_uuid
)
790 struct ovsdb_idl
*idl
= src
->table
->idl
;
791 struct ovsdb_idl_table
*dst_table
;
792 struct ovsdb_idl_arc
*arc
;
793 struct ovsdb_idl_row
*dst
;
795 dst_table
= ovsdb_idl_table_from_class(idl
, dst_table_class
);
796 dst
= ovsdb_idl_get_row(dst_table
, dst_uuid
);
798 /* We're being called from ovsdb_idl_txn_write(). We must not update
799 * any arcs, because the transaction will be backed out at commit or
800 * abort time and we don't want our graph screwed up.
802 * Just return the destination row, if there is one and it has not been
804 if (dst
&& (hmap_node_is_null(&dst
->txn_node
) || dst
->new)) {
809 /* We're being called from some other context. Update the graph. */
811 dst
= ovsdb_idl_row_create(dst_table
, dst_uuid
);
814 /* Add a new arc, if it wouldn't be a self-arc or a duplicate arc. */
815 if (may_add_arc(src
, dst
)) {
816 /* The arc *must* be added at the front of the dst_arcs list. See
817 * ovsdb_idl_row_reparse_backrefs() for details. */
818 arc
= xmalloc(sizeof *arc
);
819 list_push_front(&src
->src_arcs
, &arc
->src_node
);
820 list_push_front(&dst
->dst_arcs
, &arc
->dst_node
);
825 return !ovsdb_idl_row_is_orphan(dst
) ? dst
: NULL
;
829 const struct ovsdb_idl_row
*
830 ovsdb_idl_get_row_for_uuid(const struct ovsdb_idl
*idl
,
831 const struct ovsdb_idl_table_class
*tc
,
832 const struct uuid
*uuid
)
834 return ovsdb_idl_get_row(ovsdb_idl_table_from_class(idl
, tc
), uuid
);
837 static struct ovsdb_idl_row
*
838 next_real_row(struct ovsdb_idl_table
*table
, struct hmap_node
*node
)
840 for (; node
; node
= hmap_next(&table
->rows
, node
)) {
841 struct ovsdb_idl_row
*row
;
843 row
= CONTAINER_OF(node
, struct ovsdb_idl_row
, hmap_node
);
844 if (ovsdb_idl_row_exists(row
)) {
851 const struct ovsdb_idl_row
*
852 ovsdb_idl_first_row(const struct ovsdb_idl
*idl
,
853 const struct ovsdb_idl_table_class
*table_class
)
855 struct ovsdb_idl_table
*table
856 = ovsdb_idl_table_from_class(idl
, table_class
);
857 return next_real_row(table
, hmap_first(&table
->rows
));
860 const struct ovsdb_idl_row
*
861 ovsdb_idl_next_row(const struct ovsdb_idl_row
*row
)
863 struct ovsdb_idl_table
*table
= row
->table
;
865 return next_real_row(table
, hmap_next(&table
->rows
, &row
->hmap_node
));
870 static void ovsdb_idl_txn_complete(struct ovsdb_idl_txn
*txn
,
871 enum ovsdb_idl_txn_status
);
874 ovsdb_idl_txn_status_to_string(enum ovsdb_idl_txn_status status
)
893 struct ovsdb_idl_txn
*
894 ovsdb_idl_txn_create(struct ovsdb_idl
*idl
)
896 struct ovsdb_idl_txn
*txn
;
899 idl
->txn
= txn
= xmalloc(sizeof *txn
);
900 txn
->request_id
= NULL
;
902 hmap_init(&txn
->txn_rows
);
903 txn
->status
= TXN_INCOMPLETE
;
905 txn
->dry_run
= false;
906 ds_init(&txn
->comment
);
908 txn
->inc_table
= NULL
;
909 txn
->inc_column
= NULL
;
910 txn
->inc_where
= NULL
;
912 hmap_init(&txn
->inserted_rows
);
918 ovsdb_idl_txn_add_comment(struct ovsdb_idl_txn
*txn
, const char *s
)
920 if (txn
->comment
.length
) {
921 ds_put_char(&txn
->comment
, '\n');
923 ds_put_cstr(&txn
->comment
, s
);
927 ovsdb_idl_txn_set_dry_run(struct ovsdb_idl_txn
*txn
)
933 ovsdb_idl_txn_increment(struct ovsdb_idl_txn
*txn
, const char *table
,
934 const char *column
, const struct json
*where
)
936 assert(!txn
->inc_table
);
937 txn
->inc_table
= xstrdup(table
);
938 txn
->inc_column
= xstrdup(column
);
939 txn
->inc_where
= where
? json_clone(where
) : json_array_create_empty();
943 ovsdb_idl_txn_destroy(struct ovsdb_idl_txn
*txn
)
945 struct ovsdb_idl_txn_insert
*insert
, *next
;
947 json_destroy(txn
->request_id
);
948 if (txn
->status
== TXN_INCOMPLETE
) {
949 hmap_remove(&txn
->idl
->outstanding_txns
, &txn
->hmap_node
);
951 ovsdb_idl_txn_abort(txn
);
952 ds_destroy(&txn
->comment
);
954 free(txn
->inc_table
);
955 free(txn
->inc_column
);
956 json_destroy(txn
->inc_where
);
957 HMAP_FOR_EACH_SAFE (insert
, next
, struct ovsdb_idl_txn_insert
, hmap_node
,
958 &txn
->inserted_rows
) {
961 hmap_destroy(&txn
->inserted_rows
);
966 ovsdb_idl_txn_wait(const struct ovsdb_idl_txn
*txn
)
968 if (txn
->status
!= TXN_INCOMPLETE
) {
969 poll_immediate_wake();
974 where_uuid_equals(const struct uuid
*uuid
)
979 json_string_create("_uuid"),
980 json_string_create("=="),
982 json_string_create("uuid"),
983 json_string_create_nocopy(
984 xasprintf(UUID_FMT
, UUID_ARGS(uuid
))))));
988 uuid_name_from_uuid(const struct uuid
*uuid
)
993 name
= xasprintf("row"UUID_FMT
, UUID_ARGS(uuid
));
994 for (p
= name
; *p
!= '\0'; p
++) {
1003 static const struct ovsdb_idl_row
*
1004 ovsdb_idl_txn_get_row(const struct ovsdb_idl_txn
*txn
, const struct uuid
*uuid
)
1006 const struct ovsdb_idl_row
*row
;
1008 HMAP_FOR_EACH_WITH_HASH (row
, struct ovsdb_idl_row
, txn_node
,
1009 uuid_hash(uuid
), &txn
->txn_rows
) {
1010 if (uuid_equals(&row
->uuid
, uuid
)) {
1017 /* XXX there must be a cleaner way to do this */
1018 static struct json
*
1019 substitute_uuids(struct json
*json
, const struct ovsdb_idl_txn
*txn
)
1021 if (json
->type
== JSON_ARRAY
) {
1025 if (json
->u
.array
.n
== 2
1026 && json
->u
.array
.elems
[0]->type
== JSON_STRING
1027 && json
->u
.array
.elems
[1]->type
== JSON_STRING
1028 && !strcmp(json
->u
.array
.elems
[0]->u
.string
, "uuid")
1029 && uuid_from_string(&uuid
, json
->u
.array
.elems
[1]->u
.string
)) {
1030 const struct ovsdb_idl_row
*row
;
1032 row
= ovsdb_idl_txn_get_row(txn
, &uuid
);
1033 if (row
&& !row
->old
&& row
->new) {
1036 return json_array_create_2(
1037 json_string_create("named-uuid"),
1038 json_string_create_nocopy(uuid_name_from_uuid(&uuid
)));
1042 for (i
= 0; i
< json
->u
.array
.n
; i
++) {
1043 json
->u
.array
.elems
[i
] = substitute_uuids(json
->u
.array
.elems
[i
],
1046 } else if (json
->type
== JSON_OBJECT
) {
1047 struct shash_node
*node
;
1049 SHASH_FOR_EACH (node
, json_object(json
)) {
1050 node
->data
= substitute_uuids(node
->data
, txn
);
1057 ovsdb_idl_txn_disassemble(struct ovsdb_idl_txn
*txn
)
1059 struct ovsdb_idl_row
*row
, *next
;
1061 /* This must happen early. Otherwise, ovsdb_idl_row_parse() will call an
1062 * ovsdb_idl_column's 'parse' function, which will call
1063 * ovsdb_idl_get_row_arc(), which will seen that the IDL is in a
1064 * transaction and fail to update the graph. */
1065 txn
->idl
->txn
= NULL
;
1067 HMAP_FOR_EACH_SAFE (row
, next
, struct ovsdb_idl_row
, txn_node
,
1071 ovsdb_idl_row_unparse(row
);
1072 ovsdb_idl_row_clear_arcs(row
, false);
1073 ovsdb_idl_row_parse(row
);
1076 ovsdb_idl_row_unparse(row
);
1078 ovsdb_idl_row_clear_new(row
);
1081 row
->prereqs
= NULL
;
1084 row
->written
= NULL
;
1086 hmap_remove(&txn
->txn_rows
, &row
->txn_node
);
1087 hmap_node_nullify(&row
->txn_node
);
1089 hmap_remove(&row
->table
->rows
, &row
->hmap_node
);
1093 hmap_destroy(&txn
->txn_rows
);
1094 hmap_init(&txn
->txn_rows
);
1097 enum ovsdb_idl_txn_status
1098 ovsdb_idl_txn_commit(struct ovsdb_idl_txn
*txn
)
1100 struct ovsdb_idl_row
*row
;
1101 struct json
*operations
;
1104 if (txn
!= txn
->idl
->txn
) {
1108 operations
= json_array_create_1(
1109 json_string_create(txn
->idl
->class->database
));
1111 /* Add prerequisites and declarations of new rows. */
1112 HMAP_FOR_EACH (row
, struct ovsdb_idl_row
, txn_node
, &txn
->txn_rows
) {
1113 /* XXX check that deleted rows exist even if no prereqs? */
1115 const struct ovsdb_idl_table_class
*class = row
->table
->class;
1116 size_t n_columns
= class->n_columns
;
1117 struct json
*op
, *columns
, *row_json
;
1120 op
= json_object_create();
1121 json_array_add(operations
, op
);
1122 json_object_put_string(op
, "op", "wait");
1123 json_object_put_string(op
, "table", class->name
);
1124 json_object_put(op
, "timeout", json_integer_create(0));
1125 json_object_put(op
, "where", where_uuid_equals(&row
->uuid
));
1126 json_object_put_string(op
, "until", "==");
1127 columns
= json_array_create_empty();
1128 json_object_put(op
, "columns", columns
);
1129 row_json
= json_object_create();
1130 json_object_put(op
, "rows", json_array_create_1(row_json
));
1132 BITMAP_FOR_EACH_1 (idx
, n_columns
, row
->prereqs
) {
1133 const struct ovsdb_idl_column
*column
= &class->columns
[idx
];
1134 json_array_add(columns
, json_string_create(column
->name
));
1135 json_object_put(row_json
, column
->name
,
1136 ovsdb_datum_to_json(&row
->old
[idx
],
1143 any_updates
= false;
1144 HMAP_FOR_EACH (row
, struct ovsdb_idl_row
, txn_node
, &txn
->txn_rows
) {
1145 const struct ovsdb_idl_table_class
*class = row
->table
->class;
1147 if (row
->old
== row
->new) {
1149 } else if (!row
->new) {
1150 struct json
*op
= json_object_create();
1151 json_object_put_string(op
, "op", "delete");
1152 json_object_put_string(op
, "table", class->name
);
1153 json_object_put(op
, "where", where_uuid_equals(&row
->uuid
));
1154 json_array_add(operations
, op
);
1157 struct json
*row_json
;
1161 op
= json_object_create();
1162 json_object_put_string(op
, "op", row
->old
? "update" : "insert");
1163 json_object_put_string(op
, "table", class->name
);
1165 json_object_put(op
, "where", where_uuid_equals(&row
->uuid
));
1167 struct ovsdb_idl_txn_insert
*insert
;
1169 json_object_put(op
, "uuid-name",
1170 json_string_create_nocopy(
1171 uuid_name_from_uuid(&row
->uuid
)));
1173 insert
= xmalloc(sizeof *insert
);
1174 insert
->dummy
= row
->uuid
;
1175 insert
->op_index
= operations
->u
.array
.n
- 1;
1176 uuid_zero(&insert
->real
);
1177 hmap_insert(&txn
->inserted_rows
, &insert
->hmap_node
,
1178 uuid_hash(&insert
->dummy
));
1180 row_json
= json_object_create();
1181 json_object_put(op
, "row", row_json
);
1183 BITMAP_FOR_EACH_1 (idx
, class->n_columns
, row
->written
) {
1184 const struct ovsdb_idl_column
*column
= &class->columns
[idx
];
1187 ? !ovsdb_datum_equals(&row
->old
[idx
], &row
->new[idx
],
1189 : !ovsdb_datum_is_default(&row
->new[idx
], &column
->type
)) {
1190 json_object_put(row_json
, column
->name
,
1192 ovsdb_datum_to_json(&row
->new[idx
],
1198 if (!row
->old
|| !shash_is_empty(json_object(row_json
))) {
1199 json_array_add(operations
, op
);
1207 /* Add increment. */
1208 if (txn
->inc_table
&& any_updates
) {
1211 txn
->inc_index
= operations
->u
.array
.n
- 1;
1213 op
= json_object_create();
1214 json_object_put_string(op
, "op", "mutate");
1215 json_object_put_string(op
, "table", txn
->inc_table
);
1216 json_object_put(op
, "where",
1217 substitute_uuids(json_clone(txn
->inc_where
), txn
));
1218 json_object_put(op
, "mutations",
1219 json_array_create_1(
1220 json_array_create_3(
1221 json_string_create(txn
->inc_column
),
1222 json_string_create("+="),
1223 json_integer_create(1))));
1224 json_array_add(operations
, op
);
1226 op
= json_object_create();
1227 json_object_put_string(op
, "op", "select");
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
, "columns",
1232 json_array_create_1(json_string_create(
1234 json_array_add(operations
, op
);
1237 if (txn
->comment
.length
) {
1238 struct json
*op
= json_object_create();
1239 json_object_put_string(op
, "op", "comment");
1240 json_object_put_string(op
, "comment", ds_cstr(&txn
->comment
));
1241 json_array_add(operations
, op
);
1245 struct json
*op
= json_object_create();
1246 json_object_put_string(op
, "op", "abort");
1247 json_array_add(operations
, op
);
1251 txn
->status
= TXN_UNCHANGED
;
1252 json_destroy(operations
);
1253 } else if (!jsonrpc_session_send(
1255 jsonrpc_create_request(
1256 "transact", operations
, &txn
->request_id
))) {
1257 hmap_insert(&txn
->idl
->outstanding_txns
, &txn
->hmap_node
,
1258 json_hash(txn
->request_id
, 0));
1260 txn
->status
= TXN_TRY_AGAIN
;
1263 ovsdb_idl_txn_disassemble(txn
);
1267 /* Attempts to commit 'txn', blocking until the commit either succeeds or
1268 * fails. Returns the final commit status, which may be any TXN_* value other
1269 * than TXN_INCOMPLETE. */
1270 enum ovsdb_idl_txn_status
1271 ovsdb_idl_txn_commit_block(struct ovsdb_idl_txn
*txn
)
1273 enum ovsdb_idl_txn_status status
;
1275 while ((status
= ovsdb_idl_txn_commit(txn
)) == TXN_INCOMPLETE
) {
1276 ovsdb_idl_run(txn
->idl
);
1277 ovsdb_idl_wait(txn
->idl
);
1278 ovsdb_idl_txn_wait(txn
);
1285 ovsdb_idl_txn_get_increment_new_value(const struct ovsdb_idl_txn
*txn
)
1287 assert(txn
->status
== TXN_SUCCESS
);
1288 return txn
->inc_new_value
;
1292 ovsdb_idl_txn_abort(struct ovsdb_idl_txn
*txn
)
1294 ovsdb_idl_txn_disassemble(txn
);
1295 if (txn
->status
== TXN_INCOMPLETE
) {
1296 txn
->status
= TXN_ABORTED
;
1301 ovsdb_idl_txn_get_error(const struct ovsdb_idl_txn
*txn
)
1303 if (txn
->status
!= TXN_ERROR
) {
1304 return ovsdb_idl_txn_status_to_string(txn
->status
);
1305 } else if (txn
->error
) {
1308 return "no error details available";
1313 ovsdb_idl_txn_set_error_json(struct ovsdb_idl_txn
*txn
,
1314 const struct json
*json
)
1316 if (txn
->error
== NULL
) {
1317 txn
->error
= json_to_string(json
, JSSF_SORT
);
1321 /* For transaction 'txn' that completed successfully, finds and returns the
1322 * permanent UUID that the database assigned to a newly inserted row, given the
1323 * 'uuid' that ovsdb_idl_txn_insert() assigned locally to that row.
1325 * Returns NULL if 'uuid' is not a UUID assigned by ovsdb_idl_txn_insert() or
1326 * if it was assigned by that function and then deleted by
1327 * ovsdb_idl_txn_delete() within the same transaction. (Rows that are inserted
1328 * and then deleted within a single transaction are never sent to the database
1329 * server, so it never assigns them a permanent UUID.) */
1331 ovsdb_idl_txn_get_insert_uuid(const struct ovsdb_idl_txn
*txn
,
1332 const struct uuid
*uuid
)
1334 const struct ovsdb_idl_txn_insert
*insert
;
1336 assert(txn
->status
== TXN_SUCCESS
|| txn
->status
== TXN_UNCHANGED
);
1337 HMAP_FOR_EACH_IN_BUCKET (insert
, struct ovsdb_idl_txn_insert
, hmap_node
,
1338 uuid_hash(uuid
), &txn
->inserted_rows
) {
1339 if (uuid_equals(uuid
, &insert
->dummy
)) {
1340 return &insert
->real
;
1347 ovsdb_idl_txn_complete(struct ovsdb_idl_txn
*txn
,
1348 enum ovsdb_idl_txn_status status
)
1350 txn
->status
= status
;
1351 hmap_remove(&txn
->idl
->outstanding_txns
, &txn
->hmap_node
);
1355 ovsdb_idl_txn_read(const struct ovsdb_idl_row
*row
,
1356 const struct ovsdb_idl_column
*column
,
1357 struct ovsdb_datum
*datum
)
1359 const struct ovsdb_idl_table_class
*class = row
->table
->class;
1360 size_t column_idx
= column
- class->columns
;
1362 assert(row
->new != NULL
);
1363 if (row
->written
&& bitmap_is_set(row
->written
, column_idx
)) {
1364 ovsdb_datum_clone(datum
, &row
->new[column_idx
], &column
->type
);
1365 } else if (row
->old
) {
1366 ovsdb_datum_clone(datum
, &row
->old
[column_idx
], &column
->type
);
1368 ovsdb_datum_init_default(datum
, &column
->type
);
1373 ovsdb_idl_txn_write(const struct ovsdb_idl_row
*row_
,
1374 const struct ovsdb_idl_column
*column
,
1375 struct ovsdb_datum
*datum
)
1377 struct ovsdb_idl_row
*row
= (struct ovsdb_idl_row
*) row_
;
1378 const struct ovsdb_idl_table_class
*class = row
->table
->class;
1379 size_t column_idx
= column
- class->columns
;
1381 assert(row
->new != NULL
);
1382 assert(column_idx
< class->n_columns
);
1383 if (hmap_node_is_null(&row
->txn_node
)) {
1384 hmap_insert(&row
->table
->idl
->txn
->txn_rows
, &row
->txn_node
,
1385 uuid_hash(&row
->uuid
));
1387 if (row
->old
== row
->new) {
1388 row
->new = xmalloc(class->n_columns
* sizeof *row
->new);
1390 if (!row
->written
) {
1391 row
->written
= bitmap_allocate(class->n_columns
);
1393 if (bitmap_is_set(row
->written
, column_idx
)) {
1394 ovsdb_datum_destroy(&row
->new[column_idx
], &column
->type
);
1396 bitmap_set1(row
->written
, column_idx
);
1398 row
->new[column_idx
] = *datum
;
1399 (column
->unparse
)(row
);
1400 (column
->parse
)(row
, &row
->new[column_idx
]);
1404 ovsdb_idl_txn_verify(const struct ovsdb_idl_row
*row_
,
1405 const struct ovsdb_idl_column
*column
)
1407 struct ovsdb_idl_row
*row
= (struct ovsdb_idl_row
*) row_
;
1408 const struct ovsdb_idl_table_class
*class = row
->table
->class;
1409 size_t column_idx
= column
- class->columns
;
1411 assert(row
->new != NULL
);
1413 || (row
->written
&& bitmap_is_set(row
->written
, column_idx
))) {
1417 if (hmap_node_is_null(&row
->txn_node
)) {
1418 hmap_insert(&row
->table
->idl
->txn
->txn_rows
, &row
->txn_node
,
1419 uuid_hash(&row
->uuid
));
1421 if (!row
->prereqs
) {
1422 row
->prereqs
= bitmap_allocate(class->n_columns
);
1424 bitmap_set1(row
->prereqs
, column_idx
);
1428 ovsdb_idl_txn_delete(const struct ovsdb_idl_row
*row_
)
1430 struct ovsdb_idl_row
*row
= (struct ovsdb_idl_row
*) row_
;
1432 assert(row
->new != NULL
);
1434 ovsdb_idl_row_unparse(row
);
1435 ovsdb_idl_row_clear_new(row
);
1436 assert(!row
->prereqs
);
1437 hmap_remove(&row
->table
->rows
, &row
->hmap_node
);
1438 hmap_remove(&row
->table
->idl
->txn
->txn_rows
, &row
->txn_node
);
1442 if (hmap_node_is_null(&row
->txn_node
)) {
1443 hmap_insert(&row
->table
->idl
->txn
->txn_rows
, &row
->txn_node
,
1444 uuid_hash(&row
->uuid
));
1446 ovsdb_idl_row_clear_new(row
);
1450 const struct ovsdb_idl_row
*
1451 ovsdb_idl_txn_insert(struct ovsdb_idl_txn
*txn
,
1452 const struct ovsdb_idl_table_class
*class)
1454 struct ovsdb_idl_row
*row
= ovsdb_idl_row_create__(class);
1455 uuid_generate(&row
->uuid
);
1456 row
->table
= ovsdb_idl_table_from_class(txn
->idl
, class);
1457 row
->new = xmalloc(class->n_columns
* sizeof *row
->new);
1458 row
->written
= bitmap_allocate(class->n_columns
);
1459 hmap_insert(&row
->table
->rows
, &row
->hmap_node
, uuid_hash(&row
->uuid
));
1460 hmap_insert(&txn
->txn_rows
, &row
->txn_node
, uuid_hash(&row
->uuid
));
1465 ovsdb_idl_txn_abort_all(struct ovsdb_idl
*idl
)
1467 struct ovsdb_idl_txn
*txn
;
1469 HMAP_FOR_EACH (txn
, struct ovsdb_idl_txn
, hmap_node
,
1470 &idl
->outstanding_txns
) {
1471 ovsdb_idl_txn_complete(txn
, TXN_TRY_AGAIN
);
1475 static struct ovsdb_idl_txn
*
1476 ovsdb_idl_txn_find(struct ovsdb_idl
*idl
, const struct json
*id
)
1478 struct ovsdb_idl_txn
*txn
;
1480 HMAP_FOR_EACH_WITH_HASH (txn
, struct ovsdb_idl_txn
, hmap_node
,
1481 json_hash(id
, 0), &idl
->outstanding_txns
) {
1482 if (json_equal(id
, txn
->request_id
)) {
1490 check_json_type(const struct json
*json
, enum json_type type
, const char *name
)
1493 VLOG_WARN_RL(&syntax_rl
, "%s is missing", name
);
1495 } else if (json
->type
!= type
) {
1496 VLOG_WARN_RL(&syntax_rl
, "%s is %s instead of %s",
1497 name
, json_type_to_string(json
->type
),
1498 json_type_to_string(type
));
1506 ovsdb_idl_txn_process_inc_reply(struct ovsdb_idl_txn
*txn
,
1507 const struct json_array
*results
)
1509 struct json
*count
, *rows
, *row
, *column
;
1510 struct shash
*mutate
, *select
;
1512 if (txn
->inc_index
+ 2 > results
->n
) {
1513 VLOG_WARN_RL(&syntax_rl
, "reply does not contain enough operations "
1514 "for increment (has %u, needs %u)",
1515 results
->n
, txn
->inc_index
+ 2);
1519 /* We know that this is a JSON object because the loop in
1520 * ovsdb_idl_txn_process_reply() checked. */
1521 mutate
= json_object(results
->elems
[txn
->inc_index
]);
1522 count
= shash_find_data(mutate
, "count");
1523 if (!check_json_type(count
, JSON_INTEGER
, "\"mutate\" reply \"count\"")) {
1526 if (count
->u
.integer
!= 1) {
1527 VLOG_WARN_RL(&syntax_rl
,
1528 "\"mutate\" reply \"count\" is %"PRId64
" instead of 1",
1533 select
= json_object(results
->elems
[txn
->inc_index
+ 1]);
1534 rows
= shash_find_data(select
, "rows");
1535 if (!check_json_type(rows
, JSON_ARRAY
, "\"select\" reply \"rows\"")) {
1538 if (rows
->u
.array
.n
!= 1) {
1539 VLOG_WARN_RL(&syntax_rl
, "\"select\" reply \"rows\" has %u elements "
1544 row
= rows
->u
.array
.elems
[0];
1545 if (!check_json_type(row
, JSON_OBJECT
, "\"select\" reply row")) {
1548 column
= shash_find_data(json_object(row
), txn
->inc_column
);
1549 if (!check_json_type(column
, JSON_INTEGER
,
1550 "\"select\" reply inc column")) {
1553 txn
->inc_new_value
= column
->u
.integer
;
1558 ovsdb_idl_txn_process_insert_reply(struct ovsdb_idl_txn_insert
*insert
,
1559 const struct json_array
*results
)
1561 static const struct ovsdb_base_type uuid_type
= OVSDB_BASE_UUID_INIT
;
1562 struct ovsdb_error
*error
;
1563 struct json
*json_uuid
;
1564 union ovsdb_atom uuid
;
1565 struct shash
*reply
;
1567 if (insert
->op_index
>= results
->n
) {
1568 VLOG_WARN_RL(&syntax_rl
, "reply does not contain enough operations "
1569 "for insert (has %u, needs %u)",
1570 results
->n
, insert
->op_index
);
1574 /* We know that this is a JSON object because the loop in
1575 * ovsdb_idl_txn_process_reply() checked. */
1576 reply
= json_object(results
->elems
[insert
->op_index
]);
1577 json_uuid
= shash_find_data(reply
, "uuid");
1578 if (!check_json_type(json_uuid
, JSON_ARRAY
, "\"insert\" reply \"uuid\"")) {
1582 error
= ovsdb_atom_from_json(&uuid
, &uuid_type
, json_uuid
, NULL
);
1584 char *s
= ovsdb_error_to_string(error
);
1585 VLOG_WARN_RL(&syntax_rl
, "\"insert\" reply \"uuid\" is not a JSON "
1591 insert
->real
= uuid
.uuid
;
1597 ovsdb_idl_txn_process_reply(struct ovsdb_idl
*idl
,
1598 const struct jsonrpc_msg
*msg
)
1600 struct ovsdb_idl_txn
*txn
;
1601 enum ovsdb_idl_txn_status status
;
1603 txn
= ovsdb_idl_txn_find(idl
, msg
->id
);
1608 if (msg
->type
== JSONRPC_ERROR
) {
1610 } else if (msg
->result
->type
!= JSON_ARRAY
) {
1611 VLOG_WARN_RL(&syntax_rl
, "reply to \"transact\" is not JSON array");
1614 struct json_array
*ops
= &msg
->result
->u
.array
;
1615 int hard_errors
= 0;
1616 int soft_errors
= 0;
1619 for (i
= 0; i
< ops
->n
; i
++) {
1620 struct json
*op
= ops
->elems
[i
];
1622 if (op
->type
== JSON_NULL
) {
1623 /* This isn't an error in itself but indicates that some prior
1624 * operation failed, so make sure that we know about it. */
1626 } else if (op
->type
== JSON_OBJECT
) {
1629 error
= shash_find_data(json_object(op
), "error");
1631 if (error
->type
== JSON_STRING
) {
1632 if (!strcmp(error
->u
.string
, "timed out")) {
1634 } else if (strcmp(error
->u
.string
, "aborted")) {
1636 ovsdb_idl_txn_set_error_json(txn
, op
);
1640 ovsdb_idl_txn_set_error_json(txn
, op
);
1641 VLOG_WARN_RL(&syntax_rl
,
1642 "\"error\" in reply is not JSON string");
1647 ovsdb_idl_txn_set_error_json(txn
, op
);
1648 VLOG_WARN_RL(&syntax_rl
,
1649 "operation reply is not JSON null or object");
1653 if (!soft_errors
&& !hard_errors
) {
1654 struct ovsdb_idl_txn_insert
*insert
;
1656 if (txn
->inc_table
&& !ovsdb_idl_txn_process_inc_reply(txn
, ops
)) {
1660 HMAP_FOR_EACH (insert
, struct ovsdb_idl_txn_insert
, hmap_node
,
1661 &txn
->inserted_rows
) {
1662 if (!ovsdb_idl_txn_process_insert_reply(insert
, ops
)) {
1668 status
= (hard_errors
? TXN_ERROR
1669 : soft_errors
? TXN_TRY_AGAIN
1673 ovsdb_idl_txn_complete(txn
, status
);
1677 struct ovsdb_idl_txn
*
1678 ovsdb_idl_txn_get(const struct ovsdb_idl_row
*row
)
1680 struct ovsdb_idl_txn
*txn
= row
->table
->idl
->txn
;
1681 assert(txn
!= NULL
);