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.concurrent.atomic.AtomicBoolean;
24  
25  import org.apache.helix.HelixDataAccessor;
26  import org.apache.helix.HelixManager;
27  import org.apache.helix.NotificationContext;
28  import org.apache.helix.TestHelper;
29  import org.apache.helix.ZNRecord;
30  import org.apache.helix.PropertyKey.Builder;
31  import org.apache.helix.integration.ZkIntegrationTestBase;
32  import org.apache.helix.manager.zk.ZKHelixDataAccessor;
33  import org.apache.helix.manager.zk.ZkBaseDataAccessor;
34  import org.apache.helix.mock.controller.ClusterController;
35  import org.apache.helix.mock.participant.MockParticipant;
36  import org.apache.helix.mock.participant.MockTransition;
37  import org.apache.helix.model.HealthStat;
38  import org.apache.helix.model.Message;
39  import org.apache.helix.tools.ClusterSetup;
40  import org.apache.helix.tools.ClusterStateVerifier;
41  import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
42  import org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier;
43  import org.testng.Assert;
44  import org.testng.annotations.Test;
45  
46  
47  public class TestDummyAlerts extends ZkIntegrationTestBase
48  {
49    public class DummyAlertsTransition extends MockTransition
50    {
51      private final AtomicBoolean _done = new AtomicBoolean(false);
52  
53      @Override
54      public void doTransition(Message message, NotificationContext context)
55      {
56        HelixManager manager = context.getManager();
57        HelixDataAccessor accessor = manager.getHelixDataAccessor();
58        Builder keyBuilder = accessor.keyBuilder();
59        
60        String instance = message.getTgtName();
61        if (_done.getAndSet(true) == false)
62        {
63          for (int i = 0; i < 5; i++)
64          {
65  //          System.out.println(instance + " sets healthReport: " + "mockAlerts" + i);
66            accessor.setProperty(keyBuilder.healthReport(instance, "mockAlerts"),
67                                 new HealthStat(new ZNRecord("mockAlerts" + i)));
68          }
69        }
70      }
71  
72    }
73  
74    @Test()
75    public void testDummyAlerts() throws Exception
76    {
77      // Logger.getRootLogger().setLevel(Level.INFO);
78      String className = TestHelper.getTestClassName();
79      String methodName = TestHelper.getTestMethodName();
80      String clusterName = className + "_" + methodName;
81      final int n = 5;
82  
83      MockParticipant[] participants = new MockParticipant[n];
84  
85      System.out.println("START " + clusterName + " at "
86          + new Date(System.currentTimeMillis()));
87  
88      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant start
89                                                           // port
90                              "localhost", // participant name prefix
91                              "TestDB", // resource name prefix
92                              1, // resources
93                              10, // partitions per resource
94                              n, // number of nodes
95                              3, // replicas
96                              "MasterSlave",
97                              true); // do rebalance
98  
99      ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
100     enableHealthCheck(clusterName);
101     setupTool.getClusterManagementTool()
102              .addAlert(clusterName,
103                        "EXP(decay(1.0)(*.defaultPerfCounters@defaultPerfCounters.availableCPUs))CMP(GREATER)CON(2)");
104 
105     // start controller
106     ClusterController controller =
107         new ClusterController(clusterName, "controller_0", ZK_ADDR);
108     controller.syncStart();
109 
110     
111     // start participants
112     for (int i = 0; i < n; i++)
113     {
114       String instanceName = "localhost_" + (12918 + i);
115 
116       participants[i] =
117           new MockParticipant(clusterName,
118                               instanceName,
119                               ZK_ADDR,
120                               new DummyAlertsTransition());
121       participants[i].syncStart();
122     }
123 
124     boolean result =
125         ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
126                                                                               clusterName));
127     Assert.assertTrue(result);
128 
129     result =
130         ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
131                                                                                  clusterName));
132     Assert.assertTrue(result);
133 
134     // other verifications go here
135     ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
136     Builder keyBuilder = accessor.keyBuilder();
137 
138     for (int i = 0; i < n; i++)
139     {
140       String instance = "localhost_" + (12918 + i);
141       ZNRecord record = null;
142       for(int j = 0; j < 10; j++)
143       {
144         record =
145             accessor.getProperty(keyBuilder.healthReport(instance, "mockAlerts")).getRecord();
146         if(record.getId().equals("mockAlerts4"))
147         {
148           break;
149         }
150         else
151         {
152           Thread.sleep(500);
153         }
154       }
155       Assert.assertEquals(record.getId(), "mockAlerts4");
156     }
157 
158     // clean up
159     Thread.sleep(1000);
160     controller.syncStop();
161     for (int i = 0; i < 5; i++)
162     {
163       participants[i].syncStop();
164     }
165     
166     System.out.println("END " + clusterName + " at "
167         + new Date(System.currentTimeMillis()));
168   }
169 }