diff mbox series

[ovs-dev] relay:add transaction history for relay server

Message ID AIwAkgAEICHAAc0h8CDptarM.1.1641527657234.Hmail.wentao.jia@easystack.cn
State Superseded
Headers show
Series [ovs-dev] relay:add transaction history for relay server | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test fail github build: failed

Commit Message

Wentao Jia Jan. 7, 2022, 3:54 a.m. UTC
relay server inherit transaction historys from cluster source server


Signed-off-by: Wentao Jia <wentao.jia@easystack.cn>
---
 lib/ovsdb-cs.c       |  1 +
 lib/ovsdb-cs.h       |  1 +
 ovsdb/ovsdb-server.c |  4 +++-
 ovsdb/relay.c        | 22 ++++++++++++++++++++--
 ovsdb/transaction.c  | 17 +++++++++++++++--
 ovsdb/transaction.h  |  2 ++
 6 files changed, 42 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/lib/ovsdb-cs.c b/lib/ovsdb-cs.c
index fcb6fe1b3..b8f159ab1 100644
--- a/lib/ovsdb-cs.c
+++ b/lib/ovsdb-cs.c
@@ -1525,6 +1525,7 @@  ovsdb_cs_db_add_update(struct ovsdb_cs_db *db,
     struct ovsdb_cs_event *event = ovsdb_cs_db_add_event(
         db, OVSDB_CS_EVENT_TYPE_UPDATE);
     event->update = (struct ovsdb_cs_update_event) {
+        .last_id = db->last_id,
         .table_updates = json_clone(table_updates),
         .clear = clear,
         .monitor_reply = monitor_reply,
diff --git a/lib/ovsdb-cs.h b/lib/ovsdb-cs.h
index 03bbd7ee1..66f55c67e 100644
--- a/lib/ovsdb-cs.h
+++ b/lib/ovsdb-cs.h
@@ -97,6 +97,7 @@  struct ovsdb_cs_event {
         struct ovsdb_cs_update_event {
             bool clear;
             bool monitor_reply;
+            struct uuid last_id;
             struct json *table_updates;
             int version;
         } update;
diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c
index 9fe90592e..2536de06e 100644
--- a/ovsdb/ovsdb-server.c
+++ b/ovsdb/ovsdb-server.c
@@ -683,6 +683,7 @@  open_db(struct server_config *config, const char *filename)
     struct ovsdb_error *error;
     bool is_relay;
     char *name;
+    bool need_txn_history;
 
     is_relay = !strncmp(filename, relay_prefix, relay_prefix_len);
     if (!is_relay) {
@@ -731,7 +732,8 @@  open_db(struct server_config *config, const char *filename)
 
     /* Enable txn history for clustered mode. It is not enabled for other mode
      * for now, since txn id is available for clustered mode only. */
-    ovsdb_txn_history_init(db->db, ovsdb_storage_is_clustered(storage));
+    need_txn_history = (is_relay || ovsdb_storage_is_clustered(storage));
+    ovsdb_txn_history_init(db->db, need_txn_history);;
 
     read_db(config, db);
 
diff --git a/ovsdb/relay.c b/ovsdb/relay.c
index ef0e44d34..78705057a 100644
--- a/ovsdb/relay.c
+++ b/ovsdb/relay.c
@@ -222,7 +222,9 @@  ovsdb_relay_process_row_update(struct ovsdb_table *table,
 
 static struct ovsdb_error *
 ovsdb_relay_parse_update__(struct ovsdb *db,
-                           const struct ovsdb_cs_db_update *du)
+                           const struct ovsdb_cs_db_update *du,
+                           bool txn_add_to_history,
+                           const struct uuid *last_id)
 {
     struct ovsdb_error *error = NULL;
     struct ovsdb_txn *txn;
@@ -254,8 +256,19 @@  exit:
         ovsdb_txn_abort(txn);
         return error;
     } else {
+        /*add transaction to history*/
+        if (txn_add_to_history) {
+            ovsdb_txn_set_txnid(last_id, txn);
+            VLOG_DBG("last_id = "UUID_FMT, UUID_ARGS(last_id));
+            ovsdb_txn_add_to_history(txn);
+       }
+
         /* Commit transaction. */
         error = ovsdb_txn_propose_commit_block(txn, false);
+        if (error && txn_add_to_history) {
+            VLOG_DBG("remove history");
+            ovsdb_txn_history_remove_back(db);
+        }
     }
 
     return error;
@@ -304,7 +317,12 @@  ovsdb_relay_parse_update(struct relay_ctx *ctx,
             error = ovsdb_relay_clear(ctx->db);
         }
         if (!error) {
-            error = ovsdb_relay_parse_update__(ctx->db, du);
+            bool txn_add_to_history = false;
+            txn_add_to_history = (update->version == 3
+                                  && !uuid_is_zero(&update->last_id));
+            error = ovsdb_relay_parse_update__(ctx->db, du,
+                                               txn_add_to_history,
+                                               &update->last_id);
         }
     }
     ovsdb_cs_db_update_destroy(du);
diff --git a/ovsdb/transaction.c b/ovsdb/transaction.c
index 88e052800..72817ac4d 100644
--- a/ovsdb/transaction.c
+++ b/ovsdb/transaction.c
@@ -1114,7 +1114,7 @@  ovsdb_txn_destroy_cloned(struct ovsdb_txn *txn)
     free(txn);
 }
 
-static void
+void
 ovsdb_txn_add_to_history(struct ovsdb_txn *txn)
 {
     if (txn->db->need_txn_history) {
@@ -1122,7 +1122,20 @@  ovsdb_txn_add_to_history(struct ovsdb_txn *txn)
         node->txn = ovsdb_txn_clone(txn);
         ovs_list_push_back(&txn->db->txn_history, &node->node);
         txn->db->n_txn_history++;
-        txn->db->n_txn_history_atoms += txn->n_atoms;
+    }
+}
+
+void
+ovsdb_txn_history_remove_back(struct ovsdb *db)
+{
+    if (db->need_txn_history) {
+        struct ovsdb_txn_history_node *txn_h_node = CONTAINER_OF(
+                ovs_list_pop_back(&db->txn_history),
+                struct ovsdb_txn_history_node, node);
+
+        ovsdb_txn_destroy_cloned(txn_h_node->txn);
+        free(txn_h_node);
+        db->n_txn_history--;
     }
 }
 
diff --git a/ovsdb/transaction.h b/ovsdb/transaction.h
index 6b5bb7f24..5021e0fc3 100644
--- a/ovsdb/transaction.h
+++ b/ovsdb/transaction.h
@@ -68,5 +68,7 @@  const char *ovsdb_txn_get_comment(const struct ovsdb_txn *);
 void ovsdb_txn_history_run(struct ovsdb *);
 void ovsdb_txn_history_init(struct ovsdb *, bool need_txn_history);
 void ovsdb_txn_history_destroy(struct ovsdb *);
+void ovsdb_txn_add_to_history(struct ovsdb_txn *);
+void ovsdb_txn_history_remove_back(struct ovsdb *);
 
 #endif /* ovsdb/transaction.h */