Search in sources :

Example 1 with RetriableUpgradeException

use of org.apache.phoenix.exception.RetriableUpgradeException 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 (initialized) {
                    if (initializationException != null) {
                        // Throw previous initialization exception, as we won't resuse this instance
                        throw initializationException;
                    }
                    return null;
                }
                synchronized (ConnectionQueryServicesImpl.this) {
                    if (initialized) {
                        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);
                                }
                                ensureSystemTablesUpgraded(ConnectionQueryServicesImpl.this.getProps());
                            } else if (mappedSystemCatalogExists) {
                                throw new SQLExceptionInfo.Builder(SQLExceptionCode.INCONSISTENET_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();
                            }
                            createSysMutexTable(admin);
                        }
                        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);
                        try (PhoenixConnection metaConnection = new PhoenixConnection(ConnectionQueryServicesImpl.this, globalUrl, scnProps, newEmptyMetaData())) {
                            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();
                                if (currentServerSideTableTimeStamp < MIN_SYSTEM_TABLE_TIMESTAMP) {
                                    ConnectionQueryServicesImpl.this.upgradeRequired.set(true);
                                }
                            } catch (PhoenixIOException e) {
                                if (!Iterables.isEmpty(Iterables.filter(Throwables.getCausalChain(e), AccessDeniedException.class))) {
                                    // 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 {
                                    initializationException = e;
                                }
                                return null;
                            }
                            if (!ConnectionQueryServicesImpl.this.upgradeRequired.get()) {
                                createOtherSystemTables(metaConnection);
                            } 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 {
                                initialized = 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) 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) 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) ReadOnlyTableException(org.apache.phoenix.schema.ReadOnlyTableException) EmptySequenceCacheException(org.apache.phoenix.schema.EmptySequenceCacheException) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) RetriableUpgradeException(org.apache.phoenix.exception.RetriableUpgradeException)

Aggregations

IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 TableExistsException (org.apache.hadoop.hbase.TableExistsException)1 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)1 AccessDeniedException (org.apache.hadoop.hbase.security.AccessDeniedException)1 PhoenixIOException (org.apache.phoenix.exception.PhoenixIOException)1 RetriableUpgradeException (org.apache.phoenix.exception.RetriableUpgradeException)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 UpgradeInProgressException (org.apache.phoenix.exception.UpgradeInProgressException)1 UpgradeNotRequiredException (org.apache.phoenix.exception.UpgradeNotRequiredException)1 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)1 ColumnAlreadyExistsException (org.apache.phoenix.schema.ColumnAlreadyExistsException)1 ColumnFamilyNotFoundException (org.apache.phoenix.schema.ColumnFamilyNotFoundException)1 EmptySequenceCacheException (org.apache.phoenix.schema.EmptySequenceCacheException)1 FunctionNotFoundException (org.apache.phoenix.schema.FunctionNotFoundException)1 NewerSchemaAlreadyExistsException (org.apache.phoenix.schema.NewerSchemaAlreadyExistsException)1 NewerTableAlreadyExistsException (org.apache.phoenix.schema.NewerTableAlreadyExistsException)1