Search in sources :

Example 6 with ZNRecordSerializer

use of org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer in project helix by apache.

the class TestRawZkClient method testCreatePersistentSequentialWithTTL.

@Test
void testCreatePersistentSequentialWithTTL() {
    // Enable extended types and create a ZkClient
    System.setProperty("zookeeper.extendedTypesEnabled", "true");
    ZkClient zkClient = new ZkClient(ZkTestBase.ZK_ADDR);
    zkClient.setZkSerializer(new ZNRecordSerializer());
    // Make sure the test path is clear
    String parentPath = "/tmp";
    String path = "/tmp/createSequentialTTL";
    zkClient.deleteRecursively(parentPath);
    AssertJUnit.assertFalse(zkClient.exists(parentPath));
    AssertJUnit.assertFalse(zkClient.exists(path + "0000000000"));
    long ttl = 1L;
    ZNRecord record = new ZNRecord("record");
    String key = "key";
    String value = "value";
    record.setSimpleField(key, value);
    // Create a ZNode with the above ZNRecord and read back its data
    zkClient.createPersistent(parentPath);
    zkClient.createPersistentSequentialWithTTL(path, record, ttl);
    AssertJUnit.assertTrue(zkClient.exists(path + "0000000000"));
    ZNRecord retrievedRecord = zkClient.readData(path + "0000000000");
    AssertJUnit.assertEquals(value, retrievedRecord.getSimpleField(key));
    // Clean up
    zkClient.deleteRecursively(parentPath);
    zkClient.close();
    System.clearProperty("zookeeper.extendedTypesEnabled");
}
Also used : SessionAwareZNRecord(org.apache.helix.zookeeper.datamodel.SessionAwareZNRecord) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer) Test(org.testng.annotations.Test)

Example 7 with ZNRecordSerializer

use of org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer in project helix by apache.

the class TestRawZkClient method testAsyncWriteOperations.

@Test
public void testAsyncWriteOperations() {
    ZkClient zkClient = new ZkClient(ZkTestBase.ZK_ADDR);
    String originSizeLimit = System.getProperty(ZkSystemPropertyKeys.ZK_SERIALIZER_ZNRECORD_WRITE_SIZE_LIMIT_BYTES);
    System.setProperty(ZkSystemPropertyKeys.ZK_SERIALIZER_ZNRECORD_WRITE_SIZE_LIMIT_BYTES, "2000");
    try {
        zkClient.setZkSerializer(new ZNRecordSerializer());
        ZNRecord oversizeZNRecord = new ZNRecord("Oversize");
        char[] buff = new char[1024];
        Random ran = new Random();
        for (int i = 0; i < 1024; i++) {
            buff[i] = (char) (ran.nextInt(26) + 'a');
        }
        String buffString = new String(buff);
        for (int i = 0; i < 1024; i++) {
            oversizeZNRecord.setSimpleField(Integer.toString(i), buffString);
        }
        // ensure /tmp exists for the test
        if (!zkClient.exists("/tmp")) {
            zkClient.create("/tmp", null, CreateMode.PERSISTENT);
        }
        org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.CreateCallbackHandler createCallback = new org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.CreateCallbackHandler();
        zkClient.asyncCreate("/tmp/async", null, CreateMode.PERSISTENT, createCallback);
        createCallback.waitForSuccess();
        Assert.assertEquals(createCallback.getRc(), 0);
        Assert.assertTrue(zkClient.exists("/tmp/async"));
        Assert.assertFalse(zkClient.exists("/tmp/asyncOversize"));
        // try to create oversize node, should fail
        zkClient.asyncCreate("/tmp/asyncOversize", oversizeZNRecord, CreateMode.PERSISTENT, createCallback);
        createCallback.waitForSuccess();
        Assert.assertEquals(createCallback.getRc(), KeeperException.Code.MARSHALLINGERROR.intValue());
        Assert.assertFalse(zkClient.exists("/tmp/asyncOversize"));
        ZNRecord normalZNRecord = new ZNRecord("normal");
        normalZNRecord.setSimpleField("key", buffString);
        org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.SetDataCallbackHandler setDataCallbackHandler = new org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks.SetDataCallbackHandler();
        zkClient.asyncSetData("/tmp/async", normalZNRecord, -1, setDataCallbackHandler);
        setDataCallbackHandler.waitForSuccess();
        Assert.assertEquals(setDataCallbackHandler.getRc(), 0);
        zkClient.asyncSetData("/tmp/async", oversizeZNRecord, -1, setDataCallbackHandler);
        setDataCallbackHandler.waitForSuccess();
        Assert.assertEquals(setDataCallbackHandler.getRc(), KeeperException.Code.MARSHALLINGERROR.intValue());
        Assert.assertEquals(zkClient.readData("/tmp/async"), normalZNRecord);
    } finally {
        TestHelper.resetSystemProperty(ZkSystemPropertyKeys.ZK_SERIALIZER_ZNRECORD_WRITE_SIZE_LIMIT_BYTES, originSizeLimit);
        zkClient.delete("/tmp/async");
        zkClient.delete("/tmp/asyncOversize");
        zkClient.close();
    }
}
Also used : ZkAsyncCallbacks(org.apache.helix.zookeeper.zkclient.callback.ZkAsyncCallbacks) Random(java.util.Random) SessionAwareZNRecord(org.apache.helix.zookeeper.datamodel.SessionAwareZNRecord) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer) Test(org.testng.annotations.Test)

Example 8 with ZNRecordSerializer

use of org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer in project helix by apache.

the class ClusterSetup method getConfig.

/**
 * get configs
 * @param type config-scope-type, e.g. CLUSTER, RESOURCE, etc.
 * @param scopeArgsCsv csv-formatted scope-args, e.g myCluster,testDB
 * @param keysCsv csv-formatted keys. e.g. k1,k2
 * @return json-formated key-value pairs, e.g. {k1=v1,k2=v2}
 */
public String getConfig(ConfigScopeProperty type, String scopeArgsCsv, String keysCsv) {
    // ConfigScope scope = new ConfigScopeBuilder().build(scopesStr);
    String[] scopeArgs = scopeArgsCsv.split("[\\s,]");
    HelixConfigScope scope = new HelixConfigScopeBuilder(type, scopeArgs).build();
    String[] keys = keysCsv.split("[\\s,]");
    // parse keys
    // String[] keys = keysStr.split("[\\s,]");
    // Set<String> keysSet = new HashSet<String>(Arrays.asList(keys));
    Map<String, String> keyValueMap = _admin.getConfig(scope, Arrays.asList(keys));
    ZNRecord record = new ZNRecord(type.toString());
    // record.setMapField(scopesStr, propertiesMap);
    record.getSimpleFields().putAll(keyValueMap);
    ZNRecordSerializer serializer = new ZNRecordSerializer();
    return new String(serializer.serialize(record));
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)

Example 9 with ZNRecordSerializer

use of org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer in project helix by apache.

the class HelixCustomCodeRunner method start.

/**
 * This method will be invoked when there is a change in any subscribed
 * notificationTypes
 * @throws Exception
 */
public void start() throws Exception {
    if (_callback == null || _notificationTypes == null || _notificationTypes.size() == 0 || _resourceName == null) {
        throw new IllegalArgumentException("Require callback | notificationTypes | resourceName");
    }
    LOG.info("Register participantLeader on " + _notificationTypes + " using " + _resourceName);
    _stateModelFty = new GenericLeaderStandbyStateModelFactory(_callback, _notificationTypes);
    StateMachineEngine stateMach = _manager.getStateMachineEngine();
    stateMach.registerStateModelFactory(LEADER_STANDBY, _stateModelFty, _resourceName);
    RealmAwareZkClient zkClient = null;
    try {
        // model
        if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || _zkAddr == null) {
            // Use multi-zk mode (FederatedZkClient)
            RealmAwareZkClient.RealmAwareZkClientConfig clientConfig = new RealmAwareZkClient.RealmAwareZkClientConfig();
            clientConfig.setZkSerializer(new ZNRecordSerializer());
            zkClient = new FederatedZkClient(_connectionConfig, clientConfig);
        } else {
            // Use single-zk mode using the ZkAddr given
            HelixZkClient.ZkClientConfig clientConfig = new HelixZkClient.ZkClientConfig();
            clientConfig.setZkSerializer(new ZNRecordSerializer());
            zkClient = SharedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(_zkAddr), clientConfig);
        }
        HelixDataAccessor accessor = new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<>(zkClient));
        Builder keyBuilder = accessor.keyBuilder();
        IdealState idealState = new IdealState(_resourceName);
        idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
        idealState.setReplicas(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString());
        idealState.setNumPartitions(1);
        idealState.setStateModelDefRef(LEADER_STANDBY);
        idealState.setStateModelFactoryName(_resourceName);
        List<String> prefList = new ArrayList<String>(Arrays.asList(IdealState.IdealStateConstants.ANY_LIVEINSTANCE.toString()));
        idealState.getRecord().setListField(_resourceName + "_0", prefList);
        List<String> idealStates = accessor.getChildNames(keyBuilder.idealStates());
        while (idealStates == null || !idealStates.contains(_resourceName)) {
            accessor.setProperty(keyBuilder.idealStates(_resourceName), idealState);
            idealStates = accessor.getChildNames(keyBuilder.idealStates());
        }
        LOG.info("Set idealState for participantLeader:" + _resourceName + ", idealState:" + idealState);
    } finally {
        if (zkClient != null && !zkClient.isClosed()) {
            zkClient.close();
        }
    }
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) Builder(org.apache.helix.PropertyKey.Builder) ArrayList(java.util.ArrayList) IdealState(org.apache.helix.model.IdealState) FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) HelixDataAccessor(org.apache.helix.HelixDataAccessor) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 10 with ZNRecordSerializer

use of org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer in project helix by apache.

the class HelixPropertyFactory method getCloudConfig.

public static CloudConfig getCloudConfig(String zkAddress, String clusterName, RealmAwareZkClient.RealmAwareZkConnectionConfig realmAwareZkConnectionConfig) {
    CloudConfig cloudConfig;
    RealmAwareZkClient dedicatedZkClient = null;
    try {
        if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || zkAddress == null) {
            // DedicatedZkClient
            try {
                if (realmAwareZkConnectionConfig == null) {
                    realmAwareZkConnectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRealmMode(RealmAwareZkClient.RealmMode.SINGLE_REALM).setZkRealmShardingKey("/" + clusterName).build();
                }
                dedicatedZkClient = DedicatedZkClientFactory.getInstance().buildZkClient(realmAwareZkConnectionConfig);
            } catch (IOException | InvalidRoutingDataException e) {
                throw new HelixException("Not able to connect on multi-ZK mode!", e);
            }
        } else {
            // Use a dedicated ZK client single-ZK mode
            HelixZkClient.ZkConnectionConfig connectionConfig = new HelixZkClient.ZkConnectionConfig(zkAddress);
            dedicatedZkClient = DedicatedZkClientFactory.getInstance().buildZkClient(connectionConfig);
        }
        dedicatedZkClient.setZkSerializer(new ZNRecordSerializer());
        ConfigAccessor configAccessor = new ConfigAccessor(dedicatedZkClient);
        // up yet, constructing a new ZKHelixManager should not throw an exception
        try {
            cloudConfig = configAccessor.getCloudConfig(clusterName);
            if (cloudConfig == null) {
                cloudConfig = new CloudConfig();
            }
        } catch (HelixException e) {
            cloudConfig = new CloudConfig();
        }
    } finally {
        // Use a try-finally to make sure zkclient connection is closed properly
        if (dedicatedZkClient != null && !dedicatedZkClient.isClosed()) {
            dedicatedZkClient.close();
        }
    }
    return cloudConfig;
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) CloudConfig(org.apache.helix.model.CloudConfig) IOException(java.io.IOException) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)

Aggregations

ZNRecordSerializer (org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)30 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)16 Test (org.testng.annotations.Test)11 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)9 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)7 BeforeClass (org.testng.annotations.BeforeClass)6 SessionAwareZNRecord (org.apache.helix.zookeeper.datamodel.SessionAwareZNRecord)5 InvalidRoutingDataException (org.apache.helix.msdcommon.exception.InvalidRoutingDataException)4 FederatedZkClient (org.apache.helix.zookeeper.impl.client.FederatedZkClient)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IdealState (org.apache.helix.model.IdealState)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 NoSuchElementException (java.util.NoSuchElementException)2 HelixException (org.apache.helix.HelixException)2 CloudConfig (org.apache.helix.model.CloudConfig)2 MockMetadataStoreDirectoryServer (org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer)2 ZkRoutingDataReader (org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataReader)2 ZkClient (org.apache.helix.zookeeper.impl.client.ZkClient)2