forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Doc: Finish gRPC example raft-kv-memstore-grpc
A rust test `tests/test_cluster.rs` brings up a 3 nodes cluster and executes write and read on it. - Fix: databendlabs#1287
- Loading branch information
1 parent
ce83ecc
commit c64caaf
Showing
14 changed files
with
494 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,71 @@ | ||
syntax = "proto3"; | ||
|
||
import "google/protobuf/empty.proto"; | ||
import "google/protobuf/wrappers.proto"; | ||
import "raft.proto"; | ||
import "app_types.proto"; | ||
|
||
package openraftpb; | ||
|
||
// SetRequest represents a key-value pair to be stored | ||
message SetRequest { | ||
string key = 1; // Key to store | ||
string value = 2; // Value to associate with the key | ||
// InitRequest contains the initial set of nodes for cluster initialization | ||
message InitRequest { | ||
// List of initial cluster nodes | ||
repeated Node nodes = 1; | ||
} | ||
|
||
// AddLearnerRequest specifies parameters for adding a learner node | ||
message AddLearnerRequest { | ||
// Node to be added as a learner | ||
Node node = 1; | ||
} | ||
|
||
// GetRequest represents a key lookup request | ||
message GetRequest { | ||
string key = 1; // Key to look up | ||
// ChangeMembershipRequest specifies parameters for modifying cluster membership | ||
message ChangeMembershipRequest { | ||
// New set of voter node IDs | ||
repeated uint64 members = 1; | ||
// Whether to retain existing configuration | ||
bool retain = 2; | ||
} | ||
|
||
message ClientWriteResponse { | ||
// The log id of the committed log entry. | ||
LogId log_id = 1; | ||
|
||
// If the committed log entry is a normal one. | ||
Response data = 2; | ||
|
||
// If the committed log entry is a change-membership entry. | ||
Membership membership = 3; | ||
} | ||
|
||
// GetResponse contains the value associated with the requested key | ||
message Response { | ||
optional string value = 1; // Retrieved value | ||
message MetricsResponse { | ||
// Cluster membership config | ||
Membership membership = 1; | ||
|
||
// In this example, only membership is used. | ||
// Other metrics are just encoded in string for simplicity. | ||
// In real-world scenarios, metrics should be encoded in a more structured format. | ||
string other_metrics = 2; | ||
} | ||
|
||
// ApiService provides the key-value store API operations | ||
// ApiService provides the key-value store API operations and Raft cluster management operations | ||
service AppService { | ||
// Get retrieves the value associated with a given key | ||
rpc Get(GetRequest) returns (Response) {} | ||
|
||
// Set stores a key-value pair in the distributed store | ||
rpc Set(SetRequest) returns (Response) {} | ||
} | ||
|
||
// Init initializes a new Raft cluster with the given nodes | ||
rpc Init(InitRequest) returns (google.protobuf.Empty) {} | ||
|
||
// AddLearner adds a new learner node to the Raft cluster | ||
rpc AddLearner(AddLearnerRequest) returns (ClientWriteResponse) {} | ||
|
||
// ChangeMembership modifies the cluster membership configuration | ||
rpc ChangeMembership(ChangeMembershipRequest) returns (ClientWriteResponse) {} | ||
|
||
// Metrics retrieves cluster metrics and status information | ||
rpc Metrics(google.protobuf.Empty) returns (MetricsResponse) {} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
syntax = "proto3"; | ||
|
||
package openraftpb; | ||
|
||
// SetRequest represents a key-value pair to be stored | ||
message SetRequest { | ||
string key = 1; // Key to store | ||
string value = 2; // Value to associate with the key | ||
} | ||
|
||
// GetRequest represents a key lookup request | ||
message GetRequest { | ||
string key = 1; // Key to look up | ||
} | ||
|
||
// GetResponse contains the value associated with the requested key | ||
message Response { | ||
optional string value = 1; // Retrieved value | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::sync::Arc; | ||
|
||
use openraft::Config; | ||
use tonic::transport::Server; | ||
use tracing::info; | ||
|
||
use crate::grpc::app_service::AppServiceImpl; | ||
use crate::grpc::raft_service::RaftServiceImpl; | ||
use crate::network::Network; | ||
use crate::pb::app_service_server::AppServiceServer; | ||
use crate::pb::raft_service_server::RaftServiceServer; | ||
use crate::store::LogStore; | ||
use crate::store::StateMachineStore; | ||
use crate::typ::*; | ||
use crate::NodeId; | ||
|
||
pub async fn start_raft_app(node_id: NodeId, http_addr: String) -> Result<(), Box<dyn std::error::Error>> { | ||
// Create a configuration for the raft instance. | ||
let config = Arc::new( | ||
Config { | ||
heartbeat_interval: 500, | ||
election_timeout_min: 1500, | ||
election_timeout_max: 3000, | ||
..Default::default() | ||
} | ||
.validate()?, | ||
); | ||
|
||
// Create stores and network | ||
let log_store = LogStore::default(); | ||
let state_machine_store = Arc::new(StateMachineStore::default()); | ||
let network = Network {}; | ||
|
||
// Create Raft instance | ||
let raft = Raft::new(node_id, config.clone(), network, log_store, state_machine_store.clone()).await?; | ||
|
||
// Create the management service with raft instance | ||
let internal_service = RaftServiceImpl::new(raft.clone()); | ||
let api_service = AppServiceImpl::new(raft, state_machine_store); | ||
|
||
// Start server | ||
let server_future = Server::builder() | ||
.add_service(RaftServiceServer::new(internal_service)) | ||
.add_service(AppServiceServer::new(api_service)) | ||
.serve(http_addr.parse()?); | ||
|
||
info!("Node {node_id} starting server at {http_addr}"); | ||
server_future.await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.