Search in sources :

Example 1 with TableSpace

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

the class MemoryMetadataStorageManager method updateTableSpace.

@Override
public boolean updateTableSpace(TableSpace tableSpace, TableSpace previous) throws DDLException {
    validateTableSpace(tableSpace);
    lock.writeLock().lock();
    try {
        TableSpace prev = tableSpaces.get(tableSpace.name);
        if (prev == null) {
            throw new TableSpaceDoesNotExistException(tableSpace.name);
        }
        tableSpaces.put(tableSpace.name, tableSpace);
        return false;
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : TableSpace(herddb.model.TableSpace) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException)

Example 2 with TableSpace

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

the class AlterTablespaceSQLTest method createAlterTableSpace.

@Test
public void createAlterTableSpace() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assertTrue(manager.waitForTablespace(TableSpace.DEFAULT, 10000));
        execute(manager, "EXECUTE CREATETABLESPACE 'ttt'", Collections.emptyList());
        execute(manager, "CREATE TABLESPACE 'ttt2','leader:" + nodeId + "'", Collections.emptyList());
        try {
            execute(manager, "EXECUTE CREATETABLESPACE 'ttt2','leader:othernode'", Collections.emptyList());
            fail();
        } catch (TableSpaceAlreadyExistsException err) {
        }
        execute(manager, "EXECUTE CREATETABLESPACE 'ttt3','leader:othernode'", Collections.emptyList());
        execute(manager, "EXECUTE CREATETABLESPACE 'ttt4','leader:othernode','replica:" + nodeId + ",othernode'", Collections.emptyList());
        execute(manager, "EXECUTE ALTERTABLESPACE 'ttt3','replica:" + nodeId + ",othernode','expectedReplicaCount:2'", Collections.emptyList());
        execute(manager, "EXECUTE ALTERTABLESPACE 'ttt3','leader:othernode'", Collections.emptyList());
        execute(manager, "EXECUTE ALTERTABLESPACE 'ttt3','expectedReplicaCount:12'", Collections.emptyList());
        TableSpace ttt3 = manager.getMetadataStorageManager().describeTableSpace("ttt3");
        assertEquals("othernode", ttt3.leaderId);
        assertEquals(12, ttt3.expectedReplicaCount);
        assertTrue(ttt3.replicas.contains("othernode"));
        assertTrue(ttt3.replicas.contains(nodeId));
        try (DataScanner scan = scan(manager, "SELECT * FROM SYSTABLESPACES", Collections.emptyList())) {
            List<DataAccessor> tuples = scan.consume();
            assertEquals(5, tuples.size());
            for (DataAccessor t : tuples) {
                System.out.println("tablespace: " + t.toMap());
                assertNotNull(t.get("expectedreplicacount"));
                assertNotNull(t.get("tablespace_name"));
                assertNotNull(t.get("replica"));
                assertNotNull(t.get("leader"));
            }
        }
        try (DataScanner scan = scan(manager, "SELECT expectedreplicacount FROM SYSTABLESPACES where tablespace_name='ttt3'", Collections.emptyList())) {
            List<DataAccessor> tuples = scan.consume();
            assertEquals(1, tuples.size());
            for (DataAccessor t : tuples) {
                System.out.println("tablespace: " + t.toMap());
                assertEquals(12, t.get("expectedreplicacount"));
            }
        }
    }
}
Also used : TableSpaceAlreadyExistsException(herddb.model.TableSpaceAlreadyExistsException) TableSpace(herddb.model.TableSpace) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 3 with TableSpace

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

the class AlterTablespaceSQLTest method escapeTableSpaceName.

@Test
public void escapeTableSpaceName() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager(nodeId, new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assertTrue(manager.waitForTablespace(TableSpace.DEFAULT, 10000));
        execute(manager, "CREATE TABLESPACE `default`,'leader:" + nodeId + "'", Collections.emptyList());
        try {
            execute(manager, "EXECUTE CREATETABLESPACE `default`,'leader:othernode'", Collections.emptyList());
            fail();
        } catch (TableSpaceAlreadyExistsException err) {
        }
        execute(manager, "EXECUTE ALTERTABLESPACE `default`,`replica:" + nodeId + ",othernode`,'expectedReplicaCount:2'", Collections.emptyList());
        execute(manager, "EXECUTE ALTERTABLESPACE 'default','leader:othernode'", Collections.emptyList());
        execute(manager, "EXECUTE ALTERTABLESPACE 'default','expectedReplicaCount:12'", Collections.emptyList());
        TableSpace ttt3 = manager.getMetadataStorageManager().describeTableSpace("default");
        assertEquals("othernode", ttt3.leaderId);
        assertEquals(12, ttt3.expectedReplicaCount);
        assertTrue(ttt3.replicas.contains("othernode"));
        assertTrue(ttt3.replicas.contains(nodeId));
        try (DataScanner scan = scan(manager, "SELECT * FROM SYSTABLESPACES", Collections.emptyList())) {
            List<DataAccessor> tuples = scan.consume();
            assertEquals(2, tuples.size());
            for (DataAccessor t : tuples) {
                System.out.println("tablespace: " + t.toMap());
                assertNotNull(t.get("expectedreplicacount"));
                assertNotNull(t.get("tablespace_name"));
                assertNotNull(t.get("replica"));
                assertNotNull(t.get("leader"));
            }
        }
        try (DataScanner scan = scan(manager, "SELECT expectedreplicacount FROM SYSTABLESPACES where tablespace_name='default'", Collections.emptyList())) {
            List<DataAccessor> tuples = scan.consume();
            assertEquals(1, tuples.size());
            for (DataAccessor t : tuples) {
                System.out.println("tablespace: " + t.toMap());
                assertEquals(12, t.get("expectedreplicacount"));
            }
        }
    }
}
Also used : TableSpaceAlreadyExistsException(herddb.model.TableSpaceAlreadyExistsException) TableSpace(herddb.model.TableSpace) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 4 with TableSpace

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

the class PrimaryIndexScanRangeTest method primaryIndexPrefixScanFileTest.

@Test
public void primaryIndexPrefixScanFileTest() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new FileDataStorageManager(dataPath), new MemoryCommitLogManager(), null, 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("n1", ColumnTypes.INTEGER).column("n2", ColumnTypes.INTEGER).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("n1").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('a',1,5,'n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('b',2,5,'n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('c',3,6,'n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('d',5,7,'n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('e',6,5,'n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('f',7,7,'n2')", Collections.emptyList());
        performBasicPlannerTests(manager);
        assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("t1").getKeyToPageIndex().isSortedAscending());
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(3, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
                assertEquals(6, tuples.get(2).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "limit 2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertNull(scan.getComparator());
            assertNotNull(scan.getLimits());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(3, tuples.get(0).get("n1"));
                assertEquals(6, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 1,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(3, tuples.get(0).get("n1"));
                assertEquals(5, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 3,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(6, tuples.get(0).get("n1"));
                assertEquals(7, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 4,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(1, tuples.size());
                assertEquals(7, tuples.get(0).get("n1"));
            }
        }
        // add a record in the middle of the sorted recordset
        DMLStatementExecutionResult res = TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',4,6,'n2')", Collections.emptyList(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
        long tx = res.transactionId;
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(4, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
                assertEquals(4, tuples.get(2).get("n1"));
                assertEquals(6, tuples.get(3).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(6, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
                assertEquals(4, tuples.get(2).get("n1"));
                assertEquals(5, tuples.get(3).get("n1"));
                assertEquals(6, tuples.get(4).get("n1"));
                assertEquals(7, tuples.get(5).get("n1"));
            }
        }
        System.out.println("QUAAAAAAAA");
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
            }
        }
        System.out.println("QUIIIIII");
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                for (DataAccessor tuple : tuples) {
                    System.out.println("tuple: " + tuple.toMap());
                }
                assertEquals(3, tuples.size());
                assertEquals(3, tuples.get(0).get("n1"));
                assertEquals(4, tuples.get(1).get("n1"));
                assertEquals(5, tuples.get(2).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=6 " + "order by n1 " + "limit 1,3", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(3, tuples.size());
                assertEquals(3, tuples.get(0).get("n1"));
                assertEquals(4, tuples.get(1).get("n1"));
                assertEquals(6, tuples.get(2).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 1,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(3, tuples.get(0).get("n1"));
                assertEquals(4, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 3,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(5, tuples.get(0).get("n1"));
                assertEquals(6, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 4,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(2, tuples.size());
                assertEquals(6, tuples.get(0).get("n1"));
                assertEquals(7, tuples.get(1).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 5,2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(1, tuples.size());
                assertEquals(7, tuples.get(0).get("n1"));
            }
        }
        // add other records in the context of the transaction
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',8,6,'n3')", Collections.emptyList(), new TransactionContext(tx));
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',9,6,'n3')", Collections.emptyList(), new TransactionContext(tx));
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('g',10,6,'n3')", Collections.emptyList(), new TransactionContext(tx));
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 ", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(9, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
                assertEquals(4, tuples.get(2).get("n1"));
                assertEquals(5, tuples.get(3).get("n1"));
                assertEquals(6, tuples.get(4).get("n1"));
                assertEquals(7, tuples.get(5).get("n1"));
                assertEquals(8, tuples.get(6).get("n1"));
                assertEquals(9, tuples.get(7).get("n1"));
                assertEquals(10, tuples.get(8).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2 " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and n2<=7 " + "order by n1 " + "limit 7", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                assertEquals(7, tuples.size());
                assertEquals(2, tuples.get(0).get("n1"));
                assertEquals(3, tuples.get(1).get("n1"));
                assertEquals(4, tuples.get(2).get("n1"));
                assertEquals(5, tuples.get(3).get("n1"));
                assertEquals(6, tuples.get(4).get("n1"));
                assertEquals(7, tuples.get(5).get("n1"));
                assertEquals(8, tuples.get(6).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 " + "limit 200", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                tuples.forEach(t -> {
                    System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
                });
                assertEquals(3, tuples.size());
                assertEquals(8, tuples.get(0).get("n1"));
                assertEquals(9, tuples.get(1).get("n1"));
                assertEquals(10, tuples.get(2).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 ", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                tuples.forEach(t -> {
                    System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
                });
                assertEquals(3, tuples.size());
                assertEquals(8, tuples.get(0).get("n1"));
                assertEquals(9, tuples.get(1).get("n1"));
                assertEquals(10, tuples.get(2).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 desc", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertFalse(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                tuples.forEach(t -> {
                    System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
                });
                assertEquals(3, tuples.size());
                assertEquals(10, tuples.get(0).get("n1"));
                assertEquals(9, tuples.get(1).get("n1"));
                assertEquals(8, tuples.get(2).get("n1"));
            }
        }
        {
            TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT n1,n2,name " + "FROM tblspace1.t1 " + "WHERE n1>=2 " + "and name='n3' " + "order by n1 " + "limit 2", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate().getIndexOperation() instanceof PrimaryIndexRangeScan);
            assertTrue(scan.getComparator().isOnlyPrimaryKeyAndAscending());
            try (DataScanner scan1 = manager.scan(scan, translated.context, new TransactionContext(tx))) {
                List<DataAccessor> tuples = scan1.consume();
                tuples.forEach(t -> {
                    System.out.println("OK sortedByClusteredIndex tuple " + t.toMap());
                });
                assertEquals(2, tuples.size());
                assertEquals(8, tuples.get(0).get("n1"));
                assertEquals(9, tuples.get(1).get("n1"));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) PrimaryIndexRangeScan(herddb.index.PrimaryIndexRangeScan) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScannerException(herddb.model.DataScannerException) Table(herddb.model.Table) CreateTableStatement(herddb.model.commands.CreateTableStatement) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) TransactionContext(herddb.model.TransactionContext) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) Path(java.nio.file.Path) DataAccessor(herddb.utils.DataAccessor) DataScanner(herddb.model.DataScanner) StatementExecutionException(herddb.model.StatementExecutionException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ColumnTypes(herddb.model.ColumnTypes) ScanStatement(herddb.model.commands.ScanStatement) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) FileDataStorageManager(herddb.file.FileDataStorageManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) TranslatedQuery(herddb.sql.TranslatedQuery) StatementEvaluationContext(herddb.model.StatementEvaluationContext) PlannerOp(herddb.model.planner.PlannerOp) TableSpace(herddb.model.TableSpace) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) TemporaryFolder(org.junit.rules.TemporaryFolder) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) DataAccessor(herddb.utils.DataAccessor) CreateTableStatement(herddb.model.commands.CreateTableStatement) PrimaryIndexRangeScan(herddb.index.PrimaryIndexRangeScan) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) FileDataStorageManager(herddb.file.FileDataStorageManager) List(java.util.List) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 5 with TableSpace

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

the class DBManager method manageTableSpaces.

private boolean manageTableSpaces() {
    Collection<String> actualTablesSpaces;
    try {
        actualTablesSpaces = metadataStorageManager.listTableSpaces();
    } catch (MetadataStorageManagerException error) {
        LOGGER.log(Level.SEVERE, "cannot access tablespaces metadata", error);
        return true;
    }
    Map<String, TableSpace> actualTableSpaceMetadata = new HashMap<>();
    generalLock.writeLock().lock();
    try {
        for (String tableSpace : actualTablesSpaces) {
            TableSpace tableSpaceMetadata = metadataStorageManager.describeTableSpace(tableSpace);
            actualTableSpaceMetadata.put(tableSpaceMetadata.uuid, tableSpaceMetadata);
            try {
                handleTableSpace(tableSpaceMetadata);
            } catch (Exception err) {
                LOGGER.log(Level.SEVERE, "cannot handle tablespace " + tableSpace, err);
                if (haltOnTableSpaceBootError && haltProcedure != null) {
                    err.printStackTrace();
                    haltProcedure.run();
                }
            }
        }
    } catch (MetadataStorageManagerException error) {
        LOGGER.log(Level.SEVERE, "cannot access tablespaces metadata", error);
        return true;
    } finally {
        generalLock.writeLock().unlock();
    }
    List<TableSpaceManager> followingActiveTableSpaces = new ArrayList<>();
    Set<String> failedTableSpaces = new HashSet<>();
    for (Map.Entry<String, TableSpaceManager> entry : tablesSpaces.entrySet()) {
        try {
            String tableSpaceUuid = entry.getValue().getTableSpaceUUID();
            if (entry.getValue().isFailed()) {
                failedTableSpaces.add(entry.getKey());
            } else if (!entry.getKey().equals(virtualTableSpaceId) && !actualTablesSpaces.contains(entry.getKey())) {
                failedTableSpaces.add(entry.getKey());
            } else if (entry.getValue().isLeader()) {
                metadataStorageManager.updateTableSpaceReplicaState(TableSpaceReplicaState.builder().mode(TableSpaceReplicaState.MODE_LEADER).nodeId(nodeId).uuid(tableSpaceUuid).timestamp(System.currentTimeMillis()).build());
            } else {
                metadataStorageManager.updateTableSpaceReplicaState(TableSpaceReplicaState.builder().mode(TableSpaceReplicaState.MODE_FOLLOWER).nodeId(nodeId).uuid(tableSpaceUuid).timestamp(System.currentTimeMillis()).build());
                followingActiveTableSpaces.add(entry.getValue());
            }
        } catch (MetadataStorageManagerException error) {
            LOGGER.log(Level.SEVERE, "cannot access tablespace " + entry.getKey() + " metadata", error);
            return true;
        }
    }
    if (!failedTableSpaces.isEmpty()) {
        generalLock.writeLock().lock();
        try {
            for (String tableSpace : failedTableSpaces) {
                stopTableSpace(tableSpace, null);
            }
        } catch (MetadataStorageManagerException error) {
            LOGGER.log(Level.SEVERE, "cannot access tablespace metadata", error);
            return true;
        } finally {
            generalLock.writeLock().unlock();
        }
    }
    if (!followingActiveTableSpaces.isEmpty()) {
        long now = System.currentTimeMillis();
        try {
            for (TableSpaceManager tableSpaceManager : followingActiveTableSpaces) {
                String tableSpaceUuid = tableSpaceManager.getTableSpaceUUID();
                TableSpace tableSpaceInfo = actualTableSpaceMetadata.get(tableSpaceUuid);
                if (tableSpaceInfo != null && tableSpaceInfo.maxLeaderInactivityTime > 0 && !tableSpaceManager.isFailed()) {
                    List<TableSpaceReplicaState> allReplicas = metadataStorageManager.getTableSpaceReplicaState(tableSpaceUuid);
                    TableSpaceReplicaState leaderState = allReplicas.stream().filter(t -> t.mode == TableSpaceReplicaState.MODE_LEADER && !t.nodeId.equals(nodeId)).findAny().orElse(null);
                    if (leaderState == null) {
                        LOGGER.log(Level.SEVERE, "Leader for " + tableSpaceUuid + " should be " + tableSpaceInfo.leaderId + ", but it never sent pings or it disappeared");
                        tryBecomeLeaderFor(tableSpaceInfo);
                    } else {
                        long delta = now - leaderState.timestamp;
                        if (tableSpaceInfo.maxLeaderInactivityTime > delta) {
                            LOGGER.log(Level.FINER, "Leader for " + tableSpaceUuid + " is " + leaderState.nodeId + ", last ping " + new java.sql.Timestamp(leaderState.timestamp) + ". leader is healty");
                        } else {
                            LOGGER.log(Level.SEVERE, "Leader for " + tableSpaceUuid + " is " + leaderState.nodeId + ", last ping " + new java.sql.Timestamp(leaderState.timestamp) + ". leader is failed. trying to take leadership");
                            tryBecomeLeaderFor(tableSpaceInfo);
                            // only one change at a time
                            break;
                        }
                    }
                }
            }
        } catch (MetadataStorageManagerException | DDLException error) {
            LOGGER.log(Level.SEVERE, "cannot access tablespace metadata", error);
            return true;
        }
    }
    return false;
}
Also used : TableSpace(herddb.model.TableSpace) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataStorageManagerException(herddb.storage.DataStorageManagerException) LogNotAvailableException(herddb.log.LogNotAvailableException) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DDLException(herddb.model.DDLException) HashSet(java.util.HashSet)

Aggregations

TableSpace (herddb.model.TableSpace)22 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)15 StatementExecutionException (herddb.model.StatementExecutionException)10 DDLException (herddb.model.DDLException)9 LogNotAvailableException (herddb.log.LogNotAvailableException)7 DataStorageManagerException (herddb.storage.DataStorageManagerException)7 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)6 DataScanner (herddb.model.DataScanner)6 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)5 DataScannerException (herddb.model.DataScannerException)5 TableSpaceDoesNotExistException (herddb.model.TableSpaceDoesNotExistException)5 ArrayList (java.util.ArrayList)5 NotLeaderException (herddb.model.NotLeaderException)4 TableSpaceReplicaState (herddb.model.TableSpaceReplicaState)4 DataAccessor (herddb.utils.DataAccessor)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ClientConfiguration (herddb.client.ClientConfiguration)3 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)3 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)3 Path (java.nio.file.Path)3