fsync sync

fsync/sync
sync is a standard system call in the Unix operating system, which commits to disk all data in the kernel filesystem buffers,data which has been scheduled for writing via low-level I/O system calls.Higher-level I/O layers such as stdio may maintain separate buffers of their own.The related system call fsync() commits just the buffered data relating to a specified file descriptor.[1] fdatasync() is also available to write out just the changes made to the data in the file, and not necessarily the file's related metadata.

inside a shard

inverted index

  1. 词汇列表
  2. 相似度算法
  3. 词汇:大小写,单数复数,同义词

每个被索引的字段都有倒排索引
every indexed field in a JSON document has its own inverted index.

写到磁盘上的inverted index是不可变的。不可变的好处有:
  1. 不需要加锁,不用考虑更新时的多线程问题
  2. 当index被写入filesystem cache的时候,就一直在cache里了,因为他是不变的,从内存里读取而不用访问磁盘,
    提高性能
  3. 其他的缓存,比如filter cache,也将是一直有效的,因为index不变,所以其他的缓存也不需要在数据变化是跟着变化
  4. 允许数据压缩(Writing a single large inverted index allows the data to be compressed, reducing costly disk I/O and the amount of RAM needed to cache the index)
inverted index不可变的缺点
  1. 如果需要让一个新文档可以被搜索,就要重建整个索引,这就限制了一个index的数据容量和索引重建的频率
如何让索引可以更新,任然具有不可变的优势
  1. 使用更多的索引
  2. per-segment search的概念,一个片段是一个倒排索引。
    一个shard有多个segments
  3. 一个分片就是一个 Lucene index,es的一个索引是多个分片的集合
  4. 新文档存在缓存里的indexing buffer,每隔一段时间提交一次
indexing buffer提交时做什么
  1. A Lucene index 的内存缓冲区里的新文档准备提交
  2. 一个新的片段(a supplementary inverted index)写入到磁
  3. 这个new segment加入到commit point,缓冲区清空。
    commit point lists all known segments
  4. 所有在filesystem cache的数据被写入到文件(持久化)

查询时,所有的segment会被轮流查询,

segment是不可变的,没法从老的片段删除或添加文档。所以每个commit point有一个.del文件,
里面记录了哪个片段的哪个文档被删除了,当文档更新时,老版本的文档被表示删除,新版本的文档索引到新segment里

如果让变化的文档更快的searchable

瓶颈在于磁盘,提交一个new segment到磁盘需要fsync,fsync是昂贵的。
在es和磁盘直接的是filesystem cache,new segment先写入filesystemcache,之后在写入磁盘。
这个过程叫refresh,分片默认每秒refresh,配置参数refresh_interval,

PUT /my_logs
{
"settings": {
"refresh_interval": "30s"
}
}

这个参数可以动态的修改。可以在建立索引时关闭refresh,使用时打开

PUT /my_logs/_settings
{ "refresh_interval": -1 } PUT /my_logs/_settings
{ "refresh_interval": "1s" }

持久化

full commit: 将在filesystem cache里的segment写入磁盘,commit point。用在失败后恢复
commit point lists all known segments,es在启动和重新打卡索引时,通过commit point知道segments属于哪个shards.
当full commit时文件改变了怎么办?

translog

translog记录了es发生的每个行为。
文档先添加到in-memory buffer, 再添加到translog.
refresh的时候,buffer清空,translog不变。
The docs in the in-memory buffer are written to a new segment, without an fsync.
The segment is opened to make it visible to search.
The in-memory buffer is cleared.

full commit

flush + create new translog
当translog太大或者一定时间后,index is flushed,创建新的translog.

  1. Any docs in the in-memory buffer are written to a new segment.
  2. The buffer is cleared.
  3. A commit point is written to disk.
  4. The filesystem cache is flushed with an fsync.
  5. The old translog is delete

es启动时,通过last commit point来恢复segments,接着重新执行translog里记录的操作
(When starting up, Elasticsearch will use the last commit point to recover known segments from disk, and will then replay all operations in the translog to add the changes that happened after the last commit.)

translog还被用来做实时的CRUD,当需要通过id retrieve, update, or delete a document,会先检查translog有没有更改,再去segment取文档。这就提供了实时访问最新的文档的方式。

full commit and truncating the translog is called flush.分片默认30分钟flush或当translog太大的时候

translog
  1. index.translog.sync_interval:
    How often the translog is fsynced to disk and committed, regardless of write operations. Defaults to 5s.
  2. index.translog.durability:
    每次索引,删除,更新,或批量请求之后,是否需要fsync和提交translog,
    request: (default) fsync and commit after every write request((e.g. index, delete, update, bulk).). In the event of hardware failure, all acknowledged writes will already have been committed to disk
    async: (lose sync_interval's worth of data )fsync and commit in the background every sync_interval. In the event of hardware failure, all acknowledged writes since the last automatic commit will be discarded.
  3. index.translog.fs.type:
    Whether to buffer writes to the transaction log in memory or not. This setting accepts the following parameters:
    buffered: (default) Translog writes first go to a 64kB buffer in memory, and are only written to the disk when the buffer is full, or when an fsync is triggered by a write request or the sync_interval.
    simple: Translog writes are written to the file system immediately, without buffering. However, these writes will only be persisted to disk when an fsync and commit is triggered by a write request or the sync_interval.

segment merge

自动refresh每秒就创建一个segment,每次搜索都会查询每个segment,so,segment越多查询越慢。
es会在后台合并segment,小的合并到大的,这个时候那些已经删除的旧的文档就会从文件系统清除。删除的文档和旧版本的修改过的文档不会复制到新的大segment里
合并结束之后:

  1. The new segment is flushed to disk.
  2. A new commit point is written that includes the new segment and excludes the old, smaller 3. segments.
  3. The new segment is opened for search.
  4. The old segments are deleted.
optimize api

强制合并的api。强制分片的segment数量小于max_num_segments 参数。不应该在活跃的索引上使用。

POST /logstash-2014-10/_optimize?max_num_segments=1

optimize 出发的merge是完全没有限制的,他们可能用掉所有的I/O, If you plan on optimizing an index, you should use shard allocation (see Migrate Old Indices) to first move the index to a node where it is safe to run.

inside a shard的更多相关文章

  1. 可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询

    https://www.elastic.co/guide/en/elasticsearch/guide/current/getting-started.html Elasticsearch is a ...

  2. cacheed 限制 4节点 3000万 es 批量删除 shell脚本练习 elasticsearch_action

    文件分割 "www.laiwunews.cn/xinxi/25324717.html""www.zznews.cn/xinxi/10411214.html"&q ...

  3. vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法

    ---恢复内容开始--- 最近在安装了vsftpd后 添加了虚拟账户后 新建用户 为新用户创立独立的工作目录 因为虚拟用户在工作目录需要上传文件 所以必须拥有此目录的W权限,但每当给此目录加上W权限后 ...

  4. 解决vsftpd的refusing to run with writable root inside chroot错误

    参考 http://www.cnblogs.com/CSGrandeur/p/3754126.html 在Ubuntu下用 vsftpd 配置FTP服务器,配置 “ sudo chmod a-w /h ...

  5. MongoDBV3.0.7版本(shard+replica)集群的搭建及验证

    集群的模块介绍: 从MongoDB官方给的集群架构了解,整个集群主要有4个模块:Config Server.mongs. shard.replica set: Config Server:用来存放集群 ...

  6. 《Inside UE4》目录

    <Inside UE4>目录 InsideUE4 UE4无疑是非常优秀的世界上最顶尖的引擎之一,性能和效果都非常出众,编辑器工作流也非常的出色,更难得宝贵的是完全的开源让我们有机会去从中吸 ...

  7. 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  8. 《Inside UE4》-0-开篇

    <Inside UE4>-0-开篇 InsideUE4   前言 VR行业发展是越来越火热了,硬件设备上有HTC VIVE,Oculus rift,PS VR,各种魔镜:平台上有Steam ...

  9. 500 OOPS: vsftpd: refusing to run with writable root inside chroot()

    Ubuntu 12.04 64bit系统下安装的vsftpd,在登陆时提示500 OOPS: vsftpd: refusing to run with writable root inside chr ...

随机推荐

  1. 团队项目-第五次Scrum 会议

    时间:10.31 时长:30分钟 地点:教室(主南201) 工作情况 团队成员 已完成任务 待完成任务 解小锐 修复在接受任务时,前端和后端对接中的bug 完成员工信息的简单初始化 陈鑫 完成hire ...

  2. AtomicIntegerFieldUpdater使用

    假设现在有这样的一个场景: 一百个线程同时对一个int对象进行修改,要求只能有一个线程可以修改. 看看下面程序是否正确: private static int a = 100; private sta ...

  3. php开发中处理emoji表情和颜文字的兼容问题

    背景:随着手机的普及,现在移动开发很火爆,已经远远超过了pc端.在移动设备经常会发生用户发送的内容中包含emoji表情,在显示时就是乱码.一般是因为Mysql表设计时,都是用UTF8字符集的.把带有e ...

  4. Flink之状态之状态获取

    1.什么是状态 对于任何一个操作,都可以被看成是一个函数,比如y=f(x),如果对于同一个x的任何一次输入,得到的y都是相同的,则可以认为这个函数是无状态,否则,这个函数就是有状态的.Flink的一大 ...

  5. AJAX基本演示使用

    Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...

  6. [洛谷P2824][HEOI2016/TJOI2016]排序

    题目大意:一个全排列,两种操作: 1. $0\;l\;r:$把$[l,r]$升序排序2. $1\;l\;r:$把$[l,r]$降序排序 最后询问第$k$位是什么 题解:二分答案,把比这个数大的赋成$1 ...

  7. [洛谷P1337][JSOI2004]平衡点 / 吊打XXX

    题目大意:有$n$个重物,每个重物系在一条绳子上.所有绳子系在一起,问绳结最终平衡于何处. 题解:$NOIP$前学学模拟退火,但发现我脸好黑啊... 卡点:脸黑 C++ Code: #include ...

  8. VB托盘图标不响应WM_MOUSEMOVE的原因及解决方法

    文章参考地址:http://blog.csdn.net/txh0001/article/details/38265895:http://bbs.csdn.net/topics/330106030 网上 ...

  9. [bzoj 2733]启发式合并权值线段树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 平衡树待学习.从一个博客学到了合并权值线段树的姿势:http://blog.csdn ...

  10. [hdu 4348]区间修改区间查询可持久化线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348 一开始把lazy标记给push_down了,后来发现这样会让持久化变乱,然后想到不用push_d ...