View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
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.testclassification.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.client.Table;
36  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
39  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
40  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
41  import org.apache.zookeeper.ZooKeeper;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(MediumTests.class)
45  public class TestZooKeeperScanPolicyObserver {
46    private static final Log LOG = LogFactory.getLog(TestZooKeeperScanPolicyObserver.class);
47    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48    private static final byte[] F = Bytes.toBytes("fam");
49    private static final byte[] Q = Bytes.toBytes("qual");
50    private static final byte[] R = Bytes.toBytes("row");
51  
52    // @BeforeClass
53    public static void setUpBeforeClass() throws Exception {
54      System.out.println("HERE!!!!!!!!");
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    // @Ignore @Test
69    public void testScanPolicyObserver() throws Exception {
70      TableName tableName =
71          TableName.valueOf("testScanPolicyObserver");
72      HTableDescriptor desc = new HTableDescriptor(tableName);
73      HColumnDescriptor hcd = new HColumnDescriptor(F)
74      .setMaxVersions(10)
75      .setTimeToLive(1);
76      desc.addFamily(hcd);
77      TEST_UTIL.getHBaseAdmin().createTable(desc);
78      Table t = new HTable(new Configuration(TEST_UTIL.getConfiguration()), tableName);
79      long now = EnvironmentEdgeManager.currentTime();
80  
81      ZooKeeperWatcher zkw = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(), "test", null);
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 }