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 */
019
020package org.apache.hadoop.hbase.regionserver.compactions;
021
022import org.apache.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * This class holds information relevant for tracking the progress of a
028 * compaction.
029 *
030 * <p>The metrics tracked allow one to calculate the percent completion of the
031 * compaction based on the number of Key/Value pairs already compacted vs.
032 * total amount scheduled to be compacted.
033 *
034 */
035@InterfaceAudience.Private
036public class CompactionProgress {
037  private static final Logger LOG = LoggerFactory.getLogger(CompactionProgress.class);
038
039  /** the total compacting key values in currently running compaction */
040  private long totalCompactingKVs;
041  /** the completed count of key values in currently running compaction */
042  public long currentCompactedKVs = 0;
043  /** the total size of data processed by the currently running compaction, in bytes */
044  public long totalCompactedSize = 0;
045
046  /** Constructor
047   * @param totalCompactingKVs the total Key/Value pairs to be compacted
048   */
049  public CompactionProgress(long totalCompactingKVs) {
050    this.totalCompactingKVs = totalCompactingKVs;
051  }
052
053  /** getter for calculated percent complete
054   * @return float
055   */
056  public float getProgressPct() {
057    return (float)currentCompactedKVs / getTotalCompactingKVs();
058  }
059
060  /**
061   * Cancels the compaction progress, setting things to 0.
062   */
063  public void cancel() {
064    this.currentCompactedKVs = this.totalCompactingKVs = 0;
065  }
066
067  /**
068   * Marks the compaction as complete by setting total to current KV count;
069   * Total KV count is an estimate, so there might be a discrepancy otherwise.
070   */
071  public void complete() {
072    this.totalCompactingKVs = this.currentCompactedKVs;
073  }
074
075  /**
076   * @return the total compacting key values in currently running compaction
077   */
078  public long getTotalCompactingKVs() {
079    if (totalCompactingKVs < currentCompactedKVs) {
080      LOG.warn("totalCompactingKVs={} less than currentCompactedKVs={}",
081          totalCompactingKVs, currentCompactedKVs);
082      return currentCompactedKVs;
083    }
084    return totalCompactingKVs;
085  }
086
087  /**
088   * @return the completed count of key values in currently running compaction
089   */
090  public long getCurrentCompactedKvs() {
091    return currentCompactedKVs;
092  }
093
094  /**
095   * @return the total data size processed by the currently running compaction, in bytes
096   */
097  public long getTotalCompactedSize() {
098    return totalCompactedSize;
099  }
100
101  @Override
102  public String toString() {
103    return String.format("%d/%d (%.2f%%)", currentCompactedKVs, getTotalCompactingKVs(),
104      100 * getProgressPct());
105  }
106}