jillesvangurp / kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.
MIT License
100 stars 22 forks source link

[BUG] `java.lang.ArrayIndexOutOfBoundsException` at `at com.jillesvangurp.ktsearch.RoundRobinNodeSelector.selectNode(RoundRobinNodeSelector.kt:6)` #23

Closed vdimchev closed 1 year ago

vdimchev commented 1 year ago

Describe the bug

When querying an AWS hosted OpenSearch 2.3 service with 3 nodes we get the following exception:

java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1
    at com.jillesvangurp.ktsearch.RoundRobinNodeSelector.selectNode(RoundRobinNodeSelector.kt:6)
    at com.jillesvangurp.ktsearch.KtorRestClient.nextNode(KtorRestClient.kt:45)
    at com.jillesvangurp.ktsearch.KtorRestClient.doRequest(KtorRestClient.kt:56)
    at com.jillesvangurp.ktsearch.RestClient$DefaultImpls.doRequest$default(Restclient.kt:18)
    at com.jillesvangurp.ktsearch.Request_dslKt.post(request-dsl.kt:65)
    at com.jillesvangurp.ktsearch.Search_apiKt.search-yk69jQ8(search-api.kt:266)
    at com.jillesvangurp.ktsearch.Search_apiKt.search-yk69jQ8(search-api.kt:166)
    at com.jillesvangurp.ktsearch.Search_apiKt.search-mdzbyIQ(search-api.kt:70)
    at com.jillesvangurp.ktsearch.Search_apiKt.search-mdzbyIQ$default(search-api.kt:21)
    at com.jillesvangurp.ktsearch.repository.IndexRepository.search(IndexRepository.kt:279)
    at com.transportapi.placesSearch.search.PlaceIndex$query$1.invokeSuspend(PlaceIndex.kt:320)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284)
    at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
    at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
    at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)

To Reproduce

The error is triggered by a call to the search()

Expected behavior

The node selection should not fail.

Your context

com.jillesvangurp:search-client:1.99.13 AWS hosted OpenSearch 2.3

Also, on subsequent calls it seems the index is just kept increasing and the exceptions looks like:

java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 1

 

Will you be able to help with a pull request?

A guard condition fix looks trivial but without deeper knowledge of the library structure I'm not 100% sure this is the correct approach. If you are happy with it we can open a PR.

yassenb commented 1 year ago

Looks like a concurrency issue. A more general question - is the library meant to be thread-safe?

jillesvangurp commented 1 year ago

merged and fixed. Thanks!