cassandra_lock 0.0.1 → 0.0.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: 2642cc2249fff7aa6fe90ad11623eec933578cb8
4
- data.tar.gz: 37600867bbcaa92229336fed9cde9ab8591ee406
3
+ metadata.gz: e9931caf03a8350866ebfc36e5a8ab3d73a5a764
4
+ data.tar.gz: a2f6da68a06576fe8df2759e7285abc5484ee14e
5
5
  SHA512:
6
- metadata.gz: 8293c26b5672dce2eab1af349e438ede799466f78fdd7d11a5e9508be79e0af5985ff1e13d97becf9d31a9730d2d03c81f06a9126d1affe5b27950560f616b6d
7
- data.tar.gz: 5199215ed36264f509fed20a54be9c999b9610fcb1b9ac54f2192061c140d4822d0f499de1536dcb8a4f1b6ba23b765401859d2f0e2a8f6d05fe1cafa731574a
6
+ metadata.gz: ad1b0637052225d18e2e1b1bf20ca9f1ada8cb9796da4680d99f18a1d255342758692e436990ba7e9e4d234d74b3b561d64490929376d79b1aa143a1b352bd22
7
+ data.tar.gz: aa7d9eb2bd32f6abdbad0bee4f1a816494c94c006f0f4997ccd45d0cc6ca04ee889644edc3de41011c2d917ff7b918c892a7a33951918ccf0962eeab437b366d
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cassandra_lock (0.0.2)
5
+ cassandra-driver
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ cassandra-driver (1.1.1)
11
+ ione (~> 1.2)
12
+ diff-lcs (1.2.5)
13
+ ione (1.2.0)
14
+ rake (10.4.2)
15
+ rspec (3.1.0)
16
+ rspec-core (~> 3.1.0)
17
+ rspec-expectations (~> 3.1.0)
18
+ rspec-mocks (~> 3.1.0)
19
+ rspec-core (3.1.7)
20
+ rspec-support (~> 3.1.0)
21
+ rspec-expectations (3.1.2)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.1.0)
24
+ rspec-mocks (3.1.3)
25
+ rspec-support (~> 3.1.0)
26
+ rspec-support (3.1.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 1.7)
33
+ cassandra_lock!
34
+ rake (~> 10.0)
35
+ rspec (~> 3.1)
data/README.md CHANGED
@@ -12,19 +12,56 @@ gem 'cassandra_lock'
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
15
+ ```$ bundle```
16
16
 
17
17
  Or install it yourself as:
18
18
 
19
- $ gem install cassandra_lock
19
+ ```$ gem install cassandra_lock```
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ Create the keyspace, see the example bellow.
24
+
25
+ ```SQL
26
+ CREATE KEYSPACE test_lock WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };
27
+ ```
28
+
29
+ Create the table, see the example bellow.
30
+
31
+ ```SQL
32
+ CREATE TABLE test_lock.leases (
33
+ name text PRIMARY KEY,
34
+ owner text,
35
+ value text
36
+ ) with default_time_to_live = 5
37
+ ```
38
+
39
+ And finally to use in your code:
40
+
41
+ ```ruby
42
+ #to create
43
+ contact_points = ["127.0.0.1"]
44
+ keyspace = "test_lock"
45
+ client = CassandraLock::Client.new(contact_points, keyspace)
46
+
47
+ #to lock
48
+ if client.lock("my_resource")
49
+ puts "I acquired the lock"
50
+ else
51
+ puts "Someone took the lock"
52
+ end
53
+
54
+ #to unlock
55
+ client.unlock("my_resource")
56
+
57
+ #to keep_alive
58
+ client.keep_alive("my_resource")
59
+
60
+ ```
24
61
 
25
62
  ## Contributing
26
63
 
27
- 1. Fork it ( https://github.com/[my-github-username]/cassandra_lock/fork )
64
+ 1. Fork it ( https://github.com/leandromoreira/cassandra_lock/fork )
28
65
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
66
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
67
  4. Push to the branch (`git push origin my-new-feature`)
@@ -17,6 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_dependency "cassandra-driver"
21
+
20
22
  spec.add_development_dependency "bundler", "~> 1.7"
21
23
  spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.1"
22
25
  end
@@ -1,5 +1,5 @@
1
1
  require "cassandra_lock/version"
2
2
 
3
3
  module CassandraLock
4
- # Your code goes here...
4
+ autoload :Client, 'cassandra_lock/client'
5
5
  end
@@ -0,0 +1,30 @@
1
+ require 'cassandra'
2
+ require 'securerandom'
3
+
4
+ module CassandraLock
5
+ class Client
6
+ attr_reader :id
7
+
8
+ def initialize(contact_points, keyspace, id=nil)
9
+ cluster = Cassandra.cluster hosts: contact_points
10
+ @cassandra = cluster.connect keyspace
11
+ @id = id || SecureRandom.uuid
12
+ @LOCK = @cassandra.prepare "INSERT INTO leases (name, owner) VALUES (?,?) IF NOT EXISTS"
13
+ @KEEPALIVE = @cassandra.prepare "UPDATE leases set owner = ? where name = ? IF owner = ?"
14
+ @UNLOCK = @cassandra.prepare "DELETE FROM leases where name = ? IF owner = ?"
15
+ end
16
+
17
+ def lock(resource)
18
+ result = @cassandra.execute(@LOCK, resource, @id)
19
+ result.first["[applied]"]
20
+ end
21
+
22
+ def keep_alive(resource)
23
+ @cassandra.execute(@KEEPALIVE, @id, resource, @id)
24
+ end
25
+
26
+ def unlock(resource)
27
+ @cassandra.execute(@UNLOCK, resource, @id)
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module CassandraLock
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CassandraLock::Client do
4
+ let(:contact_points) {["127.0.0.1"]}
5
+ let(:keyspace) { "test_lock" }
6
+ let(:resource_key) { "foo" }
7
+ let(:ttl) { 1000 }
8
+
9
+ it 'locks' do
10
+ first_client = CassandraLock::Client.new(contact_points, keyspace)
11
+ second_client = CassandraLock::Client.new(contact_points, keyspace)
12
+
13
+ first_try_lock_info = first_client.lock(resource_key)
14
+ second_try_lock_info = second_client.lock(resource_key)
15
+
16
+ expect(first_try_lock_info).to be_truthy
17
+ expect(second_try_lock_info).to be_falsy
18
+
19
+ first_client.unlock(resource_key)
20
+ end
21
+
22
+ it 'unlocks' do
23
+ first_client = CassandraLock::Client.new(contact_points, keyspace)
24
+ second_client = CassandraLock::Client.new(contact_points, keyspace)
25
+
26
+ first_try_lock_info = first_client.lock(resource_key)
27
+ first_client.unlock(resource_key)
28
+
29
+ second_try_lock_info = second_client.lock(resource_key)
30
+
31
+ expect(second_try_lock_info).to be_truthy
32
+ end
33
+
34
+ it 'expires' do
35
+ first_client = CassandraLock::Client.new(contact_points, keyspace)
36
+ first_try_lock_info = first_client.lock(resource_key)
37
+
38
+ sleep 6
39
+
40
+ expect(first_try_lock_info).to be_falsy
41
+ end
42
+
43
+ it 'keeps alive' do
44
+ first_client = CassandraLock::Client.new(contact_points, keyspace)
45
+ first_try_lock_info = first_client.lock(resource_key)
46
+
47
+ sleep 3
48
+
49
+ first_client.keep_alive(resource_key)
50
+
51
+ sleep 3
52
+
53
+ expect(first_try_lock_info).to be_truthy
54
+ end
55
+ end
@@ -0,0 +1 @@
1
+ require 'cassandra_lock'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandra_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Moreira
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cassandra-driver
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
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: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
41
69
  description:
42
70
  email:
43
71
  - leandro.ribeiro.moreira@gmail.com
@@ -46,13 +74,18 @@ extensions: []
46
74
  extra_rdoc_files: []
47
75
  files:
48
76
  - ".gitignore"
77
+ - ".rspec"
49
78
  - Gemfile
79
+ - Gemfile.lock
50
80
  - LICENSE
51
81
  - README.md
52
82
  - Rakefile
53
83
  - cassandra_lock.gemspec
54
84
  - lib/cassandra_lock.rb
85
+ - lib/cassandra_lock/client.rb
55
86
  - lib/cassandra_lock/version.rb
87
+ - spec/client_spec.rb
88
+ - spec/spec_helper.rb
56
89
  homepage: https://github.com/leandromoreira/cassandra-lock
57
90
  licenses:
58
91
  - MIT
@@ -77,4 +110,6 @@ rubygems_version: 2.2.2
77
110
  signing_key:
78
111
  specification_version: 4
79
112
  summary: A ruby lib to achieve consensus with Cassandra
80
- test_files: []
113
+ test_files:
114
+ - spec/client_spec.rb
115
+ - spec/spec_helper.rb