1 package org.apache.helix.healthcheck;
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 org.apache.log4j.Logger;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28
29 public class StatHealthReportProvider extends HealthReportProvider
30 {
31
32 private static final Logger _logger = Logger
33 .getLogger(StatHealthReportProvider.class);
34
35 /*
36 * public final static String _testStat = "testStat"; public final static
37 * String _readLatencyStat = "readLatencyStat"; public final static String
38 * _requestCountStat = "requestCountStat"; public final static String
39 * _partitionRequestCountStat = "partitionRequestCountStat";
40 */
41
42 public static final String REPORT_NAME = "ParticipantStats";
43 public String _reportName = REPORT_NAME;
44
45 public static final String STAT_VALUE = "value";
46 public static final String TIMESTAMP = "timestamp";
47
48 public int readLatencyCount = 0;
49 public double readLatencySum = 0;
50
51 public int requestCount = 0;
52
53 // private final Map<String, String> _partitionCountsMap = new HashMap<String,
54 // String>();
55
56 // private final Map<String, HashMap<String,String>> _partitionStatMaps = new
57 // HashMap<String, HashMap<String,String>>();
58 private final ConcurrentHashMap<String, String> _statsToValues = new ConcurrentHashMap<String, String>();
59 private final ConcurrentHashMap<String, String> _statsToTimestamps = new ConcurrentHashMap<String, String>();
60
61 public StatHealthReportProvider()
62 {
63 }
64
65 @Override
66 public Map<String, String> getRecentHealthReport()
67 {
68 return null;
69 }
70
71 // TODO: function is misnamed, but return type is what I want
72 @Override
73 public Map<String, Map<String, String>> getRecentPartitionHealthReport()
74 {
75 Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
76 for (String stat : _statsToValues.keySet())
77 {
78 Map<String, String> currStat = new HashMap<String, String>();
79 /*
80 * currStat.put(Stat.OP_TYPE, stat._opType);
81 * currStat.put(Stat.MEASUREMENT_TYPE, stat._measurementType);
82 * currStat.put(Stat.NODE_NAME, stat._nodeName);
83 * currStat.put(Stat.PARTITION_NAME, stat._partitionName);
84 * currStat.put(Stat.RESOURCE_NAME, stat._resourceName);
85 * currStat.put(Stat.RETURN_STATUS, stat._returnStatus);
86 * currStat.put(Stat.METRIC_NAME, stat._metricName);
87 * currStat.put(Stat.AGG_TYPE, stat._aggTypeName);
88 */
89 currStat.put(TIMESTAMP, _statsToTimestamps.get(stat));
90 currStat.put(STAT_VALUE, _statsToValues.get(stat));
91 result.put(stat, currStat);
92 }
93 return result;
94 }
95
96 public boolean contains(Stat inStat)
97 {
98 return _statsToValues.containsKey(inStat);
99 }
100
101 public Set<String> keySet()
102 {
103 return _statsToValues.keySet();
104 }
105
106 public String getStatValue(Stat inStat)
107 {
108 return _statsToValues.get(inStat);
109 }
110
111 public long getStatTimestamp(Stat inStat)
112 {
113 return Long.parseLong(_statsToTimestamps.get(inStat));
114 }
115
116 /*
117 * public String getStatValue(String opType, String measurementType, String
118 * resourceName, String partitionName, String nodeName, boolean
119 * createIfMissing) { Stat rs = new Stat(opType, measurementType,
120 * resourceName, partitionName, nodeName); String val =
121 * _statsToValues.get(rs); if (val == null && createIfMissing) { val = "0";
122 * _statsToValues.put(rs, val); } return val; }
123 */
124
125 public void writeStat(String statName, String val, String timestamp)
126 {
127 _statsToValues.put(statName, val);
128 _statsToTimestamps.put(statName, timestamp);
129 }
130
131 /*
132 * public void setStat(Stat es, String val, String timestamp) { writeStat(es,
133 * val, timestamp); }
134 *
135 * public void setStat(String opType, String measurementType, String
136 * resourceName, String partitionName, String nodeName, double val, String
137 * timestamp) { Stat rs = new Stat(opType, measurementType, resourceName,
138 * partitionName, nodeName); writeStat(rs, String.valueOf(val), timestamp); }
139 */
140
141 public void incrementStat(String statName, String timestamp)
142 {
143 // Stat rs = new Stat(opType, measurementType, resourceName, partitionName,
144 // nodeName);
145 String val = _statsToValues.get(statName);
146 if (val == null)
147 {
148 val = "0";
149 }
150 else
151 {
152 val = String.valueOf(Double.parseDouble(val) + 1);
153 }
154 writeStat(statName, val, timestamp);
155 }
156
157 public int size()
158 {
159 return _statsToValues.size();
160 }
161
162 public void resetStats()
163 {
164 _statsToValues.clear();
165 _statsToTimestamps.clear();
166 }
167
168 public void setReportName(String name)
169 {
170 _reportName = name;
171 }
172
173 public String getReportName()
174 {
175 return _reportName;
176 }
177 }