use of org.apache.twill.api.TwillController in project cdap by caskdata.
the class DistributedProgramRuntimeService method list.
@Override
public synchronized Map<RunId, RuntimeInfo> list(ProgramType type) {
Map<RunId, RuntimeInfo> result = Maps.newHashMap();
result.putAll(super.list(type));
// Table holds the Twill RunId and TwillController associated with the program matching the input type
Table<ProgramId, RunId, TwillController> twillProgramInfo = HashBasedTable.create();
// Goes through all live application and fill the twillProgramInfo table
for (TwillRunner.LiveInfo liveInfo : twillRunner.lookupLive()) {
String appName = liveInfo.getApplicationName();
ProgramId programId = TwillAppNames.fromTwillAppName(appName, false);
if (programId == null) {
continue;
}
if (!type.equals(programId.getType())) {
continue;
}
for (TwillController controller : liveInfo.getControllers()) {
RunId twillRunId = controller.getRunId();
if (isTwillRunIdCached(twillRunId)) {
continue;
}
twillProgramInfo.put(programId, twillRunId, controller);
}
}
if (twillProgramInfo.isEmpty()) {
return ImmutableMap.copyOf(result);
}
final Set<RunId> twillRunIds = twillProgramInfo.columnKeySet();
Collection<RunRecordMeta> activeRunRecords = store.getRuns(ProgramRunStatus.RUNNING, new Predicate<RunRecordMeta>() {
@Override
public boolean apply(RunRecordMeta record) {
return record.getTwillRunId() != null && twillRunIds.contains(org.apache.twill.internal.RunIds.fromString(record.getTwillRunId()));
}
}).values();
for (RunRecordMeta record : activeRunRecords) {
String twillRunId = record.getTwillRunId();
if (twillRunId == null) {
// This is unexpected. Just log and ignore the run record
LOG.warn("No twill runId for in run record {}.", record);
continue;
}
RunId twillRunIdFromRecord = org.apache.twill.internal.RunIds.fromString(twillRunId);
// Get the CDAP RunId from RunRecord
RunId runId = RunIds.fromString(record.getPid());
// Get the Program and TwillController for the current twillRunId
Map<ProgramId, TwillController> mapForTwillId = twillProgramInfo.columnMap().get(twillRunIdFromRecord);
Map.Entry<ProgramId, TwillController> entry = mapForTwillId.entrySet().iterator().next();
// Create RuntimeInfo for the current Twill RunId
RuntimeInfo runtimeInfo = createRuntimeInfo(entry.getKey(), entry.getValue(), runId);
if (runtimeInfo != null) {
result.put(runId, runtimeInfo);
updateRuntimeInfo(type, runId, runtimeInfo);
} else {
LOG.warn("Unable to find program {} {}", type, entry.getKey());
}
}
return ImmutableMap.copyOf(result);
}
use of org.apache.twill.api.TwillController in project cdap by caskdata.
the class DistributedProgramRuntimeService method lookup.
@Override
public synchronized RuntimeInfo lookup(ProgramId programId, final RunId runId) {
RuntimeInfo runtimeInfo = super.lookup(programId, runId);
if (runtimeInfo != null) {
return runtimeInfo;
}
// Goes through all live application and fill the twillProgramInfo table
for (TwillRunner.LiveInfo liveInfo : twillRunner.lookupLive()) {
String appName = liveInfo.getApplicationName();
ProgramId id = TwillAppNames.fromTwillAppName(appName, false);
if (id == null) {
continue;
}
if (!id.equals(programId)) {
continue;
}
// Program matched
RunRecordMeta record = store.getRun(programId, runId.getId());
if (record == null) {
return null;
}
if (record.getTwillRunId() == null) {
LOG.warn("Twill RunId does not exist for the program {}, runId {}", programId, runId.getId());
return null;
}
RunId twillRunIdFromRecord = org.apache.twill.internal.RunIds.fromString(record.getTwillRunId());
for (TwillController controller : liveInfo.getControllers()) {
RunId twillRunId = controller.getRunId();
if (!twillRunId.equals(twillRunIdFromRecord)) {
continue;
}
runtimeInfo = createRuntimeInfo(programId, controller, runId);
if (runtimeInfo != null) {
updateRuntimeInfo(programId.getType(), runId, runtimeInfo);
} else {
LOG.warn("Unable to find program for runId {}", runId);
}
return runtimeInfo;
}
}
return null;
}
use of org.apache.twill.api.TwillController in project cdap by caskdata.
the class AbstractDistributedMasterServiceManager method getInstances.
@Override
public int getInstances() {
Iterable<TwillController> twillControllerList = twillRunnerService.lookup(Constants.Service.MASTER_SERVICES);
int instances = 0;
if (twillControllerList != null) {
for (TwillController twillController : twillControllerList) {
if (twillController.getResourceReport() != null) {
instances = twillController.getResourceReport().getRunnableResources(serviceName).size();
}
}
}
return instances;
}
use of org.apache.twill.api.TwillController in project cdap by caskdata.
the class AbstractDistributedMasterServiceManager method getLiveInfo.
@Override
public SystemServiceLiveInfo getLiveInfo() {
SystemServiceLiveInfo.Builder builder = SystemServiceLiveInfo.builder();
Iterable<TwillController> twillControllerList = twillRunnerService.lookup(Constants.Service.MASTER_SERVICES);
if (twillControllerList == null) {
return builder.build();
}
for (TwillController twillController : twillControllerList) {
if (twillController.getResourceReport() == null) {
continue;
}
ResourceReport resourceReport = twillController.getResourceReport();
Collection<TwillRunResources> runResources = resourceReport.getResources().get(serviceName);
for (TwillRunResources resources : runResources) {
Containers.ContainerInfo containerInfo = new Containers.ContainerInfo(Containers.ContainerType.SYSTEM_SERVICE, serviceName, resources.getInstanceId(), resources.getContainerId(), resources.getHost(), resources.getMemoryMB(), resources.getVirtualCores(), resources.getDebugPort());
builder.addContainer(containerInfo);
}
}
return builder.build();
}
use of org.apache.twill.api.TwillController in project cdap by caskdata.
the class DistributedProgramRuntimeService method getLiveInfo.
@Override
public ProgramLiveInfo getLiveInfo(ProgramId program) {
String twillAppName = TwillAppNames.toTwillAppName(program);
Iterator<TwillController> controllers = twillRunner.lookup(twillAppName).iterator();
// this will return an empty Json if there is no live instance
if (controllers.hasNext()) {
TwillController controller = controllers.next();
if (controllers.hasNext()) {
LOG.warn("Expected at most one live instance of Twill app {} but found at least two.", twillAppName);
}
ResourceReport report = controller.getResourceReport();
if (report != null) {
DistributedProgramLiveInfo liveInfo = new DistributedProgramLiveInfo(program, report.getApplicationId());
// if program type is flow then the container type is flowlet.
Containers.ContainerType containerType = ProgramType.FLOW.equals(program.getType()) ? FLOWLET : Containers.ContainerType.valueOf(program.getType().name());
for (Map.Entry<String, Collection<TwillRunResources>> entry : report.getResources().entrySet()) {
for (TwillRunResources resources : entry.getValue()) {
liveInfo.addContainer(new ContainerInfo(containerType, entry.getKey(), resources.getInstanceId(), resources.getContainerId(), resources.getHost(), resources.getMemoryMB(), resources.getVirtualCores(), resources.getDebugPort()));
}
}
// Add a list of announced services and their discoverables to the liveInfo.
liveInfo.addServices(report.getServices());
return liveInfo;
}
}
return new NotRunningProgramLiveInfo(program);
}
Aggregations