Search in sources :

Example 1 with ZkClientException

use of org.apache.helix.zookeeper.exception.ZkClientException in project helix by apache.

the class ParticipantManager method carryOverPreviousCurrentState.

/**
 * carry over current-states from last sessions
 * set to initial state for current session only when state doesn't exist in current session
 */
public static synchronized void carryOverPreviousCurrentState(HelixDataAccessor dataAccessor, String instanceName, String sessionId, StateMachineEngine stateMachineEngine, boolean setToInitState) {
    PropertyKey.Builder keyBuilder = dataAccessor.keyBuilder();
    List<String> sessions = dataAccessor.getChildNames(keyBuilder.sessions(instanceName));
    for (String session : sessions) {
        if (session.equals(sessionId)) {
            continue;
        }
        // Ignore if any current states in the previous folder cannot be read.
        List<CurrentState> lastCurStates = dataAccessor.getChildValues(keyBuilder.currentStates(instanceName, session), false);
        for (CurrentState lastCurState : lastCurStates) {
            LOG.info("Carrying over old session: " + session + ", resource: " + lastCurState.getId() + " to current session: " + sessionId + ", setToInitState: " + setToInitState);
            String stateModelDefRef = lastCurState.getStateModelDefRef();
            if (stateModelDefRef == null) {
                LOG.error("skip carry-over because previous current state doesn't have a state model definition. previous current-state: " + lastCurState);
                continue;
            }
            // Note: this check is not necessary due to TaskCurrentStates, but keep it for backwards compatibility
            if (stateModelDefRef.equals(TaskConstants.STATE_MODEL_NAME)) {
                continue;
            }
            StateModelDefinition stateModelDef = dataAccessor.getProperty(keyBuilder.stateModelDef(stateModelDefRef));
            String initState = stateModelDef.getInitialState();
            Map<String, String> partitionExpectedStateMap = new HashMap<>();
            if (setToInitState) {
                lastCurState.getPartitionStateMap().keySet().forEach(partition -> partitionExpectedStateMap.put(partition, initState));
            } else {
                String factoryName = lastCurState.getStateModelFactoryName();
                StateModelFactory<? extends StateModel> stateModelFactory = stateMachineEngine.getStateModelFactory(stateModelDefRef, factoryName);
                lastCurState.getPartitionStateMap().keySet().forEach(partition -> {
                    StateModel stateModel = stateModelFactory.getStateModel(lastCurState.getResourceName(), partition);
                    if (stateModel != null) {
                        partitionExpectedStateMap.put(partition, stateModel.getCurrentState());
                    }
                });
            }
            BaseDataAccessor<ZNRecord> baseAccessor = dataAccessor.getBaseDataAccessor();
            String curStatePath = keyBuilder.currentState(instanceName, sessionId, lastCurState.getResourceName()).getPath();
            if (lastCurState.getBucketSize() > 0) {
                // update parent node
                ZNRecord metaRecord = new ZNRecord(lastCurState.getId());
                metaRecord.setSimpleFields(lastCurState.getRecord().getSimpleFields());
                DataUpdater<ZNRecord> metaRecordUpdater = new CurStateCarryOverUpdater(sessionId, partitionExpectedStateMap, new CurrentState(metaRecord));
                boolean success = baseAccessor.update(curStatePath, metaRecordUpdater, AccessOption.PERSISTENT);
                if (success) {
                    // update current state buckets
                    ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(lastCurState.getBucketSize());
                    Map<String, ZNRecord> map = bucketizer.bucketize(lastCurState.getRecord());
                    List<String> paths = new ArrayList<String>();
                    List<DataUpdater<ZNRecord>> updaters = new ArrayList<DataUpdater<ZNRecord>>();
                    for (String bucketName : map.keySet()) {
                        paths.add(curStatePath + "/" + bucketName);
                        updaters.add(new CurStateCarryOverUpdater(sessionId, partitionExpectedStateMap, new CurrentState(map.get(bucketName))));
                    }
                    baseAccessor.updateChildren(paths, updaters, AccessOption.PERSISTENT);
                }
            } else {
                dataAccessor.getBaseDataAccessor().update(curStatePath, new CurStateCarryOverUpdater(sessionId, partitionExpectedStateMap, lastCurState), AccessOption.PERSISTENT);
            }
        }
    }
    /**
     * remove previous current state parent nodes
     */
    for (String session : sessions) {
        if (session.equals(sessionId)) {
            continue;
        }
        PropertyKey currentStatesProperty = keyBuilder.currentStates(instanceName, session);
        String path = currentStatesProperty.getPath();
        LOG.info("Removing current states from previous sessions. path: {}", path);
        if (!dataAccessor.removeProperty(currentStatesProperty)) {
            throw new ZkClientException("Failed to delete " + path);
        }
    }
}
Also used : ZNRecordBucketizer(org.apache.helix.zookeeper.datamodel.ZNRecordBucketizer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StateModelDefinition(org.apache.helix.model.StateModelDefinition) CurrentState(org.apache.helix.model.CurrentState) DataUpdater(org.apache.helix.zookeeper.zkclient.DataUpdater) StateModel(org.apache.helix.participant.statemachine.StateModel) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 2 with ZkClientException

use of org.apache.helix.zookeeper.exception.ZkClientException in project helix by apache.

the class JacksonPayloadSerializer method serialize.

@Override
public <T> byte[] serialize(final T data) {
    if (data == null) {
        return null;
    }
    ObjectMapper mapper = new ObjectMapper();
    SerializationConfig serializationConfig = mapper.getSerializationConfig();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.enable(MapperFeature.AUTO_DETECT_FIELDS);
    mapper.enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
    StringWriter sw = new StringWriter();
    try {
        mapper.writeValue(sw, data);
    } catch (Exception e) {
        logger.error("Exception during payload data serialization.", e);
        throw new ZkClientException(e);
    }
    return sw.toString().getBytes();
}
Also used : StringWriter(java.io.StringWriter) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException)

Example 3 with ZkClientException

use of org.apache.helix.zookeeper.exception.ZkClientException in project helix by apache.

the class ZkClient method getSessionId.

public long getSessionId() {
    ZkConnection zkConnection = ((ZkConnection) getConnection());
    ZooKeeper zk = zkConnection.getZookeeper();
    if (zk == null) {
        throw new ZkClientException("ZooKeeper connection information is not available now. ZkClient might be disconnected.");
    } else {
        return zkConnection.getZookeeper().getSessionId();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException)

Example 4 with ZkClientException

use of org.apache.helix.zookeeper.exception.ZkClientException in project helix by apache.

the class TestZkBaseDataAccessor method testSyncRemove.

@Test
public void testSyncRemove() {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String testName = className + "_" + methodName;
    System.out.println("START " + testName + " at " + new Date(System.currentTimeMillis()));
    String path = String.format("/%s/%s", _rootPath, "msg_0");
    ZNRecord record = new ZNRecord("msg_0");
    ZkBaseDataAccessor<ZNRecord> accessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    // Base data accessor shall not fail when remove a non-exist path
    boolean success = accessor.remove(path, 0);
    Assert.assertTrue(success);
    success = accessor.create(path, record, AccessOption.PERSISTENT);
    Assert.assertTrue(success);
    ZNRecord getRecord = _gZkClient.readData(path);
    Assert.assertNotNull(getRecord);
    Assert.assertEquals(getRecord.getId(), "msg_0");
    // Tests that ZkClientException thrown from ZkClient should be caught
    // and remove() should return false.
    RealmAwareZkClient mockZkClient = Mockito.mock(RealmAwareZkClient.class);
    Mockito.doThrow(new ZkException("Failed to delete " + path)).when(mockZkClient).delete(path);
    Mockito.doThrow(new ZkClientException("Failed to recursively delete " + path)).when(mockZkClient).deleteRecursively(path);
    ZkBaseDataAccessor<ZNRecord> accessorMock = new ZkBaseDataAccessor<>(mockZkClient);
    try {
        Assert.assertFalse(accessorMock.remove(path, AccessOption.PERSISTENT), "Should return false because ZkClientException is thrown");
    } catch (ZkClientException e) {
        Assert.fail("Should not throw ZkClientException because it should be caught.");
    }
    success = accessor.remove(path, 0);
    Assert.assertTrue(success);
    Assert.assertFalse(_gZkClient.exists(path));
    System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZkException(org.apache.helix.zookeeper.zkclient.exception.ZkException) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) Date(java.util.Date) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Test(org.testng.annotations.Test)

Example 5 with ZkClientException

use of org.apache.helix.zookeeper.exception.ZkClientException in project helix by apache.

the class TestHelixZkClient method testZkConnectionManager.

@Test
public void testZkConnectionManager() {
    final String TEST_ROOT = "/testZkConnectionManager/IDEALSTATES";
    final String TEST_PATH = TEST_ROOT + TEST_NODE;
    ZkConnectionManager zkConnectionManager = new ZkConnectionManager(new ZkConnection(ZK_ADDR), HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, null);
    Assert.assertTrue(zkConnectionManager.waitUntilConnected(1, TimeUnit.SECONDS));
    // This client can write/read from ZK
    zkConnectionManager.createPersistent(TEST_PATH, true);
    zkConnectionManager.writeData(TEST_PATH, "Test");
    Assert.assertNotNull(zkConnectionManager.readData(TEST_PATH));
    zkConnectionManager.deleteRecursively(TEST_ROOT);
    // This client can be shared, and cannot close when sharing
    SharedZkClient sharedZkClient = new SharedZkClient(zkConnectionManager, new HelixZkClient.ZkClientConfig(), null);
    try {
        zkConnectionManager.close();
        Assert.fail("Dedicated ZkClient cannot be closed while sharing!");
    } catch (ZkClientException hex) {
    // expected
    }
    // This client can be closed normally when sharing ends
    sharedZkClient.close();
    Assert.assertTrue(sharedZkClient.isClosed());
    Assert.assertFalse(sharedZkClient.waitUntilConnected(100, TimeUnit.MILLISECONDS));
    zkConnectionManager.close();
    Assert.assertTrue(zkConnectionManager.isClosed());
    Assert.assertFalse(zkConnectionManager.waitUntilConnected(100, TimeUnit.MILLISECONDS));
    // Sharing a closed dedicated ZkClient shall fail
    try {
        new SharedZkClient(zkConnectionManager, new HelixZkClient.ZkClientConfig(), null);
        Assert.fail("Sharing a closed dedicated ZkClient shall fail.");
    } catch (ZkClientException hex) {
    // expected
    }
    deleteCluster("testZkConnectionManager");
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) ZkConnection(org.apache.helix.zookeeper.zkclient.ZkConnection) Test(org.testng.annotations.Test)

Aggregations

ZkClientException (org.apache.helix.zookeeper.exception.ZkClientException)6 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)3 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 StateModelDefinition (org.apache.helix.model.StateModelDefinition)2 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)2 ZkException (org.apache.helix.zookeeper.zkclient.exception.ZkException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HelixAdmin (org.apache.helix.HelixAdmin)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1 PropertyKey (org.apache.helix.PropertyKey)1 HelixConflictException (org.apache.helix.api.exceptions.HelixConflictException)1 CurrentState (org.apache.helix.model.CurrentState)1 CustomizedView (org.apache.helix.model.CustomizedView)1