marloss 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +6 -0
- data/CONTRIBUTING.md +11 -0
- data/Gemfile +3 -0
- data/LICENSE.md +21 -0
- data/README.md +63 -0
- data/Rakefile +6 -0
- data/lib/marloss.rb +20 -0
- data/lib/marloss/error.rb +9 -0
- data/lib/marloss/locker.rb +48 -0
- data/lib/marloss/store.rb +105 -0
- data/lib/marloss/version.rb +7 -0
- data/marloss.gemspec +25 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 629f069429a911325be9035a724ce9216268189a6b58239754db8b83faab9688
|
4
|
+
data.tar.gz: a9ce90e625c14b49a7595fb147cafabae54202097d4a4d2683cc52b27cef991a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 11f765e28d25d5e63f49e06b463f3203acc9fdbf726698cd76763a86b1bae6b288237bacf6cc1d557a1c66a1845d0bf05c97b05bba114ece203ab0073f6dfacc
|
7
|
+
data.tar.gz: fc3c12bf10577a5d19febd069f97bb5028d14d467ab6ce18062af4c633458f483fe116855e26a50396cb2932b7e8e5754bc56a2d582f4074c9864ccd0d98fa67
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
### Pull Requests
|
2
|
+
|
3
|
+
Here are some reasons why a pull request may not be merged:
|
4
|
+
|
5
|
+
1. It hasn’t been reviewed.
|
6
|
+
2. It doesn’t include specs for new functionality.
|
7
|
+
3. It doesn’t include documentation for new functionality.
|
8
|
+
4. It changes behavior without changing the relevant documentation, comments, or specs.
|
9
|
+
5. It changes behavior of an existing public API, breaking backward compatibility.
|
10
|
+
6. It breaks the tests on a supported platform.
|
11
|
+
7. It doesn’t merge cleanly (requiring Git rebasing and conflict resolution).
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jacopo Scrinzi
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
## Marloss
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/eredi93/marloss.svg?branch=master)](https://travis-ci.org/eredi93/marloss)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/marloss.svg)](http://badge.fury.io/rb/marloss)
|
5
|
+
|
6
|
+
Marloss is a general DynamoDB-based locking implementation.
|
7
|
+
|
8
|
+
### Installation
|
9
|
+
|
10
|
+
```sh
|
11
|
+
gem install marloss
|
12
|
+
```
|
13
|
+
|
14
|
+
### Usage
|
15
|
+
|
16
|
+
Firstly, we need to initialize a lock store:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
store = Marloss::Store.new("lock_table_name", "LockHashKeyName")
|
20
|
+
```
|
21
|
+
|
22
|
+
We can use this store to create a single lock
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
locker = Marloss::Locker.new(store, "my_resource")
|
26
|
+
|
27
|
+
# raise exception if we fail to get a lock
|
28
|
+
locker.obtain_lock
|
29
|
+
|
30
|
+
# or we can block until we get a lock
|
31
|
+
locker.wait_until_lock_obtained
|
32
|
+
|
33
|
+
# refresh the lock once
|
34
|
+
locker.refresh
|
35
|
+
|
36
|
+
# or execute block with lock being refreshed
|
37
|
+
locker.with_refreshed_lock do
|
38
|
+
# execute long running code
|
39
|
+
end
|
40
|
+
|
41
|
+
# delete the lock
|
42
|
+
locker.delete_lock
|
43
|
+
```
|
44
|
+
|
45
|
+
### Testing
|
46
|
+
|
47
|
+
`rspec`
|
48
|
+
|
49
|
+
### Logging
|
50
|
+
|
51
|
+
By default Marloss logs to STDOUT, you can override it with the following command.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
Marloss.logger = Logger.new("my_app.log")
|
55
|
+
```
|
56
|
+
|
57
|
+
### What's in a name?
|
58
|
+
|
59
|
+
"Marlòss" means lock, in Trentino's dialect. I'm from [Volano](https://en.wikipedia.org/wiki/Volano), and I liked the idea of using a word from my hometown.
|
60
|
+
|
61
|
+
### Contributing
|
62
|
+
|
63
|
+
This repository is [open to contributions](.github/CONTRIBUTING.md).
|
data/Rakefile
ADDED
data/lib/marloss.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
require "aws-sdk-dynamodb"
|
4
|
+
|
5
|
+
require "marloss/version"
|
6
|
+
require "marloss/error"
|
7
|
+
require "marloss/store"
|
8
|
+
require "marloss/locker"
|
9
|
+
|
10
|
+
module Marloss
|
11
|
+
|
12
|
+
def self.logger
|
13
|
+
@logger ||= ::Logger.new(STDOUT)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.logger=(logger)
|
17
|
+
@logger = logger
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
module Marloss
|
4
|
+
class Locker
|
5
|
+
|
6
|
+
attr_reader :store, :name
|
7
|
+
|
8
|
+
def initialize(store, name)
|
9
|
+
@store = store
|
10
|
+
@name = name
|
11
|
+
end
|
12
|
+
|
13
|
+
def obtain_lock
|
14
|
+
store.create_lock(name)
|
15
|
+
end
|
16
|
+
|
17
|
+
def refresh_lock
|
18
|
+
store.refresh_lock(name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def wait_until_lock_obtained(sleep_seconds: 3)
|
22
|
+
store.create_lock(name)
|
23
|
+
rescue LockNotObtainedError
|
24
|
+
sleep(sleep_seconds)
|
25
|
+
retry
|
26
|
+
end
|
27
|
+
|
28
|
+
def with_refreshed_lock
|
29
|
+
thr = Thread.new do
|
30
|
+
loop do
|
31
|
+
begin
|
32
|
+
store.refresh_lock(name)
|
33
|
+
rescue Exception => e
|
34
|
+
Thread.main.raise(e)
|
35
|
+
end
|
36
|
+
|
37
|
+
sleep(store.ttl / 3.0)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
yield
|
42
|
+
|
43
|
+
ensure
|
44
|
+
thr.kill
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
module Marloss
|
4
|
+
class Store
|
5
|
+
|
6
|
+
attr_reader :client, :table, :hash_key, :ttl
|
7
|
+
|
8
|
+
def initialize(table, hash_key, ttl: 30, client_options: {})
|
9
|
+
@client = Aws::DynamoDB::Client.new(client_options)
|
10
|
+
@table = table
|
11
|
+
@hash_key = hash_key
|
12
|
+
@ttl = ttl
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_table
|
16
|
+
client.create_table(
|
17
|
+
attribute_definitions: [
|
18
|
+
{
|
19
|
+
attribute_name: hash_key,
|
20
|
+
attribute_type: "S",
|
21
|
+
}
|
22
|
+
],
|
23
|
+
key_schema: [
|
24
|
+
{
|
25
|
+
attribute_name: hash_key,
|
26
|
+
key_type: "HASH",
|
27
|
+
}
|
28
|
+
],
|
29
|
+
provisioned_throughput: {
|
30
|
+
read_capacity_units: 5,
|
31
|
+
write_capacity_units: 5,
|
32
|
+
},
|
33
|
+
table_name: table
|
34
|
+
)
|
35
|
+
Marloss.logger.info("DynamoDB table created successfully")
|
36
|
+
client.update_time_to_live(
|
37
|
+
table_name: table,
|
38
|
+
time_to_live_specification: {
|
39
|
+
enabled: true,
|
40
|
+
attribute_name: "Expires"
|
41
|
+
}
|
42
|
+
)
|
43
|
+
Marloss.logger.info("DynamoDB table TTL configured successfully")
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete_table
|
47
|
+
client.delete_table(table_name: table)
|
48
|
+
Marloss.logger.info("DynamoDB table deleted successfully")
|
49
|
+
end
|
50
|
+
|
51
|
+
def create_lock(name)
|
52
|
+
client.put_item(
|
53
|
+
table_name: table,
|
54
|
+
item: {
|
55
|
+
hash_key => name,
|
56
|
+
"ProcessID" => process_id,
|
57
|
+
"Expires" => (Time.now + ttl).to_i
|
58
|
+
},
|
59
|
+
expression_attribute_names: {
|
60
|
+
"#E" => "Expires",
|
61
|
+
"#P" => "ProcessID"
|
62
|
+
},
|
63
|
+
expression_attribute_values: {
|
64
|
+
":now" => Time.now.to_i,
|
65
|
+
":process_id" => process_id,
|
66
|
+
},
|
67
|
+
condition_expression: "attribute_not_exists(#{hash_key}) OR #E < :now OR #P = :process_id"
|
68
|
+
)
|
69
|
+
Marloss.logger.info("Lock for #{name} created successfully, will expire in #{ttl} seconds")
|
70
|
+
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e
|
71
|
+
Marloss.logger.error("Failed to create lock for #{name}")
|
72
|
+
raise(LockNotObtainedError, e.message)
|
73
|
+
end
|
74
|
+
|
75
|
+
def refresh_lock(name)
|
76
|
+
client.update_item(
|
77
|
+
table_name: table,
|
78
|
+
key: { hash_key => name },
|
79
|
+
expression_attribute_names: {
|
80
|
+
"#E" => "Expires",
|
81
|
+
"#P" => "ProcessID"
|
82
|
+
},
|
83
|
+
expression_attribute_values: {
|
84
|
+
":expires" => (Time.now + ttl).to_i,
|
85
|
+
":now" => Time.now.to_i,
|
86
|
+
":process_id" => process_id,
|
87
|
+
},
|
88
|
+
update_expression: "SET #E = :expires",
|
89
|
+
condition_expression: "attribute_exists(#{hash_key}) AND (#E < :now OR #P = :process_id)"
|
90
|
+
)
|
91
|
+
Marloss.logger.info("Lock for #{name} refreshed successfully, will expire in #{ttl} seconds")
|
92
|
+
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException => e
|
93
|
+
Marloss.logger.error("Failed to refresh lock for #{name}")
|
94
|
+
raise(LockNotRefreshedError, e.message)
|
95
|
+
end
|
96
|
+
|
97
|
+
private def process_id
|
98
|
+
hostname = `hostname`.chomp
|
99
|
+
pid = Process.pid
|
100
|
+
|
101
|
+
"#{hostname}:#{pid}"
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
data/marloss.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 "marloss/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "marloss"
|
8
|
+
spec.version = Marloss::VERSION
|
9
|
+
spec.authors = ["Jacopo Scrinzi"]
|
10
|
+
spec.email = "scrinzi.jcopo@gmail.com"
|
11
|
+
|
12
|
+
spec.summary = %q{AWS DynamoDB based Locking}
|
13
|
+
spec.description = %q{Distributed locking using AWS DynamoDB}
|
14
|
+
spec.homepage = "https://github.com/eredi93/marloss"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = %w(lib)
|
19
|
+
|
20
|
+
spec.add_dependency "aws-sdk-dynamodb", "~> 1.2"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marloss
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacopo Scrinzi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-dynamodb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !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: Distributed locking using AWS DynamoDB
|
70
|
+
email: scrinzi.jcopo@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".travis.yml"
|
77
|
+
- CONTRIBUTING.md
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.md
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/marloss.rb
|
83
|
+
- lib/marloss/error.rb
|
84
|
+
- lib/marloss/locker.rb
|
85
|
+
- lib/marloss/store.rb
|
86
|
+
- lib/marloss/version.rb
|
87
|
+
- marloss.gemspec
|
88
|
+
homepage: https://github.com/eredi93/marloss
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.7.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: AWS DynamoDB based Locking
|
112
|
+
test_files: []
|