Search in sources :

Example 31 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class GrpcMessagingConnection method onError.

@Override
public void onError(Throwable t) {
    LOG.debug("Connection failed: {}", mConnectionId, t);
    // Lock and set the state.
    try (LockResource lock = new LockResource(mStateLock.writeLock())) {
        mClosed = true;
    }
    // Used to fail exception listeners immediately until connection is reclaimed.
    mLastFailure = t;
    // Close pending requests.
    failPendingRequests(t);
    // Call exception listeners.
    for (Listener<Throwable> listener : mExceptionListeners) {
        listener.accept(t);
    }
    // Call close listeners as we can't reactivate this connection.
    for (Listener<GrpcMessagingConnection> listener : mCloseListeners) {
        listener.accept(this);
    }
}
Also used : LockResource(alluxio.resource.LockResource)

Example 32 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class GrpcMessagingConnection method close.

/**
 * Closes the connection.
 *
 * @return future of close results
 */
public CompletableFuture<Void> close() {
    if (mClosed) {
        return CompletableFuture.completedFuture(null);
    }
    return CompletableFuture.runAsync(() -> {
        LOG.debug("Closing connection: {}", mConnectionId);
        // Lock and set the state.
        try (LockResource lock = new LockResource(mStateLock.writeLock())) {
            mClosed = true;
        }
        // Stop timeout timer.
        mTimeoutScheduler.cancel();
        // Complete underlying gRPC stream.
        if (!mStreamCompleted) {
            try {
                mTargetObserver.onCompleted();
            } catch (Exception e) {
                LOG.debug("Completing underlying gRPC stream failed.", e);
            }
        }
        // Close pending requests.
        failPendingRequests(new ConnectException("Connection closed."));
        // Call close listeners.
        for (Listener<GrpcMessagingConnection> listener : mCloseListeners) {
            listener.accept(this);
        }
    }, mExecutor);
}
Also used : LockResource(alluxio.resource.LockResource) TimeoutException(java.util.concurrent.TimeoutException) ConnectException(java.net.ConnectException) SerializationException(io.atomix.catalyst.serializer.SerializationException) ConnectException(java.net.ConnectException)

Example 33 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class GrpcMessagingConnection method sendAndReceiveInternal.

/**
 * Send the request to target. If "fireAndForget" then returned future will be complete.
 *
 * @param request request to send
 * @param fireAndForget whether to not wait for response
 * @param <T> Request type
 * @param <U> Response type
 * @return future for result
 */
private <T, U> CompletableFuture<U> sendAndReceiveInternal(T request, boolean fireAndForget) {
    try (LockResource lock = new LockResource(mStateLock.readLock())) {
        Preconditions.checkNotNull(request, "request should not be null");
        // Create a contextual future for the request.
        GrpcMessagingConnection.ContextualFuture<U> future = new GrpcMessagingConnection.ContextualFuture<>(System.currentTimeMillis(), GrpcMessagingContext.currentContextOrThrow());
        // Don't allow request if connection is closed.
        if (mClosed) {
            future.completeExceptionally(new IllegalStateException("GrpcMessagingConnection closed"));
            return future;
        }
        // Get a new request Id.
        long requestId = mRequestCounter.incrementAndGet();
        // Register request future.
        mResponseFutures.put(requestId, future);
        // Serialize the request and send it over to target.
        try {
            mTargetObserver.onNext(TransportMessage.newBuilder().setRequestHeader(MessagingRequestHeader.newBuilder().setRequestId(requestId)).setMessage(UnsafeByteOperations.unsafeWrap(future.getContext().serializer().writeObject(request).array())).build());
        } catch (Exception e) {
            future.completeExceptionally(e);
            return future;
        }
        // Complete the future if response is not requested.
        if (fireAndForget) {
            future.complete(null);
        }
        // Request is sent over.
        LOG.debug("Submitted request({}) of type: {}. Connection: {} FireAndForget: {}", requestId, request.getClass().getName(), mConnectionId, fireAndForget);
        return future;
    }
}
Also used : LockResource(alluxio.resource.LockResource) TimeoutException(java.util.concurrent.TimeoutException) ConnectException(java.net.ConnectException) SerializationException(io.atomix.catalyst.serializer.SerializationException)

Example 34 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class PathProperties method add.

/**
 * Adds properties for path.
 *
 * If there are existing properties for path, they are merged with the new properties.
 * If a property key already exists, its old value is overwritten.
 *
 * @param ctx the journal context
 * @param path the path
 * @param properties the new properties
 */
public void add(Supplier<JournalContext> ctx, String path, Map<PropertyKey, String> properties) {
    try (LockResource r = new LockResource(mLock.writeLock())) {
        if (!properties.isEmpty()) {
            Map<String, String> newProperties = mState.getProperties(path);
            properties.forEach((key, value) -> newProperties.put(key.getName(), value));
            mState.applyAndJournal(ctx, PathPropertiesEntry.newBuilder().setPath(path).putAllProperties(newProperties).build());
            mHash.markOutdated();
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource)

Example 35 with LockResource

use of alluxio.resource.LockResource in project alluxio by Alluxio.

the class PathProperties method remove.

/**
 * Removes the specified set of keys from the properties for path.
 *
 * @param ctx the journal context
 * @param path the path
 * @param keys the keys to remove
 */
public void remove(Supplier<JournalContext> ctx, String path, Set<String> keys) {
    try (LockResource r = new LockResource(mLock.writeLock())) {
        Map<String, String> properties = mState.getProperties(path);
        if (!properties.isEmpty()) {
            keys.forEach(key -> properties.remove(key));
            if (properties.isEmpty()) {
                mState.applyAndJournal(ctx, RemovePathPropertiesEntry.newBuilder().setPath(path).build());
            } else {
                mState.applyAndJournal(ctx, PathPropertiesEntry.newBuilder().setPath(path).putAllProperties(properties).build());
            }
            mHash.markOutdated();
        }
    }
}
Also used : LockResource(alluxio.resource.LockResource)

Aggregations

LockResource (alluxio.resource.LockResource)116 IOException (java.io.IOException)14 TempBlockMeta (alluxio.worker.block.meta.TempBlockMeta)13 HashMap (java.util.HashMap)12 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)11 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)11 Map (java.util.Map)11 AlluxioURI (alluxio.AlluxioURI)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)10 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)9 ArrayList (java.util.ArrayList)9 MasterWorkerInfo (alluxio.master.block.meta.MasterWorkerInfo)8 InvalidPathException (alluxio.exception.InvalidPathException)7 NotFoundException (alluxio.exception.status.NotFoundException)7 List (java.util.List)7 Lock (java.util.concurrent.locks.Lock)7 ConcurrentHashSet (alluxio.collections.ConcurrentHashSet)6 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)6 UnavailableException (alluxio.exception.status.UnavailableException)6