Search in sources :

Example 41 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class BaseTest method verifySequence.

private static void verifySequence(String tenantID, String sequenceName, String sequenceSchemaName, boolean exists, long value) throws SQLException {
    PhoenixConnection phxConn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
    String ddl = "SELECT " + PhoenixDatabaseMetaData.TENANT_ID + "," + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + PhoenixDatabaseMetaData.SEQUENCE_NAME + "," + PhoenixDatabaseMetaData.CURRENT_VALUE + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE ";
    ddl += " TENANT_ID  " + ((tenantID == null) ? "IS NULL " : " = '" + tenantID + "'");
    ddl += " AND SEQUENCE_NAME " + ((sequenceName == null) ? "IS NULL " : " = '" + sequenceName + "'");
    ddl += " AND SEQUENCE_SCHEMA " + ((sequenceSchemaName == null) ? "IS NULL " : " = '" + sequenceSchemaName + "'");
    ResultSet rs = phxConn.createStatement().executeQuery(ddl);
    if (exists) {
        assertTrue(rs.next());
        assertEquals(value, rs.getLong(4));
    } else {
        assertFalse(rs.next());
    }
    phxConn.close();
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet)

Example 42 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class ScannerLeaseRenewalTest method testRenewLeaseTaskBehavior.

@Test
public void testRenewLeaseTaskBehavior() throws Exception {
    // add connection to the queue
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    LinkedBlockingQueue<WeakReference<PhoenixConnection>> connectionsQueue = new LinkedBlockingQueue<>();
    connectionsQueue.add(new WeakReference<PhoenixConnection>(pconn));
    // create a scanner and add it to the queue
    int numLeaseRenewals = 4;
    int skipRenewLeaseCount = 2;
    int failToAcquireLockAt = 3;
    RenewLeaseOnlyTableIterator itr = new RenewLeaseOnlyTableIterator(numLeaseRenewals, skipRenewLeaseCount, failToAcquireLockAt, -1);
    LinkedBlockingQueue<WeakReference<TableResultIterator>> scannerQueue = pconn.getScanners();
    scannerQueue.add(new WeakReference<TableResultIterator>(itr));
    RenewLeaseTask task = new RenewLeaseTask(connectionsQueue);
    assertTrue(connectionsQueue.size() == 1);
    assertTrue(scannerQueue.size() == 1);
    task.run();
    assertTrue(connectionsQueue.size() == 1);
    // lease renewed
    assertTrue(scannerQueue.size() == 1);
    assertEquals(RENEWED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    // renew lease skipped but scanner still in the queue
    assertTrue(connectionsQueue.size() == 1);
    assertEquals(THRESHOLD_NOT_REACHED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1);
    // lock couldn't be acquired
    assertEquals(LOCK_NOT_ACQUIRED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1);
    // lease renewed
    assertEquals(RENEWED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue(connectionsQueue.size() == 1);
    // scanner closed and removed from the queue
    assertEquals(CLOSED, itr.getLastRenewLeaseStatus());
    pconn.close();
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue("Closing the connection should have removed it from the queue", connectionsQueue.size() == 0);
}
Also used : RenewLeaseOnlyTableIterator(org.apache.phoenix.iterate.RenewLeaseOnlyTableIterator) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) RenewLeaseTask(org.apache.phoenix.query.ConnectionQueryServicesImpl.RenewLeaseTask) WeakReference(java.lang.ref.WeakReference) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 43 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class AlterTableIT method testMetadataForMutableTable.

@Test
public void testMetadataForMutableTable() throws Exception {
    String schemaName = "XYZ";
    String baseTableName = generateUniqueName();
    String viewName = generateUniqueName();
    String fullTableName = schemaName + "." + baseTableName;
    String fullViewName = schemaName + "." + viewName;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS " + fullTableName + " (" + " ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " KV1 VARCHAR" + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)" + " ) " + tableDDLOptions);
        PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        long initBaseTableSeqNumber = baseTable.getSequenceNumber();
        // assert that the client side cache is updated.
        EncodedCQCounter cqCounter = baseTable.getEncodedCQCounter();
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, cqCounter.getNextQualifier(QueryConstants.DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata is updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "KV1", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber);
        // now create a view and validate client and server side metadata
        String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName;
        conn.createStatement().execute(viewDDL);
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));
        // verify that the client side cache is updated. Base table's cq counters should be updated.
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 3) : null, baseTable.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        assertNull("A view should always have the null cq counter", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata for the base table and the view is also updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + (columnEncoded ? 1 : 0));
        assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM);
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) EncodedCQCounter(org.apache.phoenix.schema.PTable.EncodedCQCounter) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 44 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class AlterTableWithViewsIT method testAlterPropertiesOfParentTable.

@Test
public void testAlterPropertiesOfParentTable() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl());
        Connection viewConn = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : conn) {
        String tableName = generateUniqueName();
        String viewOfTable1 = tableName + "_VIEW1";
        String viewOfTable2 = tableName + "_VIEW2";
        String ddlFormat = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + " %s ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (%s ID, COL1, COL2)" + " ) %s ";
        conn.createStatement().execute(generateDDL("UPDATE_CACHE_FREQUENCY=2", ddlFormat));
        viewConn.createStatement().execute("CREATE VIEW " + viewOfTable1 + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableName);
        viewConn.createStatement().execute("CREATE VIEW " + viewOfTable2 + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableName);
        viewConn.createStatement().execute("ALTER VIEW " + viewOfTable2 + " SET UPDATE_CACHE_FREQUENCY = 1");
        PhoenixConnection phoenixConn = conn.unwrap(PhoenixConnection.class);
        PTable table = phoenixConn.getTable(new PTableKey(null, tableName));
        PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
        assertFalse(table.isImmutableRows());
        assertEquals(2, table.getUpdateCacheFrequency());
        PTable viewTable1 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable1));
        assertFalse(viewTable1.isImmutableRows());
        assertEquals(2, viewTable1.getUpdateCacheFrequency());
        // query the view to force the table cache to be updated
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable2);
        PTable viewTable2 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable2));
        assertFalse(viewTable2.isImmutableRows());
        assertEquals(1, viewTable2.getUpdateCacheFrequency());
        conn.createStatement().execute("ALTER TABLE " + tableName + " SET IMMUTABLE_ROWS=true, UPDATE_CACHE_FREQUENCY=3");
        // query the views to force the table cache to be updated
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable1);
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable2);
        phoenixConn = conn.unwrap(PhoenixConnection.class);
        table = phoenixConn.getTable(new PTableKey(null, tableName));
        assertTrue(table.isImmutableRows());
        assertEquals(3, table.getUpdateCacheFrequency());
        viewTable1 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable1));
        assertTrue(viewTable1.isImmutableRows());
        assertEquals(3, viewTable1.getUpdateCacheFrequency());
        viewTable2 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable2));
        assertTrue(viewTable2.isImmutableRows());
        // update cache frequency is not propagated to the view since it was altered on the view
        assertEquals(1, viewTable2.getUpdateCacheFrequency());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PName(org.apache.phoenix.schema.PName) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Example 45 with PhoenixConnection

use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.

the class AlterTableIT method assertImmutableRows.

private static void assertImmutableRows(Connection conn, String fullTableName, boolean expectedValue) throws SQLException {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    assertEquals(expectedValue, pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName)).isImmutableRows());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey)

Aggregations

PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)216 Test (org.junit.Test)111 Connection (java.sql.Connection)57 Properties (java.util.Properties)52 PTable (org.apache.phoenix.schema.PTable)52 Scan (org.apache.hadoop.hbase.client.Scan)51 PhoenixPreparedStatement (org.apache.phoenix.jdbc.PhoenixPreparedStatement)48 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)47 PTableKey (org.apache.phoenix.schema.PTableKey)43 ResultSet (java.sql.ResultSet)41 PreparedStatement (java.sql.PreparedStatement)40 SQLException (java.sql.SQLException)40 Filter (org.apache.hadoop.hbase.filter.Filter)29 SkipScanFilter (org.apache.phoenix.filter.SkipScanFilter)29 RowKeyComparisonFilter (org.apache.phoenix.filter.RowKeyComparisonFilter)28 TestUtil.multiEncodedKVFilter (org.apache.phoenix.util.TestUtil.multiEncodedKVFilter)28 TestUtil.singleKVFilter (org.apache.phoenix.util.TestUtil.singleKVFilter)28 Statement (java.sql.Statement)19 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)17 PColumn (org.apache.phoenix.schema.PColumn)17