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.FSDataOutputStream;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.ServerName;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
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    Path walRootDir = HTU.createWALRootDir();
064    Mockito.when(mfs.getWALRootDir()).thenReturn(walRootDir);
065    this.masterServices = Mockito.mock(MasterServices.class);
066    Mockito.when(this.masterServices.getConfiguration()).thenReturn(HTU.getConfiguration());
067    Mockito.when(this.masterServices.getMasterFileSystem()).thenReturn(mfs);
068    Mockito.when(this.masterServices.getServerName()).
069        thenReturn(ServerName.parseServerName("master.example.org,0123,456"));
070    this.mwm = new MasterWalManager(this.masterServices);
071  }
072
073  @Test
074  public void testIsWALDirectoryNameWithWALs() throws IOException {
075    ServerName sn = ServerName.parseServerName("x.example.org,1234,5678");
076    assertFalse(this.mwm.isWALDirectoryNameWithWALs(sn));
077    FileSystem walFS = this.masterServices.getMasterFileSystem().getWALFileSystem();
078    Path dir = new Path(this.mwm.getWALDirPath(), sn.toString());
079    assertTrue(walFS.mkdirs(dir));
080    assertTrue(this.mwm.isWALDirectoryNameWithWALs(sn));
081    // Make sure works when dir is SPLITTING
082    Set<ServerName> sns = new HashSet<ServerName>();
083    sns.add(sn);
084    List<Path> paths = this.mwm.createAndGetLogDirs(sns);
085    assertTrue(this.mwm.isWALDirectoryNameWithWALs(sn));
086  }
087}