ovsdb: Check for changed columns only once per transaction commit.
[cascardo/ovs.git] / ovsdb / transaction.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "transaction.h"
19
20 #include <assert.h>
21
22 #include "bitmap.h"
23 #include "dynamic-string.h"
24 #include "hash.h"
25 #include "hmap.h"
26 #include "json.h"
27 #include "list.h"
28 #include "ovsdb-error.h"
29 #include "ovsdb.h"
30 #include "row.h"
31 #include "table.h"
32 #include "uuid.h"
33
34 struct ovsdb_txn {
35     struct ovsdb *db;
36     struct list txn_tables;     /* Contains "struct ovsdb_txn_table"s. */
37     struct ds comment;
38 };
39
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. */
45
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. */
49 };
50
51 /* A row modified by the transaction:
52  *
53  *      - A row added by a transaction will have null 'old' and non-null 'new'.
54  *
55  *      - A row deleted by a transaction will have non-null 'old' and null
56  *        'new'.
57  *
58  *      - A row modified by a transaction will have non-null 'old' and 'new'.
59  *
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.
63  */
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. */
69
70     /* Used by for_each_txn_row(). */
71     unsigned int serial;        /* Serial number of in-progress commit. */
72
73     unsigned long changed[];    /* Bits set to 1 for columns that changed. */
74 };
75
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 *));
81
82 /* Used by for_each_txn_row() to track tables and rows that have been
83  * processed.  */
84 static unsigned int serial;
85
86 struct ovsdb_txn *
87 ovsdb_txn_create(struct ovsdb *db)
88 {
89     struct ovsdb_txn *txn = xmalloc(sizeof *txn);
90     txn->db = db;
91     list_init(&txn->txn_tables);
92     ds_init(&txn->comment);
93     return txn;
94 }
95
96 static void
97 ovsdb_txn_free(struct ovsdb_txn *txn)
98 {
99     assert(list_is_empty(&txn->txn_tables));
100     ds_destroy(&txn->comment);
101     free(txn);
102 }
103
104 static struct ovsdb_error *
105 ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
106                     struct ovsdb_txn_row *txn_row)
107 {
108     struct ovsdb_row *old = txn_row->old;
109     struct ovsdb_row *new = txn_row->new;
110
111     ovsdb_txn_row_prefree(txn_row);
112     if (!old) {
113         hmap_remove(&new->table->rows, &new->hmap_node);
114     } else if (!new) {
115         hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
116     } else {
117         hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
118     }
119     ovsdb_row_destroy(new);
120     free(txn_row);
121
122     return NULL;
123 }
124
125 void
126 ovsdb_txn_abort(struct ovsdb_txn *txn)
127 {
128     ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
129     ovsdb_txn_free(txn);
130 }
131
132 static struct ovsdb_txn_row *
133 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
134 {
135     struct ovsdb_txn_row *txn_row;
136
137     if (!table->txn_table) {
138         return NULL;
139     }
140
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;
144
145         row = txn_row->old ? txn_row->old : txn_row->new;
146         if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
147             return txn_row;
148         }
149     }
150
151     return NULL;
152 }
153
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,
158                            int delta)
159 {
160     const struct ovsdb_table *table;
161     unsigned int i;
162
163     if (base->type != OVSDB_TYPE_UUID || !base->u.uuid.refTable) {
164         return NULL;
165     }
166
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);
171         if (!txn_row) {
172             const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
173             if (row) {
174                 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
175             } else {
176                 return ovsdb_error("referential integrity violation",
177                                    "reference to nonexistent row "
178                                    UUID_FMT, UUID_ARGS(uuid));
179             }
180         }
181         txn_row->n_refs += delta;
182     }
183
184     return NULL;
185 }
186
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)
190 {
191     const struct ovsdb_datum *field = &r->fields[column->index];
192     struct ovsdb_error *error;
193
194     error = ovsdb_txn_adjust_atom_refs(txn, &column->type.key,
195                                        field->keys, field->n, delta);
196     if (!error) {
197         error = ovsdb_txn_adjust_atom_refs(txn, &column->type.value,
198                                            field->values, field->n, delta);
199     }
200     return error;
201 }
202
203 static struct ovsdb_error * WARN_UNUSED_RESULT
204 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
205 {
206     struct ovsdb_table *table = r->old ? r->old->table : r->new->table;
207     struct shash_node *node;
208
209     SHASH_FOR_EACH (node, &table->schema->columns) {
210         const struct ovsdb_column *column = node->data;
211         struct ovsdb_error *error;
212
213         if (r->old) {
214             error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
215             if (error) {
216                 ovsdb_error_destroy(error);
217                 return OVSDB_BUG("error decreasing refcount");
218             }
219         }
220         if (r->new) {
221             error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
222             if (error) {
223                 return error;
224             }
225         }
226     }
227
228     return NULL;
229 }
230
231 static struct ovsdb_error * WARN_UNUSED_RESULT
232 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
233 {
234     if (r->new || !r->n_refs) {
235         return NULL;
236     } else {
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)),
242                            r->n_refs);
243     }
244 }
245
246 static struct ovsdb_error * WARN_UNUSED_RESULT
247 update_ref_counts(struct ovsdb_txn *txn)
248 {
249     struct ovsdb_error *error;
250
251     error = for_each_txn_row(txn, update_row_ref_count);
252     if (error) {
253         return error;
254     }
255
256     return for_each_txn_row(txn, check_ref_count);
257 }
258
259 static struct ovsdb_error *
260 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
261                      struct ovsdb_txn_row *txn_row)
262 {
263     ovsdb_txn_row_prefree(txn_row);
264     if (txn_row->new) {
265         txn_row->new->n_refs = txn_row->n_refs;
266     }
267     ovsdb_row_destroy(txn_row->old);
268     free(txn_row);
269
270     return NULL;
271 }
272
273 static struct ovsdb_error * WARN_UNUSED_RESULT
274 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
275 {
276     struct ovsdb_table *table;
277
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;
282
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;
287
288             if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
289                                     &txn_row->new->fields[idx],
290                                     type)) {
291                 bitmap_set1(txn_row->changed, idx);
292                 changed = true;
293             }
294         }
295
296         if (!changed) {
297             /* Nothing actually changed in this row, so drop it. */
298             ovsdb_txn_row_abort(txn, txn_row);
299         }
300     } else {
301         bitmap_set_multiple(txn_row->changed, 0,
302                             shash_count(&table->schema->columns), 1);
303     }
304
305     return NULL;
306 }
307
308 struct ovsdb_error *
309 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
310 {
311     struct ovsdb_replica *replica;
312     struct ovsdb_error *error;
313
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);
317     if (error) {
318         ovsdb_error_destroy(error);
319         return OVSDB_BUG("can't happen");
320     }
321     if (list_is_empty(&txn->txn_tables)) {
322         ovsdb_txn_abort(txn);
323         return NULL;
324     }
325
326     /* Update reference counts and check referential integrity. */
327     error = update_ref_counts(txn);
328     if (error) {
329         ovsdb_txn_abort(txn);
330         return error;
331     }
332
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);
336         if (error) {
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);
340
341             ovsdb_txn_abort(txn);
342             return error;
343         }
344     }
345
346     /* Finalize commit. */
347     txn->db->run_triggers = true;
348     ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
349     ovsdb_txn_free(txn);
350
351     return NULL;
352 }
353
354 void
355 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
356                           ovsdb_txn_row_cb_func *cb, void *aux)
357 {
358     struct ovsdb_txn_table *t;
359     struct ovsdb_txn_row *r;
360
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)) {
364                 break;
365             }
366         }
367    }
368 }
369
370 static struct ovsdb_txn_table *
371 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
372 {
373     if (!table->txn_table) {
374         struct ovsdb_txn_table *txn_table;
375
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);
381     }
382     return table->txn_table;
383 }
384
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)
388 {
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;
393
394     txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
395                       + bitmap_n_bytes(n_columns));
396     txn_row->old = (struct ovsdb_row *) old;
397     txn_row->new = new;
398     txn_row->n_refs = old ? old->n_refs : 0;
399     txn_row->serial = serial - 1;
400
401     if (old) {
402         old->txn_row = txn_row;
403     }
404     if (new) {
405         new->txn_row = txn_row;
406     }
407
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));
411
412     return txn_row;
413 }
414
415 struct ovsdb_row *
416 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
417 {
418     struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
419
420     if (ro_row->txn_row) {
421         assert(ro_row == ro_row->txn_row->new);
422         return ro_row;
423     } else {
424         struct ovsdb_table *table = ro_row->table;
425         struct ovsdb_row *rw_row;
426
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);
432
433         return rw_row;
434     }
435 }
436
437 void
438 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
439 {
440     uint32_t hash = ovsdb_row_hash(row);
441     struct ovsdb_table *table = row->table;
442
443     uuid_generate(ovsdb_row_get_version_rw(row));
444
445     ovsdb_txn_row_create(txn, table, NULL, row);
446     hmap_insert(&table->rows, &row->hmap_node, hash);
447 }
448
449 /* 'row' must be assumed destroyed upon return; the caller must not reference
450  * it again. */
451 void
452 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
453 {
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;
457
458     hmap_remove(&table->rows, &row->hmap_node);
459
460     if (!txn_row) {
461         ovsdb_txn_row_create(txn, table, row, NULL);
462     } else {
463         assert(txn_row->new == row);
464         if (txn_row->old) {
465             txn_row->new = NULL;
466         } else {
467             hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
468             free(txn_row);
469         }
470         ovsdb_row_destroy(row);
471     }
472 }
473
474 void
475 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
476 {
477     if (txn->comment.length) {
478         ds_put_char(&txn->comment, '\n');
479     }
480     ds_put_cstr(&txn->comment, s);
481 }
482
483 const char *
484 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
485 {
486     return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
487 }
488 \f
489 static void
490 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
491 {
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;
494
495     txn_table->n_processed--;
496     hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
497
498     if (txn_row->old) {
499         txn_row->old->txn_row = NULL;
500     }
501     if (txn_row->new) {
502         txn_row->new->txn_row = NULL;
503     }
504 }
505
506 static void
507 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
508 {
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);
513     free(txn_table);
514 }
515
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.
519  *
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'.
526  */
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 *))
531 {
532     bool any_work;
533
534     serial++;
535
536     do {
537         struct ovsdb_txn_table *t, *next_txn_table;
538
539         any_work = false;
540         LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
541                             &txn->txn_tables) {
542             if (t->serial != serial) {
543                 t->serial = serial;
544                 t->n_processed = 0;
545             }
546
547             while (t->n_processed < hmap_count(&t->txn_rows)) {
548                 struct ovsdb_txn_row *r, *next_txn_row;
549
550                 HMAP_FOR_EACH_SAFE (r, next_txn_row,
551                                     struct ovsdb_txn_row, hmap_node,
552                                     &t->txn_rows) {
553                     if (r->serial != serial) {
554                         struct ovsdb_error *error;
555
556                         r->serial = serial;
557                         t->n_processed++;
558                         any_work = true;
559
560                         error = cb(txn, r);
561                         if (error) {
562                             return error;
563                         }
564                     }
565                 }
566             }
567             if (hmap_is_empty(&t->txn_rows)) {
568                 /* Table is empty.  Drop it. */
569                 ovsdb_txn_table_destroy(t);
570             }
571         }
572     } while (any_work);
573
574     return NULL;
575 }