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


25
26
27
28
29
30
31
32
# File 'lib/druid_config/zk.rb', line 25

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/druid_config/zk.rb', line 179

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



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/druid_config/zk.rb', line 129

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



50
51
52
53
# File 'lib/druid_config/zk.rb', line 50

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



59
60
61
# File 'lib/druid_config/zk.rb', line 59

def coordinator
  random_node(COORDINATOR)
end

#overlordObject

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



67
68
69
# File 'lib/druid_config/zk.rb', line 67

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



78
79
80
81
82
83
# File 'lib/druid_config/zk.rb', line 78

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



37
38
39
40
41
42
43
44
45
# File 'lib/druid_config/zk.rb', line 37

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



88
89
90
91
92
# File 'lib/druid_config/zk.rb', line 88

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



208
209
210
# File 'lib/druid_config/zk.rb', line 208

def services
  @registry.keys
end

#to_sObject



212
213
214
# File 'lib/druid_config/zk.rb', line 212

def to_s
  @registry.to_s
end

#unregister_service(service) ⇒ Object

Unregister a service



97
98
99
100
101
# File 'lib/druid_config/zk.rb', line 97

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



120
121
122
123
124
# File 'lib/druid_config/zk.rb', line 120

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/druid_config/zk.rb', line 154

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



172
173
174
# File 'lib/druid_config/zk.rb', line 172

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

#watch_service(service) ⇒ Object

Set a watcher for a service



106
107
108
109
110
111
112
113
114
115
# File 'lib/druid_config/zk.rb', line 106

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