1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
67
68
69
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 }