@Override public int getInstanceId() { return twillContext.getInstanceId(); }
/** * Returns the metric context. A metric context is of the form {flowY}.{flowletZ}. */ private String getMetricContext(Program program, TwillContext context) { String metricContext = program.getName(); metricContext += "." + context.getSpecification().getName() + "." + context.getInstanceId(); return metricContext; } }
/** * Returns a set of extra system arguments that will be available through the {@link ProgramOptions#getArguments()} * for the program execution. */ protected Map<String, String> getExtraSystemArguments() { Map<String, String> args = new HashMap<>(); args.put(ProgramOptionConstants.INSTANCE_ID, context == null ? "0" : Integer.toString(context.getInstanceId())); args.put(ProgramOptionConstants.INSTANCES, context == null ? "1" : Integer.toString(context.getInstanceCount())); args.put(ProgramOptionConstants.TWILL_RUN_ID, context.getApplicationRunId().getId()); args.put(ProgramOptionConstants.HOST, context.getHost().getCanonicalHostName()); return args; }
@Override public void reportResources() { sendMetrics(metricContext, 1, runContext.getMaxMemoryMB(), runContext.getVirtualCores()); }
/** * Creates program arguments. It includes all configurations from the specification, excluding hConf and cConf. */ private Arguments createProgramArguments(TwillContext context, Map<String, String> configs) { Map<String, String> args = ImmutableMap.<String, String>builder() .put(ProgramOptionConstants.INSTANCE_ID, Integer.toString(context.getInstanceId())) .put(ProgramOptionConstants.INSTANCES, Integer.toString(context.getInstanceCount())) .put(ProgramOptionConstants.RUN_ID, context.getApplicationRunId().getId()) .putAll(Maps.filterKeys(configs, Predicates.not(Predicates.in(ImmutableSet.of("hConf", "cConf"))))) .build(); return new BasicArguments(args); }
@Override protected Injector doInit(TwillContext context) { // Set the hostname of the machine so that cConf can be used to start internal services getCConfiguration().set(Constants.Metrics.ADDRESS, context.getHost().getCanonicalHostName()); LOG.info("{} Setting host name to {}", name, context.getHost().getCanonicalHostName()); String txClientId = String.format("cdap.service.%s.%d", Constants.Service.METRICS, context.getInstanceId()); injector = createGuiceInjector(getCConfiguration(), getConfiguration(), txClientId); injector.getInstance(LogAppenderInitializer.class).initialize(); LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.METRICS)); return injector; }
@Override public void initialize(TwillContext context) { super.initialize(context); running = true; try { serverSocket = new ServerSocket(0); LOG.info("Server started: " + serverSocket.getLocalSocketAddress() + ", id: " + context.getInstanceId() + ", count: " + context.getInstanceCount()); // Announce with service names as specified in app arguments and runnable arguments final List<Cancellable> cancellables = new ArrayList<>(); for (String[] args : new String[][] {context.getApplicationArguments(), context.getArguments()}) { if (args.length > 0) { cancellables.add(context.announce(args[0], serverSocket.getLocalPort())); } } canceller = new Cancellable() { @Override public void cancel() { for (Cancellable c : cancellables) { c.cancel(); } } }; } catch (IOException e) { throw Throwables.propagate(e); } }
@Override public void initialize(TwillContext context) { this.context = context; this.args = context.getSpecification().getConfigs(); }
@Override public final void initialize(TwillContext context) { this.context = context; if (name == null) { name = context.getSpecification().getName(); } LOG.info("Initializing runnable: " + name); try { doInitialize(new File(context.getApplicationArguments()[0])); LOG.info("Runnable initialized: {}", name); } catch (Throwable t) { LOG.error(t.getMessage(), t); throw Throwables.propagate(t); } }
@Override public void run() { runThread = Thread.currentThread(); getContext().announce("sleep", Integer.parseInt(getContext().getSpecification().getConfigs().get("port"))); try { TimeUnit.SECONDS.sleep(120); } catch (InterruptedException e) { // Ignore. } }
/** * Returns the metric context. A metric context is of the form * {applicationId}.{programTypeId}.{programId}.{componentId}. So for flows, it will look like * appX.f.flowY.flowletZ. For mapreduce jobs, appX.b.mapredY.{optional m|r}. */ private static Map<String, String> getMetricContext(ProgramId programId, TwillContext context) { Map<String, String> tags = new HashMap<>(); tags.put(Constants.Metrics.Tag.NAMESPACE, programId.getNamespace()); tags.put(Constants.Metrics.Tag.RUN_ID, context.getRunId().getId()); tags.put(Constants.Metrics.Tag.APP, programId.getApplication()); tags.put(ProgramTypeMetricTag.getTagName(programId.getType()), programId.getProgram()); if (programId.getType() == ProgramType.FLOW) { tags.put(Constants.Metrics.Tag.FLOWLET, context.getSpecification().getName()); } return tags; } }
@Override public int getInstanceCount() { return twillContext.getInstanceCount(); } }
@Override public void run() { final BlockingQueue<Discoverable> discoverables = new LinkedBlockingQueue<>(); ServiceDiscovered serviceDiscovered = getContext().discover(getContext().getApplicationArguments()[0]); serviceDiscovered.watchChanges(new ServiceDiscovered.ChangeListener() { @Override Cancellable cancellable = getContext().announce("discovered", 12345); completion.await(); cancellable.cancel();
@Override protected void doInit(TwillContext context) { try { getCConfiguration().set(Constants.Transaction.ADDRESS, context.getHost().getCanonicalHostName()); LOG.info("Initializing runnable {}", name); Injector injector = createGuiceInjector(getCConfiguration(), getConfiguration()); //Get Zookeeper Client Instances zkClient = injector.getInstance(ZKClientService.class); // Get the metrics collection service metricsCollectionService = injector.getInstance(MetricsCollectionService.class); // Get the Transaction Service txService = injector.getInstance(TransactionService.class); LOG.info("Runnable initialized {}", name); } catch (Throwable t) { LOG.error(t.getMessage(), t); throw Throwables.propagate(t); } }
@Override public final void initialize(TwillContext context) { arguments = Arguments.fromArray(context.getArguments()); final File jarFile = new File(arguments.getJarFileName()); Preconditions.checkArgument(jarFile != null, "Jar file %s cannot be null", jarFile.getAbsolutePath()); Preconditions.checkArgument(jarFile.exists(), "Jar file %s must exist", jarFile.getAbsolutePath()); Preconditions.checkArgument(jarFile.canRead(), "Jar file %s must be readable", jarFile.getAbsolutePath()); jarRunner = loadJarRunner(jarFile, arguments); }
@Override public void run() { try { File startedFile = new File(getContext().getApplicationArguments()[0], STARTED_FILE); // CompleteRunnable#run() method should be called after EventHandler#started() method is called if (startedFile.exists()) { new File(startedFile.getParent(), RUN_FILE).createNewFile(); } } catch (IOException e) { throw Throwables.propagate(e); } } }
@Override protected Injector doInit(TwillContext context) { getCConfiguration().set(Constants.Transaction.Container.ADDRESS, context.getHost().getCanonicalHostName()); // Set the hostname of the machine so that cConf can be used to start internal services LOG.info("{} Setting host name to {}", name, context.getHost().getCanonicalHostName()); String txClientId = String.format("cdap.service.%s.%d", Constants.Service.TRANSACTION, context.getInstanceId()); injector = createGuiceInjector(getCConfiguration(), getConfiguration(), txClientId); injector.getInstance(LogAppenderInitializer.class).initialize(); LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.TRANSACTION)); return injector; }
@Override public void initialize(TwillContext context) { this.context = context; this.args = context.getSpecification().getConfigs(); }
@Override public final void initialize(TwillContext context) { this.context = context; if (name == null) { name = context.getSpecification().getName(); } LOG.info("Initializing runnable: " + name); try { doInitialize(new File(context.getApplicationArguments()[0])); LOG.info("Runnable initialized: {}", name); } catch (Throwable t) { LOG.error(t.getMessage(), t); throw Throwables.propagate(t); } }
@Override public void run() { String runnableName = getContext().getSpecification().getName(); String serviceName = System.getProperty("service.name"); LOG.info("Announcing with name {} for runnable {}", serviceName, runnableName); // Compute a unique port name based on runnable name (running names are r[0-9]+) getContext().announce(serviceName, 12345 + Integer.parseInt(runnableName.substring(1))); try { stopLatch.await(); } catch (InterruptedException e) { LOG.warn("Run thread interrupted", e); } }