zk-server 0.9.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/zk-server/java_embedded.rb +128 -0
- data/lib/zk-server/version.rb +1 -1
- data/lib/zk-server.rb +10 -4
- data/spec/zk-server/java_embedded_spec.rb +32 -0
- data/spec/zk-server/process_spec.rb +1 -1
- metadata +8 -5
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'java'
|
2
|
+
|
3
|
+
module ZK
|
4
|
+
module Server
|
5
|
+
# gah, this needs to be a singleton, but whaddyagonnado
|
6
|
+
class JavaEmbedded < Base
|
7
|
+
System = java.lang.System
|
8
|
+
|
9
|
+
JFile = java.io.File
|
10
|
+
|
11
|
+
attr_reader :zookeeper_thread
|
12
|
+
|
13
|
+
# yeah, holla atcha boy, JZ
|
14
|
+
module JZ
|
15
|
+
# ok, this is seriously seriously ugly, but we want to dynamically
|
16
|
+
# config log4j in this process which would require brain surgery if
|
17
|
+
# i didn't do it dynamically, therefore we need to have all of this stuff
|
18
|
+
# bind late, and we want some readable shortcuts to these fully-qualified
|
19
|
+
# path names *after* we've done our requires in 'require_dependencies'
|
20
|
+
#
|
21
|
+
# so, yeah, sorry about this
|
22
|
+
|
23
|
+
def self.dynamically_create_consts
|
24
|
+
hash = {
|
25
|
+
:Quorum => org.apache.zookeeper.server.quorum,
|
26
|
+
:Server => org.apache.zookeeper.server,
|
27
|
+
:FileTxnSnapLog => org.apache.zookeeper.server.persistence.FileTxnSnapLog
|
28
|
+
}
|
29
|
+
|
30
|
+
hash.each do |k,v|
|
31
|
+
const_set(k, v) unless const_defined?(k)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def shutdown
|
37
|
+
@mutex.synchronize do
|
38
|
+
return unless @run_called and @cnxn_factory and @zk_server and running?
|
39
|
+
|
40
|
+
@cnxn_factory.shutdown
|
41
|
+
@zk_server.shutdown
|
42
|
+
end
|
43
|
+
|
44
|
+
@zookeeper_thread and @zookeeper_thread.join(5)
|
45
|
+
end
|
46
|
+
|
47
|
+
def running?
|
48
|
+
@mutex.synchronize do
|
49
|
+
@zk_server && @zk_server.running?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def run
|
54
|
+
@mutex.synchronize do
|
55
|
+
return false if @run_called
|
56
|
+
@run_called = true
|
57
|
+
|
58
|
+
create_files!
|
59
|
+
require_dependencies
|
60
|
+
spawn_zookeeper_thread!
|
61
|
+
wait_until_ping
|
62
|
+
true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def spawned?
|
67
|
+
!!@zookeeper_thread
|
68
|
+
end
|
69
|
+
|
70
|
+
def pid
|
71
|
+
$$ # CHEEKY!!
|
72
|
+
end
|
73
|
+
|
74
|
+
def logging_related_system_properties
|
75
|
+
[ ['log4j.configuration', "file://#{ZK::Server.default_log4j_props_path}"],
|
76
|
+
['zookeeper.log.dir', config.log_dir],
|
77
|
+
['zookeeper.root.logger', 'INFO,CONSOLE'], ]
|
78
|
+
end
|
79
|
+
|
80
|
+
# XXX: this assumes that we're only going to ever run one of these per
|
81
|
+
# process (or at least have only one log)
|
82
|
+
def require_dependencies
|
83
|
+
logging_related_system_properties.each do |k,v|
|
84
|
+
System.set_property(k,v)
|
85
|
+
end
|
86
|
+
|
87
|
+
require 'log4j'
|
88
|
+
require 'zookeeper_jar'
|
89
|
+
|
90
|
+
JZ.dynamically_create_consts
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def spawn_zookeeper_thread!
|
95
|
+
@zookeeper_thread ||= Thread.new do
|
96
|
+
Thread.abort_on_exception = true
|
97
|
+
cnxn_factory.startup(zk_server)
|
98
|
+
cnxn_factory.join
|
99
|
+
cnxn_factory.shutdown
|
100
|
+
|
101
|
+
@mutex.synchronize do
|
102
|
+
@exit_cond.broadcast
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def cnxn_factory
|
108
|
+
@cnxn_factory ||= JZ::Server::NIOServerCnxn::Factory.new(zk_config.client_port_address, zk_config.max_client_cnxns)
|
109
|
+
end
|
110
|
+
|
111
|
+
def j_data_log_dir
|
112
|
+
@j_data_log_dir ||= JFile.new(zk_config.data_log_dir)
|
113
|
+
end
|
114
|
+
|
115
|
+
def j_data_dir
|
116
|
+
@j_data_dir ||= JFile.new(zk_config.data_dir)
|
117
|
+
end
|
118
|
+
|
119
|
+
def zk_server
|
120
|
+
@zk_server ||= JZ::Server::ZooKeeperServer.new(j_data_log_dir, j_data_dir, zk_config.tick_time)
|
121
|
+
end
|
122
|
+
|
123
|
+
def zk_config
|
124
|
+
@zk_config ||= JZ::Server::ServerConfig.new.tap { |c| c.parse(zoo_cfg_path) }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
data/lib/zk-server/version.rb
CHANGED
data/lib/zk-server.rb
CHANGED
@@ -35,6 +35,10 @@ module ZK
|
|
35
35
|
defined?(::JRUBY_VERSION)
|
36
36
|
end
|
37
37
|
|
38
|
+
def self.jruby_18?
|
39
|
+
jruby? and ruby_187?
|
40
|
+
end
|
41
|
+
|
38
42
|
def self.jruby_19?
|
39
43
|
jruby? and ruby_19?
|
40
44
|
end
|
@@ -48,7 +52,9 @@ module ZK
|
|
48
52
|
#
|
49
53
|
# @yield [Config] server config instance if block given
|
50
54
|
def self.new(opts={})
|
51
|
-
|
55
|
+
klass = jruby? ? JavaEmbedded : SubProcess
|
56
|
+
|
57
|
+
klass.new(opts).tap do |server|
|
52
58
|
yield server.config if block_given?
|
53
59
|
end
|
54
60
|
end
|
@@ -125,7 +131,7 @@ require 'zk-server/config'
|
|
125
131
|
require 'zk-server/base'
|
126
132
|
require 'zk-server/sub_process'
|
127
133
|
|
128
|
-
|
129
|
-
|
130
|
-
|
134
|
+
if defined?(::JRUBY_VERSION)
|
135
|
+
require 'zk-server/java_embedded'
|
136
|
+
end
|
131
137
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined?(::JRUBY_VERSION)
|
4
|
+
|
5
|
+
describe ZK::Server::JavaEmbedded do
|
6
|
+
let(:tmpdir) { '/tmp/zookeeper' }
|
7
|
+
|
8
|
+
subject do
|
9
|
+
described_class.new(:client_port => 21812, :base_dir => tmpdir)
|
10
|
+
end
|
11
|
+
|
12
|
+
after do
|
13
|
+
subject.shutdown
|
14
|
+
subject.clobber!
|
15
|
+
FileUtils.rm_rf(tmpdir)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
it %[should run the zk server, ping, and then shutdown properly] do
|
20
|
+
subject.run
|
21
|
+
subject.should be_pingable
|
22
|
+
subject.should be_running
|
23
|
+
subject.should be_spawned
|
24
|
+
|
25
|
+
subject.shutdown
|
26
|
+
|
27
|
+
subject.should_not be_pingable
|
28
|
+
subject.should_not be_running
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -14,7 +14,7 @@ describe ZK::Server::SubProcess do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it %[should spawn a ZK server, ping, and then shutdown properly] do
|
17
|
-
|
17
|
+
pending "cannot run this under JRuby" if defined?(::JRUBY_VERSION)
|
18
18
|
|
19
19
|
subject.run
|
20
20
|
subject.should be_pingable
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zk-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
- 0
|
8
|
-
- 9
|
9
7
|
- 1
|
10
|
-
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jonathan D. Simms
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-04 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: bundler
|
@@ -86,11 +86,13 @@ files:
|
|
86
86
|
- lib/zk-server.rb
|
87
87
|
- lib/zk-server/base.rb
|
88
88
|
- lib/zk-server/config.rb
|
89
|
+
- lib/zk-server/java_embedded.rb
|
89
90
|
- lib/zk-server/log4j.properties
|
90
91
|
- lib/zk-server/logging.rb
|
91
92
|
- lib/zk-server/sub_process.rb
|
92
93
|
- lib/zk-server/version.rb
|
93
94
|
- spec/spec_helper.rb
|
95
|
+
- spec/zk-server/java_embedded_spec.rb
|
94
96
|
- spec/zk-server/process_spec.rb
|
95
97
|
- zk-server.gemspec
|
96
98
|
homepage: http://github.com/slyphon/zk-server
|
@@ -128,4 +130,5 @@ specification_version: 3
|
|
128
130
|
summary: runs a standalone zookeeper server
|
129
131
|
test_files:
|
130
132
|
- spec/spec_helper.rb
|
133
|
+
- spec/zk-server/java_embedded_spec.rb
|
131
134
|
- spec/zk-server/process_spec.rb
|