-
Notifications
You must be signed in to change notification settings - Fork 1
/
raft.go
215 lines (190 loc) · 6.6 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
// Copyright 2015 The Cockroach Authors.
//
// 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. See the AUTHORS file
// for names of contributors.
package storage
import (
"bytes"
"fmt"
"strings"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/coreos/etcd/raft"
"github.com/coreos/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{}) {
log.FatalfDepth(r.ctx, 1, "", v...)
}
func (r *raftLogger) Fatalf(format string, v ...interface{}) {
log.FatalfDepth(r.ctx, 1, format, v...)
}
func (r *raftLogger) Panic(v ...interface{}) {
s := fmt.Sprint(v...)
log.ErrorfDepth(r.ctx, 1, s)
if is14231Error(s) {
log.Shout(r.ctx, log.Severity_ERROR, is14231HelpMessage)
}
panic(s)
}
func (r *raftLogger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
log.ErrorfDepth(r.ctx, 1, s)
if is14231Error(s) {
log.Shout(r.ctx, log.Severity_ERROR, is14231HelpMessage)
}
panic(fmt.Sprintf(format, v...))
}
// Returns whether the provided error message matches the error observed in
// issue #14231. Such errors are likely to be the result of wiping a store's
// data directory and bringing it back online without join flags but with the
// same address, node ID, and store ID, and join flags. While we should prevent
// these errors in our next release, for now the best we can do is help users
// understand them.
//
// TODO(#14231): Remove this method once the issue is fixed (hopefully in 1.2).
func is14231Error(s string) bool {
return strings.Contains(s, "is out of range [lastIndex(") &&
strings.Contains(s, "Was the raft log corrupted, truncated, or lost?")
}
// TODO(#14231): Remove this once the issue is fixed (hopefully in 1.2).
const is14231HelpMessage = "Server crashing due to missing data. Was the server restarted with a\n" +
"different store directory than before? A --join parameter must be specified\n" +
"in order to make restarts with a new store directory safe. Please try again\n" +
"with a new directory and a valid --join flag. If this server was started with\n" +
"its old data or with a valid --join parameter and you are still seeing this,\n" +
"please report an issue at https://github.com/cockroachdb/cockroach/issues/new"
func logRaftReady(ctx context.Context, ready raft.Ready) {
if log.V(5) {
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\n%s", 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")
if m.RejectHint != 0 {
fmt.Fprintf(&buf, "(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))
}
var _ security.RequestWithUser = &RaftMessageRequest{}
// GetUser implements security.RequestWithUser.
// Raft messages are always sent by the node user.
func (*RaftMessageRequest) GetUser() string {
return security.NodeUser
}