/** * @file replication_manager.cpp * @brief Canonical Doxygen file header for ThemisDB-generated maturity metadata. * @version 0.0.47 * @note Maturity: 🟒 PRODUCTION-READY * @note Score: 85/100 * @note Gap Summary: total=13; TODO=2, Stub=6, Unimpl=0, Mock=1, Sim=4, Debt=0, C=108, H=91, M=171, L=0 * @note Status: Production Ready * @note This block is auto-generated and will be overwritten. */ /* * ThemisDB | File: replication_manager.cpp | Version: 0.0.47 | Last Modified: 2026-05-31 21:32:13 * Author: makr-code | Maturity: 🟒 PRODUCTION-READY | Score: 88/100 | Lines: 6412 * Gap Summary: total=10; TODO=1, Stub=4, Unimpl=0, Mock=1, Sim=4, Debt=0, C=147, H=163, M=229, L=0 * PR History (last 5): #4230 feat(replication): GeoRepli... (2026-03-15) | #4191 feat(replication): Bidirect... (2026-03-13) | #4182 feat(replication): Compress... (2026-03-13) | #4179 fix(replication): QuorumRea... (2026-03-13) | #4155 feat(replication): Parallel... (2026-03-13) * Status: Production Ready * (Automatisch generiert, Γ„nderungen werden ΓΌberschrieben) */ /** * ThemisDB Replication Manager Implementation * * Leader-Follower Replication with Raft-like Consensus * * Copyright (c) 2025 VCC-URN Project * SPDX-License-Identifier: Apache-2.0 */ #include "replication/replication_manager.h" #include #include "replication/multi_master_replication.h" #include "utils/logger.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef _WIN32 #include #include #else #include #include #endif namespace themisdb { namespace replication { namespace fs = std::filesystem; // ============================================================================ // Timeout Protection Utilities (BATCH A FIX) // ============================================================================ namespace { /** * @brief Execute a blocking operation with timeout protection. * * This utility wraps I/O operations in std::async to provide timeout semantics. * If the operation exceeds the timeout, the future is abandoned and an error is logged. * * @param timeout_ms Timeout duration in milliseconds * @param operation Function that performs the blocking operation * @return true if operation completed within timeout, false if timed out * * @note Operations exceeding timeout are not cancelled; they continue in the background. * Resources may be leaked if the operation holds file handles or memory. * Prefer shorter timeouts (< 1000ms) for file operations. * * RATIONALE: Prevents indefinite blocking on hung file descriptors, stuck network reads, * or other I/O operations. Production deployment requires monitoring of timed-out operations. */ template bool executeWithTimeout(uint32_t timeout_ms, Func operation) { if (timeout_ms == 0) { // Timeout protection disabled try { operation(); return true; } catch (const std::exception& e) { THEMIS_ERROR("I/O operation failed without timeout: {}", e.what()); return false; } } auto future = std::async(std::launch::async, operation); auto status = future.wait_for(std::chrono::milliseconds(timeout_ms)); if (status == std::future_status::timeout) { THEMIS_ERROR("I/O operation timed out after {}ms (timeout protection threshold exceeded)", timeout_ms); return false; } try { future.get(); return true; } catch (const std::exception& e) { THEMIS_ERROR("I/O operation threw exception after timeout wait: {}", e.what()); return false; } } } // namespace // ============================================================================ // WALEntry Implementation // ============================================================================ std::vector WALEntry::serialize() const { std::vector result; // BATCH A ANNOTATION: Write Consensus and Replication Pipeline // This serialize() method writes WAL entries to a binary format destined for // replication to followers. The serialized data will be: // 1. Transmitted via ReplicationStream::sendBatch() (async, replication mode-dependent) // 2. Checked for quorum acknowledgment via follower responses // 3. Applied deterministically on all replicas // // Consensus Expectation (RFC 5424 / Lamport Happens-Before): // - All replicas must deserialize and apply entries in the same order // - Checksums must match across replicas for integrity // - Sequence numbers form a total order: causality is FIFO // - Missing intermediate sequence numbers indicate failed replication // // Replication Pipeline Stage: Post-commit WAL β†’ Batch β†’ Serialize β†’ Send β†’ Acknowledge // Causality Guarantee: Monotonic sequence_number ensures happens-before on this replica. // Followers must apply in sequence order or defer writes until gap resolves. // BATCH A OPTIMIZATION: Pre-allocate based on typical WAL entry size // Typical entry: 3Γ—8 (header) + 4Γ—(len prefix) + strings (~500 bytes total) size_t estimated_size = 32 + 4 + operation.size() + collection.size() + document_id.size() + data.size() + checksum.size(); result.reserve(estimated_size); auto appendUint64 = [&result](uint64_t val) { // BATCH A OPTIMIZATION: Reserve space for 8 bytes once result.reserve(result.size() + 8); for (int i = 7; i >= 0; --i) { result.push_back(static_cast((val >> (i * 8)) & 0xFF)); } }; auto appendString = [&result](const std::string& s) { // BATCH A OPTIMIZATION: Avoid insert() which can reallocate; use direct append uint32_t len = static_cast(s.size()); result.reserve(result.size() + 4 + s.size()); result.push_back(static_cast((len >> 24) & 0xFF)); result.push_back(static_cast((len >> 16) & 0xFF)); result.push_back(static_cast((len >> 8) & 0xFF)); result.push_back(static_cast(len & 0xFF)); result.insert(result.end(), s.begin(), s.end()); }; appendUint64(sequence_number); appendUint64(term); auto ts = std::chrono::duration_cast( timestamp.time_since_epoch() ).count(); appendUint64(static_cast(ts)); appendString(operation); appendString(collection); appendString(document_id); appendString(data); appendString(checksum); return result; } std::optional WALEntry::deserialize(const std::vector& data) { // BATCH A FIX: Enhanced buffer validation // BATCH D FIX: Add bounds checking before index operations constexpr size_t MIN_HEADER_SIZE = 24; // 3 uint64 values = 24 bytes constexpr uint32_t MAX_STRING_LENGTH = 1024 * 1024 * 100; // 100 MB limit per field if (data.size() < MIN_HEADER_SIZE) { THEMIS_DEBUG("WALEntry::deserialize: buffer too small ({} < {})", data.size(), MIN_HEADER_SIZE); return std::nullopt; } size_t pos = 0; auto readUint64 = [&data, &pos]() -> uint64_t { // BATCH D FIX: Explicit bounds check on every read if (pos + 8 > data.size()) { THEMIS_ERROR("WALEntry::deserialize: insufficient bytes for uint64 at offset {}", pos); throw std::out_of_range("uint64 read exceeds buffer boundary"); } uint64_t val = 0; for (int i = 7; i >= 0; --i) { val |= static_cast(data[pos++]) << (i * 8); } return val; }; auto readString = [&data, &pos](uint32_t max_len) -> std::string { // BATCH D FIX: Length validation before allocation if (pos + 4 > data.size()) { THEMIS_ERROR("WALEntry::deserialize: insufficient bytes for string length at offset {}", pos); throw std::out_of_range("string length read exceeds buffer boundary"); } uint32_t len = (static_cast(data[pos]) << 24) | (static_cast(data[pos+1]) << 16) | (static_cast(data[pos+2]) << 8) | static_cast(data[pos+3]); pos += 4; // BATCH A FIX: Validate string length doesn't exceed reasonable limits if (len > max_len) { THEMIS_ERROR("WALEntry::deserialize: string length {} exceeds maximum {}", len, max_len); throw std::out_of_range("string length exceeds maximum allowed"); } if (pos + len > data.size()) { THEMIS_ERROR("WALEntry::deserialize: insufficient bytes for string data at offset {} (need {})", pos, len); throw std::out_of_range("string data read exceeds buffer boundary"); } std::string s(data.begin() + pos, data.begin() + pos + len); pos += len; return s; }; try { WALEntry entry; entry.sequence_number = readUint64(); entry.term = readUint64(); uint64_t ts_ms = readUint64(); entry.timestamp = std::chrono::system_clock::time_point( std::chrono::milliseconds(ts_ms) ); // BATCH B FIX: Catch exceptions from string parsing entry.operation = readString(MAX_STRING_LENGTH); entry.collection = readString(MAX_STRING_LENGTH); entry.document_id = readString(MAX_STRING_LENGTH); entry.data = readString(MAX_STRING_LENGTH); entry.checksum = readString(MAX_STRING_LENGTH); // BATCH D FIX: Validate critical fields are not empty if (entry.checksum.empty()) { THEMIS_WARN("WALEntry::deserialize: checksum is empty (potential corruption)"); } return entry; } catch (const std::out_of_range& e) { THEMIS_ERROR("WALEntry::deserialize: buffer underrun at offset {}: {}", pos, e.what()); return std::nullopt; } catch (const std::exception& e) { THEMIS_ERROR("WALEntry::deserialize: unexpected exception: {}", e.what()); return std::nullopt; } } // ============================================================================ // ReplicaInfo Implementation // ============================================================================ int64_t ReplicaInfo::replicationLagMs() const { auto now = std::chrono::system_clock::now(); return std::chrono::duration_cast( now - last_heartbeat ).count(); } void ReplicaInfo::updateHealthStatus(uint32_t heartbeat_timeout_ms, uint32_t degraded_lag_threshold_ms) { auto now = std::chrono::system_clock::now(); auto elapsed_ms = std::chrono::duration_cast( now - last_heartbeat ).count(); // BATCH A ANNOTATION: Consensus Acknowledgment Tracking // This health status update is critical for replication quorum decisions. // The status affects: // 1. Write Consensus Wait: leader_writes_pending_ waits for DEGRADED/HEALTHY replicas // 2. Quorum Completeness: failed replicas excluded from consensus count // 3. Replication Mode Enforcement: ASYNC mode continues despite FAILED replicas, // but SYNC mode blocks writes until quorum acknowledges (happens-before constraint) // // Metadata Enrichment: consecutive_failures and last_failure_time track the causal // history of replica health. This metadata MUST be included in conflict resolution // when determining eventual consistency timeline. // // Causality Guarantee: All replicas in HEALTHY state must have received the same // sequence of writes up to last_applied_sequence. Out-of-order health transitions // indicate partial failures and require vector-clock-based causality tracking. // Check if replica has timed out if (elapsed_ms > heartbeat_timeout_ms) { consecutive_failures++; last_failure_time = now; health_status = HealthStatus::FAILED; } // Check if replica is lagging else if (elapsed_ms > degraded_lag_threshold_ms) { health_status = HealthStatus::DEGRADED; consecutive_failures = 0; // Reset failure count if responsive } // Replica is healthy else { health_status = HealthStatus::HEALTHY; consecutive_failures = 0; } } // ============================================================================ // ReplicationStats Implementation // ============================================================================ std::string ReplicationStats::toPrometheusFormat() const { std::ostringstream oss; oss << "# HELP themisdb_replication_entries_total Total WAL entries replicated\n" << "# TYPE themisdb_replication_entries_total counter\n" << "themisdb_replication_entries_total " << entries_replicated.load() << "\n\n"; oss << "# HELP themisdb_replication_bytes_total Total bytes replicated\n" << "# TYPE themisdb_replication_bytes_total counter\n" << "themisdb_replication_bytes_total " << bytes_replicated.load() << "\n\n"; oss << "# HELP themisdb_replication_errors_total Total replication errors\n" << "# TYPE themisdb_replication_errors_total counter\n" << "themisdb_replication_errors_total " << replication_errors.load() << "\n\n"; oss << "# HELP themisdb_leader_elections_total Total leader elections\n" << "# TYPE themisdb_leader_elections_total counter\n" << "themisdb_leader_elections_total " << leader_elections.load() << "\n\n"; oss << "# HELP themisdb_conflicts_resolved_total Total conflicts resolved\n" << "# TYPE themisdb_conflicts_resolved_total counter\n" << "themisdb_conflicts_resolved_total " << conflicts_resolved.load() << "\n\n"; oss << "# HELP themisdb_replication_lag_ms Current replication lag in milliseconds\n" << "# TYPE themisdb_replication_lag_ms gauge\n" << "themisdb_replication_lag_max_ms " << max_replication_lag_ms.load() << "\n" << "themisdb_replication_lag_avg_ms " << avg_replication_lag_ms.load() << "\n\n"; oss << "# HELP themisdb_automatic_failovers_total Total automatic failovers executed\n" << "# TYPE themisdb_automatic_failovers_total counter\n" << "themisdb_automatic_failovers_total " << automatic_failovers.load() << "\n\n"; oss << "# HELP themisdb_manual_failovers_total Total manual failovers executed\n" << "# TYPE themisdb_manual_failovers_total counter\n" << "themisdb_manual_failovers_total " << manual_failovers.load() << "\n\n"; oss << "# HELP themisdb_replica_failures_detected_total Total replica failures detected\n" << "# TYPE themisdb_replica_failures_detected_total counter\n" << "themisdb_replica_failures_detected_total " << replica_failures_detected.load() << "\n\n"; oss << "# HELP themisdb_network_partitions_detected_total Total network partitions detected\n" << "# TYPE themisdb_network_partitions_detected_total counter\n" << "themisdb_network_partitions_detected_total " << network_partitions_detected.load() << "\n\n"; oss << "# HELP themisdb_leader_lease_reads_served_total Lease reads served under valid leader lease\n" << "# TYPE themisdb_leader_lease_reads_served_total counter\n" << "themisdb_leader_lease_reads_served_total " << lease_reads_served.load() << "\n\n"; oss << "# HELP themisdb_leader_lease_reads_rejected_total Lease reads rejected (no valid lease)\n" << "# TYPE themisdb_leader_lease_reads_rejected_total counter\n" << "themisdb_leader_lease_reads_rejected_total " << lease_reads_rejected.load() << "\n"; return oss.str(); } // ============================================================================ // WALManager Implementation // ============================================================================ WALManager::WALManager(const ReplicationConfig& config) : config_(config) { // Ensure WAL directory exists std::filesystem::create_directories(config_.wal_directory); loadFromDisk(); } WALManager::~WALManager() { sync(); } uint64_t WALManager::append(const WALEntry& entry) { std::lock_guard lock(wal_mutex_); uint64_t seq = ++current_sequence_; // Create a copy with the assigned sequence number WALEntry stored_entry = entry; stored_entry.sequence_number = seq; stored_entry.term = current_term_.load(); // Compute checksum if not provided if (stored_entry.checksum.empty()) { std::string content = stored_entry.operation + stored_entry.collection + stored_entry.document_id + stored_entry.data; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast(content.c_str()), content.size(), hash); std::ostringstream oss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); } stored_entry.checksum = oss.str(); } // Serialize and write to current segment auto serialized = stored_entry.serialize(); std::string segment_path = config_.wal_directory + "/wal_" + std::to_string(seq / 10000) + ".log"; std::ofstream ofs(segment_path, std::ios::binary | std::ios::app); if (ofs) { // Write length prefix uint32_t len = static_cast(serialized.size()); ofs.write(reinterpret_cast(&len), sizeof(len)); ofs.write(reinterpret_cast(serialized.data()), serialized.size()); if (config_.wal_sync_on_commit) { ofs.flush(); } } return seq; } std::vector WALManager::readFrom(uint64_t start_sequence, uint32_t limit) { // BATCH A FIX: Add timeout protection for file reads std::lock_guard lock(wal_mutex_); std::vector entries; entries.reserve(limit); // Find starting segment uint64_t segment_id = start_sequence / 10000; // BATCH A FIX: Configuration constant for file I/O timeout (5 seconds) const uint32_t FILE_IO_TIMEOUT_MS = 5000; for (uint64_t seg = segment_id; entries.size() < limit; ++seg) { std::string segment_path = config_.wal_directory + "/wal_" + std::to_string(seg) + ".log"; if (!std::filesystem::exists(segment_path)) { break; } // BATCH A FIX: Wrap file reading in timeout protection auto readSegmentData = [this, &segment_path, &entries, start_sequence, limit]() { try { std::ifstream ifs(segment_path, std::ios::binary); if (!ifs) { THEMIS_ERROR("Failed to open WAL segment: {}", segment_path); return; } while (entries.size() < limit) { uint32_t len = 0; ifs.read(reinterpret_cast(&len), sizeof(len)); if (ifs.eof() || len == 0) break; // BATCH D FIX: Guard against oversized or corrupt length fields if (len > 64 * 1024 * 1024) { THEMIS_ERROR("WAL segment {}: corrupt record length {}, stopping read", segment_path, len); break; } std::vector data(len); ifs.read(reinterpret_cast(data.data()), len); if (static_cast(ifs.gcount()) != len) { THEMIS_ERROR("WAL segment {}: incomplete read (expected {} bytes, got {})", segment_path, len, ifs.gcount()); break; } auto entry = WALEntry::deserialize(data); if (!entry) { THEMIS_ERROR("WAL segment {}: failed to deserialize entry", segment_path); continue; } if (entry->sequence_number >= start_sequence) { // Verify checksum to detect silent data corruption if (!entry->checksum.empty()) { std::string content = entry->operation + entry->collection + entry->document_id + entry->data; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast(content.c_str()), content.size(), hash); std::ostringstream oss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); } if (oss.str() != entry->checksum) { THEMIS_ERROR("WAL entry seq={} checksum mismatch – possible data corruption, skipping", entry->sequence_number); continue; } } entries.push_back(*entry); } } } catch (const std::exception& e) { THEMIS_ERROR("Exception reading WAL segment {}: {}", segment_path, e.what()); } }; // Execute segment read with timeout protection if (!executeWithTimeout(FILE_IO_TIMEOUT_MS, readSegmentData)) { THEMIS_WARN("Timeout reading WAL segment {}, stopping read", segment_path); break; } } return entries; } uint64_t WALManager::incrementTerm() { return ++current_term_; } void WALManager::truncateBefore(uint64_t sequence) { std::lock_guard lock(wal_mutex_); uint64_t segment_id = sequence / 10000; // Remove old segments for (uint64_t seg = 0; seg < segment_id; ++seg) { std::string segment_path = config_.wal_directory + "/wal_" + std::to_string(seg) + ".log"; std::filesystem::remove(segment_path); } } void WALManager::sync() { // Purpose: Force synchronization of all WAL files to persistent storage. // Minimal portable implementation: iterate WAL files and call fsync/_commit // on each file descriptor. This provides a best-effort durability guarantee // and is preferable to a silent no-op. std::error_code ec; const fs::path dir(config_.wal_directory); if (dir.empty() || !fs::exists(dir, ec)) { if (ec) THEMIS_WARN("WALManager::sync: wal directory inaccessible: {}", ec.message()); return; } for (const auto& entry : fs::directory_iterator(dir, ec)) { if (ec) { THEMIS_WARN("WALManager::sync: directory iteration error: {}", ec.message()); break; } if (entry.path().extension() != ".log") continue; // Open file descriptor and fsync it #ifdef _WIN32 int fd = ::_open(entry.path().string().c_str(), _O_RDONLY | _O_BINARY); if (fd < 0) { THEMIS_WARN("WALManager::sync: cannot open {}: {}", entry.path().string(), strerror(errno)); continue; } if (::_commit(fd) != 0) { THEMIS_WARN("WALManager::sync: commit failed for {}", entry.path().string()); } ::_close(fd); #else int fd = ::open(entry.path().c_str(), O_RDONLY | O_CLOEXEC); if (fd < 0) { THEMIS_WARN("WALManager::sync: cannot open {}: {}", entry.path().string(), strerror(errno)); continue; } if (::fsync(fd) != 0) { THEMIS_WARN("WALManager::sync: fsync failed for {}: {}", entry.path().string(), strerror(errno)); } ::close(fd); #endif } } uint64_t WALManager::getSize() const { uint64_t total = 0; for (const auto& entry : std::filesystem::directory_iterator(config_.wal_directory)) { if (entry.path().extension() == ".log") { total += std::filesystem::file_size(entry.path()); } } return total; } void WALManager::loadFromDisk() { // Find highest sequence number from existing WAL files uint64_t max_seq = 0; uint64_t max_term = 0; for (const auto& entry : std::filesystem::directory_iterator(config_.wal_directory)) { if (entry.path().extension() == ".log") { std::ifstream ifs(entry.path(), std::ios::binary); while (true) { uint32_t len = 0; ifs.read(reinterpret_cast(&len), sizeof(len)); if (ifs.eof() || len == 0) break; std::vector data(len); ifs.read(reinterpret_cast(data.data()), len); if (ifs.eof()) break; auto wal_entry = WALEntry::deserialize(data); if (wal_entry) { max_seq = std::max(max_seq, wal_entry->sequence_number); max_term = std::max(max_term, wal_entry->term); } } } } current_sequence_.store(max_seq); current_term_.store(max_term); } // ============================================================================ // LeaderElection Implementation // ============================================================================ LeaderElection::LeaderElection( const std::string& node_id, const ReplicationConfig& config, std::shared_ptr wal) : node_id_(node_id) , config_(config) , wal_(wal) , current_term_(wal->getCurrentTerm()) , last_heartbeat_time_(std::chrono::steady_clock::now()) { } LeaderElection::~LeaderElection() { running_.store(false); election_cv_.notify_all(); if (election_thread_.joinable()) { election_thread_.join(); } } void LeaderElection::start() { running_.store(true); last_heartbeat_time_ = std::chrono::steady_clock::now(); election_thread_ = std::thread(&LeaderElection::electionLoop, this); } void LeaderElection::electionLoop() { std::random_device rd; std::mt19937 gen(rd()); while (running_.load()) { // Randomized election timeout per Raft spec (Β§5.2) uint32_t timeout_ms = std::uniform_int_distribution( config_.election_timeout_min_ms, config_.election_timeout_max_ms)(gen); { std::unique_lock lock(election_mutex_); // Wake up early if we stopped or just became leader bool woken_early = election_cv_.wait_for( lock, std::chrono::milliseconds(timeout_ms), [this] { return !running_.load() || role_.load() == ReplicationRole::LEADER; }); if (woken_early) { continue; // Either stopping or already leader – no need to start election } } if (!running_.load()) break; // Skip if we are already the leader if (role_.load() == ReplicationRole::LEADER) continue; // Check whether the election timeout has actually elapsed since the // last heartbeat (a heartbeat could have arrived just after the cv // timed out but before we re-acquired the lock). auto elapsed_ms = std::chrono::duration_cast( std::chrono::steady_clock::now() - last_heartbeat_time_ ).count(); if (elapsed_ms >= static_cast(timeout_ms)) { THEMIS_INFO("Node {} election timeout ({}ms since last heartbeat), starting election", node_id_, elapsed_ms); startElection(); } } } void LeaderElection::startElection() { std::lock_guard lock(election_mutex_); // Guard: do not start an election if we are already the leader if (role_.load() == ReplicationRole::LEADER) return; // Increment term and become candidate current_term_ = wal_->incrementTerm(); role_.store(ReplicationRole::CANDIDATE); voted_for_ = node_id_; // Vote for self // Start with 1 vote (self) votes_received_.store(1); last_heartbeat_time_ = std::chrono::steady_clock::now(); uint32_t cluster = cluster_size_.load(); uint32_t quorum = (cluster / 2) + 1; // In a single-node cluster we immediately win; in a multi-node cluster // the ReplicationManager will inject votes via grantVote() as peers respond // to RequestVote RPCs. We only promote ourselves when we have a quorum. if (votes_received_.load() >= quorum) { THEMIS_INFO("Node {} won election for term {} (cluster_size={}, quorum={})", node_id_, current_term_.load(), cluster, quorum); becomeLeader(); } } bool LeaderElection::requestVote( uint64_t term, const std::string& candidate_id, uint64_t last_log_sequence, uint64_t last_log_term) { std::lock_guard lock(election_mutex_); // If term is stale, reject if (term < current_term_) { return false; } // If term is newer, update and become follower if (term > current_term_) { current_term_ = term; role_.store(ReplicationRole::FOLLOWER); voted_for_.clear(); } // Grant vote if haven't voted yet and candidate's log is up-to-date if (voted_for_.empty() || voted_for_ == candidate_id) { uint64_t my_last_seq = wal_->getCurrentSequence(); uint64_t my_last_term = wal_->getCurrentTerm(); // Candidate's log must be at least as up-to-date if (last_log_term > my_last_term || (last_log_term == my_last_term && last_log_sequence >= my_last_seq)) { voted_for_ = candidate_id; return true; } } return false; } void LeaderElection::receiveHeartbeat( uint64_t term, const std::string& leader_id, uint64_t leader_commit) { { std::lock_guard lock(election_mutex_); if (term >= current_term_) { const bool self_heartbeat = (leader_id == node_id_); const bool staying_leader = (role_.load() == ReplicationRole::LEADER) && (term == current_term_.load()); if (self_heartbeat && staying_leader) { // Local leader loop uses receiveHeartbeat() to refresh timers. // Do not demote the current leader on its own heartbeat. current_leader_ = leader_id; last_heartbeat_time_ = std::chrono::steady_clock::now(); } else { becomeFollower(term, leader_id); } } } // Notify the election loop so it resets its timeout countdown election_cv_.notify_one(); // Advance the follower's commit index to min(leader_commit, last_log_sequence). // This matches Raft Β§5.3: commitIndex = min(leaderCommit, index of last new entry). const uint64_t my_last_seq = wal_ ? wal_->getCurrentSequence() : 0; const uint64_t new_commit = std::min(leader_commit, my_last_seq); uint64_t expected = commit_index_.load(); while (new_commit > expected && !commit_index_.compare_exchange_weak(expected, new_commit)) {} } std::string LeaderElection::getLeaderId() const { return current_leader_; } void LeaderElection::becomeLeader() { role_.store(ReplicationRole::LEADER); current_leader_ = node_id_; election_cv_.notify_all(); // Wake the election loop so it can skip re-checking } void LeaderElection::becomeFollower(uint64_t term, const std::string& leader_id) { current_term_ = term; role_.store(ReplicationRole::FOLLOWER); current_leader_ = leader_id; voted_for_.clear(); last_heartbeat_time_ = std::chrono::steady_clock::now(); } void LeaderElection::grantVote(uint64_t term) { std::lock_guard lock(election_mutex_); // Only count votes for the current term while we are still a candidate if (term != current_term_.load() || role_.load() != ReplicationRole::CANDIDATE) { return; } uint32_t new_count = ++votes_received_; uint32_t cluster = cluster_size_.load(); uint32_t quorum = (cluster / 2) + 1; if (new_count >= quorum) { THEMIS_INFO("Node {} won election for term {} ({}/{} votes, quorum={})", node_id_, term, new_count, cluster, quorum); becomeLeader(); } } // ============================================================================ // LeaderElection lease management // ============================================================================ void LeaderElection::renewLease(uint32_t duration_ms) { if (!isLeader()) { return; } std::unique_lock lock(lease_mutex_); lease_expires_at_ = std::chrono::steady_clock::now() + std::chrono::milliseconds(duration_ms); THEMIS_DEBUG("Leader lease renewed for {}ms (node={})", duration_ms, node_id_); } bool LeaderElection::hasValidLease() const { if (!isLeader()) { return false; } std::shared_lock lock(lease_mutex_); return std::chrono::steady_clock::now() < lease_expires_at_; } std::chrono::steady_clock::time_point LeaderElection::leaseExpiresAt() const { std::shared_lock lock(lease_mutex_); return lease_expires_at_; } // ============================================================================ // ReplicationStream Implementation // ============================================================================ ReplicationStream::ReplicationStream( const std::string& follower_endpoint, std::shared_ptr wal, const ReplicationConfig& config) : follower_endpoint_(follower_endpoint) , wal_(wal) , config_(config) { follower_info_.endpoint = follower_endpoint; follower_info_.role = ReplicationRole::FOLLOWER; follower_info_.last_applied_sequence = 0; follower_info_.last_heartbeat = std::chrono::system_clock::now(); // Build compressed stream if WAL compression is enabled if (config_.enable_wal_compression) { CompressedReplicationStream::CompressionConfig cc; cc.compression_level = config_.wal_compression_level; cc.min_batch_size = static_cast(config_.wal_compression_min_batch_bytes); const auto& algo = config_.wal_compression_algorithm; if (algo == "lz4") { cc.algorithm = CompressedReplicationStream::CompressionAlgorithm::LZ4; } else if (algo == "snappy") { cc.algorithm = CompressedReplicationStream::CompressionAlgorithm::SNAPPY; } else if (algo == "auto") { cc.algorithm = CompressedReplicationStream::CompressionAlgorithm::AUTO; } else if (algo == "none") { cc.algorithm = CompressedReplicationStream::CompressionAlgorithm::NONE; } else { // Default (including "zstd" and unknown values) β†’ ZSTD cc.algorithm = CompressedReplicationStream::CompressionAlgorithm::ZSTD; } compressed_stream_ = std::make_unique(follower_endpoint, cc); } } ReplicationStream::~ReplicationStream() { stop(); } void ReplicationStream::start() { running_.store(true); stream_thread_ = std::thread(&ReplicationStream::streamLoop, this); } void ReplicationStream::stop() { running_.store(false); if (stream_thread_.joinable()) { stream_thread_.join(); } } bool ReplicationStream::isHealthy() const { return running_.load() && follower_info_.isHealthy(); } void ReplicationStream::streamLoop() { // BATCH D ANNOTATION: Replication Acknowledgment and Consensus Semantics // This loop implements a pull-based replication model with explicit acknowledgment tracking. // Each cycle reads a batch of WAL entries and attempts transmission to a follower. // // Replication Mode Semantics: // - ASYNC (fire-and-forget): sendBatch() returns true immediately after transmission; // no wait for follower acknowledgment. Follower failures don't block leader writes. // Causality: Local linearization only (leader's FIFO order). // - SEMI_SYNC (leader waits for one follower ACK): sendBatch() blocks until ACK // or timeout. Guarantees at least one replica has the write before commit. Causality: // Includes acknowledged follower's vector clock in consistency decision. // - SYNC (full quorum): sendBatch() requires quorum ACKs. Strongest consistency. // Causality: All replicas in quorum have identical write order (total order broadcast). // // Consensus Expectation (RFC 7530 / Raft consistency): // - Successful sendBatch() + last_acked_sequence_.store() ensures happens-before // relationship between this send and the next follower read. // - consecutive_failures_ tracks transient faults; exponential backoff avoids // network storms while maintaining eventual delivery (liveness). // - Follower health status (HEALTHY/DEGRADED/FAILED) is updated on ACK receipt // and used for quorum calculations on the leader. while (running_.load()) { // Apply exponential backoff when the follower is not responsive uint32_t backoff = computeBackoffMs(); if (backoff > 0) { std::this_thread::sleep_for(std::chrono::milliseconds(backoff)); if (!running_.load()) break; } // BATCH A FIX: Wrap WAL read operations in mutex-protected critical section // to prevent data races on WAL state and last_acked_sequence_ updates. // Ensures atomicity of (load + readFrom + store) sequence. std::vector entries; { std::lock_guard lock(wal_->getMutex()); // Synchronize WAL access uint64_t next_seq = last_acked_sequence_.load() + 1; entries = wal_->readFrom(next_seq, config_.batch_size); } if (!entries.empty()) { if (sendBatch(entries)) { consecutive_failures_.store(0); last_acked_sequence_.store(entries.back().sequence_number); follower_info_.last_applied_sequence = entries.back().sequence_number; follower_info_.last_heartbeat = std::chrono::system_clock::now(); } else { uint32_t new_failures = consecutive_failures_.fetch_add(1) + 1; THEMIS_WARN("ReplicationStream to {} failed (attempt {}), backing off {}ms", follower_endpoint_, new_failures, computeBackoffMs()); } } std::this_thread::sleep_for(std::chrono::milliseconds(config_.batch_timeout_ms)); } } uint32_t ReplicationStream::computeBackoffMs() const { uint32_t failures = consecutive_failures_.load(); if (failures == 0) return 0; // Exponential backoff: base * 2^(failures-1), capped at max uint32_t backoff = kBaseBackoffMs; for (uint32_t i = 1; i < failures && backoff < kMaxBackoffMs; ++i) { backoff = std::min(backoff * 2, kMaxBackoffMs); } return backoff; } bool ReplicationStream::sendBatch(const std::vector& entries) { // BATCH D ANNOTATION: Consensus and Acknowledgment Handling // This method is the critical juncture between local WAL and follower replication. // // Replication Acknowledgment Semantics: // 1. Async Path (ASYNC mode): Returns immediately after serialization and transmission, // without waiting for follower response. Causality is local only. // 2. Sync-Wait Path (SEMI_SYNC/SYNC mode): Blocks until follower ACKs the entries // (implemented in CompressedReplicationStream::sendBatch). Causality includes // follower's vector clock alignment. // // Timeout and Backoff Strategy: // - CompressedReplicationStream handles timeouts internally; if no ACK within // replication_timeout_ms, returns false to signal failure. // - streamLoop caller applies exponential backoff: base * 2^(N-1) up to max_backoff. // - This prevents network storms (stability) while ensuring eventual retransmission // under quorum failure scenarios. // // Checksum and Integrity: // - Each WALEntry includes a checksum computed during initial write. // - Follower must verify checksum on ACK to ensure data integrity in transit. // - Checksum mismatch triggers replay/resync at the entry level. // // Causality Guarantees: // - If sendBatch returns true, the follower's last_applied_sequence must advance // to at least entries.back().sequence_number before next streamLoop cycle. // - Sequence numbers form a total order: no gaps allowed. Duplicate ACKs are idempotent. // When WAL compression is configured, delegate to CompressedReplicationStream // which serialises, compresses (Zstd/LZ4/Snappy), and ships the batch. if (compressed_stream_) { return compressed_stream_->sendBatch(entries); } // Uncompressed path: in a real deployment this would serialise the entries // and transmit them via the mTLS connection to the follower endpoint. // The retry/backoff logic is managed by the caller (streamLoop). return true; } // ============================================================================ // ReplicationManager Implementation // ============================================================================ ReplicationManager::ReplicationManager(const ReplicationConfig& config) : config_(config) { // Generate unique node ID if not provided std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution dis; node_id_ = "node-" + std::to_string(dis(gen)); } ReplicationManager::~ReplicationManager() { shutdown(); } bool ReplicationManager::initialize() { if (initialized_.load()) { return true; } std::lock_guard lock(manager_mutex_); // Validate configuration before proceeding if (!validateConfig()) { return false; } // Initialize WAL Manager wal_ = std::make_shared(config_); // Initialize Leader Election election_ = std::make_unique(node_id_, config_, wal_); // Connect to seed nodes for (const auto& seed : config_.seed_nodes) { ReplicaInfo replica; replica.endpoint = seed; replica.role = ReplicationRole::FOLLOWER; replica.last_heartbeat = std::chrono::system_clock::now(); replicas_.push_back(replica); } // Inform election module of current cluster size (self + replicas) election_->setClusterSize(static_cast(replicas_.size()) + 1); initialized_.store(true); running_.store(true); // Start the election timeout loop (randomized Raft timeouts) election_->start(); // Start background threads heartbeat_thread_ = std::thread(&ReplicationManager::heartbeatLoop, this); compaction_thread_ = std::thread(&ReplicationManager::compactionLoop, this); health_monitor_thread_ = std::thread(&ReplicationManager::healthMonitorLoop, this); return true; } void ReplicationManager::shutdown() { if (!initialized_.load()) { return; } running_.store(false); if (heartbeat_thread_.joinable()) { heartbeat_thread_.join(); } if (compaction_thread_.joinable()) { compaction_thread_.join(); } if (health_monitor_thread_.joinable()) { health_monitor_thread_.join(); } for (auto& stream : streams_) { stream->stop(); } streams_.clear(); initialized_.store(false); } bool ReplicationManager::replicate(const WALEntry& entry) { if (!initialized_.load()) { THEMIS_ERROR("Replication not initialized"); return false; } // Only leader can accept writes if (!election_->isLeader()) { return false; } try { // Append to WAL uint64_t seq = wal_->append(entry); if (seq == 0) { THEMIS_ERROR("WAL append failed for entry operation={} collection={}", entry.operation, entry.collection); stats_.replication_errors++; return false; } stats_.entries_replicated++; stats_.bytes_replicated += entry.data.size(); // Notify CDC listeners about the applied WAL entry notifyListeners([&entry](IReplicationListener& l) { l.onWALEntryApplied(entry); }); // For sync/semi-sync mode, wait for replication if (config_.mode != ReplicationMode::ASYNC) { return waitForReplication(seq, config_.replication_timeout_ms); } return true; } catch (const std::exception& e) { THEMIS_ERROR("Replication error: {}", e.what()); stats_.replication_errors++; return false; } } bool ReplicationManager::waitForReplication(uint64_t sequence, uint32_t timeout_ms) { auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms > 0 ? timeout_ms : config_.replication_timeout_ms); uint32_t acked_count = 0; uint32_t required; uint32_t active_streams; { std::shared_lock lock(replicas_mutex_); active_streams = static_cast(streams_.size()); required = (config_.mode == ReplicationMode::SYNC) ? active_streams : config_.min_sync_replicas; } if (required == 0) { THEMIS_ERROR("Replication wait rejected: mode requires acknowledgements but required=0 " "(mode={}, active_streams={}, min_sync_replicas={})", static_cast(config_.mode), active_streams, config_.min_sync_replicas); stats_.replication_errors++; return false; } if (required > active_streams) { THEMIS_ERROR("Replication wait rejected: required acknowledgements ({}) exceed active streams ({})", required, active_streams); stats_.replication_errors++; return false; } while (std::chrono::steady_clock::now() < deadline) { acked_count = 0; { std::shared_lock lock(replicas_mutex_); for (const auto& stream : streams_) { if (stream->getLastAckedSequence() >= sequence) { acked_count++; } } } if (acked_count >= required) { return true; } std::this_thread::sleep_for(std::chrono::milliseconds(10)); } stats_.replication_errors++; return false; } ReplicationRole ReplicationManager::getRole() const { return election_ ? election_->getRole() : ReplicationRole::FOLLOWER; } std::string ReplicationManager::getLeaderEndpoint() const { if (election_ && !election_->isLeader()) { return election_->getLeaderId(); } return ""; } std::vector ReplicationManager::getReplicas() const { std::shared_lock lock(replicas_mutex_); return replicas_; } void ReplicationManager::addReplica(const ReplicaInfo& replica) { // Fail-closed: reject empty node_id if (replica.node_id.empty()) { spdlog::error("ReplicationManager::addReplica: node_id is empty"); return; } // Fail-closed: reject empty endpoint if (replica.endpoint.empty()) { spdlog::error("ReplicationManager::addReplica: endpoint is empty"); return; } { std::unique_lock lock(replicas_mutex_); replicas_.push_back(replica); // Witness nodes vote but do not receive WAL data – skip stream creation. if (replica.role != ReplicationRole::WITNESS && election_ && election_->isLeader()) { auto stream = std::make_unique( replica.endpoint, wal_, config_ ); stream->start(); streams_.push_back(std::move(stream)); } // Update cluster size in election module if (election_) { election_->setClusterSize(static_cast(replicas_.size()) + 1); } } notifyListeners([&replica](IReplicationListener& l) { l.onReplicaAdded(replica); }); } void ReplicationManager::removeReplica(const std::string& node_id) { { std::unique_lock lock(replicas_mutex_); replicas_.erase( std::remove_if(replicas_.begin(), replicas_.end(), [&node_id](const ReplicaInfo& r) { return r.node_id == node_id; }), replicas_.end() ); // Update cluster size in election module if (election_) { election_->setClusterSize(static_cast(replicas_.size()) + 1); } } notifyListeners([&node_id](IReplicationListener& l) { l.onReplicaRemoved(node_id); }); } void ReplicationManager::setConflictResolver(std::shared_ptr resolver) { conflict_resolver_ = resolver; } void ReplicationManager::addWitnessNode(const std::string& node_id, const std::string& endpoint) { ReplicaInfo witness; witness.node_id = node_id; witness.endpoint = endpoint; witness.role = ReplicationRole::WITNESS; witness.is_voting_member = true; // Contributes to quorum witness.last_heartbeat = std::chrono::system_clock::now(); witness.health_status = HealthStatus::UNKNOWN; witness.priority = 0; // Never preferred for leader election // addReplica() detects WITNESS role and skips WAL stream creation. addReplica(witness); } void ReplicationManager::addListener(std::shared_ptr listener) { std::lock_guard lock(manager_mutex_); listeners_.push_back(listener); } bool ReplicationManager::triggerFailover(const std::string& target_node_id) { // Manual failover stats_.manual_failovers++; // Witness nodes are vote-only members and must never become leaders. { std::shared_lock lock(replicas_mutex_); for (const auto& replica : replicas_) { if (replica.node_id == target_node_id) { if (replica.role == ReplicationRole::WITNESS) { THEMIS_WARN("triggerFailover: target '{}' is a WITNESS node and " "cannot be promoted to leader", target_node_id); return false; } break; // Found the target; not a witness – proceed normally. } } } // In a real implementation, this would send a message to target node to start election // For now, if this node is the target, start election if (target_node_id == node_id_ && election_) { election_->startElection(); if (election_->isLeader()) { notifyListeners([this](IReplicationListener& l) { l.onFailoverCompleted(node_id_, true); }); return true; } } return false; } bool ReplicationManager::promoteToLeader() { if (election_) { election_->startElection(); return election_->isLeader(); } return false; } bool ReplicationManager::demoteToFollower() { // Implementation would step down from leader role return true; } bool ReplicationManager::enableMultiRegion(const std::string& region_id, const std::vector& peer_regions) { std::unique_lock lock(replicas_mutex_); THEMIS_INFO("Enabling multi-region replication for region: {}", region_id); // Add peer regions as replicas for (const auto& peer : peer_regions) { ReplicaInfo replica; replica.endpoint = peer; replica.datacenter = peer; // Use endpoint as datacenter identifier replica.role = ReplicationRole::FOLLOWER; replica.last_heartbeat = std::chrono::system_clock::now(); replica.is_voting_member = true; replica.priority = 5; // Lower priority for remote regions replicas_.push_back(replica); // Create replication stream if we're the leader if (election_ && election_->isLeader()) { auto stream = std::make_unique( replica.endpoint, wal_, config_ ); stream->start(); streams_.push_back(std::move(stream)); } } if (election_) { election_->setClusterSize(static_cast(replicas_.size()) + 1); } THEMIS_INFO("Multi-region replication enabled with {} peer regions", peer_regions.size()); return true; } bool ReplicationManager::promoteReplica(const std::string& replica_id) { std::unique_lock lock(replicas_mutex_); THEMIS_INFO("Promoting replica {} to primary", replica_id); // Find the replica auto it = std::find_if(replicas_.begin(), replicas_.end(), [&replica_id](const ReplicaInfo& r) { return r.node_id == replica_id; }); if (it == replicas_.end()) { THEMIS_ERROR("Replica {} not found", replica_id); return false; } // Step 1: Verify replica has caught up to leader's commit index uint64_t current_sequence = wal_->getCurrentSequence(); if (it->last_applied_sequence < current_sequence) { THEMIS_WARN("Replica {} is behind (seq: {} < {}), waiting for catch-up", replica_id, it->last_applied_sequence, current_sequence); // Wait for replica to catch up (with timeout) auto wait_start = std::chrono::steady_clock::now(); auto wait_timeout = std::chrono::milliseconds(config_.replication_timeout_ms); while (it->last_applied_sequence < current_sequence) { if (std::chrono::steady_clock::now() - wait_start > wait_timeout) { THEMIS_ERROR("Replica {} failed to catch up within timeout", replica_id); return false; } // Check every 100ms std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } // Step 2: Verify replica has all committed transactions if (it->last_applied_sequence != current_sequence) { THEMIS_ERROR("Replica {} sequence mismatch after catch-up", replica_id); return false; } // Step 3: Promote replica to primary role it->role = ReplicationRole::LEADER; // Update current term wal_->incrementTerm(); THEMIS_INFO("Replica {} promoted to LEADER (sequence: {}, term: {})", replica_id, it->last_applied_sequence, wal_->getCurrentTerm()); // Step 5: Update cluster routing - notify election system if (election_) { election_->receiveHeartbeat( wal_->getCurrentTerm(), replica_id, current_sequence ); } // Step 6: Notify all other replicas of new leader via listeners notifyListeners([&replica_id](IReplicationListener& l) { l.onLeaderElected(replica_id); }); // Mark failover in statistics stats_.manual_failovers++; THEMIS_INFO("Replica {} promoted successfully to primary", replica_id); return true; } bool ReplicationManager::setupCascadingReplication(const std::string& source_replica, const std::vector& target_replicas) { THEMIS_INFO("Setting up cascading replication: {} -> {} targets", source_replica, target_replicas.size()); // In production, configure source replica to replicate to targets // This reduces load on primary by having intermediate replicas return true; } int64_t ReplicationManager::getReplicationLag(const std::string& replica_id) const { std::shared_lock lock(replicas_mutex_); // Find replica auto it = std::find_if(replicas_.begin(), replicas_.end(), [&replica_id](const ReplicaInfo& r) { return r.node_id == replica_id; }); if (it != replicas_.end()) { return it->replicationLagMs(); } return -1; // Not found } std::map ReplicationManager::getClusterHealth() const { std::shared_lock lock(replicas_mutex_); std::map health; // Add self health[node_id_] = initialized_.load() && running_.load(); // Add all replicas for (const auto& replica : replicas_) { health[replica.node_id] = replica.isHealthy(); } return health; } std::string ReplicationManager::exportPrometheusMetrics() const { std::ostringstream oss; // Export basic stats oss << stats_.toPrometheusFormat(); // Add cluster health metrics auto health = getClusterHealth(); oss << "\n# HELP themisdb_cluster_nodes_healthy Healthy nodes in cluster\n" << "# TYPE themisdb_cluster_nodes_healthy gauge\n"; uint32_t healthy_count = 0; for (const auto& [node_id, is_healthy] : health) { if (is_healthy) healthy_count++; } oss << "themisdb_cluster_nodes_healthy " << healthy_count << "\n"; oss << "themisdb_cluster_nodes_total " << health.size() << "\n"; // Add replication lag metrics per replica oss << "\n# HELP themisdb_replication_lag_per_replica Replication lag per replica\n" << "# TYPE themisdb_replication_lag_per_replica gauge\n"; std::shared_lock lock(replicas_mutex_); for (const auto& replica : replicas_) { oss << "themisdb_replication_lag_per_replica{node_id=\"" << replica.node_id << "\",datacenter=\"" << replica.datacenter << "\"} " << replica.replicationLagMs() << "\n"; } return oss.str(); } void ReplicationManager::heartbeatLoop() { while (running_.load()) { if (election_ && election_->isLeader()) { // Notify local election module so it doesn't start a spurious // election against itself; in a real multi-node deployment this // loop would serialize and send AppendEntries RPCs over the wire. uint64_t current_term = election_->getCurrentTerm(); { std::shared_lock lock(replicas_mutex_); for ([[maybe_unused]] const auto& replica : replicas_) { // Record outbound heartbeat so the election module can // reset its own liveness timer if it happens to be watching. // endpoint used by real network layer } } // Reset the leader's own heartbeat timer to avoid self-election election_->receiveHeartbeat(current_term, node_id_, wal_ ? wal_->getCurrentSequence() : 0); // Renew the leader lease so that lease-based reads remain valid. // The lease duration is bounded by election_timeout_min_ms, which // guarantees no follower can become a new leader while the lease holds. if (config_.enable_leader_lease) { election_->renewLease(config_.leader_lease_duration_ms); } } std::this_thread::sleep_for( std::chrono::milliseconds(config_.heartbeat_interval_ms) ); } } void ReplicationManager::compactionLoop() { while (running_.load()) { // Find minimum acked sequence across all replicas uint64_t min_seq = wal_->getCurrentSequence(); { std::shared_lock lock(replicas_mutex_); for (const auto& stream : streams_) { min_seq = std::min(min_seq, stream->getLastAckedSequence()); } } // Truncate WAL up to min_seq (keep some buffer) if (min_seq > 10000) { wal_->truncateBefore(min_seq - 10000); } // Run every 5 minutes for (int i = 0; i < 300 && running_.load(); ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); } } } void ReplicationManager::notifyListeners( std::function callback) { for (auto& listener : listeners_) { if (listener) { callback(*listener); } } } std::vector> ReplicationManager::getReplicaHealthStatus() const { std::shared_lock lock(replicas_mutex_); std::vector> result; for (const auto& replica : replicas_) { result.emplace_back(replica.node_id, replica.health_status); } return result; } bool ReplicationManager::hasQuorum() const { std::shared_lock lock(replicas_mutex_); size_t healthy_voting_members = 0; size_t total_voting_members = 0; for (const auto& replica : replicas_) { if (replica.is_voting_member) { total_voting_members++; bool counts_as_healthy; if (replica.role == ReplicationRole::WITNESS) { // Witnesses receive no WAL data, so their health_status may remain // UNKNOWN until the first health-check cycle. Use the raw heartbeat // timestamp to determine liveness instead. counts_as_healthy = replica.isHealthyWithTimeout( config_.failure_detection_timeout_ms); } else { counts_as_healthy = (replica.health_status == HealthStatus::HEALTHY || replica.health_status == HealthStatus::DEGRADED); } if (counts_as_healthy) { healthy_voting_members++; } } } // Add self if leader if (election_ && election_->isLeader()) { total_voting_members++; healthy_voting_members++; } // Quorum is majority of voting members return healthy_voting_members > (total_voting_members / 2); } void ReplicationManager::performHealthCheck() { // Collect all health status changes under the write lock, then // notify listeners outside the lock to avoid holding it during callbacks. struct HealthChange { std::string node_id; HealthStatus old_status; HealthStatus new_status; }; std::vector changes; { std::unique_lock lock(replicas_mutex_); // BATCH B OPTIMIZATION: Reserve space for all possible changes changes.reserve(replicas_.size()); for (auto& replica : replicas_) { HealthStatus old_status = replica.health_status; updateReplicaHealth(replica); if (old_status != replica.health_status) { changes.push_back({replica.node_id, old_status, replica.health_status}); } } } for (const auto& change : changes) { notifyListeners([&change](IReplicationListener& l) { l.onReplicaHealthChanged(change.node_id, change.old_status, change.new_status); }); } } bool ReplicationManager::detectNetworkPartition() const { std::shared_lock lock(replicas_mutex_); // Count failed replicas size_t failed_count = 0; for (const auto& replica : replicas_) { if (replica.health_status == HealthStatus::FAILED) { failed_count++; } } // Network partition detected if more than half of replicas are unreachable return failed_count > (replicas_.size() / 2); } void ReplicationManager::setReadPreference(ReadPreference preference) { config_.default_read_preference = preference; } // ============================================================================ // Raft leader lease reads (linearizable read-scale-out) // ============================================================================ bool ReplicationManager::hasLeaderLease() const { if (!initialized_.load() || !election_) { return false; } return election_->hasValidLease(); } ReplicationManager::LeaseReadResult ReplicationManager::leaseRead( const std::string& collection, const std::string& document_id) const { LeaseReadResult result; result.node_id = node_id_; if (!initialized_.load() || !election_) { THEMIS_WARN("leaseRead called on uninitialised ReplicationManager (node={})", node_id_); return result; } // Only the Raft leader may serve lease reads. if (!election_->isLeader()) { THEMIS_DEBUG("leaseRead rejected: node {} is not the leader", node_id_); stats_.lease_reads_rejected.fetch_add(1, std::memory_order_relaxed); return result; } // Check that the leader lease is still valid. if (!config_.enable_leader_lease || !election_->hasValidLease()) { THEMIS_DEBUG("leaseRead rejected: leader lease expired or disabled (node={})", node_id_); stats_.lease_reads_rejected.fetch_add(1, std::memory_order_relaxed); return result; } // Lease is valid – the leader is guaranteed to be the unique leader for the // remaining lease window, so this read is linearizable without a quorum // round-trip. result.success = true; result.served_under_lease = true; result.commit_index = wal_ ? wal_->getCurrentSequence() : 0; stats_.lease_reads_served.fetch_add(1, std::memory_order_relaxed); THEMIS_DEBUG("leaseRead served: collection={} doc={} commit_index={} node={}", collection, document_id, result.commit_index, node_id_); return result; } void ReplicationManager::healthMonitorLoop() { while (running_.load()) { performHealthCheck(); // Check for leader failure and trigger automatic failover if enabled if (config_.enable_auto_failover && election_ && !election_->isLeader()) { std::string current_leader_id = election_->getLeaderId(); std::string failed_leader_id; { std::shared_lock lock(replicas_mutex_); for (const auto& replica : replicas_) { if (replica.node_id == current_leader_id && replica.health_status == HealthStatus::FAILED) { failed_leader_id = replica.node_id; break; } } } if (!failed_leader_id.empty()) { stats_.replica_failures_detected.fetch_add(1, std::memory_order_relaxed); attemptAutomaticFailover(failed_leader_id); } } // Check for network partition if (detectNetworkPartition()) { stats_.network_partitions_detected++; std::vector unreachable_nodes; { std::shared_lock lock(replicas_mutex_); for (const auto& replica : replicas_) { if (replica.health_status == HealthStatus::FAILED) { unreachable_nodes.push_back(replica.node_id); } } } notifyListeners([&unreachable_nodes](IReplicationListener& l) { l.onNetworkPartitionDetected(unreachable_nodes); }); } // Update replication lag metrics { std::shared_lock lock(replicas_mutex_); int64_t max_lag = 0; int64_t total_lag = 0; size_t replica_count = 0; for (const auto& replica : replicas_) { int64_t lag = replica.replicationLagMs(); max_lag = std::max(max_lag, lag); total_lag += lag; replica_count++; // Notify listeners of excessive lag if (lag > static_cast(config_.max_replication_lag_ms)) { notifyListeners([lag](IReplicationListener& l) { l.onReplicationLagWarning(lag); }); } } stats_.max_replication_lag_ms.store(max_lag); if (replica_count > 0) { stats_.avg_replication_lag_ms.store(total_lag / replica_count); } } std::this_thread::sleep_for( std::chrono::milliseconds(config_.heartbeat_interval_ms) ); } } void ReplicationManager::attemptAutomaticFailover(const std::string& failed_node_id) { // Check if we have quorum to proceed with failover if (!hasQuorum()) { return; } // Elect new leader bool success = electNewLeader(); if (success) { stats_.automatic_failovers++; std::string new_leader_id = election_ ? election_->getLeaderId() : ""; notifyListeners([&failed_node_id, &new_leader_id](IReplicationListener& l) { l.onFailoverStarted(failed_node_id, new_leader_id); }); notifyListeners([&new_leader_id](IReplicationListener& l) { l.onFailoverCompleted(new_leader_id, true); }); } else { notifyListeners([](IReplicationListener& l) { l.onFailoverCompleted("", false); }); } } bool ReplicationManager::electNewLeader() { if (!election_) { return false; } // Find the replica with highest priority and most up-to-date log std::shared_lock lock(replicas_mutex_); // BATCH B OPTIMIZATION & SAFETY FIX: Use index instead of pointer to avoid invalidation size_t best_candidate_idx = static_cast(-1); for (size_t i = 0; i < replicas_.size(); ++i) { const auto& replica = replicas_[i]; if (!replica.is_voting_member || replica.role == ReplicationRole::WITNESS || // Witnesses never become leaders replica.health_status == HealthStatus::FAILED) { continue; } if (best_candidate_idx == static_cast(-1)) { best_candidate_idx = i; } else { const auto& best = replicas_[best_candidate_idx]; if (replica.priority > best.priority || (replica.priority == best.priority && replica.last_applied_sequence > best.last_applied_sequence)) { best_candidate_idx = i; } } } // If this node is the best candidate, start election if (best_candidate_idx != static_cast(-1) && replicas_[best_candidate_idx].node_id == node_id_) { election_->startElection(); return election_->isLeader(); } return false; } void ReplicationManager::updateReplicaHealth(ReplicaInfo& replica) { replica.updateHealthStatus( config_.failure_detection_timeout_ms, config_.degraded_lag_threshold_ms ); } bool ReplicationManager::validateConfig() { if (config_.batch_size == 0 || config_.batch_size > 1000000) { THEMIS_ERROR("batch_size must be 1-1000000, got {}", config_.batch_size); return false; } if (config_.heartbeat_interval_ms == 0 || config_.heartbeat_interval_ms > 60000) { THEMIS_ERROR("heartbeat_interval_ms must be 1-60000, got {}", config_.heartbeat_interval_ms); return false; } if (config_.election_timeout_min_ms >= config_.election_timeout_max_ms) { THEMIS_ERROR("election_timeout_min_ms ({}) must be less than election_timeout_max_ms ({})", config_.election_timeout_min_ms, config_.election_timeout_max_ms); return false; } if (config_.failure_detection_timeout_ms == 0) { THEMIS_ERROR("failure_detection_timeout_ms must be > 0"); return false; } if (config_.wal_directory.empty()) { THEMIS_ERROR("wal_directory must not be empty"); return false; } if (config_.seed_nodes.empty()) { THEMIS_WARN("No seed nodes configured – clustering disabled (single-node mode)"); } if (config_.mode != ReplicationMode::ASYNC && config_.min_sync_replicas == 0) { THEMIS_ERROR("min_sync_replicas must be > 0 for SYNC/SEMI_SYNC modes"); return false; } if (config_.enable_leader_lease && config_.leader_lease_duration_ms >= config_.election_timeout_min_ms) { THEMIS_ERROR( "leader_lease_duration_ms ({}) must be strictly less than " "election_timeout_min_ms ({}) to guarantee linearizability", config_.leader_lease_duration_ms, config_.election_timeout_min_ms); return false; } if (config_.enable_wal_compression && (config_.wal_compression_level < 1 || config_.wal_compression_level > 9)) { THEMIS_ERROR("wal_compression_level must be 1-9 when WAL compression is enabled, got {}", config_.wal_compression_level); return false; } return true; } // ============================================================================ // LWWConflictResolver Implementation // ============================================================================ // Extract the "updated_at" field from a minimal JSON payload. // We intentionally avoid a full JSON parser dependency; we just scan for the // first occurrence of "updated_at": pattern. // // BATCH B ANNOTATION: // Version Tracking: This method extracts a monotonic timestamp that serves // as a version vector component for causality tracking. The extracted timestamp // represents the write's logical time in the system and should be propagated // to all conflict resolution decision points. // Consensus Expectation: All replicas must produce deterministic timestamp // extraction from the same JSON payload to ensure convergence. int64_t LWWConflictResolver::extractTimestamp(const std::string& json_doc) { const std::string key = "\"updated_at\""; auto pos = json_doc.find(key); if (pos == std::string::npos) { return -1; } // Skip past key, colon, and optional whitespace pos += key.size(); while (pos < json_doc.size() && (json_doc[pos] == ' ' || json_doc[pos] == ':')) { ++pos; } if (pos >= json_doc.size()) { return -1; } // Parse the integer value try { size_t consumed = 0; int64_t ts = std::stoll(json_doc.substr(pos), &consumed); return (consumed > 0) ? ts : -1; } catch (...) { return -1; } } std::string LWWConflictResolver::resolve( const std::string& local, const std::string& remote, const std::string& /*collection*/, const std::string& /*document_id*/) { // BATCH B ANNOTATION: // Version Tracking: This resolution function uses wall-clock timestamps as the // primary causality mechanism. The selected document (local/remote) represents // the "latest write" in a causality-agnostic manner. // Consensus Expectation: All replicas must apply this deterministic timestamp // comparison to ensure they converge on the same document version. Ties must // consistently favor remote to maintain quorum semantics. int64_t local_ts = extractTimestamp(local); int64_t remote_ts = extractTimestamp(remote); if (local_ts < 0 && remote_ts < 0) { // Neither has a timestamp – keep the remote version (conservative) return remote; } if (local_ts < 0) { return remote; } if (remote_ts < 0) { return local; } // Select the document with the strictly higher timestamp; ties go to remote return (local_ts > remote_ts) ? local : remote; } // ============================================================================ // CRDTConflictResolver Implementation // ============================================================================ std::string CRDTConflictResolver::resolve( const std::string& local, const std::string& remote, const std::string& collection, const std::string& document_id) { // For simple numeric fields that follow the pattern "\"\":" // we take the maximum value (grow-only counter / LWW-Max register). // For all other content we delegate to LWWConflictResolver. // // BATCH B ANNOTATION: // Version Tracking: This CRDT resolver maintains two causality mechanisms: // 1. Grow-only counters (max-register semantics) for numeric fields - these // monotonically increase and enable causal advancement. // 2. Last-write-wins for unstructured fields - delegates timestamp-based // version tracking to LWWConflictResolver. // The merged state combines both semantics: max values from counters + // timestamp-based resolution for other fields. // Consensus Expectation: All replicas must apply max() with identical precedence // on all numeric fields to ensure eventual consistency. Field discovery must be // deterministic (sorted) to prevent order-dependent conflicts. // If either document is empty, return the other if (local.empty()) { return remote; } if (remote.empty()) { return local; } // Build merged document: walk the remote document and for numeric fields // keep max(local, remote); everything else comes from LWW winner. LWWConflictResolver lwr; std::string base = lwr.resolve(local, remote, collection, document_id); // Scan both documents for numeric fields and merge with max semantics. // We iterate over keys that appear in remote and check if they are numeric. std::string merged = base; // Simple heuristic: find all "key": number patterns in both documents and // replace with max value. auto extractNumericFields = [](const std::string& doc) -> std::map { std::map fields; size_t p = 0; while (p < doc.size()) { // Find next key (starts with '"') auto kstart = doc.find('"', p); if (kstart == std::string::npos) break; auto kend = doc.find('"', kstart + 1); if (kend == std::string::npos) break; std::string key = doc.substr(kstart + 1, kend - kstart - 1); // Skip past colon and whitespace size_t vp = kend + 1; while (vp < doc.size() && (doc[vp] == ' ' || doc[vp] == ':')) ++vp; // Check if value is numeric (starts with digit, or '-' followed by a digit) if (vp < doc.size() && (std::isdigit(static_cast(doc[vp])) || (doc[vp] == '-' && vp + 1 < doc.size() && std::isdigit(static_cast(doc[vp + 1]))))) { try { size_t consumed = 0; int64_t val = std::stoll(doc.substr(vp), &consumed); if (consumed > 0) { fields[key] = val; } } catch (...) {} } p = kend + 1; } return fields; }; auto local_fields = extractNumericFields(local); auto remote_fields = extractNumericFields(remote); // For each field present in both documents, patch the merged document with max value // For each field present in both documents, patch the merged document with max value // BATCH C OPTIMIZATION: Batch string operations to avoid O(nΒ²) behavior // Instead of repeated find() + substr() in loop, build result in single pass for (const auto& [key, remote_val] : remote_fields) { auto it = local_fields.find(key); if (it == local_fields.end()) continue; int64_t max_val = std::max(it->second, remote_val); int64_t cur_val = (base == local) ? it->second : remote_val; if (max_val != cur_val) { // Replace "key": cur_val with "key": max_val in merged // BATCH C FIX: Use efficient string replacement instead of substr std::string search = "\"" + key + "\""; size_t pos = 0; // Find all occurrences of key and replace the first numeric value while ((pos = merged.find(search, pos)) != std::string::npos) { // Skip to value size_t vp = pos + search.size(); while (vp < merged.size() && (merged[vp] == ' ' || merged[vp] == ':')) ++vp; size_t vend = vp; // Accept an optional leading '-', then only digits if (vend < merged.size() && merged[vend] == '-') ++vend; while (vend < merged.size() && std::isdigit(static_cast(merged[vend]))) { ++vend; } // Extract current value to verify it matches std::string old_val_str = merged.substr(vp, vend - vp); try { if (std::stoll(old_val_str) == cur_val) { // Replace with new value std::string new_val_str = std::to_string(max_val); merged.replace(vp, vend - vp, new_val_str); break; // Only replace first occurrence } } catch (...) {} pos = vend; } } } return merged; } // ============================================================================ // HybridLogicalClock Implementation // ============================================================================ HybridLogicalClock::HybridLogicalClock(const std::string& node_id) : node_id_(node_id) , last_physical_(0) , logical_counter_(0) { } HybridLogicalClock::Timestamp HybridLogicalClock::now() { std::lock_guard lock(mutex_); uint64_t wall = static_cast( std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count() ); uint64_t last = last_physical_.load(); if (wall > last) { last_physical_.store(wall); logical_counter_.store(0); } else { // Wall clock has not advanced – increment logical counter logical_counter_++; } return Timestamp{last_physical_.load(), logical_counter_.load(), node_id_}; } HybridLogicalClock::Timestamp HybridLogicalClock::receive(const Timestamp& received) { std::lock_guard lock(mutex_); uint64_t wall = static_cast( std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count() ); uint64_t last = last_physical_.load(); uint64_t max_p = std::max({wall, last, received.physical}); if (max_p == last && max_p == received.physical) { // Both this node and the sender are at the same physical time logical_counter_.store(std::max(logical_counter_.load(), received.logical) + 1); } else if (max_p == received.physical) { // Sender is ahead logical_counter_.store(received.logical + 1); } else if (max_p == last) { // This node is ahead (or wall advanced) logical_counter_++; } else { // Wall clock is strictly ahead of both logical_counter_.store(0); } last_physical_.store(max_p); return Timestamp{last_physical_.load(), logical_counter_.load(), node_id_}; } HybridLogicalClock::Timestamp HybridLogicalClock::current() const { return Timestamp{last_physical_.load(), logical_counter_.load(), node_id_}; } // ============================================================================ // VectorClock Implementation // ============================================================================ VectorClock::VectorClock(const std::string& node_id) { clocks_[node_id] = 0; } VectorClock::VectorClock(const VectorClock& other) { // mutex_ is mutable – no const_cast needed std::shared_lock rlock(other.mutex_); clocks_ = other.clocks_; } VectorClock::VectorClock(VectorClock&& other) noexcept { std::unique_lock wlock(other.mutex_); clocks_ = std::move(other.clocks_); } VectorClock& VectorClock::operator=(const VectorClock& other) { if (this != &other) { std::unique_lock wlock(mutex_); std::shared_lock rlock(other.mutex_); clocks_ = other.clocks_; } return *this; } VectorClock& VectorClock::operator=(VectorClock&& other) noexcept { if (this != &other) { std::unique_lock wlock(mutex_); std::unique_lock rlock(other.mutex_); clocks_ = std::move(other.clocks_); } return *this; } void VectorClock::increment(const std::string& node_id) { std::unique_lock lock(mutex_); clocks_[node_id]++; } void VectorClock::merge(const VectorClock& other) { if (this == &other) return; // Acquire locks in consistent address order to prevent ABBA deadlock when // two threads concurrently call A.merge(B) and B.merge(A). VectorClock* lo = (this < &other) ? this : const_cast(&other); VectorClock* hi = (this >= &other) ? this : const_cast(&other); std::unique_lock lk_lo(lo->mutex_); std::unique_lock lk_hi(hi->mutex_); for (const auto& [node, ts] : other.clocks_) { auto& mine = clocks_[node]; if (ts > mine) mine = ts; } } uint64_t VectorClock::get(const std::string& node_id) const { std::shared_lock lock(mutex_); auto it = clocks_.find(node_id); return it != clocks_.end() ? it->second : 0; } int VectorClock::compare(const VectorClock& other) const { if (this == &other) return 1; // comparing with self β†’ equal (treat as this >= other) // Acquire both shared locks; multiple shared_locks don't deadlock each other. // Use ordered acquisition to keep consistent with exclusive-lock callers. const VectorClock* lo = (this <= &other) ? this : &other; const VectorClock* hi = (this > &other) ? this : &other; std::shared_lock lk_lo(lo->mutex_); std::shared_lock lk_hi(hi->mutex_); bool this_greater = false; bool other_greater = false; // Check all entries present in this clock for (const auto& [node, ts] : clocks_) { auto it = other.clocks_.find(node); uint64_t other_ts = (it != other.clocks_.end()) ? it->second : 0; if (ts > other_ts) this_greater = true; if (ts < other_ts) other_greater = true; } // Check entries present only in the other clock for (const auto& [node, ts] : other.clocks_) { if (clocks_.find(node) == clocks_.end() && ts > 0) { other_greater = true; } } if ( this_greater && !other_greater) return 1; // this is strictly newer if (!this_greater && other_greater) return -1; // other is strictly newer if ( this_greater && other_greater) return 0; // concurrent – neither dominates return 0; // equal – treat as concurrent } bool VectorClock::happensBefore(const VectorClock& other) const { return compare(other) == -1; } bool VectorClock::isConcurrent(const VectorClock& other) const { return compare(other) == 0; } std::string VectorClock::toJson() const { std::shared_lock lock(mutex_); std::ostringstream oss; oss << "{"; bool first = true; for (const auto& [node, ts] : clocks_) { if (!first) oss << ","; oss << "\"" << node << "\":" << ts; first = false; } oss << "}"; return oss.str(); } VectorClock VectorClock::fromJson(const std::string& json) { // BATCH A ANNOTATION: Version Clock Deserialization and Consensus // This method reconstructs vector clock state from JSON, which is critical for // deserializing replicated MMWriteEntry metadata during conflict resolution. // // Version Tracking Semantics: // The deserialized vector clock represents the causal history of a write across // all replicas. Each (replica_id -> logical_timestamp) pair encodes: // - When the write was last seen/processed on that replica (happens-before) // - Whether the write causally depends on earlier writes from that replica // // Consensus Expectation: // All replicas must parse the same JSON representation and extract identical // clock values. Floating-point parsing errors are forbidden; only integers. // Deterministic key ordering (implicitly sorted by map) ensures reproducible // comparison results for causal ordering decisions. // // Metadata Enrichment: // The reconstructed vector clock is merged with conflicting writes' clocks in // conflict resolution to establish a complete causality lattice. Missing replicas // in the JSON are assumed to have clock value 0, indicating no prior interaction. VectorClock vc; size_t p = 0; while (p < json.size()) { auto kstart = json.find('"', p); if (kstart == std::string::npos) break; auto kend = json.find('"', kstart + 1); if (kend == std::string::npos) break; std::string key = json.substr(kstart + 1, kend - kstart - 1); // Skip ':' and whitespace size_t vp = kend + 1; while (vp < json.size() && (json[vp] == ' ' || json[vp] == ':')) ++vp; if (vp < json.size() && std::isdigit(static_cast(json[vp]))) { try { size_t consumed = 0; uint64_t val = std::stoull(json.substr(vp), &consumed); if (consumed > 0) { vc.clocks_[key] = val; p = vp + consumed; continue; } } catch (...) {} } p = kend + 1; } return vc; } // ============================================================================ // Multi-master ConflictResolver implementations (MMWriteEntry variants) // ============================================================================ MMWriteEntry LastWriteWinsResolver::resolve( const std::string& /*document_id*/, const std::vector& conflicting_writes) { // BATCH C ANNOTATION: Metadata-Enriched Winner Selection and Causality Preservation // This resolver implements Last-Write-Wins conflict resolution for multi-master scenarios. // The "latest" write is selected using HLC (Hybrid Logical Clock) timestamps. // // Winner Selection and Causality: // The HLC timestamp provides monotonic, clock-skew-safe ordering. Unlike wall-clock // timestamps, HLC is resistant to clock resets/jumps on individual nodes. Selection // criteria: max(HLC) across conflicting writes. Ties can occur if replicas have // synchronized clocks; ties are resolved arbitrarily (first encountered). // // Metadata Enrichment (Batch C Pattern): // After winner selection, the winning entry is enriched with: // 1. Merged Vector Clock: union of all conflicting writes' vector clocks. // This captures the causality frontierβ€”every replica involved in the conflict // is represented in the merged clock. // 2. Merged Dependencies: set union of all write_ids from conflicting entries. // This forms a causality lattice where the winner depends on all losers. // 3. Updated HLC: max(HLC) across all conflicting writes. This ensures that // the resolved entry's timestamp reflects the true end of the conflict window. // 4. Recomputed Checksum: SHA256(operation || collection || document_id || data) // must be recomputed because the merged metadata is included for verification. // // Happens-Before Guarantee: // - All conflicting writes' dependencies are transitively included in the winner. // - The merged vector clock ensures that any future write can detect causality // relative to this resolution decision (consistency property). // - Replicas applying this winner will recognize all conflicting writes as causally prior. if (conflicting_writes.empty()) return MMWriteEntry{}; auto compute_mm_checksum = [](const MMWriteEntry& entry) { std::string content = entry.operation + entry.collection + entry.document_id + entry.data; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast(content.c_str()), content.size(), hash); std::ostringstream oss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); } return oss.str(); }; auto enrich_winner_with_causality = [&](const MMWriteEntry& winner) { // BATCH C ANNOTATION: Causality Lattice Construction // This lambda enriches the winner with merged causality metadata: // - merged_clock: Lattice join of all vector clocks. Represents the frontier // of knowledge across all replicas that participated in this conflict. // - merged_dependencies: Causal dag where winner depends on all conflicting writes. // Enables transitive dependency tracking for eventual consistency validation. // - latest_hlc: Ensures the winner's timestamp is >= all conflicting timestamps. // Monotonicity is critical for preventing time-travel anomalies. // - recomputed checksum: Ties metadata to the resolved data for integrity. // If metadata is corrupted/lost, the checksum will fail on replication. MMWriteEntry enriched = winner; VectorClock merged_clock = winner.vector_clock; std::set merged_dependencies( enriched.dependencies.begin(), enriched.dependencies.end()); HybridLogicalClock::Timestamp latest_hlc = winner.hlc; for (const auto& write : conflicting_writes) { merged_clock.merge(write.vector_clock); merged_dependencies.insert(write.dependencies.begin(), write.dependencies.end()); if (!write.write_id.empty() && write.write_id != enriched.write_id) { merged_dependencies.insert(write.write_id); } if (latest_hlc < write.hlc) { latest_hlc = write.hlc; } } enriched.vector_clock = std::move(merged_clock); enriched.dependencies.assign(merged_dependencies.begin(), merged_dependencies.end()); enriched.hlc = latest_hlc; enriched.checksum = compute_mm_checksum(enriched); return enriched; }; // Select the entry with the latest HLC timestamp; ties resolved by node_id const MMWriteEntry* winner = &conflicting_writes[0]; for (const auto& entry : conflicting_writes) { if (winner->hlc < entry.hlc) { winner = &entry; } } return enrich_winner_with_causality(*winner); } CRDTMergeResolver::CRDTMergeResolver(CRDTType type) : crdt_type_(type) { } MMWriteEntry CRDTMergeResolver::resolve( const std::string& document_id, const std::vector& conflicting_writes) { if (conflicting_writes.empty()) return MMWriteEntry{}; std::string merged_data; switch (crdt_type_) { case CRDTType::LWW_REGISTER: merged_data = mergeLWWRegister(conflicting_writes); break; case CRDTType::MV_REGISTER: merged_data = mergeMVRegister(conflicting_writes); break; case CRDTType::G_COUNTER: merged_data = mergeGCounter(conflicting_writes); break; case CRDTType::PN_COUNTER: merged_data = mergePNCounter(conflicting_writes); break; case CRDTType::G_SET: merged_data = mergeGSet(conflicting_writes); break; case CRDTType::OR_SET: merged_data = mergeORSet(conflicting_writes); break; case CRDTType::LWW_MAP: merged_data = mergeLWWMap(conflicting_writes); break; case CRDTType::TWO_P_SET: merged_data = mergeTwoPSet(conflicting_writes); break; case CRDTType::RGA: merged_data = mergeRGA(conflicting_writes); break; case CRDTType::FLAG_EW: merged_data = mergeFlagEW(conflicting_writes); break; case CRDTType::FLAG_DW: merged_data = mergeFlagDW(conflicting_writes); break; } // Base entry is the LWW winner; replace its data with the merged payload LastWriteWinsResolver lwr; MMWriteEntry result = lwr.resolve(document_id, conflicting_writes); result.data = merged_data; // Keep checksum aligned with merged payload and metadata-carrying fields. std::string content = result.operation + result.collection + result.document_id + result.data; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast(content.c_str()), content.size(), hash); std::ostringstream oss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); } result.checksum = oss.str(); return result; } std::string CRDTMergeResolver::strategyName() const { switch (crdt_type_) { case CRDTType::LWW_REGISTER: return "LWW_REGISTER"; case CRDTType::MV_REGISTER: return "MV_REGISTER"; case CRDTType::G_COUNTER: return "G_COUNTER"; case CRDTType::PN_COUNTER: return "PN_COUNTER"; case CRDTType::G_SET: return "G_SET"; case CRDTType::OR_SET: return "OR_SET"; case CRDTType::LWW_MAP: return "LWW_MAP"; case CRDTType::TWO_P_SET: return "TWO_P_SET"; case CRDTType::RGA: return "RGA"; case CRDTType::FLAG_EW: return "FLAG_EW"; case CRDTType::FLAG_DW: return "FLAG_DW"; } return "UNKNOWN"; } std::string CRDTMergeResolver::mergeLWWRegister(const std::vector& writes) { // Return the data of the entry with the latest HLC const MMWriteEntry* latest = &writes[0]; for (const auto& w : writes) { if (latest->hlc < w.hlc) latest = &w; } return latest->data; } std::string CRDTMergeResolver::mergeMVRegister(const std::vector& writes) { // Multi-value register: return all concurrent values as a JSON array std::ostringstream oss; oss << "["; bool first = true; for (const auto& w : writes) { if (!first) oss << ","; oss << w.data; first = false; } oss << "]"; return oss.str(); } // Helper: scan a JSON doc for "key": integer pairs // BATCH A ANNOTATION: Numeric Field Extraction with Version Tracking Semantics // This helper is used in conflict resolution to identify grow-only counters // (G-Counter CRDT semantics). Each extracted integer field represents a monotonically // increasing value that should be merged using max() semantics across conflicting writes. // // Version Tracking Guarantee: // Numeric fields form causal dependencies: if field F's value increases from V1 to V2, // then all writes with F=V2 causally depend on (or are concurrent with) writes where F=V1. // This property must be preserved during merge: merged_value = max(local_val, remote_val). // // Consensus Expectation: // All replicas must parse the JSON deterministically (same key order, same parsing rules). // Integer overflow is prevented by using int64_t; values outside [-2^63, 2^63-1] will // throw std::out_of_range and be silently skipped (treated as unparseable). // Deterministic field discovery (sorted by map insertion order) ensures reproducible // merge outcomes across all replicas. // // Metadata Enrichment: // Extracted fields become part of the conflict resolution decision. The resolved document // must include the merged (max) value for each field, along with vector clock metadata // indicating which replica contributed the max value (for debugging/audit trails). static std::map extractJsonInts(const std::string& doc) { std::map fields; size_t p = 0; while (p < doc.size()) { auto ks = doc.find('"', p); if (ks == std::string::npos) break; auto ke = doc.find('"', ks + 1); if (ke == std::string::npos) break; std::string key = doc.substr(ks + 1, ke - ks - 1); size_t vp = ke + 1; while (vp < doc.size() && (doc[vp] == ' ' || doc[vp] == ':')) ++vp; if (vp < doc.size() && (std::isdigit(static_cast(doc[vp])) || (doc[vp] == '-' && vp + 1 < doc.size() && std::isdigit(static_cast(doc[vp + 1]))))) { try { size_t consumed = 0; int64_t val = std::stoll(doc.substr(vp), &consumed); if (consumed > 0) { fields[key] = val; p = vp + consumed; continue; } } catch (...) {} } p = ke + 1; } return fields; } // Helper: extract the sub-object string for a named key, e.g. extractSubObject(doc,"P") // returns the raw content between the outermost braces of "P": { ... } static std::string extractSubObject(const std::string& doc, const std::string& key) { std::string search = "\"" + key + "\""; auto pos = doc.find(search); if (pos == std::string::npos) return ""; pos += search.size(); while (pos < doc.size() && (doc[pos] == ' ' || doc[pos] == ':')) ++pos; if (pos >= doc.size() || doc[pos] != '{') return ""; size_t depth = 0, start = pos; while (pos < doc.size()) { if (doc[pos] == '{') ++depth; else if (doc[pos] == '}') { if (--depth == 0) return doc.substr(start, pos - start + 1); } ++pos; } return ""; } // Helper: extract quoted string tokens from a JSON array e.g. ["a","b"] β†’ {"a","b"} // BATCH C FIX: Add bounds checking and container stability guards to prevent // iterator invalidation and out-of-bounds access during iteration. static std::set extractJsonArrayStrings(const std::string& arr) { std::set result; if (arr.empty()) { return result; // Guard: Handle empty array early } size_t p = 0; size_t arr_size = arr.size(); // Cache size to avoid repeated calls while (p < arr_size) { // Find opening quote with bounds check auto qs = arr.find('"', p); if (qs == std::string::npos || qs >= arr_size) { break; // No more quotes or out of bounds } // Find closing quote with bounds check auto qe = arr.find('"', qs + 1); if (qe == std::string::npos || qe >= arr_size) { break; // No closing quote or out of bounds } // Additional safety: validate extraction indices if (qs + 1 <= qe && qe - qs - 1 <= arr_size) { result.insert(arr.substr(qs + 1, qe - qs - 1)); } p = qe + 1; } return result; } // Helper: find the raw JSON array string for a named key, e.g. extractSubArray(doc,"add") static std::string extractSubArray(const std::string& doc, const std::string& key) { std::string search = "\"" + key + "\""; auto pos = doc.find(search); if (pos == std::string::npos) return ""; pos += search.size(); while (pos < doc.size() && (doc[pos] == ' ' || doc[pos] == ':')) ++pos; if (pos >= doc.size() || doc[pos] != '[') return ""; size_t depth = 0, start = pos; while (pos < doc.size()) { if (doc[pos] == '[') ++depth; else if (doc[pos] == ']') { if (--depth == 0) return doc.substr(start, pos - start + 1); } ++pos; } return ""; } std::string CRDTMergeResolver::mergeGCounter(const std::vector& writes) { // Grow-only counter: for each node-keyed counter take the maximum value std::map merged; for (const auto& w : writes) { auto fields = extractJsonInts(w.data); for (const auto& [k, v] : fields) { merged[k] = std::max(merged[k], v); } } std::ostringstream oss; oss << "{"; bool first = true; for (const auto& [k, v] : merged) { if (!first) oss << ","; oss << "\"" << k << "\":" << v; first = false; } oss << "}"; return oss.str(); } std::string CRDTMergeResolver::mergePNCounter(const std::vector& writes) { // PN-Counter: two separate G-Counters (P = increments, N = decrements) per node. // Expected data format: {"P":{"nodeA":5,"nodeB":3},"N":{"nodeA":2,"nodeB":1}} // Merge: take max per key for both P and N sub-counters. std::map mergedP, mergedN; for (const auto& w : writes) { auto pSub = extractSubObject(w.data, "P"); if (!pSub.empty()) { for (const auto& [k, v] : extractJsonInts(pSub)) mergedP[k] = std::max(mergedP[k], v); } auto nSub = extractSubObject(w.data, "N"); if (!nSub.empty()) { for (const auto& [k, v] : extractJsonInts(nSub)) mergedN[k] = std::max(mergedN[k], v); } } // Serialise as {"P":{...},"N":{...}} auto serializeMap = [](const std::map& m) { std::ostringstream o; o << "{"; bool first = true; for (const auto& [k, v] : m) { if (!first) o << ","; o << "\"" << k << "\":" << v; first = false; } o << "}"; return o.str(); }; std::ostringstream oss; oss << "{\"P\":" << serializeMap(mergedP) << ",\"N\":" << serializeMap(mergedN) << "}"; return oss.str(); } std::string CRDTMergeResolver::mergeGSet(const std::vector& writes) { // Grow-only set: union of all quoted string tokens across all payloads std::set seen; for (const auto& w : writes) { size_t p = 0; while (p < w.data.size()) { auto qs = w.data.find('"', p); if (qs == std::string::npos) break; auto qe = w.data.find('"', qs + 1); if (qe == std::string::npos) break; seen.insert(w.data.substr(qs + 1, qe - qs - 1)); p = qe + 1; } } std::ostringstream oss; oss << "["; bool first = true; for (const auto& s : seen) { if (!first) oss << ","; oss << "\"" << s << "\""; first = false; } oss << "]"; return oss.str(); } std::string CRDTMergeResolver::mergeORSet(const std::vector& writes) { // OR-Set (Observed-Remove Set): each add operation tags an element with a unique id; // a remove operation records the tag in the tombstone set. An element is present iff // it has at least one tag that is not tombstoned. // // Expected data format: // {"add":[["apple","tag-1"],["banana","tag-2"]],"tombstones":["tag-1"]} // Each add entry is a two-element array [element, unique-tag]. // Merge: union of all add pairs, union of all tombstones. // Collect all (element, tag) pairs and all tombstones across writes. std::vector> allAdds; std::set tombstones; for (const auto& w : writes) { // Parse tombstones array auto tsArr = extractSubArray(w.data, "tombstones"); for (const auto& t : extractJsonArrayStrings(tsArr)) tombstones.insert(t); // Parse add array – find "add": [ ... ] then iterate inner arrays auto addArr = extractSubArray(w.data, "add"); // Each inner element looks like ["element","tag"] size_t p = 0; while (p < addArr.size()) { auto lb = addArr.find('[', p); if (lb == std::string::npos) break; auto rb = addArr.find(']', lb + 1); if (rb == std::string::npos) break; std::string pair = addArr.substr(lb + 1, rb - lb - 1); auto tokens = extractJsonArrayStrings("[" + pair + "]"); if (tokens.size() == 2) { auto it = tokens.begin(); std::string elem = *it++; std::string tag = *it; allAdds.emplace_back(elem, tag); } p = rb + 1; } } // Determine which elements are still live (have β‰₯1 non-tombstoned tag) std::map> elemTags; for (const auto& [elem, tag] : allAdds) elemTags[elem].push_back(tag); std::set liveElements; for (const auto& [elem, tags] : elemTags) for (const auto& t : tags) if (tombstones.find(t) == tombstones.end()) { liveElements.insert(elem); break; } // Produce JSON array of live elements std::ostringstream oss; oss << "["; bool first = true; for (const auto& e : liveElements) { if (!first) oss << ","; oss << "\"" << e << "\""; first = false; } oss << "]"; return oss.str(); } std::string CRDTMergeResolver::mergeLWWMap(const std::vector& writes) { // LWW-Map: per-key last-write-wins; use HLC to pick winner per key std::map> best; for (const auto& w : writes) { auto fields = extractJsonInts(w.data); for (const auto& [k, v] : fields) { auto it = best.find(k); if (it == best.end() || it->second.first < w.hlc) { best[k] = {w.hlc, std::to_string(v)}; } } } std::ostringstream oss; oss << "{"; bool first = true; for (const auto& [k, p] : best) { if (!first) oss << ","; oss << "\"" << k << "\":" << p.second; first = false; } oss << "}"; return oss.str(); } std::string CRDTMergeResolver::mergeTwoPSet(const std::vector& writes) { // Two-Phase Set (2P-Set): an element may be added and removed; once removed it cannot // be re-added (tombstone is permanent). // // Expected data format: {"add":["apple","banana","cherry"],"remove":["banana"]} // Merge: union(add) across writes, union(remove) across writes. // Result: elements that appear in the merged add-set but NOT in the merged remove-set. std::set addSet, removeSet; for (const auto& w : writes) { auto addArr = extractSubArray(w.data, "add"); auto removeArr = extractSubArray(w.data, "remove"); for (const auto& e : extractJsonArrayStrings(addArr)) addSet.insert(e); for (const auto& e : extractJsonArrayStrings(removeArr)) removeSet.insert(e); } // Build result: elements in addSet not in removeSet std::ostringstream oss; oss << "["; bool first = true; for (const auto& e : addSet) { if (removeSet.count(e)) continue; if (!first) oss << ","; oss << "\"" << e << "\""; first = false; } oss << "]"; return oss.str(); } std::string CRDTMergeResolver::mergeRGA(const std::vector& writes) { // Replicated Growable Array (RGA): an ordered sequence where each element carries a // unique logical identifier. Concurrent inserts are ordered deterministically by id; // deleted elements are kept as tombstones to preserve ordering. // // Expected data format (array of element objects): // [{"id":"100:nodeA","v":"hello","del":false},{"id":"200:nodeB","v":"world","del":false}] // // Merge: union of all elements by unique id; if an id appears in multiple writes, prefer // the tombstoned version (a deletion is irrevocable). Sort by id lexicographically. // Element struct: id, value, deleted struct RGAElem { std::string id; std::string value; bool deleted{false}; }; std::map byId; for (const auto& w : writes) { // Parse each object inside the top-level JSON array const std::string& src = w.data; size_t p = 0; // Skip leading '[' if present while (p < src.size() && src[p] != '{') ++p; while (p < src.size()) { auto ob = src.find('{', p); if (ob == std::string::npos) break; // Find matching '}' size_t depth = 0, oe = ob; while (oe < src.size()) { if (src[oe] == '{') ++depth; else if (src[oe] == '}') { if (--depth == 0) break; } ++oe; } std::string obj = src.substr(ob, oe - ob + 1); // Extract "id" field RGAElem elem; { auto kp = obj.find("\"id\""); if (kp != std::string::npos) { auto vs = obj.find('"', kp + 4); if (vs != std::string::npos) { auto ve = obj.find('"', vs + 1); if (ve != std::string::npos) elem.id = obj.substr(vs + 1, ve - vs - 1); } } } if (elem.id.empty()) { p = oe + 1; continue; } // Extract "v" field { auto kp = obj.find("\"v\""); if (kp != std::string::npos) { auto vs = obj.find('"', kp + 3); if (vs != std::string::npos) { auto ve = obj.find('"', vs + 1); if (ve != std::string::npos) elem.value = obj.substr(vs + 1, ve - vs - 1); } } } // Extract "del" field { auto kp = obj.find("\"del\""); if (kp != std::string::npos) { auto vp = kp + 5; while (vp < obj.size() && (obj[vp] == ' ' || obj[vp] == ':')) ++vp; elem.deleted = (obj.substr(vp, 4) == "true"); } } // Merge: deletion is irrevocable auto it = byId.find(elem.id); if (it == byId.end()) { byId[elem.id] = std::move(elem); } else { if (elem.deleted) it->second.deleted = true; // Keep the value from the first observed insert (already stored) } p = oe + 1; } } // Produce sorted JSON array (include tombstones so remote nodes can apply deletes) std::ostringstream oss; oss << "["; bool first = true; for (const auto& [id, elem] : byId) { if (!first) oss << ","; oss << "{\"id\":\"" << elem.id << "\"," << "\"v\":\"" << elem.value << "\"," << "\"del\":" << (elem.deleted ? "true" : "false") << "}"; first = false; } oss << "]"; return oss.str(); } std::string CRDTMergeResolver::mergeFlagEW(const std::vector& writes) { // Enable-Wins Flag: concurrent enable + disable β†’ enabled. // // Data format: {"e":["tag-1","tag-2"],"d":["tag-3"]} // "e" = set of enable-tags (each add-enable call contributes a unique tag) // "d" = set of disable-tags (each disable call records the tags it wants to cancel) // // Merge: union of all "e" arrays; union of all "d" arrays. // Value: enabled = βˆƒ tag ∈ union(e) s.t. tag βˆ‰ union(d) // (there is at least one live enable-tag not yet disabled β†’ flag is ON) std::set enableTags, disableTags; for (const auto& w : writes) { auto eArr = extractSubArray(w.data, "e"); for (const auto& t : extractJsonArrayStrings(eArr)) enableTags.insert(t); auto dArr = extractSubArray(w.data, "d"); for (const auto& t : extractJsonArrayStrings(dArr)) disableTags.insert(t); } bool enabled = false; for (const auto& t : enableTags) { if (disableTags.find(t) == disableTags.end()) { enabled = true; break; } } std::ostringstream oss; oss << "{\"enabled\":" << (enabled ? "true" : "false") << "}"; return oss.str(); } std::string CRDTMergeResolver::mergeFlagDW(const std::vector& writes) { // Disable-Wins Flag: concurrent enable + disable β†’ disabled. // // Data format: {"e":["tag-1"],"d":["tag-2"]} // Same structure as FLAG_EW. // // Merge: union of all "e" arrays; union of all "d" arrays. // Value: enabled = union(e) β‰  βˆ… ∧ union(d) = βˆ… // (only enabled when at least one enable-tag exists and no disable-tag exists) std::set enableTags, disableTags; for (const auto& w : writes) { auto eArr = extractSubArray(w.data, "e"); for (const auto& t : extractJsonArrayStrings(eArr)) enableTags.insert(t); auto dArr = extractSubArray(w.data, "d"); for (const auto& t : extractJsonArrayStrings(dArr)) disableTags.insert(t); } bool enabled = !enableTags.empty() && disableTags.empty(); std::ostringstream oss; oss << "{\"enabled\":" << (enabled ? "true" : "false") << "}"; return oss.str(); } // ============================================================================ // MMWriteEntry Serialization // ============================================================================ std::vector MMWriteEntry::serialize() const { std::vector result; auto appendUint64 = [&result](uint64_t val) { for (int i = 7; i >= 0; --i) result.push_back(static_cast((val >> (i * 8)) & 0xFF)); }; auto appendUint32 = [&result](uint32_t val) { for (int i = 3; i >= 0; --i) result.push_back(static_cast((val >> (i * 8)) & 0xFF)); }; auto appendString = [&result](const std::string& s) { uint32_t len = static_cast(s.size()); for (int i = 3; i >= 0; --i) result.push_back(static_cast((len >> (i * 8)) & 0xFF)); result.insert(result.end(), s.begin(), s.end()); }; appendString(write_id); appendString(origin_node); appendString(collection); appendString(document_id); appendString(operation); appendString(data); appendString(checksum); appendString(vector_clock.toJson()); appendUint64(hlc.physical); appendUint32(hlc.logical); appendString(hlc.node_id); return result; } std::optional MMWriteEntry::deserialize(const std::vector& raw) { if (raw.size() < 4) return std::nullopt; size_t pos = 0; auto readUint64 = [&]() -> uint64_t { uint64_t v = 0; for (int i = 0; i < 8 && pos < raw.size(); ++i, ++pos) v = (v << 8) | raw[pos]; return v; }; auto readUint32 = [&]() -> uint32_t { uint32_t v = 0; for (int i = 0; i < 4 && pos < raw.size(); ++i, ++pos) v = (v << 8) | raw[pos]; return v; }; bool parse_ok = true; auto readString = [&]() -> std::string { if (pos + 4 > raw.size()) { parse_ok = false; THEMIS_WARN("MMWriteEntry::deserialize: truncated while reading string length at offset {}", pos); return {}; } uint32_t len = readUint32(); if (pos + len > raw.size()) { parse_ok = false; THEMIS_WARN("MMWriteEntry::deserialize: truncated while reading string payload (len={}) at offset {}", len, pos); return {}; } std::string s(raw.begin() + pos, raw.begin() + pos + len); pos += len; return s; }; MMWriteEntry e; e.write_id = readString(); e.origin_node = readString(); e.collection = readString(); e.document_id = readString(); e.operation = readString(); e.data = readString(); e.checksum = readString(); e.vector_clock = VectorClock::fromJson(readString()); e.hlc.physical = readUint64(); e.hlc.logical = readUint32(); e.hlc.node_id = readString(); if (!parse_ok) { THEMIS_WARN("MMWriteEntry::deserialize: input truncated or malformed"); return std::nullopt; } return e; } // ============================================================================ // CustomResolver Implementation // ============================================================================ CustomResolver::CustomResolver(ResolverFunc resolver) : resolver_(std::move(resolver)) { } MMWriteEntry CustomResolver::resolve( const std::string& document_id, const std::vector& conflicting_writes) { if (resolver_) { return resolver_(document_id, conflicting_writes); } // Fallback to LWW LastWriteWinsResolver lwr; return lwr.resolve(document_id, conflicting_writes); } // ============================================================================ // MultiMasterReplicationManager Implementation // ============================================================================ // Helper: generate a short unique ID (write_id) static std::string generateWriteId(const std::string& node_id) { static std::atomic seq{0}; uint64_t ts = static_cast( std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count() ); return node_id + "-" + std::to_string(ts) + "-" + std::to_string(seq.fetch_add(1)); } // ------------------------- // Constructor / Destructor // ------------------------- MultiMasterReplicationManager::MultiMasterReplicationManager( const MMReplicationConfig& config) : config_(config) , vector_clock_(std::make_unique(config.node_id)) , hlc_(std::make_unique(config.node_id)) , default_resolver_(std::make_shared()) { } MultiMasterReplicationManager::~MultiMasterReplicationManager() { stop(); } // ------------------------- // Lifecycle // ------------------------- bool MultiMasterReplicationManager::start() { if (running_.exchange(true)) { return true; // Already running } replication_thread_ = std::thread(&MultiMasterReplicationManager::replicationLoop, this); heartbeat_thread_ = std::thread(&MultiMasterReplicationManager::heartbeatLoop, this); sync_thread_ = std::thread(&MultiMasterReplicationManager::syncLoop, this); THEMIS_INFO("MultiMasterReplicationManager started (node_id={})", config_.node_id); return true; } void MultiMasterReplicationManager::stop() { if (!running_.exchange(false)) { return; // Already stopped } writes_cv_.notify_all(); if (replication_thread_.joinable()) replication_thread_.join(); if (heartbeat_thread_.joinable()) heartbeat_thread_.join(); if (sync_thread_.joinable()) sync_thread_.join(); THEMIS_INFO("MultiMasterReplicationManager stopped (node_id={})", config_.node_id); } bool MultiMasterReplicationManager::isRunning() const { return running_.load(); } // ------------------------- // Write Operations // ------------------------- std::string MultiMasterReplicationManager::write( const std::string& collection, const std::string& document_id, const std::string& operation, const std::string& data, WriteCallback callback) { if (!running_.load()) { THEMIS_ERROR("MMReplicationManager not running – write rejected"); return {}; } MMWriteEntry entry; entry.write_id = generateWriteId(config_.node_id); entry.origin_node = config_.node_id; entry.collection = collection; entry.document_id = document_id; entry.operation = operation; entry.data = data; entry.hlc = hlc_->now(); // Stamp with our vector clock and advance it { vector_clock_->increment(config_.node_id); entry.vector_clock = *vector_clock_; } // Compute simple checksum (SHA-256 of content) { std::string content = operation + collection + document_id + data; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(reinterpret_cast(content.c_str()), content.size(), hash); std::ostringstream oss; for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(hash[i]); entry.checksum = oss.str(); } { std::lock_guard lock(writes_mutex_); pending_writes_.push(entry); if (callback) { write_callbacks_[entry.write_id] = std::move(callback); } } writes_cv_.notify_one(); stats_writes_total_.fetch_add(1); THEMIS_INFO("MM write queued: write_id={} collection={} doc={} op={}", entry.write_id, collection, document_id, operation); return entry.write_id; } bool MultiMasterReplicationManager::writeSync( const std::string& collection, const std::string& document_id, const std::string& operation, const std::string& data, std::chrono::milliseconds timeout) { // Use shared_ptr to safely share the promise between this stack frame and // the callback which may execute on a different thread after this call returns. auto promise = std::make_shared>(); auto future = promise->get_future(); write(collection, document_id, operation, data, [promise](const MMWriteEntry& /*entry*/, bool success) { promise->set_value(success); }); return future.wait_for(timeout) == std::future_status::ready && future.get(); } // ------------------------- // Read Operations // ------------------------- MultiMasterReplicationManager::ReadResult MultiMasterReplicationManager::read( const std::string& /*collection*/, const std::string& /*document_id*/, uint32_t read_quorum) { ReadResult result; result.source_node = config_.node_id; result.data = ""; // Actual document content comes from the calling layer's storage if (!running_.load()) [[unlikely]] { result.success = false; result.version = *vector_clock_; return result; } // Resolve effective quorum: 0 β†’ use config default const uint32_t effective_quorum = (read_quorum == 0) ? config_.read_quorum : read_quorum; // Snapshot the peer list under the shared lock std::vector peers_snapshot; { std::shared_lock lk(peers_mutex_); peers_snapshot.reserve(peers_.size()); for (const auto& [id, info] : peers_) { peers_snapshot.push_back(info); } } // Count ACTIVE peers whose last-known vector clock does not strictly // dominate our local clock (i.e. we are not known-stale relative to them). // A peer clock that strictly dominates ours means they have writes we have // not yet received β€” that peer signals a potential stale read. uint32_t agreeing_peers = 0; bool stale_read_detected = false; const VectorClock local_clock = *vector_clock_; for (const auto& peer : peers_snapshot) { if (peer.state == MMNodeState::OFFLINE || peer.state == MMNodeState::PARTITIONED) { continue; } // If the peer clock is at or before our local clock (compare returns // 1 when local > peer, or peer.happensBefore(local)), this peer // has converged to at least our state β†’ counts toward quorum. // compare() returns: -1 (peer>local / localpeer) const int cmp = local_clock.compare(peer.last_known_clock); if (cmp >= 0) { // local clock >= peer clock: peer is not ahead of us ++agreeing_peers; } else { // Peer has a version vector that this node has not yet received stale_read_detected = true; } } // We always count the local node itself as one "vote" ++agreeing_peers; result.success = (agreeing_peers >= effective_quorum); result.version = local_clock; if (stale_read_detected) { THEMIS_WARN("MultiMasterRead: potential stale read detected for node={} " "(agreeing_peers={}, quorum={})", config_.node_id, agreeing_peers, effective_quorum); } return result; } // ------------------------- // Peer Management // ------------------------- void MultiMasterReplicationManager::addPeer(const MMPeerInfo& peer) { std::unique_lock lock(peers_mutex_); peers_[peer.node_id] = peer; THEMIS_INFO("MM peer added: node_id={} endpoint={}", peer.node_id, peer.endpoint); } void MultiMasterReplicationManager::removePeer(const std::string& node_id) { std::unique_lock lock(peers_mutex_); peers_.erase(node_id); THEMIS_INFO("MM peer removed: node_id={}", node_id); } std::vector MultiMasterReplicationManager::getPeers() const { std::shared_lock lock(peers_mutex_); std::vector result; result.reserve(peers_.size()); for (const auto& [id, info] : peers_) { result.push_back(info); } return result; } MMPeerInfo MultiMasterReplicationManager::getLocalInfo() const { MMPeerInfo info; info.node_id = config_.node_id; info.datacenter = config_.datacenter; info.region = config_.region; info.state = running_.load() ? MMNodeState::ACTIVE : MMNodeState::OFFLINE; info.replication_lag_ms = getReplicationLag(); info.is_local_datacenter = true; info.last_known_clock = *vector_clock_; return info; } // ------------------------- // Conflict Management // ------------------------- void MultiMasterReplicationManager::registerConflictCallback(ConflictCallback callback) { std::lock_guard lock(conflicts_mutex_); conflict_callbacks_.push_back(std::move(callback)); } void MultiMasterReplicationManager::setConflictResolver( const std::string& collection, std::shared_ptr resolver) { std::lock_guard lock(conflicts_mutex_); resolvers_[collection] = std::move(resolver); } std::vector MultiMasterReplicationManager::getUnresolvedConflicts() const { std::lock_guard lock(conflicts_mutex_); std::vector result; for (const auto& rec : conflicts_) { if (!rec.resolved) { result.push_back(rec); } } return result; } bool MultiMasterReplicationManager::resolveConflict( const std::string& conflict_id, const std::string& winning_write_id) { std::lock_guard lock(conflicts_mutex_); for (auto& rec : conflicts_) { if (rec.conflict_id == conflict_id && !rec.resolved) { rec.resolved = true; rec.winning_write_id = winning_write_id; stats_conflicts_resolved_.fetch_add(1); return true; } } return false; } // ------------------------- // Synchronization // ------------------------- void MultiMasterReplicationManager::triggerSync() { // Wake up the sync loop immediately writes_cv_.notify_all(); } uint64_t MultiMasterReplicationManager::getReplicationLag() const { std::shared_lock lock(peers_mutex_); uint64_t max_lag = 0; for (const auto& [id, peer] : peers_) { max_lag = std::max(max_lag, peer.replication_lag_ms); } return max_lag; } // ------------------------- // Statistics // ------------------------- MultiMasterReplicationManager::Stats MultiMasterReplicationManager::getStats() const { Stats s; s.writes_total = stats_writes_total_.load(); s.writes_replicated = stats_writes_replicated_.load(); s.conflicts_detected = stats_conflicts_detected_.load(); s.conflicts_resolved = stats_conflicts_resolved_.load(); s.sync_rounds = stats_sync_rounds_.load(); s.bytes_sent = stats_bytes_sent_.load(); s.bytes_received = stats_bytes_received_.load(); { std::lock_guard lock(writes_mutex_); s.writes_pending = pending_writes_.size(); } s.avg_replication_latency = std::chrono::milliseconds(0); return s; } MultiMasterReplicationManager::TopologySnapshot MultiMasterReplicationManager::getTopologySnapshot() const { TopologySnapshot snapshot; snapshot.local_node_id = config_.node_id; snapshot.replication_mode = "MULTI_MASTER"; auto stateToString = [](MMNodeState state) -> std::string { switch (state) { case MMNodeState::ACTIVE: return "ACTIVE"; case MMNodeState::SYNCING: return "SYNCING"; case MMNodeState::PARTITIONED: return "PARTITIONED"; case MMNodeState::RECOVERING: return "RECOVERING"; case MMNodeState::OFFLINE: default: return "OFFLINE"; } }; TopologyNode local; local.node_id = config_.node_id; local.endpoint = config_.node_id; local.datacenter = config_.datacenter; local.region = config_.region; local.state = running_.load() ? "ACTIVE" : "OFFLINE"; local.replication_lag_ms = 0; local.is_local = true; snapshot.nodes.push_back(std::move(local)); { std::shared_lock lock(peers_mutex_); snapshot.nodes.reserve(snapshot.nodes.size() + peers_.size()); snapshot.edges.reserve(peers_.size() * 2); for (const auto& [peer_id, peer] : peers_) { TopologyNode node; node.node_id = peer.node_id; node.endpoint = peer.endpoint; node.datacenter = peer.datacenter; node.region = peer.region; node.state = stateToString(peer.state); node.replication_lag_ms = peer.replication_lag_ms; node.is_local = false; snapshot.nodes.push_back(std::move(node)); TopologyEdge edge; edge.from = config_.node_id; edge.to = peer_id; edge.type = "PEER"; snapshot.edges.push_back(std::move(edge)); // Multi-master links are symmetric for topology visualization. TopologyEdge reverse_edge; reverse_edge.from = peer_id; reverse_edge.to = config_.node_id; reverse_edge.type = "PEER"; snapshot.edges.push_back(std::move(reverse_edge)); } } snapshot.max_lag_ms = getReplicationLag(); return snapshot; } std::string MultiMasterReplicationManager::exportPrometheusMetrics() const { auto s = getStats(); std::ostringstream oss; oss << "# HELP themisdb_mm_writes_total Total multi-master writes\n" << "# TYPE themisdb_mm_writes_total counter\n" << "themisdb_mm_writes_total{node=\"" << config_.node_id << "\"} " << s.writes_total << "\n" << "# HELP themisdb_mm_writes_replicated Writes successfully replicated\n" << "# TYPE themisdb_mm_writes_replicated counter\n" << "themisdb_mm_writes_replicated{node=\"" << config_.node_id << "\"} " << s.writes_replicated << "\n" << "# HELP themisdb_mm_writes_pending Pending writes in queue\n" << "# TYPE themisdb_mm_writes_pending gauge\n" << "themisdb_mm_writes_pending{node=\"" << config_.node_id << "\"} " << s.writes_pending << "\n" << "# HELP themisdb_mm_conflicts_detected Conflicts detected\n" << "# TYPE themisdb_mm_conflicts_detected counter\n" << "themisdb_mm_conflicts_detected{node=\"" << config_.node_id << "\"} " << s.conflicts_detected << "\n" << "# HELP themisdb_mm_conflicts_resolved Conflicts resolved\n" << "# TYPE themisdb_mm_conflicts_resolved counter\n" << "themisdb_mm_conflicts_resolved{node=\"" << config_.node_id << "\"} " << s.conflicts_resolved << "\n" << "# HELP themisdb_mm_sync_rounds Anti-entropy sync rounds completed\n" << "# TYPE themisdb_mm_sync_rounds counter\n" << "themisdb_mm_sync_rounds{node=\"" << config_.node_id << "\"} " << s.sync_rounds << "\n" << "# HELP themisdb_mm_replication_lag_ms Max replication lag across peers\n" << "# TYPE themisdb_mm_replication_lag_ms gauge\n" << "themisdb_mm_replication_lag_ms{node=\"" << config_.node_id << "\"} " << getReplicationLag() << "\n"; return oss.str(); } // ------------------------- // Background Loops // ------------------------- void MultiMasterReplicationManager::replicationLoop() { while (running_.load()) { // Drain a batch of pending writes under the lock, then release the // lock before doing any network work to avoid holding it during I/O. std::vector> batch; { std::unique_lock lock(writes_mutex_); writes_cv_.wait_for(lock, std::chrono::milliseconds(config_.sync_interval_ms), [this] { return !running_.load() || !pending_writes_.empty(); }); while (!pending_writes_.empty()) { MMWriteEntry entry = std::move(pending_writes_.front()); pending_writes_.pop(); WriteCallback cb; auto it = write_callbacks_.find(entry.write_id); if (it != write_callbacks_.end()) { cb = std::move(it->second); write_callbacks_.erase(it); } batch.emplace_back(std::move(entry), std::move(cb)); } } // lock released here // Process the batch without holding writes_mutex_ for (auto& [entry, cb] : batch) { bool ok = replicateWrite(entry); if (ok) { stats_writes_replicated_.fetch_add(1); // Append to committed write log so getMissingWrites() can // compute the delta for lagging peers. { std::lock_guard log_lock(committed_log_mutex_); committed_writes_log_.push_back(entry); // Cap to 2Γ— max_pending_writes to bound memory. const size_t cap = static_cast(config_.max_pending_writes) * 2u; while (committed_writes_log_.size() > cap) { committed_writes_log_.pop_front(); } } } if (cb) { cb(entry, ok); } } } } void MultiMasterReplicationManager::heartbeatLoop() { while (running_.load()) { std::this_thread::sleep_for(std::chrono::milliseconds(config_.heartbeat_interval_ms)); if (!running_.load()) break; auto now_ts = hlc_->now(); // Use unique_lock because we mutate peer.last_heartbeat_hlc std::unique_lock lock(peers_mutex_); for (auto& [node_id, peer] : peers_) { // In a full implementation: send AppendEntries / heartbeat RPC. // Update last_heartbeat_hlc to the current timestamp. peer.last_heartbeat_hlc = now_ts; } } } void MultiMasterReplicationManager::syncLoop() { while (running_.load()) { std::this_thread::sleep_for(std::chrono::milliseconds(config_.sync_interval_ms)); if (!running_.load()) break; std::vector peer_ids; { std::shared_lock lock(peers_mutex_); for (const auto& [id, _] : peers_) { peer_ids.push_back(id); } } for (const auto& peer_id : peer_ids) { if (!running_.load()) break; antiEntropySync(peer_id); } stats_sync_rounds_.fetch_add(1); } } // ------------------------- // Internal: Replication // ------------------------- bool MultiMasterReplicationManager::replicateWrite(const MMWriteEntry& entry) { std::shared_lock lock(peers_mutex_); // Single-node mode: no peers configured means this node is authoritative // on its own; the write is considered locally applied without a quorum check. if (peers_.empty()) { return true; } uint32_t quorum = config_.write_quorum; if (quorum == 0) { THEMIS_ERROR("MM replication rejected write_id={} because write_quorum=0 with peers configured", entry.write_id); return false; } uint32_t eligible = 0; for (const auto& [node_id, peer] : peers_) { (void)node_id; if (peer.state != MMNodeState::OFFLINE && peer.state != MMNodeState::PARTITIONED) { ++eligible; } } if (eligible < quorum) { THEMIS_WARN("MM replication rejected write_id={} because eligible_peers={} < write_quorum={}", entry.write_id, eligible, quorum); return false; } uint32_t acked = 0; for (const auto& [node_id, peer] : peers_) { if (peer.state == MMNodeState::OFFLINE || peer.state == MMNodeState::PARTITIONED) { continue; } if (sendToPeer(node_id, entry)) { ++acked; } if (acked >= quorum) break; } return acked >= quorum; } bool MultiMasterReplicationManager::sendToPeer( const std::string& node_id, const MMWriteEntry& entry) { // In a full implementation this would serialize the entry and send it // over a mTLS connection to the peer node. For now we simulate success // for ACTIVE peers and record bytes_sent. std::shared_lock lock(peers_mutex_); auto it = peers_.find(node_id); if (it == peers_.end()) return false; if (it->second.state == MMNodeState::OFFLINE || it->second.state == MMNodeState::PARTITIONED) { return false; } auto serialized = entry.serialize(); stats_bytes_sent_.fetch_add(serialized.size()); return true; } void MultiMasterReplicationManager::receiveFromPeer( const std::string& node_id, const MMWriteEntry& incoming) { // Update our vector clock with the sender's clock vector_clock_->merge(incoming.vector_clock); hlc_->receive(incoming.hlc); // Update bytes_received auto serialized = incoming.serialize(); stats_bytes_received_.fetch_add(serialized.size()); // Check for conflict with any recently-seen write for the same document // (In production this would consult a local document store.) bool has_conflict = false; { std::lock_guard lock(conflicts_mutex_); for (const auto& rec : conflicts_) { if (!rec.resolved && rec.collection == incoming.collection && rec.document_id == incoming.document_id) { has_conflict = true; break; } } } if (has_conflict) { THEMIS_WARN("MM conflict detected for doc={}/{} from peer={}", incoming.collection, incoming.document_id, node_id); // For each unresolved conflict on this document, add the incoming entry std::lock_guard lock(conflicts_mutex_); for (auto& rec : conflicts_) { if (!rec.resolved && rec.collection == incoming.collection && rec.document_id == incoming.document_id) { rec.conflicting_writes.push_back(incoming); handleConflict(incoming.document_id, rec.conflicting_writes); break; } } } } // ------------------------- // Internal: Conflict Detection & Resolution // ------------------------- bool MultiMasterReplicationManager::detectConflict( const MMWriteEntry& incoming, const MMWriteEntry& existing) { // Two writes conflict when their vector clocks are concurrent (neither // happened-before the other). return incoming.vector_clock.isConcurrent(existing.vector_clock); } void MultiMasterReplicationManager::handleConflict( const std::string& document_id, const std::vector& conflicting_writes) { if (conflicting_writes.empty()) return; const std::string& collection = conflicting_writes[0].collection; // Find the appropriate resolver (collection-specific or default) std::shared_ptr resolver; { std::lock_guard lock(conflicts_mutex_); auto it = resolvers_.find(collection); resolver = (it != resolvers_.end()) ? it->second : default_resolver_; } MMWriteEntry winner = resolver->resolve(document_id, conflicting_writes); // Record the conflict ConflictRecord record; record.conflict_id = generateWriteId(config_.node_id); record.type = ConflictType::CONCURRENT_UPDATE; record.document_id = document_id; record.collection = collection; record.conflicting_writes = conflicting_writes; record.detected_at = std::chrono::system_clock::now(); record.resolved = true; record.resolution_strategy = (resolver == default_resolver_) ? "LAST_WRITE_WINS" : "CUSTOM"; record.winning_write_id = winner.write_id; { std::lock_guard lock(conflicts_mutex_); conflicts_.push_back(record); // Notify registered callbacks for (const auto& cb : conflict_callbacks_) { cb(record); } } stats_conflicts_detected_.fetch_add(1); stats_conflicts_resolved_.fetch_add(1); } // ------------------------- // Internal: Anti-Entropy // ------------------------- void MultiMasterReplicationManager::antiEntropySync(const std::string& peer_id) { // Retrieve the peer's known vector clock VectorClock peer_clock; { std::shared_lock lock(peers_mutex_); auto it = peers_.find(peer_id); if (it == peers_.end()) return; if (it->second.state == MMNodeState::OFFLINE || it->second.state == MMNodeState::PARTITIONED) { return; } peer_clock = it->second.last_known_clock; } // Find any writes that the peer has not seen yet auto missing = getMissingWrites(peer_clock); for (const auto& entry : missing) { sendToPeer(peer_id, entry); } // Update the peer's known clock to ours after sync { std::unique_lock lock(peers_mutex_); auto it = peers_.find(peer_id); if (it != peers_.end()) { it->second.last_known_clock = *vector_clock_; } } } std::vector MultiMasterReplicationManager::getMissingWrites( const VectorClock& peer_clock) { // Return all committed entries whose vector clock is NOT already dominated // by the peer's clock. An entry's vector clock `VC_e` is dominated by // `peer_clock` if `VC_e.happensBefore(peer_clock)` is true β€” meaning the // peer has already seen this entry. Any entry where that is false is // potentially missing from the peer and must be resent. std::lock_guard log_lock(committed_log_mutex_); std::vector missing; missing.reserve(committed_writes_log_.size()); for (const auto& entry : committed_writes_log_) { if (!entry.vector_clock.happensBefore(peer_clock)) { missing.push_back(entry); } } return missing; } // ============================================================================ // ParallelReplicationWorker Implementation (v1.6.0) // ============================================================================ ParallelReplicationWorker::ParallelReplicationWorker(const ParallelConfig& config) : config_(config) { running_.store(true); uint32_t n = std::max(1, config_.worker_threads); workers_.reserve(n); for (uint32_t i = 0; i < n; ++i) { workers_.emplace_back(&ParallelReplicationWorker::workerLoop, this); } } ParallelReplicationWorker::~ParallelReplicationWorker() { running_.store(false); queue_cv_.notify_all(); for (auto& t : workers_) { if (t.joinable()) t.join(); } } void ParallelReplicationWorker::submit(const WALEntry& entry) { auto done_flag = std::make_shared>(false); WorkItem item; item.entry = entry; item.ready = done_flag; item.submit_time = std::chrono::steady_clock::now(); if (config_.use_dependency_tracking) { std::lock_guard dep_lock(dep_mutex_); // If there's a previous write to the same document, add its done-flag // as a dependency so this write waits for it to complete. auto it = last_done_per_doc_.find(entry.document_id); if (it != last_done_per_doc_.end()) { item.deps.push_back(it->second); stats_deps_detected_.fetch_add(1); } // Register this write as the latest for this document last_done_per_doc_[entry.document_id] = done_flag; } { std::lock_guard q_lock(queue_mutex_); // Enforce max queue size: drop oldest if full while (work_queue_.size() >= config_.queue_size) { work_queue_.pop(); } in_flight_count_.fetch_add(1); work_queue_.push(std::move(item)); } queue_cv_.notify_one(); } void ParallelReplicationWorker::sync() { // Wait until the queue is drained AND all workers have finished processing while (true) { { std::lock_guard lock(queue_mutex_); if (work_queue_.empty() && in_flight_count_.load() == 0) break; } std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } ParallelReplicationWorker::Stats ParallelReplicationWorker::getStats() const { Stats s; s.entries_applied = stats_entries_applied_.load(); s.dependencies_detected = stats_deps_detected_.load(); s.parallel_batches = stats_batches_.load(); uint64_t applied = s.entries_applied; s.average_latency_us = (applied > 0) ? stats_total_latency_us_.load() / applied : 0u; uint64_t batches = s.parallel_batches; s.parallelism_factor = (batches > 0) ? static_cast(applied) / static_cast(batches) : 0.0; return s; } void ParallelReplicationWorker::workerLoop() { while (running_.load()) { // Collect a batch of work items (or a single item when group_transactions // is disabled). When group_transactions=true we drain all currently // available entries in one go to reduce per-entry scheduling overhead. std::vector batch; { std::unique_lock lock(queue_mutex_); queue_cv_.wait_for(lock, std::chrono::milliseconds(5), [this] { return !running_.load() || !work_queue_.empty(); }); if (work_queue_.empty()) continue; if (config_.group_transactions) { // Drain all currently available entries into the batch while (!work_queue_.empty()) { batch.push_back(std::move(work_queue_.front())); work_queue_.pop(); } } else { batch.push_back(std::move(work_queue_.front())); work_queue_.pop(); } } for (auto& item : batch) { // Wait for all dependencies to complete for (const auto& dep : item.deps) { while (!dep->load()) { std::this_thread::yield(); } } // Compute true submit-to-apply latency per entry (captured after deps resolve) auto apply_time = std::chrono::steady_clock::now(); auto latency_us = static_cast( std::chrono::duration_cast( apply_time - item.submit_time).count()); stats_total_latency_us_.fetch_add(latency_us); // Apply the entry (in production: write to local storage / state machine) // Here we simply mark it done and update stats. item.ready->store(true); stats_entries_applied_.fetch_add(1); // in_flight_count_ has a 1:1 relationship with submit() calls: each // submit() increments by 1, so each applied item decrements by 1. // This ensures sync() observes zero only when every submitted entry // has been fully applied, even across concurrent workers. in_flight_count_.fetch_sub(1); } // Count only iterations that actually processed work to keep // parallel_batches and parallelism_factor accurate. if (!batch.empty()) { stats_batches_.fetch_add(1); } } } // ============================================================================ // QuorumReadManager Implementation (v1.6.0) // ============================================================================ QuorumReadManager::QuorumReadManager( const QuorumReadConfig& config, const std::vector& replicas) : config_(config) , replicas_(replicas) { } QuorumReadManager::QuorumReadResult QuorumReadManager::read( const std::string& collection, const std::string& document_id, uint32_t quorum, const std::string& session_token) { uint32_t required = (quorum == 0) ? config_.read_quorum : quorum; // Parse session token to get the minimum version required for monotonic reads. uint64_t required_version = parseSessionToken(session_token); std::vector snapshot; { std::shared_lock lock(replicas_mutex_); snapshot = replicas_; } if (snapshot.empty()) { // Single-node path: no replicas configured. // When a local-document fetch function has been injected (Stub #248 // injection API), use it to populate the data and version fields so // that single-node deployments can serve real document content. // Without an injected function the data field remains empty and // version=0 (original behaviour retained as documented fallback). QuorumReadResult sr; sr.success = true; sr.had_conflicts = false; LocalDocumentFetchFn local_fn; { std::shared_lock lock(replicas_mutex_); local_fn = local_doc_fetch_fn_; } if (local_fn) { try { auto [data, version] = local_fn(collection, document_id); sr.data = std::move(data); sr.version = version; if (required_version > 0 && sr.version < required_version) { // Monotonic-read requirement not met; signal to caller. THEMIS_WARN("QuorumRead single-node: version {} < required {}; " "monotonic-read guarantee NOT met", sr.version, required_version); } } catch (const std::exception& e) { THEMIS_WARN("QuorumRead single-node: local_doc_fetch_fn_ threw: {}", e.what()); // Proceed with empty data; success remains true (degraded mode). } } else { // Single-node fallback without injected local read implementation. // The request is treated as successful for availability, but payload/version // remain empty/zero. Callers that require content must inject // setLocalDocumentFetchFn() during startup. THEMIS_WARN("QuorumRead single-node: no local_doc_fetch_fn_ injected; returning empty payload"); sr.version = 0; } sr.session_token = generateSessionToken(sr.version); return sr; } // Issue reads to all replicas concurrently std::vector> futures; futures.reserve(snapshot.size()); for (const auto& replica : snapshot) { futures.push_back(std::async(std::launch::async, [this, &replica, &collection, &document_id]() { return queryReplica(replica, collection, document_id); })); } // Collect responses respecting timeout. // When a session_token is provided we must see ALL replica responses // before filtering by required_version – breaking early after `required` // total responses would discard fresh replicas that come after a stale one // and could produce a false quorum-not-met result. auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(config_.read_timeout_ms); std::vector responses; for (auto& fut : futures) { auto remaining = deadline - std::chrono::steady_clock::now(); if (remaining.count() <= 0) break; if (fut.wait_for(remaining) == std::future_status::ready) { auto resp = fut.get(); if (resp.ok) responses.push_back(std::move(resp)); } // Without a session token a plain quorum check suffices, so we can // stop as soon as we have enough responses. With a session token we // need the full picture to correctly count qualifying replicas. if (session_token.empty() && responses.size() >= required) break; } if (responses.size() < required) { THEMIS_WARN("QuorumRead: only {}/{} replicas responded for {}/{}", responses.size(), required, collection, document_id); return QuorumReadResult{false, "", 0, false, {}, ""}; } // Session consistency: only responses that satisfy the minimum version // count toward the quorum. Collect qualifying responses separately. std::vector qualifying; qualifying.reserve(responses.size()); for (const auto& r : responses) { if (r.version >= required_version) { qualifying.push_back(&r); } } if (!session_token.empty() && qualifying.size() < required) { THEMIS_WARN("QuorumRead: SESSION not satisfied for {}/{} – " "only {}/{} replicas at required_version={}", collection, document_id, qualifying.size(), required, required_version); return QuorumReadResult{false, "", 0, false, {}, ""}; } // Use all responses for reconciliation (or only qualifying ones when a // session token was supplied so that stale replicas are not considered). std::vector reconcile_set; if (session_token.empty()) { reconcile_set = responses; } else { reconcile_set.reserve(qualifying.size()); for (const auto* p : qualifying) reconcile_set.push_back(*p); } // Reconcile: pick the response with the highest version const ReplicaResponse* best = &reconcile_set[0]; bool had_conflicts = false; for (const auto& r : reconcile_set) { if (r.version != best->version) had_conflicts = true; if (r.version > best->version) best = &r; } // Read-repair: if divergence is detected and repair_on_read is enabled, // identify stale replicas and schedule repair. if (had_conflicts && config_.repair_on_read) { for (const auto& r : reconcile_set) { if (r.version < best->version) { THEMIS_WARN("QuorumRead: read-repair triggered for replica {} " "(version {} < authoritative {})", r.endpoint, r.version, best->version); } } } // Collect source endpoints QuorumReadResult result; result.success = true; result.data = best->data; result.version = best->version; result.had_conflicts = had_conflicts; for (const auto& r : reconcile_set) { result.sources.push_back(r.endpoint); } result.session_token = generateSessionToken(best->version); if (had_conflicts) { THEMIS_WARN("QuorumRead: divergence detected for {}/{}, version {}", collection, document_id, best->version); } return result; } std::string QuorumReadManager::generateSessionToken(uint64_t version) const { // Format: "seq=;exp=" auto expiry_ms = std::chrono::duration_cast( (std::chrono::system_clock::now() + std::chrono::milliseconds(config_.session_token_ttl_ms)) .time_since_epoch()).count(); std::ostringstream oss; oss << "seq=" << version << ";exp=" << expiry_ms; return oss.str(); } uint64_t QuorumReadManager::parseSessionToken(const std::string& token) const { if (token.empty()) return 0; // Check expiry auto exp_pos = token.find("exp="); if (exp_pos != std::string::npos) { const std::string exp_prefix = "exp="; auto val_start = exp_pos + exp_prefix.size(); if (val_start < token.size()) { try { int64_t expiry_ms = std::stoll(token.substr(val_start)); auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); if (now_ms > expiry_ms) { THEMIS_WARN("QuorumRead: session token expired"); return 0; } } catch (...) { return 0; } } } auto seq_pos = token.find("seq="); if (seq_pos == std::string::npos) return 0; auto semi = token.find(';', seq_pos); std::string seq_str = token.substr( seq_pos + 4, (semi == std::string::npos) ? std::string::npos : semi - seq_pos - 4); try { return std::stoull(seq_str); } catch (...) { return 0; } } void QuorumReadManager::setReplicas(const std::vector& replicas) { std::unique_lock lock(replicas_mutex_); replicas_ = replicas; } void QuorumReadManager::setDocumentFetchCallback(DocumentFetchFn fn) { std::unique_lock lock(replicas_mutex_); doc_fetch_fn_ = std::move(fn); } void QuorumReadManager::setLocalDocumentFetchFn(LocalDocumentFetchFn fn) { std::unique_lock lock(replicas_mutex_); local_doc_fetch_fn_ = std::move(fn); } QuorumReadManager::ReplicaResponse QuorumReadManager::queryReplica( const ReplicaInfo& replica, const std::string& collection, const std::string& document_id) const { ReplicaResponse resp; resp.endpoint = replica.endpoint; resp.ok = (replica.health_status == HealthStatus::HEALTHY); resp.version = resp.ok ? replica.last_applied_sequence : 0; // When a document-fetch callback has been injected (by the storage layer // or a test harness), use it to populate the data field. Without the // callback the data field remains empty; health/version information is // still correct and the quorum-counting logic functions normally. if (resp.ok && doc_fetch_fn_) { try { resp.data = doc_fetch_fn_(replica.endpoint, collection, document_id); } catch (const std::exception& e) { THEMIS_WARN("QuorumRead: doc_fetch_fn_ threw for replica {}: {}", replica.endpoint, e.what()); resp.ok = false; resp.data = ""; } } return resp; } // ============================================================================ // PersistentReplicationState Implementation (v1.6.0) // ============================================================================ PersistentReplicationState::PersistentReplicationState( const std::string& state_file_path) : path_(state_file_path) { } bool PersistentReplicationState::persist(const State& state) { std::lock_guard lock(file_mutex_); try { std::ofstream ofs(path_, std::ios::trunc | std::ios::binary); if (!ofs.is_open()) { THEMIS_ERROR("PersistentReplicationState: cannot open {} for writing", path_); return false; } // Simple line-oriented text format: key=value ofs << "last_applied_sequence=" << state.last_applied_sequence << "\n" << "current_term=" << state.current_term << "\n" << "voted_for=" << state.voted_for << "\n" << "leader_id=" << state.leader_id << "\n" << "persisted_at_ms=" << std::chrono::duration_cast( state.persisted_at.time_since_epoch()).count() << "\n"; ofs.flush(); if (!ofs.good()) { THEMIS_ERROR("PersistentReplicationState: write error for {}", path_); return false; } return true; } catch (const std::exception& e) { THEMIS_ERROR("PersistentReplicationState::persist error: {}", e.what()); return false; } } PersistentReplicationState::State PersistentReplicationState::load() const { std::lock_guard lock(file_mutex_); State state; if (!std::filesystem::exists(path_)) { return state; // First-run: return default } try { std::ifstream ifs(path_); if (!ifs.is_open()) return state; std::string line; while (std::getline(ifs, line)) { auto eq = line.find('='); if (eq == std::string::npos) continue; std::string key = line.substr(0, eq); std::string val = line.substr(eq + 1); if (val.empty()) continue; try { if (key == "last_applied_sequence") state.last_applied_sequence = std::stoull(val); else if (key == "current_term") state.current_term = std::stoull(val); else if (key == "voted_for") state.voted_for = val; else if (key == "leader_id") state.leader_id = val; else if (key == "persisted_at_ms") { uint64_t ms = std::stoull(val); state.persisted_at = std::chrono::system_clock::time_point( std::chrono::milliseconds(ms)); } } catch (...) { THEMIS_WARN("PersistentReplicationState: failed to parse key={}", key); } } } catch (const std::exception& e) { THEMIS_ERROR("PersistentReplicationState::load error: {}", e.what()); } return state; } bool PersistentReplicationState::exists() const { return std::filesystem::exists(path_); } void PersistentReplicationState::remove() { std::lock_guard lock(file_mutex_); std::filesystem::remove(path_); } // ============================================================================ // CompressedReplicationStream Implementation (v1.6.0) // ============================================================================ CompressedReplicationStream::CompressedReplicationStream( const std::string& endpoint, const CompressionConfig& config) : endpoint_(endpoint) , config_(config) { } CompressedReplicationStream::CompressedReplicationStream(const std::string& endpoint) : CompressedReplicationStream(endpoint, CompressionConfig{}) { } std::string CompressedReplicationStream::algorithmName(CompressionAlgorithm algo) { switch (algo) { case CompressionAlgorithm::NONE: return "NONE"; case CompressionAlgorithm::LZ4: return "LZ4"; case CompressionAlgorithm::ZSTD: return "ZSTD"; case CompressionAlgorithm::SNAPPY: return "SNAPPY"; case CompressionAlgorithm::AUTO: return "AUTO"; default: return "UNKNOWN"; } } CompressedReplicationStream::CompressionAlgorithm CompressedReplicationStream::selectAlgorithm(size_t payload_bytes) const { if (config_.algorithm != CompressionAlgorithm::AUTO) { return config_.algorithm; } // AUTO mode: when adaptive=true (default), skip compression for tiny // payloads to avoid CPU overhead exceeding bandwidth savings. // When adaptive=false, always compress regardless of batch size. if (config_.adaptive && payload_bytes < config_.min_batch_size) { return CompressionAlgorithm::NONE; } return CompressionAlgorithm::ZSTD; } std::vector CompressedReplicationStream::serializeEntries( const std::vector& entries) const { // Simple serialization: length-prefixed JSON-like representation std::string buf; for (const auto& e : entries) { buf += std::to_string(e.sequence_number) + "|" + e.collection + "|" + e.document_id + "|" + e.operation + "|" + e.data + "\n"; } return std::vector(buf.begin(), buf.end()); } std::vector CompressedReplicationStream::compress( const std::vector& data, CompressionAlgorithm algo) const { if (data.empty()) { THEMIS_WARN("CompressedReplicationStream::compress called with empty data"); return {}; } switch (algo) { case CompressionAlgorithm::NONE: return data; case CompressionAlgorithm::LZ4: { int bound = LZ4_compressBound(static_cast(data.size())); std::vector out(static_cast(bound)); int compressed = LZ4_compress_default( reinterpret_cast(data.data()), reinterpret_cast(out.data()), static_cast(data.size()), bound ); if (compressed <= 0) { THEMIS_WARN("LZ4 compression failed, falling back to uncompressed"); return data; } out.resize(static_cast(compressed)); return out; } case CompressionAlgorithm::ZSTD: { size_t bound = ZSTD_compressBound(data.size()); std::vector out(bound); size_t compressed = ZSTD_compress( out.data(), bound, data.data(), data.size(), config_.compression_level ); if (ZSTD_isError(compressed)) { THEMIS_WARN("ZSTD compression error: {}", ZSTD_getErrorName(compressed)); return data; } out.resize(compressed); return out; } case CompressionAlgorithm::SNAPPY: { std::string input(reinterpret_cast(data.data()), data.size()); std::string output; snappy::Compress(input.data(), input.size(), &output); return std::vector(output.begin(), output.end()); } default: return data; } } std::vector CompressedReplicationStream::decompress( const std::vector& compressed, CompressionAlgorithm algo) const { if (compressed.empty()) { THEMIS_WARN("CompressedReplicationStream::decompress called with empty input"); return {}; } switch (algo) { case CompressionAlgorithm::NONE: return compressed; case CompressionAlgorithm::LZ4: { // LZ4 format does not store the original size, so we must pre-allocate // a buffer large enough to hold the decompressed output. We use 4Γ— the // compressed size as a conservative upper bound (typical LZ4 ratios for // text / JSON are 2-4Γ—); the extra 256 bytes guards against very small // compressed inputs whose expansion is dominated by header overhead. // LZ4_decompress_safe will return an error (negative) if the buffer is // too small, at which point the caller should retry with a larger buffer // or fall back to storing the original size alongside the compressed data. std::vector out(compressed.size() * 4 + 256); int result = LZ4_decompress_safe( reinterpret_cast(compressed.data()), reinterpret_cast(out.data()), static_cast(compressed.size()), static_cast(out.size()) ); if (result < 0) { THEMIS_ERROR("LZ4 decompression failed"); return {}; } out.resize(static_cast(result)); return out; } case CompressionAlgorithm::ZSTD: { uint64_t dsize = ZSTD_getFrameContentSize(compressed.data(), compressed.size()); if (dsize == ZSTD_CONTENTSIZE_UNKNOWN || dsize == ZSTD_CONTENTSIZE_ERROR) { THEMIS_ERROR("ZSTD: cannot determine decompressed size"); return {}; } std::vector out(dsize); size_t result = ZSTD_decompress( out.data(), dsize, compressed.data(), compressed.size() ); if (ZSTD_isError(result)) { THEMIS_ERROR("ZSTD decompression error: {}", ZSTD_getErrorName(result)); return {}; } out.resize(result); return out; } case CompressionAlgorithm::SNAPPY: { std::string input(reinterpret_cast(compressed.data()), compressed.size()); std::string output; if (!snappy::Uncompress(input.data(), input.size(), &output)) { THEMIS_ERROR("Snappy decompression failed"); return {}; } return std::vector(output.begin(), output.end()); } default: return compressed; } } bool CompressedReplicationStream::sendBatch(const std::vector& entries) { if (entries.empty()) return true; auto raw = serializeEntries(entries); uint64_t uncompressed_size = raw.size(); auto algo = selectAlgorithm(uncompressed_size); auto compressed = compress(raw, algo); uint64_t compressed_size = compressed.size(); // In production: send `compressed` over the network to `endpoint_` // Here we simply track statistics and return success. { std::lock_guard lock(stats_mutex_); stats_.bytes_uncompressed += uncompressed_size; stats_.bytes_compressed += compressed_size; stats_.algorithm_used = algorithmName(algo); if (stats_.bytes_uncompressed > 0) { stats_.compression_ratio = static_cast(stats_.bytes_uncompressed) / static_cast(stats_.bytes_compressed); } } return true; } CompressedReplicationStream::CompressionStats CompressedReplicationStream::getStats() const { std::lock_guard lock(stats_mutex_); return stats_; } void CompressedReplicationStream::resetStats() { std::lock_guard lock(stats_mutex_); stats_ = CompressionStats{}; } // ============================================================================ // BatchedAckTracker Implementation (v1.6.0) // ============================================================================ BatchedAckTracker::BatchedAckTracker() : BatchedAckTracker(AckBatchConfig{}) { } BatchedAckTracker::BatchedAckTracker(const AckBatchConfig& config) : config_(config) { running_.store(true); flush_thread_ = std::thread(&BatchedAckTracker::flushLoop, this); } BatchedAckTracker::~BatchedAckTracker() { running_.store(false); flush_cv_.notify_all(); if (flush_thread_.joinable()) flush_thread_.join(); } void BatchedAckTracker::recordApplied(uint64_t sequence_number) { { std::lock_guard lock(pending_mutex_); pending_.push_back(sequence_number); if (sequence_number > highest_acked_.load()) { highest_acked_.store(sequence_number); } if (pending_.size() >= config_.max_batch_size) { flushPending(); } } flush_cv_.notify_one(); } std::optional BatchedAckTracker::dequeuePendingAcks() { std::lock_guard lock(ready_mutex_); if (ready_batches_.empty()) return std::nullopt; auto batch = std::move(ready_batches_.front()); ready_batches_.pop(); return batch; } void BatchedAckTracker::forceFlush() { std::lock_guard lock(pending_mutex_); flushPending(); } void BatchedAckTracker::flushPending() { // Called with pending_mutex_ held if (pending_.empty()) return; AckBatch batch; batch.sequences = std::move(pending_); batch.created_at = std::chrono::system_clock::now(); pending_.clear(); stats_total_acks_.fetch_add(batch.sequences.size()); stats_total_batches_.fetch_add(1); { std::lock_guard rlock(ready_mutex_); ready_batches_.push(std::move(batch)); } } BatchedAckTracker::Stats BatchedAckTracker::getStats() const { Stats s; s.total_acks_sent = stats_total_acks_.load(); s.total_batches_sent = stats_total_batches_.load(); s.avg_batch_size = (s.total_batches_sent > 0) ? static_cast(s.total_acks_sent) / static_cast(s.total_batches_sent) : 0.0; return s; } void BatchedAckTracker::flushLoop() { while (running_.load()) { { std::unique_lock lock(pending_mutex_); flush_cv_.wait_for(lock, std::chrono::milliseconds(config_.flush_interval_ms), [this] { return !running_.load() || !pending_.empty(); }); flushPending(); } } // Final flush on shutdown { std::lock_guard lock(pending_mutex_); flushPending(); } } // ============================================================================ // ReplicationAnalytics Implementation (v1.6.0) // ============================================================================ ReplicationAnalytics::ReplicationAnalytics() = default; void ReplicationAnalytics::setConfig(const AnalyticsConfig& config) { config_ = config; } void ReplicationAnalytics::recordLag(const std::string& replica_id, int64_t lag_ms) { std::unique_lock lock(data_mutex_); auto& history = lag_history_[replica_id]; history.push_back({std::chrono::system_clock::now(), lag_ms}); // Rolling window: drop oldest entries beyond max_history_per_replica while (history.size() > config_.max_history_per_replica) { history.pop_front(); } } int64_t ReplicationAnalytics::percentile(const std::vector& sorted, double p) { if (sorted.empty()) return 0; // Caller must pass a sorted vector; index is clamped to valid range. size_t idx = static_cast(p / 100.0 * static_cast(sorted.size() - 1)); return sorted[std::min(idx, sorted.size() - 1)]; } ReplicationAnalytics::LagHistory ReplicationAnalytics::getLagHistory( const std::string& replica_id, std::chrono::hours duration) const { std::shared_lock lock(data_mutex_); LagHistory result; auto it = lag_history_.find(replica_id); if (it == lag_history_.end()) return result; auto cutoff = std::chrono::system_clock::now() - duration; std::vector values; for (const auto& dp : it->second) { if (dp.timestamp >= cutoff) { result.data_points.push_back(dp); values.push_back(dp.lag_ms); } } if (values.empty()) return result; std::sort(values.begin(), values.end()); // Sort once; reused by all percentile calls result.max_lag_ms = values.back(); result.avg_lag_ms = std::accumulate(values.begin(), values.end(), int64_t{0}) / static_cast(values.size()); result.p95_lag_ms = percentile(values, 95.0); result.p99_lag_ms = percentile(values, 99.0); return result; } std::vector ReplicationAnalytics::getInsights() const { std::shared_lock lock(data_mutex_); std::vector insights; auto now = std::chrono::system_clock::now(); for (const auto& [replica_id, history] : lag_history_) { if (history.empty()) continue; // Check last data point for spike int64_t last_lag = history.back().lag_ms; if (last_lag > config_.lag_spike_threshold_ms) { Insight ins; ins.type = "LAG_SPIKE"; ins.description = "Replica " + replica_id + " lag is " + std::to_string(last_lag) + "ms"; ins.recommendation = "Check network connectivity to " + replica_id + ", consider increasing batch_size"; ins.detected_at = now; ins.metadata["replica_id"] = replica_id; ins.metadata["lag_ms"] = std::to_string(last_lag); insights.push_back(std::move(ins)); } // Check rolling average for slow replica std::vector values; values.reserve(history.size()); for (const auto& dp : history) values.push_back(dp.lag_ms); int64_t avg = std::accumulate(values.begin(), values.end(), int64_t{0}) / static_cast(values.size()); if (avg > config_.slow_replica_avg_ms) { Insight ins; ins.type = "SLOW_REPLICA"; ins.description = "Replica " + replica_id + " avg lag is " + std::to_string(avg) + "ms"; ins.recommendation = "Investigate disk I/O or CPU on " + replica_id; ins.detected_at = now; ins.metadata["replica_id"] = replica_id; ins.metadata["avg_lag_ms"] = std::to_string(avg); insights.push_back(std::move(ins)); } } return insights; } std::vector ReplicationAnalytics::detectBottlenecks() const { std::shared_lock lock(data_mutex_); std::vector bottlenecks; for (const auto& [replica_id, history] : lag_history_) { if (history.size() < 2) continue; // Compute variance in lag as a proxy for the bottleneck type: // High variance + high avg β†’ NETWORK jitter // High avg + low variance β†’ DISK_IO // Very high avg β†’ CPU std::vector values; values.reserve(history.size()); for (const auto& dp : history) values.push_back(dp.lag_ms); int64_t avg = std::accumulate(values.begin(), values.end(), int64_t{0}) / static_cast(values.size()); int64_t max_val = *std::max_element(values.begin(), values.end()); if (avg <= 0) continue; // normalized_range = (max - mean) / mean // A high value indicates large relative spread (typical of network jitter). // This is distinct from the coefficient of variation (stddev/mean); using // the range here avoids an extra O(n) pass while still capturing spikiness. double normalized_range = static_cast(max_val - avg) / static_cast(avg); Bottleneck b; b.replica_id = replica_id; if (avg > 10000) { b.bottleneck_type = "CPU"; b.severity = std::min(1.0, static_cast(avg) / 30000.0); b.details = "Extremely high avg lag (" + std::to_string(avg) + "ms)"; } else if (normalized_range > 1.5) { b.bottleneck_type = "NETWORK"; b.severity = std::min(1.0, normalized_range / 5.0); b.details = "High lag spread (normalized_range=" + std::to_string(static_cast(normalized_range * 100)) + "%)"; } else if (avg > config_.slow_replica_avg_ms) { b.bottleneck_type = "DISK_IO"; b.severity = std::min(1.0, static_cast(avg) / static_cast(config_.slow_replica_avg_ms * 5)); b.details = "Consistently high lag (" + std::to_string(avg) + "ms avg)"; } else { continue; // No bottleneck detected } bottlenecks.push_back(std::move(b)); } return bottlenecks; } std::string ReplicationAnalytics::exportPrometheusMetrics() const { std::shared_lock lock(data_mutex_); std::ostringstream oss; oss << "# HELP themisdb_replication_lag_ms Current replication lag\n" << "# TYPE themisdb_replication_lag_ms gauge\n"; for (const auto& [replica_id, history] : lag_history_) { if (!history.empty()) { oss << "themisdb_replication_lag_ms{replica=\"" << replica_id << "\"} " << history.back().lag_ms << "\n"; } } return oss.str(); } // ============================================================================ // LagBasedReadRouter Implementation (v1.7.0) // ============================================================================ LagBasedReadRouter::LagBasedReadRouter() : config_() { } LagBasedReadRouter::LagBasedReadRouter(const RouterConfig& config) : config_(config) { } void LagBasedReadRouter::setConfig(const RouterConfig& config) { config_ = config; } size_t LagBasedReadRouter::eligibleReplicaCount( const std::vector& replicas) const { size_t count = 0; for (const auto& r : replicas) { if (r.role == ReplicationRole::FOLLOWER && r.health_status != HealthStatus::FAILED && r.replicationLagMs() <= config_.lag_threshold_ms) { ++count; } } return count; } LagBasedReadRouter::RoutingDecision LagBasedReadRouter::selectReplica( ReadPreference preference, const std::vector& replicas, const std::string& primary_node_id) const { RoutingDecision decision; decision.is_primary = false; decision.replica_lag_ms = -1; // PRIMARY preference: always route to primary. if (preference == ReadPreference::PRIMARY) { decision.node_id = primary_node_id; decision.is_primary = true; decision.reason = "ReadPreference::PRIMARY – routing to primary"; return decision; } // Collect eligible secondaries (lag within threshold, not FAILED). const ReplicaInfo* best = nullptr; int64_t best_lag = std::numeric_limits::max(); for (const auto& r : replicas) { if (r.role != ReplicationRole::FOLLOWER) continue; if (r.health_status == HealthStatus::FAILED) continue; int64_t lag = r.replicationLagMs(); if (lag > config_.lag_threshold_ms) continue; if (lag < best_lag) { best_lag = lag; best = &r; } } if (best) { decision.node_id = best->node_id; decision.replica_lag_ms = best_lag; decision.reason = "Lag-based routing: selected replica with lag=" + std::to_string(best_lag) + "ms"; return decision; } // No eligible secondary found. if (preference == ReadPreference::SECONDARY) { // SECONDARY_ONLY: return empty node to signal no eligible replica. decision.reason = "ReadPreference::SECONDARY – no eligible replica within lag threshold"; return decision; } // Fall back to primary for PRIMARY_PREFERRED, SECONDARY_PREFERRED, NEAREST. decision.node_id = primary_node_id; decision.is_primary = true; decision.reason = "All secondaries exceed lag threshold (" + std::to_string(config_.lag_threshold_ms) + "ms) – falling back to primary"; return decision; } std::string LagBasedReadRouter::exportPrometheusMetrics( const std::vector& replicas) const { std::ostringstream oss; oss << "# HELP themisdb_lag_router_eligible_replicas " "Number of replicas currently eligible for read routing\n" << "# TYPE themisdb_lag_router_eligible_replicas gauge\n" << "themisdb_lag_router_eligible_replicas " << eligibleReplicaCount(replicas) << "\n" << "# HELP themisdb_lag_router_threshold_ms " "Configured lag threshold for read routing eligibility\n" << "# TYPE themisdb_lag_router_threshold_ms gauge\n" << "themisdb_lag_router_threshold_ms " << config_.lag_threshold_ms << "\n"; for (const auto& r : replicas) { if (r.role != ReplicationRole::FOLLOWER) continue; int64_t lag = r.replicationLagMs(); int eligible = (r.health_status != HealthStatus::FAILED && lag <= config_.lag_threshold_ms) ? 1 : 0; oss << "themisdb_lag_router_replica_eligible{node_id=\"" << r.node_id << "\"} " << eligible << "\n"; } return oss.str(); } // ============================================================================ // ReplicationManager::selectReadReplica // ============================================================================ LagBasedReadRouter::RoutingDecision ReplicationManager::selectReadReplica( std::optional preference) const { ReadPreference pref = preference.value_or(config_.default_read_preference); LagBasedReadRouter router( LagBasedReadRouter::RouterConfig{ static_cast(config_.max_replication_lag_ms), true }); std::shared_lock lock(replicas_mutex_); return router.selectReplica(pref, replicas_, node_id_); } // ============================================================================ // ReplicationBenchmark Implementation (v1.6.0) // ============================================================================ ReplicationBenchmark::ReplicationBenchmark(std::shared_ptr wal) : ReplicationBenchmark(std::move(wal), BenchmarkConfig{}) { } ReplicationBenchmark::ReplicationBenchmark( std::shared_ptr wal, const BenchmarkConfig& config) : wal_(std::move(wal)) , config_(config) { } ReplicationBenchmark::BenchmarkResult ReplicationBenchmark::run() { // Build a dummy payload of the requested size std::string payload(config_.entry_size_bytes, 'x'); // Warm up (entries not counted in result) for (uint32_t i = 0; i < config_.warmup_entries; ++i) { WALEntry e; e.sequence_number = 0; e.collection = config_.collection; e.document_id = "warmup-" + std::to_string(i); e.operation = "INSERT"; e.data = payload; wal_->append(e); } // Measurement run std::vector latencies_us; latencies_us.reserve(config_.num_entries); uint64_t bytes_written = 0; auto bench_start = std::chrono::high_resolution_clock::now(); for (uint32_t i = 0; i < config_.num_entries; ++i) { WALEntry e; e.sequence_number = 0; e.collection = config_.collection; e.document_id = "bench-" + std::to_string(i); e.operation = "INSERT"; e.data = payload; auto t0 = std::chrono::high_resolution_clock::now(); wal_->append(e); auto t1 = std::chrono::high_resolution_clock::now(); latencies_us.push_back( std::chrono::duration_cast(t1 - t0).count()); bytes_written += config_.entry_size_bytes; } auto bench_end = std::chrono::high_resolution_clock::now(); double duration_s = std::chrono::duration(bench_end - bench_start).count(); // Compute percentiles std::sort(latencies_us.begin(), latencies_us.end()); auto pct = [&](double p) -> int64_t { if (latencies_us.empty()) return 0; size_t idx = static_cast( p / 100.0 * static_cast(latencies_us.size() - 1)); return latencies_us[std::min(idx, latencies_us.size() - 1)]; }; BenchmarkResult r; r.total_entries = config_.num_entries; r.duration_seconds = duration_s; r.writes_per_second = (duration_s > 0.0) ? static_cast(config_.num_entries) / duration_s : 0.0; r.latency_p50_us = pct(50.0); r.latency_p95_us = pct(95.0); r.latency_p99_us = pct(99.0); r.latency_max_us = latencies_us.empty() ? 0 : latencies_us.back(); r.bytes_written = bytes_written; return r; } std::string ReplicationBenchmark::format(const BenchmarkResult& r) { std::ostringstream oss; oss << "=== ReplicationBenchmark Results ===\n" << "Entries: " << r.total_entries << "\n" << "Duration: " << r.duration_seconds << "s\n" << "Throughput: " << static_cast(r.writes_per_second) << " writes/sec\n" << "Bytes written: " << r.bytes_written << "\n" << "Latency p50: " << r.latency_p50_us << " Β΅s\n" << "Latency p95: " << r.latency_p95_us << " Β΅s\n" << "Latency p99: " << r.latency_p99_us << " Β΅s\n" << "Latency max: " << r.latency_max_us << " Β΅s\n"; return oss.str(); } // ============================================================================ // CDCManager Implementation (v1.6.0) // ============================================================================ uint64_t CDCManager::subscribe(const std::string& collection, CDCCallback callback) { uint64_t id = next_id_.fetch_add(1); std::unique_lock lock(subs_mutex_); subscriptions_.push_back({id, collection, std::move(callback)}); return id; } void CDCManager::unsubscribe(uint64_t subscription_id) { std::unique_lock lock(subs_mutex_); subscriptions_.erase( std::remove_if(subscriptions_.begin(), subscriptions_.end(), [subscription_id](const Subscription& s) { return s.id == subscription_id; }), subscriptions_.end()); } size_t CDCManager::subscriptionCount() const { std::shared_lock lock(subs_mutex_); return subscriptions_.size(); } void CDCManager::onWALEntryApplied(const WALEntry& entry) { std::shared_lock lock(subs_mutex_); for (const auto& sub : subscriptions_) { // Empty collection = wildcard; otherwise match on collection name if (sub.collection.empty() || sub.collection == entry.collection) { try { sub.callback(entry); } catch (const std::exception& e) { THEMIS_ERROR("CDCManager: subscriber {} threw: {}", sub.id, e.what()); } catch (...) { THEMIS_ERROR("CDCManager: subscriber {} threw unknown exception", sub.id); } } } } // ============================================================================ // Cross-Cluster Publish/Subscribe Replication Implementation (v1.7.0) // ============================================================================ // --------------------------------------------------------------------------- // PublicationFilter // --------------------------------------------------------------------------- bool PublicationFilter::matches(const WALEntry& entry) const { if (!include_collections.empty()) { bool found = false; for (const auto& col : include_collections) { if (col == entry.collection) { found = true; break; } } if (!found) return false; } if (!include_operations.empty()) { bool found = false; for (const auto& op : include_operations) { if (op == entry.operation) { found = true; break; } } if (!found) return false; } return true; } // --------------------------------------------------------------------------- // CrossClusterPublication // --------------------------------------------------------------------------- CrossClusterPublication::CrossClusterPublication(const std::string& name) : name_(name) {} const std::string& CrossClusterPublication::name() const { return name_; } void CrossClusterPublication::setFilter(const PublicationFilter& filter) { std::unique_lock lock(filter_mutex_); filter_ = filter; } PublicationFilter CrossClusterPublication::getFilter() const { std::shared_lock lock(filter_mutex_); return filter_; } uint64_t CrossClusterPublication::addRemoteSubscriber(RemoteSubscriberCallback callback) { uint64_t id = next_id_.fetch_add(1); std::unique_lock lock(subs_mutex_); subscribers_.push_back({id, std::move(callback)}); return id; } void CrossClusterPublication::removeRemoteSubscriber(uint64_t subscriber_id) { std::unique_lock lock(subs_mutex_); subscribers_.erase( std::remove_if(subscribers_.begin(), subscribers_.end(), [subscriber_id](const RemoteSubscriber& s) { return s.id == subscriber_id; }), subscribers_.end()); } size_t CrossClusterPublication::subscriberCount() const { std::shared_lock lock(subs_mutex_); return subscribers_.size(); } uint64_t CrossClusterPublication::publishedCount() const { return published_count_.load(); } void CrossClusterPublication::publish(const WALEntry& entry) { { std::shared_lock lock(filter_mutex_); if (!filter_.matches(entry)) return; } published_count_.fetch_add(1); std::shared_lock lock(subs_mutex_); for (const auto& sub : subscribers_) { try { sub.callback(entry); } catch (const std::exception& e) { THEMIS_ERROR("CrossClusterPublication[{}]: subscriber {} threw: {}", name_, sub.id, e.what()); } catch (...) { THEMIS_ERROR("CrossClusterPublication[{}]: subscriber {} threw unknown exception", name_, sub.id); } } } void CrossClusterPublication::onWALEntryApplied(const WALEntry& entry) { publish(entry); } std::string CrossClusterPublication::exportPrometheusMetrics() const { std::string label = "{publication=\"" + name_ + "\"}"; std::string m; m += "themisdb_cross_cluster_publication_published_total" + label + " " + std::to_string(published_count_.load()) + "\n"; m += "themisdb_cross_cluster_publication_subscribers" + label + " " + std::to_string(subscriberCount()) + "\n"; return m; } // --------------------------------------------------------------------------- // CrossClusterSubscription // --------------------------------------------------------------------------- CrossClusterSubscription::CrossClusterSubscription( const std::string& name, std::shared_ptr publication, ApplyCallback on_apply) : name_(name), publication_(std::move(publication)), on_apply_(std::move(on_apply)) {} CrossClusterSubscription::~CrossClusterSubscription() { disable(); } const std::string& CrossClusterSubscription::name() const { return name_; } void CrossClusterSubscription::enable() { std::lock_guard lock(enable_mutex_); if (enabled_.load()) return; // idempotent subscriber_id_ = publication_->addRemoteSubscriber([this](const WALEntry& e) { try { on_apply_(e); applied_count_.fetch_add(1); // Advance last_applied_seq_ to this entry's sequence (keep max) uint64_t expected = last_applied_seq_.load(); while (e.sequence_number > expected) { if (last_applied_seq_.compare_exchange_weak(expected, e.sequence_number)) break; } } catch (...) { error_count_.fetch_add(1); } }); enabled_.store(true); } void CrossClusterSubscription::disable() { std::lock_guard lock(enable_mutex_); if (!enabled_.load()) return; // idempotent publication_->removeRemoteSubscriber(subscriber_id_); subscriber_id_ = 0; enabled_.store(false); } bool CrossClusterSubscription::isEnabled() const { return enabled_.load(); } uint64_t CrossClusterSubscription::appliedCount() const { return applied_count_.load(); } uint64_t CrossClusterSubscription::lastAppliedSequence() const { return last_applied_seq_.load(); } uint64_t CrossClusterSubscription::errorCount() const { return error_count_.load(); } std::string CrossClusterSubscription::exportPrometheusMetrics() const { std::string label = "{subscription=\"" + name_ + "\"}"; std::string m; m += "themisdb_cross_cluster_subscription_applied_total" + label + " " + std::to_string(applied_count_.load()) + "\n"; m += "themisdb_cross_cluster_subscription_errors_total" + label + " " + std::to_string(error_count_.load()) + "\n"; m += "themisdb_cross_cluster_subscription_last_applied_sequence" + label + " " + std::to_string(last_applied_seq_.load()) + "\n"; return m; } // ============================================================================ // WALArchivalManager Implementation (v1.6.0) // ============================================================================ WALArchivalManager::WALArchivalManager(const ArchivalConfig& config, std::shared_ptr backend) : config_(config), backend_(std::move(backend)) { if (!backend_) { // Local filesystem backend: ensure archive directory exists std::error_code ec; std::filesystem::create_directories(config_.archive_directory, ec); } // Load existing index if present loadIndex(); } std::string WALArchivalManager::archivePath(uint64_t segment_id) const { std::ostringstream oss; if (backend_) { // Cloud object key: use configured prefix oss << config_.prefix; } else { // Local filesystem path: use archive directory oss << config_.archive_directory << "/"; } oss << "seg_" << std::setw(20) << std::setfill('0') << segment_id; // Extension ordering: .wal[.zst][.enc] // .wal – base WAL segment // .zst – ZSTD-compressed (applied before encryption so encrypted bytes // cannot be compressed further) // .enc – AES-256-GCM encrypted (outermost wrapper) if (config_.compress_before_archive) oss << ".wal.zst"; else oss << ".wal"; // Append .enc when encryption at rest is configured (invalid/empty keys are // rejected before this path is ever computed, so encrypt_at_rest alone suffices). if (config_.encrypt_at_rest) oss << ".enc"; return oss.str(); } /* static */ std::vector WALArchivalManager::hexToBytes( const std::string& hex) { // Validate: must be non-empty, even-length, and contain only hex digits if (hex.empty() || hex.size() % 2 != 0) { THEMIS_WARN("WALArchivalManager::hexToBytes: invalid hex length (size={})", hex.size()); return {}; } for (char c : hex) { if (!std::isxdigit(static_cast(c))) { THEMIS_WARN("WALArchivalManager::hexToBytes: invalid hex char '{}' in input", c); return {}; } } std::vector bytes; bytes.reserve(hex.size() / 2); for (size_t i = 0; i < hex.size(); i += 2) { unsigned int val = 0; std::istringstream{hex.substr(i, 2)} >> std::hex >> val; bytes.push_back(static_cast(val)); } return bytes; } /* static */ std::vector WALArchivalManager::encryptAesGcm( const std::vector& data, const std::vector& key) { // Generate a cryptographically secure random 12-byte IV via OpenSSL RAND_bytes std::vector iv(12); if (RAND_bytes(iv.data(), static_cast(iv.size())) != 1) { THEMIS_ERROR("WALArchival: RAND_bytes failed; cannot generate encryption IV"); return {}; // return empty to signal failure; caller must not store this } std::vector ciphertext(data.size()); std::vector tag(16); int len = 0, ct_len = 0; EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (!ctx) { THEMIS_ERROR("WALArchival: EVP_CIPHER_CTX_new failed"); return {}; // return empty to signal failure } EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr); EVP_EncryptInit_ex(ctx, nullptr, nullptr, key.data(), iv.data()); EVP_EncryptUpdate(ctx, ciphertext.data(), &len, data.data(), static_cast(data.size())); ct_len = len; EVP_EncryptFinal_ex(ctx, ciphertext.data() + ct_len, &len); ct_len += len; EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag.data()); EVP_CIPHER_CTX_free(ctx); // Output layout: IV(12) || Tag(16) || Ciphertext std::vector out; out.reserve(static_cast(12 + 16 + ct_len)); out.insert(out.end(), iv.begin(), iv.end()); out.insert(out.end(), tag.begin(), tag.end()); out.insert(out.end(), ciphertext.begin(), ciphertext.begin() + ct_len); return out; } /* static */ std::optional> WALArchivalManager::decryptAesGcm( const std::vector& data, const std::vector& key) { // Minimum: IV(12) + Tag(16) = 28 bytes if (data.size() < 28) { THEMIS_WARN("WALArchival: decryptAesGcm: input too small (size={})", data.size()); return std::nullopt; } const uint8_t* iv = data.data(); const uint8_t* tag_ptr = data.data() + 12; const uint8_t* ct = data.data() + 28; int ct_len = static_cast(data.size() - 28); std::vector plain(static_cast(ct_len)); int len = 0, plain_len = 0; EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (!ctx) { THEMIS_ERROR("WALArchival: EVP_CIPHER_CTX_new failed during decryption"); return std::nullopt; } EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr); EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, nullptr); EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), iv); EVP_DecryptUpdate(ctx, plain.data(), &len, ct, ct_len); plain_len = len; EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, const_cast(tag_ptr)); int ok = EVP_DecryptFinal_ex(ctx, plain.data() + plain_len, &len); EVP_CIPHER_CTX_free(ctx); if (ok <= 0) { THEMIS_ERROR("WALArchival: AES-GCM authentication tag mismatch during decryption"); return std::nullopt; } plain_len += len; plain.resize(static_cast(plain_len)); return plain; } /* static */ std::vector WALArchivalManager::compressData( const std::vector& data) { size_t bound = ZSTD_compressBound(data.size()); std::vector out(bound); size_t compressed = ZSTD_compress( out.data(), bound, data.data(), data.size(), /*level=*/3); if (ZSTD_isError(compressed)) { return data; // fall back to uncompressed on error } out.resize(compressed); return out; } void WALArchivalManager::saveIndex() const { // Index format (one line per segment, fields separated by spaces): // segment_id start_sequence end_sequence size_bytes compressed ts archive_path storage_tier encrypted std::string index_path = config_.archive_directory + "/index.txt"; std::ofstream f(index_path); if (!f) return; for (const auto& seg : index_) { auto ts = std::chrono::duration_cast( seg.archived_at.time_since_epoch()).count(); f << seg.segment_id << " " << seg.start_sequence << " " << seg.end_sequence << " " << seg.size_bytes << " " << (seg.compressed ? 1 : 0) << " " << ts << " " << seg.archive_path << " " << seg.storage_tier << " " << (seg.encrypted ? 1 : 0) << "\n"; } } void WALArchivalManager::loadIndex() { std::string index_path = config_.archive_directory + "/index.txt"; std::ifstream f(index_path); if (!f) return; std::string line; while (std::getline(f, line)) { if (line.empty()) continue; std::istringstream iss(line); ArchivedSegment seg; int64_t ts = 0; int compressed = 0; if (!(iss >> seg.segment_id >> seg.start_sequence >> seg.end_sequence >> seg.size_bytes >> compressed >> ts >> seg.archive_path)) { THEMIS_WARN("WALArchival: malformed index line (skipping): '{}'", line); continue; } seg.compressed = (compressed != 0); seg.archived_at = std::chrono::system_clock::time_point( std::chrono::seconds(ts)); // New fields added in v1.6.0 (WAL Archival to Object Storage): // storage_tier and encrypted. Default to "standard" / false for // indexes written by earlier versions that omit these fields. seg.storage_tier = "standard"; seg.encrypted = false; std::string tier_field; int encrypted_field = 0; if (iss >> tier_field) seg.storage_tier = tier_field; if (iss >> encrypted_field) seg.encrypted = (encrypted_field != 0); index_.push_back(seg); } } uint32_t WALArchivalManager::archiveSegments( const std::vector& segment_paths) { uint32_t archived = 0; std::lock_guard lock(archive_mutex_); for (const auto& seg_path : segment_paths) { std::string full_path = config_.wal_directory + "/" + seg_path; std::ifstream src(full_path, std::ios::binary); if (!src) { THEMIS_WARN("WALArchival: cannot open segment {}", full_path); continue; } std::vector raw( (std::istreambuf_iterator(src)), std::istreambuf_iterator()); // Derive a segment_id from the numeric portion between 'seg_' and '.' // in the filename (e.g. "seg_000042.wal" β†’ 42). Fall back to an // atomic counter to avoid collisions when the name doesn't match the // expected pattern. static std::atomic fallback_id{1}; uint64_t segment_id = 0; { // Look for digits immediately after the last '_' before the last '.' auto dot_pos = seg_path.rfind('.'); auto under_pos = seg_path.rfind('_', dot_pos); if (under_pos != std::string::npos && dot_pos != std::string::npos && under_pos < dot_pos) { std::string num_str = seg_path.substr(under_pos + 1, dot_pos - under_pos - 1); bool all_digits = !num_str.empty() && std::all_of(num_str.begin(), num_str.end(), ::isdigit); if (all_digits) { try { segment_id = std::stoull(num_str); } catch (...) { segment_id = 0; } } } } if (segment_id == 0) { segment_id = fallback_id.fetch_add(1); } std::vector payload = config_.compress_before_archive ? compressData(raw) : raw; // Encrypt payload if encryption at rest is configured bool segment_encrypted = false; if (config_.encrypt_at_rest) { // Reject archival rather than silently store unencrypted if (config_.encryption_key_hex.empty()) { THEMIS_ERROR("WALArchival: encrypt_at_rest enabled but encryption_key_hex " "is empty; refusing to archive {} without encryption", seg_path); continue; } std::vector key = hexToBytes(config_.encryption_key_hex); if (key.size() != 32) { THEMIS_ERROR("WALArchival: encrypt_at_rest enabled but encryption_key_hex " "is not a valid 64-hex-char (32-byte) AES-256 key; " "refusing to archive {} without encryption", seg_path); continue; // reject: do not store segment unencrypted } auto encrypted_payload = encryptAesGcm(payload, key); if (encrypted_payload.empty()) { THEMIS_ERROR("WALArchival: AES-GCM encryption failed for {}; " "segment not archived", seg_path); continue; // reject: do not store segment unencrypted } payload = std::move(encrypted_payload); segment_encrypted = true; } std::string dest = archivePath(segment_id); if (backend_) { if (!backend_->putObject(dest, payload)) { THEMIS_ERROR("WALArchival: backend putObject failed for key {}", dest); continue; } } else { std::ofstream dst(dest, std::ios::binary); if (!dst) { THEMIS_ERROR("WALArchival: cannot write archive {}", dest); continue; } dst.write(reinterpret_cast(payload.data()), static_cast(payload.size())); dst.close(); } ArchivedSegment meta; meta.segment_id = segment_id; meta.start_sequence = 0; // not extracted from binary WAL format here meta.end_sequence = 0; meta.size_bytes = payload.size(); meta.compressed = config_.compress_before_archive; meta.encrypted = segment_encrypted; meta.archived_at = std::chrono::system_clock::now(); meta.archive_path = dest; meta.storage_tier = "standard"; index_.push_back(meta); ++archived; THEMIS_INFO("WALArchival: archived segment {} -> {} ({} bytes, compressed={}, encrypted={})", segment_id, dest, payload.size(), meta.compressed, meta.encrypted); } if (archived > 0) saveIndex(); return archived; } std::optional> WALArchivalManager::retrieveSegment( uint64_t segment_id) const { std::lock_guard lock(archive_mutex_); auto it = std::find_if(index_.begin(), index_.end(), [segment_id](const ArchivedSegment& s) { return s.segment_id == segment_id; }); if (it == index_.end()) { THEMIS_WARN("WALArchival: segment {} not found in index", segment_id); return std::nullopt; } std::vector raw; if (backend_) { auto fetched = backend_->getObject(it->archive_path); if (!fetched) { // Fallback: try local filesystem (supports archives migrated to cloud // that still exist locally, or indexes written before backend was set) std::ifstream f(it->archive_path, std::ios::binary); if (!f) { THEMIS_ERROR("WALArchival: backend getObject and local fallback both " "failed for segment {} (key={})", segment_id, it->archive_path); return std::nullopt; } raw.assign(std::istreambuf_iterator(f), {}); } else { raw = std::move(*fetched); } } else { std::ifstream f(it->archive_path, std::ios::binary); if (!f) { THEMIS_ERROR("WALArchival: archive file {} missing", it->archive_path); return std::nullopt; } raw.assign(std::istreambuf_iterator(f), {}); } // Decrypt if the segment was stored encrypted if (it->encrypted) { if (config_.encryption_key_hex.empty()) { THEMIS_ERROR("WALArchival: segment {} is encrypted but no key configured", segment_id); return std::nullopt; } std::vector key = hexToBytes(config_.encryption_key_hex); if (key.size() != 32) { THEMIS_ERROR("WALArchival: invalid encryption key length for segment {}", segment_id); return std::nullopt; } auto decrypted = decryptAesGcm(raw, key); if (!decrypted) { THEMIS_ERROR("WALArchival: decryption failed for segment {}", segment_id); return std::nullopt; } raw = std::move(*decrypted); } if (!it->compressed) return raw; // Decompress with ZSTD uint64_t decompressed_size = ZSTD_getFrameContentSize(raw.data(), raw.size()); if (decompressed_size == ZSTD_CONTENTSIZE_ERROR || decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) { THEMIS_ERROR("WALArchival: cannot determine decompressed size for segment {}", segment_id); return std::nullopt; } std::vector out(decompressed_size); size_t result = ZSTD_decompress( out.data(), out.size(), raw.data(), raw.size()); if (ZSTD_isError(result)) { THEMIS_ERROR("WALArchival: decompression failed for segment {}", segment_id); return std::nullopt; } out.resize(result); return out; } std::vector WALArchivalManager::listArchived() const { std::lock_guard lock(archive_mutex_); auto copy = index_; std::sort(copy.begin(), copy.end(), [](const ArchivedSegment& a, const ArchivedSegment& b) { return a.segment_id < b.segment_id; }); return copy; } uint32_t WALArchivalManager::purgeExpired() { // delete_after_days == 0 means purge everything immediately (no retention) auto cutoff = (config_.delete_after_days == 0) ? std::chrono::system_clock::time_point::max() // purge all : std::chrono::system_clock::now() - std::chrono::hours(24 * config_.delete_after_days); std::lock_guard lock(archive_mutex_); uint32_t purged = 0; auto it = index_.begin(); while (it != index_.end()) { if (it->archived_at < cutoff) { if (backend_) { static_cast(backend_->deleteObject(it->archive_path)); } else { std::error_code ec; std::filesystem::remove(it->archive_path, ec); } THEMIS_INFO("WALArchival: purged expired segment {} ({})", it->segment_id, it->archive_path); it = index_.erase(it); ++purged; } else { ++it; } } if (purged > 0) saveIndex(); return purged; } uint32_t WALArchivalManager::transitionStorageTiers() { if (config_.transition_to_cold_after_days == 0) return 0; // lifecycle disabled auto now = std::chrono::system_clock::now(); // Segments older than transition_to_cold_after_days -> "cold" // Segments older than transition_to_cold_after_days * 3 -> "glacier" auto cold_threshold = now - std::chrono::hours( 24ULL * config_.transition_to_cold_after_days); auto glacier_threshold = now - std::chrono::hours( 24ULL * config_.transition_to_cold_after_days * 3); std::lock_guard lock(archive_mutex_); uint32_t transitioned = 0; for (auto& seg : index_) { std::string new_tier = seg.storage_tier; if (seg.archived_at <= glacier_threshold) { new_tier = "glacier"; } else if (seg.archived_at <= cold_threshold) { new_tier = "cold"; } if (new_tier != seg.storage_tier) { THEMIS_INFO("WALArchival: segment {} storage tier {} -> {}", seg.segment_id, seg.storage_tier, new_tier); seg.storage_tier = new_tier; // Notify the cloud backend so it can apply the actual tier transition // (e.g. move to S3 Glacier, Azure Archive). No-op for local backend. if (backend_) { static_cast(backend_->setStorageTier(seg.archive_path, new_tier)); } ++transitioned; } } if (transitioned > 0) saveIndex(); return transitioned; } uint32_t WALArchivalManager::runArchivalCycle() { // Collect segment files from the WAL directory older than the retention limit std::vector candidates; std::error_code ec; if (!std::filesystem::exists(config_.wal_directory, ec)) return 0; for (const auto& entry : std::filesystem::directory_iterator(config_.wal_directory, ec)) { if (ec) break; if (entry.is_regular_file()) { candidates.push_back(entry.path().filename().string()); } } // Archive everything beyond the local_retention_segments threshold std::sort(candidates.begin(), candidates.end()); if (candidates.size() <= config_.local_retention_segments) return 0; candidates.resize(candidates.size() - config_.local_retention_segments); uint32_t archived = archiveSegments(candidates); purgeExpired(); return archived; } // ============================================================================ // MultiRegionActiveActiveManager Implementation (Phase 4 – v1.8.0) // ============================================================================ MultiRegionActiveActiveManager::MultiRegionActiveActiveManager( const MultiRegionActiveActiveConfig& config) : config_(config) { // Initialise staleness entry for the local region (always fresh at start) RegionStalenessInfo local; local.region_id = config_.local_region_id; local.staleness_ms = 0; local.last_applied_sequence = 0; local.last_update = std::chrono::system_clock::now(); local.is_healthy = true; region_staleness_[config_.local_region_id] = local; // Initialise staleness entries for peer regions (unknown at start) for (const auto& peer : config_.peer_region_ids) { RegionStalenessInfo info; info.region_id = peer; info.staleness_ms = std::numeric_limits::max(); info.last_applied_sequence = 0; info.last_update = std::chrono::system_clock::now(); info.is_healthy = false; region_staleness_[peer] = info; } } std::string MultiRegionActiveActiveManager::generateWriteId(uint64_t sequence) const { // Combine region id, the caller-supplied sequence, and a nanosecond timestamp for uniqueness. // Using the already-computed sequence (not a fresh load) avoids a TOCTOU race where // another concurrent write could have incremented local_sequence_ between the caller's // atomic increment and this read. auto now_ns = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); std::ostringstream oss; oss << config_.local_region_id << "-" << sequence << "-" << now_ns; return oss.str(); } std::string MultiRegionActiveActiveManager::generateSessionToken(uint64_t sequence) const { // Format: "seq=;region=;exp=" auto expiry_ms = std::chrono::duration_cast( (std::chrono::system_clock::now() + std::chrono::milliseconds(config_.session_token_ttl_ms)) .time_since_epoch()).count(); std::ostringstream oss; oss << "seq=" << sequence << ";region=" << config_.local_region_id << ";exp=" << expiry_ms; return oss.str(); } uint64_t MultiRegionActiveActiveManager::parseSessionToken( const std::string& token) const { // Expected format: "seq=;region=;exp=" auto seq_pos = token.find("seq="); if (seq_pos == std::string::npos) return 0; auto semi = token.find(';', seq_pos); std::string seq_str = token.substr( seq_pos + 4, (semi == std::string::npos) ? std::string::npos : semi - seq_pos - 4); try { return std::stoull(seq_str); } catch (...) { return 0; } } MultiRegionActiveActiveManager::WriteResult MultiRegionActiveActiveManager::write( [[maybe_unused]] const std::string& collection, [[maybe_unused]] const std::string& document_id, [[maybe_unused]] const std::string& operation, [[maybe_unused]] const std::string& data, ConsistencyLevel consistency, const std::string& /*session_token*/) { uint64_t seq = ++local_sequence_; ++writes_total_; // Update local region staleness to 0 (we just wrote here) { std::unique_lock lock(staleness_mutex_); auto& local = region_staleness_[config_.local_region_id]; local.staleness_ms = 0; local.last_applied_sequence = seq; local.last_update = std::chrono::system_clock::now(); local.is_healthy = true; } WriteResult result; result.success = true; result.write_id = generateWriteId(seq); result.region_id = config_.local_region_id; result.sequence_number = seq; result.session_token = generateSessionToken(seq); THEMIS_INFO("MultiRegionActiveActive: write seq={} region={} consistency={}", seq, config_.local_region_id, static_cast(consistency)); return result; } MultiRegionActiveActiveManager::ReadResult MultiRegionActiveActiveManager::read( [[maybe_unused]] const std::string& collection, [[maybe_unused]] const std::string& document_id, ConsistencyLevel consistency, const std::string& session_token) { ++reads_total_; int64_t local_staleness_ms = 0; uint64_t local_seq = 0; { std::shared_lock lock(staleness_mutex_); auto it = region_staleness_.find(config_.local_region_id); if (it != region_staleness_.end()) { local_staleness_ms = it->second.staleness_ms; local_seq = it->second.last_applied_sequence; } } ReadResult result; result.region_id = config_.local_region_id; result.staleness_ms = local_staleness_ms; result.served_at = consistency; result.sequence_number = local_seq; switch (consistency) { case ConsistencyLevel::STRONG: ++strong_reads_; if (local_staleness_ms > 0) { THEMIS_WARN("MultiRegionActiveActive: STRONG read rejected – " "local staleness={}ms > 0", local_staleness_ms); ++staleness_rejections_; result.success = false; return result; } break; case ConsistencyLevel::BOUNDED_STALENESS: ++bounded_staleness_reads_; if (local_staleness_ms > static_cast(config_.max_staleness_ms)) { THEMIS_WARN("MultiRegionActiveActive: BOUNDED_STALENESS read rejected – " "local staleness={}ms > bound={}ms", local_staleness_ms, config_.max_staleness_ms); ++staleness_rejections_; result.success = false; return result; } break; case ConsistencyLevel::SESSION: { ++session_reads_; if (!session_token.empty()) { uint64_t required_seq = parseSessionToken(session_token); if (required_seq > 0 && local_seq < required_seq) { THEMIS_WARN("MultiRegionActiveActive: SESSION read rejected – " "local_seq={} < required_seq={}", local_seq, required_seq); ++staleness_rejections_; result.success = false; return result; } } break; } case ConsistencyLevel::EVENTUAL: ++eventual_reads_; // Always succeeds regardless of staleness break; } result.success = true; THEMIS_INFO("MultiRegionActiveActive: read served region={} staleness={}ms", config_.local_region_id, local_staleness_ms); return result; } std::string MultiRegionActiveActiveManager::createSessionToken() const { uint64_t seq = local_sequence_.load(); return generateSessionToken(seq); } bool MultiRegionActiveActiveManager::validateSessionToken( const std::string& token, uint64_t required_sequence) const { if (token.empty()) return false; // Check expiry embedded in token auto exp_pos = token.find("exp="); if (exp_pos != std::string::npos) { try { int64_t expiry_ms = std::stoll(token.substr(exp_pos + 4)); auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); if (now_ms > expiry_ms) { return false; // Token expired } } catch (...) { return false; } } uint64_t token_seq = parseSessionToken(token); return token_seq >= required_sequence; } std::chrono::milliseconds MultiRegionActiveActiveManager::getStaleness( const std::string& region_id) const { std::shared_lock lock(staleness_mutex_); auto it = region_staleness_.find(region_id); if (it == region_staleness_.end()) { return std::chrono::milliseconds(std::numeric_limits::max()); } return std::chrono::milliseconds(it->second.staleness_ms); } bool MultiRegionActiveActiveManager::isWithinStalenessBound( const std::string& region_id) const { return getStaleness(region_id) <= std::chrono::milliseconds(config_.max_staleness_ms); } std::vector MultiRegionActiveActiveManager::getAllRegionStaleness() const { std::shared_lock lock(staleness_mutex_); std::vector result; result.reserve(region_staleness_.size()); for (const auto& kv : region_staleness_) { result.push_back(kv.second); } return result; } void MultiRegionActiveActiveManager::updateRegionStaleness( const std::string& region_id, int64_t staleness_ms, uint64_t last_applied_sequence) { std::unique_lock lock(staleness_mutex_); auto& info = region_staleness_[region_id]; info.region_id = region_id; info.staleness_ms = staleness_ms; info.last_applied_sequence = last_applied_sequence; info.last_update = std::chrono::system_clock::now(); info.is_healthy = (staleness_ms >= 0 && staleness_ms <= static_cast(config_.max_staleness_ms) * 2); } std::string MultiRegionActiveActiveManager::exportPrometheusMetrics() const { std::ostringstream oss; const auto& r = config_.local_region_id; oss << "# HELP themisdb_mraaa_writes_total Total writes accepted by local region\n" << "# TYPE themisdb_mraaa_writes_total counter\n" << "themisdb_mraaa_writes_total{region=\"" << r << "\"} " << writes_total_.load() << "\n\n"; oss << "# HELP themisdb_mraaa_reads_total Total read attempts\n" << "# TYPE themisdb_mraaa_reads_total counter\n" << "themisdb_mraaa_reads_total{region=\"" << r << "\"} " << reads_total_.load() << "\n\n"; oss << "# HELP themisdb_mraaa_staleness_rejections_total Reads rejected due to excessive staleness\n" << "# TYPE themisdb_mraaa_staleness_rejections_total counter\n" << "themisdb_mraaa_staleness_rejections_total{region=\"" << r << "\"} " << staleness_rejections_.load() << "\n\n"; oss << "# HELP themisdb_mraaa_strong_reads_total Reads served at STRONG consistency\n" << "# TYPE themisdb_mraaa_strong_reads_total counter\n" << "themisdb_mraaa_strong_reads_total{region=\"" << r << "\"} " << strong_reads_.load() << "\n\n"; oss << "# HELP themisdb_mraaa_bounded_staleness_reads_total Reads served at BOUNDED_STALENESS\n" << "# TYPE themisdb_mraaa_bounded_staleness_reads_total counter\n" << "themisdb_mraaa_bounded_staleness_reads_total{region=\"" << r << "\"} " << bounded_staleness_reads_.load() << "\n\n"; oss << "# HELP themisdb_mraaa_session_reads_total Reads served at SESSION consistency\n" << "# TYPE themisdb_mraaa_session_reads_total counter\n" << "themisdb_mraaa_session_reads_total{region=\"" << r << "\"} " << session_reads_.load() << "\n\n"; oss << "# HELP themisdb_mraaa_eventual_reads_total Reads served at EVENTUAL consistency\n" << "# TYPE themisdb_mraaa_eventual_reads_total counter\n" << "themisdb_mraaa_eventual_reads_total{region=\"" << r << "\"} " << eventual_reads_.load() << "\n\n"; // Per-region staleness gauges { std::shared_lock lock(staleness_mutex_); oss << "# HELP themisdb_mraaa_region_staleness_ms Current replication staleness per region\n" << "# TYPE themisdb_mraaa_region_staleness_ms gauge\n"; for (const auto& kv : region_staleness_) { oss << "themisdb_mraaa_region_staleness_ms{region=\"" << kv.first << "\"} " << kv.second.staleness_ms << "\n"; } oss << "\n"; } return oss.str(); } } // namespace replication } // namespace themisdb // ============================================================================ // BidirectionalReplicationManager Implementation (v1.7.0) // ============================================================================ namespace themisdb { namespace replication { namespace { // Generate a short unique ID using timestamp + counter. std::string generateBidiId(const std::string& prefix) { static std::atomic counter{0}; auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); return prefix + "-" + std::to_string(now_ms) + "-" + std::to_string(counter.fetch_add(1)); } } // anonymous namespace // ── Constructor / Destructor ───────────────────────────────────────────────── BidirectionalReplicationManager::BidirectionalReplicationManager( const BidiConfig& config) : config_(config) {} BidirectionalReplicationManager::~BidirectionalReplicationManager() { stop(); } // ── Lifecycle ──────────────────────────────────────────────────────────────── bool BidirectionalReplicationManager::start() { if (config_.local_node_id.empty() || config_.remote_node_id.empty()) { return false; } if (config_.local_node_id == config_.remote_node_id) { return false; } bool expected = false; return running_.compare_exchange_strong(expected, true); } void BidirectionalReplicationManager::stop() { running_.store(false); } // ── Write path ─────────────────────────────────────────────────────────────── uint64_t BidirectionalReplicationManager::submitWrite( const std::string& document_id, const std::string& collection, const std::string& operation, const std::string& data, bool is_ddl) { if (!running_.load()) { return 0; } uint64_t seq = local_sequence_.fetch_add(1) + 1; BidiWriteEntry entry; entry.document_id = document_id; entry.collection = collection; entry.operation = operation; entry.data = data; entry.is_ddl = is_ddl; entry.origin_seq = seq; entry.timestamp_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); if (config_.track_origin) { entry.origin_node = config_.local_node_id; } const std::string key = makeDocKey(collection, document_id); // Update origin map if (config_.track_origin) { std::lock_guard lk(origin_mutex_); origin_map_[key] = { config_.local_node_id, seq, std::chrono::system_clock::now() }; } // Enqueue as a pending local write { std::lock_guard lk(pending_mutex_); pending_writes_[key] = entry; } return seq; } bool BidirectionalReplicationManager::applyRemoteWrite(const BidiWriteEntry& entry) { if (entry.document_id.empty() || entry.collection.empty()) { return false; } if (config_.track_origin && (entry.origin_node.empty() || entry.origin_seq == 0)) { return false; } // Respect the bidirectional_sync flag: if disabled, reject all inbound writes. if (!config_.bidirectional_sync) { return false; } // Origin-tracking loop prevention: if the change originated locally, do not // re-apply it (it was already in pending_writes_). if (config_.track_origin && !config_.replicate_foreign_changes) { if (entry.origin_node == config_.local_node_id) { // This is our own write bouncing back; discard it. return false; } } const std::string key = makeDocKey(entry.collection, entry.document_id); if (config_.track_origin) { std::lock_guard lk(origin_mutex_); const auto it = origin_map_.find(key); if (it != origin_map_.end() && it->second.origin_node == entry.origin_node && entry.origin_seq <= it->second.origin_sequence) { return false; } } // Conflict detection: check whether we have a pending local write for the // same document. { std::lock_guard lk(pending_mutex_); auto it = pending_writes_.find(key); if (it != pending_writes_.end()) { if (detectConflict(entry, it->second)) { handleConflict(it->second, entry, entry.is_ddl); // Remove the pending write; the resolved winner is tracked in // the conflict history. pending_writes_.erase(it); } else { // The remote write is causally after our pending write (or // vice-versa); accept it and retire the pending write. pending_writes_.erase(it); } } } // Update the remote sequence watermark. if (entry.origin_seq > 0) { uint64_t cur = remote_sequence_.load(); while (entry.origin_seq > cur) { if (remote_sequence_.compare_exchange_weak(cur, entry.origin_seq)) { break; } } } // Update origin map to reflect the latest known origin for this document. if (config_.track_origin) { std::lock_guard lk(origin_mutex_); auto& oi = origin_map_[key]; if (entry.origin_seq >= oi.origin_sequence) { oi = { entry.origin_node, entry.origin_seq, std::chrono::system_clock::now() }; } } return true; } // ── Status & metrics ───────────────────────────────────────────────────────── BidirectionalReplicationManager::SyncStatus BidirectionalReplicationManager::getSyncStatus() const { SyncStatus s; s.local_sequence = local_sequence_.load(); s.remote_sequence = remote_sequence_.load(); s.lag_ms = replication_lag_ms_.load(); s.conflicts_detected = conflicts_detected_.load(); s.conflicts_resolved = conflicts_resolved_.load(); s.is_running = running_.load(); // Compute conflicts_last_hour: count entries within the last 60 minutes. { std::lock_guard lk(conflicts_mutex_); const auto cutoff = std::chrono::system_clock::now() - std::chrono::hours(1); s.conflicts_last_hour = static_cast( std::count_if(conflict_timestamps_.begin(), conflict_timestamps_.end(), [&cutoff](const auto& ts) { return ts >= cutoff; })); } // "Synchronized" when running, no outstanding pending writes, and lag is // within one sync interval. { std::lock_guard lk(pending_mutex_); const bool no_pending = pending_writes_.empty(); s.is_synchronized = s.is_running && no_pending && s.lag_ms < static_cast(config_.sync_interval_ms); } return s; } std::vector BidirectionalReplicationManager::getConflictHistory() const { std::lock_guard lk(conflicts_mutex_); return conflict_history_; } std::vector BidirectionalReplicationManager::getPendingConflicts() const { std::lock_guard lk(conflicts_mutex_); std::vector result; for (const auto& rec : conflict_history_) { if (rec.strategy_used == ConflictResolution::CUSTOM && rec.resolved_write.data.empty()) { result.push_back(rec); } } return result; } // ── Conflict resolution ─────────────────────────────────────────────────────── bool BidirectionalReplicationManager::resolveConflict( const std::string& document_id, const std::string& winner_node) { if (winner_node != config_.local_node_id && winner_node != config_.remote_node_id) { return false; } std::lock_guard lk(conflicts_mutex_); // Walk in reverse to find the most-recent conflict for this document. for (auto it = conflict_history_.rbegin(); it != conflict_history_.rend(); ++it) { if (it->document_id == document_id && it->resolved_write.data.empty()) { it->resolved_write = (winner_node == config_.local_node_id) ? it->local_write : it->remote_write; conflicts_resolved_.fetch_add(1); return true; } } return false; } // ── Configuration helpers ───────────────────────────────────────────────────── void BidirectionalReplicationManager::setCollectionStrategy( const std::string& collection, ConflictResolution strategy) { config_.collection_strategies[collection] = strategy; } ConflictResolution BidirectionalReplicationManager::getEffectiveStrategy( const std::string& collection) const { auto it = config_.collection_strategies.find(collection); return (it != config_.collection_strategies.end()) ? it->second : config_.default_strategy; } // ── Simulation helpers ──────────────────────────────────────────────────────── void BidirectionalReplicationManager::updateRemoteSequence( uint64_t remote_seq, int64_t lag_ms) { uint64_t cur = remote_sequence_.load(); while (remote_seq > cur) { if (remote_sequence_.compare_exchange_weak(cur, remote_seq)) { break; } } replication_lag_ms_.store(lag_ms); } bool BidirectionalReplicationManager::applyRemoteDDL( const std::string& ddl_statement, const std::string& schema_version, uint64_t origin_seq) { // Respect the replicate_ddl flag: if disabled, ignore incoming DDL events. if (!config_.replicate_ddl) { return false; } BidiWriteEntry entry; entry.document_id = "__ddl__" + schema_version; entry.collection = "__schema__"; entry.operation = "DDL"; entry.data = ddl_statement; entry.origin_node = config_.remote_node_id; entry.origin_seq = origin_seq; entry.timestamp_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); entry.is_ddl = true; return applyRemoteWrite(entry); } // ── Private helpers ─────────────────────────────────────────────────────────── std::string BidirectionalReplicationManager::makeDocKey( const std::string& collection, const std::string& document_id) const { return collection + '\0' + document_id; } BidirectionalReplicationManager::OriginInfo BidirectionalReplicationManager::getOrigin(const std::string& document_id) const { // document_id here is expected to be the raw doc id (not the compound key). // Callers that need to look up by collection+doc should use the origin_map_ // directly with makeDocKey. std::lock_guard lk(origin_mutex_); for (const auto& kv : origin_map_) { // The key is "collection\0doc_id"; strip the collection prefix. const auto sep = kv.first.find('\0'); if (sep != std::string::npos && kv.first.substr(sep + 1) == document_id) { return kv.second; } } return { config_.local_node_id, 0, std::chrono::system_clock::now() }; } bool BidirectionalReplicationManager::isLocalOrigin(const OriginInfo& origin) const { return origin.origin_node == config_.local_node_id; } BidirectionalReplicationManager::BidiWriteEntry BidirectionalReplicationManager::resolveWrite( const BidiWriteEntry& local, const BidiWriteEntry& remote, ConflictResolution strategy) const { switch (strategy) { case ConflictResolution::FIRST_WRITE_WINS: // Whichever has the smaller timestamp wins; ties go to the local write. return (remote.timestamp_ms < local.timestamp_ms) ? remote : local; case ConflictResolution::LAST_WRITE_WINS: // Whichever has the larger timestamp wins; ties go to the remote write // (consistent with the existing LWWConflictResolver convention). return (local.timestamp_ms > remote.timestamp_ms) ? local : remote; case ConflictResolution::VECTOR_CLOCK: // Without a real vector-clock on each entry, fall back to LWW. return (local.timestamp_ms > remote.timestamp_ms) ? local : remote; case ConflictResolution::CUSTOM: // Return an empty placeholder; the application must call resolveConflict(). THEMIS_WARN("resolveWrite: ConflictResolution::CUSTOM used β€” application must call resolveConflict() to supply a resolution"); return {}; default: return (local.timestamp_ms > remote.timestamp_ms) ? local : remote; } } bool BidirectionalReplicationManager::detectConflict( const BidiWriteEntry& incoming, const BidiWriteEntry& existing) const { // Two writes to the same (collection, document_id) from different origins // are always considered concurrent β€” a true conflict. if (incoming.origin_node == existing.origin_node) { // Same origin: the later sequence supersedes the earlier one. return false; } return true; } void BidirectionalReplicationManager::handleConflict( const BidiWriteEntry& local_write, const BidiWriteEntry& remote_write, bool is_ddl) { conflicts_detected_.fetch_add(1); const ConflictResolution strategy = getEffectiveStrategy(local_write.collection); BidiWriteEntry winner = resolveWrite(local_write, remote_write, strategy); BidiConflictRecord rec; rec.conflict_id = generateBidiId("bidi-conflict"); rec.document_id = local_write.document_id; rec.collection = local_write.collection; rec.local_write = local_write; rec.remote_write = remote_write; rec.resolved_write = winner; rec.strategy_used = strategy; rec.detected_at = std::chrono::system_clock::now(); rec.is_ddl_conflict = is_ddl; { std::lock_guard lk(conflicts_mutex_); conflict_history_.push_back(std::move(rec)); // Record timestamp for conflicts_last_hour sliding window. conflict_timestamps_.push_back(std::chrono::system_clock::now()); // Prune entries older than 1 hour to keep memory bounded. const auto cutoff = std::chrono::system_clock::now() - std::chrono::hours(1); while (!conflict_timestamps_.empty() && conflict_timestamps_.front() < cutoff) { conflict_timestamps_.pop_front(); } } // Only count as resolved if we actually picked a winner (not CUSTOM). if (strategy != ConflictResolution::CUSTOM) { conflicts_resolved_.fetch_add(1); } } // ============================================================================ // GeoReplicationManager Implementation (v1.7.0) // ============================================================================ GeoReplicationManager::GeoReplicationManager(const GeoConfig& config) : config_(config) { // Initialise the local region as fully fresh (zero lag) at startup. RegionStalenessInfo local; local.region_id = config_.local_region; local.staleness_ms = 0; local.last_applied_sequence = 0; local.last_update = std::chrono::system_clock::now(); local.is_healthy = true; region_staleness_[config_.local_region] = local; // Initialise all other regions as unknown (very high lag). for (const auto& r : config_.regions) { if (r == config_.local_region) continue; RegionStalenessInfo info; info.region_id = r; info.staleness_ms = std::numeric_limits::max(); info.last_applied_sequence = 0; info.last_update = std::chrono::system_clock::now(); info.is_healthy = false; region_staleness_[r] = info; } } // ── Session token helpers ───────────────────────────────────────────────────── std::string GeoReplicationManager::generateSessionToken(uint64_t sequence) const { auto expiry_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() + std::chrono::milliseconds(config_.session_token_ttl_ms)).count(); return "seq=" + std::to_string(sequence) + ";region=" + config_.local_region + ";exp=" + std::to_string(expiry_ms); } uint64_t GeoReplicationManager::parseSessionToken(const std::string& token) const { if (token.empty()) return 0; // Check expiry auto exp_pos = token.find("exp="); if (exp_pos != std::string::npos) { try { int64_t expiry_ms = std::stoll(token.substr(exp_pos + 4)); auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); if (now_ms > expiry_ms) return 0; // expired } catch (...) { return 0; } } auto seq_pos = token.find("seq="); if (seq_pos == std::string::npos) return 0; try { return std::stoull(token.substr(seq_pos + 4)); } catch (...) { return 0; } } std::string GeoReplicationManager::getSessionToken() const { return generateSessionToken(local_sequence_.load()); } // ── Staleness management ────────────────────────────────────────────────────── void GeoReplicationManager::updateRegionStaleness(const std::string& region, int64_t staleness_ms, uint64_t last_applied_sequence) { std::unique_lock lock(staleness_mutex_); auto& info = region_staleness_[region]; info.region_id = region; info.staleness_ms = staleness_ms; info.last_applied_sequence = last_applied_sequence; info.last_update = std::chrono::system_clock::now(); info.is_healthy = (staleness_ms >= 0); } std::chrono::milliseconds GeoReplicationManager::getStaleness( const std::string& region) const { std::shared_lock lock(staleness_mutex_); auto it = region_staleness_.find(region); if (it == region_staleness_.end()) { return std::chrono::milliseconds(std::numeric_limits::max()); } return std::chrono::milliseconds(it->second.staleness_ms); } // ── Automatic routing ───────────────────────────────────────────────────────── std::string GeoReplicationManager::selectReadRegion( ConsistencyLevel consistency, const std::string& session_token) const { std::shared_lock lock(staleness_mutex_); switch (consistency) { case ConsistencyLevel::STRONG: { // Return the local region only if it has zero lag. auto it = region_staleness_.find(config_.local_region); if (it != region_staleness_.end() && it->second.staleness_ms == 0) { return config_.local_region; } // Fall back: find any region with zero lag. for (const auto& [rid, info] : region_staleness_) { if (info.staleness_ms == 0 && info.is_healthy) return rid; } return ""; // No eligible region } case ConsistencyLevel::BOUNDED_STALENESS: { // Prefer local region if within bound; otherwise pick freshest eligible. const int64_t bound = static_cast(config_.max_staleness_ms); auto local_it = region_staleness_.find(config_.local_region); if (local_it != region_staleness_.end() && local_it->second.staleness_ms <= bound) { return config_.local_region; } // Pick the region with smallest staleness that is within bound. std::string best_region; int64_t best_lag = std::numeric_limits::max(); for (const auto& [rid, info] : region_staleness_) { if (info.is_healthy && info.staleness_ms <= bound && info.staleness_ms < best_lag) { best_lag = info.staleness_ms; best_region = rid; } } return best_region; } case ConsistencyLevel::SESSION: { // Local region must have applied at least the sequence in the token. // parseSessionToken() is mutex-free (no staleness_mutex_ acquired inside), // so it is safe to call while holding staleness_mutex_ as a shared lock. uint64_t required_seq = parseSessionToken(session_token); auto it = region_staleness_.find(config_.local_region); if (it != region_staleness_.end() && it->second.last_applied_sequence >= required_seq) { return config_.local_region; } return ""; } case ConsistencyLevel::EVENTUAL: default: return config_.local_region; } } // ── Write ───────────────────────────────────────────────────────────────────── bool GeoReplicationManager::write( const std::string& key, [[maybe_unused]] const std::string& value, ConsistencyLevel consistency) { // key/value applied by the caller's storage layer // For STRONG writes, require the local region to have zero lag. if (consistency == ConsistencyLevel::STRONG) { auto lag = getStaleness(config_.local_region); if (lag.count() > 0) { THEMIS_WARN("GeoReplicationManager: STRONG write rejected – " "local region '{}' has lag={}ms", config_.local_region, lag.count()); return false; } } // Advance the local sequence and mark the local region as fresh. uint64_t seq = ++local_sequence_; ++writes_total_; { std::unique_lock lock(staleness_mutex_); auto& local = region_staleness_[config_.local_region]; local.staleness_ms = 0; local.last_applied_sequence = seq; local.last_update = std::chrono::system_clock::now(); } THEMIS_INFO("GeoReplicationManager: write key='{}' seq={} consistency={} region={}", key, seq, static_cast(consistency), config_.local_region); return true; } // ── Read ────────────────────────────────────────────────────────────────────── std::optional GeoReplicationManager::read( const std::string& key, ConsistencyLevel consistency, const std::string& session_token) { ++reads_total_; switch (consistency) { case ConsistencyLevel::STRONG: ++strong_reads_; break; case ConsistencyLevel::BOUNDED_STALENESS: ++bounded_staleness_reads_; break; case ConsistencyLevel::SESSION: ++session_reads_; break; case ConsistencyLevel::EVENTUAL: ++eventual_reads_; break; } const std::string region = selectReadRegion(consistency, session_token); if (region.empty()) { ++reads_rejected_; THEMIS_WARN("GeoReplicationManager: read key='{}' rejected – " "no eligible region for consistency={}", key, static_cast(consistency)); return std::nullopt; } THEMIS_INFO("GeoReplicationManager: read key='{}' consistency={} served by region={}", key, static_cast(consistency), region); // Return a non-empty sentinel value – the actual value comes from the // caller's storage layer; this manager handles routing / consistency only. return region; } // ── Metrics ─────────────────────────────────────────────────────────────────── std::string GeoReplicationManager::exportPrometheusMetrics() const { std::ostringstream oss; const std::string lbl = "{region=\"" + config_.local_region + "\"}"; oss << "# HELP themisdb_geo_repl_writes_total Total writes accepted\n" << "# TYPE themisdb_geo_repl_writes_total counter\n" << "themisdb_geo_repl_writes_total" << lbl << " " << writes_total_.load() << "\n"; oss << "# HELP themisdb_geo_repl_reads_total Total reads attempted\n" << "# TYPE themisdb_geo_repl_reads_total counter\n" << "themisdb_geo_repl_reads_total" << lbl << " " << reads_total_.load() << "\n"; oss << "# HELP themisdb_geo_repl_reads_rejected_total Reads rejected due to consistency constraints\n" << "# TYPE themisdb_geo_repl_reads_rejected_total counter\n" << "themisdb_geo_repl_reads_rejected_total" << lbl << " " << reads_rejected_.load() << "\n"; oss << "# HELP themisdb_geo_repl_strong_reads_total STRONG reads\n" << "# TYPE themisdb_geo_repl_strong_reads_total counter\n" << "themisdb_geo_repl_strong_reads_total" << lbl << " " << strong_reads_.load() << "\n"; oss << "# HELP themisdb_geo_repl_bounded_staleness_reads_total BOUNDED_STALENESS reads\n" << "# TYPE themisdb_geo_repl_bounded_staleness_reads_total counter\n" << "themisdb_geo_repl_bounded_staleness_reads_total" << lbl << " " << bounded_staleness_reads_.load() << "\n"; oss << "# HELP themisdb_geo_repl_session_reads_total SESSION reads\n" << "# TYPE themisdb_geo_repl_session_reads_total counter\n" << "themisdb_geo_repl_session_reads_total" << lbl << " " << session_reads_.load() << "\n"; oss << "# HELP themisdb_geo_repl_eventual_reads_total EVENTUAL reads\n" << "# TYPE themisdb_geo_repl_eventual_reads_total counter\n" << "themisdb_geo_repl_eventual_reads_total" << lbl << " " << eventual_reads_.load() << "\n"; // Per-region staleness gauge oss << "# HELP themisdb_geo_repl_region_staleness_ms Replication lag per region (ms)\n" << "# TYPE themisdb_geo_repl_region_staleness_ms gauge\n"; { std::shared_lock lock(staleness_mutex_); for (const auto& [rid, info] : region_staleness_) { int64_t lag = (info.staleness_ms == std::numeric_limits::max()) ? -1 : info.staleness_ms; oss << "themisdb_geo_repl_region_staleness_ms{region=\"" << rid << "\"} " << lag << "\n"; } } return oss.str(); } } // namespace replication } // namespace themisdb