Search in sources :

Example 71 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class SimpleRecoveryTest method ignoreDeleteWithMissingTransactionOnRecovery.

@Test
public void ignoreDeleteWithMissingTransactionOnRecovery() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Bytes key = Bytes.from_int(1234);
    Bytes value = Bytes.from_long(8888);
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    String nodeId = "localhost";
    String tablespaceUUID = null;
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.checkpoint();
        InsertStatement insert = new InsertStatement("tblspace1", "t1", new Record(key, value));
        assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        tablespaceUUID = manager.getTableSpaceManager("tblspace1").getTableSpaceUUID();
    }
    try (FileCommitLogManager commitLogManager = new FileCommitLogManager(logsPath)) {
        commitLogManager.start();
        try (FileCommitLog log = commitLogManager.createCommitLog(tablespaceUUID, "tblspace1", nodeId)) {
            log.recovery(LogSequenceNumber.START_OF_TIME, (n, e) -> {
            }, false);
            log.startWriting(1);
            /* Insert an entry for a unknown transaction id */
            LogEntry entry = new LogEntry(System.currentTimeMillis(), LogEntryType.DELETE, 1024, "t1", key, null);
            log.log(entry, true).getLogSequenceNumber();
        }
    }
    final boolean original = TableManager.ignoreMissingTransactionsOnRecovery;
    TableManager.ignoreMissingTransactionsOnRecovery = true;
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace("tblspace1", 10000);
        {
            GetStatement get = new GetStatement("tblspace1", "t1", key, null, false);
            GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
        }
    } finally {
        TableManager.ignoreMissingTransactionsOnRecovery = original;
    }
}
Also used : Path(java.nio.file.Path) Table(herddb.model.Table) GetResult(herddb.model.GetResult) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) FileCommitLog(herddb.file.FileCommitLog) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) Record(herddb.model.Record) LogEntry(herddb.log.LogEntry) Test(org.junit.Test)

Example 72 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class SimpleRecoveryTest method createInsertAndRestart.

@Test
public void createInsertAndRestart() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Bytes key = Bytes.from_int(1234);
    Bytes value = Bytes.from_long(8888);
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.checkpoint();
        InsertStatement insert = new InsertStatement("tblspace1", "t1", new Record(key, value));
        assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace("tblspace1", 10000);
        GetStatement get = new GetStatement("tblspace1", "t1", key, null, false);
        GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        assertEquals(key, result.getRecord().key);
        assertEquals(value, result.getRecord().value);
    }
}
Also used : Path(java.nio.file.Path) Table(herddb.model.Table) GetResult(herddb.model.GetResult) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) Record(herddb.model.Record) Test(org.junit.Test)

Example 73 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class SimpleRecoveryTest method createInsertDeleteDifferentTransactionWithFlushTableAndRestart.

@Test
public void createInsertDeleteDifferentTransactionWithFlushTableAndRestart() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Bytes key = Bytes.from_int(1234);
    Bytes key2 = Bytes.from_int(1235);
    Bytes value = Bytes.from_long(8888);
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        long tx = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
        InsertStatement insert = new InsertStatement("tblspace1", "t1", new Record(key, value));
        assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
        // flushing table data in the middle of the transaction
        // the record will not be present in the snapshot of the table
        manager.getTableSpaceManager("tblspace1").getTableManager("t1").flush();
        manager.executeStatement(new CommitTransactionStatement("tblspace1", tx), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        long tx2 = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
        DeleteStatement delete = new DeleteStatement("tblspace1", "t1", key, null);
        assertEquals(1, manager.executeUpdate(delete, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx2)).getUpdateCount());
        InsertStatement insert2 = new InsertStatement("tblspace1", "t1", new Record(key2, value));
        assertEquals(1, manager.executeUpdate(insert2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx2)).getUpdateCount());
        manager.executeStatement(new CommitTransactionStatement("tblspace1", tx2), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace("tblspace1", 10000);
        {
            GetStatement get = new GetStatement("tblspace1", "t1", key, null, false);
            GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            GetStatement get = new GetStatement("tblspace1", "t1", key2, null, false);
            GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(key2, result.getRecord().key);
            assertEquals(value, result.getRecord().value);
        }
    }
}
Also used : Path(java.nio.file.Path) TransactionResult(herddb.model.TransactionResult) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Record(herddb.model.Record) Test(org.junit.Test)

Example 74 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class SimpleRecoveryTest method rollbackCreateTableOnRestart.

@Test
public void rollbackCreateTableOnRestart() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Bytes key = Bytes.from_int(1234);
    Bytes value = Bytes.from_long(8888);
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.checkpoint();
        Table transacted_table = Table.builder().tablespace("tblspace1").name("t2").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        long tx = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
        CreateTableStatement st_create = new CreateTableStatement(transacted_table);
        manager.executeStatement(st_create, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        InsertStatement insert = new InsertStatement("tblspace1", "t2", new Record(key, value));
        assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
        GetStatement get = new GetStatement("tblspace1", "t2", key, null, false);
        GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        assertTrue(result.found());
        assertEquals(key, result.getRecord().key);
        assertEquals(value, result.getRecord().value);
    // transaction is not committed, and the leader dies, the table will disappear
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace("tblspace1", 10000);
        GetStatement get = new GetStatement("tblspace1", "t2", key, null, false);
        try {
            manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            fail();
        } catch (TableDoesNotExistException error) {
        }
    }
}
Also used : Path(java.nio.file.Path) TransactionResult(herddb.model.TransactionResult) Table(herddb.model.Table) GetResult(herddb.model.GetResult) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) InsertStatement(herddb.model.commands.InsertStatement) TableDoesNotExistException(herddb.model.TableDoesNotExistException) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Record(herddb.model.Record) Test(org.junit.Test)

Example 75 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class SimpleClusterTest method test.

@Test
public void test() throws Exception {
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("0"));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    assertEquals(0, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableName));
    manager.checkpoint();
    String tableUuid = manager.getTableSpaceManager(tableSpace).getTableManager(tableName).getTable().uuid;
    assertNotNull(dataStorageManager.readPage(tableSpaceUUID, tableUuid, 1L));
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
    }
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("5"));
        UpdateStatement st = new UpdateStatement(tableSpace, tableName, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    // a new page must be allocated
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("6"));
        UpdateStatement st = new UpdateStatement(tableSpace, tableName, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        Record record = new Record(Bytes.from_string("key1"), Bytes.from_string("7"));
        UpdateStatement st = new UpdateStatement(tableSpace, tableName, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    // only a new page must be allocated, not two more
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key1"), null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        GetResult result = manager.get(new GetStatement(tableSpace, tableName, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertFalse(result.found());
    }
    // a delete does not trigger new pages in this case
    manager.checkpoint();
    assertEquals(0, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        assertEquals(1, manager.executeUpdate(new InsertStatement(tableSpace, tableName, new Record(Bytes.from_string("key2"), Bytes.from_string("50"))), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
        assertEquals(1, manager.executeUpdate(new InsertStatement(tableSpace, tableName, new Record(Bytes.from_string("key3"), Bytes.from_string("60"))), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key2"), null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    // a new page, containg the key3 record is needed
    manager.checkpoint();
    assertEquals(1, dataStorageManager.getActualNumberOfPages(tableSpaceUUID, tableUuid));
    Holder<TableStatus> _tableStatus = new Holder<>();
    dataStorageManager.fullTableScan(tableSpaceUUID, tableUuid, new FullTableScanConsumer() {

        @Override
        public void acceptTableStatus(TableStatus tableStatus) {
            _tableStatus.value = tableStatus;
        }
    });
    for (long pageId : _tableStatus.value.activePages.keySet()) {
        List<Record> records = dataStorageManager.readPage(tableSpaceUUID, tableUuid, pageId);
        System.out.println("PAGE #" + pageId + " records :" + records);
    }
    assertEquals(1, _tableStatus.value.activePages.size());
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) GetResult(herddb.model.GetResult) Holder(herddb.utils.Holder) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) GetStatement(herddb.model.commands.GetStatement) TableStatus(herddb.storage.TableStatus) Record(herddb.model.Record) Test(org.junit.Test)

Aggregations

GetResult (herddb.model.GetResult)96 GetStatement (herddb.model.commands.GetStatement)91 Test (org.junit.Test)85 InsertStatement (herddb.model.commands.InsertStatement)84 Table (herddb.model.Table)70 CreateTableStatement (herddb.model.commands.CreateTableStatement)68 Record (herddb.model.Record)58 Bytes (herddb.utils.Bytes)49 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)46 Path (java.nio.file.Path)43 TransactionContext (herddb.model.TransactionContext)41 TransactionResult (herddb.model.TransactionResult)30 FileCommitLogManager (herddb.file.FileCommitLogManager)29 FileDataStorageManager (herddb.file.FileDataStorageManager)29 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)29 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)29 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)28 UpdateStatement (herddb.model.commands.UpdateStatement)24 DeleteStatement (herddb.model.commands.DeleteStatement)21 ScanStatement (herddb.model.commands.ScanStatement)21