A platform for community discussion. Free, open, simple.
Revert "FIX: Redis fallback handler refactoring (#8771)" (#8776)
Krzysztof Kotlarek8cc09fc6687f
diff --git a/lib/concurrency.rb b/lib/concurrency.rbdeleted file mode 100644index 3bca0031055..00000000000--- a/lib/concurrency.rb+++ /dev/null@@ -1,21 +0,0 @@-# frozen_string_literal: true--# This is the 'actually concurrent' counterpart to-# Concurrency::Scenario::Execution from spec/support/concurrency.rb-module Concurrency- class ThreadedExecution- def new_mutex- Mutex.new- end-- def sleep(delay)- super(delay)- nil- end-- def spawn(&blk)- Thread.new(&blk)- nil- end- end-enddiff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rbindex 912aea25843..728e244db74 100644--- a/lib/discourse_redis.rb+++ b/lib/discourse_redis.rb@@ -3,248 +3,139 @@## A wrapper around redis that namespaces keys with the current site id#-require_dependency 'cache'-require_dependency 'concurrency'class DiscourseRedis- class RedisStatus+ class FallbackHandler+ include Singleton+MASTER_ROLE_STATUS = "role:master".freeze+ MASTER_LOADING_STATUS = "loading:1".freezeMASTER_LOADED_STATUS = "loading:0".freezeCONNECTION_TYPES = %w{normal pubsub}.each(&:freeze)- def initialize(master_config, slave_config)- master_config = master_config.dup.freeze unless master_config.frozen?- slave_config = slave_config.dup.freeze unless slave_config.frozen?+ def initialize+ @master = true+ @running = false+ @mutex = Mutex.new+ @slave_config = DiscourseRedis.slave_config+ @message_bus_keepalive_interval = MessageBus.keepalive_interval+ end- @master_config = master_config- @slave_config = slave_config+ def verify_master+ synchronize do+ return if @thread && @thread.alive?++ @thread = Thread.new do+ loop do+ begin+ thread = Thread.new { initiate_fallback_to_master }+ thread.join+ break if synchronize { @master }+ sleep 5+ ensure+ thread.kill+ end+ end+ end+ endend- def master_alive?- master_client = connect(@master_config)+ def initiate_fallback_to_master+ success = falsebegin+ redis_config = DiscourseRedis.config.dup+ redis_config.delete(:connector)+ master_client = ::Redis::Client.new(redis_config)+ logger.warn "#{log_prefix}: Checking connection to master server..."info = master_client.call([:info])- rescue Redis::ConnectionError, Redis::CannotConnectError, RuntimeError => ex- raise ex if ex.class == RuntimeError && ex.message != "Name or service not known"- warn "Master not alive, error connecting"- return false- ensure- master_client.disconnect- end-- unless info.include?(MASTER_LOADED_STATUS)- warn "Master not alive, status is loading"- return false- end-- unless info.include?(MASTER_ROLE_STATUS)- warn "Master not alive, role != master"- return false- end- true- end+ if info.include?(MASTER_LOADED_STATUS) && info.include?(MASTER_ROLE_STATUS)+ begin+ logger.warn "#{log_prefix}: Master server is active, killing all connections to slave..."- def fallback- warn "Killing connections to slave..."+ self.master = true+ slave_client = ::Redis::Client.new(@slave_config)- slave_client = connect(@slave_config)+ CONNECTION_TYPES.each do |connection_type|+ slave_client.call([:client, [:kill, 'type', connection_type]])+ end- begin- CONNECTION_TYPES.each do |connection_type|- slave_client.call([:client, [:kill, 'type', connection_type]])+ MessageBus.keepalive_interval = @message_bus_keepalive_interval+ Discourse.clear_readonly!+ Discourse.request_refresh!+ success = true+ ensure+ slave_client&.disconnect+ endend- rescue Redis::ConnectionError, Redis::CannotConnectError, RuntimeError => ex- raise ex if ex.class == RuntimeError && ex.message != "Name or service not known"- warn "Attempted a redis fallback, but connection to slave failed"+ rescue => e+ logger.warn "#{log_prefix}: Connection to Master server failed with '#{e.message}'"ensure- slave_client.disconnect- end- end-- private-- def connect(config)- config = config.dup- config.delete(:connector)- ::Redis::Client.new(config)- end-- def log_prefix- @log_prefix ||= begin- master_string = "#{@master_config[:host]}:#{@master_config[:port]}"- slave_string = "#{@slave_config[:host]}:#{@slave_config[:port]}"- "RedisStatus master=#{master_string} slave=#{slave_string}"+ master_client&.disconnectend- end-- def warn(message)- Rails.logger.warn "#{log_prefix}: #{message}"- end- end-- class FallbackHandler- def initialize(log_prefix, redis_status, execution)- @log_prefix = log_prefix- @redis_status = redis_status- @mutex = execution.new_mutex- @execution = execution- @master = true- @event_handlers = []- end- def add_callbacks(handler)- @mutex.synchronize do- @event_handlers << handler- end+ successend- def start_reset- @mutex.synchronize do- if @master- @master = false- trigger(:down)- true- else- false- end- end+ def master+ synchronize { @master }end- def use_master?- master = @mutex.synchronize { @master }- if !master- false- elsif safe_master_alive?- true- else- if start_reset- @execution.spawn do- loop do- @execution.sleep 5- info "Checking connection to master"- if safe_master_alive?- @mutex.synchronize do- @master = true- @redis_status.fallback- trigger(:up)- end- break- end- end- end- end+ def master=(args)+ synchronize do+ @master = args- false+ # Disables MessageBus keepalive when Redis is in readonly mode+ MessageBus.keepalive_interval = 0 if !@masterendendprivate- attr_reader :log_prefix-- def trigger(event)- @event_handlers.each do |handler|- begin- handler.public_send(event)- rescue Exception => e- Discourse.warn_exception(e, message: "Error running FallbackHandler callback")- end- end- end-- def info(message)- Rails.logger.info "#{log_prefix}: #{message}"- end-- def safe_master_alive?- begin- @redis_status.master_alive?- rescue Exception => e- Discourse.warn_exception(e, message: "Error running master_alive?")- false- end- end- end-- class MessageBusFallbackCallbacks- def down- @keepalive_interval, MessageBus.keepalive_interval =- MessageBus.keepalive_interval, 0- end-- def up- MessageBus.keepalive_interval = @keepalive_interval+ def synchronize+ @mutex.synchronize { yield }end- end- class MainRedisReadOnlyCallbacks- def down+ def logger+ Rails.loggerend- def up- Discourse.clear_readonly!- Discourse.request_refresh!+ def log_prefix+ "#{self.class}"endend- class FallbackHandlers- include Singleton-- def initialize- @mutex = Mutex.new- @fallback_handlers = {}+ class Connector < Redis::Client::Connector+ def initialize(options)+ super(options)+ @slave_options = DiscourseRedis.slave_config(options)+ @fallback_handler = DiscourseRedis::FallbackHandler.instanceend- def handler_for(config)- config = config.dup.freeze unless config.frozen?-- @mutex.synchronize do- @fallback_handlers[[config[:host], config[:port]]] ||= begin- log_prefix = "FallbackHandler #{config[:host]}:#{config[:port]}"- slave_config = DiscourseRedis.slave_config(config)- redis_status = RedisStatus.new(config, slave_config)-- handler =- FallbackHandler.new(- log_prefix,- redis_status,- Concurrency::ThreadedExecution.new- )-- if config == GlobalSetting.redis_config- handler.add_callbacks(MainRedisReadOnlyCallbacks.new)- end-- if config == GlobalSetting.message_bus_redis_config- handler.add_callbacks(MessageBusFallbackCallbacks.new)- end-- handler- end+ def resolve(client = nil)+ if !@fallback_handler.master+ @fallback_handler.verify_master+ return @slave_optionsend- end-- def self.handler_for(config)- instance.handler_for(config)- end- end- class Connector < Redis::Client::Connector- def initialize(options)- options = options.dup.freeze unless options.frozen?+ begin+ options = @options.dup+ options.delete(:connector)+ client ||= Redis::Client.new(options)- super(options)- @slave_options = DiscourseRedis.slave_config(options).freeze- @fallback_handler = DiscourseRedis::FallbackHandlers.handler_for(options)- end+ loading = client.call([:info, :persistence]).include?(+ DiscourseRedis::FallbackHandler::MASTER_LOADING_STATUS+ )- def resolve- if @fallback_handler.use_master?- @options- else- @slave_options+ loading ? @slave_options : @options+ rescue Redis::ConnectionError, Redis::CannotConnectError, RuntimeError => ex+ raise ex if ex.class == RuntimeError && ex.message != "Name or service not known"+ @fallback_handler.master = false+ @fallback_handler.verify_master+ raise ex+ ensure+ client.disconnectendendend@@ -268,6 +159,10 @@ class DiscourseRedis@namespace = namespaceend+ def self.fallback_handler+ @fallback_handler ||= DiscourseRedis::FallbackHandler.instance+ end+def without_namespace# Only use this if you want to store and fetch data that's shared between sites@redis@@ -281,6 +176,7 @@ class DiscourseRedisSTDERR.puts "WARN: Redis is in a readonly state. Performed a noop"end+ fallback_handler.verify_master if !fallback_handler.masterDiscourse.received_redis_readonly!nilelse@@ -406,4 +302,5 @@ class DiscourseRedisdef remove_namespace(key)key[(namespace.length + 1)..-1]end+enddiff --git a/spec/components/discourse_redis_spec.rb b/spec/components/discourse_redis_spec.rbindex d769afdebfa..6d4af1d8174 100644--- a/spec/components/discourse_redis_spec.rb+++ b/spec/components/discourse_redis_spec.rb@@ -3,84 +3,20 @@require 'rails_helper'describe DiscourseRedis do- before do- DiscourseRedis::FallbackHandlers.instance.instance_variable_set(:@fallback_handlers, {})- end-let(:slave_host) { 'testhost' }let(:slave_port) { 1234 }let(:config) do- GlobalSetting.redis_config.dup.merge(slave_host: 'testhost', slave_port: 1234, connector: DiscourseRedis::Connector)+ DiscourseRedis.config.dup.merge(slave_host: 'testhost', slave_port: 1234, connector: DiscourseRedis::Connector)end- let(:slave_config) { DiscourseRedis.slave_config(config) }+ let(:fallback_handler) { DiscourseRedis::FallbackHandler.instance }it "ignore_readonly returns nil from a pure exception" doresult = DiscourseRedis.ignore_readonly { raise Redis::CommandError.new("READONLY") }expect(result).to eq(nil)end- let!(:master_conn) { mock('master') }-- def self.use_fake_threads- attr_reader :execution-- around(:each) do |example|- scenario =- Concurrency::Scenario.new do |execution|- @execution = execution- example.run- end-- scenario.run(sleep_order: true, runs: 1)- end-- after(:each) do- # Doing this here, as opposed to after example.run, ensures that it- # happens before the mocha expectations are checked.- execution.wait_done- end- end-- def stop_after(time)- execution.sleep(time)- execution.stop_other_tasks- end-- def expect_master_info(conf = config)- conf = conf.dup- conf.delete(:connector)-- Redis::Client.expects(:new)- .with(conf)- .returns(master_conn)-- master_conn.expects(:disconnect)- master_conn- .expects(:call)- .with([:info])- end-- def info_response(*values)- values.map { |x| x.join(':') }.join("\r\n")- end-- def expect_fallback(config = slave_config)- slave_conn = mock('slave')-- config = config.dup- config.delete(:connector)-- Redis::Client.expects(:new)- .with(config)- .returns(slave_conn)-- slave_conn.expects(:call).with([:client, [:kill, 'type', 'normal']])- slave_conn.expects(:call).with([:client, [:kill, 'type', 'pubsub']])- slave_conn.expects(:disconnect)- end-describe 'redis commands' dolet(:raw_redis) { Redis.new(DiscourseRedis.config) }@@ -161,349 +97,150 @@ describe DiscourseRedis doendend- describe DiscourseRedis::RedisStatus do- let(:redis_status) { DiscourseRedis::RedisStatus.new(config, slave_config) }-- context "#master_alive?" do- it "returns false when the master's hostname cannot be resolved" do- expect_master_info- .raises(RuntimeError.new('Name or service not known'))-- expect(redis_status.master_alive?).to eq(false)- end-- it "raises an error if a runtime error is raised" do- error = RuntimeError.new('a random runtime error')- expect_master_info.raises(error)-- expect {- redis_status.master_alive?- }.to raise_error(error)- end-- it "returns false if the master is unavailable" do- expect_master_info.raises(Redis::ConnectionError.new)-- expect(redis_status.master_alive?).to eq(false)- end-- it "returns false if the master is loading" do- expect_master_info- .returns(info_response(['loading', '1'], ['role', 'master']))-- expect(redis_status.master_alive?).to eq(false)- end-- it "returns false if the master is a slave" do- expect_master_info- .returns(info_response(['loading', '0'], ['role', 'slave']))-- expect(redis_status.master_alive?).to eq(false)- end-- it "returns true when the master isn't loading and the role is master" do- expect_master_info- .returns(info_response(['loading', '0'], ['role', 'master']))-- expect(redis_status.master_alive?).to eq(true)- end- end-- context "#fallback" do- it "instructs redis to kill client connections" do- expect_fallback-- redis_status.fallback+ context 'when redis connection is to a slave redis server' do+ it 'should check the status of the master server' do+ begin+ fallback_handler.master = false+ Discourse.redis.without_namespace.expects(:set).raises(Redis::CommandError.new("READONLY"))+ fallback_handler.expects(:verify_master).once+ Discourse.redis.set('test', '1')+ ensure+ fallback_handler.master = true+ Discourse.redis.del('test')endendenddescribe DiscourseRedis::Connector dolet(:connector) { DiscourseRedis::Connector.new(config) }- let(:fallback_handler) { mock('fallback_handler') }- before do- DiscourseRedis::FallbackHandlers.stubs(:handler_for).returns(fallback_handler)+ after do+ fallback_handler.master = trueendit 'should return the master config when master is up' do- fallback_handler.expects(:use_master?).returns(true)expect(connector.resolve).to eq(config)end- it 'should return the slave config when master is down' do- fallback_handler.expects(:use_master?).returns(false)- expect(connector.resolve).to eq(slave_config)- end- end-- describe DiscourseRedis::FallbackHandler do- use_fake_threads-- let!(:redis_status) { mock }- let!(:fallback_handler) { DiscourseRedis::FallbackHandler.new("", redis_status, execution) }-- context "in the initial configuration" do- it "tests that the master is alive and returns true if it is" do- redis_status.expects(:master_alive?).returns(true)-- expect(fallback_handler.use_master?).to eq(true)+ class BrokenRedis+ def initialize(error)+ @error = errorend- it "tests that the master is alive and returns false if it is not" do- redis_status.expects(:master_alive?).returns(false)- expect(fallback_handler.use_master?).to eq(false)-- stop_after(1)+ def call(*args)+ raise @errorend- it "tests that the master is alive and returns false if it raises an exception" do- error = Exception.new- redis_status.expects(:master_alive?).raises(error)-- Discourse.expects(:warn_exception)- .with(error, message: "Error running master_alive?")-- expect(fallback_handler.use_master?).to eq(false)-- stop_after(1)+ def disconnectendend- context "after master_alive? has returned false" do- before do- redis_status.expects(:master_alive?).returns(false)- expect(fallback_handler.use_master?).to eq(false)- end-- it "responds with false to the next call to use_master? without consulting redis_status" do- expect(fallback_handler.use_master?).to eq(false)-- stop_after(1)- end-- it "checks that master is alive again after a timeout" do- redis_status.expects(:master_alive?).returns(false)-- stop_after(6)- end-- it "checks that master is alive again and checks again if an exception is raised" do- error = Exception.new- redis_status.expects(:master_alive?).raises(error)-- Discourse.expects(:warn_exception)- .with(error, message: "Error running master_alive?")-- execution.sleep(6)-- redis_status.expects(:master_alive?).returns(true)- redis_status.expects(:fallback)-- stop_after(5)- end-- it "triggers a fallback after master_alive? returns true" do- redis_status.expects(:master_alive?).returns(true)- redis_status.expects(:fallback)-- stop_after(6)- end-- context "after falling back" do- before do- redis_status.expects(:master_alive?).returns(true)- redis_status.expects(:fallback)-- stop_after(6)- end-- it "tests that the master is alive and returns true if it is" do- redis_status.expects(:master_alive?).returns(true)-- expect(fallback_handler.use_master?).to eq(true)- end-- it "tests that the master is alive and returns false if it is not" do- redis_status.expects(:master_alive?).returns(false)- expect(fallback_handler.use_master?).to eq(false)-- stop_after(1)- end-- it "tests that the master is alive and returns false if it raises an exception" do- error = Exception.new- redis_status.expects(:master_alive?).raises(error)-- Discourse.expects(:warn_exception)- .with(error, message: "Error running master_alive?")+ it 'should return the slave config when master is down' do+ error = Redis::CannotConnectError- expect(fallback_handler.use_master?).to eq(false)+ expect do+ connector.resolve(BrokenRedis.new(error))+ end.to raise_error(Redis::CannotConnectError)- stop_after(1)- end+ config = connector.resolve- it "doesn't do anything to redis_status for a really long time" do- stop_after(1e9)- end- end+ expect(config[:host]).to eq(slave_host)+ expect(config[:port]).to eq(slave_port)end- end-- context "when message bus and main are on the same host" do- use_fake_threads- before do- # Since config is based on GlobalSetting, we need to fetch it before- # stubbing- conf = config+ it "should return the slave config when master's hostname cannot be resolved" do+ error = RuntimeError.new('Name or service not known')- GlobalSetting.stubs(:redis_config).returns(conf)- GlobalSetting.stubs(:message_bus_redis_config).returns(conf)+ expect do+ connector.resolve(BrokenRedis.new(error))+ end.to raise_error(error)- Concurrency::ThreadedExecution.stubs(:new).returns(execution)- end+ expect(fallback_handler.master).to eq(false)- context "when the redis master goes down" do- it "sets the message bus keepalive interval to 0" do- expect_master_info- .raises(Redis::ConnectionError.new)+ config = connector.resolve- MessageBus.expects(:keepalive_interval=).with(0)-- DiscourseRedis::Connector.new(config).resolve-- execution.stop_other_tasks- end+ expect(config[:host]).to eq(slave_host)+ expect(config[:port]).to eq(slave_port)+ expect(fallback_handler.master).to eq(false)end- context "when the redis master comes back up" do- before do- MessageBus.keepalive_interval = 60-- expect_master_info- .raises(Redis::ConnectionError.new)+ it "should return the slave config when master is still loading data" do+ Redis::Client.any_instance+ .expects(:call)+ .with([:info, :persistence])+ .returns("+ someconfig:haha\r+ #{DiscourseRedis::FallbackHandler::MASTER_LOADING_STATUS}+ ")- DiscourseRedis::Connector.new(config).resolve+ config = connector.resolve- expect_master_info- .returns(info_response(['loading', '0'], ['role', 'master']))-- expect_fallback- end+ expect(config[:host]).to eq(slave_host)+ expect(config[:port]).to eq(slave_port)+ end- it "sets the message bus keepalive interval to its original value" do- MessageBus.expects(:keepalive_interval=).with(60)- end+ it "should raise the right error" do+ error = RuntimeError.new('test')- it "calls clear_readonly! and request_refresh! on Discourse" do- Discourse.expects(:clear_readonly!)- Discourse.expects(:request_refresh!)+ 2.times do+ expect { connector.resolve(BrokenRedis.new(error)) }+ .to raise_error(error)endendend- context "when message bus and main are on different hosts" do- use_fake_threads-+ describe DiscourseRedis::FallbackHandler dobefore do- # Since config is based on GlobalSetting, we need to fetch it before stubbing- conf = config-- GlobalSetting.stubs(:redis_config).returns(conf)-- message_bus_config = conf.dup- message_bus_config[:port] = message_bus_config[:port].to_i + 1- message_bus_config[:slave_port] = message_bus_config[:slave_port].to_i + 1-- GlobalSetting.stubs(:message_bus_redis_config).returns(message_bus_config)-- Concurrency::ThreadedExecution.stubs(:new).returns(execution)+ @original_keepalive_interval = MessageBus.keepalive_intervalend- let(:message_bus_master_config) {- GlobalSetting.message_bus_redis_config- }-- context "when the message bus master goes down" do- before do- expect_master_info(message_bus_master_config)- .raises(Redis::ConnectionError.new)- end-- it "sets the message bus keepalive interval to 0" do- MessageBus.expects(:keepalive_interval=).with(0)-- DiscourseRedis::Connector.new(message_bus_master_config).resolve-- execution.stop_other_tasks- end-- it "does not call clear_readonly! or request_refresh! on Discourse" do- Discourse.expects(:clear_readonly!).never- Discourse.expects(:request_refresh!).never-- DiscourseRedis::Connector.new(message_bus_master_config).resolve-- execution.stop_other_tasks- end+ after do+ fallback_handler.master = true+ MessageBus.keepalive_interval = @original_keepalive_intervalend- context "when the message bus master comes back up" do- before do- MessageBus.keepalive_interval = 60+ describe '#initiate_fallback_to_master' do+ it 'should return the right value if the master server is still down' do+ fallback_handler.master = false+ Redis::Client.any_instance.expects(:call).with([:info]).returns("Some other stuff")- expect_master_info(message_bus_master_config)- .raises(Redis::ConnectionError.new)-- DiscourseRedis::Connector.new(message_bus_master_config).resolve-- expect_master_info(message_bus_master_config)- .returns(info_response(['loading', '0'], ['role', 'master']))-- expect_fallback(DiscourseRedis.slave_config(message_bus_master_config))- end-- it "sets the message bus keepalive interval to its original value" do- MessageBus.expects(:keepalive_interval=).with(60)- end- end-- context "when the main master goes down" do- before do- expect_master_info- .raises(Redis::ConnectionError.new)+ expect(fallback_handler.initiate_fallback_to_master).to eq(false)+ expect(MessageBus.keepalive_interval).to eq(0)end- it "does not change the message bus keepalive interval" do- MessageBus.expects(:keepalive_interval=).never+ it 'should fallback to the master server once it is up' do+ fallback_handler.master = false+ master_conn = mock('master')+ slave_conn = mock('slave')- DiscourseRedis::Connector.new(config).resolve+ Redis::Client.expects(:new)+ .with(DiscourseRedis.config)+ .returns(master_conn)- execution.stop_other_tasks- end- end-- context "when the main master comes back up" do- before do- expect_master_info- .raises(Redis::ConnectionError.new)-- DiscourseRedis::Connector.new(config).resolve+ Redis::Client.expects(:new)+ .with(DiscourseRedis.slave_config)+ .returns(slave_conn)- expect_master_info- .returns(info_response(['loading', '0'], ['role', 'master']))+ master_conn.expects(:call)+ .with([:info])+ .returns("+ #{DiscourseRedis::FallbackHandler::MASTER_ROLE_STATUS}\r\n+ #{DiscourseRedis::FallbackHandler::MASTER_LOADED_STATUS}+ ")- expect_fallback- end+ DiscourseRedis::FallbackHandler::CONNECTION_TYPES.each do |connection_type|+ slave_conn.expects(:call).with(+ [:client, [:kill, 'type', connection_type]]+ )+ end- it "does not change the message bus keepalive interval" do- MessageBus.expects(:keepalive_interval=).never- end+ master_conn.expects(:disconnect)+ slave_conn.expects(:disconnect)- it "calls clear_readonly! and request_refresh! on Discourse" do- Discourse.expects(:clear_readonly!)- Discourse.expects(:request_refresh!)+ expect(fallback_handler.initiate_fallback_to_master).to eq(true)+ expect(fallback_handler.master).to eq(true)+ expect(Discourse.recently_readonly?).to eq(false)+ expect(MessageBus.keepalive_interval).to eq(-1)endendenddiff --git a/spec/support/concurrency.rb b/spec/support/concurrency.rbindex 062bcab56b5..5d3cd0c87c4 100644--- a/spec/support/concurrency.rb+++ b/spec/support/concurrency.rb@@ -30,7 +30,7 @@ module Concurrencyenddef choose_with_weights(*options)- choose(*options.map(&:first))+ choose(options.map(&:first))enddef dead_end@@ -147,11 +147,10 @@ module Concurrencydef initialize(path)@path = path@tasks = []- @time = 0enddef yield- sleep(0)+ Fiber.yieldenddef choose(*options)@@ -162,86 +161,30 @@ module Concurrency@path.choose_with_weights(*options)end- def stop_other_tasks- @tasts = @tasks.select! { |task| task[:fiber] == Fiber.current }- end-- def sleep(length)- Fiber.yield(@time + length)- end-- def start_root(&blk)- descriptor = {- fiber: Fiber.new(&blk),- run_at: 0- }-- @tasks << descriptor- end-def spawn(&blk)- descriptor = {- fiber: Fiber.new(&blk),- run_at: @time- }-- @tasks << descriptor-- self.yield+ @tasks << Fiber.new(&blk)end- def run(sleep_order: false)+ def rununtil @tasks.empty?- descriptor =- if sleep_order- @tasks.sort_by! { |x| x[:run_at] }- run_at = @tasks.first[:run_at]- @path.choose(*@tasks.take_while { |x| x[:run_at] == run_at })- else- @path.choose(*@tasks)- end-- @time = [@time, descriptor[:run_at]].max- fiber = descriptor[:fiber]-- begin- run_at = fiber.resume- rescue Exception- end-- if fiber.alive?- descriptor[:run_at] = run_at- else- @tasks.delete(descriptor)+ task = @path.choose(*@tasks)+ task.resume+ unless task.alive?+ @tasks.delete(task)endendend-- def wait_done- until @tasks.size == 1- self.sleep(1e9)- end- end-- def new_mutex- Mutex.new(self)- endend- def run_with_path(path, sleep_order: false)+ def run_with_path(path)execution = Execution.new(path)- result = {}- execution.start_root {- result[:value] = @blk.call(execution)- }- execution.run(sleep_order: sleep_order)+ result = @blk.call(execution)+ execution.runresultend- def run(sleep_order: false, **opts)- Logic.run(**opts) do |path|- run_with_path(path, sleep_order: sleep_order)- end+ def run(**opts)+ Logic.run(**opts, &method(:run_with_path))endend@@ -307,45 +250,4 @@ module Concurrencyresultendend-- class Mutex- def initialize(execution)- @execution = execution- @locked_by = nil- end-- def lock- @execution.yield-- fiber = Fiber.current- while true- if @locked_by.nil?- @locked_by = fiber- return- elsif @locked_by == fiber- raise ThreadError, "deadlock; recursive locking"- else- @execution.yield- end- end- end-- def unlock- @execution.yield-- if @locked_by != Fiber.current- raise ThreadError, "Attempt to unlock a mutex which is locked by another thread"- end- @locked_by = nil- end-- def synchronize- lock- begin- yield- ensure- unlock- end- end- endend