-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathElasticsearchBulkSender.scala
260 lines (229 loc) · 8.91 KB
/
ElasticsearchBulkSender.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* Copyright (c) 2014-2021 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.stream.loader
package clients
// Java
import com.google.common.base.Charsets
import com.google.common.io.BaseEncoding
import org.slf4j.LoggerFactory
// Scala
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure => SFailure, Success => SSuccess}
import org.elasticsearch.client.RestClient
import com.sksamuel.elastic4s.Index
import com.sksamuel.elastic4s.{ElasticClient, ElasticRequest, Handler, Response}
import com.sksamuel.elastic4s.ElasticDsl.{BulkHandler => _, _}
import com.sksamuel.elastic4s.requests.indexes.IndexRequest
import com.sksamuel.elastic4s.http.{JavaClient, NoOpHttpClientConfigCallback}
import com.sksamuel.elastic4s.requests.bulk.{BulkRequest, BulkResponse}
import com.sksamuel.elastic4s.handlers.bulk.BulkHandlers
import com.sksamuel.exts.Logging
import org.apache.http.{Header, HttpHost}
import org.apache.http.message.BasicHeader
import cats.Id
import cats.effect.{IO, Timer}
import cats.data.Validated
import cats.syntax.validated._
import retry.implicits._
import retry.{RetryDetails, RetryPolicy}
import retry.CatsEffect._
import com.snowplowanalytics.snowplow.scalatracker.Tracker
import com.snowplowanalytics.stream.loader.Config.Region
import com.snowplowanalytics.stream.loader.Config.Sink.GoodSink
import com.snowplowanalytics.stream.loader.Config.Sink.GoodSink.Elasticsearch.ESChunk
/**
* Main ES component responsible for inserting data into a specific index,
* data is passed here by [[Emitter]]
*/
class ElasticsearchBulkSender(
endpoint: String,
port: Int,
ssl: Boolean,
awsSigning: Boolean,
awsSigningRegion: Region,
username: Option[String],
password: Option[String],
documentIndex: String,
documentType: Option[String],
val maxConnectionWaitTimeMs: Long,
val tracker: Option[Tracker[Id]],
val maxAttempts: Int = 6,
chunkConf: ESChunk
) extends BulkSender[EmitterJsonInput] {
require(maxAttempts > 0)
require(maxConnectionWaitTimeMs > 0)
import ElasticsearchBulkSender._
override val log = LoggerFactory.getLogger(getClass)
private val client = {
val httpClientConfigCallback =
if (awsSigning) new SignedHttpClientConfigCallback(awsSigningRegion)
else NoOpHttpClientConfigCallback
val formedHost = new HttpHost(endpoint, port, if (ssl) "https" else "http")
val headers: Array[Header] = (username, password) match {
case (Some(_), Some(_)) =>
val userpass =
BaseEncoding.base64().encode(s"${username.get}:${password.get}".getBytes(Charsets.UTF_8))
Array(new BasicHeader("Authorization", s"Basic $userpass"))
case _ => Array.empty[Header]
}
val restClientBuilder = RestClient
.builder(formedHost)
.setHttpClientConfigCallback(httpClientConfigCallback)
.setDefaultHeaders(headers)
ElasticClient(JavaClient.fromRestClient(restClientBuilder.build()))
}
/**
* This BulkHandler is added to change endpoints of bulk api request to add
* document type to them. This change is made in order to continue to support ES 6.x
*/
implicit object CustomBulkHandler extends Handler[BulkRequest, BulkResponse] with Logging {
override def build(t: BulkRequest): ElasticRequest = {
val req = BulkHandlers.BulkHandler.build(t)
documentType match {
case None => req
case Some(t) => req.copy(endpoint = s"/$documentIndex/$t${req.endpoint}")
}
}
}
// do not close the es client, otherwise it will fail when resharding
override def close(): Unit =
log.info("Closing BulkSender")
override def send(records: List[EmitterJsonInput]): List[EmitterJsonInput] = {
val connectionAttemptStartTime = System.currentTimeMillis()
implicit def onErrorHandler: (Throwable, RetryDetails) => IO[Unit] =
BulkSender.onError(log, tracker, connectionAttemptStartTime)
implicit def retryPolicy: RetryPolicy[IO] =
BulkSender.delayPolicy[IO](maxAttempts, maxConnectionWaitTimeMs)
// oldFailures - failed at the transformation step
val (successes, oldFailures) = records.partition(_._2.isValid)
val esObjects = successes.collect { case (_, Validated.Valid(jsonRecord)) =>
composeObject(jsonRecord)
}
val actions = esObjects.map(composeRequest)
// Sublist of records that could not be inserted
val newFailures: List[EmitterJsonInput] = if (actions.nonEmpty) {
BulkSender
.futureToTask(client.execute(bulk(actions)))
.retryingOnSomeErrors(BulkSender.exPredicate)
.map(extractResult(records))
.attempt
.unsafeRunSync() match {
case Right(s) => s
case Left(f) =>
log.error(
s"Shutting down application as unable to connect to Elasticsearch for over $maxConnectionWaitTimeMs ms",
f
)
// if the request failed more than it should have we force shutdown
forceShutdown()
Nil
}
} else Nil
log.info(s"Emitted ${esObjects.size - newFailures.size} records to Elasticsearch")
if (newFailures.nonEmpty) logHealth()
val allFailures = oldFailures ++ newFailures
if (allFailures.nonEmpty) log.warn(s"Returning ${allFailures.size} records as failed")
allFailures
}
override def chunkConfig(): ESChunk = chunkConf
/**
* Get sublist of records that could not be inserted
* @param records list of original records to send
* @param response response with successful and failed results
*/
def extractResult(
records: List[EmitterJsonInput]
)(response: Response[BulkResponse]): List[EmitterJsonInput] =
response.result.items
.zip(records)
.flatMap { case (bulkResponseItem, record) =>
handleResponse(bulkResponseItem.error.map(_.reason), record)
}
.toList
def composeObject(jsonRecord: JsonRecord): ElasticsearchObject = {
val index = jsonRecord.shard match {
case Some(shardSuffix) => documentIndex + shardSuffix
case None => documentIndex
}
val eventId = utils.extractEventId(jsonRecord.json)
ElasticsearchObject(index, eventId, jsonRecord.json.noSpaces)
}
/** Logs the cluster health */
override def logHealth(): Unit =
client.execute(clusterHealth).onComplete {
case SSuccess(health) =>
health match {
case response =>
response.result.status match {
case "green" => log.info("Cluster health is green")
case "yellow" => log.warn("Cluster health is yellow")
case "red" => log.error("Cluster health is red")
}
}
case SFailure(e) => log.error("Couldn't retrieve cluster health", e)
}
/**
* Handle the response given for a bulk request, by producing a failure if we failed to insert
* a given item.
* @param error possible error
* @param record associated to this item
* @return a failure if an unforeseen error happened (e.g. not that the document already exists)
*/
private def handleResponse(
error: Option[String],
record: EmitterJsonInput
): Option[EmitterJsonInput] = {
error.foreach(e => log.error(s"Record [$record] failed with message $e"))
error
.flatMap { e =>
if (
e.contains("DocumentAlreadyExistsException") || e.contains(
"VersionConflictEngineException"
)
)
None
else
Some(
record._1.take(maxSizeWhenReportingFailure) ->
s"Elasticsearch rejected record with message $e".invalidNel
)
}
}
}
object ElasticsearchBulkSender {
implicit val ioTimer: Timer[IO] =
IO.timer(concurrent.ExecutionContext.global)
def composeRequest(obj: ElasticsearchObject): IndexRequest =
indexInto(Index(obj.index)).id(obj.id.orNull).doc(obj.doc)
def apply(
config: GoodSink.Elasticsearch,
tracker: Option[Tracker[Id]]
): ElasticsearchBulkSender = {
new ElasticsearchBulkSender(
config.client.endpoint,
config.client.port,
config.client.ssl,
config.aws.signing,
config.aws.region,
config.client.username,
config.client.password,
config.cluster.index,
config.cluster.documentType,
config.client.maxTimeout,
tracker,
config.client.maxRetries,
config.chunk
)
}
case class ElasticsearchObject(index: String, id: Option[String], doc: String)
}