Skip to content

Commit 5c44faf

Browse files
committed
feat(distributed): transition to Multi-Group Raft with RaftManager
1 parent 9eaf126 commit 5c44faf

9 files changed

Lines changed: 243 additions & 87 deletions

File tree

include/catalog/catalog.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace cloudsql {
2323

2424
namespace raft {
25-
class RaftNode;
25+
class RaftGroup;
2626
}
2727

2828
// Type aliases
@@ -158,9 +158,9 @@ class Catalog {
158158
[[nodiscard]] static std::unique_ptr<Catalog> create();
159159

160160
/**
161-
* @brief Set Raft node for distributed operations
161+
* @brief Set Raft group for distributed operations
162162
*/
163-
void set_raft_node(raft::RaftNode* raft_node) { raft_node_ = raft_node; }
163+
void set_raft_group(raft::RaftGroup* raft_group) { raft_group_ = raft_group; }
164164

165165
/**
166166
* @brief Load catalog from file
@@ -271,7 +271,7 @@ class Catalog {
271271
DatabaseInfo database_;
272272
oid_t next_oid_ = 1;
273273
uint64_t version_ = 1;
274-
raft::RaftNode* raft_node_ = nullptr;
274+
raft::RaftGroup* raft_group_ = nullptr;
275275

276276
[[nodiscard]] static uint64_t get_current_time();
277277
};
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* @file raft_node.hpp
3-
* @brief Raft consensus node implementation
2+
* @file raft_group.hpp
3+
* @brief Raft consensus group implementation
44
*/
55

6-
#ifndef SQL_ENGINE_DISTRIBUTED_RAFT_NODE_HPP
7-
#define SQL_ENGINE_DISTRIBUTED_RAFT_NODE_HPP
6+
#ifndef SQL_ENGINE_DISTRIBUTED_RAFT_GROUP_HPP
7+
#define SQL_ENGINE_DISTRIBUTED_RAFT_GROUP_HPP
88

99
#include <atomic>
1010
#include <chrono>
@@ -22,24 +22,24 @@
2222
namespace cloudsql::raft {
2323

2424
/**
25-
* @brief Implementation of a Raft consensus node
25+
* @brief Implementation of a Raft consensus group
2626
*/
27-
class RaftNode {
27+
class RaftGroup {
2828
public:
29-
RaftNode(std::string node_id, cluster::ClusterManager& cluster_manager,
30-
network::RpcServer& rpc_server);
31-
~RaftNode();
29+
RaftGroup(uint16_t group_id, std::string node_id, cluster::ClusterManager& cluster_manager,
30+
network::RpcServer& rpc_server);
31+
~RaftGroup();
3232

3333
// Prevent copying and moving
34-
RaftNode(const RaftNode&) = delete;
35-
RaftNode& operator=(const RaftNode&) = delete;
36-
RaftNode(RaftNode&&) = delete;
37-
RaftNode& operator=(RaftNode&&) = delete;
34+
RaftGroup(const RaftGroup&) = delete;
35+
RaftGroup& operator=(const RaftGroup&) = delete;
36+
RaftGroup(RaftGroup&&) = delete;
37+
RaftGroup& operator=(RaftGroup&&) = delete;
3838

3939
void start();
4040
void stop();
4141

42-
// Raft RPC Handlers
42+
// Raft RPC Handlers (called by RaftManager)
4343
void handle_request_vote(const network::RpcHeader& header, const std::vector<uint8_t>& payload,
4444
int client_fd);
4545
void handle_append_entries(const network::RpcHeader& header,
@@ -48,6 +48,7 @@ class RaftNode {
4848
// Client interface
4949
bool replicate(const std::string& command);
5050
[[nodiscard]] bool is_leader() const { return state_.load() == NodeState::Leader; }
51+
[[nodiscard]] uint16_t group_id() const { return group_id_; }
5152

5253
private:
5354
void run_loop();
@@ -62,6 +63,7 @@ class RaftNode {
6263
// Helpers
6364
[[nodiscard]] std::chrono::milliseconds get_random_timeout() const;
6465

66+
uint16_t group_id_;
6567
std::string node_id_;
6668
cluster::ClusterManager& cluster_manager_;
6769
network::RpcServer& rpc_server_;
@@ -83,4 +85,4 @@ class RaftNode {
8385

8486
} // namespace cloudsql::raft
8587

86-
#endif // SQL_ENGINE_DISTRIBUTED_RAFT_NODE_HPP
88+
#endif // SQL_ENGINE_DISTRIBUTED_RAFT_GROUP_HPP
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @file raft_manager.hpp
3+
* @brief Manages multiple Raft consensus groups on a single node
4+
*/
5+
6+
#ifndef SQL_ENGINE_DISTRIBUTED_RAFT_MANAGER_HPP
7+
#define SQL_ENGINE_DISTRIBUTED_RAFT_MANAGER_HPP
8+
9+
#include <memory>
10+
#include <mutex>
11+
#include <unordered_map>
12+
#include <vector>
13+
14+
#include "distributed/raft_group.hpp"
15+
#include "network/rpc_server.hpp"
16+
17+
namespace cloudsql::raft {
18+
19+
/**
20+
* @brief Manager for Multi-Raft implementation
21+
*/
22+
class RaftManager {
23+
public:
24+
RaftManager(std::string node_id, cluster::ClusterManager& cluster_manager,
25+
network::RpcServer& rpc_server);
26+
~RaftManager() = default;
27+
28+
// Prevent copying
29+
RaftManager(const RaftManager&) = delete;
30+
RaftManager& operator=(const RaftManager&) = delete;
31+
32+
void start();
33+
void stop();
34+
35+
/**
36+
* @brief Create or get a Raft group
37+
*/
38+
std::shared_ptr<RaftGroup> get_or_create_group(uint16_t group_id);
39+
40+
/**
41+
* @brief Get an existing group
42+
*/
43+
std::shared_ptr<RaftGroup> get_group(uint16_t group_id);
44+
45+
private:
46+
/**
47+
* @brief Route incoming Raft RPCs to the correct group
48+
*/
49+
void handle_raft_rpc(const network::RpcHeader& header, const std::vector<uint8_t>& payload,
50+
int client_fd);
51+
52+
std::string node_id_;
53+
cluster::ClusterManager& cluster_manager_;
54+
network::RpcServer& rpc_server_;
55+
56+
std::mutex mutex_;
57+
std::unordered_map<uint16_t, std::shared_ptr<RaftGroup>> groups_;
58+
};
59+
60+
} // namespace cloudsql::raft
61+
62+
#endif // SQL_ENGINE_DISTRIBUTED_RAFT_MANAGER_HPP

include/network/rpc_message.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,35 +157,47 @@ class Serializer {
157157
};
158158

159159
/**
160-
* @brief Header for all internal RPC messages (fixed 8 bytes)
160+
* @brief Header for all internal RPC messages (fixed 12 bytes)
161161
*/
162162
struct RpcHeader {
163163
static constexpr uint32_t MAGIC = 0x4353514C; // 'CSQL'
164-
static constexpr size_t HEADER_SIZE = 8;
164+
static constexpr size_t HEADER_SIZE = 12;
165165

166166
uint32_t magic = MAGIC;
167167
RpcType type = RpcType::Error;
168168
uint8_t flags = 0;
169+
uint16_t group_id = 0; // For Multi-Group Raft
170+
uint16_t reserved = 0;
169171
uint16_t payload_len = 0;
170172

171173
void encode(char* out) const {
172174
uint32_t n_magic = htonl(magic);
175+
uint16_t n_group = htons(group_id);
176+
uint16_t n_reserved = htons(reserved);
173177
uint16_t n_len = htons(payload_len);
174178
std::memcpy(out, &n_magic, 4);
175179
out[4] = static_cast<char>(type);
176180
out[5] = static_cast<char>(flags);
177-
std::memcpy(out + 6, &n_len, 2);
181+
std::memcpy(out + 6, &n_group, 2);
182+
std::memcpy(out + 8, &n_reserved, 2);
183+
std::memcpy(out + 10, &n_len, 2);
178184
}
179185

180186
static RpcHeader decode(const char* in) {
181187
RpcHeader h;
182188
uint32_t n_magic = 0;
189+
uint16_t n_group = 0;
190+
uint16_t n_reserved = 0;
183191
uint16_t n_len = 0;
184192
std::memcpy(&n_magic, in, 4);
185193
h.magic = ntohl(n_magic);
186194
h.type = static_cast<RpcType>(static_cast<uint8_t>(in[4]));
187195
h.flags = static_cast<uint8_t>(in[5]);
188-
std::memcpy(&n_len, in + 6, 2);
196+
std::memcpy(&n_group, in + 6, 2);
197+
h.group_id = ntohs(n_group);
198+
std::memcpy(&n_reserved, in + 8, 2);
199+
h.reserved = ntohs(n_reserved);
200+
std::memcpy(&n_len, in + 10, 2);
189201
h.payload_len = ntohs(n_len);
190202
return h;
191203
}

0 commit comments

Comments
 (0)