Search in sources :

Example 1 with ZkNodeExistsException

use of org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException in project helix by apache.

the class ParticipantManager method createLiveInstance.

private void createLiveInstance() {
    String liveInstancePath = _keyBuilder.liveInstance(_instanceName).getPath();
    LiveInstance liveInstance = new LiveInstance(_instanceName);
    liveInstance.setSessionId(_sessionId);
    liveInstance.setHelixVersion(_manager.getVersion());
    liveInstance.setLiveInstance(ManagementFactory.getRuntimeMXBean().getName());
    liveInstance.setCurrentTaskThreadPoolSize(TaskUtil.getTargetThreadPoolSize(_zkclient, _clusterName, _instanceName));
    // LiveInstanceInfoProvider liveInstanceInfoProvider = _manager._liveInstanceInfoProvider;
    if (_liveInstanceInfoProvider != null) {
        LOG.info("invoke liveInstanceInfoProvider");
        ZNRecord additionalLiveInstanceInfo = _liveInstanceInfoProvider.getAdditionalLiveInstanceInfo();
        if (additionalLiveInstanceInfo != null) {
            additionalLiveInstanceInfo.merge(liveInstance.getRecord());
            ZNRecord mergedLiveInstance = new ZNRecord(additionalLiveInstanceInfo, _instanceName);
            liveInstance = new LiveInstance(mergedLiveInstance);
            LOG.info("instanceName: " + _instanceName + ", mergedLiveInstance: " + liveInstance);
        }
    }
    boolean retry;
    do {
        retry = false;
        try {
            // Zk session ID will be validated in createEphemeral.
            _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord(), _sessionId);
            LOG.info("LiveInstance created, path: {}, sessionId: {}", liveInstancePath, liveInstance.getEphemeralOwner());
        } catch (ZkSessionMismatchedException e) {
            throw new HelixException("Failed to create live instance, path: " + liveInstancePath + ". Caused by: " + e.getMessage());
        } catch (ZkNodeExistsException e) {
            LOG.warn("Found another instance with same instance name: {} in cluster: {}", _instanceName, _clusterName);
            Stat stat = new Stat();
            ZNRecord record = _zkclient.readData(liveInstancePath, stat, true);
            if (record == null) {
                /**
                 * live-instance is gone as we check it, retry create live-instance
                 */
                retry = true;
            } else {
                /**
                 * wait for a while, in case previous helix-participant exits unexpectedly
                 * and its live-instance still hangs around until session timeout
                 */
                try {
                    TimeUnit.MILLISECONDS.sleep(_sessionTimeout + 5000);
                } catch (InterruptedException ex) {
                    LOG.warn("Sleep interrupted while waiting for previous live-instance to go away.", ex);
                }
                /**
                 * give a last try after exit while loop
                 */
                retry = true;
                break;
            }
        }
    } while (retry);
    /**
     * give a last shot
     */
    if (retry) {
        try {
            // Zk session ID will be validated in createEphemeral.
            _zkclient.createEphemeral(liveInstancePath, liveInstance.getRecord(), _sessionId);
            LOG.info("LiveInstance created, path: {}, sessionId: {}", liveInstancePath, liveInstance.getEphemeralOwner());
        } catch (ZkSessionMismatchedException e) {
            throw new HelixException("Failed to create live instance, path: " + liveInstancePath + ". Caused by: " + e.getMessage());
        } catch (ZkNodeExistsException e) {
            throw new HelixException("Failed to create live instance because instance: " + _instanceName + " already has a live-instance in cluster: " + _clusterName + ". Path is: " + liveInstancePath);
        } catch (Exception e) {
            throw new HelixException("Failed to create live instance. " + e.getMessage());
        }
    }
    ParticipantHistory history = getHistory();
    history.reportOnline(_sessionId, _manager.getVersion());
    persistHistory(history, false);
}
Also used : HelixException(org.apache.helix.HelixException) ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) Stat(org.apache.zookeeper.data.Stat) LiveInstance(org.apache.helix.model.LiveInstance) ZkSessionMismatchedException(org.apache.helix.zookeeper.zkclient.exception.ZkSessionMismatchedException) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) HelixException(org.apache.helix.HelixException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) ZkSessionMismatchedException(org.apache.helix.zookeeper.zkclient.exception.ZkSessionMismatchedException) ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) ParticipantHistory(org.apache.helix.model.ParticipantHistory)

Example 2 with ZkNodeExistsException

use of org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException in project helix by apache.

the class ClusterStateVerifier method verifyByZkCallback.

/**
 * This function should be always single threaded
 *
 * @param verifier
 * @param timeout
 * @return
 */
public static boolean verifyByZkCallback(ZkVerifier verifier, long timeout) {
    long startTime = System.currentTimeMillis();
    CountDownLatch countDown = new CountDownLatch(1);
    HelixZkClient zkClient = verifier.getZkClient();
    String clusterName = verifier.getClusterName();
    // so when analyze zk log, we know when a test ends
    try {
        zkClient.createEphemeral("/" + clusterName + "/CONFIGS/CLUSTER/verify");
    } catch (ZkNodeExistsException ex) {
        LOG.error("There is already a verification in progress", ex);
        throw ex;
    }
    ExtViewVeriferZkListener listener = new ExtViewVeriferZkListener(countDown, zkClient, verifier);
    String extViewPath = PropertyPathBuilder.externalView(clusterName);
    zkClient.subscribeChildChanges(extViewPath, listener);
    for (String child : zkClient.getChildren(extViewPath)) {
        String childPath = extViewPath.equals("/") ? extViewPath + child : extViewPath + "/" + child;
        zkClient.subscribeDataChanges(childPath, listener);
    }
    // do initial verify
    boolean result = verifier.verify();
    if (result == false) {
        try {
            result = countDown.await(timeout, TimeUnit.MILLISECONDS);
            if (result == false) {
                // make a final try if timeout
                result = verifier.verify();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // clean up
    zkClient.unsubscribeChildChanges(extViewPath, listener);
    for (String child : zkClient.getChildren(extViewPath)) {
        String childPath = extViewPath.equals("/") ? extViewPath + child : extViewPath + "/" + child;
        zkClient.unsubscribeDataChanges(childPath, listener);
    }
    long endTime = System.currentTimeMillis();
    zkClient.delete("/" + clusterName + "/CONFIGS/CLUSTER/verify");
    // debug
    System.err.println(result + ": wait " + (endTime - startTime) + "ms, " + verifier);
    return result;
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) CountDownLatch(java.util.concurrent.CountDownLatch) ParseException(org.apache.commons.cli.ParseException) ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException)

Example 3 with ZkNodeExistsException

use of org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException in project helix by apache.

the class TestExecutor method compareAndSetZnode.

private static boolean compareAndSetZnode(ZnodeValue expect, ZnodeOpArg arg, HelixZkClient zkClient, ZNRecord diff) {
    String path = arg._znodePath;
    ZnodePropertyType type = arg._propertyType;
    String key = arg._key;
    boolean success = true;
    // retry 3 times in case there are write conflicts
    // ms
    long backoffTime = 20;
    for (int i = 0; i < 3; i++) {
        try {
            Stat stat = new Stat();
            ZNRecord record = zkClient.<ZNRecord>readDataAndStat(path, stat, true);
            if (isValueExpected(record, type, key, expect, diff)) {
                if (arg._operation.compareTo("+") == 0) {
                    if (record == null) {
                        record = new ZNRecord("default");
                    }
                    ZnodeModValueType valueType = getValueType(arg._propertyType, arg._key);
                    switch(valueType) {
                        case SINGLE_VALUE:
                            setSingleValue(record, arg._propertyType, arg._key, arg._updateValue._singleValue);
                            break;
                        case LIST_VALUE:
                            setListValue(record, arg._key, arg._updateValue._listValue);
                            break;
                        case MAP_VALUE:
                            setMapValue(record, arg._key, arg._updateValue._mapValue);
                            break;
                        case ZNODE_VALUE:
                            // deep copy
                            record = ZNRECORD_SERIALIZER.deserialize(ZNRECORD_SERIALIZER.serialize(arg._updateValue._znodeValue));
                            break;
                        case INVALID:
                            break;
                        default:
                            break;
                    }
                } else if (arg._operation.compareTo("-") == 0) {
                    ZnodeModValueType valueType = getValueType(arg._propertyType, arg._key);
                    switch(valueType) {
                        case SINGLE_VALUE:
                            removeSingleValue(record, arg._propertyType, arg._key);
                            break;
                        case LIST_VALUE:
                            removeListValue(record, arg._key);
                            break;
                        case MAP_VALUE:
                            removeMapValue(record, arg._key);
                            break;
                        case ZNODE_VALUE:
                            record = null;
                            break;
                        case INVALID:
                            break;
                        default:
                            break;
                    }
                } else {
                    logger.warn("fail to execute (unsupport operation): " + arg._operation);
                    success = false;
                }
                if (success == true) {
                    if (record == null) {
                        zkClient.delete(path);
                    } else {
                        try {
                            zkClient.createPersistent(path, true);
                        } catch (ZkNodeExistsException e) {
                        // OK
                        }
                        zkClient.writeData(path, record, stat.getVersion());
                    }
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ZkBadVersionException e) {
        // e.printStackTrace();
        } catch (PropertyStoreException e) {
        // e.printStackTrace();
        }
        try {
            Thread.sleep(backoffTime);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        backoffTime *= 2;
    }
    return false;
}
Also used : ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) Stat(org.apache.zookeeper.data.Stat) PropertyStoreException(org.apache.helix.store.PropertyStoreException) ZkBadVersionException(org.apache.helix.zookeeper.zkclient.exception.ZkBadVersionException) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 4 with ZkNodeExistsException

use of org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException in project helix by apache.

the class ZkMetadataStoreDirectory method createRoutingDataPath.

/**
 * Make sure the root routing data path exists. Also, register the routing ZK address.
 * @param zkClient
 */
public static void createRoutingDataPath(HelixZkClient zkClient, String zkAddress) {
    try {
        zkClient.createPersistent(MetadataStoreRoutingConstants.ROUTING_DATA_PATH, true);
    } catch (ZkNodeExistsException e) {
    // The node already exists and it's okay
    }
    // Make sure ROUTING_DATA_PATH is mapped to the routing ZK so that FederatedZkClient used
    // in Helix REST can subscribe to the routing data path
    ZNRecord znRecord = new ZNRecord(MetadataStoreRoutingConstants.ROUTING_DATA_PATH.substring(1));
    znRecord.setListField(MetadataStoreRoutingConstants.ROUTING_ZK_ADDRESS_KEY, Collections.singletonList(zkAddress));
    zkClient.writeData(MetadataStoreRoutingConstants.ROUTING_DATA_PATH, znRecord);
}
Also used : ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord)

Example 5 with ZkNodeExistsException

use of org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException in project helix by apache.

the class ZkBaseDataAccessor method doCreate.

/**
 * sync create with TTL
 */
public AccessResult doCreate(String path, T record, int options, long ttl) {
    AccessResult result = new AccessResult();
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid create mode. options: " + options);
        result._retCode = RetCode.ERROR;
        return result;
    }
    boolean retry;
    do {
        retry = false;
        try {
            _zkClient.create(path, record, mode, ttl);
            result._pathCreated.add(path);
            result._retCode = RetCode.OK;
            return result;
        } catch (ZkNoNodeException e) {
            // this will happen if parent node does not exist
            String parentPath = HelixUtil.getZkParentPath(path);
            try {
                AccessResult res;
                if (mode.isTTL()) {
                    res = doCreate(parentPath, null, options, ttl);
                } else if (mode.isContainer()) {
                    res = doCreate(parentPath, null, AccessOption.CONTAINER);
                } else {
                    res = doCreate(parentPath, null, AccessOption.PERSISTENT);
                }
                result._pathCreated.addAll(res._pathCreated);
                RetCode rc = res._retCode;
                if (rc == RetCode.OK || rc == RetCode.NODE_EXISTS) {
                    // if parent node created/exists, retry
                    retry = true;
                }
            } catch (Exception e1) {
                LOG.error("Exception while creating path: " + parentPath, e1);
                result._retCode = RetCode.ERROR;
                return result;
            }
        } catch (ZkNodeExistsException e) {
            LOG.warn("Node already exists. path: " + path);
            result._retCode = RetCode.NODE_EXISTS;
            return result;
        } catch (Exception e) {
            LOG.error("Exception while creating path: " + path, e);
            result._retCode = RetCode.ERROR;
            return result;
        }
    } while (retry);
    result._retCode = RetCode.OK;
    return result;
}
Also used : ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) ZkNoNodeException(org.apache.helix.zookeeper.zkclient.exception.ZkNoNodeException) CreateMode(org.apache.zookeeper.CreateMode) ZkBadVersionException(org.apache.helix.zookeeper.zkclient.exception.ZkBadVersionException) HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) HelixException(org.apache.helix.HelixException) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) ZkNoNodeException(org.apache.helix.zookeeper.zkclient.exception.ZkNoNodeException) ZkException(org.apache.helix.zookeeper.zkclient.exception.ZkException) ZkNodeExistsException(org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException)

Aggregations

ZkNodeExistsException (org.apache.helix.zookeeper.zkclient.exception.ZkNodeExistsException)6 HelixException (org.apache.helix.HelixException)3 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)3 ZkClientException (org.apache.helix.zookeeper.exception.ZkClientException)3 ZkBadVersionException (org.apache.helix.zookeeper.zkclient.exception.ZkBadVersionException)3 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)2 InvalidRoutingDataException (org.apache.helix.msdcommon.exception.InvalidRoutingDataException)2 ZkException (org.apache.helix.zookeeper.zkclient.exception.ZkException)2 ZkNoNodeException (org.apache.helix.zookeeper.zkclient.exception.ZkNoNodeException)2 CreateMode (org.apache.zookeeper.CreateMode)2 Stat (org.apache.zookeeper.data.Stat)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ParseException (org.apache.commons.cli.ParseException)1 LiveInstance (org.apache.helix.model.LiveInstance)1 ParticipantHistory (org.apache.helix.model.ParticipantHistory)1 PropertyStoreException (org.apache.helix.store.PropertyStoreException)1 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)1 ZkSessionMismatchedException (org.apache.helix.zookeeper.zkclient.exception.ZkSessionMismatchedException)1