go.etcd.io/etcd@v3.3.27+incompatible/contrib/raftexample/main.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"strings"
    20  
    21  	"github.com/coreos/etcd/raft/raftpb"
    22  )
    23  
    24  func main() {
    25  	cluster := flag.String("cluster", "http://127.0.0.1:9021", "comma separated cluster peers")
    26  	id := flag.Int("id", 1, "node ID")
    27  	kvport := flag.Int("port", 9121, "key-value server port")
    28  	join := flag.Bool("join", false, "join an existing cluster")
    29  	flag.Parse()
    30  
    31  	proposeC := make(chan string)
    32  	defer close(proposeC)
    33  	confChangeC := make(chan raftpb.ConfChange)
    34  	defer close(confChangeC)
    35  
    36  	// raft provides a commit stream for the proposals from the http api
    37  	var kvs *kvstore
    38  	getSnapshot := func() ([]byte, error) { return kvs.getSnapshot() }
    39  	commitC, errorC, snapshotterReady := newRaftNode(*id, strings.Split(*cluster, ","), *join, getSnapshot, proposeC, confChangeC)
    40  
    41  	kvs = newKVStore(<-snapshotterReady, proposeC, commitC, errorC)
    42  
    43  	// the key-value http handler will propose updates to raft
    44  	serveHttpKVAPI(kvs, *kvport, confChangeC, errorC)
    45  }