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.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.testclassification.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   * Tests to read old ROOT, Meta edits.
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     * Inserts three waledits in the wal file, and reads them back. The first edit is of a regular
75     * table, second waledit is for the ROOT table (it will be ignored while reading),
76     * and last waledit is for the hbase:meta table, which will be linked to the new system:meta table.
77     * @throws IOException
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      // kv list to be used for all WALEdits.
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      // a regular table
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       // create a old root edit (-ROOT-).
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       // create a old meta edit (hbase:meta).
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       // write above entries
115       writer = HLogFactory.createWALWriter(fs, path, conf);
116       writer.append(tEntry);
117       writer.append(rootEntry);
118       writer.append(oldMetaEntry);
119 
120       // sync/close the writer
121       writer.sync();
122       writer.close();
123 
124       // read the log and see things are okay.
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       // read the ROOT waledit, but that will be ignored, and hbase:meta waledit will be read instead.
133       entry = reader.next();
134       assertEquals(entry.getKey().getTablename(), TableName.META_TABLE_NAME);
135       // should reach end of log
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    * Creates a WALEdit for the passed KeyValues and returns a HLog.Entry instance composed of
148    * the WALEdit and passed HLogKey.
149    * @return HLog.Entry instance for the passed HLogKey and KeyValues
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 }