Skip to content

Commit

Permalink
Add timeout parameter support
Browse files Browse the repository at this point in the history
Convert timeout values to milliseconds if less than 10 seconds.
UsenkoArtem authored and anti-social committed Dec 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent dd7f060 commit 741d5fd
Showing 6 changed files with 107 additions and 6 deletions.
4 changes: 4 additions & 0 deletions elasticmagic/api/elasticmagic.api
Original file line number Diff line number Diff line change
@@ -4793,6 +4793,10 @@ public final class dev/evo/elasticmagic/types/ValueSerializationException : java
public synthetic fun <init> (Ljava/lang/Object;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class dev/evo/elasticmagic/util/HelperKt {
public static final fun toTimeoutString-LRDsOJo (J)Ljava/lang/String;
}

public final class dev/evo/elasticmagic/util/OrderedMap {
public fun <init> ()V
public fun <init> ([Lkotlin/Pair;)V
Original file line number Diff line number Diff line change
@@ -2,27 +2,35 @@ package dev.evo.elasticmagic

import dev.evo.elasticmagic.bulk.Action
import dev.evo.elasticmagic.compile.ActionCompiler
import dev.evo.elasticmagic.compile.PreparedBulk
import dev.evo.elasticmagic.compile.CompilerSet
import dev.evo.elasticmagic.compile.PreparedBulk
import dev.evo.elasticmagic.compile.PreparedCreateIndex
import dev.evo.elasticmagic.compile.PreparedUpdateMapping
import dev.evo.elasticmagic.doc.BaseDocSource
import dev.evo.elasticmagic.doc.Document
import dev.evo.elasticmagic.serde.Serde
import dev.evo.elasticmagic.transport.ApiRequest
import dev.evo.elasticmagic.transport.BulkRequest
import dev.evo.elasticmagic.transport.ElasticsearchException
import dev.evo.elasticmagic.transport.ElasticsearchTransport
import dev.evo.elasticmagic.transport.ApiRequest
import dev.evo.elasticmagic.transport.Method
import dev.evo.elasticmagic.transport.Parameters
import dev.evo.elasticmagic.util.toTimeoutString
import kotlinx.coroutines.CompletableDeferred
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
import kotlinx.coroutines.CompletableDeferred

internal fun Params.toRequestParameters(): Parameters {
val entries = this.map { (key, value) ->
key to when (value) {
is ToValue<*> -> value.toValue()
is Duration -> {
if (key == "timeout")
value.toTimeoutString()
else value
}

else -> value
}
}
@@ -42,7 +50,7 @@ class ElasticsearchCluster(
transport: ElasticsearchTransport,
serde: Serde.OneLineJson,
compilers: CompilerSet? = null,
): this(
) : this(
transport,
apiSerde = serde,
bulkSerde = serde,
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ import dev.evo.elasticmagic.transport.ApiRequest
import dev.evo.elasticmagic.transport.BulkRequest
import dev.evo.elasticmagic.transport.Method
import dev.evo.elasticmagic.transport.Parameters
import dev.evo.elasticmagic.util.toTimeoutString

abstract class BaseSearchQueryCompiler(
features: ElasticsearchFeatures,
@@ -91,12 +92,15 @@ abstract class BaseSearchQueryCompiler(
is ObjExpression -> ctx.obj {
visit(this, value)
}

is ArrayExpression -> {
visit(ctx, value)
}

is ToValue<*> -> {
ctx.value(value.toValue())
}

else -> super.dispatch(ctx, value)
}
}
@@ -106,12 +110,15 @@ abstract class BaseSearchQueryCompiler(
is ObjExpression -> ctx.obj(name) {
visit(this, value)
}

is ArrayExpression -> ctx.array(name) {
visit(this, value)
}

is ToValue<*> -> {
ctx.field(name, value.toValue())
}

else -> super.dispatch(ctx, name, value)
}
}
@@ -131,7 +138,7 @@ open class SearchQueryCompiler(
}

if (searchQuery.timeout != null) {
ctx.field("timeout", searchQuery.timeout.inWholeSeconds.toString() + "s")
ctx.field("timeout", searchQuery.timeout.toTimeoutString())
}

if (searchQuery.rescores.isNotEmpty()) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.evo.elasticmagic.util

import kotlin.time.Duration

private const val USE_MILLISECONDS_WHILE_SECONDS_LESS_THAN = 10

fun Duration.toTimeoutString() = if (inWholeSeconds > USE_MILLISECONDS_WHILE_SECONDS_LESS_THAN) {
"${inWholeSeconds}s"
} else {
"${inWholeMilliseconds}ms"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.evo.elasticmagic

import dev.evo.elasticmagic.compile.BaseCompilerTest
import dev.evo.elasticmagic.compile.SearchQueryCompiler
import dev.evo.elasticmagic.doc.Document
import io.kotest.matchers.maps.shouldContainExactly
import io.kotest.matchers.shouldBe
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime

@OptIn(ExperimentalTime::class)
class SearchQueryTimeoutTests : BaseCompilerTest<SearchQueryCompiler>(::SearchQueryCompiler) {
@Test
fun timeoutInSearchQuery() = testWithCompiler {
val userDoc = object : Document() {
val login by keyword()
val isActive by boolean()
}

val sq1 = SearchQuery()
.filter(userDoc.login.eq("root"))
.setTimeout(4.seconds)

sq1.prepareSearch().let {
it.size shouldBe null
it.filters.size shouldBe 1
it.timeout shouldBe 4.seconds
}

compile(sq1).body shouldBe mapOf(
"query" to mapOf(
"bool" to mapOf(
"filter" to listOf(
mapOf("term" to mapOf("login" to "root"))
)
)
),
"timeout" to "4000ms"
)
}

@Test
fun timeoutInParams() = testWithCompiler {
val userDoc = object : Document() {
val login by keyword()
val isActive by boolean()
}

val sq1 = SearchQuery(params = Params("timeout" to 4.seconds))
.filter(userDoc.login.eq("root"))


sq1.prepareSearch().let {
it.size shouldBe null
it.filters.size shouldBe 1
it.timeout shouldBe null
}
val compiled = compile(sq1)
compiled.body shouldBe mapOf(
"query" to mapOf(
"bool" to mapOf(
"filter" to listOf(
mapOf("term" to mapOf("login" to "root"))
)
)
)
)
compiled.params shouldContainExactly mapOf("timeout" to listOf("4000ms"))
}
}
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ class SearchQueryCompilerTests : BaseCompilerTest<SearchQueryCompiler>(::SearchQ
)
)
),
"timeout" to "10s"
"timeout" to "10000ms"
)
}

0 comments on commit 741d5fd

Please sign in to comment.