Search in sources :

Example 1 with HazelcastInstanceNotActiveException

use of com.hazelcast.core.HazelcastInstanceNotActiveException in project orientdb by orientechnologies.

the class ODistributedStorage method readRecord.

public OStorageOperationResult<ORawBuffer> readRecord(final ORecordId iRecordId, final String iFetchPlan, final boolean iIgnoreCache, final boolean prefetchRecords, final ORecordCallback<ORawBuffer> iCallback) {
    final ORawBuffer memCopy = localDistributedDatabase.getRecordIfLocked(iRecordId);
    if (memCopy != null)
        return new OStorageOperationResult<ORawBuffer>(memCopy);
    try {
        final String clusterName = getClusterNameByRID(iRecordId);
        final ODistributedConfiguration dbCfg = distributedConfiguration;
        final List<String> nodes = dbCfg.getServers(clusterName, null);
        final int availableNodes = nodes.size();
        // CHECK IF LOCAL NODE OWNS THE DATA AND READ-QUORUM = 1: GET IT LOCALLY BECAUSE IT'S FASTER
        final String localNodeName = dManager.getLocalNodeName();
        if (nodes.isEmpty() || nodes.contains(dManager.getLocalNodeName()) && dbCfg.getReadQuorum(clusterName, availableNodes, localNodeName) <= 1) {
            // DON'T REPLICATE
            return (OStorageOperationResult<ORawBuffer>) OScenarioThreadLocal.executeAsDistributed(new Callable() {

                @Override
                public Object call() throws Exception {
                    return wrapped.readRecord(iRecordId, iFetchPlan, iIgnoreCache, prefetchRecords, iCallback);
                }
            });
        }
        // DISTRIBUTE IT
        final ODistributedResponse response = dManager.sendRequest(getName(), Collections.singleton(clusterName), nodes, new OReadRecordTask(iRecordId), dManager.getNextMessageIdCounter(), EXECUTION_MODE.RESPONSE, null, null);
        final Object dResult = response != null ? response.getPayload() : null;
        if (dResult instanceof ONeedRetryException)
            throw (ONeedRetryException) dResult;
        else if (dResult instanceof Exception)
            throw OException.wrapException(new ODistributedException("Error on execution distributed read record"), (Exception) dResult);
        return new OStorageOperationResult<ORawBuffer>((ORawBuffer) dResult);
    } catch (ONeedRetryException e) {
        // PASS THROUGH
        throw e;
    } catch (HazelcastInstanceNotActiveException e) {
        throw new OOfflineNodeException("Hazelcast instance is not available");
    } catch (HazelcastException e) {
        throw new OOfflineNodeException("Hazelcast instance is not available");
    } catch (Exception e) {
        handleDistributedException("Cannot route read record operation for %s to the distributed node", e, iRecordId);
        // UNREACHABLE
        return null;
    }
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OCallable(com.orientechnologies.common.util.OCallable) Callable(java.util.concurrent.Callable) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) HazelcastException(com.hazelcast.core.HazelcastException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException)

Example 2 with HazelcastInstanceNotActiveException

use of com.hazelcast.core.HazelcastInstanceNotActiveException in project orientdb by orientechnologies.

the class ODistributedStorage method updateRecord.

@Override
public OStorageOperationResult<Integer> updateRecord(final ORecordId iRecordId, final boolean updateContent, final byte[] iContent, final int iVersion, final byte iRecordType, final int iMode, final ORecordCallback<Integer> iCallback) {
    resetLastValidBackup();
    if (OScenarioThreadLocal.INSTANCE.isRunModeDistributed()) {
        // ALREADY DISTRIBUTED
        return wrapped.updateRecord(iRecordId, updateContent, iContent, iVersion, iRecordType, iMode, iCallback);
    }
    checkLocalNodeIsAvailable();
    final ODistributedConfiguration dbCfg = distributedConfiguration;
    final String clusterName = getClusterNameByRID(iRecordId);
    final String localNodeName = dManager.getLocalNodeName();
    checkWriteQuorum(dbCfg, clusterName, localNodeName);
    try {
        checkNodeIsMaster(localNodeName, dbCfg);
        final List<String> nodes = dbCfg.getServers(clusterName, null);
        if (nodes.isEmpty())
            // NO REPLICATION: EXECUTE IT LOCALLY
            return wrapped.updateRecord(iRecordId, updateContent, iContent, iVersion, iRecordType, iMode, iCallback);
        final Set<String> clusterNames = Collections.singleton(clusterName);
        Boolean executionModeSynch = dbCfg.isExecutionModeSynchronous(clusterName);
        if (executionModeSynch == null)
            executionModeSynch = iMode == 0;
        final boolean syncMode = executionModeSynch;
        return (OStorageOperationResult<Integer>) executeRecordOperationInLock(syncMode, iRecordId, new OCallable<Object, OCallable<Void, ODistributedRequestId>>() {

            @Override
            public Object call(OCallable<Void, ODistributedRequestId> unlockCallback) {
                final OUpdateRecordTask task = new OUpdateRecordTask(iRecordId, iContent, iVersion, iRecordType);
                final OStorageOperationResult<Integer> localResult;
                final boolean executedLocally = nodes.contains(localNodeName);
                if (executedLocally) {
                    // EXECUTE ON LOCAL NODE FIRST
                    try {
                        // LOAD CURRENT RECORD
                        task.checkRecordExists();
                        localResult = (OStorageOperationResult<Integer>) OScenarioThreadLocal.executeAsDistributed(new Callable() {

                            @Override
                            public Object call() throws Exception {
                                task.setLastLSN(wrapped.getLSN());
                                return wrapped.updateRecord(iRecordId, updateContent, iContent, iVersion, iRecordType, iMode, iCallback);
                            }
                        });
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception e) {
                        throw OException.wrapException(new ODistributedException("Cannot delete record " + iRecordId), e);
                    }
                    nodes.remove(localNodeName);
                } else
                    localResult = null;
                if (nodes.isEmpty()) {
                    unlockCallback.call(null);
                    if (!executedLocally)
                        throw new ODistributedException("Cannot execute distributed update on record " + iRecordId + " because no nodes are available");
                } else {
                    final Integer localResultPayload = localResult != null ? localResult.getResult() : null;
                    if (syncMode || localResult == null) {
                        // REPLICATE IT
                        try {
                            final ODistributedResponse dResponse = dManager.sendRequest(getName(), clusterNames, nodes, task, dManager.getNextMessageIdCounter(), EXECUTION_MODE.RESPONSE, localResultPayload, unlockCallback);
                            final Object payload = dResponse.getPayload();
                            if (payload instanceof Exception) {
                                if (payload instanceof ORecordNotFoundException) {
                                    // REPAIR THE RECORD IMMEDIATELY
                                    localDistributedDatabase.getDatabaseRepairer().enqueueRepairRecord((ORecordId) ((ORecordNotFoundException) payload).getRid());
                                }
                                executeUndoOnLocalServer(dResponse.getRequestId(), task);
                                if (payload instanceof ONeedRetryException)
                                    throw (ONeedRetryException) payload;
                                throw OException.wrapException(new ODistributedException("Error on execution distributed update record"), (Exception) payload);
                            }
                            // UPDATE LOCALLY
                            return new OStorageOperationResult<Integer>((Integer) payload);
                        } catch (RuntimeException e) {
                            executeUndoOnLocalServer(null, task);
                            throw e;
                        } catch (Exception e) {
                            executeUndoOnLocalServer(null, task);
                            ODatabaseException.wrapException(new ODistributedException("Cannot execute distributed update record"), e);
                        }
                    }
                    // ASYNCHRONOUS CALL: EXECUTE LOCALLY AND THEN DISTRIBUTE
                    asynchronousExecution(new OAsynchDistributedOperation(getName(), Collections.singleton(clusterName), nodes, task, dManager.getNextMessageIdCounter(), localResultPayload, unlockCallback, null));
                }
                return localResult;
            }
        });
    } catch (ONeedRetryException e) {
        localDistributedDatabase.getDatabaseRepairer().enqueueRepairRecord(iRecordId);
        // PASS THROUGH
        throw e;
    } catch (HazelcastInstanceNotActiveException e) {
        throw new OOfflineNodeException("Hazelcast instance is not available");
    } catch (HazelcastException e) {
        throw new OOfflineNodeException("Hazelcast instance is not available");
    } catch (Exception e) {
        localDistributedDatabase.getDatabaseRepairer().enqueueRepairRecord(iRecordId);
        handleDistributedException("Cannot route UPDATE_RECORD operation for %s to the distributed node", e, iRecordId);
        // UNREACHABLE
        return null;
    }
}
Also used : OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OCallable(com.orientechnologies.common.util.OCallable) Callable(java.util.concurrent.Callable) OCallable(com.orientechnologies.common.util.OCallable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastException(com.hazelcast.core.HazelcastException) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) HazelcastException(com.hazelcast.core.HazelcastException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException)

Example 3 with HazelcastInstanceNotActiveException

use of com.hazelcast.core.HazelcastInstanceNotActiveException in project hazelcast by hazelcast.

the class AbstractMessageTask method initializeAndProcessMessage.

private void initializeAndProcessMessage() throws Throwable {
    if (!node.getNodeExtension().isStartCompleted()) {
        throw new HazelcastInstanceNotActiveException("Hazelcast instance is not ready yet!");
    }
    parameters = decodeClientMessage(clientMessage);
    Credentials credentials = endpoint.getCredentials();
    interceptBefore(credentials);
    checkPermissions(endpoint);
    processMessage();
    interceptAfter(credentials);
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) Credentials(com.hazelcast.security.Credentials)

Example 4 with HazelcastInstanceNotActiveException

use of com.hazelcast.core.HazelcastInstanceNotActiveException in project hazelcast by hazelcast.

the class ClusterMergeTask method waitOnFutureInterruptible.

private <V> V waitOnFutureInterruptible(Future<V> future, long timeout, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException {
    isNotNull(timeUnit, "timeUnit");
    long totalTimeoutMs = timeUnit.toMillis(timeout);
    while (true) {
        long timeoutStepMs = Math.min(MIN_WAIT_ON_FUTURE_TIMEOUT_MILLIS, totalTimeoutMs);
        try {
            return future.get(timeoutStepMs, TimeUnit.MILLISECONDS);
        } catch (TimeoutException t) {
            totalTimeoutMs -= timeoutStepMs;
            if (totalTimeoutMs <= 0) {
                throw t;
            }
            if (!node.isRunning()) {
                future.cancel(true);
                throw new HazelcastInstanceNotActiveException();
            }
        }
    }
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with HazelcastInstanceNotActiveException

use of com.hazelcast.core.HazelcastInstanceNotActiveException in project hazelcast by hazelcast.

the class AbstractSerializationService method toObject.

@Override
public final <T> T toObject(final Object object, Class aClass) {
    if (!(object instanceof Data)) {
        return (T) object;
    }
    Data data = (Data) object;
    if (isNullData(data)) {
        return null;
    }
    BufferPool pool = bufferPoolThreadLocal.get();
    BufferObjectDataInput in = pool.takeInputBuffer(data);
    try {
        ClassLocator.onStartDeserialization();
        final int typeId = data.getType();
        final SerializerAdapter serializer = serializerFor(typeId);
        if (serializer == null) {
            if (active) {
                throw newHazelcastSerializationException(typeId);
            }
            throw new HazelcastInstanceNotActiveException();
        }
        Object obj = serializer.read(in, aClass);
        if (managedContext != null) {
            obj = managedContext.initialize(obj);
        }
        return (T) obj;
    } catch (Throwable e) {
        throw handleException(e);
    } finally {
        ClassLocator.onFinishDeserialization();
        pool.returnInputBuffer(in);
    }
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) BufferPool(com.hazelcast.internal.serialization.impl.bufferpool.BufferPool) SerializationUtil.createSerializerAdapter(com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter) SerializationUtil.isNullData(com.hazelcast.internal.serialization.impl.SerializationUtil.isNullData) Data(com.hazelcast.nio.serialization.Data) BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput)

Aggregations

HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)34 HazelcastException (com.hazelcast.core.HazelcastException)8 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)8 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)8 Test (org.junit.Test)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)7 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)7 OException (com.orientechnologies.common.exception.OException)7 OIOException (com.orientechnologies.common.io.OIOException)7 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)7 TransactionException (com.hazelcast.transaction.TransactionException)6 OCallable (com.orientechnologies.common.util.OCallable)6 ExecutionException (java.util.concurrent.ExecutionException)6 SerializationUtil.createSerializerAdapter (com.hazelcast.internal.serialization.impl.SerializationUtil.createSerializerAdapter)5 TargetNotMemberException (com.hazelcast.spi.exception.TargetNotMemberException)5 AssertTask (com.hazelcast.test.AssertTask)5 Callable (java.util.concurrent.Callable)5 TransactionOptions (com.hazelcast.transaction.TransactionOptions)4