From b4539223196c8136fec9632a389f08126475d2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20L=C3=A9aut=C3=A9?= Date: Tue, 4 Nov 2014 14:35:39 -0800 Subject: [PATCH] add tests for task and task status storage --- .../SQLMetadataStorageActionHandler.java | 8 +- ...QLMetadataStorageActionHandlerFactory.java | 4 +- .../SQLMetadataStorageActionHandlerTest.java | 152 ++++++++++++++++++ 3 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java diff --git a/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java b/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java index de260965bca..227f78efac6 100644 --- a/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java +++ b/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandler.java @@ -37,7 +37,6 @@ import org.joda.time.DateTime; import org.skife.jdbi.v2.FoldController; import org.skife.jdbi.v2.Folder3; import org.skife.jdbi.v2.Handle; -import org.skife.jdbi.v2.IDBI; import org.skife.jdbi.v2.StatementContext; import org.skife.jdbi.v2.exceptions.CallbackFailedException; import org.skife.jdbi.v2.exceptions.DBIException; @@ -61,7 +60,6 @@ public class SQLMetadataStorageActionHandler types ) { - this.dbi = dbi; this.connector = connector; this.config = config; this.jsonMapper = jsonMapper; @@ -152,7 +148,7 @@ public class SQLMetadataStorageActionHandler shouldRetry = new Predicate() diff --git a/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandlerFactory.java b/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandlerFactory.java index 5b9a8d7836d..c40eda77ea3 100644 --- a/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandlerFactory.java +++ b/server/src/main/java/io/druid/metadata/SQLMetadataStorageActionHandlerFactory.java @@ -28,7 +28,6 @@ import org.skife.jdbi.v2.IDBI; public class SQLMetadataStorageActionHandlerFactory implements MetadataStorageActionHandlerFactory { - private final IDBI dbi; private final SQLMetadataConnector connector; private final MetadataStorageTablesConfig config; private final ObjectMapper jsonMapper; @@ -40,7 +39,6 @@ public class SQLMetadataStorageActionHandlerFactory implements MetadataStorageAc ObjectMapper jsonMapper ) { - this.dbi = connector.getDBI(); this.connector = connector; this.config = config; this.jsonMapper = jsonMapper; @@ -48,6 +46,6 @@ public class SQLMetadataStorageActionHandlerFactory implements MetadataStorageAc public MetadataStorageActionHandler create(MetadataStorageActionHandlerTypes types) { - return new SQLMetadataStorageActionHandler<>(dbi, connector, config, jsonMapper, types); + return new SQLMetadataStorageActionHandler<>(connector, config, jsonMapper, types); } } diff --git a/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java b/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java new file mode 100644 index 00000000000..809cdea51db --- /dev/null +++ b/server/src/test/java/io/druid/metadata/SQLMetadataStorageActionHandlerTest.java @@ -0,0 +1,152 @@ +/* + * Druid - a distributed column store. + * Copyright (C) 2014 Metamarkets Group Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package io.druid.metadata; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Optional; +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.metamx.common.Pair; +import io.druid.indexing.overlord.MetadataStorageActionHandlerTypes; +import io.druid.jackson.DefaultObjectMapper; +import org.joda.time.DateTime; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +public class SQLMetadataStorageActionHandlerTest +{ + private static final ObjectMapper jsonMapper = new DefaultObjectMapper(); + private TestDerbyConnector connector; + private MetadataStorageTablesConfig tablesConfig = MetadataStorageTablesConfig.fromBase("test"); + + @Before + public void setUp() throws Exception { + MetadataStorageConnectorConfig config = jsonMapper.readValue( + "{" + + "\"type\" : \"db\",\n" + + "\"segmentTable\" : \"segments\"\n" + + "}", + MetadataStorageConnectorConfig.class + ); + + connector = new TestDerbyConnector( + Suppliers.ofInstance(config), + Suppliers.ofInstance(tablesConfig) + ); + } + + @Test + public void testTaskAndTaskStatus() throws Exception + { + SQLMetadataStorageActionHandler,Map,Map,Map> handler = new SQLMetadataStorageActionHandler(connector, tablesConfig, jsonMapper, new MetadataStorageActionHandlerTypes() + { + @Override + public TypeReference getTaskType() + { + return new TypeReference>() {}; + } + + @Override + public TypeReference getTaskStatusType() + { + return new TypeReference>() {}; + } + + @Override + public TypeReference getTaskActionType() + { + return new TypeReference>() {}; + } + + @Override + public TypeReference getTaskLockType() + { + return new TypeReference>() {}; + } + }); + + Map task = ImmutableMap.of("taskId", 1); + Map taskStatus = ImmutableMap.of("count", 42); + Map taskStatus2 = ImmutableMap.of("count", 42, "temp", 1); + + connector.createTaskTables(); + + final String taskId = "1234"; + + handler.insert(taskId, new DateTime("2014-01-02T00:00:00.123"), "testDataSource", task, true, taskStatus); + + Assert.assertEquals( + Optional.of(task), + handler.getTask(taskId) + ); + + Assert.assertEquals( + ImmutableList.of(Pair.of(task, taskStatus)), + handler.getActiveTasksWithStatus() + ); + + Assert.assertTrue(handler.setStatus(taskId, true, taskStatus2)); + + Assert.assertEquals( + ImmutableList.of(Pair.of(task, taskStatus2)), + handler.getActiveTasksWithStatus() + ); + + Assert.assertEquals( + ImmutableList.of(), + handler.getRecentlyFinishedTaskStatuses(new DateTime("2014-01-01")) + ); + + Assert.assertTrue(handler.setStatus(taskId, false, taskStatus)); + + Assert.assertEquals( + Optional.of(taskStatus), + handler.getTaskStatus(taskId) + ); + + // inactive statuses cannot be updated, this should fail + Assert.assertFalse(handler.setStatus(taskId, false, taskStatus2)); + + Assert.assertEquals( + Optional.of(taskStatus), + handler.getTaskStatus(taskId) + ); + + Assert.assertEquals( + Optional.of(task), + handler.getTask(taskId) + ); + + Assert.assertEquals( + ImmutableList.of(), + handler.getRecentlyFinishedTaskStatuses(new DateTime("2014-01-03")) + ); + + Assert.assertEquals( + ImmutableList.of(taskStatus), + handler.getRecentlyFinishedTaskStatuses(new DateTime("2014-01-01")) + ); + } +}