Program: 17248
Project: 17248
Buggy source:
TezRawKeyValueIterator merge(Class keyClass,Class valueClass,int factor,int inMem,Path tmpDir,TezCounter readsCounter,TezCounter writesCounter,TezCounter bytesReadCounter,Progress mergePhase) throws IOException {
  LOG.info("String_Node_Str" + segments.size() + "String_Node_Str");
  int numSegments=segments.size();
  int origFactor=factor;
  int passNo=1;
  if (mergePhase != null) {
    mergeProgress=mergePhase;
  }
  long totalBytes=computeBytesInMerges(factor,inMem);
  if (totalBytes != 0) {
    progPerByte=1.0f / (float)totalBytes;
  }
  do {
    factor=getPassFactor(factor,passNo,numSegments - inMem);
    if (1 == passNo) {
      factor+=inMem;
    }
    List<Segment> segmentsToMerge=new ArrayList<Segment>();
    int segmentsConsidered=0;
    int numSegmentsToConsider=factor;
    long startBytes=0;
    while (true) {
      List<Segment> mStream=getSegmentDescriptors(numSegmentsToConsider);
      for (      Segment segment : mStream) {
        segment.init(readsCounter,bytesReadCounter);
        long startPos=segment.getPosition();
        boolean hasNext=segment.nextRawKey();
        long endPos=segment.getPosition();
        if (hasNext) {
          startBytes+=endPos - startPos;
          segmentsToMerge.add(segment);
          segmentsConsidered++;
        }
 else {
          segment.close();
          numSegments--;
        }
      }
      if (segmentsConsidered == factor || segments.size() == 0) {
        break;
      }
      numSegmentsToConsider=factor - segmentsConsidered;
    }
    initialize(segmentsToMerge.size());
    clear();
    for (    Segment segment : segmentsToMerge) {
      put(segment);
    }
    if (numSegments <= factor) {
      if (!includeFinalMerge) {
        totalBytesProcessed=0;
        totalBytes=0;
        for (int i=0; i < segmentsToMerge.size(); i++) {
          totalBytes+=segmentsToMerge.get(i).getLength();
        }
      }
      if (totalBytes != 0)       progPerByte=1.0f / (float)totalBytes;
      totalBytesProcessed+=startBytes;
      if (totalBytes != 0)       mergeProgress.set(totalBytesProcessed * progPerByte);
 else       mergeProgress.set(1.0f);
      LOG.info("String_Node_Str" + numSegments + "String_Node_Str"+ (totalBytes - totalBytesProcessed)+ "String_Node_Str");
      return this;
    }
 else {
      LOG.info("String_Node_Str" + segmentsToMerge.size() + "String_Node_Str"+ (segments.size() + segmentsToMerge.size()));
      long bytesProcessedInPrevMerges=totalBytesProcessed;
      totalBytesProcessed+=startBytes;
      long approxOutputSize=0;
      for (      Segment s : segmentsToMerge) {
        approxOutputSize+=s.getLength() + ChecksumFileSystem.getApproxChkSumLength(s.getLength());
      }
      Path tmpFilename=new Path(tmpDir,"String_Node_Str").suffix("String_Node_Str" + passNo);
      Path outputFile=lDirAlloc.getLocalPathForWrite(tmpFilename.toString(),approxOutputSize,conf);
      Writer writer=new Writer(conf,fs,outputFile,keyClass,valueClass,codec,writesCounter,null);
      writeFile(this,writer,reporter,recordsBeforeProgress);
      writer.close();
      this.close();
      Segment tempSegment=new Segment(conf,fs,outputFile,codec,ifileReadAhead,ifileReadAheadLength,ifileBufferSize,false);
      int pos=Collections.binarySearch(segments,tempSegment,segmentComparator);
      if (pos < 0) {
        pos=-pos - 1;
      }
      segments.add(pos,tempSegment);
      numSegments=segments.size();
      long inputBytesOfThisMerge=totalBytesProcessed - bytesProcessedInPrevMerges;
      totalBytes-=inputBytesOfThisMerge - tempSegment.getLength();
      if (totalBytes != 0) {
        progPerByte=1.0f / (float)totalBytes;
      }
      passNo++;
    }
    factor=origFactor;
  }
 while (true);
}

Fixed version (ground truth):
TezRawKeyValueIterator merge(Class keyClass,Class valueClass,int factor,int inMem,Path tmpDir,TezCounter readsCounter,TezCounter writesCounter,TezCounter bytesReadCounter,Progress mergePhase) throws IOException {
  LOG.info("String_Node_Str" + segments.size() + "String_Node_Str");
  if (segments.size() == 0) {
    LOG.info("String_Node_Str");
    return new EmptyIterator();
  }
  int numSegments=segments.size();
  int origFactor=factor;
  int passNo=1;
  if (mergePhase != null) {
    mergeProgress=mergePhase;
  }
  long totalBytes=computeBytesInMerges(factor,inMem);
  if (totalBytes != 0) {
    progPerByte=1.0f / (float)totalBytes;
  }
  do {
    factor=getPassFactor(factor,passNo,numSegments - inMem);
    if (1 == passNo) {
      factor+=inMem;
    }
    List<Segment> segmentsToMerge=new ArrayList<Segment>();
    int segmentsConsidered=0;
    int numSegmentsToConsider=factor;
    long startBytes=0;
    while (true) {
      List<Segment> mStream=getSegmentDescriptors(numSegmentsToConsider);
      for (      Segment segment : mStream) {
        segment.init(readsCounter,bytesReadCounter);
        long startPos=segment.getPosition();
        boolean hasNext=segment.nextRawKey();
        long endPos=segment.getPosition();
        if (hasNext) {
          startBytes+=endPos - startPos;
          segmentsToMerge.add(segment);
          segmentsConsidered++;
        }
 else {
          segment.close();
          numSegments--;
        }
      }
      if (segmentsConsidered == factor || segments.size() == 0) {
        break;
      }
      numSegmentsToConsider=factor - segmentsConsidered;
    }
    initialize(segmentsToMerge.size());
    clear();
    for (    Segment segment : segmentsToMerge) {
      put(segment);
    }
    if (numSegments <= factor) {
      if (!considerFinalMergeForProgress) {
        totalBytesProcessed=0;
        totalBytes=0;
        for (int i=0; i < segmentsToMerge.size(); i++) {
          totalBytes+=segmentsToMerge.get(i).getLength();
        }
      }
      if (totalBytes != 0)       progPerByte=1.0f / (float)totalBytes;
      totalBytesProcessed+=startBytes;
      if (totalBytes != 0)       mergeProgress.set(totalBytesProcessed * progPerByte);
 else       mergeProgress.set(1.0f);
      LOG.info("String_Node_Str" + numSegments + "String_Node_Str"+ (totalBytes - totalBytesProcessed)+ "String_Node_Str");
      return this;
    }
 else {
      LOG.info("String_Node_Str" + segmentsToMerge.size() + "String_Node_Str"+ (segments.size() + segmentsToMerge.size()));
      long bytesProcessedInPrevMerges=totalBytesProcessed;
      totalBytesProcessed+=startBytes;
      long approxOutputSize=0;
      for (      Segment s : segmentsToMerge) {
        approxOutputSize+=s.getLength() + ChecksumFileSystem.getApproxChkSumLength(s.getLength());
      }
      Path tmpFilename=new Path(tmpDir,"String_Node_Str").suffix("String_Node_Str" + passNo);
      Path outputFile=lDirAlloc.getLocalPathForWrite(tmpFilename.toString(),approxOutputSize,conf);
      Writer writer=new Writer(conf,fs,outputFile,keyClass,valueClass,codec,writesCounter,null);
      writeFile(this,writer,reporter,recordsBeforeProgress);
      writer.close();
      this.close();
      Segment tempSegment=new Segment(conf,fs,outputFile,codec,ifileReadAhead,ifileReadAheadLength,ifileBufferSize,false);
      int pos=Collections.binarySearch(segments,tempSegment,segmentComparator);
      if (pos < 0) {
        pos=-pos - 1;
      }
      segments.add(pos,tempSegment);
      numSegments=segments.size();
      long inputBytesOfThisMerge=totalBytesProcessed - bytesProcessedInPrevMerges;
      totalBytes-=inputBytesOfThisMerge - tempSegment.getLength();
      if (totalBytes != 0) {
        progPerByte=1.0f / (float)totalBytes;
      }
      passNo++;
    }
    factor=origFactor;
  }
 while (true);
}