Search in sources :

Example 26 with ConnectionQueryServices

use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.

the class PhoenixDriver method getConnectionQueryServices.

@Override
protected ConnectionQueryServices getConnectionQueryServices(String url, final Properties info) throws SQLException {
    try {
        lockInterruptibly(LockMode.READ);
        checkClosed();
        ConnectionInfo connInfo = ConnectionInfo.create(url);
        SQLException sqlE = null;
        boolean success = false;
        final QueryServices services = getQueryServices();
        ConnectionQueryServices connectionQueryServices = null;
        // Also performs the Kerberos login if the URL/properties request this
        final ConnectionInfo normalizedConnInfo = connInfo.normalize(services.getProps(), info);
        try {
            connectionQueryServices = connectionQueryServicesCache.get(normalizedConnInfo, new Callable<ConnectionQueryServices>() {

                @Override
                public ConnectionQueryServices call() throws Exception {
                    ConnectionQueryServices connectionQueryServices;
                    if (normalizedConnInfo.isConnectionless()) {
                        connectionQueryServices = new ConnectionlessQueryServicesImpl(services, normalizedConnInfo, info);
                    } else {
                        connectionQueryServices = new ConnectionQueryServicesImpl(services, normalizedConnInfo, info);
                    }
                    return connectionQueryServices;
                }
            });
            connectionQueryServices.init(url, info);
            success = true;
        } catch (ExecutionException ee) {
            if (ee.getCause() instanceof SQLException) {
                sqlE = (SQLException) ee.getCause();
            } else {
                throw new SQLException(ee);
            }
        } catch (SQLException e) {
            sqlE = e;
        } finally {
            if (!success) {
                // Remove from map, as initialization failed
                connectionQueryServicesCache.invalidate(normalizedConnInfo);
                if (sqlE != null) {
                    throw sqlE;
                }
            }
        }
        return connectionQueryServices;
    } finally {
        unlock(LockMode.READ);
    }
}
Also used : ConnectionlessQueryServicesImpl(org.apache.phoenix.query.ConnectionlessQueryServicesImpl) ConnectionQueryServicesImpl(org.apache.phoenix.query.ConnectionQueryServicesImpl) SQLException(java.sql.SQLException) QueryServices(org.apache.phoenix.query.QueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices)

Example 27 with ConnectionQueryServices

use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.

the class PhoenixEmbeddedDriver method createConnection.

protected final Connection createConnection(String url, Properties info) throws SQLException {
    Properties augmentedInfo = PropertiesUtil.deepCopy(info);
    augmentedInfo.putAll(getDefaultProps().asMap());
    ConnectionQueryServices connectionServices = getConnectionQueryServices(url, augmentedInfo);
    PhoenixConnection connection = connectionServices.connect(url, augmentedInfo);
    return connection;
}
Also used : Properties(java.util.Properties) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices)

Example 28 with ConnectionQueryServices

use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.

the class MutableIndexIT method testIndexHalfStoreFileReader.

@Test
public void testIndexHalfStoreFileReader() throws Exception {
    Connection conn1 = getConnection();
    ConnectionQueryServices connectionQueryServices = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES);
    HBaseAdmin admin = connectionQueryServices.getAdmin();
    String tableName = "TBL_" + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    createBaseTable(conn1, tableName, "('e')");
    conn1.createStatement().execute("CREATE " + (localIndex ? "LOCAL" : "") + " INDEX " + indexName + " ON " + tableName + "(v1)" + (localIndex ? "" : " SPLIT ON ('e')"));
    conn1.createStatement().execute("UPSERT INTO " + tableName + " values('b',1,2,4,'z')");
    conn1.createStatement().execute("UPSERT INTO " + tableName + " values('f',1,2,3,'z')");
    conn1.createStatement().execute("UPSERT INTO " + tableName + " values('j',2,4,2,'a')");
    conn1.createStatement().execute("UPSERT INTO " + tableName + " values('q',3,1,1,'c')");
    conn1.commit();
    String query = "SELECT count(*) FROM " + tableName + " where v1<='z'";
    ResultSet rs = conn1.createStatement().executeQuery(query);
    assertTrue(rs.next());
    assertEquals(4, rs.getInt(1));
    TableName indexTable = TableName.valueOf(localIndex ? tableName : indexName);
    admin.flush(indexTable);
    boolean merged = false;
    HTableInterface table = connectionQueryServices.getTable(indexTable.getName());
    // merge regions until 1 left
    long numRegions = 0;
    while (true) {
        rs = conn1.createStatement().executeQuery(query);
        assertTrue(rs.next());
        //TODO this returns 5 sometimes instead of 4, duplicate results?
        assertEquals(4, rs.getInt(1));
        try {
            List<HRegionInfo> indexRegions = admin.getTableRegions(indexTable);
            numRegions = indexRegions.size();
            if (numRegions == 1) {
                break;
            }
            if (!merged) {
                List<HRegionInfo> regions = admin.getTableRegions(indexTable);
                Log.info("Merging: " + regions.size());
                admin.mergeRegions(regions.get(0).getEncodedNameAsBytes(), regions.get(1).getEncodedNameAsBytes(), false);
                merged = true;
                Threads.sleep(10000);
            }
        } catch (Exception ex) {
            Log.info(ex);
        }
        long waitStartTime = System.currentTimeMillis();
        // wait until merge happened
        while (System.currentTimeMillis() - waitStartTime < 10000) {
            List<HRegionInfo> regions = admin.getTableRegions(indexTable);
            Log.info("Waiting:" + regions.size());
            if (regions.size() < numRegions) {
                break;
            }
            Threads.sleep(1000);
        }
        SnapshotTestingUtils.waitForTableToBeOnline(BaseTest.getUtility(), indexTable);
        assertTrue("Index table should be online ", admin.isTableAvailable(indexTable));
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableName(org.apache.hadoop.hbase.TableName) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) SQLException(java.sql.SQLException) IOException(java.io.IOException) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 29 with ConnectionQueryServices

use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.

the class UpgradeIT method testConcurrentUpgradeThrowsUprgadeInProgressException.

@Test
public void testConcurrentUpgradeThrowsUprgadeInProgressException() throws Exception {
    final AtomicBoolean mutexStatus1 = new AtomicBoolean(false);
    final AtomicBoolean mutexStatus2 = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(2);
    final AtomicInteger numExceptions = new AtomicInteger(0);
    ConnectionQueryServices services = null;
    final byte[] mutexKey = Bytes.toBytes(generateUniqueName());
    try (Connection conn = getConnection(false, null)) {
        services = conn.unwrap(PhoenixConnection.class).getQueryServices();
        putUnlockKVInSysMutex(mutexKey);
        FutureTask<Void> task1 = new FutureTask<>(new AcquireMutexRunnable(mutexStatus1, services, latch, numExceptions, mutexKey));
        FutureTask<Void> task2 = new FutureTask<>(new AcquireMutexRunnable(mutexStatus2, services, latch, numExceptions, mutexKey));
        Thread t1 = new Thread(task1);
        t1.setDaemon(true);
        Thread t2 = new Thread(task2);
        t2.setDaemon(true);
        t1.start();
        t2.start();
        latch.await();
        // make sure tasks didn't fail by calling get()
        task1.get();
        task2.get();
        assertTrue("One of the threads should have acquired the mutex", mutexStatus1.get() || mutexStatus2.get());
        assertNotEquals("One and only one thread should have acquired the mutex ", mutexStatus1.get(), mutexStatus2.get());
        assertEquals("One and only one thread should have caught UpgradeRequiredException ", 1, numExceptions.get());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FutureTask(java.util.concurrent.FutureTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) CountDownLatch(java.util.concurrent.CountDownLatch) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) Test(org.junit.Test)

Example 30 with ConnectionQueryServices

use of org.apache.phoenix.query.ConnectionQueryServices in project phoenix by apache.

the class ProductMetricsIT method destroyTable.

private static void destroyTable() throws Exception {
    // Physically delete HBase table so that splits occur as expected for each test
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    ConnectionQueryServices services = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class).getQueryServices();
    HBaseAdmin admin = services.getAdmin();
    try {
        try {
            admin.disableTable(PRODUCT_METRICS_NAME);
            admin.deleteTable(PRODUCT_METRICS_NAME);
        } catch (TableNotFoundException e) {
        }
    } finally {
        admin.close();
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices)

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