/** * Given a Twill app name, returns the id of the program that was used to construct this Twill app name. * * @throws IllegalArgumentException if the given app name does not match the {@link #APP_NAME_PATTERN}. */ public static ProgramId fromTwillAppName(String twillAppName) { return fromTwillAppName(twillAppName, true); }
@Nullable @Override public TwillController lookup(String applicationName, RunId runId) { ProgramId programId = TwillAppNames.fromTwillAppName(applicationName, false); if (programId == null) { return null; } return controllers.get(programId.run(runId)); }
@Override public Iterable<TwillController> lookup(String applicationName) { ProgramId programId = TwillAppNames.fromTwillAppName(applicationName, false); if (programId == null) { return Collections.emptyList(); } return controllers.entrySet().stream() .filter(entry -> programId.equals(entry.getKey().getParent())) .map(Map.Entry::getValue) .map(TwillController.class::cast) ::iterator; }
private Map<String, String> getMetricContext(TwillRunner.LiveInfo info) { ProgramId programId = TwillAppNames.fromTwillAppName(info.getApplicationName(), false); if (programId == null) { return null; } return getMetricsContext(programId.getType(), programId); } }
private Iterable<TwillController> wrapControllers(Iterable<TwillController> controllers, String applicationName) { if (isMasterService(applicationName)) { return controllers; } try { final ProgramId programId = TwillAppNames.fromTwillAppName(applicationName); return Iterables.transform(controllers, new Function<TwillController, TwillController>() { @Override public TwillController apply(TwillController controller) { return new ImpersonatedTwillController(controller, impersonator, programId); } }); } catch (IllegalArgumentException e) { // If the conversion from twill app name to programId failed, don't wrap return controllers; } }
@Override public TwillController lookup(String applicationName, RunId runId) { TwillController controller = delegate.lookup(applicationName, runId); if (isMasterService(applicationName)) { return controller; } try { return new ImpersonatedTwillController(controller, impersonator, TwillAppNames.fromTwillAppName(applicationName)); } catch (IllegalArgumentException e) { // If the conversion from twill app name to programId failed, don't wrap return controller; } }
@Override public Map<RunId, RuntimeInfo> list(ProgramType type) { Map<RunId, RuntimeInfo> result = new HashMap<>(); Lock lock = runtimeInfosLock.readLock(); lock.lock(); try { result.putAll(runtimeInfos.row(type)); } finally { lock.unlock(); } // Add any missing RuntimeInfo from the remote twill runner if (remoteTwillRunnerService == null) { return Collections.unmodifiableMap(result); } for (TwillRunner.LiveInfo liveInfo : remoteTwillRunnerService.lookupLive()) { ProgramId programId = TwillAppNames.fromTwillAppName(liveInfo.getApplicationName(), false); if (programId == null || !programId.getType().equals(type)) { continue; } for (TwillController controller : liveInfo.getControllers()) { // For remote twill runner, the twill run id and cdap run id are the same RunId runId = controller.getRunId(); if (result.computeIfAbsent(runId, rid -> createRuntimeInfo(programId, runId, controller)) == null) { LOG.warn("Unable to create runtime info for program {} with run id {}", programId, runId); } } } return Collections.unmodifiableMap(result); }
programId = TwillAppNames.fromTwillAppName(application); } catch (IllegalArgumentException e) {
ProgramId programId = TwillAppNames.fromTwillAppName(appName, false); if (programId == null) { continue;
@Test public void test() { ProgramId serviceId = new NamespaceId("test_ns").app("my_app").service("myservice"); String twillAppName = TwillAppNames.toTwillAppName(serviceId); Assert.assertEquals("service.test_ns.my_app.myservice", twillAppName); Assert.assertEquals(serviceId, TwillAppNames.fromTwillAppName(twillAppName)); // parsing from twill app name can be optional (return null) Assert.assertNull(TwillAppNames.fromTwillAppName(Constants.Service.MASTER_SERVICES, false)); try { // if passing true, throws exception, when parsing is not possible TwillAppNames.fromTwillAppName(Constants.Service.MASTER_SERVICES, true); Assert.fail("Expected not being able to parse "); } catch (IllegalArgumentException e) { // expected Assert.assertTrue(e.getMessage().contains("does not match pattern for programs")); } } }