Search in sources :

Example 76 with PTableKey

use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.

the class PartialIndexRebuilderIT method testIndexWriteFailureDuringRebuild.

private void testIndexWriteFailureDuringRebuild(PIndexState indexStateOnFailure) throws Throwable {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    String indexName = generateUniqueName();
    final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    final String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
    PTableKey key = new PTableKey(null, fullTableName);
    final MyClock clock = new MyClock(1000);
    EnvironmentEdgeManager.injectEdge(clock);
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PMetaData metaCache = conn.unwrap(PhoenixConnection.class).getMetaDataCache();
        conn.createStatement().execute("CREATE TABLE " + fullTableName + "(k VARCHAR PRIMARY KEY, v1 VARCHAR, v2 VARCHAR, v3 VARCHAR) COLUMN_ENCODED_BYTES = 0, DISABLE_INDEX_ON_WRITE_FAILURE = " + (indexStateOnFailure == PIndexState.DISABLE));
        clock.time += 100;
        conn.createStatement().execute("CREATE INDEX " + indexName + " ON " + fullTableName + " (v1, v2)");
        clock.time += 100;
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a','a','0')");
        conn.commit();
        clock.time += 100;
        HTableInterface metaTable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        long disableTime = clock.currentTime();
        // Simulates an index write failure
        IndexUtil.updateIndexState(fullIndexName, indexStateOnFailure == PIndexState.DISABLE ? disableTime : -disableTime, metaTable, indexStateOnFailure);
        clock.time += 100;
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('bb','bb', '11')");
        conn.commit();
        // Large enough to be in separate time batch
        clock.time += 2 * REBUILD_PERIOD;
        assertTrue(hasIndexWithState(metaCache, key, indexStateOnFailure));
        assertEquals(2, TestUtil.getRowCount(conn, fullTableName));
        assertEquals(1, TestUtil.getRowCount(conn, fullIndexName));
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('ccc','ccc','222')");
        conn.commit();
        assertEquals(3, TestUtil.getRowCount(conn, fullTableName));
        assertEquals(1, TestUtil.getRowCount(conn, fullIndexName));
        clock.time += 100;
        waitForIndexState(conn, fullTableName, fullIndexName, indexStateOnFailure == PIndexState.DISABLE ? PIndexState.INACTIVE : PIndexState.ACTIVE);
        clock.time += WAIT_AFTER_DISABLED;
        // First batch should have been processed
        runIndexRebuilder(fullTableName);
        assertEquals(2, TestUtil.getRowCount(conn, fullIndexName));
        // Simulate write failure
        TestUtil.addCoprocessor(conn, fullIndexName, WriteFailingRegionObserver.class);
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('dddd','dddd','3333')");
        try {
            conn.commit();
            fail();
        } catch (CommitException e) {
        // Expected
        }
        assertTrue(TestUtil.checkIndexState(conn, fullIndexName, indexStateOnFailure, null));
        PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
        ResultSet rs = stmt.executeQuery("SELECT V2 FROM " + fullTableName + " WHERE V1 = 'a'");
        assertTrue(rs.next());
        assertEquals("0", rs.getString(1));
        assertEquals(indexStateOnFailure == PIndexState.DISABLE ? fullTableName : fullIndexName, stmt.getQueryPlan().getContext().getCurrentTable().getTable().getName().getString());
        TestUtil.removeCoprocessor(conn, fullIndexName, WriteFailingRegionObserver.class);
        clock.time += 1000;
        waitForIndexState(conn, fullTableName, fullIndexName, indexStateOnFailure == PIndexState.DISABLE ? PIndexState.INACTIVE : PIndexState.ACTIVE);
        clock.time += WAIT_AFTER_DISABLED;
        // First batch should have been processed again because we started over
        runIndexRebuilder(fullTableName);
        assertEquals(3, TestUtil.getRowCount(conn, fullIndexName));
        clock.time += 2 * REBUILD_PERIOD;
        // Second batch should have been processed now
        runIndexRebuilder(fullTableName);
        clock.time += 2 * REBUILD_PERIOD;
        runIndexRebuilder(fullTableName);
        TestUtil.assertIndexState(conn, fullIndexName, PIndexState.ACTIVE, 0L);
        // Verify that other batches were processed
        IndexScrutiny.scrutinizeIndex(conn, fullTableName, fullIndexName);
    } finally {
        EnvironmentEdgeManager.injectEdge(null);
    }
}
Also used : CommitException(org.apache.phoenix.execute.CommitException) PMetaData(org.apache.phoenix.schema.PMetaData) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) PTableKey(org.apache.phoenix.schema.PTableKey) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement)

Example 77 with PTableKey

use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.

the class PartialIndexRebuilderIT method testUpperBoundSetOnRebuild.

@Test
public void testUpperBoundSetOnRebuild() throws Throwable {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    String indexName = generateUniqueName();
    final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    final String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
    PTableKey key = new PTableKey(null, fullTableName);
    final MyClock clock = new MyClock(1000);
    EnvironmentEdgeManager.injectEdge(clock);
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PMetaData metaCache = conn.unwrap(PhoenixConnection.class).getMetaDataCache();
        conn.createStatement().execute("CREATE TABLE " + fullTableName + "(k VARCHAR PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true");
        clock.time += 100;
        conn.createStatement().execute("CREATE INDEX " + indexName + " ON " + fullTableName + " (v1, v2)");
        clock.time += 100;
        HTableInterface metaTable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        IndexUtil.updateIndexState(fullIndexName, 0L, metaTable, PIndexState.DISABLE);
        clock.time += 100;
        long disableTime = clock.currentTime();
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('a','a', '0')");
        conn.commit();
        // Set clock forward in time past the "overlap" amount we wait for index maintenance to kick in
        clock.time += 2 * WAIT_AFTER_DISABLED;
        assertTrue(hasDisabledIndex(metaCache, key));
        assertEquals(1, TestUtil.getRowCount(conn, fullTableName));
        assertEquals(0, TestUtil.getRowCount(conn, fullIndexName));
        conn.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES('bb','bb','11')");
        conn.commit();
        assertEquals(2, TestUtil.getRowCount(conn, fullTableName));
        assertEquals(0, TestUtil.getRowCount(conn, fullIndexName));
        // Set clock back in time and start rebuild
        clock.time = disableTime + 100;
        IndexUtil.updateIndexState(fullIndexName, disableTime, metaTable, PIndexState.DISABLE);
        waitForIndexState(conn, fullTableName, fullIndexName, PIndexState.INACTIVE);
        clock.time += WAIT_AFTER_DISABLED;
        runIndexRebuilder(fullTableName);
        assertTrue(TestUtil.checkIndexState(conn, fullIndexName, PIndexState.ACTIVE, 0L));
        assertEquals(2, TestUtil.getRowCount(conn, fullTableName));
        // If an upper bound was set on the rebuilder, we should only have found one row
        assertEquals(1, TestUtil.getRowCount(conn, fullIndexName));
    } finally {
        EnvironmentEdgeManager.injectEdge(null);
    }
}
Also used : PMetaData(org.apache.phoenix.schema.PMetaData) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Test(org.junit.Test)

Example 78 with PTableKey

use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.

the class IndexMetadataIT method asssertIsWALDisabled.

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

Example 79 with PTableKey

use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.

the class BaseIndexIT method assertNoIndexDeletes.

private void assertNoIndexDeletes(Connection conn, long minTimestamp, String fullIndexName) throws IOException, SQLException {
    if (!this.mutable) {
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable index = pconn.getTable(new PTableKey(null, fullIndexName));
        byte[] physicalIndexTable = index.getPhysicalName().getBytes();
        try (HTableInterface hIndex = pconn.getQueryServices().getTable(physicalIndexTable)) {
            Scan scan = new Scan();
            scan.setRaw(true);
            if (this.transactional) {
                minTimestamp = TransactionUtil.convertToNanoseconds(minTimestamp);
            }
            scan.setTimeRange(minTimestamp, HConstants.LATEST_TIMESTAMP);
            ResultScanner scanner = hIndex.getScanner(scan);
            Result result;
            while ((result = scanner.next()) != null) {
                CellScanner cellScanner = result.cellScanner();
                while (cellScanner.advance()) {
                    Cell current = cellScanner.current();
                    assertEquals(KeyValue.Type.Put.getCode(), current.getTypeByte());
                }
            }
        }
        ;
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Scan(org.apache.hadoop.hbase.client.Scan) PTableKey(org.apache.phoenix.schema.PTableKey) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) CellScanner(org.apache.hadoop.hbase.CellScanner) Cell(org.apache.hadoop.hbase.Cell) PTable(org.apache.phoenix.schema.PTable) Result(org.apache.hadoop.hbase.client.Result)

Example 80 with PTableKey

use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.

the class LocalIndexIT method testLocalIndexRoundTrip.

@Test
public void testLocalIndexRoundTrip() throws Exception {
    String tableName = schemaName + "." + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    String indexTableName = schemaName + "." + indexName;
    createBaseTable(tableName, null, null);
    Connection conn1 = DriverManager.getConnection(getUrl());
    conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " + tableName + "(v1)");
    conn1.createStatement().executeQuery("SELECT * FROM " + tableName).next();
    PTable localIndex = conn1.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
    assertEquals(IndexType.LOCAL, localIndex.getIndexType());
    assertNotNull(localIndex.getViewIndexId());
    String tableName2 = "test_table" + generateUniqueName();
    String indexName2 = "idx_test_table" + generateUniqueName();
    String createTable = "CREATE TABLE IF NOT EXISTS " + tableName2 + " (user_time UNSIGNED_TIMESTAMP NOT NULL,user_id varchar NOT NULL,col1 varchar,col2 double," + "CONSTRAINT pk PRIMARY KEY(user_time,user_id)) SALT_BUCKETS = 20";
    conn1.createStatement().execute(createTable);
    conn1.createStatement().execute("CREATE local INDEX IF NOT EXISTS " + indexName2 + " on " + tableName2 + "(\"HOUR\"(user_time))");
    conn1.createStatement().execute("upsert into " + tableName2 + " values(TO_TIME('2005-10-01 14:03:22.559'), 'foo')");
    conn1.commit();
    ResultSet rs = conn1.createStatement().executeQuery("select substr(to_char(user_time), 0, 10) as ddate, \"HOUR\"(user_time) as hhour, user_id, col1,col2 from " + tableName2 + " where \"HOUR\"(user_time)=14 group by user_id, col1, col2, ddate, hhour limit 1");
    assertTrue(rs.next());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Aggregations

PTableKey (org.apache.phoenix.schema.PTableKey)89 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)77 PTable (org.apache.phoenix.schema.PTable)55 Connection (java.sql.Connection)48 Test (org.junit.Test)40 Properties (java.util.Properties)23 ResultSet (java.sql.ResultSet)14 PColumn (org.apache.phoenix.schema.PColumn)14 PreparedStatement (java.sql.PreparedStatement)13 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)12 SQLException (java.sql.SQLException)11 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)11 PMetaData (org.apache.phoenix.schema.PMetaData)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)9 PName (org.apache.phoenix.schema.PName)9 BaseTest (org.apache.phoenix.query.BaseTest)8 Result (org.apache.hadoop.hbase.client.Result)7 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)7 Scan (org.apache.hadoop.hbase.client.Scan)7 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)6