org.elasticsearch.common.lease.Releasables Java Examples

The following examples show how to use org.elasticsearch.common.lease.Releasables. 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: Translog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static Snapshot createSnapshot(TranslogReader... translogs) {
    Snapshot[] snapshots = new Snapshot[translogs.length];
    boolean success = false;
    try {
        for (int i = 0; i < translogs.length; i++) {
            snapshots[i] = translogs[i].newSnapshot();
        }

        Snapshot snapshot = new MultiSnapshot(snapshots);
        success = true;
        return snapshot;
    } finally {
        if (success == false) {
            Releasables.close(snapshots);
        }
    }
}
 
Example #2
Source File: Translog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Writes all operations in the given iterable to the given output stream including the size of the array
 * use {@link #readOperations(StreamInput)} to read it back.
 */
public static void writeOperations(StreamOutput outStream, List<Operation> toWrite) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE);
    try {
        outStream.writeInt(toWrite.size());
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        for (Operation op : toWrite) {
            out.reset();
            final long start = out.position();
            out.skip(RamUsageEstimator.NUM_BYTES_INT);
            writeOperationNoSize(checksumStreamOutput, op);
            long end = out.position();
            int operationSize = (int) (out.position() - RamUsageEstimator.NUM_BYTES_INT - start);
            out.seek(start);
            out.writeInt(operationSize);
            out.seek(end);
            ReleasablePagedBytesReference bytes = out.bytes();
            bytes.writeTo(outStream);
        }
    } finally {
        Releasables.close(out.bytes());
    }

}
 
Example #3
Source File: GatewayAllocator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void setReallocation(final ClusterService clusterService, final RoutingService routingService) {
    this.routingService = routingService;
    clusterService.add(new ClusterStateListener() {
        @Override
        public void clusterChanged(ClusterChangedEvent event) {
            boolean cleanCache = false;
            DiscoveryNode localNode = event.state().nodes().localNode();
            if (localNode != null) {
                if (localNode.masterNode() == true && event.localNodeMaster() == false) {
                    cleanCache = true;
                }
            } else {
                cleanCache = true;
            }
            if (cleanCache) {
                Releasables.close(asyncFetchStarted.values());
                asyncFetchStarted.clear();
                Releasables.close(asyncFetchStore.values());
                asyncFetchStore.clear();
            }
        }
    });
}
 
Example #4
Source File: Netty4Transport.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressForbidden(reason = "debug")
protected void stopInternal() {
    Releasables.close(() -> {
        final List<Tuple<String, Future<?>>> serverBootstrapCloseFutures = new ArrayList<>(serverBootstraps.size());
        for (final Map.Entry<String, ServerBootstrap> entry : serverBootstraps.entrySet()) {
            serverBootstrapCloseFutures.add(
                Tuple.tuple(entry.getKey(), entry.getValue().config().group().shutdownGracefully(0, 5, TimeUnit.SECONDS)));
        }
        for (final Tuple<String, Future<?>> future : serverBootstrapCloseFutures) {
            future.v2().awaitUninterruptibly();
            if (!future.v2().isSuccess()) {
                logger.debug(
                    (Supplier<?>) () -> new ParameterizedMessage(
                        "Error closing server bootstrap for profile [{}]", future.v1()), future.v2().cause());
            }
        }
        serverBootstraps.clear();

        if (clientBootstrap != null) {
            clientBootstrap.config().group().shutdownGracefully(0, 5, TimeUnit.SECONDS).awaitUninterruptibly();
            clientBootstrap = null;
        }
    });
}
 
Example #5
Source File: TcpChannel.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Closes the channels.
 *
 * @param channels to close
 * @param blocking indicates if we should block on channel close
 */
static <C extends TcpChannel> void closeChannels(List<C> channels, boolean blocking) {
    if (blocking) {
        ArrayList<ActionFuture<Void>> futures = new ArrayList<>(channels.size());
        for (final C channel : channels) {
            if (channel.isOpen()) {
                PlainActionFuture<Void> closeFuture = PlainActionFuture.newFuture();
                channel.addCloseListener(closeFuture);
                channel.close();
                futures.add(closeFuture);
            }
        }
        blockOnFutures(futures);
    } else {
        Releasables.close(channels);
    }
}
 
Example #6
Source File: Translog.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Writes all operations in the given iterable to the given output stream including the size of the array
 * use {@link #readOperations(StreamInput, String)} to read it back.
 */
public static void writeOperations(StreamOutput outStream, List<Operation> toWrite) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE);
    try {
        outStream.writeInt(toWrite.size());
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        for (Operation op : toWrite) {
            out.reset();
            final long start = out.position();
            out.skip(Integer.BYTES);
            writeOperationNoSize(checksumStreamOutput, op);
            long end = out.position();
            int operationSize = (int) (out.position() - Integer.BYTES - start);
            out.seek(start);
            out.writeInt(operationSize);
            out.seek(end);
            ReleasablePagedBytesReference bytes = out.bytes();
            bytes.writeTo(outStream);
        }
    } finally {
        Releasables.close(out);
    }

}
 
Example #7
Source File: ParentQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Weight doCreateWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    SearchContext sc = SearchContext.current();
    ChildWeight childWeight;
    boolean releaseCollectorResource = true;
    ParentOrdAndScoreCollector collector = null;
    IndexParentChildFieldData globalIfd = parentChildIndexFieldData.loadGlobal((DirectoryReader)searcher.getIndexReader());
    if (globalIfd == null) {
        // No docs of the specified type don't exist on this shard
        return new BooleanQuery.Builder().build().createWeight(searcher, needsScores);
    }

    try {
        collector = new ParentOrdAndScoreCollector(sc, globalIfd, parentType);
        searcher.search(parentQuery, collector);
        if (collector.parentCount() == 0) {
            return new BooleanQuery.Builder().build().createWeight(searcher, needsScores);
        }
        childWeight = new ChildWeight(this, parentQuery.createWeight(searcher, needsScores), childrenFilter, collector, globalIfd);
        releaseCollectorResource = false;
    } finally {
        if (releaseCollectorResource) {
            // either if we run into an exception or if we return early
            Releasables.close(collector);
        }
    }
    sc.addReleasable(collector, Lifetime.COLLECTION);
    return childWeight;
}
 
Example #8
Source File: HyperLogLogPlusPlus.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void merge(long thisBucket, HyperLogLogPlusPlus other, long otherBucket) {
    Preconditions.checkArgument(p == other.p);
    ensureCapacity(thisBucket + 1);
    if (other.algorithm.get(otherBucket) == LINEAR_COUNTING) {
        final IntArray values = other.hashSet.values(otherBucket);
        try {
            for (long i = 0; i < values.size(); ++i) {
                final int encoded = values.get(i);
                if (algorithm.get(thisBucket) == LINEAR_COUNTING) {
                    collectLcEncoded(thisBucket, encoded);
                } else {
                    collectHllEncoded(thisBucket, encoded);
                }
            }
        } finally {
            Releasables.close(values);
        }
    } else {
        if (algorithm.get(thisBucket) != HYPERLOGLOG) {
            upgradeToHll(thisBucket);
        }
        final long thisStart = thisBucket << p;
        final long otherStart = otherBucket << p;
        for (int i = 0; i < m; ++i) {
            runLens.set(thisStart + i, (byte) Math.max(runLens.get(thisStart + i), other.runLens.get(otherStart + i)));
        }
    }
}
 
Example #9
Source File: HyperLogLogPlusPlus.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
void upgradeToHll(long bucket) {
    ensureCapacity(bucket + 1);
    final IntArray values = hashSet.values(bucket);
    try {
        runLens.fill(bucket << p, (bucket << p) + m, (byte) 0);
        for (long i = 0; i < values.size(); ++i) {
            final int encoded = values.get(i);
            collectHllEncoded(bucket, encoded);
        }
        algorithm.set(bucket);
    } finally {
        Releasables.close(values);
    }
}
 
Example #10
Source File: ReleasableBytesStreamOutput.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void reset() {
    final ByteArray prevBytes = this.bytes;
    super.reset();
    if (prevBytes != this.bytes) {
        // re-create the releasable with the new reference
        releasable = Releasables.releaseOnce(this.bytes);
    }
}
 
Example #11
Source File: BigArrays.java    From crate with Apache License 2.0 5 votes vote down vote up
private <T extends BigArray> T validate(T array) {
    boolean success = false;
    try {
        adjustBreaker(array.ramBytesUsed(), true);
        success = true;
    } finally {
        if (!success) {
            Releasables.closeWhileHandlingException(array);
        }
    }
    return array;
}
 
Example #12
Source File: DLBasedIndexRecoverySourceHandler.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * performs the recovery from the local engine to the target
 */
public RecoveryResponse recoverToTarget() {
    final SnapshotIndexCommit phase1Snapshot;
    phase1Snapshot = shard.snapshotIndex(false);
    
    try {
        recoverLuceneFiles(phase1Snapshot);
    } catch (Throwable e) {
        logger.error("errors while recovery to target", e);
        throw new RecoveryEngineException(shard.shardId(), 1, "phase1 failed", e);
    } finally {
        Releasables.closeWhileHandlingException(phase1Snapshot);
    }
    return response;
}
 
Example #13
Source File: TopKAggregator.java    From elasticsearch-topk-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void doClose() {
    if (this.summaries != null) {
        Releasables.close(this.summaries);
    }
    if (this.bucketOrds != null) {
        Releasables.close(this.bucketOrds);
    }
    if (this.termToBucket != null) {
        Releasables.close(this.termToBucket);
    }
}
 
Example #14
Source File: TransportReplicationAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void doFinish() {
    if (finished.compareAndSet(false, true)) {
        setPhase(task, "finished");
        Releasables.close(indexShardReference);
        final ActionWriteResponse.ShardInfo.Failure[] failuresArray;
        if (!shardReplicaFailures.isEmpty()) {
            int slot = 0;
            failuresArray = new ActionWriteResponse.ShardInfo.Failure[shardReplicaFailures.size()];
            for (Map.Entry<String, Throwable> entry : shardReplicaFailures.entrySet()) {
                RestStatus restStatus = ExceptionsHelper.status(entry.getValue());
                failuresArray[slot++] = new ActionWriteResponse.ShardInfo.Failure(
                    shardId.getIndex(), shardId.getId(), entry.getKey(), entry.getValue(), restStatus, false
                );
            }
        } else {
            failuresArray = ActionWriteResponse.EMPTY;
        }
        finalResponse.setShardInfo(new ActionWriteResponse.ShardInfo(
                totalShards,
                success.get(),
                failuresArray

            )
        );
        try {
            channel.sendResponse(finalResponse);
        } catch (IOException responseException) {
            logger.warn("failed to send error message back to client for action [" + transportReplicaAction + "]", responseException);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("action [{}] completed on all replicas [{}] for request [{}]", transportReplicaAction, shardId, replicaRequest);
        }
    }
}
 
Example #15
Source File: TransportReplicationAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void forceFinishAsFailed(Throwable t) {
    setPhase(task, "failed");
    if (finished.compareAndSet(false, true)) {
        Releasables.close(indexShardReference);
        try {
            channel.sendResponse(t);
        } catch (IOException responseException) {
            logger.warn("failed to send error message back to client for action [{}]", responseException, transportReplicaAction);
            logger.warn("actual Exception", t);
        }
    }
}
 
Example #16
Source File: AbstractBigArray.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doClose() {
    if (recycler != null) {
        Releasables.close(cache);
        cache = null;
    }
}
 
Example #17
Source File: AbstractBigArray.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doClose() {
    if (recycler != null) {
        Releasables.close(cache);
        cache = null;
    }
}
 
Example #18
Source File: BigArrays.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private <T extends BigArray> T validate(T array) {
    boolean success = false;
    try {
        adjustBreaker(array.ramBytesUsed());
        success = true;
    } finally {
        if (!success) {
            Releasables.closeWhileHandlingException(array);
        }
    }
    return array;
}
 
Example #19
Source File: TransportReplicationAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(Releasable releasable) {
    try {
        final ReplicaResult replicaResult = shardOperationOnReplica(request, replica);
        releasable.close(); // release shard operation lock before responding to caller
        final TransportReplicationAction.ReplicaResponse response =
                new ReplicaResponse(replica.getLocalCheckpoint(), replica.getLastSyncedGlobalCheckpoint());
        replicaResult.respond(new ResponseListener(response));
    } catch (final Exception e) {
        Releasables.closeWhileHandlingException(releasable); // release shard operation lock before responding to caller
        AsyncReplicaAction.this.onFailure(e);
    }
}
 
Example #20
Source File: SearchContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void clearReleasables(Lifetime lifetime) {
    if (clearables != null) {
        List<Collection<Releasable>> releasables = new ArrayList<>();
        for (Lifetime lc : Lifetime.values()) {
            if (lc.compareTo(lifetime) > 0) {
                break;
            }
            releasables.add(clearables.removeAll(lc));
        }
        Releasables.close(Iterables.concat(releasables));
    }
}
 
Example #21
Source File: ReleasableBytesStreamOutput.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
void ensureCapacity(long offset) {
    final ByteArray prevBytes = this.bytes;
    super.ensureCapacity(offset);
    if (prevBytes != this.bytes) {
        // re-create the releasable with the new reference
        releasable = Releasables.releaseOnce(this.bytes);
    }
}
 
Example #22
Source File: InternalTestCluster.java    From crate with Apache License 2.0 5 votes vote down vote up
void resetClient() {
    if (closed.get() == false) {
        Releasables.close(nodeClient, transportClient);
        nodeClient = null;
        transportClient = null;
    }
}
 
Example #23
Source File: NettyTransportChannel.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
    if (transport.compress) {
        options = TransportResponseOptions.builder(options).withCompress(transport.compress).build();
    }

    byte status = 0;
    status = TransportStatus.setResponse(status);

    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(transport.bigArrays);
    boolean addedReleaseListener = false;
    try {
        bStream.skip(NettyHeader.HEADER_SIZE);
        StreamOutput stream = bStream;
        if (options.compress()) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.defaultCompressor().streamOutput(stream);
        }
        stream.setVersion(version);
        response.writeTo(stream);
        stream.close();

        ReleasablePagedBytesReference bytes = bStream.bytes();
        ChannelBuffer buffer = bytes.toChannelBuffer();
        NettyHeader.writeHeader(buffer, requestId, status, version);
        ChannelFuture future = channel.write(buffer);
        ReleaseChannelFutureListener listener = new ReleaseChannelFutureListener(bytes);
        future.addListener(listener);
        addedReleaseListener = true;
        transportServiceAdapter.onResponseSent(requestId, action, response, options);
    } finally {
        if (!addedReleaseListener) {
            Releasables.close(bStream.bytes());
        }
    }
}
 
Example #24
Source File: BootstrapProxy.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static void stop() {
    try {
        Releasables.close(INSTANCE.node);
    } finally {
        INSTANCE.keepAliveLatch.countDown();
    }
}
 
Example #25
Source File: GatewayAllocator.java    From crate with Apache License 2.0 4 votes vote down vote up
public void applyFailedShards(final RoutingAllocation allocation, final List<FailedShard> failedShards) {
    for (FailedShard failedShard : failedShards) {
        Releasables.close(asyncFetchStarted.remove(failedShard.getRoutingEntry().shardId()));
        Releasables.close(asyncFetchStore.remove(failedShard.getRoutingEntry().shardId()));
    }
}
 
Example #26
Source File: GatewayAllocator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void applyFailedShards(FailedRerouteAllocation allocation) {
    for (FailedRerouteAllocation.FailedShard shard : allocation.failedShards()) {
        Releasables.close(asyncFetchStarted.remove(shard.shard.shardId()));
        Releasables.close(asyncFetchStore.remove(shard.shard.shardId()));
    }
}
 
Example #27
Source File: GatewayAllocator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void applyStartedShards(StartedRerouteAllocation allocation) {
    for (ShardRouting shard : allocation.startedShards()) {
        Releasables.close(asyncFetchStarted.remove(shard.shardId()));
        Releasables.close(asyncFetchStore.remove(shard.shardId()));
    }
}
 
Example #28
Source File: BigArrays.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected final void doClose() {
    Releasables.close(releasable);
}
 
Example #29
Source File: PageCacheRecycler.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    Releasables.close(true, bytePage, intPage, longPage, objectPage);
}
 
Example #30
Source File: MultiSnapshot.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws ElasticsearchException {
    if (closed.compareAndSet(false, true)) {
        Releasables.close(translogs);
    }
}