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