use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestControllerLeadershipChange method testMissingTopStateDurationMonitoring.
@Test
public void testMissingTopStateDurationMonitoring() throws Exception {
String clusterName = "testCluster-TestControllerLeadershipChange";
String instanceName = clusterName + "-participant";
String resourceName = "testResource";
int numPartition = 1;
int numReplica = 1;
String stateModel = "LeaderStandby";
ObjectName resourceMBeanObjectName = getResourceMonitorObjectName(clusterName, resourceName);
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
// Create cluster
_gSetupTool.addCluster(clusterName, true);
// Create participant
_gSetupTool.addInstanceToCluster(clusterName, instanceName);
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participant.syncStart();
// Create controller, since this is the only controller, it will be the leader
HelixManager manager1 = HelixManagerFactory.getZKHelixManager(clusterName, clusterName + "-manager1", InstanceType.CONTROLLER, ZK_ADDR);
manager1.connect();
Assert.assertTrue(manager1.isLeader());
// Create resource
_gSetupTool.addResourceToCluster(clusterName, resourceName, numPartition, stateModel, IdealState.RebalanceMode.SEMI_AUTO.name());
// Rebalance Resource
_gSetupTool.rebalanceResource(clusterName, resourceName, numReplica);
// Wait for rebalance
Thread.sleep(2000);
// Trigger missing top state in manager1
participant.syncStop();
Thread.sleep(2000);
// Starting manager2
HelixManager manager2 = HelixManagerFactory.getZKHelixManager(clusterName, clusterName + "-manager2", InstanceType.CONTROLLER, ZK_ADDR);
manager2.connect();
Assert.assertFalse(manager2.isLeader());
// Set leader to manager2
setLeader(manager2);
Assert.assertFalse(manager1.isLeader());
Assert.assertTrue(manager2.isLeader());
// Make resource top state to come back
participant = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participant.syncStart();
// Wait for rebalance
Thread.sleep(2000);
setLeader(manager1);
Assert.assertTrue(manager1.isLeader());
Assert.assertFalse(manager2.isLeader());
_gSetupTool.rebalanceResource(clusterName, resourceName, numReplica);
// Wait for manager1 to update
Thread.sleep(2000);
// Resource lost top state, and manager1 lost leadership for 4000ms, because manager1 will
// clean monitoring cache after re-gaining leadership, so max value of hand off duration should
// not have such a large value
Assert.assertTrue((long) beanServer.getAttribute(resourceMBeanObjectName, "PartitionTopStateHandoffDurationGauge.Max") < 500);
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestAutoRebalance method testAutoRebalance.
@Test()
public void testAutoRebalance() throws Exception {
// kill 1 node
_participants[0].syncStop();
boolean result = ClusterStateVerifier.verifyByZkCallback(new ExternalViewBalancedVerifier(_gZkClient, CLUSTER_NAME, TEST_DB));
Assert.assertTrue(result);
// add 2 nodes
for (int i = 0; i < 2; i++) {
String storageNodeName = PARTICIPANT_PREFIX + "_" + (1000 + i);
_setupTool.addInstanceToCluster(CLUSTER_NAME, storageNodeName);
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, storageNodeName.replace(':', '_'));
participant.syncStart();
}
Thread.sleep(1000);
result = ClusterStateVerifier.verifyByZkCallback(new ExternalViewBalancedVerifier(_gZkClient, CLUSTER_NAME, TEST_DB));
Assert.assertTrue(result);
result = ClusterStateVerifier.verifyByZkCallback(new ExternalViewBalancedVerifier(_gZkClient, CLUSTER_NAME, db2));
Assert.assertTrue(result);
HelixDataAccessor accessor = new ZKHelixDataAccessor(CLUSTER_NAME, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
ExternalView ev = accessor.getProperty(keyBuilder.externalView(db2));
Set<String> instancesSet = new HashSet<String>();
for (String partitionName : ev.getRecord().getMapFields().keySet()) {
Map<String, String> assignmentMap = ev.getRecord().getMapField(partitionName);
for (String instance : assignmentMap.keySet()) {
instancesSet.add(instance);
}
}
Assert.assertEquals(instancesSet.size(), 2);
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestClusterInMaintenanceModeWhenReachingMaxPartition method beforeClass.
@BeforeClass
public void beforeClass() throws Exception {
System.out.println("START " + CLASS_NAME + " at " + new Date(System.currentTimeMillis()));
String namespace = "/" + CLUSTER_NAME;
if (_gZkClient.exists(namespace)) {
_gZkClient.deleteRecursively(namespace);
}
_gSetupTool.addCluster(CLUSTER_NAME, true);
for (int i = 0; i < NUM_NODE; i++) {
String storageNodeName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
_gSetupTool.addInstanceToCluster(CLUSTER_NAME, storageNodeName);
// start dummy participants
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, storageNodeName);
participant.syncStart();
_participants.add(participant);
}
// start controller
String controllerName = CONTROLLER_PREFIX + "_0";
_controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
_controller.syncStart();
_clusterVerifier = new BestPossibleExternalViewVerifier.Builder(CLUSTER_NAME).setZkAddr(ZK_ADDR).build();
enablePersistBestPossibleAssignment(_gZkClient, CLUSTER_NAME, true);
_dataAccessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestClusterInMaintenanceModeWhenReachingOfflineInstancesLimit method beforeClass.
@BeforeClass
public void beforeClass() throws Exception {
System.out.println("START " + CLASS_NAME + " at " + new Date(System.currentTimeMillis()));
String namespace = "/" + CLUSTER_NAME;
if (_gZkClient.exists(namespace)) {
_gZkClient.deleteRecursively(namespace);
}
_gSetupTool.addCluster(CLUSTER_NAME, true);
for (int i = 0; i < NUM_NODE; i++) {
String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
_gSetupTool.addInstanceToCluster(CLUSTER_NAME, instanceName);
// start dummy participants
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instanceName);
participant.syncStart();
_participants.add(participant);
}
// start controller
String controllerName = CONTROLLER_PREFIX + "_0";
_controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
_controller.syncStart();
_clusterVerifier = new BestPossibleExternalViewVerifier.Builder(CLUSTER_NAME).setZkAddr(ZK_ADDR).build();
enablePersistBestPossibleAssignment(_gZkClient, CLUSTER_NAME, true);
_dataAccessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
ClusterConfig clusterConfig = configAccessor.getClusterConfig(CLUSTER_NAME);
clusterConfig.setMaxOfflineInstancesAllowed(_maxOfflineInstancesAllowed);
configAccessor.setClusterConfig(CLUSTER_NAME, clusterConfig);
for (int i = 0; i < 3; i++) {
String db = "Test-DB-" + i++;
createResourceWithDelayedRebalance(CLUSTER_NAME, db, BuiltInStateModelDefinitions.MasterSlave.name(), _PARTITIONS, 3, 3, -1);
}
Thread.sleep(100);
Assert.assertTrue(_clusterVerifier.verify());
}
use of org.apache.helix.integration.manager.MockParticipantManager in project helix by apache.
the class TestFullAutoNodeTagging method testUntag.
@Test
public void testUntag() throws Exception {
final int NUM_PARTICIPANTS = 2;
final int NUM_PARTITIONS = 4;
final int NUM_REPLICAS = 1;
final String RESOURCE_NAME = "TestResource0";
final String TAG = "ASSIGNABLE";
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
final String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// Set up cluster
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestResource", // resources
1, // partitions per resource
NUM_PARTITIONS, // number of nodes
NUM_PARTICIPANTS, // replicas
NUM_REPLICAS, // use FULL_AUTO mode to test node tagging
"OnlineOffline", // use FULL_AUTO mode to test node tagging
RebalanceMode.FULL_AUTO, // do rebalance
true);
// Tag the resource
final HelixAdmin helixAdmin = new ZKHelixAdmin(_gZkClient);
IdealState idealState = helixAdmin.getResourceIdealState(clusterName, RESOURCE_NAME);
idealState.setInstanceGroupTag(TAG);
helixAdmin.setResourceIdealState(clusterName, RESOURCE_NAME, idealState);
// Get a data accessor
final HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
final PropertyKey.Builder keyBuilder = accessor.keyBuilder();
// Tag the participants
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
final String instanceName = "localhost_" + (12918 + i);
helixAdmin.addInstanceTag(clusterName, instanceName, TAG);
}
// Start controller
ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller");
controller.syncStart();
// Start participants
MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
final String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
// Verify that there are NUM_PARTITIONS partitions in the external view, each having
// NUM_REPLICAS replicas, where all assigned replicas are to tagged nodes, and they are all
// ONLINE.
Verifier v = new Verifier() {
@Override
public boolean verify() throws Exception {
ExternalView externalView = pollForProperty(ExternalView.class, accessor, keyBuilder.externalView(RESOURCE_NAME), true);
if (externalView == null) {
return false;
}
Set<String> taggedInstances = Sets.newHashSet(helixAdmin.getInstancesInClusterWithTag(clusterName, TAG));
Set<String> partitionSet = externalView.getPartitionSet();
if (partitionSet.size() != NUM_PARTITIONS) {
return false;
}
for (String partitionName : partitionSet) {
Map<String, String> stateMap = externalView.getStateMap(partitionName);
if (stateMap.size() != NUM_REPLICAS) {
return false;
}
for (String participantName : stateMap.keySet()) {
if (!taggedInstances.contains(participantName)) {
return false;
}
String state = stateMap.get(participantName);
if (!state.equalsIgnoreCase("ONLINE")) {
return false;
}
}
}
return true;
}
};
// Run the verifier for both nodes tagged
boolean initialResult = TestHelper.verify(v, 10 * 1000);
Assert.assertTrue(initialResult);
// Untag a node
helixAdmin.removeInstanceTag(clusterName, "localhost_12918", TAG);
// Verify again
boolean finalResult = TestHelper.verify(v, 10 * 1000);
Assert.assertTrue(finalResult);
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Aggregations