[ovs-dev] [PATCH v2 3/9] ovsdb: table: Expose functions to execute operations on ovsdb tables.

Ilya Maximets i.maximets at ovn.org
Sat Jun 12 02:00:02 UTC 2021


These functions will be used later for ovsdb 'relay' service model, so
moving them to a common code.

Warnings translated to ovsdb errors, caller in replication.c only
printed inconsistency warnings, but mostly ignored them.  Implementing
the same logic by checking the error tag.

Also ovsdb_execute_insert() previously printed incorrect warning about
duplicate row while it was a syntax error in json.  Fixing that by
actually checking for the duplicate and reporting correct ovsdb error.

Signed-off-by: Ilya Maximets <i.maximets at ovn.org>
---
 ovsdb/replication.c | 83 ++++-----------------------------------------
 ovsdb/table.c       | 69 +++++++++++++++++++++++++++++++++++++
 ovsdb/table.h       | 14 ++++++++
 3 files changed, 90 insertions(+), 76 deletions(-)

diff --git a/ovsdb/replication.c b/ovsdb/replication.c
index bb1bd4250..b755976b0 100644
--- a/ovsdb/replication.c
+++ b/ovsdb/replication.c
@@ -54,18 +54,6 @@ static struct ovsdb_error *process_table_update(struct json *table_update,
                                                 const char *table_name,
                                                 struct ovsdb *database,
                                                 struct ovsdb_txn *txn);
-
-static struct ovsdb_error *execute_insert(struct ovsdb_txn *txn,
-                                          const struct uuid *row_uuid,
-                                          struct ovsdb_table *table,
-                                          struct json *new);
-static struct ovsdb_error *execute_delete(struct ovsdb_txn *txn,
-                                          const struct uuid *row_uuid,
-                                          struct ovsdb_table *table);
-static struct ovsdb_error *execute_update(struct ovsdb_txn *txn,
-                                          const struct uuid *row_uuid,
-                                          struct ovsdb_table *table,
-                                          struct json *new);
 
 /* Maps from db name to sset of table names. */
 static struct shash excluded_tables = SHASH_INITIALIZER(&excluded_tables);
@@ -687,77 +675,20 @@ process_table_update(struct json *table_update, const char *table_name,
         new = shash_find_data(json_object(row_update), "new");
 
         struct ovsdb_error *error;
-        error = (!new ? execute_delete(txn, &uuid, table)
-                 : !old ? execute_insert(txn, &uuid, table, new)
-                 : execute_update(txn, &uuid, table, new));
+        error = (!new ? ovsdb_table_execute_delete(txn, &uuid, table)
+                 : !old ? ovsdb_table_execute_insert(txn, &uuid, table, new)
+                 : ovsdb_table_execute_update(txn, &uuid, table, new));
         if (error) {
+            if (!strcmp(ovsdb_error_get_tag(error), "consistency violation")) {
+                ovsdb_error_assert(error);
+                error = NULL;
+            }
             return error;
         }
     }
     return NULL;
 }
 
-static struct ovsdb_error *
-execute_insert(struct ovsdb_txn *txn, const struct uuid *row_uuid,
-               struct ovsdb_table *table, struct json *json_row)
-{
-    struct ovsdb_row *row = ovsdb_row_create(table);
-    struct ovsdb_error *error = ovsdb_row_from_json(row, json_row, NULL, NULL);
-    if (!error) {
-        *ovsdb_row_get_uuid_rw(row) = *row_uuid;
-        ovsdb_txn_row_insert(txn, row);
-    } else {
-        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-        VLOG_WARN_RL(&rl, "cannot add existing row "UUID_FMT" to table %s",
-                     UUID_ARGS(row_uuid), table->schema->name);
-        ovsdb_row_destroy(row);
-    }
-
-    return error;
-}
-
-static struct ovsdb_error *
-execute_delete(struct ovsdb_txn *txn, const struct uuid *row_uuid,
-               struct ovsdb_table *table)
-{
-    const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
-    if (row) {
-        ovsdb_txn_row_delete(txn, row);
-    } else {
-        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-        VLOG_WARN_RL(&rl, "cannot delete missing row "UUID_FMT" from table %s",
-                     UUID_ARGS(row_uuid), table->schema->name);
-    }
-    return NULL;
-}
-
-static struct ovsdb_error *
-execute_update(struct ovsdb_txn *txn, const struct uuid *row_uuid,
-               struct ovsdb_table *table, struct json *json_row)
-{
-    const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
-    if (!row) {
-        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-        VLOG_WARN_RL(&rl, "cannot modify missing row "UUID_FMT" in table %s",
-                     UUID_ARGS(row_uuid), table->schema->name);
-        return NULL;
-    }
-
-    struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
-    struct ovsdb_row *update = ovsdb_row_create(table);
-    struct ovsdb_error *error = ovsdb_row_from_json(update, json_row,
-                                                    NULL, &columns);
-
-    if (!error && !ovsdb_row_equal_columns(row, update, &columns)) {
-        ovsdb_row_update_columns(ovsdb_txn_row_modify(txn, row),
-                                 update, &columns);
-    }
-
-    ovsdb_column_set_destroy(&columns);
-    ovsdb_row_destroy(update);
-    return error;
-}
-
 void
 request_ids_add(const struct json *id, struct ovsdb *db)
 {
diff --git a/ovsdb/table.c b/ovsdb/table.c
index 6cd2d886d..2935bd897 100644
--- a/ovsdb/table.c
+++ b/ovsdb/table.c
@@ -25,6 +25,7 @@
 #include "ovsdb-parser.h"
 #include "ovsdb-types.h"
 #include "row.h"
+#include "transaction.h"
 
 static void
 add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column)
@@ -339,3 +340,71 @@ ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid)
 
     return NULL;
 }
+
+struct ovsdb_error *
+ovsdb_table_execute_insert(struct ovsdb_txn *txn, const struct uuid *row_uuid,
+                           struct ovsdb_table *table, struct json *json_row)
+{
+    const struct ovsdb_row *old_row = ovsdb_table_get_row(table, row_uuid);
+    if (old_row) {
+        return ovsdb_error(
+                    "consistency violation",
+                    "cannot delete missing row "UUID_FMT" from table %s",
+                    UUID_ARGS(row_uuid), table->schema->name);
+    }
+
+    struct ovsdb_row *row = ovsdb_row_create(table);
+
+    struct ovsdb_error *error = ovsdb_row_from_json(row, json_row, NULL, NULL);
+    if (!error) {
+        *ovsdb_row_get_uuid_rw(row) = *row_uuid;
+        ovsdb_txn_row_insert(txn, row);
+    } else {
+        ovsdb_row_destroy(row);
+    }
+
+    return error;
+}
+
+struct ovsdb_error *
+ovsdb_table_execute_delete(struct ovsdb_txn *txn, const struct uuid *row_uuid,
+                           struct ovsdb_table *table)
+{
+    const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
+    if (!row) {
+        return ovsdb_error(
+                    "consistency violation",
+                    "cannot delete missing row "UUID_FMT" from table %s",
+                    UUID_ARGS(row_uuid), table->schema->name);
+    }
+
+    ovsdb_txn_row_delete(txn, row);
+    return NULL;
+}
+
+struct ovsdb_error *
+ovsdb_table_execute_update(struct ovsdb_txn *txn, const struct uuid *row_uuid,
+                           struct ovsdb_table *table, struct json *json_row)
+{
+    const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
+    if (!row) {
+        return ovsdb_error(
+                    "consistency violation",
+                    "cannot modify missing row "UUID_FMT" from table %s",
+                    UUID_ARGS(row_uuid), table->schema->name);
+    }
+
+    struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
+    struct ovsdb_row *update = ovsdb_row_create(table);
+    struct ovsdb_error *error = ovsdb_row_from_json(update, json_row,
+                                                    NULL, &columns);
+
+    if (!error && !ovsdb_row_equal_columns(row, update, &columns)) {
+        ovsdb_row_update_columns(ovsdb_txn_row_modify(txn, row),
+                                 update, &columns);
+    }
+
+    ovsdb_column_set_destroy(&columns);
+    ovsdb_row_destroy(update);
+    return error;
+}
diff --git a/ovsdb/table.h b/ovsdb/table.h
index 69dd649df..e21ec7b31 100644
--- a/ovsdb/table.h
+++ b/ovsdb/table.h
@@ -23,6 +23,7 @@
 
 struct json;
 struct uuid;
+struct ovsdb_txn;
 
 /* Schema for a database table. */
 struct ovsdb_table_schema {
@@ -70,4 +71,17 @@ void ovsdb_table_destroy(struct ovsdb_table *);
 const struct ovsdb_row *ovsdb_table_get_row(const struct ovsdb_table *,
                                             const struct uuid *);
 
+/* Below functions adds row modification for ovsdb table to the transaction. */
+struct ovsdb_error *ovsdb_table_execute_insert(struct ovsdb_txn *txn,
+                                               const struct uuid *row_uuid,
+                                               struct ovsdb_table *table,
+                                               struct json *new);
+struct ovsdb_error *ovsdb_table_execute_delete(struct ovsdb_txn *txn,
+                                               const struct uuid *row_uuid,
+                                               struct ovsdb_table *table);
+struct ovsdb_error *ovsdb_table_execute_update(struct ovsdb_txn *txn,
+                                               const struct uuid *row_uuid,
+                                               struct ovsdb_table *table,
+                                               struct json *new);
+
 #endif /* ovsdb/table.h */
-- 
2.26.3



More information about the dev mailing list