-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathRepairRunner.java
423 lines (377 loc) · 16 KB
/
RepairRunner.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* 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.spotify.reaper.service;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLongArray;
import org.apache.cassandra.repair.RepairParallelism;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.spotify.reaper.AppContext;
import com.spotify.reaper.ReaperException;
import com.spotify.reaper.cassandra.JmxProxy;
import com.spotify.reaper.core.Cluster;
import com.spotify.reaper.core.RepairRun;
import com.spotify.reaper.core.RepairSegment;
import com.spotify.reaper.core.RepairUnit;
public class RepairRunner implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(RepairRunner.class);
private final AppContext context;
private final long repairRunId;
private final String clusterName;
private JmxProxy jmxConnection;
private final AtomicLongArray currentlyRunningSegments;
private final List<RingRange> parallelRanges;
public RepairRunner(AppContext context, long repairRunId)
throws ReaperException {
LOG.debug("Creating RepairRunner for run with ID {}", repairRunId);
this.context = context;
this.repairRunId = repairRunId;
Optional<RepairRun> repairRun = context.storage.getRepairRun(repairRunId);
assert repairRun.isPresent() : "No RepairRun with ID " + repairRunId + " found from storage";
Optional<Cluster> cluster = context.storage.getCluster(repairRun.get().getClusterName());
assert cluster.isPresent() : "No Cluster with name " + repairRun.get().getClusterName()
+ " found from storage";
Optional<RepairUnit> repairUnitOpt =
context.storage.getRepairUnit(repairRun.get().getRepairUnitId());
assert repairUnitOpt.isPresent() : "No RepairUnit with id " + repairRun.get().getRepairUnitId()
+ " found in storage";
this.clusterName = cluster.get().getName();
JmxProxy jmx = this.context.jmxConnectionFactory.connectAny(cluster.get());
String keyspace = repairUnitOpt.get().getKeyspaceName();
int parallelRepairs = getPossibleParallelRepairsCount(jmx.getRangeToEndpointMap(keyspace));
if(repairUnitOpt.isPresent() && repairUnitOpt.get().getIncrementalRepair()) {
// with incremental repair, can't have more parallel repairs than nodes
parallelRepairs = 1;
}
currentlyRunningSegments = new AtomicLongArray(parallelRepairs);
for (int i = 0; i < parallelRepairs; i++) {
currentlyRunningSegments.set(i, -1);
}
parallelRanges = getParallelRanges(
parallelRepairs,
Lists.newArrayList(Collections2.transform(
context.storage.getRepairSegmentsForRun(repairRunId),
new Function<RepairSegment, RingRange>() {
@Override
public RingRange apply(RepairSegment input) {
return input.getTokenRange();
}
})));
}
public long getRepairRunId() {
return repairRunId;
}
@VisibleForTesting
public static int getPossibleParallelRepairsCount(Map<List<String>, List<String>> ranges)
throws ReaperException {
if (ranges.isEmpty()) {
String msg = "Repairing 0-sized cluster.";
LOG.error(msg);
throw new ReaperException(msg);
}
return ranges.size() / ranges.values().iterator().next().size();
}
@VisibleForTesting
public static List<RingRange> getParallelRanges(int parallelRepairs, List<RingRange> segments)
throws ReaperException {
if (parallelRepairs == 0) {
String msg = "Can't repair anything with 0 threads";
LOG.error(msg);
throw new ReaperException(msg);
}
Collections.sort(segments, RingRange.startComparator);
List<RingRange> parallelRanges = Lists.newArrayList();
for (int i = 0; i < parallelRepairs - 1; i++) {
parallelRanges.add(new RingRange(
segments.get(i * segments.size() / parallelRepairs).getStart(),
segments.get((i + 1) * segments.size() / parallelRepairs).getStart()
));
}
parallelRanges.add(new RingRange(
segments.get((parallelRepairs - 1) * segments.size() / parallelRepairs).getStart(),
segments.get(0).getStart()
));
return parallelRanges;
}
/**
* Starts/resumes a repair run that is supposed to run.
*/
@Override
public void run() {
Thread.currentThread().setName(clusterName + ":" + repairRunId);
try {
Optional<RepairRun> repairRun = context.storage.getRepairRun(repairRunId);
if ((!repairRun.isPresent() || repairRun.get().getRunState().isTerminated()) &&
context.repairManager.repairRunners.containsKey(repairRunId)) {
// this might happen if a run is deleted while paused etc.
LOG.warn("RepairRun \"{}\" does not exist. Killing "
+ "RepairRunner for this run instance.", repairRunId);
killAndCleanupRunner();
return;
}
RepairRun.RunState state = repairRun.get().getRunState();
LOG.debug("run() called for repair run #{} with run state {}", repairRunId, state);
switch (state) {
case NOT_STARTED:
start();
break;
case RUNNING:
startNextSegment();
break;
case PAUSED:
context.repairManager.scheduleRetry(this);
break;
}
} catch (RuntimeException | ReaperException e) {
LOG.error("RepairRun FAILURE, scheduling retry", e);
context.repairManager.scheduleRetry(this);
}
// Adding this here to catch a deadlock
LOG.debug("run() exiting for repair run #{}", repairRunId);
}
/**
* Starts the repair run.
*/
private void start() throws ReaperException {
LOG.info("Repairs for repair run #{} starting", repairRunId);
synchronized (this) {
RepairRun repairRun = context.storage.getRepairRun(repairRunId).get();
context.storage.updateRepairRun(repairRun.with()
.runState(RepairRun.RunState.RUNNING)
.startTime(DateTime.now())
.build(repairRun.getId()));
}
startNextSegment();
}
private void endRepairRun() {
LOG.info("Repairs for repair run #{} done", repairRunId);
synchronized (this) {
RepairRun repairRun = context.storage.getRepairRun(repairRunId).get();
context.storage.updateRepairRun(repairRun.with()
.runState(RepairRun.RunState.DONE)
.endTime(DateTime.now())
.lastEvent("All done")
.build(repairRun.getId()));
killAndCleanupRunner();
}
}
private void confirmJMXConnectionIsOpen() throws ReaperException {
if (jmxConnection == null || !jmxConnection.isConnectionAlive()) {
LOG.debug("connecting JMX proxy for repair runner on run id: {}", repairRunId);
Cluster cluster = context.storage.getCluster(this.clusterName).get();
jmxConnection = context.jmxConnectionFactory.connectAny(cluster);
LOG.debug("successfully reestablished JMX proxy for repair runner");
}
}
/**
* Get the next segment and repair it. If there is none, we're done.
*/
private void startNextSegment() throws ReaperException {
boolean scheduleRetry = true;
boolean anythingRunningStill = false;
confirmJMXConnectionIsOpen();
// We want to know whether a repair was started,
// so that a rescheduling of this runner will happen.
boolean repairStarted = false;
for (int rangeIndex = 0; rangeIndex < currentlyRunningSegments.length(); rangeIndex++) {
if (currentlyRunningSegments.get(rangeIndex) != -1L) {
anythingRunningStill = true;
// Just checking that no currently running segment runner is stuck.
RepairSegment supposedlyRunningSegment =
context.storage.getRepairSegment(currentlyRunningSegments.get(rangeIndex)).get();
DateTime startTime = supposedlyRunningSegment.getStartTime();
if (startTime != null && startTime.isBefore(DateTime.now().minusDays(1))) {
LOG.warn("Looks like segment #{} has been running more than a day. Start time: {}",
supposedlyRunningSegment.getId(), supposedlyRunningSegment.getStartTime());
} else if (startTime != null && startTime.isBefore(DateTime.now().minusHours(1))) {
LOG.info("Looks like segment #{} has been running more than an hour. Start time: {}",
supposedlyRunningSegment.getId(), supposedlyRunningSegment.getStartTime());
} else if (startTime != null && startTime.isBefore(DateTime.now().minusMinutes(2))) {
LOG.debug("Looks like segment #{} has been running more than two minutes. Start time: {}",
supposedlyRunningSegment.getId(), supposedlyRunningSegment.getStartTime());
}
// No need to try starting new repair for already active slot.
continue;
}
// We have an empty slot, so let's start new segment runner if possible.
Optional<RepairSegment> nextRepairSegment =
context.storage.getNextFreeSegmentInRange(repairRunId, parallelRanges.get(rangeIndex));
if (!nextRepairSegment.isPresent()) {
LOG.debug("No repair segment available for range {}", parallelRanges.get(rangeIndex));
} else {
LOG.info("Next segment to run : {}", nextRepairSegment.get().getId());
long segmentId = nextRepairSegment.get().getId();
boolean wasSet = currentlyRunningSegments.compareAndSet(rangeIndex, -1, segmentId);
if (!wasSet) {
LOG.debug("Didn't set segment id `{}` to slot {} because it was busy",
segmentId, rangeIndex);
} else {
LOG.debug("Did set segment id `{}` to slot {}", segmentId, rangeIndex);
scheduleRetry = repairSegment(rangeIndex, nextRepairSegment.get().getId(),
nextRepairSegment.get().getTokenRange());
if (!scheduleRetry) {
break;
}
repairStarted = true;
}
}
}
if (!repairStarted && !anythingRunningStill) {
int amountDone = context.storage
.getSegmentAmountForRepairRunWithState(repairRunId, RepairSegment.State.DONE);
LOG.info("Repair amount done {}", amountDone);
if (amountDone == context.storage.getSegmentAmountForRepairRun(repairRunId)) {
endRepairRun();
scheduleRetry = false;
}
}
if (scheduleRetry) {
context.repairManager.scheduleRetry(this);
}
}
/**
* Start the repair of a segment.
*
* @param segmentId id of the segment to repair.
* @param tokenRange token range of the segment to repair.
* @return Boolean indicating whether rescheduling next run is needed.
*/
private boolean repairSegment(final int rangeIndex, final long segmentId, RingRange tokenRange) {
final long unitId;
final double intensity;
final RepairParallelism validationParallelism;
{
RepairRun repairRun = context.storage.getRepairRun(repairRunId).get();
unitId = repairRun.getRepairUnitId();
intensity = repairRun.getIntensity();
validationParallelism = repairRun.getRepairParallelism();
}
RepairUnit repairUnit = context.storage.getRepairUnit(unitId).get();
String keyspace = repairUnit.getKeyspaceName();
LOG.debug("preparing to repair segment {} on run with id {}", segmentId, repairRunId);
try {
confirmJMXConnectionIsOpen();
} catch (ReaperException e) {
LOG.warn("Failed to reestablish JMX connection in runner {}, retrying", repairRunId, e);
currentlyRunningSegments.set(rangeIndex, -1);
return true;
}
List<String> potentialCoordinators;
if(!repairUnit.getIncrementalRepair()) {
// full repair
try {
potentialCoordinators = jmxConnection.tokenRangeToEndpoint(keyspace, tokenRange);
} catch (RuntimeException e) {
LOG.warn("Couldn't get token ranges from coordinator: #{}", e);
return true;
}
if (potentialCoordinators.isEmpty()) {
LOG.warn("Segment #{} is faulty, no potential coordinators for range: {}",
segmentId, tokenRange.toString());
// This segment has a faulty token range. Abort the entire repair run.
synchronized (this) {
RepairRun repairRun = context.storage.getRepairRun(repairRunId).get();
context.storage.updateRepairRun(repairRun
.with()
.runState(RepairRun.RunState.ERROR)
.lastEvent(String.format("No coordinators for range %s", tokenRange))
.endTime(DateTime.now())
.build(repairRunId));
killAndCleanupRunner();
}
return false;
}
}
else {
potentialCoordinators = Arrays.asList(context.storage.getRepairSegment(segmentId).get().getCoordinatorHost());
}
SegmentRunner segmentRunner = new SegmentRunner(context, segmentId, potentialCoordinators,
context.repairManager.getRepairTimeoutMillis(), intensity, validationParallelism,
clusterName, repairUnit, this);
ListenableFuture<?> segmentResult = context.repairManager.submitSegment(segmentRunner);
Futures.addCallback(segmentResult, new FutureCallback<Object>() {
@Override
public void onSuccess(Object ignored) {
currentlyRunningSegments.set(rangeIndex, -1);
handleResult(segmentId);
}
@Override
public void onFailure(Throwable t) {
currentlyRunningSegments.set(rangeIndex, -1);
LOG.error("Executing SegmentRunner failed: {}", t.getMessage());
}
});
return true;
}
private void handleResult(long segmentId) {
RepairSegment segment = context.storage.getRepairSegment(segmentId).get();
RepairSegment.State segmentState = segment.getState();
LOG.debug("In repair run #{}, triggerRepair on segment {} ended with state {}",
repairRunId, segmentId, segmentState);
// Don't do rescheduling here, not to spawn uncontrolled amount of threads
switch (segmentState) {
case NOT_STARTED:
// Unsuccessful repair
break;
case DONE:
// Successful repair
break;
default:
// Another thread has started a new repair on this segment already
// Or maybe the same repair segment id should never be re-run in which case this is an error
String msg = "handleResult called with a segment state (" + segmentState + ") that it "
+ "should not have after segmentRunner has tried a repair";
LOG.error(msg);
throw new RuntimeException(msg);
}
}
public void updateLastEvent(String newEvent) {
synchronized (this) {
RepairRun repairRun = context.storage.getRepairRun(repairRunId).get();
if (repairRun.getRunState().isTerminated()) {
LOG.warn("Will not update lastEvent of run that has already terminated. The message was: "
+ "\"{}\"", newEvent);
} else {
context.storage.updateRepairRun(repairRun.with()
.lastEvent(newEvent)
.build(repairRunId));
}
}
}
public void killAndCleanupRunner() {
context.repairManager.removeRunner(this);
if (jmxConnection != null) {
try {
jmxConnection.close();
jmxConnection = null;
} catch (ReaperException e) {
LOG.warn("failed closing JMX connection on runner exit: " + e);
}
}
Thread.currentThread().interrupt();
return;
}
}