View Javadoc

1   package org.apache.helix.integration;
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.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.helix.ExternalViewChangeListener;
27  import org.apache.helix.HelixManager;
28  import org.apache.helix.HelixManagerFactory;
29  import org.apache.helix.InstanceType;
30  import org.apache.helix.NotificationContext;
31  import org.apache.helix.model.ExternalView;
32  import org.apache.helix.tools.ClusterStateVerifier;
33  import org.testng.Assert;
34  import org.testng.annotations.Test;
35  
36  
37  public class TestBasicSpectator extends ZkStandAloneCMTestBase implements ExternalViewChangeListener
38  {
39    Map<String, Integer> _externalViewChanges = new HashMap<String, Integer>();
40    
41    @Test
42    public void TestSpectator() throws Exception
43    {
44      HelixManager relayHelixManager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME,
45          null,
46          InstanceType.SPECTATOR,
47          ZK_ADDR);
48  
49      relayHelixManager.connect();
50      relayHelixManager.addExternalViewChangeListener(this);
51      
52      _setupTool.addResourceToCluster(CLUSTER_NAME, "NextDB", 64, STATE_MODEL);
53      _setupTool.rebalanceStorageCluster(CLUSTER_NAME, "NextDB", 3);
54      
55      boolean result = ClusterStateVerifier.verifyByPolling(
56          new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
57      Assert.assertTrue(result);
58      
59      Assert.assertTrue(_externalViewChanges.containsKey("NextDB"));
60      Assert.assertTrue(_externalViewChanges.containsKey(TEST_DB));
61      
62    }
63  
64    @Override
65    public void onExternalViewChange(List<ExternalView> externalViewList,
66        NotificationContext changeContext)
67    {
68      for(ExternalView view : externalViewList)
69      {
70        if(!_externalViewChanges.containsKey(view.getResourceName()))
71        {
72          _externalViewChanges.put(view.getResourceName(), 1);
73        }
74        else
75        {
76          _externalViewChanges.put(view.getResourceName(), _externalViewChanges.get(view.getResourceName())+ 1);
77        }
78      }
79    }
80  }