public void sort(String name, Path srcPath, String destPath) { synchronized (this) { sortStart = System.currentTimeMillis(); } String formerThreadName = Thread.currentThread().getName(); int part = 0; try { // the following call does not throw an exception if the file/dir does not exist fs.deleteRecursively(new Path(destPath)); DFSLoggerInputStreams inputStreams = DfsLogger.readHeaderAndReturnStream(fs, srcPath, conf); this.input = inputStreams.getOriginalInput(); this.decryptingInput = inputStreams.getDecryptingInputStream(); final long bufferSize = conf.getMemoryInBytes(Property.TSERV_SORT_BUFFER_SIZE); Thread.currentThread().setName("Sorting " + name + " for recovery"); while (true) { final ArrayList<Pair<LogFileKey, LogFileValue>> buffer = new ArrayList<Pair<LogFileKey, LogFileValue>>(); try { long start = input.getPos(); while (input.getPos() - start < bufferSize) { LogFileKey key = new LogFileKey(); LogFileValue value = new LogFileValue(); key.readFields(decryptingInput); value.readFields(decryptingInput); buffer.add(new Pair<LogFileKey, LogFileValue>(key, value)); } writeBuffer(destPath, buffer, part++); buffer.clear(); } catch (EOFException ex) { writeBuffer(destPath, buffer, part++); break; } } fs.create(new Path(destPath, "finished")).close(); log.info( "Finished log sort " + name + " " + getBytesCopied() + " bytes " + part + " parts in " + getSortTime() + "ms"); } catch (Throwable t) { try { // parent dir may not exist fs.mkdirs(new Path(destPath)); fs.create(new Path(destPath, "failed")).close(); } catch (IOException e) { log.error("Error creating failed flag file " + name, e); } log.error(t, t); } finally { Thread.currentThread().setName(formerThreadName); try { close(); } catch (Exception e) { log.error("Error during cleanup sort/copy " + name, e); } synchronized (this) { sortStop = System.currentTimeMillis(); } } }