-
Type:
Task
-
Resolution: Gone away
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
Ruby Drivers
-
None
-
None
-
None
-
None
-
None
-
None
We use multiple MongoDB clusters for our application. (We are intentionally not using a sharded cluster, as we want greater control/isolation.)
Current Behavior
When we perform a session on one cluster, it becomes impossible to execute
queries on any other cluster. Such queries fail with "The configuration of the client used to create this session does not match that of the client owning this operation..."
Desired Behavior
We would like to request the following changes:
- When performing a session/transaction on Cluster A, queries on Cluster B should still execute outside that session/transaction.
- Moreover, it should be possible to execute a session/transaction specific to Cluster B within the Ruby block for the session on Cluster A.
- Within a session/transaction Ruby block, we should be able to "escape" out of that Session, i.e. explicitly execute commands outside of the transaction (see Workaround patch below.)
Workaround
We've made the following monkey patch which allows us to "escape" out of the session for the purpose of executing commands outside a transaction. It relies on the coincidental behavior that Ruby threads escape out of the session. However, it would be be much nicer if we could do this without spawning a thread.
module Mongo class Session def self.escape(&) # Note: #value joins the thread Thread.new(&).value end end end
Example Use Case
We have Customer object and Audit object. Audit is a changelog that is created each time a Customer is saved. It therefore looks something like:
class Customer include Mongoid::Document store_in client: :default after_save do |customer| Audit.new(customer.changes).save! end end class Audit include Mongoid::Document store_in client: :other_database end
Note that Audits are stored in a separate cluster. Executing a session/transaction with the Customer model will fail when it's callback attempts to save the corresponding Audit model.