From 9a26d59dd4670a366186f0872ba74b8cfcbe76ce Mon Sep 17 00:00:00 2001 From: Alec Jen Date: Mon, 1 Mar 2021 13:55:29 -0800 Subject: [PATCH] Allow buffered applyCh (#445) * Allow buffered applyCh * comment --- api.go | 8 +++++++- config.go | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 492859e37..76275fd14 100644 --- a/api.go +++ b/api.go @@ -495,10 +495,16 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna return nil, fmt.Errorf("when running with ProtocolVersion < 3, LocalID must be set to the network address") } + // Buffer applyCh to MaxAppendEntries if the option is enabled + applyCh := make(chan *logFuture) + if conf.BatchApplyCh { + applyCh = make(chan *logFuture, conf.MaxAppendEntries) + } + // Create Raft struct. r := &Raft{ protocolVersion: protocolVersion, - applyCh: make(chan *logFuture), + applyCh: applyCh, conf: *conf, fsm: fsm, fsmMutateCh: make(chan interface{}, 128), diff --git a/config.go b/config.go index 5ddd88453..7abbf5d97 100644 --- a/config.go +++ b/config.go @@ -151,6 +151,13 @@ type Config struct { // an inconsistent log. MaxAppendEntries int + // BatchApplyCh indicates whether we should buffer applyCh + // to size MaxAppendEntries. This enables batch log commitment, + // but breaks the timeout guarantee on Apply. Specifically, + // a log can be added to the applyCh buffer but not actually be + // processed until after the specified timeout. + BatchApplyCh bool + // If we are a member of a cluster, and RemovePeer is invoked for the // local node, then we forget all peers and transition into the follower state. // If ShutdownOnRemove is is set, we additional shutdown Raft. Otherwise,