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.hadoop.hbase.coprocessor.example;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.MediumTests;
29  import org.apache.hadoop.hbase.client.Get;
30  import org.apache.hadoop.hbase.client.HConnectionManager;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
37  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
38  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
39  import org.apache.zookeeper.ZooKeeper;
40  import org.junit.AfterClass;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(MediumTests.class)
46  public class TestZooKeeperScanPolicyObserver {
47    private static final Log LOG = LogFactory.getLog(TestZooKeeperScanPolicyObserver.class);
48    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
49    private static final byte[] F = Bytes.toBytes("fam");
50    private static final byte[] Q = Bytes.toBytes("qual");
51    private static final byte[] R = Bytes.toBytes("row");
52  
53    @BeforeClass
54    public static void setUpBeforeClass() throws Exception {
55      // Test we can first start the ZK cluster by itself
56      Configuration conf = TEST_UTIL.getConfiguration();
57      conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
58          ZooKeeperScanPolicyObserver.class.getName());
59      TEST_UTIL.startMiniZKCluster();
60      TEST_UTIL.startMiniCluster();
61    }
62  
63    @AfterClass
64    public static void tearDownAfterClass() throws Exception {
65      TEST_UTIL.shutdownMiniCluster();
66    }
67  
68    @Test
69    public void testScanPolicyObserver() throws Exception {
70      byte[] tableName = Bytes.toBytes("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 = HConnectionManager.getConnection(TEST_UTIL.getConfiguration())
81          .getZooKeeperWatcher();
82      ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper();
83      ZKUtil.createWithParents(zkw, ZooKeeperScanPolicyObserver.node);
84      // let's say test last backup was 1h ago
85      // using plain ZK here, because RecoverableZooKeeper add extra encoding to the data
86      zk.setData(ZooKeeperScanPolicyObserver.node, Bytes.toBytes(now - 3600*1000), -1);
87  
88      LOG.debug("Set time: "+Bytes.toLong(Bytes.toBytes(now - 3600*1000)));
89  
90      // sleep for 1s to give the ZK change a chance to reach the watcher in the observer.
91      // TODO: Better to wait for the data to be propagated
92      Thread.sleep(1000);
93  
94      long ts = now - 2000;
95      Put p = new Put(R);
96      p.add(F, Q, ts, Q);
97      t.put(p);
98      p = new Put(R);
99      p.add(F, Q, ts+1, Q);
100     t.put(p);
101 
102     // these two should be expired but for the override
103     // (their ts was 2s in the past)
104     Get g = new Get(R);
105     g.setMaxVersions(10);
106     Result r = t.get(g);
107     // still there?
108     assertEquals(2, r.size());
109 
110     TEST_UTIL.flush(tableName);
111     TEST_UTIL.compact(tableName, true);
112 
113     g = new Get(R);
114     g.setMaxVersions(10);
115     r = t.get(g);
116     // still there?
117     assertEquals(2, r.size());
118     zk.setData(ZooKeeperScanPolicyObserver.node, Bytes.toBytes(now), -1);
119     LOG.debug("Set time: "+now);
120 
121     TEST_UTIL.compact(tableName, true);
122 
123     g = new Get(R);
124     g.setMaxVersions(10);
125     r = t.get(g);
126     // should be gone now
127     assertEquals(0, r.size());
128     t.close();
129   }
130 }