001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static junit.framework.TestCase.assertTrue;
021import static org.junit.Assert.assertFalse;
022
023import java.io.IOException;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.apache.hadoop.fs.FileSystem;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtility;
032import org.apache.hadoop.hbase.ServerName;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.wal.AbstractFSWALProvider;
036import org.junit.Before;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.mockito.Mockito;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044
045
046@Category({MasterTests.class, SmallTests.class})
047public class TestMasterWALManager {
048  private static final Logger LOG = LoggerFactory.getLogger(TestMasterWALManager.class);
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestMasterWALManager.class);
053
054  private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
055
056  private MasterWalManager mwm;
057  private MasterServices masterServices;
058
059  @Before
060  public void before() throws IOException {
061    MasterFileSystem mfs = Mockito.mock(MasterFileSystem.class);
062    Mockito.when(mfs.getWALFileSystem()).thenReturn(HTU.getTestFileSystem());
063    final Path walRootDir = HTU.getDataTestDir();;
064
065    Mockito.when(mfs.getWALRootDir()).thenReturn(walRootDir);
066    this.masterServices = Mockito.mock(MasterServices.class);
067    Mockito.when(this.masterServices.getConfiguration()).thenReturn(HTU.getConfiguration());
068    Mockito.when(this.masterServices.getMasterFileSystem()).thenReturn(mfs);
069    Mockito.when(this.masterServices.getServerName())
070        .thenReturn(ServerName.parseServerName("master.example.org,0123,456"));
071    this.mwm = new MasterWalManager(this.masterServices) {
072
073      @Override
074      Path getWALDirPath() throws IOException {
075        return walRootDir;
076      }
077
078      @Override
079      Path getWALDirectoryName(ServerName serverName) {
080        return new Path(walRootDir,
081            AbstractFSWALProvider.getWALDirectoryName(serverName.toString()));
082      }
083    };
084  }
085
086  @Test
087  public void testIsWALDirectoryNameWithWALs() throws IOException {
088    ServerName sn = ServerName.parseServerName("x.example.org,1234,5678");
089    assertFalse(this.mwm.isWALDirectoryNameWithWALs(sn));
090    FileSystem walFS = this.masterServices.getMasterFileSystem().getWALFileSystem();
091    Path dir = new Path(this.mwm.getWALDirPath(), sn.toString());
092    assertTrue(walFS.mkdirs(dir));
093    assertTrue(this.mwm.isWALDirectoryNameWithWALs(sn));
094    // Make sure works when dir is SPLITTING
095    Set<ServerName> sns = new HashSet<ServerName>();
096    sns.add(sn);
097    List<Path> paths = this.mwm.createAndGetLogDirs(sns);
098    assertTrue(this.mwm.isWALDirectoryNameWithWALs(sn));
099  }
100}