Merge "master" branch into "db".
[cascardo/ovs.git] / ovsdb / transaction.c
1 /* Copyright (c) 2009 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 "hash.h"
23 #include "hmap.h"
24 #include "json.h"
25 #include "ovsdb-error.h"
26 #include "ovsdb.h"
27 #include "row.h"
28 #include "table.h"
29 #include "uuid.h"
30
31 struct ovsdb_txn {
32     struct ovsdb *db;
33     struct hmap txn_tables;     /* Contains "struct ovsdb_txn_table"s. */
34 };
35
36 /* A table modified by a transaction. */
37 struct ovsdb_txn_table {
38     struct hmap_node hmap_node; /* Element in ovsdb_txn's txn_tables hmap. */
39     struct ovsdb_table *table;
40     struct hmap txn_rows;       /* Contains "struct ovsdb_txn_row"s. */
41 };
42
43 /* A row modified by the transaction:
44  *
45  *      - A row added by a transaction will have null 'old' and non-null 'new'.
46  *
47  *      - A row deleted by a transaction will have non-null 'old' and null
48  *        'new'.
49  *
50  *      - A row modified by a transaction will have non-null 'old' and 'new'.
51  *
52  *      - 'old' and 'new' both null is invalid.  It would indicate that a row
53  *        was added then deleted within a single transaction, but we instead
54  *        handle that case by deleting the txn_row entirely.
55  */
56 struct ovsdb_txn_row {
57     struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
58     struct ovsdb_row *old;      /* The old row. */
59     struct ovsdb_row *new;      /* The new row. */
60 };
61
62 struct ovsdb_txn *
63 ovsdb_txn_create(struct ovsdb *db)
64 {
65     struct ovsdb_txn *txn = xmalloc(sizeof *txn);
66     txn->db = db;
67     hmap_init(&txn->txn_tables);
68     return txn;
69 }
70
71 static void
72 ovsdb_txn_destroy(struct ovsdb_txn *txn, void (*cb)(struct ovsdb_txn_row *))
73 {
74     struct ovsdb_txn_table *txn_table, *next_txn_table;
75
76     HMAP_FOR_EACH_SAFE (txn_table, next_txn_table,
77                         struct ovsdb_txn_table, hmap_node, &txn->txn_tables)
78     {
79         struct ovsdb_txn_row *txn_row, *next_txn_row;
80
81         HMAP_FOR_EACH_SAFE (txn_row, next_txn_row,
82                             struct ovsdb_txn_row, hmap_node,
83                             &txn_table->txn_rows)
84         {
85             if (txn_row->new) {
86                 txn_row->new->txn_row = NULL;
87             }
88             cb(txn_row);
89             free(txn_row);
90         }
91
92         hmap_destroy(&txn_table->txn_rows);
93         free(txn_table);
94     }
95     hmap_destroy(&txn->txn_tables);
96     free(txn);
97 }
98
99 static void
100 ovsdb_txn_row_abort(struct ovsdb_txn_row *txn_row)
101 {
102     struct ovsdb_row *old = txn_row->old;
103     struct ovsdb_row *new = txn_row->new;
104
105     if (!old) {
106         hmap_remove(&new->table->rows, &new->hmap_node);
107     } else if (!new) {
108         hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
109     } else {
110         hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
111     }
112     ovsdb_row_destroy(new);
113 }
114
115 void
116 ovsdb_txn_abort(struct ovsdb_txn *txn)
117 {
118     ovsdb_txn_destroy(txn, ovsdb_txn_row_abort);
119 }
120
121 static void
122 ovsdb_txn_row_commit(struct ovsdb_txn_row *txn_row)
123 {
124     ovsdb_row_destroy(txn_row->old);
125 }
126
127 struct ovsdb_error *
128 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
129 {
130     struct ovsdb_replica *replica;
131     struct ovsdb_error *error;
132
133     LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
134         error = (replica->class->commit)(replica, txn, durable);
135         if (error) {
136             /* We don't support two-phase commit so only the first replica is
137              * allowed to report an error. */
138             assert(&replica->node == txn->db->replicas.next);
139
140             ovsdb_txn_abort(txn);
141             return error;
142         }
143     }
144
145     txn->db->run_triggers = true;
146     ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
147     return NULL;
148 }
149
150 void
151 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
152                           ovsdb_txn_row_cb_func *cb, void *aux)
153 {
154     struct ovsdb_txn_table *t;
155     struct ovsdb_txn_row *r;
156
157     HMAP_FOR_EACH (t, struct ovsdb_txn_table, hmap_node, &txn->txn_tables) {
158         HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
159             if (!cb(r->old, r->new, aux)) {
160                 break;
161             }
162         }
163    }
164 }
165
166 static struct ovsdb_txn_table *
167 ovsdb_txn_get_txn_table__(struct ovsdb_txn *txn,
168                           const struct ovsdb_table *table,
169                           uint32_t hash)
170 {
171     struct ovsdb_txn_table *txn_table;
172
173     HMAP_FOR_EACH_IN_BUCKET (txn_table, struct ovsdb_txn_table, hmap_node,
174                              hash, &txn->txn_tables) {
175         if (txn_table->table == table) {
176             return txn_table;
177         }
178     }
179
180     return NULL;
181 }
182
183 static struct ovsdb_txn_table *
184 ovsdb_txn_get_txn_table(struct ovsdb_txn *txn, const struct ovsdb_table *table)
185 {
186     return ovsdb_txn_get_txn_table__(txn, table, hash_pointer(table, 0));
187 }
188
189 static struct ovsdb_txn_table *
190 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn,
191                            struct ovsdb_table *table)
192 {
193     uint32_t hash = hash_pointer(table, 0);
194     struct ovsdb_txn_table *txn_table;
195
196     txn_table = ovsdb_txn_get_txn_table__(txn, table, hash);
197     if (!txn_table) {
198         txn_table = xmalloc(sizeof *txn_table);
199         txn_table->table = table;
200         hmap_init(&txn_table->txn_rows);
201         hmap_insert(&txn->txn_tables, &txn_table->hmap_node, hash);
202     }
203     return txn_table;
204 }
205
206 static struct ovsdb_txn_row *
207 ovsdb_txn_row_create(struct ovsdb_txn_table *txn_table,
208                      const struct ovsdb_row *old, struct ovsdb_row *new)
209 {
210     uint32_t hash = ovsdb_row_hash(old ? old : new);
211     struct ovsdb_txn_row *txn_row;
212
213     txn_row = xmalloc(sizeof *txn_row);
214     txn_row->old = (struct ovsdb_row *) old;
215     txn_row->new = new;
216     hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node, hash);
217
218     return txn_row;
219 }
220
221 struct ovsdb_row *
222 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
223 {
224     struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
225
226     if (ro_row->txn_row) {
227         assert(ro_row == ro_row->txn_row->new);
228         return ro_row;
229     } else {
230         struct ovsdb_table *table = ro_row->table;
231         struct ovsdb_txn_table *txn_table;
232         struct ovsdb_row *rw_row;
233
234         txn_table = ovsdb_txn_create_txn_table(txn, table);
235         rw_row = ovsdb_row_clone(ro_row);
236         uuid_generate(ovsdb_row_get_version_rw(rw_row));
237         rw_row->txn_row = ovsdb_txn_row_create(txn_table, ro_row, rw_row);
238         hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
239
240         return rw_row;
241     }
242 }
243
244 void
245 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
246 {
247     uint32_t hash = ovsdb_row_hash(row);
248     struct ovsdb_table *table = row->table;
249     struct ovsdb_txn_table *txn_table;
250
251     uuid_generate(ovsdb_row_get_version_rw(row));
252
253     txn_table = ovsdb_txn_create_txn_table(txn, table);
254     row->txn_row = ovsdb_txn_row_create(txn_table, NULL, row);
255     hmap_insert(&table->rows, &row->hmap_node, hash);
256 }
257
258 /* 'row' must be assumed destroyed upon return; the caller must not reference
259  * it again. */
260 void
261 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
262 {
263     struct ovsdb_row *row = (struct ovsdb_row *) row_;
264     struct ovsdb_table *table = row->table;
265     struct ovsdb_txn_row *txn_row = row->txn_row;
266     struct ovsdb_txn_table *txn_table;
267
268     hmap_remove(&table->rows, &row->hmap_node);
269
270     if (!txn_row) {
271         txn_table = ovsdb_txn_create_txn_table(txn, table);
272         row->txn_row = ovsdb_txn_row_create(txn_table, row, NULL);
273     } else {
274         assert(txn_row->new == row);
275         if (txn_row->old) {
276             txn_row->new = NULL;
277         } else {
278             txn_table = ovsdb_txn_get_txn_table(txn, table);
279             hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
280         }
281         ovsdb_row_destroy(row);
282     }
283 }