forked from voldemort/voldemort
/
StreamingSlopPusherJob.java
459 lines (407 loc) · 18.7 KB
/
StreamingSlopPusherJob.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package voldemort.server.scheduler.slop;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.log4j.Logger;
import voldemort.VoldemortException;
import voldemort.client.protocol.admin.AdminClient;
import voldemort.client.protocol.admin.AdminClientConfig;
import voldemort.cluster.Cluster;
import voldemort.cluster.Node;
import voldemort.cluster.Zone;
import voldemort.cluster.failuredetector.FailureDetector;
import voldemort.server.StoreRepository;
import voldemort.server.VoldemortConfig;
import voldemort.server.storage.ScanPermitWrapper;
import voldemort.store.StorageEngine;
import voldemort.store.UnreachableStoreException;
import voldemort.store.metadata.MetadataStore;
import voldemort.store.slop.Slop;
import voldemort.store.slop.SlopStorageEngine;
import voldemort.utils.ByteArray;
import voldemort.utils.ClosableIterator;
import voldemort.utils.EventThrottler;
import voldemort.utils.Pair;
import voldemort.utils.Utils;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Version;
import voldemort.versioning.Versioned;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
@SuppressWarnings("unchecked")
public class StreamingSlopPusherJob implements Runnable {
private final static Logger logger = Logger.getLogger(StreamingSlopPusherJob.class.getName());
public final static String TYPE_NAME = "streaming";
private final static Versioned<Slop> END = Versioned.value(null);
private final MetadataStore metadataStore;
private final StoreRepository storeRepo;
private final FailureDetector failureDetector;
private ConcurrentMap<Integer, SynchronousQueue<Versioned<Slop>>> slopQueues;
private ExecutorService consumerExecutor;
private final EventThrottler readThrottler;
private AdminClient adminClient;
private Cluster cluster;
private final List<Future> consumerResults;
private final VoldemortConfig voldemortConfig;
private final Map<Integer, Set<Integer>> zoneMapping;
private ConcurrentHashMap<Integer, Long> attemptedByNode;
private ConcurrentHashMap<Integer, Long> succeededByNode;
private final ScanPermitWrapper repairPermits;
public StreamingSlopPusherJob(StoreRepository storeRepo,
MetadataStore metadataStore,
FailureDetector failureDetector,
VoldemortConfig voldemortConfig,
ScanPermitWrapper repairPermits) {
this.storeRepo = storeRepo;
this.metadataStore = metadataStore;
this.failureDetector = failureDetector;
this.voldemortConfig = voldemortConfig;
this.repairPermits = Utils.notNull(repairPermits);
this.readThrottler = new EventThrottler(voldemortConfig.getSlopMaxReadBytesPerSec());
this.adminClient = null;
this.consumerResults = Lists.newArrayList();
this.zoneMapping = Maps.newHashMap();
this.consumerExecutor = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("slop-pusher");
return thread;
}
});
}
public void run() {
// load the metadata before each run, in case the cluster is changed
loadMetadata();
// don't try to run slop pusher job when rebalancing
if(metadataStore.getServerState()
.equals(MetadataStore.VoldemortState.REBALANCING_MASTER_SERVER)) {
logger.error("Cannot run slop pusher job since Voldemort server is rebalancing");
return;
}
boolean terminatedEarly = false;
Date startTime = new Date();
logger.info("Started streaming slop pusher job at " + startTime);
SlopStorageEngine slopStorageEngine = storeRepo.getSlopStore();
ClosableIterator<Pair<ByteArray, Versioned<Slop>>> iterator = null;
if(adminClient == null) {
adminClient = new AdminClient(cluster,
new AdminClientConfig().setMaxConnectionsPerNode(1));
}
if(voldemortConfig.getSlopZonesDownToTerminate() > 0) {
// Populating the zone mapping for early termination
zoneMapping.clear();
for(Node n: cluster.getNodes()) {
if(failureDetector.isAvailable(n)) {
Set<Integer> nodes = zoneMapping.get(n.getZoneId());
if(nodes == null) {
nodes = Sets.newHashSet();
zoneMapping.put(n.getZoneId(), nodes);
}
nodes.add(n.getId());
}
}
// Check how many zones are down
int zonesDown = 0;
for(Zone zone: cluster.getZones()) {
if(zoneMapping.get(zone.getId()) == null
|| zoneMapping.get(zone.getId()).size() == 0)
zonesDown++;
}
// Terminate early
if(voldemortConfig.getSlopZonesDownToTerminate() <= zoneMapping.size()
&& zonesDown >= voldemortConfig.getSlopZonesDownToTerminate()) {
logger.info("Completed streaming slop pusher job at " + startTime
+ " early because " + zonesDown + " zones are down");
stopAdminClient();
return;
}
}
// Clearing the statistics
AtomicLong attemptedPushes = new AtomicLong(0);
for(Node node: cluster.getNodes()) {
attemptedByNode.put(node.getId(), 0L);
succeededByNode.put(node.getId(), 0L);
}
acquireRepairPermit();
try {
StorageEngine<ByteArray, Slop, byte[]> slopStore = slopStorageEngine.asSlopStore();
iterator = slopStore.entries();
while(iterator.hasNext()) {
Pair<ByteArray, Versioned<Slop>> keyAndVal;
try {
keyAndVal = iterator.next();
Versioned<Slop> versioned = keyAndVal.getSecond();
// Retrieve the node
int nodeId = versioned.getValue().getNodeId();
Node node = cluster.getNodeById(nodeId);
attemptedPushes.incrementAndGet();
Long attempted = attemptedByNode.get(nodeId);
attemptedByNode.put(nodeId, attempted + 1L);
if(attemptedPushes.get() % 10000 == 0)
logger.info("Attempted pushing " + attemptedPushes + " slops");
if(logger.isTraceEnabled())
logger.trace("Pushing slop for " + versioned.getValue().getNodeId()
+ " and store " + versioned.getValue().getStoreName());
if(failureDetector.isAvailable(node)) {
SynchronousQueue<Versioned<Slop>> slopQueue = slopQueues.get(nodeId);
if(slopQueue == null) {
// No previous slop queue, add one
slopQueue = new SynchronousQueue<Versioned<Slop>>();
slopQueues.put(nodeId, slopQueue);
consumerResults.add(consumerExecutor.submit(new SlopConsumer(nodeId,
slopQueue,
slopStorageEngine)));
}
boolean offered = slopQueue.offer(versioned,
voldemortConfig.getClientRoutingTimeoutMs(),
TimeUnit.MILLISECONDS);
if(!offered) {
if(logger.isDebugEnabled())
logger.debug("No consumer appeared for slop in "
+ voldemortConfig.getClientConnectionTimeoutMs()
+ " ms");
}
readThrottler.maybeThrottle(nBytesRead(keyAndVal));
} else {
logger.trace(node + " declared down, won't push slop");
}
} catch(RejectedExecutionException e) {
throw new VoldemortException("Ran out of threads in executor", e);
}
}
} catch(InterruptedException e) {
logger.warn("Interrupted exception", e);
terminatedEarly = true;
} catch(Exception e) {
logger.error(e, e);
terminatedEarly = true;
} finally {
try {
if(iterator != null)
iterator.close();
} catch(Exception e) {
logger.warn("Failed to close iterator cleanly as database might be closed", e);
}
// Adding the poison pill
for(SynchronousQueue<Versioned<Slop>> slopQueue: slopQueues.values()) {
try {
slopQueue.put(END);
} catch(InterruptedException e) {
logger.warn("Error putting poison pill", e);
}
}
for(Future result: consumerResults) {
try {
result.get();
} catch(Exception e) {
logger.warn("Exception in consumer", e);
}
}
// Only if exception didn't take place do we update the counts
if(!terminatedEarly) {
Map<Integer, Long> outstanding = Maps.newHashMapWithExpectedSize(cluster.getNumberOfNodes());
for(int nodeId: succeededByNode.keySet()) {
logger.info("Slops to node " + nodeId + " - Succeeded - "
+ succeededByNode.get(nodeId) + " - Attempted - "
+ attemptedByNode.get(nodeId));
outstanding.put(nodeId,
attemptedByNode.get(nodeId) - succeededByNode.get(nodeId));
}
slopStorageEngine.resetStats(outstanding);
logger.info("Completed streaming slop pusher job which started at " + startTime);
} else {
for(int nodeId: succeededByNode.keySet()) {
logger.info("Slops to node " + nodeId + " - Succeeded - "
+ succeededByNode.get(nodeId) + " - Attempted - "
+ attemptedByNode.get(nodeId));
}
logger.info("Completed early streaming slop pusher job which started at "
+ startTime);
}
// Shut down admin client as not to waste connections
consumerResults.clear();
slopQueues.clear();
stopAdminClient();
this.repairPermits.release();
}
}
private void loadMetadata() {
this.cluster = metadataStore.getCluster();
this.slopQueues = new ConcurrentHashMap<Integer, SynchronousQueue<Versioned<Slop>>>(cluster.getNumberOfNodes());
this.attemptedByNode = new ConcurrentHashMap<Integer, Long>(cluster.getNumberOfNodes());
this.succeededByNode = new ConcurrentHashMap<Integer, Long>(cluster.getNumberOfNodes());
}
private void stopAdminClient() {
if(adminClient != null) {
adminClient.stop();
adminClient = null;
}
}
private int nBytesRead(Pair<ByteArray, Versioned<Slop>> keyAndVal) {
return keyAndVal.getFirst().length() + slopSize(keyAndVal.getSecond());
}
/**
* Returns the approximate size of slop to help in throttling
*
* @param slopVersioned The versioned slop whose size we want
* @return Size in bytes
*/
private int slopSize(Versioned<Slop> slopVersioned) {
int nBytes = 0;
Slop slop = slopVersioned.getValue();
nBytes += slop.getKey().length();
nBytes += ((VectorClock) slopVersioned.getVersion()).sizeInBytes();
switch(slop.getOperation()) {
case PUT: {
nBytes += slop.getValue().length;
break;
}
case DELETE: {
break;
}
default:
logger.error("Unknown slop operation: " + slop.getOperation());
}
return nBytes;
}
/**
* Smart slop iterator which keeps two previous batches of data
*
*/
private class SlopIterator extends AbstractIterator<Versioned<Slop>> {
private final SynchronousQueue<Versioned<Slop>> slopQueue;
private final List<Pair<ByteArray, Version>> deleteBatch;
private final EventThrottler writeThrottler;
private int writtenLast = 0;
private long slopsDone = 0L;
private boolean shutDown = false, isComplete = false;
public SlopIterator(SynchronousQueue<Versioned<Slop>> slopQueue,
List<Pair<ByteArray, Version>> deleteBatch) {
this.slopQueue = slopQueue;
this.deleteBatch = deleteBatch;
this.writeThrottler = new EventThrottler(voldemortConfig.getSlopMaxWriteBytesPerSec());
}
public boolean isComplete() {
return isComplete;
}
@Override
protected Versioned<Slop> computeNext() {
try {
Versioned<Slop> head = null;
if(!shutDown) {
head = slopQueue.take();
if(head.equals(END)) {
shutDown = true;
isComplete = true;
} else {
slopsDone++;
if(slopsDone % voldemortConfig.getSlopBatchSize() == 0) {
shutDown = true;
}
writeThrottler.maybeThrottle(writtenLast);
writtenLast = slopSize(head);
deleteBatch.add(Pair.create(head.getValue().makeKey(),
(Version) head.getVersion()));
return head;
}
}
return endOfData();
} catch(Exception e) {
logger.error("Got an exception " + e);
return endOfData();
}
}
}
private void acquireRepairPermit() {
logger.info("Acquiring lock to perform streaming slop pusher job ");
try {
this.repairPermits.acquire(null);
logger.info("Acquired lock to perform streaming slop pusher job ");
} catch(InterruptedException e) {
stopAdminClient();
throw new IllegalStateException("Streaming slop pusher job interrupted while waiting for permit.",
e);
}
}
private class SlopConsumer implements Runnable {
private final int nodeId;
private SynchronousQueue<Versioned<Slop>> slopQueue;
private long startTime;
private SlopStorageEngine slopStorageEngine;
// Keep two lists to track deleted items
private List<Pair<ByteArray, Version>> previous, current;
public SlopConsumer(int nodeId,
SynchronousQueue<Versioned<Slop>> slopQueue,
SlopStorageEngine slopStorageEngine) {
this.nodeId = nodeId;
this.slopQueue = slopQueue;
this.slopStorageEngine = slopStorageEngine;
this.previous = Lists.newArrayList();
this.current = Lists.newArrayList();
}
public void run() {
try {
SlopIterator iterator = null;
do {
if(!current.isEmpty()) {
if(!previous.isEmpty()) {
for(Pair<ByteArray, Version> entry: previous) {
slopStorageEngine.delete(entry.getFirst(), entry.getSecond());
}
Long succeeded = succeededByNode.get(nodeId);
succeeded += previous.size();
succeededByNode.put(nodeId, succeeded);
previous.clear();
}
previous = null;
previous = current;
current = Lists.newArrayList();
}
this.startTime = System.currentTimeMillis();
iterator = new SlopIterator(slopQueue, current);
adminClient.streamingOps.updateSlopEntries(nodeId, iterator);
} while(!iterator.isComplete());
// Clear up both previous and current
if(!previous.isEmpty()) {
for(Pair<ByteArray, Version> entry: previous)
slopStorageEngine.delete(entry.getFirst(), entry.getSecond());
Long succeeded = succeededByNode.get(nodeId);
succeeded += previous.size();
succeededByNode.put(nodeId, succeeded);
previous.clear();
}
if(!current.isEmpty()) {
for(Pair<ByteArray, Version> entry: current)
slopStorageEngine.delete(entry.getFirst(), entry.getSecond());
Long succeeded = succeededByNode.get(nodeId);
succeeded += current.size();
succeededByNode.put(nodeId, succeeded);
current.clear();
}
} catch(UnreachableStoreException e) {
failureDetector.recordException(metadataStore.getCluster().getNodeById(nodeId),
System.currentTimeMillis() - this.startTime,
e);
throw e;
} finally {
// Clean the slop queue and remove the queue from the global
// queue
slopQueue.clear();
slopQueues.remove(nodeId);
}
}
}
}