-
Notifications
You must be signed in to change notification settings - Fork 542
/
noop.go
158 lines (135 loc) · 3.84 KB
/
noop.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
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// 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 transport
import (
"context"
"errors"
"sync"
"sync/atomic"
"github.com/lni/dragonboat/config"
"github.com/lni/dragonboat/raftio"
"github.com/lni/dragonboat/raftpb"
)
var (
// NOOPRaftName is the module name for the NOOP transport module.
NOOPRaftName = "noop-test-transport"
// ErrRequestedToFail is the error used to indicate that the error is
// requested.
ErrRequestedToFail = errors.New("requested to returned error")
)
type noopRequest struct {
mu sync.Mutex
fail bool
}
func (r *noopRequest) SetToFail(v bool) {
r.mu.Lock()
defer r.mu.Unlock()
r.fail = v
}
func (r *noopRequest) Fail() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.fail
}
type noopConnectRequest struct {
mu sync.Mutex
fail bool
}
func (r *noopConnectRequest) SetToFail(v bool) {
r.mu.Lock()
defer r.mu.Unlock()
r.fail = v
}
func (r *noopConnectRequest) Fail() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.fail
}
// NOOPConnection is the connection used to exchange messages between node hosts.
type NOOPConnection struct {
req *noopRequest
}
// Close closes the NOOPConnection instance.
func (c *NOOPConnection) Close() {
}
// SendMessageBatch return ErrRequestedToFail when requested.
func (c *NOOPConnection) SendMessageBatch(batch raftpb.MessageBatch) error {
if c.req.Fail() {
return ErrRequestedToFail
}
return nil
}
// NOOPSnapshotConnection is the connection used to send snapshots.
type NOOPSnapshotConnection struct {
req *noopRequest
}
// Close closes the NOOPSnapshotConnection.
func (c *NOOPSnapshotConnection) Close() {
}
// SendSnapshotChunk returns ErrRequestedToFail when requested.
func (c *NOOPSnapshotConnection) SendSnapshotChunk(chunk raftpb.SnapshotChunk) error {
if c.req.Fail() {
return ErrRequestedToFail
}
return nil
}
// NOOPTransport is a transport module for testing purposes. It does not
// actually has the ability to exchange messages or snapshots between
// nodehosts.
type NOOPTransport struct {
connected uint64
tryConnect uint64
req *noopRequest
connReq *noopConnectRequest
}
// NewNOOPTransport creates a new NOOPTransport instance.
func NewNOOPTransport(nhConfig config.NodeHostConfig,
requestHandler raftio.RequestHandler,
sinkFactory raftio.ChunkSinkFactory) raftio.IRaftRPC {
return &NOOPTransport{
req: &noopRequest{},
connReq: &noopConnectRequest{},
}
}
// Start starts the NOOPTransport instance.
func (g *NOOPTransport) Start() error {
return nil
}
// Stop stops the NOOPTransport instance.
func (g *NOOPTransport) Stop() {
}
// GetConnection returns a connection.
func (g *NOOPTransport) GetConnection(ctx context.Context,
target string) (raftio.IConnection, error) {
atomic.AddUint64(&g.tryConnect, 1)
if g.connReq.Fail() {
return nil, ErrRequestedToFail
}
atomic.AddUint64(&g.connected, 1)
return &NOOPConnection{req: g.req}, nil
}
// GetSnapshotConnection returns a snapshot connection.
func (g *NOOPTransport) GetSnapshotConnection(ctx context.Context,
target string) (raftio.ISnapshotConnection, error) {
atomic.AddUint64(&g.tryConnect, 1)
if g.connReq.Fail() {
return nil, ErrRequestedToFail
}
atomic.AddUint64(&g.connected, 1)
return &NOOPSnapshotConnection{req: g.req}, nil
}
// Name returns the module name.
func (g *NOOPTransport) Name() string {
return NOOPRaftName
}