diff --git a/CHANGES.txt b/CHANGES.txt index 1337db5d0731..3217b79426bf 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,10 @@ Changes for Crate Unreleased ========== + - Fix: In some cases a ``DROP TABLE`` statment does not delete all + partitions. Now it's possible to repeat the statement in order + to delete the orphaned partitions. + - Added the IF NOT EXISTS clause to the CREATE TABLE statement 2015/05/07 0.49.0 diff --git a/sql/src/main/java/io/crate/executor/transport/task/DropTableTask.java b/sql/src/main/java/io/crate/executor/transport/task/DropTableTask.java index cb375866d171..02d426da2f03 100644 --- a/sql/src/main/java/io/crate/executor/transport/task/DropTableTask.java +++ b/sql/src/main/java/io/crate/executor/transport/task/DropTableTask.java @@ -25,6 +25,7 @@ import io.crate.metadata.PartitionName; import io.crate.metadata.table.TableInfo; import io.crate.planner.node.ddl.DropTableNode; +import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; @@ -34,6 +35,7 @@ import org.elasticsearch.action.admin.indices.template.delete.TransportDeleteIndexTemplateAction; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; +import org.elasticsearch.indices.IndexTemplateMissingException; import java.util.List; import java.util.Locale; @@ -79,7 +81,13 @@ public void onResponse(DeleteIndexTemplateResponse response) { @Override public void onFailure(Throwable e) { - result.setException(e); + e = ExceptionsHelper.unwrapCause(e); + if (e instanceof IndexTemplateMissingException && !tableInfo.partitions().isEmpty()) { + logger.warn(e.getMessage()); + deleteESIndex(tableInfo.ident().esName()); + } else { + result.setException(e); + } } }); } else { diff --git a/sql/src/test/java/io/crate/integrationtests/PartitionedTableIntegrationTest.java b/sql/src/test/java/io/crate/integrationtests/PartitionedTableIntegrationTest.java index 15a41677c128..a18c96139b17 100644 --- a/sql/src/test/java/io/crate/integrationtests/PartitionedTableIntegrationTest.java +++ b/sql/src/test/java/io/crate/integrationtests/PartitionedTableIntegrationTest.java @@ -42,6 +42,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.MatchAllQueryBuilder; +import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; import org.hamcrest.core.Is; import org.junit.After; @@ -1793,6 +1794,25 @@ public void testFetchPartitionedTable() throws Exception { assertThat(response.rowCount(), is(lessThanOrEqualTo(1L))); } + + @Test + public void testDeleteOrphanedPartitions() throws Throwable { + execute("create table foo (name string, p string) partitioned by (p) with (number_of_replicas=0, refresh_interval = 0)"); + ensureYellow(); + execute("insert into foo (name, p) values (?, ?)", new Object[]{"Marvin", 1}); + execute("refresh table foo"); + + String templateName = PartitionName.templateName(null, "foo"); + client().admin().indices().prepareDeleteTemplate(templateName).execute().actionGet(); + waitNoPendingTasksOnAll(); + execute("select * from sys.shards where table_name = 'foo'"); + assertThat(response.rowCount(), CoreMatchers.is(5L)); + execute("drop table foo"); + + execute("select * from sys.shards where table_name = 'foo'"); + assertThat(response.rowCount(), CoreMatchers.is(0L)); + } + @After @Override public void tearDown() throws Exception {