private void registerControl(ControlService cs) { if (cs == null) throw new IllegalArgumentException("ControlService must not be null"); logger.trace("Registering control for job id {}, job name {}", job.getId(), job.getName()); this.controlService = cs; JobMXBean oldControl = cs.getControl(JobMXBean.TYPE, job.getName(), JobMXBean.class); if (oldControl != null) { String oldControlId = cs.getControlId(JobMXBean.TYPE, job.getName(), JobMXBean.class); if (oldControlId != null) { if (isJobClosed(oldControl)) { cs.unregister(oldControlId); logger.debug("Old control id {} for CLOSED job name {} was unregistered", oldControlId, job.getName()); } else { throw new IllegalStateException( "Cannot register job control for alias " + job.getName() + " because a job control with id " + oldControlId + " for the same alias already exists and is not CLOSED"); } } } this.controlId = cs.registerControl(JobMXBean.TYPE, job.getId(), job.getName(), JobMXBean.class, this); logger.debug("Control for job id {}, job name {} was registered with id {}", job.getId(), job.getName(), controlId); }
/** * Register a {@link StreamScopeRegistryMXBean} with the registered * {@link ControlService} (for use by the Edgent Console). */ private void registerRegistryBean(ServiceContainer services) { ControlService cs = services.getService(ControlService.class); if (cs == null || rgy == null) throw new IllegalStateException(); { // more workaround... // // If a rgyBean control is already registered, then don't reregister // (this will/should be the JMXControlService case) so as to avoid // an "already registered" exception from the ControlService. // The rgyBean is gotta be for the matching rgy with this // singleton StreamScopeSetup instance scheme so everything is OK. StreamScopeRegistryMXBean mbean = cs.getControl(StreamScopeRegistryMXBean.TYPE, StreamScopeRegistryMXBean.TYPE, StreamScopeRegistryMXBean.class); if (mbean != null) { return; } } if (rgyBean == null) rgyBean = new StreamScopeRegistryBean(rgy, cs); rgyBeanControlId = cs.registerControl(StreamScopeRegistryMXBean.TYPE, StreamScopeRegistryMXBean.TYPE+"_0", StreamScopeRegistryMXBean.TYPE, StreamScopeRegistryMXBean.class, rgyBean); }
@Test(expected=RuntimeException.class) public void testDoubleunregister() throws Exception { ControlService cs = new JMXControlService(DOMAIN, new Hashtable<>()); String type = "timer"; String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); cs.unregister(controlId); cs.unregister(controlId); } }
/** * Submits an application using an {@code ApplicationServiceMXBean} control * registered with the specified {@code ControlService}. * * @param applicationName the name of the application to submit * @param controlService the control service */ public static void submitApplication(String applicationName, ControlService controlService) { try { ApplicationServiceMXBean control = controlService.getControl( ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); if (control == null) { throw new IllegalStateException( "Could not find a registered control with the following interface: " + ApplicationServiceMXBean.class.getName()); } // TODO add ability to submit with the initial application configuration logger.info("Restarting monitored application {}", applicationName); control.submit(applicationName, null); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Create an {@code ApplicationService} instance. * @param provider Provider to create topology instances for registered applications. * @param submitter Submitter for registered applications. * @param alias Alias used to register the control MBean. */ public AppService(TopologyProvider provider, DirectSubmitter<Topology, Job> submitter, String alias) { this.provider = provider; this.submitter = submitter; ControlService cs = submitter.getServices().getService(ControlService.class); if (cs != null) cs.registerControl(ApplicationServiceMXBean.TYPE, ALIAS+System.currentTimeMillis(), alias, ApplicationServiceMXBean.class, new AppServiceControl(this)); }
/** * Unregister all StreamScopeMXBean for the oplet. * no-op if none registered. * <BR> * N.B. This does not unregister StreamScopes from the underlying StreamScopeRegistry. * @param jobId * @param opletId */ void unregister(String jobId, String opletId) { for (String controlId : controlIds(jobId, opletId)) { cs.unregister(controlId); } }
/** * Submits an application using an {@code ApplicationServiceMXBean} control * registered with the specified {@code ControlService}. * * @param applicationName the name of the application to submit * @param controlService the control service */ public static void submitApplication(String applicationName, ControlService controlService) { try { ApplicationServiceMXBean control = controlService.getControl( ApplicationServiceMXBean.TYPE, ApplicationService.ALIAS, ApplicationServiceMXBean.class); if (control == null) { throw new IllegalStateException( "Could not find a registered control with the following interface: " + ApplicationServiceMXBean.class.getName()); } // TODO add ability to submit with the initial application configuration logger.info("Restarting monitored application {}", applicationName); control.submit(applicationName, null); } catch (Exception e) { throw new RuntimeException(e); } }
@Test(expected=RuntimeException.class) public void testDoubleRegister() throws Exception { ControlService cs = new JMXControlService(DOMAIN, new Hashtable<>()); String type = "timer"; String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); try { cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); } finally { cs.unregister(controlId); } } @Test(expected=RuntimeException.class)
@Override public StreamScopeMXBean lookup(String jobId, String opletId, int oport) { // lazy-register the mbeans String streamId = StreamScopeRegistry.mkStreamId(jobId, opletId, oport); StreamScopeMXBean mbean = cs.getControl(StreamScopeMXBean.TYPE, streamId, StreamScopeMXBean.class); if (mbean == null) { String name = StreamScopeRegistry.nameForStreamId(streamId); StreamScope<?> ss = rgy.lookup(name); if (ss != null) { mbean = new StreamScopeBean(ss); String controlId = cs.registerControl(StreamScopeMXBean.TYPE, StreamScopeMXBean.TYPE+streamId, streamId, StreamScopeMXBean.class, mbean); controlIdMap.put(name, controlId); } } return mbean; }
/** * Create an {@code ApplicationService} instance. * @param provider Provider to create topology instances for registered applications. * @param submitter Submitter for registered applications. * @param alias Alias used to register the control MBean. */ public AppService(TopologyProvider provider, DirectSubmitter<Topology, Job> submitter, String alias) { this.provider = provider; this.submitter = submitter; ControlService cs = submitter.getServices().getService(ControlService.class); if (cs != null) cs.registerControl(ApplicationServiceMXBean.TYPE, ALIAS+System.currentTimeMillis(), alias, ApplicationServiceMXBean.class, new AppServiceControl(this)); }
controlService.unregister(controlId); logger.trace("Control {} unregistered", controlId);
private void registerControl(ControlService cs) { if (cs == null) throw new IllegalArgumentException("ControlService must not be null"); logger.trace("Registering control for job id {}, job name {}", job.getId(), job.getName()); this.controlService = cs; JobMXBean oldControl = cs.getControl(JobMXBean.TYPE, job.getName(), JobMXBean.class); if (oldControl != null) { String oldControlId = cs.getControlId(JobMXBean.TYPE, job.getName(), JobMXBean.class); if (oldControlId != null) { if (isJobClosed(oldControl)) { cs.unregister(oldControlId); logger.debug("Old control id {} for CLOSED job name {} was unregistered", oldControlId, job.getName()); } else { throw new IllegalStateException( "Cannot register job control for alias " + job.getName() + " because a job control with id " + oldControlId + " for the same alias already exists and is not CLOSED"); } } } this.controlId = cs.registerControl(JobMXBean.TYPE, job.getId(), job.getName(), JobMXBean.class, this); logger.debug("Control for job id {}, job name {} was registered with id {}", job.getId(), job.getName(), controlId); }
JobMXBean jobMbean = controlService.getControl(JobMXBean.TYPE, jobName, JobMXBean.class); if (jobMbean == null) { throw new IllegalStateException(
@Test public void testControlObject() throws Exception { ControlService cs = new JMXControlService(DOMAIN, new Hashtable<>()); String type = "timer"; String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); assertNotNull(controlId); ObjectName on = ObjectName.getInstance(controlId); assertEquals(DOMAIN, on.getDomain()); assertEquals(type, ObjectName.unquote(on.getKeyProperty("type"))); assertEquals(id, ObjectName.unquote(on.getKeyProperty("id"))); assertEquals(alias, ObjectName.unquote(on.getKeyProperty("alias"))); assertEquals(TimerMBean.class.getName(), ObjectName.unquote(on.getKeyProperty("interface"))); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); assertTrue(mbs.isRegistered(on)); cs.unregister(controlId); assertFalse(mbs.isRegistered(on)); }
@Override public synchronized void start() { ControlService cs = getOpletContext().getService(ControlService.class); // TODO BUG HERE: the control alias needs to be unique across the // entire provider instance (multiple topologies) because the ControlService // is provider-wide, not topology specific. // Scope it with just the jobId. What's going to unregister this control? if (cs != null) cs.registerControl(TSTREAM_TYPE, getOpletContext().uniquify(getClass().getSimpleName()), getAlias(), PeriodMXBean.class, this); schedule(false); }
controlService.unregister(controlId); logger.trace("Control {} unregistered", controlId);
JobMXBean jobMbean = controlService.getControl(JobMXBean.TYPE, jobName, JobMXBean.class); if (jobMbean == null) { throw new IllegalStateException(
String id = "a"; String alias = "ControlA"; String controlId = cs.registerControl(type, id, alias, TimerMBean.class, new Timer()); cs.unregister(controlId); assertFalse(mbs.isRegistered(on));
@Override public synchronized void start() { ControlService cs = getOpletContext().getService(ControlService.class); // TODO BUG HERE: the control alias needs to be unique across the // entire provider instance (multiple topologies) because the ControlService // is provider-wide, not topology specific. // Scope it with just the jobId. What's going to unregister this control? if (cs != null) cs.registerControl(TSTREAM_TYPE, getOpletContext().uniquify(getClass().getSimpleName()), getAlias(), PeriodMXBean.class, this); schedule(false); }
static <T> void setPollFrequency(TStream<T> pollStream, long period, TimeUnit unit) { ControlService cs = pollStream.topology().getRuntimeServiceSupplier() .get().getService(ControlService.class); PeriodMXBean control = cs.getControl(TStream.TYPE, pollStream.getAlias(), PeriodMXBean.class); control.setPeriod(period, unit); }