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