Search in sources :

Example 51 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class SimpleFollowerTest method testLeaderOnlineLogAvailableMultipleVersionsActivePages.

@Test
public void testLeaderOnlineLogAvailableMultipleVersionsActivePages() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 1000);
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    // delete ledgers soon
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_RETENTION_PERIOD, 1);
    serverconfig_1.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    // disabled
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_MAX_IDLE_TIME, 0);
    /*
         * Disable page compaction (avoid compaction of dirty page)
         */
    serverconfig_1.set(ServerConfiguration.PROPERTY_FILL_PAGE_THRESHOLD, 0.0D);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT, true).set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("s", ColumnTypes.STRING).primaryKey("c").build();
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TableSpaceManager tableSpaceManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        AbstractTableManager tableManager = tableSpaceManager.getTableManager("t1");
        // fill table
        long tx = herddb.core.TestUtils.beginTransaction(server_1.getManager(), TableSpace.DEFAULT);
        for (int i = 0; i < 1000; i++) {
            server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", i, "s", "1")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        }
        herddb.core.TestUtils.commitTransaction(server_1.getManager(), TableSpace.DEFAULT, tx);
        // when we send the dump to the follower we must send only the latest version of the record
        for (int i = 0; i < 10; i++) {
            tableManager.flush();
            server_1.getManager().executeUpdate(new UpdateStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1, "s", "2" + i), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        }
        BooleanHolder foundDuplicate = new BooleanHolder(false);
        server_1.getManager().getDataStorageManager().fullTableScan(tableSpaceManager.getTableSpaceUUID(), tableManager.getTable().uuid, new FullTableScanConsumer() {

            Map<Bytes, Long> recordPage = new HashMap<>();

            @Override
            public void acceptPage(long pageId, List<Record> records) {
                for (Record record : records) {
                    Long prev = recordPage.put(record.key, pageId);
                    if (prev != null) {
                        foundDuplicate.value = true;
                    }
                }
            }
        });
        assertTrue(foundDuplicate.value);
        // data will be downloaded from the server_1 (PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT)
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            assertTrue(server_2.getManager().isTableSpaceLocallyRecoverable(server_1.getMetadataStorageManager().describeTableSpace(TableSpace.DEFAULT)));
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
        }
    }
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) Server(herddb.server.Server) HashMap(java.util.HashMap) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) BooleanHolder(herddb.utils.BooleanHolder) InsertStatement(herddb.model.commands.InsertStatement) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) Bytes(herddb.utils.Bytes) AbstractTableManager(herddb.core.AbstractTableManager) TransactionContext(herddb.model.TransactionContext) TableSpaceManager(herddb.core.TableSpaceManager) Record(herddb.model.Record) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class SimpleTransactionTest method testInsertDelete.

@Test
public void testInsertDelete() throws Exception {
    Bytes key = Bytes.from_string("key1");
    {
        Record record = new Record(key, Bytes.from_int(0));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    long tx = beginTransaction();
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, key, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    {
        Record record = new Record(key, Bytes.from_int(1));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    commitTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get.found());
    assertEquals(Bytes.from_int(1), get.getRecord().value);
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 53 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class SimpleTransactionTest method testRollbackUpdate1.

@Test
public void testRollbackUpdate1() throws Exception {
    Bytes key = Bytes.from_string("key1");
    Record record = new Record(key, Bytes.from_int(0));
    InsertStatement st_insert = new InsertStatement(tableSpace, tableName, record);
    assertEquals(1, manager.executeUpdate(st_insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    long tx = beginTransaction();
    Record record2 = new Record(key, Bytes.from_int(1));
    UpdateStatement st_update = new UpdateStatement(tableSpace, tableName, record2, null);
    assertEquals(1, manager.executeUpdate(st_update, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    rollbackTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get.found());
    assertEquals(get.getRecord().value, record.value);
}
Also used : Bytes(herddb.utils.Bytes) UpdateStatement(herddb.model.commands.UpdateStatement) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 54 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class SimpleTransactionTest method testInsertInsert.

@Test
public void testInsertInsert() throws Exception {
    Bytes key = Bytes.from_string("key1");
    {
        Record record = new Record(key, Bytes.from_int(0));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    long tx = beginTransaction();
    {
        Record record = new Record(key, Bytes.from_int(1));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        try {
            manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount();
            fail();
        } catch (DuplicatePrimaryKeyException expected) {
        }
    }
}
Also used : Bytes(herddb.utils.Bytes) TransactionContext(herddb.model.TransactionContext) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 55 with Bytes

use of herddb.utils.Bytes in project herddb by diennea.

the class SimpleTransactionTest method testCommit.

@Test
public void testCommit() throws Exception {
    long tx = beginTransaction();
    Bytes key = Bytes.from_string("key1");
    {
        Record record = new Record(key, Bytes.from_int(0));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    commitTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get.found());
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Aggregations

Bytes (herddb.utils.Bytes)139 Record (herddb.model.Record)77 Test (org.junit.Test)71 Table (herddb.model.Table)68 TransactionContext (herddb.model.TransactionContext)62 InsertStatement (herddb.model.commands.InsertStatement)61 GetResult (herddb.model.GetResult)53 GetStatement (herddb.model.commands.GetStatement)51 CreateTableStatement (herddb.model.commands.CreateTableStatement)50 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)49 Path (java.nio.file.Path)34 TransactionResult (herddb.model.TransactionResult)28 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)28 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)28 DataStorageManagerException (herddb.storage.DataStorageManagerException)28 DataScanner (herddb.model.DataScanner)26 DeleteStatement (herddb.model.commands.DeleteStatement)24 UpdateStatement (herddb.model.commands.UpdateStatement)24 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)23 ArrayList (java.util.ArrayList)23