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, struct ovsdb_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,
156 const struct ovsdb_base_type *base,
157 const union ovsdb_atom *atoms, unsigned int n,
160 const struct ovsdb_table *table;
163 if (!ovsdb_base_type_is_strong_ref(base)) {
167 table = base->u.uuid.refTable;
168 for (i = 0; i < n; i++) {
169 const struct uuid *uuid = &atoms[i].uuid;
170 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
172 const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
174 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
176 return ovsdb_error("referential integrity violation",
177 "reference to nonexistent row "
178 UUID_FMT, UUID_ARGS(uuid));
181 txn_row->n_refs += delta;
187 static struct ovsdb_error * WARN_UNUSED_RESULT
188 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
189 const struct ovsdb_column *column, int delta)
191 const struct ovsdb_datum *field = &r->fields[column->index];
192 struct ovsdb_error *error;
194 error = ovsdb_txn_adjust_atom_refs(txn, &column->type.key,
195 field->keys, field->n, delta);
197 error = ovsdb_txn_adjust_atom_refs(txn, &column->type.value,
198 field->values, field->n, delta);
203 static struct ovsdb_error * WARN_UNUSED_RESULT
204 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
206 struct ovsdb_table *table = r->old ? r->old->table : r->new->table;
207 struct shash_node *node;
209 SHASH_FOR_EACH (node, &table->schema->columns) {
210 const struct ovsdb_column *column = node->data;
211 struct ovsdb_error *error;
214 error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
216 ovsdb_error_destroy(error);
217 return OVSDB_BUG("error decreasing refcount");
221 error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
231 static struct ovsdb_error * WARN_UNUSED_RESULT
232 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
234 if (r->new || !r->n_refs) {
237 return ovsdb_error("referential integrity violation",
238 "cannot delete %s row "UUID_FMT" because "
239 "of %zu remaining reference(s)",
240 r->old->table->schema->name,
241 UUID_ARGS(ovsdb_row_get_uuid(r->old)),
246 static struct ovsdb_error * WARN_UNUSED_RESULT
247 update_ref_counts(struct ovsdb_txn *txn)
249 struct ovsdb_error *error;
251 error = for_each_txn_row(txn, update_row_ref_count);
256 return for_each_txn_row(txn, check_ref_count);
259 static struct ovsdb_error *
260 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
261 struct ovsdb_txn_row *txn_row)
263 ovsdb_txn_row_prefree(txn_row);
265 txn_row->new->n_refs = txn_row->n_refs;
267 ovsdb_row_destroy(txn_row->old);
274 add_weak_ref(struct ovsdb_txn *txn,
275 const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
277 struct ovsdb_row *src = (struct ovsdb_row *) src_;
278 struct ovsdb_row *dst = (struct ovsdb_row *) dst_;
279 struct ovsdb_weak_ref *weak;
285 dst = ovsdb_txn_row_modify(txn, dst);
287 if (!list_is_empty(&dst->dst_refs)) {
288 /* Omit duplicates. */
289 weak = CONTAINER_OF(list_back(&dst->dst_refs),
290 struct ovsdb_weak_ref, dst_node);
291 if (weak->src == src) {
296 weak = xmalloc(sizeof *weak);
298 list_push_back(&dst->dst_refs, &weak->dst_node);
299 list_push_back(&src->src_refs, &weak->src_node);
302 static struct ovsdb_error * WARN_UNUSED_RESULT
303 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
305 struct ovsdb_table *table;
306 struct shash_node *node;
309 /* Mark rows that have weak references to 'txn_row' as modified, so
310 * that their weak references will get reassessed. */
311 struct ovsdb_weak_ref *weak, *next;
313 LIST_FOR_EACH_SAFE (weak, next, struct ovsdb_weak_ref, dst_node,
314 &txn_row->old->dst_refs) {
315 if (!weak->src->txn_row) {
316 ovsdb_txn_row_modify(txn, weak->src);
322 /* We don't have to do anything about references that originate at
323 * 'txn_row', because ovsdb_row_destroy() will remove those weak
328 table = txn_row->new->table;
329 SHASH_FOR_EACH (node, &table->schema->columns) {
330 const struct ovsdb_column *column = node->data;
331 struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
332 unsigned int orig_n, i;
337 if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
338 for (i = 0; i < datum->n; ) {
339 const struct ovsdb_row *row;
341 row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
342 &datum->keys[i].uuid);
344 add_weak_ref(txn, txn_row->new, row);
347 if (uuid_is_zero(&datum->keys[i].uuid)) {
350 ovsdb_datum_remove_unsafe(datum, i, &column->type);
355 if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
356 for (i = 0; i < datum->n; ) {
357 const struct ovsdb_row *row;
359 row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
360 &datum->values[i].uuid);
362 add_weak_ref(txn, txn_row->new, row);
365 if (uuid_is_zero(&datum->values[i].uuid)) {
368 ovsdb_datum_remove_unsafe(datum, i, &column->type);
373 if (datum->n != orig_n) {
374 bitmap_set1(txn_row->changed, column->index);
375 ovsdb_datum_sort_assert(datum, column->type.key.type);
376 if (datum->n < column->type.n_min) {
377 const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
378 if (zero && !txn_row->old) {
380 "constraint violation",
381 "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
382 " (inserted within this transaction) contained "
383 "all-zeros UUID (probably as the default value for "
384 "this column) but deleting this value caused a "
385 "constraint volation because this column is not "
386 "allowed to be empty.", column->name,
387 table->schema->name, UUID_ARGS(row_uuid));
390 "constraint violation",
391 "Deletion of %u weak reference(s) to deleted (or "
392 "never-existing) rows from column \"%s\" in \"%s\" "
393 "row "UUID_FMT" %scaused this column to become empty, "
394 "but constraints on this column disallow an "
396 orig_n - datum->n, column->name, table->schema->name,
400 : "(inserted within this transaction) "));
409 static struct ovsdb_error * WARN_UNUSED_RESULT
410 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
412 struct ovsdb_table *table;
414 table = (txn_row->old ? txn_row->old : txn_row->new)->table;
415 if (txn_row->old && txn_row->new) {
416 struct shash_node *node;
417 bool changed = false;
419 SHASH_FOR_EACH (node, &table->schema->columns) {
420 const struct ovsdb_column *column = node->data;
421 const struct ovsdb_type *type = &column->type;
422 unsigned int idx = column->index;
424 if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
425 &txn_row->new->fields[idx],
427 bitmap_set1(txn_row->changed, idx);
433 /* Nothing actually changed in this row, so drop it. */
434 ovsdb_txn_row_abort(txn, txn_row);
437 bitmap_set_multiple(txn_row->changed, 0,
438 shash_count(&table->schema->columns), 1);
444 static struct ovsdb_error * WARN_UNUSED_RESULT
445 check_max_rows(struct ovsdb_txn *txn)
447 struct ovsdb_txn_table *t;
449 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
450 size_t n_rows = hmap_count(&t->table->rows);
451 unsigned int max_rows = t->table->schema->max_rows;
453 if (n_rows > max_rows) {
454 return ovsdb_error("constraint violation",
455 "transaction causes \"%s\" table to contain "
456 "%zu rows, greater than the schema-defined "
457 "limit of %u row(s)",
458 t->table->schema->name, n_rows, max_rows);
466 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
468 struct ovsdb_replica *replica;
469 struct ovsdb_error *error;
471 /* Figure out what actually changed, and abort early if the transaction
472 * was really a no-op. */
473 error = for_each_txn_row(txn, determine_changes);
475 ovsdb_error_destroy(error);
476 return OVSDB_BUG("can't happen");
478 if (list_is_empty(&txn->txn_tables)) {
479 ovsdb_txn_abort(txn);
483 /* Check maximum rows table constraints. */
484 error = check_max_rows(txn);
486 ovsdb_txn_abort(txn);
490 /* Update reference counts and check referential integrity. */
491 error = update_ref_counts(txn);
493 ovsdb_txn_abort(txn);
497 /* Check reference counts and remove bad reference for "weak" referential
499 error = for_each_txn_row(txn, assess_weak_refs);
501 ovsdb_txn_abort(txn);
505 /* Send the commit to each replica. */
506 LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
507 error = (replica->class->commit)(replica, txn, durable);
509 /* We don't support two-phase commit so only the first replica is
510 * allowed to report an error. */
511 assert(&replica->node == txn->db->replicas.next);
513 ovsdb_txn_abort(txn);
518 /* Finalize commit. */
519 txn->db->run_triggers = true;
520 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
527 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
528 ovsdb_txn_row_cb_func *cb, void *aux)
530 struct ovsdb_txn_table *t;
531 struct ovsdb_txn_row *r;
533 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
534 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
535 if (!cb(r->old, r->new, r->changed, aux)) {
542 static struct ovsdb_txn_table *
543 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
545 if (!table->txn_table) {
546 struct ovsdb_txn_table *txn_table;
548 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
549 txn_table->table = table;
550 hmap_init(&txn_table->txn_rows);
551 txn_table->serial = serial - 1;
552 list_push_back(&txn->txn_tables, &txn_table->node);
554 return table->txn_table;
557 static struct ovsdb_txn_row *
558 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
559 const struct ovsdb_row *old_, struct ovsdb_row *new)
561 struct ovsdb_row *old = (struct ovsdb_row *) old_;
562 size_t n_columns = shash_count(&table->schema->columns);
563 struct ovsdb_txn_table *txn_table;
564 struct ovsdb_txn_row *txn_row;
566 txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
567 + bitmap_n_bytes(n_columns));
568 txn_row->old = (struct ovsdb_row *) old;
570 txn_row->n_refs = old ? old->n_refs : 0;
571 txn_row->serial = serial - 1;
574 old->txn_row = txn_row;
577 new->txn_row = txn_row;
580 txn_table = ovsdb_txn_create_txn_table(txn, table);
581 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
582 ovsdb_row_hash(old ? old : new));
588 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
590 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
592 if (ro_row->txn_row) {
593 assert(ro_row == ro_row->txn_row->new);
596 struct ovsdb_table *table = ro_row->table;
597 struct ovsdb_row *rw_row;
599 rw_row = ovsdb_row_clone(ro_row);
600 rw_row->n_refs = ro_row->n_refs;
601 uuid_generate(ovsdb_row_get_version_rw(rw_row));
602 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
603 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
610 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
612 uint32_t hash = ovsdb_row_hash(row);
613 struct ovsdb_table *table = row->table;
615 uuid_generate(ovsdb_row_get_version_rw(row));
617 ovsdb_txn_row_create(txn, table, NULL, row);
618 hmap_insert(&table->rows, &row->hmap_node, hash);
621 /* 'row' must be assumed destroyed upon return; the caller must not reference
624 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
626 struct ovsdb_row *row = (struct ovsdb_row *) row_;
627 struct ovsdb_table *table = row->table;
628 struct ovsdb_txn_row *txn_row = row->txn_row;
630 hmap_remove(&table->rows, &row->hmap_node);
633 ovsdb_txn_row_create(txn, table, row, NULL);
635 assert(txn_row->new == row);
639 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
642 ovsdb_row_destroy(row);
647 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
649 if (txn->comment.length) {
650 ds_put_char(&txn->comment, '\n');
652 ds_put_cstr(&txn->comment, s);
656 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
658 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
662 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
664 struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
665 struct ovsdb_txn_table *txn_table = row->table->txn_table;
667 txn_table->n_processed--;
668 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
671 txn_row->old->txn_row = NULL;
674 txn_row->new->txn_row = NULL;
679 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
681 assert(hmap_is_empty(&txn_table->txn_rows));
682 txn_table->table->txn_table = NULL;
683 hmap_destroy(&txn_table->txn_rows);
684 list_remove(&txn_table->node);
688 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
689 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
690 * returns a null pointer after iteration is complete.
692 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
693 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
694 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
695 * in within the same txn_table. It may *not* delete any txn_tables. As long
696 * as these rules are followed, 'cb' will be called exactly once for each
697 * txn_row in 'txn', even those added by 'cb'.
699 static struct ovsdb_error * WARN_UNUSED_RESULT
700 for_each_txn_row(struct ovsdb_txn *txn,
701 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
702 struct ovsdb_txn_row *))
709 struct ovsdb_txn_table *t, *next_txn_table;
712 LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
714 if (t->serial != serial) {
719 while (t->n_processed < hmap_count(&t->txn_rows)) {
720 struct ovsdb_txn_row *r, *next_txn_row;
722 HMAP_FOR_EACH_SAFE (r, next_txn_row,
723 struct ovsdb_txn_row, hmap_node,
725 if (r->serial != serial) {
726 struct ovsdb_error *error;
739 if (hmap_is_empty(&t->txn_rows)) {
740 /* Table is empty. Drop it. */
741 ovsdb_txn_table_destroy(t);