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 org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FileSystem;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.HBaseConfiguration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.KeyValue;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.hadoop.hbase.wal.WAL;
36  import org.apache.hadoop.hbase.wal.WALProvider;
37  import org.apache.hadoop.hbase.wal.WALFactory;
38  import org.apache.hadoop.hbase.wal.WALKey;
39  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.FSUtils;
42  import org.junit.BeforeClass;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  @Category(MediumTests.class)
47  public class TestReplicationSource {
48  
49    private static final Log LOG =
50        LogFactory.getLog(TestReplicationSource.class);
51    private final static HBaseTestingUtility TEST_UTIL =
52        new HBaseTestingUtility();
53    private static FileSystem FS;
54    private static Path oldLogDir;
55    private static Path logDir;
56    private static Configuration conf = HBaseConfiguration.create();
57  
58    /**
59     * @throws java.lang.Exception
60     */
61    @BeforeClass
62    public static void setUpBeforeClass() throws Exception {
63      TEST_UTIL.startMiniDFSCluster(1);
64      FS = TEST_UTIL.getDFSCluster().getFileSystem();
65      Path rootDir = TEST_UTIL.createRootDir();
66      oldLogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME);
67      if (FS.exists(oldLogDir)) FS.delete(oldLogDir, true);
68      logDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
69      if (FS.exists(logDir)) FS.delete(logDir, true);
70    }
71  
72    /**
73     * Sanity check that we can move logs around while we are reading
74     * from them. Should this test fail, ReplicationSource would have a hard
75     * time reading logs that are being archived.
76     * @throws Exception
77     */
78    @Test
79    public void testLogMoving() throws Exception{
80      Path logPath = new Path(logDir, "log");
81      if (!FS.exists(logDir)) FS.mkdirs(logDir);
82      if (!FS.exists(oldLogDir)) FS.mkdirs(oldLogDir);
83      WALProvider.Writer writer = WALFactory.createWALWriter(FS, logPath,
84          TEST_UTIL.getConfiguration());
85      for(int i = 0; i < 3; i++) {
86        byte[] b = Bytes.toBytes(Integer.toString(i));
87        KeyValue kv = new KeyValue(b,b,b);
88        WALEdit edit = new WALEdit();
89        edit.add(kv);
90        WALKey key = new WALKey(b, TableName.valueOf(b), 0, 0,
91            HConstants.DEFAULT_CLUSTER_ID);
92        writer.append(new WAL.Entry(key, edit));
93        writer.sync();
94      }
95      writer.close();
96  
97      WAL.Reader reader = WALFactory.createReader(FS, logPath, TEST_UTIL.getConfiguration());
98      WAL.Entry entry = reader.next();
99      assertNotNull(entry);
100 
101     Path oldLogPath = new Path(oldLogDir, "log");
102     FS.rename(logPath, oldLogPath);
103 
104     entry = reader.next();
105     assertNotNull(entry);
106 
107     entry = reader.next();
108     entry = reader.next();
109 
110     assertNull(entry);
111     reader.close();
112   }
113 
114 }
115