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);
180 const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
182 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
184 return ovsdb_error("referential integrity violation",
185 "Table %s column %s row "UUID_FMT" "
186 "references nonexistent row "UUID_FMT" in "
188 r->table->schema->name, c->name,
189 UUID_ARGS(ovsdb_row_get_uuid(r)),
190 UUID_ARGS(uuid), table->schema->name);
193 txn_row->n_refs += delta;
199 static struct ovsdb_error * WARN_UNUSED_RESULT
200 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
201 const struct ovsdb_column *column, int delta)
203 const struct ovsdb_datum *field = &r->fields[column->index];
204 struct ovsdb_error *error;
206 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.key,
207 field->keys, field->n, delta);
209 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.value,
210 field->values, field->n, delta);
215 static struct ovsdb_error * WARN_UNUSED_RESULT
216 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
218 struct ovsdb_table *table = r->table;
219 struct shash_node *node;
221 SHASH_FOR_EACH (node, &table->schema->columns) {
222 const struct ovsdb_column *column = node->data;
223 struct ovsdb_error *error;
226 error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
228 return OVSDB_WRAP_BUG("error decreasing refcount", error);
232 error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
242 static struct ovsdb_error * WARN_UNUSED_RESULT
243 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
245 if (r->new || !r->n_refs) {
248 return ovsdb_error("referential integrity violation",
249 "cannot delete %s row "UUID_FMT" because "
250 "of %zu remaining reference(s)",
251 r->table->schema->name, UUID_ARGS(&r->uuid),
256 static struct ovsdb_error * WARN_UNUSED_RESULT
257 update_ref_counts(struct ovsdb_txn *txn)
259 struct ovsdb_error *error;
261 error = for_each_txn_row(txn, update_row_ref_count);
266 return for_each_txn_row(txn, check_ref_count);
269 static struct ovsdb_error *
270 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
271 struct ovsdb_txn_row *txn_row)
273 ovsdb_txn_row_prefree(txn_row);
275 txn_row->new->n_refs = txn_row->n_refs;
277 ovsdb_row_destroy(txn_row->old);
284 add_weak_ref(struct ovsdb_txn *txn,
285 const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
287 struct ovsdb_row *src = (struct ovsdb_row *) src_;
288 struct ovsdb_row *dst = (struct ovsdb_row *) dst_;
289 struct ovsdb_weak_ref *weak;
295 dst = ovsdb_txn_row_modify(txn, dst);
297 if (!list_is_empty(&dst->dst_refs)) {
298 /* Omit duplicates. */
299 weak = CONTAINER_OF(list_back(&dst->dst_refs),
300 struct ovsdb_weak_ref, dst_node);
301 if (weak->src == src) {
306 weak = xmalloc(sizeof *weak);
308 list_push_back(&dst->dst_refs, &weak->dst_node);
309 list_push_back(&src->src_refs, &weak->src_node);
312 static struct ovsdb_error * WARN_UNUSED_RESULT
313 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
315 struct ovsdb_table *table;
316 struct shash_node *node;
319 /* Mark rows that have weak references to 'txn_row' as modified, so
320 * that their weak references will get reassessed. */
321 struct ovsdb_weak_ref *weak, *next;
323 LIST_FOR_EACH_SAFE (weak, next, dst_node, &txn_row->old->dst_refs) {
324 if (!weak->src->txn_row) {
325 ovsdb_txn_row_modify(txn, weak->src);
331 /* We don't have to do anything about references that originate at
332 * 'txn_row', because ovsdb_row_destroy() will remove those weak
337 table = txn_row->table;
338 SHASH_FOR_EACH (node, &table->schema->columns) {
339 const struct ovsdb_column *column = node->data;
340 struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
341 unsigned int orig_n, i;
346 if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
347 for (i = 0; i < datum->n; ) {
348 const struct ovsdb_row *row;
350 row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
351 &datum->keys[i].uuid);
353 add_weak_ref(txn, txn_row->new, row);
356 if (uuid_is_zero(&datum->keys[i].uuid)) {
359 ovsdb_datum_remove_unsafe(datum, i, &column->type);
364 if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
365 for (i = 0; i < datum->n; ) {
366 const struct ovsdb_row *row;
368 row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
369 &datum->values[i].uuid);
371 add_weak_ref(txn, txn_row->new, row);
374 if (uuid_is_zero(&datum->values[i].uuid)) {
377 ovsdb_datum_remove_unsafe(datum, i, &column->type);
382 if (datum->n != orig_n) {
383 bitmap_set1(txn_row->changed, column->index);
384 ovsdb_datum_sort_assert(datum, column->type.key.type);
385 if (datum->n < column->type.n_min) {
386 const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
387 if (zero && !txn_row->old) {
389 "constraint violation",
390 "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
391 " (inserted within this transaction) contained "
392 "all-zeros UUID (probably as the default value for "
393 "this column) but deleting this value caused a "
394 "constraint volation because this column is not "
395 "allowed to be empty.", column->name,
396 table->schema->name, UUID_ARGS(row_uuid));
399 "constraint violation",
400 "Deletion of %u weak reference(s) to deleted (or "
401 "never-existing) rows from column \"%s\" in \"%s\" "
402 "row "UUID_FMT" %scaused this column to become empty, "
403 "but constraints on this column disallow an "
405 orig_n - datum->n, column->name, table->schema->name,
409 : "(inserted within this transaction) "));
418 static struct ovsdb_error * WARN_UNUSED_RESULT
419 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
421 struct ovsdb_table *table = txn_row->table;
423 if (txn_row->old && txn_row->new) {
424 struct shash_node *node;
425 bool changed = false;
427 SHASH_FOR_EACH (node, &table->schema->columns) {
428 const struct ovsdb_column *column = node->data;
429 const struct ovsdb_type *type = &column->type;
430 unsigned int idx = column->index;
432 if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
433 &txn_row->new->fields[idx],
435 bitmap_set1(txn_row->changed, idx);
441 /* Nothing actually changed in this row, so drop it. */
442 ovsdb_txn_row_abort(txn, txn_row);
445 bitmap_set_multiple(txn_row->changed, 0,
446 shash_count(&table->schema->columns), 1);
452 static struct ovsdb_error * WARN_UNUSED_RESULT
453 check_max_rows(struct ovsdb_txn *txn)
455 struct ovsdb_txn_table *t;
457 LIST_FOR_EACH (t, node, &txn->txn_tables) {
458 size_t n_rows = hmap_count(&t->table->rows);
459 unsigned int max_rows = t->table->schema->max_rows;
461 if (n_rows > max_rows) {
462 return ovsdb_error("constraint violation",
463 "transaction causes \"%s\" table to contain "
464 "%zu rows, greater than the schema-defined "
465 "limit of %u row(s)",
466 t->table->schema->name, n_rows, max_rows);
474 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
476 struct ovsdb_replica *replica;
477 struct ovsdb_error *error;
479 /* Figure out what actually changed, and abort early if the transaction
480 * was really a no-op. */
481 error = for_each_txn_row(txn, determine_changes);
483 return OVSDB_WRAP_BUG("can't happen", error);
485 if (list_is_empty(&txn->txn_tables)) {
486 ovsdb_txn_abort(txn);
490 /* Check maximum rows table constraints. */
491 error = check_max_rows(txn);
493 ovsdb_txn_abort(txn);
497 /* Update reference counts and check referential integrity. */
498 error = update_ref_counts(txn);
500 ovsdb_txn_abort(txn);
504 /* Check reference counts and remove bad reference for "weak" referential
506 error = for_each_txn_row(txn, assess_weak_refs);
508 ovsdb_txn_abort(txn);
512 /* Send the commit to each replica. */
513 LIST_FOR_EACH (replica, node, &txn->db->replicas) {
514 error = (replica->class->commit)(replica, txn, durable);
516 /* We don't support two-phase commit so only the first replica is
517 * allowed to report an error. */
518 assert(&replica->node == txn->db->replicas.next);
520 ovsdb_txn_abort(txn);
525 /* Finalize commit. */
526 txn->db->run_triggers = true;
527 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
534 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
535 ovsdb_txn_row_cb_func *cb, void *aux)
537 struct ovsdb_txn_table *t;
538 struct ovsdb_txn_row *r;
540 LIST_FOR_EACH (t, node, &txn->txn_tables) {
541 HMAP_FOR_EACH (r, hmap_node, &t->txn_rows) {
542 if ((r->old || r->new) && !cb(r->old, r->new, r->changed, aux)) {
549 static struct ovsdb_txn_table *
550 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
552 if (!table->txn_table) {
553 struct ovsdb_txn_table *txn_table;
555 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
556 txn_table->table = table;
557 hmap_init(&txn_table->txn_rows);
558 txn_table->serial = serial - 1;
559 list_push_back(&txn->txn_tables, &txn_table->node);
561 return table->txn_table;
564 static struct ovsdb_txn_row *
565 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
566 const struct ovsdb_row *old_, struct ovsdb_row *new)
568 const struct ovsdb_row *row = old_ ? old_ : new;
569 struct ovsdb_row *old = (struct ovsdb_row *) old_;
570 size_t n_columns = shash_count(&table->schema->columns);
571 struct ovsdb_txn_table *txn_table;
572 struct ovsdb_txn_row *txn_row;
574 txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
575 + bitmap_n_bytes(n_columns));
576 txn_row->uuid = *ovsdb_row_get_uuid(row);
577 txn_row->table = row->table;
580 txn_row->n_refs = old ? old->n_refs : 0;
581 txn_row->serial = serial - 1;
584 old->txn_row = txn_row;
587 new->txn_row = txn_row;
590 txn_table = ovsdb_txn_create_txn_table(txn, table);
591 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
592 ovsdb_row_hash(old ? old : new));
598 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
600 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
602 if (ro_row->txn_row) {
603 assert(ro_row == ro_row->txn_row->new);
606 struct ovsdb_table *table = ro_row->table;
607 struct ovsdb_row *rw_row;
609 rw_row = ovsdb_row_clone(ro_row);
610 rw_row->n_refs = ro_row->n_refs;
611 uuid_generate(ovsdb_row_get_version_rw(rw_row));
612 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
613 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
620 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
622 uint32_t hash = ovsdb_row_hash(row);
623 struct ovsdb_table *table = row->table;
625 uuid_generate(ovsdb_row_get_version_rw(row));
627 ovsdb_txn_row_create(txn, table, NULL, row);
628 hmap_insert(&table->rows, &row->hmap_node, hash);
631 /* 'row' must be assumed destroyed upon return; the caller must not reference
634 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
636 struct ovsdb_row *row = (struct ovsdb_row *) row_;
637 struct ovsdb_table *table = row->table;
638 struct ovsdb_txn_row *txn_row = row->txn_row;
640 hmap_remove(&table->rows, &row->hmap_node);
643 ovsdb_txn_row_create(txn, table, row, NULL);
645 assert(txn_row->new == row);
649 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
652 ovsdb_row_destroy(row);
657 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
659 if (txn->comment.length) {
660 ds_put_char(&txn->comment, '\n');
662 ds_put_cstr(&txn->comment, s);
666 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
668 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
672 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
674 struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
676 txn_table->n_processed--;
677 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
680 txn_row->old->txn_row = NULL;
683 txn_row->new->txn_row = NULL;
688 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
690 assert(hmap_is_empty(&txn_table->txn_rows));
691 txn_table->table->txn_table = NULL;
692 hmap_destroy(&txn_table->txn_rows);
693 list_remove(&txn_table->node);
697 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
698 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
699 * returns a null pointer after iteration is complete.
701 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
702 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
703 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
704 * in within the same txn_table. It may *not* delete any txn_tables. As long
705 * as these rules are followed, 'cb' will be called exactly once for each
706 * txn_row in 'txn', even those added by 'cb'.
708 * (Even though 'cb' is not allowed to delete some txn_rows, it can still
709 * delete any actual row by clearing a txn_row's 'new' member.)
711 static struct ovsdb_error * WARN_UNUSED_RESULT
712 for_each_txn_row(struct ovsdb_txn *txn,
713 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
714 struct ovsdb_txn_row *))
721 struct ovsdb_txn_table *t, *next_txn_table;
724 LIST_FOR_EACH_SAFE (t, next_txn_table, node, &txn->txn_tables) {
725 if (t->serial != serial) {
730 while (t->n_processed < hmap_count(&t->txn_rows)) {
731 struct ovsdb_txn_row *r, *next_txn_row;
733 HMAP_FOR_EACH_SAFE (r, next_txn_row, hmap_node, &t->txn_rows) {
734 if (r->serial != serial) {
735 struct ovsdb_error *error;
748 if (hmap_is_empty(&t->txn_rows)) {
749 /* Table is empty. Drop it. */
750 ovsdb_txn_table_destroy(t);