Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.paimon.io.DataOutputView;
import org.apache.paimon.memory.Buffer;
import org.apache.paimon.memory.MemorySegment;
import org.apache.paimon.utils.ExceptionUtils;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -64,13 +65,27 @@ public FileIOChannel getChannel() {
@Override
public void close() throws IOException {
if (!writer.isClosed()) {
int currentPositionInSegment = getCurrentPositionInSegment();
if (currentPositionInSegment > 0) {
writeCompressed(currentSegment, currentPositionInSegment);
Throwable failure = null;
try {
int currentPositionInSegment = getCurrentPositionInSegment();
if (currentPositionInSegment > 0) {
writeCompressed(currentSegment, currentPositionInSegment);
}
this.writeBytes = writer.getSize();
} catch (IOException | RuntimeException | Error e) {
failure = e;
throw e;
} finally {
clear();
try {
this.writer.close();
} catch (IOException | RuntimeException | Error e) {
if (failure == null) {
throw e;
}
ExceptionUtils.firstOrSuppressed(e, failure);
}
}
clear();
this.writeBytes = writer.getSize();
this.writer.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@

import org.apache.paimon.compression.BlockCompressionFactory;
import org.apache.paimon.compression.BlockCompressionType;
import org.apache.paimon.compression.CompressOptions;
import org.apache.paimon.data.GenericRow;
import org.apache.paimon.data.serializer.BinaryRowSerializer;
import org.apache.paimon.data.serializer.InternalRowSerializer;
import org.apache.paimon.memory.Buffer;
import org.apache.paimon.memory.HeapMemorySegmentPool;
import org.apache.paimon.memory.MemorySegmentPool;
import org.apache.paimon.options.MemorySize;
import org.apache.paimon.sort.BinaryExternalSortBuffer;
import org.apache.paimon.sort.BinaryInMemorySortBuffer;
import org.apache.paimon.sort.IntNormalizedKeyComputer;
import org.apache.paimon.sort.IntRecordComparator;
import org.apache.paimon.types.DataTypes;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicReference;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for {@link ChannelWriterOutputView}. */
public class ChannelWriterOutputViewTest {
Expand Down Expand Up @@ -66,4 +81,197 @@ input, null, new BinaryRowSerializer(1))
}
}
}

@Test
public void testCloseClosesChannelWhenFinalFlushFails() throws Exception {
IOException failure = new IOException("spill write failed");
try (IOManager ioManager = IOManager.create(tempDir.toString())) {
BufferFileWriter writer = failingWriter(ioManager.createChannel(), failure, null);
ChannelWriterOutputView output = output(writer);
try {
output.writeInt(1);
assertThatThrownBy(output::close).isSameAs(failure);
assertThat(writer.isClosed()).isTrue();
} finally {
writer.getNioFileChannel().close();
}
}
}

@Test
public void testCloseClosesChannelWhenReadingSizeFails() throws Exception {
IOException failure = new IOException("channel size failed");
try (IOManager ioManager = IOManager.create(tempDir.toString())) {
BufferFileWriter writer =
new BufferFileWriterImpl(ioManager.createChannel()) {
@Override
public long getSize() throws IOException {
throw failure;
}
};
ChannelWriterOutputView output = output(writer);
try {
assertThatThrownBy(output::close).isSameAs(failure);
assertThat(writer.isClosed()).isTrue();
} finally {
writer.getNioFileChannel().close();
}
}
}

@Test
public void testNonEmptyOutput() throws Exception {
try (IOManager ioManager = IOManager.create(tempDir.toString())) {
FileIOChannel.ID channel = ioManager.createChannel();
ChannelWriterOutputView output = output(ioManager.createBufferFileWriter(channel));
output.writeInt(123);
output.close();
output.close();
assertThat(output.getNumBytes()).isEqualTo(4);
assertThat(output.getBlockCount()).isEqualTo(1);
assertThat(output.getWriteBytes()).isEqualTo(channel.getPathFile().length());
assertThat(output.getNumCompressedBytes()).isPositive();

ChannelReaderInputView input =
new ChannelReaderInputView(
channel,
ioManager,
BlockCompressionFactory.create(BlockCompressionType.LZ4),
BLOCK_SIZE,
output.getBlockCount());
try {
assertThat(input.readInt()).isEqualTo(123);
} finally {
input.getChannel().closeAndDelete();
}
}
}

@Test
public void testClosePreservesFlushFailureWhenChannelCloseFails() throws Exception {
IOException failure = new IOException("spill write failed");
IOException closeFailure = new IOException("channel close failed");
try (IOManager ioManager = IOManager.create(tempDir.toString())) {
BufferFileWriter writer =
failingWriter(ioManager.createChannel(), failure, closeFailure);
ChannelWriterOutputView output = output(writer);
try {
output.writeInt(1);
assertThatThrownBy(output::close).isSameAs(failure);
assertThat(writer.isClosed()).isTrue();
assertThat(failure.getSuppressed()).containsExactly(closeFailure);
} finally {
writer.getNioFileChannel().close();
}
}
}

@Test
public void testClosePreservesSharedFlushAndCloseFailure() throws Exception {
IOException failure = new IOException("spill write and close failed");
try (IOManager ioManager = IOManager.create(tempDir.toString())) {
BufferFileWriter writer = failingWriter(ioManager.createChannel(), failure, failure);
ChannelWriterOutputView output = output(writer);
try {
output.writeInt(1);
assertThatThrownBy(output::close).isSameAs(failure);
assertThat(writer.isClosed()).isTrue();
assertThat(failure.getSuppressed()).isEmpty();
} finally {
writer.getNioFileChannel().close();
}
}
}

@Test
public void testCloseAndDeleteDeletesChannelWhenFinalFlushFails() throws Exception {
IOException failure = new IOException("spill write failed");
try (IOManager ioManager = IOManager.create(tempDir.toString())) {
FileIOChannel.ID channel = ioManager.createChannel();
BufferFileWriter writer = failingWriter(channel, failure, null);
ChannelWriterOutputView output = output(writer);
try {
output.writeInt(1);
assertThatThrownBy(output::closeAndDelete).isSameAs(failure);
assertThat(writer.isClosed()).isTrue();
assertThat(channel.getPathFile()).doesNotExist();
} finally {
writer.getNioFileChannel().close();
}
}
}

@Test
public void testExternalSortClosesFailedSpillChannel() throws Exception {
IOException failure = new IOException("spill write failed");
AtomicReference<BufferFileWriter> createdWriter = new AtomicReference<>();
try (IOManager ioManager =
new IOManagerImpl(tempDir.toString()) {
@Override
public BufferFileWriter createBufferFileWriter(FileIOChannel.ID channel)
throws IOException {
BufferFileWriter writer = failingWriter(channel, failure, null);
createdWriter.set(writer);
return writer;
}
}) {
HeapMemorySegmentPool pool =
new HeapMemorySegmentPool(1024 * 1024, MemorySegmentPool.DEFAULT_PAGE_SIZE);
int freePages = pool.freePages();
BinaryInMemorySortBuffer inMemory =
BinaryInMemorySortBuffer.createBuffer(
IntNormalizedKeyComputer.INSTANCE,
new InternalRowSerializer(DataTypes.INT()),
IntRecordComparator.INSTANCE,
pool);
BinaryExternalSortBuffer sorter =
new BinaryExternalSortBuffer(
new BinaryRowSerializer(1),
IntRecordComparator.INSTANCE,
pool.pageSize(),
inMemory,
ioManager,
128,
new CompressOptions("lz4", 1),
MemorySize.MAX_VALUE);
try {
assertThat(sorter.write(GenericRow.of(1))).isTrue();
assertThatThrownBy(sorter::flushMemory).isSameAs(failure);
BufferFileWriter writer = createdWriter.get();
assertThat(writer).isNotNull();
assertThat(writer.isClosed()).isTrue();
assertThat(writer.getChannelID().getPathFile()).doesNotExist();
} finally {
if (createdWriter.get() != null) {
createdWriter.get().getNioFileChannel().close();
}
sorter.clear();
}
assertThat(pool.freePages()).isEqualTo(freePages);
}
}

private ChannelWriterOutputView output(BufferFileWriter writer) {
return new ChannelWriterOutputView(
writer, BlockCompressionFactory.create(BlockCompressionType.LZ4), BLOCK_SIZE);
}

private BufferFileWriter failingWriter(
FileIOChannel.ID channel, IOException failure, IOException closeFailure)
throws IOException {
return new BufferFileWriterImpl(channel) {
@Override
public void writeBlock(Buffer buffer) throws IOException {
throw failure;
}

@Override
public void close() throws IOException {
super.close();
if (closeFailure != null) {
throw closeFailure;
}
}
};
}
}
Loading