-
Notifications
You must be signed in to change notification settings - Fork 32
/
GooglePubSubSink.scala
265 lines (239 loc) · 10.1 KB
/
GooglePubSubSink.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
261
262
263
264
265
/*
* Copyright (c) 2013-2023 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.snowplow.collectors.scalastream
package sinks
import java.util.concurrent.Executors
import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util._
import scala.concurrent.duration.{FiniteDuration, MILLISECONDS}
import scala.concurrent.duration._
import org.threeten.bp.Duration
import com.google.api.core.{ApiFutureCallback, ApiFutures}
import com.google.api.gax.batching.BatchingSettings
import com.google.api.gax.retrying.RetrySettings
import com.google.api.gax.core.{CredentialsProvider, NoCredentialsProvider}
import com.google.api.gax.grpc.GrpcTransportChannel
import com.google.api.gax.rpc.{
ApiException,
FixedHeaderProvider,
FixedTransportChannelProvider,
TransportChannelProvider
}
import com.google.cloud.pubsub.v1.{Publisher, TopicAdminClient, TopicAdminSettings}
import com.google.pubsub.v1.{ProjectName, ProjectTopicName, PubsubMessage, TopicName}
import com.google.protobuf.ByteString
import io.grpc.ManagedChannelBuilder
import cats.syntax.either._
import com.snowplowanalytics.snowplow.collectors.scalastream.model._
class GooglePubSubSink private (
val maxBytes: Int,
publisher: Publisher,
projectId: String,
topicName: String,
retryInterval: FiniteDuration
) extends Sink {
private val logExecutor = Executors.newSingleThreadExecutor()
// 2 = 1 for health check + 1 for retrying failed inserts
private val scheduledExecutor = Executors.newScheduledThreadPool(2)
private val failedInsertsBuffer = ListBuffer.empty[Array[Byte]]
@volatile private var pubsubHealthy: Boolean = false
override def isHealthy: Boolean = pubsubHealthy
override def storeRawEvents(events: List[Array[Byte]], key: String): Unit =
if (events.nonEmpty) {
log.info(s"Writing ${events.size} records to PubSub topic $topicName")
events.foreach { event =>
publisher.asRight.map { p =>
val future = p.publish(eventToPubsubMessage(event))
ApiFutures.addCallback(
future,
new ApiFutureCallback[String]() {
override def onSuccess(messageId: String): Unit = {
pubsubHealthy = true
log.debug(s"Successfully published event with id $messageId to $topicName")
}
override def onFailure(throwable: Throwable): Unit = {
pubsubHealthy = false
throwable match {
case apiEx: ApiException =>
val retryable = if (apiEx.isRetryable()) "retryable" else "non-retryable"
log.error(
s"Publishing message to $topicName failed with code ${apiEx.getStatusCode} and $retryable error: ${apiEx.getMessage}"
)
case t => log.error(s"Publishing message to $topicName failed with error: ${t.getMessage}")
}
failedInsertsBuffer.synchronized {
failedInsertsBuffer.prepend(event)
}
}
},
logExecutor
)
}
}
}
override def shutdown(): Unit = {
publisher.shutdown()
scheduledExecutor.shutdown()
scheduledExecutor.awaitTermination(10000, MILLISECONDS)
()
}
/**
* Convert event bytes to a PubsubMessage to be published
* @param event Event to be converted
* @return a PubsubMessage
*/
private def eventToPubsubMessage(event: Array[Byte]): PubsubMessage =
PubsubMessage.newBuilder.setData(ByteString.copyFrom(event)).build()
private def retryRunnable: Runnable = new Runnable {
override def run() {
val failedInserts = failedInsertsBuffer.synchronized {
val records = failedInsertsBuffer.toList
failedInsertsBuffer.clear()
records
}
if (failedInserts.nonEmpty) {
log.info(s"Retrying to insert ${failedInserts.size} records into $topicName")
storeRawEvents(failedInserts, "NOT USED")
}
}
}
scheduledExecutor.scheduleWithFixedDelay(retryRunnable, retryInterval.toMillis, retryInterval.toMillis, MILLISECONDS)
private def checkPubsubHealth(
customProviders: Option[(TransportChannelProvider, CredentialsProvider)],
startupCheckInterval: FiniteDuration
): Unit = {
val healthRunnable = new Runnable {
override def run() {
val topicAdmin = GooglePubSubSink.createTopicAdmin(customProviders)
while (!pubsubHealthy) {
GooglePubSubSink.topicExists(topicAdmin, projectId, topicName) match {
case Right(true) =>
log.info(s"Topic $topicName exists")
pubsubHealthy = true
case Right(false) =>
log.error(s"Topic $topicName doesn't exist")
Thread.sleep(startupCheckInterval.toMillis)
case Left(err) =>
log.error(s"Error while checking if topic $topicName exists: ${err.getCause()}")
Thread.sleep(startupCheckInterval.toMillis)
}
}
Either.catchNonFatal(topicAdmin.close()) match {
case Right(_) =>
case Left(err) =>
log.error(s"Error when closing topicAdmin: ${err.getMessage()}")
}
}
}
scheduledExecutor.execute(healthRunnable)
}
}
/** GooglePubSubSink companion object with factory method */
object GooglePubSubSink {
def createAndInitialize(
maxBytes: Int,
googlePubSubConfig: GooglePubSub,
bufferConfig: BufferConfig,
topicName: String
): Either[Throwable, GooglePubSubSink] =
for {
batching <- batchingSettings(bufferConfig).asRight
retry = retrySettings(googlePubSubConfig.backoffPolicy)
customProviders = sys.env.get("PUBSUB_EMULATOR_HOST").map { hostPort =>
val channel = ManagedChannelBuilder.forTarget(hostPort).usePlaintext().build()
val channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel))
val credentialsProvider = NoCredentialsProvider.create()
(channelProvider, credentialsProvider)
}
publisher <- createPublisher(googlePubSubConfig.googleProjectId, topicName, batching, retry, customProviders)
sink = new GooglePubSubSink(
maxBytes,
publisher,
googlePubSubConfig.googleProjectId,
topicName,
googlePubSubConfig.retryInterval
)
_ = sink.checkPubsubHealth(customProviders, googlePubSubConfig.startupCheckInterval)
} yield sink
private val UserAgent = s"snowplow/stream-collector-${generated.BuildInfo.version}"
/**
* Instantiates a Publisher on a topic with the given configuration options.
* This can fail if the publisher can't be created.
* @return a PubSub publisher or an error
*/
private def createPublisher(
projectId: String,
topicName: String,
batchingSettings: BatchingSettings,
retrySettings: RetrySettings,
customProviders: Option[(TransportChannelProvider, CredentialsProvider)]
): Either[Throwable, Publisher] = {
val builder = Publisher
.newBuilder(ProjectTopicName.of(projectId, topicName))
.setBatchingSettings(batchingSettings)
.setRetrySettings(retrySettings)
.setHeaderProvider(FixedHeaderProvider.create("User-Agent", UserAgent))
customProviders.foreach {
case (channelProvider, credentialsProvider) =>
builder.setChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider)
}
Either.catchNonFatal(builder.build()).leftMap(e => new RuntimeException("Couldn't build PubSub publisher", e))
}
private def batchingSettings(bufferConfig: BufferConfig): BatchingSettings =
BatchingSettings
.newBuilder()
.setElementCountThreshold(bufferConfig.recordLimit)
.setRequestByteThreshold(bufferConfig.byteLimit)
.setDelayThreshold(Duration.ofMillis(bufferConfig.timeLimit))
.build()
/** Defaults are used for the rpc configuration, see Publisher.java */
private def retrySettings(backoffPolicy: GooglePubSubBackoffPolicyConfig): RetrySettings =
RetrySettings
.newBuilder()
.setInitialRetryDelay(Duration.ofMillis(backoffPolicy.minBackoff))
.setMaxRetryDelay(Duration.ofMillis(backoffPolicy.maxBackoff))
.setRetryDelayMultiplier(backoffPolicy.multiplier)
.setTotalTimeout(Duration.ofMillis(backoffPolicy.totalBackoff))
.setInitialRpcTimeout(Duration.ofMillis(backoffPolicy.initialRpcTimeout))
.setRpcTimeoutMultiplier(backoffPolicy.rpcTimeoutMultiplier)
.setMaxRpcTimeout(Duration.ofMillis(backoffPolicy.maxRpcTimeout))
.build()
private def createTopicAdmin(
customProviders: Option[(TransportChannelProvider, CredentialsProvider)]
): TopicAdminClient =
customProviders match {
case Some((channelProvider, credentialsProvider)) =>
TopicAdminClient.create(
TopicAdminSettings
.newBuilder()
.setTransportChannelProvider(channelProvider)
.setCredentialsProvider(credentialsProvider)
.build()
)
case None =>
TopicAdminClient.create()
}
private def topicExists(
topicAdmin: TopicAdminClient,
projectId: String,
topicName: String
): Either[Throwable, Boolean] =
Either
.catchNonFatal(topicAdmin.listTopics(ProjectName.of(projectId)))
.leftMap(new RuntimeException(s"Can't list topics", _))
.map(_.iterateAll.asScala.toList.map(_.getName()))
.flatMap { topics =>
topics.contains(TopicName.of(projectId, topicName).toString()).asRight
}
}