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.io.IOException;
23  import java.io.StringReader;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.management.AttributeNotFoundException;
29  import javax.management.InstanceNotFoundException;
30  import javax.management.IntrospectionException;
31  import javax.management.MBeanException;
32  import javax.management.MalformedObjectNameException;
33  import javax.management.ReflectionException;
34  
35  import org.apache.helix.ZNRecord;
36  import org.apache.helix.alerts.AlertValueAndStatus;
37  import org.apache.helix.alerts.Tuple;
38  import org.apache.helix.healthcheck.TestWildcardAlert.TestClusterMBeanObserver;
39  import org.apache.helix.monitoring.mbeans.ClusterAlertMBeanCollection;
40  import org.apache.log4j.Logger;
41  import org.codehaus.jackson.map.ObjectMapper;
42  import org.codehaus.jackson.type.TypeReference;
43  import org.testng.Assert;
44  import org.testng.annotations.Test;
45  
46  
47  public class TestClusterAlertItemMBeanCollection
48  {
49    private static final Logger _logger = Logger.getLogger(TestClusterAlertItemMBeanCollection.class);
50  
51    @Test
52    public void TestAlertReportingHistory() throws InstanceNotFoundException, MalformedObjectNameException, NullPointerException, IOException, IntrospectionException, AttributeNotFoundException, ReflectionException, MBeanException
53    {
54      ClusterAlertMBeanCollection beanCollection = new ClusterAlertMBeanCollection();
55      
56      String clusterName = "TestCluster";
57      String originAlert1 = "EXP(decay(1.0)(esv4-app7*.RestQueryStats@DBName=BizProfile.MinServerLatency))CMP(GREATER)CON(10)";
58      Map<String, AlertValueAndStatus> alertResultMap1 = new HashMap<String, AlertValueAndStatus>();
59      int nAlerts1 = 5;
60      
61      String originAlert2 = "EXP(decay(1.0)(esv4-app9*.RestQueryStats@DBName=BizProfile.MaxServerLatency))CMP(GREATER)CON(10)";
62      Map<String, AlertValueAndStatus> alertResultMap2 = new HashMap<String, AlertValueAndStatus>();
63      int nAlerts2 = 3;
64      
65      TestClusterMBeanObserver jmxMBeanObserver = new TestClusterMBeanObserver(ClusterAlertMBeanCollection.DOMAIN_ALERT);
66      
67      for(int i = 0; i < nAlerts1; i++)
68      {
69        String alertName = "esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MinServerLatency";
70        Tuple<String> value = new Tuple<String>();
71        value.add("22" + i);
72        AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
73        alertResultMap1.put(alertName, valueAndStatus);
74      }
75      
76      for(int i = 0; i < nAlerts2; i++)
77      {
78        String alertName = "esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MaxServerLatency";
79        Tuple<String> value = new Tuple<String>();
80        value.add("22" + i);
81        AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
82        alertResultMap1.put(alertName, valueAndStatus);
83      }
84      
85      beanCollection.setAlerts(originAlert1, alertResultMap1, clusterName);
86      beanCollection.setAlerts(originAlert2, alertResultMap2, clusterName);
87      
88      beanCollection.refreshAlertDelta(clusterName);
89      String summaryKey = ClusterAlertMBeanCollection.ALERT_SUMMARY + "_" + clusterName;
90      jmxMBeanObserver.refresh();
91      
92      // Get the history list
93      String beanName = "HelixAlerts:alert=" + summaryKey;
94      Map<String, Object> beanValueMap = jmxMBeanObserver._beanValueMap.get(beanName);
95      String history1 = (String) (beanValueMap.get("AlertFiredHistory"));
96      
97      StringReader sr = new StringReader(history1);
98      ObjectMapper mapper = new ObjectMapper();
99  
100     // check the history
101    
102     Map<String, String> delta = beanCollection.getRecentAlertDelta();
103     Assert.assertEquals(delta.size(), nAlerts1 + nAlerts2);
104     for(int i = 0; i < nAlerts1; i++)
105     {
106       String alertBeanName = "(esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName#BizProfile.MinServerLatency)GREATER(10)";
107       Assert.assertTrue(delta.get(alertBeanName).equals("ON"));
108     }
109     
110     for(int i = 0; i < nAlerts2; i++)
111     {
112       String alertBeanName = "(esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName#BizProfile.MaxServerLatency)GREATER(10)";
113       Assert.assertTrue(delta.get(alertBeanName).equals("ON"));
114     }
115     
116     alertResultMap1 = new HashMap<String, AlertValueAndStatus>();
117     for(int i = 0; i < 3; i++)
118     {
119       String alertName = "esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MinServerLatency";
120       Tuple<String> value = new Tuple<String>();
121       value.add("22" + i);
122       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
123       alertResultMap1.put(alertName, valueAndStatus);
124     }
125     
126     for(int i = 3; i < 5; i++)
127     {
128       String alertName = "esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MinServerLatency";
129       Tuple<String> value = new Tuple<String>();
130       value.add("22" + i);
131       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , false);
132       alertResultMap1.put(alertName, valueAndStatus);
133     }
134     
135     for(int i = 7; i < 9; i++)
136     {
137       String alertName = "esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MinServerLatency";
138       Tuple<String> value = new Tuple<String>();
139       value.add("22" + i);
140       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
141       alertResultMap1.put(alertName, valueAndStatus);
142     }
143     
144     for(int i = 0; i < 2; i++)
145     {
146       String alertName = "esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MaxServerLatency";
147       Tuple<String> value = new Tuple<String>();
148       value.add("22" + i);
149       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , false);
150       alertResultMap1.put(alertName, valueAndStatus);
151     }
152     
153     for(int i = 2; i < 3; i++)
154     {
155       String alertName = "esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MaxServerLatency";
156       Tuple<String> value = new Tuple<String>();
157       value.add("22" + i);
158       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
159       alertResultMap1.put(alertName, valueAndStatus);
160     }
161     for(int i = 7; i < 9; i++)
162     {
163       String alertName = "esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MaxServerLatency";
164       Tuple<String> value = new Tuple<String>();
165       value.add("22" + i);
166       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
167       alertResultMap1.put(alertName, valueAndStatus);
168     }
169     
170     beanCollection.setAlerts(originAlert1, alertResultMap1, clusterName);
171     beanCollection.refreshAlertDelta(clusterName);
172     jmxMBeanObserver.refresh();
173 
174     beanValueMap = jmxMBeanObserver._beanValueMap.get(beanName);
175     history1 = (String) (beanValueMap.get("AlertFiredHistory"));
176     
177     sr = new StringReader(history1);
178     mapper = new ObjectMapper();
179 
180     // check the history
181     delta = beanCollection.getRecentAlertDelta();
182     Assert.assertEquals(delta.size(),  8);
183     for(int i = 3; i < 5; i++)
184     {
185       String alertBeanName = "(esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName#BizProfile.MinServerLatency)GREATER(10)";
186       Assert.assertTrue(delta.get(alertBeanName).equals("OFF"));
187     }
188     for(int i = 7; i < 9; i++)
189     {
190       String alertBeanName = "(esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName#BizProfile.MinServerLatency)GREATER(10)";
191       Assert.assertTrue(delta.get(alertBeanName).equals("ON"));
192     }
193     
194     for(int i = 0; i < 2; i++)
195     {
196       String alertBeanName = "(esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName#BizProfile.MaxServerLatency)GREATER(10)";
197       Assert.assertTrue(delta.get(alertBeanName).equals("OFF"));
198     }
199     for(int i = 7; i < 9; i++)
200     {
201       String alertBeanName = "(esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName#BizProfile.MaxServerLatency)GREATER(10)";
202       Assert.assertTrue(delta.get(alertBeanName).equals("ON"));
203     }
204   }
205   @Test
206   public void TestAlertRefresh() throws InstanceNotFoundException, MalformedObjectNameException, NullPointerException, IOException, IntrospectionException, AttributeNotFoundException, ReflectionException, MBeanException, InterruptedException
207   {
208     ClusterAlertMBeanCollection beanCollection = new ClusterAlertMBeanCollection();
209     
210     String clusterName = "TestCluster";
211     String originAlert1 = "EXP(decay(1.0)(esv4-app7*.RestQueryStats@DBName=BizProfile.MinServerLatency))CMP(GREATER)CON(10)";
212     Map<String, AlertValueAndStatus> alertResultMap1 = new HashMap<String, AlertValueAndStatus>();
213     int nAlerts1 = 5;
214     
215     String originAlert2 = "EXP(decay(1.0)(esv4-app9*.RestQueryStats@DBName=BizProfile.MaxServerLatency))CMP(GREATER)CON(10)";
216     Map<String, AlertValueAndStatus> alertResultMap2 = new HashMap<String, AlertValueAndStatus>();
217     int nAlerts2 = 3;
218     
219     TestClusterMBeanObserver jmxMBeanObserver = new TestClusterMBeanObserver(ClusterAlertMBeanCollection.DOMAIN_ALERT);
220     
221     for(int i = 0; i < nAlerts1; i++)
222     {
223       String alertName = "esv4-app7" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MinServerLatency";
224       Tuple<String> value = new Tuple<String>();
225       value.add("22" + i);
226       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
227       alertResultMap1.put(alertName, valueAndStatus);
228     }
229     
230     for(int i = 0; i < nAlerts2; i++)
231     {
232       String alertName = "esv4-app9" + i + ".stg.linkedin.com_12918.RestQueryStats@DBName=BizProfile.MaxServerLatency";
233       Tuple<String> value = new Tuple<String>();
234       value.add("22" + i);
235       AlertValueAndStatus valueAndStatus = new AlertValueAndStatus(value , true);
236       alertResultMap2.put(alertName, valueAndStatus);
237     }
238     
239     beanCollection.setAlerts(originAlert1, alertResultMap1, clusterName);
240     beanCollection.setAlerts(originAlert2, alertResultMap2, clusterName);
241     
242     beanCollection.refreshAlertDelta(clusterName);
243     String summaryKey = ClusterAlertMBeanCollection.ALERT_SUMMARY + "_" + clusterName;
244     jmxMBeanObserver.refresh();
245     
246     Assert.assertEquals(jmxMBeanObserver._beanValueMap.size(), nAlerts2 + nAlerts1 + 1);
247     
248     Thread.sleep(300);
249     
250     beanCollection.setAlerts(originAlert1, alertResultMap1, clusterName);
251     beanCollection.checkMBeanFreshness(200);
252     
253     for(int i = 0; i < 20; i++)
254     {
255       Thread.sleep(500);
256       
257       jmxMBeanObserver.refresh();
258       
259       if(jmxMBeanObserver._beanValueMap.size() == nAlerts1 + 1)
260       {
261         break;
262       }
263     }
264     Assert.assertEquals(jmxMBeanObserver._beanValueMap.size(), nAlerts1 + 1);
265   }
266 }