View Javadoc

1   package org.apache.helix.healthcheck;
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.Map;
24  import java.util.Set;
25  
26  import org.apache.helix.HelixDataAccessor;
27  import org.apache.helix.HelixManager;
28  import org.apache.helix.NotificationContext;
29  import org.apache.helix.TestHelper;
30  import org.apache.helix.ZNRecord;
31  import org.apache.helix.PropertyKey.Builder;
32  import org.apache.helix.TestHelper.StartCMResult;
33  import org.apache.helix.alerts.AlertValueAndStatus;
34  import org.apache.helix.controller.HelixControllerMain;
35  import org.apache.helix.healthcheck.HealthStatsAggregationTask;
36  import org.apache.helix.healthcheck.ParticipantHealthReportCollectorImpl;
37  import org.apache.helix.integration.ZkIntegrationTestBase;
38  import org.apache.helix.manager.zk.ZKHelixDataAccessor;
39  import org.apache.helix.manager.zk.ZNRecordSerializer;
40  import org.apache.helix.manager.zk.ZkBaseDataAccessor;
41  import org.apache.helix.manager.zk.ZkClient;
42  import org.apache.helix.mock.participant.MockEspressoHealthReportProvider;
43  import org.apache.helix.mock.participant.MockParticipant;
44  import org.apache.helix.mock.participant.MockTransition;
45  import org.apache.helix.model.Message;
46  import org.apache.helix.tools.ClusterSetup;
47  import org.apache.helix.tools.ClusterStateVerifier;
48  import org.testng.Assert;
49  import org.testng.annotations.AfterClass;
50  import org.testng.annotations.BeforeClass;
51  import org.testng.annotations.Test;
52  
53  
54  public class TestExpandAlert extends ZkIntegrationTestBase
55  {
56    ZkClient _zkClient;
57    protected ClusterSetup _setupTool = null;
58    protected final String _alertStr = "EXP(decay(1.0)(localhost_*.RestQueryStats@DBName=TestDB0.latency))CMP(GREATER)CON(16)";
59    protected final String _alertStatusStr = _alertStr+" : (localhost_12918.RestQueryStats@DBName=TestDB0.latency)";
60    protected final String _dbName = "TestDB0";
61  
62    @BeforeClass ()
63    public void beforeClass() throws Exception
64    {
65      _zkClient = new ZkClient(ZK_ADDR);
66      _zkClient.setZkSerializer(new ZNRecordSerializer());
67  
68      _setupTool = new ClusterSetup(ZK_ADDR);
69    }
70  
71    @AfterClass
72    public void afterClass()
73    {
74      _zkClient.close();
75    }
76  
77    public class ExpandAlertTransition extends MockTransition
78    {
79      @Override
80      public void doTransition(Message message, NotificationContext context)
81      {
82        HelixManager manager = context.getManager();
83        HelixDataAccessor accessor = manager.getHelixDataAccessor();
84        String fromState = message.getFromState();
85        String toState = message.getToState();
86        String instance = message.getTgtName();
87        String partition = message.getPartitionName();
88  
89        if (fromState.equalsIgnoreCase("SLAVE")
90            && toState.equalsIgnoreCase("MASTER"))
91        {
92  
93      	//add a stat and report to ZK
94      	//perhaps should keep reporter per instance...
95      	ParticipantHealthReportCollectorImpl reporter =
96      			new ParticipantHealthReportCollectorImpl(manager, instance);
97      	MockEspressoHealthReportProvider provider = new
98      			MockEspressoHealthReportProvider();
99      	reporter.addHealthReportProvider(provider);
100     	String statName = "latency";
101     	provider.setStat(_dbName, statName,"15");
102      	reporter.transmitHealthReports();
103 
104 
105     	/*
106         for (int i = 0; i < 5; i++)
107         {
108           accessor.setProperty(PropertyType.HEALTHREPORT,
109                                new ZNRecord("mockAlerts" + i),
110                                instance,
111                                "mockAlerts");
112           try
113           {
114             Thread.sleep(1000);
115           }
116           catch (InterruptedException e)
117           {
118             // TODO Auto-generated catch block
119             e.printStackTrace();
120           }
121         }
122         */
123       }
124     }
125 
126   }
127 
128   @Test()
129   public void testExpandAlert() throws Exception
130   {
131     String clusterName = getShortClassName();
132     MockParticipant[] participants = new MockParticipant[5];
133 
134     System.out.println("START TestExpandAlert at " + new Date(System.currentTimeMillis()));
135 
136     TestHelper.setupCluster(clusterName,
137                             ZK_ADDR,
138                             12918,        // participant start port
139                             "localhost",  // participant name prefix
140                             "TestDB",     // resource  name prefix
141                             1,            // resources
142                             10,           // partitions per resource
143                             5,            // number of nodes //change back to 5!!!
144                             3,            // replicas //change back to 3!!!
145                             "MasterSlave",
146                             true);        // do rebalance
147     // enableHealthCheck(clusterName);
148 
149     _setupTool.getClusterManagementTool().addAlert(clusterName, _alertStr);
150 
151     StartCMResult cmResult = TestHelper.startController(clusterName,
152                                "controller_0",
153                                ZK_ADDR,
154                                HelixControllerMain.STANDALONE);
155     // start participants
156     for (int i = 0; i < 5; i++) //!!!change back to 5
157     {
158       String instanceName = "localhost_" + (12918 + i);
159 
160       participants[i] = new MockParticipant(clusterName,
161                                             instanceName,
162                                             ZK_ADDR,
163                                             new ExpandAlertTransition());
164       participants[i].start();
165 //      new Thread(participants[i]).start();
166     }
167 
168     boolean result = ClusterStateVerifier.verifyByPolling(
169         new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
170     Assert.assertTrue(result);
171 
172     Thread.sleep(1000);
173     // HealthAggregationTask is supposed to run by a timer every 30s
174     // To make sure HealthAggregationTask is run, we invoke it explicitly for this test
175     new HealthStatsAggregationTask(cmResult._manager).run();
176   //sleep for a few seconds to give stats stage time to trigger
177     Thread.sleep(3000);
178 
179     // other verifications go here
180     ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_zkClient));
181     Builder keyBuilder = accessor.keyBuilder();
182 
183     //for (int i = 0; i < 1; i++) //change 1 back to 5
184     //{
185       //String instance = "localhost_" + (12918 + i);
186       //String instance = "localhost_12918";
187       ZNRecord record = accessor.getProperty(keyBuilder.alertStatus()).getRecord();
188       Map<String, Map<String,String>> recMap = record.getMapFields();
189       Set<String> keySet = recMap.keySet();
190       Map<String,String> alertStatusMap = recMap.get(_alertStatusStr);
191       String val = alertStatusMap.get(AlertValueAndStatus.VALUE_NAME);
192       boolean fired = Boolean.parseBoolean(alertStatusMap.get(AlertValueAndStatus.FIRED_NAME));
193       Assert.assertEquals(Double.parseDouble(val), Double.parseDouble("15.0"));
194       Assert.assertFalse(fired);
195     //}
196 
197     System.out.println("END TestExpandAlert at " + new Date(System.currentTimeMillis()));
198   }
199 }