Skip to content

Commit 124affa

Browse files
fix(jni): preserve creation failures during schema cleanup
Signed-off-by: Christopher Wirt <[email protected]>
1 parent c75b877 commit 124affa

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

‎java/vortex-jni/src/main/java/dev/vortex/api/VortexWriter.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dev.vortex.VortexCleaner;
88
import dev.vortex.io.NativeWritable;
99
import dev.vortex.jni.NativeWriter;
10+
import java.io.Closeable;
1011
import java.io.IOException;
1112
import java.util.HashMap;
1213
import java.util.Map;
@@ -146,8 +147,14 @@ public Builder putMetadata(String key, byte[] value) {
146147
* metadata
147148
*/
148149
public VortexWriter build() throws IOException {
149-
ArrowSchema ffi = ArrowSchema.allocateNew(allocator);
150-
try {
150+
// Native code borrows the schema. Release its children before closing the struct;
151+
// separate resources ensure both cleanups run and preserve the original failure.
152+
try (ArrowSchema ffi = ArrowSchema.allocateNew(allocator);
153+
Closeable releaseSchema = () -> {
154+
if (ffi.snapshot().release != 0) {
155+
ffi.release();
156+
}
157+
}) {
151158
Data.exportSchema(allocator, arrowSchema, null, ffi);
152159
final long pointer;
153160
try {
@@ -164,12 +171,6 @@ public VortexWriter build() throws IOException {
164171
throw new IOException("failed to create writer for " + destination() + " (ptr=" + pointer + ")");
165172
}
166173
return new VortexWriter(pointer);
167-
} finally {
168-
// Native code only borrows the schema, so the children the export allocated are ours to release.
169-
if (ffi.snapshot().release != 0) {
170-
ffi.release();
171-
}
172-
ffi.close();
173174
}
174175
}
175176

‎java/vortex-jni/src/test/java/dev/vortex/jni/JNIWriterTest.java‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import dev.vortex.api.VortexWriteSummary;
2020
import dev.vortex.api.VortexWriter;
2121
import dev.vortex.arrow.ArrowAllocation;
22+
import dev.vortex.io.NativeWritable;
23+
import java.io.ByteArrayOutputStream;
2224
import java.io.IOException;
2325
import java.nio.file.Files;
2426
import java.nio.file.Path;
@@ -30,7 +32,6 @@
3032
import org.apache.arrow.c.ArrowSchema;
3133
import org.apache.arrow.c.Data;
3234
import org.apache.arrow.memory.BufferAllocator;
33-
import org.apache.arrow.memory.RootAllocator;
3435
import org.apache.arrow.vector.BigIntVector;
3536
import org.apache.arrow.vector.IntVector;
3637
import org.apache.arrow.vector.VarBinaryVector;
@@ -50,6 +51,8 @@
5051
import org.junit.jupiter.api.Disabled;
5152
import org.junit.jupiter.api.Test;
5253
import org.junit.jupiter.api.io.TempDir;
54+
import org.junit.jupiter.params.ParameterizedTest;
55+
import org.junit.jupiter.params.provider.CsvSource;
5356

5457
public final class JNIWriterTest {
5558
private static final String ARROW_EXTENSION_NAME = "ARROW:extension:name";
@@ -170,15 +173,55 @@ public void testCreateWriter() throws IOException {
170173
source.arrowSchema(allocator));
171174
}
172175

173-
@Test
174-
public void testCreateWriterReleasesSchemaExport() throws IOException {
176+
@ParameterizedTest(name = "stream={0}, nested={1}, rejected={2}")
177+
@CsvSource({
178+
"false, false, false",
179+
"false, true, false",
180+
"true, false, false",
181+
"true, true, false",
182+
"false, false, true",
183+
"false, true, true",
184+
"true, false, true",
185+
"true, true, true"
186+
})
187+
public void testCreateWriterReleasesSchemaExport(boolean stream, boolean nested, boolean rejected)
188+
throws IOException {
175189
String writePath = tempDir.resolve("test_schema_release.vortex").toUri().toString();
176190
Session session = Session.create();
177-
try (BufferAllocator allocator = new RootAllocator()) {
178-
try (VortexWriter writer = VortexWriter.builder(session, writePath, personSchema(), allocator)
179-
.build()) {
180-
assertEquals(0, allocator.getAllocatedMemory());
191+
Schema schema = nested ? mapSchema() : personSchema();
192+
ByteArrayOutputStream output = new ByteArrayOutputStream();
193+
NativeWritable writable = new NativeWritable() {
194+
@Override
195+
public void write(byte[] buffer, int offset, int length) {
196+
output.write(buffer, offset, length);
197+
}
198+
199+
@Override
200+
public void flush() {}
201+
202+
@Override
203+
public void close() {}
204+
};
205+
try (BufferAllocator allocator =
206+
ArrowAllocation.rootAllocator().newChildAllocator("schema-release", 0, Long.MAX_VALUE)) {
207+
VortexWriter.Builder builder = stream
208+
? VortexWriter.builder(session, writable, schema, allocator)
209+
: VortexWriter.builder(session, writePath, schema, allocator);
210+
if (rejected) {
211+
// Native validation rejects this after the schema has been exported, for both destinations.
212+
builder.putMetadata("k".repeat(1024), new byte[] {1});
213+
IOException failure = assertThrows(IOException.class, builder::build);
214+
assertNotNull(failure.getCause());
215+
assertTrue(failure.getCause().getMessage().contains("metadata key"));
216+
} else {
217+
try (VortexWriter writer = builder.build()) {
218+
assertEquals(0, allocator.getAllocatedMemory());
219+
}
220+
if (stream) {
221+
assertTrue(output.size() > 0, "stream writer should have finalized a file");
222+
}
181223
}
224+
assertEquals(0, allocator.getAllocatedMemory());
182225
}
183226
}
184227

0 commit comments

Comments
 (0)