zk_client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +216 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/zk_client/version.rb +3 -0
- data/lib/zk_client/zk_client.rb +112 -0
- data/lib/zk_client.rb +5 -0
- data/zk_client.gemspec +29 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 367305f9db2180c442ee459fb4e04f4fb3acaf55
|
4
|
+
data.tar.gz: 78cacce7e7d0e816f5d53a1b68e64dc31652f327
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f19f3acc2afd60742807cdcf1e267bcc7801603949b4a7120cc0c9e4be60fd7c205277425d0bcef4c71b79df1863c360ef39e96658e030b633a694174990d27
|
7
|
+
data.tar.gz: 438a242921e985e15e0c95022b874b37d5c9537805be1e25a16f49a6b1aba7a2a042996188efb951c9b7bc30b0b0f9453788f618680743815d93a5f88bf641bf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Autodesk, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
# ZkClient
|
2
|
+
|
3
|
+
A simple Zookeeper client used in the Mojo framework. This gem allows quick and simple access to a Zookeeper server.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'zk_client'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install zk_client
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
#### How to configure your Zookeeper client
|
24
|
+
|
25
|
+
By default ZkClient uses 'localhost:2181' as the Zookeeper server, and '/' as the root_path. More on that later. But if you want to point to a different zk server, or change the root_path, then you will have to 'configure' the Zookeeper Client.
|
26
|
+
|
27
|
+
There are a few ways to configure the Zookeeper client (ZkClient).
|
28
|
+
|
29
|
+
##### Using the config block and allowing ZkClient to parse a uri
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'zk_client'
|
33
|
+
|
34
|
+
ZkClient.config do |zk|
|
35
|
+
zk.uri = 'my-zk-server.com:8000/application/path'
|
36
|
+
end
|
37
|
+
|
38
|
+
ZkClient.host
|
39
|
+
#=> "my-zk-server.com"
|
40
|
+
ZkClient.port
|
41
|
+
#=> 8000
|
42
|
+
ZkClient.root_path
|
43
|
+
#=> "/application/path"
|
44
|
+
ZkClient.uri
|
45
|
+
#=> "my-zk-server.com:8000"
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
##### Using the config block and directly setting zk variables
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'zk_client'
|
53
|
+
|
54
|
+
ZkClient.config do |zk|
|
55
|
+
zk.host = 'test.myexample.com'
|
56
|
+
zk.port = 9999
|
57
|
+
zk.root_path = '/new/application/path'
|
58
|
+
end
|
59
|
+
|
60
|
+
ZkClient.host
|
61
|
+
#=> "test.myexample.com"
|
62
|
+
ZkClient.port
|
63
|
+
#=> 9999
|
64
|
+
ZkClient.root_path
|
65
|
+
#=> "/new/application/path"
|
66
|
+
ZkClient.uri
|
67
|
+
#=> "test.myexample.com:9999"
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
##### Using the ZkClient directly and setting zk variables
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
require 'zk_client'
|
75
|
+
|
76
|
+
ZkClient.host = 'test.myexample.com'
|
77
|
+
ZkClient.port = 9999
|
78
|
+
ZkClient.root_path = '/new/application/path'
|
79
|
+
|
80
|
+
ZkClient.host
|
81
|
+
#=> "test.myexample.com"
|
82
|
+
ZkClient.port
|
83
|
+
#=> 9999
|
84
|
+
ZkClient.root_path
|
85
|
+
#=> "/new/application/path"
|
86
|
+
ZkClient.uri
|
87
|
+
#=> "test.myexample.com:9999"
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
#### Interacting with a Zookeeper Server
|
93
|
+
|
94
|
+
##### Reading data from an existing node
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
require 'zk_client'
|
98
|
+
|
99
|
+
# Assuming you have a node at the path '/mynode' with the data value of 'my data!'
|
100
|
+
ZkClient.read('/mynode')
|
101
|
+
#=> "my data!"
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
##### Reading data from node that doesn't exist
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
require 'zk_client'
|
109
|
+
|
110
|
+
ZkClient.read('/doesntexist')
|
111
|
+
#=> nil
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
##### Sometimes you want to return the whole node with stats too
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
require 'zk_client'
|
119
|
+
|
120
|
+
ZkClient.read_node('/mynode')
|
121
|
+
#=> {:req_id=>3, :rc=>0, :data=>"my data!", :stat=>#<Zookeeper::Stat:0x000000028569f0 @exists=true, @czxid=2, @mzxid=10665, @ctime=1425596120895, @mtime=1441087085411, @version=2, @cversion=3, @aversion=0, @ephemeralOwner=0, @dataLength=0, @numChildren=3, @pzxid=9621>}
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
##### Writing data
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
require 'zk_client'
|
129
|
+
|
130
|
+
ZkClient.write('/mynode', "This is my data")
|
131
|
+
#=> {:req_id=>5, :rc=>0, :path=>"/mynode"}
|
132
|
+
ZkClient.read('/mynode')
|
133
|
+
#=> "This is my data"
|
134
|
+
```
|
135
|
+
|
136
|
+
##### Deleting a node
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
require 'zk_client'
|
140
|
+
|
141
|
+
ZkClient.delete('/mynode')
|
142
|
+
#=> {:req_id=>7, :rc=>0}
|
143
|
+
ZkClient.read_node('/mynode')
|
144
|
+
#=> {:req_id=>9, :rc=>-101, :data=>nil, :stat=>#<Zookeeper::Stat:0x00000002803f70 @exists=false>}
|
145
|
+
```
|
146
|
+
|
147
|
+
##### Close connection to ZK
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
require 'zk_client'
|
151
|
+
|
152
|
+
ZkClient.client.connected?
|
153
|
+
#=> true
|
154
|
+
ZkClient.close
|
155
|
+
#=> nil
|
156
|
+
ZkClient.client.connected?
|
157
|
+
#=> false
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
##### Access the underlying Zookeeper.new instance
|
162
|
+
```ruby
|
163
|
+
require 'zk_client'
|
164
|
+
|
165
|
+
# Sometimes you might want to interact with the Zookeeper::Client directly.
|
166
|
+
# To do that just call .client:
|
167
|
+
raw_client = ZkClient.client
|
168
|
+
#=> #<Zookeeper::Client:0x0000000280da70 @host="localhost:2181", @chroot_path="", 0x0000000280c198 @level=0,
|
169
|
+
# ...
|
170
|
+
# ...
|
171
|
+
raw_client.class
|
172
|
+
#=> Zookeeper::Client
|
173
|
+
|
174
|
+
```
|
175
|
+
|
176
|
+
#### How Root Path works
|
177
|
+
Depending on your Zookeeper server you might have nested nodes that stores your application data. It can be quite annoying writing the leading path when that doesn't change. Lets say you share Zookeeper server accross your organization, and you need to specify your department and application name. When you acccess your data, it might look something like this:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
require 'zk_client'
|
181
|
+
|
182
|
+
ZkClient.root_path
|
183
|
+
#=> "/"
|
184
|
+
|
185
|
+
ZkClient.read('/org/department/application/my_data')
|
186
|
+
#=> "This is my data!"
|
187
|
+
|
188
|
+
```
|
189
|
+
|
190
|
+
That's annoying. Any you might accidentally mess with other people's data if you fat finger the application name. ZkClient allows you to configure a root path and use relative paths to access data:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
require 'zk_client'
|
194
|
+
|
195
|
+
ZkClient.root_path = '/org/department/application'
|
196
|
+
ZkClient.root_path
|
197
|
+
#=> "/org/department/application"
|
198
|
+
|
199
|
+
ZkClient.read('/my_data')
|
200
|
+
#=> "This is my data!"
|
201
|
+
|
202
|
+
# It will also append a leading '/' if you forget....
|
203
|
+
ZkClient.read('my_data')
|
204
|
+
#=> "This is my data!"
|
205
|
+
|
206
|
+
```
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
## Contributing
|
211
|
+
|
212
|
+
1. Fork it ( https://git.autodesk.com/EIS-EA-MOJO/zk_client/fork )
|
213
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
214
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
215
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
216
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zk_client"
|
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,112 @@
|
|
1
|
+
require 'zookeeper'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
|
6
|
+
module ZkClient
|
7
|
+
ROOT = '/'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def client
|
12
|
+
@@client ||= create_client
|
13
|
+
end
|
14
|
+
|
15
|
+
def read(path)
|
16
|
+
read_node(path)[:data]
|
17
|
+
end
|
18
|
+
|
19
|
+
def read_node(path)
|
20
|
+
key = process_path(path)
|
21
|
+
client.get(path: key)
|
22
|
+
end
|
23
|
+
|
24
|
+
def write(path, data)
|
25
|
+
key = process_path(path)
|
26
|
+
response = client.set(path: key, data: data)
|
27
|
+
if !response[:stat].exists
|
28
|
+
create(key, data)
|
29
|
+
else
|
30
|
+
response
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def create(path, data)
|
35
|
+
key = process_path(path)
|
36
|
+
resp = client.create(path: key, data: data)
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete(path)
|
40
|
+
key = process_path(path)
|
41
|
+
client.delete(path: key)
|
42
|
+
end
|
43
|
+
|
44
|
+
def close
|
45
|
+
client.close
|
46
|
+
end
|
47
|
+
|
48
|
+
def config
|
49
|
+
if block_given?
|
50
|
+
yield(self)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def root_path
|
55
|
+
@@root_path ||= ROOT
|
56
|
+
end
|
57
|
+
|
58
|
+
def root_path=(root)
|
59
|
+
@@root_path = root
|
60
|
+
end
|
61
|
+
|
62
|
+
def host
|
63
|
+
@@host ||= "localhost"
|
64
|
+
end
|
65
|
+
|
66
|
+
def host=(host)
|
67
|
+
@@host = host
|
68
|
+
end
|
69
|
+
|
70
|
+
def port
|
71
|
+
@@port ||= 2181
|
72
|
+
end
|
73
|
+
|
74
|
+
def port=(port)
|
75
|
+
@@port = port.to_i
|
76
|
+
end
|
77
|
+
|
78
|
+
def uri
|
79
|
+
"#{host}:#{port}"
|
80
|
+
end
|
81
|
+
|
82
|
+
def uri=(uri)
|
83
|
+
parsed_uri = URI(uri)
|
84
|
+
|
85
|
+
@@host = parsed_uri.host
|
86
|
+
@@port = parsed_uri.port
|
87
|
+
@@root_path = parsed_uri.path
|
88
|
+
end
|
89
|
+
|
90
|
+
# Backward compatibility
|
91
|
+
def root
|
92
|
+
root_path
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def create_client
|
98
|
+
if defined?(@@client) && @@client.respond_to?(close)
|
99
|
+
@@client.close
|
100
|
+
end
|
101
|
+
Zookeeper.new(uri)
|
102
|
+
end
|
103
|
+
|
104
|
+
def process_path(path)
|
105
|
+
path = "/#{path}" unless path.start_with?('/')
|
106
|
+
path = "#{root_path}#{path}" unless path.start_with?(root_path)
|
107
|
+
|
108
|
+
path
|
109
|
+
end
|
110
|
+
|
111
|
+
end # End class methods
|
112
|
+
end
|
data/lib/zk_client.rb
ADDED
data/zk_client.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'zk_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zk_client"
|
8
|
+
spec.version = ZkClient::VERSION
|
9
|
+
spec.authors = ["John Thomas"]
|
10
|
+
spec.email = ["john.thomas@autodesk.com"]
|
11
|
+
|
12
|
+
spec.summary = "A simple Zookeeper client used in the Mojo framework."
|
13
|
+
spec.description = %q{ This gem allows quick and simple access to a Zookeeper
|
14
|
+
server.
|
15
|
+
}
|
16
|
+
spec.homepage = "https://git.autodesk.com/EIS-EA-MOJO/zk_client"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
#spec.platform = 'java'
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "zookeeper"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
end
|
29
|
+
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zk_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Thomas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: zookeeper
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: " This gem allows quick and simple access to a Zookeeper\nserver.\n "
|
70
|
+
email:
|
71
|
+
- john.thomas@autodesk.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/zk_client.rb
|
86
|
+
- lib/zk_client/version.rb
|
87
|
+
- lib/zk_client/zk_client.rb
|
88
|
+
- zk_client.gemspec
|
89
|
+
homepage: https://git.autodesk.com/EIS-EA-MOJO/zk_client
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.6
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: A simple Zookeeper client used in the Mojo framework.
|
113
|
+
test_files: []
|