1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe 'Transactions examples' do
require_wired_tiger
require_transaction_support
let(:client) do
authorized_client.with(read_concern: {level: :majority}, write: {w: :majority})
end
before do
if SpecConfig.instance.client_debug?
Mongo::Logger.logger.level = 0
end
end
let(:hr) do
client.use(:hr).database
end
let(:reporting) do
client.use(:reporting).database
end
before(:each) do
hr[:employees].insert_one(employee: 3, status: 'Active')
# Sanity check since this test likes to fail
employee = hr[:employees].find({ employee: 3 }, limit: 1).first
expect(employee).to_not be_nil
reporting[:events].insert_one(employee: 3, status: { new: 'Active', old: nil})
end
after(:each) do
hr.drop
reporting.drop
# Work around https://jira.mongodb.org/browse/SERVER-53015
::Utils.mongos_each_direct_client do |client|
client.database.command(flushRouterConfig: 1)
end
end
context 'individual examples' do
let(:session) do
client.start_session
end
# Start Transactions Intro Example 1
def update_employee_info(session)
employees_coll = session.client.use(:hr)[:employees]
events_coll = session.client.use(:reporting)[:events]
session.start_transaction(read_concern: { level: :snapshot },
write_concern: { w: :majority })
employees_coll.update_one({ employee: 3 }, { '$set' => { status: 'Inactive'} },
session: session)
events_coll.insert_one({ employee: 3, status: { new: 'Inactive', old: 'Active' } },
session: session)
begin
session.commit_transaction
puts 'Transaction committed.'
rescue Mongo::Error => e
if e.label?('UnknownTransactionCommitResult')
puts "UnknownTransactionCommitResult, retrying commit operation..."
retry
else
puts 'Error during commit ...'
raise
end
end
end
# End Transactions Intro Example 1
context 'Transactions Intro Example 1' do
let(:run_transaction) do
update_employee_info(session)
end
it 'makes the changes to the database' do
run_transaction
employee = hr[:employees].find({ employee: 3 }, limit: 1).first
expect(employee).to_not be_nil
expect(employee['status']).to eq('Inactive')
end
end
context 'Transactions Retry Example 1' do
# Start Transactions Retry Example 1
def run_transaction_with_retry(session)
begin
yield session # performs transaction
rescue Mongo::Error => e
puts 'Transaction aborted. Caught exception during transaction.'
raise unless e.label?('TransientTransactionError')
puts "TransientTransactionError, retrying transaction ..."
retry
end
end
# End Transactions Retry Example 1
let(:run_transaction) do
run_transaction_with_retry(session) { |s| update_employee_info(s) }
end
it 'makes the changes to the database' do
run_transaction
employee = hr[:employees].find({ employee: 3 }, limit: 1).first
expect(employee).to_not be_nil
expect(employee['status']).to eq('Inactive')
end
end
context 'Transactions Retry Example 2' do
# Start Transactions Retry Example 2
def commit_with_retry(session)
begin
session.commit_transaction
puts 'Transaction committed.'
rescue Mongo::Error=> e
if e.label?('UnknownTransactionCommitResult')
puts "UnknownTransactionCommitResult, retrying commit operation..."
retry
else
puts 'Error during commit ...'
raise
end
end
end
# End Transactions Retry Example 2
let(:run_transaction) do
session.start_transaction
hr[:employees].insert_one({ employee: 4, status: 'Active' }, session: session)
reporting[:events].insert_one({ employee: 4, status: { new: 'Active', old: nil } },
session: session)
commit_with_retry(session)
end
it 'makes the changes to the database' do
run_transaction
employee = hr[:employees].find({ employee: 4 }, limit: 1).first
expect(employee).to_not be_nil
expect(employee['status']).to eq('Active')
end
end
end
context 'Transactions Retry Example 3 (combined example)' do
let(:run_transaction) do
# Start Transactions Retry Example 3
def run_transaction_with_retry(session)
begin
yield session # performs transaction
rescue Mongo::Error => e
puts 'Transaction aborted. Caught exception during transaction.'
raise unless e.label?('TransientTransactionError')
puts "TransientTransactionError, retrying transaction ..."
retry
end
end
def commit_with_retry(session)
begin
session.commit_transaction
puts 'Transaction committed.'
rescue Mongo::Error => e
if e.label?('UnknownTransactionCommitResult')
puts "UnknownTransactionCommitResult, retrying commit operation ..."
retry
else
puts 'Error during commit ...'
raise
end
end
end
# updates two collections in a transaction
def update_employee_info(session)
employees_coll = session.client.use(:hr)[:employees]
events_coll = session.client.use(:reporting)[:events]
session.start_transaction(read_concern: { level: :snapshot },
write_concern: { w: :majority },
read: {mode: :primary})
employees_coll.update_one({ employee: 3 }, { '$set' => { status: 'Inactive'} },
session: session)
events_coll.insert_one({ employee: 3, status: { new: 'Inactive', old: 'Active' } },
session: session)
commit_with_retry(session)
end
session = client.start_session
begin
run_transaction_with_retry(session) do
update_employee_info(session)
end
rescue StandardError => e
# Do something with error
raise
end
# End Transactions Retry Example 3
end
it 'makes the changes to the database' do
run_transaction
employee = hr[:employees].find({ employee: 3 }, limit: 1).first
expect(employee).to_not be_nil
expect(employee['status']).to eq('Inactive')
end
end
end
|