use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestAutoFallbackPropertyStore method testFailOnMultiGet.
@Test
public void testFailOnMultiGet() {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
String root = String.format("/%s/%s", clusterName, PropertyType.PROPERTYSTORE.name());
String fallbackRoot = String.format("/%s/%s", clusterName, "HELIX_PROPERTYSTORE");
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
// create 0-9 under fallbackRoot
for (int i = 0; i < 10; i++) {
String path = String.format("%s/%d", fallbackRoot, i);
baseAccessor.create(path, new ZNRecord(Integer.toString(i)), AccessOption.PERSISTENT);
}
AutoFallbackPropertyStore<ZNRecord> store = new AutoFallbackPropertyStore<ZNRecord>(baseAccessor, root, fallbackRoot);
List<String> paths = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
String path = String.format("/%d", i);
Assert.assertFalse(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should not exist under new location");
if (i < 10) {
Assert.assertTrue(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should exist under fallback location");
} else {
Assert.assertFalse(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should not exist under fallback location");
}
paths.add(path);
}
// test multi-exist
boolean[] exists = store.exists(paths, 0);
for (int i = 0; i < paths.size(); i++) {
if (i < 10) {
Assert.assertTrue(exists[i]);
} else {
Assert.assertFalse(exists[i]);
}
}
// test multi-getStat
Stat[] stats = store.getStats(paths, 0);
for (int i = 0; i < paths.size(); i++) {
if (i < 10) {
Assert.assertNotNull(stats[i]);
} else {
Assert.assertNull(stats[i]);
}
}
// test multi-get
List<ZNRecord> records = store.get(paths, null, 0);
Assert.assertNotNull(records);
Assert.assertEquals(records.size(), 20);
for (int i = 0; i < 20; i++) {
ZNRecord record = records.get(i);
String path = paths.get(i);
if (i < 10) {
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), Integer.toString(i));
} else {
Assert.assertNull(record);
}
Assert.assertFalse(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should not exist under new location after get");
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestAutoFallbackPropertyStore method testMultiSet.
@Test
public void testMultiSet() {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
String root = String.format("/%s/%s", clusterName, PropertyType.PROPERTYSTORE.name());
String fallbackRoot = String.format("/%s/%s", clusterName, "HELIX_PROPERTYSTORE");
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
// create 0-9 under fallbackRoot
for (int i = 0; i < 10; i++) {
String path = String.format("%s/%d", fallbackRoot, i);
baseAccessor.create(path, new ZNRecord(Integer.toString(i)), AccessOption.PERSISTENT);
}
AutoFallbackPropertyStore<ZNRecord> store = new AutoFallbackPropertyStore<ZNRecord>(baseAccessor, root, fallbackRoot);
List<String> paths = new ArrayList<String>();
List<ZNRecord> records = new ArrayList<ZNRecord>();
for (int i = 0; i < 10; i++) {
String path = String.format("/%d", i);
Assert.assertFalse(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should not exist under new location");
Assert.assertTrue(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should exist under fallback location");
paths.add(path);
ZNRecord record = new ZNRecord("new" + i);
records.add(record);
}
boolean[] succeed = store.setChildren(paths, records, AccessOption.PERSISTENT);
for (int i = 0; i < 10; i++) {
Assert.assertTrue(succeed[i]);
String path = String.format("/%d", i);
ZNRecord record = baseAccessor.get(String.format("%s%s", fallbackRoot, path), null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), Integer.toString(i));
record = baseAccessor.get(String.format("%s%s", root, path), null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), "new" + i);
}
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestAutoFallbackPropertyStore method testSingleSet.
@Test
public void testSingleSet() {
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
String root = String.format("/%s/%s", clusterName, PropertyType.PROPERTYSTORE.name());
String fallbackRoot = String.format("/%s/%s", clusterName, "HELIX_PROPERTYSTORE");
ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
// create 0 under fallbackRoot
for (int i = 0; i < 1; i++) {
String path = String.format("%s/%d", fallbackRoot, i);
baseAccessor.create(path, new ZNRecord(Integer.toString(i)), AccessOption.PERSISTENT);
}
AutoFallbackPropertyStore<ZNRecord> store = new AutoFallbackPropertyStore<ZNRecord>(baseAccessor, root, fallbackRoot);
String path = String.format("/%d", 0);
Assert.assertFalse(baseAccessor.exists(String.format("%s%s", root, path), 0), "Should not exist under new location");
Assert.assertTrue(baseAccessor.exists(String.format("%s%s", fallbackRoot, path), 0), "Should exist under fallback location");
ZNRecord record = new ZNRecord("new0");
boolean succeed = store.set(path, record, AccessOption.PERSISTENT);
Assert.assertTrue(succeed);
record = baseAccessor.get(String.format("%s%s", fallbackRoot, path), null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), "0");
record = baseAccessor.get(String.format("%s%s", root, path), null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), "new0");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestDistControllerElection method testControllerParticipant.
@Test()
public void testControllerParticipant() throws Exception {
String className = getShortClassName();
LOG.info("RUN " + className + " at " + new Date(System.currentTimeMillis()));
final String clusterName = CONTROLLER_CLUSTER_PREFIX + "_" + className + "_" + "testControllerParticipant";
String path = "/" + clusterName;
if (_gZkClient.exists(path)) {
_gZkClient.deleteRecursively(path);
}
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
final String controllerName = "controller_0";
HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.CONTROLLER_PARTICIPANT, _gZkClient);
GenericHelixController controller0 = new GenericHelixController();
List<HelixTimerTask> timerTasks = Collections.emptyList();
DistributedLeaderElection election = new DistributedLeaderElection(manager, controller0, timerTasks);
NotificationContext context = new NotificationContext(manager);
context.setType(NotificationContext.Type.CALLBACK);
election.onControllerChange(context);
LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
// path = PropertyPathConfig.getPath(PropertyType.LEADER, clusterName);
// ZNRecord leaderRecord = _gZkClient.<ZNRecord> readData(path);
// AssertJUnit.assertEquals(controllerName, leaderRecord.getSimpleField("LEADER"));
// AssertJUnit.assertNotNull(election.getController());
// AssertJUnit.assertNotNull(election.getLeader());
manager = new MockZKHelixManager(clusterName, "controller_1", InstanceType.CONTROLLER_PARTICIPANT, _gZkClient);
GenericHelixController controller1 = new GenericHelixController();
election = new DistributedLeaderElection(manager, controller1, timerTasks);
context = new NotificationContext(manager);
context.setType(NotificationContext.Type.CALLBACK);
election.onControllerChange(context);
liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
// leaderRecord = _gZkClient.<ZNRecord> readData(path);
// AssertJUnit.assertEquals(controllerName, leaderRecord.getSimpleField("LEADER"));
// AssertJUnit.assertNull(election.getController());
// AssertJUnit.assertNull(election.getLeader());
LOG.info("END " + getShortClassName() + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.manager.zk.ZkBaseDataAccessor in project helix by apache.
the class TestDistControllerElection method testController.
@Test()
public void testController() throws Exception {
System.out.println("START TestDistControllerElection at " + new Date(System.currentTimeMillis()));
String className = getShortClassName();
final String clusterName = CLUSTER_PREFIX + "_" + className + "_" + "testController";
String path = "/" + clusterName;
if (_gZkClient.exists(path)) {
_gZkClient.deleteRecursively(path);
}
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
final String controllerName = "controller_0";
HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.CONTROLLER, _gZkClient);
GenericHelixController controller0 = new GenericHelixController();
List<HelixTimerTask> timerTasks = Collections.emptyList();
DistributedLeaderElection election = new DistributedLeaderElection(manager, controller0, timerTasks);
NotificationContext context = new NotificationContext(manager);
context.setType(NotificationContext.Type.INIT);
election.onControllerChange(context);
// path = PropertyPathConfig.getPath(PropertyType.LEADER, clusterName);
// ZNRecord leaderRecord = _gZkClient.<ZNRecord> readData(path);
LiveInstance liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
// AssertJUnit.assertNotNull(election.getController());
// AssertJUnit.assertNull(election.getLeader());
manager = new MockZKHelixManager(clusterName, "controller_1", InstanceType.CONTROLLER, _gZkClient);
GenericHelixController controller1 = new GenericHelixController();
election = new DistributedLeaderElection(manager, controller1, timerTasks);
context = new NotificationContext(manager);
context.setType(NotificationContext.Type.INIT);
election.onControllerChange(context);
// leaderRecord = _gZkClient.<ZNRecord> readData(path);
liveInstance = accessor.getProperty(keyBuilder.controllerLeader());
AssertJUnit.assertEquals(controllerName, liveInstance.getInstanceName());
// AssertJUnit.assertNull(election.getController());
// AssertJUnit.assertNull(election.getLeader());
System.out.println("END TestDistControllerElection at " + new Date(System.currentTimeMillis()));
}
Aggregations