Class: DruidConfig::ZK

Inherits:
Object
  • Object
show all
Defined in:
lib/druid_config/zk.rb

Overview

Class to connect and get information about nodes in cluster using Zookeeper

Constant Summary collapse

COORDINATOR =

Coordinator service

'coordinator'
OVERLORD =
'overlord'
SERVICES =
[COORDINATOR, OVERLORD]

Instance Method Summary collapse

Constructor Details

#initialize(uri, opts = {}) ⇒ ZK

Initialize variables and call register

Parameters:

uri

Uri of zookeper

opts

Hash with options:

- discovery_path: Custom URL of discovery path for Druid


31
32
33
34
35
36
37
38
# File 'lib/druid_config/zk.rb', line 31

def initialize(uri, opts = {})
  # Control Zookeper connection
  @zk = ::ZK.new(uri, chroot: :check)
  @registry = Hash.new { |hash, key| hash[key] = [] }
  @discovery_path = opts[:discovery_path] || '/discovery'
  @watched_services = {}
  register
end

Instance Method Details

#check_service(service) ⇒ Object

Check a service



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/druid_config/zk.rb', line 185

def check_service(service)
  return if @watched_services.include?(service) ||
            !SERVICES.include?(service)

  # Start to watch this service
  watch_service(service)

  known = @registry[service].map { |node| node[:name] }
  live = @zk.children(watch_path(service), watch: true)
  new_list = @registry[service].select { |node| live.include?(node[:name]) }
  $log.info("druid.zk checking", service: service, known: known, live: live, new_list: new_list) if $log

  # verify the new entries to be living brokers
  (live - known).each do |name|
    uri = verify_node(name, service)
    new_list.push(name: name, uri: uri) if uri
  end

  if new_list.empty?
    # don't show services w/o active brokers
    unregister_service(service)
  else
    register_service(service, new_list)
  end
end

#check_servicesObject

Check current services



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/druid_config/zk.rb', line 135

def check_services
  $log.info("druid.zk checking services") if $log
  zk_services = @zk.children(@discovery_path, watch: true)

  (services - zk_services).each do |service|
    unregister_service(service)
  end

  zk_services.each do |service|
    check_service(service)
  end
end

#close!Object

Force to close Zookeper connection



56
57
58
59
# File 'lib/druid_config/zk.rb', line 56

def close!
  $log.info('druid.zk shutting down') if $log
  @zk.close!
end

#coordinatorObject

Return the URI of a random available coordinator. Poor mans load balancing



65
66
67
# File 'lib/druid_config/zk.rb', line 65

def coordinator
  random_node(COORDINATOR)
end

#overlordObject

Return the URI of a random available overlord. Poor mans load balancing



73
74
75
# File 'lib/druid_config/zk.rb', line 73

def overlord
  random_node(OVERLORD)
end

#random_node(service) ⇒ Object

Return a random value of a service

Parameters:

service

String with the name of the service



84
85
86
87
88
89
# File 'lib/druid_config/zk.rb', line 84

def random_node(service)
  return nil if @registry[service].size == 0
  # Return a random broker from available brokers
  i = Random.rand(@registry[service].size)
  @registry[service][i][:uri]
end

#registerObject

Load the data from Zookeeper



43
44
45
46
47
48
49
50
51
# File 'lib/druid_config/zk.rb', line 43

def register
  $log.info('druid.zk register discovery path') if $log
  @zk.on_expired_session { register }
  @zk.register(@discovery_path, only: :child) do
    $log.info('druid.zk got event on discovery path') if $log
    check_services
  end
  check_services
end

#register_service(service, brokers) ⇒ Object

Register a new service



94
95
96
97
98
# File 'lib/druid_config/zk.rb', line 94

def register_service(service, brokers)
  $log.info("druid.zk register", service: service, brokers: brokers) if $log
  # poor mans load balancing
  @registry[service] = brokers.shuffle
end

#servicesObject

Get all available services



214
215
216
# File 'lib/druid_config/zk.rb', line 214

def services
  @registry.keys
end

#to_sObject



218
219
220
# File 'lib/druid_config/zk.rb', line 218

def to_s
  @registry.to_s
end

#unregister_service(service) ⇒ Object

Unregister a service



103
104
105
106
107
# File 'lib/druid_config/zk.rb', line 103

def unregister_service(service)
  $log.info("druid.zk unregister", service: service) if $log
  @registry.delete(service)
  unwatch_service(service)
end

#unwatch_service(service) ⇒ Object

Unset a service to watch



126
127
128
129
130
# File 'lib/druid_config/zk.rb', line 126

def unwatch_service(service)
  return unless @watched_services.include?(service)
  $log.info("druid.zk unwatch", service: service) if $log
  @watched_services.delete(service).unregister
end

#verify_node(name, service) ⇒ Object

Verify is a Coordinator is available

Parameters:

name

String with the name of the coordinator

service

String with the service

Returns:

URI of the coordinator or false



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/druid_config/zk.rb', line 160

def verify_node(name, service)
  $log.info("druid.zk verify", node: name, service: service) if $log
  info = @zk.get("#{watch_path(service)}/#{name}")
  node = JSON.parse(info[0])
  uri = "http://#{node['address']}:#{node['port']}/"
  check = RestClient::Request.execute(
    method: :get, url: "#{uri}status",
    timeout: 5, open_timeout: 5
  )
  $log.info("druid.zk verified", uri: uri, sources: check) if $log
  return uri if check.code == 200
rescue
  return false
end

#watch_path(service) ⇒ Object

Watch path of a service



178
179
180
# File 'lib/druid_config/zk.rb', line 178

def watch_path(service)
  "#{@discovery_path}/#{service}"
end

#watch_service(service) ⇒ Object

Set a watcher for a service



112
113
114
115
116
117
118
119
120
121
# File 'lib/druid_config/zk.rb', line 112

def watch_service(service)
  return if @watched_services.include?(service)
  $log.info("druid.zk watch", service: service) if $log
  watch = @zk.register(watch_path(service), only: :child) do |event|
    $log.info("druid.zk got event on watch path for", service: service, event: event) if $log
    unwatch_service(service)
    check_service(service)
  end
  @watched_services[service] = watch
end