[ https://issues.apache.org/jira/browse/BEAM-4018?focusedWorklogId=94526&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-94526 ]
ASF GitHub Bot logged work on BEAM-4018: ---------------------------------------- Author: ASF GitHub Bot Created on: 24/Apr/18 08:45 Start Date: 24/Apr/18 08:45 Worklog Time Spent: 10m Work Description: iemejia commented on a change in pull request #5175: [BEAM-4018] Add a ByteKeyRangeTracker based on RestrictionTracker for SDF URL: https://github.com/apache/beam/pull/5175#discussion_r183650380 ########## File path: sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/splittabledofn/ByteKeyRangeTrackerTest.java ########## @@ -0,0 +1,196 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.transforms.splittabledofn; + +import static org.apache.beam.sdk.transforms.splittabledofn.ByteKeyRangeTracker.next; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.beam.sdk.io.range.ByteKey; +import org.apache.beam.sdk.io.range.ByteKeyRange; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link ByteKeyRangeTrackerTest}. */ +@RunWith(JUnit4.class) +public class ByteKeyRangeTrackerTest { + @Rule public final ExpectedException expected = ExpectedException.none(); + + @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))); + } + + @Test + public void testCheckpointUnstarted() throws Exception { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + expected.expect(IllegalStateException.class); + tracker.checkpoint(); + } + + @Test + public void testCheckpointOnlyFailedClaim() throws Exception { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertFalse(tracker.tryClaim(ByteKey.of(0xd0))); + expected.expect(IllegalStateException.class); + tracker.checkpoint(); + } + + @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.checkpoint(); + assertEquals(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0x11)), tracker.currentRestriction()); + assertEquals(ByteKeyRange.of(ByteKey.of(0x11), ByteKey.of(0xc0)), checkpoint); + } + + @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.checkpoint(); + assertEquals(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0x91)), tracker.currentRestriction()); + assertEquals(ByteKeyRange.of(ByteKey.of(0x91), ByteKey.of(0xc0)), checkpoint); + } + + @Test + public void testCheckpointClaimedLast() 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))); + assertTrue(tracker.tryClaim(ByteKey.of(0xbf))); + ByteKeyRange checkpoint = tracker.checkpoint(); + assertEquals(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0)), tracker.currentRestriction()); + assertEquals(ByteKeyRange.of(ByteKey.of(0xc0), ByteKey.of(0xc0)), checkpoint); + } + + @Test + public void testCheckpointAfterFailedClaim() 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))); + assertTrue(tracker.tryClaim(ByteKey.of(0xa0))); + assertFalse(tracker.tryClaim(ByteKey.of(0xd0))); + ByteKeyRange checkpoint = tracker.checkpoint(); + assertEquals(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xa1)), tracker.currentRestriction()); + assertEquals(ByteKeyRange.of(ByteKey.of(0xa1), ByteKey.of(0xc0)), checkpoint); + } + + @Test + public void testNonMonotonicClaim() throws Exception { + expected.expectMessage("Trying to claim key [70] while last attempted was [90]"); + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertTrue(tracker.tryClaim(ByteKey.of(0x50))); + assertTrue(tracker.tryClaim(ByteKey.of(0x90))); + tracker.tryClaim(ByteKey.of(0x70)); + } + + @Test + public void testClaimBeforeStartOfRange() throws Exception { + expected.expectMessage( + "Trying to claim key [05] before start of the range " + + "ByteKeyRange{startKey=[10], endKey=[c0]}"); + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + tracker.tryClaim(ByteKey.of(0x05)); + } + + @Test + public void testCheckDoneAfterTryClaimPastEndOfRange() { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertTrue(tracker.tryClaim(ByteKey.of(0x50))); + assertTrue(tracker.tryClaim(ByteKey.of(0x90))); + assertFalse(tracker.tryClaim(ByteKey.of(0xd0))); + tracker.checkDone(); + } + + @Test + public void testCheckDoneAfterTryClaimAtEndOfRange() { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertTrue(tracker.tryClaim(ByteKey.of(0x50))); + assertTrue(tracker.tryClaim(ByteKey.of(0x90))); + assertFalse(tracker.tryClaim(ByteKey.of(0xc0))); + tracker.checkDone(); + } + + @Test + public void testCheckDoneAfterTryClaimRightBeforeEndOfRange() { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertTrue(tracker.tryClaim(ByteKey.of(0x50))); + assertTrue(tracker.tryClaim(ByteKey.of(0x90))); + assertTrue(tracker.tryClaim(ByteKey.of(0xbf))); + tracker.checkDone(); + } + + @Test + public void testCheckDoneWhenNotDone() { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertTrue(tracker.tryClaim(ByteKey.of(0x50))); + assertTrue(tracker.tryClaim(ByteKey.of(0x90))); + expected.expectMessage( + "Last attempted key was [90] in range ByteKeyRange{startKey=[10], endKey=[c0]}, " + + "claiming work in [[91], [c0]) was not attempted"); + tracker.checkDone(); + } + + @Test + public void testCheckDoneWhenExplicitlyMarkedDone() { + ByteKeyRangeTracker tracker = + ByteKeyRangeTracker.of(ByteKeyRange.of(ByteKey.of(0x10), ByteKey.of(0xc0))); + assertTrue(tracker.tryClaim(ByteKey.of(0x50))); + assertTrue(tracker.tryClaim(ByteKey.of(0x90))); + tracker.markDone(); + tracker.checkDone(); + } + + @Test + public void testNextByteKey() { + assertEquals(next(ByteKey.EMPTY), ByteKey.of(0x00)); + assertEquals(next(ByteKey.of(0x00)), ByteKey.of(0x01)); + assertEquals(next(ByteKey.of(0x9f)), ByteKey.of(0xa0)); + assertEquals(next(ByteKey.of(0xff)), ByteKey.of(0x01, 0x00)); + assertEquals(next(ByteKey.of(0x10, 0x10)), ByteKey.of(0x10, 0x11)); + assertEquals(next(ByteKey.of(0x00, 0xff)), ByteKey.of(0x01, 0x00)); + assertEquals(next(ByteKey.of(0xff, 0xff)), ByteKey.of(0x01, 0x00, 0x00)); + } Review comment: Yes `testCheckpointClaimedLast` produces an empty range and does not fail. And `testTryClaim` covers the second case. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 94526) Time Spent: 6h 50m (was: 6h 40m) > Add a ByteKeyRangeTracker based on RestrictionTracker for SDF > ------------------------------------------------------------- > > Key: BEAM-4018 > URL: https://issues.apache.org/jira/browse/BEAM-4018 > Project: Beam > Issue Type: New Feature > Components: sdk-java-core > Reporter: Ismaël Mejía > Assignee: Ismaël Mejía > Priority: Minor > Time Spent: 6h 50m > Remaining Estimate: 0h > > We can have a RestrictionTracker for ByteKey ranges as part of the core sdk > so it can be reused by future SDF based IOs like Bigtable, HBase among others. -- This message was sent by Atlassian JIRA (v7.6.3#76005)