Search in sources :

Example 6 with ZNRecord

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;
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) InstanceConfig(org.apache.helix.model.InstanceConfig) CurrentState(org.apache.helix.model.CurrentState) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 7 with ZNRecord

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;
}
Also used : ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 8 with ZNRecord

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);
    }
}
Also used : ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 9 with ZNRecord

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();
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) HelixPropertyStoreConfig(com.github.ambry.config.HelixPropertyStoreConfig) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) Test(org.junit.Test)

Example 10 with ZNRecord

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);
}
Also used : ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) Test(org.junit.Test)

Aggregations

ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)37 HashMap (java.util.HashMap)19 Test (org.junit.Test)18 Map (java.util.Map)10 ArrayList (java.util.ArrayList)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 VerifiableProperties (com.github.ambry.config.VerifiableProperties)6 HashSet (java.util.HashSet)6 InstanceConfig (org.apache.helix.model.InstanceConfig)6 MetricRegistry (com.codahale.metrics.MetricRegistry)5 Properties (java.util.Properties)5 Stat (org.apache.zookeeper.data.Stat)5 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)4 HelixPropertyStoreConfig (com.github.ambry.config.HelixPropertyStoreConfig)4 IOException (java.io.IOException)4 JSONObject (org.json.JSONObject)4 List (java.util.List)3 Random (java.util.Random)3 PropertyKey (org.apache.helix.PropertyKey)3 IdealState (org.apache.helix.model.IdealState)3