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. */
46 /* A row modified by the transaction:
48 * - A row added by a transaction will have null 'old' and non-null 'new'.
50 * - A row deleted by a transaction will have non-null 'old' and null
53 * - A row modified by a transaction will have non-null 'old' and 'new'.
55 * - 'old' and 'new' both null is invalid. It would indicate that a row
56 * was added then deleted within a single transaction, but we instead
57 * handle that case by deleting the txn_row entirely.
59 struct ovsdb_txn_row {
60 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
61 struct ovsdb_row *old; /* The old row. */
62 struct ovsdb_row *new; /* The new row. */
66 ovsdb_txn_create(struct ovsdb *db)
68 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
70 list_init(&txn->txn_tables);
71 ds_init(&txn->comment);
76 ovsdb_txn_destroy(struct ovsdb_txn *txn, void (*cb)(struct ovsdb_txn_row *))
78 struct ovsdb_txn_table *txn_table, *next_txn_table;
80 LIST_FOR_EACH_SAFE (txn_table, next_txn_table,
81 struct ovsdb_txn_table, node, &txn->txn_tables) {
82 struct ovsdb_txn_row *txn_row, *next_txn_row;
84 HMAP_FOR_EACH_SAFE (txn_row, next_txn_row,
85 struct ovsdb_txn_row, hmap_node,
89 txn_row->old->txn_row = NULL;
92 txn_row->new->txn_row = NULL;
98 txn_table->table->txn_table = NULL;
99 hmap_destroy(&txn_table->txn_rows);
102 ds_destroy(&txn->comment);
107 ovsdb_txn_row_abort(struct ovsdb_txn_row *txn_row)
109 struct ovsdb_row *old = txn_row->old;
110 struct ovsdb_row *new = txn_row->new;
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);
123 ovsdb_txn_abort(struct ovsdb_txn *txn)
125 ovsdb_txn_destroy(txn, ovsdb_txn_row_abort);
129 ovsdb_txn_row_commit(struct ovsdb_txn_row *txn_row)
131 ovsdb_row_destroy(txn_row->old);
134 static struct ovsdb_txn_row *
135 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
137 struct ovsdb_txn_row *txn_row;
139 if (!table->txn_table) {
143 HMAP_FOR_EACH_WITH_HASH (txn_row, struct ovsdb_txn_row, hmap_node,
144 uuid_hash(uuid), &table->txn_table->txn_rows) {
145 const struct ovsdb_row *row;
147 row = txn_row->old ? txn_row->old : txn_row->new;
148 if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
157 ovsdb_txn_adjust_atom_refs(const union ovsdb_atom *atoms, unsigned int n,
158 const struct ovsdb_table *table,
159 int delta, struct ovsdb_error **errorp)
163 for (i = 0; i < n; i++) {
164 const struct uuid *uuid = &atoms[i].uuid;
165 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
168 txn_row->old->n_refs += delta;
171 txn_row->new->n_refs += delta;
174 const struct ovsdb_row *row_ = ovsdb_table_get_row(table, uuid);
176 struct ovsdb_row *row = (struct ovsdb_row *) row_;
177 row->n_refs += delta;
180 *errorp = ovsdb_error("referential integrity violation",
181 "reference to nonexistent row "
182 UUID_FMT, UUID_ARGS(uuid));
192 ovsdb_txn_adjust_row_refs(const struct ovsdb_row *r,
193 const struct ovsdb_column *column, int delta,
194 struct ovsdb_error **errorp)
196 const struct ovsdb_datum *field = &r->fields[column->index];
197 const struct ovsdb_type *type = &column->type;
199 if (type->key.type == OVSDB_TYPE_UUID && type->key.u.uuid.refTable) {
200 ovsdb_txn_adjust_atom_refs(field->keys, field->n,
201 type->key.u.uuid.refTable, delta, errorp);
203 if (type->value.type == OVSDB_TYPE_UUID && type->value.u.uuid.refTable) {
204 ovsdb_txn_adjust_atom_refs(field->values, field->n,
205 type->value.u.uuid.refTable, delta, errorp);
209 static struct ovsdb_error * WARN_UNUSED_RESULT
210 ovsdb_txn_adjust_ref_counts__(struct ovsdb_txn *txn, int delta)
212 struct ovsdb_txn_table *t;
213 struct ovsdb_error *error;
216 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
217 struct ovsdb_table *table = t->table;
218 struct ovsdb_txn_row *r;
220 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
221 struct shash_node *node;
223 SHASH_FOR_EACH (node, &table->schema->columns) {
224 const struct ovsdb_column *column = node->data;
227 ovsdb_txn_adjust_row_refs(r->old, column, -delta, NULL);
230 ovsdb_txn_adjust_row_refs(r->new, column, delta, &error);
239 ovsdb_txn_rollback_counts(struct ovsdb_txn *txn)
241 ovsdb_error_destroy(ovsdb_txn_adjust_ref_counts__(txn, -1));
244 static struct ovsdb_error * WARN_UNUSED_RESULT
245 ovsdb_txn_commit_ref_counts(struct ovsdb_txn *txn)
247 struct ovsdb_error *error = ovsdb_txn_adjust_ref_counts__(txn, 1);
249 ovsdb_txn_rollback_counts(txn);
254 static struct ovsdb_error * WARN_UNUSED_RESULT
255 update_ref_counts(struct ovsdb_txn *txn)
257 struct ovsdb_error *error;
258 struct ovsdb_txn_table *t;
260 error = ovsdb_txn_commit_ref_counts(txn);
265 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
266 struct ovsdb_txn_row *r;
268 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
269 if (!r->new && r->old->n_refs) {
270 error = ovsdb_error("referential integrity violation",
271 "cannot delete %s row "UUID_FMT" because "
272 "of %zu remaining reference(s)",
273 t->table->schema->name,
274 UUID_ARGS(ovsdb_row_get_uuid(r->old)),
276 ovsdb_txn_rollback_counts(txn);
286 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
288 struct ovsdb_replica *replica;
289 struct ovsdb_error *error;
291 error = update_ref_counts(txn);
293 ovsdb_txn_abort(txn);
297 LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
298 error = (replica->class->commit)(replica, txn, durable);
300 /* We don't support two-phase commit so only the first replica is
301 * allowed to report an error. */
302 assert(&replica->node == txn->db->replicas.next);
304 ovsdb_txn_abort(txn);
309 txn->db->run_triggers = true;
310 ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
315 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
316 ovsdb_txn_row_cb_func *cb, void *aux)
318 struct ovsdb_txn_table *t;
319 struct ovsdb_txn_row *r;
321 LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
322 HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
323 if (!cb(r->old, r->new, aux)) {
330 static struct ovsdb_txn_table *
331 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
333 if (!table->txn_table) {
334 struct ovsdb_txn_table *txn_table;
336 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
337 txn_table->table = table;
338 hmap_init(&txn_table->txn_rows);
339 list_push_back(&txn->txn_tables, &txn_table->node);
341 return table->txn_table;
344 static struct ovsdb_txn_row *
345 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
346 const struct ovsdb_row *old, struct ovsdb_row *new)
348 struct ovsdb_txn_table *txn_table;
349 struct ovsdb_txn_row *txn_row;
351 txn_row = xmalloc(sizeof *txn_row);
352 txn_row->old = (struct ovsdb_row *) old;
355 txn_table = ovsdb_txn_create_txn_table(txn, table);
356 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
357 ovsdb_row_hash(old ? old : new));
363 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
365 struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
367 if (ro_row->txn_row) {
368 assert(ro_row == ro_row->txn_row->new);
371 struct ovsdb_table *table = ro_row->table;
372 struct ovsdb_row *rw_row;
374 rw_row = ovsdb_row_clone(ro_row);
375 rw_row->n_refs = ro_row->n_refs;
376 uuid_generate(ovsdb_row_get_version_rw(rw_row));
377 rw_row->txn_row = ovsdb_txn_row_create(txn, table, ro_row, rw_row);
378 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
385 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
387 uint32_t hash = ovsdb_row_hash(row);
388 struct ovsdb_table *table = row->table;
390 uuid_generate(ovsdb_row_get_version_rw(row));
392 row->txn_row = ovsdb_txn_row_create(txn, table, NULL, row);
393 hmap_insert(&table->rows, &row->hmap_node, hash);
396 /* 'row' must be assumed destroyed upon return; the caller must not reference
399 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
401 struct ovsdb_row *row = (struct ovsdb_row *) row_;
402 struct ovsdb_table *table = row->table;
403 struct ovsdb_txn_row *txn_row = row->txn_row;
405 hmap_remove(&table->rows, &row->hmap_node);
408 row->txn_row = ovsdb_txn_row_create(txn, table, row, NULL);
410 assert(txn_row->new == row);
414 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
417 ovsdb_row_destroy(row);
422 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
424 if (txn->comment.length) {
425 ds_put_char(&txn->comment, '\n');
427 ds_put_cstr(&txn->comment, s);
431 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
433 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;