Java Code Examples for org.elasticsearch.common.lease.Releasables

The following examples show how to use org.elasticsearch.common.lease.Releasables. These examples are extracted from open source projects. 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 Project: Elasticsearch   Source File: Translog.java    License: 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 Project: Elasticsearch   Source File: Translog.java    License: 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 Project: Elasticsearch   Source File: GatewayAllocator.java    License: 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 Project: crate   Source File: Translog.java    License: 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 5
Source Project: crate   Source File: TcpChannel.java    License: 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 Project: crate   Source File: Netty4Transport.java    License: 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 7
/**
 * 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 8
Source Project: Elasticsearch   Source File: BootstrapProxy.java    License: Apache License 2.0 5 votes vote down vote up
public static void stop() {
    try {
        Releasables.close(INSTANCE.node);
    } finally {
        INSTANCE.keepAliveLatch.countDown();
    }
}
 
Example 9
Source Project: Elasticsearch   Source File: HyperLogLogPlusPlus.java    License: 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 10
Source Project: Elasticsearch   Source File: HyperLogLogPlusPlus.java    License: 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 11
Source Project: crate   Source File: ReleasableBytesStreamOutput.java    License: 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 12
Source Project: Elasticsearch   Source File: SearchContext.java    License: 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 13
Source Project: crate   Source File: ReleasableBytesStreamOutput.java    License: 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 14
Source Project: crate   Source File: InternalTestCluster.java    License: 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 15
Source Project: Elasticsearch   Source File: ParentQuery.java    License: 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 16
Source Project: Elasticsearch   Source File: NettyTransportChannel.java    License: 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 17
Source Project: crate   Source File: TransportReplicationAction.java    License: 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 18
Source Project: Elasticsearch   Source File: BigArrays.java    License: 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 Project: crate   Source File: AbstractBigArray.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected final void doClose() {
    if (recycler != null) {
        Releasables.close(cache);
        cache = null;
    }
}
 
Example 20
Source Project: Elasticsearch   Source File: AbstractBigArray.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected final void doClose() {
    if (recycler != null) {
        Releasables.close(cache);
        cache = null;
    }
}
 
Example 21
Source Project: Elasticsearch   Source File: TransportReplicationAction.java    License: 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 22
Source Project: Elasticsearch   Source File: TransportReplicationAction.java    License: 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 23
Source Project: elasticsearch-topk-plugin   Source File: TopKAggregator.java    License: 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 24
Source Project: crate   Source File: BigArrays.java    License: 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 25
Source Project: Elasticsearch   Source File: PercolateContext.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void doClose() {
    Releasables.close(engineSearcher, docSearcher);
}
 
Example 26
Source Project: Elasticsearch   Source File: GeoBoundsAggregator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void doClose() {
    Releasables.close(tops, bottoms, posLefts, posRights, negLefts, negRights);
}
 
Example 27
Source Project: Elasticsearch   Source File: TopHitsAggregator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void doClose() {
    Releasables.close(topDocsCollectors);
}
 
Example 28
@Override
protected void doClose() {
    Releasables.close(states);
}
 
Example 29
Source Project: crate   Source File: Node.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void close() throws IOException {
    if (lifecycle.started()) {
        stop();
    }
    if (!lifecycle.moveToClosed()) {
        return;
    }

    logger.info("closing ...");
    List<Closeable> toClose = new ArrayList<>();
    StopWatch stopWatch = new StopWatch("node_close");
    toClose.add(() -> stopWatch.start("node_service"));
    toClose.add(nodeService);
    toClose.add(() -> stopWatch.stop().start("http"));
    toClose.add(injector.getInstance(HttpServerTransport.class));
    toClose.add(() -> stopWatch.stop().start("snapshot_service"));
    toClose.add(injector.getInstance(SnapshotsService.class));
    toClose.add(injector.getInstance(SnapshotShardsService.class));
    toClose.add(() -> stopWatch.stop().start("client"));
    Releasables.close(injector.getInstance(Client.class));
    toClose.add(() -> stopWatch.stop().start("indices_cluster"));
    toClose.add(injector.getInstance(IndicesClusterStateService.class));
    toClose.add(() -> stopWatch.stop().start("indices"));
    toClose.add(injector.getInstance(IndicesService.class));
    // close filter/fielddata caches after indices
    toClose.add(injector.getInstance(IndicesStore.class));
    toClose.add(() -> stopWatch.stop().start("routing"));
    toClose.add(injector.getInstance(RoutingService.class));
    toClose.add(() -> stopWatch.stop().start("cluster"));
    toClose.add(injector.getInstance(ClusterService.class));
    toClose.add(() -> stopWatch.stop().start("node_connections_service"));
    toClose.add(injector.getInstance(NodeConnectionsService.class));
    toClose.add(() -> stopWatch.stop().start("discovery"));
    toClose.add(injector.getInstance(Discovery.class));
    toClose.add(() -> stopWatch.stop().start("monitor"));
    toClose.add(nodeService.getMonitorService());
    toClose.add(() -> stopWatch.stop().start("gateway"));
    toClose.add(injector.getInstance(GatewayService.class));
    toClose.add(() -> stopWatch.stop().start("transport"));
    toClose.add(injector.getInstance(TransportService.class));

    for (LifecycleComponent plugin : pluginLifecycleComponents) {
        toClose.add(() -> stopWatch.stop().start("plugin(" + plugin.getClass().getName() + ")"));
        toClose.add(plugin);
    }
    toClose.addAll(pluginsService.filterPlugins(Plugin.class));

    toClose.add(() -> stopWatch.stop().start("thread_pool"));
    // TODO this should really use ThreadPool.terminate()
    toClose.add(() -> injector.getInstance(ThreadPool.class).shutdown());
    toClose.add(() -> {
        try {
            injector.getInstance(ThreadPool.class).awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // ignore
        }
    });

    toClose.add(() -> stopWatch.stop().start("thread_pool_force_shutdown"));
    toClose.add(() -> injector.getInstance(ThreadPool.class).shutdownNow());
    toClose.add(() -> stopWatch.stop());


    toClose.add(injector.getInstance(NodeEnvironment.class));
    toClose.add(injector.getInstance(PageCacheRecycler.class));

    if (logger.isTraceEnabled()) {
        logger.trace("Close times for each service:\n{}", stopWatch.prettyPrint());
    }
    IOUtils.close(toClose);
    logger.info("closed");
}
 
Example 30
Source Project: Elasticsearch   Source File: HyperLogLogPlusPlus.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    Releasables.close(runLens, hashSet.sizes);
}