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.Map;
23  
24  import org.apache.helix.HelixDefinedState;
25  import org.apache.helix.HelixManager;
26  import org.apache.helix.PropertyType;
27  import org.apache.helix.model.ExternalView;
28  import org.apache.helix.model.IdealState;
29  import org.apache.log4j.Logger;
30  
31  
32  public class ResourceMonitor implements ResourceMonitorMBean
33  {
34    int                         _numOfPartitions;
35    int                         _numOfPartitionsInExternalView;
36    int                         _numOfErrorPartitions;
37    int                         _externalViewIdealStateDiff;
38    private static final Logger LOG = Logger.getLogger(ResourceMonitor.class);
39  
40    String                      _resourceName, _clusterName;
41  
42    public ResourceMonitor(String clusterName, String resourceName)
43    {
44      _clusterName = clusterName;
45      _resourceName = resourceName;
46    }
47  
48    @Override
49    public long getPartitionGauge()
50    {
51      return _numOfPartitions;
52    }
53  
54    @Override
55    public long getErrorPartitionGauge()
56    {
57      return _numOfErrorPartitions;
58    }
59  
60    @Override
61    public long getDifferenceWithIdealStateGauge()
62    {
63      return _externalViewIdealStateDiff;
64    }
65  
66    @Override
67    public String getSensorName()
68    {
69      return ClusterStatusMonitor.RESOURCE_STATUS_KEY + "_" + _clusterName + "_"
70          + _resourceName;
71    }
72  
73    public void updateExternalView(ExternalView externalView, IdealState idealState)
74    {
75      if (externalView == null)
76      {
77        LOG.warn("external view is null");
78        return;
79      }
80      String resourceName = externalView.getId();
81  
82      if (idealState == null)
83      {
84        LOG.warn("ideal state is null for " + resourceName);
85        _numOfErrorPartitions = 0;
86        _externalViewIdealStateDiff = 0;
87        _numOfPartitionsInExternalView = 0;
88        return;
89      }
90  
91      assert (resourceName.equals(idealState.getId()));
92  
93      int numOfErrorPartitions = 0;
94      int numOfDiff = 0;
95  
96      if (_numOfPartitions == 0)
97      {
98        _numOfPartitions = idealState.getRecord().getMapFields().size();
99      }
100 
101     // TODO fix this; IdealState shall have either map fields (CUSTOM mode)
102     // or list fields (AUDO mode)
103     for (String partitionName : idealState.getRecord().getMapFields().keySet())
104     {
105       Map<String, String> idealRecord = idealState.getInstanceStateMap(partitionName);
106       Map<String, String> externalViewRecord = externalView.getStateMap(partitionName);
107 
108       if (externalViewRecord == null)
109       {
110         numOfDiff += idealRecord.size();
111         continue;
112       }
113       for (String host : idealRecord.keySet())
114       {
115         if (!externalViewRecord.containsKey(host)
116             || !externalViewRecord.get(host).equals(idealRecord.get(host)))
117         {
118           numOfDiff++;
119         }
120       }
121 
122       for (String host : externalViewRecord.keySet())
123       {
124         if (externalViewRecord.get(host).equalsIgnoreCase(HelixDefinedState.ERROR.toString()))
125         {
126           numOfErrorPartitions++;
127         }
128       }
129     }
130     _numOfErrorPartitions = numOfErrorPartitions;
131     _externalViewIdealStateDiff = numOfDiff;
132     _numOfPartitionsInExternalView = externalView.getPartitionSet().size();
133   }
134 
135   @Override
136   public long getExternalViewPartitionGauge()
137   {
138     return _numOfPartitionsInExternalView;
139   }
140 
141   public String getBeanName()
142   {
143     return _clusterName + " " + _resourceName;
144   }
145 }