1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "condition.h"
26 #include "ovsdb-data.h"
27 #include "ovsdb-error.h"
28 #include "ovsdb-parser.h"
35 #include "transaction.h"
37 struct ovsdb_execution {
39 const struct ovsdb_session *session;
40 struct ovsdb_txn *txn;
41 struct ovsdb_symbol_table *symtab;
45 long long int elapsed_msec;
46 long long int timeout_msec;
49 typedef struct ovsdb_error *ovsdb_operation_executor(struct ovsdb_execution *,
50 struct ovsdb_parser *,
53 static ovsdb_operation_executor ovsdb_execute_insert;
54 static ovsdb_operation_executor ovsdb_execute_select;
55 static ovsdb_operation_executor ovsdb_execute_update;
56 static ovsdb_operation_executor ovsdb_execute_mutate;
57 static ovsdb_operation_executor ovsdb_execute_delete;
58 static ovsdb_operation_executor ovsdb_execute_wait;
59 static ovsdb_operation_executor ovsdb_execute_commit;
60 static ovsdb_operation_executor ovsdb_execute_abort;
61 static ovsdb_operation_executor ovsdb_execute_comment;
62 static ovsdb_operation_executor ovsdb_execute_assert;
64 static ovsdb_operation_executor *
65 lookup_executor(const char *name)
67 struct ovsdb_operation {
69 ovsdb_operation_executor *executor;
72 static const struct ovsdb_operation operations[] = {
73 { "insert", ovsdb_execute_insert },
74 { "select", ovsdb_execute_select },
75 { "update", ovsdb_execute_update },
76 { "mutate", ovsdb_execute_mutate },
77 { "delete", ovsdb_execute_delete },
78 { "wait", ovsdb_execute_wait },
79 { "commit", ovsdb_execute_commit },
80 { "abort", ovsdb_execute_abort },
81 { "comment", ovsdb_execute_comment },
82 { "assert", ovsdb_execute_assert },
87 for (i = 0; i < ARRAY_SIZE(operations); i++) {
88 const struct ovsdb_operation *c = &operations[i];
89 if (!strcmp(c->name, name)) {
97 ovsdb_execute(struct ovsdb *db, const struct ovsdb_session *session,
98 const struct json *params,
99 long long int elapsed_msec, long long int *timeout_msec)
101 struct ovsdb_execution x;
102 struct ovsdb_error *error;
103 struct json *results;
107 if (params->type != JSON_ARRAY
108 || !params->u.array.n
109 || params->u.array.elems[0]->type != JSON_STRING
110 || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) {
111 if (params->type != JSON_ARRAY) {
112 error = ovsdb_syntax_error(params, NULL, "array expected");
114 error = ovsdb_syntax_error(params, NULL, "database name expected "
115 "as first parameter");
118 results = ovsdb_error_to_json(error);
119 ovsdb_error_destroy(error);
125 x.txn = ovsdb_txn_create(db);
126 x.symtab = ovsdb_symbol_table_create();
128 x.elapsed_msec = elapsed_msec;
129 x.timeout_msec = LLONG_MAX;
132 results = json_array_create_empty();
133 n_operations = params->u.array.n - 1;
135 for (i = 1; i <= n_operations; i++) {
136 struct json *operation = params->u.array.elems[i];
137 struct ovsdb_error *parse_error;
138 struct ovsdb_parser parser;
140 const struct json *op;
142 /* Parse and execute operation. */
143 ovsdb_parser_init(&parser, operation,
144 "ovsdb operation %zu of %zu", i, n_operations);
145 op = ovsdb_parser_member(&parser, "op", OP_ID);
146 result = json_object_create();
148 const char *op_name = json_string(op);
149 ovsdb_operation_executor *executor = lookup_executor(op_name);
151 error = executor(&x, &parser, result);
153 ovsdb_parser_raise_error(&parser, "No operation \"%s\"",
157 assert(ovsdb_parser_has_error(&parser));
160 /* A parse error overrides any other error.
161 * An error overrides any other result. */
162 parse_error = ovsdb_parser_finish(&parser);
164 ovsdb_error_destroy(error);
168 json_destroy(result);
169 result = ovsdb_error_to_json(error);
171 if (error && !strcmp(ovsdb_error_get_tag(error), "not supported")
173 ovsdb_txn_abort(x.txn);
174 *timeout_msec = x.timeout_msec;
176 json_destroy(result);
177 json_destroy(results);
182 /* Add result to array. */
183 json_array_add(results, result);
190 error = ovsdb_txn_commit(x.txn, x.durable);
192 json_array_add(results, ovsdb_error_to_json(error));
195 ovsdb_txn_abort(x.txn);
198 while (json_array(results)->n < n_operations) {
199 json_array_add(results, json_null_create());
203 ovsdb_error_destroy(error);
204 ovsdb_symbol_table_destroy(x.symtab);
209 static struct ovsdb_error *
210 ovsdb_execute_commit(struct ovsdb_execution *x, struct ovsdb_parser *parser,
211 struct json *result OVS_UNUSED)
213 const struct json *durable;
215 durable = ovsdb_parser_member(parser, "durable", OP_BOOLEAN);
216 if (durable && json_boolean(durable)) {
222 static struct ovsdb_error *
223 ovsdb_execute_abort(struct ovsdb_execution *x OVS_UNUSED,
224 struct ovsdb_parser *parser OVS_UNUSED,
225 struct json *result OVS_UNUSED)
227 return ovsdb_error("aborted", "aborted by request");
230 static struct ovsdb_table *
231 parse_table(struct ovsdb_execution *x,
232 struct ovsdb_parser *parser, const char *member)
234 struct ovsdb_table *table;
235 const char *table_name;
236 const struct json *json;
238 json = ovsdb_parser_member(parser, member, OP_ID);
242 table_name = json_string(json);
244 table = shash_find_data(&x->db->tables, table_name);
246 ovsdb_parser_raise_error(parser, "No table named %s.", table_name);
251 static WARN_UNUSED_RESULT struct ovsdb_error *
252 parse_row(const struct json *json, const struct ovsdb_table *table,
253 struct ovsdb_symbol_table *symtab,
254 struct ovsdb_row **rowp, struct ovsdb_column_set *columns)
256 struct ovsdb_error *error;
257 struct ovsdb_row *row;
262 return OVSDB_BUG("null table");
265 return OVSDB_BUG("null row");
268 row = ovsdb_row_create(table);
269 error = ovsdb_row_from_json(row, json, symtab, columns);
271 ovsdb_row_destroy(row);
279 static struct ovsdb_error *
280 ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
283 struct ovsdb_table *table;
284 struct ovsdb_row *row = NULL;
285 const struct json *uuid_name, *row_json;
286 struct ovsdb_error *error;
287 struct uuid row_uuid;
289 table = parse_table(x, parser, "table");
290 uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID | OP_OPTIONAL);
291 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
292 error = ovsdb_parser_get_error(parser);
298 struct ovsdb_symbol *symbol;
300 symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name));
301 if (symbol->created) {
302 return ovsdb_syntax_error(uuid_name, "duplicate uuid-name",
303 "This \"uuid-name\" appeared on an "
304 "earlier \"insert\" operation.");
306 row_uuid = symbol->uuid;
307 symbol->created = true;
309 uuid_generate(&row_uuid);
313 error = parse_row(row_json, table, x->symtab, &row, NULL);
316 /* Check constraints for columns not included in "row", in case the
317 * default values do not satisfy the constraints. We could check only
318 * the columns that have their default values by supplying an
319 * ovsdb_column_set to parse_row() above, but I suspect that this is
321 const struct shash_node *node;
323 SHASH_FOR_EACH (node, &table->schema->columns) {
324 const struct ovsdb_column *column = node->data;
325 const struct ovsdb_datum *datum = &row->fields[column->index];
327 /* If there are 0 keys or pairs, there's nothing to check.
328 * If there is 1, it might be a default value.
329 * If there are more, it can't be a default value, so the value has
330 * already been checked. */
332 error = ovsdb_datum_check_constraints(datum, &column->type);
334 ovsdb_row_destroy(row);
341 *ovsdb_row_get_uuid_rw(row) = row_uuid;
342 ovsdb_txn_row_insert(x->txn, row);
343 json_object_put(result, "uuid",
344 ovsdb_datum_to_json(&row->fields[OVSDB_COL_UUID],
350 static struct ovsdb_error *
351 ovsdb_execute_select(struct ovsdb_execution *x, struct ovsdb_parser *parser,
354 struct ovsdb_table *table;
355 const struct json *where, *columns_json, *sort_json;
356 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
357 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
358 struct ovsdb_column_set sort = OVSDB_COLUMN_SET_INITIALIZER;
359 struct ovsdb_error *error;
361 table = parse_table(x, parser, "table");
362 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
363 columns_json = ovsdb_parser_member(parser, "columns",
364 OP_ARRAY | OP_OPTIONAL);
365 sort_json = ovsdb_parser_member(parser, "sort", OP_ARRAY | OP_OPTIONAL);
367 error = ovsdb_parser_get_error(parser);
369 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
373 error = ovsdb_column_set_from_json(columns_json, table->schema,
377 error = ovsdb_column_set_from_json(sort_json, table->schema, &sort);
380 struct ovsdb_row_set rows = OVSDB_ROW_SET_INITIALIZER;
382 ovsdb_query_distinct(table, &condition, &columns, &rows);
383 ovsdb_row_set_sort(&rows, &sort);
384 json_object_put(result, "rows",
385 ovsdb_row_set_to_json(&rows, &columns));
387 ovsdb_row_set_destroy(&rows);
390 ovsdb_column_set_destroy(&columns);
391 ovsdb_column_set_destroy(&sort);
392 ovsdb_condition_destroy(&condition);
397 struct update_row_cbdata {
399 struct ovsdb_txn *txn;
400 const struct ovsdb_row *row;
401 const struct ovsdb_column_set *columns;
405 update_row_cb(const struct ovsdb_row *row, void *ur_)
407 struct update_row_cbdata *ur = ur_;
410 if (!ovsdb_row_equal_columns(row, ur->row, ur->columns)) {
411 ovsdb_row_update_columns(ovsdb_txn_row_modify(ur->txn, row),
412 ur->row, ur->columns);
418 static struct ovsdb_error *
419 ovsdb_execute_update(struct ovsdb_execution *x, struct ovsdb_parser *parser,
422 struct ovsdb_table *table;
423 const struct json *where, *row_json;
424 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
425 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
426 struct ovsdb_row *row = NULL;
427 struct update_row_cbdata ur;
428 struct ovsdb_error *error;
430 table = parse_table(x, parser, "table");
431 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
432 row_json = ovsdb_parser_member(parser, "row", OP_OBJECT);
433 error = ovsdb_parser_get_error(parser);
435 error = parse_row(row_json, table, x->symtab, &row, &columns);
438 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
445 ur.columns = &columns;
446 ovsdb_query(table, &condition, update_row_cb, &ur);
447 json_object_put(result, "count", json_integer_create(ur.n_matches));
450 ovsdb_row_destroy(row);
451 ovsdb_column_set_destroy(&columns);
452 ovsdb_condition_destroy(&condition);
457 struct mutate_row_cbdata {
459 struct ovsdb_txn *txn;
460 const struct ovsdb_mutation_set *mutations;
461 struct ovsdb_error **error;
465 mutate_row_cb(const struct ovsdb_row *row, void *mr_)
467 struct mutate_row_cbdata *mr = mr_;
470 *mr->error = ovsdb_mutation_set_execute(ovsdb_txn_row_modify(mr->txn, row),
472 return *mr->error == NULL;
475 static struct ovsdb_error *
476 ovsdb_execute_mutate(struct ovsdb_execution *x, struct ovsdb_parser *parser,
479 struct ovsdb_table *table;
480 const struct json *where;
481 const struct json *mutations_json;
482 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
483 struct ovsdb_mutation_set mutations = OVSDB_MUTATION_SET_INITIALIZER;
484 struct ovsdb_row *row = NULL;
485 struct mutate_row_cbdata mr;
486 struct ovsdb_error *error;
488 table = parse_table(x, parser, "table");
489 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
490 mutations_json = ovsdb_parser_member(parser, "mutations", OP_ARRAY);
491 error = ovsdb_parser_get_error(parser);
493 error = ovsdb_mutation_set_from_json(table->schema, mutations_json,
494 x->symtab, &mutations);
497 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
503 mr.mutations = &mutations;
505 ovsdb_query(table, &condition, mutate_row_cb, &mr);
506 json_object_put(result, "count", json_integer_create(mr.n_matches));
509 ovsdb_row_destroy(row);
510 ovsdb_mutation_set_destroy(&mutations);
511 ovsdb_condition_destroy(&condition);
516 struct delete_row_cbdata {
518 const struct ovsdb_table *table;
519 struct ovsdb_txn *txn;
523 delete_row_cb(const struct ovsdb_row *row, void *dr_)
525 struct delete_row_cbdata *dr = dr_;
528 ovsdb_txn_row_delete(dr->txn, row);
533 static struct ovsdb_error *
534 ovsdb_execute_delete(struct ovsdb_execution *x, struct ovsdb_parser *parser,
537 struct ovsdb_table *table;
538 const struct json *where;
539 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
540 struct ovsdb_error *error;
542 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
543 table = parse_table(x, parser, "table");
544 error = ovsdb_parser_get_error(parser);
546 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
550 struct delete_row_cbdata dr;
555 ovsdb_query(table, &condition, delete_row_cb, &dr);
557 json_object_put(result, "count", json_integer_create(dr.n_matches));
560 ovsdb_condition_destroy(&condition);
565 struct wait_auxdata {
566 struct ovsdb_row_hash *actual;
567 struct ovsdb_row_hash *expected;
572 ovsdb_execute_wait_query_cb(const struct ovsdb_row *row, void *aux_)
574 struct wait_auxdata *aux = aux_;
576 if (ovsdb_row_hash_contains(aux->expected, row)) {
577 ovsdb_row_hash_insert(aux->actual, row);
580 /* The query row isn't in the expected result set, so the actual and
581 * expected results sets definitely differ and we can short-circuit the
582 * rest of the query. */
588 static struct ovsdb_error *
589 ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser,
590 struct json *result OVS_UNUSED)
592 struct ovsdb_table *table;
593 const struct json *timeout, *where, *columns_json, *until, *rows;
594 struct ovsdb_condition condition = OVSDB_CONDITION_INITIALIZER;
595 struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
596 struct ovsdb_row_hash expected = OVSDB_ROW_HASH_INITIALIZER(expected);
597 struct ovsdb_row_hash actual = OVSDB_ROW_HASH_INITIALIZER(actual);
598 struct ovsdb_error *error;
599 struct wait_auxdata aux;
600 long long int timeout_msec = 0;
603 timeout = ovsdb_parser_member(parser, "timeout", OP_NUMBER | OP_OPTIONAL);
604 where = ovsdb_parser_member(parser, "where", OP_ARRAY);
605 columns_json = ovsdb_parser_member(parser, "columns",
606 OP_ARRAY | OP_OPTIONAL);
607 until = ovsdb_parser_member(parser, "until", OP_STRING);
608 rows = ovsdb_parser_member(parser, "rows", OP_ARRAY);
609 table = parse_table(x, parser, "table");
610 error = ovsdb_parser_get_error(parser);
612 error = ovsdb_condition_from_json(table->schema, where, x->symtab,
616 error = ovsdb_column_set_from_json(columns_json, table->schema,
621 timeout_msec = MIN(LLONG_MAX, json_real(timeout));
622 if (timeout_msec < 0) {
623 error = ovsdb_syntax_error(timeout, NULL,
624 "timeout must be nonnegative");
625 } else if (timeout_msec < x->timeout_msec) {
626 x->timeout_msec = timeout_msec;
629 timeout_msec = LLONG_MAX;
631 if (strcmp(json_string(until), "==")
632 && strcmp(json_string(until), "!=")) {
633 error = ovsdb_syntax_error(until, NULL,
634 "\"until\" must be \"==\" or \"!=\"");
638 /* Parse "rows" into 'expected'. */
639 ovsdb_row_hash_init(&expected, &columns);
640 for (i = 0; i < rows->u.array.n; i++) {
641 struct ovsdb_row *row;
643 row = ovsdb_row_create(table);
644 error = ovsdb_row_from_json(row, rows->u.array.elems[i], x->symtab,
650 if (!ovsdb_row_hash_insert(&expected, row)) {
651 /* XXX Perhaps we should abort with an error or log a
653 ovsdb_row_destroy(row);
660 ovsdb_row_hash_init(&actual, &columns);
661 aux.actual = &actual;
662 aux.expected = &expected;
664 ovsdb_query(table, &condition, ovsdb_execute_wait_query_cb, &aux);
666 /* We know that every row in 'actual' is also in 'expected'. We
667 * also know that all of the rows in 'actual' are distinct and that
668 * all of the rows in 'expected' are distinct. Therefore, if
669 * 'actual' and 'expected' have the same number of rows, then they
670 * have the same content. */
671 size_t n_actual = ovsdb_row_hash_count(&actual);
672 size_t n_expected = ovsdb_row_hash_count(&expected);
673 equal = n_actual == n_expected;
675 if (!strcmp(json_string(until), "==") != equal) {
676 if (timeout && x->elapsed_msec >= timeout_msec) {
677 if (x->elapsed_msec) {
678 error = ovsdb_error("timed out",
679 "\"wait\" timed out after %lld ms",
682 error = ovsdb_error("timed out", "\"wait\" timed out");
685 /* ovsdb_execute() will change this, if triggers really are
687 error = ovsdb_error("not supported", "triggers not supported");
693 ovsdb_row_hash_destroy(&expected, true);
694 ovsdb_row_hash_destroy(&actual, false);
695 ovsdb_column_set_destroy(&columns);
696 ovsdb_condition_destroy(&condition);
701 static struct ovsdb_error *
702 ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser,
703 struct json *result OVS_UNUSED)
705 const struct json *comment;
707 comment = ovsdb_parser_member(parser, "comment", OP_STRING);
711 ovsdb_txn_add_comment(x->txn, json_string(comment));
716 static struct ovsdb_error *
717 ovsdb_execute_assert(struct ovsdb_execution *x, struct ovsdb_parser *parser,
718 struct json *result OVS_UNUSED)
720 const struct json *lock_name;
722 lock_name = ovsdb_parser_member(parser, "lock", OP_ID);
728 const struct ovsdb_lock_waiter *waiter;
730 waiter = ovsdb_session_get_lock_waiter(x->session,
731 json_string(lock_name));
732 if (waiter && ovsdb_lock_waiter_is_owner(waiter)) {
737 return ovsdb_error("not owner", "Asserted lock %s not held.",
738 json_string(lock_name));