1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Nicira, Inc.
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 "transaction.h"
21 #include "openvswitch/dynamic-string.h"
25 #include "openvswitch/hmap.h"
26 #include "openvswitch/json.h"
27 #include "openvswitch/list.h"
28 #include "openvswitch/poll-loop.h"
29 #include "openvswitch/vlog.h"
30 #include "ovsdb-error.h"
37 VLOG_DEFINE_THIS_MODULE(transaction
);
41 struct ovs_list txn_tables
; /* Contains "struct ovsdb_txn_table"s. */
45 /* A table modified by a transaction. */
46 struct ovsdb_txn_table
{
47 struct ovs_list node
; /* Element in ovsdb_txn's txn_tables list. */
48 struct ovsdb_table
*table
;
49 struct hmap txn_rows
; /* Contains "struct ovsdb_txn_row"s. */
51 /* This has the same form as the 'indexes' member of struct ovsdb_table,
52 * but it is only used or updated at transaction commit time, from
53 * check_index_uniqueness(). */
54 struct hmap
*txn_indexes
;
56 /* Used by for_each_txn_row(). */
57 unsigned int serial
; /* Serial number of in-progress iteration. */
58 unsigned int n_processed
; /* Number of rows processed. */
61 /* A row modified by the transaction:
63 * - A row added by a transaction will have null 'old' and non-null 'new'.
65 * - A row deleted by a transaction will have non-null 'old' and null
68 * - A row modified by a transaction will have non-null 'old' and 'new'.
70 * - 'old' and 'new' both null indicates that a row was added then deleted
71 * within a single transaction. Most of the time we instead delete the
72 * ovsdb_txn_row entirely, but inside a for_each_txn_row() callback
73 * there are restrictions that sometimes mean we have to leave the
74 * ovsdb_txn_row in place.
76 struct ovsdb_txn_row
{
77 struct hmap_node hmap_node
; /* In ovsdb_txn_table's txn_rows hmap. */
78 struct ovsdb_row
*old
; /* The old row. */
79 struct ovsdb_row
*new; /* The new row. */
80 size_t n_refs
; /* Number of remaining references. */
82 /* These members are the same as the corresponding members of 'old' or
83 * 'new'. They are present here for convenience and because occasionally
84 * there can be an ovsdb_txn_row where both 'old' and 'new' are NULL. */
86 struct ovsdb_table
*table
;
88 /* Used by for_each_txn_row(). */
89 unsigned int serial
; /* Serial number of in-progress commit. */
91 unsigned long changed
[]; /* Bits set to 1 for columns that changed. */
94 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
95 delete_garbage_row(struct ovsdb_txn
*txn
, struct ovsdb_txn_row
*r
);
96 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row
*);
97 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
98 for_each_txn_row(struct ovsdb_txn
*txn
,
99 struct ovsdb_error
*(*)(struct ovsdb_txn
*,
100 struct ovsdb_txn_row
*));
102 /* Used by for_each_txn_row() to track tables and rows that have been
104 static unsigned int serial
;
107 ovsdb_txn_create(struct ovsdb
*db
)
109 struct ovsdb_txn
*txn
= xmalloc(sizeof *txn
);
111 ovs_list_init(&txn
->txn_tables
);
112 ds_init(&txn
->comment
);
117 ovsdb_txn_free(struct ovsdb_txn
*txn
)
119 ovs_assert(ovs_list_is_empty(&txn
->txn_tables
));
120 ds_destroy(&txn
->comment
);
124 static struct ovsdb_error
*
125 ovsdb_txn_row_abort(struct ovsdb_txn
*txn OVS_UNUSED
,
126 struct ovsdb_txn_row
*txn_row
)
128 struct ovsdb_row
*old
= txn_row
->old
;
129 struct ovsdb_row
*new = txn_row
->new;
131 ovsdb_txn_row_prefree(txn_row
);
134 hmap_remove(&new->table
->rows
, &new->hmap_node
);
137 hmap_insert(&old
->table
->rows
, &old
->hmap_node
, ovsdb_row_hash(old
));
139 hmap_replace(&new->table
->rows
, &new->hmap_node
, &old
->hmap_node
);
141 ovsdb_row_destroy(new);
147 /* Returns the offset in bytes from the start of an ovsdb_row for 'table' to
148 * the hmap_node for the index numbered 'i'. */
150 ovsdb_row_index_offset__(const struct ovsdb_table
*table
, size_t i
)
152 size_t n_fields
= shash_count(&table
->schema
->columns
);
153 return (offsetof(struct ovsdb_row
, fields
)
154 + n_fields
* sizeof(struct ovsdb_datum
)
155 + i
* sizeof(struct hmap_node
));
158 /* Returns the hmap_node in 'row' for the index numbered 'i'. */
159 static struct hmap_node
*
160 ovsdb_row_get_index_node(struct ovsdb_row
*row
, size_t i
)
162 return (void *) ((char *) row
+ ovsdb_row_index_offset__(row
->table
, i
));
165 /* Returns the ovsdb_row given 'index_node', which is a pointer to that row's
166 * hmap_node for the index numbered 'i' within 'table'. */
167 static struct ovsdb_row
*
168 ovsdb_row_from_index_node(struct hmap_node
*index_node
,
169 const struct ovsdb_table
*table
, size_t i
)
171 return (void *) ((char *) index_node
- ovsdb_row_index_offset__(table
, i
));
175 ovsdb_txn_abort(struct ovsdb_txn
*txn
)
177 ovsdb_error_assert(for_each_txn_row(txn
, ovsdb_txn_row_abort
));
181 static struct ovsdb_txn_row
*
182 find_txn_row(const struct ovsdb_table
*table
, const struct uuid
*uuid
)
184 struct ovsdb_txn_row
*txn_row
;
186 if (!table
->txn_table
) {
190 HMAP_FOR_EACH_WITH_HASH (txn_row
, hmap_node
,
191 uuid_hash(uuid
), &table
->txn_table
->txn_rows
) {
192 if (uuid_equals(uuid
, &txn_row
->uuid
)) {
200 static struct ovsdb_txn_row
*
201 find_or_make_txn_row(struct ovsdb_txn
*txn
, const struct ovsdb_table
*table
,
202 const struct uuid
*uuid
)
204 struct ovsdb_txn_row
*txn_row
= find_txn_row(table
, uuid
);
206 const struct ovsdb_row
*row
= ovsdb_table_get_row(table
, uuid
);
208 txn_row
= ovsdb_txn_row_modify(txn
, row
)->txn_row
;
214 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
215 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn
*txn
, const struct ovsdb_row
*r
,
216 const struct ovsdb_column
*c
,
217 const struct ovsdb_base_type
*base
,
218 const union ovsdb_atom
*atoms
, unsigned int n
,
221 const struct ovsdb_table
*table
;
224 if (!ovsdb_base_type_is_strong_ref(base
)) {
228 table
= base
->u
.uuid
.refTable
;
229 for (i
= 0; i
< n
; i
++) {
230 const struct uuid
*uuid
= &atoms
[i
].uuid
;
231 struct ovsdb_txn_row
*txn_row
;
233 if (uuid_equals(uuid
, ovsdb_row_get_uuid(r
))) {
234 /* Self-references don't count. */
238 txn_row
= find_or_make_txn_row(txn
, table
, uuid
);
240 return ovsdb_error("referential integrity violation",
241 "Table %s column %s row "UUID_FMT
" "
242 "references nonexistent row "UUID_FMT
" in "
244 r
->table
->schema
->name
, c
->name
,
245 UUID_ARGS(ovsdb_row_get_uuid(r
)),
246 UUID_ARGS(uuid
), table
->schema
->name
);
248 txn_row
->n_refs
+= delta
;
254 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
255 ovsdb_txn_adjust_row_refs(struct ovsdb_txn
*txn
, const struct ovsdb_row
*r
,
256 const struct ovsdb_column
*column
, int delta
)
258 const struct ovsdb_datum
*field
= &r
->fields
[column
->index
];
259 struct ovsdb_error
*error
;
261 error
= ovsdb_txn_adjust_atom_refs(txn
, r
, column
, &column
->type
.key
,
262 field
->keys
, field
->n
, delta
);
264 error
= ovsdb_txn_adjust_atom_refs(txn
, r
, column
, &column
->type
.value
,
265 field
->values
, field
->n
, delta
);
270 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
271 update_row_ref_count(struct ovsdb_txn
*txn
, struct ovsdb_txn_row
*r
)
273 struct ovsdb_table
*table
= r
->table
;
274 struct shash_node
*node
;
276 SHASH_FOR_EACH (node
, &table
->schema
->columns
) {
277 const struct ovsdb_column
*column
= node
->data
;
278 struct ovsdb_error
*error
;
280 if (bitmap_is_set(r
->changed
, column
->index
)) {
282 error
= ovsdb_txn_adjust_row_refs(txn
, r
->old
, column
, -1);
284 return OVSDB_WRAP_BUG("error decreasing refcount", error
);
288 error
= ovsdb_txn_adjust_row_refs(txn
, r
->new, column
, 1);
299 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
300 check_ref_count(struct ovsdb_txn
*txn OVS_UNUSED
, struct ovsdb_txn_row
*r
)
302 if (r
->new || !r
->n_refs
) {
305 return ovsdb_error("referential integrity violation",
306 "cannot delete %s row "UUID_FMT
" because "
307 "of %"PRIuSIZE
" remaining reference(s)",
308 r
->table
->schema
->name
, UUID_ARGS(&r
->uuid
),
313 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
314 delete_row_refs(struct ovsdb_txn
*txn
, const struct ovsdb_row
*row
,
315 const struct ovsdb_base_type
*base
,
316 const union ovsdb_atom
*atoms
, unsigned int n
)
318 const struct ovsdb_table
*table
;
321 if (!ovsdb_base_type_is_strong_ref(base
)) {
325 table
= base
->u
.uuid
.refTable
;
326 for (i
= 0; i
< n
; i
++) {
327 const struct uuid
*uuid
= &atoms
[i
].uuid
;
328 struct ovsdb_txn_row
*txn_row
;
330 if (uuid_equals(uuid
, ovsdb_row_get_uuid(row
))) {
331 /* Self-references don't count. */
335 txn_row
= find_or_make_txn_row(txn
, table
, uuid
);
337 return OVSDB_BUG("strong ref target missing");
338 } else if (!txn_row
->n_refs
) {
339 return OVSDB_BUG("strong ref target has zero n_refs");
340 } else if (!txn_row
->new) {
341 return OVSDB_BUG("deleted strong ref target");
344 if (--txn_row
->n_refs
== 0) {
345 struct ovsdb_error
*error
= delete_garbage_row(txn
, txn_row
);
355 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
356 delete_garbage_row(struct ovsdb_txn
*txn
, struct ovsdb_txn_row
*txn_row
)
358 struct shash_node
*node
;
359 struct ovsdb_row
*row
;
361 if (txn_row
->table
->schema
->is_root
) {
367 hmap_remove(&txn_row
->table
->rows
, &row
->hmap_node
);
368 SHASH_FOR_EACH (node
, &txn_row
->table
->schema
->columns
) {
369 const struct ovsdb_column
*column
= node
->data
;
370 const struct ovsdb_datum
*field
= &row
->fields
[column
->index
];
371 struct ovsdb_error
*error
;
373 error
= delete_row_refs(txn
, row
,
374 &column
->type
.key
, field
->keys
, field
->n
);
379 error
= delete_row_refs(txn
, row
,
380 &column
->type
.value
, field
->values
, field
->n
);
385 ovsdb_row_destroy(row
);
390 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
391 collect_garbage(struct ovsdb_txn
*txn
, struct ovsdb_txn_row
*txn_row
)
393 if (txn_row
->new && !txn_row
->n_refs
) {
394 return delete_garbage_row(txn
, txn_row
);
399 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
400 update_ref_counts(struct ovsdb_txn
*txn
)
402 struct ovsdb_error
*error
;
404 error
= for_each_txn_row(txn
, update_row_ref_count
);
409 return for_each_txn_row(txn
, check_ref_count
);
412 static struct ovsdb_error
*
413 ovsdb_txn_row_commit(struct ovsdb_txn
*txn OVS_UNUSED
,
414 struct ovsdb_txn_row
*txn_row
)
416 size_t n_indexes
= txn_row
->table
->schema
->n_indexes
;
421 for (i
= 0; i
< n_indexes
; i
++) {
422 struct hmap_node
*node
= ovsdb_row_get_index_node(txn_row
->old
, i
);
423 hmap_remove(&txn_row
->table
->indexes
[i
], node
);
429 for (i
= 0; i
< n_indexes
; i
++) {
430 struct hmap_node
*node
= ovsdb_row_get_index_node(txn_row
->new, i
);
431 hmap_insert(&txn_row
->table
->indexes
[i
], node
, node
->hash
);
435 ovsdb_txn_row_prefree(txn_row
);
437 txn_row
->new->n_refs
= txn_row
->n_refs
;
439 ovsdb_row_destroy(txn_row
->old
);
445 static struct ovsdb_error
*
446 ovsdb_txn_update_weak_refs(struct ovsdb_txn
*txn OVS_UNUSED
,
447 struct ovsdb_txn_row
*txn_row
)
449 struct ovsdb_weak_ref
*weak
, *next
;
451 /* Remove the weak references originating in the old version of the row. */
453 LIST_FOR_EACH_SAFE (weak
, next
, src_node
, &txn_row
->old
->src_refs
) {
454 ovs_list_remove(&weak
->src_node
);
455 ovs_list_remove(&weak
->dst_node
);
460 /* Although the originating rows have the responsibility of updating the
461 * weak references in the dst, it is possible that some source rows aren't
462 * part of the transaction. In that situation this row needs to move the
463 * list of incoming weak references from the old row into the new one.
465 if (txn_row
->old
&& txn_row
->new) {
466 /* Move the incoming weak references from old to new. */
467 ovs_list_push_back_all(&txn_row
->new->dst_refs
,
468 &txn_row
->old
->dst_refs
);
471 /* Insert the weak references originating in the new version of the row. */
472 struct ovsdb_row
*dst_row
;
474 LIST_FOR_EACH (weak
, src_node
, &txn_row
->new->src_refs
) {
475 /* dst_row MUST exist. */
476 dst_row
= CONST_CAST(struct ovsdb_row
*,
477 ovsdb_table_get_row(weak
->dst_table
, &weak
->dst
));
478 ovs_list_insert(&dst_row
->dst_refs
, &weak
->dst_node
);
486 add_weak_ref(const struct ovsdb_row
*src_
, const struct ovsdb_row
*dst_
)
488 struct ovsdb_row
*src
= CONST_CAST(struct ovsdb_row
*, src_
);
489 struct ovsdb_row
*dst
= CONST_CAST(struct ovsdb_row
*, dst_
);
490 struct ovsdb_weak_ref
*weak
;
496 if (!ovs_list_is_empty(&dst
->dst_refs
)) {
497 /* Omit duplicates. */
498 weak
= CONTAINER_OF(ovs_list_back(&dst
->dst_refs
),
499 struct ovsdb_weak_ref
, dst_node
);
500 if (weak
->src
== src
) {
505 weak
= xmalloc(sizeof *weak
);
507 weak
->dst_table
= dst
->table
;
508 weak
->dst
= *ovsdb_row_get_uuid(dst
);
509 /* The dst_refs list is updated at commit time. */
510 ovs_list_init(&weak
->dst_node
);
511 ovs_list_push_back(&src
->src_refs
, &weak
->src_node
);
514 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
515 assess_weak_refs(struct ovsdb_txn
*txn
, struct ovsdb_txn_row
*txn_row
)
517 struct ovsdb_table
*table
;
518 struct shash_node
*node
;
520 if (txn_row
->old
&& !txn_row
->new) {
521 /* Mark rows that have weak references to 'txn_row' as modified, so
522 * that their weak references will get reassessed. */
523 struct ovsdb_weak_ref
*weak
, *next
;
525 LIST_FOR_EACH_SAFE (weak
, next
, dst_node
, &txn_row
->old
->dst_refs
) {
526 if (!weak
->src
->txn_row
) {
527 ovsdb_txn_row_modify(txn
, weak
->src
);
533 /* We don't have to do anything about references that originate at
534 * 'txn_row', because ovsdb_row_destroy() will remove those weak
539 table
= txn_row
->table
;
540 SHASH_FOR_EACH (node
, &table
->schema
->columns
) {
541 const struct ovsdb_column
*column
= node
->data
;
542 struct ovsdb_datum
*datum
= &txn_row
->new->fields
[column
->index
];
543 unsigned int orig_n
, i
;
548 if (ovsdb_base_type_is_weak_ref(&column
->type
.key
)) {
549 for (i
= 0; i
< datum
->n
; ) {
550 const struct ovsdb_row
*row
;
552 row
= ovsdb_table_get_row(column
->type
.key
.u
.uuid
.refTable
,
553 &datum
->keys
[i
].uuid
);
555 add_weak_ref(txn_row
->new, row
);
558 if (uuid_is_zero(&datum
->keys
[i
].uuid
)) {
561 ovsdb_datum_remove_unsafe(datum
, i
, &column
->type
);
566 if (ovsdb_base_type_is_weak_ref(&column
->type
.value
)) {
567 for (i
= 0; i
< datum
->n
; ) {
568 const struct ovsdb_row
*row
;
570 row
= ovsdb_table_get_row(column
->type
.value
.u
.uuid
.refTable
,
571 &datum
->values
[i
].uuid
);
573 add_weak_ref(txn_row
->new, row
);
576 if (uuid_is_zero(&datum
->values
[i
].uuid
)) {
579 ovsdb_datum_remove_unsafe(datum
, i
, &column
->type
);
584 if (datum
->n
!= orig_n
) {
585 bitmap_set1(txn_row
->changed
, column
->index
);
586 ovsdb_datum_sort_assert(datum
, column
->type
.key
.type
);
587 if (datum
->n
< column
->type
.n_min
) {
588 const struct uuid
*row_uuid
= ovsdb_row_get_uuid(txn_row
->new);
589 if (zero
&& !txn_row
->old
) {
591 "constraint violation",
592 "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
593 " (inserted within this transaction) contained "
594 "all-zeros UUID (probably as the default value for "
595 "this column) but deleting this value caused a "
596 "constraint volation because this column is not "
597 "allowed to be empty.", column
->name
,
598 table
->schema
->name
, UUID_ARGS(row_uuid
));
601 "constraint violation",
602 "Deletion of %u weak reference(s) to deleted (or "
603 "never-existing) rows from column \"%s\" in \"%s\" "
604 "row "UUID_FMT
" %scaused this column to become empty, "
605 "but constraints on this column disallow an "
607 orig_n
- datum
->n
, column
->name
, table
->schema
->name
,
611 : "(inserted within this transaction) "));
620 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
621 determine_changes(struct ovsdb_txn
*txn
, struct ovsdb_txn_row
*txn_row
)
623 struct ovsdb_table
*table
= txn_row
->table
;
625 if (txn_row
->old
&& txn_row
->new) {
626 struct shash_node
*node
;
627 bool changed
= false;
629 SHASH_FOR_EACH (node
, &table
->schema
->columns
) {
630 const struct ovsdb_column
*column
= node
->data
;
631 const struct ovsdb_type
*type
= &column
->type
;
632 unsigned int idx
= column
->index
;
634 if (!ovsdb_datum_equals(&txn_row
->old
->fields
[idx
],
635 &txn_row
->new->fields
[idx
],
637 bitmap_set1(txn_row
->changed
, idx
);
643 /* Nothing actually changed in this row, so drop it. */
644 ovsdb_txn_row_abort(txn
, txn_row
);
647 bitmap_set_multiple(txn_row
->changed
, 0,
648 shash_count(&table
->schema
->columns
), 1);
654 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
655 check_max_rows(struct ovsdb_txn
*txn
)
657 struct ovsdb_txn_table
*t
;
659 LIST_FOR_EACH (t
, node
, &txn
->txn_tables
) {
660 size_t n_rows
= hmap_count(&t
->table
->rows
);
661 unsigned int max_rows
= t
->table
->schema
->max_rows
;
663 if (n_rows
> max_rows
) {
664 return ovsdb_error("constraint violation",
665 "transaction causes \"%s\" table to contain "
666 "%"PRIuSIZE
" rows, greater than the schema-defined "
667 "limit of %u row(s)",
668 t
->table
->schema
->name
, n_rows
, max_rows
);
675 static struct ovsdb_row
*
676 ovsdb_index_search(struct hmap
*index
, struct ovsdb_row
*row
, size_t i
,
679 const struct ovsdb_table
*table
= row
->table
;
680 const struct ovsdb_column_set
*columns
= &table
->schema
->indexes
[i
];
681 struct hmap_node
*node
;
683 for (node
= hmap_first_with_hash(index
, hash
); node
;
684 node
= hmap_next_with_hash(node
)) {
685 struct ovsdb_row
*irow
= ovsdb_row_from_index_node(node
, table
, i
);
686 if (ovsdb_row_equal_columns(row
, irow
, columns
)) {
695 duplicate_index_row__(const struct ovsdb_column_set
*index
,
696 const struct ovsdb_row
*row
,
700 size_t n_columns
= shash_count(&row
->table
->schema
->columns
);
702 ds_put_format(out
, "%s row, with UUID "UUID_FMT
", ",
703 title
, UUID_ARGS(ovsdb_row_get_uuid(row
)));
705 || bitmap_scan(row
->txn_row
->changed
, 1, 0, n_columns
) == n_columns
) {
706 ds_put_cstr(out
, "existed in the database before this "
707 "transaction and was not modified by the transaction.");
708 } else if (!row
->txn_row
->old
) {
709 ds_put_cstr(out
, "was inserted by this transaction.");
710 } else if (ovsdb_row_equal_columns(row
->txn_row
->old
,
711 row
->txn_row
->new, index
)) {
712 ds_put_cstr(out
, "existed in the database before this "
713 "transaction, which modified some of the row's columns "
714 "but not any columns in this index.");
716 ds_put_cstr(out
, "had the following index values before the "
718 ovsdb_row_columns_to_string(row
->txn_row
->old
, index
, out
);
719 ds_put_char(out
, '.');
723 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
724 duplicate_index_row(const struct ovsdb_column_set
*index
,
725 const struct ovsdb_row
*a
,
726 const struct ovsdb_row
*b
)
728 struct ovsdb_column_set all_columns
;
729 struct ovsdb_error
*error
;
733 /* Put 'a' and 'b' in a predictable order to make error messages
734 * reproducible for testing. */
735 ovsdb_column_set_init(&all_columns
);
736 ovsdb_column_set_add_all(&all_columns
, a
->table
);
737 if (ovsdb_row_compare_columns_3way(a
, b
, &all_columns
) < 0) {
738 const struct ovsdb_row
*tmp
= a
;
742 ovsdb_column_set_destroy(&all_columns
);
744 index_s
= ovsdb_column_set_to_string(index
);
747 ds_put_format(&s
, "Transaction causes multiple rows in \"%s\" table to "
748 "have identical values (", a
->table
->schema
->name
);
749 ovsdb_row_columns_to_string(a
, index
, &s
);
750 ds_put_format(&s
, ") for index on %s. ", index_s
);
751 duplicate_index_row__(index
, a
, "First", &s
);
752 ds_put_cstr(&s
, " ");
753 duplicate_index_row__(index
, b
, "Second", &s
);
757 error
= ovsdb_error("constraint violation", "%s", ds_cstr(&s
));
762 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
763 check_index_uniqueness(struct ovsdb_txn
*txn OVS_UNUSED
,
764 struct ovsdb_txn_row
*txn_row
)
766 struct ovsdb_txn_table
*txn_table
= txn_row
->table
->txn_table
;
767 struct ovsdb_table
*table
= txn_row
->table
;
768 struct ovsdb_row
*row
= txn_row
->new;
775 for (i
= 0; i
< table
->schema
->n_indexes
; i
++) {
776 const struct ovsdb_column_set
*index
= &table
->schema
->indexes
[i
];
777 struct ovsdb_row
*irow
;
780 hash
= ovsdb_row_hash_columns(row
, index
, 0);
781 irow
= ovsdb_index_search(&txn_table
->txn_indexes
[i
], row
, i
, hash
);
783 return duplicate_index_row(index
, irow
, row
);
786 irow
= ovsdb_index_search(&table
->indexes
[i
], row
, i
, hash
);
787 if (irow
&& !irow
->txn_row
) {
788 return duplicate_index_row(index
, irow
, row
);
791 hmap_insert(&txn_table
->txn_indexes
[i
],
792 ovsdb_row_get_index_node(row
, i
), hash
);
798 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
799 update_version(struct ovsdb_txn
*txn OVS_UNUSED
, struct ovsdb_txn_row
*txn_row
)
801 struct ovsdb_table
*table
= txn_row
->table
;
802 size_t n_columns
= shash_count(&table
->schema
->columns
);
804 if (txn_row
->old
&& txn_row
->new
805 && !bitmap_is_all_zeros(txn_row
->changed
, n_columns
)) {
806 bitmap_set1(txn_row
->changed
, OVSDB_COL_VERSION
);
807 uuid_generate(ovsdb_row_get_version_rw(txn_row
->new));
814 ovsdb_txn_is_empty(const struct ovsdb_txn
*txn
)
816 return ovs_list_is_empty(&txn
->txn_tables
);
819 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
820 ovsdb_txn_precommit(struct ovsdb_txn
*txn
)
822 struct ovsdb_error
*error
;
824 /* Figure out what actually changed, and abort early if the transaction
825 * was really a no-op. */
826 error
= for_each_txn_row(txn
, determine_changes
);
828 ovsdb_txn_abort(txn
);
829 return OVSDB_WRAP_BUG("can't happen", error
);
831 if (ovs_list_is_empty(&txn
->txn_tables
)) {
835 /* Update reference counts and check referential integrity. */
836 error
= update_ref_counts(txn
);
841 /* Delete unreferenced, non-root rows. */
842 error
= for_each_txn_row(txn
, collect_garbage
);
844 return OVSDB_WRAP_BUG("can't happen", error
);
847 /* Check maximum rows table constraints. */
848 error
= check_max_rows(txn
);
853 /* Check reference counts and remove bad references for "weak" referential
855 error
= for_each_txn_row(txn
, assess_weak_refs
);
860 /* Verify that the indexes will still be unique post-transaction. */
861 error
= for_each_txn_row(txn
, check_index_uniqueness
);
866 /* Update _version for rows that changed. */
867 error
= for_each_txn_row(txn
, update_version
);
869 return OVSDB_WRAP_BUG("can't happen", error
);
875 /* Finalize commit. */
877 ovsdb_txn_complete(struct ovsdb_txn
*txn
)
879 if (!ovsdb_txn_is_empty(txn
)) {
880 txn
->db
->run_triggers
= true;
881 ovsdb_monitors_commit(txn
->db
, txn
);
882 ovsdb_error_assert(for_each_txn_row(txn
, ovsdb_txn_update_weak_refs
));
883 ovsdb_error_assert(for_each_txn_row(txn
, ovsdb_txn_row_commit
));
888 /* Applies 'txn' to the internal representation of the database. This is for
889 * transactions that don't need to be written to storage; probably, they came
890 * from storage. These transactions shouldn't ordinarily fail because storage
891 * should contain only consistent transactions. (One exception is for database
892 * conversion in ovsdb_convert().) */
893 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
894 ovsdb_txn_replay_commit(struct ovsdb_txn
*txn
)
896 struct ovsdb_error
*error
= ovsdb_txn_precommit(txn
);
898 ovsdb_txn_abort(txn
);
900 ovsdb_txn_complete(txn
);
905 /* If 'error' is nonnull, the transaction is complete, with the given error as
908 * Otherwise, if 'write' is nonnull, then the transaction is waiting for
909 * 'write' to complete.
911 * Otherwise, if 'commit_index' is nonzero, then the transaction is waiting for
912 * 'commit_index' to be applied to the storage.
914 * Otherwise, the transaction is complete and successful. */
915 struct ovsdb_txn_progress
{
916 struct ovsdb_error
*error
;
917 struct ovsdb_write
*write
;
918 uint64_t commit_index
;
920 struct ovsdb_storage
*storage
;
923 struct ovsdb_txn_progress
*
924 ovsdb_txn_propose_schema_change(struct ovsdb
*db
,
925 const struct json
*schema
,
926 const struct json
*data
)
928 struct ovsdb_txn_progress
*progress
= xzalloc(sizeof *progress
);
929 progress
->storage
= db
->storage
;
932 struct ovsdb_write
*write
= ovsdb_storage_write_schema_change(
933 db
->storage
, schema
, data
, &db
->prereq
, &next
);
934 if (!ovsdb_write_is_complete(write
)) {
935 progress
->write
= write
;
937 progress
->error
= ovsdb_error_clone(ovsdb_write_get_error(write
));
938 ovsdb_write_destroy(write
);
943 struct ovsdb_txn_progress
*
944 ovsdb_txn_propose_commit(struct ovsdb_txn
*txn
, bool durable
)
946 struct ovsdb_txn_progress
*progress
= xzalloc(sizeof *progress
);
947 progress
->storage
= txn
->db
->storage
;
948 progress
->error
= ovsdb_txn_precommit(txn
);
949 if (progress
->error
) {
953 /* Turn the commit into the format used for the storage logs.. */
954 struct json
*txn_json
= ovsdb_file_txn_to_json(txn
);
956 /* Nothing to do, so success. */
959 txn_json
= ovsdb_file_txn_annotate(txn_json
, ovsdb_txn_get_comment(txn
));
962 struct ovsdb_write
*write
= ovsdb_storage_write(
963 txn
->db
->storage
, txn_json
, &txn
->db
->prereq
, &next
, durable
);
964 json_destroy(txn_json
);
965 if (!ovsdb_write_is_complete(write
)) {
966 progress
->write
= write
;
968 progress
->error
= ovsdb_error_clone(ovsdb_write_get_error(write
));
969 ovsdb_write_destroy(write
);
974 /* Proposes 'txn' for commitment and then waits for the commit to succeed or
975 * fail. Returns null if successful, otherwise the error.
977 * **In addition**, this function also completes or aborts the transaction if
978 * the transaction succeeded or failed, respectively. */
979 struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
980 ovsdb_txn_propose_commit_block(struct ovsdb_txn
*txn
, bool durable
)
982 struct ovsdb_txn_progress
*p
= ovsdb_txn_propose_commit(txn
, durable
);
984 ovsdb_storage_run(p
->storage
);
985 if (ovsdb_txn_progress_is_complete(p
)) {
986 struct ovsdb_error
*error
987 = ovsdb_error_clone(ovsdb_txn_progress_get_error(p
));
988 ovsdb_txn_progress_destroy(p
);
991 ovsdb_txn_abort(txn
);
993 ovsdb_txn_complete(txn
);
998 ovsdb_storage_wait(p
->storage
);
1004 ovsdb_txn_progress_run(struct ovsdb_txn_progress
*p
)
1011 if (!ovsdb_write_is_complete(p
->write
)) {
1014 p
->error
= ovsdb_error_clone(ovsdb_write_get_error(p
->write
));
1015 p
->commit_index
= ovsdb_write_get_commit_index(p
->write
);
1016 ovsdb_write_destroy(p
->write
);
1024 if (p
->commit_index
) {
1025 if (ovsdb_storage_get_applied_index(p
->storage
) >= p
->commit_index
) {
1026 p
->commit_index
= 0;
1032 ovsdb_txn_progress_is_complete__(const struct ovsdb_txn_progress
*p
)
1034 return p
->error
|| (!p
->write
&& !p
->commit_index
);
1038 ovsdb_txn_progress_is_complete(const struct ovsdb_txn_progress
*p
)
1040 ovsdb_txn_progress_run(CONST_CAST(struct ovsdb_txn_progress
*, p
));
1041 return ovsdb_txn_progress_is_complete__(p
);
1044 const struct ovsdb_error
*
1045 ovsdb_txn_progress_get_error(const struct ovsdb_txn_progress
*p
)
1047 ovs_assert(ovsdb_txn_progress_is_complete__(p
));
1052 ovsdb_txn_progress_destroy(struct ovsdb_txn_progress
*p
)
1055 ovsdb_error_destroy(p
->error
);
1056 ovsdb_write_destroy(p
->write
);
1062 ovsdb_txn_for_each_change(const struct ovsdb_txn
*txn
,
1063 ovsdb_txn_row_cb_func
*cb
, void *aux
)
1065 struct ovsdb_txn_table
*t
;
1066 struct ovsdb_txn_row
*r
;
1068 LIST_FOR_EACH (t
, node
, &txn
->txn_tables
) {
1069 HMAP_FOR_EACH (r
, hmap_node
, &t
->txn_rows
) {
1070 if ((r
->old
|| r
->new) && !cb(r
->old
, r
->new, r
->changed
, aux
)) {
1077 static struct ovsdb_txn_table
*
1078 ovsdb_txn_create_txn_table(struct ovsdb_txn
*txn
, struct ovsdb_table
*table
)
1080 if (!table
->txn_table
) {
1081 struct ovsdb_txn_table
*txn_table
;
1084 table
->txn_table
= txn_table
= xmalloc(sizeof *table
->txn_table
);
1085 txn_table
->table
= table
;
1086 hmap_init(&txn_table
->txn_rows
);
1087 txn_table
->serial
= serial
- 1;
1088 txn_table
->txn_indexes
= xmalloc(table
->schema
->n_indexes
1089 * sizeof *txn_table
->txn_indexes
);
1090 for (i
= 0; i
< table
->schema
->n_indexes
; i
++) {
1091 hmap_init(&txn_table
->txn_indexes
[i
]);
1093 ovs_list_push_back(&txn
->txn_tables
, &txn_table
->node
);
1095 return table
->txn_table
;
1098 static struct ovsdb_txn_row
*
1099 ovsdb_txn_row_create(struct ovsdb_txn
*txn
, struct ovsdb_table
*table
,
1100 const struct ovsdb_row
*old_
, struct ovsdb_row
*new)
1102 const struct ovsdb_row
*row
= old_
? old_
: new;
1103 struct ovsdb_row
*old
= CONST_CAST(struct ovsdb_row
*, old_
);
1104 size_t n_columns
= shash_count(&table
->schema
->columns
);
1105 struct ovsdb_txn_table
*txn_table
;
1106 struct ovsdb_txn_row
*txn_row
;
1108 txn_row
= xzalloc(offsetof(struct ovsdb_txn_row
, changed
)
1109 + bitmap_n_bytes(n_columns
));
1110 txn_row
->uuid
= *ovsdb_row_get_uuid(row
);
1111 txn_row
->table
= row
->table
;
1114 txn_row
->n_refs
= old
? old
->n_refs
: 0;
1115 txn_row
->serial
= serial
- 1;
1118 old
->txn_row
= txn_row
;
1121 new->txn_row
= txn_row
;
1124 txn_table
= ovsdb_txn_create_txn_table(txn
, table
);
1125 hmap_insert(&txn_table
->txn_rows
, &txn_row
->hmap_node
,
1126 ovsdb_row_hash(old
? old
: new));
1132 ovsdb_txn_row_modify(struct ovsdb_txn
*txn
, const struct ovsdb_row
*ro_row_
)
1134 struct ovsdb_row
*ro_row
= CONST_CAST(struct ovsdb_row
*, ro_row_
);
1136 if (ro_row
->txn_row
) {
1137 ovs_assert(ro_row
== ro_row
->txn_row
->new);
1140 struct ovsdb_table
*table
= ro_row
->table
;
1141 struct ovsdb_row
*rw_row
;
1143 rw_row
= ovsdb_row_clone(ro_row
);
1144 rw_row
->n_refs
= ro_row
->n_refs
;
1145 ovsdb_txn_row_create(txn
, table
, ro_row
, rw_row
);
1146 hmap_replace(&table
->rows
, &ro_row
->hmap_node
, &rw_row
->hmap_node
);
1153 ovsdb_txn_row_insert(struct ovsdb_txn
*txn
, struct ovsdb_row
*row
)
1155 uint32_t hash
= ovsdb_row_hash(row
);
1156 struct ovsdb_table
*table
= row
->table
;
1158 uuid_generate(ovsdb_row_get_version_rw(row
));
1160 ovsdb_txn_row_create(txn
, table
, NULL
, row
);
1161 hmap_insert(&table
->rows
, &row
->hmap_node
, hash
);
1164 /* 'row' must be assumed destroyed upon return; the caller must not reference
1167 ovsdb_txn_row_delete(struct ovsdb_txn
*txn
, const struct ovsdb_row
*row_
)
1169 struct ovsdb_row
*row
= CONST_CAST(struct ovsdb_row
*, row_
);
1170 struct ovsdb_table
*table
= row
->table
;
1171 struct ovsdb_txn_row
*txn_row
= row
->txn_row
;
1173 hmap_remove(&table
->rows
, &row
->hmap_node
);
1176 ovsdb_txn_row_create(txn
, table
, row
, NULL
);
1178 ovs_assert(txn_row
->new == row
);
1180 txn_row
->new = NULL
;
1182 hmap_remove(&table
->txn_table
->txn_rows
, &txn_row
->hmap_node
);
1185 ovsdb_row_destroy(row
);
1190 ovsdb_txn_add_comment(struct ovsdb_txn
*txn
, const char *s
)
1192 if (txn
->comment
.length
) {
1193 ds_put_char(&txn
->comment
, '\n');
1195 ds_put_cstr(&txn
->comment
, s
);
1199 ovsdb_txn_get_comment(const struct ovsdb_txn
*txn
)
1201 return txn
->comment
.length
? ds_cstr_ro(&txn
->comment
) : NULL
;
1205 ovsdb_txn_row_prefree(struct ovsdb_txn_row
*txn_row
)
1207 struct ovsdb_txn_table
*txn_table
= txn_row
->table
->txn_table
;
1209 txn_table
->n_processed
--;
1210 hmap_remove(&txn_table
->txn_rows
, &txn_row
->hmap_node
);
1213 txn_row
->old
->txn_row
= NULL
;
1216 txn_row
->new->txn_row
= NULL
;
1221 ovsdb_txn_table_destroy(struct ovsdb_txn_table
*txn_table
)
1225 ovs_assert(hmap_is_empty(&txn_table
->txn_rows
));
1227 for (i
= 0; i
< txn_table
->table
->schema
->n_indexes
; i
++) {
1228 hmap_destroy(&txn_table
->txn_indexes
[i
]);
1230 free(txn_table
->txn_indexes
);
1232 txn_table
->table
->txn_table
= NULL
;
1233 hmap_destroy(&txn_table
->txn_rows
);
1234 ovs_list_remove(&txn_table
->node
);
1238 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
1239 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
1240 * returns a null pointer after iteration is complete.
1242 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
1243 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
1244 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
1245 * in within the same txn_table. It may *not* delete any txn_tables. As long
1246 * as these rules are followed, 'cb' will be called exactly once for each
1247 * txn_row in 'txn', even those added by 'cb'.
1249 * (Even though 'cb' is not allowed to delete some txn_rows, it can still
1250 * delete any actual row by clearing a txn_row's 'new' member.)
1252 static struct ovsdb_error
* OVS_WARN_UNUSED_RESULT
1253 for_each_txn_row(struct ovsdb_txn
*txn
,
1254 struct ovsdb_error
*(*cb
)(struct ovsdb_txn
*,
1255 struct ovsdb_txn_row
*))
1262 struct ovsdb_txn_table
*t
, *next_txn_table
;
1265 LIST_FOR_EACH_SAFE (t
, next_txn_table
, node
, &txn
->txn_tables
) {
1266 if (t
->serial
!= serial
) {
1271 while (t
->n_processed
< hmap_count(&t
->txn_rows
)) {
1272 struct ovsdb_txn_row
*r
, *next_txn_row
;
1274 HMAP_FOR_EACH_SAFE (r
, next_txn_row
, hmap_node
, &t
->txn_rows
) {
1275 if (r
->serial
!= serial
) {
1276 struct ovsdb_error
*error
;
1289 if (hmap_is_empty(&t
->txn_rows
)) {
1290 /* Table is empty. Drop it. */
1291 ovsdb_txn_table_destroy(t
);