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.testclassification.MediumTests;
35 import org.apache.hadoop.hbase.wal.WAL;
36 import org.apache.hadoop.hbase.wal.WALProvider;
37 import org.apache.hadoop.hbase.wal.WALFactory;
38 import org.apache.hadoop.hbase.wal.WALKey;
39 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.apache.hadoop.hbase.util.FSUtils;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45
46 @Category(MediumTests.class)
47 public class TestReplicationSource {
48
49 private static final Log LOG =
50 LogFactory.getLog(TestReplicationSource.class);
51 private final static HBaseTestingUtility TEST_UTIL =
52 new HBaseTestingUtility();
53 private static FileSystem FS;
54 private static Path oldLogDir;
55 private static Path logDir;
56 private static Configuration conf = HBaseConfiguration.create();
57
58
59
60
61 @BeforeClass
62 public static void setUpBeforeClass() throws Exception {
63 TEST_UTIL.startMiniDFSCluster(1);
64 FS = TEST_UTIL.getDFSCluster().getFileSystem();
65 Path rootDir = TEST_UTIL.createRootDir();
66 oldLogDir = new Path(rootDir, HConstants.HREGION_OLDLOGDIR_NAME);
67 if (FS.exists(oldLogDir)) FS.delete(oldLogDir, true);
68 logDir = new Path(rootDir, HConstants.HREGION_LOGDIR_NAME);
69 if (FS.exists(logDir)) FS.delete(logDir, true);
70 }
71
72
73
74
75
76
77
78 @Test
79 public void testLogMoving() throws Exception{
80 Path logPath = new Path(logDir, "log");
81 if (!FS.exists(logDir)) FS.mkdirs(logDir);
82 if (!FS.exists(oldLogDir)) FS.mkdirs(oldLogDir);
83 WALProvider.Writer writer = WALFactory.createWALWriter(FS, logPath,
84 TEST_UTIL.getConfiguration());
85 for(int i = 0; i < 3; i++) {
86 byte[] b = Bytes.toBytes(Integer.toString(i));
87 KeyValue kv = new KeyValue(b,b,b);
88 WALEdit edit = new WALEdit();
89 edit.add(kv);
90 WALKey key = new WALKey(b, TableName.valueOf(b), 0, 0,
91 HConstants.DEFAULT_CLUSTER_ID);
92 writer.append(new WAL.Entry(key, edit));
93 writer.sync();
94 }
95 writer.close();
96
97 WAL.Reader reader = WALFactory.createReader(FS, logPath, TEST_UTIL.getConfiguration());
98 WAL.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 reader.close();
112 }
113
114 }
115