distlock 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dist_lock.rb +1 -0
- data/lib/distlock.rb +5 -0
- data/lib/distlock/version.rb +1 -1
- data/lib/distlock/zk/affinity_pool.rb +3 -9
- data/lib/distlock/zk/common.rb +48 -0
- data/lib/distlock/zk/zk.rb +1 -0
- metadata +10 -8
data/lib/dist_lock.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'distlock'
|
data/lib/distlock.rb
CHANGED
data/lib/distlock/version.rb
CHANGED
@@ -1,19 +1,13 @@
|
|
1
1
|
module Distlock
|
2
2
|
module ZK
|
3
3
|
class AffinityPool
|
4
|
+
include Distlock::ZK::Common
|
5
|
+
|
4
6
|
def initialize(options={})
|
5
|
-
defaults = {:host => "localhost:2181", :timeout => 10, :root_path => "/affinity/
|
7
|
+
defaults = {:host => "localhost:2181", :timeout => 10, :root_path => "/affinity/pool/test"}
|
6
8
|
@options = defaults.merge(options)
|
7
9
|
end
|
8
10
|
|
9
|
-
def zk
|
10
|
-
@zk ||= begin
|
11
|
-
zk = Zookeeper.new(@options[:host], @options[:timeout])
|
12
|
-
|
13
|
-
# todo - initialize the root_path hierarchy here
|
14
|
-
zk.stat(:path => @options[:root_path])
|
15
|
-
end
|
16
|
-
end
|
17
11
|
end
|
18
12
|
end
|
19
13
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Distlock
|
2
|
+
module ZK
|
3
|
+
module Common
|
4
|
+
def zk
|
5
|
+
@zk ||= begin
|
6
|
+
zk = Zookeeper.new(@options[:host], @options[:timeout])
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# does a node exist for the given path?
|
11
|
+
def exists?(path)
|
12
|
+
puts "checking if #{path} exists"
|
13
|
+
result = zk.stat(:path => path)[:stat].exists
|
14
|
+
puts result
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
# create all levels of the hierarchy as necessary
|
19
|
+
# i.e. for "/foo/bar/baz", creates the following nodes -
|
20
|
+
#
|
21
|
+
# /foo
|
22
|
+
# /foo/bar
|
23
|
+
# /foo/bar/baz
|
24
|
+
#
|
25
|
+
def safe_create(path)
|
26
|
+
path_elements = path.split("/").reject{ |x| x=="" }
|
27
|
+
|
28
|
+
all = []
|
29
|
+
while(!path_elements.empty?)
|
30
|
+
all << path_elements
|
31
|
+
path_elements = path_elements[0..-2]
|
32
|
+
end
|
33
|
+
|
34
|
+
all.reverse.each do |path_elements|
|
35
|
+
path = "/" + path_elements.join("/")
|
36
|
+
zk.create(:path => path) unless exists?(path)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_sequenced_ephemeral(path, prefix="lock")
|
41
|
+
lock_path = [path, "#{prefix}-#{zk.client_id}-"].join("/")
|
42
|
+
puts lock_path
|
43
|
+
result = zk.create(:path => lock_path, :sequence => true, :ephemeral => true)
|
44
|
+
puts result
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/distlock/zk/zk.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: distlock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70294611917200 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0.9'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70294611917200
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: zookeeper
|
27
|
-
requirement: &
|
27
|
+
requirement: &70294611916060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0.4'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70294611916060
|
36
36
|
description: Distributed Locking
|
37
37
|
email:
|
38
38
|
- simon@soulware.co.uk
|
@@ -45,9 +45,11 @@ files:
|
|
45
45
|
- README
|
46
46
|
- Rakefile
|
47
47
|
- distlock.gemspec
|
48
|
+
- lib/dist_lock.rb
|
48
49
|
- lib/distlock.rb
|
49
50
|
- lib/distlock/version.rb
|
50
51
|
- lib/distlock/zk/affinity_pool.rb
|
52
|
+
- lib/distlock/zk/common.rb
|
51
53
|
- lib/distlock/zk/zk.rb
|
52
54
|
homepage: http://soulware.github.com/distlock
|
53
55
|
licenses: []
|
@@ -63,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
65
|
version: '0'
|
64
66
|
segments:
|
65
67
|
- 0
|
66
|
-
hash: -
|
68
|
+
hash: -2701141366311173259
|
67
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
70
|
none: false
|
69
71
|
requirements:
|
@@ -72,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
74
|
version: '0'
|
73
75
|
segments:
|
74
76
|
- 0
|
75
|
-
hash: -
|
77
|
+
hash: -2701141366311173259
|
76
78
|
requirements: []
|
77
79
|
rubyforge_project: distlock
|
78
80
|
rubygems_version: 1.8.10
|