Skip to content

Commit bd86a85

Browse files
committed
fix: Fixed filesystem object store failback
Signed-off-by: dhoard <doug.hoard@gmail.com>
1 parent bac9862 commit bd86a85

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎src/main/c/com_tidesdb_TidesDB.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <pthread.h>
2121
#include <stdlib.h>
2222
#include <string.h>
23+
#include <sys/stat.h>
2324
#include <tidesdb/db.h>
2425
#ifndef _WIN32
2526
#include <dlfcn.h>
@@ -156,7 +157,30 @@ JNIEXPORT jlong JNICALL Java_com_tidesdb_TidesDB_nativeOpen(
156157
}
157158
if (fs_path != NULL)
158159
{
160+
/* Validate that the path is an existing directory before creating the
161+
* connector. tidesdb_objstore_fs_create silently succeeds on a regular
162+
* file path, which would cause the database to open with a broken
163+
* object store backend. */
164+
struct stat st;
165+
if (stat(fs_path, &st) != 0 || !S_ISDIR(st.st_mode))
166+
{
167+
(*env)->ReleaseStringUTFChars(env, objectStoreFsPath, fs_path);
168+
(*env)->ReleaseStringUTFChars(env, dbPath, path);
169+
if (!jvm_exception_pending(env))
170+
throwTidesDBException(env, TDB_ERR_IO,
171+
"Failed to create filesystem object store connector");
172+
return 0;
173+
}
159174
obj_store = tidesdb_objstore_fs_create(fs_path);
175+
if (obj_store == NULL)
176+
{
177+
(*env)->ReleaseStringUTFChars(env, objectStoreFsPath, fs_path);
178+
(*env)->ReleaseStringUTFChars(env, dbPath, path);
179+
if (!jvm_exception_pending(env))
180+
throwTidesDBException(env, TDB_ERR_IO,
181+
"Failed to create filesystem object store connector");
182+
return 0;
183+
}
160184
}
161185
}
162186

‎src/test/java/com/tidesdb/TidesDBTest.java‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.io.TempDir;
2323

2424
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
2526
import java.nio.file.Path;
2627
import java.time.Instant;
2728
import java.util.ArrayList;
@@ -2647,6 +2648,87 @@ void testSetClearHookNotRacingClose() throws TidesDBException, InterruptedExcept
26472648
}
26482649
}
26492650

2651+
@Test
2652+
@Order(71)
2653+
void testOpenWithObjectStoreFsPathFailsOnBadPath() throws Exception {
2654+
// A regular file (not a directory) as objectStoreFsPath must cause tidesdb_objstore_fs_create
2655+
// to return NULL, which should surface as TidesDBException instead of silently opening
2656+
// an ordinary local database.
2657+
Path osFile = tempDir.resolve("os_file");
2658+
Files.createFile(osFile);
2659+
2660+
Config config = Config.builder(tempDir.resolve("testdb_fs_badpath").toString())
2661+
.objectStoreFsPath(osFile.toString())
2662+
.build();
2663+
2664+
TidesDBException ex = assertThrows(TidesDBException.class, () -> TidesDB.open(config));
2665+
assertEquals(TidesDBException.ERR_IO, ex.getErrorCode());
2666+
}
2667+
2668+
@Test
2669+
@Order(72)
2670+
void testOpenWithObjectStoreFsPathSucceedsWithValidDirectory() throws TidesDBException {
2671+
Path osDir = tempDir.resolve("os_dir");
2672+
osDir.toFile().mkdirs();
2673+
2674+
Config config = Config.builder(tempDir.resolve("testdb_fs_goodpath").toString())
2675+
.objectStoreFsPath(osDir.toString())
2676+
.build();
2677+
2678+
try (TidesDB db = TidesDB.open(config)) {
2679+
assertNotNull(db);
2680+
DbStats dbStats = db.getDbStats();
2681+
assertNotNull(dbStats);
2682+
assertTrue(dbStats.isObjectStoreEnabled());
2683+
}
2684+
}
2685+
2686+
@Test
2687+
@Order(73)
2688+
void testOpenWithoutObjectStoreFsPathSucceeds() throws TidesDBException {
2689+
Config config = Config.builder(tempDir.resolve("plaindb").toString())
2690+
.numFlushThreads(2)
2691+
.numCompactionThreads(2)
2692+
.logLevel(LogLevel.INFO)
2693+
.blockCacheSize(64 * 1024 * 1024)
2694+
.maxOpenSSTables(256)
2695+
.build();
2696+
2697+
try (TidesDB db = TidesDB.open(config)) {
2698+
assertNotNull(db);
2699+
}
2700+
}
2701+
2702+
@Test
2703+
@Order(74)
2704+
void testS3PrecedenceOverFsPathOnFailure() throws TidesDBException {
2705+
Path osFile = tempDir.resolve("os_file_s3");
2706+
try {
2707+
Files.createFile(osFile);
2708+
} catch (java.io.IOException e) {
2709+
throw new RuntimeException(e);
2710+
}
2711+
2712+
S3Config s3 = S3Config.builder()
2713+
.endpoint("127.0.0.1:19001")
2714+
.bucket("tidesdb-test")
2715+
.accessKey("minioadmin")
2716+
.secretKey("minioadmin")
2717+
.usePathStyle(true)
2718+
.useSsl(false)
2719+
.build();
2720+
2721+
Config config = Config.builder(tempDir.resolve("testdb_s3_over_fs").toString())
2722+
.objectStoreS3Config(s3)
2723+
.objectStoreFsPath(osFile.toString())
2724+
.build();
2725+
2726+
// S3 takes precedence over fs path. If S3 is available, the S3 connector creation or
2727+
// open fails with TidesDBException. If S3 is unavailable, the S3 connector creation
2728+
// itself throws. In neither case does a silent filesystem fallback occur.
2729+
assertThrows(TidesDBException.class, () -> TidesDB.open(config));
2730+
}
2731+
26502732
@Test
26512733
@Order(70)
26522734
void testJniBufferMethodsCoverage() throws TidesDBException {

0 commit comments

Comments
 (0)