forked from soabase/exhibitor
/
ConfigManager.java
356 lines (310 loc) · 12.9 KB
/
ConfigManager.java
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright 2012 Netflix, Inc.
*
* 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 com.netflix.exhibitor.core.config;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.io.Closeables;
import com.netflix.exhibitor.core.Exhibitor;
import com.netflix.exhibitor.core.activity.Activity;
import com.netflix.exhibitor.core.activity.ActivityLog;
import com.netflix.exhibitor.core.activity.QueueGroups;
import com.netflix.exhibitor.core.activity.RepeatingActivity;
import com.netflix.exhibitor.core.config.none.NoneConfigProvider;
import com.netflix.exhibitor.core.state.InstanceState;
import com.netflix.exhibitor.core.state.InstanceStateTypes;
import com.netflix.exhibitor.core.automanage.RemoteInstanceRequest;
import java.io.Closeable;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class ConfigManager implements Closeable
{
private final Exhibitor exhibitor;
private final ConfigProvider provider;
private final int maxAttempts;
private final RepeatingActivity repeatingActivity;
private final AtomicReference<LoadedInstanceConfig> config = new AtomicReference<LoadedInstanceConfig>();
private final Set<ConfigListener> configListeners = Sets.newSetFromMap(Maps.<ConfigListener, Boolean>newConcurrentMap());
private final AtomicReference<RollingConfigAdvanceAttempt> rollingConfigAdvanceAttempt = new AtomicReference<RollingConfigAdvanceAttempt>(null);
private final AtomicInteger waitingForQuorumAttempts = new AtomicInteger(0);
@VisibleForTesting
final static int DEFAULT_MAX_ATTEMPTS = 4;
public ConfigManager(Exhibitor exhibitor, ConfigProvider provider, int checkMs) throws Exception
{
this(exhibitor, provider, checkMs, DEFAULT_MAX_ATTEMPTS);
}
@VisibleForTesting
ConfigManager(Exhibitor exhibitor, ConfigProvider provider, int checkMs, int maxAttempts) throws Exception
{
this.exhibitor = exhibitor;
this.provider = provider;
this.maxAttempts = maxAttempts;
Activity activity = new Activity()
{
@Override
public void completed(boolean wasSuccessful)
{
}
@Override
public Boolean call() throws Exception
{
doWork();
return true;
}
};
repeatingActivity = new RepeatingActivity(exhibitor.getLog(), exhibitor.getActivityQueue(), QueueGroups.MAIN, activity, checkMs);
config.set(provider.loadConfig());
}
public void start() throws Exception
{
provider.start();
repeatingActivity.start();
}
@Override
public void close() throws IOException
{
repeatingActivity.close();
Closeables.closeQuietly(provider);
}
public InstanceConfig getConfig()
{
return getCollection().getConfigForThisInstance(exhibitor.getThisJVMHostname());
}
public boolean isRolling()
{
return getCollection().isRolling();
}
public RollingConfigState getRollingConfigState()
{
return getCollection().getRollingConfigState();
}
/**
* Add a listener for config changes
*
* @param listener listener
*/
public void addConfigListener(ConfigListener listener)
{
configListeners.add(listener);
}
public boolean isStandaloneMode()
{
return provider instanceof NoneConfigProvider;
}
public enum CancelMode
{
ROLLBACK,
FORCE_COMMIT
}
public PseudoLock newConfigBasedLock() throws Exception
{
return provider.newPseudoLock();
}
public synchronized void cancelRollingConfig(CancelMode mode) throws Exception
{
ConfigCollection localConfig = getCollection();
if ( localConfig.isRolling() )
{
clearAttempts();
InstanceConfig newConfig = (mode == CancelMode.ROLLBACK) ? localConfig.getRootConfig() : localConfig.getRollingConfig();
ConfigCollection newCollection = new ConfigCollectionImpl(newConfig, null);
internalUpdateConfig(newCollection);
}
}
public synchronized void checkRollingConfig(InstanceState instanceState) throws Exception
{
ConfigCollection localConfig = getCollection();
if ( localConfig.isRolling() )
{
RollingReleaseState state = new RollingReleaseState(instanceState, localConfig);
if ( state.getCurrentRollingHostname().equals(exhibitor.getThisJVMHostname()) )
{
if ( state.serverListHasSynced() )
{
if ( instanceState.getState() == InstanceStateTypes.SERVING )
{
advanceRollingConfig(localConfig);
}
else if ( instanceState.getState() == InstanceStateTypes.NOT_SERVING )
{
if ( waitingForQuorumAttempts.incrementAndGet() >= maxAttempts )
{
exhibitor.getLog().add(ActivityLog.Type.INFO, "Ensemble is not achieving quorum. Now falling back to a force commit of the configuration change.");
cancelRollingConfig(CancelMode.FORCE_COMMIT);
}
else
{
exhibitor.getLog().add(ActivityLog.Type.INFO, "Waiting for instance sync before advancing rolling config. Attempt " + waitingForQuorumAttempts.get() + " of " + maxAttempts);
}
}
}
}
}
}
public synchronized boolean startRollingConfig(final InstanceConfig newConfig, String leaderHostname) throws Exception
{
ConfigCollection localConfig = getCollection();
if ( localConfig.isRolling() )
{
return false;
}
InstanceConfig currentConfig = getCollection().getRootConfig();
RollingHostNamesBuilder builder = new RollingHostNamesBuilder(currentConfig, newConfig, leaderHostname);
clearAttempts();
ConfigCollection newCollection = new ConfigCollectionImpl(currentConfig, newConfig, builder.getRollingHostNames(), 0);
return advanceOrStartRollingConfig(newCollection, -1);
}
public synchronized boolean updateConfig(final InstanceConfig newConfig) throws Exception
{
ConfigCollection localConfig = getCollection();
if ( localConfig.isRolling() )
{
return false;
}
clearAttempts();
ConfigCollection newCollection = new ConfigCollectionImpl(newConfig, null);
return internalUpdateConfig(newCollection);
}
@VisibleForTesting
ConfigCollection getCollection()
{
return config.get().getConfig();
}
@VisibleForTesting
RollingConfigAdvanceAttempt getRollingConfigAdvanceAttempt()
{
return rollingConfigAdvanceAttempt.get();
}
private void advanceRollingConfig(ConfigCollection config) throws Exception
{
int rollingHostNamesIndex = config.getRollingConfigState().getRollingHostNamesIndex();
advanceOrStartRollingConfig(config, rollingHostNamesIndex);
}
private boolean advanceOrStartRollingConfig(ConfigCollection config, int rollingHostNamesIndex) throws Exception
{
waitingForQuorumAttempts.set(0);
List<String> rollingHostNames = config.getRollingConfigState().getRollingHostNames();
boolean updateConfigResult;
ConfigCollection newCollection = checkNextInstanceState(config, rollingHostNames, rollingHostNamesIndex);
if ( newCollection != null )
{
clearAttempts();
updateConfigResult = internalUpdateConfig(newCollection);
}
else
{
if ( rollingHostNamesIndex < 0 )
{
// this is the start phase - park the bad instance in the back for now
List<String> newRollingHostNames = Lists.newArrayList(rollingHostNames);
Collections.rotate(newRollingHostNames, -1);
ConfigCollection collection = new ConfigCollectionImpl(config.getRootConfig(), config.getRollingConfig(), newRollingHostNames, rollingHostNamesIndex + 1);
clearAttempts();
updateConfigResult = internalUpdateConfig(collection);
}
else
{
updateConfigResult = true;
}
}
return updateConfigResult;
}
private void clearAttempts()
{
rollingConfigAdvanceAttempt.set(null);
waitingForQuorumAttempts.set(0);
}
private ConfigCollection checkNextInstanceState(ConfigCollection config, List<String> rollingHostNames, int rollingHostNamesIndex)
{
if ( (rollingHostNamesIndex + 1) >= rollingHostNames.size() )
{
// we're done - switch back to single config
return new ConfigCollectionImpl(config.getRollingConfig(), null);
}
ConfigCollection newCollection = new ConfigCollectionImpl(config.getRootConfig(), config.getRollingConfig(), rollingHostNames, rollingHostNamesIndex + 1);
RollingReleaseState state = new RollingReleaseState(new InstanceState(), newCollection);
if ( state.getCurrentRollingHostname().equals(exhibitor.getThisJVMHostname()) )
{
return newCollection;
}
RollingConfigAdvanceAttempt activeAttempt = rollingConfigAdvanceAttempt.get();
RemoteInstanceRequest.Result result;
if ( (activeAttempt == null) || !activeAttempt.getHostname().equals(state.getCurrentRollingHostname()) || (activeAttempt.getAttemptCount() < maxAttempts) )
{
RemoteInstanceRequest remoteInstanceRequest = new RemoteInstanceRequest(exhibitor, state.getCurrentRollingHostname());
result = remoteInstanceRequest.makeRequest(exhibitor.getRemoteInstanceRequestClient(), "getStatus");
if ( activeAttempt == null )
{
activeAttempt = new RollingConfigAdvanceAttempt(state.getCurrentRollingHostname());
rollingConfigAdvanceAttempt.set(activeAttempt);
}
activeAttempt.incrementAttemptCount();
if ( result.errorMessage.length() != 0 )
{
if ( activeAttempt.getAttemptCount() >= maxAttempts )
{
exhibitor.getLog().add(ActivityLog.Type.INFO, "Exhausted attempts to connect to " + remoteInstanceRequest.getHostname() + " - skipping and moving on to next instance");
newCollection = checkNextInstanceState(config, rollingHostNames, rollingHostNamesIndex + 1); // it must be down. Skip it.
}
else
{
exhibitor.getLog().add(ActivityLog.Type.INFO, "Could not connect to " + remoteInstanceRequest.getHostname() + " - attempt #" + activeAttempt.getAttemptCount());
newCollection = null;
}
}
}
else
{
newCollection = null;
}
return newCollection;
}
private boolean internalUpdateConfig(ConfigCollection newCollection) throws Exception
{
LoadedInstanceConfig updated = provider.storeConfig(newCollection, config.get().getVersion());
if ( updated != null )
{
setNewConfig(updated);
return true;
}
return false;
}
private void setNewConfig(LoadedInstanceConfig newConfig) throws Exception
{
config.getAndSet(newConfig);
notifyListeners();
}
private synchronized void notifyListeners()
{
for ( ConfigListener listener : configListeners )
{
listener.configUpdated();
}
}
private synchronized void doWork() throws Exception
{
LoadedInstanceConfig newConfig = provider.loadConfig();
if ( newConfig.getVersion() != config.get().getVersion() )
{
setNewConfig(newConfig);
}
}
}