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.client;
19  
20  import org.apache.phoenix.thirdparty.com.google.common.collect.Sets;
21  import com.google.inject.Guice;
22  import com.google.inject.Injector;
23  import com.google.inject.Module;
24  
25  import org.apache.omid.TestUtils;
26  import org.apache.omid.tso.TSOMockModule;
27  import org.apache.omid.tso.TSOServer;
28  import org.apache.omid.tso.TSOServerConfig;
29  import org.apache.omid.tso.client.OmidClientConfiguration.ConflictDetectionLevel;
30  import org.apache.omid.tso.util.DummyCellIdImpl;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.testng.annotations.AfterMethod;
34  import org.testng.annotations.BeforeMethod;
35  import org.testng.annotations.Test;
36  
37  import java.util.Set;
38  import java.util.concurrent.ExecutionException;
39  
40  import static org.testng.Assert.assertFalse;
41  import static org.testng.Assert.assertTrue;
42  
43  public class TestTSOClientRowAndCellLevelConflict {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(TestTSOClientRowAndCellLevelConflict.class);
46  
47      private static final String TSO_SERVER_HOST = "localhost";
48      private static final int TSO_SERVER_PORT = 5678;
49  
50      private OmidClientConfiguration tsoClientConf;
51  
52      // Required infrastructure for TSOClient test
53      private TSOServer tsoServer;
54  
55      @BeforeMethod
56      public void beforeMethod() throws Exception {
57  
58          TSOServerConfig tsoConfig = new TSOServerConfig();
59          tsoConfig.setConflictMapSize(1000);
60          tsoConfig.setPort(TSO_SERVER_PORT);
61          tsoConfig.setNumConcurrentCTWriters(2);
62          Module tsoServerMockModule = new TSOMockModule(tsoConfig);
63          Injector injector = Guice.createInjector(tsoServerMockModule);
64  
65          LOG.info("==================================================================================================");
66          LOG.info("======================================= Init TSO Server ==========================================");
67          LOG.info("==================================================================================================");
68  
69          tsoServer = injector.getInstance(TSOServer.class);
70          tsoServer.startAsync();
71          tsoServer.awaitRunning();
72          TestUtils.waitForSocketListening(TSO_SERVER_HOST, TSO_SERVER_PORT, 100);
73  
74          LOG.info("==================================================================================================");
75          LOG.info("===================================== TSO Server Initialized =====================================");
76          LOG.info("==================================================================================================");
77  
78          OmidClientConfiguration tsoClientConf = new OmidClientConfiguration();
79          tsoClientConf.setConnectionString(TSO_SERVER_HOST + ":" + TSO_SERVER_PORT);
80  
81          this.tsoClientConf = tsoClientConf;
82  
83      }
84  
85      @AfterMethod
86      public void afterMethod() throws Exception {
87          tsoServer.stopAsync();
88          tsoServer.awaitTerminated();
89          tsoServer = null;
90          TestUtils.waitForSocketNotListening(TSO_SERVER_HOST, TSO_SERVER_PORT, 1000);
91      }
92  
93      @Test(timeOut = 30_000)
94      public void testRowLevelConflictAnalysisConflict() throws Exception {
95  
96          tsoClientConf.setConflictAnalysisLevel(ConflictDetectionLevel.ROW);
97  
98          TSOClient client = TSOClient.newInstance(tsoClientConf);
99  
100         CellId c1 = new DummyCellIdImpl(0xdeadbeefL, 0xdeadbeeeL);
101         CellId c2 = new DummyCellIdImpl(0xfeedcafeL, 0xdeadbeeeL);
102 
103         Set<CellId> testWriteSet1 = Sets.newHashSet(c1);
104         Set<CellId> testWriteSet2 = Sets.newHashSet(c2);
105         
106         long ts1 = client.getNewStartTimestamp().get();
107         long ts2 = client.getNewStartTimestamp().get();
108         
109         client.commit(ts1, testWriteSet1).get();
110 
111         try {
112             client.commit(ts2, testWriteSet2).get();
113         } catch (ExecutionException e) {
114             assertTrue(e.getCause() instanceof AbortException, "Transaction should be aborted");
115             return;
116         }
117 
118         assertTrue(false, "Transaction should be aborted");
119     }
120 
121     @Test(timeOut = 30_000)
122     public void testRowLevelConflictAnalysisCommit() throws Exception {
123 
124         tsoClientConf.setConflictAnalysisLevel(ConflictDetectionLevel.ROW);
125 
126         TSOClient client = TSOClient.newInstance(tsoClientConf);
127 
128         CellId c1 = new DummyCellIdImpl(0xdeadbeefL, 0xdeadbeeeL);
129         CellId c2 = new DummyCellIdImpl(0xfeedcafeL, 0xdeadbeefL);
130 
131         Set<CellId> testWriteSet1 = Sets.newHashSet(c1);
132         Set<CellId> testWriteSet2 = Sets.newHashSet(c2);
133         
134         long ts1 = client.getNewStartTimestamp().get();
135         long ts2 = client.getNewStartTimestamp().get();
136         
137         client.commit(ts1, testWriteSet1).get();
138 
139         try {
140             client.commit(ts2, testWriteSet2).get();
141         } catch (ExecutionException e) {
142             assertFalse(e.getCause() instanceof AbortException, "Transaction should be committed");
143             return;
144         }
145 
146         assertTrue(true, "Transaction should be committed");
147     }
148 
149     @Test(timeOut = 30_000)
150     public void testCellLevelConflictAnalysisConflict() throws Exception {
151 
152         tsoClientConf.setConflictAnalysisLevel(ConflictDetectionLevel.CELL);
153 
154         TSOClient client = TSOClient.newInstance(tsoClientConf);
155 
156         CellId c1 = new DummyCellIdImpl(0xdeadbeefL, 0xdeadbeeeL);
157         CellId c2 = new DummyCellIdImpl(0xdeadbeefL, 0xdeadbeeeL);
158 
159         Set<CellId> testWriteSet1 = Sets.newHashSet(c1);
160         Set<CellId> testWriteSet2 = Sets.newHashSet(c2);
161         
162         long ts1 = client.getNewStartTimestamp().get();
163         long ts2 = client.getNewStartTimestamp().get();
164         
165         client.commit(ts1, testWriteSet1).get();
166 
167         try {
168             client.commit(ts2, testWriteSet2).get();
169         } catch (ExecutionException e) {
170             assertTrue(e.getCause() instanceof AbortException, "Transaction should be aborted");
171             return;
172         }
173 
174         assertTrue(false, "Transaction should be aborted");
175     }
176 
177     @Test(timeOut = 30_000)
178     public void testCellLevelConflictAnalysisCommit() throws Exception {
179 
180         tsoClientConf.setConflictAnalysisLevel(ConflictDetectionLevel.CELL);
181 
182         TSOClient client = TSOClient.newInstance(tsoClientConf);
183 
184         CellId c1 = new DummyCellIdImpl(0xdeadbeefL, 0xdeadbeeeL);
185         CellId c2 = new DummyCellIdImpl(0xfeedcafeL, 0xdeadbeefL);
186 
187         Set<CellId> testWriteSet1 = Sets.newHashSet(c1);
188         Set<CellId> testWriteSet2 = Sets.newHashSet(c2);
189         
190         long ts1 = client.getNewStartTimestamp().get();
191         long ts2 = client.getNewStartTimestamp().get();
192         
193         client.commit(ts1, testWriteSet1).get();
194 
195         try {
196             client.commit(ts2, testWriteSet2).get();
197         } catch (ExecutionException e) {
198             assertFalse(e.getCause() instanceof AbortException, "Transaction should be committed");
199             return;
200         }
201 
202         assertTrue(true, "Transaction should be committed");
203     }
204     
205 }