/** * @param baseInjector base {@link Injector} to extend upon * @return an {@link Injector} which has {@link LocalMessageDispatcher} for testing. * @throws InjectionException when failed to inject {@link LocalMessageDispatcher} */ public static Injector forkInjector(final Injector baseInjector) throws InjectionException { final Injector injector = baseInjector .forkInjector(LocalMessageEnvironment.LOCAL_MESSAGE_ENVIRONMENT_CONFIGURATION); injector.getInstance(LocalMessageDispatcher.class); return injector; } }
/** * Run user-provided main method. * * @param jobConf the job configuration * @throws Exception on any exceptions on the way */ private static void runUserProgramMain(final Configuration jobConf) throws Exception { final Injector injector = TANG.newInjector(jobConf); final String className = injector.getNamedInstance(JobConf.UserMainClass.class); final String userArgsString = injector.getNamedInstance(JobConf.UserMainArguments.class); final String[] args = userArgsString.isEmpty() ? EMPTY_USER_ARGS : userArgsString.split(" "); final Class userCode = Class.forName(className); final Method method = userCode.getMethod("main", String[].class); if (!Modifier.isStatic(method.getModifiers())) { throw new RuntimeException("User Main Method not static"); } if (!Modifier.isPublic(userCode.getModifiers())) { throw new RuntimeException("User Main Class not public"); } LOG.info("User program started"); method.invoke(null, (Object) args); LOG.info("User program finished"); }
/** * Instantiates a new Topology instance. * @param operatorName specified name of the operator * @param topologyClass specified topology type * @return Topology instance * @throws InjectionException */ public Topology getNewInstance(final Class<? extends Name<String>> operatorName, final Class<? extends Topology> topologyClass) throws InjectionException { final Injector newInjector = injector.forkInjector(); newInjector.bindVolatileParameter(OperatorNameClass.class, operatorName); return newInjector.getInstance(topologyClass); } }
/** * Copy event handler from current class configuration into runtime injector. * This is a helper method called from initializeInjector() only. * @param runtimeInjector Runtime injector to copy event handler to. * @param param Class that identifies the event handler parameter. * @param <T> Type of the event handler. * @throws InjectionException If configuration error occurs. */ private <T extends EventHandler<?>> void copyEventHandler( final Injector runtimeInjector, final Class<? extends Name<T>> param) throws InjectionException { runtimeInjector.bindVolatileParameter(param, this.originalInjector.getNamedInstance(param)); }
/** * Instantiate a launcher for the given Configuration. * * @param runtimeConfiguration the resourcemanager configuration to be used * @return a DriverLauncher based on the given resourcemanager configuration * @throws InjectionException on configuration errors */ public static DriverLauncher getLauncher(final Configuration runtimeConfiguration) throws InjectionException { return Tang.Factory.getTang() .newInjector(runtimeConfiguration, CLIENT_CONFIG) .getInstance(DriverLauncher.class); }
/** * Runs the RPC server. * Specifically, creates a {@link NettyMessagingTransport} and binds it to a listening port. */ public void run() { // Calling 'run' multiple times is considered invalid, since it will override state variables like // 'transport', and 'host'. ensureServerState(false); try { final Injector injector = Tang.Factory.getTang().newInjector(); final LocalAddressProvider localAddressProvider = injector.getInstance(LocalAddressProvider.class); host = localAddressProvider.getLocalAddress(); injector.bindVolatileParameter(RemoteConfiguration.HostAddress.class, host); injector.bindVolatileParameter(RemoteConfiguration.Port.class, 0); injector.bindVolatileParameter(RemoteConfiguration.RemoteServerStage.class, new SyncStage<>(new ServerEventHandler())); transport = injector.getInstance(NettyMessagingTransport.class); LOG.info("DriverRPCServer running at {}", transport.getListeningPort()); isRunning = true; } catch (final InjectionException e) { throw new RuntimeException(e); } }
final Injector forkedInjector = injector.forkInjector(operatorConfig); forkedInjector.bindVolatileInstance(CommunicationGroupServiceClient.class, this); final GroupCommOperator operator = forkedInjector.getInstance(GroupCommOperator.class); final String operName = forkedInjector.getNamedInstance(OperatorName.class); this.operators.put(Utils.getClass(operName), operator); LOG.finest(operName + " has CommGroupHandler-" + commGroupNetworkHandler.toString());
/** * Create a new ContextRuntime. * * @param serviceInjector the serviceInjector to be used. * @param contextConfiguration the Configuration for this context. * @throws ContextClientCodeException if the context cannot be instantiated. */ ContextRuntime(final Injector serviceInjector, final Configuration contextConfiguration, final Optional<ContextRuntime> parentContext) throws ContextClientCodeException { this.serviceInjector = serviceInjector; this.parentContext = parentContext; // Trigger the instantiation of the services try { final Set<Object> services = serviceInjector.getNamedInstance(Services.class); this.contextInjector = serviceInjector.forkInjector(contextConfiguration); this.contextLifeCycle = this.contextInjector.getInstance(ContextLifeCycle.class); } catch (BindException | InjectionException e) { final Optional<String> parentID = this.getParentContext().isPresent() ? Optional.of(this.getParentContext().get().getIdentifier()) : Optional.<String>empty(); throw new ContextClientCodeException( ContextClientCodeException.getIdentifier(contextConfiguration), parentID, "Unable to spawn context", e); } // Trigger the context start events on contextInjector. this.contextLifeCycle.start(); }
nameResolver = Tang.Factory.getTang().newInjector(nameResolverConf).getInstance(NameResolver.class); } catch (final InjectionException e) { throw new RuntimeException("Failed to instantiate NameResolver", e); injector.bindVolatileParameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class, idFac); injector.bindVolatileInstance(NameResolver.class, nameResolver); injector.bindVolatileParameter(NetworkServiceParameters.NetworkServiceCodec.class, new GroupCommunicationMessageCodec()); injector.bindVolatileParameter(NetworkServiceParameters.NetworkServiceTransportFactory.class, tpFactory); injector.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new EventHandler<Message<GroupCommunicationMessage>>() { @Override injector.bindVolatileParameter(NetworkServiceParameters.NetworkServiceExceptionHandler.class, new LoggingEventHandler<Exception>()); this.netService = injector.getInstance(NetworkService.class); } catch (final InjectionException e) { throw new RuntimeException("Failed to instantiate NetworkService", e); injector.bindVolatileParameter(GroupCommSenderStage.class, senderStage); injector.bindVolatileParameter(DriverIdentifier.class, driverId); injector.bindVolatileParameter(GroupCommRunningTaskHandler.class, groupCommRunningTaskHandler); injector.bindVolatileParameter(GroupCommFailedTaskHandler.class, groupCommFailedTaskHandler); injector.bindVolatileParameter(GroupCommFailedEvalHandler.class, groupCommFailedEvaluatorHandler); injector.bindVolatileInstance(GroupCommMessageHandler.class, groupCommMessageHandler); commGroupDriverFactory = injector.getInstance(CommunicationGroupDriverFactory.class); } catch (final InjectionException e) { throw new RuntimeException(e);
/** * Initializes injector by copying needed handlers. * @param runtimeInjector The injector to initialize * @throws InjectionException on configuration error. */ private void initializeInjector(final Injector runtimeInjector) throws InjectionException { copyEventHandler(runtimeInjector, RuntimeParameters.ResourceStatusHandler.class); copyEventHandler(runtimeInjector, RuntimeParameters.NodeDescriptorHandler.class); copyEventHandler(runtimeInjector, RuntimeParameters.ResourceAllocationHandler.class); copyEventHandler(runtimeInjector, RuntimeParameters.RuntimeStatusHandler.class); try { runtimeInjector.bindVolatileInstance(HttpServer.class, this.originalInjector.getInstance(HttpServer.class)); LOG.log(Level.INFO, "Binding http server for the runtime implementation"); } catch (final InjectionException e) { LOG.log(Level.INFO, "Http Server is not configured for the runtime", e); } }
/** * @param args command line arguments. * @throws BindException configuration error. * @throws InjectionException configuration error. * @throws IOException cannot process command line parameters. */ public static void main(final String[] args) throws BindException, InjectionException, IOException { final Tang tang = Tang.Factory.getTang(); final ConfigurationBuilder confBuilder = tang.newConfigurationBuilder(); new CommandLine(confBuilder).processCommandLine(args); final Configuration config = confBuilder.build(); final Injector injector = tang.newInjector(config); final PrintTypeHierarchy myself = injector.getInstance(PrintTypeHierarchy.class); try (final Writer out = new OutputStreamWriter( new FileOutputStream("type-hierarchy.dot"), StandardCharsets.UTF_8)) { out.write(GraphvizConfigVisitor.getGraphvizString(config, true, true)); } final InjectionPlan<PrintTypeHierarchy> plan = injector.getInjectionPlan(PrintTypeHierarchy.class); try (final Writer out = new OutputStreamWriter( new FileOutputStream("injection-plan.dot"), StandardCharsets.UTF_8)) { out.write(GraphvizInjectionPlanVisitor.getGraphvizString(plan, true)); } System.out.println(myself); }
final WakeProfiler profiler = new WakeProfiler(); ProfilingStopHandler.setProfiler(profiler); injector.bindAspect(profiler); injector.getInstance(REEFVersion.class).logVersion(); final REEFErrorHandler errorHandler = injector.getInstance(REEFErrorHandler.class); final JobStatusHandler jobStatusHandler = injector.getInstance(JobStatusHandler.class); injector.getInstance(UserCredentials.class).set("reef-proxy", hostUser); } catch (final IOException ex) { final String msg = "Cannot copy user credentials: " + hostUser; final Clock clock = injector.getInstance(Clock.class); return new REEFEnvironment(clock, errorHandler, jobStatusHandler);
@Inject private CommunicationGroupDriverFactory( @Parameter(DriverIdentifier.class) final String driverId, @Parameter(GroupCommSenderStage.class) final EStage<GroupCommunicationMessage> senderStage, @Parameter(GroupCommRunningTaskHandler.class) final BroadcastingEventHandler<RunningTask> groupCommRunningTaskHandler, @Parameter(GroupCommFailedTaskHandler.class) final BroadcastingEventHandler<FailedTask> groupCommFailedTaskHandler, @Parameter(GroupCommFailedEvalHandler.class) final BroadcastingEventHandler<FailedEvaluator> groupCommFailedEvaluatorHandler, final GroupCommMessageHandler groupCommMessageHandler) { injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(GroupCommSenderStage.class, senderStage); injector.bindVolatileParameter(DriverIdentifier.class, driverId); injector.bindVolatileParameter(GroupCommRunningTaskHandler.class, groupCommRunningTaskHandler); injector.bindVolatileParameter(GroupCommFailedTaskHandler.class, groupCommFailedTaskHandler); injector.bindVolatileParameter(GroupCommFailedEvalHandler.class, groupCommFailedEvaluatorHandler); injector.bindVolatileInstance(GroupCommMessageHandler.class, groupCommMessageHandler); }
/** * Instantiates the root context. * <p> * This also launches the initial task if there is any. * * @param injector * @param rootContextConfiguration * @param rootServiceConfiguration * @return ContextRuntime * @throws ContextClientCodeException */ private static ContextRuntime getRootContext(final Injector injector, final Configuration rootContextConfiguration, final Optional<Configuration> rootServiceConfiguration) throws ContextClientCodeException { final ContextRuntime result; if (rootServiceConfiguration.isPresent()) { final Injector rootServiceInjector; try { rootServiceInjector = injector.forkInjector(rootServiceConfiguration.get()); } catch (final BindException e) { throw new ContextClientCodeException(ContextClientCodeException.getIdentifier(rootContextConfiguration), Optional.<String>empty(), "Unable to instatiate the root context", e); } result = new ContextRuntime(rootServiceInjector, rootContextConfiguration); } else { result = new ContextRuntime(injector.forkInjector(), rootContextConfiguration); } return result; }
@Inject private TopologyFactory(@Parameter(GroupCommSenderStage.class) final EStage<GroupCommunicationMessage> senderStage, @Parameter(CommGroupNameClass.class) final Class<? extends Name<String>> groupName, @Parameter(DriverIdentifier.class) final String driverId, @Parameter(CommGroupNumTask.class) final int numberOfTasks, @Parameter(TreeTopologyFanOut.class) final int fanOut) { injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(GroupCommSenderStage.class, senderStage); injector.bindVolatileParameter(CommGroupNameClass.class, groupName); injector.bindVolatileParameter(DriverIdentifier.class, driverId); injector.bindVolatileParameter(CommGroupNumTask.class, numberOfTasks); injector.bindVolatileParameter(TreeTopologyFanOut.class, fanOut); }
@Override public void registerRunTimeOptimizations(final Injector injector, final PubSubEventHandlerWrapper pubSubWrapper) { LOG.info("Register run-time optimizations to the PubSubHandler"); runtimePasses.forEach(runtimePass -> runtimePass.getEventHandlerClasses().forEach(runtimeEventHandlerClass -> { try { final RuntimeEventHandler runtimeEventHandler = injector.getInstance(runtimeEventHandlerClass); pubSubWrapper.getPubSubEventHandler() .subscribe(runtimeEventHandler.getEventClass(), runtimeEventHandler); } catch (final Exception e) { throw new RuntimeException(e); } })); } }
commGroupMessageHandler.addHandler(new TopologyMessageHandler(this)); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(CommGroupNameClass.class, groupName); injector.bindVolatileParameter(GroupCommSenderStage.class, senderStage); injector.bindVolatileParameter(DriverIdentifier.class, driverId); injector.bindVolatileParameter(CommGroupNumTask.class, numberOfTasks); injector.bindVolatileParameter(TreeTopologyFanOut.class, fanOut); try { topologyFactory = injector.getInstance(TopologyFactory.class); } catch (final InjectionException e) { throw new RuntimeException(e);
final Injector childServiceInjector = this.serviceInjector.forkInjector(); final ContextRuntime newChildContext = new ContextRuntime(childServiceInjector, contextConfiguration, Optional.of(this));
/** * @param baseInjector base {@link Injector} to extend upon * @return an {@link Injector} which has {@link LocalMessageDispatcher} for testing. * @throws InjectionException when failed to inject {@link LocalMessageDispatcher} */ public static Injector forkInjector(final Injector baseInjector) throws InjectionException { final Injector injector = baseInjector .forkInjector(LocalMessageEnvironment.LOCAL_MESSAGE_ENVIRONMENT_CONFIGURATION); injector.getInstance(LocalMessageDispatcher.class); return injector; } }
@Override public void registerRunTimeOptimizations(final Injector injector, final PubSubEventHandlerWrapper pubSubWrapper) { LOG.info("Register run-time optimizations to the PubSubHandler"); runtimePasses.forEach(runtimePass -> runtimePass.getEventHandlerClasses().forEach(runtimeEventHandlerClass -> { try { final RuntimeEventHandler runtimeEventHandler = injector.getInstance(runtimeEventHandlerClass); pubSubWrapper.getPubSubEventHandler() .subscribe(runtimeEventHandler.getEventClass(), runtimeEventHandler); } catch (final Exception e) { throw new RuntimeException(e); } })); } }