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.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.helix.TestHelper;
27  import org.apache.helix.ZNRecord;
28  import org.apache.helix.PropertyKey.Builder;
29  import org.apache.helix.controller.HelixControllerMain;
30  import org.apache.helix.manager.zk.ZkBaseDataAccessor;
31  import org.apache.helix.mock.participant.MockParticipant;
32  import org.apache.helix.tools.ClusterStateVerifier;
33  import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
34  import org.apache.helix.tools.ClusterStateVerifier.MasterNbInExtViewVerifier;
35  import org.apache.zookeeper.data.Stat;
36  import org.testng.Assert;
37  import org.testng.annotations.Test;
38  
39  
40  public class TestExternalViewUpdates extends ZkIntegrationTestBase
41  {
42    @Test
43    public void testExternalViewUpdates() throws Exception
44    {
45      System.out.println("START testExternalViewUpdates at "
46          + new Date(System.currentTimeMillis()));
47  
48      String clusterName = getShortClassName();
49      MockParticipant[] participants = new MockParticipant[5];
50      int resourceNb = 10;
51      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
52                              "localhost", // participant name prefix
53                              "TestDB", // resource name prefix
54                              resourceNb, // resources
55                              1, // partitions per resource
56                              5, // number of nodes
57                              1, // replicas
58                              "MasterSlave",
59                              true); // do rebalance
60  
61      TestHelper.startController(clusterName,
62                                 "controller_0",
63                                 ZK_ADDR,
64                                 HelixControllerMain.STANDALONE);
65      // start participants
66      for (int i = 0; i < 5; i++)
67      {
68        String instanceName = "localhost_" + (12918 + i);
69  
70        participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR, null);
71        participants[i].syncStart();
72      }
73  
74      boolean result =
75          ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
76                                                                                clusterName));
77      Assert.assertTrue(result);
78  
79      result =
80          ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
81                                                                                   clusterName));
82      Assert.assertTrue(result);
83  
84      // need to verify that each ExternalView's version number is 2
85      Builder keyBuilder = new Builder(clusterName);
86      ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
87      String parentPath = keyBuilder.externalViews().getPath();
88      List<String> childNames = accessor.getChildNames(parentPath, 0);
89      
90      List<String> paths = new ArrayList<String>();
91      for (String name : childNames)
92      {
93        paths.add(parentPath + "/" + name);
94      }
95      
96  //    Stat[] stats = accessor.getStats(paths);
97      for (String path : paths)
98      {
99        Stat stat = accessor.getStat(path, 0);
100       Assert.assertTrue(stat.getVersion() <= 2, "ExternalView should be updated at most 2 times");
101     }
102     
103     // TODO: need stop controller and participants
104     
105     System.out.println("END testExternalViewUpdates at "
106         + new Date(System.currentTimeMillis()));
107   }
108 }