Commit 72df0be9 authored by jan.koester's avatar jan.koester
Browse files

test

parent 7522200f
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,3 +19,4 @@ AUTHDB:
        PATH: "/tmp/cluster.db"
    ADMINDB:
        PATH: "/tmp/admin.db"
    VACUUM_INTERVAL: 6
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ AUTHDB:
    KEY: "/etc/authdb/ssl/key.der"
  ADMINDB:
    PATH: "/var/lib/authdb/admin.db"
  VACUUM_INTERVAL: 6
  PLUGINS:
    PATH: "/usr/lib/DEB_HOST_MULTIARCH/authdb"
  CLUSTER:
+77 −0
Original line number Diff line number Diff line
@@ -35,6 +35,9 @@
#include <thread>
#include <map>
#include <memory>
#include <atomic>
#include <condition_variable>
#include <chrono>

#include <cmdplus/cmdplus.h>
#include <confplus/conf.h>
@@ -565,6 +568,41 @@ namespace authdb {
            _DomainCache.erase(key);
        }

        void vacuumAll() {
            /* Vacuum admin backend */
            try {
                size_t before = 0, after = 0;
                _AdminBackend.vacuum(before, after);
                if (before != after) {
                    std::cerr << "[AUTHDB] auto-vacuum admin: "
                              << before << " -> " << after << " bytes"
                              << std::endl;
                }
            } catch (const std::exception &e) {
                std::cerr << "[AUTHDB] auto-vacuum admin error: "
                          << e.what() << std::endl;
            }
            /* Vacuum all cached domain backends */
            {
                std::shared_lock<std::shared_mutex> rl(_DomainCacheMtx);
                for (auto &[key, domBackend] : _DomainCache) {
                    try {
                        size_t before = 0, after = 0;
                        domBackend->vacuum(before, after);
                        if (before != after) {
                            std::cerr << "[AUTHDB] auto-vacuum domain ("
                                      << domBackend->getDomain() << "): "
                                      << before << " -> " << after
                                      << " bytes" << std::endl;
                        }
                    } catch (const std::exception &e) {
                        std::cerr << "[AUTHDB] auto-vacuum domain error: "
                                  << e.what() << std::endl;
                    }
                }
            }
        }

    private:
        AuthBackend        &_AdminBackend;
        std::unique_ptr<Session>  _Session;
@@ -863,8 +901,47 @@ int main(int argc,char *argv[]){
            pluginThreads.back().detach();
        }

        /* Periodic auto-vacuum thread — compacts file backends to prevent
           bloat from deleted/overwritten records which causes slow I/O and
           timeouts over time.  Interval is configurable via config (hours),
           default 6 hours. */
        int vacuumIntervalHours = 6;
        {
            auto *vacKey = config.getKey("/AUTHDB/VACUUM_INTERVAL");
            if (vacKey)
                vacuumIntervalHours = config.getIntValue(vacKey, 0);
            if (vacuumIntervalHours < 1)
                vacuumIntervalHours = 1;
        }

        std::atomic<bool> vacuumRunning{true};
        std::mutex vacuumMtx;
        std::condition_variable vacuumCv;

        std::thread vacuumThread([&](){
            std::cerr << "[AUTHDB] auto-vacuum thread started (interval: "
                      << vacuumIntervalHours << "h)" << std::endl;
            while (vacuumRunning) {
                {
                    std::unique_lock<std::mutex> lk(vacuumMtx);
                    vacuumCv.wait_for(lk,
                        std::chrono::hours(vacuumIntervalHours),
                        [&]{ return !vacuumRunning.load(); });
                }
                if (!vacuumRunning) break;

                authdb.vacuumAll();
            }
            std::cerr << "[AUTHDB] auto-vacuum thread stopped" << std::endl;
        });
        vacuumThread.detach();

        authdb.runEventloop();

        /* Stop auto-vacuum */
        vacuumRunning = false;
        vacuumCv.notify_all();

        /* Shutdown cluster */
        authdb::g_Cluster = nullptr;
        cluster.stop();