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);
}
}
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);
}
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;
}
}
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();
}
}
}
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();
}
}
}
Aggregations