View Javadoc

1   package org.apache.helix.alerts;
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.HelixDataAccessor;
25  import org.apache.helix.ZNRecord;
26  import org.apache.helix.Mocks.MockManager;
27  import org.apache.helix.PropertyKey.Builder;
28  import org.apache.helix.alerts.AlertParser;
29  import org.apache.helix.alerts.AlertsHolder;
30  import org.apache.helix.controller.stages.HealthDataCache;
31  import org.testng.AssertJUnit;
32  import org.testng.annotations.BeforeMethod;
33  import org.testng.annotations.Test;
34  
35  
36  public class TestAddAlerts
37  {
38  
39    protected static final String CLUSTER_NAME = "TestCluster";
40  
41    MockManager                   _helixManager;
42    AlertsHolder                  _alertsHolder;
43  
44    public final String           EXP          = AlertParser.EXPRESSION_NAME;
45    public final String           CMP          = AlertParser.COMPARATOR_NAME;
46    public final String           CON          = AlertParser.CONSTANT_NAME;
47  
48    @BeforeMethod()
49    public void setup()
50    {
51      _helixManager = new MockManager(CLUSTER_NAME);
52      _alertsHolder = new AlertsHolder(_helixManager, new HealthDataCache());
53    }
54  
55    public boolean alertRecordContains(ZNRecord rec, String alertName)
56    {
57      Map<String, Map<String, String>> alerts = rec.getMapFields();
58      return alerts.containsKey(alertName);
59    }
60  
61    public int alertsSize(ZNRecord rec)
62    {
63      Map<String, Map<String, String>> alerts = rec.getMapFields();
64      return alerts.size();
65    }
66  
67    @Test()
68    public void testAddAlert() throws Exception
69    {
70      String alert =
71          EXP + "(accumulate()(dbFoo.partition10.latency))" + CMP + "(GREATER)" + CON
72              + "(10)";
73      _alertsHolder.addAlert(alert);
74      HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
75      Builder keyBuilder = accessor.keyBuilder();
76  
77      ZNRecord rec = accessor.getProperty(keyBuilder.alerts()).getRecord();
78      System.out.println("alert: " + alert);
79      System.out.println("rec: " + rec.toString());
80      AssertJUnit.assertTrue(alertRecordContains(rec, alert));
81      AssertJUnit.assertEquals(1, alertsSize(rec));
82    }
83  
84    @Test()
85    public void testAddTwoAlerts() throws Exception
86    {
87      String alert1 =
88          EXP + "(accumulate()(dbFoo.partition10.latency))" + CMP + "(GREATER)" + CON
89              + "(10)";
90      String alert2 =
91          EXP + "(accumulate()(dbFoo.partition10.latency))" + CMP + "(GREATER)" + CON
92              + "(100)";
93      _alertsHolder.addAlert(alert1);
94      _alertsHolder.addAlert(alert2);
95  
96      HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
97      Builder keyBuilder = accessor.keyBuilder();
98  
99      ZNRecord rec = accessor.getProperty(keyBuilder.alerts()).getRecord();
100     // System.out.println("alert: "+alert1);
101     System.out.println("rec: " + rec.toString());
102     AssertJUnit.assertTrue(alertRecordContains(rec, alert1));
103     AssertJUnit.assertTrue(alertRecordContains(rec, alert2));
104     AssertJUnit.assertEquals(2, alertsSize(rec));
105   }
106 
107   @Test(groups = { "unitTest" })
108   public void testAddTwoWildcardAlert() throws Exception
109   {
110     String alert1 =
111         EXP + "(accumulate()(dbFoo.partition*.put*))" + CMP + "(GREATER)" + CON + "(10)";
112     _alertsHolder.addAlert(alert1);
113     
114     HelixDataAccessor accessor = _helixManager.getHelixDataAccessor();
115     Builder keyBuilder = accessor.keyBuilder();
116 
117     ZNRecord rec = accessor.getProperty(keyBuilder.alerts()).getRecord();
118     // System.out.println("alert: "+alert1);
119     System.out.println("rec: " + rec.toString());
120     AssertJUnit.assertTrue(alertRecordContains(rec, alert1));
121     AssertJUnit.assertEquals(1, alertsSize(rec));
122   }
123 
124   // add 2 wildcard alert here
125 }