Imporve stability of elasticsearch tests (#2170)

This commit is contained in:
Lauri Tulmin 2021-02-02 15:46:49 +02:00 committed by GitHub
parent 575a71f316
commit 89fd887c74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.env.Environment
@ -51,6 +52,8 @@ class Elasticsearch53NodeClientTest extends AgentInstrumentationSpecification {
// this may potentially create multiple requests and therefore multiple spans, so we wrap this call
// into a top level trace to get exactly one trace in the result.
testNode.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT)
// disable periodic refresh in InternalClusterInfoService as it creates spans that tests don't expect
testNode.client().admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().transientSettings(["cluster.routing.allocation.disk.threshold_enabled": false]))
}
testWriter.waitForTraces(1)
}

View File

@ -10,6 +10,7 @@ import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
@ -68,6 +69,8 @@ class Elasticsearch53TransportClientTest extends AgentInstrumentationSpecificati
// this may potentially create multiple requests and therefore multiple spans, so we wrap this call
// into a top level trace to get exactly one trace in the result.
client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT)
// disable periodic refresh in InternalClusterInfoService as it creates spans that tests don't expect
client.admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().transientSettings(["cluster.routing.allocation.disk.threshold_enabled": false]))
}
testWriter.waitForTraces(1)
}

View File

@ -5,6 +5,7 @@
package springdata
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.env.Environment
@ -28,8 +29,8 @@ class Config {
return new NodeBuilder()
}
@Bean
ElasticsearchOperations elasticsearchTemplate() {
@Bean(destroyMethod = "close")
Node elasticSearchNode() {
def tmpDir = File.createTempFile("test-es-working-dir-", "")
tmpDir.delete()
tmpDir.mkdir()
@ -53,6 +54,16 @@ class Config {
println "ES work dir: $tmpDir"
return new ElasticsearchTemplate(new Node(new Environment(InternalSettingsPreparer.prepareSettings(settings)), [Netty3Plugin]).start().client())
def testNode = new Node(new Environment(InternalSettingsPreparer.prepareSettings(settings)), [Netty3Plugin])
testNode.start()
// disable periodic refresh in InternalClusterInfoService as it creates spans that tests don't expect
testNode.client().admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().transientSettings(["cluster.routing.allocation.disk.threshold_enabled": false]))
return testNode
}
@Bean
ElasticsearchOperations elasticsearchTemplate(Node node) {
return new ElasticsearchTemplate(node.client())
}
}

View File

@ -23,26 +23,34 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
// To change the timing without adding ugly checks everywhere -
// use a dynamic proxy. There's probably a more "groovy" way to do this.
@Shared
LazyProxyInvoker lazyProxyInvoker = new LazyProxyInvoker()
@Shared
DocRepository repo = Proxy.newProxyInstance(
getClass().getClassLoader(),
[DocRepository] as Class[],
new LazyProxyInvoker())
lazyProxyInvoker)
static class LazyProxyInvoker implements InvocationHandler {
def repo
def applicationContext
DocRepository getOrCreateRepository() {
if (repo != null) {
return repo
}
def applicationContext = new AnnotationConfigApplicationContext(Config)
applicationContext = new AnnotationConfigApplicationContext(Config)
repo = applicationContext.getBean(DocRepository)
return repo
}
void close() {
applicationContext.close()
}
@Override
Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(getOrCreateRepository(), args)
@ -59,6 +67,10 @@ class Elasticsearch53SpringRepositoryTest extends AgentInstrumentationSpecificat
testWriter.clear()
}
def cleanupSpec() {
lazyProxyInvoker.close()
}
def "test empty repo"() {
when:
def result = repo.findAll()

View File

@ -12,6 +12,7 @@ import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import java.util.concurrent.atomic.AtomicLong
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest
import org.elasticsearch.action.search.SearchResponse
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
@ -32,11 +33,6 @@ import spock.lang.Shared
class Elasticsearch53SpringTemplateTest extends AgentInstrumentationSpecification {
public static final long TIMEOUT = 10000 // 10 seconds
// Some ES actions are not caused by clients and seem to just happen from time to time.
// We will just ignore these actions in traces.
// TODO: check if other ES tests need this protection and potentially pull this into global class
public static final Set<String> IGNORED_ACTIONS = ["NodesStatsAction", "IndicesStatsAction"] as Set
@Shared
Node testNode
@Shared
@ -67,6 +63,8 @@ class Elasticsearch53SpringTemplateTest extends AgentInstrumentationSpecificatio
// this may potentially create multiple requests and therefore multiple spans, so we wrap this call
// into a top level trace to get exactly one trace in the result.
testNode.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT)
// disable periodic refresh in InternalClusterInfoService as it creates spans that tests don't expect
testNode.client().admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().transientSettings(["cluster.routing.allocation.disk.threshold_enabled": false]))
}
testWriter.waitForTraces(1)

View File

@ -10,6 +10,7 @@ import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.index.IndexNotFoundException
@ -48,6 +49,8 @@ class Elasticsearch6NodeClientTest extends AgentInstrumentationSpecification {
// this may potentially create multiple requests and therefore multiple spans, so we wrap this call
// into a top level trace to get exactly one trace in the result.
testNode.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT)
// disable periodic refresh in InternalClusterInfoService as it creates spans that tests don't expect
testNode.client().admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().transientSettings(["cluster.routing.allocation.disk.threshold_enabled": false]))
}
testWriter.waitForTraces(1)
}

View File

@ -10,6 +10,7 @@ import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
@ -63,6 +64,8 @@ class Elasticsearch6TransportClientTest extends AgentInstrumentationSpecificatio
// this may potentially create multiple requests and therefore multiple spans, so we wrap this call
// into a top level trace to get exactly one trace in the result.
client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT)
// disable periodic refresh in InternalClusterInfoService as it creates spans that tests don't expect
client.admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().transientSettings(["cluster.routing.allocation.disk.threshold_enabled": false]))
}
testWriter.waitForTraces(1)
}