1 /* Copyright (c) 2009, 2010, 2011 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 "transaction.h"
23 #include "dynamic-string.h"
28 #include "ovsdb-error.h"
36 struct list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
40 /* A table modified by a transaction. */
41 struct ovsdb_txn_table {
42 struct list node; /* Element in ovsdb_txn's txn_tables list. */
43 struct ovsdb_table *table;
44 struct hmap txn_rows; /* Contains "struct ovsdb_txn_row"s. */
46 /* Used by for_each_txn_row(). */
47 unsigned int serial; /* Serial number of in-progress iteration. */
48 unsigned int n_processed; /* Number of rows processed. */
51 /* A row modified by the transaction:
53 * - A row added by a transaction will have null 'old' and non-null 'new'.
55 * - A row deleted by a transaction will have non-null 'old' and null
58 * - A row modified by a transaction will have non-null 'old' and 'new'.
60 * - 'old' and 'new' both null indicates that a row was added then deleted
61 * within a single transaction. Most of the time we instead delete the
62 * ovsdb_txn_row entirely, but inside a for_each_txn_row() callback
63 * there are restrictions that sometimes mean we have to leave the
64 * ovsdb_txn_row in place.
66 struct ovsdb_txn_row {
67 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
68 struct ovsdb_row *old; /* The old row. */
69 struct ovsdb_row *new; /* The new row. */
70 size_t n_refs; /* Number of remaining references. */
72 /* These members are the same as the corresponding members of 'old' or
73 * 'new'. They are present here for convenience and because occasionally
74 * there can be an ovsdb_txn_row where both 'old' and 'new' are NULL. */
76 struct ovsdb_table *table;
78 /* Used by for_each_txn_row(). */
79 unsigned int serial; /* Serial number of in-progress commit. */
81 unsigned long changed[]; /* Bits set to 1 for columns that changed. */
84 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
85 static struct ovsdb_error * WARN_UNUSED_RESULT
86 for_each_txn_row(struct ovsdb_txn *txn,
87 struct ovsdb_error *(*)(struct ovsdb_txn *,
88 struct ovsdb_txn_row *));
90 /* Used by for_each_txn_row() to track tables and rows that have been
92 static unsigned int serial;
95 ovsdb_txn_create(struct ovsdb *db)
97 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
99 list_init(&txn->txn_tables);
100 ds_init(&txn->comment);
105 ovsdb_txn_free(struct ovsdb_txn *txn)
107 assert(list_is_empty(&txn->txn_tables));
108 ds_destroy(&txn->comment);
112 static struct ovsdb_error *
113 ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
114 struct ovsdb_txn_row *txn_row)
116 struct ovsdb_row *old = txn_row->old;
117 struct ovsdb_row *new = txn_row->new;
119 ovsdb_txn_row_prefree(txn_row);
122 hmap_remove(&new->table->rows, &new->hmap_node);
125 hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
127 hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
129 ovsdb_row_destroy(new);
136 ovsdb_txn_abort(struct ovsdb_txn *txn)
138 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
142 static struct ovsdb_txn_row *
143 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
145 struct ovsdb_txn_row *txn_row;
147 if (!table->txn_table) {
151 HMAP_FOR_EACH_WITH_HASH (txn_row, hmap_node,
152 uuid_hash(uuid), &table->txn_table->txn_rows) {
153 if (uuid_equals(uuid, &txn_row->uuid)) {
161 static struct ovsdb_error * WARN_UNUSED_RESULT
162 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
163 const struct ovsdb_column *c,
164 const struct ovsdb_base_type *base,
165 const union ovsdb_atom *atoms, unsigned int n,
168 const struct ovsdb_table *table;
171 if (!ovsdb_base_type_is_strong_ref(base)) {
175 table = base->u.uuid.refTable;
176 for (i = 0; i < n; i++) {
177 const struct uuid *uuid = &atoms[i].uuid;
178 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
179 if (uuid_equals(uuid, ovsdb_row_get_uuid(r))) {
180 /* Self-references don't count. */
184 const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
186 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
188 return ovsdb_error("referential integrity violation",
189 "Table %s column %s row "UUID_FMT" "
190 "references nonexistent row "UUID_FMT" in "
192 r->table->schema->name, c->name,
193 UUID_ARGS(ovsdb_row_get_uuid(r)),
194 UUID_ARGS(uuid), table->schema->name);
197 txn_row->n_refs += delta;
203 static struct ovsdb_error * WARN_UNUSED_RESULT
204 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
205 const struct ovsdb_column *column, int delta)
207 const struct ovsdb_datum *field = &r->fields[column->index];
208 struct ovsdb_error *error;
210 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.key,
211 field->keys, field->n, delta);
213 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.value,
214 field->values, field->n, delta);
219 static struct ovsdb_error * WARN_UNUSED_RESULT
220 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
222 struct ovsdb_table *table = r->table;
223 struct shash_node *node;
225 SHASH_FOR_EACH (node, &table->schema->columns) {
226 const struct ovsdb_column *column = node->data;
227 struct ovsdb_error *error;
230 error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
232 return OVSDB_WRAP_BUG("error decreasing refcount", error);
236 error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
246 static struct ovsdb_error * WARN_UNUSED_RESULT
247 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
249 if (r->new || !r->n_refs) {
252 return ovsdb_error("referential integrity violation",
253 "cannot delete %s row "UUID_FMT" because "
254 "of %zu remaining reference(s)",
255 r->table->schema->name, UUID_ARGS(&r->uuid),
260 static struct ovsdb_error * WARN_UNUSED_RESULT
261 update_ref_counts(struct ovsdb_txn *txn)
263 struct ovsdb_error *error;
265 error = for_each_txn_row(txn, update_row_ref_count);
270 return for_each_txn_row(txn, check_ref_count);
273 static struct ovsdb_error *
274 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
275 struct ovsdb_txn_row *txn_row)
277 ovsdb_txn_row_prefree(txn_row);
279 txn_row->new->n_refs = txn_row->n_refs;
281 ovsdb_row_destroy(txn_row->old);
288 add_weak_ref(struct ovsdb_txn *txn,
289 const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
291 struct ovsdb_row *src = (struct ovsdb_row *) src_;
292 struct ovsdb_row *dst = (struct ovsdb_row *) dst_;
293 struct ovsdb_weak_ref *weak;
299 dst = ovsdb_txn_row_modify(txn, dst);
301 if (!list_is_empty(&dst->dst_refs)) {
302 /* Omit duplicates. */
303 weak = CONTAINER_OF(list_back(&dst->dst_refs),
304 struct ovsdb_weak_ref, dst_node);
305 if (weak->src == src) {
310 weak = xmalloc(sizeof *weak);
312 list_push_back(&dst->dst_refs, &weak->dst_node);
313 list_push_back(&src->src_refs, &weak->src_node);
316 static struct ovsdb_error * WARN_UNUSED_RESULT
317 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
319 struct ovsdb_table *table;
320 struct shash_node *node;
323 /* Mark rows that have weak references to 'txn_row' as modified, so
324 * that their weak references will get reassessed. */
325 struct ovsdb_weak_ref *weak, *next;
327 LIST_FOR_EACH_SAFE (weak, next, dst_node, &txn_row->old->dst_refs) {
328 if (!weak->src->txn_row) {
329 ovsdb_txn_row_modify(txn, weak->src);
335 /* We don't have to do anything about references that originate at
336 * 'txn_row', because ovsdb_row_destroy() will remove those weak
341 table = txn_row->table;
342 SHASH_FOR_EACH (node, &table->schema->columns) {
343 const struct ovsdb_column *column = node->data;
344 struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
345 unsigned int orig_n, i;
350 if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
351 for (i = 0; i < datum->n; ) {
352 const struct ovsdb_row *row;
354 row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
355 &datum->keys[i].uuid);
357 add_weak_ref(txn, txn_row->new, row);
360 if (uuid_is_zero(&datum->keys[i].uuid)) {
363 ovsdb_datum_remove_unsafe(datum, i, &column->type);
368 if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
369 for (i = 0; i < datum->n; ) {
370 const struct ovsdb_row *row;
372 row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
373 &datum->values[i].uuid);
375 add_weak_ref(txn, txn_row->new, row);
378 if (uuid_is_zero(&datum->values[i].uuid)) {
381 ovsdb_datum_remove_unsafe(datum, i, &column->type);
386 if (datum->n != orig_n) {
387 bitmap_set1(txn_row->changed, column->index);
388 ovsdb_datum_sort_assert(datum, column->type.key.type);
389 if (datum->n < column->type.n_min) {
390 const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
391 if (zero && !txn_row->old) {
393 "constraint violation",
394 "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
395 " (inserted within this transaction) contained "
396 "all-zeros UUID (probably as the default value for "
397 "this column) but deleting this value caused a "
398 "constraint volation because this column is not "
399 "allowed to be empty.", column->name,
400 table->schema->name, UUID_ARGS(row_uuid));
403 "constraint violation",
404 "Deletion of %u weak reference(s) to deleted (or "
405 "never-existing) rows from column \"%s\" in \"%s\" "
406 "row "UUID_FMT" %scaused this column to become empty, "
407 "but constraints on this column disallow an "
409 orig_n - datum->n, column->name, table->schema->name,
413 : "(inserted within this transaction) "));
422 static struct ovsdb_error * WARN_UNUSED_RESULT
423 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
425 struct ovsdb_table *table = txn_row->table;
427 if (txn_row->old && txn_row->new) {
428 struct shash_node *node;
429 bool changed = false;
431 SHASH_FOR_EACH (node, &table->schema->columns) {
432 const struct ovsdb_column *column = node->data;
433 const struct ovsdb_type *type = &column->type;
434 unsigned int idx = column->index;
436 if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
437 &txn_row->new->fields[idx],
439 bitmap_set1(txn_row->changed, idx);
445 /* Nothing actually changed in this row, so drop it. */
446 ovsdb_txn_row_abort(txn, txn_row);
449 bitmap_set_multiple(txn_row->changed, 0,
450 shash_count(&table->schema->columns), 1);
456 static struct ovsdb_error * WARN_UNUSED_RESULT
457 check_max_rows(struct ovsdb_txn *txn)
459 struct ovsdb_txn_table *t;
461 LIST_FOR_EACH (t, node, &txn->txn_tables) {
462 size_t n_rows = hmap_count(&t->table->rows);
463 unsigned int max_rows = t->table->schema->max_rows;
465 if (n_rows > max_rows) {
466 return ovsdb_error("constraint violation",
467 "transaction causes \"%s\" table to contain "
468 "%zu rows, greater than the schema-defined "
469 "limit of %u row(s)",
470 t->table->schema->name, n_rows, max_rows);
478 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
480 struct ovsdb_replica *replica;
481 struct ovsdb_error *error;
483 /* Figure out what actually changed, and abort early if the transaction
484 * was really a no-op. */
485 error = for_each_txn_row(txn, determine_changes);
487 return OVSDB_WRAP_BUG("can't happen", error);
489 if (list_is_empty(&txn->txn_tables)) {
490 ovsdb_txn_abort(txn);
494 /* Check maximum rows table constraints. */
495 error = check_max_rows(txn);
497 ovsdb_txn_abort(txn);
501 /* Update reference counts and check referential integrity. */
502 error = update_ref_counts(txn);
504 ovsdb_txn_abort(txn);
508 /* Check reference counts and remove bad reference for "weak" referential
510 error = for_each_txn_row(txn, assess_weak_refs);
512 ovsdb_txn_abort(txn);
516 /* Send the commit to each replica. */
517 LIST_FOR_EACH (replica, node, &txn->db->replicas) {
518 error = (replica->class->commit)(replica, txn, durable);
520 /* We don't support two-phase commit so only the first replica is
521 * allowed to report an error. */
522 assert(&replica->node == txn->db->replicas.next);
524 ovsdb_txn_abort(txn);
529 /* Finalize commit. */
530 txn->db->run_triggers = true;
531 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
538 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
539 ovsdb_txn_row_cb_func *cb, void *aux)
541 struct ovsdb_txn_table *t;
542 struct ovsdb_txn_row *r;
544 LIST_FOR_EACH (t, node, &txn->txn_tables) {
545 HMAP_FOR_EACH (r, hmap_node, &t->txn_rows) {
546 if ((r->old || r->new) && !cb(r->old, r->new, r->changed, aux)) {
553 static struct ovsdb_txn_table *
554 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
556 if (!table->txn_table) {
557 struct ovsdb_txn_table *txn_table;
559 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
560 txn_table->table = table;
561 hmap_init(&txn_table->txn_rows);
562 txn_table->serial = serial - 1;
563 list_push_back(&txn->txn_tables, &txn_table->node);
565 return table->txn_table;
568 static struct ovsdb_txn_row *
569 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
570 const struct ovsdb_row *old_, struct ovsdb_row *new)
572 const struct ovsdb_row *row = old_ ? old_ : new;
573 struct ovsdb_row *old = (struct ovsdb_row *) old_;
574 size_t n_columns = shash_count(&table->schema->columns);
575 struct ovsdb_txn_table *txn_table;
576 struct ovsdb_txn_row *txn_row;
578 txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
579 + bitmap_n_bytes(n_columns));
580 txn_row->uuid = *ovsdb_row_get_uuid(row);
581 txn_row->table = row->table;
584 txn_row->n_refs = old ? old->n_refs : 0;
585 txn_row->serial = serial - 1;
588 old->txn_row = txn_row;
591 new->txn_row = txn_row;
594 txn_table = ovsdb_txn_create_txn_table(txn, table);
595 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
596 ovsdb_row_hash(old ? old : new));
602 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
604 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
606 if (ro_row->txn_row) {
607 assert(ro_row == ro_row->txn_row->new);
610 struct ovsdb_table *table = ro_row->table;
611 struct ovsdb_row *rw_row;
613 rw_row = ovsdb_row_clone(ro_row);
614 rw_row->n_refs = ro_row->n_refs;
615 uuid_generate(ovsdb_row_get_version_rw(rw_row));
616 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
617 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
624 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
626 uint32_t hash = ovsdb_row_hash(row);
627 struct ovsdb_table *table = row->table;
629 uuid_generate(ovsdb_row_get_version_rw(row));
631 ovsdb_txn_row_create(txn, table, NULL, row);
632 hmap_insert(&table->rows, &row->hmap_node, hash);
635 /* 'row' must be assumed destroyed upon return; the caller must not reference
638 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
640 struct ovsdb_row *row = (struct ovsdb_row *) row_;
641 struct ovsdb_table *table = row->table;
642 struct ovsdb_txn_row *txn_row = row->txn_row;
644 hmap_remove(&table->rows, &row->hmap_node);
647 ovsdb_txn_row_create(txn, table, row, NULL);
649 assert(txn_row->new == row);
653 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
656 ovsdb_row_destroy(row);
661 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
663 if (txn->comment.length) {
664 ds_put_char(&txn->comment, '\n');
666 ds_put_cstr(&txn->comment, s);
670 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
672 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
676 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
678 struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
680 txn_table->n_processed--;
681 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
684 txn_row->old->txn_row = NULL;
687 txn_row->new->txn_row = NULL;
692 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
694 assert(hmap_is_empty(&txn_table->txn_rows));
695 txn_table->table->txn_table = NULL;
696 hmap_destroy(&txn_table->txn_rows);
697 list_remove(&txn_table->node);
701 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
702 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
703 * returns a null pointer after iteration is complete.
705 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
706 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
707 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
708 * in within the same txn_table. It may *not* delete any txn_tables. As long
709 * as these rules are followed, 'cb' will be called exactly once for each
710 * txn_row in 'txn', even those added by 'cb'.
712 * (Even though 'cb' is not allowed to delete some txn_rows, it can still
713 * delete any actual row by clearing a txn_row's 'new' member.)
715 static struct ovsdb_error * WARN_UNUSED_RESULT
716 for_each_txn_row(struct ovsdb_txn *txn,
717 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
718 struct ovsdb_txn_row *))
725 struct ovsdb_txn_table *t, *next_txn_table;
728 LIST_FOR_EACH_SAFE (t, next_txn_table, node, &txn->txn_tables) {
729 if (t->serial != serial) {
734 while (t->n_processed < hmap_count(&t->txn_rows)) {
735 struct ovsdb_txn_row *r, *next_txn_row;
737 HMAP_FOR_EACH_SAFE (r, next_txn_row, hmap_node, &t->txn_rows) {
738 if (r->serial != serial) {
739 struct ovsdb_error *error;
752 if (hmap_is_empty(&t->txn_rows)) {
753 /* Table is empty. Drop it. */
754 ovsdb_txn_table_destroy(t);