-
Notifications
You must be signed in to change notification settings - Fork 13
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
JPERF-716 background process results #13
Merged
dagguh
merged 7 commits into
atlassian:master
from
dagguh:issue/JPERF-716-background-process-results
Jan 7, 2021
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49573c2
JPERF-715: Test detaching a process
dagguh 2a99d22
JPERF-715: Extract `WaitingCommand` for reuse
dagguh 3cdc0d2
JPERF-716: Add `runInBackground`
dagguh 1ab56f8
JPERF-716: Tolerate background processes finishing early
dagguh 4469ee6
JPERF-716: Deprecate `DetachedProcess` for `BackgroundProcess`
dagguh cdc5f53
Add GitHub Actions
dagguh fc9107f
Trigger GHA on non-master
dagguh 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
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/com/atlassian/performance/tools/ssh/SshjBackgroundProcess.kt
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,54 @@ | ||
package com.atlassian.performance.tools.ssh | ||
|
||
import com.atlassian.performance.tools.ssh.api.BackgroundProcess | ||
import com.atlassian.performance.tools.ssh.api.SshConnection | ||
import net.schmizz.sshj.connection.channel.direct.Session | ||
import org.apache.logging.log4j.Level | ||
import org.apache.logging.log4j.LogManager | ||
import java.time.Duration | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
internal class SshjBackgroundProcess( | ||
private val session: Session, | ||
private val command: Session.Command | ||
) : BackgroundProcess { | ||
|
||
private var closed = AtomicBoolean(false) | ||
|
||
override fun stop(timeout: Duration): SshConnection.SshResult { | ||
tryToInterrupt() | ||
val result = WaitingCommand(command, timeout, Level.DEBUG, Level.DEBUG).waitForResult() | ||
close() | ||
return result | ||
} | ||
|
||
private fun tryToInterrupt() { | ||
try { | ||
sendSigint() | ||
} catch (e: Exception) { | ||
LOG.debug("cannot interrupt, if the command doesn't run anymore, then the write connection is closed", e) | ||
} | ||
} | ||
|
||
/** | ||
* [Session.Command.signal] doesn't work, so send the CTRL-C character rather than SSH-level SIGINT signal. | ||
* [OpenSSH server was not supporting this standard](https://bugzilla.mindrot.org/show_bug.cgi?id=1424). | ||
* It's supported since 7.9p1 (late 2018), but our test Ubuntu still runs on 7.6p1. | ||
*/ | ||
private fun sendSigint() { | ||
val ctrlC = 3 | ||
command.outputStream.write(ctrlC); | ||
command.outputStream.flush(); | ||
} | ||
|
||
override fun close() { | ||
if (!closed.getAndSet(true)) { | ||
command.use {} | ||
session.use {} | ||
} | ||
} | ||
|
||
private companion object { | ||
private val LOG = LogManager.getLogger(this::class.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
79 changes: 79 additions & 0 deletions
79
src/main/kotlin/com/atlassian/performance/tools/ssh/WaitingCommand.kt
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,79 @@ | ||
package com.atlassian.performance.tools.ssh | ||
|
||
import com.atlassian.performance.tools.ssh.api.SshConnection | ||
import net.schmizz.sshj.connection.channel.direct.Session | ||
import org.apache.logging.log4j.Level | ||
import org.apache.logging.log4j.LogManager | ||
import org.apache.logging.log4j.Logger | ||
import java.io.InputStream | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal class WaitingCommand( | ||
private val command: Session.Command, | ||
private val timeout: Duration, | ||
private val stdout: Level, | ||
private val stderr: Level | ||
) { | ||
|
||
fun waitForResult(): SshConnection.SshResult { | ||
command.waitForCompletion(timeout) | ||
return SshConnection.SshResult( | ||
exitStatus = command.exitStatus, | ||
output = command.inputStream.readAndLog(stdout), | ||
errorOutput = command.errorStream.readAndLog(stderr) | ||
) | ||
} | ||
|
||
private fun Session.Command.waitForCompletion( | ||
timeout: Duration | ||
) { | ||
val expectedEnd = Instant.now().plus(timeout) | ||
val extendedTime = timeout.multipliedBy(5).dividedBy(4) | ||
try { | ||
this.join(extendedTime.toMillis(), TimeUnit.MILLISECONDS) | ||
} catch (e: Exception) { | ||
val output = readOutput() | ||
throw Exception("SSH command failed to finish in extended time ($extendedTime): $output", e) | ||
} | ||
val actualEnd = Instant.now() | ||
if (actualEnd.isAfter(expectedEnd)) { | ||
val overtime = Duration.between(expectedEnd, actualEnd) | ||
throw Exception("SSH command exceeded timeout $timeout by $overtime") | ||
} | ||
} | ||
|
||
private fun Session.Command.readOutput(): SshjExecutedCommand { | ||
return try { | ||
this.close() | ||
SshjExecutedCommand( | ||
stdout = this.inputStream.reader().use { it.readText() }, | ||
stderr = this.errorStream.reader().use { it.readText() } | ||
) | ||
} catch (e: Exception) { | ||
LOG.error("Failed do close ssh channel. Can't get command output", e) | ||
SshjExecutedCommand( | ||
stdout = "<couldn't get command stdout>", | ||
stderr = "<couldn't get command stderr>" | ||
) | ||
} | ||
} | ||
|
||
private fun InputStream.readAndLog(level: Level): String { | ||
val output = this.reader().use { it.readText() } | ||
if (output.isNotBlank()) { | ||
LOG.log(level, output) | ||
} | ||
return output | ||
} | ||
|
||
private data class SshjExecutedCommand( | ||
val stdout: String, | ||
val stderr: String | ||
) | ||
|
||
private companion object { | ||
private val LOG: Logger = LogManager.getLogger(this::class.java) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/atlassian/performance/tools/ssh/api/BackgroundProcess.kt
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,21 @@ | ||
package com.atlassian.performance.tools.ssh.api | ||
|
||
import java.time.Duration | ||
|
||
/** | ||
* Runs in the background. Is independent of `SshConnection`s being closed. | ||
* Can be used for commands, which will not stop on their own, e.g. `tail -f`, `ping`, `top`, etc. | ||
* @since 2.4.0 | ||
*/ | ||
interface BackgroundProcess : AutoCloseable { | ||
|
||
/** | ||
* Interrupts the process, then waits up to [timeout] for its completion. | ||
* Skips the interrupt if the process is already finished. | ||
* Throws if getting the [SshConnection.SshResult] fails. | ||
* Closes the open resources. | ||
* | ||
* @return the result of the stopped process, could have a non-zero exit code | ||
*/ | ||
fun stop(timeout: Duration): SshConnection.SshResult | ||
} |
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
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 |
---|---|---|
|
@@ -14,4 +14,4 @@ class SshConnectionTest { | |
Assert.assertEquals(sshResult.output, "test\n") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
I tried to do it in
DetachedProcess
, but none of the approaches seemed to satisfy all requirements. So I wrote the tests, which guard the requirements. Maybe someone will come in the future and will be able to make them work withDetachedProcess
.But for now, I had to resign from the requirement of starting/stopping on different
Session.Command
s.