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.hbase.classification.InterfaceAudience;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.hadoop.hbase.util.GsonUtil;
31  import org.apache.hbase.thirdparty.com.google.gson.Gson;
32  
33  @InterfaceAudience.Private
34  class MonitoredTaskImpl implements MonitoredTask {
35    private long startTime;
36    private long statusTime;
37    private long stateTime;
38    private long warnTime;
39  
40    private volatile String status;
41    private volatile String description;
42    
43    protected volatile State state = State.RUNNING;
44  
45    private boolean journalEnabled = false;
46    private List<StatusJournalEntry> journal;
47  
48    private static final Gson GSON = GsonUtil.createGson().create();
49  
50    public MonitoredTaskImpl() {
51      startTime = System.currentTimeMillis();
52      statusTime = startTime;
53      stateTime = startTime;
54      warnTime = startTime;
55    }
56  
57    private static class StatusJournalEntryImpl implements StatusJournalEntry {
58      private long statusTime;
59      private String status;
60  
61      public StatusJournalEntryImpl(String status, long statusTime) {
62        this.status = status;
63        this.statusTime = statusTime;
64      }
65  
66      @Override
67      public String getStatus() {
68        return status;
69      }
70  
71      @Override
72      public long getTimeStamp() {
73        return statusTime;
74      }
75  
76      @Override
77      public String toString() {
78        StringBuilder sb = new StringBuilder();
79        sb.append(status);
80        sb.append(" at ");
81        sb.append(statusTime);
82        return sb.toString();
83      }
84    }
85  
86    @Override
87    public synchronized MonitoredTaskImpl clone() {
88      try {
89        return (MonitoredTaskImpl) super.clone();
90      } catch (CloneNotSupportedException e) {
91        throw new AssertionError(); // Won't happen
92      }
93    }
94  
95    @Override
96    public long getStartTime() {
97      return startTime;
98    }
99    
100   @Override
101   public String getDescription() {
102     return description;
103   }
104 
105   @Override
106   public String getStatus() {
107     return status;
108   }
109 
110   @Override
111   public long getStatusTime() {
112     return statusTime;
113   }
114   
115   @Override
116   public State getState() {
117     return state;
118   }
119   
120   @Override
121   public long getStateTime() {
122     return stateTime;
123   }
124 
125   @Override
126   public long getWarnTime() {
127     return warnTime;
128   }
129 
130   @Override
131   public long getCompletionTimestamp() {
132     if (state == State.COMPLETE || state == State.ABORTED) {
133       return stateTime;
134     }
135     return -1;
136   }
137 
138   @Override
139   public void markComplete(String status) {
140     setState(State.COMPLETE);
141     setStatus(status);
142   }
143 
144   @Override
145   public void pause(String msg) {
146     setState(State.WAITING);
147     setStatus(msg);
148   }
149 
150   @Override
151   public void resume(String msg) {
152     setState(State.RUNNING);
153     setStatus(msg);
154   }
155 
156   @Override
157   public void abort(String msg) {
158     setStatus(msg);
159     setState(State.ABORTED);
160   }
161   
162   @Override
163   public void setStatus(String status) {
164     this.status = status;
165     statusTime = System.currentTimeMillis();
166     if (journalEnabled) {
167       journal.add(new StatusJournalEntryImpl(this.status, statusTime));
168     }
169   }
170 
171   protected void setState(State state) {
172     this.state = state;
173     stateTime = System.currentTimeMillis();
174   }
175 
176   @Override
177   public void setDescription(String description) {
178     this.description = description;
179   }
180 
181   @Override
182   public void setWarnTime(long t) {
183     this.warnTime = t;
184   }
185 
186   @Override
187   public void cleanup() {
188     if (state == State.RUNNING) {
189       setState(State.ABORTED);
190     }
191   }
192 
193   /**
194    * Force the completion timestamp backwards so that
195    * it expires now.
196    */
197   public void expireNow() {
198     stateTime -= 180 * 1000;
199   }
200 
201   @Override
202   public Map<String, Object> toMap() {
203     Map<String, Object> map = new HashMap<String, Object>();
204     map.put("description", getDescription());
205     map.put("status", getStatus());
206     map.put("state", getState());
207     map.put("starttimems", getStartTime());
208     map.put("statustimems", getCompletionTimestamp());
209     map.put("statetimems", getCompletionTimestamp());
210     return map;
211   }
212 
213   @Override
214   public String toJSON() throws IOException {
215     return GSON.toJson(toMap());
216   }
217 
218   @Override
219   public String toString() {
220     StringBuilder sb = new StringBuilder(512);
221     sb.append(getDescription());
222     sb.append(": status=");
223     sb.append(getStatus());
224     sb.append(", state=");
225     sb.append(getState());
226     sb.append(", startTime=");
227     sb.append(getStartTime());
228     sb.append(", completionTime=");
229     sb.append(getCompletionTimestamp());
230     return sb.toString();
231   }
232 
233   /**
234    * Returns the status journal. This implementation of status journal is not thread-safe. Currently
235    * we use this to track various stages of flushes and compactions where we can use this/pretty
236    * print for post task analysis, by which time we are already done changing states (writing to
237    * journal)
238    */
239   @Override
240   public List<StatusJournalEntry> getStatusJournal() {
241     if (journal == null) {
242       return Collections.emptyList();
243     } else {
244       return Collections.unmodifiableList(journal);
245     }
246   }
247 
248   /**
249    * Enables journaling of this monitored task, the first invocation will lazily initialize the
250    * journal. The journal implementation itself and this method are not thread safe
251    */
252   @Override
253   public void enableStatusJournal(boolean includeCurrentStatus) {
254     if (journalEnabled && journal != null) {
255       return;
256     }
257     journalEnabled = true;
258     if (journal == null) {
259       journal = new ArrayList<StatusJournalEntry>();
260     }
261     if (includeCurrentStatus) {
262       journal.add(new StatusJournalEntryImpl(status, statusTime));
263     }
264   }
265 
266   @Override
267   public void disableStatusJournal() {
268     journalEnabled = false;
269   }
270 
271   @Override
272   public String prettyPrintJournal() {
273     StringBuilder sb = new StringBuilder();
274     for (int i = 0; i < journal.size(); i++) {
275       StatusJournalEntry je = journal.get(i);
276       sb.append(je.toString());
277       if (i != 0) {
278         StatusJournalEntry jep = journal.get(i-1);
279         long delta = je.getTimeStamp() - jep.getTimeStamp();
280         if (delta != 0) {
281           sb.append(" (+" + delta + " ms)");
282         }
283       }
284       sb.append("\n");
285     }
286     return sb.toString();
287   }
288 
289 }