kafka-consumer 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/kafka/consumer.rb +2 -2
- data/lib/kafka/consumer/version.rb +1 -1
- data/lib/kazoo/cli.rb +2 -3
- data/lib/kazoo/cluster.rb +8 -12
- data/lib/kazoo/consumergroup.rb +24 -38
- data/lib/kazoo/partition.rb +1 -1
- data/lib/kazoo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e0b4f66c8e873f254c213a4d12ec89a2a46134a
|
4
|
+
data.tar.gz: 0dcc6b6cdce939337a51acebc95b9c7d8ac6b757
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22f95d7e30a7c4e8c83b1e2ddea5c962a88b1655e19b68df9eadc670286f5a5ae98376b95e7eac9fd85e5f6c13d78e74f009787b5e0eaeb64f4217656a7df104
|
7
|
+
data.tar.gz: f4b831cb109d4ee7b05efa2c61cb3ba00874088e62b03d83af981c10ce411a4dc10535f56a163bc7058f0f5a7d27cac286dff4ef4ad4b359126f757f843583a0
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ It's your responsibility to deal with this if that is a problem for you, e.g. by
|
|
17
17
|
|
18
18
|
## Usage
|
19
19
|
|
20
|
-
First, add `kafka-consumer` to your **Gemfile**, and run `bundle install
|
20
|
+
First, add `kafka-consumer` to your **Gemfile**, and run `bundle install`.
|
21
21
|
If your messages are snappy-compressed, add the `snappy` gem as well.
|
22
22
|
|
23
23
|
``` ruby
|
data/lib/kafka/consumer.rb
CHANGED
@@ -18,12 +18,12 @@ module Kafka
|
|
18
18
|
:max_wait_ms, :initial_offset,
|
19
19
|
:logger
|
20
20
|
|
21
|
-
def initialize(name, subscription, zookeeper: [],
|
21
|
+
def initialize(name, subscription, zookeeper: [], max_wait_ms: 200, initial_offset: :latest_offset, logger: nil)
|
22
22
|
@name, @subscription = name, subscription
|
23
23
|
@max_wait_ms, @initial_offset = max_wait_ms, initial_offset
|
24
24
|
@logger = logger || Logger.new($stdout)
|
25
25
|
|
26
|
-
@cluster = Kazoo::Cluster.new(zookeeper
|
26
|
+
@cluster = Kazoo::Cluster.new(zookeeper)
|
27
27
|
@group = Kazoo::Consumergroup.new(@cluster, name)
|
28
28
|
@group.create unless @group.exists?
|
29
29
|
|
data/lib/kazoo/cli.rb
CHANGED
@@ -3,8 +3,7 @@ require 'thor'
|
|
3
3
|
|
4
4
|
module Kazoo
|
5
5
|
class CLI < Thor
|
6
|
-
class_option :zookeeper, :type => :string, :default => ENV['
|
7
|
-
class_option :chroot, :type => :string, :default => ""
|
6
|
+
class_option :zookeeper, :type => :string, :default => ENV['ZOOKEEPER']
|
8
7
|
|
9
8
|
desc "cluster", "Describes the Kafka cluster as registered in Zookeeper"
|
10
9
|
def cluster
|
@@ -73,7 +72,7 @@ module Kazoo
|
|
73
72
|
end
|
74
73
|
|
75
74
|
def kafka_cluster
|
76
|
-
@kafka_cluster ||= Kazoo::Cluster.new(options[:zookeeper]
|
75
|
+
@kafka_cluster ||= Kazoo::Cluster.new(options[:zookeeper])
|
77
76
|
end
|
78
77
|
end
|
79
78
|
end
|
data/lib/kazoo/cluster.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Kazoo
|
2
2
|
class Cluster
|
3
3
|
|
4
|
-
attr_reader :zookeeper
|
4
|
+
attr_reader :zookeeper
|
5
5
|
|
6
|
-
def initialize(zookeeper
|
7
|
-
@zookeeper
|
6
|
+
def initialize(zookeeper)
|
7
|
+
@zookeeper = zookeeper
|
8
8
|
@zk_mutex, @brokers_mutex, @topics_mutex, @consumergroups_mutex = Mutex.new, Mutex.new, Mutex.new, Mutex.new
|
9
9
|
end
|
10
10
|
|
@@ -17,11 +17,11 @@ module Kazoo
|
|
17
17
|
def brokers
|
18
18
|
@brokers_mutex.synchronize do
|
19
19
|
@brokers ||= begin
|
20
|
-
brokers = zk.get_children(path:
|
20
|
+
brokers = zk.get_children(path: "/brokers/ids")
|
21
21
|
result, threads, mutex = {}, ThreadGroup.new, Mutex.new
|
22
22
|
brokers.fetch(:children).map do |id|
|
23
23
|
t = Thread.new do
|
24
|
-
broker_info = zk.get(path:
|
24
|
+
broker_info = zk.get(path: "/brokers/ids/#{id}")
|
25
25
|
broker = Kazoo::Broker.from_json(self, id, JSON.parse(broker_info.fetch(:data)))
|
26
26
|
mutex.synchronize { result[id.to_i] = broker }
|
27
27
|
end
|
@@ -35,7 +35,7 @@ module Kazoo
|
|
35
35
|
|
36
36
|
def consumergroups
|
37
37
|
@consumergroups ||= begin
|
38
|
-
consumers = zk.get_children(path:
|
38
|
+
consumers = zk.get_children(path: "/consumers")
|
39
39
|
consumers.fetch(:children).map { |name| Kazoo::Consumergroup.new(self, name) }
|
40
40
|
end
|
41
41
|
end
|
@@ -43,11 +43,11 @@ module Kazoo
|
|
43
43
|
def topics
|
44
44
|
@topics_mutex.synchronize do
|
45
45
|
@topics ||= begin
|
46
|
-
topics = zk.get_children(path:
|
46
|
+
topics = zk.get_children(path: "/brokers/topics")
|
47
47
|
result, threads, mutex = {}, ThreadGroup.new, Mutex.new
|
48
48
|
topics.fetch(:children).each do |name|
|
49
49
|
t = Thread.new do
|
50
|
-
topic_info = zk.get(path:
|
50
|
+
topic_info = zk.get(path: "/brokers/topics/#{name}")
|
51
51
|
topic = Kazoo::Topic.from_json(self, name, JSON.parse(topic_info.fetch(:data)))
|
52
52
|
mutex.synchronize { result[name] = topic }
|
53
53
|
end
|
@@ -71,10 +71,6 @@ module Kazoo
|
|
71
71
|
partitions.any?(&:under_replicated?)
|
72
72
|
end
|
73
73
|
|
74
|
-
def node_with_chroot(path)
|
75
|
-
"#{@chroot}#{path}"
|
76
|
-
end
|
77
|
-
|
78
74
|
def close
|
79
75
|
zk.close
|
80
76
|
end
|
data/lib/kazoo/consumergroup.rb
CHANGED
@@ -7,14 +7,14 @@ module Kazoo
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def create
|
10
|
-
cluster.zk.create(path:
|
11
|
-
cluster.zk.create(path:
|
12
|
-
cluster.zk.create(path:
|
13
|
-
cluster.zk.create(path:
|
10
|
+
cluster.zk.create(path: "/consumers/#{name}")
|
11
|
+
cluster.zk.create(path: "/consumers/#{name}/ids")
|
12
|
+
cluster.zk.create(path: "/consumers/#{name}/owners")
|
13
|
+
cluster.zk.create(path: "/consumers/#{name}/offsets")
|
14
14
|
end
|
15
15
|
|
16
16
|
def exists?
|
17
|
-
stat = cluster.zk.stat(path:
|
17
|
+
stat = cluster.zk.stat(path: "/consumers/#{name}")
|
18
18
|
stat.fetch(:stat).exists?
|
19
19
|
end
|
20
20
|
|
@@ -24,23 +24,19 @@ module Kazoo
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def instances
|
27
|
-
instances = cluster.zk.get_children(path:
|
27
|
+
instances = cluster.zk.get_children(path: "/consumers/#{name}/ids")
|
28
28
|
instances.fetch(:children).map { |id| Instance.new(self, id: id) }
|
29
29
|
end
|
30
30
|
|
31
31
|
def watch_instances(&block)
|
32
32
|
cb = Zookeeper::Callbacks::WatcherCallback.create(&block)
|
33
|
-
result = cluster.zk.get_children(
|
34
|
-
path: cluster.node_with_chroot("/consumers/#{name}/ids"),
|
35
|
-
watcher: cb,
|
36
|
-
)
|
37
|
-
|
38
|
-
instances = result.fetch(:children).map { |id| Instance.new(self, id: id) }
|
33
|
+
result = cluster.zk.get_children(path: "/consumers/#{name}/ids", watcher: cb)
|
39
34
|
|
40
35
|
if result.fetch(:rc) != Zookeeper::Constants::ZOK
|
41
36
|
raise Kazoo::Error, "Failed to watch instances. Error code result[:rc]"
|
42
37
|
end
|
43
38
|
|
39
|
+
instances = result.fetch(:children).map { |id| Instance.new(self, id: id) }
|
44
40
|
[instances, cb]
|
45
41
|
end
|
46
42
|
|
@@ -48,10 +44,7 @@ module Kazoo
|
|
48
44
|
def watch_partition_claim(partition, &block)
|
49
45
|
cb = Zookeeper::Callbacks::WatcherCallback.create(&block)
|
50
46
|
|
51
|
-
result = cluster.zk.get(
|
52
|
-
path: cluster.node_with_chroot("/consumers/#{name}/owners/#{partition.topic.name}/#{partition.id}"),
|
53
|
-
watcher: cb
|
54
|
-
)
|
47
|
+
result = cluster.zk.get(path: "/consumers/#{name}/owners/#{partition.topic.name}/#{partition.id}", watcher: cb)
|
55
48
|
|
56
49
|
case result.fetch(:rc)
|
57
50
|
when Zookeeper::Constants::ZNONODE # Nobody is claiming this partition yet
|
@@ -64,7 +57,7 @@ module Kazoo
|
|
64
57
|
end
|
65
58
|
|
66
59
|
def retrieve_offset(partition)
|
67
|
-
result = cluster.zk.get(path:
|
60
|
+
result = cluster.zk.get(path: "/consumers/#{name}/offsets/#{partition.topic.name}/#{partition.id}")
|
68
61
|
case result.fetch(:rc)
|
69
62
|
when Zookeeper::Constants::ZOK;
|
70
63
|
result.fetch(:data).to_i
|
@@ -76,23 +69,16 @@ module Kazoo
|
|
76
69
|
end
|
77
70
|
|
78
71
|
def commit_offset(partition, offset)
|
79
|
-
result = cluster.zk.set(
|
80
|
-
path: cluster.node_with_chroot("/consumers/#{name}/offsets/#{partition.topic.name}/#{partition.id}"),
|
81
|
-
data: (offset + 1).to_s
|
82
|
-
)
|
83
|
-
|
72
|
+
result = cluster.zk.set(path: "/consumers/#{name}/offsets/#{partition.topic.name}/#{partition.id}", data: (offset + 1).to_s)
|
84
73
|
if result.fetch(:rc) == Zookeeper::Constants::ZNONODE
|
85
|
-
result = cluster.zk.create(path:
|
74
|
+
result = cluster.zk.create(path: "/consumers/#{name}/offsets/#{partition.topic.name}")
|
86
75
|
case result.fetch(:rc)
|
87
76
|
when Zookeeper::Constants::ZOK, Zookeeper::Constants::ZNODEEXISTS
|
88
77
|
else
|
89
78
|
raise Kazoo::Error, "Failed to commit offset #{offset} for partition #{partition.topic.name}/#{partition.id}. Error code: #{result.fetch(:rc)}"
|
90
79
|
end
|
91
80
|
|
92
|
-
result = cluster.zk.create(
|
93
|
-
path: cluster.node_with_chroot("/consumers/#{name}/offsets/#{partition.topic.name}/#{partition.id}"),
|
94
|
-
data: (offset + 1).to_s
|
95
|
-
)
|
81
|
+
result = cluster.zk.create(path: "/consumers/#{name}/offsets/#{partition.topic.name}/#{partition.id}", data: (offset + 1).to_s)
|
96
82
|
end
|
97
83
|
|
98
84
|
if result.fetch(:rc) != Zookeeper::Constants::ZOK
|
@@ -101,19 +87,19 @@ module Kazoo
|
|
101
87
|
end
|
102
88
|
|
103
89
|
def reset_offsets
|
104
|
-
result = cluster.zk.get_children(path:
|
90
|
+
result = cluster.zk.get_children(path: "/consumers/#{name}/offsets")
|
105
91
|
raise Kazoo::Error unless result.fetch(:rc) == Zookeeper::Constants::ZOK
|
106
92
|
|
107
93
|
result.fetch(:children).each do |topic|
|
108
|
-
result = cluster.zk.get_children(path:
|
94
|
+
result = cluster.zk.get_children(path: "/consumers/#{name}/offsets/#{topic}")
|
109
95
|
raise Kazoo::Error unless result.fetch(:rc) == Zookeeper::Constants::ZOK
|
110
96
|
|
111
97
|
result.fetch(:children).each do |partition|
|
112
|
-
cluster.zk.delete(path:
|
98
|
+
cluster.zk.delete(path: "/consumers/#{name}/offsets/#{topic}/#{partition}")
|
113
99
|
raise Kazoo::Error unless result.fetch(:rc) == Zookeeper::Constants::ZOK
|
114
100
|
end
|
115
101
|
|
116
|
-
cluster.zk.delete(path:
|
102
|
+
cluster.zk.delete(path: "/consumers/#{name}/offsets/#{topic}")
|
117
103
|
raise Kazoo::Error unless result.fetch(:rc) == Zookeeper::Constants::ZOK
|
118
104
|
end
|
119
105
|
end
|
@@ -146,13 +132,13 @@ module Kazoo
|
|
146
132
|
end
|
147
133
|
|
148
134
|
def registered?
|
149
|
-
stat = cluster.zk.stat(path:
|
135
|
+
stat = cluster.zk.stat(path: "/consumers/#{group.name}/ids/#{id}")
|
150
136
|
stat.fetch(:stat).exists?
|
151
137
|
end
|
152
138
|
|
153
139
|
def register(subscription)
|
154
140
|
result = cluster.zk.create(
|
155
|
-
path:
|
141
|
+
path: "/consumers/#{group.name}/ids/#{id}",
|
156
142
|
ephemeral: true,
|
157
143
|
data: JSON.generate({
|
158
144
|
version: 1,
|
@@ -167,9 +153,9 @@ module Kazoo
|
|
167
153
|
end
|
168
154
|
|
169
155
|
subscription.each do |topic|
|
170
|
-
stat = cluster.zk.stat(path:
|
156
|
+
stat = cluster.zk.stat(path: "/consumers/#{group.name}/owners/#{topic.name}")
|
171
157
|
unless stat.fetch(:stat).exists?
|
172
|
-
result = cluster.zk.create(path:
|
158
|
+
result = cluster.zk.create(path: "/consumers/#{group.name}/owners/#{topic.name}")
|
173
159
|
if result.fetch(:rc) != Zookeeper::Constants::ZOK
|
174
160
|
raise Kazoo::ConsumerInstanceRegistrationFailed, "Failed to register subscription of #{topic.name} for consumer group #{group.name}! Error code: #{result.fetch(:rc)}"
|
175
161
|
end
|
@@ -178,12 +164,12 @@ module Kazoo
|
|
178
164
|
end
|
179
165
|
|
180
166
|
def deregister
|
181
|
-
cluster.zk.delete(path:
|
167
|
+
cluster.zk.delete(path: "/consumers/#{group.name}/ids/#{id}")
|
182
168
|
end
|
183
169
|
|
184
170
|
def claim_partition(partition)
|
185
171
|
result = cluster.zk.create(
|
186
|
-
path:
|
172
|
+
path: "/consumers/#{group.name}/owners/#{partition.topic.name}/#{partition.id}",
|
187
173
|
ephemeral: true,
|
188
174
|
data: id,
|
189
175
|
)
|
@@ -199,7 +185,7 @@ module Kazoo
|
|
199
185
|
end
|
200
186
|
|
201
187
|
def release_partition(partition)
|
202
|
-
result = cluster.zk.delete(path:
|
188
|
+
result = cluster.zk.delete(path: "/consumers/#{group.name}/owners/#{partition.topic.name}/#{partition.id}")
|
203
189
|
if result.fetch(:rc) != Zookeeper::Constants::ZOK
|
204
190
|
raise Kazoo::Error, "Failed to release partition #{partition.topic.name}/#{partition.id}. Error code: #{result.fetch(:rc)}"
|
205
191
|
end
|
data/lib/kazoo/partition.rb
CHANGED
@@ -50,7 +50,7 @@ module Kazoo
|
|
50
50
|
protected
|
51
51
|
|
52
52
|
def refresh_state
|
53
|
-
state_json = cluster.zk.get(path:
|
53
|
+
state_json = cluster.zk.get(path: "/brokers/topics/#{topic.name}/partitions/#{id}/state")
|
54
54
|
set_state(JSON.parse(state_json.fetch(:data)))
|
55
55
|
end
|
56
56
|
|
data/lib/kazoo/version.rb
CHANGED