Преглед изворни кода

Add memory eviction stats to gRPC API

- Add eviction stats fields to GetStatsResponse proto (evicted_documents,
  total_evictions, recovery_count, max_memory_bytes, threshold/target percents)
- Add getConfig() method to MemoryStore for accessing memory configuration
- Update DatabaseGrpcImpl::GetStats() to populate eviction stats
- Add StatsInfo struct and getStats() method to client library
Fszontagh пре 5 месеци
родитељ
комит
f9e4e9c710

+ 31 - 0
client/include/smartbotic/database/client.hpp

@@ -210,6 +210,37 @@ public:
      */
     std::shared_ptr<void> subscribe(const std::vector<std::string>& collections, EventCallback callback);
 
+    // ===== Statistics =====
+
+    struct StatsInfo {
+        uint64_t totalDocuments = 0;
+        uint64_t totalCollections = 0;
+        uint64_t memoryUsedBytes = 0;
+        uint64_t walSequence = 0;
+        uint64_t walSizeBytes = 0;
+        uint64_t snapshotCount = 0;
+        uint64_t lastSnapshotSequence = 0;
+        uint64_t insertCount = 0;
+        uint64_t updateCount = 0;
+        uint64_t deleteCount = 0;
+        uint64_t queryCount = 0;
+
+        // Memory eviction stats
+        uint64_t evictedDocuments = 0;
+        uint64_t totalEvictions = 0;
+        uint64_t recoveryCount = 0;
+
+        // Memory configuration
+        uint64_t maxMemoryBytes = 0;
+        uint32_t evictionThresholdPercent = 0;
+        uint32_t evictionTargetPercent = 0;
+    };
+
+    /**
+     * Get detailed storage statistics including memory eviction info.
+     */
+    [[nodiscard]] std::optional<StatsInfo> getStats();
+
     // ===== Health =====
 
     struct HealthInfo {

+ 42 - 0
client/src/client.cpp

@@ -736,6 +736,44 @@ public:
         return info;
     }
 
+    std::optional<Client::StatsInfo> getStats() {
+        smartbotic::databasepb::GetStatsRequest request;
+
+        smartbotic::databasepb::GetStatsResponse response;
+        grpc::ClientContext context;
+        setDeadline(context);
+
+        auto status = stub_->GetStats(&context, request, &response);
+        if (!status.ok()) {
+            return std::nullopt;
+        }
+
+        Client::StatsInfo info;
+        info.totalDocuments = response.total_documents();
+        info.totalCollections = response.total_collections();
+        info.memoryUsedBytes = response.memory_used_bytes();
+        info.walSequence = response.wal_sequence();
+        info.walSizeBytes = response.wal_size_bytes();
+        info.snapshotCount = response.snapshot_count();
+        info.lastSnapshotSequence = response.last_snapshot_sequence();
+        info.insertCount = response.insert_count();
+        info.updateCount = response.update_count();
+        info.deleteCount = response.delete_count();
+        info.queryCount = response.query_count();
+
+        // Memory eviction stats
+        info.evictedDocuments = response.evicted_documents();
+        info.totalEvictions = response.total_evictions();
+        info.recoveryCount = response.recovery_count();
+
+        // Memory configuration
+        info.maxMemoryBytes = response.max_memory_bytes();
+        info.evictionThresholdPercent = response.eviction_threshold_percent();
+        info.evictionTargetPercent = response.eviction_target_percent();
+
+        return info;
+    }
+
 private:
     void setDeadline(grpc::ClientContext& context) {
         context.set_deadline(
@@ -885,4 +923,8 @@ std::optional<Client::HealthInfo> Client::getHealthInfo() {
     return impl_->getHealthInfo();
 }
 
+std::optional<Client::StatsInfo> Client::getStats() {
+    return impl_->getStats();
+}
+
 } // namespace smartbotic::database

+ 10 - 0
proto/database.proto

@@ -612,4 +612,14 @@ message GetStatsResponse {
     uint64 update_count = 9;
     uint64 delete_count = 10;
     uint64 query_count = 11;
+
+    // Memory eviction stats
+    uint64 evicted_documents = 12;      // Documents currently evicted to disk
+    uint64 total_evictions = 13;        // Total eviction operations performed
+    uint64 recovery_count = 14;         // Documents recovered from eviction
+
+    // Memory configuration
+    uint64 max_memory_bytes = 15;       // Configured max memory limit
+    uint32 eviction_threshold_percent = 16;  // Start evicting at this % of max
+    uint32 eviction_target_percent = 17;     // Evict down to this % of max
 }

+ 11 - 0
service/src/database_grpc_impl.cpp

@@ -738,6 +738,7 @@ grpc::Status DatabaseGrpcImpl::GetStats(
 ) {
     auto stats = store_.getStats();
     auto persistStats = persistence_.getStats();
+    const auto& config = store_.getConfig();
 
     response->set_total_documents(stats.totalDocuments);
     response->set_total_collections(stats.totalCollections);
@@ -751,6 +752,16 @@ grpc::Status DatabaseGrpcImpl::GetStats(
     response->set_delete_count(stats.deleteCount);
     response->set_query_count(stats.queryCount);
 
+    // Memory eviction stats
+    response->set_evicted_documents(stats.evictedCount);
+    response->set_total_evictions(stats.evictionCount);
+    response->set_recovery_count(stats.recoveryCount);
+
+    // Memory configuration
+    response->set_max_memory_bytes(config.maxMemoryBytes);
+    response->set_eviction_threshold_percent(config.evictionThresholdPercent);
+    response->set_eviction_target_percent(config.evictionTargetPercent);
+
     return grpc::Status::OK;
 }
 

+ 5 - 0
service/src/memory_store.hpp

@@ -256,6 +256,11 @@ public:
 
     [[nodiscard]] Stats getStats() const;
 
+    /**
+     * Get the current configuration.
+     */
+    [[nodiscard]] const Config& getConfig() const { return config_; }
+
     // ===== LRU Eviction =====
 
     /**