forked from myc880628/lilraft
/
server.go
751 lines (697 loc) · 19.8 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package lilraft
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"sync"
"time"
"github.com/golang/protobuf/proto"
)
func init() {
gob.Register(&HTTPNode{})
}
// Logger
var logger = log.New(os.Stdout, "[lilraft]", log.Lmicroseconds)
// Error
var (
errDepose = fmt.Errorf("leader got deposed")
errNoLeader = fmt.Errorf("cannot find leader")
)
// state constant
const (
follower = iota
candidate
leader
stopped
noleader
cOld
cOldNew
)
var (
minTimeout = 150 // minimum timeout: 150 ms
maxTimeout = minTimeout * 2 //maximum timeout: 300ms
)
func randomElectionTimeout() time.Duration {
d := minTimeout + rand.Intn(maxTimeout-minTimeout)
return time.Duration(d) * time.Millisecond
}
func hearbeatInterval() time.Duration {
return time.Duration(minTimeout/10) * time.Millisecond
}
// mutexState wraps stateInt with a Mutex
type mutexState struct {
stateInt uint8
sync.RWMutex
}
func (mstate *mutexState) getState() uint8 {
mstate.RLock()
defer mstate.RUnlock()
return mstate.stateInt
}
func (mstate *mutexState) setState(stateInt uint8) {
mstate.Lock()
mstate.stateInt = stateInt
mstate.Unlock()
}
type nextIndex struct {
m map[int32]int64
sync.RWMutex
}
func (ni *nextIndex) get(id int32) int64 {
ni.RLock()
defer ni.RUnlock()
if index, ok := ni.m[id]; !ok {
panic("lilraft: wrong node in []NextIndex")
} else {
return index
}
}
func (ni *nextIndex) set(id int32, index int64) {
ni.Lock()
ni.m[id] = index
ni.Unlock()
}
func (ni *nextIndex) inc(id int32) {
ni.Lock()
ni.m[id]++
ni.Unlock()
}
func (ni *nextIndex) dec(id int32) {
ni.Lock()
if ni.m[id] > 0 {
ni.m[id]--
}
ni.Unlock()
}
// Server is a concrete machine that process command, appendentries and requestvote, etc.
type Server struct {
//----from the paper------
voted bool
currentTerm int64
lastAppliedIndex int64
matchIndex []int64
nextIndex nextIndex
log *Log
//------------------------
id int32
leader int32
context interface{}
electionTimeout *time.Timer
httpClient http.Client
config *configuration
getVoteRequestChan chan wrappedVoteRequest
getAppendEntriesChan chan wrappedAppendRequest
getConfigChan chan wrappedSetConfig
// responseChan
//-------leader only------------
deposeChan chan int64
commandChan chan wrappedCommand
//-----candidate only-----------
getVoteResponseChan chan wrappedVoteResponse
nodesVoteGranted map[int32]bool
//-----------------------------
path string
mutexState
}
type wrappedVoteResponse struct {
id int32
*RequestVoteResponse
}
type wrappedCommand struct {
command Command
errChan chan error
valChan chan interface{}
}
type wrappedAppendRequest struct {
request *AppendEntriesRequest
responseChan chan *AppendEntriesResponse
}
type wrappedVoteRequest struct {
request *RequestVoteRequest
responseChan chan *RequestVoteResponse
}
type wrappedSetConfig struct {
nodes []Node
errChan chan error
}
func (s *Server) resetElectionTimeout() {
s.electionTimeout = time.NewTimer(randomElectionTimeout())
}
// Exec executes client's command. client should
// use concrete and registered commands that
// implement Command interface
func (s *Server) Exec(command Command) (interface{}, error) {
errChan := make(chan error)
valChan := make(chan interface{})
s.commandChan <- wrappedCommand{
errChan: errChan,
command: command,
valChan: valChan,
}
retErr := <-errChan
retVal := <-valChan
return retVal, retErr
}
// RegisterCommand registers client's commands that
// implement Command interface. Only registered commands
// can be decoded and executed by other nodes.
func (s *Server) RegisterCommand(command Command) {
if command == nil {
panic("lilraft: Command cannot be nil")
} else if commandType[command.Name()] != nil {
panic("lilraft: Command exists!")
}
commandType[command.Name()] = command
}
// SetConfig allows client to change the nodes of configuration
func (s *Server) SetConfig(nodes ...Node) error {
errChan := make(chan error)
s.getConfigChan <- wrappedSetConfig{
nodes: nodes,
errChan: errChan,
}
return <-errChan
}
// NewServer can return a new server for clients
func NewServer(id int32, context interface{}, config *configuration, path string) (s *Server) {
if context == nil {
panic("lilraft: contex is required")
}
if id <= 0 {
panic("lilraft: id must be > 0")
}
if config == nil {
panic("lilraft: configuration unset")
}
s = &Server{
id: id,
leader: noleader,
context: context,
path: path,
currentTerm: 0,
log: newLog(),
voted: false,
config: config,
nextIndex: nextIndex{m: make(map[int32]int64)},
commandChan: make(chan wrappedCommand),
getVoteRequestChan: make(chan wrappedVoteRequest),
getAppendEntriesChan: make(chan wrappedAppendRequest),
getConfigChan: make(chan wrappedSetConfig),
}
s.mutexState.setState(stopped)
// http.Client can cache TCP connections
s.httpClient.Transport = &http.Transport{DisableKeepAlives: false}
s.httpClient.Timeout = time.Duration(minTimeout/2) * time.Millisecond
rand.Seed(time.Now().Unix()) // random seed for election timeout
s.resetElectionTimeout()
err := os.MkdirAll(path, 0700)
if err != nil && !os.IsExist(err) {
panic(err.Error())
}
return
}
// Start starts a server, remember to call NewServer before call this.
func (s *Server) Start(recoverContex bool) {
if err := s.log.recover(s.logPath(), s.context, recoverContex); err != nil {
logger.Println("server recover", s.logPath(), "error: ", err.Error())
}
s.setState(follower) // all servers start as follower
go s.loop()
}
// Stop stops a server by setting server's state(a integer) to stopped
func (s *Server) Stop() {
s.setState(stopped)
s.log.close()
}
func (s *Server) loop() {
for {
switch s.getState() {
case follower:
s.followerloop()
case candidate:
s.candidateloop()
case leader:
s.leaderloop()
case stopped:
return
default:
panic("lilraft: impossible state")
}
}
}
// TODO: fill this.
func (s *Server) followerloop() {
for s.getState() == follower {
select {
case <-s.electionTimeout.C:
s.setState(candidate)
logger.Printf("node %d become candidate\n", s.id)
return
case wrappedVoteRequest := <-s.getVoteRequestChan:
voteRequest := wrappedVoteRequest.request
wrappedVoteRequest.responseChan <- s.handleVoteRequest(voteRequest)
case wrappedAppendRequest := <-s.getAppendEntriesChan:
appendRequest := wrappedAppendRequest.request
wrappedAppendRequest.responseChan <- s.handleAppendEntriesRequest(appendRequest)
case wrappedCommand := <-s.commandChan:
val, err := s.redirectClientCommand(wrappedCommand.command)
wrappedCommand.errChan <- err
wrappedCommand.valChan <- val
case wrappedSetConfig := <-s.getConfigChan:
wrappedSetConfig.errChan <- s.redirectClientConfig(wrappedSetConfig.nodes)
}
}
}
func (s *Server) redirectClientCommand(command Command) (interface{}, error) {
if s.leader == noleader {
return nil, errNoLeader
}
logger.Printf("node %d redirecting client to node %d", s.id, s.leader)
var bytesBuffer bytes.Buffer
if err := json.NewEncoder(&bytesBuffer).Encode(command); err != nil {
return nil, err
}
redirectedCommand := &RedirectedCommand{
CommandName: proto.String(command.Name()),
Command: bytesBuffer.Bytes(),
}
return s.theOtherNodes()[s.leader].rpcCommand(s, redirectedCommand)
}
func (s *Server) redirectClientConfig(nodes []Node) error {
if s.leader == noleader {
return errNoLeader
}
logger.Printf("node %d redirecting config to node %d", s.id, s.leader)
return s.theOtherNodes()[s.leader].rpcSetConfig(s, nodes)
}
func (s *Server) candidateloop() {
s.nodesVoteGranted = make(map[int32]bool)
s.leader = noleader
s.updateCurrentTerm(s.currentTerm + 1) // enter candidate state. Server increments its term
s.resetElectionTimeout()
s.requestVotes()
go s.voteForItself()
for s.getState() == candidate {
select {
case <-s.electionTimeout.C:
// Candidate timeouts and start a new election
return
case resp := <-s.getVoteResponseChan:
if resp.GetTerm() > s.currentTerm {
s.updateCurrentTerm(resp.GetTerm())
s.stepDown()
return
}
if resp.GetVoteGranted() {
s.nodesVoteGranted[resp.id] = true
}
if s.electionPass() {
// TODO: delete these logger
for id := range s.nodesVoteGranted {
logger.Printf("node %d vote for node %d", id, s.id)
}
logger.Printf("node %d became leader\n", s.id)
s.setState(leader)
s.voted = false
s.leader = s.id
return
}
case wrappedVoteRequest := <-s.getVoteRequestChan:
voteRequest := wrappedVoteRequest.request
if voteRequest.GetTerm() > s.currentTerm {
s.voted = false // candidate has voted for itself, we need to change that
s.stepDown()
// candidate become follower
wrappedVoteRequest.responseChan <- s.handleVoteRequest(voteRequest)
return
}
// candidate does not step down
wrappedVoteRequest.responseChan <- &RequestVoteResponse{
Term: proto.Int64(s.currentTerm),
VoteGranted: proto.Bool(false),
}
case wrappedAppendRequest := <-s.getAppendEntriesChan:
appendRequest := wrappedAppendRequest.request
if appendRequest.GetTerm() >= s.currentTerm {
s.stepDown()
wrappedAppendRequest.responseChan <- s.handleAppendEntriesRequest(appendRequest)
return
}
wrappedAppendRequest.responseChan <- &AppendEntriesResponse{
Term: proto.Int64(s.currentTerm),
Success: proto.Bool(false),
}
case wrappedCommand := <-s.commandChan:
wrappedCommand.errChan <- fmt.Errorf("leader doesn't exist")
wrappedCommand.valChan <- nil
case wrappedSetConfig := <-s.getConfigChan:
wrappedSetConfig.errChan <- fmt.Errorf("leader doesn't exist")
}
}
}
func (s *Server) leaderloop() {
theOtherNdoes := s.theOtherNodes()
s.deposeChan = make(chan int64, len(theOtherNdoes))
// initialize nextIndex
for id := range theOtherNdoes {
s.nextIndex.set(id, s.log.lastLogIndex())
}
hearbeatChan := make(chan struct{})
heartBeat := time.NewTicker(hearbeatInterval())
defer heartBeat.Stop()
go func() {
for _ = range heartBeat.C {
// s.broadcastHeartbeats()
hearbeatChan <- struct{}{}
}
}()
for s.getState() == leader {
select {
case wrappedCommand := <-s.commandChan:
newEntry, err := s.log.newLogEntry(s.currentTerm, wrappedCommand.command)
if err != nil {
wrappedCommand.errChan <- err
}
s.log.appendEntry(newEntry)
err = s.sendAppendEntries()
if err != nil {
logger.Println("append entries error: ", err.Error())
wrappedCommand.errChan <- err
continue
}
val, err := s.log.setCommitIndex(s.log.lastLogIndex(), s.context)
wrappedCommand.errChan <- err
wrappedCommand.valChan <- val
case wrappedSetConfig := <-s.getConfigChan:
go func() {
wrappedSetConfig.errChan <- s.processSetConfig(&wrappedSetConfig)
}()
case wrappedVoteRequest := <-s.getVoteRequestChan:
voteRequest := wrappedVoteRequest.request
if voteRequest.GetTerm() > s.currentTerm {
s.stepDown()
}
// now leader stepped down and become follower
wrappedVoteRequest.responseChan <- s.handleVoteRequest(voteRequest)
case wrappedAppendRequest := <-s.getAppendEntriesChan:
appendRequest := wrappedAppendRequest.request
if appendRequest.GetTerm() > s.currentTerm {
s.stepDown()
}
// now leader stepped down and become follower
wrappedAppendRequest.responseChan <- s.handleAppendEntriesRequest(appendRequest)
case responseTerm := <-s.deposeChan:
s.stepDown()
s.updateCurrentTerm(responseTerm)
case <-hearbeatChan:
s.broadcastHeartbeats()
}
}
}
// TODO: refactor this
func (s *Server) broadcastHeartbeats() {
theOtherNdoes := s.theOtherNodes()
for id, node := range theOtherNdoes {
go func(id int32, node Node) {
response, err := s.sendAppendRequestTo(id, node)
if err != nil {
logger.Println(err.Error())
}
if rTerm := response.GetTerm(); rTerm > s.currentTerm {
s.deposeChan <- rTerm
}
if response.GetSuccess() {
s.nextIndex.set(id, s.log.lastLogIndex())
} else {
s.nextIndex.dec(id)
}
}(id, node)
}
}
func (s *Server) sendAppendRequestTo(id int32, node Node) (*AppendEntriesResponse, error) {
appendRequest := AppendEntriesRequest{
LeaderID: proto.Int32(s.id),
Term: proto.Int64(s.currentTerm),
PrevLogIndex: proto.Int64(s.log.prevLogIndex(s.nextIndex.get(id))),
PrevLogTerm: proto.Int64(s.log.prevLogTerm(s.nextIndex.get(id))),
CommitIndex: proto.Int64(s.log.commitIndex),
Entries: s.log.entriesAfer(s.nextIndex.get(id)),
}
return node.rpcAppendEntries(s, &appendRequest)
}
func (s *Server) sendAppendEntries() error {
theOtherNodes := s.theOtherNodes()
oldNode := s.config.cOldNode
newNode := s.config.cNewNode
successChan := make(chan int32, len(theOtherNodes))
errChan := make(chan error, len(theOtherNodes))
go func() { successChan <- s.id }()
for id, node := range theOtherNodes {
go func(id int32, node Node) {
for {
reponse, err := s.sendAppendRequestTo(id, node)
if err != nil {
continue
}
if rTerm := reponse.GetTerm(); rTerm > s.currentTerm {
errChan <- errDepose
s.deposeChan <- rTerm // leader stale
return
}
if !reponse.GetSuccess() {
s.nextIndex.dec(id)
logger.Printf("append entries not succeedded, node %d dec ni to %d\n",
id, s.nextIndex.get(id))
continue
}
logger.Printf("node %d append entries succeedded\n", id)
s.nextIndex.set(id, s.log.lastLogIndex())
successChan <- id
return
}
}(id, node)
}
oldSuccessCount := 0
newSuccessCount := 0
go func() {
acceptCount := 0
for {
select {
case id := <-successChan:
acceptCount++
if oldNode[id] != nil {
oldSuccessCount++
}
if newNode[id] != nil {
newSuccessCount++
}
if oldSuccessCount >= len(oldNode)/2+1 {
// PAPER: Agreement(for elections and entry commitment)requires
// separate majorities from both the old and new
// configurations.
if s.config.getState() == cOldNew {
if newSuccessCount >= len(newNode)/2+1 {
errChan <- nil
}
} else {
errChan <- nil
}
}
if acceptCount >= len(theOtherNodes) {
return // all nodes have accept request
}
}
}
}()
return <-errChan
}
func (s *Server) theOtherNodes() nodeMap {
nMap := make(nodeMap)
for id, node := range s.config.cNewNode {
if id == s.id {
continue
}
nMap[id] = node
}
for id, node := range s.config.cOldNode {
if id == s.id {
continue
}
nMap[id] = node
}
return nMap
}
func (s *Server) updateCurrentTerm(term int64) {
s.currentTerm = term
s.voted = false // follower vote exactly once on one specific term
}
func (s *Server) handleAppendEntriesRequest(appendRequest *AppendEntriesRequest) *AppendEntriesResponse {
resp := &AppendEntriesResponse{
Term: proto.Int64(s.currentTerm),
Success: proto.Bool(false)}
if s.currentTerm > appendRequest.GetTerm() {
return resp
}
if s.currentTerm < appendRequest.GetTerm() {
s.updateCurrentTerm(appendRequest.GetTerm())
}
if s.log.contains(appendRequest.GetPrevLogIndex(), appendRequest.GetPrevLogTerm()) {
s.log.appendEntries(s, appendRequest.GetPrevLogIndex(), appendRequest.GetEntries())
resp.Success = proto.Bool(true)
if s.leader == noleader {
s.leader = appendRequest.GetLeaderID()
}
if appendRequest.GetCommitIndex() > s.log.commitIndex {
s.log.setCommitIndex(appendRequest.GetCommitIndex(), s.context)
}
s.resetElectionTimeout()
}
return resp
}
func (s *Server) handleVoteRequest(voteRequest *RequestVoteRequest) *RequestVoteResponse {
resp := &RequestVoteResponse{
Term: proto.Int64(s.currentTerm),
VoteGranted: proto.Bool(false),
}
if s.voted == true { // follower vote exactly once on one specific term
return resp
} else if s.currentTerm >= voteRequest.GetTerm() {
return resp
} else if voteRequest.GetLastLogTerm() >= s.log.lastLogTerm() && voteRequest.GetLastLogIndex() >= s.log.lastLogIndex() {
// candidate's log is at leat up-to-date as Server s.
// logger.Printf("node %d get vote")
resp.VoteGranted = proto.Bool(true)
s.voted = true
}
return resp
}
func (s *Server) voteForItself() {
responseProto := &RequestVoteResponse{
VoteGranted: proto.Bool(true),
}
s.getVoteResponseChan <- wrappedVoteResponse{
id: s.id,
RequestVoteResponse: responseProto,
}
}
func (s *Server) requestVotes() {
theOtherNodes := s.theOtherNodes()
// if re-relect, reset the voteChan
s.getVoteResponseChan = make(chan wrappedVoteResponse, len(theOtherNodes))
for id, node := range theOtherNodes {
go func(id int32, node Node) { // send vote request simultaneously
for { // not responded, keep trying
logger.Printf("node %d send to node %d\n", s.id, id)
pb := &RequestVoteRequest{
CandidateID: proto.Int32(s.id),
Term: proto.Int64(s.currentTerm),
LastLogIndex: proto.Int64(s.log.lastLogIndex()),
LastLogTerm: proto.Int64(s.log.lastLogTerm()),
}
responseProto, err := node.rpcRequestVote(s, pb)
if err == nil {
if responseProto.GetVoteGranted() {
logger.Printf("node %d got node %d granted\n", s.id, id)
} else {
logger.Printf("node %d got node %d reject\n", s.id, id)
}
s.getVoteResponseChan <- wrappedVoteResponse{
id: id,
RequestVoteResponse: responseProto,
}
return
}
logger.Println("vote response error:", err.Error())
}
}(id, node)
}
}
func (s *Server) electionPass() bool {
if s.config.getState() == cOld {
votesCount := 0
for id := range s.config.cOldNode {
if s.nodesVoteGranted[id] == true {
votesCount++
}
}
if votesCount >= len(s.config.cOldNode)/2+1 {
return true
}
} else if s.config.getState() == cOldNew { // candidate must be approved by Cold and Cold,new.
voteCountOld := 0
voteCountOldNew := 0
for id := range s.config.cOldNode {
if s.nodesVoteGranted[id] == true {
voteCountOld++
}
}
for id := range s.config.cNewNode {
if s.nodesVoteGranted[id] == true {
voteCountOldNew++
}
}
if voteCountOld >= len(s.config.cOldNode)/2+1 && voteCountOldNew >= len(s.config.cNewNode)/2+1 {
// if pass both old and new config
return true
}
} else {
panic("lilraft: config state unknown")
}
return false
}
// candidate or leader will call this if they detect legit leader
func (s *Server) stepDown() {
s.setState(follower)
s.leader = noleader
logger.Printf("node %d step down to follower\n", s.id)
}
func (s *Server) processSetConfig(wrappedSetConfig *wrappedSetConfig) error {
nodes := wrappedSetConfig.nodes
cOldNewEntry, err := s.log.newConfigEntry(s.currentTerm, nodes, cOldNewStr)
if err != nil {
return err
}
s.config.setState(cOldNew)
s.config.cNewNode = makeNodeMap(nodes...)
for _, node := range nodes {
if _, ok := s.nextIndex.m[node.id()]; !ok {
s.nextIndex.set(node.id(), s.log.lastLogIndex())
}
}
s.log.appendEntry(cOldNewEntry)
err = s.sendAppendEntries()
if err != nil {
logger.Println("append Coldnew config entries error: ", err.Error())
return err
}
if _, err := s.log.setCommitIndex(s.log.lastLogIndex(), s.context); err != nil {
return err
}
// now cOldNew has send to majorities of c_new and cOld and commit ColdNew
cNewEntry, err := s.log.newConfigEntry(s.currentTerm, nodes, cNewStr)
if err != nil {
return err
}
s.log.appendEntry(cNewEntry)
s.config.setState(cOld)
s.config.cOldNode = makeNodeMap(nodes...)
s.config.cNewNode = nil
err = s.sendAppendEntries()
if err != nil {
logger.Println("append Cnew config entries error: ", err.Error())
return err
}
if _, err := s.log.setCommitIndex(s.log.lastLogIndex(), s.context); err != nil {
return err
}
// now c_new has been commited
if s.config.cOldNode[s.id] == nil {
s.setState(stopped)
}
return nil
}