Skip to content

Commit 5a5cb8c

Browse files
Magesh Chandramouliashishagg
authored andcommitted
Making elasticsearchwriter have no dependency on es availability at startup (#234)
* making elasticsearch client lazy so there is no dependency on startup * making elasticsearch client lazy so there is no dependency on startup
1 parent f70b4c0 commit 5a5cb8c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

indexer/src/main/scala/com/expedia/www/haystack/trace/indexer/writers/es/ElasticSearchWriter.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ElasticSearchWriter(esConfig: ElasticSearchConfiguration, indexConf: White
7070
private val inflightRequestsSemaphore = new Semaphore(esConfig.maxInFlightBulkRequests, true)
7171

7272
// initialize the elastic search client
73-
private val esClient: JestClient = {
73+
private lazy val esClient: JestClient = {
7474
LOGGER.info("Initializing the http elastic search client with endpoint={}", esConfig.endpoint)
7575

7676
val factory = new JestClientFactory()
@@ -85,13 +85,21 @@ class ElasticSearchWriter(esConfig: ElasticSearchConfiguration, indexConf: White
8585
}
8686

8787
factory.setHttpClientConfig(builder.build())
88-
factory.getObject
88+
val client = factory.getObject
89+
90+
if (esConfig.indexTemplateJson.isDefined) {
91+
val putTemplateRequest = new PutTemplate.Builder("spans-index-template", esConfig.indexTemplateJson.get).build()
92+
val result = client.execute(putTemplateRequest)
93+
if (!result.isSucceeded) {
94+
throw new RuntimeException(s"Fail to apply the following template to elastic search with reason=${result.getErrorMessage}")
95+
}
96+
}
97+
98+
client
8999
}
90100

91101
private val bulkBuilder = new ThreadSafeBulkBuilder(esConfig.maxDocsInBulk, esConfig.maxBulkDocSizeInBytes)
92102

93-
if (esConfig.indexTemplateJson.isDefined) applyTemplate(esConfig.indexTemplateJson.get)
94-
95103
override def close(): Unit = {
96104
LOGGER.info("Closing the elastic search client now.")
97105
Try(esClient.shutdownClient())
@@ -152,12 +160,4 @@ class ElasticSearchWriter(esConfig: ElasticSearchConfiguration, indexConf: White
152160
None
153161
}
154162
}
155-
156-
private def applyTemplate(templateJson: String) {
157-
val putTemplateRequest = new PutTemplate.Builder("spans-index-template", templateJson).build()
158-
val result = esClient.execute(putTemplateRequest)
159-
if (!result.isSucceeded) {
160-
throw new RuntimeException(s"Fail to apply the following template to elastic search with reason=${result.getErrorMessage}")
161-
}
162-
}
163163
}

0 commit comments

Comments
 (0)