-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add raft from etcd key value store example
- Loading branch information
Showing
11 changed files
with
1,201 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Use goreman to run `go install github.com/mattn/goreman@latest` | ||
raftexample1: ./raftexample --id 1 --cluster http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 --port 12380 --pedis 127.0.0.1:6379 | ||
raftexample2: ./raftexample --id 2 --cluster http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 --port 22380 --pedis 127.0.0.1:6389 | ||
raftexample3: ./raftexample --id 3 --cluster http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 --port 32380 --pedis 127.0.0.1:6399 |
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
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,56 +1,52 @@ | ||
// Copyright 2015 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"pedis/node" | ||
"pedis/internal/storage" | ||
"pedis/praft" | ||
"strings" | ||
) | ||
|
||
var addr string | ||
var raftAddr string | ||
var joinAddr string | ||
var startJoinAddrs string | ||
var id string | ||
var membershipAddr string | ||
var bootstrap bool | ||
|
||
func init() { | ||
flag.StringVar(&membershipAddr, "serf-addr", "localhost:6359", "the address where the cluster membership server is listening") | ||
flag.StringVar(&raftAddr, "raft-addr", "localhost:6389", "the address where the raft server is listening") | ||
flag.StringVar(&addr, "addr", "localhost:6379", "the address where the server will listen") | ||
flag.StringVar(&id, "id", "primary", "the unique id of the server in the cluster") | ||
flag.StringVar(&joinAddr, "join-addr", "", "set address of the leader of the cluster") | ||
flag.StringVar(&startJoinAddrs, "sjoin-addr", "", "set addresses of cluster members to join when starting") | ||
flag.BoolVar(&bootstrap, "bootstrap", false, "start as bootstrap node") | ||
} | ||
"go.etcd.io/etcd/raft/v3/raftpb" | ||
) | ||
|
||
func main() { | ||
cluster := flag.String("cluster", "http://127.0.0.1:9021", "comma separated cluster peers") | ||
id := flag.Int("id", 1, "node ID") | ||
kvport := flag.Int("port", 9121, "key-value server port") | ||
join := flag.Bool("join", false, "join an existing cluster") | ||
pedis := flag.String("pedis", ":6379", "port where pedis server is running") | ||
flag.Parse() | ||
|
||
joinAddrs := []string{} | ||
proposeC := make(chan string) | ||
defer close(proposeC) | ||
|
||
if startJoinAddrs != "" { | ||
joinAddrs = strings.Split(startJoinAddrs, ",") | ||
} | ||
confChangeC := make(chan raftpb.ConfChange) | ||
defer close(confChangeC) | ||
|
||
node, err := node.NewNode(node.Config{ | ||
Bootstrap: bootstrap, | ||
JoinAddr: joinAddr, | ||
RaftAddr: raftAddr, | ||
MembershipAddr: membershipAddr, | ||
ServerAddr: addr, | ||
ServerId: id, | ||
StartJoinAddrs: joinAddrs, | ||
}) | ||
storageProposeChan := make(chan storage.StorageData) | ||
defer close(storageProposeChan) | ||
|
||
if err != nil { | ||
log.Fatalf("error creating node %v", err) | ||
} | ||
// raft provides a commit stream for the proposals from the http api | ||
var kvs *praft.PedisServer | ||
getSnapshot := func() ([]byte, error) { return kvs.GetSnapshot() } | ||
commitC, errorC, snapshotterReady := praft.NewRaftNode(*id, strings.Split(*cluster, ","), *join, getSnapshot, proposeC, confChangeC) | ||
|
||
err = node.Start() | ||
kvs = praft.NewKVStore(<-snapshotterReady, proposeC, commitC, errorC, storage.NewSimpleStorage(storageProposeChan), *pedis, storageProposeChan) | ||
|
||
if err != nil { | ||
log.Fatalf("error starting pedis node %v", err) | ||
} | ||
// the key-value http handler will propose updates to raft | ||
praft.ServeHTTPKVAPI(kvs, *kvport, confChangeC, errorC) | ||
} |
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,128 @@ | ||
// Copyright 2015 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package praft | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
|
||
"go.etcd.io/etcd/raft/v3/raftpb" | ||
) | ||
|
||
// Handler for a http based key-value store backed by raft | ||
type httpKVAPI struct { | ||
store *PedisServer | ||
confChangeC chan<- raftpb.ConfChange | ||
} | ||
|
||
func (h *httpKVAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
key := r.RequestURI | ||
defer r.Body.Close() | ||
switch r.Method { | ||
case http.MethodPut: | ||
v, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
log.Printf("Failed to read on PUT (%v)\n", err) | ||
http.Error(w, "Failed on PUT", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
log.Printf("command change (%v)\n", v) | ||
// h.store.Propose(key, string(v)) | ||
|
||
// Optimistic-- no waiting for ack from raft. Value is not yet | ||
// committed so a subsequent GET on the key may return old value | ||
w.WriteHeader(http.StatusNoContent) | ||
case http.MethodGet: | ||
if v, ok := h.store.Lookup(key); ok { | ||
w.Write([]byte(v)) | ||
} else { | ||
http.Error(w, "Failed to GET", http.StatusNotFound) | ||
} | ||
case http.MethodPost: | ||
url, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
log.Printf("Failed to read on POST (%v)\n", err) | ||
http.Error(w, "Failed on POST", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
nodeID, err := strconv.ParseUint(key[1:], 0, 64) | ||
if err != nil { | ||
log.Printf("Failed to convert ID for conf change (%v)\n", err) | ||
http.Error(w, "Failed on POST", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
cc := raftpb.ConfChange{ | ||
Type: raftpb.ConfChangeAddNode, | ||
NodeID: nodeID, | ||
Context: url, | ||
} | ||
h.confChangeC <- cc | ||
// As above, optimistic that raft will apply the conf change | ||
w.WriteHeader(http.StatusNoContent) | ||
case http.MethodDelete: | ||
nodeID, err := strconv.ParseUint(key[1:], 0, 64) | ||
if err != nil { | ||
log.Printf("Failed to convert ID for conf change (%v)\n", err) | ||
http.Error(w, "Failed on DELETE", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
cc := raftpb.ConfChange{ | ||
Type: raftpb.ConfChangeRemoveNode, | ||
NodeID: nodeID, | ||
} | ||
h.confChangeC <- cc | ||
|
||
// As above, optimistic that raft will apply the conf change | ||
w.WriteHeader(http.StatusNoContent) | ||
default: | ||
w.Header().Set("Allow", http.MethodPut) | ||
w.Header().Add("Allow", http.MethodGet) | ||
w.Header().Add("Allow", http.MethodPost) | ||
w.Header().Add("Allow", http.MethodDelete) | ||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | ||
} | ||
} | ||
|
||
// serveHTTPKVAPI starts a key-value server with a GET/PUT API and listens. | ||
func ServeHTTPKVAPI(kv *PedisServer, port int, confChangeC chan<- raftpb.ConfChange, errorC <-chan error) { | ||
srv := http.Server{ | ||
Addr: ":" + strconv.Itoa(port), | ||
Handler: &httpKVAPI{ | ||
store: kv, | ||
confChangeC: confChangeC, | ||
}, | ||
} | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
go func() { | ||
if err := kv.StartPedis(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
// exit when raft goes down | ||
if err, ok := <-errorC; ok { | ||
log.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.