Search in sources :

Example 1 with ProgramRunClusterStatus

use of io.cdap.cdap.proto.ProgramRunClusterStatus in project cdap by caskdata.

the class ProgramNotificationSubscriberService method processNotification.

/**
 * Process a {@link Notification} received from TMS.
 *
 * @param programHeartbeatTable the {@link ProgramHeartbeatTable} for writing heart beats and program status
 * @param messageIdBytes the raw message id in the TMS for the notification
 * @param notification the {@link Notification} to process
 * @param context context to get the table for operations
 * @return a {@link List} of {@link Runnable} tasks to run after the transactional processing of the whole
 *         messages batch is completed
 * @throws Exception if failed to process the given notification
 */
private List<Runnable> processNotification(ProgramHeartbeatTable programHeartbeatTable, byte[] messageIdBytes, Notification notification, StructuredTableContext context) throws Exception {
    AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
    Map<String, String> properties = notification.getProperties();
    // Required parameters
    String programRun = properties.get(ProgramOptionConstants.PROGRAM_RUN_ID);
    String programStatusStr = properties.get(ProgramOptionConstants.PROGRAM_STATUS);
    String clusterStatusStr = properties.get(ProgramOptionConstants.CLUSTER_STATUS);
    // Ignore notifications which specify an invalid ProgramRunId, which shouldn't happen
    if (programRun == null) {
        LOG.warn("Ignore notification that misses program run state information, {}", notification);
        return Collections.emptyList();
    }
    ProgramRunId programRunId = GSON.fromJson(programRun, ProgramRunId.class);
    ProgramRunStatus programRunStatus = null;
    if (programStatusStr != null) {
        try {
            programRunStatus = ProgramRunStatus.valueOf(programStatusStr);
        } catch (IllegalArgumentException e) {
            LOG.warn("Ignore notification with invalid program run status {} for program {}, {}", programStatusStr, programRun, notification);
            return Collections.emptyList();
        }
    }
    ProgramRunClusterStatus clusterStatus = null;
    if (clusterStatusStr != null) {
        try {
            clusterStatus = ProgramRunClusterStatus.valueOf(clusterStatusStr);
        } catch (IllegalArgumentException e) {
            LOG.warn("Ignore notification with invalid program run cluster status {} for program {}", clusterStatusStr, programRun);
            return Collections.emptyList();
        }
    }
    if (notification.getNotificationType().equals(Notification.Type.PROGRAM_HEART_BEAT)) {
        RunRecordDetail runRecordMeta = appMetadataStore.getRun(programRunId);
        long heartBeatTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(Long.parseLong(properties.get(ProgramOptionConstants.HEART_BEAT_TIME)));
        writeToHeartBeatTable(runRecordMeta, heartBeatTimeInSeconds, programHeartbeatTable);
        // we can return after writing to heart beat table
        return Collections.emptyList();
    }
    List<Runnable> result = new ArrayList<>();
    if (programRunStatus != null) {
        handleProgramEvent(programRunId, programRunStatus, notification, messageIdBytes, appMetadataStore, programHeartbeatTable, result);
    }
    if (clusterStatus == null) {
        return result;
    }
    handleClusterEvent(programRunId, clusterStatus, notification, messageIdBytes, appMetadataStore, context).ifPresent(result::add);
    return result;
}
Also used : AppMetadataStore(io.cdap.cdap.internal.app.store.AppMetadataStore) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ArrayList(java.util.ArrayList) ProgramRunClusterStatus(io.cdap.cdap.proto.ProgramRunClusterStatus) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId)

Aggregations

AppMetadataStore (io.cdap.cdap.internal.app.store.AppMetadataStore)1 RunRecordDetail (io.cdap.cdap.internal.app.store.RunRecordDetail)1 ProgramRunClusterStatus (io.cdap.cdap.proto.ProgramRunClusterStatus)1 ProgramRunStatus (io.cdap.cdap.proto.ProgramRunStatus)1 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)1 ArrayList (java.util.ArrayList)1