1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.replication;
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.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.regionserver.wal.HLog;
32  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
33  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  
38  import static org.junit.Assert.assertNotNull;
39  import static org.junit.Assert.assertNull;
40  
41  public class TestReplicationSource {
42  
43    private static final Log LOG =
44        LogFactory.getLog(TestReplicationSource.class);
45    private final static HBaseTestingUtility TEST_UTIL =
46        new HBaseTestingUtility();
47    private static FileSystem fs;
48    private static Path oldLogDir;
49    private static Path logDir;
50    private static Configuration conf = HBaseConfiguration.create();
51  
52    /**
53     * @throws java.lang.Exception
54     */
55    @BeforeClass
56    public static void setUpBeforeClass() throws Exception {
57      TEST_UTIL.startMiniDFSCluster(1);
58      fs = TEST_UTIL.getDFSCluster().getFileSystem();
59      oldLogDir = new Path(fs.getHomeDirectory(),
60          HConstants.HREGION_OLDLOGDIR_NAME);
61      logDir = new Path(fs.getHomeDirectory(),
62          HConstants.HREGION_LOGDIR_NAME);
63    }
64  
65    /**
66     * Sanity check that we can move logs around while we are reading
67     * from them. Should this test fail, ReplicationSource would have a hard
68     * time reading logs that are being archived.
69     * @throws Exception
70     */
71    @Test
72    public void testLogMoving() throws Exception{
73      Path logPath = new Path(logDir, "log");
74      HLog.Writer writer = HLog.createWriter(fs, logPath, conf);
75      for(int i = 0; i < 3; i++) {
76        byte[] b = Bytes.toBytes(Integer.toString(i));
77        KeyValue kv = new KeyValue(b,b,b);
78        WALEdit edit = new WALEdit();
79        edit.add(kv);
80        HLogKey key = new HLogKey(b, b, 0, 0);
81        writer.append(new HLog.Entry(key, edit));
82        writer.sync();
83      }
84      writer.close();
85  
86      HLog.Reader reader = HLog.getReader(fs, logPath, conf);
87      HLog.Entry entry = reader.next();
88      assertNotNull(entry);
89  
90      Path oldLogPath = new Path(oldLogDir, "log");
91      fs.rename(logPath, oldLogPath);
92  
93      entry = reader.next();
94      assertNotNull(entry);
95  
96      entry = reader.next();
97      entry = reader.next();
98  
99      assertNull(entry);
100 
101   }
102 }