use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestSemiAutoRebalance method beforeClass.
@BeforeClass
public void beforeClass() throws InterruptedException {
System.out.println("START " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
String namespace = "/" + CLUSTER_NAME;
if (_gZkClient.exists(namespace)) {
_gZkClient.deleteRecursively(namespace);
}
// setup storage cluster
_gSetupTool.addCluster(CLUSTER_NAME, true);
_gSetupTool.addResourceToCluster(CLUSTER_NAME, DB_NAME, PARTITION_NUMBER, STATE_MODEL, IdealState.RebalanceMode.SEMI_AUTO.toString());
_accessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
_keyBuilder = _accessor.keyBuilder();
List<String> instances = new ArrayList<String>();
for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
String instance = PARTICIPANT_PREFIX + "_" + (PARTICIPANT_START_PORT + i);
_gSetupTool.addInstanceToCluster(CLUSTER_NAME, instance);
instances.add(instance);
}
_gSetupTool.rebalanceStorageCluster(CLUSTER_NAME, DB_NAME, REPLICA_NUMBER);
// start dummy participants
for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, instances.get(i));
participant.syncStart();
_participants.add(participant);
}
// start controller
String controllerName = CONTROLLER_PREFIX + "_0";
_controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
_controller.syncStart();
Thread.sleep(1000);
// verify ideal state and external view
IdealState idealState = _accessor.getProperty(_keyBuilder.idealStates(DB_NAME));
Assert.assertNotNull(idealState);
Assert.assertEquals(idealState.getNumPartitions(), PARTITION_NUMBER);
for (String partition : idealState.getPartitionSet()) {
List<String> preferenceList = idealState.getPreferenceList(partition);
Assert.assertNotNull(preferenceList);
Assert.assertEquals(preferenceList.size(), REPLICA_NUMBER);
}
ExternalView externalView = _accessor.getProperty(_keyBuilder.externalView(DB_NAME));
Assert.assertNotNull(externalView);
Assert.assertEquals(externalView.getPartitionSet().size(), PARTITION_NUMBER);
for (String partition : externalView.getPartitionSet()) {
Map<String, String> stateMap = externalView.getStateMap(partition);
Assert.assertEquals(stateMap.size(), REPLICA_NUMBER);
int masters = 0;
for (String state : stateMap.values()) {
if (state.equals(MasterSlaveSMD.States.MASTER.name())) {
++masters;
}
}
Assert.assertEquals(masters, 1);
}
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestP2PMessageSemiAuto method beforeClass.
@BeforeClass
public void beforeClass() throws InterruptedException {
System.out.println("START " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
// setup storage cluster
_gSetupTool.addCluster(CLUSTER_NAME, true);
for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
String instance = PARTICIPANT_PREFIX + "_" + (PARTICIPANT_START_PORT + i);
_gSetupTool.addInstanceToCluster(CLUSTER_NAME, instance);
_instances.add(instance);
}
// start dummy participants
for (int i = 0; i < PARTICIPANT_NUMBER; i++) {
MockParticipantManager participant = new MockParticipantManager(ZK_ADDR, CLUSTER_NAME, _instances.get(i));
participant.setTransition(new DelayedTransitionBase(50));
participant.syncStart();
_participants.add(participant);
}
createDBInSemiAuto(_gSetupTool, CLUSTER_NAME, DB_NAME_1, _instances, BuiltInStateModelDefinitions.MasterSlave.name(), PARTITION_NUMBER, REPLICA_NUMBER);
createDBInSemiAuto(_gSetupTool, CLUSTER_NAME, DB_NAME_2, _instances, BuiltInStateModelDefinitions.MasterSlave.name(), PARTITION_NUMBER, REPLICA_NUMBER);
// start controller
String controllerName = CONTROLLER_PREFIX + "_0";
_controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, controllerName);
_controller.syncStart();
_clusterVerifier = new BestPossibleExternalViewVerifier.Builder(CLUSTER_NAME).setZkClient(_gZkClient).build();
Assert.assertTrue(_clusterVerifier.verify());
_configAccessor = new ConfigAccessor(_gZkClient);
_accessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class TestParticipantManager method simpleIntegrationTest.
@Test
public void simpleIntegrationTest() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 1;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
// participant port
TestHelper.setupCluster(// participant port
clusterName, // participant port
ZK_ADDR, // participant port
12918, // participant name prefix
"localhost", // resource name prefix
"TestDB", // resources
1, // partitions per resource
4, // number of nodes
n, // replicas
1, "MasterSlave", // do rebalance
true);
HelixManager participant = new ZKHelixManager(clusterName, "localhost_12918", InstanceType.PARTICIPANT, ZK_ADDR);
participant.getStateMachineEngine().registerStateModelFactory("MasterSlave", new MockMSModelFactory());
participant.connect();
HelixManager controller = new ZKHelixManager(clusterName, "controller_0", InstanceType.CONTROLLER, ZK_ADDR);
controller.connect();
boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
Assert.assertTrue(result);
// cleanup
controller.disconnect();
participant.disconnect();
// verify all live-instances and leader nodes are gone
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Assert.assertNull(accessor.getProperty(keyBuilder.liveInstance("localhost_12918")));
Assert.assertNull(accessor.getProperty(keyBuilder.controllerLeader()));
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class ClusterSetup method swapInstance.
public void swapInstance(String clusterName, String oldInstanceName, String newInstanceName) {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
Builder keyBuilder = accessor.keyBuilder();
InstanceConfig oldConfig = accessor.getProperty(keyBuilder.instanceConfig(oldInstanceName));
if (oldConfig == null) {
String error = "Old instance " + oldInstanceName + " does not exist, cannot swap";
_logger.warn(error);
throw new HelixException(error);
}
InstanceConfig newConfig = accessor.getProperty(keyBuilder.instanceConfig(newInstanceName));
if (newConfig == null) {
String error = "New instance " + newInstanceName + " does not exist, cannot swap";
_logger.warn(error);
throw new HelixException(error);
}
ClusterConfig clusterConfig = accessor.getProperty(keyBuilder.clusterConfig());
// ensure old instance is disabled, otherwise fail
if (oldConfig.getInstanceEnabled() && (clusterConfig.getDisabledInstances() == null || !clusterConfig.getDisabledInstances().containsKey(oldInstanceName))) {
String error = "Old instance " + oldInstanceName + " is enabled, it need to be disabled and turned off";
_logger.warn(error);
throw new HelixException(error);
}
// ensure old instance is down, otherwise fail
List<String> liveInstanceNames = accessor.getChildNames(accessor.keyBuilder().liveInstances());
if (liveInstanceNames.contains(oldInstanceName)) {
String error = "Old instance " + oldInstanceName + " is still on, it need to be disabled and turned off";
_logger.warn(error);
throw new HelixException(error);
}
dropInstanceFromCluster(clusterName, oldInstanceName);
List<IdealState> existingIdealStates = accessor.getChildValues(accessor.keyBuilder().idealStates());
for (IdealState idealState : existingIdealStates) {
swapInstanceInIdealState(idealState, oldInstanceName, newInstanceName);
accessor.setProperty(accessor.keyBuilder().idealStates(idealState.getResourceName()), idealState);
}
}
use of org.apache.helix.manager.zk.ZKHelixDataAccessor in project helix by apache.
the class ClusterSetup method dropInstanceFromCluster.
public void dropInstanceFromCluster(String clusterName, String instanceId) {
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
Builder keyBuilder = accessor.keyBuilder();
InstanceConfig instanceConfig = InstanceConfig.toInstanceConfig(instanceId);
instanceId = instanceConfig.getInstanceName();
// ensure node is stopped
LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance(instanceId));
if (liveInstance != null) {
throw new HelixException("Can't drop " + instanceId + ", please stop " + instanceId + " before drop it");
}
InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceId));
if (config == null) {
String error = "Node " + instanceId + " does not exist, cannot drop";
_logger.warn(error);
throw new HelixException(error);
}
ClusterConfig clusterConfig = accessor.getProperty(keyBuilder.clusterConfig());
// ensure node is disabled, otherwise fail
if (config.getInstanceEnabled() && (clusterConfig.getDisabledInstances() == null || !clusterConfig.getDisabledInstances().containsKey(instanceId))) {
String error = "Node " + instanceId + " is enabled, cannot drop";
_logger.warn(error);
throw new HelixException(error);
}
_admin.dropInstance(clusterName, config);
}
Aggregations