File: options.go

package info (click to toggle)
golang-github-canonicalltd-raft-test 0.0~git20180628.c3345b5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 284 kB
  • sloc: makefile: 2
file content (105 lines) | stat: -rw-r--r-- 3,127 bytes parent folder | download
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
// Copyright 2017 Canonical Ltd.
//
// 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.

package rafttest

import (
	"io/ioutil"
	"log"
	"time"

	"github.com/hashicorp/raft"
)

// Config sets a hook for tweaking the raft configuration of individual nodes.
func Config(f func(int, *raft.Config)) Option {
	return func(nodes []*dependencies) {
		for i, node := range nodes {
			f(i, node.Conf)
		}
	}
}

// LogStore can be used to create custom log stores.
//
// The given function takes a node index as argument and returns the LogStore
// that the node should use.
func LogStore(factory func(int) raft.LogStore) Option {
	return func(nodes []*dependencies) {
		for i, node := range nodes {
			node.Logs = factory(i)
		}
	}
}

// Transport can be used to create custom transports.
//
// The given function takes a node index as argument and returns the Transport
// that the node should use.
//
// If the transports returned by the factory do not implement
// LoopbackTransport, the Disconnect API won't work.
func Transport(factory func(int) raft.Transport) Option {
	return func(nodes []*dependencies) {
		for i, node := range nodes {
			node.Trans = factory(i)
		}
	}
}

// Latency is a convenience around Config that scales the values of the various
// raft timeouts that would be set by default by Cluster.
//
// This option is orthogonal to the GO_RAFT_TEST_LATENCY environment
// variable. If this option is used and GO_RAFT_TEST_LATENCY is set, they will
// compound. E.g. passing a factor of 2.0 to this option and setting
// GO_RAFT_TEST_LATENCY to 3.0 will have the net effect that default timeouts
// are scaled by a factor of 6.0.
func Latency(factor float64) Option {
	return Config(func(i int, config *raft.Config) {
		timeouts := []*time.Duration{
			&config.HeartbeatTimeout,
			&config.ElectionTimeout,
			&config.LeaderLeaseTimeout,
			&config.CommitTimeout,
		}
		for _, timeout := range timeouts {
			*timeout = scaleDuration(*timeout, factor)
		}
	})
}

// DiscardLogger is a convenience around Config that sets the output stream of
// raft's logger to ioutil.Discard.
func DiscardLogger() Option {
	return Config(func(i int, config *raft.Config) {
		config.Logger = log.New(ioutil.Discard, "", 0)
	})
}

// Servers can be used to indicate which nodes should be initially part of the
// created cluster.
//
// If this option is not used, the default is to have all nodes be part of the
// cluster.
func Servers(indexes ...int) Option {
	return func(nodes []*dependencies) {
		for _, node := range nodes {
			node.Voter = false
		}
		for _, index := range indexes {
			nodes[index].Voter = true
		}
	}
}