@POST @Path("/build") public String build() throws Exception { ScheduleOption option = new ScheduleOption(); ScheduleState state = alertCoordinator.schedule(option); return JsonUtils.writeValueAsString(state); }
@Override public void contextInitialized(ServletContextEvent sce) { LOG.info("start coordinator background tasks.."); Coordinator.startSchedule(); }
@POST @Path("/disablePeriodicForceBuild") public void disablePeriodicallyBuild() { alertCoordinator.disablePeriodicallyBuild(); }
@SuppressWarnings( {"resource", "unused"}) @Test public void test_01() throws Exception { before(); Config config = ConfigFactory.load().getConfig("coordinator"); ZKConfig zkConfig = ZKConfigBuilder.getZKConfig(config); IMetadataServiceClient client = ScheduleContextBuilderTest.getSampleMetadataService(); Coordinator coordinator = new Coordinator(config, zkConfig, client); ScheduleOption option = new ScheduleOption(); ScheduleState state = coordinator.schedule(option); String v = state.getVersion(); // TODO : assert version CountDownLatch latch = new CountDownLatch(1); AtomicBoolean validated = new AtomicBoolean(false); ConfigBusConsumer consumer = new ConfigBusConsumer(zkConfig, "topo1/spout", new ConfigChangeCallback() { @Override public void onNewConfig(ConfigValue value) { String vId = value.getValue().toString(); Assert.assertEquals(v, vId); validated.set(true); latch.countDown(); } }); latch.await(3, TimeUnit.SECONDS); Assert.assertTrue(validated.get()); }
@Override public void run() { if (Coordinator.isPeriodicallyForceBuildEnable()) { LOG.info("CoordinatorTrigger started ... "); Stopwatch watch = Stopwatch.createStarted(); ZKConfig zkConfig = ZKConfigBuilder.getZKConfig(config); try (ExclusiveExecutor executor = new ExclusiveExecutor(zkConfig)) { executor.execute(Coordinator.GREEDY_SCHEDULER_ZK_PATH, () -> { // schedule IScheduleContext context = new ScheduleContextBuilder(config, client).buildContext(); TopologyMgmtService mgmtService = new TopologyMgmtService(); IPolicyScheduler scheduler = PolicySchedulerFactory.createScheduler(); scheduler.init(context, mgmtService); ScheduleState state = scheduler.schedule(new ScheduleOption()); // use try catch to use AutoCloseable interface to close producer automatically try (ConfigBusProducer producer = new ConfigBusProducer(ZKConfigBuilder.getZKConfig(config))) { Coordinator.postSchedule(client, state, producer); } watch.stop(); LOG.info("CoordinatorTrigger ended, used time {} sm.", watch.elapsed(TimeUnit.MILLISECONDS)); }); } catch (Exception e) { LOG.error("trigger schedule failed!", e); } } else { LOG.info("CoordinatorTrigger found isPeriodicallyForceBuildEnable = false, skipped build"); } }
postSchedule(client, state, producer);
@POST @Path("/enablePeriodicForceBuild") public void enforcePeriodicallyBuild() { alertCoordinator.enforcePeriodicallyBuild(); }
@SuppressWarnings("static-access") @GET @Path("/periodicForceBuildState") public boolean statPeriodicallyBuild() { return alertCoordinator.isPeriodicallyForceBuildEnable(); }
@GET @Path("/assignments") public String getAssignments() throws Exception { ScheduleState state = alertCoordinator.getState(); return JsonUtils.writeValueAsString(state); }
@POST @Path("/validate") public String validate() throws Exception { ValidateState state = alertCoordinator.validate(); return JsonUtils.writeValueAsString(state); }
@SuppressWarnings( {"resource", "unused"}) @Ignore @Test public void test() throws Exception { before(); Config config = ConfigFactory.load().getConfig("coordinator"); ZKConfig zkConfig = ZKConfigBuilder.getZKConfig(config); IMetadataServiceClient client = new MetadataServiceClientImpl(config); Coordinator coordinator = new Coordinator(config, zkConfig, client); ScheduleOption option = new ScheduleOption(); ScheduleState state = coordinator.schedule(option); String v = state.getVersion(); AtomicBoolean validated = new AtomicBoolean(false); ConfigBusConsumer consumer = new ConfigBusConsumer(zkConfig, "topo1/spout", new ConfigChangeCallback() { @Override public void onNewConfig(ConfigValue value) { String vId = value.getValue().toString(); Assert.assertEquals(v, vId); validated.set(true); } }); Thread.sleep(1000); Assert.assertTrue(validated.get()); }
@Ignore @Test public void addScheduleState() throws Exception { ConfigFactory.invalidateCaches(); System.setProperty("config.resource", "/test-application.conf"); Config config = ConfigFactory.load("test-application.conf").getConfig("coordinator"); MetadataServiceClientImpl client = new MetadataServiceClientImpl(config); ScheduleState ss = new ScheduleState(); ss.setVersion("spec_version_1463764252582"); client.addScheduleState(ss); client.close(); ss.setVersion("spec_version_1464764252582"); ZKConfig zkConfig = ZKConfigBuilder.getZKConfig(config); ConfigBusProducer producer = new ConfigBusProducer(zkConfig); Coordinator.postSchedule(client, ss, producer); } }
@Test public void test_Schedule() { Coordinator.startSchedule(); }