|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +categories: [ElasticSearch] |
| 4 | +description: none |
| 5 | +keywords: ElasticSearch |
| 6 | +--- |
| 7 | +# ElasticSearch源码删除索引 |
| 8 | + |
| 9 | +## TransportDeleteIndexAction |
| 10 | +Master节点的操作大多需要继承TransportMasterNodeAction类,具体的执行流程看实现类的masterOperation方法即可。 |
| 11 | +``` |
| 12 | +protected void masterOperation(final DeleteIndexRequest request, final ClusterState state, |
| 13 | + final ActionListener<AcknowledgedResponse> listener) { |
| 14 | + //获取要删除的索引 |
| 15 | + final Set<Index> concreteIndices = new HashSet<>(Arrays.asList(indexNameExpressionResolver.concreteIndices(state, request))); |
| 16 | + if (concreteIndices.isEmpty()) { |
| 17 | + listener.onResponse(new AcknowledgedResponse(true)); |
| 18 | + return; |
| 19 | + } |
| 20 | +
|
| 21 | + //构建删除索引的请求 |
| 22 | + DeleteIndexClusterStateUpdateRequest deleteRequest = new DeleteIndexClusterStateUpdateRequest() |
| 23 | + .ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()) |
| 24 | + .indices(concreteIndices.toArray(new Index[concreteIndices.size()])); |
| 25 | +
|
| 26 | + //调用MetaDataDeleteIndexService服务执行删除索引 |
| 27 | + deleteIndexService.deleteIndices(deleteRequest, new ActionListener<ClusterStateUpdateResponse>() { |
| 28 | +
|
| 29 | + @Override |
| 30 | + public void onResponse(ClusterStateUpdateResponse response) { |
| 31 | + listener.onResponse(new AcknowledgedResponse(response.isAcknowledged())); |
| 32 | + } |
| 33 | +
|
| 34 | + @Override |
| 35 | + public void onFailure(Exception t) { |
| 36 | + logger.debug(() -> new ParameterizedMessage("failed to delete indices [{}]", concreteIndices), t); |
| 37 | + listener.onFailure(t); |
| 38 | + } |
| 39 | + }); |
| 40 | +} |
| 41 | +``` |
| 42 | +这里和创建索引的流程类似,依旧是调用MetaDataDeleteIndexService服务执行索引删除,外部留下监听器根据删除的结果响应不同的请求。 |
| 43 | + |
| 44 | +## MetaDataDeleteIndexService |
| 45 | +这里是真正执行索引删除的服务,与MetaDataCreateIndexService功能是一样的,核心作用: |
| 46 | + |
| 47 | +1.提交删除索引的任务; |
| 48 | + |
| 49 | +2.定义具体删除索引任务的执行流程; |
| 50 | + |
| 51 | +## 任务提交 |
| 52 | +与创建索引类似,这里向clusterService提交一个删除索引的任务,具体的任务是实现AckedClusterStateUpdateTask的内部类,之前的流程已经分析过,最终会调用execute方法。 |
| 53 | +``` |
| 54 | +public void deleteIndices(final DeleteIndexClusterStateUpdateRequest request, |
| 55 | + final ActionListener<ClusterStateUpdateResponse> listener) { |
| 56 | + ...................................... |
| 57 | + clusterService.submitStateUpdateTask("delete-index " + Arrays.toString(request.indices()), |
| 58 | + new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, listener) { |
| 59 | +
|
| 60 | + @Override |
| 61 | + protected ClusterStateUpdateResponse newResponse(boolean acknowledged) { |
| 62 | + return new ClusterStateUpdateResponse(acknowledged); |
| 63 | + } |
| 64 | +
|
| 65 | + @Override |
| 66 | + public ClusterState execute(final ClusterState currentState) { |
| 67 | + return deleteIndices(currentState, Sets.newHashSet(request.indices())); |
| 68 | + } |
| 69 | + }); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## 集群元数据 |
| 74 | +删除索引的数据封装在DeleteIndexClusterStateUpdateRequest类中,这里并没有真正的从磁盘删除索引,只是删除Master节点索引的元数据。 |
| 75 | + |
| 76 | +删除的基本流程是,从Request获取对应的索引名称,并加入到ClusterState的已删除索引集合,更新元数据,最后执行路由。 |
| 77 | +``` |
| 78 | +/** |
| 79 | + * Delete some indices from the cluster state. |
| 80 | + */ |
| 81 | +public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) { |
| 82 | + //获取待删除的索引 |
| 83 | + final MetaData meta = currentState.metaData(); |
| 84 | + final Set<Index> indicesToDelete = indices.stream().map(i -> meta.getIndexSafe(i).getIndex()).collect(toSet()); |
| 85 | +
|
| 86 | + // Check if index deletion conflicts with any running snapshots |
| 87 | + Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentState, indicesToDelete); |
| 88 | + if (snapshottingIndices.isEmpty() == false) { |
| 89 | + throw new SnapshotInProgressException("Cannot delete indices that are being snapshotted: " + snapshottingIndices + |
| 90 | + ". Try again after snapshot finishes or cancel the currently running snapshot."); |
| 91 | + } |
| 92 | +
|
| 93 | + RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable()); |
| 94 | + MetaData.Builder metaDataBuilder = MetaData.builder(meta); |
| 95 | + ClusterBlocks.Builder clusterBlocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks()); |
| 96 | +
|
| 97 | + //已经删除索引的集合(索引删除是异步进行) |
| 98 | + final IndexGraveyard.Builder graveyardBuilder = IndexGraveyard.builder(metaDataBuilder.indexGraveyard()); |
| 99 | + final int previousGraveyardSize = graveyardBuilder.tombstones().size(); |
| 100 | + //删除索引主要是修改clusterstate元数据,删数其中的index。 |
| 101 | + for (final Index index : indices) { |
| 102 | + String indexName = index.getName(); |
| 103 | + logger.info("{} deleting index", index); |
| 104 | + routingTableBuilder.remove(indexName); |
| 105 | + clusterBlocksBuilder.removeIndexBlocks(indexName); |
| 106 | + metaDataBuilder.remove(indexName); |
| 107 | + } |
| 108 | + // add tombstones to the cluster state for each deleted index |
| 109 | + // 待删除索引加入已删除索引集合 |
| 110 | + final IndexGraveyard currentGraveyard = graveyardBuilder.addTombstones(indices).build(settings); |
| 111 | + metaDataBuilder.indexGraveyard(currentGraveyard); // the new graveyard set on the metadata |
| 112 | + logger.trace("{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}.", |
| 113 | + graveyardBuilder.getNumPurged(), previousGraveyardSize, currentGraveyard.getTombstones().size()); |
| 114 | +
|
| 115 | + MetaData newMetaData = metaDataBuilder.build(); |
| 116 | + ClusterBlocks blocks = clusterBlocksBuilder.build(); |
| 117 | +
|
| 118 | + // update snapshot restore entries |
| 119 | + // 正在resotre的Index |
| 120 | + ImmutableOpenMap<String, ClusterState.Custom> customs = currentState.getCustoms(); |
| 121 | + final RestoreInProgress restoreInProgress = currentState.custom(RestoreInProgress.TYPE); |
| 122 | + if (restoreInProgress != null) { |
| 123 | + RestoreInProgress updatedRestoreInProgress = RestoreService.updateRestoreStateWithDeletedIndices(restoreInProgress, indices); |
| 124 | + if (updatedRestoreInProgress != restoreInProgress) { |
| 125 | + ImmutableOpenMap.Builder<String, ClusterState.Custom> builder = ImmutableOpenMap.builder(customs); |
| 126 | + builder.put(RestoreInProgress.TYPE, updatedRestoreInProgress); |
| 127 | + customs = builder.build(); |
| 128 | + } |
| 129 | + } |
| 130 | +
|
| 131 | + //路由广播,对shard进行重新分配。 |
| 132 | + return allocationService.reroute( |
| 133 | + ClusterState.builder(currentState) |
| 134 | + .routingTable(routingTableBuilder.build()) |
| 135 | + .metaData(newMetaData) |
| 136 | + .blocks(blocks) |
| 137 | + .customs(customs) |
| 138 | + .build(), |
| 139 | + "deleted indices [" + indices + "]"); |
| 140 | +} |
| 141 | +``` |
| 142 | +到这里删除所有也只是完成了Master节点元数据的更新,Data节点对应的索引数据并没有删除,具体的删除流程索引管理--集群状态发布。 |
0 commit comments