use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.
the class UpgradeIT method testUpgradingConnectionBypassesUpgradeRequiredCheck.
@Test
public void testUpgradingConnectionBypassesUpgradeRequiredCheck() throws Exception {
String tableName = generateUniqueName();
try (Connection conn = getConnection(false, null)) {
conn.createStatement().execute("CREATE TABLE " + tableName + " (PK1 VARCHAR NOT NULL, PK2 VARCHAR, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2))");
final ConnectionQueryServices delegate = conn.unwrap(PhoenixConnection.class).getQueryServices();
ConnectionQueryServices servicesWithUpgrade = new DelegateConnectionQueryServices(delegate) {
@Override
public boolean isUpgradeRequired() {
return true;
}
};
try (PhoenixConnection phxConn = new PhoenixConnection(servicesWithUpgrade, conn.unwrap(PhoenixConnection.class), HConstants.LATEST_TIMESTAMP)) {
// Because upgrade is required, this SQL should fail.
try {
phxConn.createStatement().executeQuery("SELECT * FROM " + tableName);
fail("SELECT should have failed with UpgradeRequiredException");
} catch (UpgradeRequiredException expected) {
}
// Marking connection as the one running upgrade should let SQL execute fine.
phxConn.setRunningUpgrade(true);
phxConn.createStatement().execute("UPSERT INTO " + tableName + " VALUES ('PK1', 'PK2', 'KV1', 'KV2')");
phxConn.commit();
try (ResultSet rs = phxConn.createStatement().executeQuery("SELECT * FROM " + tableName)) {
assertTrue(rs.next());
assertFalse(rs.next());
}
}
}
}
use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.
the class UpgradeIT method testAcquiringAndReleasingUpgradeMutex.
@Test
public void testAcquiringAndReleasingUpgradeMutex() throws Exception {
ConnectionQueryServices services = null;
byte[] mutexRowKey = SchemaUtil.getTableKey(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA, generateUniqueName());
try (Connection conn = getConnection(false, null)) {
services = conn.unwrap(PhoenixConnection.class).getQueryServices();
putUnlockKVInSysMutex(mutexRowKey);
assertTrue(((ConnectionQueryServicesImpl) services).acquireUpgradeMutex(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0, mutexRowKey));
try {
((ConnectionQueryServicesImpl) services).acquireUpgradeMutex(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0, mutexRowKey);
fail();
} catch (UpgradeInProgressException expected) {
}
assertTrue(((ConnectionQueryServicesImpl) services).releaseUpgradeMutex(mutexRowKey));
assertFalse(((ConnectionQueryServicesImpl) services).releaseUpgradeMutex(mutexRowKey));
}
}
use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.
the class UpgradeIT method putUnlockKVInSysMutex.
private void putUnlockKVInSysMutex(byte[] row) throws Exception {
try (Connection conn = getConnection(false, null)) {
ConnectionQueryServices services = conn.unwrap(PhoenixConnection.class).getQueryServices();
try (HTableInterface sysMutexTable = services.getTable(PhoenixDatabaseMetaData.SYSTEM_MUTEX_NAME_BYTES)) {
byte[] family = PhoenixDatabaseMetaData.SYSTEM_MUTEX_FAMILY_NAME_BYTES;
byte[] qualifier = UPGRADE_MUTEX;
Put put = new Put(row);
put.add(family, qualifier, UPGRADE_MUTEX_UNLOCKED);
sysMutexTable.put(put);
sysMutexTable.flushCommits();
}
}
}
use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.
the class UpgradeIT method testUpgradeRequiredPreventsSQL.
@Test
public void testUpgradeRequiredPreventsSQL() throws SQLException {
String tableName = generateUniqueName();
try (Connection conn = getConnection(false, null)) {
conn.createStatement().execute("CREATE TABLE " + tableName + " (PK1 VARCHAR NOT NULL, PK2 VARCHAR, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2))");
final ConnectionQueryServices delegate = conn.unwrap(PhoenixConnection.class).getQueryServices();
ConnectionQueryServices servicesWithUpgrade = new DelegateConnectionQueryServices(delegate) {
@Override
public boolean isUpgradeRequired() {
return true;
}
};
try (PhoenixConnection phxConn = new PhoenixConnection(servicesWithUpgrade, conn.unwrap(PhoenixConnection.class), HConstants.LATEST_TIMESTAMP)) {
try {
phxConn.createStatement().execute("CREATE TABLE " + generateUniqueName() + " (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2))");
fail("CREATE TABLE should have failed with UpgradeRequiredException");
} catch (UpgradeRequiredException expected) {
}
try {
phxConn.createStatement().execute("SELECT * FROM " + tableName);
fail("SELECT should have failed with UpgradeRequiredException");
} catch (UpgradeRequiredException expected) {
}
try {
phxConn.createStatement().execute("DELETE FROM " + tableName);
fail("DELETE should have failed with UpgradeRequiredException");
} catch (UpgradeRequiredException expected) {
}
try {
phxConn.createStatement().execute("CREATE INDEX " + tableName + "_IDX ON " + tableName + " (KV1) INCLUDE (KV2)");
fail("CREATE INDEX should have failed with UpgradeRequiredException");
} catch (UpgradeRequiredException expected) {
}
try {
phxConn.createStatement().execute("UPSERT INTO " + tableName + " VALUES ('PK1', 'PK2', 'KV1', 'KV2')");
fail("UPSERT VALUES should have failed with UpgradeRequiredException");
} catch (UpgradeRequiredException expected) {
}
}
}
}
use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.
the class UpsertCompiler method upsertSelect.
public static MutationState upsertSelect(StatementContext childContext, TableRef tableRef, RowProjector projector, ResultIterator iterator, int[] columnIndexes, int[] pkSlotIndexes, boolean useServerTimestamp, boolean prefixSysColValues) throws SQLException {
PhoenixStatement statement = childContext.getStatement();
PhoenixConnection connection = statement.getConnection();
ConnectionQueryServices services = connection.getQueryServices();
int maxSize = services.getProps().getInt(QueryServices.MAX_MUTATION_SIZE_ATTRIB, QueryServicesOptions.DEFAULT_MAX_MUTATION_SIZE);
int maxSizeBytes = services.getProps().getInt(QueryServices.MAX_MUTATION_SIZE_BYTES_ATTRIB, QueryServicesOptions.DEFAULT_MAX_MUTATION_SIZE_BYTES);
int batchSize = Math.min(connection.getMutateBatchSize(), maxSize);
boolean isAutoCommit = connection.getAutoCommit();
int numSplColumns = (tableRef.getTable().isMultiTenant() ? 1 : 0) + (tableRef.getTable().getViewIndexId() != null ? 1 : 0);
byte[][] values = new byte[columnIndexes.length + numSplColumns][];
if (prefixSysColValues) {
int i = 0;
if (tableRef.getTable().isMultiTenant()) {
values[i++] = connection.getTenantId().getBytes();
}
if (tableRef.getTable().getViewIndexId() != null) {
values[i++] = PSmallint.INSTANCE.toBytes(tableRef.getTable().getViewIndexId());
}
}
int rowCount = 0;
Map<ImmutableBytesPtr, RowMutationState> mutation = Maps.newHashMapWithExpectedSize(batchSize);
PTable table = tableRef.getTable();
IndexMaintainer indexMaintainer = null;
byte[][] viewConstants = null;
if (table.getIndexType() == IndexType.LOCAL) {
PTable parentTable = statement.getConnection().getMetaDataCache().getTableRef(new PTableKey(statement.getConnection().getTenantId(), table.getParentName().getString())).getTable();
indexMaintainer = table.getIndexMaintainer(parentTable, connection);
viewConstants = IndexUtil.getViewConstants(parentTable);
}
try (ResultSet rs = new PhoenixResultSet(iterator, projector, childContext)) {
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
while (rs.next()) {
for (int i = 0, j = numSplColumns; j < values.length; j++, i++) {
PColumn column = table.getColumns().get(columnIndexes[i]);
byte[] bytes = rs.getBytes(i + 1);
ptr.set(bytes == null ? ByteUtil.EMPTY_BYTE_ARRAY : bytes);
Object value = rs.getObject(i + 1);
int rsPrecision = rs.getMetaData().getPrecision(i + 1);
Integer precision = rsPrecision == 0 ? null : rsPrecision;
int rsScale = rs.getMetaData().getScale(i + 1);
Integer scale = rsScale == 0 ? null : rsScale;
// as we checked that before.
if (!column.getDataType().isSizeCompatible(ptr, value, column.getDataType(), SortOrder.getDefault(), precision, scale, column.getMaxLength(), column.getScale())) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_EXCEEDS_MAX_CAPACITY).setColumnName(column.getName().getString()).setMessage("value=" + column.getDataType().toStringLiteral(ptr, null)).build().buildException();
}
column.getDataType().coerceBytes(ptr, value, column.getDataType(), precision, scale, SortOrder.getDefault(), column.getMaxLength(), column.getScale(), column.getSortOrder(), table.rowKeyOrderOptimizable());
values[j] = ByteUtil.copyKeyBytesIfNecessary(ptr);
}
setValues(values, pkSlotIndexes, columnIndexes, table, mutation, statement, useServerTimestamp, indexMaintainer, viewConstants, null, numSplColumns);
rowCount++;
// Commit a batch if auto commit is true and we're at our batch size
if (isAutoCommit && rowCount % batchSize == 0) {
MutationState state = new MutationState(tableRef, mutation, 0, maxSize, maxSizeBytes, connection);
connection.getMutationState().join(state);
connection.getMutationState().send();
mutation.clear();
}
}
// If auto commit is true, this last batch will be committed upon return
return new MutationState(tableRef, mutation, rowCount / batchSize * batchSize, maxSize, maxSizeBytes, connection);
}
}
Aggregations