Package org.apache.helix.manager.zk

Examples of org.apache.helix.manager.zk.MockController


    addCluster(clusterName);
    addInstancesToCluster(clusterName, "localhost:123", 6, null);
    addResource(clusterName, "db_11", 22);
    rebalanceResource(clusterName, "db_11");

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller_9900");
    controller.syncStart();

    // start mock nodes
    Map<String, MockParticipant> participants =
        new HashMap<String, MockParticipant>();
    for (int i = 0; i < 6; i++) {
      String instanceName = "localhost_123" + i;
      MockParticipant participant =
          new MockParticipant(_zkaddr, clusterName, instanceName);
      participant.syncStart();
      participants.put(instanceName, participant);
    }

    boolean verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(_zkaddr,
            clusterName));
    Assert.assertTrue(verifyResult);

    String clusterUrl = getClusterUrl(clusterName);
    String instancesUrl = clusterUrl + "/instances";

    String instances = "localhost:12331;localhost:12341;localhost:12351;localhost:12361";
    String response = assertSuccessPostOperation(instancesUrl, addInstanceCmd(instances), false);
    String[] hosts = instances.split(";");
    for (String host : hosts) {
      Assert.assertTrue(response.contains(host.replace(':', '_')));
    }

    response = assertSuccessPostOperation(clusterUrl, expandClusterCmd(), false);

    for (int i = 3; i <= 6; i++) {
      String instanceName = "localhost_123" + i + "1";
      MockParticipant participant =
          new MockParticipant(_zkaddr, clusterName, instanceName);
      participant.syncStart();
      participants.put(instanceName, participant);
    }

    verifyResult =
        ClusterStateVerifier
            .verifyByZkCallback(new MasterNbInExtViewVerifier(_zkaddr, clusterName));
    Assert.assertTrue(verifyResult);

    verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(_zkaddr,
            clusterName));
    Assert.assertTrue(verifyResult);

    // clean up
    controller.syncStop();
    for (MockParticipant participant : participants.values()) {
      participant.syncStop();
    }
  }
View Full Code Here


    addCluster(clusterName);
    addInstancesToCluster(clusterName, "localhost:123", 6, null);
    addResource(clusterName, "db_11", 22);
    rebalanceResource(clusterName, "db_11");

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller_9900");
    controller.syncStart();

    // start mock nodes
    Map<String, MockParticipant> participants =
        new HashMap<String, MockParticipant>();
    for (int i = 0; i < 6; i++) {
      String instanceName = "localhost_123" + i;
      MockParticipant participant =
          new MockParticipant(_zkaddr, clusterName, instanceName);
      participant.syncStart();
      participants.put(instanceName, participant);
    }

    HelixDataAccessor accessor = participants.get("localhost_1231").getHelixDataAccessor();
    // drop node should fail as not disabled
    String hostName = "localhost_1231";
    String instanceUrl = getInstanceUrl(clusterName, hostName);
    ExternalView ev = accessor.getProperty(accessor.keyBuilder().externalView("db_11"));

    String response =
        assertSuccessPostOperation(instanceUrl,
            enablePartitionCmd("db_11", "db_11_0;db_11_11", false), false);
    Assert.assertTrue(response.contains("DISABLED_PARTITION"));
    Assert.assertTrue(response.contains("db_11_0"));
    Assert.assertTrue(response.contains("db_11_11"));

    boolean verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(_zkaddr,
            clusterName));
    Assert.assertTrue(verifyResult);

    ev = accessor.getProperty(accessor.keyBuilder().externalView("db_11"));
    Assert.assertEquals(ev.getStateMap("db_11_0").get(hostName), "OFFLINE");
    Assert.assertEquals(ev.getStateMap("db_11_11").get(hostName), "OFFLINE");

    response =
        assertSuccessPostOperation(instanceUrl,
            enablePartitionCmd("db_11", "db_11_0;db_11_11", true), false);
    Assert.assertFalse(response.contains("db_11_0"));
    Assert.assertFalse(response.contains("db_11_11"));

    verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(_zkaddr,
            clusterName));
    Assert.assertTrue(verifyResult);

    ev = accessor.getProperty(accessor.keyBuilder().externalView("db_11"));
    Assert.assertEquals(ev.getStateMap("db_11_0").get(hostName), "MASTER");
    Assert.assertEquals(ev.getStateMap("db_11_11").get(hostName), "SLAVE");

    // clean up
    controller.syncStop();
    for (MockParticipant participant : participants.values()) {
      participant.syncStop();
    }
  }
View Full Code Here

    addCluster(clusterName);
    addInstancesToCluster(clusterName, "localhost:123", 6, null);
    addResource(clusterName, "db_11", 8);
    rebalanceResource(clusterName, "db_11");

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller_9900");
    controller.syncStart();

    // start mock nodes
    Map<String, MockParticipant> participants =
        new HashMap<String, MockParticipant>();
    for (int i = 0; i < 6; i++) {
      String instanceName = "localhost_123" + i;
      MockParticipant participant =
          new MockParticipant(_zkaddr, clusterName, instanceName);
      participant.syncStart();
      participants.put(instanceName, participant);
    }

    HelixDataAccessor accessor;
    // drop node should fail as not disabled
    String instanceUrl = getInstanceUrl(clusterName, "localhost_1232");
    deleteUrl(instanceUrl, true);

    // disabled node
    String response = assertSuccessPostOperation(instanceUrl, enableInstanceCmd(false), false);
    Assert.assertTrue(response.contains("false"));

    // Cannot drop / swap
    deleteUrl(instanceUrl, true);

    String instancesUrl = getClusterUrl(clusterName) + "/instances";
    response =
        assertSuccessPostOperation(instancesUrl,
            swapInstanceCmd("localhost_1232", "localhost_12320"), true);

    // disconnect the node
    participants.get("localhost_1232").syncStop();

    // add new node then swap instance
    response = assertSuccessPostOperation(instancesUrl, addInstanceCmd("localhost_12320"), false);
    Assert.assertTrue(response.contains("localhost_12320"));

    // swap instance. The instance get swapped out should not exist anymore
    response =
        assertSuccessPostOperation(instancesUrl,
            swapInstanceCmd("localhost_1232", "localhost_12320"), false);
    Assert.assertTrue(response.contains("localhost_12320"));
    Assert.assertFalse(response.contains("localhost_1232\""));

    accessor = participants.get("localhost_1231").getHelixDataAccessor();
    String path = accessor.keyBuilder().instanceConfig("localhost_1232").getPath();
    Assert.assertFalse(_zkclient.exists(path));

    MockParticipant newParticipant =
        new MockParticipant(_zkaddr, clusterName, "localhost_12320");
    newParticipant.syncStart();
    participants.put("localhost_12320", newParticipant);

    boolean verifyResult =
        ClusterStateVerifier
            .verifyByZkCallback(new MasterNbInExtViewVerifier(_zkaddr, clusterName));
    Assert.assertTrue(verifyResult);

    // clean up
    controller.syncStop();
    for (MockParticipant participant : participants.values()) {
      participant.syncStop();
    }
  }
View Full Code Here

    MockParticipant participant =
        new MockParticipant(_zkaddr, clusterName, "localhost_12918");
    participant.syncStart();

    // Add a controller
    MockController controller =
        new MockController(_zkaddr, clusterName, "controller_0");
    controller.syncStart();

    // Make sure everything gets assigned
    Thread.sleep(1000);
    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // Check the metrics
    Assert.assertTrue(metricsExist(clusterName, participant.getInstanceName()));

    // Stop the controller
    controller.syncStop();

    // Check the metrics
    Thread.sleep(1000);
    Assert.assertFalse(metricsExist(clusterName, participant.getInstanceName()));
  }
View Full Code Here

        PARTITION_NUM, // partitions per resource
        N, // number of nodes
        2, // replicas
        "MasterSlave", true); // do rebalance

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller");
    controller.syncStart();

    // start participants
    Map<String, MockParticipant> participants =
        new HashMap<String, MockParticipant>();
    Map<String, HelixCustomCodeRunner> customCodeRunners =
        new HashMap<String, HelixCustomCodeRunner>();
    Map<String, DummyCallback> callbacks = new HashMap<String, DummyCallback>();
    for (int i = 0; i < N; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants
          .put(instanceName, new MockParticipant(_zkaddr, clusterName, instanceName));

      customCodeRunners.put(instanceName, new HelixCustomCodeRunner(participants.get(instanceName),
          _zkaddr));
      callbacks.put(instanceName, new DummyCallback());

      customCodeRunners.get(instanceName).invoke(callbacks.get(instanceName))
          .on(ChangeType.LIVE_INSTANCE).usingLeaderStandbyModel("TestParticLeader").start();
      participants.get(instanceName).syncStart();
    }

    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // Make sure callback is registered
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_zkclient);
    final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    final String customCodeRunnerResource =
        customCodeRunners.get("localhost_12918").getResourceName();
    ExternalView extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
    Map<String, String> instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
    String leader = null;
    for (String instance : instanceStates.keySet()) {
      String state = instanceStates.get(instance);
      if ("LEADER".equals(state)) {
        leader = instance;
        break;
      }
    }
    Assert.assertNotNull(leader);
    for (String instance : callbacks.keySet()) {
      DummyCallback callback = callbacks.get(instance);
      if (instance.equals(leader)) {
        Assert.assertTrue(callback.isInitTypeInvoked());
      } else {
        Assert.assertFalse(callback.isInitTypeInvoked());
      }
      callback.reset();
    }

    // Disable custom-code runner resource
    HelixAdmin admin = new ZKHelixAdmin(_zkclient);
    admin.enableResource(clusterName, customCodeRunnerResource, false);

    // Verify that states of custom-code runner are all OFFLINE
    result = TestHelper.verify(new TestHelper.Verifier() {

      @Override
      public boolean verify() throws Exception {
        PropertyKey.Builder keyBuilder = accessor.keyBuilder();

        ExternalView extView =
            accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
        if (extView == null) {
          return false;
        }
        Set<String> partitionSet = extView.getPartitionSet();
        if (partitionSet == null || partitionSet.size() != PARTITION_NUM) {
          return false;
        }
        for (String partition : partitionSet) {
          Map<String, String> instanceStates = extView.getStateMap(partition);
          for (String state : instanceStates.values()) {
            if (!"OFFLINE".equals(state)) {
              return false;
            }
          }
        }
        return true;
      }
    }, 10 * 1000);
    Assert.assertTrue(result);

    // Change live-instance should not invoke any custom-code runner
    LiveInstance fakeInstance = new LiveInstance("fakeInstance");
    fakeInstance.setSessionId("fakeSessionId");
    fakeInstance.setHelixVersion("0.6");
    accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
    Thread.sleep(1000);

    for (Map.Entry<String, DummyCallback> e : callbacks.entrySet()) {
      String instance = e.getKey();
      DummyCallback callback = e.getValue();
      Assert.assertFalse(callback.isInitTypeInvoked());
      Assert.assertFalse(callback.isCallbackTypeInvoked());

      // Ensure that we were told that a leader stopped being the leader
      if (instance.equals(leader)) {
        Assert.assertTrue(callback.isFinalizeTypeInvoked());
      }
    }

    // Remove fake instance
    accessor.removeProperty(keyBuilder.liveInstance("fakeInstance"));

    // Re-enable custom-code runner
    admin.enableResource(clusterName, customCodeRunnerResource, true);
    result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // Verify that custom-invoke is invoked again
    extView = accessor.getProperty(keyBuilder.externalView(customCodeRunnerResource));
    instanceStates = extView.getStateMap(customCodeRunnerResource + "_0");
    leader = null;
    for (String instance : instanceStates.keySet()) {
      String state = instanceStates.get(instance);
      if ("LEADER".equals(state)) {
        leader = instance;
        break;
      }
    }
    Assert.assertNotNull(leader);
    for (String instance : callbacks.keySet()) {
      DummyCallback callback = callbacks.get(instance);
      if (instance.equals(leader)) {
        Assert.assertTrue(callback.isInitTypeInvoked());
      } else {
        Assert.assertFalse(callback.isInitTypeInvoked());
      }
      callback.reset();
    }

    // Add a fake instance should invoke custom-code runner
    accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
    Thread.sleep(1000);
    for (String instance : callbacks.keySet()) {
      DummyCallback callback = callbacks.get(instance);
      if (instance.equals(leader)) {
        Assert.assertTrue(callback.isCallbackTypeInvoked());
      } else {
        Assert.assertFalse(callback.isCallbackTypeInvoked());
      }
    }

    // Clean up
    controller.syncStop();
    for (MockParticipant participant : participants.values()) {
      participant.syncStop();
    }

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here

        PARTITION_NUM, // partitions per resource
        N, // number of nodes
        2, // replicas
        "MasterSlave", true); // do rebalance

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller");
    controller.syncStart();

    // start participants
    MockParticipant participants[] = new MockParticipant[N];
    for (int i = 0; i < N; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants[i] = new MockParticipant(_zkaddr, clusterName, instanceName);
      participants[i].syncStart();
    }

    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // Disable TestDB0
    enableResource(clusterName, false);
    checkExternalView(clusterName);

    // Re-enable TestDB0
    enableResource(clusterName, true);
    result =
        ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(
            _zkaddr, clusterName));
    Assert.assertTrue(result);

    // Clean up
    controller.syncStop();
    for (int i = 0; i < N; i++) {
      participants[i].syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
  }
View Full Code Here

        PARTITION_NUM, // partitions per resource
        N, // number of nodes
        2, // replicas
        "MasterSlave", RebalanceMode.FULL_AUTO, true); // do rebalance

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller");
    controller.syncStart();

    // start participants
    MockParticipant participants[] = new MockParticipant[N];
    for (int i = 0; i < N; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants[i] = new MockParticipant(_zkaddr, clusterName, instanceName);
      participants[i].syncStart();
    }

    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // disable TestDB0
    enableResource(clusterName, false);
    checkExternalView(clusterName);

    // Re-enable TestDB0
    enableResource(clusterName, true);
    result =
        ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(
            _zkaddr, clusterName));
    Assert.assertTrue(result);

    // Clean up
    controller.syncStop();
    for (int i = 0; i < N; i++) {
      participants[i].syncStop();
    }

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here

    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
    idealState.setPartitionState("TestDB0_0", "localhost_12918", "SLAVE");
    idealState.setPartitionState("TestDB0_0", "localhost_12919", "SLAVE");
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller");
    controller.syncStart();

    // start participants
    MockParticipant participants[] = new MockParticipant[N];
    for (int i = 0; i < N; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants[i] = new MockParticipant(_zkaddr, clusterName, instanceName);
      participants[i].syncStart();
    }

    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // Disable TestDB0
    enableResource(clusterName, false);
    checkExternalView(clusterName);

    // Re-enable TestDB0
    enableResource(clusterName, true);
    result =
        ClusterStateVerifier.verifyByPolling(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(
            _zkaddr, clusterName));
    Assert.assertTrue(result);

    // Clean up
    controller.syncStop();
    for (int i = 0; i < N; i++) {
      participants[i].syncStop();
    }

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here

        }
      });
    }

    MockController controller =
        new MockController(_zkaddr, clusterName, "controller_0");
    controller.syncStart();

    // start participants
    for (int i = 0; i < 5; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants[i] = new MockParticipant(_zkaddr, clusterName, instanceName);
      participants[i].syncStart();
    }

    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new MasterNbInExtViewVerifier(_zkaddr, clusterName));
    Assert.assertTrue(result);

    result =
        ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(_zkaddr,
            clusterName));
    Assert.assertTrue(result);

    Assert.assertTrue(success.get());

    // clean up
    controller.syncStop();
    for (int i = 0; i < 5; i++) {
      participants[i].syncStop();
    }

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here

        2, // replicas
        "MasterSlave", false);
    autoRebalance(clusterName);

    // start controller
    MockController controller =
        new MockController(_zkaddr, clusterName, "controller_0");
    controller.syncStart();

    // start participants
    MockParticipant[] participants = new MockParticipant[n];
    for (int i = 0; i < n; i++) {
      String instanceName = "localhost_" + (12918 + i);

      participants[i] = new MockParticipant(_zkaddr, clusterName, instanceName);
      participants[i].syncStart();
    }

    boolean result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // stop controller, redefine state model definition, and re-start controller
    controller.syncStop();
    redefineStateModelDef(clusterName);
    controller = new MockController(_zkaddr, clusterName, "controller_0");
    controller.syncStart();

    result =
        ClusterStateVerifier
            .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(_zkaddr,
                clusterName));
    Assert.assertTrue(result);

    // clean up
    // wait for all zk callbacks done
    Thread.sleep(1000);
    controller.syncStop();
    for (int i = 0; i < n; i++) {
      participants[i].syncStop();
    }

    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
View Full Code Here

TOP

Related Classes of org.apache.helix.manager.zk.MockController

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.