From 70fc8d9af0574c0ad3fa3da8916078034219f716 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sat, 19 Mar 2011 00:24:20 +0200 Subject: [PATCH] Percolator: When deleting an index, make sure to delete all its percolated queries from the _percolator index, closes #793. --- .../delete/TransportDeleteIndexAction.java | 28 ++++++++++++++++--- .../percolator/SimplePercolatorTests.java | 11 ++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java index 93efe6c7fed..522f1f5ab8f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java @@ -22,14 +22,19 @@ package org.elasticsearch.action.admin.indices.delete; import org.elasticsearch.ElasticSearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.TransportActions; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; +import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingResponse; +import org.elasticsearch.action.admin.indices.mapping.delete.TransportDeleteMappingAction; import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaDataDeleteIndexService; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.percolator.PercolatorService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; @@ -45,10 +50,13 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi private final MetaDataDeleteIndexService deleteIndexService; + private final TransportDeleteMappingAction deleteMappingAction; + @Inject public TransportDeleteIndexAction(Settings settings, TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, MetaDataDeleteIndexService deleteIndexService) { + ThreadPool threadPool, MetaDataDeleteIndexService deleteIndexService, TransportDeleteMappingAction deleteMappingAction) { super(settings, transportService, clusterService, threadPool); this.deleteIndexService = deleteIndexService; + this.deleteMappingAction = deleteMappingAction; } @Override protected String executor() { @@ -76,15 +84,27 @@ public class TransportDeleteIndexAction extends TransportMasterNodeOperationActi return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA, request.indices()); } - @Override protected DeleteIndexResponse masterOperation(DeleteIndexRequest request, ClusterState state) throws ElasticSearchException { + @Override protected DeleteIndexResponse masterOperation(DeleteIndexRequest request, final ClusterState state) throws ElasticSearchException { final AtomicReference responseRef = new AtomicReference(); final AtomicReference failureRef = new AtomicReference(); final CountDownLatch latch = new CountDownLatch(request.indices().length); - for (String index : request.indices()) { + for (final String index : request.indices()) { deleteIndexService.deleteIndex(new MetaDataDeleteIndexService.Request(index).timeout(request.timeout()), new MetaDataDeleteIndexService.Listener() { @Override public void onResponse(MetaDataDeleteIndexService.Response response) { responseRef.set(new DeleteIndexResponse(response.acknowledged())); - latch.countDown(); + // YACK, but here we go: If this index is also percolated, make sure to delete all percolated queries from the _percolator index + IndexMetaData percolatorMetaData = state.metaData().index(PercolatorService.INDEX_NAME); + if (percolatorMetaData != null && percolatorMetaData.mappings().containsKey(index)) { + deleteMappingAction.execute(new DeleteMappingRequest(PercolatorService.INDEX_NAME).type(index), new ActionListener() { + @Override public void onResponse(DeleteMappingResponse deleteMappingResponse) { + latch.countDown(); + } + + @Override public void onFailure(Throwable e) { + latch.countDown(); + } + }); + } } @Override public void onFailure(Throwable t) { diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java index adf70505581..b0d9efd4824 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java @@ -175,6 +175,17 @@ public class SimplePercolatorTests extends AbstractNodesTests { .execute().actionGet(); assertThat(percolate.matches().size(), equalTo(1)); } + + logger.info("--> delete the index"); + + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + + logger.info("--> make sure percoalted queries for it have been deleted as well"); + assertThat(client.prepareCount("_percolator").setQuery(matchAllQuery()).execute().actionGet().count(), equalTo(0l)); } @Test public void percolateOnIndexOperation() throws Exception {