View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.replication;
20  
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNull;
23  
24  import java.util.concurrent.ExecutorService;
25  import java.util.concurrent.Executors;
26  import java.util.concurrent.Future;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.Waiter;
35  import org.apache.hadoop.hbase.Waiter.Predicate;
36  import org.apache.hadoop.hbase.HBaseConfiguration;
37  import org.apache.hadoop.hbase.HBaseTestingUtility;
38  import org.apache.hadoop.hbase.HConstants;
39  import org.apache.hadoop.hbase.KeyValue;
40  import org.apache.hadoop.hbase.testclassification.MediumTests;
41  import org.apache.hadoop.hbase.wal.WAL;
42  import org.apache.hadoop.hbase.wal.WALProvider;
43  import org.apache.hadoop.hbase.wal.WALFactory;
44  import org.apache.hadoop.hbase.wal.WALKey;
45  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
46  import org.apache.hadoop.hbase.replication.regionserver.HBaseInterClusterReplicationEndpoint;
47  import org.apache.hadoop.hbase.replication.regionserver.ReplicationSource;
48  import org.apache.hadoop.hbase.util.Bytes;
49  import org.apache.hadoop.hbase.util.FSUtils;
50  import org.junit.BeforeClass;
51  import org.junit.Test;
52  import org.junit.experimental.categories.Category;
53  
54  import static org.mockito.Mockito.mock;
55  
56  @Category(MediumTests.class)
57  public class TestReplicationSource {
58  
59    private static final Log LOG =
60        LogFactory.getLog(TestReplicationSource.class);
61    private final static HBaseTestingUtility TEST_UTIL =
62        new HBaseTestingUtility();
63    private static FileSystem FS;
64    private static Path oldLogDir;
65    private static Path logDir;
66    private static Configuration conf = HBaseConfiguration.create();
67  
68    /**
69     * @throws java.lang.Exception
70     */
71    @BeforeClass
72    public static void setUpBeforeClass() throws Exception {
73      TEST_UTIL.startMiniDFSCluster(1);
74      FS = TEST_UTIL.getDFSCluster().getFileSystem();
75      Path rootDir = TEST_UTIL.createRootDir();
76      oldLogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME);
77      if (FS.exists(oldLogDir)) FS.delete(oldLogDir, true);
78      logDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
79      if (FS.exists(logDir)) FS.delete(logDir, true);
80    }
81  
82    /**
83     * Sanity check that we can move logs around while we are reading
84     * from them. Should this test fail, ReplicationSource would have a hard
85     * time reading logs that are being archived.
86     * @throws Exception
87     */
88    @Test
89    public void testLogMoving() throws Exception{
90      Path logPath = new Path(logDir, "log");
91      if (!FS.exists(logDir)) FS.mkdirs(logDir);
92      if (!FS.exists(oldLogDir)) FS.mkdirs(oldLogDir);
93      WALProvider.Writer writer = WALFactory.createWALWriter(FS, logPath,
94          TEST_UTIL.getConfiguration());
95      for(int i = 0; i < 3; i++) {
96        byte[] b = Bytes.toBytes(Integer.toString(i));
97        KeyValue kv = new KeyValue(b,b,b);
98        WALEdit edit = new WALEdit();
99        edit.add(kv);
100       WALKey key = new WALKey(b, TableName.valueOf(b), 0, 0,
101           HConstants.DEFAULT_CLUSTER_ID);
102       writer.append(new WAL.Entry(key, edit));
103       writer.sync();
104     }
105     writer.close();
106 
107     WAL.Reader reader = WALFactory.createReader(FS, logPath, TEST_UTIL.getConfiguration());
108     WAL.Entry entry = reader.next();
109     assertNotNull(entry);
110 
111     Path oldLogPath = new Path(oldLogDir, "log");
112     FS.rename(logPath, oldLogPath);
113 
114     entry = reader.next();
115     assertNotNull(entry);
116 
117     entry = reader.next();
118     entry = reader.next();
119 
120     assertNull(entry);
121     reader.close();
122   }
123 
124   /**
125    * Tests that {@link ReplicationSource#terminate(String)} will timeout properly
126    */
127   @Test
128   public void testTerminateTimeout() throws Exception {
129     final ReplicationSource source = new ReplicationSource();
130     ReplicationEndpoint replicationEndpoint = new HBaseInterClusterReplicationEndpoint() {
131       @Override
132       protected void doStart() {
133         notifyStarted();
134       }
135 
136       @Override
137       protected void doStop() {
138         // not calling notifyStopped() here causes the caller of stop() to get a Future that never
139         // completes
140       }
141     };
142     replicationEndpoint.start();
143     ReplicationPeers mockPeers = mock(ReplicationPeers.class);
144     Configuration testConf = HBaseConfiguration.create();
145     testConf.setInt("replication.source.maxretriesmultiplier", 1);
146     source.init(testConf, null, null, null, mockPeers, null, "testPeer", null, replicationEndpoint,
147       null);
148     ExecutorService executor = Executors.newSingleThreadExecutor();
149     final Future<?> future = executor.submit(new Runnable() {
150 
151       @Override
152       public void run() {
153         source.terminate("testing source termination");
154       }
155     });
156     long sleepForRetries = testConf.getLong("replication.source.sleepforretries", 1000);
157     Waiter.waitFor(testConf, sleepForRetries * 2, new Predicate<Exception>() {
158 
159       @Override
160       public boolean evaluate() throws Exception {
161         return future.isDone();
162       }
163 
164     });
165 
166   }
167 
168 }
169