Search in sources :

Example 1 with ByteKeyRange

use of org.apache.beam.sdk.io.range.ByteKeyRange in project beam by apache.

the class ByteKeyRangeTracker method trySplit.

@Override
public SplitResult<ByteKeyRange> trySplit(double fractionOfRemainder) {
    // No split on an empty range.
    if (NO_KEYS.equals(range) || (!range.getEndKey().isEmpty() && range.getStartKey().equals(range.getEndKey()))) {
        return null;
    }
    // There is no more remaining work after the entire range has been claimed.
    if (lastAttemptedKey != null && lastAttemptedKey.isEmpty()) {
        return null;
    }
    ByteKey unprocessedRangeStartKey = (lastAttemptedKey == null) ? range.getStartKey() : next(lastAttemptedKey);
    ByteKey endKey = range.getEndKey();
    // There is no more space for split.
    if (!endKey.isEmpty() && unprocessedRangeStartKey.compareTo(endKey) >= 0) {
        return null;
    }
    // trailing zeros when fraction is 0.
    if (fractionOfRemainder == 0.0) {
        // as the checkpoint.
        if (lastAttemptedKey == null) {
            // We update our current range to an interval that contains no elements.
            ByteKeyRange rval = range;
            range = range.getStartKey().isEmpty() ? NO_KEYS : ByteKeyRange.of(range.getStartKey(), range.getStartKey());
            return SplitResult.of(range, rval);
        } else {
            range = ByteKeyRange.of(range.getStartKey(), unprocessedRangeStartKey);
            return SplitResult.of(range, ByteKeyRange.of(unprocessedRangeStartKey, endKey));
        }
    }
    ByteKeyRange unprocessedRange = ByteKeyRange.of(unprocessedRangeStartKey, range.getEndKey());
    ByteKey splitPos;
    try {
        // The interpolateKey shouldn't return empty key. Please refer to {@link
        // ByteKeyRange#interpolateKey}.
        splitPos = unprocessedRange.interpolateKey(fractionOfRemainder);
        checkState(!splitPos.isEmpty());
    } catch (Exception e) {
        // There is no way to interpolate a key based on provided fraction.
        return null;
    }
    // Computed splitPos is out of current tracking restriction.
    if (!range.getEndKey().isEmpty() && splitPos.compareTo(range.getEndKey()) >= 0) {
        return null;
    }
    range = ByteKeyRange.of(range.getStartKey(), splitPos);
    return SplitResult.of(range, ByteKeyRange.of(splitPos, endKey));
}
Also used : ByteKey(org.apache.beam.sdk.io.range.ByteKey) ByteKeyRange(org.apache.beam.sdk.io.range.ByteKeyRange)

Example 2 with ByteKeyRange

use of org.apache.beam.sdk.io.range.ByteKeyRange in project beam by apache.

the class ByteKeyRangeTrackerTest method testCheckpointUnstartedForAllKeysRange.

@Test
public void testCheckpointUnstartedForAllKeysRange() throws Exception {
    ByteKeyRangeTracker tracker = ByteKeyRangeTracker.of(ByteKeyRange.ALL_KEYS);
    ByteKeyRange checkpoint = tracker.trySplit(0).getResidual();
    // We expect to get the original range back and that the current restriction
    // is effectively made empty.
    assertEquals(ByteKeyRange.ALL_KEYS, checkpoint);
    assertEquals(ByteKeyRangeTracker.NO_KEYS, tracker.currentRestriction());
    tracker.checkDone();
}
Also used : ByteKeyRange(org.apache.beam.sdk.io.range.ByteKeyRange) Test(org.junit.Test)

Example 3 with ByteKeyRange

use of org.apache.beam.sdk.io.range.ByteKeyRange in project beam by apache.

the class ByteKeyRangeTrackerTest method testCheckpointJustStarted.

@Test
public void testCheckpointJustStarted() throws Exception {
    ByteKeyRangeTracker tracker = ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x10)));
    ByteKeyRange checkpoint = tracker.trySplit(0).getResidual();
    assertEquals(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0x10, 0x00)), tracker.currentRestriction());
    assertEquals(ByteKeyRange.of(ByteKey.of(0x10, 0x00), ByteKey.of(0xc0)), checkpoint);
    tracker.checkDone();
}
Also used : ByteKeyRange(org.apache.beam.sdk.io.range.ByteKeyRange) Test(org.junit.Test)

Example 4 with ByteKeyRange

use of org.apache.beam.sdk.io.range.ByteKeyRange in project beam by apache.

the class ByteKeyRangeTrackerTest method testTryClaim.

@Test
public void testTryClaim() throws Exception {
    ByteKeyRange range = ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0));
    ByteKeyRangeTracker tracker = ByteKeyRangeTracker.of(range);
    assertEquals(range, tracker.currentRestriction());
    assertTrue(tracker.tryClaim(ByteKey.of(0x10)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x10, 0x00)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x10, 0x00, 0x00)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x50)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x99)));
    assertFalse(tracker.tryClaim(ByteKey.of(0xc0)));
    tracker.checkDone();
}
Also used : ByteKeyRange(org.apache.beam.sdk.io.range.ByteKeyRange) Test(org.junit.Test)

Example 5 with ByteKeyRange

use of org.apache.beam.sdk.io.range.ByteKeyRange in project beam by apache.

the class ByteKeyRangeTrackerTest method testCheckpointRegular.

@Test
public void testCheckpointRegular() throws Exception {
    ByteKeyRangeTracker tracker = ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x50)));
    assertTrue(tracker.tryClaim(ByteKey.of(0x90)));
    ByteKeyRange checkpoint = tracker.trySplit(0).getResidual();
    assertEquals(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0x90, 0x00)), tracker.currentRestriction());
    assertEquals(ByteKeyRange.of(ByteKey.of(0x90, 0x00), ByteKey.of(0xc0)), checkpoint);
    tracker.checkDone();
}
Also used : ByteKeyRange(org.apache.beam.sdk.io.range.ByteKeyRange) Test(org.junit.Test)

Aggregations

ByteKeyRange (org.apache.beam.sdk.io.range.ByteKeyRange)22 Test (org.junit.Test)17 ByteKey (org.apache.beam.sdk.io.range.ByteKey)10 ByteString (com.google.protobuf.ByteString)8 BigtableSource (org.apache.beam.sdk.io.gcp.bigtable.BigtableIO.BigtableSource)5 ArrayList (java.util.ArrayList)4 Row (com.google.bigtable.v2.Row)3 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)2 Connection (org.apache.hadoop.hbase.client.Connection)2 RowFilter (com.google.bigtable.v2.RowFilter)1 DisplayData (org.apache.beam.sdk.transforms.display.DisplayData)1 TableName (org.apache.hadoop.hbase.TableName)1 Result (org.apache.hadoop.hbase.client.Result)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1 Table (org.apache.hadoop.hbase.client.Table)1