ruby-kafka 0.1.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a697874e8d60e20fefd56d530fae7af5d9a0122
4
+ data.tar.gz: c93b6564dde9e1630a1675a13dbc4227f4aa0053
5
+ SHA512:
6
+ metadata.gz: e64575cd8677e03e565f9ed64d7b9d6b548a80ab68c7f37d108a0f6d95161843acef2805a625db9d9502d5861fe166200b048119835209162235d9c4783d679b
7
+ data.tar.gz: 4caa3012931623f4f3372368c195736aee833995b76937b7e2dd8bea5dec2fa9ce31dc1bbb209d435144884a158e674b6fd55fb5087a0f971cc9245c678229cd
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kafka.gemspec
4
+ gemspec
5
+
6
+ gem "dotenv"
data/LICENSE.txt ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Kafka
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kafka`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kafka'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kafka
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kafka.
36
+
37
+
38
+ ## Copyright and license
39
+
40
+ Copyright 2015 Zendesk
41
+
42
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
43
+
44
+ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
45
+
46
+ 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.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kafka"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/kafka.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kafka/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-kafka"
8
+ spec.version = Kafka::VERSION
9
+ spec.authors = ["Daniel Schierbeck"]
10
+ spec.email = ["daniel.schierbeck@gmail.com"]
11
+
12
+ spec.summary = %q{A client library for the Kafka distributed commit log.}
13
+ spec.description = %q{A client library for the Kafka distributed commit log. Still very much at the alpha stage.}
14
+ spec.homepage = "https://github.com/zendesk/ruby-kafka"
15
+ spec.license = "Apache License Version 2.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
data/lib/kafka.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "kafka/version"
2
+ require "kafka/cluster"
3
+ require "kafka/producer"
4
+
5
+ module Kafka
6
+ def self.new(**options)
7
+ Cluster.new(**options)
8
+ end
9
+ end
@@ -0,0 +1,53 @@
1
+ require "logger"
2
+ require "kafka/connection"
3
+ require "kafka/protocol"
4
+
5
+ module Kafka
6
+ class Cluster
7
+ def self.connect(brokers:, client_id:, logger:)
8
+ host, port = brokers.first.split(":", 2)
9
+
10
+ connection = Connection.new(
11
+ host: host,
12
+ port: port.to_i,
13
+ client_id: client_id,
14
+ logger: logger
15
+ )
16
+
17
+ connection.open
18
+
19
+ new(connection: connection, logger: logger)
20
+ end
21
+
22
+ def initialize(connection:, logger: nil)
23
+ @connection = connection
24
+ @logger = logger
25
+ end
26
+
27
+ def fetch_metadata(**options)
28
+ api_key = Protocol::TOPIC_METADATA_API_KEY
29
+ request = Protocol::TopicMetadataRequest.new(**options)
30
+ response = Protocol::MetadataResponse.new
31
+
32
+ @connection.write_request(api_key, request)
33
+ @connection.read_response(response)
34
+
35
+ response
36
+ end
37
+
38
+ def produce(**options)
39
+ api_key = Protocol::PRODUCE_API_KEY
40
+ request = Protocol::ProduceRequest.new(**options)
41
+
42
+ @connection.write_request(api_key, request)
43
+
44
+ if request.requires_acks?
45
+ response = Protocol::ProduceResponse.new
46
+ @connection.read_response(response)
47
+ response
48
+ else
49
+ nil
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ require "socket"
2
+ require "kafka/protocol/request_message"
3
+ require "kafka/protocol/encoder"
4
+ require "kafka/protocol/decoder"
5
+
6
+ module Kafka
7
+ ConnectionError = Class.new(StandardError)
8
+
9
+ class Connection
10
+ def initialize(host:, port:, client_id:, logger:)
11
+ @host = host
12
+ @port = port
13
+ @client_id = client_id
14
+ @logger = logger
15
+ @socket = nil
16
+ @correlation_id = 0
17
+ @socket_timeout = 1000
18
+ end
19
+
20
+ def open
21
+ @logger.info "Opening connection to #{@host}:#{@port} with client id #{@client_id}..."
22
+
23
+ @socket = TCPSocket.new(@host, @port)
24
+ rescue SocketError => e
25
+ @logger.error "Failed to connect to #{@host}:#{@port}: #{e}"
26
+
27
+ raise ConnectionError, e
28
+ end
29
+
30
+ def write_request(api_key, request)
31
+ @correlation_id += 1
32
+
33
+ message = Kafka::Protocol::RequestMessage.new(
34
+ api_key: api_key,
35
+ api_version: 0,
36
+ correlation_id: @correlation_id,
37
+ client_id: @client_id,
38
+ request: request,
39
+ )
40
+
41
+ buffer = StringIO.new
42
+ message_encoder = Kafka::Protocol::Encoder.new(buffer)
43
+ message.encode(message_encoder)
44
+
45
+ @logger.info "Sending request #{@correlation_id} (#{request.class})..."
46
+
47
+ connection_encoder = Kafka::Protocol::Encoder.new(@socket)
48
+ connection_encoder.write_bytes(buffer.string)
49
+ end
50
+
51
+ def read_response(response)
52
+ @logger.info "Reading response #{response.class}"
53
+
54
+ connection_decoder = Kafka::Protocol::Decoder.new(@socket)
55
+ bytes = connection_decoder.bytes
56
+
57
+ buffer = StringIO.new(bytes)
58
+ response_decoder = Kafka::Protocol::Decoder.new(buffer)
59
+
60
+ correlation_id = response_decoder.int32
61
+
62
+ @logger.info "Correlation id #{correlation_id}"
63
+
64
+ response.decode(response_decoder)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,35 @@
1
+ require "kafka/protocol/message"
2
+
3
+ module Kafka
4
+ class Producer
5
+ # @param timeout [Integer] The number of milliseconds to wait for an
6
+ # acknowledgement from the broker before timing out.
7
+ # @param required_acks [Integer] The number of replicas that must acknowledge
8
+ # a write.
9
+ def initialize(cluster:, logger:, timeout: 10_000, required_acks: 1)
10
+ @cluster = cluster
11
+ @logger = logger
12
+ @required_acks = required_acks
13
+ @timeout = timeout
14
+ @buffer = {}
15
+ end
16
+
17
+ def write(value, key:, topic:, partition:)
18
+ message = Protocol::Message.new(value: value, key: key)
19
+
20
+ @buffer[topic] ||= {}
21
+ @buffer[topic][partition] ||= []
22
+ @buffer[topic][partition] << message
23
+ end
24
+
25
+ def flush
26
+ @cluster.produce(
27
+ required_acks: @required_acks,
28
+ timeout: @timeout,
29
+ messages_for_topics: @buffer
30
+ )
31
+
32
+ @buffer = {}
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module Kafka
2
+ module Protocol
3
+ PRODUCE_API_KEY = 0
4
+ TOPIC_METADATA_API_KEY = 3
5
+ end
6
+ end
7
+
8
+ require "kafka/protocol/topic_metadata_request"
9
+ require "kafka/protocol/metadata_response"
10
+ require "kafka/protocol/produce_request"
11
+ require "kafka/protocol/produce_response"
@@ -0,0 +1,54 @@
1
+ module Kafka
2
+ module Protocol
3
+ class Decoder
4
+ def initialize(io)
5
+ @io = io
6
+ end
7
+
8
+ def int8
9
+ read(1).unpack("C").first
10
+ end
11
+
12
+ def int16
13
+ read(2).unpack("s>").first
14
+ end
15
+
16
+ def int32
17
+ read(4).unpack("l>").first
18
+ end
19
+
20
+ def int64
21
+ read(8).unpack("q>").first
22
+ end
23
+
24
+ def array
25
+ size = int32
26
+ size.times.map { yield }
27
+ end
28
+
29
+ def string
30
+ size = int16
31
+
32
+ if size == -1
33
+ nil
34
+ else
35
+ read(size)
36
+ end
37
+ end
38
+
39
+ def bytes
40
+ size = int32
41
+
42
+ if size == -1
43
+ nil
44
+ else
45
+ read(size)
46
+ end
47
+ end
48
+
49
+ def read(number_of_bytes)
50
+ @io.read(number_of_bytes)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,53 @@
1
+ module Kafka
2
+ module Protocol
3
+ class Encoder
4
+ def initialize(io)
5
+ @io = io
6
+ @io.set_encoding(Encoding::BINARY)
7
+ end
8
+
9
+ def write(bytes)
10
+ @io.write(bytes)
11
+ end
12
+
13
+ def write_int8(int)
14
+ write([int].pack("C"))
15
+ end
16
+
17
+ def write_int16(int)
18
+ write([int].pack("s>"))
19
+ end
20
+
21
+ def write_int32(int)
22
+ write([int].pack("l>"))
23
+ end
24
+
25
+ def write_int64(int)
26
+ write([int].pack("q>"))
27
+ end
28
+
29
+ def write_array(array, &block)
30
+ write_int32(array.size)
31
+ array.each(&block)
32
+ end
33
+
34
+ def write_string(string)
35
+ if string.nil?
36
+ write_int16(-1)
37
+ else
38
+ write_int16(string.bytesize)
39
+ write(string)
40
+ end
41
+ end
42
+
43
+ def write_bytes(bytes)
44
+ if bytes.nil?
45
+ write_int32(-1)
46
+ else
47
+ write_int32(bytes.bytesize)
48
+ write(bytes)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ require "zlib"
2
+
3
+ module Kafka
4
+ module Protocol
5
+
6
+ # == API Specification
7
+ #
8
+ # Message => Crc MagicByte Attributes Key Value
9
+ # Crc => int32
10
+ # MagicByte => int8
11
+ # Attributes => int8
12
+ # Key => bytes
13
+ # Value => bytes
14
+ #
15
+ class Message
16
+ MAGIC_BYTE = 0
17
+
18
+ attr_reader :key, :value, :attributes
19
+
20
+ def initialize(key:, value:, attributes: 0)
21
+ @key = key
22
+ @value = value
23
+ @attributes = attributes
24
+ end
25
+
26
+ def encode(encoder)
27
+ data = encode_without_crc
28
+ crc = Zlib.crc32(data)
29
+
30
+ encoder.write_int32(crc)
31
+ encoder.write(data)
32
+ end
33
+
34
+ def ==(other)
35
+ @key == other.key && @value == other.value && @attributes == other.attributes
36
+ end
37
+
38
+ private
39
+
40
+ def encode_without_crc
41
+ buffer = StringIO.new
42
+ encoder = Encoder.new(buffer)
43
+
44
+ encoder.write_int8(MAGIC_BYTE)
45
+ encoder.write_int8(@attributes)
46
+ encoder.write_bytes(@key)
47
+ encoder.write_bytes(@value)
48
+
49
+ buffer.string
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,70 @@
1
+ module Kafka
2
+ module Protocol
3
+ class MetadataResponse
4
+ class Broker
5
+ attr_reader :node_id, :host, :port
6
+
7
+ def initialize(node_id:, host:, port:)
8
+ @node_id = node_id
9
+ @host = host
10
+ @port = port
11
+ end
12
+ end
13
+
14
+ class PartitionMetadata
15
+ def initialize(partition_error_code:, partition_id:, leader:, replicas:, isr:)
16
+ @partition_error_code = partition_error_code
17
+ @partition_id = partition_id
18
+ @leader = leader
19
+ @replicas = replicas
20
+ @isr = isr
21
+ end
22
+ end
23
+
24
+ class TopicMetadata
25
+ def initialize(topic_error_code:, topic_name:, partitions:)
26
+ @topic_error_code = topic_error_code
27
+ @topic_name = topic_name
28
+ @partitions = partitions
29
+ end
30
+ end
31
+
32
+ attr_reader :brokers, :topics
33
+
34
+ def decode(decoder)
35
+ @brokers = decoder.array do
36
+ node_id = decoder.int32
37
+ host = decoder.string
38
+ port = decoder.int32
39
+
40
+ Broker.new(
41
+ node_id: node_id,
42
+ host: host,
43
+ port: port
44
+ )
45
+ end
46
+
47
+ @topics = decoder.array do
48
+ topic_error_code = decoder.int16
49
+ topic_name = decoder.string
50
+
51
+ partitions = decoder.array do
52
+ PartitionMetadata.new(
53
+ partition_error_code: decoder.int16,
54
+ partition_id: decoder.int32,
55
+ leader: decoder.int32,
56
+ replicas: decoder.array { decoder.int32 },
57
+ isr: decoder.array { decoder.int32 },
58
+ )
59
+ end
60
+
61
+ TopicMetadata.new(
62
+ topic_error_code: topic_error_code,
63
+ topic_name: topic_name,
64
+ partitions: partitions,
65
+ )
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,98 @@
1
+ module Kafka
2
+ module Protocol
3
+
4
+ # A produce request sends a message set to the server.
5
+ #
6
+ # == API Specification
7
+ #
8
+ # ProduceRequest => RequiredAcks Timeout [TopicName [Partition MessageSetSize MessageSet]]
9
+ # RequiredAcks => int16
10
+ # Timeout => int32
11
+ # Partition => int32
12
+ # MessageSetSize => int32
13
+ #
14
+ # MessageSet => [Offset MessageSize Message]
15
+ # Offset => int64
16
+ # MessageSize => int32
17
+ #
18
+ # Message => Crc MagicByte Attributes Key Value
19
+ # Crc => int32
20
+ # MagicByte => int8
21
+ # Attributes => int8
22
+ # Key => bytes
23
+ # Value => bytes
24
+ #
25
+ class ProduceRequest
26
+ attr_reader :required_acks, :timeout, :messages_for_topics
27
+
28
+ def initialize(required_acks:, timeout:, messages_for_topics:)
29
+ @required_acks = required_acks
30
+ @timeout = timeout
31
+ @messages_for_topics = messages_for_topics
32
+ end
33
+
34
+ # Whether this request requires any acknowledgements at all. If no acknowledgements
35
+ # are required, the server will not send back a response at all.
36
+ #
37
+ # @return [Boolean] true if acknowledgements are required, false otherwise.
38
+ def requires_acks?
39
+ @required_acks != 0
40
+ end
41
+
42
+ def encode(encoder)
43
+ encoder.write_int16(@required_acks)
44
+ encoder.write_int32(@timeout)
45
+
46
+ encoder.write_array(@messages_for_topics) do |topic, messages_for_partition|
47
+ encoder.write_string(topic)
48
+
49
+ encoder.write_array(messages_for_partition) do |partition, messages|
50
+ # When encoding the message set into the request, the bytesize of the message
51
+ # set must precede the actual bytes. Therefore we need to encode the entire
52
+ # message set into a separate buffer first.
53
+ encoded_message_set = encode_message_set(messages)
54
+
55
+ encoder.write_int32(partition)
56
+
57
+ # When encoding bytes, the 32 bit size of the byte buffer is encoded first.
58
+ encoder.write_bytes(encoded_message_set)
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def encode_message_set(messages)
66
+ buffer = StringIO.new
67
+ encoder = Encoder.new(buffer)
68
+
69
+ # Messages in a message set are *not* encoded as an array. Rather,
70
+ # they are written in sequence with only the byte size prepended.
71
+ messages.each do |message|
72
+ offset = -1 # offsets don't matter here.
73
+
74
+ # When encoding a message into a message set, the bytesize of the message must
75
+ # precede the actual bytes. Therefore we need to encode the message into a
76
+ # separate buffer first.
77
+ encoded_message = encode_message(message)
78
+
79
+ encoder.write_int64(offset)
80
+
81
+ # When encoding bytes, the 32 bit size of the byte buffer is encoded first.
82
+ encoder.write_bytes(encoded_message)
83
+ end
84
+
85
+ buffer.string
86
+ end
87
+
88
+ def encode_message(message)
89
+ buffer = StringIO.new
90
+ encoder = Encoder.new(buffer)
91
+
92
+ message.encode(encoder)
93
+
94
+ buffer.string
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,42 @@
1
+ module Kafka
2
+ module Protocol
3
+ class ProduceResponse
4
+ class TopicInfo
5
+ attr_reader :topic, :partitions
6
+
7
+ def initialize(topic, partitions)
8
+ @topic = topic
9
+ @partitions = partitions
10
+ end
11
+ end
12
+
13
+ class PartitionInfo
14
+ attr_reader :partition, :error_code, :offset
15
+
16
+ def initialize(partition, error_code, offset)
17
+ @partition = partition
18
+ @error_code = error_code
19
+ @offset = offset
20
+ end
21
+ end
22
+
23
+ attr_reader :topics
24
+
25
+ def decode(decoder)
26
+ @topics = decoder.array do
27
+ topic = decoder.string
28
+
29
+ partitions = decoder.array do
30
+ partition = decoder.int32
31
+ error_code = decoder.int16
32
+ offset = decoder.int64
33
+
34
+ PartitionInfo.new(partition, error_code, offset)
35
+ end
36
+
37
+ TopicInfo.new(topic, partitions)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ module Kafka
2
+ module Protocol
3
+ class RequestMessage
4
+ def initialize(api_key:, api_version:, correlation_id:, client_id:, request:)
5
+ @api_key = api_key
6
+ @api_version = api_version
7
+ @correlation_id = correlation_id
8
+ @client_id = client_id
9
+ @request = request
10
+ end
11
+
12
+ def encode(encoder)
13
+ encoder.write_int16(@api_key)
14
+ encoder.write_int16(@api_version)
15
+ encoder.write_int32(@correlation_id)
16
+ encoder.write_string(@client_id)
17
+
18
+ @request.encode(encoder)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ module Kafka
2
+ module Protocol
3
+ class TopicMetadataRequest
4
+ def initialize(topics: [])
5
+ @topics = topics
6
+ end
7
+
8
+ def encode(encoder)
9
+ encoder.write_array(@topics) {|topic| encoder.write_string(topic) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Kafka
2
+ VERSION = "0.1.0-alpha"
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-kafka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Schierbeck
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A client library for the Kafka distributed commit log. Still very much
56
+ at the alpha stage.
57
+ email:
58
+ - daniel.schierbeck@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - kafka.gemspec
73
+ - lib/kafka.rb
74
+ - lib/kafka/cluster.rb
75
+ - lib/kafka/connection.rb
76
+ - lib/kafka/producer.rb
77
+ - lib/kafka/protocol.rb
78
+ - lib/kafka/protocol/decoder.rb
79
+ - lib/kafka/protocol/encoder.rb
80
+ - lib/kafka/protocol/message.rb
81
+ - lib/kafka/protocol/metadata_response.rb
82
+ - lib/kafka/protocol/produce_request.rb
83
+ - lib/kafka/protocol/produce_response.rb
84
+ - lib/kafka/protocol/request_message.rb
85
+ - lib/kafka/protocol/topic_metadata_request.rb
86
+ - lib/kafka/version.rb
87
+ homepage: https://github.com/zendesk/ruby-kafka
88
+ licenses:
89
+ - Apache License Version 2.0
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">"
103
+ - !ruby/object:Gem::Version
104
+ version: 1.3.1
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.5.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A client library for the Kafka distributed commit log.
111
+ test_files: []
112
+ has_rdoc: