View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // Low Watermark writer
54          ThreadFactoryBuilder lwmThreadFactory = new ThreadFactoryBuilder().setNameFormat("lwm-writer-%d");
55          this.lowWatermarkWriterExecutor = Executors.newSingleThreadExecutor(lwmThreadFactory.build());
56  
57          // Metrics config
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  }