Java Code Examples for org.apache.reef.tang.Injector#forkInjector()
The following examples show how to use
org.apache.reef.tang.Injector#forkInjector() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataTransferTest.java From incubator-nemo with Apache License 2.0 | 6 votes |
private Injector createNameClientInjector() { try { final Configuration configuration = TANG.newConfigurationBuilder() .bindImplementation(IdentifierFactory.class, StringIdentifierFactory.class) .build(); final Injector injector = TANG.newInjector(configuration); final LocalAddressProvider localAddressProvider = injector.getInstance(LocalAddressProvider.class); final NameServer nameServer = injector.getInstance(NameServer.class); final Configuration nameClientConfiguration = NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, localAddressProvider.getLocalAddress()) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServer.getPort()) .build(); return injector.forkInjector(nameClientConfiguration); } catch (final InjectionException e) { throw new RuntimeException(e); } }
Example 2
Source File: DataTransferTest.java From nemo with Apache License 2.0 | 6 votes |
private Injector createNameClientInjector() { try { final Configuration configuration = TANG.newConfigurationBuilder() .bindImplementation(IdentifierFactory.class, StringIdentifierFactory.class) .build(); final Injector injector = TANG.newInjector(configuration); final LocalAddressProvider localAddressProvider = injector.getInstance(LocalAddressProvider.class); final NameServer nameServer = injector.getInstance(NameServer.class); final Configuration nameClientConfiguration = NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, localAddressProvider.getLocalAddress()) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServer.getPort()) .build(); return injector.forkInjector(nameClientConfiguration); } catch (final InjectionException e) { throw new RuntimeException(e); } }
Example 3
Source File: GroupCommClientImpl.java From reef with Apache License 2.0 | 6 votes |
@Inject private GroupCommClientImpl(@Parameter(SerializedGroupConfigs.class) final Set<String> groupConfigs, final GroupCommNetworkHandler groupCommNetworkHandler, final ConfigurationSerializer configSerializer, final Injector injector) { LOG.log(Level.FINEST, "GroupCommHandler-{0}", groupCommNetworkHandler); for (final String groupConfigStr : groupConfigs) { try { final Configuration groupConfig = configSerializer.fromString(groupConfigStr); final Injector forkedInjector = injector.forkInjector(groupConfig); final CommunicationGroupServiceClient commGroupClient = forkedInjector.getInstance(CommunicationGroupServiceClient.class); this.communicationGroups.put(commGroupClient.getName(), commGroupClient); } catch (final InjectionException | IOException e) { throw new RuntimeException("Unable to deserialize operator config", e); } } }
Example 4
Source File: NetworkMessagingTestService.java From reef with Apache License 2.0 | 6 votes |
public NetworkMessagingTestService(final String localAddress) throws InjectionException { // name server final Injector injector = Tang.Factory.getTang().newInjector(); this.nameServer = injector.getInstance(NameServer.class); final Configuration netConf = NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServer.getPort()) .build(); LOG.log(Level.FINEST, "=== Test network connection service receiver start"); // network service for receiver final Injector injectorReceiver = injector.forkInjector(netConf); this.receiverNetworkConnService = injectorReceiver.getInstance(NetworkConnectionService.class); this.factory = injectorReceiver.getNamedInstance(NetworkConnectionServiceIdFactory.class); // network service for sender LOG.log(Level.FINEST, "=== Test network connection service sender start"); final Injector injectorSender = injector.forkInjector(netConf); senderNetworkConnService = injectorSender.getInstance(NetworkConnectionService.class); }
Example 5
Source File: RootContextLauncher.java From reef with Apache License 2.0 | 6 votes |
/** * 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; }
Example 6
Source File: LocalMessageDispatcher.java From incubator-nemo with Apache License 2.0 | 5 votes |
/** * @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; }
Example 7
Source File: DataTransferTest.java From incubator-nemo with Apache License 2.0 | 5 votes |
private Pair<BlockManagerWorker, IntermediateDataIOFactory> createWorker( final String executorId, final Injector dispatcherInjector, final Injector nameClientInjector) throws InjectionException { final Injector messageEnvironmentInjector = LocalMessageEnvironment.forkInjector(dispatcherInjector, executorId); final MessageEnvironment messageEnvironment = messageEnvironmentInjector.getInstance(MessageEnvironment.class); final PersistentConnectionToMasterMap conToMaster = messageEnvironmentInjector .getInstance(PersistentConnectionToMasterMap.class); final Configuration executorConfiguration = TANG.newConfigurationBuilder() .bindNamedParameter(JobConf.ExecutorId.class, executorId) .bindNamedParameter(MessageParameters.SenderId.class, executorId) .bindNamedParameter(JobConf.ExecutorMemoryMb.class, "640") .bindNamedParameter(JobConf.MaxOffheapRatio.class, "0.2") .build(); final Injector injector = nameClientInjector.forkInjector(executorConfiguration); injector.bindVolatileInstance(MessageEnvironment.class, messageEnvironment); injector.bindVolatileInstance(PersistentConnectionToMasterMap.class, conToMaster); injector.bindVolatileParameter(JobConf.FileDirectory.class, TMP_LOCAL_FILE_DIRECTORY); injector.bindVolatileParameter(JobConf.GlusterVolumeDirectory.class, TMP_REMOTE_FILE_DIRECTORY); final BlockManagerWorker blockManagerWorker; final SerializerManager serializerManager; final IntermediateDataIOFactory intermediateDataIOFactory; try { blockManagerWorker = injector.getInstance(BlockManagerWorker.class); serializerManager = injector.getInstance(SerializerManager.class); serializerManagers.put(blockManagerWorker, serializerManager); intermediateDataIOFactory = injector.getInstance(IntermediateDataIOFactory.class); } catch (final InjectionException e) { throw new RuntimeException(e); } // Unused, but necessary for wiring up the message environments injector.getInstance(Executor.class); return Pair.of(blockManagerWorker, intermediateDataIOFactory); }
Example 8
Source File: DataTransferTest.java From nemo with Apache License 2.0 | 5 votes |
private BlockManagerWorker createWorker(final String executorId, final LocalMessageDispatcher messageDispatcher, final Injector nameClientInjector) { final LocalMessageEnvironment messageEnvironment = new LocalMessageEnvironment(executorId, messageDispatcher); final PersistentConnectionToMasterMap conToMaster = new PersistentConnectionToMasterMap(messageEnvironment); final Configuration executorConfiguration = TANG.newConfigurationBuilder() .bindNamedParameter(JobConf.ExecutorId.class, executorId) .bindNamedParameter(MessageParameters.SenderId.class, executorId) .build(); final Injector injector = nameClientInjector.forkInjector(executorConfiguration); injector.bindVolatileInstance(MessageEnvironment.class, messageEnvironment); injector.bindVolatileInstance(PersistentConnectionToMasterMap.class, conToMaster); injector.bindVolatileParameter(JobConf.FileDirectory.class, TMP_LOCAL_FILE_DIRECTORY); injector.bindVolatileParameter(JobConf.GlusterVolumeDirectory.class, TMP_REMOTE_FILE_DIRECTORY); final BlockManagerWorker blockManagerWorker; final MetricManagerWorker metricManagerWorker; final SerializerManager serializerManager; try { blockManagerWorker = injector.getInstance(BlockManagerWorker.class); metricManagerWorker = injector.getInstance(MetricManagerWorker.class); serializerManager = injector.getInstance(SerializerManager.class); serializerManagers.put(blockManagerWorker, serializerManager); } catch (final InjectionException e) { throw new RuntimeException(e); } // Unused, but necessary for wiring up the message environments final Executor executor = new Executor( executorId, EXECUTOR_CAPACITY, conToMaster, messageEnvironment, serializerManager, new DataTransferFactory(HASH_RANGE_MULTIPLIER, blockManagerWorker), metricManagerWorker); injector.bindVolatileInstance(Executor.class, executor); return blockManagerWorker; }
Example 9
Source File: ContextRuntime.java From reef with Apache License 2.0 | 5 votes |
/** * 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(); }
Example 10
Source File: RuntimesHost.java From reef with Apache License 2.0 | 5 votes |
/** * Initializes the configured runtimes. */ private synchronized void initialize() { if (this.runtimes != null) { return; } this.runtimes = new HashMap<>(); for (final AvroRuntimeDefinition rd : runtimeDefinition.getRuntimes()) { try { // We need to create different injector for each runtime as they define conflicting bindings. Also we cannot // fork the original injector because of the same reason. // We create new injectors and copy form the original injector what we need. // rootInjector is an emptyInjector that we copy bindings from the original injector into. Then we fork // it to instantiate the actual runtime. final Injector rootInjector = Tang.Factory.getTang().newInjector(); initializeInjector(rootInjector); final Configuration runtimeConfig = Tang.Factory.getTang().newConfigurationBuilder() .bindNamedParameter(RuntimeName.class, rd.getRuntimeName().toString()) .bindImplementation(Runtime.class, RuntimeImpl.class) .build(); final Configuration config = new AvroConfigurationSerializer().fromString(rd.getSerializedConfiguration().toString()); final Injector runtimeInjector = rootInjector.forkInjector(config, runtimeConfig); this.runtimes.put(rd.getRuntimeName().toString(), runtimeInjector.getInstance(Runtime.class)); } catch (final IOException | InjectionException e) { throw new RuntimeException("Unable to initialize runtimes.", e); } } }
Example 11
Source File: CommunicationGroupClientImpl.java From reef with Apache License 2.0 | 4 votes |
@Inject private CommunicationGroupClientImpl(@Parameter(CommunicationGroupName.class) final String groupName, @Parameter(TaskConfigurationOptions.Identifier.class) final String taskId, @Parameter(DriverIdentifierGroupComm.class) final String driverId, final GroupCommNetworkHandler groupCommNetworkHandler, @Parameter(SerializedOperConfigs.class) final Set<String> operatorConfigs, final ConfigurationSerializer configSerializer, final NetworkService<GroupCommunicationMessage> netService, final CommGroupNetworkHandler commGroupNetworkHandler, final Injector injector) { this.taskId = taskId; this.driverId = driverId; LOG.finest(groupName + " has GroupCommHandler-" + groupCommNetworkHandler.toString()); this.identifierFactory = netService.getIdentifierFactory(); this.groupName = Utils.getClass(groupName); this.groupCommNetworkHandler = groupCommNetworkHandler; this.commGroupNetworkHandler = commGroupNetworkHandler; this.sender = new Sender(netService); this.operators = new TreeMap<>(new Comparator<Class<? extends Name<String>>>() { @Override public int compare(final Class<? extends Name<String>> o1, final Class<? extends Name<String>> o2) { final String s1 = o1.getSimpleName(); final String s2 = o2.getSimpleName(); return s1.compareTo(s2); } }); try { this.groupCommNetworkHandler.register(this.groupName, commGroupNetworkHandler); boolean operatorIsScatterSender = false; for (final String operatorConfigStr : operatorConfigs) { final Configuration operatorConfig = configSerializer.fromString(operatorConfigStr); 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()); if (!operatorIsScatterSender && operator instanceof Scatter.Sender) { LOG.fine(operName + " is a scatter sender. Will keep track of active slave tasks."); operatorIsScatterSender = true; } } this.isScatterSender = operatorIsScatterSender; } catch (final InjectionException | IOException e) { throw new RuntimeException("Unable to deserialize operator config", e); } }
Example 12
Source File: NetworkServiceTest.java From reef with Apache License 2.0 | 4 votes |
/** * NetworkService messaging test. */ @Test public void testMessagingNetworkService() throws Exception { LOG.log(Level.FINEST, name.getMethodName()); final IdentifierFactory factory = new StringIdentifierFactory(); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(NameServerParameters.NameServerIdentifierFactory.class, factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (NameServer server = injector.getInstance(NameServer.class)) { final int nameServerPort = server.getPort(); final int numMessages = 10; final Monitor monitor = new Monitor(); // network service final String name2 = "task2"; final String name1 = "task1"; final Configuration nameResolverConf = Tang.Factory.getTang().newConfigurationBuilder(NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, this.localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServerPort) .build()) .build(); final Injector injector2 = Tang.Factory.getTang().newInjector(nameResolverConf); LOG.log(Level.FINEST, "=== Test network service receiver start"); LOG.log(Level.FINEST, "=== Test network service sender start"); try (NameResolver nameResolver = injector2.getInstance(NameResolver.class)) { injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class, factory); injector2.bindVolatileInstance(NameResolver.class, nameResolver); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceCodec.class, new StringCodec()); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceTransportFactory.class, injector.getInstance(MessagingTransportFactory.class)); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceExceptionHandler.class, new ExceptionHandler()); final Injector injectorNs2 = injector2.forkInjector(); injectorNs2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name2, monitor, numMessages)); final NetworkService<String> ns2 = injectorNs2.getInstance(NetworkService.class); final Injector injectorNs1 = injector2.forkInjector(); injectorNs1.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name1, null, 0)); final NetworkService<String> ns1 = injectorNs1.getInstance(NetworkService.class); ns2.registerId(factory.getNewInstance(name2)); final int port2 = ns2.getTransport().getListeningPort(); server.register(factory.getNewInstance("task2"), new InetSocketAddress(this.localAddress, port2)); ns1.registerId(factory.getNewInstance(name1)); final int port1 = ns1.getTransport().getListeningPort(); server.register(factory.getNewInstance("task1"), new InetSocketAddress(this.localAddress, port1)); final Identifier destId = factory.getNewInstance(name2); try (Connection<String> conn = ns1.newConnection(destId)) { conn.open(); for (int count = 0; count < numMessages; ++count) { conn.write("hello! " + count); } monitor.mwait(); } catch (final NetworkException e) { e.printStackTrace(); throw new RuntimeException(e); } } } }
Example 13
Source File: NetworkServiceTest.java From reef with Apache License 2.0 | 4 votes |
/** * NetworkService messaging rate benchmark. */ @Test public void testMessagingNetworkServiceRate() throws Exception { Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST)); LOG.log(Level.FINEST, name.getMethodName()); final IdentifierFactory factory = new StringIdentifierFactory(); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(NameServerParameters.NameServerIdentifierFactory.class, factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (NameServer server = injector.getInstance(NameServer.class)) { final int nameServerPort = server.getPort(); final int[] messageSizes = {1, 16, 32, 64, 512, 64 * 1024, 1024 * 1024}; for (final int size : messageSizes) { final int numMessages = 300000 / (Math.max(1, size / 512)); final Monitor monitor = new Monitor(); // network service final String name2 = "task2"; final String name1 = "task1"; final Configuration nameResolverConf = Tang.Factory.getTang().newConfigurationBuilder(NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, this.localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServerPort) .build()) .build(); final Injector injector2 = Tang.Factory.getTang().newInjector(nameResolverConf); LOG.log(Level.FINEST, "=== Test network service receiver start"); LOG.log(Level.FINEST, "=== Test network service sender start"); try (NameResolver nameResolver = injector2.getInstance(NameResolver.class)) { injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class, factory); injector2.bindVolatileInstance(NameResolver.class, nameResolver); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceCodec.class, new StringCodec()); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceTransportFactory.class, injector.getInstance(MessagingTransportFactory.class)); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceExceptionHandler.class, new ExceptionHandler()); final Injector injectorNs2 = injector2.forkInjector(); injectorNs2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name2, monitor, numMessages)); final NetworkService<String> ns2 = injectorNs2.getInstance(NetworkService.class); final Injector injectorNs1 = injector2.forkInjector(); injectorNs1.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name1, null, 0)); final NetworkService<String> ns1 = injectorNs1.getInstance(NetworkService.class); ns2.registerId(factory.getNewInstance(name2)); final int port2 = ns2.getTransport().getListeningPort(); server.register(factory.getNewInstance("task2"), new InetSocketAddress(this.localAddress, port2)); ns1.registerId(factory.getNewInstance(name1)); final int port1 = ns1.getTransport().getListeningPort(); server.register(factory.getNewInstance("task1"), new InetSocketAddress(this.localAddress, port1)); final Identifier destId = factory.getNewInstance(name2); final String message = StringUtils.repeat('1', size); final long start = System.currentTimeMillis(); try (Connection<String> conn = ns1.newConnection(destId)) { conn.open(); for (int i = 0; i < numMessages; i++) { conn.write(message); } monitor.mwait(); } catch (final NetworkException e) { e.printStackTrace(); throw new RuntimeException(e); } final long end = System.currentTimeMillis(); final double runtime = ((double) end - start) / 1000; LOG.log(Level.FINEST, "size: " + size + "; messages/s: " + numMessages / runtime + " bandwidth(bytes/s): " + ((double) numMessages * 2 * size) / runtime); // x2 for unicode chars } } } }
Example 14
Source File: NetworkServiceTest.java From reef with Apache License 2.0 | 4 votes |
@Test public void testMultithreadedSharedConnMessagingNetworkServiceRate() throws Exception { Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST)); LOG.log(Level.FINEST, name.getMethodName()); final IdentifierFactory factory = new StringIdentifierFactory(); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(NameServerParameters.NameServerIdentifierFactory.class, factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (NameServer server = injector.getInstance(NameServer.class)) { final int nameServerPort = server.getPort(); final int[] messageSizes = {2000}; // {1,16,32,64,512,64*1024,1024*1024}; for (final int size : messageSizes) { final int numMessages = 300000 / (Math.max(1, size / 512)); final int numThreads = 2; final int totalNumMessages = numMessages * numThreads; final Monitor monitor = new Monitor(); // network service final String name2 = "task2"; final String name1 = "task1"; final Configuration nameResolverConf = Tang.Factory.getTang().newConfigurationBuilder(NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, this.localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServerPort) .build()) .build(); final Injector injector2 = Tang.Factory.getTang().newInjector(nameResolverConf); LOG.log(Level.FINEST, "=== Test network service receiver start"); LOG.log(Level.FINEST, "=== Test network service sender start"); try (NameResolver nameResolver = injector2.getInstance(NameResolver.class)) { injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class, factory); injector2.bindVolatileInstance(NameResolver.class, nameResolver); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceCodec.class, new StringCodec()); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceTransportFactory.class, injector.getInstance(MessagingTransportFactory.class)); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceExceptionHandler.class, new ExceptionHandler()); final Injector injectorNs2 = injector2.forkInjector(); injectorNs2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name2, monitor, totalNumMessages)); final NetworkService<String> ns2 = injectorNs2.getInstance(NetworkService.class); final Injector injectorNs1 = injector2.forkInjector(); injectorNs1.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name1, null, 0)); final NetworkService<String> ns1 = injectorNs1.getInstance(NetworkService.class); ns2.registerId(factory.getNewInstance(name2)); final int port2 = ns2.getTransport().getListeningPort(); server.register(factory.getNewInstance("task2"), new InetSocketAddress(this.localAddress, port2)); ns1.registerId(factory.getNewInstance(name1)); final int port1 = ns1.getTransport().getListeningPort(); server.register(factory.getNewInstance("task1"), new InetSocketAddress(this.localAddress, port1)); final Identifier destId = factory.getNewInstance(name2); try (Connection<String> conn = ns1.newConnection(destId)) { conn.open(); final String message = StringUtils.repeat('1', size); final ExecutorService e = Executors.newCachedThreadPool(); final long start = System.currentTimeMillis(); for (int i = 0; i < numThreads; i++) { e.submit(new Runnable() { @Override public void run() { for (int i = 0; i < numMessages; i++) { conn.write(message); } } }); } e.shutdown(); e.awaitTermination(30, TimeUnit.SECONDS); monitor.mwait(); final long end = System.currentTimeMillis(); final double runtime = ((double) end - start) / 1000; LOG.log(Level.FINEST, "size: " + size + "; messages/s: " + totalNumMessages / runtime + " bandwidth(bytes/s): " + ((double) totalNumMessages * 2 * size) / runtime); // x2 for unicode chars } } } } }
Example 15
Source File: NetworkServiceTest.java From reef with Apache License 2.0 | 4 votes |
/** * NetworkService messaging rate benchmark. */ @Test public void testMessagingNetworkServiceBatchingRate() throws Exception { Assume.assumeFalse("Use log level INFO to run benchmarking", LOG.isLoggable(Level.FINEST)); LOG.log(Level.FINEST, name.getMethodName()); final IdentifierFactory factory = new StringIdentifierFactory(); final Injector injector = Tang.Factory.getTang().newInjector(); injector.bindVolatileParameter(NameServerParameters.NameServerIdentifierFactory.class, factory); injector.bindVolatileInstance(LocalAddressProvider.class, this.localAddressProvider); try (NameServer server = injector.getInstance(NameServer.class)) { final int nameServerPort = server.getPort(); final int batchSize = 1024 * 1024; final int[] messageSizes = {32, 64, 512}; for (final int size : messageSizes) { final int numMessages = 300 / (Math.max(1, size / 512)); final Monitor monitor = new Monitor(); // network service final String name2 = "task2"; final String name1 = "task1"; final Configuration nameResolverConf = Tang.Factory.getTang().newConfigurationBuilder(NameResolverConfiguration.CONF .set(NameResolverConfiguration.NAME_SERVER_HOSTNAME, this.localAddress) .set(NameResolverConfiguration.NAME_SERVICE_PORT, nameServerPort) .build()) .build(); final Injector injector2 = Tang.Factory.getTang().newInjector(nameResolverConf); LOG.log(Level.FINEST, "=== Test network service receiver start"); LOG.log(Level.FINEST, "=== Test network service sender start"); try (NameResolver nameResolver = injector2.getInstance(NameResolver.class)) { injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class, factory); injector2.bindVolatileInstance(NameResolver.class, nameResolver); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceCodec.class, new StringCodec()); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceTransportFactory.class, injector.getInstance(MessagingTransportFactory.class)); injector2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceExceptionHandler.class, new ExceptionHandler()); final Injector injectorNs2 = injector2.forkInjector(); injectorNs2.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name2, monitor, numMessages)); final NetworkService<String> ns2 = injectorNs2.getInstance(NetworkService.class); final Injector injectorNs1 = injector2.forkInjector(); injectorNs1.bindVolatileParameter(NetworkServiceParameters.NetworkServiceHandler.class, new MessageHandler<String>(name1, null, 0)); final NetworkService<String> ns1 = injectorNs1.getInstance(NetworkService.class); ns2.registerId(factory.getNewInstance(name2)); final int port2 = ns2.getTransport().getListeningPort(); server.register(factory.getNewInstance("task2"), new InetSocketAddress(this.localAddress, port2)); ns1.registerId(factory.getNewInstance(name1)); final int port1 = ns1.getTransport().getListeningPort(); server.register(factory.getNewInstance("task1"), new InetSocketAddress(this.localAddress, port1)); final Identifier destId = factory.getNewInstance(name2); final String message = StringUtils.repeat('1', batchSize); final long start = System.currentTimeMillis(); try (Connection<String> conn = ns1.newConnection(destId)) { conn.open(); for (int i = 0; i < numMessages; i++) { conn.write(message); } monitor.mwait(); } catch (final NetworkException e) { e.printStackTrace(); throw new RuntimeException(e); } final long end = System.currentTimeMillis(); final double runtime = ((double) end - start) / 1000; final long numAppMessages = numMessages * batchSize / size; LOG.log(Level.FINEST, "size: " + size + "; messages/s: " + numAppMessages / runtime + " bandwidth(bytes/s): " + ((double) numAppMessages * 2 * size) / runtime); // x2 for unicode chars } } } }
Example 16
Source File: LocalMessageEnvironment.java From incubator-nemo with Apache License 2.0 | 3 votes |
/** * Extends {@code baseInjector} to have {@link LocalMessageEnvironment} instance for the given {@code senderId}. * * @param baseInjector provided by {@link LocalMessageDispatcher#getInjector()} * or {@link LocalMessageDispatcher#forkInjector(Injector)} * @param senderId the identifier for the sender * @return an {@link Injector} which has {@link LocalMessageDispatcher} instance for {@link MessageEnvironment} * @throws InjectionException when fails to inject {@link MessageEnvironment} */ public static Injector forkInjector(final Injector baseInjector, final String senderId) throws InjectionException { final Injector injector = baseInjector.forkInjector(TANG.newConfigurationBuilder() .bindNamedParameter(MessageParameters.SenderId.class, senderId).build()); injector.getInstance(MessageEnvironment.class); return injector; }