etcd/raft/example_test.go

48 lines
1.2 KiB
Go
Raw Normal View History

/*
Copyright 2014 CoreOS, Inc.
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.
*/
2014-08-23 02:45:39 +04:00
package raft
2014-08-28 05:53:18 +04:00
import (
pb "github.com/coreos/etcd/raft/raftpb"
)
2014-09-16 04:35:02 +04:00
func applyToStore(ents []pb.Entry) {}
func sendMessages(msgs []pb.Message) {}
func saveStateToDisk(st pb.HardState) {}
func saveToDisk(ents []pb.Entry) {}
2014-08-23 02:45:39 +04:00
func Example_Node() {
2014-09-17 23:23:44 +04:00
n := StartNode(0, nil, 0, 0)
2014-08-23 02:45:39 +04:00
2014-08-24 03:06:55 +04:00
// stuff to n happens in other goroutines
2014-08-23 02:45:39 +04:00
2014-08-23 03:53:56 +04:00
// the last known state
2014-09-16 04:35:02 +04:00
var prev pb.HardState
2014-08-23 02:45:39 +04:00
for {
// ReadState blocks until there is new state ready.
rd := <-n.Ready()
if !isHardStateEqual(prev, rd.HardState) {
2014-09-16 04:35:02 +04:00
saveStateToDisk(rd.HardState)
prev = rd.HardState
2014-08-23 02:45:39 +04:00
}
2014-08-27 03:19:49 +04:00
saveToDisk(rd.Entries)
go applyToStore(rd.CommittedEntries)
sendMessages(rd.Messages)
2014-08-23 02:45:39 +04:00
}
}