1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.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
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
73
74
75
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