@@ -29,9 +29,7 @@ extern std::unique_ptr<PikaServer> g_pika_server;
2929namespace pika_raft {
3030
3131// PikaStateMachine implementation
32- PikaStateMachine::PikaStateMachine ()
33- : applied_index_(0 ), leader_term_(-1 ) {
34- }
32+ PikaStateMachine::PikaStateMachine () {}
3533
3634void PikaStateMachine::on_apply (braft::Iterator& iter) {
3735 for (; iter.valid (); iter.next ()) {
@@ -41,7 +39,6 @@ void PikaStateMachine::on_apply(braft::Iterator& iter) {
4139 int64_t index = iter.index ();
4240
4341 if (!g_pika_server || !g_pika_server->GetRaftManager ()) {
44- applied_index_.store (index, std::memory_order_relaxed);
4542 // Run closure asynchronously in bthread to avoid blocking on_apply
4643 if (done) {
4744 braft::run_closure_in_bthread (done_guard.release ());
@@ -55,14 +52,14 @@ void PikaStateMachine::on_apply(braft::Iterator& iter) {
5552 if (done) {
5653 done->status ().set_error (EINVAL, " Failed to parse binlog" );
5754 }
58- applied_index_.store (index, std::memory_order_relaxed);
5955 if (done) {
6056 braft::run_closure_in_bthread (done_guard.release ());
6157 }
6258 continue ;
6359 }
6460
65- rocksdb::Status apply_status = g_pika_server->GetRaftManager ()->ApplyBinlogEntry (binlog);
61+ // Apply binlog with log index for tracking
62+ rocksdb::Status apply_status = g_pika_server->GetRaftManager ()->ApplyBinlogEntry (binlog, index);
6663
6764 if (done) {
6865 if (apply_status.ok ()) {
@@ -73,7 +70,6 @@ void PikaStateMachine::on_apply(braft::Iterator& iter) {
7370 }
7471 }
7572
76- applied_index_.store (index, std::memory_order_relaxed);
7773
7874 // Run closure asynchronously in bthread to avoid blocking on_apply
7975 if (done) {
@@ -94,19 +90,16 @@ int PikaStateMachine::on_snapshot_load(braft::SnapshotReader* reader) {
9490 in >> index;
9591 in.close ();
9692
97- applied_index_.store (index, std::memory_order_relaxed);
9893 return 0 ;
9994 }
10095
10196 return -1 ;
10297}
10398
10499void PikaStateMachine::on_leader_start (int64_t term) {
105- leader_term_.store (term, std::memory_order_relaxed);
106100}
107101
108102void PikaStateMachine::on_leader_stop (const butil::Status& status) {
109- leader_term_.store (-1 , std::memory_order_relaxed);
110103}
111104
112105void PikaStateMachine::on_error (const ::braft::Error& e) {
@@ -625,7 +618,7 @@ void RaftManager::AppendLog(const std::string& db_name,
625618 node->GetRaftNode ()->apply (task);
626619}
627620
628- rocksdb::Status RaftManager::ApplyBinlogEntry (const ::pikiwidb::Binlog& binlog) {
621+ rocksdb::Status RaftManager::ApplyBinlogEntry (const ::pikiwidb::Binlog& binlog, uint64_t log_index ) {
629622 std::string db_name = " db0" ;
630623
631624 auto db = g_pika_server->GetDB (db_name);
@@ -640,14 +633,15 @@ rocksdb::Status RaftManager::ApplyBinlogEntry(const ::pikiwidb::Binlog& binlog)
640633 return rocksdb::Status::InvalidArgument (" Storage is null" );
641634 }
642635
643- auto status = storage->OnBinlogWrite (binlog, 0 );
636+ // Pass log_index to storage layer for tracking
637+ auto status = storage->OnBinlogWrite (binlog, log_index);
644638
645639 if (!status.ok ()) {
646- LOG (ERROR) << " Failed to apply binlog to " << db_name << " : " << status.ToString ();
640+ LOG (ERROR) << " Failed to apply binlog to " << db_name << " at log_index " << log_index
641+ << " : " << status.ToString ();
647642 }
648643
649644 return status;
650645}
651646
652647} // namespace pika_raft
653-
0 commit comments