Skip to content

Commit 4e5a688

Browse files
committed
Support explicit creation of Monasca Kafka topics
With this patch, Monasca no longer relies on automatic topic creation in Kafka, and instead pre-creates all topics before bringing up the containers. If the topic already exists then it will not be changed, therefore existing users are not affected. This patch allows per topic customisations, such as increasing the number of partitions on particular topics and also works around a race condition in automatic topic creation where multiple instances of the same service could race to create a topic causing some of the services to restart and throw an error before resuming normal operation. Change-Id: Ib15c95bb72cf79e9e55945d757b248e06f5f4065 (cherry picked from commit e689f95)
1 parent 641671b commit 4e5a688

File tree

5 files changed

+115
-5
lines changed

5 files changed

+115
-5
lines changed

ansible/group_vars/all.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ infuxdb_internal_endpoint: "{{ internal_protocol }}://{{ kolla_internal_fqdn | p
11191119
#################
11201120
kafka_datadir_volume: "kafka"
11211121

1122+
# The number of brokers in a Kafka cluster. This is used for automatically
1123+
# setting quantities such as topic replicas and it is not recommended to
1124+
# change it unless you know what you are doing.
1125+
kafka_broker_count: "{{ groups['kafka'] | length }}"
1126+
11221127
#########################
11231128
# Internal Image options
11241129
#########################

ansible/roles/kafka/defaults/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ kafka_cluster_name: "kolla_kafka"
2020
kafka_log_dir: "/var/log/kolla/kafka"
2121
kafka_heap_opts: "-Xmx1G -Xms1G"
2222
kafka_zookeeper: "{% for host in groups['zookeeper'] %}{{ 'api' | kolla_address(host) | put_address_in_context('url') }}:{{ zookeeper_client_port }}{% if not loop.last %},{% endif %}{% endfor %}"
23-
kafka_broker_count: "{{ groups['kafka'] | length }}"
2423

2524
####################
2625
# Docker

ansible/roles/monasca/defaults/main.yml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,22 @@ monasca_storm_nimbus_servers: "{% for host in groups['storm-nimbus'] %}'{{ 'api'
131131
# NOTE(dszumski): Only one NTP server is currently supported by the Monasca Agent plugin
132132
monasca_ntp_server: "{{ external_ntp_servers | first }}"
133133

134-
# Kafka topics used by Monasca services
134+
# The default number of Kafka topic partitions. This effectively limits
135+
# the maximum number of workers per topic, counted over all nodes in the
136+
# Monasca deployment. For example, if you have a 3 node Monasca
137+
# deployment, you will by default have 3 instances of Monasca Persister,
138+
# with each instance having 2 workers by default for the metrics topic.
139+
# In this case, each worker on the metrics topic will be assigned 5
140+
# partitions of the metrics topic. If you increase the worker or instance
141+
# count, you may need to increase the partition count to ensure that all
142+
# workers can get a share of the work.
143+
monasca_default_topic_partitions: 30
144+
145+
# The default number of topic replicas. Generally you should not change
146+
# this.
147+
monasca_default_topic_replication_factor: "{{ kafka_broker_count if kafka_broker_count|int < 3 else 3 }}"
148+
149+
# Kafka topic names used by Monasca services
135150
monasca_metrics_topic: "metrics"
136151
monasca_raw_logs_topic: "logs"
137152
monasca_transformed_logs_topic: "transformed-logs"
@@ -141,6 +156,47 @@ monasca_alarm_notifications_topic: "alarm-notifications"
141156
monasca_alarm_notifications_retry_topic: "retry-notifications"
142157
monasca_periodic_notifications_topic: "60-seconds-notifications"
143158

159+
# Kafka topic configuration. Most users will not need to modify these
160+
# settings, however for deployments where resources are tightly
161+
# constrained, or very large deployments where there are many parallel
162+
# workers, it is worth considering changing them. Note that if you do
163+
# change these settings, then you will need to manually remove each
164+
# topic from the Kafka deployment for the change to take effect when
165+
# the Monasca service is reconfigured.
166+
monasca_all_topics:
167+
- name: "{{ monasca_metrics_topic }}"
168+
partitions: "{{ monasca_default_topic_partitions }}"
169+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
170+
enabled: True
171+
- name: "{{ monasca_raw_logs_topic }}"
172+
partitions: "{{ monasca_default_topic_partitions }}"
173+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
174+
enabled: True
175+
- name: "{{ monasca_transformed_logs_topic }}"
176+
partitions: "{{ monasca_default_topic_partitions }}"
177+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
178+
enabled: True
179+
- name: "{{ monasca_events_topic }}"
180+
partitions: "{{ monasca_default_topic_partitions }}"
181+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
182+
enabled: True
183+
- name: "{{ monasca_alarm_state_transitions_topic }}"
184+
partitions: "{{ monasca_default_topic_partitions }}"
185+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
186+
enabled: True
187+
- name: "{{ monasca_alarm_notifications_topic }}"
188+
partitions: "{{ monasca_default_topic_partitions }}"
189+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
190+
enabled: True
191+
- name: "{{ monasca_alarm_notifications_retry_topic }}"
192+
partitions: "{{ monasca_default_topic_partitions }}"
193+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
194+
enabled: True
195+
- name: "{{ monasca_periodic_notifications_topic }}"
196+
partitions: "{{ monasca_default_topic_partitions }}"
197+
replication_factor: "{{ monasca_default_topic_replication_factor }}"
198+
enabled: True
199+
144200
# NOTE(dszumski): Due to the way monasca-notification is currently
145201
# implemented it is not recommended to change this period.
146202
monasca_periodic_notifications_period: 60

ansible/roles/monasca/tasks/bootstrap.yml

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,47 @@
6161
delegate_to: "{{ groups['influxdb'][0] }}"
6262
when: monasca_influxdb_name not in monasca_influxdb_database.stdout_lines
6363

64-
# NOTE(dszumski): The Monasca APIs write logs and messages to Kafka. Since
65-
# Kafka has automatic topic generation enabled by default we don't need to
66-
# create topics here.
64+
# NOTE(dszumski): Although we can take advantage of automatic topic
65+
# creation in Kafka, creating the topics manually allows unique settings
66+
# to be used per topic, rather than the defaults. It also avoids an edge
67+
# case where services on multiple nodes may race to create topics, and
68+
# paves the way for enabling things like compression on a per topic basis.
69+
- name: List monasca kafka topics
70+
become: true
71+
command: >
72+
docker exec kafka /opt/kafka/bin/kafka-topics.sh
73+
--zookeeper localhost
74+
--list
75+
register: kafka_topics
76+
run_once: True
77+
delegate_to: "{{ groups['kafka'][0] }}"
78+
79+
- name: Create monasca kafka topics if they don't exist
80+
become: true
81+
command: >
82+
docker exec kafka /opt/kafka/bin/kafka-topics.sh
83+
--create
84+
--topic {{ item.name }}
85+
--partitions {{ item.partitions }}
86+
--replication-factor {{ item.replication_factor }}
87+
--zookeeper localhost
88+
run_once: True
89+
delegate_to: "{{ groups['kafka'][0] }}"
90+
when:
91+
- item.name not in kafka_topics.stdout_lines
92+
- item.enabled | bool
93+
with_items: "{{ monasca_all_topics }}"
94+
95+
- name: Remove monasca kafka topics for disabled services
96+
become: true
97+
command: >
98+
docker exec kafka /opt/kafka/bin/kafka-topics.sh
99+
--delete
100+
--topic "{{ item.name }}"
101+
--zookeeper localhost
102+
run_once: True
103+
delegate_to: "{{ groups['kafka'][0] }}"
104+
when:
105+
- item.name in kafka_topics.stdout_lines
106+
- not item.enabled | bool
107+
with_items: "{{ monasca_all_topics }}"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Adds support for explicitly creating individually customisable topics
5+
in Kafka for Monasca.
6+
fixes:
7+
- |
8+
Fixes a trivial issue where some Monasca containers could momentarily
9+
restart when initially racing to create topics in Kafka.

0 commit comments

Comments
 (0)