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.transaction;
19  
20  import org.apache.hadoop.hbase.client.Get;
21  import org.apache.hadoop.hbase.client.Put;
22  import org.apache.hadoop.hbase.client.Result;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.testng.ITestContext;
27  import org.testng.annotations.Test;
28  
29  import static org.testng.Assert.assertTrue;
30  
31  @Test(groups = "sharedHBase")
32  public class TestMultiplePut extends OmidTestBase {
33  
34      private static final Logger LOG = LoggerFactory.getLogger(TestMultiplePut.class);
35  
36      private static final byte[] testTable = Bytes.toBytes(TEST_TABLE);
37      private static final byte[] family = Bytes.toBytes(TEST_FAMILY);
38      private static final byte[] col1 = Bytes.toBytes("col1");
39      private static final byte[] col2 = Bytes.toBytes("col2");
40      private static final byte[] data = Bytes.toBytes("testData");
41  
42      @Test(timeOut = 30_000)
43      public void testMultiPutInTwoDifferentColsOfSameRowAreInTheTableAfterCommit(ITestContext context) throws Exception {
44  
45          TransactionManager tm = newTransactionManager(context);
46  
47          try (TTable txTable = new TTable(hbaseConf, TEST_TABLE)) {
48  
49              Transaction tx = tm.begin();
50  
51              byte[] rowToAdd = Bytes.toBytes(1000);
52  
53              Put put1 = new Put(rowToAdd);
54              put1.add(family, col1, data);
55              txTable.put(tx, put1);
56  
57              Put put2 = new Put(rowToAdd);
58              put2.add(family, col2, data);
59              txTable.put(tx, put2);
60  
61              tm.commit(tx);
62  
63              assertTrue(verifyValue(testTable, rowToAdd, family, col1, data), "Invalid value in table");
64              assertTrue(verifyValue(testTable, rowToAdd, family, col2, data), "Invalid value in table");
65          }
66  
67      }
68  
69      @Test(timeOut = 30_000)
70      public void testManyManyPutsInDifferentRowsAreInTheTableAfterCommit(ITestContext context) throws Exception {
71  
72          final int NUM_ROWS_TO_ADD = 50;
73  
74          TransactionManager tm = newTransactionManager(context);
75  
76          try (TTable txTable = new TTable(hbaseConf, TEST_TABLE)) {
77  
78              Transaction tx = tm.begin();
79  
80              for (int i = 0; i <= NUM_ROWS_TO_ADD; i++) {
81                  byte[] rowToAdd = Bytes.toBytes(i);
82                  byte[] dataForRowCol = Bytes.toBytes("testData" + i);
83                  Put put = new Put(rowToAdd);
84                  put.add(family, col1, dataForRowCol);
85                  txTable.put(tx, put);
86              }
87  
88              tm.commit(tx);
89  
90              // Check some of the added values are there in the table
91              byte[] rowToCheck = Bytes.toBytes(0);
92              byte[] dataToCheck = Bytes.toBytes("testData" + 0);
93              assertTrue(verifyValue(testTable, rowToCheck, family, col1, dataToCheck), "Invalid value in table");
94              rowToCheck = Bytes.toBytes(NUM_ROWS_TO_ADD / 2);
95              dataToCheck = Bytes.toBytes("testData" + (NUM_ROWS_TO_ADD / 2));
96              assertTrue(verifyValue(testTable, rowToCheck, family, col1, dataToCheck), "Invalid value in table");
97              rowToCheck = Bytes.toBytes(NUM_ROWS_TO_ADD);
98              dataToCheck = Bytes.toBytes("testData" + NUM_ROWS_TO_ADD);
99              assertTrue(verifyValue(testTable, rowToCheck, family, col1, dataToCheck), "Invalid value in table");
100 
101         }
102     }
103 
104     @Test(timeOut = 30_000)
105     public void testGetFromNonExistentRowAfterMultiplePutsReturnsNoResult(ITestContext context) throws Exception {
106 
107         final int NUM_ROWS_TO_ADD = 10;
108 
109         TransactionManager tm = newTransactionManager(context);
110 
111         try (TTable txTable = new TTable(hbaseConf, TEST_TABLE)) {
112 
113             Transaction tx = tm.begin();
114 
115             for (int i = 0; i < NUM_ROWS_TO_ADD; i++) {
116                 byte[] rowToAdd = Bytes.toBytes(i);
117                 Put put = new Put(rowToAdd);
118                 put.add(family, col1, Bytes.toBytes("testData" + i));
119                 txTable.put(tx, put);
120             }
121 
122             byte[] nonExistentRow = Bytes.toBytes(NUM_ROWS_TO_ADD + 5);
123             Get get = new Get(nonExistentRow);
124             Result result = txTable.get(tx, get);
125 
126             assertTrue(result.isEmpty(), "Found a row that should not exist");
127 
128             tm.commit(tx);
129 
130         }
131 
132     }
133 
134 }