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