Example usage for com.google.common.util.concurrent SettableFuture set

List of usage examples for com.google.common.util.concurrent SettableFuture set

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture set.

Prototype

@Override
    public boolean set(@Nullable V value) 

Source Link

Usage

From source file:io.prestosql.operator.PagesSpatialIndexFactory.java

/**
 * Called by {@link SpatialIndexBuilderOperator} to provide a
 * {@link Supplier} of spatial indexes for {@link SpatialJoinOperator}s to use.
 * <p>//  w  w  w  .  j av a  2  s. c  om
 * Returns a Future that completes once all the {@link SpatialJoinOperator}s have completed.
 */
public ListenableFuture<?> lendPagesSpatialIndex(Supplier<PagesSpatialIndex> pagesSpatialIndex) {
    requireNonNull(pagesSpatialIndex, "pagesSpatialIndex is null");

    if (activeProbeOperators.getFreeFuture().isDone()) {
        return NOT_BLOCKED;
    }

    List<SettableFuture<PagesSpatialIndex>> settableFutures;
    synchronized (this) {
        verify(this.pagesSpatialIndex == null);
        this.pagesSpatialIndex = pagesSpatialIndex;
        settableFutures = ImmutableList.copyOf(pagesSpatialIndexFutures);
        pagesSpatialIndexFutures.clear();
    }

    for (SettableFuture<PagesSpatialIndex> settableFuture : settableFutures) {
        settableFuture.set(pagesSpatialIndex.get());
    }

    return activeProbeOperators.getFreeFuture();
}

From source file:com.microsoft.services.orc.core.OrcMediaEntityFetcher.java

public ListenableFuture<InputStream> getStreamedContent() {

    Request request = getResolver().createRequest();
    request.setVerb(HttpVerb.GET);/*from w  ww  . j  av a 2s .c o  m*/
    request.addOption(Request.MUST_STREAM_RESPONSE_CONTENT, "true");
    OrcURL url = request.getUrl();
    url.appendPathComponent("$value");

    ListenableFuture<OrcResponse> future = oDataExecute(request);

    return Futures.transform(future, new AsyncFunction<OrcResponse, InputStream>() {
        @Override
        public ListenableFuture<InputStream> apply(OrcResponse response) throws Exception {
            SettableFuture<InputStream> result = SettableFuture.create();
            result.set(new MediaEntityInputStream(response.openStreamedResponse(), response));
            return result;
        }
    });
}

From source file:com.google.cloud.bigtable.grpc.async.BulkRead.java

/**
 * Sends all remaining requests to the server. This method does not wait for the method to
 * complete./*from w ww  .  j  a  va2s .  co  m*/
 */
public void flush() {
    if (futures != null && !futures.isEmpty()) {
        try {
            ResultScanner<Row> scanner = client
                    .readRows(ReadRowsRequest.newBuilder().setTableName(tableName).setFilter(currentFilter)
                            .setRows(RowSet.newBuilder().addAllRowKeys(futures.keys()).build()).build());
            while (true) {
                Row row = scanner.next();
                if (row == null) {
                    break;
                }
                Collection<SettableFuture<List<Row>>> rowFutures = futures.get(row.getKey());
                if (rowFutures != null) {
                    // TODO: What about missing keys?
                    for (SettableFuture<List<Row>> rowFuture : rowFutures) {
                        rowFuture.set(ImmutableList.of(row));
                    }
                    futures.removeAll(row.getKey());
                } else {
                    LOG.warn("Found key: %s, but it was not in the original request.", row.getKey());
                }
            }
            for (Entry<ByteString, SettableFuture<List<Row>>> entry : futures.entries()) {
                entry.getValue().set(ImmutableList.<Row>of());
            }
        } catch (IOException e) {
            for (Entry<ByteString, SettableFuture<List<Row>>> entry : futures.entries()) {
                entry.getValue().setException(e);
            }
        }
    }
    futures = null;
    currentFilter = null;
}

From source file:io.prestosql.execution.buffer.OutputBufferMemoryManager.java

public synchronized void setNoBlockOnFull() {
    blockOnFull.set(false);//from www  .  j ava2 s .c o  m

    // Complete future in a new thread to avoid making a callback on the caller thread.
    SettableFuture<?> future = this.bufferBlockedFuture;
    notificationExecutor.execute(() -> future.set(null));
}

From source file:org.apache.bookkeeper.bookie.LedgerDescriptorImpl.java

synchronized SettableFuture<Boolean> fenceAndLogInJournal(Journal journal) throws IOException {
    boolean success = this.setFenced();
    if (success) {
        // fenced for first time, we should add the key to journal ensure we can rebuild.
        return logFenceEntryInJournal(journal);
    } else {//from   w w  w.j ava 2 s  . co m
        // If we reach here, it means the fence state in FileInfo has been set (may not be persisted yet).
        // However, writing the fence log entry to the journal might still be in progress. This can happen
        // when a bookie receives two fence requests almost at the same time. The subsequent logic is used
        // to check the fencing progress.
        if (logFenceResult == null || fenceEntryPersisted.get()) {
            // Either ledger's fenced state is recovered from Journal
            // Or Log fence entry in Journal succeed
            SettableFuture<Boolean> result = SettableFuture.create();
            result.set(true);
            return result;
        } else if (logFenceResult.isDone()) {
            // We failed to log fence entry in Journal, try again.
            return logFenceEntryInJournal(journal);
        }
        // Fencing is in progress
        return logFenceResult;
    }
}

From source file:io.prestosql.execution.buffer.OutputBufferMemoryManager.java

@VisibleForTesting
synchronized void onMemoryAvailable() {
    // Do not notify the listeners if the buffer is full
    if (bufferedBytes.get() > maxBufferedBytes) {
        return;/*ww w. ja  v  a2  s  . com*/
    }

    // notify listeners if the buffer is not full
    SettableFuture<?> future = this.bufferBlockedFuture;
    notificationExecutor.execute(() -> future.set(null));
}

From source file:com.facebook.presto.operator.PartitionedLookupSourceFactory.java

public void setPartitionLookupSourceSupplier(int partitionIndex, Supplier<LookupSource> partitionLookupSource) {
    requireNonNull(partitionLookupSource, "partitionLookupSource is null");

    TrackingLookupSourceSupplier lookupSourceSupplier = null;
    List<SettableFuture<LookupSource>> lookupSourceFutures = null;
    synchronized (this) {
        if (destroyed.isDone()) {
            return;
        }/*  w  ww  .  j  av  a 2s  .  c  o m*/

        checkState(partitions[partitionIndex] == null, "Partition already set");
        partitions[partitionIndex] = partitionLookupSource;
        partitionsSet++;

        if (partitionsSet == partitions.length) {
            if (partitionsSet != 1) {
                List<Supplier<LookupSource>> partitions = ImmutableList.copyOf(this.partitions);
                this.lookupSourceSupplier = createPartitionedLookupSourceSupplier(partitions, hashChannelTypes,
                        outer);
            } else if (outer) {
                this.lookupSourceSupplier = createOuterLookupSourceSupplier(partitionLookupSource);
            } else {
                this.lookupSourceSupplier = TrackingLookupSourceSupplier.nonTracking(partitionLookupSource);
            }

            // store lookup source supplier and futures into local variables so they can be used outside of the lock
            lookupSourceSupplier = this.lookupSourceSupplier;
            lookupSourceFutures = ImmutableList.copyOf(this.lookupSourceFutures);
        }
    }

    if (lookupSourceSupplier != null) {
        for (SettableFuture<LookupSource> lookupSourceFuture : lookupSourceFutures) {
            lookupSourceFuture.set(lookupSourceSupplier.getLookupSource());
        }
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.BatchCommit.java

void executeIndividually() {
    DocumentStore store = queue.getStore();
    for (UpdateOp op : ops) {
        SettableFuture<NodeDocument> result = SettableFuture.create();
        try {/*from   www. j av  a2  s .  c o  m*/
            result.set(store.findAndUpdate(NODES, op));
        } catch (Throwable t) {
            result.setException(t);
        }
        results.add(result);
    }
}

From source file:org.opendaylight.controller.cluster.datastore.messages.ReadData.java

@Override
public void processResponse(Object readResponse, SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
    if (ReadDataReply.isSerializedType(readResponse)) {
        ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
        returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
    } else {/*from w  w  w  .j a  v  a2  s  .  com*/
        returnFuture
                .setException(new ReadFailedException("Invalid response reading data for path " + getPath()));
    }
}

From source file:io.prestosql.execution.buffer.OutputBufferMemoryManager.java

public synchronized void updateMemoryUsage(long bytesAdded) {
    Optional<LocalMemoryContext> systemMemoryContext = getSystemMemoryContext();

    // If closed is true, that means the task is completed. In that state,
    // the output buffers already ignore the newly added pages, and therefore
    // we can also safely ignore any calls after OutputBufferMemoryManager is closed.
    // If the systemMemoryContext doesn't exist, the task is probably already
    // aborted, so we can just return (see the comment in getSystemMemoryContext()).
    if (closed || !systemMemoryContext.isPresent()) {
        return;/*from  www .  j  a  va  2  s  .  c  o m*/
    }

    long currentBufferedBytes = bufferedBytes.addAndGet(bytesAdded);
    peakMemoryUsage.accumulateAndGet(currentBufferedBytes, Math::max);
    this.blockedOnMemory = systemMemoryContext.get().setBytes(currentBufferedBytes);
    if (!isBufferFull() && !isBlockedOnMemory() && !bufferBlockedFuture.isDone()) {
        // Complete future in a new thread to avoid making a callback on the caller thread.
        // This make is easier for callers to use this class since they can update the memory
        // usage while holding locks.
        SettableFuture<?> future = this.bufferBlockedFuture;
        notificationExecutor.execute(() -> future.set(null));
        return;
    }
    this.blockedOnMemory.addListener(this::onMemoryAvailable, notificationExecutor);
}