From commits-return-5696-apmail-helix-commits-archive=helix.apache.org@helix.apache.org Sun Nov 1 23:01:28 2015 Return-Path: X-Original-To: apmail-helix-commits-archive@minotaur.apache.org Delivered-To: apmail-helix-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7BF6318683 for ; Sun, 1 Nov 2015 23:01:28 +0000 (UTC) Received: (qmail 99473 invoked by uid 500); 1 Nov 2015 23:01:28 -0000 Delivered-To: apmail-helix-commits-archive@helix.apache.org Received: (qmail 99395 invoked by uid 500); 1 Nov 2015 23:01:28 -0000 Mailing-List: contact commits-help@helix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@helix.apache.org Delivered-To: mailing list commits@helix.apache.org Received: (qmail 99365 invoked by uid 99); 1 Nov 2015 23:01:28 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Nov 2015 23:01:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 29E8CDFBAF; Sun, 1 Nov 2015 23:01:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kanak@apache.org To: commits@helix.apache.org Date: Sun, 01 Nov 2015 23:01:30 -0000 Message-Id: <4f0d0ccbab7f4af180abbbd906ecb7bf@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [3/5] helix git commit: Makes TaskStateModel gets properly shutdown when the JVM exists Makes TaskStateModel gets properly shutdown when the JVM exists Signed-off-by: Yinan Li Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/6ae7891e Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/6ae7891e Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/6ae7891e Branch: refs/heads/master Commit: 6ae7891e1754190219cc47940013aff215abacf8 Parents: eb82c95 Author: Yinan Li Authored: Tue Sep 8 17:14:55 2015 -0700 Committer: Yinan Li Committed: Wed Sep 9 12:40:58 2015 -0700 ---------------------------------------------------------------------- .../helix/task/TaskStateModelFactory.java | 28 ++++++- .../helix/task/TaskStateModelFactoryTest.java | 82 ++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/6ae7891e/helix-core/src/main/java/org/apache/helix/task/TaskStateModelFactory.java ---------------------------------------------------------------------- diff --git a/helix-core/src/main/java/org/apache/helix/task/TaskStateModelFactory.java b/helix-core/src/main/java/org/apache/helix/task/TaskStateModelFactory.java index 23d6160..43da591 100644 --- a/helix-core/src/main/java/org/apache/helix/task/TaskStateModelFactory.java +++ b/helix-core/src/main/java/org/apache/helix/task/TaskStateModelFactory.java @@ -19,6 +19,7 @@ package org.apache.helix.task; * under the License. */ +import java.util.Deque; import java.util.Map; import org.apache.helix.HelixManager; @@ -26,20 +27,43 @@ import org.apache.helix.api.StateTransitionHandlerFactory; import org.apache.helix.api.id.PartitionId; import org.apache.helix.api.id.ResourceId; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; + /** * Factory class for {@link TaskStateModel}. */ public class TaskStateModelFactory extends StateTransitionHandlerFactory { + private final HelixManager _manager; private final Map _taskFactoryRegistry; + private final Deque _taskStateModels; public TaskStateModelFactory(HelixManager manager, Map taskFactoryRegistry) { _manager = manager; - _taskFactoryRegistry = taskFactoryRegistry; + _taskFactoryRegistry = ImmutableMap.copyOf(taskFactoryRegistry); + _taskStateModels = Lists.newLinkedList(); } @Override public TaskStateModel createStateTransitionHandler(ResourceId resourceId, PartitionId partitionId) { - return new TaskStateModel(_manager, _taskFactoryRegistry); + final TaskStateModel taskStateModel = new TaskStateModel(_manager, _taskFactoryRegistry); + _taskStateModels.push(taskStateModel); + return taskStateModel; + } + + /** + * Shutdown this {@link TaskStateModelFactory} instance. + * + *

+ * This method shuts down all of the {@link TaskStateModel} instances created by this + * {@link TaskStateModelFactory} instance. + *

+ */ + public void shutdown() { + while (!_taskStateModels.isEmpty()) { + TaskStateModel taskStateModel = _taskStateModels.pop(); + taskStateModel.shutdown(); + } } } http://git-wip-us.apache.org/repos/asf/helix/blob/6ae7891e/helix-core/src/test/java/org/apache/helix/task/TaskStateModelFactoryTest.java ---------------------------------------------------------------------- diff --git a/helix-core/src/test/java/org/apache/helix/task/TaskStateModelFactoryTest.java b/helix-core/src/test/java/org/apache/helix/task/TaskStateModelFactoryTest.java new file mode 100644 index 0000000..d0f6a41 --- /dev/null +++ b/helix-core/src/test/java/org/apache/helix/task/TaskStateModelFactoryTest.java @@ -0,0 +1,82 @@ +package org.apache.helix.task; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.List; +import java.util.Map; + +import org.apache.helix.Mocks; +import org.apache.helix.api.id.PartitionId; +import org.apache.helix.api.id.ResourceId; +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +/** + * Unit tests for {@link TaskStateModelFactory}. + * + * @author liyinan926 + */ +@Test(groups = { "org.apache.helix.task" }) +public class TaskStateModelFactoryTest { + + @Test + public void testCreateAndShutdownTaskStateModels() { + Map taskFactoryMap = Maps.newHashMap(); + taskFactoryMap.put("Test", new TestTaskFactory()); + TaskStateModelFactory taskStateModelFactory = new TaskStateModelFactory(new Mocks.MockManager(), taskFactoryMap); + + List taskStateModelList = Lists.newArrayList(); + for (int i = 0; i < 3; i++) { + ResourceId resourceId = ResourceId.from("Task_" + i); + taskStateModelList.add( + taskStateModelFactory.createStateTransitionHandler(resourceId, PartitionId.from(resourceId, ""))); + } + + taskStateModelFactory.shutdown(); + + for (TaskStateModel taskStateModel : taskStateModelList) { + Assert.assertTrue(taskStateModel.isShutdown()); + } + } + + private static class TestTaskFactory implements TaskFactory { + + @Override + public Task createNewTask(TaskCallbackContext context) { + return new TestTask(); + } + + private static class TestTask implements Task { + + @Override + public TaskResult run() { + return new TaskResult(TaskResult.Status.COMPLETED, ""); + } + + @Override + public void cancel() { + + } + } + } +}