[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.util; |
---|
| 20 | |
---|
| 21 | import java.util.ArrayList; |
---|
| 22 | |
---|
| 23 | /** Utility to assist with generation of progress reports. Applications build |
---|
| 24 | * a hierarchy of {@link Progress} instances, each modelling a phase of |
---|
| 25 | * execution. The root is constructed with {@link #Progress()}. Nodes for |
---|
| 26 | * sub-phases are created by calling {@link #addPhase()}. |
---|
| 27 | */ |
---|
| 28 | public class Progress { |
---|
| 29 | private String status = ""; |
---|
| 30 | private float progress; |
---|
| 31 | private int currentPhase; |
---|
| 32 | private ArrayList<Progress> phases = new ArrayList<Progress>(); |
---|
| 33 | private Progress parent; |
---|
| 34 | private float progressPerPhase; |
---|
| 35 | |
---|
| 36 | /** Creates a new root node. */ |
---|
| 37 | public Progress() {} |
---|
| 38 | |
---|
| 39 | /** Adds a named node to the tree. */ |
---|
| 40 | public Progress addPhase(String status) { |
---|
| 41 | Progress phase = addPhase(); |
---|
| 42 | phase.setStatus(status); |
---|
| 43 | return phase; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /** Adds a node to the tree. */ |
---|
| 47 | public synchronized Progress addPhase() { |
---|
| 48 | Progress phase = new Progress(); |
---|
| 49 | phases.add(phase); |
---|
| 50 | phase.parent = this; |
---|
| 51 | progressPerPhase = 1.0f / (float)phases.size(); |
---|
| 52 | return phase; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** Called during execution to move to the next phase at this level in the |
---|
| 56 | * tree. */ |
---|
| 57 | public synchronized void startNextPhase() { |
---|
| 58 | currentPhase++; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** Returns the current sub-node executing. */ |
---|
| 62 | public synchronized Progress phase() { |
---|
| 63 | return phases.get(currentPhase); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** Completes this node, moving the parent node to its next child. */ |
---|
| 67 | public void complete() { |
---|
| 68 | // we have to traverse up to our parent, so be careful about locking. |
---|
| 69 | Progress myParent; |
---|
| 70 | synchronized(this) { |
---|
| 71 | progress = 1.0f; |
---|
| 72 | myParent = parent; |
---|
| 73 | } |
---|
| 74 | if (myParent != null) { |
---|
| 75 | // this will synchronize on the parent, so we make sure we release |
---|
| 76 | // our lock before getting the parent's, since we're traversing |
---|
| 77 | // against the normal traversal direction used by get() or toString(). |
---|
| 78 | // We don't need transactional semantics, so we're OK doing this. |
---|
| 79 | myParent.startNextPhase(); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** Called during execution on a leaf node to set its progress. */ |
---|
| 84 | public synchronized void set(float progress) { |
---|
| 85 | this.progress = progress; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** Returns the overall progress of the root. */ |
---|
| 89 | // this method probably does not need to be synchronized as getINternal() is synchronized |
---|
| 90 | // and the node's parent never changes. Still, it doesn't hurt. |
---|
| 91 | public synchronized float get() { |
---|
| 92 | Progress node = this; |
---|
| 93 | while (node.parent != null) { // find the root |
---|
| 94 | node = parent; |
---|
| 95 | } |
---|
| 96 | return node.getInternal(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** Computes progress in this node. */ |
---|
| 100 | private synchronized float getInternal() { |
---|
| 101 | int phaseCount = phases.size(); |
---|
| 102 | if (phaseCount != 0) { |
---|
| 103 | float subProgress = |
---|
| 104 | currentPhase < phaseCount ? phase().getInternal() : 0.0f; |
---|
| 105 | return progressPerPhase*(currentPhase + subProgress); |
---|
| 106 | } else { |
---|
| 107 | return progress; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | public synchronized void setStatus(String status) { |
---|
| 112 | this.status = status; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public String toString() { |
---|
| 116 | StringBuffer result = new StringBuffer(); |
---|
| 117 | toString(result); |
---|
| 118 | return result.toString(); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | private synchronized void toString(StringBuffer buffer) { |
---|
| 122 | buffer.append(status); |
---|
| 123 | if (phases.size() != 0 && currentPhase < phases.size()) { |
---|
| 124 | buffer.append(" > "); |
---|
| 125 | phase().toString(buffer); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | } |
---|