View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Tests to read old ROOT, Meta edits.
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     * Inserts three waledits in the wal file, and reads them back. The first edit is of a regular
80     * table, second waledit is for the ROOT table (it will be ignored while reading),
81     * and last waledit is for the hbase:meta table, which will be linked to the new system:meta table.
82     * @throws IOException
83     */
84    @Test
85    public void testReadOldRootAndMetaEdits() throws IOException {
86      LOG.debug("testReadOldRootAndMetaEdits");
87      // kv list to be used for all WALEdits.
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      // a regular table
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       // create a old root edit (-ROOT-).
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       // create a old meta edit (hbase:meta).
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       // write above entries
117       writer = WALFactory.createWALWriter(fs, path, conf);
118       writer.append(tEntry);
119       writer.append(rootEntry);
120       writer.append(oldMetaEntry);
121 
122       // sync/close the writer
123       writer.sync();
124       writer.close();
125 
126       // read the log and see things are okay.
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       // read the ROOT waledit, but that will be ignored, and hbase:meta waledit will be read instead.
135       entry = reader.next();
136       assertEquals(entry.getKey().getTablename(), TableName.META_TABLE_NAME);
137       // should reach end of log
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    * Creates a WALEdit for the passed KeyValues and returns a WALProvider.Entry instance composed of
150    * the WALEdit and passed WALKey.
151    * @return WAL.Entry instance for the passed WALKey and KeyValues
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 }