001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import java.util.Map;
022import org.apache.hadoop.hbase.wal.AbstractWALRoller;
023import org.apache.hadoop.hbase.wal.WAL;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
029
030/**
031 * Runs periodically to determine if the WAL should be rolled.
032 *
033 * NOTE: This class extends Thread rather than Chore because the sleep time
034 * can be interrupted when there is something to do, rather than the Chore
035 * sleep time which is invariant.
036 *
037 * TODO: change to a pool of threads
038 */
039@InterfaceAudience.Private
040@VisibleForTesting
041public class LogRoller extends AbstractWALRoller<RegionServerServices> {
042  private static final Logger LOG = LoggerFactory.getLogger(LogRoller.class);
043
044  public LogRoller(RegionServerServices services) {
045    super("LogRoller", services.getConfiguration(), services);
046  }
047
048  protected void scheduleFlush(String encodedRegionName) {
049    RegionServerServices services = this.abortable;
050    HRegion r = (HRegion) services.getRegion(encodedRegionName);
051    if (r == null) {
052      LOG.warn("Failed to schedule flush of {}, because it is not online on us", encodedRegionName);
053      return;
054    }
055    FlushRequester requester = services.getFlushRequester();
056    if (requester == null) {
057      LOG.warn("Failed to schedule flush of {}, region={}, because FlushRequester is null",
058        encodedRegionName, r);
059      return;
060    }
061    // force flushing all stores to clean old logs
062    requester.requestFlush(r, true, FlushLifeCycleTracker.DUMMY);
063  }
064
065  @VisibleForTesting
066  Map<WAL, Boolean> getWalNeedsRoll() {
067    return this.walNeedsRoll;
068  }
069}