Просмотр исходного кода

feat: 2.18.1 (client only) - smartbotic-db-cli configure: versioning on/off, max-versions prune

Asked by smartbotic-vectorapi: the CLI, the only admin tool on a server host,
could neither switch versioning off nor set maxVersions. configure passes the
current default TTL back (0 would clear it), leaves timestamp precision alone,
refuses --max-versions 0 (unlimited, not off) and reports what it changed.
info always prints default_ttl_seconds. Test: test_collection_admin drives the
real CLI; reverted guards fail 3 checks. The server is unchanged (2.18.0).
fszontagh 1 месяц назад
Родитель
Сommit
7a96526c2b

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
CLAUDE.md


+ 1 - 1
VERSION

@@ -1 +1 @@
-2.18.0
+2.18.1

+ 77 - 3
cli/main.cpp

@@ -24,6 +24,7 @@
 #include <cstring>
 #include <fstream>
 #include <iterator>
+#include <chrono>
 #include <iostream>
 #include <sstream>
 #include <string>
@@ -78,6 +79,9 @@ void printUsage() {
               << "  " << C_CYAN << "unlock" << C_RESET << "                               Unlock database (accept writes)\n"
               << "  " << C_CYAN << "status" << C_RESET << "                               Show read-only + recovery status\n"
               << "  " << C_CYAN << "server-info" << C_RESET << "                          Server version, commit and feature names (2.15.0+)\n"
+              << "  " << C_CYAN << "configure" << C_RESET << " <collection> [--versioning on|off] [--max-versions N]\n"
+              << "    " << C_DIM << "versioning off stops NEW history; --max-versions N prunes existing history to the newest N" << C_RESET << "\n"
+              << "    " << C_DIM << "per document (writes recording history in that collection wait while it runs)" << C_RESET << "\n"
               << "  " << C_CYAN << "encryption-verify" << C_RESET << " [collection]         Audit encryption at rest (exit 1 on any finding).\n"
               << "                                       Against a 2.17.0 SERVER it stalls history-recording writes\n"
               << "                                       server-wide while it reads history; 2.17.1+ does not.\n"
@@ -637,6 +641,73 @@ bool execCommand(smartbotic::database::Client& client,
             return true;
         }
 
+        // v2.18.1 - collection administration an operator could not reach
+        // from the CLI (asked by smartbotic-vectorapi before a production
+        // prune): versioning on/off, and maxVersions with its history prune.
+        if (cmd == "configure") {
+            if (params.size() < 3) {
+                printError("usage: configure <collection> [--versioning on|off] [--max-versions N]");
+                return false;
+            }
+            const std::string coll = params[0];
+            std::optional<bool> versioning;
+            std::optional<uint32_t> maxVersions;
+            for (size_t i = 1; i < params.size(); ++i) {
+                if (params[i] == "--versioning" && i + 1 < params.size()) {
+                    const std::string v = params[++i];
+                    if (v != "on" && v != "off") { printError("--versioning takes on or off"); return false; }
+                    versioning = (v == "on");
+                } else if (params[i] == "--max-versions" && i + 1 < params.size()) {
+                    long long n = 0;
+                    try { n = std::stoll(params[++i]); } catch (...) { n = -1; }
+                    if (n < 1 || n > 1000000) {
+                        printError("--max-versions takes 1..1000000. 0 means UNLIMITED history, not off: "
+                                   "to stop recording history use --versioning off");
+                        return false;
+                    }
+                    maxVersions = static_cast<uint32_t>(n);
+                } else {
+                    printError("unknown option " + params[i]);
+                    return false;
+                }
+            }
+            if (!versioning && !maxVersions) { printError("nothing to change"); return false; }
+            const auto before = client.getCollectionInfo(coll);
+            if (!before) { printError("collection not found: " + coll); return false; }
+            if (versioning) {
+                smartbotic::database::Client::CollectionConfig cfg;
+                cfg.timestampPrecision = "";        // "" = leave precision unchanged
+                cfg.versioningEnabled = *versioning;
+                if (!client.configureCollection(coll, cfg)) { printError("configureCollection failed"); return false; }
+                std::cout << "versioning: " << (*versioning ? "on" : "OFF (no new history is recorded; "
+                             "history already written stays, see --max-versions)") << "\n";
+            }
+            if (maxVersions) {
+                // createCollection on an existing collection re-applies its
+                // options, and a default TTL of 0 is a real value: pass the
+                // current one back so the prune cannot clear it. encrypted=false
+                // never downgrades; a vector dimension of 0 is ignored.
+                const bool prunes = before->maxVersions == 0 || *maxVersions < before->maxVersions;
+                const auto t0 = std::chrono::steady_clock::now();
+                (void)client.createCollection(coll, before->defaultTtlSeconds, false, *maxVersions, 0);
+                const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
+                    std::chrono::steady_clock::now() - t0).count();
+                const auto after = client.getCollectionInfo(coll);
+                std::cout << "max_versions: " << before->maxVersions
+                          << (before->maxVersions == 0 ? " (unlimited)" : "") << " -> "
+                          << (after ? after->maxVersions : *maxVersions) << "\n"
+                          << "default_ttl_seconds kept: " << before->defaultTtlSeconds
+                          << (after && after->defaultTtlSeconds != before->defaultTtlSeconds ? "  ⚠ CHANGED" : "") << "\n"
+                          << (prunes ? "history pruned to the newest " + std::to_string(*maxVersions) +
+                                       " version(s) per document"
+                                     : std::string("cap raised: nothing pruned"))
+                          << " in " << ms << " ms\n"
+                          << "  (the history file's size is not visible over the API: compare "
+                             "<data_directory>/history/<collection>.hlog before and after)\n";
+            }
+            return true;
+        }
+
         if (cmd == "info") {
             if (params.empty()) { printError("usage: info <collection>"); return false; }
             auto info = client.getCollectionInfo(params[0]);
@@ -645,9 +716,12 @@ bool execCommand(smartbotic::database::Client& client,
                       << "  documents: " << info->documentCount << "\n"
                       << "  size: " << info->sizeBytes << " bytes\n"
                       << "  encrypted: " << (info->encrypted ? "yes" : "no") << "\n"
-                      << "  max_versions: " << info->maxVersions << "\n";
-            if (info->defaultTtlSeconds > 0)
-                std::cout << "  ttl: " << info->defaultTtlSeconds << "s\n";
+                      << "  max_versions: " << info->maxVersions
+                      << (info->maxVersions == 0 ? " (unlimited)" : "") << "\n"
+                      // v2.18.1 - always printed: `configure --max-versions`
+                      // re-applies it, and an operator must be able to see it.
+                      << "  default_ttl_seconds: " << info->defaultTtlSeconds
+                      << (info->defaultTtlSeconds == 0 ? " (none)" : "") << "\n";
             // v2.11.4 - relations_enforced is not part of CollectionInfo (it
             // is a changeable setting, served by GetCollectionConfig), and
             // its absence here read as "the server cannot report it". Always

+ 4 - 2
docs/ROADMAP.md

@@ -1,6 +1,6 @@
 # Smartbotic Database - Status and Roadmap
 
-**Released version: 2.18.0** (see `VERSION`). Last reviewed: 2026-10-13.
+**Released version: 2.18.1** (client side; server 2.18.0) (see `VERSION`). Last reviewed: 2026-10-13.
 
 > 2.11.5 was prepared but never released on its own: its work (event actors,
 > `removeAs()`) ships inside 2.12.0. There is no 2.11.5 package.
@@ -398,7 +398,9 @@ joins; see 5c), which does not fit in 10 s. Run the container with
   and other options (a TTL of 0 clears an existing default - a trap a consumer
   nearly hit on prod), and nothing can discard a live collection's history or
   the history of deleted documents. A `pruneHistory(collection, keep)` /
-  `discardHistory(collection)` RPC would close both.
+  `discardHistory(collection)` RPC would close both. (2.18.1 added the CLI
+  `configure` command, which avoids the TTL trap by passing the current value
+  back; the API itself still has the trap.)
 - Collection schemas cannot be declared in a migration file yet (the
   existing-row check lives in the SetCollectionSchema handler).
 - ~~**Bogus index declarations on a production instance (found 2026-10-09, predates 2.14.0):**~~ removed

+ 5 - 0
docs/integration-guide.md

@@ -1343,6 +1343,11 @@ collection's operations waited: on 2.17.1 run it at a quiet moment, one
 collection at a time. Before 2.17.1 the call changed the setting and trimmed nothing,
 and no supported call could cut back history kept with `maxVersions = 0`.
 
+- **From the CLI (2.18.1+):** `smartbotic-db-cli configure <collection>
+  [--versioning on|off] [--max-versions N]` does this safely: it passes the
+  current default TTL back, refuses `--max-versions 0` (unlimited, not off),
+  leaves timestamp precision alone, and prints what it changed and how long
+  the prune took. `info` prints `default_ttl_seconds`.
 - **⚠ Pass the collection's current `defaultTtlSeconds`.** `createCollection` on
   an existing collection applies every argument that differs, and a default TTL
   of 0 is a real value ("no default expiry"): a prune call written as

+ 33 - 1
tests/load_test/test_collection_admin.cpp

@@ -28,13 +28,45 @@ static bool refusedReserved(const std::function<void()>& f) {
     return false;
 }
 
-int main() {
+// v2.18.1 - the CLI `configure` command, driven by the .sh between these phases.
+static int cliSeed(Client& c) {
+    c.createCollection("clihist", 3600, false, 0, 0);       // default TTL 3600, unlimited history
+    c.insert("clihist", {{"n", 0}}, "h1");
+    for (int i = 1; i <= 10; ++i) c.update("clihist", "h1", {{"n", i}});
+    ck(c.getVersionHistory("clihist", "h1", 0).versions.size() >= 10, "cli seed: 10 versions recorded");
+    return 0;
+}
+static int cliCheck(Client& c) {
+    const auto h = c.getVersionHistory("clihist", "h1", 0);
+    ck(h.versions.size() <= 2, "cli configure --max-versions 2 pruned history (" + std::to_string(h.versions.size()) + " left)");
+    auto info = c.getCollectionInfo("clihist");
+    ck(info && info->defaultTtlSeconds == 3600, "cli configure kept the default TTL (" +
+       std::to_string(info ? info->defaultTtlSeconds : 0) + ")");
+    ck(info && info->maxVersions == 2, "cli configure set max_versions 2");
+    auto cfg = c.getCollectionConfig("clihist");
+    ck(cfg.versioningEnabled && !*cfg.versioningEnabled, "cli configure --versioning off took effect");
+    ck(cfg.timestampPrecision == "ns", "cli configure left timestamp precision alone (" + cfg.timestampPrecision + ")");
+    const auto before = c.getVersionHistory("clihist", "h1", 0).versions.size();
+    c.update("clihist", "h1", {{"n", 99}});
+    ck(c.getVersionHistory("clihist", "h1", 0).versions.size() == before, "with versioning off an update records no history");
+    auto cur = c.get("clihist", "h1");
+    ck(cur && cur->value("n", -1) == 99, "the live document is current");
+    return 0;
+}
+
+int main(int argc, char** argv) {
+    const std::string phase = argc > 1 ? argv[1] : "";
     Client::Config cfg;
     cfg.address = "127.0.0.1:9033";
     cfg.project = "app";
     cfg.writeRetries = 0;
     Client c(cfg);
     c.connect();
+    if (phase == "cliseed" || phase == "clicheck") {
+        if (phase == "cliseed") cliSeed(c); else cliCheck(c);
+        std::cout << phase << ": " << pass << " passed, " << fail << " failed\n";
+        return fail == 0 ? 0 : 1;
+    }
 
     ck(refusedReserved([&] { c.insert("app:_hidden", {{"name", "plain"}}, "d1"); }),
        "insert into app:_hidden is refused with reserved_collection_name");

+ 11 - 0
tests/load_test/test_collection_admin.sh

@@ -39,5 +39,16 @@ for _ in $(seq 1 120); do grep -q "Notified systemd: READY" "$DIR/server.log" &&
 
 LD_LIBRARY_PATH="$ROOT/build/client" "$DIR/t" 2>&1 | grep -v '\[error\]\|\[info\]'
 rc=${PIPESTATUS[0]}
+
+# v2.18.1 - the CLI configure command (operators have only the CLI on a server).
+CLI="$ROOT/build/cli/smartbotic-db-cli --address 127.0.0.1:$PORT --project app"
+export LD_LIBRARY_PATH="$ROOT/build/client"
+"$DIR/t" cliseed 2>&1 | grep -v '\[info\]' || rc=1
+$CLI configure clihist --max-versions 0 > "$DIR/cli0.txt" 2>&1 && { echo "FAIL: --max-versions 0 was accepted"; rc=1; } || echo "ok   --max-versions 0 refused (0 means unlimited)"
+$CLI configure clihist --versioning off --max-versions 2 2>&1 | grep -v "connected to" | tee "$DIR/cli.txt"
+grep -q "default_ttl_seconds kept: 3600" "$DIR/cli.txt" && echo "ok   the CLI reports the TTL it kept" || { echo "FAIL: no kept-TTL line"; rc=1; }
+grep -q "history pruned to the newest 2" "$DIR/cli.txt" && echo "ok   the CLI reports the prune" || { echo "FAIL: no prune line"; rc=1; }
+$CLI info clihist 2>&1 | grep -q "default_ttl_seconds: 3600" && echo "ok   info prints default_ttl_seconds" || { echo "FAIL: info lacks default_ttl_seconds"; rc=1; }
+"$DIR/t" clicheck 2>&1 | grep -v '\[info\]' || rc=1
 [[ $rc -eq 0 ]] && echo "OK - collection admin e2e passed" || { echo "FAILED"; tail -20 "$DIR/server.log"; }
 exit $rc

Некоторые файлы не были показаны из-за большого количества измененных файлов