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.replication;
19  
20  import junit.framework.Assert;
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.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
28  import org.apache.zookeeper.KeeperException.SessionExpiredException;
29  import org.junit.*;
30  
31  public class TestReplicationPeer {
32  
33    private static final Log LOG = LogFactory.getLog(TestReplicationPeer.class);
34    private static HBaseTestingUtility utility;
35    private static Configuration conf;
36    private static ReplicationPeer rp;
37  
38    @BeforeClass
39    public static void setUpBeforeClass() throws Exception {
40      conf = HBaseConfiguration.create();
41      utility = new HBaseTestingUtility(conf);
42      conf = utility.getConfiguration();
43      utility.startMiniZKCluster();
44  
45      rp = new ReplicationPeer(conf, "clusterKey", "clusterId");
46    }
47  
48    @Test(timeout=300000)
49    public void testResetZooKeeperSession() throws Exception {
50      ZooKeeperWatcher zkw = rp.getZkw();
51      zkw.getRecoverableZooKeeper().exists("/1/2", false);
52  
53      LOG.info("Expiring ReplicationPeer ZooKeeper session.");
54      utility.expireSession(zkw, null, false);
55  
56      try {
57        LOG.info("Attempting to use expired ReplicationPeer ZooKeeper session.");
58        // Trying to use the expired session to assert that it is indeed closed
59        zkw.getRecoverableZooKeeper().exists("/1/2", false);
60      } catch (SessionExpiredException k) {
61        rp.reloadZkWatcher();
62  
63        zkw = rp.getZkw();
64  
65        // Try to use the connection again
66        LOG.info("Attempting to use refreshed "
67            + "ReplicationPeer ZooKeeper session.");
68        zkw.getRecoverableZooKeeper().exists("/1/2", false);
69  
70        return;
71      }
72  
73      Assert.fail("ReplicationPeer ZooKeeper session was not properly expired.");
74    }
75  
76  }