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 java.io.IOException;
21
22 /**
23 * Allows to reset the TSO state and register observers for being notified
24 * when changes occur
25 */
26 public interface TSOStateManager {
27
28 /**
29 * Represents the state of the TSO
30 */
31 class TSOState {
32
33 // TSO state variables
34 private final long lowWatermark;
35
36 public TSOState(long lowWatermark, long epoch) {
37 this.lowWatermark = lowWatermark;
38 }
39
40 public long getLowWatermark() {
41 return lowWatermark;
42 }
43
44 public long getEpoch() {
45 // In this implementation the epoch == low watermark
46 return lowWatermark;
47 }
48
49 @Override
50 public String toString() {
51 return String.format("LWM %d/Epoch %d", getLowWatermark(), getEpoch());
52 }
53
54 }
55
56 /**
57 * Allows implementors to receive the new state when changes occur
58 */
59 interface StateObserver {
60
61 /**
62 * Notifies the observer about the change in state
63 * @param state
64 * the new TSOState
65 * @throws InterruptedException
66 */
67 void update(TSOState state) throws Exception;
68
69 }
70
71 /**
72 * Allows to register observers for receiving state changes
73 *
74 * @param observer
75 * the observer to register
76 */
77 void register(StateObserver observer);
78
79 /**
80 * Allows to de-register observers for stopping receiving changes
81 *
82 * @param observer
83 * the observer to unregister
84 */
85 void unregister(StateObserver observer);
86
87 /**
88 * Allows to initialize the state
89 *
90 * @return the new state
91 * @throws IOException
92 * when problems resetting occur
93 * @throws InterruptedException
94 */
95 TSOState initialize() throws Exception;
96
97 }