/
debug_force.go
148 lines (134 loc) · 3.97 KB
/
debug_force.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
package scheduler
import (
"bytes"
"sort"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
"github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
)
type debugForceElectState struct {
params map[signature.PublicKey]scheduler.ForceElectCommitteeRole
elected map[signature.PublicKey]bool
}
func (app *schedulerApplication) debugForceElect(
ctx *api.Context,
schedulerParameters *scheduler.ConsensusParameters,
rt *registry.Runtime,
kind scheduler.CommitteeKind,
role scheduler.Role,
nodeList []*node.Node,
wantedNodes int,
) (bool, []*scheduler.CommitteeNode, *debugForceElectState) {
elected := make([]*scheduler.CommitteeNode, 0, wantedNodes)
if !flags.DebugDontBlameOasis() || schedulerParameters.DebugForceElect == nil {
return true, elected, nil
}
var (
toForce []signature.PublicKey
state = &debugForceElectState{
params: make(map[signature.PublicKey]scheduler.ForceElectCommitteeRole),
elected: make(map[signature.PublicKey]bool),
}
)
for nodeID, ri := range schedulerParameters.DebugForceElect[rt.ID] {
if kind == ri.Kind && ri.HasRole(role) {
toForce = append(toForce, nodeID)
state.params[nodeID] = *ri
}
}
sort.SliceStable(toForce, func(i, j int) bool {
a, b := toForce[i], toForce[j]
return bytes.Compare(a[:], b[:]) < 0
})
forceLoop:
for _, nodeID := range toForce {
ctx.Logger().Debug("attempting to force-elect node",
"runtime", rt.ID,
"node", nodeID,
"role", role,
)
if len(elected) >= wantedNodes {
break
}
// Ensure the node is currently registered and eligible.
for _, v := range nodeList {
ctx.Logger().Debug("checking to see if this is the force elected node",
"iter_id", v.ID,
"node", nodeID,
)
if v.ID.Equal(nodeID) {
// And force it into the committee.
elected = append(elected, &scheduler.CommitteeNode{
Role: role,
PublicKey: nodeID,
})
state.elected[nodeID] = true
ctx.Logger().Debug("force elected node to committee",
"runtime", rt.ID,
"node", nodeID,
"role", role,
)
continue forceLoop
}
}
}
if len(elected) != len(toForce) {
ctx.Logger().Error("available nodes can't fulfill forced committee members",
"kind", kind,
"runtime_id", rt.ID,
"nr_nodes", len(nodeList),
"mandatory_nodes", len(toForce),
)
return false, nil, nil
}
return true, elected, state
}
func (app *schedulerApplication) debugForceRoles(
ctx *api.Context,
state *debugForceElectState,
elected []*scheduler.CommitteeNode,
role scheduler.Role,
) (bool, []*scheduler.CommitteeNode) {
if !flags.DebugDontBlameOasis() || state == nil || len(state.elected) == 0 || role != scheduler.RoleWorker {
return true, elected
}
var (
mustBeScheduler *scheduler.CommitteeNode
mustNotBeScheduler []*scheduler.CommitteeNode
mayBeAny []*scheduler.CommitteeNode
)
for i, n := range elected {
committeeNode := elected[i]
if ri, ok := state.params[n.PublicKey]; ok {
if ri.IsScheduler {
if mustBeScheduler != nil {
ctx.Logger().Error("already have a forced scheduler",
"existing", mustBeScheduler.PublicKey,
"new", n.PublicKey,
)
return false, nil
}
mustBeScheduler = committeeNode
} else {
mustNotBeScheduler = append(mustNotBeScheduler, committeeNode)
}
} else {
mayBeAny = append(mayBeAny, committeeNode)
}
}
if mustBeScheduler == nil {
if len(mayBeAny) == 0 && len(mustNotBeScheduler) > 0 {
ctx.Logger().Error("can't fulfil not committee scheduler requirements")
return false, nil
}
mustBeScheduler = mayBeAny[0]
mayBeAny = mayBeAny[1:]
}
elected = []*scheduler.CommitteeNode{mustBeScheduler}
elected = append(elected, mustNotBeScheduler...)
elected = append(elected, mayBeAny...)
return true, elected
}