Merge "master" branch into "db".
[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     struct uuid row_uuid;
270
271     table = parse_table(x, parser, "table");
272     uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
273     error = ovsdb_parser_get_error(parser);
274
275     uuid_generate(&row_uuid);
276     if (uuid_name) {
277         ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &row_uuid);
278     }
279
280     if (!error) {
281         error = parse_row(parser, "row", table, x->symtab, &row, NULL);
282     }
283     if (!error) {
284         *ovsdb_row_get_uuid_rw(row) = row_uuid;
285         ovsdb_txn_row_insert(x->txn, row);
286         json_object_put(result, "uuid",
287                         ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
288                                             &ovsdb_type_uuid));
289         row = NULL;
290     }
291     return error;
292 }
293
294 struct ovsdb_error *
295 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
296                      struct json *result)
297 {
298     struct ovsdb_table *table;
299     const struct json *where, *columns_json, *sort_json;
300     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
301     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
302     struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
303     struct ovsdb_error *error;
304
305     table = parse_table(x, parser, "table");
306     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
307     columns_json = ovsdb_parser_member(parser, "columns",
308                                        OP_ARRAY | OP_OPTIONAL);
309     sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
310
311     error = ovsdb_parser_get_error(parser);
312     if (!error) {
313         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
314                                           &condition);
315     }
316     if (!error) {
317         error = ovsdb_column_set_from_json(columns_json, table, &columns);
318     }
319     if (!error) {
320         error = ovsdb_column_set_from_json(sort_json, table, &sort);
321     }
322     if (!error) {
323         struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
324
325         ovsdb_query_distinct(table, &condition, &columns, &rows);
326         ovsdb_row_set_sort(&rows, &sort);
327         json_object_put(result, "rows",
328                         ovsdb_row_set_to_json(&rows, &columns));
329
330         ovsdb_row_set_destroy(&rows);
331     }
332
333     ovsdb_column_set_destroy(&columns);
334     ovsdb_column_set_destroy(&sort);
335     ovsdb_condition_destroy(&condition);
336
337     return error;
338 }
339
340 struct update_row_cbdata {
341     size_t n_matches;
342     struct ovsdb_txn *txn;
343     const struct ovsdb_row *row;
344     const struct ovsdb_column_set *columns;
345 };
346
347 static bool
348 update_row_cb(const struct ovsdb_row *row, void *ur_)
349 {
350     struct update_row_cbdata *ur = ur_;
351
352     ur->n_matches++;
353     if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
354         ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
355                                  ur->row, ur->columns);
356     }
357
358     return true;
359 }
360
361 struct ovsdb_error *
362 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
363                      struct json *result)
364 {
365     struct ovsdb_table *table;
366     const struct json *where;
367     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
368     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
369     struct ovsdb_row *row = NULL;
370     struct update_row_cbdata ur;
371     struct ovsdb_error *error;
372
373     table = parse_table(x, parser, "table");
374     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
375     error = ovsdb_parser_get_error(parser);
376     if (!error) {
377         error = parse_row(parser, "row", table, x->symtab, &row, &columns);
378     }
379     if (!error) {
380         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
381                                           &condition);
382     }
383     if (!error) {
384         ur.n_matches = 0;
385         ur.txn = x->txn;
386         ur.row = row;
387         ur.columns = &columns;
388         ovsdb_query(table, &condition, update_row_cb, &ur);
389         json_object_put(result, "count", json_integer_create(ur.n_matches));
390     }
391
392     ovsdb_row_destroy(row);
393     ovsdb_column_set_destroy(&columns);
394     ovsdb_condition_destroy(&condition);
395
396     return error;
397 }
398
399 struct delete_row_cbdata {
400     size_t n_matches;
401     const struct ovsdb_table *table;
402     struct ovsdb_txn *txn;
403 };
404
405 static bool
406 delete_row_cb(const struct ovsdb_row *row, void *dr_)
407 {
408     struct delete_row_cbdata *dr = dr_;
409
410     dr->n_matches++;
411     ovsdb_txn_row_delete(dr->txn, row);
412
413     return true;
414 }
415
416 struct ovsdb_error *
417 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
418                      struct json *result)
419 {
420     struct ovsdb_table *table;
421     const struct json *where;
422     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
423     struct ovsdb_error *error;
424
425     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
426     table = parse_table(x, parser, "table");
427     error = ovsdb_parser_get_error(parser);
428     if (!error) {
429         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
430                                           &condition);
431     }
432     if (!error) {
433         struct delete_row_cbdata dr;
434
435         dr.n_matches = 0;
436         dr.table = table;
437         dr.txn = x->txn;
438         ovsdb_query(table, &condition, delete_row_cb, &dr);
439
440         json_object_put(result, "count", json_integer_create(dr.n_matches));
441     }
442
443     ovsdb_condition_destroy(&condition);
444
445     return error;
446 }
447
448 struct wait_auxdata {
449     struct ovsdb_row_hash *actual;
450     struct ovsdb_row_hash *expected;
451     bool *equal;
452 };
453
454 static bool
455 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
456 {
457     struct wait_auxdata *aux = aux_;
458
459     if (ovsdb_row_hash_contains(aux->expected, row)) {
460         ovsdb_row_hash_insert(aux->actual, row);
461         return true;
462     } else {
463         /* The query row isn't in the expected result set, so the actual and
464          * expected results sets definitely differ and we can short-circuit the
465          * rest of the query. */
466         *aux->equal = false;
467         return false;
468     }
469 }
470
471 static struct ovsdb_error *
472 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
473                    struct json *result UNUSED)
474 {
475     struct ovsdb_table *table;
476     const struct json *timeout, *where, *columns_json, *until, *rows;
477     struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
478     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
479     struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
480     struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
481     struct ovsdb_error *error;
482     struct wait_auxdata aux;
483     long long int timeout_msec = 0;
484     size_t i;
485
486     timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
487     where = ovsdb_parser_member(parser, "where", OP_ARRAY);
488     columns_json = ovsdb_parser_member(parser, "columns",
489                                        OP_ARRAY | OP_OPTIONAL);
490     until = ovsdb_parser_member(parser, "until", OP_STRING);
491     rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
492     table = parse_table(x, parser, "table");
493     error = ovsdb_parser_get_error(parser);
494     if (!error) {
495         error = ovsdb_condition_from_json(table->schema, where, x->symtab,
496                                           &condition);
497     }
498     if (!error) {
499         error = ovsdb_column_set_from_json(columns_json, table, &columns);
500     }
501     if (!error) {
502         if (timeout) {
503             timeout_msec = MIN(LLONG_MAX, json_real(timeout));
504             if (timeout_msec < 0) {
505                 error = ovsdb_syntax_error(timeout, NULL,
506                                            "timeout must be nonnegative");
507             } else if (timeout_msec < x->timeout_msec) {
508                 x->timeout_msec = timeout_msec;
509             }
510         } else {
511             timeout_msec = LLONG_MAX;
512         }
513         if (strcmp(json_string(until), "==")
514             && strcmp(json_string(until), "!=")) {
515             error = ovsdb_syntax_error(until, NULL,
516                                        "\"until\" must be \"==\" or \"!=\"");
517         }
518     }
519     if (!error) {
520         /* Parse "rows" into 'expected'. */
521         ovsdb_row_hash_init(&expected, &columns);
522         for (i = 0; i < rows->u.array.n; i++) {
523             struct ovsdb_error *error;
524             struct ovsdb_row *row;
525
526             row = ovsdb_row_create(table);
527             error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
528                                         NULL);
529             if (error) {
530                 break;
531             }
532
533             if (!ovsdb_row_hash_insert(&expected, row)) {
534                 /* XXX Perhaps we should abort with an error or log a
535                  * warning. */
536                 ovsdb_row_destroy(row);
537             }
538         }
539     }
540     if (!error) {
541         /* Execute query. */
542         bool equal = true;
543         ovsdb_row_hash_init(&actual, &columns);
544         aux.actual = &actual;
545         aux.expected = &expected;
546         aux.equal = &equal;
547         ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
548         if (equal) {
549             /* We know that every row in 'actual' is also in 'expected'.  We
550              * also know that all of the rows in 'actual' are distinct and that
551              * all of the rows in 'expected' are distinct.  Therefore, if
552              * 'actual' and 'expected' have the same number of rows, then they
553              * have the same content. */
554             size_t n_actual = ovsdb_row_hash_count(&actual);
555             size_t n_expected = ovsdb_row_hash_count(&expected);
556             equal = n_actual == n_expected;
557         }
558         if (!strcmp(json_string(until), "==") != equal) {
559             if (timeout && x->elapsed_msec >= timeout_msec) {
560                 if (x->elapsed_msec) {
561                     error = ovsdb_error("timed out",
562                                         "\"wait\" timed out after %lld ms",
563                                         x->elapsed_msec);
564                 } else {
565                     error = ovsdb_error("timed out", "\"wait\" timed out");
566                 }
567             } else {
568                 /* ovsdb_execute() will change this, if triggers really are
569                  * supported. */
570                 error = ovsdb_error("not supported", "triggers not supported");
571             }
572         }
573     }
574
575
576     ovsdb_row_hash_destroy(&expected, true);
577     ovsdb_row_hash_destroy(&actual, false);
578     ovsdb_column_set_destroy(&columns);
579     ovsdb_condition_destroy(&condition);
580
581     return error;
582 }