kazoo-ruby 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70929027601215b692188a34a3996cd02cad834e
4
- data.tar.gz: 86ac0c773242273960484dadb3ad21cf986fc02c
3
+ metadata.gz: 30dc75656a63c816b7af84a6f013a8fc0455d053
4
+ data.tar.gz: 9d14d2fda925b544d932a08ef693376d93198d2c
5
5
  SHA512:
6
- metadata.gz: bbb219d17b2b0be9d829702d34be76eecdf51c85f9b27bf2a3c19e0acb9ba352324e85a335b0d33b727c6c5ffb8f95f8254c289e270cec92659cc6a1aaa43f5f
7
- data.tar.gz: 199b6acea7b81fab0d6179d81b5a0d745c45adf1dcd3198d813bc5c26bb4a58d7f0c87b51fe8ddf5627f794b23a00323023fc4defc70ff837a098cf83d3751df
6
+ metadata.gz: 6966ad7d849f74a8d37c50b856e8a9fbf9d512fe59d42cf00063b25be211c6d08505f2fbdbdeb10e94892677369cfa0abfe39ae9f664576405a5b46a7083ab17
7
+ data.tar.gz: be22baa2a58a49278d8bd7f6222916281a60a7783d8e8f18ed89258d33d5bd4cec9f43f7a016b1e13d62dde3f971513367be129978a33fc20b43d8084a9074b6
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require "rake/testtask"
3
3
 
4
4
  Rake::TestTask.new do |t|
5
5
  t.libs = ["lib", "test"]
6
- t.test_files = FileList['test/*_test.rb']
6
+ t.test_files = FileList['test/**/*_test.rb']
7
7
  end
8
8
 
9
9
  task :default => :test
@@ -8,7 +8,9 @@ module Kazoo
8
8
  Error = Class.new(StandardError)
9
9
 
10
10
  ValidationError = Class.new(Kazoo::Error)
11
+ VersionNotSupported = Class.new(Kazoo::Error)
11
12
  NoClusterRegistered = Class.new(Kazoo::Error)
13
+ TopicNotFound = Class.new(Kazoo::Error)
12
14
  ConsumerInstanceRegistrationFailed = Class.new(Kazoo::Error)
13
15
  PartitionAlreadyClaimed = Class.new(Kazoo::Error)
14
16
  ReleasePartitionFailure = Class.new(Kazoo::Error)
@@ -87,6 +87,8 @@ module Kazoo
87
87
  end
88
88
 
89
89
  def set_state(json)
90
+ raise Kazoo::VersionNotSupported unless json.fetch('version') == 1
91
+
90
92
  @leader = cluster.brokers.fetch(json.fetch('leader'))
91
93
  @isr = json.fetch('isr').map { |r| cluster.brokers.fetch(r) }
92
94
  end
@@ -11,6 +11,8 @@ module Kazoo
11
11
  end
12
12
 
13
13
  def self.from_json(cluster, name, json)
14
+ raise Kazoo::VersionNotSupported unless json.fetch('version') == 1
15
+
14
16
  topic = new(cluster, name)
15
17
  topic.partitions = json.fetch('partitions').map do |(id, replicas)|
16
18
  topic.partition(id.to_i, replicas: replicas.map { |b| cluster.brokers[b] })
@@ -82,9 +84,18 @@ module Kazoo
82
84
  raise Kazoo::Error, "The topic #{name} already exists!" if exists?
83
85
  validate
84
86
 
87
+ result = cluster.zk.create(
88
+ path: "/config/topics/#{name}",
89
+ data: JSON.generate(version: 1, config: {})
90
+ )
91
+
92
+ if result.fetch(:rc) != Zookeeper::Constants::ZOK
93
+ raise Kazoo::Error, "Failed to create topic config node for #{name}. Error code: #{result.fetch(:rc)}"
94
+ end
95
+
85
96
  result = cluster.zk.create(
86
97
  path: "/brokers/topics/#{name}",
87
- data: JSON.dump(version: 1, partitions: partitions_as_json)
98
+ data: JSON.generate(version: 1, partitions: partitions_as_json)
88
99
  )
89
100
 
90
101
  if result.fetch(:rc) != Zookeeper::Constants::ZOK
@@ -107,10 +118,10 @@ module Kazoo
107
118
 
108
119
  result = cluster.zk.stat(path: "/brokers/topics/#{name}", watcher: cb)
109
120
  case result.fetch(:rc)
110
- when Zookeeper::Constants::ZOK
121
+ when Zookeeper::Constants::ZOK
111
122
  # continue
112
123
  when Zookeeper::Constants::ZNONODE
113
- raise Kazoo::Error, "Topic #{name} does not exist!"
124
+ raise Kazoo::TopicNotFound, "Topic #{name} does not exist!"
114
125
  else
115
126
  raise Kazoo::Error, "Failed to monitor topic"
116
127
  end
@@ -127,6 +138,61 @@ module Kazoo
127
138
  end
128
139
  end
129
140
 
141
+ def config
142
+ result = cluster.zk.get(path: "/config/topics/#{name}")
143
+ case result.fetch(:rc)
144
+ when Zookeeper::Constants::ZOK
145
+ # continue
146
+ when Zookeeper::Constants::ZNONODE
147
+ raise Kazoo::TopicNotFound, "Topic #{name} does not exist!"
148
+ else
149
+ raise Kazoo::Error, "Failed to set topic config"
150
+ end
151
+
152
+ config = JSON.parse(result.fetch(:data))
153
+ raise Kazoo::VersionNotSupported if config.fetch('version') != 1
154
+
155
+ config.fetch('config')
156
+ end
157
+
158
+ def set_config(key, value)
159
+ new_config = config
160
+ new_config[key.to_s] = value.to_s
161
+ write_config(new_config)
162
+ end
163
+
164
+ def delete_config(key)
165
+ new_config = config
166
+ new_config.delete(key.to_s)
167
+ write_config(new_config)
168
+ end
169
+
170
+ def reset_default_config
171
+ write_config({})
172
+ end
173
+
174
+ def write_config(config_hash)
175
+ raise Kazoo::TopicNotFound, "Topic #{name.inspect} does not exist" unless exists?
176
+
177
+ config = config_hash.inject({}) { |h, (k,v)| h[k.to_s] = v.to_s; h }
178
+ config_json = JSON.generate(version: 1, config: config)
179
+
180
+ # Set topic config
181
+ result = cluster.zk.set(path: "/config/topics/#{name}", data: config_json)
182
+ case result.fetch(:rc)
183
+ when Zookeeper::Constants::ZOK
184
+ # continue
185
+ when Zookeeper::Constants::ZNONODE
186
+ raise Kazoo::TopicNotFound, "Topic #{name} does not exist!"
187
+ else
188
+ raise Kazoo::Error, "Failed to set topic config"
189
+ end
190
+
191
+ # Set config change notification
192
+ result = cluster.zk.create(path: "/config/changes/config_change_", data: name.inspect, sequence: true)
193
+ raise Kazoo::Error, "Failed to set topic config change notification" unless result.fetch(:rc) == Zookeeper::Constants::ZOK
194
+ end
195
+
130
196
  def self.create(cluster, name, partitions: nil, replication_factor: nil)
131
197
  topic = new(cluster, name)
132
198
  topic.send(:sequentially_assign_partitions, partitions, replication_factor)
@@ -1,3 +1,3 @@
1
1
  module Kazoo
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -19,4 +19,21 @@ class FunctionalTopicManagementTest < Minitest::Test
19
19
  refute topic.exists?
20
20
  refute @cluster.topics.key?(topic.name)
21
21
  end
22
+
23
+ def test_topic_config_management
24
+ topic = @cluster.create_topic('test.kazoo.config', partitions: 1, replication_factor: 1)
25
+
26
+ topic.write_config("flush.messages" => 1, "max.message.bytes" => 64000)
27
+ assert_equal "1", topic.config["flush.messages"]
28
+ assert_equal "64000", topic.config["max.message.bytes"]
29
+
30
+ topic.set_config("max.message.bytes", 128000)
31
+ topic.delete_config("flush.messages")
32
+ assert_equal topic.config, { "max.message.bytes" => "128000" }
33
+
34
+ topic.reset_default_config
35
+ assert_equal Hash.new, topic.config
36
+
37
+ topic.destroy
38
+ end
22
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kazoo-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-28 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor