1 package org.apache.helix.healthcheck;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.TreeMap;
25
26 import org.apache.log4j.Logger;
27
28 public class PerformanceHealthReportProvider extends HealthReportProvider
29 {
30
31 private static final Logger _logger = Logger
32 .getLogger(PerformanceHealthReportProvider.class);
33
34 public final static String _testStat = "testStat";
35 public final static String _readLatencyStat = "readLatencyStat";
36 public final static String _requestCountStat = "requestCountStat";
37 public final static String _partitionRequestCountStat = "partitionRequestCountStat";
38
39 public static final String _performanceCounters = "performanceCounters";
40
41 public int readLatencyCount = 0;
42 public double readLatencySum = 0;
43
44 public int requestCount = 0;
45
46
47
48
49 private final Map<String, HashMap<String, String>> _partitionStatMaps = new HashMap<String, HashMap<String, String>>();
50
51 public PerformanceHealthReportProvider()
52 {
53 }
54
55 @Override
56 public Map<String, String> getRecentHealthReport()
57 {
58 long testStat = 10;
59
60 Map<String, String> result = new TreeMap<String, String>();
61
62 result.put(_testStat, "" + testStat);
63 result.put(_readLatencyStat, "" + readLatencySum
64 / (double) readLatencyCount);
65 result.put(_requestCountStat, "" + requestCount);
66
67 return result;
68 }
69
70 @Override
71 public Map<String, Map<String, String>> getRecentPartitionHealthReport()
72 {
73 Map<String, Map<String, String>> result = new TreeMap<String, Map<String, String>>();
74 for (String statName : _partitionStatMaps.keySet())
75 {
76 result.put(statName, _partitionStatMaps.get(statName));
77 }
78 return result;
79 }
80
81 HashMap<String, String> getStatMap(String statName, boolean createIfMissing)
82 {
83
84 HashMap<String, String> statMap;
85 if (!_partitionStatMaps.containsKey(statName))
86 {
87 if (!createIfMissing)
88 {
89 return null;
90 }
91 statMap = new HashMap<String, String>();
92 _partitionStatMaps.put(statName, statMap);
93 }
94 else
95 {
96 statMap = _partitionStatMaps.get(statName);
97 }
98 return statMap;
99 }
100
101
102
103
104
105
106
107 String getPartitionStat(HashMap<String, String> partitionMap,
108 String partitionName)
109 {
110 return partitionMap.get(partitionName);
111 }
112
113 void setPartitionStat(HashMap<String, String> partitionMap,
114 String partitionName, String value)
115 {
116 partitionMap.put(partitionName, value);
117 }
118
119 public void incrementPartitionStat(String statName, String partitionName)
120 {
121 HashMap<String, String> statMap = getStatMap(statName, true);
122 String currValStr = getPartitionStat(statMap, partitionName);
123 double currVal;
124 if (currValStr == null)
125 {
126 currVal = 1.0;
127 }
128 else
129 {
130 currVal = Double.parseDouble(getPartitionStat(statMap, partitionName));
131 currVal++;
132 }
133 setPartitionStat(statMap, partitionName, String.valueOf(currVal));
134 }
135
136 public void submitPartitionStat(String statName, String partitionName,
137 String value)
138 {
139 HashMap<String, String> statMap = getStatMap(statName, true);
140 setPartitionStat(statMap, partitionName, value);
141 }
142
143 public String getPartitionStat(String statName, String partitionName)
144 {
145 HashMap<String, String> statMap = getStatMap(statName, false);
146 if (statMap == null)
147 {
148 return null;
149 }
150 else
151 {
152 return statMap.get(partitionName);
153 }
154 }
155
156 public void resetStats()
157 {
158 _partitionStatMaps.clear();
159 }
160
161 public String getReportName()
162 {
163 return _performanceCounters;
164 }
165 }