View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver.compactions;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  
24  /**
25   * This class holds information relevant for tracking the progress of a
26   * compaction.
27   *
28   * <p>The metrics tracked allow one to calculate the percent completion of the
29   * compaction based on the number of Key/Value pairs already compacted vs.
30   * total amount scheduled to be compacted.
31   *
32   */
33  @InterfaceAudience.Private
34  public class CompactionProgress {
35  
36    /** the total compacting key values in currently running compaction */
37    public long totalCompactingKVs;
38    /** the completed count of key values in currently running compaction */
39    public long currentCompactedKVs = 0;
40    /** the total size of data processed by the currently running compaction, in bytes */
41    public long totalCompactedSize = 0;
42  
43    /** Constructor
44     * @param totalCompactingKVs the total Key/Value pairs to be compacted
45     */
46    public CompactionProgress(long totalCompactingKVs) {
47      this.totalCompactingKVs = totalCompactingKVs;
48    }
49  
50    /** getter for calculated percent complete
51     * @return float
52     */
53    public float getProgressPct() {
54      return (float)currentCompactedKVs / totalCompactingKVs;
55    }
56  
57    /**
58     * Cancels the compaction progress, setting things to 0.
59     */
60    public void cancel() {
61      this.currentCompactedKVs = this.totalCompactingKVs = 0;
62    }
63  
64    /**
65     * Marks the compaction as complete by setting total to current KV count;
66     * Total KV count is an estimate, so there might be a discrepancy otherwise.
67     */
68    public void complete() {
69      this.totalCompactingKVs = this.currentCompactedKVs;
70    }
71  
72    /**
73     * @return the total compacting key values in currently running compaction
74     */
75    public long getTotalCompactingKvs() {
76      return totalCompactingKVs;
77    }
78  
79    /**
80     * @return the completed count of key values in currently running compaction
81     */
82    public long getCurrentCompactedKvs() {
83      return currentCompactedKVs;
84    }
85  
86    /**
87     * @return the total data size processed by the currently running compaction, in bytes
88     */
89    public long getTotalCompactedSize() {
90      return totalCompactedSize;
91    }
92  
93    @Override
94    public String toString() {
95      return String.format("%d/%d (%.2f%%)", currentCompactedKVs, totalCompactingKVs,
96        100 * getProgressPct());
97    }
98  }