use of org.apache.helix.ZNRecord 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.ZNRecord in project helix by apache.
the class TestZkHelixPropertyStore method testSet.
@Test
public void testSet() {
// Logger.getRootLogger().setLevel(Level.INFO);
System.out.println("START testSet() at " + new Date(System.currentTimeMillis()));
String subRoot = _root + "/" + "set";
List<String> subscribedPaths = new ArrayList<String>();
subscribedPaths.add(subRoot);
ZkHelixPropertyStore<ZNRecord> store = new ZkHelixPropertyStore<ZNRecord>(new ZkBaseDataAccessor<ZNRecord>(_gZkClient), subRoot, subscribedPaths);
// test set
setNodes(store, 'a', false);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
String nodeId = getNodeId(i, j);
String key = getSecondLevelKey(i, j);
ZNRecord record = store.get(key, null, 0);
Assert.assertEquals(record.getId(), nodeId);
}
}
// test get from cache
long startT = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
ZNRecord record = store.get("/node_0/childNode_0_0", null, 0);
Assert.assertNotNull(record);
}
long endT = System.currentTimeMillis();
System.out.println("1000 Get() time used: " + (endT - startT) + "ms");
long latency = endT - startT;
Assert.assertTrue(latency < 200, "1000 Gets should be finished within 200ms, but was " + latency + " ms");
store.stop();
System.out.println("END testSet() at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkHelixPropertyStore method testSetInvalidPath.
@Test
public void testSetInvalidPath() {
String subRoot = _root + "/" + "setInvalidPath";
ZkHelixPropertyStore<ZNRecord> store = new ZkHelixPropertyStore<ZNRecord>(new ZkBaseDataAccessor<ZNRecord>(_gZkClient), subRoot, null);
try {
store.set("abc/xyz", new ZNRecord("testInvalid"), AccessOption.PERSISTENT);
Assert.fail("Should throw illegal-argument-exception since path doesn't start with /");
} catch (IllegalArgumentException e) {
// e.printStackTrace();
// OK
} catch (Exception e) {
Assert.fail("Should not throw exceptions other than illegal-argument");
}
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestZkManagerWithAutoFallbackStore method testBasic.
@Test
public void testBasic() throws Exception {
// Logger.getRootLogger().setLevel(Level.INFO);
String className = TestHelper.getTestClassName();
String methodName = TestHelper.getTestMethodName();
String clusterName = className + "_" + methodName;
int n = 2;
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
32, // number of nodes
n, // replicas
2, "MasterSlave", // do rebalance
false);
// start participants
MockParticipantManager[] participants = new MockParticipantManager[n];
for (int i = 0; i < 1; i++) {
String instanceName = "localhost_" + (12918 + i);
participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
participants[i].syncStart();
}
// add some data to fallback path: HELIX_PROPERTYSTORE
BaseDataAccessor<ZNRecord> accessor = participants[0].getHelixDataAccessor().getBaseDataAccessor();
for (int i = 0; i < 10; i++) {
String path = String.format("/%s/HELIX_PROPERTYSTORE/%d", clusterName, i);
ZNRecord record = new ZNRecord("" + i);
record.setSimpleField("key1", "value1");
accessor.set(path, record, AccessOption.PERSISTENT);
}
ZkHelixPropertyStore<ZNRecord> store = participants[0].getHelixPropertyStore();
// read shall use fallback paths
for (int i = 0; i < 10; i++) {
String path = String.format("/%d", i);
ZNRecord record = store.get(path, null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), "" + i);
Assert.assertNotNull(record.getSimpleField("key1"));
Assert.assertEquals(record.getSimpleField("key1"), "value1");
}
// update shall update new paths
for (int i = 0; i < 10; i++) {
String path = String.format("/%d", i);
store.update(path, new DataUpdater<ZNRecord>() {
@Override
public ZNRecord update(ZNRecord currentData) {
if (currentData != null) {
currentData.setSimpleField("key2", "value2");
}
return currentData;
}
}, AccessOption.PERSISTENT);
}
for (int i = 0; i < 10; i++) {
String path = String.format("/%d", i);
ZNRecord record = store.get(path, null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), "" + i);
Assert.assertNotNull(record.getSimpleField("key1"));
Assert.assertEquals(record.getSimpleField("key1"), "value1");
Assert.assertNotNull(record.getSimpleField("key2"));
Assert.assertEquals(record.getSimpleField("key2"), "value2");
}
// set shall use new path
for (int i = 10; i < 20; i++) {
String path = String.format("/%d", i);
ZNRecord record = new ZNRecord("" + i);
record.setSimpleField("key3", "value3");
store.set(path, record, AccessOption.PERSISTENT);
}
for (int i = 10; i < 20; i++) {
String path = String.format("/%d", i);
ZNRecord record = store.get(path, null, 0);
Assert.assertNotNull(record);
Assert.assertEquals(record.getId(), "" + i);
Assert.assertNotNull(record.getSimpleField("key3"));
Assert.assertEquals(record.getSimpleField("key3"), "value3");
}
participants[0].syncStop();
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
use of org.apache.helix.ZNRecord in project helix by apache.
the class TestDistControllerElection method testParticipant.
@Test()
public void testParticipant() throws Exception {
String className = getShortClassName();
LOG.info("RUN " + className + " at " + new Date(System.currentTimeMillis()));
final String clusterName = CLUSTER_PREFIX + "_" + className + "_" + "testParticipant";
String path = "/" + clusterName;
if (_gZkClient.exists(path)) {
_gZkClient.deleteRecursively(path);
}
TestHelper.setupEmptyCluster(_gZkClient, clusterName);
final String controllerName = "participant_0";
HelixManager manager = new MockZKHelixManager(clusterName, controllerName, InstanceType.PARTICIPANT, _gZkClient);
GenericHelixController participant0 = new GenericHelixController();
List<HelixTimerTask> timerTasks = Collections.emptyList();
DistributedLeaderElection election = new DistributedLeaderElection(manager, participant0, timerTasks);
NotificationContext context = new NotificationContext(manager);
context.setType(NotificationContext.Type.INIT);
election.onControllerChange(context);
path = PropertyPathBuilder.controllerLeader(clusterName);
ZNRecord leaderRecord = _gZkClient.<ZNRecord>readData(path, true);
AssertJUnit.assertNull(leaderRecord);
// AssertJUnit.assertNull(election.getController());
// AssertJUnit.assertNull(election.getLeader());
}
Aggregations