View Javadoc

1   package org.apache.helix.model;
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.Date;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.helix.HelixProperty;
27  import org.apache.helix.ZNRecord;
28  import org.apache.helix.alerts.ExpressionParser;
29  import org.apache.helix.alerts.StatsHolder;
30  import org.apache.helix.model.Message.Attributes;
31  import org.apache.log4j.Logger;
32  
33  
34  public class HealthStat extends HelixProperty 
35  {
36    public enum HealthStatProperty
37    {
38      FIELDS
39    }
40    private static final Logger _logger = Logger.getLogger(HealthStat.class.getName());
41  
42    public HealthStat(String id)
43    {
44      super(id);
45    }
46  
47    public HealthStat(ZNRecord record)
48      {
49        super(record);
50        if(getCreateTimeStamp() == 0)
51        {
52          _record.setSimpleField(Attributes.CREATE_TIMESTAMP.toString(), ""
53              + new Date().getTime());
54        }
55      }
56  
57    public long getLastModifiedTimeStamp()
58    {
59      return _record.getModifiedTime();
60    }
61  
62    public long getCreateTimeStamp()
63    {
64      if (_record.getSimpleField(Attributes.CREATE_TIMESTAMP.toString()) == null)
65      {
66        return 0;
67      }
68      try
69      {
70        return Long.parseLong(_record.getSimpleField(Attributes.CREATE_TIMESTAMP.toString()));
71      } catch (Exception e)
72      {
73        return 0;
74      }
75    }
76    
77    public String getTestField()
78    {
79      return _record.getSimpleField("requestCountStat");
80    }
81    
82    public void setHealthFields(Map<String, Map<String, String>> healthFields)
83    {
84      _record.setMapFields(healthFields);
85    }
86    
87    public String buildCompositeKey(String instance, String parentKey, String statName ) {
88      String delim = ExpressionParser.statFieldDelim;
89      return instance+delim+parentKey+delim+statName;
90    }
91  
92    public Map<String, Map<String, String>> getHealthFields(String instanceName) // ,
93                                                                                 // String
94                                                                                 // timestamp)
95    {
96      // XXX: need to do some conversion of input format to the format that stats
97      // computation wants
98      Map<String, Map<String, String>> currMapFields = _record.getMapFields();
99      Map<String, Map<String, String>> convertedMapFields = new HashMap<String, Map<String, String>>();
100     for (String key : currMapFields.keySet())
101     {
102       Map<String, String> currMap = currMapFields.get(key);
103       String timestamp = "-1";
104       if (_record.getSimpleFields().keySet().contains(StatsHolder.TIMESTAMP_NAME))
105       {
106         timestamp = _record.getSimpleField(StatsHolder.TIMESTAMP_NAME);
107       }
108       for (String subKey : currMap.keySet())
109       {
110         if (subKey.equals("StatsHolder.TIMESTAMP_NAME"))
111         { // don't want to get timestamp again
112           continue;
113         }
114         String compositeKey = buildCompositeKey(instanceName, key, subKey);
115         String value = currMap.get(subKey);
116         Map<String, String> convertedMap = new HashMap<String, String>();
117         convertedMap.put(StatsHolder.VALUE_NAME, value);
118         convertedMap.put(StatsHolder.TIMESTAMP_NAME, timestamp);
119         convertedMapFields.put(compositeKey, convertedMap);
120       }
121     } 
122     return convertedMapFields;
123   }
124   
125   @Override
126   public boolean isValid() {
127     // TODO Auto-generated method stub
128     return true;
129   }
130 }