-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-3552: CSOT: Transactions #1745
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* Copyright 2010-present 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. | ||
*/ | ||
|
||
using System; | ||
using MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver | ||
{ | ||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal sealed class AbortTransactionOptions | ||
{ | ||
public AbortTransactionOptions() | ||
{} | ||
|
||
public AbortTransactionOptions(TimeSpan? timeout) | ||
{ | ||
Timeout = Ensure.IsNullOrValidTimeout(timeout, nameof(timeout)); | ||
} | ||
|
||
public TimeSpan? Timeout { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright 2010-present 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. | ||
*/ | ||
|
||
using System; | ||
using MongoDB.Driver.Core.Misc; | ||
|
||
namespace MongoDB.Driver | ||
{ | ||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal sealed class CommitTransactionOptions | ||
{ | ||
public CommitTransactionOptions() | ||
{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same no need for a new line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose we do not need the parameter less constructor at all here. Thank you for bringing attention to this. |
||
|
||
public CommitTransactionOptions(TimeSpan? timeout) | ||
{ | ||
Timeout = Ensure.IsNullOrValidTimeout(timeout, nameof(timeout)); | ||
} | ||
|
||
public TimeSpan? Timeout { get; } | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* Copyright 2018-present MongoDB Inc. | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -141,12 +141,15 @@ public bool IsInTransaction | |
|
||
// public methods | ||
/// <inheritdoc /> | ||
public void AbortTransaction(CancellationToken cancellationToken = default(CancellationToken)) | ||
public void AbortTransaction(CancellationToken cancellationToken = default) | ||
=> AbortTransaction(null, cancellationToken); | ||
|
||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal void AbortTransaction(AbortTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction)); | ||
|
||
// TODO: CSOT implement proper way to obtain the operationContext | ||
var operationContext = new OperationContext(null, cancellationToken); | ||
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); | ||
try | ||
{ | ||
if (_currentTransaction.IsEmpty) | ||
|
@@ -192,12 +195,15 @@ public bool IsInTransaction | |
} | ||
|
||
/// <inheritdoc /> | ||
public async Task AbortTransactionAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
public Task AbortTransactionAsync(CancellationToken cancellationToken = default) | ||
=> AbortTransactionAsync(null, cancellationToken); | ||
|
||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal async Task AbortTransactionAsync(AbortTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction)); | ||
|
||
// TODO: CSOT implement proper way to obtain the operationContext | ||
var operationContext = new OperationContext(null, cancellationToken); | ||
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); | ||
try | ||
{ | ||
if (_currentTransaction.IsEmpty) | ||
|
@@ -292,12 +298,15 @@ public long AdvanceTransactionNumber() | |
} | ||
|
||
/// <inheritdoc /> | ||
public void CommitTransaction(CancellationToken cancellationToken = default(CancellationToken)) | ||
public void CommitTransaction(CancellationToken cancellationToken = default) | ||
=> CommitTransaction(null, cancellationToken); | ||
|
||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal void CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction)); | ||
|
||
// TODO: CSOT implement proper way to obtain the operationContext | ||
var operationContext = new OperationContext(null, cancellationToken); | ||
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); | ||
try | ||
{ | ||
_isCommitTransactionInProgress = true; | ||
|
@@ -329,12 +338,15 @@ public long AdvanceTransactionNumber() | |
} | ||
|
||
/// <inheritdoc /> | ||
public async Task CommitTransactionAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
public Task CommitTransactionAsync(CancellationToken cancellationToken = default) | ||
=> CommitTransactionAsync(null, cancellationToken); | ||
|
||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal async Task CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken = default(CancellationToken)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent default parameter syntax for cancellationToken for both methods. Standardize on either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
{ | ||
EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction)); | ||
|
||
// TODO: CSOT implement proper way to obtain the operationContext | ||
var operationContext = new OperationContext(null, cancellationToken); | ||
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), cancellationToken); | ||
try | ||
{ | ||
_isCommitTransactionInProgress = true; | ||
|
@@ -563,6 +575,9 @@ private async Task<TResult> ExecuteEndTransactionOnPrimaryAsync<TResult>(Operati | |
} | ||
} | ||
|
||
private TimeSpan? GetTimeout(TimeSpan? timeout) | ||
=> timeout ?? _options.DefaultTransactionOptions?.Timeout; | ||
|
||
private TransactionOptions GetEffectiveTransactionOptions(TransactionOptions transactionOptions) | ||
{ | ||
var readConcern = transactionOptions?.ReadConcern ?? _options.DefaultTransactionOptions?.ReadConcern ?? ReadConcern.Default; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* Copyright 2010-present 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. | ||
*/ | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MongoDB.Driver.Core.Bindings | ||
{ | ||
// TODO: CSOT: Make it public when CSOT will be ready for GA | ||
internal static class ICoreSessionExtensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend creating a new These extension methods are useful to make it "look" (to us) like the new methods are already part of These extension methods should cast There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All classes that implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thank you! |
||
{ | ||
// TODO: CSOT: Merge this extension methods in ICoreSession interface on major release | ||
/// <summary> | ||
/// Aborts the transaction. | ||
/// </summary> | ||
/// <param name="session">The session.</param> | ||
/// <param name="options">Abort transaction options.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
public static void AbortTransaction(this ICoreSession session, AbortTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout) | ||
{ | ||
session.AbortTransaction(cancellationToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forwarding the new method to the old method doesn't seem necessary but it also doesn't do any real harm other than adding more lines of code that probably aren't needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've decided to have this forwarding, just in case there are any external implementation of ICoreSession, this forwarding might reduce risk of breaking client's code. |
||
return; | ||
} | ||
|
||
if (session is CoreSession coreSession) | ||
{ | ||
coreSession.AbortTransaction(options, cancellationToken); | ||
return; | ||
} | ||
|
||
if (session is WrappingCoreSession wrappingCoreSession) | ||
{ | ||
wrappingCoreSession.AbortTransaction(options, cancellationToken); | ||
return; | ||
} | ||
|
||
throw new InvalidOperationException("Cannot apply options on non CoreSession."); | ||
} | ||
|
||
/// <summary> | ||
/// Aborts the transaction. | ||
/// </summary> | ||
/// <param name="session">The session.</param> | ||
/// <param name="options">Abort transaction options.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
public static Task AbortTransactionAsync(this ICoreSession session, AbortTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout) | ||
{ | ||
return session.AbortTransactionAsync(cancellationToken); | ||
} | ||
|
||
if (session is CoreSession coreSession) | ||
{ | ||
return coreSession.AbortTransactionAsync(options, cancellationToken); | ||
} | ||
|
||
if (session is WrappingCoreSession wrappingCoreSession) | ||
{ | ||
return wrappingCoreSession.AbortTransactionAsync(options, cancellationToken); | ||
} | ||
|
||
throw new InvalidOperationException("Cannot apply options on non CoreSession."); | ||
} | ||
|
||
/// <summary> | ||
/// Commits the transaction. | ||
/// </summary> | ||
/// <param name="session">The session.</param> | ||
/// <param name="options">Commit transaction options.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
public static void CommitTransaction(this ICoreSession session, CommitTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout) | ||
{ | ||
session.CommitTransaction(cancellationToken); | ||
return; | ||
} | ||
|
||
if (session is CoreSession coreSession) | ||
{ | ||
coreSession.CommitTransaction(options, cancellationToken); | ||
return; | ||
} | ||
|
||
if (session is WrappingCoreSession wrappingCoreSession) | ||
{ | ||
wrappingCoreSession.CommitTransaction(options, cancellationToken); | ||
return; | ||
} | ||
|
||
throw new InvalidOperationException("Cannot apply options on non CoreSession."); | ||
} | ||
|
||
/// <summary> | ||
/// Commits the transaction. | ||
/// </summary> | ||
/// <param name="session">The session.</param> | ||
/// <param name="options">Commit transaction options.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
public static Task CommitTransactionAsync(this ICoreSession session, CommitTransactionOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
if (options == null || session.Options.DefaultTransactionOptions?.Timeout == options.Timeout) | ||
{ | ||
return session.CommitTransactionAsync(cancellationToken); | ||
} | ||
|
||
if (session is CoreSession coreSession) | ||
{ | ||
return coreSession.CommitTransactionAsync(options, cancellationToken); | ||
} | ||
|
||
if (session is WrappingCoreSession wrappingCoreSession) | ||
{ | ||
return wrappingCoreSession.CommitTransactionAsync(options, cancellationToken); | ||
} | ||
|
||
throw new InvalidOperationException("Cannot apply options on non CoreSession."); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for a new line for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we do not need the parameter less constructor at all here. Thank you for bringing attention to this.