/
KafkaManagerActor.scala
544 lines (461 loc) · 24.9 KB
/
KafkaManagerActor.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
* Copyright 2015 Yahoo Inc. Licensed under the Apache License, Version 2.0
* See accompanying LICENSE file.
*/
package kafka.manager.actor
import java.util.Properties
import java.util.concurrent.{LinkedBlockingQueue, ThreadPoolExecutor, TimeUnit}
import akka.actor.{ActorPath, Props}
import akka.pattern._
import kafka.manager.actor.cluster.{ClusterManagerActor, ClusterManagerActorConfig}
import kafka.manager.base.{LongRunningPoolConfig, BaseZkPath, CuratorAwareActor, BaseQueryCommandActor}
import kafka.manager.model.{ClusterTuning, ClusterConfig, CuratorConfig}
import kafka.manager.model.ActorModel.CMShutdown
import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode
import org.apache.curator.framework.recipes.cache.{PathChildrenCache, PathChildrenCacheEvent, PathChildrenCacheListener}
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex
import org.apache.zookeeper.CreateMode
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}
/**
* @author hiral
*/
object KafkaManagerActor {
val ZkRoot : String = "/kafka-manager"
def getClusterPath(config: ClusterConfig) : String = s"$ZkRoot/${config.name}"
}
import kafka.manager.model.ActorModel._
import scala.collection.JavaConverters._
import scala.concurrent.duration._
case class KafkaManagerActorConfig(curatorConfig: CuratorConfig
, baseZkPath : String = KafkaManagerActor.ZkRoot
, pinnedDispatcherName : String = "pinned-dispatcher"
, startDelayMillis: Long = 1000
, threadPoolSize: Int = 2
, mutexTimeoutMillis: Int = 4000
, maxQueueSize: Int = 100
, kafkaManagerUpdatePeriod: FiniteDuration = 10 seconds
, deleteClusterUpdatePeriod: FiniteDuration = 10 seconds
, deletionBatchSize : Int = 2
, clusterActorsAskTimeoutMillis: Int = 2000
, simpleConsumerSocketTimeoutMillis : Int = 10000
, defaultTuning: ClusterTuning
, consumerProperties: Option[Properties]
)
class KafkaManagerActor(kafkaManagerConfig: KafkaManagerActorConfig)
extends BaseQueryCommandActor with CuratorAwareActor with BaseZkPath {
//this is for baze zk path trait
override def baseZkPath : String = kafkaManagerConfig.baseZkPath
//this is for curator aware actor
override def curatorConfig: CuratorConfig = kafkaManagerConfig.curatorConfig
private[this] val baseClusterZkPath = zkPath("clusters")
private[this] val configsZkPath = zkPath("configs")
private[this] val deleteClustersZkPath = zkPath("deleteClusters")
log.info(s"zk=${kafkaManagerConfig.curatorConfig.zkConnect}")
log.info(s"baseZkPath=$baseZkPath")
//create kafka manager base path
Try(curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(baseZkPath))
require(curator.checkExists().forPath(baseZkPath) != null,s"Kafka manager base path not found : $baseZkPath")
//create kafka manager base clusters path
Try(curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(baseClusterZkPath))
require(curator.checkExists().forPath(baseClusterZkPath) != null,s"Kafka manager base clusters path not found : $baseClusterZkPath")
//create kafka manager delete clusters path
Try(curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(deleteClustersZkPath))
require(curator.checkExists().forPath(deleteClustersZkPath) != null,s"Kafka manager delete clusters path not found : $deleteClustersZkPath")
//create kafka manager configs path
Try(curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(configsZkPath))
require(curator.checkExists().forPath(configsZkPath) != null,s"Kafka manager configs path not found : $configsZkPath")
private[this] val longRunningExecutor = new ThreadPoolExecutor(
kafkaManagerConfig.threadPoolSize,
kafkaManagerConfig.threadPoolSize,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue[Runnable](kafkaManagerConfig.maxQueueSize))
private[this] val longRunningExecutionContext = ExecutionContext.fromExecutor(longRunningExecutor)
private[this] val kafkaManagerPathCache = new PathChildrenCache(curator,configsZkPath,true)
private[this] val mutex = new InterProcessSemaphoreMutex(curator, zkPath("mutex"))
private[this] val dcProps = {
val dcConfig = DeleteClusterActorConfig(
curator,
deleteClustersZkPath,
baseClusterZkPath,
configsZkPath,
kafkaManagerConfig.deleteClusterUpdatePeriod,
kafkaManagerConfig.deletionBatchSize)
Props(classOf[DeleteClusterActor],dcConfig)
}
private[this] val deleteClustersActor: ActorPath = context.actorOf(dcProps.withDispatcher(kafkaManagerConfig.pinnedDispatcherName),"delete-cluster").path
private[this] val deleteClustersPathCache = new PathChildrenCache(curator,deleteClustersZkPath,true)
private[this] val pathCacheListener = new PathChildrenCacheListener {
override def childEvent(client: CuratorFramework, event: PathChildrenCacheEvent): Unit = {
log.debug(s"Got event : ${event.getType} path=${Option(event.getData).map(_.getPath)}")
event.getType match {
case PathChildrenCacheEvent.Type.CONNECTION_RECONNECTED =>
self ! KMUpdateState
self ! KMPruneClusters
case PathChildrenCacheEvent.Type.CHILD_ADDED |
PathChildrenCacheEvent.Type.CHILD_UPDATED =>
self ! KMUpdateState
case PathChildrenCacheEvent.Type.CHILD_REMOVED =>
self ! KMPruneClusters
case _ => //don't care
}
}
}
private[this] var lastUpdateMillis: Long = 0L
private[this] var clusterManagerMap : Map[String,ActorPath] = Map.empty
private[this] var clusterConfigMap : Map[String,ClusterConfig] = Map.empty
private[this] var pendingClusterConfigMap : Map[String,ClusterConfig] = Map.empty
private[this] def modify(fn: => Any) : Unit = {
if(longRunningExecutor.getQueue.remainingCapacity() == 0) {
Future.successful(KMCommandResult(Try(throw new UnsupportedOperationException("Long running executor blocking queue is full!"))))
} else {
implicit val ec = longRunningExecutionContext
Future {
try {
log.debug(s"Acquiring kafka manager mutex...")
mutex.acquire(kafkaManagerConfig.mutexTimeoutMillis,TimeUnit.MILLISECONDS)
KMCommandResult(Try {
fn
})
} finally {
if(mutex.isAcquiredInThisProcess) {
log.debug(s"Releasing kafka manger mutex...")
mutex.release()
}
}
} pipeTo sender()
}
}
@scala.throws[Exception](classOf[Exception])
override def preStart() = {
super.preStart()
import scala.concurrent.duration._
log.info("Started actor %s".format(self.path))
log.info("Starting delete clusters path cache...")
deleteClustersPathCache.start(StartMode.BUILD_INITIAL_CACHE)
log.info("Starting kafka manager path cache...")
kafkaManagerPathCache.start(StartMode.BUILD_INITIAL_CACHE)
log.info("Adding kafka manager path cache listener...")
kafkaManagerPathCache.getListenable.addListener(pathCacheListener)
implicit val ec = longRunningExecutionContext
//schedule periodic forced update
context.system.scheduler.schedule(
Duration(kafkaManagerConfig.startDelayMillis,TimeUnit.MILLISECONDS),kafkaManagerConfig.kafkaManagerUpdatePeriod) {
self ! KMUpdateState
}
}
@scala.throws[Exception](classOf[Exception])
override def preRestart(reason: Throwable, message: Option[Any]) {
log.error(reason, "Restarting due to [{}] when processing [{}]",
reason.getMessage, message.getOrElse(""))
super.preRestart(reason, message)
}
@scala.throws[Exception](classOf[Exception])
override def postStop(): Unit = {
log.info("Stopped actor %s".format(self.path))
log.info("Removing kafka manager path cache listener...")
Try(kafkaManagerPathCache.getListenable.removeListener(pathCacheListener))
log.info("Shutting down long running executor...")
Try(longRunningExecutor.shutdown())
log.info("Shutting down kafka manager path cache...")
Try(kafkaManagerPathCache.close())
log.info("Shutting down delete clusters path cache...")
Try(deleteClustersPathCache.close())
super.postStop()
}
override def processActorResponse(response: ActorResponse): Unit = {
response match {
case any: Any => log.warning("kma : processActorResponse : Received unknown message: {}", any)
}
}
override def processQueryRequest(request: QueryRequest): Unit = {
request match {
case KMGetActiveClusters =>
sender ! KMQueryResult(clusterConfigMap.values.filter(_.enabled).toIndexedSeq)
case KMGetAllClusters =>
sender ! KMClusterList(clusterConfigMap.values.toIndexedSeq, pendingClusterConfigMap.values.toIndexedSeq)
case KMGetClusterConfig(name) =>
sender ! KMClusterConfigResult(Try {
val cc = clusterConfigMap.get(name)
require(cc.isDefined, s"Unknown cluster : $name")
cc.get
})
case KMClusterQueryRequest(clusterName, request) =>
clusterManagerMap.get(clusterName).fold[Unit] {
sender ! ActorErrorResponse(s"Unknown cluster : $clusterName")
} {
clusterManagerPath:ActorPath =>
context.actorSelection(clusterManagerPath).forward(request)
}
case any: Any => log.warning("kma : processQueryRequest : Received unknown message: {}", any)
}
}
override def processCommandRequest(request: CommandRequest): Unit = {
request match {
case KMAddCluster(clusterConfig) =>
modify {
val data: Array[Byte] = ClusterConfig.serialize(clusterConfig)
val zkpath: String = getConfigsZkPath(clusterConfig)
require(!(clusterConfig.displaySizeEnabled && !clusterConfig.jmxEnabled),
"Display topic and broker size can only be enabled when JMX is enabled")
require(!(clusterConfig.filterConsumers && !clusterConfig.pollConsumers),
"Filter consumers can only be enabled when consumer polling is enabled")
require(kafkaManagerPathCache.getCurrentData(zkpath) == null,
s"Cluster already exists : ${clusterConfig.name}")
require(deleteClustersPathCache.getCurrentData(getDeleteClusterZkPath(clusterConfig.name)) == null,
s"Cluster is marked for deletion : ${clusterConfig.name}")
log.debug(s"Creating new config node $zkpath")
curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(zkpath, data)
}
case KMUpdateCluster(clusterConfig) =>
modify {
val data: Array[Byte] = ClusterConfig.serialize(clusterConfig)
val zkpath: String = getConfigsZkPath(clusterConfig)
require(!(clusterConfig.displaySizeEnabled && !clusterConfig.jmxEnabled),
"Display topic and broker size can only be enabled when JMX is enabled")
require(!(clusterConfig.filterConsumers && !clusterConfig.pollConsumers),
"Filter consumers can only be enabled when consumer polling is enabled")
require(deleteClustersPathCache.getCurrentData(getDeleteClusterZkPath(clusterConfig.name)) == null,
s"Cluster is marked for deletion : ${clusterConfig.name}")
require(kafkaManagerPathCache.getCurrentData(zkpath) != null,
s"Cannot update non-existing cluster : ${clusterConfig.name}")
curator.setData().forPath(zkpath, data)
}
case KMDisableCluster(clusterName) =>
modify {
val existingConfigOption = clusterConfigMap.get(clusterName)
require(existingConfigOption.isDefined, s"Cannot disable non-existing cluster : $clusterName")
require(deleteClustersPathCache.getCurrentData(getDeleteClusterZkPath(clusterName)) == null,
s"Cluster is marked for deletion : $clusterName")
for {
existingConfig <- existingConfigOption
} yield {
val disabledConfig = existingConfig.copy(enabled = false)
val data: Array[Byte] = ClusterConfig.serialize(disabledConfig)
val zkpath = getConfigsZkPath(existingConfig)
require(kafkaManagerPathCache.getCurrentData(zkpath) != null,
s"Cannot disable non-existing cluster : $clusterName")
curator.setData().forPath(zkpath, data)
}
}
case KMEnableCluster(clusterName) =>
modify {
val existingManagerOption = clusterManagerMap.get(clusterName)
require(existingManagerOption.isEmpty, s"Cannot enable already enabled cluster : $clusterName")
val existingConfigOption = clusterConfigMap.get(clusterName)
require(existingConfigOption.isDefined, s"Cannot enable non-existing cluster : $clusterName")
require(deleteClustersPathCache.getCurrentData(getDeleteClusterZkPath(clusterName)) == null,
s"Cluster is marked for deletion : $clusterName")
for {
existingConfig <- existingConfigOption
} yield {
val enabledConfig = existingConfig.copy(enabled = true)
val data: Array[Byte] = ClusterConfig.serialize(enabledConfig)
val zkpath = getConfigsZkPath(existingConfig)
require(kafkaManagerPathCache.getCurrentData(zkpath) != null,
s"Cannot enable non-existing cluster : $clusterName")
curator.setData().forPath(zkpath, data)
}
}
case KMDeleteCluster(clusterName) =>
modify {
val existingManagerOption = clusterManagerMap.get(clusterName)
require(existingManagerOption.isEmpty, s"Cannot delete enabled cluster : $clusterName")
val existingConfigOption = clusterConfigMap.get(clusterName)
require(existingConfigOption.isDefined, s"Cannot delete non-existing cluster : $clusterName")
require(existingConfigOption.exists(!_.enabled), s"Cannot delete enabled cluster : $clusterName")
for {
existingConfig <- existingConfigOption
} yield {
val zkpath = getConfigsZkPath(existingConfig)
require(kafkaManagerPathCache.getCurrentData(zkpath) != null,
s"Cannot delete non-existing cluster : $clusterName")
//mark for deletion
val deleteZkPath = getDeleteClusterZkPath(existingConfig.name)
curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(deleteZkPath)
}
}
case KMClusterCommandRequest(clusterName, request) =>
clusterManagerMap.get(clusterName).fold[Unit] {
sender ! ActorErrorResponse(s"Unknown cluster : $clusterName")
} {
clusterManagerPath:ActorPath =>
context.actorSelection(clusterManagerPath).forward(request)
}
case KMUpdateState =>
updateState()
case KMPruneClusters =>
pruneClusters()
case KMShutdown =>
log.info(s"Shutting down kafka manager")
context.children.foreach(context.stop)
shutdown = true
case any: Any => log.warning("kma : processCommandRequest : Received unknown message: {}", any)
}
}
private[this] def getDeleteClusterZkPath(clusterName: String) : String = {
zkPathFrom(deleteClustersZkPath,clusterName)
}
private[this] def getConfigsZkPath(clusterConfig: ClusterConfig) : String = {
zkPathFrom(configsZkPath,clusterConfig.name)
}
private[this] def getClusterZkPath(clusterConfig: ClusterConfig) : String = {
zkPathFrom(baseClusterZkPath,clusterConfig.name)
}
private[this] def markPendingClusterManager(clusterConfig: ClusterConfig) : Unit = {
implicit val ec = context.system.dispatcher
log.info(s"Mark pending cluster manager $clusterConfig")
pendingClusterConfigMap += (clusterConfig.name -> clusterConfig)
}
private[this] def removeClusterManager(clusterConfig: ClusterConfig) : Unit = {
implicit val ec = context.system.dispatcher
clusterManagerMap.get(clusterConfig.name).foreach { actorPath =>
log.info(s"Removing cluster manager $clusterConfig")
val selection = context.actorSelection(actorPath)
selection.tell(CMShutdown,self)
//this is non-blocking
selection.resolveOne(1 seconds).foreach( ref => context.stop(ref) )
}
clusterManagerMap -= clusterConfig.name
clusterConfigMap -= clusterConfig.name
}
private[this] def getConfigWithDefaults(config: ClusterConfig, kmConfig: KafkaManagerActorConfig) : ClusterConfig = {
val brokerViewUpdatePeriodSeconds = config.tuning.flatMap(_.brokerViewUpdatePeriodSeconds) orElse kmConfig.defaultTuning.brokerViewUpdatePeriodSeconds
val clusterManagerThreadPoolSize = config.tuning.flatMap(_.clusterManagerThreadPoolSize) orElse kmConfig.defaultTuning.clusterManagerThreadPoolSize
val clusterManagerThreadPoolQueueSize = config.tuning.flatMap(_.clusterManagerThreadPoolQueueSize) orElse kmConfig.defaultTuning.clusterManagerThreadPoolQueueSize
val kafkaCommandThreadPoolSize = config.tuning.flatMap(_.kafkaCommandThreadPoolSize) orElse kmConfig.defaultTuning.kafkaCommandThreadPoolSize
val kafkaCommandThreadPoolQueueSize = config.tuning.flatMap(_.kafkaCommandThreadPoolQueueSize) orElse kmConfig.defaultTuning.kafkaCommandThreadPoolQueueSize
val logkafkaCommandThreadPoolSize = config.tuning.flatMap(_.logkafkaCommandThreadPoolSize) orElse kmConfig.defaultTuning.logkafkaCommandThreadPoolSize
val logkafkaCommandThreadPoolQueueSize = config.tuning.flatMap(_.logkafkaCommandThreadPoolQueueSize) orElse kmConfig.defaultTuning.logkafkaCommandThreadPoolQueueSize
val logkafkaUpdatePeriodSeconds = config.tuning.flatMap(_.logkafkaUpdatePeriodSeconds) orElse kmConfig.defaultTuning.brokerViewUpdatePeriodSeconds
val partitionOffsetCacheTimeoutSecs = config.tuning.flatMap(_.partitionOffsetCacheTimeoutSecs) orElse kmConfig.defaultTuning.partitionOffsetCacheTimeoutSecs
val brokerViewThreadPoolSize = config.tuning.flatMap(_.brokerViewThreadPoolSize) orElse kmConfig.defaultTuning.brokerViewThreadPoolSize
val brokerViewThreadPoolQueueSize = config.tuning.flatMap(_.brokerViewThreadPoolQueueSize) orElse kmConfig.defaultTuning.brokerViewThreadPoolQueueSize
val offsetCacheThreadPoolSize = config.tuning.flatMap(_.offsetCacheThreadPoolSize) orElse kmConfig.defaultTuning.offsetCacheThreadPoolSize
val offsetCacheThreadPoolQueueSize = config.tuning.flatMap(_.offsetCacheThreadPoolQueueSize) orElse kmConfig.defaultTuning.offsetCacheThreadPoolQueueSize
val kafkaAdminClientThreadPoolSize = config.tuning.flatMap(_.kafkaAdminClientThreadPoolSize) orElse kmConfig.defaultTuning.kafkaAdminClientThreadPoolSize
val kafkaAdminClientThreadPoolQueueSize = config.tuning.flatMap(_.kafkaAdminClientThreadPoolQueueSize) orElse kmConfig.defaultTuning.kafkaAdminClientThreadPoolQueueSize
val kafkaManagedOffsetMetadataCheckMillis = config.tuning.flatMap(_.kafkaManagedOffsetMetadataCheckMillis) orElse kmConfig.defaultTuning.kafkaManagedOffsetMetadataCheckMillis
val kafkaManagedOffsetGroupCacheSize = config.tuning.flatMap(_.kafkaManagedOffsetGroupCacheSize) orElse kmConfig.defaultTuning.kafkaManagedOffsetGroupCacheSize
val kafkaManagedOffsetGroupExpireDays = config.tuning.flatMap(_.kafkaManagedOffsetGroupExpireDays) orElse kmConfig.defaultTuning.kafkaManagedOffsetGroupExpireDays
val tuning = Option(
ClusterTuning(
brokerViewUpdatePeriodSeconds = brokerViewUpdatePeriodSeconds
, clusterManagerThreadPoolSize = clusterManagerThreadPoolSize
, clusterManagerThreadPoolQueueSize = clusterManagerThreadPoolQueueSize
, kafkaCommandThreadPoolSize = kafkaCommandThreadPoolSize
, kafkaCommandThreadPoolQueueSize = kafkaCommandThreadPoolQueueSize
, logkafkaCommandThreadPoolSize = logkafkaCommandThreadPoolSize
, logkafkaCommandThreadPoolQueueSize = logkafkaCommandThreadPoolQueueSize
, logkafkaUpdatePeriodSeconds = logkafkaUpdatePeriodSeconds
, partitionOffsetCacheTimeoutSecs = partitionOffsetCacheTimeoutSecs
, brokerViewThreadPoolSize = brokerViewThreadPoolSize
, brokerViewThreadPoolQueueSize = brokerViewThreadPoolQueueSize
, offsetCacheThreadPoolSize = offsetCacheThreadPoolSize
, offsetCacheThreadPoolQueueSize = offsetCacheThreadPoolQueueSize
, kafkaAdminClientThreadPoolSize = kafkaAdminClientThreadPoolSize
, kafkaAdminClientThreadPoolQueueSize = kafkaAdminClientThreadPoolQueueSize
, kafkaManagedOffsetMetadataCheckMillis = kafkaManagedOffsetMetadataCheckMillis
, kafkaManagedOffsetGroupCacheSize = kafkaManagedOffsetGroupCacheSize
, kafkaManagedOffsetGroupExpireDays = kafkaManagedOffsetGroupExpireDays
)
)
config.copy(
tuning = tuning
)
}
private[this] def addCluster(config: ClusterConfig): Try[Boolean] = {
Try {
if(!config.enabled) {
log.info("Not adding cluster manager for disabled cluster : {}", config.name)
clusterConfigMap += (config.name -> config)
pendingClusterConfigMap -= config.name
false
} else {
log.info("Adding new cluster manager for cluster : {}", config.name)
val clusterManagerConfig = ClusterManagerActorConfig(
kafkaManagerConfig.pinnedDispatcherName
, getClusterZkPath(config)
, kafkaManagerConfig.curatorConfig
, config
, askTimeoutMillis = kafkaManagerConfig.clusterActorsAskTimeoutMillis
, mutexTimeoutMillis = kafkaManagerConfig.mutexTimeoutMillis
, simpleConsumerSocketTimeoutMillis = kafkaManagerConfig.simpleConsumerSocketTimeoutMillis
, consumerProperties = kafkaManagerConfig.consumerProperties
)
val props = Props(classOf[ClusterManagerActor], clusterManagerConfig)
val newClusterManager = context.actorOf(props, config.name).path
clusterConfigMap += (config.name -> config)
clusterManagerMap += (config.name -> newClusterManager)
pendingClusterConfigMap -= config.name
true
}
}
}
private[this] def updateCluster(currentConfig: ClusterConfig, newConfig: ClusterConfig): Try[Boolean] = {
Try {
if(newConfig.curatorConfig.zkConnect == currentConfig.curatorConfig.zkConnect
&& newConfig.enabled == currentConfig.enabled
&& newConfig.version == currentConfig.version
&& newConfig.jmxEnabled == currentConfig.jmxEnabled
&& newConfig.jmxUser == currentConfig.jmxUser
&& newConfig.jmxPass == currentConfig.jmxPass
&& newConfig.logkafkaEnabled == currentConfig.logkafkaEnabled
&& newConfig.pollConsumers == currentConfig.pollConsumers
&& newConfig.filterConsumers == currentConfig.filterConsumers
&& newConfig.activeOffsetCacheEnabled == currentConfig.activeOffsetCacheEnabled
&& newConfig.displaySizeEnabled == currentConfig.displaySizeEnabled
&& newConfig.tuning == currentConfig.tuning
&& newConfig.securityProtocol == currentConfig.securityProtocol
) {
//nothing changed
false
} else {
//only need to shutdown enabled cluster
log.info("Updating cluster manager for cluster={} , old={}, new={}",
currentConfig.name,currentConfig,newConfig)
markPendingClusterManager(newConfig)
removeClusterManager(currentConfig)
true
}
}
}
private[this] def updateState(): Unit = {
log.info("Updating internal state...")
val result = Try {
kafkaManagerPathCache.getCurrentData.asScala.foreach { data =>
ClusterConfig.deserialize(data.getData) match {
case Failure(t) =>
log.error("Failed to deserialize cluster config",t)
case Success(newConfig) =>
val configWithDefaults = getConfigWithDefaults(newConfig, kafkaManagerConfig)
clusterConfigMap.get(newConfig.name).fold(addCluster(configWithDefaults))(updateCluster(_,configWithDefaults))
}
}
}
result match {
case Failure(t) =>
log.error("Failed to update internal state ... ",t)
case _ =>
}
lastUpdateMillis = System.currentTimeMillis()
}
private[this] def pruneClusters(): Unit = {
log.info("Pruning clusters...")
Try {
val localClusterConfigMap = clusterConfigMap
localClusterConfigMap.foreach { case (name, clusterConfig) =>
val zkpath : String = getConfigsZkPath(clusterConfig)
if(kafkaManagerPathCache.getCurrentData(zkpath) == null) {
pendingClusterConfigMap -= clusterConfig.name
removeClusterManager(clusterConfig)
clusterConfigMap -= name
}
}
}
lastUpdateMillis = System.currentTimeMillis()
}
}