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.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    // @BeforeClass
52    public static void setUpBeforeClass() throws Exception {
53      System.out.println("HERE!!!!!!!!");
54      // Test we can first start the ZK cluster by itself
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    // @AfterClass
63    public static void tearDownAfterClass() throws Exception {
64      TEST_UTIL.shutdownMiniCluster();
65    }
66  
67    // @Ignore @Test
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      // let's say test last backup was 1h ago
84      // using plain ZK here, because RecoverableZooKeeper add extra encoding to the data
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      // sleep for 1s to give the ZK change a chance to reach the watcher in the observer.
90      // TODO: Better to wait for the data to be propagated
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     // these two should be expired but for the override
102     // (their ts was 2s in the past)
103     Get g = new Get(R);
104     g.setMaxVersions(10);
105     Result r = t.get(g);
106     // still there?
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     // still there?
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     // should be gone now
126     assertEquals(0, r.size());
127     t.close();
128   }
129 }