Search in sources :

Example 1 with TableAlreadyExistsException

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

the class BaseTest method createTestTable.

protected static void createTestTable(String url, String ddl, byte[][] splits, Long ts, boolean swallowTableAlreadyExistsException) throws SQLException {
    assertNotNull(ddl);
    StringBuilder buf = new StringBuilder(ddl);
    if (splits != null) {
        buf.append(" SPLIT ON (");
        for (int i = 0; i < splits.length; i++) {
            buf.append("'").append(Bytes.toString(splits[i])).append("'").append(",");
        }
        buf.setCharAt(buf.length() - 1, ')');
    }
    ddl = buf.toString();
    Properties props = new Properties();
    if (ts != null) {
        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts));
    }
    Connection conn = DriverManager.getConnection(url, props);
    try {
        conn.createStatement().execute(ddl);
    } catch (TableAlreadyExistsException e) {
        if (!swallowTableAlreadyExistsException) {
            throw e;
        }
    } finally {
        conn.close();
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) Properties(java.util.Properties)

Example 2 with TableAlreadyExistsException

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

the class ConnectionQueryServicesImpl method init.

@Override
public void init(final String url, final Properties props) throws SQLException {
    try {
        PhoenixContextExecutor.call(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                if (isInitialized()) {
                    if (initializationException != null) {
                        // Throw previous initialization exception, as we won't resuse this instance
                        throw initializationException;
                    }
                    return null;
                }
                synchronized (ConnectionQueryServicesImpl.this) {
                    if (isInitialized()) {
                        if (initializationException != null) {
                            // Throw previous initialization exception, as we won't resuse this instance
                            throw initializationException;
                        }
                        return null;
                    }
                    checkClosed();
                    boolean hConnectionEstablished = false;
                    boolean success = false;
                    try {
                        GLOBAL_QUERY_SERVICES_COUNTER.increment();
                        logger.info("An instance of ConnectionQueryServices was created.");
                        openConnection();
                        hConnectionEstablished = true;
                        boolean isDoNotUpgradePropSet = UpgradeUtil.isNoUpgradeSet(props);
                        try (HBaseAdmin admin = getAdmin()) {
                            boolean mappedSystemCatalogExists = admin.tableExists(SchemaUtil.getPhysicalTableName(SYSTEM_CATALOG_NAME_BYTES, true));
                            if (SchemaUtil.isNamespaceMappingEnabled(PTableType.SYSTEM, ConnectionQueryServicesImpl.this.getProps())) {
                                if (admin.tableExists(SYSTEM_CATALOG_NAME_BYTES)) {
                                    // check if the server is already updated and have namespace config properly set.
                                    checkClientServerCompatibility(SYSTEM_CATALOG_NAME_BYTES);
                                }
                                // If SYSTEM tables exist, they are migrated to HBase SYSTEM namespace
                                // If they don't exist, this method will create HBase SYSTEM namespace and return
                                ensureSystemTablesMigratedToSystemNamespace(ConnectionQueryServicesImpl.this.getProps());
                            } else if (mappedSystemCatalogExists) {
                                throw new SQLExceptionInfo.Builder(SQLExceptionCode.INCONSISTENT_NAMESPACE_MAPPING_PROPERTIES).setMessage("Cannot initiate connection as " + SchemaUtil.getPhysicalTableName(SYSTEM_CATALOG_NAME_BYTES, true) + " is found but client does not have " + IS_NAMESPACE_MAPPING_ENABLED + " enabled").build().buildException();
                            }
                        }
                        Properties scnProps = PropertiesUtil.deepCopy(props);
                        scnProps.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(getSystemTableVersion()));
                        scnProps.remove(PhoenixRuntime.TENANT_ID_ATTRIB);
                        String globalUrl = JDBCUtil.removeProperty(url, PhoenixRuntime.TENANT_ID_ATTRIB);
                        try (HBaseAdmin hBaseAdmin = getAdmin();
                            PhoenixConnection metaConnection = new PhoenixConnection(ConnectionQueryServicesImpl.this, globalUrl, scnProps, newEmptyMetaData())) {
                            try {
                                metaConnection.setRunningUpgrade(true);
                                metaConnection.createStatement().executeUpdate(getSystemCatalogDML());
                            } catch (NewerTableAlreadyExistsException ignore) {
                            // Ignore, as this will happen if the SYSTEM.CATALOG already exists at this fixed
                            // timestamp. A TableAlreadyExistsException is not thrown, since the table only exists
                            // *after* this fixed timestamp.
                            } catch (TableAlreadyExistsException e) {
                                long currentServerSideTableTimeStamp = e.getTable().getTimeStamp();
                                if (currentServerSideTableTimeStamp < MIN_SYSTEM_TABLE_TIMESTAMP) {
                                    setUpgradeRequired();
                                }
                            } catch (PhoenixIOException e) {
                                boolean foundAccessDeniedException = false;
                                // in a RemoteException
                                for (Throwable t : Throwables.getCausalChain(e)) {
                                    if (t instanceof AccessDeniedException || (t instanceof RemoteException && ((RemoteException) t).getClassName().equals(AccessDeniedException.class.getName()))) {
                                        foundAccessDeniedException = true;
                                        break;
                                    }
                                }
                                if (foundAccessDeniedException) {
                                    // Pass
                                    logger.warn("Could not check for Phoenix SYSTEM tables, assuming they exist and are properly configured");
                                    checkClientServerCompatibility(SchemaUtil.getPhysicalName(SYSTEM_CATALOG_NAME_BYTES, getProps()).getName());
                                    success = true;
                                } else if (!Iterables.isEmpty(Iterables.filter(Throwables.getCausalChain(e), NamespaceNotFoundException.class))) {
                                    // This exception is only possible if SYSTEM namespace mapping is enabled and SYSTEM namespace is missing
                                    // It implies that SYSTEM tables are not created and hence we shouldn't provide a connection
                                    AccessDeniedException ade = new AccessDeniedException("Insufficient permissions to create SYSTEM namespace and SYSTEM Tables");
                                    initializationException = ServerUtil.parseServerException(ade);
                                } else {
                                    initializationException = e;
                                }
                                return null;
                            }
                            // with SYSTEM Namespace. (See PHOENIX-4227 https://issues.apache.org/jira/browse/PHOENIX-4227)
                            if (SchemaUtil.isNamespaceMappingEnabled(PTableType.SYSTEM, ConnectionQueryServicesImpl.this.getProps())) {
                                try {
                                    metaConnection.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA);
                                } catch (NewerSchemaAlreadyExistsException e) {
                                // Older clients with appropriate perms may try getting a new connection
                                // This results in NewerSchemaAlreadyExistsException, so we can safely ignore it here
                                } catch (PhoenixIOException e) {
                                    if (!Iterables.isEmpty(Iterables.filter(Throwables.getCausalChain(e), AccessDeniedException.class))) {
                                    // Ignore ADE
                                    } else {
                                        throw e;
                                    }
                                }
                            }
                            if (!ConnectionQueryServicesImpl.this.upgradeRequired.get()) {
                                createOtherSystemTables(metaConnection, hBaseAdmin);
                            } else if (isAutoUpgradeEnabled && !isDoNotUpgradePropSet) {
                                upgradeSystemTables(url, props);
                            }
                        }
                        scheduleRenewLeaseTasks();
                        success = true;
                    } catch (RetriableUpgradeException e) {
                        // to retry establishing connection.
                        throw e;
                    } catch (Exception e) {
                        if (e instanceof SQLException) {
                            initializationException = (SQLException) e;
                        } else {
                            // wrap every other exception into a SQLException
                            initializationException = new SQLException(e);
                        }
                    } finally {
                        try {
                            if (!success && hConnectionEstablished) {
                                connection.close();
                            }
                        } catch (IOException e) {
                            SQLException ex = new SQLException(e);
                            if (initializationException != null) {
                                initializationException.setNextException(ex);
                            } else {
                                initializationException = ex;
                            }
                        } finally {
                            try {
                                if (initializationException != null) {
                                    throw initializationException;
                                }
                            } finally {
                                setInitialized(true);
                            }
                        }
                    }
                }
                return null;
            }
        });
    } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, SQLException.class);
        Throwables.propagate(e);
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) AccessDeniedException(org.apache.hadoop.hbase.security.AccessDeniedException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) IOException(java.io.IOException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) Properties(java.util.Properties) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) UpgradeInProgressException(org.apache.phoenix.exception.UpgradeInProgressException) AccessDeniedException(org.apache.hadoop.hbase.security.AccessDeniedException) RetriableUpgradeException(org.apache.phoenix.exception.RetriableUpgradeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UpgradeNotRequiredException(org.apache.phoenix.exception.UpgradeNotRequiredException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) NewerSchemaAlreadyExistsException(org.apache.phoenix.schema.NewerSchemaAlreadyExistsException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) TableExistsException(org.apache.hadoop.hbase.TableExistsException) RemoteException(org.apache.hadoop.ipc.RemoteException) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) SQLException(java.sql.SQLException) ColumnAlreadyExistsException(org.apache.phoenix.schema.ColumnAlreadyExistsException) TimeoutException(java.util.concurrent.TimeoutException) FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) NamespaceNotFoundException(org.apache.hadoop.hbase.NamespaceNotFoundException) ReadOnlyTableException(org.apache.phoenix.schema.ReadOnlyTableException) EmptySequenceCacheException(org.apache.phoenix.schema.EmptySequenceCacheException) NamespaceNotFoundException(org.apache.hadoop.hbase.NamespaceNotFoundException) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) NewerSchemaAlreadyExistsException(org.apache.phoenix.schema.NewerSchemaAlreadyExistsException) RemoteException(org.apache.hadoop.ipc.RemoteException) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) RetriableUpgradeException(org.apache.phoenix.exception.RetriableUpgradeException)

Example 3 with TableAlreadyExistsException

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

the class ConnectionQueryServicesImpl method upgradeSystemTables.

/**
 * There is no other locking needed here since only one connection (on the same or different JVM) will be able to
 * acquire the upgrade mutex via {@link #acquireUpgradeMutex(long, byte[])}.
 */
@Override
public void upgradeSystemTables(final String url, final Properties props) throws SQLException {
    PhoenixConnection metaConnection = null;
    boolean success = false;
    String snapshotName = null;
    String sysCatalogTableName = null;
    SQLException toThrow = null;
    boolean acquiredMutexLock = false;
    byte[] mutexRowKey = SchemaUtil.getTableKey(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA, PhoenixDatabaseMetaData.SYSTEM_CATALOG_TABLE);
    boolean snapshotCreated = false;
    try {
        if (!isUpgradeRequired()) {
            throw new UpgradeNotRequiredException();
        }
        Properties scnProps = PropertiesUtil.deepCopy(props);
        scnProps.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP));
        scnProps.remove(PhoenixRuntime.TENANT_ID_ATTRIB);
        String globalUrl = JDBCUtil.removeProperty(url, PhoenixRuntime.TENANT_ID_ATTRIB);
        metaConnection = new PhoenixConnection(ConnectionQueryServicesImpl.this, globalUrl, scnProps, newEmptyMetaData());
        metaConnection.setRunningUpgrade(true);
        try {
            metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_TABLE_METADATA);
        } catch (NewerTableAlreadyExistsException ignore) {
        // Ignore, as this will happen if the SYSTEM.CATALOG already exists at this fixed
        // timestamp. A TableAlreadyExistsException is not thrown, since the table only exists
        // *after* this fixed timestamp.
        } catch (TableAlreadyExistsException e) {
            long currentServerSideTableTimeStamp = e.getTable().getTimeStamp();
            sysCatalogTableName = e.getTable().getPhysicalName().getString();
            if (currentServerSideTableTimeStamp < MIN_SYSTEM_TABLE_TIMESTAMP) {
                // upgrade, so no need for a bunch of wasted RPCs.
                if (currentServerSideTableTimeStamp <= MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0 && !SchemaUtil.isNamespaceMappingEnabled(PTableType.SYSTEM, ConnectionQueryServicesImpl.this.getProps())) {
                    try (HBaseAdmin admin = getAdmin()) {
                        createSysMutexTableIfNotExists(admin, this.getProps());
                    }
                }
                if (acquiredMutexLock = acquireUpgradeMutex(currentServerSideTableTimeStamp, mutexRowKey)) {
                    snapshotName = getSysCatalogSnapshotName(currentServerSideTableTimeStamp);
                    createSnapshot(snapshotName, sysCatalogTableName);
                    snapshotCreated = true;
                }
            }
            String columnsToAdd = "";
            // include any new columns we've added.
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_3_0) {
                // We know that we always need to add the STORE_NULLS column for 4.3 release
                columnsToAdd = addColumn(columnsToAdd, PhoenixDatabaseMetaData.STORE_NULLS + " " + PBoolean.INSTANCE.getSqlTypeName());
                try (HBaseAdmin admin = getAdmin()) {
                    HTableDescriptor[] localIndexTables = admin.listTables(MetaDataUtil.LOCAL_INDEX_TABLE_PREFIX + ".*");
                    for (HTableDescriptor table : localIndexTables) {
                        if (table.getValue(MetaDataUtil.PARENT_TABLE_KEY) == null && table.getValue(MetaDataUtil.IS_LOCAL_INDEX_TABLE_PROP_NAME) != null) {
                            table.setValue(MetaDataUtil.PARENT_TABLE_KEY, MetaDataUtil.getLocalIndexUserTableName(table.getNameAsString()));
                            // Explicitly disable, modify and enable the table to ensure
                            // co-location of data and index regions. If we just modify the
                            // table descriptor when online schema change enabled may reopen
                            // the region in same region server instead of following data region.
                            admin.disableTable(table.getTableName());
                            admin.modifyTable(table.getTableName(), table);
                            admin.enableTable(table.getTableName());
                        }
                    }
                }
            }
            // the column names that have been added to SYSTEM.CATALOG since 4.0.
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_1_0) {
                columnsToAdd = addColumn(columnsToAdd, PhoenixDatabaseMetaData.INDEX_TYPE + " " + PUnsignedTinyint.INSTANCE.getSqlTypeName() + ", " + PhoenixDatabaseMetaData.INDEX_DISABLE_TIMESTAMP + " " + PLong.INSTANCE.getSqlTypeName());
            }
            // If we have some new columns from 4.1-4.3 to add, add them now.
            if (!columnsToAdd.isEmpty()) {
                // Ugh..need to assign to another local variable to keep eclipse happy.
                PhoenixConnection newMetaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_3_0, columnsToAdd);
                metaConnection = newMetaConnection;
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_5_0) {
                columnsToAdd = PhoenixDatabaseMetaData.BASE_COLUMN_COUNT + " " + PInteger.INSTANCE.getSqlTypeName();
                try {
                    metaConnection = addColumn(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_5_0, columnsToAdd, false);
                    upgradeTo4_5_0(metaConnection);
                } catch (ColumnAlreadyExistsException ignored) {
                    /*
                         * Upgrade to 4.5 is a slightly special case. We use the fact that the
                         * column BASE_COLUMN_COUNT is already part of the meta-data schema as the
                         * signal that the server side upgrade has finished or is in progress.
                         */
                    logger.debug("No need to run 4.5 upgrade");
                }
                Properties p = PropertiesUtil.deepCopy(metaConnection.getClientInfo());
                p.remove(PhoenixRuntime.CURRENT_SCN_ATTRIB);
                p.remove(PhoenixRuntime.TENANT_ID_ATTRIB);
                PhoenixConnection conn = new PhoenixConnection(ConnectionQueryServicesImpl.this, metaConnection.getURL(), p, metaConnection.getMetaDataCache());
                try {
                    List<String> tablesNeedingUpgrade = UpgradeUtil.getPhysicalTablesWithDescRowKey(conn);
                    if (!tablesNeedingUpgrade.isEmpty()) {
                        logger.warn("The following tables require upgrade due to a bug causing the row key to be incorrect for descending columns and ascending BINARY columns (PHOENIX-2067 and PHOENIX-2120):\n" + Joiner.on(' ').join(tablesNeedingUpgrade) + "\nTo upgrade issue the \"bin/psql.py -u\" command.");
                    }
                    List<String> unsupportedTables = UpgradeUtil.getPhysicalTablesWithDescVarbinaryRowKey(conn);
                    if (!unsupportedTables.isEmpty()) {
                        logger.warn("The following tables use an unsupported VARBINARY DESC construct and need to be changed:\n" + Joiner.on(' ').join(unsupportedTables));
                    }
                } catch (Exception ex) {
                    logger.error("Unable to determine tables requiring upgrade due to PHOENIX-2067", ex);
                } finally {
                    conn.close();
                }
            }
            // parts we haven't yet done).
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_6_0) {
                columnsToAdd = PhoenixDatabaseMetaData.IS_ROW_TIMESTAMP + " " + PBoolean.INSTANCE.getSqlTypeName();
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_6_0, columnsToAdd);
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0) {
                // Drop old stats table so that new stats table is created
                metaConnection = dropStatsTable(metaConnection, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0 - 4);
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0 - 3, PhoenixDatabaseMetaData.TRANSACTIONAL + " " + PBoolean.INSTANCE.getSqlTypeName());
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0 - 2, PhoenixDatabaseMetaData.UPDATE_CACHE_FREQUENCY + " " + PLong.INSTANCE.getSqlTypeName());
                metaConnection = setImmutableTableIndexesImmutable(metaConnection, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0 - 1);
                metaConnection = updateSystemCatalogTimestamp(metaConnection, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0);
                ConnectionQueryServicesImpl.this.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_7_0);
                clearCache();
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_8_0) {
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_8_0 - 2, PhoenixDatabaseMetaData.IS_NAMESPACE_MAPPED + " " + PBoolean.INSTANCE.getSqlTypeName());
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_8_0 - 1, PhoenixDatabaseMetaData.AUTO_PARTITION_SEQ + " " + PVarchar.INSTANCE.getSqlTypeName());
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_8_0, PhoenixDatabaseMetaData.APPEND_ONLY_SCHEMA + " " + PBoolean.INSTANCE.getSqlTypeName());
                metaConnection = UpgradeUtil.disableViewIndexes(metaConnection);
                if (getProps().getBoolean(QueryServices.LOCAL_INDEX_CLIENT_UPGRADE_ATTRIB, QueryServicesOptions.DEFAULT_LOCAL_INDEX_CLIENT_UPGRADE)) {
                    metaConnection = UpgradeUtil.upgradeLocalIndexes(metaConnection);
                }
                ConnectionQueryServicesImpl.this.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_8_0);
                clearCache();
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_9_0) {
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_9_0, PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH + " " + PLong.INSTANCE.getSqlTypeName());
                ConnectionQueryServicesImpl.this.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_9_0);
                clearCache();
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0) {
                metaConnection = addColumnQualifierColumn(metaConnection, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0 - 3);
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0 - 2, PhoenixDatabaseMetaData.IMMUTABLE_STORAGE_SCHEME + " " + PTinyint.INSTANCE.getSqlTypeName());
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0 - 1, PhoenixDatabaseMetaData.ENCODING_SCHEME + " " + PTinyint.INSTANCE.getSqlTypeName());
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0, PhoenixDatabaseMetaData.COLUMN_QUALIFIER_COUNTER + " " + PInteger.INSTANCE.getSqlTypeName());
                ConnectionQueryServicesImpl.this.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_10_0);
                clearCache();
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_11_0) {
                metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_11_0, PhoenixDatabaseMetaData.USE_STATS_FOR_PARALLELIZATION + " " + PBoolean.INSTANCE.getSqlTypeName());
                addParentToChildLinks(metaConnection);
            }
        }
        int nSaltBuckets = ConnectionQueryServicesImpl.this.props.getInt(QueryServices.SEQUENCE_SALT_BUCKETS_ATTRIB, QueryServicesOptions.DEFAULT_SEQUENCE_TABLE_SALT_BUCKETS);
        try {
            String createSequenceTable = Sequence.getCreateTableStatement(nSaltBuckets);
            metaConnection.createStatement().executeUpdate(createSequenceTable);
            nSequenceSaltBuckets = nSaltBuckets;
        } catch (NewerTableAlreadyExistsException e) {
            // Ignore, as this will happen if the SYSTEM.SEQUENCE already exists at this fixed
            // timestamp.
            // A TableAlreadyExistsException is not thrown, since the table only exists *after* this
            // fixed timestamp.
            nSequenceSaltBuckets = getSaltBuckets(e);
        } catch (TableAlreadyExistsException e) {
            // This will occur if we have an older SYSTEM.SEQUENCE and we need to update it to
            // include
            // any new columns we've added.
            long currentServerSideTableTimeStamp = e.getTable().getTimeStamp();
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_1_0) {
                // If the table time stamp is before 4.1.0 then we need to add below columns
                // to the SYSTEM.SEQUENCE table.
                String columnsToAdd = PhoenixDatabaseMetaData.MIN_VALUE + " " + PLong.INSTANCE.getSqlTypeName() + ", " + PhoenixDatabaseMetaData.MAX_VALUE + " " + PLong.INSTANCE.getSqlTypeName() + ", " + PhoenixDatabaseMetaData.CYCLE_FLAG + " " + PBoolean.INSTANCE.getSqlTypeName() + ", " + PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG + " " + PBoolean.INSTANCE.getSqlTypeName();
                addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP, columnsToAdd);
            }
            // If the table timestamp is before 4.2.1 then run the upgrade script
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_2_1) {
                if (UpgradeUtil.upgradeSequenceTable(metaConnection, nSaltBuckets, e.getTable())) {
                    metaConnection.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_SCHEMA, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_TABLE, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP);
                    clearTableFromCache(ByteUtil.EMPTY_BYTE_ARRAY, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_SCHEMA_BYTES, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_TABLE_BYTES, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP);
                    clearTableRegionCache(PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_NAME_BYTES);
                }
                nSequenceSaltBuckets = nSaltBuckets;
            } else {
                nSequenceSaltBuckets = getSaltBuckets(e);
            }
        }
        try {
            metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_STATS_TABLE_METADATA);
        } catch (NewerTableAlreadyExistsException ignore) {
        } catch (TableAlreadyExistsException e) {
            long currentServerSideTableTimeStamp = e.getTable().getTimeStamp();
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_3_0) {
                metaConnection = addColumnsIfNotExists(metaConnection, SYSTEM_STATS_NAME, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP, PhoenixDatabaseMetaData.GUIDE_POSTS_ROW_COUNT + " " + PLong.INSTANCE.getSqlTypeName());
            }
            if (currentServerSideTableTimeStamp < MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_9_0) {
                // The COLUMN_FAMILY column should be nullable as we create a row in it without
                // any column family to mark when guideposts were last collected.
                metaConnection = removeNotNullConstraint(metaConnection, PhoenixDatabaseMetaData.SYSTEM_SCHEMA_NAME, PhoenixDatabaseMetaData.SYSTEM_STATS_TABLE, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_9_0, PhoenixDatabaseMetaData.COLUMN_FAMILY);
                ConnectionQueryServicesImpl.this.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_STATS_NAME, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_9_0);
                clearCache();
            }
        }
        try {
            metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_FUNCTION_METADATA);
        } catch (NewerTableAlreadyExistsException e) {
        } catch (TableAlreadyExistsException e) {
        }
        ConnectionQueryServicesImpl.this.upgradeRequired.set(false);
        success = true;
    } catch (UpgradeInProgressException | UpgradeNotRequiredException e) {
        // don't set it as initializationException because otherwise client won't be able to retry
        throw e;
    } catch (Exception e) {
        if (e instanceof SQLException) {
            toThrow = (SQLException) e;
        } else {
            // wrap every other exception into a SQLException
            toThrow = new SQLException(e);
        }
    } finally {
        try {
            if (metaConnection != null) {
                metaConnection.close();
            }
        } catch (SQLException e) {
            if (toThrow != null) {
                toThrow.setNextException(e);
            } else {
                toThrow = e;
            }
        } finally {
            try {
                if (snapshotCreated) {
                    restoreFromSnapshot(sysCatalogTableName, snapshotName, success);
                }
            } catch (SQLException e) {
                if (toThrow != null) {
                    toThrow.setNextException(e);
                } else {
                    toThrow = e;
                }
            } finally {
                if (acquiredMutexLock) {
                    try {
                        releaseUpgradeMutex(mutexRowKey);
                    } catch (IOException e) {
                        logger.warn("Release of upgrade mutex failed ", e);
                    }
                }
            }
            if (toThrow != null) {
                throw toThrow;
            }
        }
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) UpgradeNotRequiredException(org.apache.phoenix.exception.UpgradeNotRequiredException) SQLException(java.sql.SQLException) ColumnAlreadyExistsException(org.apache.phoenix.schema.ColumnAlreadyExistsException) IOException(java.io.IOException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) Properties(java.util.Properties) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) UpgradeInProgressException(org.apache.phoenix.exception.UpgradeInProgressException) AccessDeniedException(org.apache.hadoop.hbase.security.AccessDeniedException) RetriableUpgradeException(org.apache.phoenix.exception.RetriableUpgradeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UpgradeNotRequiredException(org.apache.phoenix.exception.UpgradeNotRequiredException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) NewerSchemaAlreadyExistsException(org.apache.phoenix.schema.NewerSchemaAlreadyExistsException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) TableExistsException(org.apache.hadoop.hbase.TableExistsException) RemoteException(org.apache.hadoop.ipc.RemoteException) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) SQLException(java.sql.SQLException) ColumnAlreadyExistsException(org.apache.phoenix.schema.ColumnAlreadyExistsException) TimeoutException(java.util.concurrent.TimeoutException) FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) NamespaceNotFoundException(org.apache.hadoop.hbase.NamespaceNotFoundException) ReadOnlyTableException(org.apache.phoenix.schema.ReadOnlyTableException) EmptySequenceCacheException(org.apache.phoenix.schema.EmptySequenceCacheException) PTinyint(org.apache.phoenix.schema.types.PTinyint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) MultiRowMutationEndpoint(org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) UpgradeInProgressException(org.apache.phoenix.exception.UpgradeInProgressException)

Example 4 with TableAlreadyExistsException

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

the class TenantIdTypeIT method testMultiTenantTables.

@Test
public void testMultiTenantTables() throws Exception {
    // Verify we can create the table
    try (Connection conn = regularConnection(getUrl())) {
        conn.setAutoCommit(true);
        conn.createStatement().execute(ddl);
        try {
            conn.createStatement().execute(ddl);
            fail("Table with " + dataType + " tenantId not created correctly");
        } catch (TableAlreadyExistsException e) {
        // expected
        }
    }
    // Insert test data
    try (Connection conn = regularConnection(getUrl())) {
        conn.setAutoCommit(true);
        String query = "upsert into " + table + " values (" + tenantId + ", 1 , 'valid')";
        conn.createStatement().execute("upsert into " + table + " values (" + tenantId + ", 1 , 'valid')");
        conn.createStatement().execute("upsert into " + table + " values (" + otherTenantId + ", 2 , 'invalid')");
    }
    // Make sure access is properly restricted and add some tenant-specific schema
    try (Connection conn = tenantConnection(getUrl())) {
        conn.setAutoCommit(true);
        ResultSet rs = conn.createStatement().executeQuery("select * from " + table);
        assertTrue("Expected 1 row in result set", rs.next());
        assertEquals("valid", rs.getString(2));
        assertFalse("Expected 1 row in result set", rs.next());
        try {
            conn.createStatement().executeQuery("select * from " + table + " where tenantId = 2");
            fail("TenantId column not hidden on multi-tenant connection");
        } catch (SQLException ex) {
            assertEquals(SQLExceptionCode.COLUMN_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        conn.createStatement().execute("create view " + view + " as select * from " + table);
        conn.createStatement().execute("create sequence " + sequence + " start with 100");
    }
    // Try inserting data to the view
    try (Connection conn = tenantConnection(getUrl())) {
        conn.setAutoCommit(true);
        conn.createStatement().execute("upsert into " + view + " values ( next value for " + sequence + ", 'valid')");
    }
    // Try reading data from the view
    try (Connection conn = tenantConnection(getUrl())) {
        ResultSet rs = conn.createStatement().executeQuery("select * from " + view);
        assertTrue("Expected 2 rows in result set", rs.next());
        assertEquals("valid", rs.getString(2));
        assertTrue("Expected 2 rows in result set", rs.next());
        assertEquals("valid", rs.getString(2));
        assertFalse("Expected 2 rows in result set", rs.next());
    }
    // Make sure the tenant-specific schema is specific to that tenant
    try (Connection conn = regularConnection(getUrl())) {
        try {
            conn.createStatement().execute("upsert into " + table + " values (" + tenantId + ", next value for " + sequence + ", 'valid')");
            fail();
        } catch (SequenceNotFoundException ex) {
        }
        try {
            ResultSet rs = conn.createStatement().executeQuery("select * from " + view);
            fail();
        } catch (SQLException ex) {
            assertEquals(SQLExceptionCode.TABLE_UNDEFINED.getErrorCode(), ex.getErrorCode());
        }
    }
    if (dataType != "VARCHAR" && dataType != "CHAR(10)") {
        // Try setting up an invalid tenant-specific view
        try (Connection conn = inconvertibleConnection(getUrl())) {
            conn.setAutoCommit(true);
            conn.createStatement().execute("create view " + view + " as select * from " + table);
        }
        // Try inserting data to the invalid tenant-specific view
        try (Connection conn = inconvertibleConnection(getUrl())) {
            conn.setAutoCommit(true);
            try {
                conn.createStatement().execute("upsert into " + view + " values ( 3 , 'invalid')");
                fail();
            } catch (SQLException ex) {
                assertEquals(SQLExceptionCode.TENANTID_IS_OF_WRONG_TYPE.getErrorCode(), ex.getErrorCode());
            }
        }
        // Try reading data from the invalid tenant-specific view
        try (Connection conn = inconvertibleConnection(getUrl())) {
            try {
                ResultSet rs = conn.createStatement().executeQuery("select * from " + view);
                fail();
            } catch (SQLException ex) {
                assertEquals(SQLExceptionCode.TENANTID_IS_OF_WRONG_TYPE.getErrorCode(), ex.getErrorCode());
            }
        }
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) Test(org.junit.Test)

Example 5 with TableAlreadyExistsException

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

the class CreateTableIT method testCreateMultiTenantTable.

@Test
public void testCreateMultiTenantTable() throws Exception {
    Properties props = new Properties();
    Connection conn = DriverManager.getConnection(getUrl(), props);
    String tableName = generateUniqueName();
    String ddl = "CREATE TABLE  " + tableName + " (                TenantId UNSIGNED_INT NOT NULL ,\n" + "                Id UNSIGNED_INT NOT NULL ,\n" + "                val VARCHAR ,\n" + "                CONSTRAINT pk PRIMARY KEY(TenantId, Id) \n" + "                ) MULTI_TENANT=true";
    conn.createStatement().execute(ddl);
    conn = DriverManager.getConnection(getUrl(), props);
    try {
        conn.createStatement().execute(ddl);
        fail();
    } catch (TableAlreadyExistsException e) {
    // expected
    }
    conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("DROP TABLE  " + tableName);
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

TableAlreadyExistsException (org.apache.phoenix.schema.TableAlreadyExistsException)11 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 Properties (java.util.Properties)8 Connection (java.sql.Connection)7 NewerTableAlreadyExistsException (org.apache.phoenix.schema.NewerTableAlreadyExistsException)5 Test (org.junit.Test)5 SQLException (java.sql.SQLException)4 ResultSet (java.sql.ResultSet)3 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)3 ColumnAlreadyExistsException (org.apache.phoenix.schema.ColumnAlreadyExistsException)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)2 NamespaceNotFoundException (org.apache.hadoop.hbase.NamespaceNotFoundException)2 TableExistsException (org.apache.hadoop.hbase.TableExistsException)2 AccessDeniedException (org.apache.hadoop.hbase.security.AccessDeniedException)2 RemoteException (org.apache.hadoop.ipc.RemoteException)2 PhoenixIOException (org.apache.phoenix.exception.PhoenixIOException)2 RetriableUpgradeException (org.apache.phoenix.exception.RetriableUpgradeException)2