Java Code Examples for org.apache.phoenix.jdbc.PhoenixConnection#close()

The following examples show how to use org.apache.phoenix.jdbc.PhoenixConnection#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private  static void verifySequence(String tenantID, String sequenceName, String sequenceSchemaName, boolean exists, long value) throws SQLException {

        PhoenixConnection phxConn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        String ddl = "SELECT "
                + PhoenixDatabaseMetaData.TENANT_ID + ","
                + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + ","
                + PhoenixDatabaseMetaData.SEQUENCE_NAME + ","
                + PhoenixDatabaseMetaData.CURRENT_VALUE
                + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE
                + " WHERE ";

        ddl += " TENANT_ID  " + ((tenantID == null ) ? "IS NULL " : " = '" + tenantID + "'");
        ddl += " AND SEQUENCE_NAME " + ((sequenceName == null) ? "IS NULL " : " = '" +  sequenceName + "'");
        ddl += " AND SEQUENCE_SCHEMA " + ((sequenceSchemaName == null) ? "IS NULL " : " = '" + sequenceSchemaName + "'" );

        ResultSet rs = phxConn.createStatement().executeQuery(ddl);

        if(exists) {
            assertTrue(rs.next());
            assertEquals(value, rs.getLong(4));
        } else {
            assertFalse(rs.next());
        }
        phxConn.close();
    }
 
Example 2
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCSVCommonsUpsert_NonExistentTable() throws Exception {
    PhoenixConnection conn = null;
    try {
        conn = DriverManager.getConnection(getUrl()).unwrap(
                PhoenixConnection.class);
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "NONEXISTENTTABLE",
                null, true, ',', '"', '\\', "!");
        csvUtil.upsert(
                new StringReader("ID,VALARRAY\n"
                        + "1,2!3!4\n"));
        fail("Trying to load a non-existent table should fail");
    } catch (IllegalArgumentException e) {
        assertEquals("Table NONEXISTENTTABLE not found", e.getMessage());
    } finally {
        if (conn != null) {
            conn.close();
        }
    }

}
 
Example 3
Source File: UpsertSelectAutoCommitIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testMaxMutationSize() throws Exception {
    Properties connectionProperties = new Properties();
    connectionProperties.setProperty(QueryServices.MAX_MUTATION_SIZE_ATTRIB, "3");
    connectionProperties.setProperty(QueryServices.MAX_MUTATION_SIZE_BYTES_ATTRIB, "50000");
    connectionProperties.setProperty(QueryServices.ENABLE_SERVER_SIDE_UPSERT_MUTATIONS,
        allowServerSideMutations);
    PhoenixConnection connection =
            (PhoenixConnection) DriverManager.getConnection(getUrl(), connectionProperties);
    connection.setAutoCommit(true);
    String fullTableName = generateUniqueName();
    try (Statement stmt = connection.createStatement()) {
        stmt.execute(
                "CREATE TABLE " + fullTableName + " (pk INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER)");
        stmt.execute(
                "CREATE SEQUENCE " + fullTableName + "_seq cache 1000");
        stmt.execute("UPSERT INTO " + fullTableName + " VALUES (NEXT VALUE FOR " + fullTableName + "_seq, rand(), rand())");
    }
    try (Statement stmt = connection.createStatement()) {
        for (int i=0; i<16; i++) {
            stmt.execute("UPSERT INTO " + fullTableName + " SELECT NEXT VALUE FOR " + fullTableName + "_seq, rand(), rand() FROM " + fullTableName);
        }
    }
    connection.close();
}
 
Example 4
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCSVCommonsUpsert_NonExistentTable() throws Exception {
    PhoenixConnection conn = null;
    try {
        conn = DriverManager.getConnection(getUrl()).unwrap(
                PhoenixConnection.class);
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "NONEXISTENTTABLE",
                null, true, ',', '"', null, "!");
        csvUtil.upsert(
                new StringReader("ID,VALARRAY\n"
                        + "1,2!3!4\n"));
        fail("Trying to load a non-existent table should fail");
    } catch (IllegalArgumentException e) {
        assertEquals("Table NONEXISTENTTABLE not found", e.getMessage());
    } finally {
        if (conn != null) {
            conn.close();
        }
    }

}
 
Example 5
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVCommonsUpsert_WithArray() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {

        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS ARRAY_TABLE "
                + "(ID BIGINT NOT NULL PRIMARY KEY, VALARRAY INTEGER ARRAY);";
        conn = DriverManager.getConnection(getUrl()).unwrap(
                PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "ARRAY_TABLE",
                ImmutableList.<String>of(), true, ',', '"', null, "!");
        csvUtil.upsert(
                new StringReader("ID,VALARRAY\n"
                        + "1,2!3!4\n"));

        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn
                .prepareStatement("SELECT ID, VALARRAY FROM ARRAY_TABLE");
        ResultSet phoenixResultSet = statement.executeQuery();
        assertTrue(phoenixResultSet.next());
        assertEquals(1L, phoenixResultSet.getLong(1));
        assertEquals(
                PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE, new Integer[]{2, 3, 4}),
                phoenixResultSet.getArray(2));
        assertFalse(phoenixResultSet.next());
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 6
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVUpsertWithBogusColumnStrict() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();

        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName
                + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl())
                .unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName,
                Arrays.asList(STOCK_COLUMNS_WITH_BOGUS), true);
        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail();
        } catch (SQLException e) {
            assertTrue(
                    e.getMessage(),
                    e.getMessage()
                            .contains(
                                    "ERROR 504 (42703): Undefined column. columnName=" + stockTableName + ".BOGUS"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 7
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVUpsertWithAllColumn() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();

        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName
                + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl())
                .unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName,
                Arrays.asList("FOO", "BAR"), false);

        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail();
        } catch (SQLException e) {
            assertTrue(
                    e.getMessage(),
                    e.getMessage()
                            .contains(
                                    "ERROR 504 (42703): Undefined column. columnName=" + stockTableName + ".[FOO, BAR]"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 8
Source File: BaseConnectionlessQueryTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static synchronized void doSetup() throws Exception {
    startServer(getUrl());
    ensureTableCreated(getUrl(), ATABLE_NAME);
    ensureTableCreated(getUrl(), ENTITY_HISTORY_TABLE_NAME);
    ensureTableCreated(getUrl(), FUNKY_NAME);
    ensureTableCreated(getUrl(), PTSDB_NAME);
    ensureTableCreated(getUrl(), PTSDB2_NAME);
    ensureTableCreated(getUrl(), PTSDB3_NAME);
    ensureTableCreated(getUrl(), MULTI_CF_NAME);
    ensureTableCreated(getUrl(), TABLE_WITH_ARRAY);
    
    
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(HConstants.LATEST_TIMESTAMP));
    PhoenixConnection conn = DriverManager.getConnection(PHOENIX_CONNECTIONLESS_JDBC_URL, props).unwrap(PhoenixConnection.class);
    try {
        PTable table = conn.getTable(new PTableKey(null, ATABLE_NAME));
        ATABLE = table;
        ORGANIZATION_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ORGANIZATION_ID").getPosition()).newColumnExpression();
        ENTITY_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ENTITY_ID").getPosition()).newColumnExpression();
        A_INTEGER = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression();
        A_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression();
        B_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("B_STRING").getPosition()).newColumnExpression();
        A_DATE = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_DATE").getPosition()).newColumnExpression();
        A_TIME = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIME").getPosition()).newColumnExpression();
        A_TIMESTAMP = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIMESTAMP").getPosition()).newColumnExpression();
        X_DECIMAL = new ColumnRef(new TableRef(table), table.getColumnForColumnName("X_DECIMAL").getPosition()).newColumnExpression();
    } finally {
        conn.close();
    }
}
 
Example 9
Source File: ScannerLeaseRenewalTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenewLeaseTaskBehaviorOnError() throws Exception {
    // add connection to the queue
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    LinkedBlockingQueue<WeakReference<PhoenixConnection>> connectionsQueue = new LinkedBlockingQueue<>();
    connectionsQueue.add(new WeakReference<PhoenixConnection>(pconn));
    
    // create a scanner and add it to the queue
    int numLeaseRenewals = 4;
    int lockNotAcquiredAt = 1;
    int thresholdNotReachedCount = 2;
    int failLeaseRenewalAt = 3;
    RenewLeaseOnlyTableIterator itr = new RenewLeaseOnlyTableIterator(numLeaseRenewals, thresholdNotReachedCount, lockNotAcquiredAt, failLeaseRenewalAt);
    LinkedBlockingQueue<WeakReference<TableResultIterator>> scannerQueue = pconn.getScanners();
    scannerQueue.add(new WeakReference<TableResultIterator>(itr));
    
    RenewLeaseTask task = new RenewLeaseTask(connectionsQueue);
    assertTrue(connectionsQueue.size() == 1);
    assertTrue(scannerQueue.size() == 1);
    
    task.run();
    assertTrue(connectionsQueue.size() == 1); 
    assertTrue(scannerQueue.size() == 1); // lock not acquired
    assertEquals(LOCK_NOT_ACQUIRED, itr.getLastRenewLeaseStatus());
    
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1); // renew lease skipped but scanner still in the queue
    assertEquals(THRESHOLD_NOT_REACHED, itr.getLastRenewLeaseStatus());
    
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue(connectionsQueue.size() == 0); // there was only one connection in the connectionsQueue and it wasn't added back because of error
    
    pconn.close();
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue("Closing the connection should have removed it from the queue", connectionsQueue.size() == 0);
}
 
Example 10
Source File: UpgradeUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static SQLException closeConnection(PhoenixConnection conn, SQLException sqlEx) {
    SQLException toReturn = sqlEx;
    try {
        conn.close();
    } catch (SQLException e) {
        if (toReturn != null) {
            toReturn.setNextException(e);
        } else {
            toReturn = e;
        }
    }
    return toReturn;
}
 
Example 11
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVCommonsUpsertBadEncapsulatedControlChars()
        throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS "
                + ENCAPSULATED_CHARS_TABLE
                + "(MYKEY VARCHAR NOT NULL PRIMARY KEY, MYVALUE VARCHAR);";
        conn = DriverManager.getConnection(getUrl())
                .unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn,
                ENCAPSULATED_CHARS_TABLE, Collections.<String> emptyList(),
                true);
        try {
            csvUtil.upsert(new StringReader(
                    CSV_VALUES_BAD_ENCAPSULATED_CONTROL_CHARS_WITH_HEADER));
            fail();
        } catch (RuntimeException e) {
            assertTrue(
                    e.getMessage(),
                    e.getMessage()
                            .contains(
                                    "invalid char between encapsulated token and delimiter"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 12
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVCommonsUpsert_WithArray() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {

        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS ARRAY_TABLE "
                + "(ID BIGINT NOT NULL PRIMARY KEY, VALARRAY INTEGER ARRAY);";
        conn = DriverManager.getConnection(getUrl()).unwrap(
                PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "ARRAY_TABLE",
                null, true, ',', '"', null, "!");
        csvUtil.upsert(
                new StringReader("ID,VALARRAY\n"
                        + "1,2!3!4\n"));

        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn
                .prepareStatement("SELECT ID, VALARRAY FROM ARRAY_TABLE");
        ResultSet phoenixResultSet = statement.executeQuery();
        assertTrue(phoenixResultSet.next());
        assertEquals(1L, phoenixResultSet.getLong(1));
        assertEquals(
                PArrayDataType.instantiatePhoenixArray(PInteger.INSTANCE, new Integer[]{2, 3, 4}),
                phoenixResultSet.getArray(2));
        assertFalse(phoenixResultSet.next());
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 13
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVCommonsUpsertBadEncapsulatedControlChars()
        throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS "
                + ENCAPSULATED_CHARS_TABLE
                + "(MYKEY VARCHAR NOT NULL PRIMARY KEY, MYVALUE VARCHAR);";
        conn = DriverManager.getConnection(getUrl())
                .unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn,
                ENCAPSULATED_CHARS_TABLE, Collections.<String> emptyList(),
                true);
        try {
            csvUtil.upsert(new StringReader(
                    CSV_VALUES_BAD_ENCAPSULATED_CONTROL_CHARS_WITH_HEADER));
            fail();
        } catch (RuntimeException e) {
            assertTrue(
                    e.getMessage(),
                    e.getMessage()
                            .contains(
                                    "invalid char between encapsulated token and delimiter"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 14
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVUpsertWithBogusColumnStrict() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE
                + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl())
                .unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, STOCK_TABLE,
                Arrays.asList(STOCK_COLUMNS_WITH_BOGUS), true);
        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail();
        } catch (SQLException e) {
            assertTrue(
                    e.getMessage(),
                    e.getMessage()
                            .contains(
                                    "ERROR 504 (42703): Undefined column. columnName=STOCK_SYMBOL.BOGUS"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 15
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVUpsertWithAllColumn() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE
                + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
        conn = DriverManager.getConnection(getUrl())
                .unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, STOCK_TABLE,
                Arrays.asList("FOO", "BAR"), false);

        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail();
        } catch (SQLException e) {
            assertTrue(
                    e.getMessage(),
                    e.getMessage()
                            .contains(
                                    "ERROR 504 (42703): Undefined column. columnName=STOCK_SYMBOL.[FOO, BAR]"));
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 16
Source File: CSVCommonsLoaderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCSVCommonsUpsert_WithTimestamp() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {

        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS TS_TABLE "
                + "(ID BIGINT NOT NULL PRIMARY KEY, TS TIMESTAMP);";
        conn = DriverManager.getConnection(getUrl()).unwrap(
                PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn,
                new StringReader(statements), null);

        // Upsert CSV file
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "TS_TABLE",
                ImmutableList.<String>of(), true, ',', '"', null, "!");
        csvUtil.upsert(
                new StringReader("ID,TS\n"
                        + "1,1970-01-01 00:00:10\n"
                        + "2,1970-01-01 00:00:10.123\n"));

        // Compare Phoenix ResultSet with CSV file content
        PreparedStatement statement = conn
                .prepareStatement("SELECT ID, TS FROM TS_TABLE ORDER BY ID");
        ResultSet phoenixResultSet = statement.executeQuery();
        assertTrue(phoenixResultSet.next());
        assertEquals(1L, phoenixResultSet.getLong(1));
        assertEquals(10000L, phoenixResultSet.getTimestamp(2).getTime());
        assertTrue(phoenixResultSet.next());
        assertEquals(2L, phoenixResultSet.getLong(1));
        assertEquals(10123L, phoenixResultSet.getTimestamp(2).getTime());
        assertFalse(phoenixResultSet.next());
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
 
Example 17
Source File: MutatingParallelIteratorFactory.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public PeekingResultIterator newIterator(final StatementContext parentContext,
        ResultIterator iterator, Scan scan, String tableName,
        QueryPlan plan) throws SQLException {

    final PhoenixConnection clonedConnection = new PhoenixConnection(this.connection);
    try {
        MutationState state = mutate(parentContext, iterator, clonedConnection);

        final long totalRowCount = state.getUpdateCount();
        final boolean autoFlush = connection.getAutoCommit() ||
                plan.getTableRef().getTable().isTransactional();
        if (autoFlush) {
            clonedConnection.getMutationState().join(state);
            state = clonedConnection.getMutationState();
        }
        final MutationState finalState = state;

        byte[] value = PLong.INSTANCE.toBytes(totalRowCount);
        Cell keyValue = PhoenixKeyValueUtil.newKeyValue(UNGROUPED_AGG_ROW_KEY,
                SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value, 0, value.length);
        final Tuple tuple = new SingleKeyValueTuple(keyValue);
        return new PeekingResultIterator() {
            private boolean done = false;

            @Override
            public Tuple next() {
                if (done) {
                    return null;
                }
                done = true;
                return tuple;
            }

            @Override
            public void explain(List<String> planSteps) {
            }

            @Override
            public void close() throws SQLException {
                try {
                    /*
                     * Join the child mutation states in close, since this is called in a single
                     * threaded manner after the parallel results have been processed.
                     * If auto-commit is on for the cloned child connection, then the finalState
                     * here is an empty mutation state (with no mutations). However, it still
                     * has the metrics for mutation work done by the mutating-iterator.
                     * Joining the mutation state makes sure those metrics are passed over
                     * to the parent connection.
                     */
                    MutatingParallelIteratorFactory.this.connection.getMutationState()
                            .join(finalState);
                } finally {
                    clonedConnection.close();
                }
            }

            @Override
            public Tuple peek() {
                return done ? null : tuple;
            }
        };
    } catch (Throwable ex) {
        // Catch just to make sure we close the cloned connection and then rethrow
        try {
            // closeQuietly only handles IOException
            clonedConnection.close();
        } catch (SQLException sqlEx) {
            LOGGER.error("Closing cloned Phoenix connection inside iterator, failed with: ",
                    sqlEx);
        }
        throw ex;
    }
}
 
Example 18
Source File: DropChildViewsTask.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public TaskRegionObserver.TaskResult run(Task.TaskRecord taskRecord) {
    PhoenixConnection pconn = null;
    Timestamp timestamp = taskRecord.getTimeStamp();
    try {
        String tenantId = taskRecord.getTenantId();
        if (tenantId != null) {
            Properties tenantProps = new Properties();
            tenantProps.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
            pconn = QueryUtil.getConnectionOnServer(tenantProps, env.getConfiguration())
                    .unwrap(PhoenixConnection.class);
        }
        else {
            pconn = QueryUtil.getConnectionOnServer(env.getConfiguration()).unwrap(PhoenixConnection.class);
        }

        MetaDataProtocol.MetaDataMutationResult result = new MetaDataClient(pconn).updateCache(pconn.getTenantId(),
                taskRecord.getSchemaName(), taskRecord.getTableName(), true);
        if (result.getMutationCode() != MetaDataProtocol.MutationCode.TABLE_ALREADY_EXISTS) {
            ViewUtil.dropChildViews(env, taskRecord.getTenantIdBytes(),
                    taskRecord.getSchemaNameBytes(), taskRecord.getTableNameBytes(),
                    SchemaUtil.getPhysicalTableName(
                            SYSTEM_CHILD_LINK_NAME_BYTES,
                            env.getConfiguration()).getName());
            return new TaskRegionObserver.TaskResult(TaskRegionObserver.TaskResultCode.SUCCESS, "");
        } else if (EnvironmentEdgeManager.currentTimeMillis() < timeMaxInterval + timestamp.getTime()) {
            // skip this task as it has not been expired and its parent table has not been dropped yet
            LOGGER.info("Skipping a child view drop task. " +
                    "The parent table has not been dropped yet : " +
                    taskRecord.getSchemaName() + "." + taskRecord.getTableName() +
                    " with tenant id " + (tenantId == null ? " IS NULL" : tenantId) +
                    " and timestamp " + timestamp.toString());
            return new TaskRegionObserver.TaskResult(TaskRegionObserver.TaskResultCode.SKIPPED, "");
        }
        else {
            LOGGER.warn(" A drop child view task has expired and will be marked as failed : " +
                    taskRecord.getSchemaName() + "." + taskRecord.getTableName() +
                    " with tenant id " + (tenantId == null ? " IS NULL" : tenantId) +
                    " and timestamp " + timestamp.toString());
            return new TaskRegionObserver.TaskResult(TaskRegionObserver.TaskResultCode.FAIL, "Expired");
        }
    }
    catch (Throwable t) {
        LOGGER.error("Exception while dropping a child view task. " +
                taskRecord.getSchemaName()  + "." + taskRecord.getTableName() +
                " with tenant id " + (taskRecord.getTenantId() == null ? " IS NULL" : taskRecord.getTenantId()) +
                " and timestamp " + timestamp.toString(), t);
        return new TaskRegionObserver.TaskResult(TaskRegionObserver.TaskResultCode.FAIL, t.toString());
    } finally {
        if (pconn != null) {
            try {
                pconn.close();
            } catch (SQLException ignored) {
                LOGGER.debug("DropChildViewsTask can't close connection", ignored);
            }
        }
    }
}
 
Example 19
Source File: QueryDatabaseMetaDataIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateOnExistingTable() throws Exception {
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    String tableName = MDTEST_NAME;
    String schemaName = MDTEST_SCHEMA_NAME;
    byte[] cfA = Bytes.toBytes(SchemaUtil.normalizeIdentifier("a"));
    byte[] cfB = Bytes.toBytes(SchemaUtil.normalizeIdentifier("b"));
    byte[] cfC = Bytes.toBytes("c");
    byte[][] familyNames = new byte[][] {cfB, cfC};
    byte[] htableName = SchemaUtil.getTableNameAsBytes(schemaName, tableName);
    HBaseAdmin admin = pconn.getQueryServices().getAdmin();
    try {
        admin.disableTable(htableName);
        admin.deleteTable(htableName);
        admin.enableTable(htableName);
    } catch (org.apache.hadoop.hbase.TableNotFoundException e) {
    }
    
    @SuppressWarnings("deprecation")
    HTableDescriptor descriptor = new HTableDescriptor(htableName);
    for (byte[] familyName : familyNames) {
        HColumnDescriptor columnDescriptor = new HColumnDescriptor(familyName);
        descriptor.addFamily(columnDescriptor);
    }
    admin.createTable(descriptor);
        
    long ts = nextTimestamp();
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 5));
    PhoenixConnection conn1 = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    ensureTableCreated(getUrl(), tableName, null, ts);
    
    descriptor = admin.getTableDescriptor(htableName);
    assertEquals(3,descriptor.getColumnFamilies().length);
    HColumnDescriptor cdA = descriptor.getFamily(cfA);
    assertNotEquals(HColumnDescriptor.DEFAULT_KEEP_DELETED, cdA.getKeepDeletedCellsAsEnum());
    assertEquals(DataBlockEncoding.NONE, cdA.getDataBlockEncoding()); // Overriden using WITH
    assertEquals(1,cdA.getMaxVersions());// Overriden using WITH
    HColumnDescriptor cdB = descriptor.getFamily(cfB);
    // Allow KEEP_DELETED_CELLS to be false for VIEW
    assertEquals(HColumnDescriptor.DEFAULT_KEEP_DELETED, cdB.getKeepDeletedCellsAsEnum());
    assertEquals(DataBlockEncoding.NONE, cdB.getDataBlockEncoding()); // Should keep the original value.
    // CF c should stay the same since it's not a Phoenix cf.
    HColumnDescriptor cdC = descriptor.getFamily(cfC);
    assertNotNull("Column family not found", cdC);
    assertEquals(HColumnDescriptor.DEFAULT_KEEP_DELETED, cdC.getKeepDeletedCellsAsEnum());
    assertFalse(SchemaUtil.DEFAULT_DATA_BLOCK_ENCODING == cdC.getDataBlockEncoding());
    assertTrue(descriptor.hasCoprocessor(UngroupedAggregateRegionObserver.class.getName()));
    assertTrue(descriptor.hasCoprocessor(GroupedAggregateRegionObserver.class.getName()));
    assertTrue(descriptor.hasCoprocessor(ServerCachingEndpointImpl.class.getName()));
    admin.close();
     
    int rowCount = 5;
    String upsert = "UPSERT INTO " + tableName + "(id,col1,col2) VALUES(?,?,?)";
    PreparedStatement ps = conn1.prepareStatement(upsert);
    for (int i = 0; i < rowCount; i++) {
        ps.setString(1, Integer.toString(i));
        ps.setInt(2, i+1);
        ps.setInt(3, i+2);
        ps.execute();
    }
    conn1.commit();
    conn1.close();
    
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 6));
    Connection conn2 = DriverManager.getConnection(getUrl(), props);
    String query = "SELECT count(1) FROM " + tableName;
    ResultSet rs = conn2.createStatement().executeQuery(query);
    assertTrue(rs.next());
    assertEquals(rowCount, rs.getLong(1));
    
    query = "SELECT id, col1,col2 FROM " + tableName;
    rs = conn2.createStatement().executeQuery(query);
    for (int i = 0; i < rowCount; i++) {
        assertTrue(rs.next());
        assertEquals(Integer.toString(i),rs.getString(1));
        assertEquals(i+1, rs.getInt(2));
        assertEquals(i+2, rs.getInt(3));
    }
    assertFalse(rs.next());
    conn2.close();
}
 
Example 20
Source File: ScannerLeaseRenewalTest.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testRenewLeaseTaskBehavior() throws Exception {
    // add connection to the queue
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    LinkedBlockingQueue<WeakReference<PhoenixConnection>> connectionsQueue = new LinkedBlockingQueue<>();
    connectionsQueue.add(new WeakReference<PhoenixConnection>(pconn));
    
    // create a scanner and add it to the queue
    int numLeaseRenewals = 4;
    int skipRenewLeaseCount = 2;
    int failToAcquireLockAt = 3;
    RenewLeaseOnlyTableIterator itr = new RenewLeaseOnlyTableIterator(numLeaseRenewals, skipRenewLeaseCount, failToAcquireLockAt, -1);
    LinkedBlockingQueue<WeakReference<TableResultIterator>> scannerQueue = pconn.getScanners();
    scannerQueue.add(new WeakReference<TableResultIterator>(itr));
    
    RenewLeaseTask task = new RenewLeaseTask(connectionsQueue);
    assertTrue(connectionsQueue.size() == 1);
    assertTrue(scannerQueue.size() == 1);
    
    task.run();
    assertTrue(connectionsQueue.size() == 1); 
    assertTrue(scannerQueue.size() == 1); // lease renewed
    assertEquals(RENEWED, itr.getLastRenewLeaseStatus());
    
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1); // renew lease skipped but scanner still in the queue
    assertEquals(THRESHOLD_NOT_REACHED, itr.getLastRenewLeaseStatus());
    
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1);
    assertEquals(LOCK_NOT_ACQUIRED, itr.getLastRenewLeaseStatus()); // lock couldn't be acquired
    
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1);
    assertEquals(RENEWED, itr.getLastRenewLeaseStatus()); // lease renewed
    
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue(connectionsQueue.size() == 1);
    assertEquals(CLOSED, itr.getLastRenewLeaseStatus()); // scanner closed and removed from the queue
    
    pconn.close();
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue("Closing the connection should have removed it from the queue", connectionsQueue.size() == 0);
}