-
Notifications
You must be signed in to change notification settings - Fork 1
/
track_raft_protos.go
111 lines (96 loc) · 3 KB
/
track_raft_protos.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
// Copyright 2016 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.
package storage
import (
"fmt"
"reflect"
"runtime"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
)
func funcName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
// TrackRaftProtos instruments proto marshalling to track protos which are
// marshalled downstream of raft. It returns a function that removes the
// instrumentation and returns the list of downstream-of-raft protos.
func TrackRaftProtos() func() []reflect.Type {
// Grab the name of the function that roots all raft operations.
processRaftFunc := funcName((*Replica).processRaftCommand)
// We only need to track protos that could cause replica divergence
// by being written to disk downstream of raft.
whitelist := []string{
// Some raft operations trigger gossip, but we don't require
// strict consistency there.
funcName((*gossip.Gossip).AddInfoProto),
// Replica destroyed errors are written to disk, but they are
// deliberately per-replica values.
funcName((replicaStateLoader).setReplicaDestroyedError),
}
belowRaftProtos := struct {
syncutil.Mutex
inner map[reflect.Type]struct{}
}{
inner: make(map[reflect.Type]struct{}),
}
protoutil.Interceptor = func(pb proto.Message) {
t := reflect.TypeOf(pb)
belowRaftProtos.Lock()
_, ok := belowRaftProtos.inner[t]
belowRaftProtos.Unlock()
if ok {
return
}
var pcs [100]uintptr
if numCallers := runtime.Callers(0, pcs[:]); numCallers == len(pcs) {
panic(fmt.Sprintf("number of callers %d might have exceeded slice size %d", numCallers, len(pcs)))
}
frames := runtime.CallersFrames(pcs[:])
for {
f, more := frames.Next()
whitelisted := false
for _, s := range whitelist {
if strings.Contains(f.Function, s) {
whitelisted = true
break
}
}
if whitelisted {
break
}
if strings.Contains(f.Function, processRaftFunc) {
belowRaftProtos.Lock()
belowRaftProtos.inner[t] = struct{}{}
belowRaftProtos.Unlock()
break
}
if !more {
break
}
}
}
return func() []reflect.Type {
protoutil.Interceptor = func(_ proto.Message) {}
belowRaftProtos.Lock()
types := make([]reflect.Type, 0, len(belowRaftProtos.inner))
for t := range belowRaftProtos.inner {
types = append(types, t)
}
belowRaftProtos.Unlock()
return types
}
}