forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-20261][connector source] Enumerators do not assign splits to u…
…nregistered (failed) readers.
- Loading branch information
1 parent
1cca05f
commit 75599bc
Showing
6 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...test/java/org/apache/flink/connector/file/src/impl/ContinuousFileSplitEnumeratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* 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.flink.connector.file.src.impl; | ||
|
||
import org.apache.flink.api.connector.source.SplitEnumeratorContext; | ||
import org.apache.flink.connector.file.src.FileSourceSplit; | ||
import org.apache.flink.connector.file.src.assigners.SimpleSplitAssigner; | ||
import org.apache.flink.connector.file.src.enumerate.FileEnumerator; | ||
import org.apache.flink.connector.file.src.testutils.TestingFileEnumerator; | ||
import org.apache.flink.connector.testutils.source.reader.TestingSplitEnumeratorContext; | ||
import org.apache.flink.core.fs.Path; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
|
||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Unit tests for the {@link ContinuousFileSplitEnumerator}. | ||
*/ | ||
public class ContinuousFileSplitEnumeratorTest { | ||
|
||
// this is no JUnit temporary folder, because we don't create actual files, we just | ||
// need some random file path. | ||
private static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir")); | ||
|
||
private static long splitId = 1L; | ||
|
||
@Test | ||
public void testDiscoverSplitWhenNoReaderRegistered() throws Exception { | ||
final TestingFileEnumerator fileEnumerator = new TestingFileEnumerator(); | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final ContinuousFileSplitEnumerator enumerator = createEnumerator(fileEnumerator, context); | ||
|
||
// make one split available and trigger the periodic discovery | ||
final FileSourceSplit split = createRandomSplit(); | ||
fileEnumerator.addSplits(split); | ||
context.triggerAllActions(); | ||
|
||
assertThat(enumerator.snapshotState().getSplits(), contains(split)); | ||
} | ||
|
||
@Test | ||
public void testDiscoverWhenReaderRegistered() throws Exception { | ||
final TestingFileEnumerator fileEnumerator = new TestingFileEnumerator(); | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final ContinuousFileSplitEnumerator enumerator = createEnumerator(fileEnumerator, context); | ||
|
||
// register one reader, and let it request a split | ||
context.registerReader(2, "localhost"); | ||
enumerator.addReader(2); | ||
enumerator.handleSplitRequest(2, "localhost"); | ||
|
||
// make one split available and trigger the periodic discovery | ||
final FileSourceSplit split = createRandomSplit(); | ||
fileEnumerator.addSplits(split); | ||
context.triggerAllActions(); | ||
|
||
assertThat(enumerator.snapshotState().getSplits(), empty()); | ||
assertThat(context.getSplitAssignments().get(2).getAssignedSplits(), contains(split)); | ||
} | ||
|
||
@Test | ||
public void testRequestingReaderUnavailableWhenSplitDiscovered() throws Exception { | ||
final TestingFileEnumerator fileEnumerator = new TestingFileEnumerator(); | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final ContinuousFileSplitEnumerator enumerator = createEnumerator(fileEnumerator, context); | ||
|
||
// register one reader, and let it request a split | ||
context.registerReader(2, "localhost"); | ||
enumerator.addReader(2); | ||
enumerator.handleSplitRequest(2, "localhost"); | ||
|
||
// remove the reader (like in a failure) | ||
context.registeredReaders().remove(2); | ||
|
||
// make one split available and trigger the periodic discovery | ||
final FileSourceSplit split = createRandomSplit(); | ||
fileEnumerator.addSplits(split); | ||
context.triggerAllActions(); | ||
|
||
assertFalse(context.getSplitAssignments().containsKey(2)); | ||
assertThat(enumerator.snapshotState().getSplits(), contains(split)); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
// test setup helpers | ||
// ------------------------------------------------------------------------ | ||
|
||
private static FileSourceSplit createRandomSplit() { | ||
return new FileSourceSplit( | ||
String.valueOf(splitId++), | ||
Path.fromLocalFile(new File(TMP_DIR, "foo")), | ||
0L, | ||
0L); | ||
} | ||
|
||
private static ContinuousFileSplitEnumerator createEnumerator( | ||
final FileEnumerator fileEnumerator, | ||
final SplitEnumeratorContext<FileSourceSplit> context) { | ||
|
||
final ContinuousFileSplitEnumerator enumerator = new ContinuousFileSplitEnumerator( | ||
context, | ||
fileEnumerator, | ||
new SimpleSplitAssigner(Collections.emptyList()), | ||
new Path[] { Path.fromLocalFile(TMP_DIR) }, | ||
Collections.emptySet(), | ||
10L); | ||
enumerator.start(); | ||
return enumerator; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...src/test/java/org/apache/flink/connector/file/src/impl/StaticFileSplitEnumeratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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.flink.connector.file.src.impl; | ||
|
||
import org.apache.flink.api.connector.source.SplitEnumeratorContext; | ||
import org.apache.flink.connector.file.src.FileSourceSplit; | ||
import org.apache.flink.connector.file.src.PendingSplitsCheckpoint; | ||
import org.apache.flink.connector.file.src.assigners.SimpleSplitAssigner; | ||
import org.apache.flink.connector.testutils.source.reader.TestingSplitEnumeratorContext; | ||
import org.apache.flink.core.fs.Path; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
|
||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Unit tests for the {@link ContinuousFileSplitEnumerator}. | ||
*/ | ||
public class StaticFileSplitEnumeratorTest { | ||
|
||
// this is no JUnit temporary folder, because we don't create actual files, we just | ||
// need some random file path. | ||
private static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir")); | ||
|
||
private static long splitId = 1L; | ||
|
||
@Test | ||
public void testCheckpointNoSplitRequested() throws Exception { | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final FileSourceSplit split = createRandomSplit(); | ||
final StaticFileSplitEnumerator enumerator = createEnumerator(context, split); | ||
|
||
final PendingSplitsCheckpoint<FileSourceSplit> checkpoint = enumerator.snapshotState(); | ||
|
||
assertThat(checkpoint.getSplits(), contains(split)); | ||
} | ||
|
||
@Test | ||
public void testSplitRequestForRegisteredReader() throws Exception { | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final FileSourceSplit split = createRandomSplit(); | ||
final StaticFileSplitEnumerator enumerator = createEnumerator(context, split); | ||
|
||
context.registerReader(3, "somehost"); | ||
enumerator.addReader(3); | ||
enumerator.handleSplitRequest(3, "somehost"); | ||
|
||
assertThat(enumerator.snapshotState().getSplits(), empty()); | ||
assertThat(context.getSplitAssignments().get(3).getAssignedSplits(), contains(split)); | ||
} | ||
|
||
@Test | ||
public void testSplitRequestForNonRegisteredReader() throws Exception { | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final FileSourceSplit split = createRandomSplit(); | ||
final StaticFileSplitEnumerator enumerator = createEnumerator(context, split); | ||
|
||
enumerator.handleSplitRequest(3, "somehost"); | ||
|
||
assertFalse(context.getSplitAssignments().containsKey(3)); | ||
assertThat(enumerator.snapshotState().getSplits(), contains(split)); | ||
} | ||
|
||
@Test | ||
public void testNoMoreSplits() throws Exception { | ||
final TestingSplitEnumeratorContext<FileSourceSplit> context = new TestingSplitEnumeratorContext<>(4); | ||
final FileSourceSplit split = createRandomSplit(); | ||
final StaticFileSplitEnumerator enumerator = createEnumerator(context, split); | ||
|
||
// first split assignment | ||
context.registerReader(1, "somehost"); | ||
enumerator.addReader(1); | ||
enumerator.handleSplitRequest(1, "somehost"); | ||
|
||
// second request has no more split | ||
enumerator.handleSplitRequest(1, "somehost"); | ||
|
||
assertThat(context.getSplitAssignments().get(1).getAssignedSplits(), contains(split)); | ||
assertTrue(context.getSplitAssignments().get(1).hasReceivedNoMoreSplitsSignal()); | ||
} | ||
|
||
// ------------------------------------------------------------------------ | ||
// test setup helpers | ||
// ------------------------------------------------------------------------ | ||
|
||
private static FileSourceSplit createRandomSplit() { | ||
return new FileSourceSplit( | ||
String.valueOf(splitId++), | ||
Path.fromLocalFile(new File(TMP_DIR, "foo")), | ||
0L, | ||
0L); | ||
} | ||
|
||
private static StaticFileSplitEnumerator createEnumerator( | ||
final SplitEnumeratorContext<FileSourceSplit> context, | ||
final FileSourceSplit... splits) { | ||
|
||
return new StaticFileSplitEnumerator(context, new SimpleSplitAssigner(Arrays.asList(splits))); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...es/src/test/java/org/apache/flink/connector/file/src/testutils/TestingFileEnumerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.flink.connector.file.src.testutils; | ||
|
||
import org.apache.flink.connector.file.src.FileSourceSplit; | ||
import org.apache.flink.connector.file.src.enumerate.FileEnumerator; | ||
import org.apache.flink.core.fs.Path; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
/** | ||
* A {@link FileEnumerator} where splits are manually added during tests. | ||
*/ | ||
public class TestingFileEnumerator implements FileEnumerator { | ||
|
||
private final ArrayDeque<FileSourceSplit> splits = new ArrayDeque<>(); | ||
|
||
public TestingFileEnumerator(FileSourceSplit... initialSplits) { | ||
addSplits(initialSplits); | ||
} | ||
|
||
@Override | ||
public Collection<FileSourceSplit> enumerateSplits(Path[] paths, int minDesiredSplits) throws IOException { | ||
synchronized (splits) { | ||
final ArrayList<FileSourceSplit> currentSplits = new ArrayList<>(splits); | ||
splits.clear(); | ||
return currentSplits; | ||
} | ||
} | ||
|
||
public void addSplits(FileSourceSplit... newSplits) { | ||
synchronized (splits) { | ||
splits.addAll(Arrays.asList(newSplits)); | ||
} | ||
} | ||
} |
Oops, something went wrong.