if (!streamLocation.isDirectory()) {
throw new IOException("Stream " + streamLocation.getName() + " not exist in " + streamLocation.toURI());
}
Location partitionDirectory = StreamUtils.createPartitionLocation(streamLocation,
partitionStart, partitionDuration);
// Always try to create the directory
partitionDirectory.mkdirs();
// Try to find the file of this bucket with the highest sequence number.
int maxSequence = -1;
for (Location location : partitionDirectory.list()) {
String fileName = location.getName();
if (fileName.startsWith(fileNamePrefix)) {
StreamUtils.getSequenceId(fileName);
int idx = fileName.lastIndexOf('.');
if (idx < fileNamePrefix.length()) {
LOG.warn("Ignore file with invalid stream file name {}", location.toURI());
continue;
}
try {
// File name format is [prefix].[sequenceId].[dat|idx]
int seq = StreamUtils.getSequenceId(fileName);
if (seq > maxSequence) {
maxSequence = seq;
}
} catch (NumberFormatException e) {
LOG.warn("Ignore stream file with invalid sequence id {}", location.toURI());
}
}
}
// Create the event and index file with the max sequence + 1
int fileSequence = maxSequence + 1;
Location eventFile = StreamUtils.createStreamLocation(partitionDirectory, fileNamePrefix,
fileSequence, StreamFileType.EVENT);
Location indexFile = StreamUtils.createStreamLocation(partitionDirectory, fileNamePrefix,
fileSequence, StreamFileType.INDEX);
// The creation should succeed, as it's expected to only have one process running per fileNamePrefix.
if (!eventFile.createNew() || !indexFile.createNew()) {
throw new IOException("Failed to create new file at " + eventFile.toURI() + " and " + indexFile.toURI());
}
LOG.debug("New stream file created at {}", eventFile.toURI());
return new StreamDataFileWriter(createOutputSupplier(eventFile), createOutputSupplier(indexFile), indexInterval);
}