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 org.apache.phoenix.thirdparty.com.google.common.base.Optional;
21 import org.apache.phoenix.thirdparty.com.google.common.util.concurrent.ListenableFuture;
22
23 import java.io.IOException;
24
25 public interface CommitTable {
26
27 long INVALID_TRANSACTION_MARKER = -1L;
28 int MAX_CHECKPOINTS_PER_TXN = 50;
29
30 Writer getWriter() throws IOException;
31
32 Client getClient() throws IOException;
33
34 interface Writer{
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 * Add commited transaction while checking if invalidated by other client
52 */
53 boolean atomicAddCommittedTransaction(long startTimestamp, long commitTimestamp) throws IOException;
54 }
55
56 interface Client {
57
58 /**
59 * Checks whether a transaction commit data is inside the commit table The function also checks whether the
60 * transaction was invalidated and returns a commit timestamp type accordingly.
61 *
62 * @param startTimestamp the transaction start timestamp
63 * @return Optional of CommitTimestamp that represents a valid, invalid, or no timestamp.
64 */
65 ListenableFuture<Optional<CommitTimestamp>> getCommitTimestamp(long startTimestamp);
66
67 ListenableFuture<Long> readLowWatermark();
68
69 ListenableFuture<Void> deleteCommitEntry(long startTimestamp);
70
71 /**
72 * Atomically tries to invalidate a non-committed transaction launched by a previous TSO server.
73 *
74 * @param startTimeStamp the transaction to invalidate
75 * @return true on success and false on failure
76 */
77 ListenableFuture<Boolean> tryInvalidateTransaction(long startTimeStamp);
78 }
79
80 // ----------------------------------------------------------------------------------------------------------------
81 // Helper classes
82 // ----------------------------------------------------------------------------------------------------------------
83 class CommitTimestamp {
84
85 public enum Location {
86 NOT_PRESENT, CACHE, COMMIT_TABLE, SHADOW_CELL
87 }
88
89 private final Location location;
90 private final long value;
91 private final boolean isValid;
92
93 public CommitTimestamp(Location location, long value, boolean isValid) {
94 this.location = location;
95 this.value = value;
96 this.isValid = isValid;
97 }
98
99 public Location getLocation() {
100 return location;
101 }
102
103 public long getValue() {
104 return value;
105 }
106
107 public boolean isValid() {
108 return isValid;
109 }
110
111 @Override
112 public String toString() {
113 return String.format("Is valid=%s, Location=%s, Value=%d)", isValid, location, value);
114 }
115
116 }
117
118 }