ovsdb: Cleanly abort delete operations.
[sliver-openvswitch.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->old) {
86                 txn_row->old->txn_row = NULL;
87             }
88             if (txn_row->new) {
89                 txn_row->new->txn_row = NULL;
90             }
91             cb(txn_row);
92             free(txn_row);
93         }
94
95         hmap_destroy(&txn_table->txn_rows);
96         free(txn_table);
97     }
98     hmap_destroy(&txn->txn_tables);
99     free(txn);
100 }
101
102 static void
103 ovsdb_txn_row_abort(struct ovsdb_txn_row *txn_row)
104 {
105     struct ovsdb_row *old = txn_row->old;
106     struct ovsdb_row *new = txn_row->new;
107
108     if (!old) {
109         hmap_remove(&new->table->rows, &new->hmap_node);
110     } else if (!new) {
111         hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
112     } else {
113         hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
114     }
115     ovsdb_row_destroy(new);
116 }
117
118 void
119 ovsdb_txn_abort(struct ovsdb_txn *txn)
120 {
121     ovsdb_txn_destroy(txn, ovsdb_txn_row_abort);
122 }
123
124 static void
125 ovsdb_txn_row_commit(struct ovsdb_txn_row *txn_row)
126 {
127     ovsdb_row_destroy(txn_row->old);
128 }
129
130 struct ovsdb_error *
131 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
132 {
133     struct ovsdb_replica *replica;
134     struct ovsdb_error *error;
135
136     LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
137         error = (replica->class->commit)(replica, txn, durable);
138         if (error) {
139             /* We don't support two-phase commit so only the first replica is
140              * allowed to report an error. */
141             assert(&replica->node == txn->db->replicas.next);
142
143             ovsdb_txn_abort(txn);
144             return error;
145         }
146     }
147
148     txn->db->run_triggers = true;
149     ovsdb_txn_destroy(txn, ovsdb_txn_row_commit);
150     return NULL;
151 }
152
153 void
154 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
155                           ovsdb_txn_row_cb_func *cb, void *aux)
156 {
157     struct ovsdb_txn_table *t;
158     struct ovsdb_txn_row *r;
159
160     HMAP_FOR_EACH (t, struct ovsdb_txn_table, hmap_node, &txn->txn_tables) {
161         HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
162             if (!cb(r->old, r->new, aux)) {
163                 break;
164             }
165         }
166    }
167 }
168
169 static struct ovsdb_txn_table *
170 ovsdb_txn_get_txn_table__(struct ovsdb_txn *txn,
171                           const struct ovsdb_table *table,
172                           uint32_t hash)
173 {
174     struct ovsdb_txn_table *txn_table;
175
176     HMAP_FOR_EACH_IN_BUCKET (txn_table, struct ovsdb_txn_table, hmap_node,
177                              hash, &txn->txn_tables) {
178         if (txn_table->table == table) {
179             return txn_table;
180         }
181     }
182
183     return NULL;
184 }
185
186 static struct ovsdb_txn_table *
187 ovsdb_txn_get_txn_table(struct ovsdb_txn *txn, const struct ovsdb_table *table)
188 {
189     return ovsdb_txn_get_txn_table__(txn, table, hash_pointer(table, 0));
190 }
191
192 static struct ovsdb_txn_table *
193 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn,
194                            struct ovsdb_table *table)
195 {
196     uint32_t hash = hash_pointer(table, 0);
197     struct ovsdb_txn_table *txn_table;
198
199     txn_table = ovsdb_txn_get_txn_table__(txn, table, hash);
200     if (!txn_table) {
201         txn_table = xmalloc(sizeof *txn_table);
202         txn_table->table = table;
203         hmap_init(&txn_table->txn_rows);
204         hmap_insert(&txn->txn_tables, &txn_table->hmap_node, hash);
205     }
206     return txn_table;
207 }
208
209 static struct ovsdb_txn_row *
210 ovsdb_txn_row_create(struct ovsdb_txn_table *txn_table,
211                      const struct ovsdb_row *old, struct ovsdb_row *new)
212 {
213     uint32_t hash = ovsdb_row_hash(old ? old : new);
214     struct ovsdb_txn_row *txn_row;
215
216     txn_row = xmalloc(sizeof *txn_row);
217     txn_row->old = (struct ovsdb_row *) old;
218     txn_row->new = new;
219     hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node, hash);
220
221     return txn_row;
222 }
223
224 struct ovsdb_row *
225 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
226 {
227     struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
228
229     if (ro_row->txn_row) {
230         assert(ro_row == ro_row->txn_row->new);
231         return ro_row;
232     } else {
233         struct ovsdb_table *table = ro_row->table;
234         struct ovsdb_txn_table *txn_table;
235         struct ovsdb_row *rw_row;
236
237         txn_table = ovsdb_txn_create_txn_table(txn, table);
238         rw_row = ovsdb_row_clone(ro_row);
239         uuid_generate(ovsdb_row_get_version_rw(rw_row));
240         rw_row->txn_row = ovsdb_txn_row_create(txn_table, ro_row, rw_row);
241         hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
242
243         return rw_row;
244     }
245 }
246
247 void
248 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
249 {
250     uint32_t hash = ovsdb_row_hash(row);
251     struct ovsdb_table *table = row->table;
252     struct ovsdb_txn_table *txn_table;
253
254     uuid_generate(ovsdb_row_get_version_rw(row));
255
256     txn_table = ovsdb_txn_create_txn_table(txn, table);
257     row->txn_row = ovsdb_txn_row_create(txn_table, NULL, row);
258     hmap_insert(&table->rows, &row->hmap_node, hash);
259 }
260
261 /* 'row' must be assumed destroyed upon return; the caller must not reference
262  * it again. */
263 void
264 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
265 {
266     struct ovsdb_row *row = (struct ovsdb_row *) row_;
267     struct ovsdb_table *table = row->table;
268     struct ovsdb_txn_row *txn_row = row->txn_row;
269     struct ovsdb_txn_table *txn_table;
270
271     hmap_remove(&table->rows, &row->hmap_node);
272
273     if (!txn_row) {
274         txn_table = ovsdb_txn_create_txn_table(txn, table);
275         row->txn_row = ovsdb_txn_row_create(txn_table, row, NULL);
276     } else {
277         assert(txn_row->new == row);
278         if (txn_row->old) {
279             txn_row->new = NULL;
280         } else {
281             txn_table = ovsdb_txn_get_txn_table(txn, table);
282             hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
283         }
284         ovsdb_row_destroy(row);
285     }
286 }