protected void addIfServiceDependency(Object object, Object dependency) { if (object instanceof Service && dependency instanceof Service) { Service service = (Service) object; Service dependencyService = (Service) dependency; ServiceWithDependency sd = services.get(service); sd.dependencies.add(dependencyService); dependencyService.registerServiceListener(sd); } }
/** * Run a child service -initing and starting it if this service has already passed those parts of * its own lifecycle * * @param service the service to start */ protected boolean deployChildService(Service service) { service.init(getConfig()); addService(service); if (isInState(STATE.STARTED)) { service.start(); return true; } return false; }
void start() throws InterruptedException { if (dependencies.size() > 0) { synchronized (this) { while (!canStart) { this.wait(1000 * 60 * 3L); if (dependenciesFailed) { throw new TezUncheckedException( "Skipping service start for " + service.getName() + " as dependencies failed to start"); } } } } if (LOG.isDebugEnabled()) { LOG.debug("Service: " + service.getName() + " trying to start"); } for (Service dependency : dependencies) { if (!dependency.isInState(Service.STATE.STARTED)) { LOG.info( "Service: " + service.getName() + " not started because " + " service: " + dependency.getName() + " is in state: " + dependency.getServiceState()); return; } } service.start(); }
@Override public void stateChanged(Service dependency) { if (LOG.isDebugEnabled()) { LOG.debug( "Service dependency: " + dependency.getName() + " notify" + " for service: " + service.getName()); } if (dependency.isInState(Service.STATE.STARTED)) { if (dependenciesStarted.incrementAndGet() == dependencies.size()) { synchronized (this) { if (LOG.isDebugEnabled()) { LOG.debug("Service: " + service.getName() + " notified to start"); } canStart = true; this.notifyAll(); } } } else if (!service.isInState(Service.STATE.STARTED) && dependency.getFailureState() != null) { synchronized (this) { dependenciesFailed = true; if (LOG.isDebugEnabled()) { LOG.debug( "Service: " + service.getName() + " will fail to start" + " as dependent service " + dependency.getName() + " failed to start"); } this.notifyAll(); } } }