deimos-kafka 1.0.0.pre.beta15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +74 -0
- data/.gitignore +41 -0
- data/.gitmodules +0 -0
- data/.rspec +1 -0
- data/.rubocop.yml +321 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +77 -0
- data/Dockerfile +23 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +165 -0
- data/Guardfile +22 -0
- data/LICENSE.md +195 -0
- data/README.md +742 -0
- data/Rakefile +13 -0
- data/bin/deimos +4 -0
- data/deimos-kafka.gemspec +42 -0
- data/docker-compose.yml +71 -0
- data/docs/DATABASE_BACKEND.md +147 -0
- data/docs/PULL_REQUEST_TEMPLATE.md +34 -0
- data/lib/deimos.rb +134 -0
- data/lib/deimos/active_record_consumer.rb +81 -0
- data/lib/deimos/active_record_producer.rb +64 -0
- data/lib/deimos/avro_data_coder.rb +89 -0
- data/lib/deimos/avro_data_decoder.rb +36 -0
- data/lib/deimos/avro_data_encoder.rb +51 -0
- data/lib/deimos/backends/db.rb +27 -0
- data/lib/deimos/backends/kafka.rb +27 -0
- data/lib/deimos/backends/kafka_async.rb +27 -0
- data/lib/deimos/configuration.rb +88 -0
- data/lib/deimos/consumer.rb +164 -0
- data/lib/deimos/instrumentation.rb +71 -0
- data/lib/deimos/kafka_message.rb +27 -0
- data/lib/deimos/kafka_source.rb +126 -0
- data/lib/deimos/kafka_topic_info.rb +79 -0
- data/lib/deimos/message.rb +74 -0
- data/lib/deimos/metrics/datadog.rb +47 -0
- data/lib/deimos/metrics/mock.rb +39 -0
- data/lib/deimos/metrics/provider.rb +38 -0
- data/lib/deimos/monkey_patches/phobos_cli.rb +35 -0
- data/lib/deimos/monkey_patches/phobos_producer.rb +51 -0
- data/lib/deimos/monkey_patches/ruby_kafka_heartbeat.rb +85 -0
- data/lib/deimos/monkey_patches/schema_store.rb +19 -0
- data/lib/deimos/producer.rb +218 -0
- data/lib/deimos/publish_backend.rb +30 -0
- data/lib/deimos/railtie.rb +8 -0
- data/lib/deimos/schema_coercer.rb +108 -0
- data/lib/deimos/shared_config.rb +59 -0
- data/lib/deimos/test_helpers.rb +356 -0
- data/lib/deimos/tracing/datadog.rb +35 -0
- data/lib/deimos/tracing/mock.rb +40 -0
- data/lib/deimos/tracing/provider.rb +31 -0
- data/lib/deimos/utils/db_producer.rb +95 -0
- data/lib/deimos/utils/executor.rb +117 -0
- data/lib/deimos/utils/inline_consumer.rb +144 -0
- data/lib/deimos/utils/lag_reporter.rb +182 -0
- data/lib/deimos/utils/platform_schema_validation.rb +0 -0
- data/lib/deimos/utils/signal_handler.rb +68 -0
- data/lib/deimos/version.rb +5 -0
- data/lib/generators/deimos/db_backend/templates/migration +24 -0
- data/lib/generators/deimos/db_backend/templates/rails3_migration +30 -0
- data/lib/generators/deimos/db_backend_generator.rb +48 -0
- data/lib/tasks/deimos.rake +17 -0
- data/spec/active_record_consumer_spec.rb +81 -0
- data/spec/active_record_producer_spec.rb +107 -0
- data/spec/avro_data_decoder_spec.rb +18 -0
- data/spec/avro_data_encoder_spec.rb +37 -0
- data/spec/backends/db_spec.rb +35 -0
- data/spec/backends/kafka_async_spec.rb +11 -0
- data/spec/backends/kafka_spec.rb +11 -0
- data/spec/consumer_spec.rb +169 -0
- data/spec/deimos_spec.rb +117 -0
- data/spec/kafka_source_spec.rb +168 -0
- data/spec/kafka_topic_info_spec.rb +88 -0
- data/spec/phobos.bad_db.yml +73 -0
- data/spec/phobos.yml +73 -0
- data/spec/producer_spec.rb +397 -0
- data/spec/publish_backend_spec.rb +10 -0
- data/spec/schemas/com/my-namespace/MySchema-key.avsc +13 -0
- data/spec/schemas/com/my-namespace/MySchema.avsc +18 -0
- data/spec/schemas/com/my-namespace/MySchemaWithBooleans.avsc +18 -0
- data/spec/schemas/com/my-namespace/MySchemaWithDateTimes.avsc +33 -0
- data/spec/schemas/com/my-namespace/MySchemaWithId.avsc +28 -0
- data/spec/schemas/com/my-namespace/MySchemaWithUniqueId.avsc +32 -0
- data/spec/schemas/com/my-namespace/Widget.avsc +27 -0
- data/spec/schemas/com/my-namespace/WidgetTheSecond.avsc +27 -0
- data/spec/spec_helper.rb +207 -0
- data/spec/updateable_schema_store_spec.rb +36 -0
- data/spec/utils/db_producer_spec.rb +208 -0
- data/spec/utils/executor_spec.rb +42 -0
- data/spec/utils/lag_reporter_spec.rb +69 -0
- data/spec/utils/platform_schema_validation_spec.rb +0 -0
- data/spec/utils/signal_handler_spec.rb +16 -0
- data/support/deimos-solo.png +0 -0
- data/support/deimos-with-name-next.png +0 -0
- data/support/deimos-with-name.png +0 -0
- data/support/flipp-logo.png +0 -0
- metadata +452 -0
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
begin
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
task(default: :spec)
|
9
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
10
|
+
# no rspec available
|
11
|
+
end
|
12
|
+
|
13
|
+
import('./lib/tasks/phobos.rake')
|
data/bin/deimos
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'deimos/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'deimos-kafka'
|
9
|
+
spec.version = Deimos::VERSION
|
10
|
+
spec.authors = ['Daniel Orner']
|
11
|
+
spec.email = ['daniel.orner@wishabi.com']
|
12
|
+
spec.summary = 'Kafka libraries for Ruby.'
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'Apache 2.0'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_runtime_dependency('avro-patches', '~> 0.3')
|
22
|
+
spec.add_runtime_dependency('avro_turf', '~> 0.8')
|
23
|
+
spec.add_runtime_dependency('phobos', '~> 1.8')
|
24
|
+
spec.add_runtime_dependency('ruby-kafka', '~> 0.7')
|
25
|
+
|
26
|
+
spec.add_development_dependency('activerecord', '~> 5.2')
|
27
|
+
spec.add_development_dependency('activerecord-import')
|
28
|
+
spec.add_development_dependency('bundler', '~> 1')
|
29
|
+
spec.add_development_dependency('ddtrace', '~> 0.11')
|
30
|
+
spec.add_development_dependency('dogstatsd-ruby', '~> 4.2')
|
31
|
+
spec.add_development_dependency('guard', '~> 2')
|
32
|
+
spec.add_development_dependency('guard-rspec', '~> 4')
|
33
|
+
spec.add_development_dependency('guard-rubocop', '~> 1')
|
34
|
+
spec.add_development_dependency('mysql2', '~> 0.5')
|
35
|
+
spec.add_development_dependency('pg', '~> 1.1')
|
36
|
+
spec.add_development_dependency('rake', '~> 10')
|
37
|
+
spec.add_development_dependency('rspec', '~> 3')
|
38
|
+
spec.add_development_dependency('rspec_junit_formatter', '~>0.3')
|
39
|
+
spec.add_development_dependency('rubocop', '~> 0.72')
|
40
|
+
spec.add_development_dependency('rubocop-rspec', '~> 1.27')
|
41
|
+
spec.add_development_dependency('sqlite3', '~> 1.3')
|
42
|
+
end
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
version: '3.6'
|
2
|
+
services:
|
3
|
+
mysql:
|
4
|
+
image: mysql:5.7
|
5
|
+
expose:
|
6
|
+
- 3306
|
7
|
+
environment:
|
8
|
+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
|
9
|
+
- MYSQL_DATABASE=test
|
10
|
+
- TZ=America/Toronto
|
11
|
+
|
12
|
+
postgres:
|
13
|
+
image: postgres:11.1
|
14
|
+
expose:
|
15
|
+
- 5432
|
16
|
+
environment:
|
17
|
+
POSTGRES_PASSWORD: root
|
18
|
+
|
19
|
+
test:
|
20
|
+
volumes:
|
21
|
+
- .:/var/app
|
22
|
+
depends_on:
|
23
|
+
- kafka-broker
|
24
|
+
- mysql
|
25
|
+
- postgres
|
26
|
+
build: .
|
27
|
+
environment:
|
28
|
+
- "DEFAULT_TIMEOUT=${DEFAULT_TIMEOUT}"
|
29
|
+
- MYSQL_HOST=mysql
|
30
|
+
- PG_HOST=postgres
|
31
|
+
- SCHEMA_REGISTRY=http://schema-registry:8081
|
32
|
+
- KAFKA_SEED_BROKER=kafka-broker:9092
|
33
|
+
command: dockerize -wait tcp://mysql:3306 -wait tcp://postgres:5432 -timeout 1m rspec
|
34
|
+
|
35
|
+
zookeeper:
|
36
|
+
image: wurstmeister/zookeeper:latest
|
37
|
+
ports:
|
38
|
+
- 2181:2181
|
39
|
+
|
40
|
+
schema-registry:
|
41
|
+
image: confluentinc/cp-schema-registry
|
42
|
+
hostname: schema-registry
|
43
|
+
depends_on:
|
44
|
+
- zookeeper
|
45
|
+
- kafka-broker
|
46
|
+
ports:
|
47
|
+
- "8081:8081"
|
48
|
+
environment:
|
49
|
+
SCHEMA_REGISTRY_HOST_NAME: schema-registry
|
50
|
+
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: 'zookeeper:2181'
|
51
|
+
|
52
|
+
kafka-broker:
|
53
|
+
image: confluentinc/cp-enterprise-kafka
|
54
|
+
hostname: kafka-broker
|
55
|
+
depends_on:
|
56
|
+
- zookeeper
|
57
|
+
ports:
|
58
|
+
- "9092:9092"
|
59
|
+
environment:
|
60
|
+
KAFKA_BROKER_ID: 1
|
61
|
+
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
|
62
|
+
KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://kafka-broker:9092'
|
63
|
+
KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
|
64
|
+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
|
65
|
+
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
|
66
|
+
CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: kafka-broker:9092
|
67
|
+
CONFLUENT_METRICS_REPORTER_ZOOKEEPER_CONNECT: zookeeper:2181
|
68
|
+
CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
|
69
|
+
CONFLUENT_METRICS_ENABLE: 'true'
|
70
|
+
CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'
|
71
|
+
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# Database Backend Design
|
2
|
+
|
3
|
+
Kafka is a messaging protocol, while databases are transactional and relational.
|
4
|
+
Marrying the two (e.g. by using Kafka to publish changes to a database table)
|
5
|
+
is not a simple task. This document describes the problem and the current
|
6
|
+
implementation. There will be references to microservices architecture as that
|
7
|
+
informs some of the discussion.
|
8
|
+
|
9
|
+
## A Pure Solution
|
10
|
+
|
11
|
+
The purest solution is to use Kafka as the "source of truth" by first publishing
|
12
|
+
all messages synchronously to Kafka, and then using a consumer to read these
|
13
|
+
messages back into the database. If there are any errors sending to Kafka,
|
14
|
+
the thread will crash and no data would be written. If there are errors
|
15
|
+
reading the data back into the database, the data remains in Kafka and can
|
16
|
+
be re-read at any time.
|
17
|
+
|
18
|
+
There are several real-world problems with this pure solution:
|
19
|
+
|
20
|
+
1. The solution assumes that creating a consumer is a simple task, which is
|
21
|
+
definitely not the case. Depending on how many topics are being produced to,
|
22
|
+
a separate consumer thread per topic (which is how Phobos works) is overkill.
|
23
|
+
The other option is to introduce an entirely new consumer service to handle
|
24
|
+
consuming the topics we've already produced, which is even more overkill.
|
25
|
+
2. For CRUD interfaces or any other UI that saves data to the database, we do
|
26
|
+
not want to use an asynchronous method to ensure that data is published to
|
27
|
+
Kafka before saving, and then being able to serve that data back to the user
|
28
|
+
in a single API call, which is a common use case.
|
29
|
+
This could involve a large amount of added complexity and may force the user
|
30
|
+
to wait unnecessarily.
|
31
|
+
3. We want to make use of database transactions - i.e. if an error happens
|
32
|
+
saving one record, the others should roll back. Once a message is written to
|
33
|
+
Kafka, it can't be "rolled back" easily. Kafka transactions do exist but
|
34
|
+
they are not widely supported in Kafka clients, and we still would be
|
35
|
+
faced with the fact that one transaction (Kafka or database) could finish
|
36
|
+
and then the process could be killed before the other one could be finished.
|
37
|
+
4. We want to make use of auto-increment database IDs - we can't do this if we
|
38
|
+
write to Kafka first.
|
39
|
+
5. Kafka is an external dependency. If either the DB **or** Kafka goes down,
|
40
|
+
our app becomes unusable.
|
41
|
+
|
42
|
+
Using tools like Kafka Connect and Debezium are not ideal because:
|
43
|
+
|
44
|
+
1. They are tied very closely to the internal relational schema, which is not
|
45
|
+
ideal, especially for legacy systems. It makes it nearly impossible to make
|
46
|
+
internal changes.
|
47
|
+
2. They are separate services and connectors must be created for each
|
48
|
+
microservice separately, which is a large overhead.
|
49
|
+
|
50
|
+
## Database Backend Solution
|
51
|
+
|
52
|
+
We will be using the database itself as the source of our Kafka messages.
|
53
|
+
We will first write our messages to a database table and then asynchronously
|
54
|
+
send those messages to Kafka. This solves our problems:
|
55
|
+
|
56
|
+
1. The database is the (interim) source of truth. The Kafka message log is
|
57
|
+
essentially the changelog, which we can tail and send out. If our producing
|
58
|
+
thread errors out, a new one will simply pick up where it left off.
|
59
|
+
This ensures eventual consistency.
|
60
|
+
2. Because we are only using the database in the main application thread, we do
|
61
|
+
not need to wait for Kafka production to continue and can return immediately.
|
62
|
+
3. Because we are only saving to the database, we can use transactions normally
|
63
|
+
- if a transaction fails, it will roll back along with any Kafka messages we
|
64
|
+
intended to send.
|
65
|
+
4. Records are saved normally and messages are created after that, all as part
|
66
|
+
of the transaction, so we can use database IDs as usual.
|
67
|
+
5. We remove Kafka entirely as a dependency for normal work - the Kafka sending
|
68
|
+
piece is a separate thread.
|
69
|
+
|
70
|
+
The one downside to this is a slight delay (generally less than 1 second)
|
71
|
+
between the message being written to the database and sent to Kafka - in most
|
72
|
+
cases this is an acceptable limitation.
|
73
|
+
|
74
|
+
### The Implementation
|
75
|
+
|
76
|
+
The database backend consists of three tables:
|
77
|
+
|
78
|
+
* `kafka_messages` - this keeps track of the messages that were "published",
|
79
|
+
including the payload, topic, key and partition key. These messages
|
80
|
+
are *raw data* - all processing, including Avro encoding, must happen
|
81
|
+
upstream before they are inserted.
|
82
|
+
* `kafka_topic_info` - this table is essentially a lock table used to ensure
|
83
|
+
that only one producer thread is ever "working" on a topic at a time.
|
84
|
+
|
85
|
+
The backend code structure is such that when a producer calls `publish_list`,
|
86
|
+
it delegates that logic to the configured backend. A backend of `kafka`
|
87
|
+
or `kafka_async` will use existing Phobos logic. A backend of `db` will use
|
88
|
+
the database backend instead.
|
89
|
+
|
90
|
+
### "Publishing" A Message
|
91
|
+
|
92
|
+
When `publish_list` is called when the database backend is configured,
|
93
|
+
Deimos will instead save the message to the `kafka_messages` table.
|
94
|
+
|
95
|
+
### Sending Messages to Kafka
|
96
|
+
|
97
|
+
The database executor is started by calling `Deimos.start_db_backend!`
|
98
|
+
with a specified number of threads. These threads will continually scan the
|
99
|
+
two messages tables and send the messages to Kafka.
|
100
|
+
|
101
|
+
The algorithm for sending the messages makes use of the `kafka_topic_info` table as a lock table. There is also an `error` boolean column which is used to track when a topic has errored out. When this happens, the topic is marked as errored and will not be picked up for the next minute, after which it will be treated as any other topic. The full algorithm is as follows:
|
102
|
+
|
103
|
+
* Create a UUID for the thread - this is created once on thread start.
|
104
|
+
* Find all unique topics in the `kafka_messages` table.
|
105
|
+
* For each topic:
|
106
|
+
* Create an entry in `kafka_topic_info` for this topic if it doesn't exist.
|
107
|
+
* Run the following query:
|
108
|
+
|
109
|
+
```sql
|
110
|
+
UPDATE kafka_topic_info
|
111
|
+
SET locked_by=#{uuid}, locked_at=NOW(), error=0
|
112
|
+
WHERE (locked_by IS NULL AND error=0) OR locked_at < #{1.minute.ago}
|
113
|
+
LIMIT 1
|
114
|
+
```
|
115
|
+
* If the lock was unsuccessful, move on to the next topic in the list
|
116
|
+
* Find the first 1000 messages in `kafka_messages` for that topic, ordered by ID (insertion order)
|
117
|
+
* Send the messages synchronously to Kafka, with all brokers acking the message.
|
118
|
+
* Delete the records from the DB
|
119
|
+
* Update the `locked_at` timestamp in `kafka_topic_info` to `NOW()` to ensure liveness in case a particular batch took longer than expected to send.
|
120
|
+
* If the current batch is 1000 messages, repeat with the next batch of
|
121
|
+
messages until it returns less than 1000
|
122
|
+
* When all batches are sent:
|
123
|
+
* Unlock the topic by updating the `kafka_topic_info` for this topic, setting `locked_by=NULL, locked_at=NULL, error=0, retries=0`
|
124
|
+
* Move on to the next topic
|
125
|
+
* If there are errors sending a batch:
|
126
|
+
* Update the `kafka_topic_info` for this topic to have `locked_by=NULL, locked_at=NULL, error=1, retries=retries+1` - this will effectively keep it
|
127
|
+
locked for the next minute
|
128
|
+
* Move on to the next topic.
|
129
|
+
* When all topics are done, or if there are no topics, sleep for 0.5 seconds and begin again.
|
130
|
+
|
131
|
+
### Class / Method Design
|
132
|
+
|
133
|
+
The algorithm is split up into the following classes:
|
134
|
+
|
135
|
+
* Backends::Db - this is the class that saves the message to the database.
|
136
|
+
* KafkaMessage: This is an ActiveRecord class that handle saving the messages to the database and querying them.
|
137
|
+
* KafkaTopicInfo: This is an ActiveRecord class that handles locking, unlocking and heartbeating.
|
138
|
+
* Utils::SignalHandler: This is the equivalent of Phobos's Runner class and
|
139
|
+
handles the KILL, INT and TERM signals to gracefully shut down the threads.
|
140
|
+
* Utils::Executor is the equivalent of Phobos's Executor class and handles
|
141
|
+
the thread pool of producer threads.
|
142
|
+
* Utils::DbProducer is the producer thread itself which implements most of the
|
143
|
+
algorithm listed above.
|
144
|
+
|
145
|
+
### Caveats
|
146
|
+
|
147
|
+
There is one disadvantage of this pattern, which is that it is possible for events to be sent multiple times if the thread which sent the messages dies before being able to delete it from the database. In general this is an acceptable effect, since Kafka only guarantees at-least-once delivery in any case.
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Pull Request Template
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
|
6
|
+
|
7
|
+
Fixes # (issue)
|
8
|
+
|
9
|
+
## Type of change
|
10
|
+
|
11
|
+
Please delete options that are not relevant.
|
12
|
+
|
13
|
+
- [ ] Bug fix (non-breaking change which fixes an issue)
|
14
|
+
- [ ] New feature (non-breaking change which adds functionality)
|
15
|
+
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
16
|
+
- [ ] This change requires a documentation update
|
17
|
+
|
18
|
+
## How Has This Been Tested?
|
19
|
+
|
20
|
+
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
|
21
|
+
|
22
|
+
- [ ] Test A
|
23
|
+
- [ ] Test B
|
24
|
+
|
25
|
+
## Checklist:
|
26
|
+
|
27
|
+
- [ ] My code follows the style guidelines of this project
|
28
|
+
- [ ] I have performed a self-review of my own code
|
29
|
+
- [ ] I have commented my code, particularly in hard-to-understand areas
|
30
|
+
- [ ] I have made corresponding changes to the documentation
|
31
|
+
- [ ] My changes generate no new warnings
|
32
|
+
- [ ] I have added tests that prove my fix is effective or that my feature works
|
33
|
+
- [ ] New and existing unit tests pass locally with my changes
|
34
|
+
- [ ] Any dependent changes have been merged and published in downstream modules
|
data/lib/deimos.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avro-patches'
|
4
|
+
require 'avro_turf'
|
5
|
+
require 'phobos'
|
6
|
+
require 'deimos/version'
|
7
|
+
require 'deimos/avro_data_encoder'
|
8
|
+
require 'deimos/avro_data_decoder'
|
9
|
+
require 'deimos/producer'
|
10
|
+
require 'deimos/active_record_producer'
|
11
|
+
require 'deimos/active_record_consumer'
|
12
|
+
require 'deimos/consumer'
|
13
|
+
require 'deimos/configuration'
|
14
|
+
require 'deimos/instrumentation'
|
15
|
+
require 'deimos/utils/lag_reporter'
|
16
|
+
|
17
|
+
require 'deimos/publish_backend'
|
18
|
+
require 'deimos/backends/kafka'
|
19
|
+
require 'deimos/backends/kafka_async'
|
20
|
+
|
21
|
+
require 'deimos/monkey_patches/ruby_kafka_heartbeat'
|
22
|
+
require 'deimos/monkey_patches/schema_store'
|
23
|
+
require 'deimos/monkey_patches/phobos_producer'
|
24
|
+
require 'deimos/monkey_patches/phobos_cli'
|
25
|
+
|
26
|
+
require 'deimos/railtie' if defined?(Rails)
|
27
|
+
if defined?(ActiveRecord)
|
28
|
+
require 'deimos/kafka_source'
|
29
|
+
require 'deimos/kafka_topic_info'
|
30
|
+
require 'deimos/backends/db'
|
31
|
+
require 'deimos/utils/signal_handler.rb'
|
32
|
+
require 'deimos/utils/executor.rb'
|
33
|
+
require 'deimos/utils/db_producer.rb'
|
34
|
+
end
|
35
|
+
require 'deimos/utils/inline_consumer'
|
36
|
+
require 'yaml'
|
37
|
+
require 'erb'
|
38
|
+
|
39
|
+
# Parent module.
|
40
|
+
module Deimos
|
41
|
+
class << self
|
42
|
+
attr_accessor :config
|
43
|
+
|
44
|
+
# Configure Deimos.
|
45
|
+
def configure
|
46
|
+
first_time_config = self.config.nil?
|
47
|
+
self.config ||= Configuration.new
|
48
|
+
old_config = self.config.dup
|
49
|
+
yield(config)
|
50
|
+
|
51
|
+
# Don't re-configure Phobos every time
|
52
|
+
if first_time_config || config.phobos_config_changed?(old_config)
|
53
|
+
|
54
|
+
file = config.phobos_config_file
|
55
|
+
phobos_config = YAML.load(ERB.new(File.read(File.expand_path(file))).result)
|
56
|
+
|
57
|
+
configure_kafka_for_phobos(phobos_config)
|
58
|
+
configure_loggers(phobos_config)
|
59
|
+
|
60
|
+
Phobos.configure(phobos_config)
|
61
|
+
end
|
62
|
+
|
63
|
+
validate_db_backend if self.config.publish_backend == :db
|
64
|
+
end
|
65
|
+
|
66
|
+
# Ensure everything is set up correctly for the DB backend.
|
67
|
+
def validate_db_backend
|
68
|
+
begin
|
69
|
+
require 'activerecord-import'
|
70
|
+
rescue LoadError
|
71
|
+
raise 'Cannot set publish_backend to :db without activerecord-import! Please add it to your Gemfile.'
|
72
|
+
end
|
73
|
+
if Phobos.config.producer_hash[:required_acks] != :all
|
74
|
+
raise 'Cannot set publish_backend to :db unless required_acks is set to ":all" in phobos.yml!'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Start the DB producers to send Kafka messages.
|
79
|
+
# @param thread_count [Integer] the number of threads to start.
|
80
|
+
def start_db_backend!(thread_count: 1)
|
81
|
+
return if self.config.publish_backend != :db ||
|
82
|
+
thread_count.nil? ||
|
83
|
+
thread_count.zero?
|
84
|
+
|
85
|
+
producers = (1..thread_count).map do
|
86
|
+
Deimos::Utils::DbProducer.new(self.config.logger)
|
87
|
+
end
|
88
|
+
executor = Deimos::Utils::Executor.new(producers,
|
89
|
+
self.config.logger)
|
90
|
+
signal_handler = Deimos::Utils::SignalHandler.new(executor)
|
91
|
+
run_db_backend_in_thread(signal_handler)
|
92
|
+
end
|
93
|
+
|
94
|
+
# @param signal_handler [Deimos::Utils::SignalHandler]
|
95
|
+
def run_db_backend_in_thread(signal_handler)
|
96
|
+
Thread.new { signal_handler.run! }
|
97
|
+
end
|
98
|
+
|
99
|
+
# @param phobos_config [Hash]
|
100
|
+
def configure_kafka_for_phobos(phobos_config)
|
101
|
+
if config.ssl_enabled
|
102
|
+
%w(ssl_ca_cert ssl_client_cert ssl_client_cert_key).each do |key|
|
103
|
+
next if config.send(key).blank?
|
104
|
+
|
105
|
+
phobos_config['kafka'][key] = ssl_var_contents(config.send(key))
|
106
|
+
end
|
107
|
+
end
|
108
|
+
phobos_config['kafka']['seed_brokers'] = config.seed_broker if config.seed_broker
|
109
|
+
end
|
110
|
+
|
111
|
+
# @param phobos_config [Hash]
|
112
|
+
def configure_loggers(phobos_config)
|
113
|
+
phobos_config['custom_logger'] = config.phobos_logger
|
114
|
+
phobos_config['custom_kafka_logger'] = config.kafka_logger
|
115
|
+
end
|
116
|
+
|
117
|
+
# @param filename [String] a file to read, or the contents of the SSL var
|
118
|
+
# @return [String] the contents of the file
|
119
|
+
def ssl_var_contents(filename)
|
120
|
+
File.exist?(filename) ? File.read(filename) : filename
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
at_exit do
|
126
|
+
begin
|
127
|
+
Deimos::Backends::KafkaAsync.producer.async_producer_shutdown
|
128
|
+
Deimos::Backends::KafkaAsync.producer.kafka_client&.close
|
129
|
+
rescue StandardError => e
|
130
|
+
Deimos.config.logger.error(
|
131
|
+
"Error closing async producer on shutdown: #{e.message} #{e.backtrace.join("\n")}"
|
132
|
+
)
|
133
|
+
end
|
134
|
+
end
|