@Override public synchronized OpResult addScheduleState(ScheduleState state) { // FIXME : might concurrent issue String toRemove = null; if (scheduleStates.size() > maxScheduleState) { toRemove = scheduleStates.firstKey(); } scheduleStates.put(state.getVersion(), state); if (toRemove != null) { scheduleStates.remove(toRemove); } OpResult result = new OpResult(); result.code = 200; result.message = "OK"; return result; }
@Override public void addScheduleState(ScheduleState state) { scheduleStates.put(state.getVersion(), state); }
public static <T> String getKey(T t) { if (t instanceof StreamDefinition) { return ((StreamDefinition) t).getStreamId(); } if (t instanceof PolicyAssignment) { return ((PolicyAssignment) t).getPolicyName(); } if (t instanceof ScheduleState) { return ((ScheduleState) t).getVersion(); } if (t instanceof AlertPublishEvent) { return ((AlertPublishEvent) t).getAlertId(); } try { Method m = t.getClass().getMethod("getName"); return (String) m.invoke(t); } catch (NoSuchMethodException | SecurityException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { LOG.error(" getName not found on given class :" + t.getClass().getName()); } throw new RuntimeException(String.format("no getName() found on target class %s for matching", t.getClass() .getName())); }
public static void postSchedule(IMetadataServiceClient client, ScheduleState state, ConfigBusProducer producer) { // persist state client.addScheduleState(state); // notify ConfigValue value = new ConfigValue(); value.setValue(state.getVersion()); value.setValueVersionId(true); for (String topo : state.getSpoutSpecs().keySet()) { producer.send(MessageFormat.format(ZK_ALERT_CONFIG_SPOUT, topo), value); } for (String topo : state.getGroupSpecs().keySet()) { producer.send(MessageFormat.format(ZK_ALERT_CONFIG_ROUTER, topo), value); } for (String topo : state.getAlertSpecs().keySet()) { producer.send(MessageFormat.format(ZK_ALERT_CONFIG_ALERT, topo), value); } for (String topo : state.getPublishSpecs().keySet()) { producer.send(MessageFormat.format(ZK_ALERT_CONFIG_PUBLISHER, topo), value); } }
state.getVersion(), state.getGenerateTime(), state.getCode(), state.getMessage(), state.getScheduleTimeMillis());
@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()); }
@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()); }
/** * get the basic ScheduleState, and then based on the version to get all sub-part(spoutSpecs/alertSpecs/etc) * to form a completed ScheduleState. * @return the latest ScheduleState */ @Override public ScheduleState getScheduleState() { BsonDocument sort = new BsonDocument(); sort.append("generateTime", new BsonInt32(-1)); ScheduleState state = scheduleStates.find().sort(sort).map(new Function<Document, ScheduleState>() { @Override public ScheduleState apply(Document t) { String json = t.toJson(); try { return mapper.readValue(json, ScheduleState.class); } catch (IOException e) { LOG.error("deserialize config item failed!", e); } return null; } }).first(); if (state != null) { String version = state.getVersion(); // based on version, to add content from collections of spoutSpecs/alertSpecs/etc.. state = addDetailForScheduleState(state, version); } return state; }
private void assertFirstPolicyScheduled(IScheduleContext context, ScheduleState status) { String version = status.getVersion();
private void assertSecondPolicyCreated(IScheduleContext context, ScheduleState status) { String version = status.getVersion();