forked from ravendb/ravendb
/
NodeSelector.cs
273 lines (216 loc) · 9.46 KB
/
NodeSelector.cs
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
268
269
270
271
272
273
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Raven.Client.Exceptions;
using Raven.Client.Exceptions.Database;
namespace Raven.Client.Http
{
public class NodeSelector : IDisposable
{
private class NodeSelectorState
{
public readonly Topology Topology;
public readonly List<ServerNode> Nodes;
public readonly int[] Failures;
public readonly int[] FastestRecords;
public int Fastest;
public int SpeedTestMode;
public NodeSelectorState(Topology topology)
{
Topology = topology;
Nodes = topology.Nodes;
Failures = new int[topology.Nodes.Count];
FastestRecords = new int[topology.Nodes.Count];
}
}
public Topology Topology => _state.Topology;
private Timer _updateFastestNodeTimer;
private NodeSelectorState _state;
public NodeSelector(Topology topology)
{
_state = new NodeSelectorState(topology);
}
public void OnFailedRequest(int nodeIndex)
{
var state = _state;
if (nodeIndex < 0 || nodeIndex >= state.Failures.Length)
return; // probably already changed
Interlocked.Increment(ref state.Failures[nodeIndex]);
}
public bool OnUpdateTopology(Topology topology, bool forceUpdate = false)
{
if (topology == null)
return false;
if (_state.Topology.Etag >= topology.Etag && forceUpdate == false)
return false;
var state = new NodeSelectorState(topology);
Interlocked.Exchange(ref _state, state);
return true;
}
internal (int Index, ServerNode Node) GetRequestedNode(string nodeTag)
{
var state = _state;
var stateFailures = state.Failures;
var serverNodes = state.Nodes;
Debug.Assert(serverNodes.Count == stateFailures.Length, $"Expected equals {serverNodes.Count}, but got {stateFailures.Length}");
if (serverNodes.Count == 1 && serverNodes[0].ClusterTag == nodeTag) // If this is a cluster with 1 node return it without checking it's failure.
return (0, serverNodes[0]);
for (var i = 0; i < serverNodes.Count; i++)
{
if (serverNodes[i].ClusterTag == nodeTag)
{
Debug.Assert(string.IsNullOrEmpty(serverNodes[i].Url) == false, $"Expected serverNodes Url not null or empty but got: \'{serverNodes[i].Url}\'");
if (stateFailures[i] == 0)
return (i, serverNodes[i]);
throw new RequestedNodeUnavailableException($"Requested node {nodeTag} currently unavailable, please try again later.");
}
}
if (state.Nodes.Count == 0)
throw new DatabaseDoesNotExistException("There are no nodes in the topology.");
throw new RequestedNodeUnavailableException($"Could not find requested node {nodeTag}.");
}
public (int Index, ServerNode Node) GetPreferredNode()
{
var state = _state;
return GetPreferredNodeInternal(state);
}
private static (int Index, ServerNode Node) GetPreferredNodeInternal(NodeSelectorState state)
{
var stateFailures = state.Failures;
var serverNodes = state.Nodes;
var len = Math.Min(serverNodes.Count, stateFailures.Length);
for (int i = 0; i < len; i++)
{
if (stateFailures[i] == 0 && string.IsNullOrEmpty(serverNodes[i].Url) == false)
{
return (i, serverNodes[i]);
}
}
return UnlikelyEveryoneFaultedChoice(state);
}
internal (int Index, ServerNode Node, long TopologyEtag) GetPreferredNodeWithTopology()
{
var state = _state;
var preferredNode = GetPreferredNodeInternal(state);
return (preferredNode.Index, preferredNode.Node, state.Topology?.Etag??-2);
}
private static ValueTuple<int, ServerNode> UnlikelyEveryoneFaultedChoice(NodeSelectorState state)
{
// if there are all marked as failed, we'll chose the first
// one so the user will get an error (or recover :-) );
if (state.Nodes.Count == 0)
throw new DatabaseDoesNotExistException("There are no nodes in the topology at all");
return (0, state.Nodes[0]);
}
public (int Index, ServerNode Node) GetNodeBySessionId(int sessionId)
{
var state = _state;
if (state.Topology.Nodes.Count == 0)
throw new AllTopologyNodesDownException("There are no nodes in the topology at all");
var index = Math.Abs(sessionId % state.Topology.Nodes.Count);
for (int i = index; i < state.Failures.Length; i++)
{
if (state.Failures[i] == 0 && state.Nodes[i].ServerRole == ServerNode.Role.Member)
return (i, state.Nodes[i]);
}
for (int i = 0; i < index; i++)
{
if (state.Failures[i] == 0 && state.Nodes[i].ServerRole == ServerNode.Role.Member)
return (i, state.Nodes[i]);
}
return GetPreferredNode();
}
public (int Index, ServerNode Node) GetFastestNode()
{
var state = _state;
if (state.Failures[state.Fastest] == 0 && state.Nodes[state.Fastest].ServerRole == ServerNode.Role.Member)
return (state.Fastest, state.Nodes[state.Fastest]);
// if the fastest node has failures, we'll immediately schedule
// another run of finding who the fastest node is, in the meantime
// we'll just use the server preferred node or failover as usual
SwitchToSpeedTestPhase(null);
return GetPreferredNode();
}
public void RestoreNodeIndex(int nodeIndex)
{
var state = _state;
if (state.Failures.Length <= nodeIndex)
return; // the state was changed and we no longer have it?
while (true)
{
var stateFailure = state.Failures[nodeIndex];
stateFailure = Interlocked.Add(ref state.Failures[nodeIndex], -stateFailure);// zero it
if (stateFailure >= 0)
break;// someone could add failures in the meanwhile, so we are good with values higher than 0.
}
}
protected static void ThrowEmptyTopology()
{
throw new InvalidOperationException("Empty database topology, this shouldn't happen.");
}
private void SwitchToSpeedTestPhase(object _)
{
var state = _state;
if (Interlocked.CompareExchange(ref state.SpeedTestMode, 1, 0) != 0)
return;
Array.Clear(state.FastestRecords,0, state.Failures.Length);
Interlocked.Increment(ref state.SpeedTestMode);
}
public bool InSpeedTestPhase => _state.SpeedTestMode > 1;
public void RecordFastest(int index, ServerNode node)
{
var state = _state;
var stateFastest = state.FastestRecords;
// the following two checks are to verify that things didn't move
// while we were computing the fastest node, we verify that the index
// of the fastest node and the identity of the node didn't change during
// our check
if (index < 0 || index >= stateFastest.Length)
return;
if (ReferenceEquals(node, state.Nodes[index]) == false)
return;
if (Interlocked.Increment(ref stateFastest[index]) >= 10)
{
SelectFastest(state, index);
}
if (Interlocked.Increment(ref state.SpeedTestMode) <= state.Nodes.Count * 10)
return;
//too many concurrent speed tests are happening
var maxIndex = FindMaxIndex(state);
SelectFastest(state, maxIndex);
}
private static int FindMaxIndex(NodeSelectorState state)
{
var stateFastest = state.FastestRecords;
var maxIndex = 0;
var maxValue = 0;
for (var i = 0; i < stateFastest.Length; i++)
{
if (maxValue >= stateFastest[i])
continue;
maxIndex = i;
maxValue = stateFastest[i];
}
return maxIndex;
}
private void SelectFastest(NodeSelectorState state, int index)
{
state.Fastest = index;
Interlocked.Exchange(ref state.SpeedTestMode, 0);
_updateFastestNodeTimer.Change(TimeSpan.FromMinutes(1), Timeout.InfiniteTimeSpan);
}
public void ScheduleSpeedTest()
{
if (_updateFastestNodeTimer == null)
{
_updateFastestNodeTimer = new Timer(SwitchToSpeedTestPhase, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
}
SwitchToSpeedTestPhase(null);
}
public void Dispose()
{
_updateFastestNodeTimer?.Dispose();
}
}
}