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