Search in sources :

Example 6 with Database

use of org.cojen.tupl.Database in project Tupl by cojen.

the class Shutdown method main.

/**
 * @param args a base file path for the database, and an optional cache size
 */
public static void main(String[] args) throws Exception {
    var config = new DatabaseConfig().createFilePath(false).baseFilePath(args[0]).eventListener(EventListener.printTo(System.out));
    if (args.length > 1) {
        config.minCacheSize(Long.parseLong(args[1]));
    }
    Database db = Database.open(config);
    DatabaseStats stats = db.stats();
    db.shutdown();
    System.out.println(stats);
}
Also used : DatabaseStats(org.cojen.tupl.diag.DatabaseStats) Database(org.cojen.tupl.Database) DatabaseConfig(org.cojen.tupl.DatabaseConfig)

Example 7 with Database

use of org.cojen.tupl.Database in project Tupl by cojen.

the class Verify method main.

/**
 * @param args first argument is a base file path for the database, second optional
 * argument is the cache size
 */
public static void main(String[] args) throws Exception {
    var config = new DatabaseConfig().createFilePath(false).baseFilePath(args[0]).eventListener(EventListener.printTo(System.out));
    if (args.length > 1) {
        config.minCacheSize(Long.parseLong(args[1]));
    }
    Database db = Database.open(config);
    System.out.println(db.stats());
    var v = new Verify();
    db.verify(v);
    System.out.println(v);
    System.exit(v.failed);
}
Also used : Database(org.cojen.tupl.Database) DatabaseConfig(org.cojen.tupl.DatabaseConfig)

Example 8 with Database

use of org.cojen.tupl.Database in project Tupl by cojen.

the class DatabaseReplicatorTest method valueWriteRecover.

@Test
public void valueWriteRecover() throws Exception {
    // Verifies that a checkpoint in the middle of a value write on the replica can still
    // properly recover the registered cursor.
    Database[] dbs = startGroup(2, Role.OBSERVER, null);
    Database leaderDb = dbs[0];
    Database replicaDb = dbs[1];
    Index leaderIx = leaderDb.openIndex("test");
    var rnd = new Random();
    var part1 = new byte[1000];
    rnd.nextBytes(part1);
    var part2 = new byte[1000];
    rnd.nextBytes(part2);
    Transaction txn = leaderDb.newTransaction();
    byte[] key = "key1".getBytes();
    Cursor c = leaderIx.newAccessor(txn, key);
    c.valueWrite(0, part1, 0, part1.length);
    txn.flush();
    // Wait for replica to catch up.
    fence(leaderDb, replicaDb);
    Index replicaIx = replicaDb.openIndex("test");
    assertTrue(replicaIx.exists(Transaction.BOGUS, key));
    replicaDb.checkpoint();
    replicaDb = closeAndReopen(1);
    replicaIx = replicaDb.openIndex("test");
    assertTrue(replicaIx.exists(Transaction.BOGUS, key));
    // Finish writing and wait for replica to catch up.
    c.valueWrite(part1.length, part2, 0, part2.length);
    c.close();
    txn.commit();
    fence(leaderDb, replicaDb);
    var expect = new byte[part1.length + part2.length];
    System.arraycopy(part1, 0, expect, 0, part1.length);
    System.arraycopy(part2, 0, expect, part1.length, part2.length);
    fastAssertArrayEquals(expect, leaderIx.load(null, key));
    fastAssertArrayEquals(expect, replicaIx.load(null, key));
    replicaDb.close();
}
Also used : Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Transaction(org.cojen.tupl.Transaction) Database(org.cojen.tupl.Database) Index(org.cojen.tupl.Index) Cursor(org.cojen.tupl.Cursor)

Example 9 with Database

use of org.cojen.tupl.Database in project Tupl by cojen.

the class DatabaseReplicatorTest method emergencyRecovery.

@Test
public void emergencyRecovery() throws Exception {
    // Test that database can be opened after the replication files are deleted. Anything
    // in them is lost.
    Database db = startGroup(1)[0];
    Index ix = db.openIndex("test");
    ix.store(null, "k1".getBytes(), "v1".getBytes());
    db.suspendCheckpoints();
    db.checkpoint();
    ix.store(null, "k2".getBytes(), "v2".getBytes());
    db.close();
    File baseFile = mReplBaseFiles[0];
    String prefix = baseFile.getName();
    baseFile.getParentFile().listFiles(file -> {
        String name = file.getName();
        if (name.startsWith(prefix) && !name.endsWith(".db")) {
            file.delete();
        }
        return false;
    });
    db = closeAndReopen(0);
    ix = db.openIndex("test");
    fastAssertArrayEquals("v1".getBytes(), ix.load(null, "k1".getBytes()));
    assertNull(ix.load(null, "k2".getBytes()));
    db.close();
}
Also used : Database(org.cojen.tupl.Database) Index(org.cojen.tupl.Index) File(java.io.File)

Example 10 with Database

use of org.cojen.tupl.Database in project Tupl by cojen.

the class DatabaseReplicatorTest method explicitFailover.

@Test
public void explicitFailover() throws Exception {
    Database[] dbs = startGroup(2);
    Database leaderDb = dbs[0];
    Database replicaDb = dbs[1];
    Index leaderIx = leaderDb.openIndex("test");
    // Wait for replica to catch up.
    fence(leaderDb, replicaDb);
    Index replicaIx = replicaDb.openIndex("test");
    byte[] key = "hello".getBytes();
    byte[] value = "world".getBytes();
    leaderDb.failover();
    try {
        leaderIx.store(null, key, value);
        fail();
    } catch (UnmodifiableReplicaException e) {
    }
    boolean success = false;
    for (int i = 0; i < 10; i++) {
        try {
            replicaIx.store(null, key, value);
            success = true;
            break;
        } catch (UnmodifiableReplicaException e) {
        }
        TestUtils.sleep(1000);
    }
    assertTrue(success);
    // Wait for old leader to catch up.
    fence(replicaDb, leaderDb);
    fastAssertArrayEquals(value, leaderIx.load(null, key));
}
Also used : UnmodifiableReplicaException(org.cojen.tupl.UnmodifiableReplicaException) Database(org.cojen.tupl.Database) Index(org.cojen.tupl.Index)

Aggregations

Database (org.cojen.tupl.Database)15 Index (org.cojen.tupl.Index)10 UnmodifiableReplicaException (org.cojen.tupl.UnmodifiableReplicaException)8 Transaction (org.cojen.tupl.Transaction)6 DatabaseConfig (org.cojen.tupl.DatabaseConfig)5 PrepareHandler (org.cojen.tupl.ext.PrepareHandler)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 Cursor (org.cojen.tupl.Cursor)3 LockTimeoutException (org.cojen.tupl.LockTimeoutException)2 File (java.io.File)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Random (java.util.Random)1 LinkedTransferQueue (java.util.concurrent.LinkedTransferQueue)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 DatabaseStats (org.cojen.tupl.diag.DatabaseStats)1 EventListener (org.cojen.tupl.diag.EventListener)1