Java Code Examples for com.google.longrunning.Operation#getDone()

The following examples show how to use com.google.longrunning.Operation#getDone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractServerInstance.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
protected void errorOperation(
    Operation operation, RequestMetadata requestMetadata, com.google.rpc.Status status)
    throws InterruptedException {
  if (operation.getDone()) {
    throw new IllegalStateException("Trying to error already completed operation [" + name + "]");
  }
  ExecuteOperationMetadata metadata = expectExecuteOperationMetadata(operation);
  if (metadata == null) {
    metadata = ExecuteOperationMetadata.getDefaultInstance();
  }
  CompletedOperationMetadata completedMetadata =
      CompletedOperationMetadata.newBuilder()
          .setExecuteOperationMetadata(
              metadata.toBuilder().setStage(ExecutionStage.Value.COMPLETED).build())
          .setRequestMetadata(requestMetadata)
          .build();
  putOperation(
      operation
          .toBuilder()
          .setDone(true)
          .setMetadata(Any.pack(completedMetadata))
          .setResponse(Any.pack(ExecuteResponse.newBuilder().setStatus(status).build()))
          .build());
}
 
Example 2
Source File: GrpcRemoteExecutor.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Nullable
private ExecuteResponse getOperationResponse(Operation op) throws IOException {
  if (op.getResultCase() == Operation.ResultCase.ERROR) {
    handleStatus(op.getError(), null);
  }
  if (op.getDone()) {
    Preconditions.checkState(op.getResultCase() != Operation.ResultCase.RESULT_NOT_SET);
    ExecuteResponse resp = op.getResponse().unpack(ExecuteResponse.class);
    if (resp.hasStatus()) {
      handleStatus(resp.getStatus(), resp);
    }
    Preconditions.checkState(
        resp.hasResult(), "Unexpected result of remote execution: no result");
    return resp;
  }
  return null;
}
 
Example 3
Source File: MemoryInstance.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Void> watchOperation(String operationName, Watcher watcher) {
  Operation operation = getOperation(operationName);
  try {
    watcher.observe(operation);
  } catch (Throwable t) {
    return immediateFailedFuture(t);
  }
  if (operation == null || operation.getDone()) {
    return immediateFuture(null);
  }
  WatchFuture watchFuture =
      new WatchFuture(watcher) {
        @Override
        protected void unwatch() {
          synchronized (watchers) {
            watchers.remove(operationName, this);
          }
        }
      };
  synchronized (watchers) {
    watchers.put(operationName, watchFuture);
  }
  operation = getOperation(operationName);
  if (!watchFuture.isDone() && operation == null || operation.getDone()) {
    // guarantee at least once delivery
    watchFuture.observe(operation);
  }
  return watchFuture;
}
 
Example 4
Source File: WatchFuture.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public void observe(Operation operation) {
  try {
    watcher.observe(operation);
    if (operation == null || operation.getDone()) {
      set(null);
    }
  } catch (Throwable t) {
    setException(t);
  }
}
 
Example 5
Source File: RedisShardSubscriber.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private void onOperation(
    String channel,
    @Nullable Operation operation,
    Predicate<TimedWatcher> shouldObserve,
    @Nullable Instant expiresAt) {
  List<TimedWatchFuture> operationWatchers = watchers.get(channel);
  boolean observe = operation == null || operation.hasMetadata() || operation.getDone();
  logger.log(Level.FINE, format("onOperation %s: %s", channel, operation));
  synchronized (watchers) {
    ImmutableList.Builder<Consumer<Operation>> observers = ImmutableList.builder();
    for (TimedWatchFuture watchFuture : operationWatchers) {
      TimedWatcher watcher = watchFuture.getWatcher();
      if (expiresAt != null) {
        watcher.reset(expiresAt);
      }
      if (shouldObserve.test(watcher)) {
        observers.add(watchFuture::observe);
      }
    }
    for (Consumer<Operation> observer : observers.build()) {
      executor.execute(
          () -> {
            if (observe) {
              logger.log(Level.FINE, "observing " + operation);
              observer.accept(operation);
            }
          });
    }
  }
}
 
Example 6
Source File: AbstractServerInstance.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
protected void updateOperationWatchers(Operation operation) throws InterruptedException {
  if (operation.getDone()) {
    synchronized (operationLock(operation.getName())) {
      completedOperations.put(operation.getName(), operation);
      outstandingOperations.remove(operation.getName());
    }
  } else {
    outstandingOperations.put(operation.getName(), operation);
  }
}
 
Example 7
Source File: AbstractMetricsPublisher.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected OperationRequestMetadata populateRequestMetadata(
    Operation operation, RequestMetadata requestMetadata) {
  try {
    OperationRequestMetadata operationRequestMetadata =
        OperationRequestMetadata.newBuilder()
            .setRequestMetadata(requestMetadata)
            .setOperationName(operation.getName())
            .setDone(operation.getDone())
            .setClusterId(clusterId)
            .build();
    if (operation.getDone() && operation.getResponse().is(ExecuteResponse.class)) {
      operationRequestMetadata =
          operationRequestMetadata
              .toBuilder()
              .setExecuteResponse(operation.getResponse().unpack(ExecuteResponse.class))
              .build();
    }
    if (operation.getMetadata().is(ExecuteOperationMetadata.class)) {
      operationRequestMetadata =
          operationRequestMetadata
              .toBuilder()
              .setExecuteOperationMetadata(
                  operation.getMetadata().unpack(ExecuteOperationMetadata.class))
              .build();
    }
    return operationRequestMetadata;
  } catch (Exception e) {
    logger.log(
        Level.WARNING,
        String.format("Could not populate request metadata for %s.", operation.getName()),
        e);
    return null;
  }
}
 
Example 8
Source File: ShardWorkerContext.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Override
public boolean putOperation(Operation operation, Action action)
    throws IOException, InterruptedException {
  boolean success = createBackplaneRetrier().execute(() -> instance.putOperation(operation));
  if (success && operation.getDone()) {
    logComplete(operation.getName());
  }
  return success;
}
 
Example 9
Source File: RedisShardBackplane.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
@Override
public boolean putOperation(Operation operation, ExecutionStage.Value stage) throws IOException {
  // FIXME queue and prequeue should no longer be passed to here
  boolean prequeue = stage == ExecutionStage.Value.UNKNOWN && !operation.getDone();
  boolean queue = stage == ExecutionStage.Value.QUEUED;
  boolean complete = !queue && operation.getDone();
  boolean publish = !queue && stage != ExecutionStage.Value.UNKNOWN;

  if (complete) {
    // for filtering anything that shouldn't be stored
    operation = onComplete.apply(operation);
  }

  String json;
  try {
    json = operationPrinter.print(operation);
  } catch (InvalidProtocolBufferException e) {
    logger.log(Level.SEVERE, "error printing operation " + operation.getName(), e);
    return false;
  }

  Operation publishOperation;
  if (publish) {
    publishOperation = onPublish.apply(operation);
  } else {
    publishOperation = null;
  }

  String name = operation.getName();
  client.run(
      jedis -> {
        jedis.setex(operationKey(name), config.getOperationExpire(), json);
        if (publishOperation != null) {
          publishReset(jedis, publishOperation);
        }
        if (complete) {
          completeOperation(jedis, name);
        }
      });
  return true;
}
 
Example 10
Source File: AbstractServerInstance.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
protected static boolean isErrored(Operation operation) {
  return operation.getDone()
      && operation.getResultCase() == Operation.ResultCase.RESPONSE
      && operation.getResponse().is(ExecuteResponse.class)
      && expectExecuteResponse(operation).getStatus().getCode() != Code.OK.getNumber();
}
 
Example 11
Source File: AbstractServerInstance.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
protected boolean isCancelled(Operation operation) {
  return operation.getDone()
      && operation.getResultCase() == Operation.ResultCase.RESPONSE
      && operation.getResponse().is(ExecuteResponse.class)
      && expectExecuteResponse(operation).getStatus().getCode() == Code.CANCELLED.getNumber();
}
 
Example 12
Source File: AbstractServerInstance.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
protected static ExecuteResponse getExecuteResponse(Operation operation) {
  if (operation.getDone() && operation.getResultCase() == Operation.ResultCase.RESPONSE) {
    return expectExecuteResponse(operation);
  }
  return null;
}
 
Example 13
Source File: RedisShardSubscriberTest.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
@Test
public void doneResetOperationIsObservedAndUnsubscribed()
    throws InterruptedException, InvalidProtocolBufferException {
  ListMultimap<String, TimedWatchFuture> watchers =
      Multimaps.<String, TimedWatchFuture>synchronizedListMultimap(
          MultimapBuilder.linkedHashKeys().arrayListValues().build());
  RedisShardSubscriber operationSubscriber = createSubscriber(watchers, directExecutor());

  TestClient testClient = new TestClient();
  Thread proceedThread = new Thread(() -> operationSubscriber.proceed(testClient));
  proceedThread.start();
  while (!operationSubscriber.isSubscribed()) {
    MICROSECONDS.sleep(10);
  }

  String doneMessageChannel = "done-message-channel";
  AtomicBoolean observed = new AtomicBoolean(false);
  TimedWatcher doneMessageWatcher =
      new TimedWatcher(Instant.now()) {
        @Override
        public void observe(Operation operation) {
          if (operation.getDone()) {
            observed.set(true);
          }
        }
      };
  operationSubscriber.watch(doneMessageChannel, doneMessageWatcher);
  operationSubscriber.onMessage(
      doneMessageChannel,
      printOperationChange(
          OperationChange.newBuilder()
              .setReset(
                  OperationChange.Reset.newBuilder()
                      .setOperation(Operation.newBuilder().setDone(true).build())
                      .build())
              .build()));
  assertThat(observed.get()).isTrue();
  assertThat(testClient.getSubscriptions()).doesNotContain(doneMessageChannel);
  operationSubscriber.unsubscribe();
  proceedThread.join();
}