1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
57 hbaseConf = HBaseConfiguration.create();
58 hbaseConf.setBoolean("hbase.localcluster.assign.random.ports",true);
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(TableName.valueOf(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(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME))) {
86 admin.enableTable(TableName.valueOf(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(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME));
100 admin.deleteTable(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME));
101
102 } catch (IOException e) {
103 LOG.error("Error tearing down", e);
104 }
105 }
106
107 @Test(timeOut = 10_000)
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
115 assertEquals(tsStorage.getMaxTimestamp(), INITIAL_TS_VALUE, "Initial value should be " + INITIAL_TS_VALUE);
116
117
118 long newTimestamp = 1;
119 tsStorage.updateMaxTimestamp(INITIAL_TS_VALUE, newTimestamp);
120
121
122 long wrongTimestamp = 20;
123 try {
124 tsStorage.updateMaxTimestamp(wrongTimestamp, newTimestamp);
125 Assert.fail("Shouldn't update");
126 } catch (IOException e) {
127
128 }
129 assertEquals(tsStorage.getMaxTimestamp(), newTimestamp, "Value should be still " + newTimestamp);
130
131
132 long veryNewTimestamp = 40;
133 tsStorage.updateMaxTimestamp(newTimestamp, veryNewTimestamp);
134 assertEquals(tsStorage.getMaxTimestamp(), veryNewTimestamp, "Value should be " + veryNewTimestamp);
135
136 }
137
138 }