1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.coprocessor.example;
20
21 import static org.junit.Assert.assertEquals;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HColumnDescriptor;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.MediumTests;
31 import org.apache.hadoop.hbase.client.Get;
32 import org.apache.hadoop.hbase.client.HTable;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.client.Result;
35 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
38 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
39 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
40 import org.apache.zookeeper.ZooKeeper;
41 import org.junit.experimental.categories.Category;
42
43 @Category(MediumTests.class)
44 public class TestZooKeeperScanPolicyObserver {
45 private static final Log LOG = LogFactory.getLog(TestZooKeeperScanPolicyObserver.class);
46 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47 private static final byte[] F = Bytes.toBytes("fam");
48 private static final byte[] Q = Bytes.toBytes("qual");
49 private static final byte[] R = Bytes.toBytes("row");
50
51
52 public static void setUpBeforeClass() throws Exception {
53 System.out.println("HERE!!!!!!!!");
54
55 Configuration conf = TEST_UTIL.getConfiguration();
56 conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
57 ZooKeeperScanPolicyObserver.class.getName());
58 TEST_UTIL.startMiniZKCluster();
59 TEST_UTIL.startMiniCluster();
60 }
61
62
63 public static void tearDownAfterClass() throws Exception {
64 TEST_UTIL.shutdownMiniCluster();
65 }
66
67
68 public void testScanPolicyObserver() throws Exception {
69 TableName tableName =
70 TableName.valueOf("testScanPolicyObserver");
71 HTableDescriptor desc = new HTableDescriptor(tableName);
72 HColumnDescriptor hcd = new HColumnDescriptor(F)
73 .setMaxVersions(10)
74 .setTimeToLive(1);
75 desc.addFamily(hcd);
76 TEST_UTIL.getHBaseAdmin().createTable(desc);
77 HTable t = new HTable(new Configuration(TEST_UTIL.getConfiguration()), tableName);
78 long now = EnvironmentEdgeManager.currentTimeMillis();
79
80 ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(), "test", null);
81 ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper();
82 ZKUtil.createWithParents(zkw, ZooKeeperScanPolicyObserver.node);
83
84
85 zk.setData(ZooKeeperScanPolicyObserver.node, Bytes.toBytes(now - 3600*1000), -1);
86
87 LOG.debug("Set time: "+Bytes.toLong(Bytes.toBytes(now - 3600*1000)));
88
89
90
91 Thread.sleep(1000);
92
93 long ts = now - 2000;
94 Put p = new Put(R);
95 p.add(F, Q, ts, Q);
96 t.put(p);
97 p = new Put(R);
98 p.add(F, Q, ts+1, Q);
99 t.put(p);
100
101
102
103 Get g = new Get(R);
104 g.setMaxVersions(10);
105 Result r = t.get(g);
106
107 assertEquals(2, r.size());
108
109 TEST_UTIL.flush(tableName);
110 TEST_UTIL.compact(tableName, true);
111
112 g = new Get(R);
113 g.setMaxVersions(10);
114 r = t.get(g);
115
116 assertEquals(2, r.size());
117 zk.setData(ZooKeeperScanPolicyObserver.node, Bytes.toBytes(now), -1);
118 LOG.debug("Set time: "+now);
119
120 TEST_UTIL.compact(tableName, true);
121
122 g = new Get(R);
123 g.setMaxVersions(10);
124 r = t.get(g);
125
126 assertEquals(0, r.size());
127 t.close();
128 }
129 }