-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix condition for KubePortManagerSingleton init twice #16213
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48eae83
add test for init ports manager twice
goodseeyou 16f02ed
fix bug for init more then onece
goodseeyou 3a74fa9
formatter
goodseeyou 225a17e
Merge branch 'master' into fixInitTwiceBug
davinchia 858ecb6
Merge branch 'airbytehq:master' into fixInitTwiceBug
goodseeyou 3430b7f
fix typo by pr suggestions
goodseeyou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
@@ -102,14 +103,31 @@ public void setup() throws Exception { | |
final WorkerConfigs workerConfigs = spy(new WorkerConfigs(new EnvConfigs())); | ||
when(workerConfigs.getEnvMap()).thenReturn(Map.of("ENV_VAR_1", "ENV_VALUE_1")); | ||
|
||
processFactory = | ||
new KubeProcessFactory( | ||
workerConfigs, | ||
"default", | ||
fabricClient, | ||
heartbeatUrl, | ||
getHost(), | ||
false); | ||
processFactory = new KubeProcessFactory(workerConfigs, "default", fabricClient, heartbeatUrl, getHost(), false); | ||
} | ||
|
||
@RetryingTest(3) | ||
public void testInitKubePortManagerSingletonTwice() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davinchia what I really add test here. Others code change is coding-style formatter by google-style which recommends (https://github.com/airbytehq/airbyte/blob/master/docs/contributing-to-airbyte/code-style.md) |
||
/** | ||
* Test init KubePortManagerSingleton twice: 1. with same ports shoule succeed 2. with different | ||
goodseeyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* port should fail | ||
* | ||
* Every test has been init firt times in BeforeAll with getOpenPorts(30) | ||
goodseeyou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
KubePortManagerSingleton originalKubePortManager = KubePortManagerSingleton.getInstance(); | ||
|
||
// init the second time with the same ports | ||
final List<Integer> theSameOpenPorts = new ArrayList<>(getOpenPorts(30)); | ||
KubePortManagerSingleton.init(new HashSet<>(theSameOpenPorts.subList(1, theSameOpenPorts.size() - 1))); | ||
assertEquals(originalKubePortManager, KubePortManagerSingleton.getInstance()); | ||
|
||
// init the second time with different ports | ||
final List<Integer> differentOpenPorts = new ArrayList<>(getOpenPorts(32)); | ||
Exception exception = assertThrows(RuntimeException.class, () -> { | ||
KubePortManagerSingleton.init(new HashSet<>(differentOpenPorts.subList(1, differentOpenPorts.size() - 1))); | ||
}); | ||
assertTrue(exception.getMessage().contains("Cannot initialize twice with different ports!")); | ||
} | ||
|
||
/** | ||
|
@@ -283,21 +301,14 @@ public void testDeletingPodImmediatelyAfterCompletion() throws Exception { | |
final var uuid = UUID.randomUUID(); | ||
final Process process = getProcess(Map.of("uuid", uuid.toString()), "sleep 1 && exit 10"); | ||
|
||
final var pod = fabricClient.pods().list().getItems().stream() | ||
.filter(p -> p.getMetadata() != null && p.getMetadata().getLabels() != null) | ||
final var pod = fabricClient.pods().list().getItems().stream().filter(p -> p.getMetadata() != null && p.getMetadata().getLabels() != null) | ||
.filter(p -> p.getMetadata().getLabels().containsKey("uuid") && p.getMetadata().getLabels().get("uuid").equals(uuid.toString())) | ||
.collect(Collectors.toList()).get(0); | ||
final SharedIndexInformer<Pod> podInformer = fabricClient.pods() | ||
.inNamespace(pod.getMetadata().getNamespace()) | ||
.withName(pod.getMetadata().getName()) | ||
.inform(); | ||
podInformer.addEventHandler(new ExitCodeWatcher( | ||
pod.getMetadata().getName(), | ||
pod.getMetadata().getNamespace(), | ||
exitCode -> { | ||
fabricClient.pods().delete(pod); | ||
}, | ||
() -> {})); | ||
final SharedIndexInformer<Pod> podInformer = | ||
fabricClient.pods().inNamespace(pod.getMetadata().getNamespace()).withName(pod.getMetadata().getName()).inform(); | ||
podInformer.addEventHandler(new ExitCodeWatcher(pod.getMetadata().getName(), pod.getMetadata().getNamespace(), exitCode -> { | ||
fabricClient.pods().delete(pod); | ||
}, () -> {})); | ||
|
||
process.waitFor(); | ||
|
||
|
@@ -342,14 +353,7 @@ public void testKillingWithoutHeartbeat() throws Exception { | |
final WorkerConfigs workerConfigs = spy(new WorkerConfigs(new EnvConfigs())); | ||
when(workerConfigs.getEnvMap()).thenReturn(Map.of("ENV_VAR_1", "ENV_VALUE_1")); | ||
|
||
processFactory = | ||
new KubeProcessFactory( | ||
workerConfigs, | ||
"default", | ||
fabricClient, | ||
heartbeatUrl, | ||
getHost(), | ||
false); | ||
processFactory = new KubeProcessFactory(workerConfigs, "default", fabricClient, heartbeatUrl, getHost(), false); | ||
|
||
// start an infinite process | ||
final var availablePortsBefore = KubePortManagerSingleton.getInstance().getNumAvailablePorts(); | ||
|
@@ -412,11 +416,7 @@ private static String getRandomFile(final int lines) { | |
|
||
private Process getProcess(final String entrypoint) throws WorkerException { | ||
// these files aren't used for anything, it's just to check for exceptions when uploading | ||
final var files = ImmutableMap.of( | ||
"file0", "fixed str", | ||
"file1", getRandomFile(1), | ||
"file2", getRandomFile(100), | ||
"file3", getRandomFile(1000)); | ||
final var files = ImmutableMap.of("file0", "fixed str", "file1", getRandomFile(1), "file2", getRandomFile(100), "file3", getRandomFile(1000)); | ||
|
||
return getProcess(entrypoint, files); | ||
} | ||
|
@@ -431,19 +431,8 @@ private Process getProcess(final String entrypoint, final Map<String, String> fi | |
|
||
private Process getProcess(final Map<String, String> customLabels, final String entrypoint, final Map<String, String> files) | ||
throws WorkerException { | ||
return processFactory.create( | ||
"tester", | ||
"some-id", | ||
0, | ||
Path.of("/tmp/job-root"), | ||
"busybox:latest", | ||
false, | ||
files, | ||
entrypoint, | ||
DEFAULT_RESOURCE_REQUIREMENTS, | ||
customLabels, | ||
Collections.emptyMap(), | ||
Collections.emptyMap()); | ||
return processFactory.create("tester", "some-id", 0, Path.of("/tmp/job-root"), "busybox:latest", false, files, entrypoint, | ||
DEFAULT_RESOURCE_REQUIREMENTS, customLabels, Collections.emptyMap(), Collections.emptyMap()); | ||
} | ||
|
||
private static Set<Integer> getOpenPorts(final int count) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is what I really fix