dcell-hazelcast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Bernd Ahlers
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ DCell::Hazelcast
2
+ ----------------
3
+
4
+ This provides a [Hazelcast](http://www.hazelcast.com/) registry for
5
+ [DCell](https://github.com/celluloid/dcell).
6
+
7
+ The Hazelcast adapter allows DCell to operate without a central registry like
8
+ Redis or Zookeeper. It is using Hazelcast's distributed map data structure to
9
+ implement the node registry and the global registry for DCell.
10
+
11
+ CAVEAT
12
+ ------
13
+
14
+ This is currently a quick hack which has not seen intensive tests and does not
15
+ provide any configuration options for Hazelcast yet.
16
+
17
+ Supported Platforms
18
+ -------------------
19
+
20
+ JRuby (>= 1.7)
21
+
22
+ Prerequisites
23
+ -------------
24
+
25
+ Please follow the [instructions for DCell](https://github.com/celluloid/dcell).
26
+
27
+ Usage
28
+ -----
29
+
30
+ ```ruby
31
+ require 'dcell'
32
+ require 'dcell/registries/hazelcast'
33
+
34
+ DCell.start(:registry => {:adapter => 'hazelcast'})
35
+ ```
36
+
37
+ Please see the DCell documentation for further details.
38
+
39
+ There is an executable example in `examples/dcell-hazelcast.rb`.
40
+
41
+ $ jruby -Ilib examples/dcell-hazelcast.rb -n master -p 7777
42
+ $ jruby -Ilib examples/dcell-hazelcast.rb -n node0 -p 7778
43
+
44
+ License
45
+ -------
46
+
47
+ ### Hazelcast
48
+
49
+ Hazelcast is released under [Apache License](http://www.apache.org/licenses/LICENSE-2.0)
50
+ and the project is hosted at [GitHub](https://github.com/hazelcast).
51
+ It can be freely used in commercial or non-commercial applications.
52
+
53
+ ### DCell::Hazelcast
54
+
55
+ Copyright (c) 2012 Bernd Ahlers. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run all specs'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dcell/hazelcast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dcell-hazelcast'
8
+ spec.version = DCell::Hazelcast::VERSION
9
+ spec.authors = ['Bernd Ahlers']
10
+ spec.email = ['bernd@tuneafish.de']
11
+ spec.description = %q{Hazelcast registry for DCell}
12
+ spec.summary = %q{A Hazelcast registry for DCell.}
13
+ spec.homepage = 'https://github.com/bernd/dcell-hazelcast'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+
25
+ spec.add_runtime_dependency 'dcell'
26
+ end
@@ -0,0 +1,56 @@
1
+ require 'dcell'
2
+ require 'dcell/registries/hazelcast_adapter'
3
+ require 'optparse'
4
+
5
+ # The DCell node_id
6
+ name = nil
7
+
8
+ # The local port for the 0MQ communication.
9
+ port = nil
10
+
11
+ registry = {:adapter => 'hazelcast', :env => 'test'}
12
+
13
+ OptionParser.new.tap { |options|
14
+ options.on('-n NAME', 'Node name') do |value|
15
+ name = value
16
+ end
17
+ options.on('-p PORT', 'Node port') do |value|
18
+ port = value.to_i
19
+ end
20
+ }.parse(ARGV)
21
+
22
+ unless name or port
23
+ puts "Missing options. See -h for available options."
24
+ exit 1
25
+ end
26
+
27
+ class TimeServer
28
+ include Celluloid
29
+
30
+ def run
31
+ "[#{DCell.me.id}] #{Time.now}"
32
+ end
33
+ end
34
+
35
+ class StatusServer
36
+ include Celluloid
37
+
38
+ def run
39
+ "[#{DCell.me.id}] STATUS OK"
40
+ end
41
+ end
42
+
43
+ DCell.start(:id => name, :addr => "tcp://127.0.0.1:#{port}", :registry => registry)
44
+
45
+ if name == 'master'
46
+ TimeServer.supervise_as :service
47
+ else
48
+ StatusServer.supervise_as :service
49
+ end
50
+
51
+ loop do
52
+ puts DCell::Node.all.inspect
53
+ puts DCell::Node.all.shuffle.first[:service].run
54
+
55
+ sleep 1
56
+ end
@@ -0,0 +1,56 @@
1
+ require 'dcell/hazelcast/jars'
2
+ require 'forwardable'
3
+
4
+ module DCell
5
+ class Hazelcast
6
+ class LogListener
7
+ def log(event)
8
+ DCell::Logger.info(event.get_log_record.get_message)
9
+ end
10
+ end
11
+
12
+ java_import 'com.hazelcast.core.Hazelcast'
13
+ java_import 'java.util.logging.Level'
14
+ java_import 'java.lang.System'
15
+
16
+ def self.shutdown_all
17
+ Hazelcast.shutdown_all
18
+ end
19
+
20
+ def initialize(options = {})
21
+ @env = options[:env] || 'production'
22
+
23
+ # Disable default logging to avoid messages to STDOUT.
24
+ System.set_property('hazelcast.logging.type', 'none')
25
+
26
+ cfg = com.hazelcast.config.Config.new
27
+ cfg.set_property('hazelcast.version.check.enabled', 'false')
28
+ cfg.set_property('hazelcast.memcache.enabled', 'false')
29
+ cfg.set_property('hazelcast.rest.enabled', 'false')
30
+
31
+ # Disable system log to avoid messages to STDOUT.
32
+ cfg.set_property('hazelcast.system.log.enabled', 'false')
33
+
34
+ # Set hazelcast group name to avoid hazelcast instances joining other
35
+ # existing groups.
36
+ cfg.get_group_config.set_name("hz-dcell-#{@env}")
37
+
38
+ @hazelcast = Hazelcast.new_hazelcast_instance(cfg)
39
+ @hazelcast.get_logging_service.add_log_listener(Level::INFO, LogListener.new)
40
+ end
41
+
42
+ def get_map(name)
43
+ Map.new(@hazelcast, name)
44
+ end
45
+
46
+ class Map
47
+ extend Forwardable
48
+
49
+ def_delegators :@map, :get, :put, :key_set, :clear
50
+
51
+ def initialize(hazelcast, name)
52
+ @map = hazelcast.get_map(name)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ require 'java'
2
+
3
+ Dir[File.join(File.dirname(__FILE__), "jars/*.jar")].each do |jar|
4
+ require jar
5
+ end
@@ -0,0 +1,5 @@
1
+ module DCell
2
+ class Hazelcast
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,96 @@
1
+ require 'dcell/hazelcast'
2
+
3
+ module DCell
4
+ module Registry
5
+ class HazelcastAdapter
6
+ def initialize(options = {})
7
+ # Convert all options to symbols :/
8
+ options = options.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
9
+
10
+ @env = options[:env] || 'production'
11
+
12
+ @hazelcast = Hazelcast.new(:env => @env)
13
+ @node_registry = NodeRegistry.new(@hazelcast)
14
+ @global_registry = GlobalRegistry.new(@hazelcast)
15
+ end
16
+
17
+ def clear_nodes
18
+ @node_registry.clear
19
+ end
20
+
21
+ def clear_globals
22
+ @global_registry.clear
23
+ end
24
+
25
+ class NodeRegistry
26
+ def initialize(hazelcast)
27
+ @map = hazelcast.get_map('nodes')
28
+ end
29
+
30
+ def get(node_id)
31
+ @map.get(node_id)
32
+ end
33
+
34
+ def set(node_id, addr)
35
+ @map.put(node_id, addr)
36
+ end
37
+
38
+ def nodes
39
+ @map.key_set.to_a
40
+ end
41
+
42
+ def clear
43
+ @map.clear
44
+ end
45
+ end
46
+
47
+ def get_node(node_id)
48
+ @node_registry.get(node_id)
49
+ end
50
+
51
+ def set_node(node_id, addr)
52
+ @node_registry.set(node_id, addr)
53
+ end
54
+
55
+ def nodes
56
+ @node_registry.nodes
57
+ end
58
+
59
+ class GlobalRegistry
60
+ def initialize(hazelcast)
61
+ @map = hazelcast.get_map('globals')
62
+ end
63
+
64
+ def get(key)
65
+ string = @map.get(key.to_s)
66
+ Marshal.load(string) if string
67
+ end
68
+
69
+ def set(key, value)
70
+ string = Marshal.dump(value)
71
+ @map.put(key.to_s, string)
72
+ end
73
+
74
+ def global_keys
75
+ @map.key_set.to_a
76
+ end
77
+
78
+ def clear
79
+ @map.clear
80
+ end
81
+ end
82
+
83
+ def get_global(key)
84
+ @global_registry.get(key)
85
+ end
86
+
87
+ def set_global(key, value)
88
+ @global_registry.set(key, value)
89
+ end
90
+
91
+ def global_keys
92
+ @global_registry.global_keys
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'dcell/registries/hazelcast_adapter'
3
+
4
+ describe DCell::Registry::HazelcastAdapter do
5
+ subject { described_class.new }
6
+
7
+ after do
8
+ DCell::Hazelcast.shutdown_all
9
+ end
10
+
11
+ context "node registry" do
12
+ before :each do
13
+ subject.clear_nodes
14
+ end
15
+
16
+ it "stores node addresses" do
17
+ address = "tcp://localhost:7777"
18
+
19
+ subject.set_node("foobar", address)
20
+ subject.get_node("foobar").should == address
21
+ end
22
+
23
+ it "stores the IDs of all nodes" do
24
+ subject.set_node("foobar", "tcp://localhost:7777")
25
+ subject.nodes.should include "foobar"
26
+ end
27
+ end
28
+
29
+ context "global registry" do
30
+ before :each do
31
+ subject.clear_globals
32
+ end
33
+
34
+ it "stores values" do
35
+ subject.set_global("foobar", [1,2,3])
36
+ subject.get_global("foobar").should == [1,2,3]
37
+ end
38
+
39
+ it "stores the keys of all globals" do
40
+ subject.set_global("foobar", true)
41
+ subject.global_keys.should include "foobar"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ require 'dcell'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dcell-hazelcast
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Bernd Ahlers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ none: false
28
+ prerelease: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: !binary |-
37
+ MA==
38
+ none: false
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: !binary |-
44
+ MA==
45
+ none: false
46
+ prerelease: false
47
+ type: :development
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: !binary |-
55
+ MA==
56
+ none: false
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: !binary |-
62
+ MA==
63
+ none: false
64
+ prerelease: false
65
+ type: :development
66
+ - !ruby/object:Gem::Dependency
67
+ name: dcell
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: !binary |-
73
+ MA==
74
+ none: false
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: !binary |-
80
+ MA==
81
+ none: false
82
+ prerelease: false
83
+ type: :runtime
84
+ description: Hazelcast registry for DCell
85
+ email:
86
+ - bernd@tuneafish.de
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - dcell-hazelcast.gemspec
97
+ - examples/dcell-hazelcast.rb
98
+ - lib/dcell/hazelcast.rb
99
+ - lib/dcell/hazelcast/jars.rb
100
+ - lib/dcell/hazelcast/jars/hazelcast-2.5.1.jar
101
+ - lib/dcell/hazelcast/version.rb
102
+ - lib/dcell/registries/hazelcast_adapter.rb
103
+ - spec/dcell/registries/hazelcast_adapter_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/bernd/dcell-hazelcast
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: !binary |-
117
+ MA==
118
+ segments:
119
+ - 0
120
+ hash: 2
121
+ none: false
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: !binary |-
127
+ MA==
128
+ segments:
129
+ - 0
130
+ hash: 2
131
+ none: false
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.24
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: A Hazelcast registry for DCell.
138
+ test_files:
139
+ - spec/dcell/registries/hazelcast_adapter_spec.rb
140
+ - spec/spec_helper.rb