-
Notifications
You must be signed in to change notification settings - Fork 38
/
KinesisS3Emitter.scala
256 lines (225 loc) · 8.39 KB
/
KinesisS3Emitter.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
/*
* 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.s3.loader.connector
import java.io.ByteArrayInputStream
import java.time.{Instant, LocalDateTime}
import java.time.format.DateTimeFormatter
import scala.annotation.tailrec
import scala.jdk.CollectionConverters._
import org.slf4j.LoggerFactory
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.{ObjectMetadata, PutObjectRequest}
import com.amazonaws.services.kinesis.connectors.UnmodifiableBuffer
import com.amazonaws.services.kinesis.connectors.interfaces.IEmitter
import cats.implicits._
import io.circe.syntax._
import com.snowplowanalytics.snowplow.badrows.BadRow.GenericError
import com.snowplowanalytics.s3.loader.{DynamicPath, KinesisSink, Result}
import com.snowplowanalytics.s3.loader.monitoring.{Monitoring, SnowplowTracking}
import com.snowplowanalytics.s3.loader.Config.{Output, Purpose, S3Output}
import com.snowplowanalytics.s3.loader.serializers.ISerializer
import com.snowplowanalytics.s3.loader.connector.KinesisS3Emitter._
import com.snowplowanalytics.s3.loader.processing.{Common, RowType}
/**
* Emitter for flushing Kinesis event data to S3.
*
* Once the buffer is full, the emit function is called.
*/
class KinesisS3Emitter(client: AmazonS3,
monitoring: Monitoring,
purpose: Purpose,
output: Output,
badSink: KinesisSink,
serializer: ISerializer)
extends IEmitter[Result] {
/**
* Reads items from a buffer and saves them to s3.
*
* This method is expected to return a List of items that
* failed to be written out to S3, which will be sent to
* a Kinesis stream for bad events.
*
* @param buffer BasicMemoryBuffer containing EmitterInputs
* @return list of inputs which failed transformation
*/
def emit(buffer: UnmodifiableBuffer[Result]): java.util.List[Result] = {
logger.info(s"Flushing buffer with ${buffer.getRecords.size} records.")
val records = buffer.getRecords.asScala.toList
val partitionedBatch =
Common.partition(purpose, monitoring.isStatsDEnabled, records)
val getBase: Option[String] => String =
getBaseFilename(output.s3, buffer.getFirstSequenceNumber, buffer.getLastSequenceNumber)
val afterEmit: () => Unit =
() => monitoring.report(partitionedBatch.meta)
partitionedBatch.data.flatMap {
case (RowType.Unpartitioned, partitionRecords) if partitionRecords.nonEmpty =>
emitRecords(partitionRecords, afterEmit, getBase(None))
.map(_.asLeft)
case (data: RowType.SelfDescribing, partitionRecords) if partitionRecords.nonEmpty =>
emitRecords(partitionRecords, afterEmit, getBase(Some(data.partition))).map(_.asLeft)
case _ =>
records // ReadingError or empty partition - should be handled later by serializer
}.asJava
}
/**
* Closes the client when the KinesisConnectorRecordProcessor is shut down
*/
def shutdown(): Unit =
client.shutdown()
/**
* Sends records which fail deserialization or compression
* to Kinesis with an error message
*
* @param records List of failed records to send to Kinesis
*/
def fail(records: java.util.List[Result]): Unit =
for (Left(record) <- records.asScala) {
logger.warn(s"Record failed: ${record.payload.event}")
logger.info("Sending failed record to Kinesis")
badSink.store(record.asJson.noSpaces, None)
}
/**
* Keep attempting to send the data to S3 until it succeeds
*
* @param bucket where data will be written
* @param stream stream of rows with filename
* @param now connection attempt start time
* @param callback a procedure to execute after successful emit, e.g. reporting
* @return success status of sending to S3
*/
def attemptEmit(
stream: ISerializer.NamedStream,
now: Long,
callback: () => Unit
): Unit = {
def logAndSleep(attempt: Int, e: Throwable): Unit = {
logger.error(
s"An exception during putting ${stream.filename} object to S3, attempt $attempt",
e
)
monitoring.captureError(e)
monitoring.viaSnowplow { t =>
SnowplowTracking.sendFailureEvent(t, BackoffPeriod, attempt, now, e.toString)
}
sleep(BackoffPeriod)
}
@tailrec
def go(attempt: Int): Unit =
if (attempt > 1 && System.currentTimeMillis() - now > output.s3.maxTimeout)
forceShutdown()
else {
val request = getRequest(output.s3.bucketName, stream, now)
Either.catchNonFatal(client.putObject(request)) match {
case Right(_) => callback()
case Left(error) =>
logAndSleep(attempt, error)
go(attempt + 1)
}
}
go(1)
}
/**
* Attempt to serialize record into a gz/lzo file and submit them to S3 via emitter
* @param records buffered raw records
* @param baseFilename final filename
* @return list of records that could not be emitted
*/
def emitRecords(
records: List[Result],
callback: () => Unit,
baseFilename: String
): List[GenericError] = {
val serializationResults = serializer.serialize(records, baseFilename)
val (failures, successes) = serializationResults.results.separate
val successSize = successes.size
logger.debug(
s"Successfully serialized $successSize records out of ${successSize + failures.size}"
)
if (successSize > 0)
serializationResults.namedStreams.foreach { stream =>
attemptEmit(stream, System.currentTimeMillis(), callback)
}
failures
}
/**
* Terminate the application
*
* Prevents shutdown hooks from running
*/
private def forceShutdown(): Unit = {
logger.error(s"Shutting down application as unable to connect to S3")
monitoring.viaSnowplow { t =>
SnowplowTracking.trackApplicationShutdown(t)
sleep(5000)
}
Runtime.getRuntime.halt(1)
}
}
object KinesisS3Emitter {
/**
* The amount of time to wait in between unsuccessful index requests (in milliseconds).
* 10 seconds = 10 * 1000 = 10000
*/
private val BackoffPeriod = 10000L
private val logger = LoggerFactory.getLogger(getClass)
/**
* Build a final S3 PutObject request with S3 full path, content and metadata
* @param bucket S3 bucket name
* @param stream name information and content
* @param now initial connection attempt start time
*/
def getRequest(
bucket: String,
stream: ISerializer.NamedStream,
now: Long
): PutObjectRequest = {
val outputStream = stream.stream
val key = DynamicPath.decorateDirectoryWithTime(stream.filename, Instant.ofEpochMilli(now))
val inputStream = new ByteArrayInputStream(outputStream.toByteArray)
val objMeta = new ObjectMetadata()
objMeta.setContentLength(outputStream.size.toLong)
new PutObjectRequest(bucket, key, inputStream, objMeta)
}
/**
* Determines the filename in S3, which is the corresponding
* Kinesis sequence range of records in the file.
*/
def getBaseFilename(
s3Config: S3Output,
firstSeq: String,
lastSeq: String
)(
partition: Option[String]
): String = {
val path = List(s3Config.outputDirectory, s3Config.dateFormat).flatten.mkString("/")
val fileName = (s3Config.filenamePrefix.toList ++ partition.toList ++ List(
LocalDateTime.now.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss")
),
firstSeq,
lastSeq
)).mkString("-")
val fullPath = List(path, fileName).filterNot(_.isEmpty).mkString("/")
DynamicPath.normalize(fullPath)
}
/**
* Period between retrying sending events to S3
*
* @param sleepTime Length of time between tries
*/
private def sleep(sleepTime: Long): Unit =
try Thread.sleep(sleepTime)
catch {
case _: InterruptedException => ()
}
}