Search in sources :

Example 1 with TableResultIterator

use of org.apache.phoenix.iterate.TableResultIterator in project phoenix by apache.

the class ScannerLeaseRenewalTest method testRenewLeaseTaskBehavior.

@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);
    // lease renewed
    assertTrue(scannerQueue.size() == 1);
    assertEquals(RENEWED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    // renew lease skipped but scanner still in the queue
    assertTrue(connectionsQueue.size() == 1);
    assertEquals(THRESHOLD_NOT_REACHED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1);
    // lock couldn't be acquired
    assertEquals(LOCK_NOT_ACQUIRED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    assertTrue(connectionsQueue.size() == 1);
    // lease renewed
    assertEquals(RENEWED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue(connectionsQueue.size() == 1);
    // scanner closed and removed from the queue
    assertEquals(CLOSED, itr.getLastRenewLeaseStatus());
    pconn.close();
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue("Closing the connection should have removed it from the queue", connectionsQueue.size() == 0);
}
Also used : RenewLeaseOnlyTableIterator(org.apache.phoenix.iterate.RenewLeaseOnlyTableIterator) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) RenewLeaseTask(org.apache.phoenix.query.ConnectionQueryServicesImpl.RenewLeaseTask) WeakReference(java.lang.ref.WeakReference) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 2 with TableResultIterator

use of org.apache.phoenix.iterate.TableResultIterator in project phoenix by apache.

the class PhoenixRecordReader method initialize.

public void initialize(InputSplit split) throws IOException {
    final PhoenixInputSplit pSplit = (PhoenixInputSplit) split;
    final List<Scan> scans = pSplit.getScans();
    if (LOG.isInfoEnabled()) {
        LOG.info("Target table : " + queryPlan.getTableRef().getTable().getPhysicalName());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Scan count[" + scans.size() + "] : " + Bytes.toStringBinary(scans.get(0).getStartRow()) + " ~ " + Bytes.toStringBinary(scans.get(scans.size() - 1).getStopRow()));
        LOG.debug("First scan : " + scans.get(0) + " scanAttribute : " + scans.get(0).getAttributesMap());
        for (int i = 0, limit = scans.size(); i < limit; i++) {
            LOG.debug("EXPECTED_UPPER_REGION_KEY[" + i + "] : " + Bytes.toStringBinary(scans.get(i).getAttribute(BaseScannerRegionObserver.EXPECTED_UPPER_REGION_KEY)));
        }
    }
    try {
        List<PeekingResultIterator> iterators = Lists.newArrayListWithExpectedSize(scans.size());
        StatementContext ctx = queryPlan.getContext();
        ReadMetricQueue readMetrics = ctx.getReadMetricsQueue();
        String tableName = queryPlan.getTableRef().getTable().getPhysicalName().getString();
        long renewScannerLeaseThreshold = queryPlan.getContext().getConnection().getQueryServices().getRenewLeaseThresholdMilliSeconds();
        boolean isRequestMetricsEnabled = readMetrics.isRequestMetricsEnabled();
        for (Scan scan : scans) {
            scan.setAttribute(BaseScannerRegionObserver.SKIP_REGION_BOUNDARY_CHECK, Bytes.toBytes(true));
            ScanMetricsHolder scanMetricsHolder = ScanMetricsHolder.getInstance(readMetrics, tableName, scan, isRequestMetricsEnabled);
            final TableResultIterator tableResultIterator = new TableResultIterator(queryPlan.getContext().getConnection().getMutationState(), scan, scanMetricsHolder, renewScannerLeaseThreshold, queryPlan, MapReduceParallelScanGrouper.getInstance());
            PeekingResultIterator peekingResultIterator = LookAheadResultIterator.wrap(tableResultIterator);
            iterators.add(peekingResultIterator);
        }
        ResultIterator iterator = queryPlan.useRoundRobinIterator() ? RoundRobinResultIterator.newIterator(iterators, queryPlan) : ConcatResultIterator.newIterator(iterators);
        if (queryPlan.getContext().getSequenceManager().getSequenceCount() > 0) {
            iterator = new SequenceResultIterator(iterator, queryPlan.getContext().getSequenceManager());
        }
        this.resultIterator = iterator;
        // Clone the row projector as it's not thread safe and would be used
        // simultaneously by multiple threads otherwise.
        this.resultSet = new PhoenixResultSet(this.resultIterator, queryPlan.getProjector().cloneIfNecessary(), queryPlan.getContext());
    } catch (SQLException e) {
        LOG.error(String.format(" Error [%s] initializing PhoenixRecordReader. ", e.getMessage()));
        Throwables.propagate(e);
    }
}
Also used : ReadMetricQueue(org.apache.phoenix.monitoring.ReadMetricQueue) SQLException(java.sql.SQLException) SequenceResultIterator(org.apache.phoenix.iterate.SequenceResultIterator) SequenceResultIterator(org.apache.phoenix.iterate.SequenceResultIterator) ConcatResultIterator(org.apache.phoenix.iterate.ConcatResultIterator) RoundRobinResultIterator(org.apache.phoenix.iterate.RoundRobinResultIterator) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) ResultIterator(org.apache.phoenix.iterate.ResultIterator) LookAheadResultIterator(org.apache.phoenix.iterate.LookAheadResultIterator) PeekingResultIterator(org.apache.phoenix.iterate.PeekingResultIterator) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) ScanMetricsHolder(org.apache.phoenix.monitoring.ScanMetricsHolder) PeekingResultIterator(org.apache.phoenix.iterate.PeekingResultIterator) StatementContext(org.apache.phoenix.compile.StatementContext) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) Scan(org.apache.hadoop.hbase.client.Scan)

Example 3 with TableResultIterator

use of org.apache.phoenix.iterate.TableResultIterator in project phoenix by apache.

the class ScannerLeaseRenewalTest method testRenewLeaseTaskBehaviorOnError.

@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);
    // lock not acquired
    assertTrue(scannerQueue.size() == 1);
    assertEquals(LOCK_NOT_ACQUIRED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 1);
    // renew lease skipped but scanner still in the queue
    assertTrue(connectionsQueue.size() == 1);
    assertEquals(THRESHOLD_NOT_REACHED, itr.getLastRenewLeaseStatus());
    task.run();
    assertTrue(scannerQueue.size() == 0);
    // there was only one connection in the connectionsQueue and it wasn't added back because of error
    assertTrue(connectionsQueue.size() == 0);
    pconn.close();
    task.run();
    assertTrue(scannerQueue.size() == 0);
    assertTrue("Closing the connection should have removed it from the queue", connectionsQueue.size() == 0);
}
Also used : RenewLeaseOnlyTableIterator(org.apache.phoenix.iterate.RenewLeaseOnlyTableIterator) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) RenewLeaseTask(org.apache.phoenix.query.ConnectionQueryServicesImpl.RenewLeaseTask) WeakReference(java.lang.ref.WeakReference) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Aggregations

TableResultIterator (org.apache.phoenix.iterate.TableResultIterator)3 WeakReference (java.lang.ref.WeakReference)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 RenewLeaseOnlyTableIterator (org.apache.phoenix.iterate.RenewLeaseOnlyTableIterator)2 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)2 RenewLeaseTask (org.apache.phoenix.query.ConnectionQueryServicesImpl.RenewLeaseTask)2 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1 Scan (org.apache.hadoop.hbase.client.Scan)1 StatementContext (org.apache.phoenix.compile.StatementContext)1 ConcatResultIterator (org.apache.phoenix.iterate.ConcatResultIterator)1 LookAheadResultIterator (org.apache.phoenix.iterate.LookAheadResultIterator)1 PeekingResultIterator (org.apache.phoenix.iterate.PeekingResultIterator)1 ResultIterator (org.apache.phoenix.iterate.ResultIterator)1 RoundRobinResultIterator (org.apache.phoenix.iterate.RoundRobinResultIterator)1 SequenceResultIterator (org.apache.phoenix.iterate.SequenceResultIterator)1 PhoenixResultSet (org.apache.phoenix.jdbc.PhoenixResultSet)1 ReadMetricQueue (org.apache.phoenix.monitoring.ReadMetricQueue)1 ScanMetricsHolder (org.apache.phoenix.monitoring.ScanMetricsHolder)1