2017-08-01 16:11:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-11 17:43:41 -05:00
|
|
|
# Like a hash, just does its best to stay in sync across the farm
|
|
|
|
# On boot all instances are blank, but they populate as various processes
|
|
|
|
# fill it up
|
|
|
|
|
|
|
|
require 'weakref'
|
2016-01-29 17:01:15 -05:00
|
|
|
require 'base64'
|
2014-11-11 17:43:41 -05:00
|
|
|
|
|
|
|
class DistributedCache
|
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
class Manager
|
2017-10-20 08:33:29 -04:00
|
|
|
CHANNEL_NAME ||= '/distributed_hash'.freeze
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def initialize(message_bus = nil)
|
|
|
|
@subscribers = []
|
|
|
|
@subscribed = false
|
|
|
|
@lock = Mutex.new
|
|
|
|
@message_bus = message_bus || MessageBus
|
|
|
|
end
|
2014-11-12 20:41:35 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def subscribers
|
|
|
|
@subscribers
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def process_message(message)
|
|
|
|
i = @subscribers.length - 1
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
payload = message.data
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
while i >= 0
|
|
|
|
begin
|
|
|
|
current = @subscribers[i]
|
2014-11-12 20:41:35 -05:00
|
|
|
|
2017-10-20 08:33:29 -04:00
|
|
|
next if payload["origin"] == current.identity && !Rails.env.test?
|
2017-08-01 16:11:48 -04:00
|
|
|
next if current.key != payload["hash_key"]
|
|
|
|
next if payload["discourse_version"] != Discourse.git_version
|
2014-11-12 20:41:35 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
hash = current.hash(message.site_id)
|
|
|
|
|
|
|
|
case payload["op"]
|
|
|
|
when "set" then hash[payload["key"]] = payload["marshalled"] ? Marshal.load(Base64.decode64(payload["value"])) : payload["value"]
|
|
|
|
when "delete" then hash.delete(payload["key"])
|
|
|
|
when "clear" then hash.clear
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
rescue WeakRef::RefError
|
|
|
|
@subscribers.delete_at(i)
|
|
|
|
ensure
|
|
|
|
i -= 1
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def ensure_subscribe!
|
2014-11-11 17:43:41 -05:00
|
|
|
return if @subscribed
|
2017-08-01 16:11:48 -04:00
|
|
|
@lock.synchronize do
|
|
|
|
return if @subscribed
|
2017-10-20 08:33:29 -04:00
|
|
|
@message_bus.subscribe(CHANNEL_NAME) do |message|
|
2017-08-01 16:11:48 -04:00
|
|
|
@lock.synchronize do
|
|
|
|
process_message(message)
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
end
|
2017-08-01 16:11:48 -04:00
|
|
|
@subscribed = true
|
2014-11-11 17:43:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def publish(hash, message)
|
|
|
|
message[:origin] = hash.identity
|
|
|
|
message[:hash_key] = hash.key
|
|
|
|
message[:discourse_version] = Discourse.git_version
|
2017-10-20 08:33:29 -04:00
|
|
|
@message_bus.publish(CHANNEL_NAME, message, user_ids: [-1])
|
2017-08-01 16:11:48 -04:00
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def set(hash, key, value)
|
|
|
|
# special support for set
|
2018-08-08 02:44:56 -04:00
|
|
|
marshal = (Set === value || Hash === value || Array === value)
|
2017-08-01 16:11:48 -04:00
|
|
|
value = Base64.encode64(Marshal.dump(value)) if marshal
|
|
|
|
publish(hash, op: :set, key: key, value: value, marshalled: marshal)
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def delete(hash, key)
|
|
|
|
publish(hash, op: :delete, key: key)
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def clear(hash)
|
|
|
|
publish(hash, op: :clear)
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
def register(hash)
|
|
|
|
@lock.synchronize do
|
|
|
|
@subscribers << WeakRef.new(hash)
|
|
|
|
end
|
2014-11-11 17:43:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-01 16:11:48 -04:00
|
|
|
@default_manager = Manager.new
|
|
|
|
|
|
|
|
def self.default_manager
|
|
|
|
@default_manager
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :key
|
2014-11-11 17:43:41 -05:00
|
|
|
|
2017-10-20 04:39:31 -04:00
|
|
|
def initialize(key, manager: nil, namespace: true)
|
2014-11-11 17:43:41 -05:00
|
|
|
@key = key
|
|
|
|
@data = {}
|
2017-08-01 16:11:48 -04:00
|
|
|
@manager = manager || DistributedCache.default_manager
|
2017-10-20 04:39:31 -04:00
|
|
|
@namespace = namespace
|
2017-08-01 16:11:48 -04:00
|
|
|
|
|
|
|
@manager.ensure_subscribe!
|
|
|
|
@manager.register(self)
|
2014-11-11 17:43:41 -05:00
|
|
|
end
|
|
|
|
|
2015-05-01 01:25:23 -04:00
|
|
|
def identity
|
|
|
|
# fork resilient / multi machine identity
|
|
|
|
(@seed_id ||= SecureRandom.hex) + "#{Process.pid}"
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def []=(k, v)
|
2014-11-11 17:43:41 -05:00
|
|
|
k = k.to_s if Symbol === k
|
2017-08-01 16:11:48 -04:00
|
|
|
@manager.set(self, k, v)
|
2014-11-11 17:43:41 -05:00
|
|
|
hash[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](k)
|
|
|
|
k = k.to_s if Symbol === k
|
|
|
|
hash[k]
|
|
|
|
end
|
|
|
|
|
2017-10-20 10:35:47 -04:00
|
|
|
def delete(k, publish: true)
|
2014-11-11 17:43:41 -05:00
|
|
|
k = k.to_s if Symbol === k
|
2017-10-20 10:35:47 -04:00
|
|
|
@manager.delete(self, k) if publish
|
2014-11-11 17:43:41 -05:00
|
|
|
hash.delete(k)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
2017-08-01 16:11:48 -04:00
|
|
|
@manager.clear(self)
|
2014-11-11 17:43:41 -05:00
|
|
|
hash.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash(db = nil)
|
2017-10-20 08:33:29 -04:00
|
|
|
db =
|
2017-10-20 04:39:31 -04:00
|
|
|
if @namespace
|
2017-10-20 08:33:29 -04:00
|
|
|
db || RailsMultisite::ConnectionManagement.current_db
|
2017-10-20 04:39:31 -04:00
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement::DEFAULT
|
|
|
|
end
|
|
|
|
|
2014-11-11 17:43:41 -05:00
|
|
|
@data[db] ||= ThreadSafe::Hash.new
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|