首页 > 其他分享 >es 支持实时的数据读取吗

es 支持实时的数据读取吗

时间:2023-10-16 16:37:28浏览次数:28  
标签:读取 translog 写入 实时 Translog add Location new es

通常我们说 es 是近实时的搜索服务,指的是 es 写入一条数据后,默认 1 s 后才可以搜索到,但我们在实际使用过程中指定 id 可以进行实时的查询。客户端使用 GetRequest 发起的查询默认是实时的,分片会从 translog 中读取数据并返回,为什么通过 id 查询就是实时的呢?

es 在写入数据时,首先写入 IndexWriter 的内存数据结构中,然后写入 translog 文件中,紧接着等待数据同步到副本分片中,副本分片执行上述相同的写入过程,最后才返回。

客户端写入返回后,立即指定 id 发起查询请求,此时查询请求会落到主分片或者副本分片上,很大可能会从 translog 中查询数据并返回,由此保证了实时的查询过程。

由此我们知道,translog 是实时读取的原理所在,某种程度上 translog 类似于 MySQL 的 redolog,算是一种双写,lucene commit 是很重的操作,在没有 commit 之前,将数据写入到 translog 中进行保存,前提是 translog 的写入相较于 lucene 的 commit 是非常轻量级的,我们可以认为写 translog 就是顺序追加文件,而且文件只需要保存短时间的内容,es 一旦执行完 refresh 操作,即可以删除 translog 文件。

1. 借助一个单元测试理解 translog 的 api

// org.elasticsearch.index.translog.TranslogTests#testReadLocation
public void testReadLocation() throws IOException {
    ArrayList<Translog.Operation> ops = new ArrayList<>();
    ArrayList<Translog.Location> locs = new ArrayList<>();
    locs.add(addToTranslogAndList(translog, ops, new Translog.Index("1", 0, primaryTerm.get(), new byte[] { 1 })));
    locs.add(addToTranslogAndList(translog, ops, new Translog.Index("2", 1, primaryTerm.get(), new byte[] { 1 })));
    locs.add(addToTranslogAndList(translog, ops, new Translog.Index("3", 2, primaryTerm.get(), new byte[] { 1 })));
    int i = 0;
    for (Translog.Operation op : ops) {
        assertEquals(op, translog.readOperation(locs.get(i++)));
    }
    assertNull(translog.readOperation(new Location(100, 0, 0)));
}

private Location addToTranslogAndList(Translog translog, List<Translog.Operation> list, Translog.Operation op) throws IOException {
    list.add(op);
    return translog.add(op);
}

该测试用例,向 translog 中写入 3 条数据,然后又从 translog 中读出来,进行比较,由此追溯到了 translog 的读写 api:

// org.elasticsearch.index.translog.Translog
/**
 * Adds an operation to the transaction log.
 *
 * @param operation the operation to add
 * @return the location of the operation in the translog
 * @throws IOException if adding the operation to the translog resulted in an I/O exception
 */
public Location add(final Operation operation) throws IOException 

/**
 * Reads and returns the operation from the given location if the generation it references is still available. Otherwise
 * this method will return <code>null</code>.
 */
public Operation readOperation(Location location) throws IOException 

 

2. 然后回到最初的问题,es 的 translog 到底是怎么支持实时读的呢?

大胆猜测,刚刚写入的数据非常热,此时的元数据信息还保留着,即 IndexVersionValue 对象,然后根据 Location 从 translog 读取

final class IndexVersionValue extends VersionValue {
    private static final long RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(IndexVersionValue.class);
    private final Translog.Location translogLocation;
    IndexVersionValue(Translog.Location translogLocation, long version, long seqNo, long term) {
        super(version, seqNo, term);
        this.translogLocation = translogLocation;
    }

    @Override
    public long ramBytesUsed() {
        return RAM_BYTES_USED + RamUsageEstimator.shallowSizeOf(translogLocation);
    }

    @Override
    public Translog.Location getLocation() {
        return translogLocation;
    }
}

 

标签:读取,translog,写入,实时,Translog,add,Location,new,es
From: https://www.cnblogs.com/allenwas3/p/17767424.html

相关文章

  • Testing Round 16 (Unrated) B. Square?
    给定一个矩形,然后切成两个矩形。尺寸分别为\(a\timesb\),\(c\timesd\)。你需要确定开始的矩形是否可能是个正方形。假设初始矩形为正方形,则两个小矩形的长边是正方形的边长。不妨让\(a\geqb,c\geqd\)。只需判断\(a=c,a=b+d\)是否成立即可。view#includ......
  • PostgreSql使用中遇到的问题
    PostgreSql使用中遇到的问题:持续更新注意:pq中null和空字符串是完全没啥关系的。如果要判断字段是否有值,在不确定是null或者是空字符串时,就用length(field)>01.如果ddl中定义的字段名是大写的,那么查询字段名就要带""  eg:SELECT"LegalOrgID","PID","LegalOrgSNam......
  • kubernetes集群中pod访问外网丢包严重问题排查:mtu值设置不对
    kubernetes集群中pod访问外网丢包严重问题排查:mtu值设置不对问题描述和初步判断k8s中部署的数据中台调用指云(open.imzhiyun.com)的sdk接口,调用了23次,成功了3次,其余20次都失败。我这边通过在宿主机上进行tcpdump抓包抓包命令:1.查出调用sdk的容器名称2.查出该容器所在宿主机......
  • Perceptual Losses 风格迁移论文复现小记
    看了一篇李飞飞组的论文PerceptualLossesforReal-TimeStyleTransferandSuper-Resolution。论文地址为:https://arxiv.org/pdf/1603.08155.pdf))想去找找代码复现一下。原文没有提供代码,就只有找找别人按照论文细节实现的代码。不过但是论文是2016年的,距离现在2023年已经......
  • Build ASP.NET Core applications deployed as Linux containers into an AKS/Kuberne
    原文:https://learn.microsoft.com/en-us/dotnet/architecture/containerized-lifecycle/design-develop-containerized-apps/build-aspnet-core-applications-linux-containers-aks-kubernetesAzureKubernetesServices(AKS)isAzure'smanagedKubernetesorchestrat......
  • RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.d
    问题:服务器上多块卡,使用其中一张训练的模型,在本地预测的时候报错。解决:在torch.load中加入map_location,指定一块卡 ......
  • DevExpress.18.1使用
    1.DevExpress.XtraGrid.Views.Tile.TileView 使用DataTable作为数据源时,默认选择第一行使用  tile_Reason.Columns.View.FocusedRowHandle=0; 2.绑定数据源后,能单击能查到数据,但页面不显示,需要绑定控件的_ItemCustomize方法privatevoidtile_Reason_ItemCustomize(o......
  • Python处理Request请求
    一、HTTP知识:request请求方式有GET/POST/PUT/PATCH/DELETE/COPY/HEAD/OPTIONS/LINK/VIEW等常用的request请求有:get和post两种形式。1.GET用于获取资源,当采用GET方式请求指定资源时,被访问的资源经服务器解析后立即返回响应内容。通常以GET方式请求特定资源时,请求中不应该......
  • Bulk RNA-seq process
    目的:对illumina数据进行处理,利用RNA-Seq发现新的RNA变体和剪接位点,或量化mRNA以进行基因表达分析等。对两组或多组样本的转录组数据,通过差异表达分析和对所发现的差异表达基因集合进行功能富集分析以推断生物学功能。数据准备:数据下载:Human genome(GRCh38/hg3):Indexo......
  • 报错:Could not resolve view with name 'xxx' in servlet with name 'dispatcherServl
    报错:Servlet.service()forservlet[dispatcherServlet]incontextwithpath[]threwexception[Couldnotresolveviewwithname'xxx'inservletwithname'dispatcherServlet']withrootcauseCouldnotresolveviewwithname'xxx&......