1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver.wal;
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  import java.util.NavigableSet;
26  
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.junit.Test;
32  
33  /**
34   * Simple testing of a few HLog methods.
35   */
36  public class TestHLogMethods {
37    private final HBaseTestingUtility util = new HBaseTestingUtility();
38  
39    /**
40     * Assert that getSplitEditFilesSorted returns files in expected order and
41     * that it skips moved-aside files.
42     * @throws IOException
43     */
44    @Test public void testGetSplitEditFilesSorted() throws IOException {
45      FileSystem fs = FileSystem.get(util.getConfiguration());
46      Path regiondir = HBaseTestingUtility.getTestDir("regiondir");
47      fs.delete(regiondir, true);
48      fs.mkdirs(regiondir);
49      Path recoverededits = HLog.getRegionDirRecoveredEditsDir(regiondir);
50      String first = HLog.formatRecoveredEditsFileName(-1);
51      createFile(fs, recoverededits, first);
52      createFile(fs, recoverededits, HLog.formatRecoveredEditsFileName(0));
53      createFile(fs, recoverededits, HLog.formatRecoveredEditsFileName(1));
54      createFile(fs, recoverededits, HLog.formatRecoveredEditsFileName(11));
55      createFile(fs, recoverededits, HLog.formatRecoveredEditsFileName(2));
56      createFile(fs, recoverededits, HLog.formatRecoveredEditsFileName(50));
57      String last = HLog.formatRecoveredEditsFileName(Long.MAX_VALUE);
58      createFile(fs, recoverededits, last);
59      createFile(fs, recoverededits,
60        Long.toString(Long.MAX_VALUE) + "." + System.currentTimeMillis());
61      NavigableSet<Path> files = HLog.getSplitEditFilesSorted(fs, regiondir);
62      assertEquals(7, files.size());
63      assertEquals(files.pollFirst().getName(), first);
64      assertEquals(files.pollLast().getName(), last);
65      assertEquals(files.pollFirst().getName(),
66        HLog.formatRecoveredEditsFileName(0));
67      assertEquals(files.pollFirst().getName(),
68        HLog.formatRecoveredEditsFileName(1));
69      assertEquals(files.pollFirst().getName(),
70        HLog.formatRecoveredEditsFileName(2));
71      assertEquals(files.pollFirst().getName(),
72        HLog.formatRecoveredEditsFileName(11));
73    }
74  
75    private void createFile(final FileSystem fs, final Path testdir,
76        final String name)
77    throws IOException {
78      FSDataOutputStream fdos = fs.create(new Path(testdir, name), true);
79      fdos.close();
80    }
81  }