Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUBY-3620 - Sessions should ignore "cluster mismatched" validation and execute commands on other clusters #2918

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/mongo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,11 @@ def do_close
# @api private
def get_session!(options = {})
if options[:session]
return options[:session].validate!(self)
begin
return options[:session].validate!(self)
rescue Error::SessionClusterMismatched
nil # fall through to creating a new session
end
end

cluster.validate_session_support!(timeout: timeout_sec)
Expand Down
36 changes: 36 additions & 0 deletions lib/mongo/error/session_cluster_mismatched.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2017-2025 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
class Error

# Error indicating that the session was retrieved from a client with a
# different cluster than that of the client through which it is currently being used.
class SessionClusterMismatched < InvalidSession

# Create the new exception.
#
# @example Create the new exception.
# SessionClusterMismatched.new
def initialize
super('The configured cluster of the client used to create this session does not match that ' +
'of the client owning this operation. Please only use this session for operations ' +
'through its parent client.')
end
end
end
end
12 changes: 1 addition & 11 deletions lib/mongo/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,6 @@ def session_id
# @api private
attr_accessor :recovery_token

# Error message indicating that the session was retrieved from a client with a different cluster than that of the
# client through which it is currently being used.
#
# @since 2.5.0
MISMATCHED_CLUSTER_ERROR_MSG = 'The configuration of the client used to create this session does not match that ' +
'of the client owning this operation. Please only use this session for operations through its parent ' +
'client.'.freeze

# Error message describing that the session cannot be used because it has already been ended.
#
# @since 2.5.0
Expand Down Expand Up @@ -1255,9 +1247,7 @@ def check_if_ended!
end

def check_matching_cluster!(client)
if @client.cluster != client.cluster
raise Mongo::Error::InvalidSession.new(MISMATCHED_CLUSTER_ERROR_MSG)
end
raise Mongo::Error::SessionClusterMismatched.new if @client.cluster != client.cluster
end

def check_transactions_supported!
Expand Down
Loading