consistent-cluster 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 083d031971f3257c3484fe07f3a57ec3f128c540
4
- data.tar.gz: e4672c6b95484ba06ecae0f227c72962d2b49d69
3
+ metadata.gz: 1a619832f972391c4026f17d7b373eee628a84dd
4
+ data.tar.gz: b84b0dcf405366e3caec3ed2968e775d54e7e875
5
5
  SHA512:
6
- metadata.gz: 45d9b3225dd8f65df6e0c3be4a96c2198b865815fef6a17387ef0fb8c48e79fa58d3813f147588366cd75893cbde5a302843356c0e18f5fb30026bbe431b4b35
7
- data.tar.gz: f2f031edcabcd583e32894914009fd0c1bf00a7216b0ffe799630182140bd4da12da0998e8c77c24f7fb371a1d94a8a90540221cf475a6eee769cc6e2d5d219f
6
+ metadata.gz: 4930145efffb208b04d2525f01f6821dfee4d26c18f3ec8a578ea56c7d98aa0c12ecf045929e8f1458d94fa33c7b18ff9e36602bf15f61764d8319da74879465
7
+ data.tar.gz: cdc9c0018a311e9ce11b5a8a40049edcd3d63b568d09a9e391de8e0d233bf395cd665f1cc7559f2d2342ca8e25b7a11810126f14520f734932e202573142576b
@@ -11,24 +11,25 @@ all_config = {
11
11
  consistent_hashing_replicas: 100,
12
12
  create_proc: Proc.new { |zk_content|
13
13
  app_info = JSON.parse(zk_content)
14
- ThriftClient.new("servers" => "#{app_info["host"]}:#{app_info["port"]}",
15
- "multiplexed" => app_info["serviceNames"].length > 1,
16
- "protocol" => 'binary',
17
- "transport" => 'socket',
18
- "framed" => false,
19
- "disconnect_exception_classes" => '',
20
- "application_exception_classes" => '',
21
- "size" => 1,
22
- "timeout" => 12,
23
- "client_class" => "HappyProfile::HappyProfileThriftService::Client",
24
- "test_on_borrow" => true,
25
- "pool_timeout" => 12)
14
+ client = ThriftClient.new(
15
+ "servers" => "#{app_info["host"]}:#{app_info["port"]}",
16
+ "multiplexed" => app_info["serviceNames"].length > 1,
17
+ "protocol" => 'binary',
18
+ "transport" => 'socket',
19
+ "framed" => false,
20
+ "disconnect_exception_classes" => '',
21
+ "application_exception_classes" => '',
22
+ "size" => 1,
23
+ "timeout" => 12,
24
+ "client_class" => "HappyProfile::HappyProfileThriftService::Client",
25
+ "test_on_borrow" => true,
26
+ "pool_timeout" => 12)
26
27
  },
27
28
  destroy_proc: Proc.new { |client|
28
29
  client.destroy
29
30
  },
30
31
  after_sync_proc: Proc.new { |info|
31
- puts info.inspect
32
+ puts info.keys.inspect
32
33
  },
33
34
  zookeeper_service: "127.0.0.1:2181",
34
35
  zookeeper_path: "/test"
@@ -16,14 +16,19 @@ def set(hash)
16
16
  $zk.set("#{$path}/#{app_name}",hash.to_json)
17
17
  end
18
18
 
19
- def delete(app)
20
- $zk.delete("#{$path}/#{app}")
19
+ def delete(hash)
20
+ app_name = hash[:id]
21
+ delete_with_app_name(app_name)
22
+ end
23
+
24
+ def delete_with_app_name(app_name)
25
+ $zk.delete("#{$path}/#{app_name}")
21
26
  end
22
27
 
23
28
  def clear
24
29
  apps = $zk.children($path)
25
- apps.each do |app|
26
- delete(app)
30
+ apps.each do |app_name|
31
+ delete_with_app_name(app_name)
27
32
  end
28
33
  end
29
34
 
@@ -42,7 +47,7 @@ app2 = {
42
47
  host: "127.0.0.1",
43
48
  port: "9092",
44
49
  id: "127.0.0.1:9092",
45
- protocolType: "thrift",
50
+ protocolType: "thriftx",
46
51
  serviceNames: ["com.ximalaya.service.uts.api.thrift.IUserTrackRecordServiceHandler$Iface"]
47
52
  }
48
53
 
@@ -51,7 +56,7 @@ app3 = {
51
56
  host: "127.0.0.1",
52
57
  port: "9093",
53
58
  id: "127.0.0.1:9093",
54
- protocolType: "thrift",
59
+ protocolType: "thriftxx",
55
60
  serviceNames: ["com.ximalaya.service.uts.api.thrift.IUserTrackRecordServiceHandler$Iface"]
56
61
  }
57
62
 
@@ -63,7 +68,36 @@ create(app2)
63
68
 
64
69
  create(app3)
65
70
 
66
- #set(app2)
71
+ set(app2)
72
+
73
+ delete(app2)
74
+
75
+ create(app2)
76
+
77
+ set(app2)
78
+
79
+ delete(app2)
80
+
81
+ create(app2)
82
+
83
+ delete(app2)
84
+
85
+ create(app2)
86
+
87
+ set(app2)
88
+
89
+ delete(app2)
90
+
91
+ create(app2)
92
+
93
+ set(app2)
94
+
95
+ set(app1)
96
+
97
+ set(app2)
98
+
99
+ set(app3)
100
+
67
101
 
68
102
 
69
103
 
@@ -3,8 +3,6 @@ require "consistent-cluster/version"
3
3
 
4
4
  require "consistent-cluster/consistent_hashing"
5
5
 
6
- gem "zk", "~> 1.9.5" #gem本身不默认依赖zk,此处补充依赖
7
-
8
6
  require "zk"
9
7
 
10
8
  module ConsistentCluster
@@ -17,6 +15,7 @@ module ConsistentCluster
17
15
 
18
16
  @data = {}
19
17
  @cluster = {}
18
+ @node_register = {}
20
19
 
21
20
  replicas = options[:consistent_hashing_replicas] || 3
22
21
  @ring = ConsistentHashing::Ring.new([],replicas)
@@ -38,7 +37,7 @@ module ConsistentCluster
38
37
 
39
38
  def shard(key=nil)
40
39
  cluster_sum = @cluster.length
41
- raise "no service available" if cluster_sum < 1
40
+ raise "no service available at #{@path}" if cluster_sum < 1
42
41
  if key
43
42
  point = @ring.point_for(key)
44
43
  server = @cluster[point.node]
@@ -63,7 +62,7 @@ module ConsistentCluster
63
62
  @syncing = true
64
63
  while @to_sync
65
64
  @to_sync = false
66
- app_names = sync_children
65
+ app_names = get_app_names
67
66
  current_app_names = @cluster.keys
68
67
 
69
68
  to_update = current_app_names&app_names
@@ -89,24 +88,31 @@ module ConsistentCluster
89
88
  end
90
89
  end
91
90
 
92
- def sync_children
91
+ def get_app_names
93
92
  @zk.children(@path, watch: true)
94
93
  end
95
94
 
96
95
  def create_service(app_name)
97
96
 
97
+ app_path = "#{@path}/#{app_name}"
98
+ @node_register[app_name] = @zk.register(app_path) do |event|
99
+ sync_services
100
+ end
101
+
98
102
  app_content = get_app_content(app_name)
99
103
 
100
104
  server = @create_proc.call(app_content)
101
105
 
102
- @data[app_name] = app_content
106
+ if server
103
107
 
104
- @cluster[app_name] = server
108
+ @cluster[app_name] = server
105
109
 
106
- @ring.add(app_name)
110
+ @ring.add(app_name)
111
+
112
+ @data[app_name] = app_content
113
+
114
+ end
107
115
 
108
- app_path = "#{@path}/#{app_name}"
109
- @zk.get(app_path, watch: true)
110
116
  rescue Exception => boom
111
117
  puts "sync create :#{app_name} raise #{boom.class} - #{boom.message}"
112
118
  end
@@ -123,6 +129,11 @@ module ConsistentCluster
123
129
  end
124
130
  end
125
131
 
132
+ reg = @node_register[app_name]
133
+ if reg
134
+ reg.unregister
135
+ end
136
+
126
137
  rescue Exception => boom
127
138
  puts "sync destroy :#{app_name} raise #{boom.class} - #{boom.message}"
128
139
  end
@@ -133,6 +144,7 @@ module ConsistentCluster
133
144
  app_content = get_app_content(app_name)
134
145
 
135
146
  cache_info = @data[app_name]
147
+
136
148
  if cache_info != app_content
137
149
  destroy_service(app_name)
138
150
  create_service(app_name)
@@ -143,7 +155,7 @@ module ConsistentCluster
143
155
 
144
156
  def get_app_content(app_name)
145
157
  app_path = "#{@path}/#{app_name}"
146
- content = @zk.get(app_path).first
158
+ content = @zk.get(app_path, watch: true).first
147
159
  content
148
160
  end
149
161
 
@@ -1,3 +1,3 @@
1
1
  module ConsistentCluster
2
- Version = "1.0.1"
2
+ Version = "1.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consistent-cluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jeffrey6052
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-26 00:00:00.000000000 Z
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: atomic
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.1.99
19
+ version: '1.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.1.99
26
+ version: '1.1'
27
27
  description: "用于整合服务集群接口,方便客户端调用; 调用方式包含一致性哈希逻辑,轮询逻辑; 支持绑定zookeeper,同步集群配置"
28
28
  email:
29
29
  - jeffrey6052@163.com
@@ -62,8 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  requirements: []
64
64
  rubyforge_project:
65
- rubygems_version: 2.4.5
65
+ rubygems_version: 2.2.2
66
66
  signing_key:
67
67
  specification_version: 4
68
- summary: ''
68
+ summary: "一致性哈希集群"
69
69
  test_files: []