Previously the transaction history was hard limited to 100 entries.
However in fast changing environments (e.g. with 20 transactions/sec)
this means that transactions are only in the history for a few seconds.
If now a client reconnects and tries to use monitor_cond_since it has
only a short timeframe where this will reliably work.

We make the history limit configurable here so that use can choose the
speed vs memory tradeoff as needed.
We still keep the limit based on size of the history, as syncing a
history larger than the actual database size does not make sense in any
way.

Signed-off-by: Felix Huettner <[email protected]>
---
v2->v3: adressed docs and nits as requested by Ilja
v1->v2: handle setting if the config does not specify the service-model.

 Documentation/ref/ovsdb.7.rst | 22 +++++++++++++++++++++
 NEWS                          |  3 +++
 ovsdb/ovsdb-server.1.in       |  4 ++++
 ovsdb/ovsdb-server.c          | 27 +++++++++++++++++++++++--
 ovsdb/ovsdb.h                 |  3 ++-
 ovsdb/relay.c                 |  4 ++--
 ovsdb/transaction.c           | 37 +++++++++++++++++++++++++++++------
 ovsdb/transaction.h           |  5 ++++-
 tests/ovsdb-server.at         | 25 +++++++++++++++++++++--
 9 files changed, 116 insertions(+), 14 deletions(-)

diff --git a/Documentation/ref/ovsdb.7.rst b/Documentation/ref/ovsdb.7.rst
index cf1ef3736..1c88238c0 100644
--- a/Documentation/ref/ovsdb.7.rst
+++ b/Documentation/ref/ovsdb.7.rst
@@ -624,6 +624,28 @@ above).
 
 Open vSwitch 2.16 introduced support for relay service model.
 
+Client Monitors
+===============
+
+Clients requesting monitors using ``"monitor"`` or ``"monitor_cond"`` will
+generally receive an initial dump of all of their relevant data when the
+montior is started. As changes are submitted to the ovsdb it will send updates
+to the clients. If a client where to reconnect to a ovsdb it would receive a
+full initial dump again.
+
+In case the client uses ``"monitor_cond_since"`` it can improve on this
+reconnect behaviour by providing the ovsdb server with the last known update
+ID. If the server still has this ID in its history it will respond only with
+the changes that accumulated since this ID. This feature is only available if
+the ovsdb server uses the **clustered** service model. It is also available on
+the **relay** service model if the relay source is a **clustered** service
+model.
+
+The transaction history used for this is limited to the same memory usage as
+the database itself. In addition it has a limit of 100 entries per default.
+This can be modified by using ``"transaction-history-limit": 1000`` in the
+configuration of the database.
+
 Database Replication
 ====================
 
diff --git a/NEWS b/NEWS
index 1a3044cbf..9b8fd7a54 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,9 @@ Post-v3.7.0
    - Userspace datapath:
      * ARP/ND lookups for native tunnel are now rate limited. The holdout
        timer can be configured with 'tnl/neigh/retrans_time'.
+   - OVSDB:
+     * The transaction history length is now configurable using the config
+       `"transaction-history-limit": 1000`.
 
 
 v3.7.0 - 16 Feb 2026
diff --git a/ovsdb/ovsdb-server.1.in b/ovsdb/ovsdb-server.1.in
index 23b8e6e9c..bb4b9a938 100644
--- a/ovsdb/ovsdb-server.1.in
+++ b/ovsdb/ovsdb-server.1.in
@@ -158,6 +158,10 @@ database otherwise.
 .IP "\fBexclude-tables\fR (JSON array of strings; active-backup only)"
 List of table names that should be excluded from replication in backup mode,
 e.g. \fB"exclude-tables": [ "Table_One", "Table_Two" ]\fR.
+.IP "\fBtransaction-history-limit\fR (integer)"
+Changes the transaction history length of the database. Only available for the
+service-models \fBclustered\fR and \fBrelay\fR if the relay source is
+\fBclustered\fR.
 .RE
 .RE
 .IP
diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c
index 2af62071e..4a85ae575 100644
--- a/ovsdb/ovsdb-server.c
+++ b/ovsdb/ovsdb-server.c
@@ -156,6 +156,10 @@ struct db_config {
         bool backup;  /* If true, the database is read-only and receives
                        * updates from the 'source'. */
     } ab;
+
+    /* Valid for SM_CLUSTERED or SM_RELAY. */
+    unsigned int n_txn_history_max; /* The maximum amount of history entries
+                                     * to keep. 0 means default. */
 };
 
 struct db {
@@ -462,6 +466,7 @@ db_config_clone(const struct db_config *c)
         conf->options = ovsdb_jsonrpc_options_clone(c->options);
     }
     conf->ab.sync_exclude = nullable_xstrdup(c->ab.sync_exclude);
+    conf->n_txn_history_max = c->n_txn_history_max;
 
     return conf;
 }
@@ -572,6 +577,11 @@ database_update_config(struct server_config *server_config,
         replication_set_db(db->db, conf->source, conf->ab.sync_exclude,
                            server_uuid, &conf->options->rpc);
     }
+
+    if ((conf->model == SM_CLUSTERED || conf->model == SM_RELAY) &&
+        conf->n_txn_history_max) {
+        ovsdb_txn_history_update(db->db, conf->n_txn_history_max);
+    }
 }
 
 static bool
@@ -1180,7 +1190,8 @@ open_db(struct server_config *server_config,
     /* Enable txn history for clustered and relay modes.  It is not enabled for
      * other modes for now, since txn id is available for clustered and relay
      * modes only. */
-    ovsdb_txn_history_init(db->db, model == SM_RELAY || model == SM_CLUSTERED);
+    ovsdb_txn_history_init(db->db, model == SM_RELAY || model == SM_CLUSTERED,
+                           conf->n_txn_history_max);
 
     read_db(server_config, db);
 
@@ -2910,6 +2921,12 @@ db_config_to_json(const struct db_config *conf)
         }
         json_object_put(json, "backup", json_boolean_create(conf->ab.backup));
     }
+
+    if (conf->n_txn_history_max) {
+        json_object_put(json, "transaction-history-limit",
+                        json_integer_create(conf->n_txn_history_max));
+    }
+
     return json;
 }
 
@@ -3033,7 +3050,7 @@ remotes_from_json(struct shash *remotes, const struct 
json *json)
 static struct db_config *
 db_config_from_json(const char *name, const struct json *json)
 {
-    const struct json *model, *source, *sync_exclude, *backup;
+    const struct json *model, *source, *sync_exclude, *backup, *txn_limit;
     struct db_config *conf = xzalloc(sizeof *conf);
     struct ovsdb_parser parser;
     struct ovsdb_error *error;
@@ -3114,6 +3131,12 @@ db_config_from_json(const char *name, const struct json 
*json)
         }
     }
 
+    txn_limit = ovsdb_parser_member(&parser, "transaction-history-limit",
+                                    OP_INTEGER | OP_OPTIONAL);
+    if (txn_limit) {
+        conf->n_txn_history_max = json_integer(txn_limit);
+    }
+
     error = ovsdb_parser_finish(&parser);
     if (error) {
         char *s = ovsdb_error_to_string_free(error);
diff --git a/ovsdb/ovsdb.h b/ovsdb/ovsdb.h
index 325900bc6..04c52817d 100644
--- a/ovsdb/ovsdb.h
+++ b/ovsdb/ovsdb.h
@@ -107,9 +107,10 @@ struct ovsdb {
     struct ovsdb_table *rbac_role;
 
     /* History trasanctions for incremental monitor transfer. */
-    bool need_txn_history;     /* Need to maintain history of transactions. */
     unsigned int n_txn_history; /* Current number of history transactions. */
     unsigned int n_txn_history_atoms; /* Total number of atoms in history. */
+    unsigned int n_txn_history_max; /* The maximum history length.
+                                       If 0 there is no history. */
     struct ovs_list txn_history; /* Contains "struct ovsdb_txn_history_node. */
 
     size_t n_atoms;  /* Total number of ovsdb atoms in the database. */
diff --git a/ovsdb/relay.c b/ovsdb/relay.c
index a5e1a5f3a..7691e0b73 100644
--- a/ovsdb/relay.c
+++ b/ovsdb/relay.c
@@ -274,7 +274,7 @@ exit:
             /* The relay source doesn't support unique transaction ids,
              * disabling transaction history for relay. */
             ovsdb_txn_history_destroy(db);
-            ovsdb_txn_history_init(db, false);
+            ovsdb_txn_history_init(db, false, 0);
         } else {
             ovsdb_txn_set_txnid(last_id, txn);
         }
@@ -307,7 +307,7 @@ ovsdb_relay_clear(struct ovsdb *db)
 
     /* Clearing the transaction history, and re-enabling it. */
     ovsdb_txn_history_destroy(db);
-    ovsdb_txn_history_init(db, true);
+    ovsdb_txn_history_init(db, true, db->n_txn_history_max);
 
     return error;
 }
diff --git a/ovsdb/transaction.c b/ovsdb/transaction.c
index 0d0d27b61..210030e62 100644
--- a/ovsdb/transaction.c
+++ b/ovsdb/transaction.c
@@ -18,6 +18,7 @@
 #include "transaction.h"
 
 #include "bitmap.h"
+#include "coverage.h"
 #include "openvswitch/dynamic-string.h"
 #include "file.h"
 #include "hash.h"
@@ -38,6 +39,10 @@
 
 VLOG_DEFINE_THIS_MODULE(transaction);
 
+COVERAGE_DEFINE(txn_history_add);
+
+#define TRANSACTION_HISTORY_LIMIT_DEFAULT 100
+
 struct ovsdb_txn {
     struct ovsdb *db;
     struct ovs_list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
@@ -1186,12 +1191,13 @@ ovsdb_txn_destroy_cloned(struct ovsdb_txn *txn)
 static void
 ovsdb_txn_add_to_history(struct ovsdb_txn *txn)
 {
-    if (txn->db->need_txn_history) {
+    if (txn->db->n_txn_history_max) {
         struct ovsdb_txn_history_node *node = xzalloc(sizeof *node);
         node->txn = ovsdb_txn_clone_for_history(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;
+        COVERAGE_INC(txn_history_add);
     }
 }
 
@@ -1676,7 +1682,7 @@ for_each_txn_row(struct ovsdb_txn *txn,
 void
 ovsdb_txn_history_run(struct ovsdb *db)
 {
-    if (!db->need_txn_history) {
+    if (!db->n_txn_history_max) {
         return;
     }
     /* Remove old histories to limit the size of the history.  Removing until
@@ -1686,7 +1692,7 @@ ovsdb_txn_history_run(struct ovsdb *db)
      * Keeping at least one transaction to avoid sending UUID_ZERO as a last id
      * if all entries got removed due to the size limit. */
     while (db->n_txn_history > 1 &&
-           (db->n_txn_history > 100 ||
+           (db->n_txn_history > db->n_txn_history_max ||
             db->n_txn_history_atoms > db->n_atoms)) {
         struct ovsdb_txn_history_node *txn_h_node = CONTAINER_OF(
                 ovs_list_pop_front(&db->txn_history),
@@ -1700,11 +1706,17 @@ ovsdb_txn_history_run(struct ovsdb *db)
 }
 
 void
-ovsdb_txn_history_init(struct ovsdb *db, bool need_txn_history)
+ovsdb_txn_history_init(struct ovsdb *db, bool need_txn_history,
+                       unsigned int n_txn_history_max)
 {
-    db->need_txn_history = need_txn_history;
     db->n_txn_history = 0;
     db->n_txn_history_atoms = 0;
+    if (need_txn_history) {
+        db->n_txn_history_max = MAX(n_txn_history_max,
+                                    TRANSACTION_HISTORY_LIMIT_DEFAULT);
+    } else {
+        db->n_txn_history_max = 0;
+    }
     ovs_list_init(&db->txn_history);
 }
 
@@ -1712,7 +1724,7 @@ void
 ovsdb_txn_history_destroy(struct ovsdb *db)
 {
 
-    if (!db->need_txn_history) {
+    if (!db->n_txn_history_max) {
         return;
     }
 
@@ -1725,3 +1737,16 @@ ovsdb_txn_history_destroy(struct ovsdb *db)
     db->n_txn_history = 0;
     db->n_txn_history_atoms = 0;
 }
+
+void
+ovsdb_txn_history_update(struct ovsdb *db, unsigned int n_txn_history_max)
+{
+    if (!db->n_txn_history_max) {
+        return;
+    }
+
+    db->n_txn_history_max = MAX(n_txn_history_max,
+                                TRANSACTION_HISTORY_LIMIT_DEFAULT);
+
+    ovsdb_txn_history_run(db);
+}
diff --git a/ovsdb/transaction.h b/ovsdb/transaction.h
index d94205414..04f96f079 100644
--- a/ovsdb/transaction.h
+++ b/ovsdb/transaction.h
@@ -75,7 +75,10 @@ struct ovsdb_row *ovsdb_index_search(struct hmap *index,
 void ovsdb_txn_add_comment(struct ovsdb_txn *, const char *);
 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_init(struct ovsdb *, bool need_txn_history,
+                            unsigned int n_txn_history_max);
 void ovsdb_txn_history_destroy(struct ovsdb *);
+void ovsdb_txn_history_update(struct ovsdb *db,
+                              unsigned int n_txn_history_max);
 
 #endif /* ovsdb/transaction.h */
diff --git a/tests/ovsdb-server.at b/tests/ovsdb-server.at
index d9389e12f..ad1028e2b 100644
--- a/tests/ovsdb-server.at
+++ b/tests/ovsdb-server.at
@@ -3108,6 +3108,25 @@ WARN|syntax 
"{"dscp":42,"inactivity-probe":10000,"max-backoff":8000,"role":"My-R
  syntax error: Parsing JSON-RPC options failed: Member 'role' is present but 
not allowed here.
 ])
 
+TEST_CONFIG_FILE([relay with transaction-history-limit], [
+{
+    "remotes": { "punix:db.sock": {} },
+    "databases": {
+        "RelaySchema": {
+            "service-model": "relay",
+            "source": {
+                "punix:db2.sock": {
+                    "inactivity-probe": 10000,
+                    "max-backoff": 8000,
+                    "dscp": 42
+                }
+            },
+            "transaction-history-limit": 1000
+        }
+    }
+}
+], [0])
+
 TEST_CONFIG_FILE([unknown config], [
 {
     "remotes": { "punix:db.sock": {} },
@@ -3192,7 +3211,8 @@ TEST_CONFIG_FILE([complex config], [
     },
     "databases": {
         "db_cluster": {
-            "service-model": "clustered"
+            "service-model": "clustered",
+            "transaction-history-limit": 5432
         },
         "OVN_Northbound": {
             "service-model": "relay",
@@ -3201,7 +3221,8 @@ TEST_CONFIG_FILE([complex config], [
                     "max-backoff": 3000,
                     "inactivity-probe": 16000
                 }
-            }
+            },
+            "transaction-history-limit": 1000
         },
         "db": {
             "service-model": "active-backup",

base-commit: 1159994824adcd7b45b0b3549c60a9e9a267298f
-- 
2.43.0


   
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to