added healthcheck command to client
This commit is contained in:
@@ -5,6 +5,7 @@ import net.woggioni.gbcs.cli.impl.GbcsCommand
|
|||||||
import net.woggioni.gbcs.cli.impl.commands.BenchmarkCommand
|
import net.woggioni.gbcs.cli.impl.commands.BenchmarkCommand
|
||||||
import net.woggioni.gbcs.cli.impl.commands.ClientCommand
|
import net.woggioni.gbcs.cli.impl.commands.ClientCommand
|
||||||
import net.woggioni.gbcs.cli.impl.commands.GetCommand
|
import net.woggioni.gbcs.cli.impl.commands.GetCommand
|
||||||
|
import net.woggioni.gbcs.cli.impl.commands.HealthCheckCommand
|
||||||
import net.woggioni.gbcs.cli.impl.commands.PasswordHashCommand
|
import net.woggioni.gbcs.cli.impl.commands.PasswordHashCommand
|
||||||
import net.woggioni.gbcs.cli.impl.commands.PutCommand
|
import net.woggioni.gbcs.cli.impl.commands.PutCommand
|
||||||
import net.woggioni.gbcs.cli.impl.commands.ServerCommand
|
import net.woggioni.gbcs.cli.impl.commands.ServerCommand
|
||||||
@@ -44,6 +45,7 @@ class GradleBuildCacheServerCli : GbcsCommand() {
|
|||||||
addSubcommand(BenchmarkCommand())
|
addSubcommand(BenchmarkCommand())
|
||||||
addSubcommand(PutCommand())
|
addSubcommand(PutCommand())
|
||||||
addSubcommand(GetCommand())
|
addSubcommand(GetCommand())
|
||||||
|
addSubcommand(HealthCheckCommand())
|
||||||
})
|
})
|
||||||
System.exit(commandLine.execute(*args))
|
System.exit(commandLine.execute(*args))
|
||||||
}
|
}
|
||||||
|
@@ -46,7 +46,7 @@ class BenchmarkCommand : GbcsCommand() {
|
|||||||
clientCommand.configuration.profiles[profileName]
|
clientCommand.configuration.profiles[profileName]
|
||||||
?: throw IllegalArgumentException("Profile $profileName does not exist in configuration")
|
?: throw IllegalArgumentException("Profile $profileName does not exist in configuration")
|
||||||
}
|
}
|
||||||
val client = GradleBuildCacheClient(profile)
|
GradleBuildCacheClient(profile).use { client ->
|
||||||
|
|
||||||
val entryGenerator = sequence {
|
val entryGenerator = sequence {
|
||||||
val random = Random(SecureRandom.getInstance("NativePRNGNonBlocking").nextLong())
|
val random = Random(SecureRandom.getInstance("NativePRNGNonBlocking").nextLong())
|
||||||
@@ -133,3 +133,4 @@ class BenchmarkCommand : GbcsCommand() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
@@ -0,0 +1,45 @@
|
|||||||
|
package net.woggioni.gbcs.cli.impl.commands
|
||||||
|
|
||||||
|
import net.woggioni.gbcs.cli.impl.GbcsCommand
|
||||||
|
import net.woggioni.gbcs.client.GradleBuildCacheClient
|
||||||
|
import net.woggioni.gbcs.common.contextLogger
|
||||||
|
import picocli.CommandLine
|
||||||
|
import java.security.SecureRandom
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
|
@CommandLine.Command(
|
||||||
|
name = "health",
|
||||||
|
description = ["Check server health"],
|
||||||
|
showDefaultValues = true
|
||||||
|
)
|
||||||
|
class HealthCheckCommand : GbcsCommand() {
|
||||||
|
private val log = contextLogger()
|
||||||
|
|
||||||
|
@CommandLine.Spec
|
||||||
|
private lateinit var spec: CommandLine.Model.CommandSpec
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
val clientCommand = spec.parent().userObject() as ClientCommand
|
||||||
|
val profile = clientCommand.profileName.let { profileName ->
|
||||||
|
clientCommand.configuration.profiles[profileName]
|
||||||
|
?: throw IllegalArgumentException("Profile $profileName does not exist in configuration")
|
||||||
|
}
|
||||||
|
GradleBuildCacheClient(profile).use { client ->
|
||||||
|
val random = Random(SecureRandom.getInstance("NativePRNGNonBlocking").nextLong())
|
||||||
|
val nonce = ByteArray(0xa0)
|
||||||
|
random.nextBytes(nonce)
|
||||||
|
client.healthCheck(nonce).thenApply { value ->
|
||||||
|
if(value == null) {
|
||||||
|
throw IllegalStateException("Empty response from server")
|
||||||
|
}
|
||||||
|
for(i in 0 until nonce.size) {
|
||||||
|
for(j in value.size - nonce.size until nonce.size) {
|
||||||
|
if(nonce[i] != value[j]) {
|
||||||
|
throw IllegalStateException("Server nonce does not match")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -213,6 +213,25 @@ class GradleBuildCacheClient(private val profile: Configuration.Profile) : AutoC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun healthCheck(nonce: ByteArray): CompletableFuture<ByteArray?> {
|
||||||
|
return executeWithRetry {
|
||||||
|
sendRequest(profile.serverURI, HttpMethod.TRACE, nonce)
|
||||||
|
}.thenApply {
|
||||||
|
val status = it.status()
|
||||||
|
if (it.status() != HttpResponseStatus.OK) {
|
||||||
|
throw HttpException(status)
|
||||||
|
} else {
|
||||||
|
it.content()
|
||||||
|
}
|
||||||
|
}.thenApply { maybeByteBuf ->
|
||||||
|
maybeByteBuf?.let {
|
||||||
|
val result = ByteArray(it.readableBytes())
|
||||||
|
it.getBytes(0, result)
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun get(key: String): CompletableFuture<ByteArray?> {
|
fun get(key: String): CompletableFuture<ByteArray?> {
|
||||||
return executeWithRetry {
|
return executeWithRetry {
|
||||||
sendRequest(profile.serverURI.resolve(key), HttpMethod.GET, null)
|
sendRequest(profile.serverURI.resolve(key), HttpMethod.GET, null)
|
||||||
|
Reference in New Issue
Block a user