View Javadoc

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 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    // private final Map<String, String> _partitionCountsMap = new HashMap<String,
47    // String>();
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      // check if map for this stat exists. if not, create it
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   // TODO:
102   // Currently participant is source of truth and updates ZK. We want ZK to be
103   // source of truth.
104   // Revise this approach the participant sends deltas of stats to controller
105   // (ZK?) and have controller do aggregation
106   // and update ZK. Make sure to wipe the participant between uploads.
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 }