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. */
68 /* Used by for_each_txn_row(). */
69 unsigned int serial; /* Serial number of in-progress commit. */
72 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
73 static struct ovsdb_error * WARN_UNUSED_RESULT
74 for_each_txn_row(struct ovsdb_txn *txn,
75 struct ovsdb_error *(*)(struct ovsdb_txn *,
76 struct ovsdb_txn_row *));
78 /* Used by for_each_txn_row() to track tables and rows that have been
80 static unsigned int serial;
83 ovsdb_txn_create(struct ovsdb *db)
85 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
87 list_init(&txn->txn_tables);
88 ds_init(&txn->comment);
93 ovsdb_txn_free(struct ovsdb_txn *txn)
95 assert(list_is_empty(&txn->txn_tables));
96 ds_destroy(&txn->comment);
100 static struct ovsdb_error * WARN_UNUSED_RESULT
101 ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
102 struct ovsdb_txn_row *txn_row)
104 struct ovsdb_row *old = txn_row->old;
105 struct ovsdb_row *new = txn_row->new;
107 ovsdb_txn_row_prefree(txn_row);
109 hmap_remove(&new->table->rows, &new->hmap_node);
111 hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
113 hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
115 ovsdb_row_destroy(new);
122 ovsdb_txn_abort(struct ovsdb_txn *txn)
124 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
128 static struct ovsdb_txn_row *
129 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
131 struct ovsdb_txn_row *txn_row;
133 if (!table->txn_table) {
137 HMAP_FOR_EACH_WITH_HASH (txn_row, struct ovsdb_txn_row, hmap_node,
138 uuid_hash(uuid), &table->txn_table->txn_rows) {
139 const struct ovsdb_row *row;
141 row = txn_row->old ? txn_row->old : txn_row->new;
142 if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
151 ovsdb_txn_adjust_atom_refs(const union ovsdb_atom *atoms, unsigned int n,
152 const struct ovsdb_table *table,
153 int delta, struct ovsdb_error **errorp)
157 for (i = 0; i < n; i++) {
158 const struct uuid *uuid = &atoms[i].uuid;
159 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
162 txn_row->old->n_refs += delta;
165 txn_row->new->n_refs += delta;
168 const struct ovsdb_row *row_ = ovsdb_table_get_row(table, uuid);
170 struct ovsdb_row *row = (struct ovsdb_row *) row_;
171 row->n_refs += delta;
174 *errorp = ovsdb_error("referential integrity violation",
175 "reference to nonexistent row "
176 UUID_FMT, UUID_ARGS(uuid));
186 ovsdb_txn_adjust_row_refs(const struct ovsdb_row *r,
187 const struct ovsdb_column *column, int delta,
188 struct ovsdb_error **errorp)
190 const struct ovsdb_datum *field = &r->fields[column->index];
191 const struct ovsdb_type *type = &column->type;
193 if (type->key.type == OVSDB_TYPE_UUID && type->key.u.uuid.refTable) {
194 ovsdb_txn_adjust_atom_refs(field->keys, field->n,
195 type->key.u.uuid.refTable, delta, errorp);
197 if (type->value.type == OVSDB_TYPE_UUID && type->value.u.uuid.refTable) {
198 ovsdb_txn_adjust_atom_refs(field->values, field->n,
199 type->value.u.uuid.refTable, delta, errorp);
203 static struct ovsdb_error * WARN_UNUSED_RESULT
204 ovsdb_txn_adjust_ref_counts__(struct ovsdb_txn *txn, int delta)
206 struct ovsdb_txn_table *t;
207 struct ovsdb_error *error;
210 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
211 struct ovsdb_table *table = t->table;
212 struct ovsdb_txn_row *r;
214 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
215 struct shash_node *node;
217 SHASH_FOR_EACH (node, &table->schema->columns) {
218 const struct ovsdb_column *column = node->data;
221 ovsdb_txn_adjust_row_refs(r->old, column, -delta, NULL);
224 ovsdb_txn_adjust_row_refs(r->new, column, delta, &error);
233 ovsdb_txn_rollback_counts(struct ovsdb_txn *txn)
235 ovsdb_error_destroy(ovsdb_txn_adjust_ref_counts__(txn, -1));
238 static struct ovsdb_error * WARN_UNUSED_RESULT
239 ovsdb_txn_commit_ref_counts(struct ovsdb_txn *txn)
241 struct ovsdb_error *error = ovsdb_txn_adjust_ref_counts__(txn, 1);
243 ovsdb_txn_rollback_counts(txn);
248 static struct ovsdb_error * WARN_UNUSED_RESULT
249 update_ref_counts(struct ovsdb_txn *txn)
251 struct ovsdb_error *error;
252 struct ovsdb_txn_table *t;
254 error = ovsdb_txn_commit_ref_counts(txn);
259 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
260 struct ovsdb_txn_row *r;
262 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
263 if (!r->new && r->old->n_refs) {
264 error = ovsdb_error("referential integrity violation",
265 "cannot delete %s row "UUID_FMT" because "
266 "of %zu remaining reference(s)",
267 t->table->schema->name,
268 UUID_ARGS(ovsdb_row_get_uuid(r->old)),
270 ovsdb_txn_rollback_counts(txn);
279 static struct ovsdb_error * WARN_UNUSED_RESULT
280 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
281 struct ovsdb_txn_row *txn_row)
283 ovsdb_txn_row_prefree(txn_row);
284 ovsdb_row_destroy(txn_row->old);
291 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
293 struct ovsdb_replica *replica;
294 struct ovsdb_error *error;
296 error = update_ref_counts(txn);
298 ovsdb_txn_abort(txn);
302 LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
303 error = (replica->class->commit)(replica, txn, durable);
305 /* We don't support two-phase commit so only the first replica is
306 * allowed to report an error. */
307 assert(&replica->node == txn->db->replicas.next);
309 ovsdb_txn_abort(txn);
314 /* Finalize commit. */
315 txn->db->run_triggers = true;
316 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
323 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
324 ovsdb_txn_row_cb_func *cb, void *aux)
326 struct ovsdb_txn_table *t;
327 struct ovsdb_txn_row *r;
329 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
330 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
331 if (!cb(r->old, r->new, aux)) {
338 static struct ovsdb_txn_table *
339 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
341 if (!table->txn_table) {
342 struct ovsdb_txn_table *txn_table;
344 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
345 txn_table->table = table;
346 hmap_init(&txn_table->txn_rows);
347 txn_table->serial = serial - 1;
348 list_push_back(&txn->txn_tables, &txn_table->node);
350 return table->txn_table;
353 static struct ovsdb_txn_row *
354 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
355 const struct ovsdb_row *old_, struct ovsdb_row *new)
357 struct ovsdb_row *old = (struct ovsdb_row *) old_;
358 struct ovsdb_txn_table *txn_table;
359 struct ovsdb_txn_row *txn_row;
361 txn_row = xmalloc(sizeof *txn_row);
364 txn_row->serial = serial - 1;
367 old->txn_row = txn_row;
370 new->txn_row = txn_row;
373 txn_table = ovsdb_txn_create_txn_table(txn, table);
374 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
375 ovsdb_row_hash(old ? old : new));
381 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
383 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
385 if (ro_row->txn_row) {
386 assert(ro_row == ro_row->txn_row->new);
389 struct ovsdb_table *table = ro_row->table;
390 struct ovsdb_row *rw_row;
392 rw_row = ovsdb_row_clone(ro_row);
393 rw_row->n_refs = ro_row->n_refs;
394 uuid_generate(ovsdb_row_get_version_rw(rw_row));
395 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
396 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
403 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
405 uint32_t hash = ovsdb_row_hash(row);
406 struct ovsdb_table *table = row->table;
408 uuid_generate(ovsdb_row_get_version_rw(row));
410 ovsdb_txn_row_create(txn, table, NULL, row);
411 hmap_insert(&table->rows, &row->hmap_node, hash);
414 /* 'row' must be assumed destroyed upon return; the caller must not reference
417 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
419 struct ovsdb_row *row = (struct ovsdb_row *) row_;
420 struct ovsdb_table *table = row->table;
421 struct ovsdb_txn_row *txn_row = row->txn_row;
423 hmap_remove(&table->rows, &row->hmap_node);
426 ovsdb_txn_row_create(txn, table, row, NULL);
428 assert(txn_row->new == row);
432 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
435 ovsdb_row_destroy(row);
440 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
442 if (txn->comment.length) {
443 ds_put_char(&txn->comment, '\n');
445 ds_put_cstr(&txn->comment, s);
449 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
451 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
455 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
457 struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
458 struct ovsdb_txn_table *txn_table = row->table->txn_table;
460 txn_table->n_processed--;
461 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
464 txn_row->old->txn_row = NULL;
467 txn_row->new->txn_row = NULL;
472 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
474 assert(hmap_is_empty(&txn_table->txn_rows));
475 txn_table->table->txn_table = NULL;
476 hmap_destroy(&txn_table->txn_rows);
477 list_remove(&txn_table->node);
481 /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
482 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
483 * returns a null pointer after iteration is complete.
485 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
486 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
487 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
488 * in within the same txn_table. It may *not* delete any txn_tables. As long
489 * as these rules are followed, 'cb' will be called exactly once for each
490 * txn_row in 'txn', even those added by 'cb'.
492 static struct ovsdb_error * WARN_UNUSED_RESULT
493 for_each_txn_row(struct ovsdb_txn *txn,
494 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
495 struct ovsdb_txn_row *))
502 struct ovsdb_txn_table *t, *next_txn_table;
505 LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
507 if (t->serial != serial) {
512 while (t->n_processed < hmap_count(&t->txn_rows)) {
513 struct ovsdb_txn_row *r, *next_txn_row;
515 HMAP_FOR_EACH_SAFE (r, next_txn_row,
516 struct ovsdb_txn_row, hmap_node,
518 if (r->serial != serial) {
519 struct ovsdb_error *error;
532 if (hmap_is_empty(&t->txn_rows)) {
533 /* Table is empty. Drop it. */
534 ovsdb_txn_table_destroy(t);