@@ -25,7 +25,7 @@ class Context : public pstd::noncopyable {
2525 void Reset (const LogOffset& offset);
2626
2727 std::shared_mutex rwlock_;
28- LogOffset applied_index_;
28+ LogOffset applied_index_ = LogOffset() ;
2929 SyncWindow applied_win_;
3030
3131 std::string ToString () {
@@ -52,11 +52,28 @@ class SyncProgress {
5252 pstd::Status Update (const std::string& ip, int port, const LogOffset& start, const LogOffset& end,
5353 LogOffset* committed_index);
5454 int SlaveSize ();
55+ int SlaveBinlogStateSize () {
56+ std::shared_lock l (rwlock_);
57+ return slave_binlog_state_size;
58+ }
59+ void AddSlaveBinlogStateSize () {
60+ std::lock_guard l (rwlock_);
61+ slave_binlog_state_size++;
62+ }
63+ void SubSlaveBinlogStateSize () {
64+ std::lock_guard l (rwlock_);
65+ slave_binlog_state_size--;
66+ }
67+ void AddMatchIndex (const std::string& ip, int port, const LogOffset& offset) {
68+ std::lock_guard l (rwlock_);
69+ match_index_[ip + std::to_string (port)] = offset;
70+ }
5571
5672 private:
5773 std::shared_mutex rwlock_;
5874 std::unordered_map<std::string, std::shared_ptr<SlaveNode>> slaves_;
5975 std::unordered_map<std::string, LogOffset> match_index_;
76+ int slave_binlog_state_size = 0 ;
6077};
6178
6279class MemLog {
@@ -100,6 +117,34 @@ class MemLog {
100117 LogOffset last_offset_;
101118};
102119
120+ class Log {
121+ public:
122+ struct LogItem {
123+ LogItem (const LogOffset& _offset, std::shared_ptr<Cmd> _cmd_ptr, std::string _binlog)
124+ : offset(_offset), cmd_ptr(std::move(_cmd_ptr)), binlog_(std::move(_binlog)) {}
125+ LogOffset offset;
126+ std::shared_ptr<Cmd> cmd_ptr;
127+ std::string binlog_;
128+ };
129+
130+ Log ();
131+ int Size ();
132+ void AppendLog (const LogItem& item);
133+ LogOffset LastOffset ();
134+ LogOffset FirstOffset ();
135+ LogItem At (int index);
136+ int FindOffset (const LogOffset& send_offset);
137+ pstd::Status Truncate (const LogOffset& offset);
138+ pstd::Status TruncateFrom (const LogOffset& offset);
139+
140+ private:
141+ int FindLogIndex (const LogOffset& offset);
142+ std::shared_mutex logs_mutex_;
143+ std::vector<LogItem> logs_;
144+ LogOffset last_index_ = LogOffset();
145+ LogOffset first_index_ = LogOffset();
146+ };
147+
103148class ConsensusCoordinator {
104149 public:
105150 ConsensusCoordinator (const std::string& db_name);
@@ -199,5 +244,51 @@ class ConsensusCoordinator {
199244 SyncProgress sync_pros_;
200245 std::shared_ptr<StableLog> stable_logger_;
201246 std::shared_ptr<MemLog> mem_logger_;
247+
248+ // pacificA
249+ public:
250+ void InitContext () { context_->Init (); }
251+ bool checkFinished (const LogOffset& offset);
252+ pstd::Status AppendEntries (const std::shared_ptr<Cmd>& cmd_ptr, LogOffset& cur_logoffset);
253+ void SetConsistency (bool is_consistency);
254+ bool GetISConsistency ();
255+ pstd::Status SendBinlog (std::shared_ptr<SlaveNode> slave_ptr, std::string db_name);
256+ pstd::Status Truncate (const LogOffset& offset);
257+ pstd::Status AppendSlaveEntries (const std::shared_ptr<Cmd>& cmd_ptr, const BinlogItem& attribute);
258+ pstd::Status CommitAppLog (const LogOffset& master_committed_id);
259+ pstd::Status UpdateCommittedID ();
260+ pstd::Status ApplyBinlog (const std::shared_ptr<Cmd>& cmd_ptr);
261+ pstd::Status ProcessCoordination ();
262+
263+ LogOffset GetCommittedId () {
264+ std::lock_guard l (committed_id_rwlock_);
265+ return committed_id_;
266+ }
267+ LogOffset GetPreparedId () {
268+ std::lock_guard l (prepared_id__rwlock_);
269+ return prepared_id_;
270+ }
271+ void SetPreparedId (const LogOffset& offset) {
272+ std::lock_guard l (prepared_id__rwlock_);
273+ prepared_id_ = offset;
274+ }
275+ void SetCommittedId (const LogOffset& offset) {
276+ std::lock_guard l (committed_id_rwlock_);
277+ committed_id_ = offset;
278+ context_->UpdateAppliedIndex (committed_id_);
279+ }
280+
281+ private:
282+ pstd::Status PersistAppendBinlog (const std::shared_ptr<Cmd>& cmd_ptr, LogOffset& cur_offset);
283+
284+ private:
285+ std::shared_mutex is_consistency_rwlock_;
286+ bool is_consistency_ = false ;
287+ std::shared_mutex committed_id_rwlock_;
288+ LogOffset committed_id_ = LogOffset();
289+ std::shared_mutex prepared_id__rwlock_;
290+ LogOffset prepared_id_ = LogOffset();
291+ std::shared_ptr<Log> logs_;
202292};
293+
203294#endif // INCLUDE_PIKA_CONSENSUS_H_
0 commit comments