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);
}
}
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);
}
}
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());
}
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());
}
}
}
;
}
}
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());
}
Aggregations