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 (base->type != OVSDB_TYPE_UUID || !base->u.uuid.refTable) {
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);
273 static struct ovsdb_error * WARN_UNUSED_RESULT
274 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
276 struct ovsdb_table *table;
278 table = (txn_row->old ? txn_row->old : txn_row->new)->table;
279 if (txn_row->old && txn_row->new) {
280 struct shash_node *node;
281 bool changed = false;
283 SHASH_FOR_EACH (node, &table->schema->columns) {
284 const struct ovsdb_column *column = node->data;
285 const struct ovsdb_type *type = &column->type;
286 unsigned int idx = column->index;
288 if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
289 &txn_row->new->fields[idx],
291 bitmap_set1(txn_row->changed, idx);
297 /* Nothing actually changed in this row, so drop it. */
298 ovsdb_txn_row_abort(txn, txn_row);
301 bitmap_set_multiple(txn_row->changed, 0,
302 shash_count(&table->schema->columns), 1);
309 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
311 struct ovsdb_replica *replica;
312 struct ovsdb_error *error;
314 /* Figure out what actually changed, and abort early if the transaction
315 * was really a no-op. */
316 error = for_each_txn_row(txn, determine_changes);
318 ovsdb_error_destroy(error);
319 return OVSDB_BUG("can't happen");
321 if (list_is_empty(&txn->txn_tables)) {
322 ovsdb_txn_abort(txn);
326 /* Update reference counts and check referential integrity. */
327 error = update_ref_counts(txn);
329 ovsdb_txn_abort(txn);
333 /* Send the commit to each replica. */
334 LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
335 error = (replica->class->commit)(replica, txn, durable);
337 /* We don't support two-phase commit so only the first replica is
338 * allowed to report an error. */
339 assert(&replica->node == txn->db->replicas.next);
341 ovsdb_txn_abort(txn);
346 /* Finalize commit. */
347 txn->db->run_triggers = true;
348 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
355 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
356 ovsdb_txn_row_cb_func *cb, void *aux)
358 struct ovsdb_txn_table *t;
359 struct ovsdb_txn_row *r;
361 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
362 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
363 if (!cb(r->old, r->new, r->changed, aux)) {
370 static struct ovsdb_txn_table *
371 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
373 if (!table->txn_table) {
374 struct ovsdb_txn_table *txn_table;
376 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
377 txn_table->table = table;
378 hmap_init(&txn_table->txn_rows);
379 txn_table->serial = serial - 1;
380 list_push_back(&txn->txn_tables, &txn_table->node);
382 return table->txn_table;
385 static struct ovsdb_txn_row *
386 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
387 const struct ovsdb_row *old_, struct ovsdb_row *new)
389 struct ovsdb_row *old = (struct ovsdb_row *) old_;
390 size_t n_columns = shash_count(&table->schema->columns);
391 struct ovsdb_txn_table *txn_table;
392 struct ovsdb_txn_row *txn_row;
394 txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
395 + bitmap_n_bytes(n_columns));
396 txn_row->old = (struct ovsdb_row *) old;
398 txn_row->n_refs = old ? old->n_refs : 0;
399 txn_row->serial = serial - 1;
402 old->txn_row = txn_row;
405 new->txn_row = txn_row;
408 txn_table = ovsdb_txn_create_txn_table(txn, table);
409 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
410 ovsdb_row_hash(old ? old : new));
416 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
418 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
420 if (ro_row->txn_row) {
421 assert(ro_row == ro_row->txn_row->new);
424 struct ovsdb_table *table = ro_row->table;
425 struct ovsdb_row *rw_row;
427 rw_row = ovsdb_row_clone(ro_row);
428 rw_row->n_refs = ro_row->n_refs;
429 uuid_generate(ovsdb_row_get_version_rw(rw_row));
430 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
431 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
438 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
440 uint32_t hash = ovsdb_row_hash(row);
441 struct ovsdb_table *table = row->table;
443 uuid_generate(ovsdb_row_get_version_rw(row));
445 ovsdb_txn_row_create(txn, table, NULL, row);
446 hmap_insert(&table->rows, &row->hmap_node, hash);
449 /* 'row' must be assumed destroyed upon return; the caller must not reference
452 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
454 struct ovsdb_row *row = (struct ovsdb_row *) row_;
455 struct ovsdb_table *table = row->table;
456 struct ovsdb_txn_row *txn_row = row->txn_row;
458 hmap_remove(&table->rows, &row->hmap_node);
461 ovsdb_txn_row_create(txn, table, row, NULL);
463 assert(txn_row->new == row);
467 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
470 ovsdb_row_destroy(row);
475 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
477 if (txn->comment.length) {
478 ds_put_char(&txn->comment, '\n');
480 ds_put_cstr(&txn->comment, s);
484 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
486 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
490 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
492 struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
493 struct ovsdb_txn_table *txn_table = row->table->txn_table;
495 txn_table->n_processed--;
496 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
499 txn_row->old->txn_row = NULL;
502 txn_row->new->txn_row = NULL;
507 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
509 assert(hmap_is_empty(&txn_table->txn_rows));
510 txn_table->table->txn_table = NULL;
511 hmap_destroy(&txn_table->txn_rows);
512 list_remove(&txn_table->node);
516 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
517 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
518 * returns a null pointer after iteration is complete.
520 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
521 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
522 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
523 * in within the same txn_table. It may *not* delete any txn_tables. As long
524 * as these rules are followed, 'cb' will be called exactly once for each
525 * txn_row in 'txn', even those added by 'cb'.
527 static struct ovsdb_error * WARN_UNUSED_RESULT
528 for_each_txn_row(struct ovsdb_txn *txn,
529 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
530 struct ovsdb_txn_row *))
537 struct ovsdb_txn_table *t, *next_txn_table;
540 LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
542 if (t->serial != serial) {
547 while (t->n_processed < hmap_count(&t->txn_rows)) {
548 struct ovsdb_txn_row *r, *next_txn_row;
550 HMAP_FOR_EACH_SAFE (r, next_txn_row,
551 struct ovsdb_txn_row, hmap_node,
553 if (r->serial != serial) {
554 struct ovsdb_error *error;
567 if (hmap_is_empty(&t->txn_rows)) {
568 /* Table is empty. Drop it. */
569 ovsdb_txn_table_destroy(t);