-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathraft.go
267 lines (239 loc) · 7.19 KB
/
raft.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
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package storage
import (
"bytes"
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/storage/storagebase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"go.etcd.io/etcd/raft"
"go.etcd.io/etcd/raft/raftpb"
)
// init installs an adapter to use clog for log messages from raft which
// don't belong to any range.
func init() {
raft.SetLogger(&raftLogger{ctx: context.Background()})
}
// *clogLogger implements the raft.Logger interface. Note that all methods
// must be defined on the pointer type rather than the value type because
// (at least in the go 1.4 compiler), methods on a value type called through
// an interface pointer go through an additional layer of indirection that
// appears on the stack, and would make all our stack frame offsets incorrect.
//
// Raft is fairly verbose at the "info" level, so we map "info" messages to
// clog.V(1) and "debug" messages to clog.V(2).
//
// This file is named raft.go instead of something like logger.go because this
// file's name is used to determine the vmodule parameter: --vmodule=raft=1
type raftLogger struct {
ctx context.Context
}
func (r *raftLogger) Debug(v ...interface{}) {
if log.V(3) {
log.InfofDepth(r.ctx, 1, "", v...)
}
}
func (r *raftLogger) Debugf(format string, v ...interface{}) {
if log.V(3) {
log.InfofDepth(r.ctx, 1, format, v...)
}
}
func (r *raftLogger) Info(v ...interface{}) {
if log.V(2) {
log.InfofDepth(r.ctx, 1, "", v...)
}
}
func (r *raftLogger) Infof(format string, v ...interface{}) {
if log.V(2) {
log.InfofDepth(r.ctx, 1, format, v...)
}
}
func (r *raftLogger) Warning(v ...interface{}) {
log.WarningfDepth(r.ctx, 1, "", v...)
}
func (r *raftLogger) Warningf(format string, v ...interface{}) {
log.WarningfDepth(r.ctx, 1, format, v...)
}
func (r *raftLogger) Error(v ...interface{}) {
log.ErrorfDepth(r.ctx, 1, "", v...)
}
func (r *raftLogger) Errorf(format string, v ...interface{}) {
log.ErrorfDepth(r.ctx, 1, format, v...)
}
func (r *raftLogger) Fatal(v ...interface{}) {
wrapNumbersAsSafe(v)
log.FatalfDepth(r.ctx, 1, "", v...)
}
func (r *raftLogger) Fatalf(format string, v ...interface{}) {
wrapNumbersAsSafe(v)
log.FatalfDepth(r.ctx, 1, format, v...)
}
func (r *raftLogger) Panic(v ...interface{}) {
wrapNumbersAsSafe(v)
log.FatalfDepth(r.ctx, 1, "", v...)
}
func (r *raftLogger) Panicf(format string, v ...interface{}) {
wrapNumbersAsSafe(v)
log.FatalfDepth(r.ctx, 1, format, v...)
}
func wrapNumbersAsSafe(v ...interface{}) {
for i := range v {
switch v[i].(type) {
case uint:
v[i] = log.Safe(v[i])
case uint8:
v[i] = log.Safe(v[i])
case uint16:
v[i] = log.Safe(v[i])
case uint32:
v[i] = log.Safe(v[i])
case uint64:
v[i] = log.Safe(v[i])
case int:
v[i] = log.Safe(v[i])
case int8:
v[i] = log.Safe(v[i])
case int16:
v[i] = log.Safe(v[i])
case int32:
v[i] = log.Safe(v[i])
case int64:
v[i] = log.Safe(v[i])
case float32:
v[i] = log.Safe(v[i])
case float64:
v[i] = log.Safe(v[i])
default:
}
}
}
func verboseRaftLoggingEnabled() bool {
return log.V(5)
}
func logRaftReady(ctx context.Context, ready raft.Ready) {
if !verboseRaftLoggingEnabled() {
return
}
var buf bytes.Buffer
if ready.SoftState != nil {
fmt.Fprintf(&buf, " SoftState updated: %+v\n", *ready.SoftState)
}
if !raft.IsEmptyHardState(ready.HardState) {
fmt.Fprintf(&buf, " HardState updated: %+v\n", ready.HardState)
}
for i, e := range ready.Entries {
fmt.Fprintf(&buf, " New Entry[%d]: %.200s\n",
i, raft.DescribeEntry(e, raftEntryFormatter))
}
for i, e := range ready.CommittedEntries {
fmt.Fprintf(&buf, " Committed Entry[%d]: %.200s\n",
i, raft.DescribeEntry(e, raftEntryFormatter))
}
if !raft.IsEmptySnap(ready.Snapshot) {
snap := ready.Snapshot
snap.Data = nil
fmt.Fprintf(&buf, " Snapshot updated: %v\n", snap)
}
for i, m := range ready.Messages {
fmt.Fprintf(&buf, " Outgoing Message[%d]: %.200s\n",
i, raftDescribeMessage(m, raftEntryFormatter))
}
log.Infof(ctx, "raft ready (must-sync=%t)\n%s", ready.MustSync, buf.String())
}
// This is a fork of raft.DescribeMessage with a tweak to avoid logging
// snapshot data.
func raftDescribeMessage(m raftpb.Message, f raft.EntryFormatter) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%x->%x %v Term:%d Log:%d/%d", m.From, m.To, m.Type, m.Term, m.LogTerm, m.Index)
if m.Reject {
fmt.Fprintf(&buf, " Rejected (Hint: %d)", m.RejectHint)
}
if m.Commit != 0 {
fmt.Fprintf(&buf, " Commit:%d", m.Commit)
}
if len(m.Entries) > 0 {
fmt.Fprintf(&buf, " Entries:[")
for i, e := range m.Entries {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(raft.DescribeEntry(e, f))
}
fmt.Fprintf(&buf, "]")
}
if !raft.IsEmptySnap(m.Snapshot) {
snap := m.Snapshot
snap.Data = nil
fmt.Fprintf(&buf, " Snapshot:%v", snap)
}
return buf.String()
}
func raftEntryFormatter(data []byte) string {
if len(data) == 0 {
return "[empty]"
}
commandID, _ := DecodeRaftCommand(data)
return fmt.Sprintf("[%x] [%d]", commandID, len(data))
}
// IsPreemptive returns whether this is a preemptive snapshot or a Raft
// snapshot.
func (h *SnapshotRequest_Header) IsPreemptive() bool {
// Preemptive snapshots are addressed to replica ID 0. No other requests to
// replica ID 0 are allowed.
return h.RaftMessageRequest.ToReplica.ReplicaID == 0
}
// traceEntries records the provided event for all proposals corresponding
// to the entries contained in ents. The vmodule level for raft must be at
// least 1.
func (r *Replica) traceEntries(ents []raftpb.Entry, event string) {
if log.V(1) || r.store.TestingKnobs().TraceAllRaftEvents {
ids := extractIDs(nil, ents)
traceProposals(r, ids, event)
}
}
// traceMessageSends records the provided event for all proposals contained in
// in entries contained in msgs. The vmodule level for raft must be at
// least 1.
func (r *Replica) traceMessageSends(msgs []raftpb.Message, event string) {
if log.V(1) || r.store.TestingKnobs().TraceAllRaftEvents {
var ids []storagebase.CmdIDKey
for _, m := range msgs {
ids = extractIDs(ids, m.Entries)
}
traceProposals(r, ids, event)
}
}
// extractIDs decodes and appends each of the ids corresponding to the entries
// in ents to ids and returns the result.
func extractIDs(ids []storagebase.CmdIDKey, ents []raftpb.Entry) []storagebase.CmdIDKey {
for _, e := range ents {
if e.Type == raftpb.EntryNormal && len(e.Data) > 0 {
id, _ := DecodeRaftCommand(e.Data)
ids = append(ids, id)
}
}
return ids
}
// traceLocalProposals logs a trace event with the provided string for each
// locally proposed command which corresponds to an id in ids.
func traceProposals(r *Replica, ids []storagebase.CmdIDKey, event string) {
ctxs := make([]context.Context, 0, len(ids))
r.mu.RLock()
for _, id := range ids {
if prop, ok := r.mu.proposals[id]; ok {
ctxs = append(ctxs, prop.ctx)
}
}
r.mu.RUnlock()
for _, ctx := range ctxs {
log.Event(ctx, event)
}
}