From e200e4295980ae95e040e9302c5600d1e3de9772 Mon Sep 17 00:00:00 2001 From: Neng Lu Date: Thu, 19 May 2016 00:10:48 -0700 Subject: [PATCH] implement aurora scheduler getJobLinks (#707) --- .../heron/scheduler/aurora/AuroraContext.java | 29 +++++++++++++++++++ .../scheduler/aurora/AuroraScheduler.java | 12 +++++++- .../scheduler/aurora/AuroraSchedulerTest.java | 29 +++++++++++++++++++ .../com/twitter/heron/spi/common/Misc.java | 4 +++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraContext.java diff --git a/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraContext.java b/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraContext.java new file mode 100644 index 00000000000..2ce8149cc7d --- /dev/null +++ b/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraContext.java @@ -0,0 +1,29 @@ +// Copyright 2016 Twitter. All rights reserved. +// +// Licensed 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. + +package com.twitter.heron.scheduler.aurora; + +import com.twitter.heron.spi.common.Config; +import com.twitter.heron.spi.common.Context; + +public final class AuroraContext extends Context { + public static final String JOB_LINK_TEMPLATE = "heron.scheduler.job.link.template"; + + private AuroraContext() { + } + + public static String getJobLinkTemplate(Config config) { + return config.getStringValue(JOB_LINK_TEMPLATE); + } +} diff --git a/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraScheduler.java b/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraScheduler.java index 64f7be9d0f1..171c744f8e1 100644 --- a/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraScheduler.java +++ b/heron/schedulers/src/java/com/twitter/heron/scheduler/aurora/AuroraScheduler.java @@ -29,6 +29,7 @@ import com.twitter.heron.proto.scheduler.Scheduler; import com.twitter.heron.spi.common.Config; import com.twitter.heron.spi.common.Context; +import com.twitter.heron.spi.common.Misc; import com.twitter.heron.spi.common.PackingPlan; import com.twitter.heron.spi.scheduler.IScheduler; import com.twitter.heron.spi.utils.Runtime; @@ -83,7 +84,16 @@ public boolean onSchedule(PackingPlan packing) { @Override public List getJobLinks() { - return new ArrayList<>(); + List jobLinks = new ArrayList<>(); + + //Only the aurora job page is returned + String jobLinkFormat = AuroraContext.getJobLinkTemplate(config); + if (jobLinkFormat != null && !jobLinkFormat.isEmpty()) { + String jobLink = Misc.substitute(config, jobLinkFormat); + jobLinks.add(jobLink); + } + + return jobLinks; } @Override diff --git a/heron/schedulers/tests/java/com/twitter/heron/scheduler/aurora/AuroraSchedulerTest.java b/heron/schedulers/tests/java/com/twitter/heron/scheduler/aurora/AuroraSchedulerTest.java index 674a7262ebf..ab982b4e39c 100644 --- a/heron/schedulers/tests/java/com/twitter/heron/scheduler/aurora/AuroraSchedulerTest.java +++ b/heron/schedulers/tests/java/com/twitter/heron/scheduler/aurora/AuroraSchedulerTest.java @@ -15,6 +15,7 @@ package com.twitter.heron.scheduler.aurora; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.After; @@ -23,13 +24,20 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import com.twitter.heron.proto.scheduler.Scheduler; import com.twitter.heron.spi.common.Config; +import com.twitter.heron.spi.common.Misc; import com.twitter.heron.spi.common.PackingPlan; +@RunWith(PowerMockRunner.class) +@PrepareForTest(Misc.class) public class AuroraSchedulerTest { private static final String AURORA_PATH = "path.aurora"; private static final String PACKING_PLAN_ID = "packing.plan.id"; @@ -144,4 +152,25 @@ public void testOnRestart() throws Exception { Assert.assertTrue(scheduler.onRestart(restartTopologyRequest)); Mockito.verify(controller, Mockito.times(2)).restartJob(containerToRestart); } + + @Test + public void testGetJobLinks() throws Exception { + final String JOB_LINK_FORMAT = "http://go/${CLUSTER}/${ROLE}/${ENVIRON}/${TOPOLOGY}"; + final String SUBSTITUTED_JOB_LINK = "http://go/local/heron/test/test_topology"; + + Config mockConfig = Mockito.mock(Config.class); + Mockito.when(mockConfig.getStringValue(AuroraContext.JOB_LINK_TEMPLATE)) + .thenReturn(JOB_LINK_FORMAT); + + scheduler.initialize(mockConfig, Mockito.mock(Config.class)); + + PowerMockito.spy(Misc.class); + PowerMockito.doReturn(SUBSTITUTED_JOB_LINK) + .when(Misc.class, "substitute", mockConfig, JOB_LINK_FORMAT); + + List result = scheduler.getJobLinks(); + + Assert.assertEquals(1, result.size()); + Assert.assertTrue(result.get(0).equals(SUBSTITUTED_JOB_LINK)); + } } diff --git a/heron/spi/src/java/com/twitter/heron/spi/common/Misc.java b/heron/spi/src/java/com/twitter/heron/spi/common/Misc.java index 1d8112545c6..1f8ab18e008 100644 --- a/heron/spi/src/java/com/twitter/heron/spi/common/Misc.java +++ b/heron/spi/src/java/com/twitter/heron/spi/common/Misc.java @@ -208,6 +208,10 @@ public static String substitute(Config config, String pathString) { } else if ("${TOPOLOGY}".equals(elem)) { list.set(i, Context.topologyName(config)); + + } else if ("${ENVIRON}".equals(elem)) { + list.set(i, Context.environ(config)); + } }