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.timestamp.storage;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.HBaseConfiguration;
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.HColumnDescriptor;
24  import org.apache.hadoop.hbase.HTableDescriptor;
25  import org.apache.hadoop.hbase.MiniHBaseCluster;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.testng.Assert;
31  import org.testng.annotations.AfterClass;
32  import org.testng.annotations.AfterMethod;
33  import org.testng.annotations.BeforeClass;
34  import org.testng.annotations.BeforeMethod;
35  import org.testng.annotations.Test;
36  
37  import java.io.IOException;
38  
39  import static org.apache.omid.timestamp.storage.HBaseTimestampStorageConfig.DEFAULT_TIMESTAMP_STORAGE_CF_NAME;
40  import static org.apache.omid.timestamp.storage.HBaseTimestampStorageConfig.DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME;
41  import static org.testng.Assert.assertEquals;
42  
43  public class TestHBaseTimestampStorage {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(TestHBaseTimestampStorage.class);
46  
47      private static final TableName TABLE_NAME = TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME);
48  
49      private static HBaseTestingUtility testutil;
50      private static MiniHBaseCluster hbasecluster;
51      protected static Configuration hbaseConf;
52  
53  
54      @BeforeClass
55      public static void setUpClass() throws Exception {
56          // HBase setup
57          hbaseConf = HBaseConfiguration.create();
58  
59          LOG.info("Create hbase");
60          testutil = new HBaseTestingUtility(hbaseConf);
61          hbasecluster = testutil.startMiniCluster(1);
62  
63      }
64  
65      @AfterClass
66      public static void tearDownClass() throws Exception {
67          if (hbasecluster != null) {
68              testutil.shutdownMiniCluster();
69          }
70      }
71  
72      @BeforeMethod
73      public void setUp() throws Exception {
74          HBaseAdmin admin = testutil.getHBaseAdmin();
75  
76          if (!admin.tableExists(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME)) {
77              HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
78              HColumnDescriptor datafam = new HColumnDescriptor(DEFAULT_TIMESTAMP_STORAGE_CF_NAME);
79              datafam.setMaxVersions(Integer.MAX_VALUE);
80              desc.addFamily(datafam);
81  
82              admin.createTable(desc);
83          }
84  
85          if (admin.isTableDisabled(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME)) {
86              admin.enableTable(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME);
87          }
88          HTableDescriptor[] tables = admin.listTables();
89          for (HTableDescriptor t : tables) {
90              LOG.info(t.getNameAsString());
91          }
92      }
93  
94      @AfterMethod
95      public void tearDown() {
96          try {
97              LOG.info("tearing Down");
98              HBaseAdmin admin = testutil.getHBaseAdmin();
99              admin.disableTable(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME);
100             admin.deleteTable(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME);
101 
102         } catch (Exception e) {
103             LOG.error("Error tearing down", e);
104         }
105     }
106 
107     @Test
108     public void testHBaseTimestampStorage() throws Exception {
109 
110         final long INITIAL_TS_VALUE = 0;
111         HBaseTimestampStorageConfig config = new HBaseTimestampStorageConfig();
112         HBaseTimestampStorage tsStorage = new HBaseTimestampStorage(hbaseConf, config);
113 
114         // Test that the first time we get the timestamp is the initial value
115         assertEquals(tsStorage.getMaxTimestamp(), INITIAL_TS_VALUE, "Initial value should be " + INITIAL_TS_VALUE);
116 
117         // Test that updating the timestamp succeeds when passing the initial value as the previous one
118         long newTimestamp = 1;
119         tsStorage.updateMaxTimestamp(INITIAL_TS_VALUE, newTimestamp);
120 
121         // Test setting a new timestamp fails (exception is thrown) when passing a wrong previous max timestamp
122         long wrongTimestamp = 20;
123         try {
124             tsStorage.updateMaxTimestamp(wrongTimestamp, newTimestamp);
125             Assert.fail("Shouldn't update");
126         } catch (IOException e) {
127             // Correct behavior
128         }
129         assertEquals(tsStorage.getMaxTimestamp(), newTimestamp, "Value should be still " + newTimestamp);
130 
131         // Test we can set a new timestamp when passing the right previous max timestamp
132         long veryNewTimestamp = 40;
133         tsStorage.updateMaxTimestamp(newTimestamp, veryNewTimestamp);
134         assertEquals(tsStorage.getMaxTimestamp(), veryNewTimestamp, "Value should be " + veryNewTimestamp);
135 
136     }
137 
138 }