use of org.apache.helix.zookeeper.datamodel.ZNRecord in project ambry by linkedin.
the class MockHelixDataAccessor method getProperty.
@Override
public <T extends HelixProperty> List<T> getProperty(List<PropertyKey> keys, boolean throwException) {
List<T> result = new ArrayList<>();
for (PropertyKey key : keys) {
if (key.toString().matches("/Ambry-/INSTANCES/.*/CURRENTSTATES/\\d+/\\d+")) {
// an example for the key: /Ambry-/INSTANCES/localhost_18089/CURRENTSTATES/sessionId/0
String[] segments = key.toString().split("/");
String instanceName = segments[3];
String resourceName = segments[6];
Map<String, Map<String, String>> partitionStateMap = mockHelixAdmin.getPartitionStateMapForInstance(instanceName);
ZNRecord record = new ZNRecord(resourceName);
record.setMapFields(partitionStateMap);
result.add((T) (new CurrentState(record)));
} else if (key.toString().matches("/Ambry-/LIVEINSTANCES/.*_\\d+")) {
String[] segments = key.toString().split("/");
String instanceName = segments[3];
ZNRecord record = new ZNRecord(instanceName);
record.setEphemeralOwner(SESSION_ID);
result.add((T) (new LiveInstance(record)));
} else if (key.toString().matches("/Ambry-/CONFIGS/PARTICIPANT/.*_\\d+")) {
String[] segments = key.toString().split("/");
String instanceName = segments[4];
InstanceConfig instanceConfig = mockHelixAdmin.getInstanceConfigs(clusterName).stream().filter(config -> config.getInstanceName().equals(instanceName)).findFirst().get();
result.add((T) instanceConfig);
} else {
result.add((T) properties.get(key));
}
}
return result;
}
use of org.apache.helix.zookeeper.datamodel.ZNRecord in project ambry by linkedin.
the class HelixNotifier method publish.
/**
* {@inheritDoc}
*
* Returns {@code true} does not guarantee all the {@link TopicListener} will receive the message. It just indicates
* the message has been successfully sent out.
*/
@Override
public boolean publish(String topic, String message) {
if (topic == null) {
throw new IllegalArgumentException("topic cannot be null");
}
if (message == null) {
throw new IllegalArgumentException("message cannot be null");
}
String topicPath = getTopicPath(topic);
ZNRecord record = new ZNRecord(topicPath);
record.setSimpleField(MESSAGE_KEY, message);
boolean res = helixStore.set(topicPath, record, AccessOption.PERSISTENT);
if (res) {
logger.trace("message={} has been published for topic={}", message, topic);
} else {
logger.error("failed to publish message={} for topic={}", message, topic);
}
return res;
}
use of org.apache.helix.zookeeper.datamodel.ZNRecord in project ambry by linkedin.
the class HelixNotifier method sendMessageToLocalTopicListener.
/**
* Sends a message to local {@link TopicListener} that subscribed topics through this {@code HelixNotifier}.
* @param topicListener The {@link TopicListener} to send the message.
* @param topic The topic which the message belongs to.
* @param path The path to the topic.
*/
private void sendMessageToLocalTopicListener(TopicListener<String> topicListener, String topic, String path) {
String message = null;
try {
ZNRecord zNRecord = helixStore.get(path, null, AccessOption.PERSISTENT);
if (zNRecord != null) {
message = zNRecord.getSimpleField(MESSAGE_KEY);
long startTimeMs = System.currentTimeMillis();
topicListener.onMessage(topic, message);
logger.trace("Message has been sent to TopicListener={} on topic={} with message={} using {}ms", topicListener, topic, message, System.currentTimeMillis() - startTimeMs);
} else {
logger.debug("TopicListener={} on topic={} should receive a message, but the corresponding ZNRecord is null", topicListener, topic);
}
} catch (Exception e) {
logger.error("Failed to send message to TopicListener={} for topic={} with message={}", topicListener, topic, message, e);
}
}
use of org.apache.helix.zookeeper.datamodel.ZNRecord in project ambry by linkedin.
the class CommonUtilsTest method createHelixPropertyStoreTest.
/**
* Tests for {@link CommonUtils#createHelixPropertyStore(String, HelixPropertyStoreConfig, List)}.
*/
@Test
public void createHelixPropertyStoreTest() throws IOException {
Properties storeProps = new Properties();
storeProps.setProperty("helix.property.store.root.path", "/Ambry-Test/" + ClusterMapUtils.PROPERTYSTORE_STR);
HelixPropertyStoreConfig propertyStoreConfig = new HelixPropertyStoreConfig(new VerifiableProperties(storeProps));
String tempDirPath = getTempDir("clusterMapUtils-");
ZkInfo zkInfo = new ZkInfo(tempDirPath, "DC1", (byte) 0, 2200, true);
try {
CommonUtils.createHelixPropertyStore(null, propertyStoreConfig, Collections.emptyList());
fail("create HelixPropertyStore with invalid arguments should fail");
} catch (IllegalArgumentException e) {
// expected
}
try {
CommonUtils.createHelixPropertyStore("", propertyStoreConfig, Collections.emptyList());
fail("create HelixPropertyStore with invalid arguments should fail");
} catch (IllegalArgumentException e) {
// expected
}
try {
CommonUtils.createHelixPropertyStore("localhost:" + zkInfo.getPort(), (HelixPropertyStoreConfig) null, Collections.emptyList());
fail("create HelixPropertyStore with invalid arguments should fail");
} catch (IllegalArgumentException e) {
// expected
}
HelixPropertyStore<ZNRecord> propertyStore = CommonUtils.createHelixPropertyStore("localhost:" + zkInfo.getPort(), propertyStoreConfig, Collections.singletonList(propertyStoreConfig.rootPath));
assertNotNull(propertyStore);
// Ensure the HelixPropertyStore works correctly
List<String> list = Arrays.asList("first", "second", "third");
String path = propertyStoreConfig.rootPath + ClusterMapUtils.PARTITION_OVERRIDE_ZNODE_PATH;
ZNRecord znRecord = new ZNRecord(ClusterMapUtils.PARTITION_OVERRIDE_STR);
znRecord.setListField("AmbryList", list);
if (!propertyStore.set(path, znRecord, AccessOption.PERSISTENT)) {
fail("Failed to set HelixPropertyStore");
}
// Verify path exists
assertTrue("The record path doesn't exist", propertyStore.exists(path, AccessOption.PERSISTENT));
// Verify record
ZNRecord result = propertyStore.get(path, null, AccessOption.PERSISTENT);
assertEquals("Mismatch in list content", new HashSet<>(list), new HashSet<>(result.getListField("AmbryList")));
zkInfo.shutdown();
}
use of org.apache.helix.zookeeper.datamodel.ZNRecord in project ambry by linkedin.
the class HelixAccountServiceTest method testReadBadZNRecordCase2.
/**
* Tests reading {@link ZNRecord} from {@link HelixPropertyStore}, where the {@link ZNRecord} has an irrelevant
* simple field ("key": "value"), but ("accountMetadata": someValidMap) is missing. This is a good {@link ZNRecord}
* format that should NOT fail fetch or update.
* @throws Exception Any unexpected exception.
*/
@Test
public void testReadBadZNRecordCase2() throws Exception {
ZNRecord zNRecord = makeZNRecordWithSimpleField(null, "key", "value");
updateAndWriteZNRecord(zNRecord, true);
}
Aggregations