文档章节

聊聊Elasticsearch的Releasables

go4it
 go4it
发布于 06/14 23:07
字数 510
阅读 17
收藏 0

本文主要研究一下Elasticsearch的Releasables

Releasable

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/lease/Releasable.java

public interface Releasable extends Closeable {

    @Override
    void close();

}
  • Releasable继承了java.io.Closeable接口

Releasables

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/lease/Releasables.java

public enum Releasables {
    ;

    private static void close(Iterable<? extends Releasable> releasables, boolean ignoreException) {
        try {
            // this does the right thing with respect to add suppressed and not wrapping errors etc.
            IOUtils.close(releasables);
        } catch (IOException e) {
            if (ignoreException == false) {
                throw new UncheckedIOException(e);
            }
        }
    }

    /** Release the provided {@link Releasable}s. */
    public static void close(Iterable<? extends Releasable> releasables) {
        close(releasables, false);
    }

    /** Release the provided {@link Releasable}s. */
    public static void close(Releasable... releasables) {
        close(Arrays.asList(releasables));
    }

    /** Release the provided {@link Releasable}s, ignoring exceptions. */
    public static void closeWhileHandlingException(Iterable<Releasable> releasables) {
        close(releasables, true);
    }

    /** Release the provided {@link Releasable}s, ignoring exceptions. */
    public static void closeWhileHandlingException(Releasable... releasables) {
        closeWhileHandlingException(Arrays.asList(releasables));
    }

    /** Release the provided {@link Releasable}s, ignoring exceptions if <code>success</code> is {@code false}. */
    public static void close(boolean success, Iterable<Releasable> releasables) {
        if (success) {
            close(releasables);
        } else {
            closeWhileHandlingException(releasables);
        }
    }

    /** Release the provided {@link Releasable}s, ignoring exceptions if <code>success</code> is {@code false}. */
    public static void close(boolean success, Releasable... releasables) {
        close(success, Arrays.asList(releasables));
    }

    /** Wrap several releasables into a single one. This is typically useful for use with try-with-resources: for example let's assume
     *  that you store in a list several resources that you would like to see released after execution of the try block:
     *
     *  <pre>
     *  List&lt;Releasable&gt; resources = ...;
     *  try (Releasable releasable = Releasables.wrap(resources)) {
     *      // do something
     *  }
     *  // the resources will be released when reaching here
     *  </pre>
     */
    public static Releasable wrap(final Iterable<Releasable> releasables) {
        return () -> close(releasables);
    }

    /** @see #wrap(Iterable) */
    public static Releasable wrap(final Releasable... releasables) {
        return () -> close(releasables);
    }

    /**
     * Equivalent to {@link #wrap(Releasable...)} but can be called multiple times without double releasing.
     */
    public static Releasable releaseOnce(final Releasable... releasables) {
        final AtomicBoolean released = new AtomicBoolean(false);
        return () -> {
            if (released.compareAndSet(false, true)) {
                close(releasables);
            }
        };
    }
}
  • Releasables提供close、closeWhileHandlingException、wrap、releaseOnce静态方法用于更方便地使用Releasable

实例

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/index/translog/Translog.java

public class Translog extends AbstractIndexShardComponent implements IndexShardComponent, Closeable {

	//......

    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);
        }

    }

    //......
}
  • writeOperations在finally中使用Releasables.close关闭了ReleasableBytesStreamOutput

小结

Releasables提供close、closeWhileHandlingException、wrap、releaseOnce静态方法用于更方便地使用Releasable

doc

© 著作权归作者所有

go4it
粉丝 85
博文 1034
码字总数 988857
作品 0
深圳
私信 提问
聊聊springboot elasticsearch autoconfigure

序 本文主要研究一下springboot elasticsearch autoconfigure ElasticsearchAutoConfiguration spring-boot-autoconfigure-2.1.4.RELEASE-sources.jar!/org/springframework/boot/autoconfi......

go4it
04/17
0
0
CentOS7 部署 ElasticSearch 集群

环境 主机名 IP 操作系统 ES 版本 es227 192.168.1.227 CentOS7.5 6.5.4 es228 192.168.1.228 CentOS7.5 6.5.4 es229 192.168.1.229 CentOS7.5 6.5.4 下载 elasticsearch-6.5.4.tar.gz --- 各......

俊赛潘安-才比管乐
2018/12/27
0
0
聊聊Elasticsearch的SizeBlockingQueue

序 本文主要研究一下Elasticsearch的SizeBlockingQueue SizeBlockingQueue elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/SizeBlockingQueue.java Si......

go4it
06/01
0
0
聊聊Elasticsearch的ConcurrentMapLong

序 本文主要研究一下Elasticsearch的ConcurrentMapLong ConcurrentMapLong elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/ConcurrentMapLong.java Co......

go4it
06/03
0
0
Elasticsearch存储空间不够导致索引只读的解决方法

问题描述 今天发现当天的索引在ES中并没有创建,logstash中不停的报错: 索引变成了只读: es报错,es报错也是索引只读错误 解决办法 经过分析,发现是因为ES所在服务器磁盘空间太低引起,具...

傲娇字符
01/17
0
0

没有更多内容

加载失败,请刷新页面

加载更多

ElasticSearch

一、ElasticSearch简介 1.1 什么是ElasticSearch ElasticSearch,简称es,es是一个开源的高扩展的分布式全文搜索引擎,可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务...

榴莲黑芝麻糊
43分钟前
4
0
候选人体验大奖花落金融壹账通 AI科技赋能智慧HR发展

如何让企业变得“聪明起来”是当前全球范围内众多企业的追求,“聪明的”企业可以从茫茫人海中识别出适合的候选人,并通过给其提供体验良好的人力资源管理服务,从人才层面保证自身的长远发展...

IFTNews
57分钟前
3
0
Element.getBoundingClientRect()

Element.getBoundingClientRect()方法返回元素的大小及其相对于视口的位置。 语法 rectObject = object.getBoundingClientRect(); 值 返回值是一个 DOMRect 对象,这个对象是由该元素的 getC...

JamesView
今天
6
0
设计模式的七大原则

小小小施爷
今天
6
0
Spring Cloud之API 网关Zuul

一、为什么需要网关? 安全性: 1.最主要的一点是网关可以将所有服务的API接口统一聚合,并统一对外暴露。外界系统调用API接口时,都是由网关对外暴露的API接口,外界系统不需要知道微服务系...

尹小狗家的小小喵
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部