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 "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 is invalid. It would indicate that a row
61 * was added then deleted within a single transaction, but we instead
62 * handle that case by deleting the txn_row entirely.
64 struct ovsdb_txn_row {
65 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
66 struct ovsdb_row *old; /* The old row. */
67 struct ovsdb_row *new; /* The new row. */
68 size_t n_refs; /* Number of remaining references. */
70 /* Used by for_each_txn_row(). */
71 unsigned int serial; /* Serial number of in-progress commit. */
73 unsigned long changed[]; /* Bits set to 1 for columns that changed. */
76 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
77 static struct ovsdb_error * WARN_UNUSED_RESULT
78 for_each_txn_row(struct ovsdb_txn *txn,
79 struct ovsdb_error *(*)(struct ovsdb_txn *,
80 struct ovsdb_txn_row *));
82 /* Used by for_each_txn_row() to track tables and rows that have been
84 static unsigned int serial;
87 ovsdb_txn_create(struct ovsdb *db)
89 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
91 list_init(&txn->txn_tables);
92 ds_init(&txn->comment);
97 ovsdb_txn_free(struct ovsdb_txn *txn)
99 assert(list_is_empty(&txn->txn_tables));
100 ds_destroy(&txn->comment);
104 static struct ovsdb_error *
105 ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
106 struct ovsdb_txn_row *txn_row)
108 struct ovsdb_row *old = txn_row->old;
109 struct ovsdb_row *new = txn_row->new;
111 ovsdb_txn_row_prefree(txn_row);
113 hmap_remove(&new->table->rows, &new->hmap_node);
115 hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
117 hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
119 ovsdb_row_destroy(new);
126 ovsdb_txn_abort(struct ovsdb_txn *txn)
128 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
132 static struct ovsdb_txn_row *
133 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
135 struct ovsdb_txn_row *txn_row;
137 if (!table->txn_table) {
141 HMAP_FOR_EACH_WITH_HASH (txn_row, hmap_node,
142 uuid_hash(uuid), &table->txn_table->txn_rows) {
143 const struct ovsdb_row *row;
145 row = txn_row->old ? txn_row->old : txn_row->new;
146 if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
154 static struct ovsdb_error * WARN_UNUSED_RESULT
155 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
156 const struct ovsdb_column *c,
157 const struct ovsdb_base_type *base,
158 const union ovsdb_atom *atoms, unsigned int n,
161 const struct ovsdb_table *table;
164 if (!ovsdb_base_type_is_strong_ref(base)) {
168 table = base->u.uuid.refTable;
169 for (i = 0; i < n; i++) {
170 const struct uuid *uuid = &atoms[i].uuid;
171 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
173 const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
175 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
177 return ovsdb_error("referential integrity violation",
178 "Table %s column %s row "UUID_FMT" "
179 "references nonexistent row "UUID_FMT" in "
181 r->table->schema->name, c->name,
182 UUID_ARGS(ovsdb_row_get_uuid(r)),
183 UUID_ARGS(uuid), table->schema->name);
186 txn_row->n_refs += delta;
192 static struct ovsdb_error * WARN_UNUSED_RESULT
193 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
194 const struct ovsdb_column *column, int delta)
196 const struct ovsdb_datum *field = &r->fields[column->index];
197 struct ovsdb_error *error;
199 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.key,
200 field->keys, field->n, delta);
202 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.value,
203 field->values, field->n, delta);
208 static struct ovsdb_error * WARN_UNUSED_RESULT
209 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
211 struct ovsdb_table *table = r->old ? r->old->table : r->new->table;
212 struct shash_node *node;
214 SHASH_FOR_EACH (node, &table->schema->columns) {
215 const struct ovsdb_column *column = node->data;
216 struct ovsdb_error *error;
219 error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
221 ovsdb_error_destroy(error);
222 return OVSDB_BUG("error decreasing refcount");
226 error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
236 static struct ovsdb_error * WARN_UNUSED_RESULT
237 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
239 if (r->new || !r->n_refs) {
242 return ovsdb_error("referential integrity violation",
243 "cannot delete %s row "UUID_FMT" because "
244 "of %zu remaining reference(s)",
245 r->old->table->schema->name,
246 UUID_ARGS(ovsdb_row_get_uuid(r->old)),
251 static struct ovsdb_error * WARN_UNUSED_RESULT
252 update_ref_counts(struct ovsdb_txn *txn)
254 struct ovsdb_error *error;
256 error = for_each_txn_row(txn, update_row_ref_count);
261 return for_each_txn_row(txn, check_ref_count);
264 static struct ovsdb_error *
265 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
266 struct ovsdb_txn_row *txn_row)
268 ovsdb_txn_row_prefree(txn_row);
270 txn_row->new->n_refs = txn_row->n_refs;
272 ovsdb_row_destroy(txn_row->old);
279 add_weak_ref(struct ovsdb_txn *txn,
280 const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
282 struct ovsdb_row *src = (struct ovsdb_row *) src_;
283 struct ovsdb_row *dst = (struct ovsdb_row *) dst_;
284 struct ovsdb_weak_ref *weak;
290 dst = ovsdb_txn_row_modify(txn, dst);
292 if (!list_is_empty(&dst->dst_refs)) {
293 /* Omit duplicates. */
294 weak = CONTAINER_OF(list_back(&dst->dst_refs),
295 struct ovsdb_weak_ref, dst_node);
296 if (weak->src == src) {
301 weak = xmalloc(sizeof *weak);
303 list_push_back(&dst->dst_refs, &weak->dst_node);
304 list_push_back(&src->src_refs, &weak->src_node);
307 static struct ovsdb_error * WARN_UNUSED_RESULT
308 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
310 struct ovsdb_table *table;
311 struct shash_node *node;
314 /* Mark rows that have weak references to 'txn_row' as modified, so
315 * that their weak references will get reassessed. */
316 struct ovsdb_weak_ref *weak, *next;
318 LIST_FOR_EACH_SAFE (weak, next, dst_node, &txn_row->old->dst_refs) {
319 if (!weak->src->txn_row) {
320 ovsdb_txn_row_modify(txn, weak->src);
326 /* We don't have to do anything about references that originate at
327 * 'txn_row', because ovsdb_row_destroy() will remove those weak
332 table = txn_row->new->table;
333 SHASH_FOR_EACH (node, &table->schema->columns) {
334 const struct ovsdb_column *column = node->data;
335 struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
336 unsigned int orig_n, i;
341 if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
342 for (i = 0; i < datum->n; ) {
343 const struct ovsdb_row *row;
345 row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
346 &datum->keys[i].uuid);
348 add_weak_ref(txn, txn_row->new, row);
351 if (uuid_is_zero(&datum->keys[i].uuid)) {
354 ovsdb_datum_remove_unsafe(datum, i, &column->type);
359 if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
360 for (i = 0; i < datum->n; ) {
361 const struct ovsdb_row *row;
363 row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
364 &datum->values[i].uuid);
366 add_weak_ref(txn, txn_row->new, row);
369 if (uuid_is_zero(&datum->values[i].uuid)) {
372 ovsdb_datum_remove_unsafe(datum, i, &column->type);
377 if (datum->n != orig_n) {
378 bitmap_set1(txn_row->changed, column->index);
379 ovsdb_datum_sort_assert(datum, column->type.key.type);
380 if (datum->n < column->type.n_min) {
381 const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
382 if (zero && !txn_row->old) {
384 "constraint violation",
385 "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
386 " (inserted within this transaction) contained "
387 "all-zeros UUID (probably as the default value for "
388 "this column) but deleting this value caused a "
389 "constraint volation because this column is not "
390 "allowed to be empty.", column->name,
391 table->schema->name, UUID_ARGS(row_uuid));
394 "constraint violation",
395 "Deletion of %u weak reference(s) to deleted (or "
396 "never-existing) rows from column \"%s\" in \"%s\" "
397 "row "UUID_FMT" %scaused this column to become empty, "
398 "but constraints on this column disallow an "
400 orig_n - datum->n, column->name, table->schema->name,
404 : "(inserted within this transaction) "));
413 static struct ovsdb_error * WARN_UNUSED_RESULT
414 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
416 struct ovsdb_table *table;
418 table = (txn_row->old ? txn_row->old : txn_row->new)->table;
419 if (txn_row->old && txn_row->new) {
420 struct shash_node *node;
421 bool changed = false;
423 SHASH_FOR_EACH (node, &table->schema->columns) {
424 const struct ovsdb_column *column = node->data;
425 const struct ovsdb_type *type = &column->type;
426 unsigned int idx = column->index;
428 if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
429 &txn_row->new->fields[idx],
431 bitmap_set1(txn_row->changed, idx);
437 /* Nothing actually changed in this row, so drop it. */
438 ovsdb_txn_row_abort(txn, txn_row);
441 bitmap_set_multiple(txn_row->changed, 0,
442 shash_count(&table->schema->columns), 1);
448 static struct ovsdb_error * WARN_UNUSED_RESULT
449 check_max_rows(struct ovsdb_txn *txn)
451 struct ovsdb_txn_table *t;
453 LIST_FOR_EACH (t, node, &txn->txn_tables) {
454 size_t n_rows = hmap_count(&t->table->rows);
455 unsigned int max_rows = t->table->schema->max_rows;
457 if (n_rows > max_rows) {
458 return ovsdb_error("constraint violation",
459 "transaction causes \"%s\" table to contain "
460 "%zu rows, greater than the schema-defined "
461 "limit of %u row(s)",
462 t->table->schema->name, n_rows, max_rows);
470 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
472 struct ovsdb_replica *replica;
473 struct ovsdb_error *error;
475 /* Figure out what actually changed, and abort early if the transaction
476 * was really a no-op. */
477 error = for_each_txn_row(txn, determine_changes);
479 ovsdb_error_destroy(error);
480 return OVSDB_BUG("can't happen");
482 if (list_is_empty(&txn->txn_tables)) {
483 ovsdb_txn_abort(txn);
487 /* Check maximum rows table constraints. */
488 error = check_max_rows(txn);
490 ovsdb_txn_abort(txn);
494 /* Update reference counts and check referential integrity. */
495 error = update_ref_counts(txn);
497 ovsdb_txn_abort(txn);
501 /* Check reference counts and remove bad reference for "weak" referential
503 error = for_each_txn_row(txn, assess_weak_refs);
505 ovsdb_txn_abort(txn);
509 /* Send the commit to each replica. */
510 LIST_FOR_EACH (replica, node, &txn->db->replicas) {
511 error = (replica->class->commit)(replica, txn, durable);
513 /* We don't support two-phase commit so only the first replica is
514 * allowed to report an error. */
515 assert(&replica->node == txn->db->replicas.next);
517 ovsdb_txn_abort(txn);
522 /* Finalize commit. */
523 txn->db->run_triggers = true;
524 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
531 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
532 ovsdb_txn_row_cb_func *cb, void *aux)
534 struct ovsdb_txn_table *t;
535 struct ovsdb_txn_row *r;
537 LIST_FOR_EACH (t, node, &txn->txn_tables) {
538 HMAP_FOR_EACH (r, hmap_node, &t->txn_rows) {
539 if (!cb(r->old, r->new, r->changed, aux)) {
546 static struct ovsdb_txn_table *
547 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
549 if (!table->txn_table) {
550 struct ovsdb_txn_table *txn_table;
552 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
553 txn_table->table = table;
554 hmap_init(&txn_table->txn_rows);
555 txn_table->serial = serial - 1;
556 list_push_back(&txn->txn_tables, &txn_table->node);
558 return table->txn_table;
561 static struct ovsdb_txn_row *
562 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
563 const struct ovsdb_row *old_, struct ovsdb_row *new)
565 struct ovsdb_row *old = (struct ovsdb_row *) old_;
566 size_t n_columns = shash_count(&table->schema->columns);
567 struct ovsdb_txn_table *txn_table;
568 struct ovsdb_txn_row *txn_row;
570 txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
571 + bitmap_n_bytes(n_columns));
572 txn_row->old = (struct ovsdb_row *) old;
574 txn_row->n_refs = old ? old->n_refs : 0;
575 txn_row->serial = serial - 1;
578 old->txn_row = txn_row;
581 new->txn_row = txn_row;
584 txn_table = ovsdb_txn_create_txn_table(txn, table);
585 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
586 ovsdb_row_hash(old ? old : new));
592 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
594 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
596 if (ro_row->txn_row) {
597 assert(ro_row == ro_row->txn_row->new);
600 struct ovsdb_table *table = ro_row->table;
601 struct ovsdb_row *rw_row;
603 rw_row = ovsdb_row_clone(ro_row);
604 rw_row->n_refs = ro_row->n_refs;
605 uuid_generate(ovsdb_row_get_version_rw(rw_row));
606 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
607 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
614 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
616 uint32_t hash = ovsdb_row_hash(row);
617 struct ovsdb_table *table = row->table;
619 uuid_generate(ovsdb_row_get_version_rw(row));
621 ovsdb_txn_row_create(txn, table, NULL, row);
622 hmap_insert(&table->rows, &row->hmap_node, hash);
625 /* 'row' must be assumed destroyed upon return; the caller must not reference
628 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
630 struct ovsdb_row *row = (struct ovsdb_row *) row_;
631 struct ovsdb_table *table = row->table;
632 struct ovsdb_txn_row *txn_row = row->txn_row;
634 hmap_remove(&table->rows, &row->hmap_node);
637 ovsdb_txn_row_create(txn, table, row, NULL);
639 assert(txn_row->new == row);
643 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
646 ovsdb_row_destroy(row);
651 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
653 if (txn->comment.length) {
654 ds_put_char(&txn->comment, '\n');
656 ds_put_cstr(&txn->comment, s);
660 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
662 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
666 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
668 struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
669 struct ovsdb_txn_table *txn_table = row->table->txn_table;
671 txn_table->n_processed--;
672 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
675 txn_row->old->txn_row = NULL;
678 txn_row->new->txn_row = NULL;
683 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
685 assert(hmap_is_empty(&txn_table->txn_rows));
686 txn_table->table->txn_table = NULL;
687 hmap_destroy(&txn_table->txn_rows);
688 list_remove(&txn_table->node);
692 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
693 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
694 * returns a null pointer after iteration is complete.
696 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
697 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
698 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
699 * in within the same txn_table. It may *not* delete any txn_tables. As long
700 * as these rules are followed, 'cb' will be called exactly once for each
701 * txn_row in 'txn', even those added by 'cb'.
703 static struct ovsdb_error * WARN_UNUSED_RESULT
704 for_each_txn_row(struct ovsdb_txn *txn,
705 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
706 struct ovsdb_txn_row *))
713 struct ovsdb_txn_table *t, *next_txn_table;
716 LIST_FOR_EACH_SAFE (t, next_txn_table, node, &txn->txn_tables) {
717 if (t->serial != serial) {
722 while (t->n_processed < hmap_count(&t->txn_rows)) {
723 struct ovsdb_txn_row *r, *next_txn_row;
725 HMAP_FOR_EACH_SAFE (r, next_txn_row, hmap_node, &t->txn_rows) {
726 if (r->serial != serial) {
727 struct ovsdb_error *error;
740 if (hmap_is_empty(&t->txn_rows)) {
741 /* Table is empty. Drop it. */
742 ovsdb_txn_table_destroy(t);