Skip to content

Commit 8b62744

Browse files
author
wuxianrong
committed
Add the mapping relationship between LogIndex and RocksDB SequnceNumber
1 parent e75170f commit 8b62744

File tree

15 files changed

+850
-92
lines changed

15 files changed

+850
-92
lines changed

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ ExternalProject_Add(protobuf
559559
DEPENDS
560560
zlib
561561
URL
562-
https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-cpp-3.17.3.tar.gz
562+
https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protobuf-cpp-3.21.12.tar.gz
563563
URL_HASH
564-
MD5=3fe4c2647e0991c014a386a896d0a116
564+
MD5=6b4fd9cee2fa63834f29c7d433679855
565565
DOWNLOAD_NO_PROGRESS
566566
1
567567
UPDATE_COMMAND
@@ -572,6 +572,8 @@ ExternalProject_Add(protobuf
572572
1
573573
LOG_INSTALL
574574
1
575+
LOG_OUTPUT_ON_FAILURE
576+
ON
575577
SOURCE_SUBDIR
576578
cmake
577579
CMAKE_ARGS
@@ -655,9 +657,9 @@ ExternalProject_Add(brpc
655657
snappy
656658
zlib
657659
URL
658-
https://github.com/apache/brpc/archive/refs/tags/1.6.0.tar.gz
660+
https://github.com/apache/brpc/archive/refs/tags/1.9.0.tar.gz
659661
URL_HASH
660-
MD5=0d37cea25bd006e89806f461ef7e39ba
662+
MD5=a2b626d96a5b017f2a6701ffa594530c
661663
DOWNLOAD_NO_PROGRESS
662664
1
663665
UPDATE_COMMAND
@@ -668,6 +670,8 @@ ExternalProject_Add(brpc
668670
1
669671
LOG_INSTALL
670672
1
673+
LOG_OUTPUT_ON_FAILURE
674+
ON
671675
CMAKE_ARGS
672676
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
673677
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
@@ -713,6 +717,8 @@ ExternalProject_Add(braft
713717
1
714718
LOG_INSTALL
715719
1
720+
LOG_OUTPUT_ON_FAILURE
721+
ON
716722
CMAKE_ARGS
717723
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
718724
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}

src/praft/include/praft/praft.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ class PikaStateMachine : public braft::StateMachine {
9797
void on_stop_following(const ::braft::LeaderChangeContext& ctx) override;
9898

9999
private:
100-
std::atomic<int64_t> applied_index_;
101-
std::atomic<int64_t> leader_term_;
100+
102101
};
103102

104103
// Raft node wrapper
@@ -185,8 +184,8 @@ class RaftManager {
185184
std::shared_ptr<PikaRaftNode> GetRaftNode(const std::string& db_name);
186185

187186
// Apply binlog entry to storage (public for PikaStateMachine to call)
188-
rocksdb::Status ApplyBinlogEntry(const ::pikiwidb::Binlog& binlog);
189-
187+
rocksdb::Status ApplyBinlogEntry(const ::pikiwidb::Binlog& binlog, uint64_t log_index = 0);
188+
190189
private:
191190
std::atomic<bool> initialized_;
192191
std::atomic<bool> running_;
@@ -208,4 +207,3 @@ class RaftManager {
208207
} // namespace pika_raft
209208

210209
#endif // PRAFT_PRAFT_H_
211-

src/praft/src/praft.cc

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ extern std::unique_ptr<PikaServer> g_pika_server;
2929
namespace pika_raft {
3030

3131
// PikaStateMachine implementation
32-
PikaStateMachine::PikaStateMachine()
33-
: applied_index_(0), leader_term_(-1) {
34-
}
32+
PikaStateMachine::PikaStateMachine() {}
3533

3634
void 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

10499
void PikaStateMachine::on_leader_start(int64_t term) {
105-
leader_term_.store(term, std::memory_order_relaxed);
106100
}
107101

108102
void PikaStateMachine::on_leader_stop(const butil::Status& status) {
109-
leader_term_.store(-1, std::memory_order_relaxed);
110103
}
111104

112105
void PikaStateMachine::on_error(const ::braft::Error& e) {
@@ -465,6 +458,7 @@ pstd::Status RaftManager::RemoveNode(const std::string& db_name,
465458
}
466459

467460
braft::PeerId peer_id = ParsePeerId(peer_addr);
461+
468462
if (peer_id.is_empty()) {
469463
return pstd::Status::Corruption("Invalid peer address: " + peer_addr);
470464
}
@@ -475,6 +469,7 @@ pstd::Status RaftManager::RemoveNode(const std::string& db_name,
475469
pstd::Status RaftManager::GetClusterInfo(const std::string& db_name,
476470
std::string* info) {
477471
auto node = GetRaftNode(db_name);
472+
478473
if (!node) {
479474
return pstd::Status::Corruption("Raft node not found for DB: " + db_name);
480475
}
@@ -501,14 +496,39 @@ pstd::Status RaftManager::CreateRaftNode(const std::string& db_name,
501496
return pstd::Status::Corruption("Raft node already exists for DB: " + db_name);
502497
}
503498

504-
// Create peer ID for this node
505-
// Use localhost since Pika doesn't expose a host() method
506-
std::string addr = "127.0.0.1:" + std::to_string(g_pika_conf->port() + 3000);
507-
braft::PeerId peer_id = ParsePeerId(addr);
508-
if (peer_id.is_empty()) {
509-
return pstd::Status::Corruption("Failed to create peer ID");
499+
// Determine the Raft port for this node
500+
// Raft uses Pika port + 3000
501+
int raft_port = g_pika_conf->port() + 3000;
502+
503+
// Find the peer address from the peers list that matches our Raft port
504+
// This allows the user to specify the exact address in RAFT.CLUSTER INIT command
505+
braft::PeerId peer_id;
506+
bool found = false;
507+
508+
for (const auto& peer : peers) {
509+
if (peer.addr.port == raft_port) {
510+
peer_id = peer;
511+
found = true;
512+
LOG(INFO) << "Found matching peer address in cluster config: " << peer.to_string();
513+
break;
514+
}
510515
}
511516

517+
// If no matching peer found, return error
518+
if (!found) {
519+
std::string error_msg = "No matching peer address found in cluster config for Raft port " +
520+
std::to_string(raft_port) +
521+
". Please include this node's address (with port " +
522+
std::to_string(raft_port) +
523+
") in the RAFT.CLUSTER INIT command. " +
524+
"Example: RAFT.CLUSTER INIT <ip1>:" + std::to_string(raft_port) +
525+
",<ip2>:<port2>,...";
526+
LOG(ERROR) << error_msg;
527+
return pstd::Status::Corruption(error_msg);
528+
}
529+
530+
LOG(INFO) << "Creating Raft node for DB: " << db_name << " with address: " << peer_id.to_string();
531+
512532
// Create Raft node
513533
auto node = std::make_shared<PikaRaftNode>(group_id_ + "_" + db_name, peer_id);
514534

@@ -625,7 +645,7 @@ void RaftManager::AppendLog(const std::string& db_name,
625645
node->GetRaftNode()->apply(task);
626646
}
627647

628-
rocksdb::Status RaftManager::ApplyBinlogEntry(const ::pikiwidb::Binlog& binlog) {
648+
rocksdb::Status RaftManager::ApplyBinlogEntry(const ::pikiwidb::Binlog& binlog, uint64_t log_index) {
629649
std::string db_name = "db0";
630650

631651
auto db = g_pika_server->GetDB(db_name);
@@ -640,14 +660,15 @@ rocksdb::Status RaftManager::ApplyBinlogEntry(const ::pikiwidb::Binlog& binlog)
640660
return rocksdb::Status::InvalidArgument("Storage is null");
641661
}
642662

643-
auto status = storage->OnBinlogWrite(binlog, 0);
663+
// Pass log_index to storage layer for tracking
664+
auto status = storage->OnBinlogWrite(binlog, log_index);
644665

645666
if (!status.ok()) {
646-
LOG(ERROR) << "Failed to apply binlog to " << db_name << ": " << status.ToString();
667+
LOG(ERROR) << "Failed to apply binlog to " << db_name << " at log_index " << log_index
668+
<< ": " << status.ToString();
647669
}
648670

649671
return status;
650672
}
651673

652674
} // namespace pika_raft
653-

src/storage/include/storage/storage.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ struct StorageOptions {
9999
std::function<void(const ::pikiwidb::Binlog&, std::promise<rocksdb::Status>&&,
100100
CommitCallback)> append_log_function;
101101

102+
std::function<void(int64_t, bool)> do_snapshot_function;
103+
102104
Status ResetOptions(const OptionType& option_type, const std::unordered_map<std::string, std::string>& options_map);
103105
};
104106

@@ -1159,6 +1161,8 @@ class Storage {
11591161

11601162
rocksdb::Status OnBinlogWrite(const ::pikiwidb::Binlog& binlog, uint64_t log_index);
11611163

1164+
uint64_t GetSmallestFlushedLogIndex();
1165+
11621166
private:
11631167
std::unique_ptr<RedisStrings> strings_db_;
11641168
std::unique_ptr<RedisHashes> hashes_db_;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2024-present, Qihoo, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
6+
#ifndef STORAGE_STORAGE_DEFINE_H_
7+
#define STORAGE_STORAGE_DEFINE_H_
8+
9+
#include <cstdint>
10+
11+
namespace storage {
12+
13+
using LogIndex = int64_t;
14+
15+
} // namespace storage
16+
17+
#endif // STORAGE_STORAGE_DEFINE_H_

0 commit comments

Comments
 (0)