ovsdb: Add replication support and refactor files in terms of replication.
[cascardo/ovs.git] / ovsdb / execution.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 <assert.h>
19 #include <limits.h>
20
21 #include "column.h"
22 #include "condition.h"
23 #include "file.h"
24 #include "json.h"
25 #include "ovsdb-data.h"
26 #include "ovsdb-error.h"
27 #include "ovsdb-parser.h"
28 #include "ovsdb.h"
29 #include "query.h"
30 #include "row.h"
31 #include "table.h"
32 #include "timeval.h"
33 #include "transaction.h"
34
35 struct ovsdb_execution {
36     struct ovsdb *db;
37     struct ovsdb_txn *txn;
38     struct ovsdb_symbol_table *symtab;
39     bool durable;
40
41     /* Triggers. */
42     long long int elapsed_msec;
43     long long int timeout_msec;
44 };
45
46 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
47                                                      struct ovsdb_parser *,
48                                                      struct json *result);
49
50 static ovsdb_operation_executor ovsdb_execute_insert;
51 static ovsdb_operation_executor ovsdb_execute_select;
52 static ovsdb_operation_executor ovsdb_execute_update;
53 static ovsdb_operation_executor ovsdb_execute_delete;
54 static ovsdb_operation_executor ovsdb_execute_wait;
55 static ovsdb_operation_executor ovsdb_execute_commit;
56 static ovsdb_operation_executor ovsdb_execute_abort;
57
58 static ovsdb_operation_executor *
59 lookup_executor(const char *name)
60 {
61     struct ovsdb_operation {
62         const char *name;
63         ovsdb_operation_executor *executor;
64     };
65
66     static const struct ovsdb_operation operations[] = {
67         { "insert", ovsdb_execute_insert },
68         { "select", ovsdb_execute_select },
69         { "update", ovsdb_execute_update },
70         { "delete", ovsdb_execute_delete },
71         { "wait", ovsdb_execute_wait },
72         { "commit", ovsdb_execute_commit },
73         { "abort", ovsdb_execute_abort },
74     };
75
76     size_t i;
77
78     for (i = 0; i < ARRAY_SIZE(operations); i++) {
79         const struct ovsdb_operation *c = &operations[i];
80         if (!strcmp(c->name, name)) {
81             return c->executor;
82         }
83     }
84     return NULL;
85 }
86
87 struct json *
88 ovsdb_execute(struct ovsdb *db, const struct json *params,
89               long long int elapsed_msec, long long int *timeout_msec)
90 {
91     struct ovsdb_execution x;
92     struct ovsdb_error *error;
93     struct json *results;
94     size_t n_operations;
95     size_t i;
96
97     if (params->type != JSON_ARRAY) {
98         struct ovsdb_error *error;
99
100         error = ovsdb_syntax_error(params, NULL, "array expected");
101         results = ovsdb_error_to_json(error);
102         ovsdb_error_destroy(error);
103         return results;
104     }
105
106     x.db = db;
107     x.txn = ovsdb_txn_create(db);
108     x.symtab = ovsdb_symbol_table_create();
109     x.durable = false;
110     x.elapsed_msec = elapsed_msec;
111     x.timeout_msec = LLONG_MAX;
112     results = NULL;
113
114     results = json_array_create_empty();
115     n_operations = params->u.array.n;
116     error = NULL;
117     for (i = 0; i < n_operations; i++) {
118         struct json *operation = params->u.array.elems[i];
119         struct ovsdb_error *parse_error;
120         struct ovsdb_parser parser;
121         struct json *result;
122         const struct json *op;
123
124         /* Parse and execute operation. */
125         ovsdb_parser_init(&parser, operation,
126                           "ovsdb operation %zu of %zu", i + 1, n_operations);
127         op = ovsdb_parser_member(&parser, "op", OP_ID);
128         result = json_object_create();
129         if (op) {
130             const char *op_name = json_string(op);
131             ovsdb_operation_executor *executor = lookup_executor(op_name);
132             if (executor) {
133                 error = executor(&x, &parser, result);
134             } else {
135                 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
136                                          op_name);
137             }
138         } else {
139             assert(ovsdb_parser_has_error(&parser));
140         }
141
142         /* A parse error overrides any other error.
143          * An error overrides any other result. */
144         parse_error = ovsdb_parser_finish(&parser);
145         if (parse_error) {
146             ovsdb_error_destroy(error);
147             error = parse_error;
148         }
149         if (error) {
150             json_destroy(result);
151             result = ovsdb_error_to_json(error);
152         }
153         if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
154             && timeout_msec) {
155             ovsdb_txn_abort(x.txn);
156             *timeout_msec = x.timeout_msec;
157             ovsdb_error_destroy(error);
158             json_destroy(results);
159             return NULL;
160         }
161
162         /* Add result to array. */
163         json_array_add(results, result);
164         if (error) {
165             break;
166         }
167     }
168
169     if (!error) {
170         error = ovsdb_txn_commit(x.txn, x.durable);
171         if (error) {
172             json_array_add(results, ovsdb_error_to_json(error));
173         }
174     } else {
175         ovsdb_txn_abort(x.txn);
176     }
177
178     while (json_array(results)->n < n_operations) {
179         json_array_add(results, json_null_create());
180     }
181
182     ovsdb_error_destroy(error);
183     ovsdb_symbol_table_destroy(x.symtab);
184
185     return results;
186 }
187
188 struct ovsdb_error *
189 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
190                      struct json *result UNUSED)
191 {
192     const struct json *durable;
193
194     durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
195     if (durable && json_boolean(durable)) {
196         x->durable = true;
197     }
198     return NULL;
199 }
200
201 static struct ovsdb_error *
202 ovsdb_execute_abort(struct ovsdb_execution *x UNUSED,
203                     struct ovsdb_parser *parser UNUSED,
204                     struct json *result UNUSED)
205 {
206     return ovsdb_error("aborted", "aborted by request");
207 }
208
209 static struct ovsdb_table *
210 parse_table(struct ovsdb_execution *x,
211             struct ovsdb_parser *parser, const char *member)
212 {
213     struct ovsdb_table *table;
214     const char *table_name;
215     const struct json *json;
216
217     json = ovsdb_parser_member(parser, member, OP_ID);
218     if (!json) {
219         return NULL;
220     }
221     table_name = json_string(json);
222
223     table = shash_find_data(&x->db->tables, table_name);
224     if (!table) {
225         ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
226     }
227     return table;
228 }
229
230 static WARN_UNUSED_RESULT struct ovsdb_error *
231 parse_row(struct ovsdb_parser *parser, const char *member,
232           const struct ovsdb_table *table,
233           const struct ovsdb_symbol_table *symtab,
234           struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
235 {
236     struct ovsdb_error *error;
237     const struct json *json;
238     struct ovsdb_row *row;
239
240     *rowp = NULL;
241
242     if (!table) {
243         return OVSDB_BUG("null table");
244     }
245     json = ovsdb_parser_member(parser, member, OP_OBJECT);
246     if (!json) {
247         return OVSDB_BUG("null row member");
248     }
249
250     row = ovsdb_row_create(table);
251     error = ovsdb_row_from_json(row, json, symtab, columns);
252     if (error) {
253         ovsdb_row_destroy(row);
254         return error;
255     } else {
256         *rowp = row;
257         return NULL;
258     }
259 }
260
261 struct ovsdb_error *
262 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
263                      struct json *result)
264 {
265     struct ovsdb_table *table;
266     struct ovsdb_row *row = NULL;
267     const struct json *uuid_name;
268     struct ovsdb_error *error;
269
270     table = parse_table(x, parser, "table");
271     uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
272     error = ovsdb_parser_get_error(parser);
273     if (!error) {
274         error = parse_row(parser, "row", table, x->symtab, &row, NULL);
275     }
276     if (!error) {
277         uuid_generate(ovsdb_row_get_uuid_rw(row));
278         if (uuid_name) {
279             ovsdb_symbol_table_put(x->symtab, json_string(uuid_name),
280                                    ovsdb_row_get_uuid(row));
281         }
282         ovsdb_txn_row_insert(x->txn, row);
283         json_object_put(result, "uuid",
284                         ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
285                                             &ovsdb_type_uuid));
286         row = NULL;
287     }
288     return error;
289 }
290
291 struct ovsdb_error *
292 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
293                      struct json *result)
294 {
295     struct ovsdb_table *table;
296     const struct json *where, *columns_json, *sort_json;
297     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
298     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
299     struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
300     struct ovsdb_error *error;
301
302     table = parse_table(x, parser, "table");
303     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
304     columns_json = ovsdb_parser_member(parser, "columns",
305                                        OP_ARRAY | OP_OPTIONAL);
306     sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
307
308     error = ovsdb_parser_get_error(parser);
309     if (!error) {
310         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
311                                           &condition);
312     }
313     if (!error) {
314         error = ovsdb_column_set_from_json(columns_json, table, &columns);
315     }
316     if (!error) {
317         error = ovsdb_column_set_from_json(sort_json, table, &sort);
318     }
319     if (!error) {
320         struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
321
322         ovsdb_query_distinct(table, &condition, &columns, &rows);
323         ovsdb_row_set_sort(&rows, &sort);
324         json_object_put(result, "rows",
325                         ovsdb_row_set_to_json(&rows, &columns));
326
327         ovsdb_row_set_destroy(&rows);
328     }
329
330     ovsdb_column_set_destroy(&columns);
331     ovsdb_column_set_destroy(&sort);
332     ovsdb_condition_destroy(&condition);
333
334     return error;
335 }
336
337 struct update_row_cbdata {
338     size_t n_matches;
339     struct ovsdb_txn *txn;
340     const struct ovsdb_row *row;
341     const struct ovsdb_column_set *columns;
342 };
343
344 static bool
345 update_row_cb(const struct ovsdb_row *row, void *ur_)
346 {
347     struct update_row_cbdata *ur = ur_;
348
349     ur->n_matches++;
350     if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
351         ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
352                                  ur->row, ur->columns);
353     }
354
355     return true;
356 }
357
358 struct ovsdb_error *
359 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
360                      struct json *result)
361 {
362     struct ovsdb_table *table;
363     const struct json *where;
364     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
365     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
366     struct ovsdb_row *row = NULL;
367     struct update_row_cbdata ur;
368     struct ovsdb_error *error;
369
370     table = parse_table(x, parser, "table");
371     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
372     error = ovsdb_parser_get_error(parser);
373     if (!error) {
374         error = parse_row(parser, "row", table, x->symtab, &row, &columns);
375     }
376     if (!error) {
377         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
378                                           &condition);
379     }
380     if (!error) {
381         ur.n_matches = 0;
382         ur.txn = x->txn;
383         ur.row = row;
384         ur.columns = &columns;
385         ovsdb_query(table, &condition, update_row_cb, &ur);
386         json_object_put(result, "count", json_integer_create(ur.n_matches));
387     }
388
389     ovsdb_row_destroy(row);
390     ovsdb_column_set_destroy(&columns);
391     ovsdb_condition_destroy(&condition);
392
393     return error;
394 }
395
396 struct delete_row_cbdata {
397     size_t n_matches;
398     const struct ovsdb_table *table;
399     struct ovsdb_txn *txn;
400 };
401
402 static bool
403 delete_row_cb(const struct ovsdb_row *row, void *dr_)
404 {
405     struct delete_row_cbdata *dr = dr_;
406
407     dr->n_matches++;
408     ovsdb_txn_row_delete(dr->txn, row);
409
410     return true;
411 }
412
413 struct ovsdb_error *
414 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
415                      struct json *result)
416 {
417     struct ovsdb_table *table;
418     const struct json *where;
419     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
420     struct ovsdb_error *error;
421
422     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
423     table = parse_table(x, parser, "table");
424     error = ovsdb_parser_get_error(parser);
425     if (!error) {
426         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
427                                           &condition);
428     }
429     if (!error) {
430         struct delete_row_cbdata dr;
431
432         dr.n_matches = 0;
433         dr.table = table;
434         dr.txn = x->txn;
435         ovsdb_query(table, &condition, delete_row_cb, &dr);
436
437         json_object_put(result, "count", json_integer_create(dr.n_matches));
438     }
439
440     ovsdb_condition_destroy(&condition);
441
442     return error;
443 }
444
445 struct wait_auxdata {
446     struct ovsdb_row_hash *actual;
447     struct ovsdb_row_hash *expected;
448     bool *equal;
449 };
450
451 static bool
452 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
453 {
454     struct wait_auxdata *aux = aux_;
455
456     if (ovsdb_row_hash_contains(aux->expected, row)) {
457         ovsdb_row_hash_insert(aux->actual, row);
458         return true;
459     } else {
460         /* The query row isn't in the expected result set, so the actual and
461          * expected results sets definitely differ and we can short-circuit the
462          * rest of the query. */
463         *aux->equal = false;
464         return false;
465     }
466 }
467
468 static struct ovsdb_error *
469 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
470                    struct json *result UNUSED)
471 {
472     struct ovsdb_table *table;
473     const struct json *timeout, *where, *columns_json, *until, *rows;
474     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
475     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
476     struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
477     struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
478     struct ovsdb_error *error;
479     struct wait_auxdata aux;
480     long long int timeout_msec = 0;
481     size_t i;
482
483     timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
484     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
485     columns_json = ovsdb_parser_member(parser, "columns",
486                                        OP_ARRAY | OP_OPTIONAL);
487     until = ovsdb_parser_member(parser, "until", OP_STRING);
488     rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
489     table = parse_table(x, parser, "table");
490     error = ovsdb_parser_get_error(parser);
491     if (!error) {
492         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
493                                           &condition);
494     }
495     if (!error) {
496         error = ovsdb_column_set_from_json(columns_json, table, &columns);
497     }
498     if (!error) {
499         if (timeout) {
500             timeout_msec = MIN(LLONG_MAX, json_real(timeout));
501             if (timeout_msec < 0) {
502                 error = ovsdb_syntax_error(timeout, NULL,
503                                            "timeout must be nonnegative");
504             } else if (timeout_msec < x->timeout_msec) {
505                 x->timeout_msec = timeout_msec;
506             }
507         } else {
508             timeout_msec = LLONG_MAX;
509         }
510         if (strcmp(json_string(until), "==")
511             && strcmp(json_string(until), "!=")) {
512             error = ovsdb_syntax_error(until, NULL,
513                                        "\"until\" must be \"==\" or \"!=\"");
514         }
515     }
516     if (!error) {
517         /* Parse "rows" into 'expected'. */
518         ovsdb_row_hash_init(&expected, &columns);
519         for (i = 0; i < rows->u.array.n; i++) {
520             struct ovsdb_error *error;
521             struct ovsdb_row *row;
522
523             row = ovsdb_row_create(table);
524             error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
525                                         NULL);
526             if (error) {
527                 break;
528             }
529
530             if (!ovsdb_row_hash_insert(&expected, row)) {
531                 /* XXX Perhaps we should abort with an error or log a
532                  * warning. */
533                 ovsdb_row_destroy(row);
534             }
535         }
536     }
537     if (!error) {
538         /* Execute query. */
539         bool equal = true;
540         ovsdb_row_hash_init(&actual, &columns);
541         aux.actual = &actual;
542         aux.expected = &expected;
543         aux.equal = &equal;
544         ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
545         if (equal) {
546             /* We know that every row in 'actual' is also in 'expected'.  We
547              * also know that all of the rows in 'actual' are distinct and that
548              * all of the rows in 'expected' are distinct.  Therefore, if
549              * 'actual' and 'expected' have the same number of rows, then they
550              * have the same content. */
551             size_t n_actual = ovsdb_row_hash_count(&actual);
552             size_t n_expected = ovsdb_row_hash_count(&expected);
553             equal = n_actual == n_expected;
554         }
555         if (!strcmp(json_string(until), "==") != equal) {
556             if (timeout && x->elapsed_msec >= timeout_msec) {
557                 if (x->elapsed_msec) {
558                     error = ovsdb_error("timed out",
559                                         "\"wait\" timed out after %lld ms",
560                                         x->elapsed_msec);
561                 } else {
562                     error = ovsdb_error("timed out", "\"wait\" timed out");
563                 }
564             } else {
565                 /* ovsdb_execute() will change this, if triggers really are
566                  * supported. */
567                 error = ovsdb_error("not supported", "triggers not supported");
568             }
569         }
570     }
571
572
573     ovsdb_row_hash_destroy(&expected, true);
574     ovsdb_row_hash_destroy(&actual, false);
575     ovsdb_column_set_destroy(&columns);
576     ovsdb_condition_destroy(&condition);
577
578     return error;
579 }