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.committable;
19  
20  import com.google.common.base.Optional;
21  import com.google.common.util.concurrent.ListenableFuture;
22  
23  import java.io.Closeable;
24  import java.io.IOException;
25  
26  public interface CommitTable {
27  
28      long INVALID_TRANSACTION_MARKER = -1L;
29  
30      Writer getWriter() throws IOException;
31  
32      Client getClient() throws IOException;
33  
34      interface Writer extends Closeable {
35  
36          void addCommittedTransaction(long startTimestamp, long commitTimestamp) throws IOException;
37  
38          void updateLowWatermark(long lowWatermark) throws IOException;
39  
40          /**
41           * Flushes all the buffered events to the underlying datastore
42           */
43          void flush() throws IOException;
44  
45          /**
46           * Allows to clean the write's current buffer. It is required for HA
47           */
48          void clearWriteBuffer();
49      }
50  
51      interface Client extends Closeable {
52  
53          /**
54           * Checks whether a transaction commit data is inside the commit table The function also checks whether the
55           * transaction was invalidated and returns a commit timestamp type accordingly.
56           *
57           * @param startTimestamp the transaction start timestamp
58           * @return Optional<CommitTimestamp> that represents a valid, invalid, or no timestamp.
59           */
60          ListenableFuture<Optional<CommitTimestamp>> getCommitTimestamp(long startTimestamp);
61  
62          ListenableFuture<Long> readLowWatermark();
63  
64          ListenableFuture<Void> completeTransaction(long startTimestamp);
65  
66          /**
67           * Atomically tries to invalidate a non-committed transaction launched by a previous TSO server.
68           *
69           * @param startTimeStamp the transaction to invalidate
70           * @return true on success and false on failure
71           */
72          ListenableFuture<Boolean> tryInvalidateTransaction(long startTimeStamp);
73      }
74  
75      // ----------------------------------------------------------------------------------------------------------------
76      // Helper classes
77      // ----------------------------------------------------------------------------------------------------------------
78      class CommitTimestamp {
79  
80          public enum Location {
81              NOT_PRESENT, CACHE, COMMIT_TABLE, SHADOW_CELL
82          }
83  
84          private final Location location;
85          private final long value;
86          private final boolean isValid;
87  
88          public CommitTimestamp(Location location, long value, boolean isValid) {
89              this.location = location;
90              this.value = value;
91              this.isValid = isValid;
92          }
93  
94          public Location getLocation() {
95              return location;
96          }
97  
98          public long getValue() {
99              return value;
100         }
101 
102         public boolean isValid() {
103             return isValid;
104         }
105 
106         @Override
107         public String toString() {
108             return String.format("Is valid=%s, Location=%s, Value=%d)", isValid, location, value);
109         }
110 
111     }
112 
113 }