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