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.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.UUID;
26  
27  import org.apache.helix.HelixDataAccessor;
28  import org.apache.helix.HelixProperty;
29  import org.apache.helix.Mocks;
30  import org.apache.helix.PropertyKey;
31  import org.apache.helix.PropertyType;
32  import org.apache.helix.ZNRecord;
33  import org.apache.helix.PropertyKey.Builder;
34  import org.apache.helix.model.ExternalView;
35  import org.apache.helix.model.IdealState;
36  import org.apache.helix.model.LiveInstance.LiveInstanceProperty;
37  import org.apache.helix.monitoring.mbeans.ResourceMonitor;
38  import org.apache.helix.tools.DefaultIdealStateCalculator;
39  import org.testng.AssertJUnit;
40  import org.testng.annotations.Test;
41  
42  
43  public class TestResourceMonitor
44  {
45    String _clusterName = "Test-cluster";
46    String _dbName = "TestDB";
47    int _replicas = 3;
48    int _partitions = 50;
49  
50    class MockHelixManager extends Mocks.MockManager
51    {
52      class MockDataAccessor extends Mocks.MockAccessor
53      {
54        @Override
55        public <T extends HelixProperty> List<T>  getChildValues(PropertyKey key)
56        {
57          List<T> result = new ArrayList<T>();
58          PropertyType type = key.getType();
59          Class<? extends HelixProperty> clazz = key.getTypeClass();
60          if (type == PropertyType.EXTERNALVIEW)
61          {
62            HelixProperty typedInstance = HelixProperty.convertToTypedInstance(clazz, _externalView);
63            result.add((T) typedInstance);
64            return result;
65          }
66          else if (type == PropertyType.LIVEINSTANCES)
67          {
68            return (List<T>) HelixProperty.convertToTypedList(clazz, _liveInstances);
69          }
70  
71          return result;
72        }
73  
74        @Override
75        public  <T extends HelixProperty> T  getProperty(PropertyKey key)
76        {
77          PropertyType type = key.getType();
78          if (type == PropertyType.EXTERNALVIEW)
79          {
80            return (T) new ExternalView(_externalView);
81          }
82          else if (type == PropertyType.IDEALSTATES)
83          {
84            return (T) new IdealState(_idealState);
85          }
86          return null;
87        }
88      }
89  
90      HelixDataAccessor _accessor = new MockDataAccessor();
91      ZNRecord _idealState;
92      ZNRecord _externalView;
93      List<String> _instances;
94      List<ZNRecord> _liveInstances;
95      String _db = "DB";
96  
97      public MockHelixManager()
98      {
99        _liveInstances = new ArrayList<ZNRecord>();
100       _instances = new ArrayList<String>();
101       for(int i = 0;i<5; i++)
102       {
103         String instance = "localhost_"+(12918+i);
104         _instances.add(instance);
105         ZNRecord metaData = new ZNRecord(instance);
106         metaData.setSimpleField(LiveInstanceProperty.SESSION_ID.toString(),
107             UUID.randomUUID().toString());
108 
109       }
110       _idealState= DefaultIdealStateCalculator.calculateIdealState(_instances,
111                                                                           _partitions,
112                                                                           _replicas,
113                                                                           _dbName,
114                                                                           "MASTER",
115                                                                           "SLAVE");
116       _externalView = new ZNRecord(_idealState);
117     }
118 
119     @Override
120     public HelixDataAccessor getHelixDataAccessor()
121     {
122       return _accessor;
123     }
124 
125   }
126 
127   @Test()
128   public void TestReportData()
129   {
130     MockHelixManager manager = new MockHelixManager();
131     ResourceMonitor monitor = new ResourceMonitor(_clusterName, _dbName);
132 
133     HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
134     Builder keyBuilder = helixDataAccessor.keyBuilder();
135     ExternalView externalView = 
136         helixDataAccessor.getProperty(keyBuilder.externalView(_dbName));
137     IdealState idealState = helixDataAccessor.getProperty(keyBuilder.idealStates(_dbName));
138 
139     monitor.updateExternalView(externalView, idealState);
140 
141     AssertJUnit.assertEquals(monitor.getDifferenceWithIdealStateGauge(), 0);
142     AssertJUnit.assertEquals(monitor.getErrorPartitionGauge(), 0);
143     AssertJUnit.assertEquals(monitor.getExternalViewPartitionGauge(), _partitions);
144     AssertJUnit.assertEquals(monitor.getPartitionGauge(), _partitions);
145     monitor.getBeanName();
146 
147     int n = 4;
148     for (int i = 0; i < n; i++)
149     {
150       Map<String, String> map = externalView.getStateMap(_dbName + "_" + 3 * i);
151       String key = map.keySet().toArray()[0].toString();
152       map.put(key, "ERROR");
153       externalView.setStateMap(_dbName + "_" + 3 * i, map);
154     }
155 
156     monitor.updateExternalView(externalView, idealState);
157     AssertJUnit.assertEquals(monitor.getDifferenceWithIdealStateGauge(), 0);
158     AssertJUnit.assertEquals(monitor.getErrorPartitionGauge(), n);
159     AssertJUnit.assertEquals(monitor.getExternalViewPartitionGauge(), _partitions);
160     AssertJUnit.assertEquals(monitor.getPartitionGauge(), _partitions);
161 
162     n = 5;
163     for (int i = 0; i < n; i++)
164     {
165       externalView.getRecord().getMapFields().remove(_dbName + "_" + 4 * i);
166     }
167 
168     monitor.updateExternalView(externalView, idealState);
169     AssertJUnit.assertEquals(monitor.getDifferenceWithIdealStateGauge(), n
170         * (_replicas + 1));
171     AssertJUnit.assertEquals(monitor.getErrorPartitionGauge(), 3);
172     AssertJUnit.assertEquals(monitor.getExternalViewPartitionGauge(), _partitions - n);
173     AssertJUnit.assertEquals(monitor.getPartitionGauge(), _partitions);
174   }
175 }