View Javadoc

1   package org.apache.helix.monitoring.mbeans;
2   
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,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
26  import org.apache.helix.monitoring.StatCollector;
27  import org.apache.helix.monitoring.StateTransitionContext;
28  import org.apache.helix.monitoring.StateTransitionDataPoint;
29  
30  
31  public class StateTransitionStatMonitor implements StateTransitionStatMonitorMBean
32  {
33    public enum LATENCY_TYPE {TOTAL, EXECUTION};
34    
35    private static final int DEFAULT_WINDOW_SIZE = 4000;
36    private long _numDataPoints;
37    private long _successCount;
38    private TimeUnit _unit;
39    
40    private ConcurrentHashMap<LATENCY_TYPE, StatCollector> _monitorMap
41       = new ConcurrentHashMap<LATENCY_TYPE, StatCollector>();
42    
43    StateTransitionContext _context;
44    
45    public StateTransitionStatMonitor(StateTransitionContext context, TimeUnit unit)
46    {
47      _context = context;
48      _monitorMap.put(LATENCY_TYPE.TOTAL, new StatCollector());
49      _monitorMap.put(LATENCY_TYPE.EXECUTION, new StatCollector());
50      reset();
51    }
52    
53    public StateTransitionContext getContext()
54    {
55      return _context;
56    }
57    
58    public String getBeanName()
59    {
60      return _context.getClusterName()+" "+_context.getResourceName()+" "+_context.getTransition();
61    }
62    
63    public void addDataPoint(StateTransitionDataPoint data)
64    {
65      _numDataPoints++;
66      if(data.getSuccess())
67      {
68        _successCount++;
69      }
70      // should we count only the transition time for successful transitions?
71      addLatency(LATENCY_TYPE.TOTAL, data.getTotalDelay());
72      addLatency(LATENCY_TYPE.EXECUTION, data.getExecutionDelay());
73    }
74    
75    void addLatency(LATENCY_TYPE type, double latency)
76    {
77      assert(_monitorMap.containsKey(type));
78      _monitorMap.get(type).addData(latency);
79    }
80    
81    public long getNumDataPoints()
82    {
83      return _numDataPoints;
84    }
85    
86    public void reset()
87    {
88      _numDataPoints = 0;
89      _successCount = 0;
90      for(StatCollector monitor : _monitorMap.values())
91      {
92        monitor.reset();
93      }
94    }
95  
96    @Override
97    public long getTotalStateTransitionGauge()
98    {
99      return _numDataPoints;
100   }
101 
102   @Override
103   public long getTotalFailedTransitionGauge()
104   {
105     return _numDataPoints - _successCount;
106   }
107 
108   @Override
109   public long getTotalSuccessTransitionGauge()
110   {
111     return _successCount;
112   }
113 
114   @Override
115   public double getMeanTransitionLatency()
116   {
117     return _monitorMap.get(LATENCY_TYPE.TOTAL).getMean();
118   }
119 
120   @Override
121   public double getMaxTransitionLatency()
122   {
123     return _monitorMap.get(LATENCY_TYPE.TOTAL).getMax();
124   }
125 
126   @Override
127   public double getMinTransitionLatency()
128   {
129     return _monitorMap.get(LATENCY_TYPE.TOTAL).getMin();
130   }
131 
132   @Override
133   public double getPercentileTransitionLatency(int percentage)
134   {
135     return _monitorMap.get(LATENCY_TYPE.TOTAL).getPercentile(percentage);
136   }
137 
138   @Override
139   public double getMeanTransitionExecuteLatency()
140   {
141     return _monitorMap.get(LATENCY_TYPE.EXECUTION).getMean();
142   }
143 
144   @Override
145   public double getMaxTransitionExecuteLatency()
146   {
147     return _monitorMap.get(LATENCY_TYPE.EXECUTION).getMax();
148   }
149 
150   @Override
151   public double getMinTransitionExecuteLatency()
152   {
153     return _monitorMap.get(LATENCY_TYPE.EXECUTION).getMin();
154   }
155 
156   @Override
157   public double getPercentileTransitionExecuteLatency(int percentage)
158   {
159     return _monitorMap.get(LATENCY_TYPE.EXECUTION).getPercentile(percentage);
160   }
161 }