-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure-node.sh
executable file
·1079 lines (890 loc) · 34.4 KB
/
configure-node.sh
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Script to deploy and configure the Confluent Platform packages
# as part of a total cluster deployment operation into Azure.
#
# Expectations :
# Script run as root
# Confluent Platform already installed to $CP_HOME (with proper ownerships)
#
# Input
# HOSTNAME : System hostname determines core functionality
# CLUSTERNAME : (currently extracted from hostname as ${host%%-*}
# DATA_DIRS : space-separated list of directories for Kafka Log data
# CP_HOSTS_FILE : file (defaults to /tmp/cphosts) deployed hosts
# Fixed naming schema: <cluster>-[broker | zookeeper | worker]
# CP_PARM file (defaults to /tmp/cp.parm), containing
# zknodes: list of Zookeeper hostnames
# brokers: list of nodes to run Kafka brokers
# workers: list of nodes to run Kafka brokers
# NOTE: will be constructed based on /tmp/cphosts if necessary
#
# Output
# Services configured and running
# Schema Registry service runs on *broker-1
# REST Proxy service runs on all broker nodes
#
# Future enhancements
# - Better integratio with Linux service model (so that Kafka services
# can be part of core O/S
# - Put an HAProxy service in front of REST Proxy
#
#
set -x
THIS_SCRIPT=`readlink -f $0`
SCRIPTDIR=`dirname ${THIS_SCRIPT}`
LOG=/tmp/configure-node.log
# Extract useful details from the AWS MetaData
# The information there should be treated as the source of truth,
# even if the internal settings are temporarily incorrect.
murl_top=http://instance-data/latest/meta-data
THIS_FQDN=$(curl -f -s $murl_top/hostname)
[ -z "${THIS_FQDN}" ] && THIS_FQDN=`hostname --fqdn`
THIS_HOST=${THIS_FQDN%%.*}
CLUSTERNAME="${THIS_HOST%%-*}"
CP_HOSTS_FILE=/tmp/cphosts # Helper file
# Our configuration files (default locations {tarball installation})
CP_HOME=${CP_HOME:-/opt/confluent}
# For now, make sure initscripts directory and service scripts
# are owned by root
patch_confluent_initscripts() {
INITSCRIPTS_DIR=${CP_HOME}/initscripts
INITSCRIPTS_LIST="cp-kafka-service cp-zk-service cp-schema-service cp-rest-service control-center-service"
if [ -d $INITSCRIPTS_DIR ] ; then
chown -R --reference=/etc/init.d $INITSCRIPTS_DIR
else
mkdir -p $INITSCRIPTS_DIR
# chown --reference=$CP_HOME/etc $INITSCRIPTS_DIR
fi
# Distribute patches to the image if necessary.
for s in $INITSCRIPTS_LIST ; do
if [ -f ${SCRIPTDIR}/$s ] ; then
if [ -f ${INITSCRIPTS_DIR}/$s ] ; then
cp -p ${INITSCRIPTS_DIR}/$s ${INITSCRIPTS_DIR}/${s}.image
fi
cp ${SCRIPTDIR}/$s ${INITSCRIPTS_DIR}
chmod a+x ${INITSCRIPTS_DIR}/${s}
# chown --reference=${INITSCRIPTS_DIR} $INITSCRIPTS_DIR/${s}
fi
done
}
# Locate the configuration files (since we use them all the time)
# Should be called ONLY after the software has been installed.
locate_cfg() {
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP=""
ZK_CFG=${CP_TOP}/etc/kafka/zookeeper.properties
BROKER_CFG=${CP_TOP}/etc/kafka/server.properties
REST_PROXY_CFG=${CP_TOP}/etc/kafka-rest/kafka-rest.properties
SCHEMA_REG_CFG=${CP_TOP}/etc/schema-registry/schema-registry.properties
KAFKA_CONNECT_CFG=${CP_TOP}/etc/kafka/connect-distributed.properties
LEGACY_CONSUMER_CFG=${CP_TOP}/etc/kafka/consumer.properties
LEGACY_PRODUCER_CFG=${CP_TOP}/etc/kafka/producer.properties
CONTROL_CENTER_CFG=${CP_TOP}/etc/confluent-control-center/control-center.properties
}
# Locate the start scripts for any changes.
# Should be called ONLY after the software has been installed.
locate_start_scripts() {
BIN_DIR=${CP_HOME}/bin
[ ! -d $CP_HOME ] && BIN_DIR="/usr/bin"
ZK_SCRIPT=${BIN_DIR}/zookeeper-server-start
BROKER_SCRIPT=${BIN_DIR}/kafka-server-start
REST_PROXY_SCRIPT=${BIN_DIR}/kafka-rest-start
SCHEMA_REG_SCRIPT=${BIN_DIR}/schema-registry-start
KAFKA_CONNECT_SCRIPT=${BIN_DIR}/connect-distributed
CONTROL_CENTER_SCRIPT=${BIN_DIR}/control-center-start
}
# Archive the configuration file sto a known location
archive_cfg() {
NOW=$(date +"%F-%H:%M")
backup_dir=$CP_HOME/etc/archive_${NOW}
mkdir -p $backup_dir
cp -p $ZK_CFG $backup_dir
cp -p $BROKER_CFG $backup_dir
cp -p $REST_PROXY_CFG $backup_dir
cp -p $SCHEMA_REG_CFG $backup_dir
cp -p $KAFKA_CONNECT_CFG $backup_dir
cp -p $LEGACY_CONSUMER_CFG $backup_dir
cp -p $CONTROL_CENTER_CFG $backup_dir
}
# Add/update config file parameter
# $1 : config file
# $2 : property
# $3 : new value
# $4 (optional) : 0: delete old value; 1[default]: retain old value
#
# The sed logic in this functions works given following limitations
# 1. At most one un-commented setting for a given parameter
# 2. If ONLY commented values exist, the FIRST ONE will be overwritten
#
set_property() {
[ ! -f $1 ] && return 1
local cfgFile=$1
local property=$2
local newValue=$3
local doArchive=${4:-1}
grep -q "^${property}=" $cfgFile
overwriteMode=$?
grep -q "^#${property}=" $cfgFile
restoreMode=$?
if [ $overwriteMode -eq 0 ] ; then
if [ $doArchive -ne 0 ] ; then
# Add the new setting, then comment out the old
sed -i "/^${property}=/a ${property}=$newValue" $cfgFile
sed -i "0,/^${property}=/s|^${property}=|# ${property}=|" $cfgFile
else
sed -i "s|^${property}=.*$|${property}=${newValue}|" $cfgFile
fi
elif [ $restoreMode -eq 0 ] ; then
# "Uncomment" first entry, then replace it
# This helps us by leaving the setting in the same place in the file
sed -i "0,/^#${property}=/s|^#${property}=|${property}=|" $cfgFile
sed -i "s|^${property}=.*$|${property}=${newValue}|" $cfgFile
else
echo "" >> $cfgFile
echo "${property}=${newValue}" >> $cfgFile
fi
}
# TBD
# Be smarter about updating config
configure_confluent_zk() {
[ ! -f $ZK_CFG ] && return 1
grep -q ^initLimit $ZK_CFG
[ $? -ne 0 ] && echo "initLimit=5" >> $ZK_CFG
grep -q ^syncLimit $ZK_CFG
[ $? -ne 0 ] && echo "syncLimit=2" >> $ZK_CFG
myid=0
zidx=1
for znode in ${zknodes//,/ } ; do
set_property $ZK_CFG "server.$zidx" "$znode:2888:3888" 0
[ $znode = $THIS_HOST ] && myid=$zidx
zidx=$[zidx+1]
done
# If we're not a ZK node, there's nothing more to do
echo $zknodes | grep -q -w $THIS_HOST
[ $? -ne 0 ] && return 0
# Simple deployment : ZK data in $CP_HOME/zkdata
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP=""
mkdir -p $CP_TOP/zkdata
chown --reference=$CP_TOP/etc $CP_TOP/zkdata
set_property $ZK_CFG "dataDir" "$CP_TOP/zkdata"
if [ $myid -gt 0 ] ; then
echo $myid > $CP_TOP/zkdata/myid
chown --reference=$CP_TOP/etc $CP_TOP/zkdata/myid
fi
}
configure_kafka_broker() {
[ ! -f $BROKER_CFG ] && return 1
local numBrokers=`echo ${brokers//,/ } | wc -w`
local ncpu=$(grep ^processor /proc/cpuinfo | wc -l)
ncpu=${ncpu:-2}
myid=-1
bidx=0
for bnode in ${brokers//,/ } ; do
[ $bnode = $THIS_HOST ] && myid=$bidx
bidx=$[bidx+1]
done
# Choose between explicit setting of broker.id or auto-generation
# As of 3.1.2, ConfluentMetricsReporter class did not properly
# report broker metrics when auto-generation was enabled.
if [ $myid -ge 0 ] ; then
set_property $BROKER_CFG "broker.id" "$myid"
else
sed -i "s/^broker\.id=.*$/# broker\.id=$myid/" $BROKER_CFG
sed -i "s/^broker\.id\.generation\.enabled=false/broker\.id\.generation\.enabled=true/" $BROKER_CFG
fi
# Set target zookeeper quorum and VERY LONG timeout (5 minutes)
# (since we don't know how long before other nodes will come on line)
set_property $BROKER_CFG "zookeeper.connect" "$zconnect"
set_property $BROKER_CFG "zookeeper.connection.timeout.ms" 300000
if [ -n "$DATA_DIRS" ] ; then
for d in $DATA_DIRS ; do
chown --reference=$CP_HOME/etc $d
done
set_property $BROKER_CFG "log.dirs" "${DATA_DIRS// /,}"
set_property $BROKER_CFG "num.recovery.threads.per.data.dir" $[ncpu*4]
# Could also bump num.io.threads (default: 8) and
# num.network.threads (default: 3) here.
fi
# Topic management settings
set_property $BROKER_CFG "auto.create.topics.enable" "false"
set_property $BROKER_CFG "delete.topics.enable" "true"
# Enable graceful leader migration
set_property $BROKER_CFG "controlled.shutdown.enable" "true"
# For tracking activity in the cloud.
set_property $BROKER_CFG "confluent.support.customer.id" "Azure_BYOL"
# Enable replicator settings if the rebalancer is present
if [ -x $CP_HOME/bin/confluent-rebalancer ] ; then
mr_topic_replicas=3
[ $mr_topic_replicas -gt $numBrokers ] && mr_topic_replicas=$numBrokers
set_property $BROKER_CFG "metric.reporters" "io.confluent.metrics.reporter.ConfluentMetricsReporter"
set_property $BROKER_CFG "confluent.metrics.reporter.topic.replicas" "$mr_topic_replicas"
set_property $BROKER_CFG "confluent.metrics.reporter.bootstrap.servers" "$bconnect"
set_property $BROKER_CFG "confluent.metrics.reporter.zookeeper.connect" "$zconnect"
fi
}
configure_schema_registry() {
[ ! -f $SCHEMA_REG_CFG ] && return 1
set_property $SCHEMA_REG_CFG "kafkastore.connection.url" "$zconnect"
set_property $SCHEMA_REG_CFG "kafkastore.zk.session.timeout.ms" "300000"
set_property $SCHEMA_REG_CFG "kafkastore.init.timeout.ms" "300000"
}
configure_rest_proxy() {
[ ! -f $REST_PROXY_CFG ] && return 1
set_property $REST_PROXY_CFG "id" "kafka-rest-${CLUSTERNAME}" 0
# TBD : get much smarter about Schema Registry Port
# Should grab this from zookeeper if it's available
set_property $REST_PROXY_CFG "schema.registry.url" "http://$srconnect" 0
set_property $REST_PROXY_CFG "zookeeper.connect" "$zconnect" 0
}
configure_control_center() {
[ ! -f $CONTROL_CENTER_CFG ] && return 1
local ncpu=$(grep ^processor /proc/cpuinfo | wc -l)
ncpu=${ncpu:-2}
local numBrokers=`echo ${brokers//,/ } | wc -w`
local numWorkers=`echo ${workers//,/ } | wc -w`
# When Control Center is hosted alongside brokers,
# its data dir should be isolated.
# In other cases, allow a few more threads if we won't compete with
# other services (Control Center deserves a bigger percentage of worker-0)
echo "$brokers" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 ] ; then
if [ -z "$CC_DATA_DIR" ] ; then
CC_DATA_DIR=/var/lib/confluent/control-center
mkdir -p $CC_DATA_DIR
chown --reference=$CP_HOME/etc $CC_DATA_DIR
fi
elif [ $numWorkers -gt 1 -a $ncpu -gt 8 ] ; then
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.streams.num.stream.threads" "$ncpu"
fi
# REST properties for service
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.rest.compression.enable" "true"
cc_topics_replicas=3
[ $cc_topics_replicas -gt $numBrokers ] && cc_topics_replicas=$numBrokers
monitoring_topics_replicas=2
[ $monitoring_topics_replicas -gt $numBrokers ] && monitoring_topics_replicas=$numBrokers
cc_partitions=5
[ $cc_partitions -gt $numBrokers ] && cc_partitions=$numBrokers
# Update properties for the Control Center
set_property $CONTROL_CENTER_CFG "bootstrap.servers" "$bconnect" 0
set_property $CONTROL_CENTER_CFG "zookeeper.connect" "$zconnect" 0
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.internal.topics.partitions" $cc_partitions
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.internal.topics.replication" $cc_topics_replicas
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.command.topic.partitions" $cc_partitions
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.command.topic.replication" $cc_topics_replicas
set_property $CONTROL_CENTER_CFG "confluent.monitoring.interceptor.topic.partitions" $cc_partitions
set_property $CONTROL_CENTER_CFG "confluent.monitoring.interceptor.topic.replication" $monitoring_topics_replicas
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.connect.cluster" "$wconnect" 0
# Put control center data on larger storage (if configured)
if [ -n "$DATA_DIRS" ] ; then
for d in $DATA_DIRS ; do
chown --reference=$CP_HOME/etc $d
[ -z "$CC_DATA_DIR" ] && CC_DATA_DIR=${d}/confluent/control-center
done
set_property $CONTROL_CENTER_CFG "confluent.controlcenter.data.dir" "${CC_DATA_DIR}"
fi
# Control Center requires a separate Kafka Connect cluster
CC_CONNECT_CFG=$CP_HOME/etc/confluent-control-center/connect-cc.properties
cp -p $CP_HOME/etc/schema-registry/connect-avro-distributed.properties $CC_CONNECT_CFG
set_property $CC_CONNECT_CFG "consumer.interceptor.classes" "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor" 0
set_property $CC_CONNECT_CFG "producer.interceptor.classes" "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor" 0
set_property $CC_CONNECT_CFG "key.converter.schema.registry.url" "http://$srconnect" 0
set_property $CC_CONNECT_CFG "value.converter.schema.registry.url" "http://$srconnect" 0
# Set the data location to the last disk in our list
[ -n "$DATA_DIRS" ] && set_property $CC_CONNECT_CFG "confluent.controlcenter.data.dir" "${DATA_DIRS##* }"
}
configure_workers() {
if [ -f $LEGACY_CONSUMER_CFG ] ; then
set_property $LEGACY_CONSUMER_CFG "group.id" "${CLUSTERNAME}-consumer-group"
set_property $LEGACY_CONSUMER_CFG "zookeeper.connect" "$zconnect"
set_property $LEGACY_CONSUMER_CFG "zookeeper.connection.timeout.ms" 30000
set_property $LEGACY_CONSUMER_CFG "interceptor.classes" "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor"
fi
if [ -f $LEGACY_PRODUCER_CFG ] ; then
set_property $LEGACY_PRODUCER_CFG "bootstrap.servers" "${bconnect}"
set_property $LEGACY_PRODUCER_CFG "request.timeout.ms" "100"
set_property $LEGACY_PRODUCER_CFG "interceptor.classes" "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor"
fi
# We'll default to the Avro converters since we know
# we'll have the Schema Registry . Also enable the
# interceptors for Control Center Monitoring
if [ -f $KAFKA_CONNECT_CFG ] ; then
set_property $KAFKA_CONNECT_CFG "group.id" "${CLUSTERNAME}-connect-cluster"
set_property $KAFKA_CONNECT_CFG "bootstrap.servers" "${bconnect}"
set_property $KAFKA_CONNECT_CFG "key.converter" "io.confluent.connect.avro.AvroConverter"
set_property $KAFKA_CONNECT_CFG "key.converter.schema.registry.url" "http://${srconnect}"
set_property $KAFKA_CONNECT_CFG "key.converter.schemas.enable" "true"
set_property $KAFKA_CONNECT_CFG "value.converter" "io.confluent.connect.avro.AvroConverter"
set_property $KAFKA_CONNECT_CFG "value.converter.schema.registry.url" "http://${srconnect}"
set_property $KAFKA_CONNECT_CFG "value.converter.schemas.enable" "true"
set_property $KAFKA_CONNECT_CFG "consumer.interceptor.classes" "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor"
set_property $KAFKA_CONNECT_CFG "producer.interceptor.classes" "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor"
fi
# There are multiple "connect-*.properties" files in
# the schema registry location that need to be updated as well
for f in $CP_HOME/etc/schema-registry/connect-*.properties ; do
set_property $f "bootstrap.servers" "${bconnect}" 0
set_property $f "key.converter.schema.registry.url" "http://${srconnect}" 0
set_property $f "value.converter.schema.registry.url" "http://${srconnect}" 0
done
for f in $CP_HOME/etc/schema-registry/*-distributed.properties ; do
set_property $KAFKA_CONNECT_CFG "group.id" "${CLUSTERNAME}-connect-cluster" 0
done
}
#
# Sets several important variables for use in sub-functions
# zconnect : zookeeper connect arg (<host1>:<port1>[,<host2>:<port2> ...]
# bconnect : broker connect arg (<host1>:<port1>[,<host2>:<port2> ...]
# srconnect : schema registry connect arg (<host1>:<port1>[,<host2>:<port2> ...]
#
configure_confluent_node() {
# Assemble Zookeeper Connect and Broker List strings once,
# since we may use themm in multiple places
if [ -f $ZK_CFG ] ; then
eval $(grep ^clientPort= $ZK_CFG)
zkPort=${clientPort:-2181}
fi
zconnect=""
for znode in ${zknodes//,/ } ; do
if [ -z "$zconnect" ] ; then
zconnect="$znode:${zkPort:-2181}"
else
zconnect="$zconnect,$znode:${zkPort:-2181}"
fi
done
if [ -f $BROKER_CFG ] ; then
eval $(grep ^listeners= $BROKER_CFG)
brokerPort=${listeners##*:}
brokerPort=${brokerPort:-9092}
fi
bconnect=""
for bnode in ${brokers//,/ } ; do
if [ -z "$bconnect" ] ; then
bconnect="$bnode:${brokerPort:-9092}"
else
bconnect="$bconnect,$bnode:${brokerPort:-9092}"
fi
done
# Schema Registry runs on the second worker
numWorkers=$(echo "${workers//,/ }" | wc -w)
if [ $numWorkers -le 1 ] ; then
srconnect=${workers%%,*}:8081
else
srconnect=$(echo $workers | cut -d, -f2)
srconnect=${srconnect}:8081
fi
if [ -f $KAFKA_CONNECT_CFG ] ; then
connectRestPort=$(grep -e ^rest.port= $KAFKA_CONNECT_CFG | cut -d'=' -f2)
connectRestPort=${connectRestPort:-8083}
fi
wconnect=""
for wnode in ${workers//,/ } ; do
if [ -z "$wconnect" ] ; then
wconnect="$wnode:${connectRestPort:-8083}"
else
wconnect="$wconnect,$wnode:${connectRestPort:-8083}"
fi
done
# Save off the configuration details before making our changes.
archive_cfg
configure_confluent_zk
configure_kafka_broker
configure_schema_registry
configure_rest_proxy
configure_workers
[ -n "$workers" ] && configure_control_center
}
# Sets memory allocation in start scripts
# Assumes that the "locate_start_scripts" has been run
#
update_service_heap_opts() {
if [ ! -x $SCRIPTDIR/compute-heap-opts ] ; then
return
fi
ZK_SCRIPT=${BIN_DIR}/zookeeper-server-start
BROKER_SCRIPT=${BIN_DIR}/kafka-server-start
# Source the script that sets *_HEAP_OPTS
. $SCRIPTDIR/compute-heap-opts
# Since the ZK_SCRIPT already has an override, we need to
# be careful and replace it in the right place
echo "$zknodes" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 -a -n "$ZOOKEEPER_HEAP_OPTS" ] ; then
sed -i "/ export KAFKA_HEAP_OPTS=/a\ \ \ \ export KAFKA_HEAP_OPTS=\"$ZOOKEEPER_HEAP_OPTS\"" $ZK_SCRIPT
sed -i "0,/ export KAFKA_HEAP_OPTS=/s| export KAFKA_HEAP_OPTS=|#export KAFKA_HEAP_OPTS==|" $ZK_SCRIPT
fi
# Since the BROKER_SCRIPT already has an override, we need to
# be careful and replace it in the right place
echo "$brokers" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 -a -n "$BROKER_HEAP_OPTS" ] ; then
sed -i "/ export KAFKA_HEAP_OPTS=/a\ \ \ \ export KAFKA_HEAP_OPTS=\"$BROKER_HEAP_OPTS\"" $BROKER_SCRIPT
sed -i "0,/ export KAFKA_HEAP_OPTS=/s| export KAFKA_HEAP_OPTS=|#export KAFKA_HEAP_OPTS==|" $BROKER_SCRIPT
fi
echo "$workers" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 ] ; then
[ -n "$CONNECT_HEAP_OPTS" ] && \
sed -i "/^exec /i export KAFKA_HEAP_OPTS=\"$CONNECT_HEAP_OPTS\"" $KAFKA_CONNECT_SCRIPT
[ -n "$REST_HEAP_OPTS" ] && \
sed -i "/^exec /i export KAFKAREST_HEAP_OPTS=\"$REST_HEAP_OPTS\"" $REST_PROXY_SCRIPT
fi
# Control Center is the first worker
if [ "${workers%%,*}" = $THIS_HOST ] ; then
[ -n "$CC_HEAP_OPTS" ] && \
sed -i "/^exec /i export CONTROL_CENTER_HEAP_OPTS=\"$CC_HEAP_OPTS\"" $CONTROL_CENTER_SCRIPT
fi
# Schema registy on second worker (or first if there's only one)
numWorkers=$(echo "${workers//,/ }" | wc -w)
if [ $numWorkers -le 1 ] ; then
srWorker=${workers%%,*}
else
srWorker=$(echo $workers | cut -d, -f2)
fi
if [ "${srWorker}" = $THIS_HOST ] ; then
[ -n "$SR_HEAP_OPTS" ] && \
sed -i "/^exec /i export SCHEMA_REGISTRY_HEAP_OPTS=\"$SR_HEAP_OPTS\"" $SCHEMA_REG_SCRIPT
fi
}
# Simple code to wait for formation of zookeeper quorum.
# We know that the "kafka-topics" call to retrieve metadata
# won't work until the quorum is formed ... so use that
# if the cub utility is not present.
#
wait_for_zk_quorum() {
ZOOKEEPER_WAIT=${1:-300}
STIME=5
if [ -x $SCRIPTDIR/cub -a -f $SCRIPTDIR/docker-utils.jar ] ; then
DOCKER_UTILS_JAR=$SCRIPTDIR/docker-utils.jar $SCRIPTDIR/cub zk-ready $zconnect $ZOOKEEPER_WAIT
[ $? -ne 0 ] && return 1
sleep $STIME # still need some stabilization time
else
SWAIT=$ZOOKEEPER_WAIT
${CP_TOP}/bin/kafka-topics --list --zookeeper ${zconnect} &> /dev/null
while [ $? -ne 0 -a $SWAIT -gt 0 ] ; do
sleep $STIME
SWAIT=$[SWAIT - $STIME]
${CP_TOP}/bin/kafka-topics --list --zookeeper ${zconnect} &> /dev/null
done
[ $SWAIT -le 0 ] && return 1
fi
return 0
}
resolve_zknodes() {
echo "WAITING FOR DNS RESOLUTION of zookeeper nodes {$zknodes}" >> $LOG
zkwait=600
zkready=0
while [ $zkready -eq 0 ]
do
[ $zkwait -lt 0 ] && return 1
zkready=1
echo testing DNS resolution for zknodes
for i in ${zknodes//,/ }
do
[ -z "$(dig -t a +search +short $i)" ] && zkready=0
done
echo zkready is $zkready
[ $zkready -eq 0 ] && sleep 5
zkwait=$[zkwait - 5]
done
echo "DNS has resolved all zknodes {$zknodes}" >> $LOG
return 0
}
# Kludgy function to make sure the cluster is formed before
# proceeding with the remaining startup activities.
#
# NOTE: We only need to wait for other brokers if THIS NODE
# is a broker or worker. zookeeper-only nodes need not
# waste time here
wait_for_brokers() {
echo "$brokers" | grep -q -w "$THIS_HOST"
if [ $? -ne 0 ] ; then
echo "$workers" | grep -q -w "$THIS_HOST"
[ $? -ne 0 ] && return 0
fi
BROKER_WAIT=${1:-300}
SWAIT=$BROKER_WAIT
STIME=5
${CP_HOME}/bin/kafka-topics --list --zookeeper ${zconnect} &> /dev/null
while [ $? -ne 0 -a $SWAIT -gt 0 ] ; do
sleep $STIME
SWAIT=$[SWAIT - $STIME]
${CP_HOME}/bin/kafka-topics --list --zookeeper ${zconnect} &> /dev/null
done
[ $SWAIT -le 0 ] && return 1
# Now that we know the ZK cluster is on line, we can check the number
# of registered brokers. Ideally, we'd just look for "enough" brokers,
# hence the "targetBrokers" logic below
#
local numBrokers=`echo ${brokers//,/ } | wc -w`
local targetBrokers=$numBrokers
[ $targetBrokers -gt 5 ] && targetBrokers=5
local runningBrokers=$( echo "ls /brokers/ids" | $CP_HOME/bin/zookeeper-shell ${zconnect%%,*} | grep '^\[' | tr -d "[:punct:]" | wc -w )
while [ ${runningBrokers:-0} -lt $targetBrokers -a $SWAIT -gt 0 ] ; do
sleep $STIME
SWAIT=$[SWAIT - $STIME]
runningBrokers=$( echo "ls /brokers/ids" | $CP_HOME/bin/zookeeper-shell ${zconnect%%,*} | grep '^\[' | tr -d "[:punct:]" | wc -w )
done
[ $SWAIT -le 0 ] && return 1
return 0
}
# Kludgy function to make sure the cluster is formed before
# proceeding with the remaining startup activities.
#
# Later versions of the image will have the
# "Confluent Utility Belt" (cub) utility; use that if present.
#
# NOTE: We only need to wait for other brokers if THIS NODE
# is a broker or worker. zookeeper-only nodes need not
# waste time here
wait_for_brokers() {
echo "$brokers" | grep -q -w "$THIS_HOST"
if [ $? -ne 0 ] ; then
echo "$workers" | grep -q -w "$THIS_HOST"
[ $? -ne 0 ] && return 0
fi
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
BROKER_WAIT=${1:-300}
STIME=5
# Now that we know the ZK cluster is on line, we can check the number
# of registered brokers. Ideally, we'd just look for "enough" brokers,
# hence the "targetBrokers" logic below
#
local numBrokers=`echo ${brokers//,/ } | wc -w`
local targetBrokers=$numBrokers
[ $targetBrokers -gt 5 ] && targetBrokers=5
if [ -x $SCRIPTDIR/cub -a -f $SCRIPTDIR/docker-utils.jar ] ; then
DOCKER_UTILS_JAR=$SCRIPTDIR/docker-utils.jar $SCRIPTDIR/cub kafka-ready -b $bconnect $targetBrokers $BROKER_WAIT
[ $? -ne 0 ] && return 1
else
SWAIT=$BROKER_WAIT
local runningBrokers=$( echo "ls /brokers/ids" | $CP_TOP/bin/zookeeper-shell ${zconnect%%,*} | grep '^\[' | tr -d "[:punct:]" | wc -w )
while [ ${runningBrokers:-0} -lt $targetBrokers -a $SWAIT -gt 0 ] ; do
sleep $STIME
SWAIT=$[SWAIT - $STIME]
runningBrokers=$( echo "ls /brokers/ids" | $CP_TOP/bin/zookeeper-shell ${zconnect%%,*} | grep '^\[' | tr -d "[:punct:]" | wc -w )
done
[ $SWAIT -le 0 ] && return 1
fi
return 0
}
# Use host role to determine services to start.
# Separate "core" and "worker" services, since we
# may need to do some work within the brokers once
# they are able to respond to admin requests.
#
# Configure appropriate services for auto-start
#
# DANGER : the systemctl logic needs the control
# operations to run from the SAME LOCATION. You
# cannot start with "$CP_HOME/initscripts/cp-*-service"
# and then stop with "/etc/init.d/cp-*-service"
#
start_core_services() {
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
BIN_DIR=$CP_TOP/bin
local zkhost=0
echo "$zknodes" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 ] ; then
if [ -x $CP_HOME/initscripts/cp-zk-service ] ; then
ln -s $CP_HOME/initscripts/cp-zk-service /etc/init.d
chkconfig cp-zk-service on
[ $? -ne 0 ] && systemctl enable cp-zk-service
# $CP_HOME/initscripts/cp-zk-service start
# /etc/init.d/cp-zk-service start
service cp-zk-service start
else
$BIN_DIR/zookeeper-server-start -daemon $ZK_CFG
fi
zkhost=1
fi
echo "$brokers" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 ] ; then
wait_for_zk_quorum
if [ $? -ne 0 ] ; then
echo " WARNING: Zookeeper Quorum not formed; broker start may fail" | tee -a $LOG
fi
if [ -x $CP_HOME/initscripts/cp-kafka-service ] ; then
ln -s $CP_HOME/initscripts/cp-kafka-service /etc/init.d
chkconfig cp-kafka-service on
[ $? -ne 0 ] && systemctl enable cp-kafka-service
# $CP_HOME/initscripts/cp-kafka-service start
# /etc/init.d/cp-kafka-service start
service cp-kafka-service start
else
$BIN_DIR/kafka-server-start -daemon $BROKER_CFG
fi
fi
}
start_worker_services() {
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
BIN_DIR=$CP_TOP/bin
# Schema registy on second worker (or first if there's only one)
numWorkers=$(echo "${workers//,/ }" | wc -w)
if [ $numWorkers -le 1 ] ; then
srWorker=${workers%%,*}
else
srWorker=$(echo $workers | cut -d, -f2)
fi
if [ "${srWorker}" = $THIS_HOST ] ; then
if [ -x $CP_HOME/initscripts/cp-schema-service ] ; then
ln -s $CP_HOME/initscripts/cp-schema-service /etc/init.d
chkconfig cp-schema-service on
[ $? -ne 0 ] && systemctl enable cp-schema-service
# $CP_HOME/initscripts/cp-schema-service start
# /etc/init.d/cp-schema-service start
service cp-schema-service start
else
$(cd $BIN_DIR/../logs; $BIN_DIR/schema-registry-start -daemon $SCHEMA_REG_CFG > /dev/null)
fi
fi
echo "$workers" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 ] ; then
if [ -x $CP_HOME/initscripts/cp-rest-service ] ; then
ln -s $CP_HOME/initscripts/cp-rest-service /etc/init.d
chkconfig cp-rest-service on
[ $? -ne 0 ] && systemctl enable cp-rest-service
# $CP_HOME/initscripts/cp-rest-service start
# /etc/init.d/cp-rest-service start
service cp-rest-service start
else
$(cd $BIN_DIR/../logs; $BIN_DIR/kafka-rest-start -daemon $REST_PROXY_CFG > /dev/null)
fi
fi
echo "$workers" | grep -q -w "$THIS_HOST"
if [ $? -eq 0 ] ; then
if [ -x $CP_HOME/initscripts/cp-connect-service ] ; then
ln -s $CP_HOME/initscripts/cp-connect-service /etc/init.d
chkconfig cp-connect-service on
[ $? -ne 0 ] && systemctl enable cp-connect-service
# $CP_HOME/initscripts/cp-connect-service start
# /etc/init.d/cp-connect-service start
service cp-connect-service start
else
$BIN_DIR/connect-distributed -daemon $KAFKA_CONNECT_CFG
fi
fi
}
start_control_center() {
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
BIN_DIR=$CP_HOME/bin
# Control Center on first broker only
# Control Center is VERY FRAGILE on start-up,
# so we'll try a second time if the first try fails.
if [ "${workers%%,*}" = $THIS_HOST ] ; then
if [ -x $CP_HOME/initscripts/control-center-service ] ; then
ln -s $CP_HOME/initscripts/control-center-service /etc/init.d
chkconfig control-center-service on
[ $? -ne 0 ] && systemctl enable control-center-service
# $CP_HOME/initscripts/control-center-service start
# /etc/init.d/control-center-service start
service control-center-service start
[ $? -ne 0 ] && service control-center-service start
else
$(cd $BIN_DIR/../logs; $BIN_DIR/control-center-start -daemon $CONTROL_CENTER_CFG > /dev/null)
fi
fi
}
# We routinely encounter issues where the
# workers come on line before the brokers / zookeepers are
# ready to handle topic creation. This is a silly wrapper
# to safely retry topic creation for a more robust behavior.
#
# Inputs: <topic> <partitions> <replicas>
# Return: 0 on success, 1 on failure
#
# WARNING: no error checking whatsoever
#
MAX_TOPIC_RETRIES=10
RETRY_INTERVAL_SEC=5
create_topic_safely() {
local this_retry=1
local this_topic=$1
local partitions=$2
local replicas=$3
local cleanup_policy=$4
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
[ -n "$cleanup_policy" ] && CP_ARG="--config cleanup.policy=$cleanup_policy"
$CP_TOP/bin/kafka-topics --zookeeper ${zconnect} \
--create --if-not-exists \
--topic $this_topic \
--replication-factor ${replicas} --partitions ${partitions} $CP_ARG
while [ $? -ne 0 -a $this_retry -lt $MAX_TOPIC_RETRIES ] ; do
this_retry=$[this_retry+1]
sleep $RETRY_INTERVAL_SEC
$CP_TOP/bin/kafka-topics --zookeeper ${zconnect} \
--create --if-not-exists \
--topic $this_topic \
--replication-factor ${replicas} --partitions ${partitions}
done
[ $this_retry -ge $MAX_TOPIC_RETRIES ] && return 1
return 0
}
# Crude function to wait for a topic to exist within the cluster.
#
# $1: topic name
# $2: (optional) max wait (defaults to 5 minutes)
wait_for_topic() {
local topic=${1:-}
local TOPIC_WAIT=${2:-300}
[ -z "$topic" ] && return
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
SWAIT=$TOPIC_WAIT
STIME=5
${CP_TOP}/bin/kafka-topics --zookeeper ${zconnect} \
--describe --topic ${topic} | grep -q "^Topic:"
while [ $? -ne 0 -a $SWAIT -gt 0 ] ; do
sleep $STIME
SWAIT=$[SWAIT - $STIME]
${CP_TOP}/bin/kafka-topics --zookeeper ${zconnect} \
--describe --topic ${topic} | grep -q "^Topic:"
done
}
# Some worker services require existing topics
# Do that here (ignoring errors for now).
#
# Use "create_topic_safely" on all nodes, since it
# uses the "--if-not-exists" flag that will correctly
# avoid collisions when multiple workers try to create
# the topics.
# ALTERNATIVE : Only create topics on worker-0,
# let other workers wait.
#
create_worker_topics() {
# If this instance won't create the topics ... just wait
# till the last one shows up.
# if [ "${workers%%,*}" != $THIS_HOST ] ; then
# wait_for_topic connect-status
# return
# fi
local numBrokers=`echo ${brokers//,/ } | wc -w`
local numWorkers=`echo ${workers//,/ } | wc -w`
CP_TOP=${CP_HOME}
[ ! -d $CP_HOME ] && CP_TOP="/usr"
BIN_DIR=$CP_TOP/bin
# Connect requires some simple topics. Be sure
# these align with any overrides when customzing
# the connect-distributed.properties above.
# config.storage.topic=connect-configs
# offset.storage.topic=connect-offsets
# status.storage.topic=connect-status
connect_topic_replicas=3
[ $connect_topic_replicas -gt $numBrokers ] && connect_topic_replicas=$numBrokers
connect_config_partitions=1
# $CP_TOP/bin/kafka-topics --zookeeper ${zconnect} \
# --create --topic connect-configs \
# --replication-factor ${connect_topic_replicas} \
# --partitions ${connect_config_partitions} \
# --config cleanup.policy=compact
create_topic_safely connect-configs \
${connect_config_partitions} ${connect_topic_replicas} compact
connect_offsets_partitions=50
[ $numBrokers -lt 6 ] && connect_offsets_partitions=$[numBrokers*8]
# $CP_TOP/bin/kafka-topics --zookeeper ${zconnect} \
# --create --topic connect-offsets \
# --replication-factor ${connect_topic_replicas} \
# --partitions ${connect_offsets_partitions} \
# --config cleanup.policy=compact
create_topic_safely connect-offsets \
${connect_offsets_partitions} ${connect_topic_replicas} compact
connect_status_partitions=10
# $CP_TOP/bin/kafka-topics --zookeeper ${zconnect} \
# --create --topic connect-status \
# --replication-factor ${connect_topic_replicas} \
# --partitions ${connect_status_partitions} \
# --config cleanup.policy=compact
create_topic_safely connect-status \
${connect_status_partitions} ${connect_topic_replicas} compact
}
main()
{
echo "$0 script started at "`date` >> $LOG
if [ `id -u` -ne 0 ] ; then
echo " ERROR: script must be run as root" >> $LOG
exit 1
fi
# Look for the parameter file that will have the
# variables necessary to complete the installation
CP_PARM_FILE=/tmp/cp.parm