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.base.Preconditions;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import javax.inject.Inject;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * Implements the management of the state of the TSO
30   */
31  public class TSOStateManagerImpl implements TSOStateManager {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(TSOStateManagerImpl.class);
34  
35      private List<StateObserver> stateObservers = new ArrayList<>();
36  
37      private TSOState state;
38  
39      private TimestampOracle timestampOracle;
40  
41      @Inject
42      public TSOStateManagerImpl(TimestampOracle timestampOracle) {
43          this.timestampOracle = timestampOracle;
44      }
45  
46      @Override
47      public synchronized void register(StateObserver newObserver) {
48          Preconditions.checkNotNull(newObserver, "Trying to register a null observer");
49          if (!stateObservers.contains(newObserver)) {
50              stateObservers.add(newObserver);
51          }
52      }
53  
54      @Override
55      public synchronized void unregister(StateObserver observer) {
56          stateObservers.remove(observer);
57      }
58  
59      @Override
60      public TSOState initialize() throws Exception {
61  
62          LOG.info("Initializing TSO Server state...");
63          // The timestamp oracle dictates the new state
64          timestampOracle.initialize();
65          long lowWatermark = timestampOracle.getLast();
66          // In this implementation the epoch == low watermark
67          long epoch = lowWatermark;
68          state = new TSOState(lowWatermark, epoch);
69  
70          // Then, notify registered observers about the new state
71          for (StateObserver stateObserver : stateObservers) {
72              stateObserver.update(state);
73          }
74          LOG.info("TSO Server state {}", state);
75          return state;
76  
77      }
78  
79  }