1.TransportSingleItemBulkWriteAction的doExecute方法处,构建单个item请求参数,该类已过期,但是5.x版本request还在使用。
@Override
protected void doExecute(Task task, final Request request, final ActionListener<Response> listener) {
bulkAction.execute(task, toSingleItemBulkRequest(request), wrapBulkResponse(listener));
}
2.TransportBulkAction的protected void doExecute(Task task, BulkRequest bulkRequest, ActionListener listener)方法将bulk的参数和索引一一对应。
Step 1: collect all the indices in the request
Step 2: filter that to indices that don't exist and we can create. At the same time build a map of indices we can't create
Step 3: create all the indices that are missing, if there are any missing. start the bulk after all the creates come back.
- TransportBulkAction的doRun() 方法判断bulk是创建还是增删改查操作。
4.首先,查看所有请求并创建ShardId - > Operations映射
5.TransportReplicationAction个人觉得这个名字起得不好,让人以为是只会在副本上执行相关功能的意思。
其实包含主节点等所有相关操作。
class name | 功能 |
---|---|
ReroutePhase | 将请求路由到primary 分片所在的节点 |
PrimaryPhase | 在主分片执行任务 |
ReplicationPhase | 在所有的replica分片上执行任务 |
1.第一步路由到主节点
@Override
protected void doExecute(Task task, Request request, ActionListener<Response> listener) {
new ReroutePhase((ReplicationTask) task, request, listener).run();
}
2.执行sendRequest
private void performAction(final DiscoveryNode node, final String action, final boolean isPrimaryAction,
final TransportRequest requestToPerform)
3.acquirePrimaryShardReference这里执行获取锁,然后在onresponse里面执行创建。
4.先插入主节点,再插入其他节点
5.插入主节点操作,
6.InternalEngine 类里面的planIndexingAsPrimary 方法返回几个插入逻辑判断,这个方法最终返回一个 IndexingStrategy,即一个索引的策略,总共有如下几个策略:
optimizedAppendOnly
skipDueToVersionConflict
processNormally
overrideExistingAsIfNotThere
skipAsStale
总结:
1.es 在 index 或者 create 的时候并不会 get 整个文档,而是只会获取文档的版本号做对比,而这个开销不会很大。
updateByQuery
1.用法
post http://127.0.0.1:9200/promotionsku_v3/promotionsku/_update_by_query
{
"script": {
"source": "ctx._source.unitCode+=\"zcy\""
},
"query": {
"bool": {
"must": [
{
"exists": {
"field": "unitCodes"
}
},
{
"term":{
"unitCodes":6926309213221
}
}
]
}
}
}
2.源码:
目标action:AbstractAsyncBulkByScrollAction 和 TransportUpdateByQueryAction
核心方法:apply
script语法:painless
以下是调用链,倒序展示。