com.dimajix.flowman.kernel.AbstractClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2018 The Flowman Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dimajix.flowman.kernel;
import java.io.Closeable;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import com.dimajix.shaded.grpc.StatusException;
import com.dimajix.shaded.grpc.StatusRuntimeException;
import com.dimajix.shaded.grpc.stub.StreamObserver;
import lombok.val;
import com.dimajix.flowman.grpc.ExceptionUtils;
public abstract class AbstractClient {
public abstract boolean isShutdown();
public abstract boolean isTerminated();
protected interface RpcCallable {
T call();
}
protected interface RpcMethod {
StreamObserver call(StreamObserver responseObserver);
}
protected static T call(RpcCallable callable) {
try {
return callable.call();
}
catch (StatusRuntimeException ex) {
throw ExceptionUtils.asRemoteException(ex);
}
}
protected static T stream(RpcMethod method, Iterator streamable) {
val finishLatch = new CountDownLatch(1);
val result = new AtomicReference();
val exception = new AtomicReference();
val observer = new StreamObserver() {
@Override
public void onNext(T value) {
result.set(value);
}
@Override
public void onError(Throwable t) {
if (t instanceof RuntimeException)
exception.set((RuntimeException)t);
else if (t instanceof StatusException)
exception.set(((StatusException)t).getStatus().asRuntimeException());
else
exception.set(new RuntimeException(t));
finishLatch.countDown();
}
@Override
public void onCompleted() {
finishLatch.countDown();
}
};
val sink = method.call(observer);
try {
while(streamable.hasNext()) {
val next = streamable.next();
sink.onNext(next);
if (finishLatch.getCount() == 0) {
// RPC completed or errored before we finished sending.
// Sending further requests won't error, but they will just be thrown away.
break;
}
}
}
catch(RuntimeException ex) {
sink.onError(ex);
throw ex;
}
// Mark the end of requests
sink.onCompleted();
// Receiving happens asynchronously
boolean hasFinished = false;
while(!hasFinished) {
try {
finishLatch.await(1, TimeUnit.MINUTES);
hasFinished = true;
}
catch(InterruptedException ex) {
// Do nothing, try again
}
}
if (exception.get() != null)
throw exception.get();
return result.get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy