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"
22 #include "dynamic-string.h"
27 #include "ovsdb-error.h"
35 struct list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
39 /* A table modified by a transaction. */
40 struct ovsdb_txn_table {
41 struct list node; /* Element in ovsdb_txn's txn_tables list. */
42 struct ovsdb_table *table;
43 struct hmap txn_rows; /* Contains "struct ovsdb_txn_row"s. */
45 /* Used by for_each_txn_row(). */
46 unsigned int serial; /* Serial number of in-progress iteration. */
47 unsigned int n_processed; /* Number of rows processed. */
50 /* A row modified by the transaction:
52 * - A row added by a transaction will have null 'old' and non-null 'new'.
54 * - A row deleted by a transaction will have non-null 'old' and null
57 * - A row modified by a transaction will have non-null 'old' and 'new'.
59 * - 'old' and 'new' both null is invalid. It would indicate that a row
60 * was added then deleted within a single transaction, but we instead
61 * handle that case by deleting the txn_row entirely.
63 struct ovsdb_txn_row {
64 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
65 struct ovsdb_row *old; /* The old row. */
66 struct ovsdb_row *new; /* The new row. */
67 size_t n_refs; /* Number of remaining references. */
69 /* Used by for_each_txn_row(). */
70 unsigned int serial; /* Serial number of in-progress commit. */
73 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
74 static struct ovsdb_error * WARN_UNUSED_RESULT
75 for_each_txn_row(struct ovsdb_txn *txn,
76 struct ovsdb_error *(*)(struct ovsdb_txn *,
77 struct ovsdb_txn_row *));
79 /* Used by for_each_txn_row() to track tables and rows that have been
81 static unsigned int serial;
84 ovsdb_txn_create(struct ovsdb *db)
86 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
88 list_init(&txn->txn_tables);
89 ds_init(&txn->comment);
94 ovsdb_txn_free(struct ovsdb_txn *txn)
96 assert(list_is_empty(&txn->txn_tables));
97 ds_destroy(&txn->comment);
101 static struct ovsdb_error * WARN_UNUSED_RESULT
102 ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
103 struct ovsdb_txn_row *txn_row)
105 struct ovsdb_row *old = txn_row->old;
106 struct ovsdb_row *new = txn_row->new;
108 ovsdb_txn_row_prefree(txn_row);
110 hmap_remove(&new->table->rows, &new->hmap_node);
112 hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
114 hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
116 ovsdb_row_destroy(new);
123 ovsdb_txn_abort(struct ovsdb_txn *txn)
125 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
129 static struct ovsdb_txn_row *
130 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
132 struct ovsdb_txn_row *txn_row;
134 if (!table->txn_table) {
138 HMAP_FOR_EACH_WITH_HASH (txn_row, struct ovsdb_txn_row, hmap_node,
139 uuid_hash(uuid), &table->txn_table->txn_rows) {
140 const struct ovsdb_row *row;
142 row = txn_row->old ? txn_row->old : txn_row->new;
143 if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
151 static struct ovsdb_error * WARN_UNUSED_RESULT
152 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn,
153 const struct ovsdb_base_type *base,
154 const union ovsdb_atom *atoms, unsigned int n,
157 const struct ovsdb_table *table;
160 if (base->type != OVSDB_TYPE_UUID || !base->u.uuid.refTable) {
164 table = base->u.uuid.refTable;
165 for (i = 0; i < n; i++) {
166 const struct uuid *uuid = &atoms[i].uuid;
167 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
169 const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
171 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
173 return ovsdb_error("referential integrity violation",
174 "reference to nonexistent row "
175 UUID_FMT, UUID_ARGS(uuid));
178 txn_row->n_refs += delta;
184 static struct ovsdb_error * WARN_UNUSED_RESULT
185 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
186 const struct ovsdb_column *column, int delta)
188 const struct ovsdb_datum *field = &r->fields[column->index];
189 struct ovsdb_error *error;
191 error = ovsdb_txn_adjust_atom_refs(txn, &column->type.key,
192 field->keys, field->n, delta);
194 error = ovsdb_txn_adjust_atom_refs(txn, &column->type.value,
195 field->values, field->n, delta);
200 static struct ovsdb_error * WARN_UNUSED_RESULT
201 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
203 struct ovsdb_table *table = r->old ? r->old->table : r->new->table;
204 struct shash_node *node;
206 SHASH_FOR_EACH (node, &table->schema->columns) {
207 const struct ovsdb_column *column = node->data;
208 struct ovsdb_error *error;
211 error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
213 ovsdb_error_destroy(error);
214 return OVSDB_BUG("error decreasing refcount");
218 error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
228 static struct ovsdb_error * WARN_UNUSED_RESULT
229 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
231 if (r->new || !r->n_refs) {
234 return ovsdb_error("referential integrity violation",
235 "cannot delete %s row "UUID_FMT" because "
236 "of %zu remaining reference(s)",
237 r->old->table->schema->name,
238 UUID_ARGS(ovsdb_row_get_uuid(r->old)),
243 static struct ovsdb_error * WARN_UNUSED_RESULT
244 update_ref_counts(struct ovsdb_txn *txn)
246 struct ovsdb_error *error;
248 error = for_each_txn_row(txn, update_row_ref_count);
253 return for_each_txn_row(txn, check_ref_count);
256 static struct ovsdb_error * WARN_UNUSED_RESULT
257 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
258 struct ovsdb_txn_row *txn_row)
260 ovsdb_txn_row_prefree(txn_row);
262 txn_row->new->n_refs = txn_row->n_refs;
264 ovsdb_row_destroy(txn_row->old);
271 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
273 struct ovsdb_replica *replica;
274 struct ovsdb_error *error;
276 error = update_ref_counts(txn);
278 ovsdb_txn_abort(txn);
282 LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
283 error = (replica->class->commit)(replica, txn, durable);
285 /* We don't support two-phase commit so only the first replica is
286 * allowed to report an error. */
287 assert(&replica->node == txn->db->replicas.next);
289 ovsdb_txn_abort(txn);
294 /* Finalize commit. */
295 txn->db->run_triggers = true;
296 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
303 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
304 ovsdb_txn_row_cb_func *cb, void *aux)
306 struct ovsdb_txn_table *t;
307 struct ovsdb_txn_row *r;
309 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
310 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
311 if (!cb(r->old, r->new, aux)) {
318 static struct ovsdb_txn_table *
319 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
321 if (!table->txn_table) {
322 struct ovsdb_txn_table *txn_table;
324 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
325 txn_table->table = table;
326 hmap_init(&txn_table->txn_rows);
327 txn_table->serial = serial - 1;
328 list_push_back(&txn->txn_tables, &txn_table->node);
330 return table->txn_table;
333 static struct ovsdb_txn_row *
334 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
335 const struct ovsdb_row *old_, struct ovsdb_row *new)
337 struct ovsdb_row *old = (struct ovsdb_row *) old_;
338 struct ovsdb_txn_table *txn_table;
339 struct ovsdb_txn_row *txn_row;
341 txn_row = xmalloc(sizeof *txn_row);
344 txn_row->n_refs = old ? old->n_refs : 0;
345 txn_row->serial = serial - 1;
348 old->txn_row = txn_row;
351 new->txn_row = txn_row;
354 txn_table = ovsdb_txn_create_txn_table(txn, table);
355 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
356 ovsdb_row_hash(old ? old : new));
362 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
364 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
366 if (ro_row->txn_row) {
367 assert(ro_row == ro_row->txn_row->new);
370 struct ovsdb_table *table = ro_row->table;
371 struct ovsdb_row *rw_row;
373 rw_row = ovsdb_row_clone(ro_row);
374 rw_row->n_refs = ro_row->n_refs;
375 uuid_generate(ovsdb_row_get_version_rw(rw_row));
376 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
377 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
384 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
386 uint32_t hash = ovsdb_row_hash(row);
387 struct ovsdb_table *table = row->table;
389 uuid_generate(ovsdb_row_get_version_rw(row));
391 ovsdb_txn_row_create(txn, table, NULL, row);
392 hmap_insert(&table->rows, &row->hmap_node, hash);
395 /* 'row' must be assumed destroyed upon return; the caller must not reference
398 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
400 struct ovsdb_row *row = (struct ovsdb_row *) row_;
401 struct ovsdb_table *table = row->table;
402 struct ovsdb_txn_row *txn_row = row->txn_row;
404 hmap_remove(&table->rows, &row->hmap_node);
407 ovsdb_txn_row_create(txn, table, row, NULL);
409 assert(txn_row->new == row);
413 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
416 ovsdb_row_destroy(row);
421 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
423 if (txn->comment.length) {
424 ds_put_char(&txn->comment, '\n');
426 ds_put_cstr(&txn->comment, s);
430 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
432 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
436 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
438 struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
439 struct ovsdb_txn_table *txn_table = row->table->txn_table;
441 txn_table->n_processed--;
442 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
445 txn_row->old->txn_row = NULL;
448 txn_row->new->txn_row = NULL;
453 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
455 assert(hmap_is_empty(&txn_table->txn_rows));
456 txn_table->table->txn_table = NULL;
457 hmap_destroy(&txn_table->txn_rows);
458 list_remove(&txn_table->node);
462 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
463 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
464 * returns a null pointer after iteration is complete.
466 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
467 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
468 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
469 * in within the same txn_table. It may *not* delete any txn_tables. As long
470 * as these rules are followed, 'cb' will be called exactly once for each
471 * txn_row in 'txn', even those added by 'cb'.
473 static struct ovsdb_error * WARN_UNUSED_RESULT
474 for_each_txn_row(struct ovsdb_txn *txn,
475 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
476 struct ovsdb_txn_row *))
483 struct ovsdb_txn_table *t, *next_txn_table;
486 LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
488 if (t->serial != serial) {
493 while (t->n_processed < hmap_count(&t->txn_rows)) {
494 struct ovsdb_txn_row *r, *next_txn_row;
496 HMAP_FOR_EACH_SAFE (r, next_txn_row,
497 struct ovsdb_txn_row, hmap_node,
499 if (r->serial != serial) {
500 struct ovsdb_error *error;
513 if (hmap_is_empty(&t->txn_rows)) {
514 /* Table is empty. Drop it. */
515 ovsdb_txn_table_destroy(t);