1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.wal;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.hbase.HBaseTestingUtility;
36 import org.apache.hadoop.hbase.HConstants;
37 import org.apache.hadoop.hbase.HRegionInfo;
38 import org.apache.hadoop.hbase.KeyValue;
39 import org.apache.hadoop.hbase.testclassification.MediumTests;
40 import org.apache.hadoop.hbase.TableName;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.apache.hadoop.hbase.wal.WAL;
43 import org.apache.hadoop.hbase.wal.WALFactory;
44 import org.apache.hadoop.hbase.wal.WALKey;
45 import org.apache.hadoop.hbase.wal.WALProvider;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50
51
52
53
54 @Category(MediumTests.class)
55
56 public class TestReadOldRootAndMetaEdits {
57
58 private final static Log LOG = LogFactory.getLog(TestReadOldRootAndMetaEdits.class);
59 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
60 private static Configuration conf;
61 private static FileSystem fs;
62 private static Path dir;
63
64 @BeforeClass
65 public static void setupBeforeClass() throws Exception {
66 conf = TEST_UTIL.getConfiguration();
67 conf.setClass("hbase.regionserver.hlog.writer.impl",
68 SequenceFileLogWriter.class, WALProvider.Writer.class);
69 fs = TEST_UTIL.getTestFileSystem();
70 dir = new Path(TEST_UTIL.createRootDir(), "testReadOldRootAndMetaEdits");
71 fs.mkdirs(dir);
72
73 }
74 @AfterClass
75 public static void tearDownAfterClass() throws Exception {
76 }
77
78
79
80
81
82
83
84 @Test
85 public void testReadOldRootAndMetaEdits() throws IOException {
86 LOG.debug("testReadOldRootAndMetaEdits");
87
88 byte[] row = Bytes.toBytes("row");
89 KeyValue kv = new KeyValue(row, row, row, row);
90 List<KeyValue> kvs = new ArrayList<KeyValue>();
91 kvs.add(kv);
92
93 WALProvider.Writer writer = null;
94 WAL.Reader reader = null;
95
96 TableName t = TableName.valueOf("t");
97 HRegionInfo tRegionInfo = null;
98 int logCount = 0;
99 long timestamp = System.currentTimeMillis();
100 Path path = new Path(dir, "t");
101 try {
102 tRegionInfo = new HRegionInfo(t, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW);
103 WAL.Entry tEntry = createAEntry(new HLogKey(tRegionInfo.getEncodedNameAsBytes(), t,
104 ++logCount, timestamp, HConstants.DEFAULT_CLUSTER_ID), kvs);
105
106
107 WAL.Entry rootEntry = createAEntry(new HLogKey(Bytes.toBytes(TableName.OLD_ROOT_STR),
108 TableName.OLD_ROOT_TABLE_NAME, ++logCount, timestamp,
109 HConstants.DEFAULT_CLUSTER_ID), kvs);
110
111
112 WAL.Entry oldMetaEntry = createAEntry(new HLogKey(Bytes.toBytes(TableName.OLD_META_STR),
113 TableName.OLD_META_TABLE_NAME, ++logCount, timestamp,
114 HConstants.DEFAULT_CLUSTER_ID), kvs);
115
116
117 writer = WALFactory.createWALWriter(fs, path, conf);
118 writer.append(tEntry);
119 writer.append(rootEntry);
120 writer.append(oldMetaEntry);
121
122
123 writer.sync();
124 writer.close();
125
126
127 reader = WALFactory.createReader(fs, path, conf);
128 WAL.Entry entry = reader.next();
129 assertNotNull(entry);
130 assertTrue(entry.getKey().getTablename().equals(t));
131 assertEquals(Bytes.toString(entry.getKey().getEncodedRegionName()),
132 Bytes.toString(tRegionInfo.getEncodedNameAsBytes()));
133
134
135 entry = reader.next();
136 assertEquals(entry.getKey().getTablename(), TableName.META_TABLE_NAME);
137
138 assertNull(reader.next());
139 } finally {
140 if (writer != null) {
141 writer.close();
142 }
143 if (reader != null) {
144 reader.close();
145 }
146 }
147 }
148
149
150
151
152
153 private WAL.Entry createAEntry(WALKey walKey, List<KeyValue> kvs) {
154 WALEdit edit = new WALEdit();
155 for (KeyValue kv : kvs )
156 edit.add(kv);
157 return new WAL.Entry(walKey, edit);
158 }
159
160 }