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  package org.apache.hadoop.hbase.monitoring;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.codehaus.jackson.map.ObjectMapper;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  @InterfaceAudience.Private
29  class MonitoredTaskImpl implements MonitoredTask {
30    private long startTime;
31    private long statusTime;
32    private long stateTime;
33    
34    private volatile String status;
35    private volatile String description;
36    
37    protected volatile State state = State.RUNNING;
38  
39    private static final ObjectMapper MAPPER = new ObjectMapper();
40  
41    public MonitoredTaskImpl() {
42      startTime = System.currentTimeMillis();
43      statusTime = startTime;
44      stateTime = startTime;
45    }
46  
47    @Override
48    public synchronized MonitoredTaskImpl clone() {
49      try {
50        return (MonitoredTaskImpl) super.clone();
51      } catch (CloneNotSupportedException e) {
52        throw new AssertionError(); // Won't happen
53      }
54    }
55  
56    @Override
57    public long getStartTime() {
58      return startTime;
59    }
60    
61    @Override
62    public String getDescription() {
63      return description;
64    }
65  
66    @Override
67    public String getStatus() {
68      return status;
69    }
70  
71    @Override
72    public long getStatusTime() {
73      return statusTime;
74    }
75    
76    @Override
77    public State getState() {
78      return state;
79    }
80    
81    @Override
82    public long getStateTime() {
83      return stateTime;
84    }
85    
86    @Override
87    public long getCompletionTimestamp() {
88      if (state == State.COMPLETE || state == State.ABORTED) {
89        return stateTime;
90      }
91      return -1;
92    }
93  
94    @Override
95    public void markComplete(String status) {
96      setState(State.COMPLETE);
97      setStatus(status);
98    }
99  
100   @Override
101   public void pause(String msg) {
102     setState(State.WAITING);
103     setStatus(msg);
104   }
105 
106   @Override
107   public void resume(String msg) {
108     setState(State.RUNNING);
109     setStatus(msg);
110   }
111 
112   @Override
113   public void abort(String msg) {
114     setStatus(msg);
115     setState(State.ABORTED);
116   }
117   
118   @Override
119   public void setStatus(String status) {
120     this.status = status;
121     statusTime = System.currentTimeMillis();
122   }
123 
124   protected void setState(State state) {
125     this.state = state;
126     stateTime = System.currentTimeMillis();
127   }
128 
129   @Override
130   public void setDescription(String description) {
131     this.description = description;
132   }
133 
134   @Override
135   public void cleanup() {
136     if (state == State.RUNNING) {
137       setState(State.ABORTED);
138     }
139   }
140 
141   /**
142    * Force the completion timestamp backwards so that
143    * it expires now.
144    */
145   public void expireNow() {
146     stateTime -= 180 * 1000;
147   }
148 
149   @Override
150   public Map<String, Object> toMap() {
151     Map<String, Object> map = new HashMap<String, Object>();
152     map.put("description", getDescription());
153     map.put("status", getStatus());
154     map.put("state", getState());
155     map.put("starttimems", getStartTime());
156     map.put("statustimems", getCompletionTimestamp());
157     map.put("statetimems", getCompletionTimestamp());
158     return map;
159   }
160 
161   @Override
162   public String toJSON() throws IOException {
163     return MAPPER.writeValueAsString(toMap());
164   }
165 
166   @Override
167   public String toString() {
168     StringBuilder sb = new StringBuilder(512);
169     sb.append(getDescription());
170     sb.append(": status=");
171     sb.append(getStatus());
172     sb.append(", state=");
173     sb.append(getState());
174     sb.append(", startTime=");
175     sb.append(getStartTime());
176     sb.append(", completionTime=");
177     sb.append(getCompletionTimestamp());
178     return sb.toString();
179   }
180 
181 }