86 lines
3.0 KiB
C++
86 lines
3.0 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
#include <cstdint>
|
||
|
|
#include <deque>
|
||
|
|
#include <functional>
|
||
|
|
#include <map>
|
||
|
|
#include <memory>
|
||
|
|
#include <mutex>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "supervisor/common/clock.hpp"
|
||
|
|
#include "supervisor/common/observer.hpp"
|
||
|
|
#include "supervisor/replication/oplog_entry.hpp"
|
||
|
|
|
||
|
|
namespace molo::replication {
|
||
|
|
|
||
|
|
// Per-model conflict policy, declared once (S7-8, R7-7).
|
||
|
|
enum class ConflictPolicy {
|
||
|
|
Optimistic, // compare version, reject stale with ConflictError
|
||
|
|
LastWriterWins, // higher hybrid timestamp wins, loser kept in conflict log
|
||
|
|
Hook, // typed callback decides, 5s budget, LWW fallback
|
||
|
|
};
|
||
|
|
|
||
|
|
std::string to_string(ConflictPolicy policy);
|
||
|
|
|
||
|
|
// Returns 0 when local wins, 1 when remote wins.
|
||
|
|
using ConflictHook = std::function<int(const OplogEntry& local, const OplogEntry& remote)>;
|
||
|
|
|
||
|
|
struct ConflictOutcome {
|
||
|
|
bool conflict = false; // true when both sides wrote concurrently
|
||
|
|
bool remote_won = false; // true when the remote entry becomes current
|
||
|
|
OplogEntry winner; // the entry that becomes current
|
||
|
|
std::string detail; // human-readable reason, capped at 1 KiB
|
||
|
|
bool conflict_error = false; // true when the caller must raise ConflictError
|
||
|
|
long long winner_version = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct ConflictRecord {
|
||
|
|
std::int64_t at_ms = 0;
|
||
|
|
std::string policy;
|
||
|
|
OplogEntry winner;
|
||
|
|
OplogEntry loser;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Dispatches per-model conflict policy (S7-8). The ONLY place that compares
|
||
|
|
// versions or timestamps: callers never resolve conflicts ad hoc (R7-7).
|
||
|
|
class ConflictResolver {
|
||
|
|
public:
|
||
|
|
ConflictResolver(common::Clock& clock, std::shared_ptr<common::Observer> observer,
|
||
|
|
int max_log_entries = 10000, long log_retention_days = 7);
|
||
|
|
|
||
|
|
// Declares (or replaces) the policy for one model. Hook policy needs a
|
||
|
|
// callback; without one the declaration falls back to LastWriterWins.
|
||
|
|
void declare(const std::string& model, ConflictPolicy policy,
|
||
|
|
ConflictHook hook = ConflictHook{});
|
||
|
|
|
||
|
|
ConflictPolicy policy_for(const std::string& model) const; // default: Optimistic
|
||
|
|
|
||
|
|
// Resolves local (current) vs remote (incoming) for the same model+key.
|
||
|
|
ConflictOutcome resolve(const OplogEntry& local, const OplogEntry& remote);
|
||
|
|
|
||
|
|
std::vector<ConflictRecord> conflict_log(std::size_t max) const;
|
||
|
|
std::size_t conflict_log_size() const;
|
||
|
|
std::size_t purge_expired();
|
||
|
|
|
||
|
|
private:
|
||
|
|
ConflictOutcome resolve_optimistic(const OplogEntry& local, const OplogEntry& remote);
|
||
|
|
ConflictOutcome resolve_lww(const OplogEntry& local, const OplogEntry& remote);
|
||
|
|
ConflictOutcome resolve_hook(const OplogEntry& local, const OplogEntry& remote,
|
||
|
|
const ConflictHook& hook);
|
||
|
|
void log_conflict(const std::string& policy, const OplogEntry& winner,
|
||
|
|
const OplogEntry& loser);
|
||
|
|
|
||
|
|
common::Clock& clock_;
|
||
|
|
std::shared_ptr<common::Observer> observer_;
|
||
|
|
int max_log_entries_;
|
||
|
|
long log_retention_days_;
|
||
|
|
mutable std::mutex mutex_;
|
||
|
|
std::map<std::string, ConflictPolicy> policies_;
|
||
|
|
std::map<std::string, ConflictHook> hooks_;
|
||
|
|
std::deque<ConflictRecord> log_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace molo::replication
|