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.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
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
68
69
70
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 }