Search in sources :

Example 16 with ConnectionQueryServices

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());
            }
        }
    }
}
Also used : UpgradeRequiredException(org.apache.phoenix.exception.UpgradeRequiredException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) Test(org.junit.Test)

Example 17 with ConnectionQueryServices

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));
    }
}
Also used : ConnectionQueryServicesImpl(org.apache.phoenix.query.ConnectionQueryServicesImpl) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) UpgradeInProgressException(org.apache.phoenix.exception.UpgradeInProgressException) Test(org.junit.Test)

Example 18 with ConnectionQueryServices

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();
        }
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) Put(org.apache.hadoop.hbase.client.Put)

Example 19 with ConnectionQueryServices

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) {
            }
        }
    }
}
Also used : UpgradeRequiredException(org.apache.phoenix.exception.UpgradeRequiredException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) Test(org.junit.Test)

Example 20 with ConnectionQueryServices

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);
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Hint(org.apache.phoenix.parse.HintNode.Hint) PSmallint(org.apache.phoenix.schema.types.PSmallint) PTable(org.apache.phoenix.schema.PTable) PColumn(org.apache.phoenix.schema.PColumn) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) MutationState(org.apache.phoenix.execute.MutationState) RowMutationState(org.apache.phoenix.execute.MutationState.RowMutationState) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) ResultSet(java.sql.ResultSet) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) PTableKey(org.apache.phoenix.schema.PTableKey) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) RowMutationState(org.apache.phoenix.execute.MutationState.RowMutationState)

Aggregations

ConnectionQueryServices (org.apache.phoenix.query.ConnectionQueryServices)38 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)23 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)12 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)9 PTable (org.apache.phoenix.schema.PTable)9 Test (org.junit.Test)9 ResultSet (java.sql.ResultSet)8 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)8 ArrayList (java.util.ArrayList)7 Properties (java.util.Properties)7 PreparedStatement (java.sql.PreparedStatement)5 Put (org.apache.hadoop.hbase.client.Put)5 Hint (org.apache.phoenix.parse.HintNode.Hint)5 Scan (org.apache.hadoop.hbase.client.Scan)4 MutationState (org.apache.phoenix.execute.MutationState)4 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)4 PhoenixResultSet (org.apache.phoenix.jdbc.PhoenixResultSet)4 DelegateConnectionQueryServices (org.apache.phoenix.query.DelegateConnectionQueryServices)4 PColumn (org.apache.phoenix.schema.PColumn)4