-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
🐛 Kube: Fix Source Ports not releasing. #4822
Changes from all commits
a656d24
52ead75
0d2a125
4377d68
c56337e
b967b9d
397c32b
36ba448
7616422
47bfedd
44316b9
69a7162
61e4c14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
package io.airbyte.workers; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.airbyte.config.Configs.WorkerEnvironment; | ||
import io.airbyte.config.EnvConfigs; | ||
import io.airbyte.config.ResourceRequirements; | ||
import io.airbyte.config.StandardSyncInput; | ||
|
@@ -41,6 +42,7 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
// TODO:(Issue-4824): Figure out how to log Docker process information. | ||
public class WorkerUtils { | ||
|
||
public static final ResourceRequirements DEFAULT_RESOURCE_REQUIREMENTS = initResourceRequirements(); | ||
|
@@ -52,8 +54,14 @@ public static void gentleClose(final Process process, final long timeout, final | |
return; | ||
} | ||
|
||
if (new EnvConfigs().getWorkerEnvironment().equals(WorkerEnvironment.KUBERNETES)) { | ||
LOGGER.debug("Gently closing process {}", process.info().commandLine().get()); | ||
} | ||
|
||
try { | ||
process.waitFor(timeout, timeUnit); | ||
if (process.isAlive()) { | ||
process.waitFor(timeout, timeUnit); | ||
} | ||
} catch (InterruptedException e) { | ||
LOGGER.error("Exception while while waiting for process to finish", e); | ||
} | ||
|
@@ -103,6 +111,9 @@ static void gentleCloseWithHeartbeat(final Process process, | |
|
||
while (process.isAlive() && heartbeatMonitor.isBeating()) { | ||
try { | ||
if (new EnvConfigs().getWorkerEnvironment().equals(WorkerEnvironment.KUBERNETES)) { | ||
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. DRY this check? |
||
LOGGER.debug("Gently closing process {} with heartbeat..", process.info().commandLine().get()); | ||
} | ||
process.waitFor(checkHeartbeatDuration.toMillis(), TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException e) { | ||
LOGGER.error("Exception while waiting for process to finish", e); | ||
|
@@ -111,6 +122,9 @@ static void gentleCloseWithHeartbeat(final Process process, | |
|
||
if (process.isAlive()) { | ||
try { | ||
if (new EnvConfigs().getWorkerEnvironment().equals(WorkerEnvironment.KUBERNETES)) { | ||
LOGGER.debug("Gently closing process {} without heartbeat..", process.info().commandLine().get()); | ||
} | ||
process.waitFor(gracefulShutdownDuration.toMillis(), TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException e) { | ||
LOGGER.error("Exception during grace period for process to finish. This can happen when cancelling jobs."); | ||
|
@@ -119,6 +133,9 @@ static void gentleCloseWithHeartbeat(final Process process, | |
|
||
// if we were unable to exist gracefully, force shutdown... | ||
if (process.isAlive()) { | ||
if (new EnvConfigs().getWorkerEnvironment().equals(WorkerEnvironment.KUBERNETES)) { | ||
LOGGER.debug("Force shutdown process {}..", process.info().commandLine().get()); | ||
} | ||
forceShutdown.accept(process, forcedShutdownDuration); | ||
} | ||
} | ||
|
@@ -133,7 +150,7 @@ static void forceShutdown(Process process, Duration lastChanceDuration) { | |
LOGGER.error("Exception while while killing the process", e); | ||
} | ||
if (process.isAlive()) { | ||
LOGGER.error("Couldn't kill the process. You might have a zombie ({})", process.info().commandLine()); | ||
LOGGER.error("Couldn't kill the process. You might have a zombie process."); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.ProcessHandle.Info; | ||
import java.net.InetAddress; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
@@ -489,15 +490,11 @@ public InputStream getErrorStream() { | |
*/ | ||
@Override | ||
public int waitFor() throws InterruptedException { | ||
try { | ||
Pod refreshedPod = | ||
fabricClient.pods().inNamespace(podDefinition.getMetadata().getNamespace()).withName(podDefinition.getMetadata().getName()).get(); | ||
fabricClient.resource(refreshedPod).waitUntilCondition(this::isTerminal, 10, TimeUnit.DAYS); | ||
wasKilled.set(true); | ||
return exitValue(); | ||
} finally { | ||
close(); | ||
} | ||
Pod refreshedPod = | ||
fabricClient.pods().inNamespace(podDefinition.getMetadata().getNamespace()).withName(podDefinition.getMetadata().getName()).get(); | ||
fabricClient.resource(refreshedPod).waitUntilCondition(this::isTerminal, 10, TimeUnit.DAYS); | ||
wasKilled.set(true); | ||
return exitValue(); | ||
} | ||
|
||
/** | ||
|
@@ -506,11 +503,7 @@ public int waitFor() throws InterruptedException { | |
*/ | ||
@Override | ||
public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException { | ||
try { | ||
return super.waitFor(timeout, unit); | ||
} finally { | ||
close(); | ||
} | ||
return super.waitFor(timeout, unit); | ||
} | ||
|
||
/** | ||
|
@@ -528,6 +521,11 @@ public void destroy() { | |
} | ||
} | ||
|
||
@Override | ||
public Info info() { | ||
return new KubePodProcessInfo(podDefinition.getMetadata().getName()); | ||
} | ||
|
||
/** | ||
* Close all open resource in the opposite order of resource creation. | ||
*/ | ||
|
@@ -538,8 +536,13 @@ private void close() { | |
Exceptions.swallow(this.stdoutServerSocket::close); | ||
Exceptions.swallow(this.stderrServerSocket::close); | ||
Exceptions.swallow(this.executorService::shutdownNow); | ||
Exceptions.swallow(() -> portReleaser.accept(stdoutLocalPort)); | ||
Exceptions.swallow(() -> portReleaser.accept(stderrLocalPort)); | ||
try { | ||
portReleaser.accept(stdoutLocalPort); | ||
portReleaser.accept(stderrLocalPort); | ||
} catch (Exception e) { | ||
LOGGER.error("Error releasing ports ", e); | ||
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. should this rethrow as runtime? 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. I don't think so. Since we are here, the data is already synced so failing the job doesn't add much. I think logging as an error is the best way since it makes it clear why this doesn't work later on. 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. I made the error message clearer to show this doesn't affect the data being synced, but will affect future syncs if ports leak. |
||
} | ||
LOGGER.debug("Closed {}", podDefinition.getMetadata().getName()); | ||
} | ||
|
||
private boolean isTerminal(Pod pod) { | ||
|
@@ -600,7 +603,17 @@ private int getReturnCode(Pod pod) { | |
|
||
@Override | ||
public int exitValue() { | ||
return getReturnCode(podDefinition); | ||
// getReturnCode throws IllegalThreadException if the Kube pod has not exited; | ||
// close() is only called if the Kube pod has terminated. | ||
var returnCode = getReturnCode(podDefinition); | ||
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. It may be useful to have a comment to explain that Then it wouldn't look like calling 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. good point 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. done |
||
// The OS traditionally handles process resource clean up. Therefore an exit code of 0, also | ||
// indicates that all kernel resources were shut down. | ||
// Because this is a custom implementation, manually close all the resources. | ||
// Further, since the local resources are used to talk to Kubernetes resources, shut local resources | ||
// down after Kubernetes resources are shut down, regardless of Kube termination status. | ||
close(); | ||
LOGGER.info("Closed all resources for pod {}", podDefinition.getMetadata().getName()); | ||
return returnCode; | ||
} | ||
|
||
private static ResourceRequirementsBuilder getResourceRequirementsBuilder(ResourceRequirements resourceRequirements) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Airbyte | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package io.airbyte.workers.process; | ||
|
||
import java.lang.ProcessHandle.Info; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Minimal Process info implementation to assist with debug logging. | ||
* | ||
* Current implement only logs out the Kubernetes pod corresponding to the JVM process. | ||
*/ | ||
public class KubePodProcessInfo implements Info { | ||
|
||
private final String podName; | ||
|
||
public KubePodProcessInfo(String podname) { | ||
this.podName = podname; | ||
} | ||
|
||
@Override | ||
public Optional<String> command() { | ||
return Optional.of(podName); | ||
} | ||
|
||
@Override | ||
public Optional<String> commandLine() { | ||
return Optional.of(podName); | ||
} | ||
|
||
@Override | ||
public Optional<String[]> arguments() { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<Instant> startInstant() { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<Duration> totalCpuDuration() { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<String> user() { | ||
return Optional.empty(); | ||
} | ||
|
||
} |
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.
Why is this check here?
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.
Before this change,
waitFor
was always called, which is why we saw the destination ports being released.With our new change, where
getReturnCode
is handling this, this call is only necessary if the process is alive. It also better encodes the contract where if a process has an exit value, it means all associated resources have been closed.