@Override
  protected void doSubscribe(String eventName, final SettableFuture<?> future) {
    List<SettableFuture<?>> futures = new ArrayList<SettableFuture<?>>();

    for (RedisConnection conn : connections) {
      SettableFuture<?> newFuture = SettableFuture.create();
      conn.subscribe(eventName, newFuture);
      futures.add(newFuture);
    }

    final ListenableFuture<?> combined = Futures.allAsList(futures);

    combined.addListener(
        new Runnable() {
          @Override
          public void run() {
            try {
              combined.get();
              future.set(null);
            } catch (InterruptedException e) {
              future.setException(e);
            } catch (ExecutionException e) {
              future.setException(e.getCause());
            }
          }
        },
        getExecutorService());
  }
  /**
   * Sends a {@link Message} to the remote service. Returns a future that will be completed when the
   * message has been processed.
   *
   * @param message The message to send.
   * @param result Object to set into the future when message is being processed.
   * @param <V> Type of the result.
   * @return A {@link ListenableFuture} that will be completed when the message has been processed.
   */
  protected final synchronized <V> ListenableFuture<V> sendMessage(Message message, V result) {
    if (!isRunning()) {
      return Futures.immediateFailedFuture(
          new IllegalStateException("Cannot send message to non-running application"));
    }
    final ListenableFuture<V> messageFuture =
        ZKMessages.sendMessage(zkClient, getMessagePrefix(), message, result);
    messageFutures.add(messageFuture);
    messageFuture.addListener(
        new Runnable() {
          @Override
          public void run() {
            // If the completion is triggered when stopping, do nothing.
            if (state() == State.STOPPING) {
              return;
            }
            synchronized (AbstractZKServiceController.this) {
              messageFutures.remove(messageFuture);
            }
          }
        },
        Threads.SAME_THREAD_EXECUTOR);

    return messageFuture;
  }
    private synchronized void scheduleNextRequest() {
      // stopped or done?
      TaskInfo taskInfo = HttpRemoteTask.this.taskInfo.get();
      if (!running || taskInfo.getState().isDone()) {
        return;
      }

      // outstanding request?
      if (future != null && !future.isDone()) {
        // this should never happen
        log.error("Can not reschedule update because an update is already running");
        return;
      }

      // if throttled due to error, asynchronously wait for timeout and try again
      ListenableFuture<?> errorRateLimit = getErrorTracker.acquireRequestPermit();
      if (!errorRateLimit.isDone()) {
        errorRateLimit.addListener(this::scheduleNextRequest, executor);
        return;
      }

      Request request =
          prepareGet()
              .setUri(uriBuilderFrom(taskInfo.getSelf()).addParameter("summarize").build())
              .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString())
              .setHeader(PrestoHeaders.PRESTO_CURRENT_STATE, taskInfo.getState().toString())
              .setHeader(PrestoHeaders.PRESTO_MAX_WAIT, refreshMaxWait.toString())
              .build();

      getErrorTracker.startRequest();

      future = httpClient.executeAsync(request, createFullJsonResponseHandler(taskInfoCodec));
      Futures.addCallback(
          future, new SimpleHttpResponseHandler<>(this, request.getUri()), executor);
    }
  private synchronized void scheduleUpdate() {
    // don't update if the task hasn't been started yet or if it is already finished
    if (!needsUpdate.get() || taskInfo.get().getState().isDone()) {
      return;
    }

    // if we have an old request outstanding, cancel it
    if (currentRequest != null
        && Duration.nanosSince(currentRequestStartNanos).compareTo(new Duration(2, SECONDS)) >= 0) {
      needsUpdate.set(true);
      currentRequest.cancel(true);
      currentRequest = null;
      currentRequestStartNanos = 0;
    }

    // if there is a request already running, wait for it to complete
    if (this.currentRequest != null && !this.currentRequest.isDone()) {
      return;
    }

    // if throttled due to error, asynchronously wait for timeout and try again
    ListenableFuture<?> errorRateLimit = updateErrorTracker.acquireRequestPermit();
    if (!errorRateLimit.isDone()) {
      errorRateLimit.addListener(this::scheduleUpdate, executor);
      return;
    }

    List<TaskSource> sources = getSources();
    TaskUpdateRequest updateRequest =
        new TaskUpdateRequest(
            session.toSessionRepresentation(), planFragment, sources, outputBuffers.get());

    Request request =
        preparePost()
            .setUri(uriBuilderFrom(taskInfo.get().getSelf()).addParameter("summarize").build())
            .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString())
            .setBodyGenerator(jsonBodyGenerator(taskUpdateRequestCodec, updateRequest))
            .build();

    updateErrorTracker.startRequest();

    ListenableFuture<JsonResponse<TaskInfo>> future =
        httpClient.executeAsync(request, createFullJsonResponseHandler(taskInfoCodec));
    currentRequest = future;
    currentRequestStartNanos = System.nanoTime();

    // The needsUpdate flag needs to be set to false BEFORE adding the Future callback since
    // callback might change the flag value
    // and does so without grabbing the instance lock.
    needsUpdate.set(false);

    Futures.addCallback(
        future,
        new SimpleHttpResponseHandler<>(new UpdateResponseHandler(sources), request.getUri()),
        executor);
  }
  public void recordBlocked(ListenableFuture<?> blocked) {
    requireNonNull(blocked, "blocked is null");

    BlockedMonitor monitor = new BlockedMonitor();

    BlockedMonitor oldMonitor = blockedMonitor.getAndSet(monitor);
    if (oldMonitor != null) {
      oldMonitor.run();
    }

    blocked.addListener(monitor, executor);
  }
        @Override
        public void run() {
          int numSourcesDrained = m_drainedSources.incrementAndGet();
          exportLog.info(
              "Drained source in generation "
                  + m_timestamp
                  + " with "
                  + numSourcesDrained
                  + " of "
                  + m_numSources
                  + " drained");
          if (numSourcesDrained == m_numSources) {
            if (m_partitionLeaderZKName.isEmpty()) {
              m_onAllSourcesDrained.run();
            } else {
              ListenableFuture<?> removeLeadership =
                  m_childUpdatingThread.submit(
                      new Runnable() {
                        @Override
                        public void run() {
                          for (Map.Entry<Integer, String> entry :
                              m_partitionLeaderZKName.entrySet()) {
                            m_zk.delete(
                                m_leadersZKPath + "/" + entry.getKey() + "/" + entry.getValue(),
                                -1,
                                new AsyncCallback.VoidCallback() {

                                  @Override
                                  public void processResult(int rc, String path, Object ctx) {
                                    KeeperException.Code code = KeeperException.Code.get(rc);
                                    if (code != KeeperException.Code.OK) {
                                      VoltDB.crashLocalVoltDB(
                                          "Error in export leader election giving up leadership of "
                                              + path,
                                          true,
                                          KeeperException.create(code));
                                    }
                                  }
                                },
                                null);
                          }
                        }
                      },
                      null);
              removeLeadership.addListener(
                  m_onAllSourcesDrained, MoreExecutors.sameThreadExecutor());
            }

            ;
          }
        }
Exemple #7
0
  /**
   * Creates a zip file of the metadata and recorded artifacts and stores it in the artifact cache.
   */
  public void performUploadToArtifactCache(
      ImmutableSet<RuleKey> ruleKeys, ArtifactCache artifactCache, BuckEventBus eventBus)
      throws InterruptedException {

    // Skip all of this if caching is disabled. Although artifactCache.store() will be a noop,
    // building up the zip is wasted I/O.
    if (!artifactCache.isStoreSupported()) {
      return;
    }

    ArtifactCompressionEvent.Started started =
        ArtifactCompressionEvent.started(ArtifactCompressionEvent.Operation.COMPRESS, ruleKeys);
    eventBus.post(started);

    final Path zip;
    ImmutableSet<Path> pathsToIncludeInZip = ImmutableSet.of();
    ImmutableMap<String, String> buildMetadata;
    try {
      pathsToIncludeInZip = getRecordedDirsAndFiles();
      zip =
          Files.createTempFile(
              "buck_artifact_" + MoreFiles.sanitize(buildTarget.getShortName()), ".zip");
      buildMetadata = getBuildMetadata();
      projectFilesystem.createZip(pathsToIncludeInZip, zip, ImmutableMap.<Path, String>of());
    } catch (IOException e) {
      eventBus.post(
          ConsoleEvent.info(
              "Failed to create zip for %s containing:\n%s",
              buildTarget, Joiner.on('\n').join(ImmutableSortedSet.copyOf(pathsToIncludeInZip))));
      e.printStackTrace();
      return;
    } finally {
      eventBus.post(ArtifactCompressionEvent.finished(started));
    }

    // Store the artifact, including any additional metadata.
    ListenableFuture<Void> storeFuture =
        artifactCache.store(ruleKeys, buildMetadata, BorrowablePath.notBorrowablePath(zip));
    storeFuture.addListener(
        new Runnable() {
          @Override
          public void run() {
            try {
              Files.deleteIfExists(zip);
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
          }
        },
        directExecutor());
  }
  public static void main(String[] args) {

    ExecutorService executorService = Executors.newFixedThreadPool(1);
    ListeningExecutorService executor = MoreExecutors.listeningDecorator(executorService);

    ListenableFuture<String> future =
        executor.submit(
            () -> {
              Thread.sleep(1000);
              return "Task completed";
            });

    future.addListener(() -> runOnCompletion(), executor);
  }
 /**
  * Links the state of this future with the given future. This will make this future mirror its
  * state with the given future.
  *
  * <p>This method will do nothing if this future was already {@link #isDone() done}. If this
  * future is attached to multiple other futures, one will "win", but it is unspecified which one.
  *
  * @param future The future.
  */
 public void updateUsing(final ListenableFuture<T> future) {
   future.addListener(
       new Runnable() {
         @Override
         public void run() {
           try {
             set(future.get());
           } catch (InterruptedException e) {
             setException(e);
           } catch (ExecutionException e) {
             setException(e.getCause());
           }
         }
       },
       MoreExecutors.sameThreadExecutor());
 }
Exemple #10
0
    private void launch(Instance instance, Session session) {
        final File extractDir = launcher.createExtractDir();

        // Get the process
        Runner task = new Runner(launcher, instance, session, extractDir);
        ObservableFuture<Process> processFuture = new ObservableFuture<Process>(
                launcher.getExecutor().submit(task), task);

        // Show process for the process retrieval
        ProgressDialog.showProgress(
                this, processFuture, _("launcher.launchingTItle"), _("launcher.launchingStatus", instance.getTitle()));

        // If the process is started, get rid of this window
        Futures.addCallback(processFuture, new FutureCallback<Process>() {
            @Override
            public void onSuccess(Process result) {
                dispose();
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });

        // Watch the created process
        ListenableFuture<?> future = Futures.transform(
                processFuture, new LaunchProcessHandler(launcher), launcher.getExecutor());
        SwingHelper.addErrorDialogCallback(null, future);

        // Clean up at the very end
        future.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("Process ended; cleaning up " + extractDir.getAbsolutePath());
                    FileUtils.deleteDirectory(extractDir);
                } catch (IOException e) {
                    log.log(Level.WARNING, "Failed to clean up " + extractDir.getAbsolutePath(), e);
                }
                instancesModel.update();
            }
        }, sameThreadExecutor());
    }
Exemple #11
0
    @Override
    public void run() {
      try (SetThreadName runnerName = new SetThreadName("SplitRunner-%s", runnerId)) {
        while (!closed && !Thread.currentThread().isInterrupted()) {
          // select next worker
          final PrioritizedSplitRunner split;
          try {
            split = pendingSplits.take();
            if (split.updatePriorityLevel()) {
              // priority level changed, return split to queue for re-prioritization
              pendingSplits.put(split);
              continue;
            }
          } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
          }

          try (SetThreadName splitName = new SetThreadName(split.toString())) {
            runningSplits.add(split);

            boolean finished;
            ListenableFuture<?> blocked;
            try {
              split.initializeIfNecessary();
              blocked = split.process();
              finished = split.isFinished();
            } finally {
              runningSplits.remove(split);
            }

            if (finished) {
              log.debug("%s is finished", split);
              splitFinished(split);
            } else {
              if (blocked.isDone()) {
                pendingSplits.put(split);
              } else {
                blockedSplits.add(split);
                blocked.addListener(
                    new Runnable() {
                      @Override
                      public void run() {
                        blockedSplits.remove(split);
                        split.updatePriorityLevel();
                        pendingSplits.put(split);
                      }
                    },
                    executor);
              }
            }
          } catch (Throwable t) {
            log.error(t, "Error processing %s", split);
            splitFinished(split);
          }
        }
      } finally {
        // unless we have been closed, we need to replace this thread
        if (!closed) {
          addRunnerThread();
        }
      }
    }
 public ListenableFuture<Transaction> broadcast() {
   log.info("Waiting for {} peers required for broadcast ...", minConnections);
   ListenableFuture<PeerGroup> peerAvailabilityFuture = peerGroup.waitForPeers(minConnections);
   peerAvailabilityFuture.addListener(new EnoughAvailablePeers(), Threading.SAME_THREAD);
   return future;
 }