1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.omid.tso;
19
20 import org.apache.phoenix.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
21 import com.google.inject.Inject;
22 import org.apache.omid.committable.CommitTable;
23 import org.apache.omid.metrics.MetricsRegistry;
24 import org.apache.omid.metrics.Timer;
25
26 import java.io.IOException;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import static org.apache.omid.metrics.MetricsUtils.name;
36
37 public class LowWatermarkWriterImpl implements LowWatermarkWriter {
38
39 private static final Logger LOG = LoggerFactory.getLogger(LowWatermarkWriterImpl.class);
40
41 private final Timer lwmWriteTimer;
42 private final CommitTable.Writer lowWatermarkWriter;
43 private final ExecutorService lowWatermarkWriterExecutor;
44 private MetricsRegistry metrics;
45
46 @Inject
47 LowWatermarkWriterImpl(TSOServerConfig config,
48 CommitTable commitTable,
49 MetricsRegistry metrics)
50 throws Exception {
51 this.metrics = metrics;
52 this.lowWatermarkWriter = commitTable.getWriter();
53
54 ThreadFactoryBuilder lwmThreadFactory = new ThreadFactoryBuilder().setNameFormat("lwm-writer-%d");
55 this.lowWatermarkWriterExecutor = Executors.newSingleThreadExecutor(lwmThreadFactory.build());
56
57
58 this.lwmWriteTimer = metrics.timer(name("tso", "lwmWriter", "latency"));
59 LOG.info("PersistentProcessor initialized");
60 }
61
62 @Override
63 public Future<Void> persistLowWatermark(final long lowWatermark) {
64
65 return lowWatermarkWriterExecutor.submit(new Callable<Void>() {
66 @Override
67 public Void call() throws IOException {
68 try {
69 lwmWriteTimer.start();
70 lowWatermarkWriter.updateLowWatermark(lowWatermark);
71 lowWatermarkWriter.flush();
72 } finally {
73 lwmWriteTimer.stop();
74 }
75 return null;
76 }
77 });
78 }
79 }