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.regionserver.compactions;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.regionserver.Store;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Check periodically to see if a system stop is requested
026 */
027@InterfaceAudience.Private
028public class CloseChecker {
029  public static final String SIZE_LIMIT_KEY = "hbase.hstore.close.check.interval";
030  public static final String TIME_LIMIT_KEY = "hbase.hstore.close.check.time.interval";
031
032  private final int closeCheckSizeLimit;
033  private final long closeCheckTimeLimit;
034
035  private long bytesWrittenProgressForCloseCheck;
036  private long lastCloseCheckMillis;
037
038  public CloseChecker(Configuration conf, long currentTime) {
039    this.closeCheckSizeLimit = conf.getInt(SIZE_LIMIT_KEY, 10 * 1000 * 1000 /* 10 MB */);
040    this.closeCheckTimeLimit = conf.getLong(TIME_LIMIT_KEY, 10 * 1000L /* 10 s */);
041    this.bytesWrittenProgressForCloseCheck = 0;
042    this.lastCloseCheckMillis = currentTime;
043  }
044
045  /**
046   * Check periodically to see if a system stop is requested every written bytes reach size limit.
047   * @return if true, system stop.
048   */
049  public boolean isSizeLimit(Store store, long bytesWritten) {
050    if (closeCheckSizeLimit <= 0) {
051      return false;
052    }
053
054    bytesWrittenProgressForCloseCheck += bytesWritten;
055    if (bytesWrittenProgressForCloseCheck <= closeCheckSizeLimit) {
056      return false;
057    }
058
059    bytesWrittenProgressForCloseCheck = 0;
060    return !store.areWritesEnabled();
061  }
062
063  /**
064   * Check periodically to see if a system stop is requested every time.
065   * @return if true, system stop.
066   */
067  public boolean isTimeLimit(Store store, long now) {
068    if (closeCheckTimeLimit <= 0) {
069      return false;
070    }
071
072    final long elapsedMillis = now - lastCloseCheckMillis;
073    if (elapsedMillis <= closeCheckTimeLimit) {
074      return false;
075    }
076
077    lastCloseCheckMillis = now;
078    return !store.areWritesEnabled();
079  }
080}