Skip to content

Commit

Permalink
Fix lint check failures
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuazh-x committed Nov 15, 2023
1 parent ce26bb6 commit c83c377
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
26 changes: 16 additions & 10 deletions server/etcdserver/api/rafthttp/faulty_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import (
"container/heap"
"fmt"
"io"
"math"
"math/rand"
"strconv"
"strings"
"time"

"go.uber.org/zap"

"go.etcd.io/etcd/client/pkg/v3/types"
"go.etcd.io/raft/v3/raftpb"
"go.uber.org/zap"
)

type FaultType int

const (
FaultTypeNone FaultType = 0
FaultTypeDropped = 0x02
FaultTypeDuplicated = 0x04
FaultTypeDelayed = 0x08
FaultTypeReordered = 0x10
FaultTypeBlocked = 0x12
FaultTypeDropped FaultType = 0x02
FaultTypeDuplicated FaultType = 0x04
FaultTypeDelayed FaultType = 0x08
FaultTypeReordered FaultType = 0x10
FaultTypeBlocked FaultType = 0x12
)

type FaultyNetworkFaultConfig struct {
Expand Down Expand Up @@ -53,7 +55,7 @@ type FaultStat struct {
Reordered uint64 `json:"reordered,omitempty"`
}

var reportFaultStatisticsInterval time.Duration = time.Second * 10
var reportFaultStatisticsInterval = time.Second * 10

// config example: 111->222:drop=0.1,delay=0.2:1-3;456:block=2;333<->*:dup:0.2;*:dup=0.05
// explain:
Expand Down Expand Up @@ -345,17 +347,21 @@ type faultyEncoder struct {
configStr string
}

//nolint:unused // This type is used only when gofail is enabled
type wrappedCloser struct {
fe *faultyEncoder
closer io.Closer
}

//nolint:unused // This function is used only when gofail is enabled
func (wc *wrappedCloser) Close() error {
wc.fe.Close()
return wc.closer.Close()
}

// wrap the given encoder and closer so that specified faults can be applied to the messages
//
//nolint:unused // This function is used only when gofail is enabled
func wrapEncoderWithFaultyNetwork(encoder encoder, closer io.Closer, localId types.ID, perrId types.ID, lg *zap.Logger) (encoder, io.Closer) {
fe := newFaultyEncoder(encoder, localId, perrId, lg)
return fe, &wrappedCloser{fe: fe, closer: closer}
Expand Down Expand Up @@ -463,9 +469,9 @@ func (fe *faultyEncoder) getEffectiveConfig(cfg FaultyNetworkConfig) *FaultyNetw
for tr, p := range cfg {
if ((fe.localId == tr.From || tr.From == 0) || ((fe.localId == tr.To || tr.To == 0) && tr.Duplex)) &&
((fe.peerId == tr.To || tr.To == 0) || ((fe.peerId == tr.From || tr.From == 0) && tr.Duplex)) {
fc.BlockInSecond = max(fc.BlockInSecond, p.BlockInSecond)
fc.DuplicateProbability = max(fc.DuplicateProbability, p.DuplicateProbability)
fc.DropPropability = max(fc.DropPropability, p.DropPropability)
fc.BlockInSecond = math.Max(fc.BlockInSecond, p.BlockInSecond)
fc.DuplicateProbability = math.Max(fc.DuplicateProbability, p.DuplicateProbability)
fc.DropPropability = math.Max(fc.DropPropability, p.DropPropability)
if p.DelayProbability > fc.DelayProbability {
fc.DelayProbability = p.DelayProbability
fc.MinDelayInSecond = p.MinDelayInSecond
Expand Down
7 changes: 4 additions & 3 deletions server/etcdserver/api/rafthttp/faulty_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"testing"
"time"

"go.uber.org/zap"

"go.etcd.io/etcd/client/pkg/v3/types"
"go.etcd.io/raft/v3/raftpb"
"go.uber.org/zap"
)

type testEncoder struct {
Expand Down Expand Up @@ -192,15 +193,15 @@ func TestFaultyEncoderWithBlockedNetwork(t *testing.T) {
func TestFaultyEncoderWithLossyNetwork(t *testing.T) {
tr := FaultyNetworkTransport{
From: 0,
To: 1,
To: 2,
}
cfg := FaultyNetworkConfig{
tr: FaultyNetworkFaultConfig{
DropPropability: 0.2,
},
}

result := runWithConfig(t, cfg, 20, 0, 1) // 99% confidence level
result := runWithConfig(t, cfg, 20, 0, 2) // 99% confidence level
if !result.hasDropped() {
t.Error("some messages shall be dropped")
}
Expand Down
20 changes: 11 additions & 9 deletions tests/robustness/failpoint/message_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package failpoint
import (
"context"
"fmt"
"math"
"math/rand"
"testing"
"time"

"go.uber.org/zap"

"go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp"
"go.etcd.io/etcd/tests/v3/framework/e2e"
"go.uber.org/zap"
)

var (
BlockedTransition Failpoint = makeMessageTransitionFailpoint("traffic-jam", withTrafficJam("", "", true, time.Millisecond*100))
LossyTransition Failpoint = makeMessageTransitionFailpoint("lossy", withLossyTransition("", "", true, 0.2))
RedundantTransition Failpoint = makeMessageTransitionFailpoint("redundant", withRedundantTransition("", "", true, 0.2), withLaggyTransition("", "", true, 0.2, time.Millisecond*100, time.Millisecond*500))
BlockedTransition = makeMessageTransitionFailpoint("traffic-jam", withTrafficJam("", "", true, time.Millisecond*100))
LossyTransition = makeMessageTransitionFailpoint("lossy", withLossyTransition("", "", true, 0.2))
RedundantTransition = makeMessageTransitionFailpoint("redundant", withRedundantTransition("", "", true, 0.2), withLaggyTransition("", "", true, 0.2, time.Millisecond*100, time.Millisecond*500))
// laggy transition may also introduce message reordering
LaggyTransition Failpoint = makeMessageTransitionFailpoint("laggy", withLaggyTransition("", "", true, 0.2, time.Millisecond*100, time.Millisecond*500))
LaggyTransition = makeMessageTransitionFailpoint("laggy", withLaggyTransition("", "", true, 0.2, time.Millisecond*100, time.Millisecond*500))
)

// define transport between named nodes.
Expand Down Expand Up @@ -141,9 +143,9 @@ func withFaultConfig(from string, to string, duplex bool, fc rafthttp.FaultyNetw
found := false
for _, tr := range validTr {
if f, ok := (*cfg)[tr]; ok {
f.BlockInSecond = max(f.BlockInSecond, fc.BlockInSecond)
f.DropPropability = max(f.DropPropability, fc.DropPropability)
f.DuplicateProbability = max(f.DuplicateProbability, fc.DuplicateProbability)
f.BlockInSecond = math.Max(f.BlockInSecond, fc.BlockInSecond)
f.DropPropability = math.Max(f.DropPropability, fc.DropPropability)
f.DuplicateProbability = math.Max(f.DuplicateProbability, fc.DuplicateProbability)
if f.DelayProbability < fc.DelayProbability {
f.DelayProbability = fc.DelayProbability
f.MinDelayInSecond = fc.MinDelayInSecond
Expand Down Expand Up @@ -179,7 +181,7 @@ func withRedundantTransition(from string, to string, duplex bool, p float64) mes
func withLaggyTransition(from string, to string, duplex bool, p float64, minDelay time.Duration, maxDelay time.Duration) messageTransitionFailpointOption {
return withFaultConfig(from, to, duplex, rafthttp.FaultyNetworkFaultConfig{
DelayProbability: p,
MinDelayInSecond: maxDelay.Seconds(),
MinDelayInSecond: minDelay.Seconds(),
MaxDelayInSecond: maxDelay.Seconds(),
})
}

0 comments on commit c83c377

Please sign in to comment.