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.region;
019
020import static org.apache.hadoop.hbase.HConstants.HREGION_OLDLOGDIR_NAME;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.Abortable;
027import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL;
028import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
029import org.apache.hadoop.hbase.wal.AbstractFSWALProvider;
030import org.apache.hadoop.hbase.wal.AbstractWALRoller;
031import org.apache.hadoop.hbase.wal.WAL;
032import org.apache.hadoop.hbase.wal.WALFactory;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * As long as there is no RegionServerServices for a master local region, we need implement log
039 * roller logic by our own.
040 * <p/>
041 * We can reuse most of the code for normal wal roller, the only difference is that there is only
042 * one region, so in {@link #scheduleFlush(String)} method we can just schedule flush for the master
043 * local region.
044 */
045@InterfaceAudience.Private
046public final class MasterRegionWALRoller extends AbstractWALRoller<Abortable> {
047
048  private static final Logger LOG = LoggerFactory.getLogger(MasterRegionWALRoller.class);
049
050  private volatile MasterRegionFlusherAndCompactor flusherAndCompactor;
051
052  private final FileSystem fs;
053
054  private final Path walArchiveDir;
055
056  private final Path globalWALArchiveDir;
057
058  private final String archivedWALSuffix;
059
060  private MasterRegionWALRoller(String name, Configuration conf, Abortable abortable, FileSystem fs,
061    Path walRootDir, Path globalWALRootDir, String archivedWALSuffix) {
062    super(name, conf, abortable);
063    this.fs = fs;
064    this.walArchiveDir = new Path(walRootDir, HREGION_OLDLOGDIR_NAME);
065    this.globalWALArchiveDir = new Path(globalWALRootDir, HREGION_OLDLOGDIR_NAME);
066    this.archivedWALSuffix = archivedWALSuffix;
067  }
068
069  @Override
070  protected void afterRoll(WAL wal) {
071    // move the archived WAL files to the global archive path
072    try {
073      MasterRegionUtils.moveFilesUnderDir(fs, walArchiveDir, globalWALArchiveDir,
074        archivedWALSuffix);
075    } catch (IOException e) {
076      LOG.warn("Failed to move archived wals from {} to global dir {}", walArchiveDir,
077        globalWALArchiveDir, e);
078    }
079  }
080
081  @Override
082  protected void scheduleFlush(String encodedRegionName) {
083    MasterRegionFlusherAndCompactor flusher = this.flusherAndCompactor;
084    if (flusher != null) {
085      flusher.requestFlush();
086    }
087  }
088
089  void setFlusherAndCompactor(MasterRegionFlusherAndCompactor flusherAndCompactor) {
090    this.flusherAndCompactor = flusherAndCompactor;
091  }
092
093  static MasterRegionWALRoller create(String name, Configuration conf, Abortable abortable,
094    FileSystem fs, Path walRootDir, Path globalWALRootDir, String archivedWALSuffix,
095    long rollPeriodMs, long flushSize) {
096    // we can not run with wal disabled, so force set it to true.
097    conf.setBoolean(WALFactory.WAL_ENABLED, true);
098    // we do not need this feature, so force disable it.
099    conf.setBoolean(AbstractFSWALProvider.SEPARATE_OLDLOGDIR, false);
100    conf.setLong(WAL_ROLL_PERIOD_KEY, rollPeriodMs);
101    // make the roll size the same with the flush size, as we only have one region here
102    conf.setLong(WALUtil.WAL_BLOCK_SIZE, flushSize * 2);
103    conf.setFloat(AbstractFSWAL.WAL_ROLL_MULTIPLIER, 0.5f);
104    return new MasterRegionWALRoller(name, conf, abortable, fs, walRootDir, globalWALRootDir,
105      archivedWALSuffix);
106  }
107
108}