View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.monitoring;
21  
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  class MonitoredTaskImpl implements MonitoredTask {
29    private long startTime;
30    private long statusTime;
31    private long stateTime;
32    
33    private volatile String status;
34    private volatile String description;
35    
36    protected volatile State state = State.RUNNING;
37    
38    public MonitoredTaskImpl() {
39      startTime = System.currentTimeMillis();
40      statusTime = startTime;
41      stateTime = startTime;
42    }
43  
44    @Override
45    public synchronized MonitoredTaskImpl clone() {
46      try {
47        return (MonitoredTaskImpl) super.clone();
48      } catch (CloneNotSupportedException e) {
49        throw new AssertionError(); // Won't happen
50      }
51    }
52  
53    @Override
54    public long getStartTime() {
55      return startTime;
56    }
57    
58    @Override
59    public String getDescription() {
60      return description;
61    }
62  
63    @Override
64    public String getStatus() {
65      return status;
66    }
67  
68    @Override
69    public long getStatusTime() {
70      return statusTime;
71    }
72    
73    @Override
74    public State getState() {
75      return state;
76    }
77    
78    @Override
79    public long getStateTime() {
80      return stateTime;
81    }
82    
83    @Override
84    public long getCompletionTimestamp() {
85      if (state == State.COMPLETE || state == State.ABORTED) {
86        return stateTime;
87      }
88      return -1;
89    }
90  
91    @Override
92    public void markComplete(String status) {
93      setState(State.COMPLETE);
94      setStatus(status);
95    }
96  
97    @Override
98    public void pause(String msg) {
99      setState(State.WAITING);
100     setStatus(msg);
101   }
102 
103   @Override
104   public void resume(String msg) {
105     setState(State.RUNNING);
106     setStatus(msg);
107   }
108 
109   @Override
110   public void abort(String msg) {
111     setStatus(msg);
112     setState(State.ABORTED);
113   }
114   
115   @Override
116   public void setStatus(String status) {
117     this.status = status;
118     statusTime = System.currentTimeMillis();
119   }
120 
121   protected void setState(State state) {
122     this.state = state;
123     stateTime = System.currentTimeMillis();
124   }
125 
126   @Override
127   public void setDescription(String description) {
128     this.description = description;
129   }
130 
131   @Override
132   public void cleanup() {
133     if (state == State.RUNNING) {
134       setState(State.ABORTED);
135     }
136   }
137 
138   /**
139    * Force the completion timestamp backwards so that
140    * it expires now.
141    */
142   public void expireNow() {
143     stateTime -= 180 * 1000;
144   }
145 
146   @Override
147   public Map<String, Object> toMap() {
148     Map<String, Object> map = new HashMap<String, Object>();
149     map.put("description", getDescription());
150     map.put("status", getStatus());
151     map.put("state", getState());
152     map.put("starttimems", getStartTime());
153     map.put("statustimems", getCompletionTimestamp());
154     map.put("statetimems", getCompletionTimestamp());
155     return map;
156   }
157 
158   @Override
159   public String toJSON() throws IOException {
160     ObjectMapper mapper = new ObjectMapper();
161     return mapper.writeValueAsString(toMap());
162   }
163 
164   @Override
165   public String toString() {
166     StringBuilder sb = new StringBuilder(512);
167     sb.append(getDescription());
168     sb.append(": status=");
169     sb.append(getStatus());
170     sb.append(", state=");
171     sb.append(getState());
172     sb.append(", startTime=");
173     sb.append(getStartTime());
174     sb.append(", completionTime=");
175     sb.append(getCompletionTimestamp());
176     return sb.toString();
177   }
178 
179 }