Program: 4704
Project: 4704
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} for a type of CDAP program.
 * @param programType The type of program the run records need to validate and update.
 * @param processedInvalidRunRecordIds the {@link Set} of processed invalid run record ids.
 */
private void validateAndCorrectRunningRunRecords(final ProgramType programType,final Set<String> processedInvalidRunRecordIds){
  final Map<RunId,ProgramRuntimeService.RuntimeInfo> runIdToRuntimeInfo=runtimeService.list(programType);
  LOG.trace("String_Node_Str");
  Collection<RunRecordMeta> notActuallyRunning=store.getRuns(ProgramRunStatus.RUNNING,new com.google.common.base.Predicate<RunRecordMeta>(){
    @Override public boolean apply(    RunRecordMeta input){
      String runId=input.getPid();
      return !runIdToRuntimeInfo.containsKey(RunIds.fromString(runId));
    }
  }
).values();
  LOG.trace("String_Node_Str",notActuallyRunning.size());
  final Map<String,ProgramId> runIdToProgramId=new HashMap<>();
  LOG.trace("String_Node_Str");
  Collection<RunRecordMeta> invalidRunRecords=Collections2.filter(notActuallyRunning,new com.google.common.base.Predicate<RunRecordMeta>(){
    @Override public boolean apply(    RunRecordMeta input){
      String runId=input.getPid();
      ProgramId targetProgramId=programLifecycleService.retrieveProgramIdForRunRecord(programType,runId);
      if (targetProgramId != null) {
        runIdToProgramId.put(runId,targetProgramId);
        return true;
      }
 else {
        return false;
      }
    }
  }
);
  invalidRunRecords=Collections2.filter(invalidRunRecords,new com.google.common.base.Predicate<RunRecordMeta>(){
    @Override public boolean apply(    RunRecordMeta invalidRunRecordMeta){
      boolean shouldCorrect=shouldCorrectForWorkflowChildren(invalidRunRecordMeta,processedInvalidRunRecordIds);
      if (!shouldCorrect) {
        LOG.trace("String_Node_Str",invalidRunRecordMeta);
        return false;
      }
      return true;
    }
  }
);
  LOG.trace("String_Node_Str");
  if (!invalidRunRecords.isEmpty()) {
    LOG.warn("String_Node_Str",invalidRunRecords.size(),programType.getPrettyName());
  }
 else {
    LOG.trace("String_Node_Str",programType.getPrettyName());
  }
  for (  RunRecordMeta invalidRunRecordMeta : invalidRunRecords) {
    String runId=invalidRunRecordMeta.getPid();
    ProgramId targetProgramId=runIdToProgramId.get(runId);
    programStateWriter.error(targetProgramId.run(runId),new Throwable("String_Node_Str"));
    LOG.warn("String_Node_Str" + "String_Node_Str",runId,targetProgramId);
    processedInvalidRunRecordIds.add(runId);
  }
}

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} for a type of CDAP program.
 * @param programType The type of program the run records need to validate and update.
 * @param processedInvalidRunRecordIds the {@link Set} of processed invalid run record ids.
 */
private void validateAndCorrectRunningRunRecords(final ProgramType programType,final Set<String> processedInvalidRunRecordIds){
  final Map<RunId,ProgramRuntimeService.RuntimeInfo> runIdToRuntimeInfo=runtimeService.list(programType);
  com.google.common.base.Predicate<RunRecordMeta> filter=new com.google.common.base.Predicate<RunRecordMeta>(){
    @Override public boolean apply(    RunRecordMeta input){
      String runId=input.getPid();
      return !runIdToRuntimeInfo.containsKey(RunIds.fromString(runId));
    }
  }
;
  LOG.trace("String_Node_Str");
  List<RunRecordMeta> notActuallyRunning=new ArrayList<>();
  notActuallyRunning.addAll(store.getRuns(ProgramRunStatus.RUNNING,filter).values());
  LOG.trace("String_Node_Str",notActuallyRunning.size());
  notActuallyRunning.addAll(store.getRuns(ProgramRunStatus.STARTING,filter).values());
  LOG.trace("String_Node_Str",notActuallyRunning.size());
  LOG.trace("String_Node_Str",notActuallyRunning.size());
  final Map<String,ProgramId> runIdToProgramId=new HashMap<>();
  LOG.trace("String_Node_Str");
  Collection<RunRecordMeta> invalidRunRecords=Collections2.filter(notActuallyRunning,new com.google.common.base.Predicate<RunRecordMeta>(){
    @Override public boolean apply(    RunRecordMeta input){
      String runId=input.getPid();
      ProgramId targetProgramId=programLifecycleService.retrieveProgramIdForRunRecord(programType,runId);
      if (targetProgramId != null) {
        runIdToProgramId.put(runId,targetProgramId);
        return true;
      }
 else {
        return false;
      }
    }
  }
);
  invalidRunRecords=Collections2.filter(invalidRunRecords,new com.google.common.base.Predicate<RunRecordMeta>(){
    @Override public boolean apply(    RunRecordMeta invalidRunRecordMeta){
      boolean shouldCorrect=shouldCorrectForWorkflowChildren(invalidRunRecordMeta,processedInvalidRunRecordIds);
      if (!shouldCorrect) {
        LOG.trace("String_Node_Str",invalidRunRecordMeta);
        return false;
      }
      return true;
    }
  }
);
  LOG.trace("String_Node_Str");
  if (!invalidRunRecords.isEmpty()) {
    LOG.warn("String_Node_Str" + "String_Node_Str",invalidRunRecords.size(),programType.getPrettyName());
  }
 else {
    LOG.trace("String_Node_Str" + "String_Node_Str",programType.getPrettyName());
  }
  for (  RunRecordMeta invalidRunRecordMeta : invalidRunRecords) {
    String runId=invalidRunRecordMeta.getPid();
    ProgramId targetProgramId=runIdToProgramId.get(runId);
    programStateWriter.error(targetProgramId.run(runId),new Throwable("String_Node_Str"));
    LOG.warn("String_Node_Str" + "String_Node_Str",runId,targetProgramId);
    processedInvalidRunRecordIds.add(runId);
  }
}