Program: 7020
Project: 7020
Buggy source:
/** 
 * Fix all the possible inconsistent states for RunRecords that shows it is in RUNNING state but actually not via check to   {@link ProgramRuntimeService}.
 * @param programType The type of programs the run records nee to validate and update.
 * @param store The data store that manages run records instances for all programs.
 * @param runtimeService The {@link ProgramRuntimeService} instance to check the actual state of the program.
 */
private void validateAndCorrectRunningRunRecords(ProgramType programType,Store store,ProgramRuntimeService runtimeService){
  final Map<RunId,RuntimeInfo> runIdToRuntimeInfo=runtimeService.list(programType);
  List<RunRecord> invalidRunRecords=store.getRuns(ProgramRunStatus.RUNNING,new Predicate<RunRecord>(){
    @Override public boolean apply(    @Nullable RunRecord input){
      if (input == null) {
        return false;
      }
      String runId=input.getPid();
      return !runIdToRuntimeInfo.containsKey(RunIds.fromString(runId));
    }
  }
);
  if (!invalidRunRecords.isEmpty()) {
    LOG.debug("String_Node_Str",invalidRunRecords.size());
  }
  for (  RunRecord rr : invalidRunRecords) {
    String runId=rr.getPid();
    RuntimeInfo runtimeInfo=runIdToRuntimeInfo.get(RunIds.fromString(runId));
    store.compareAndSetStatus(runtimeInfo.getProgramId(),runId,ProgramController.State.ALIVE.getRunStatus(),ProgramController.State.ERROR.getRunStatus());
  }
}

Fixed version (ground truth):
/** 
 * Fix all the possible inconsistent states for RunRecords that shows it is in RUNNING state but actually not via check to   {@link ProgramRuntimeService}.
 * @param programType The type of programs the run records nee to validate and update.
 * @param store The data store that manages run records instances for all programs.
 * @param runtimeService The {@link ProgramRuntimeService} instance to check the actual state of the program.
 */
private void validateAndCorrectRunningRunRecords(ProgramType programType,Store store,ProgramRuntimeService runtimeService){
  final Map<RunId,RuntimeInfo> runIdToRuntimeInfo=runtimeService.list(programType);
  List<RunRecord> invalidRunRecords=store.getRuns(ProgramRunStatus.RUNNING,new Predicate<RunRecord>(){
    @Override public boolean apply(    @Nullable RunRecord input){
      if (input == null) {
        return false;
      }
      String runId=input.getPid();
      return !runIdToRuntimeInfo.containsKey(RunIds.fromString(runId));
    }
  }
);
  if (!invalidRunRecords.isEmpty()) {
    LOG.debug("String_Node_Str",invalidRunRecords.size());
  }
  for (  RunRecord rr : invalidRunRecords) {
    String runId=rr.getPid();
    List<NamespaceMeta> namespaceMetas=store.listNamespaces();
    Id.Program targetProgramId=null;
    for (    NamespaceMeta nm : namespaceMetas) {
      Id.Namespace accId=Id.Namespace.from(nm.getName());
      Collection<ApplicationSpecification> appSpecs=store.getAllApplications(accId);
      for (      ApplicationSpecification appSpec : appSpecs) {
switch (programType) {
case FLOW:
          for (          String programName : appSpec.getFlows().keySet()) {
            Id.Program programId=validateProgramForRunRecord(store,nm.getName(),appSpec.getName(),programType,programName,runId);
            if (programId != null) {
              targetProgramId=programId;
              break;
            }
          }
        break;
case MAPREDUCE:
      for (      String programName : appSpec.getMapReduce().keySet()) {
        Id.Program programId=validateProgramForRunRecord(store,nm.getName(),appSpec.getName(),programType,programName,runId);
        if (programId != null) {
          targetProgramId=programId;
          break;
        }
      }
    break;
case SPARK:
  for (  String programName : appSpec.getSpark().keySet()) {
    Id.Program programId=validateProgramForRunRecord(store,nm.getName(),appSpec.getName(),programType,programName,runId);
    if (programId != null) {
      targetProgramId=programId;
      break;
    }
  }
break;
case SERVICE:
for (String programName : appSpec.getServices().keySet()) {
Id.Program programId=validateProgramForRunRecord(store,nm.getName(),appSpec.getName(),programType,programName,runId);
if (programId != null) {
  targetProgramId=programId;
  break;
}
}
break;
case WORKER:
for (String programName : appSpec.getWorkers().keySet()) {
Id.Program programId=validateProgramForRunRecord(store,nm.getName(),appSpec.getName(),programType,programName,runId);
if (programId != null) {
targetProgramId=programId;
break;
}
}
break;
case WORKFLOW:
for (String programName : appSpec.getWorkflows().keySet()) {
Id.Program programId=validateProgramForRunRecord(store,nm.getName(),appSpec.getName(),programType,programName,runId);
if (programId != null) {
targetProgramId=programId;
break;
}
}
break;
default :
throw new RuntimeException("String_Node_Str" + programType.name());
}
if (targetProgramId != null) {
break;
}
}
if (targetProgramId != null) {
break;
}
}
if (targetProgramId != null) {
store.compareAndSetStatus(targetProgramId,runId,ProgramController.State.ALIVE.getRunStatus(),ProgramController.State.ERROR.getRunStatus());
}
}
}