Skip to content

Commit af44109

Browse files
author
Oleksandr Poliakov
committed
pr
1 parent 2820bb6 commit af44109

39 files changed

+493
-303
lines changed

src/MongoDB.Driver/Core/Misc/Ensure.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public static TimeSpan IsValidTimeout(TimeSpan value, string paramName)
465465
return value;
466466
}
467467

468-
throw new ArgumentException($"Invalid timeout: {value}.", paramName);
468+
throw new ArgumentOutOfRangeException($"Invalid timeout: {value}.", paramName);
469469
}
470470

471471
/// <summary>

src/MongoDB.Driver/Core/Operations/AggregateOperation.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public IAsyncCursor<TResult> Execute(OperationContext operationContext, Retryabl
288288

289289
using (EventContext.BeginOperation())
290290
{
291-
var operation = CreateOperation(context);
291+
var operation = CreateOperation(operationContext, context);
292292
var result = operation.Execute(operationContext, context);
293293

294294
context.ChannelSource.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
@@ -317,7 +317,7 @@ public async Task<IAsyncCursor<TResult>> ExecuteAsync(OperationContext operation
317317

318318
using (EventContext.BeginOperation())
319319
{
320-
var operation = CreateOperation(context);
320+
var operation = CreateOperation(operationContext, context);
321321
var result = await operation.ExecuteAsync(operationContext, context).ConfigureAwait(false);
322322

323323
context.ChannelSource.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
@@ -326,15 +326,15 @@ public async Task<IAsyncCursor<TResult>> ExecuteAsync(OperationContext operation
326326
}
327327
}
328328

329-
internal BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
329+
internal BsonDocument CreateCommand(OperationContext operationContext, ICoreSession session, ConnectionDescription connectionDescription)
330330
{
331331
var readConcern = ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern);
332332
var command = new BsonDocument
333333
{
334334
{ "aggregate", _collectionNamespace == null ? (BsonValue)1 : _collectionNamespace.CollectionName },
335335
{ "pipeline", new BsonArray(_pipeline) },
336336
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
337-
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
337+
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue && !operationContext.IsRootContextTimeoutConfigured() },
338338
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
339339
{ "hint", _hint, _hint != null },
340340
{ "let", _let, _let != null },
@@ -354,10 +354,10 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
354354

355355
private IDisposable BeginOperation() => EventContext.BeginOperation(null, "aggregate");
356356

357-
private ReadCommandOperation<AggregateResult> CreateOperation(RetryableReadContext context)
357+
private ReadCommandOperation<AggregateResult> CreateOperation(OperationContext operationContext, RetryableReadContext context)
358358
{
359359
var databaseNamespace = _collectionNamespace == null ? _databaseNamespace : _collectionNamespace.DatabaseNamespace;
360-
var command = CreateCommand(context.Channel.ConnectionDescription, context.Binding.Session);
360+
var command = CreateCommand(operationContext, context.Binding.Session, context.Channel.ConnectionDescription);
361361
var serializer = new AggregateResultDeserializer(_resultSerializer);
362362
return new ReadCommandOperation<AggregateResult>(databaseNamespace, command, serializer, MessageEncoderSettings)
363363
{

src/MongoDB.Driver/Core/Operations/AggregateToCollectionOperation.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -157,7 +157,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
157157
using (var channel = channelSource.GetChannel(operationContext))
158158
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
159159
{
160-
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription, mayUseSecondary.EffectiveReadPreference);
160+
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription, mayUseSecondary.EffectiveReadPreference);
161161
return operation.Execute(operationContext, channelBinding);
162162
}
163163
}
@@ -172,12 +172,12 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
172172
using (var channel = await channelSource.GetChannelAsync(operationContext).ConfigureAwait(false))
173173
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
174174
{
175-
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription, mayUseSecondary.EffectiveReadPreference);
175+
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription, mayUseSecondary.EffectiveReadPreference);
176176
return await operation.ExecuteAsync(operationContext, channelBinding).ConfigureAwait(false);
177177
}
178178
}
179179

180-
public BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
180+
public BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
181181
{
182182
var readConcern = _readConcern != null
183183
? ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern)
@@ -189,7 +189,7 @@ public BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescript
189189
{ "pipeline", new BsonArray(_pipeline) },
190190
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
191191
{ "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue },
192-
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
192+
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue && !operationContext.IsRootContextTimeoutConfigured() },
193193
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
194194
{ "readConcern", readConcern, readConcern != null },
195195
{ "writeConcern", writeConcern, writeConcern != null },
@@ -202,9 +202,9 @@ public BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescript
202202

203203
private IDisposable BeginOperation() => EventContext.BeginOperation("aggregate");
204204

205-
private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session, ConnectionDescription connectionDescription, ReadPreference effectiveReadPreference)
205+
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription, ReadPreference effectiveReadPreference)
206206
{
207-
var command = CreateCommand(session, connectionDescription);
207+
var command = CreateCommand(operationContext, session, connectionDescription);
208208
var operation = new WriteCommandOperation<BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, MessageEncoderSettings);
209209
if (effectiveReadPreference != null)
210210
{

src/MongoDB.Driver/Core/Operations/CountOperation.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -109,7 +109,7 @@ public long? Skip
109109
set { _skip = value; }
110110
}
111111

112-
public BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
112+
public BsonDocument CreateCommand(OperationContext operationContext, ICoreSession session, ConnectionDescription connectionDescription)
113113
{
114114
var readConcern = ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern);
115115
return new BsonDocument
@@ -119,7 +119,7 @@ public BsonDocument CreateCommand(ConnectionDescription connectionDescription, I
119119
{ "limit", () => _limit.Value, _limit.HasValue },
120120
{ "skip", () => _skip.Value, _skip.HasValue },
121121
{ "hint", _hint, _hint != null },
122-
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
122+
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue && !operationContext.IsRootContextTimeoutConfigured() },
123123
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
124124
{ "comment", _comment, _comment != null },
125125
{ "readConcern", readConcern, readConcern != null }
@@ -139,7 +139,7 @@ public long Execute(OperationContext operationContext, IReadBinding binding)
139139

140140
public long Execute(OperationContext operationContext, RetryableReadContext context)
141141
{
142-
var operation = CreateOperation(context);
142+
var operation = CreateOperation(operationContext, context);
143143
var document = operation.Execute(operationContext, context);
144144
return document["n"].ToInt64();
145145
}
@@ -157,16 +157,16 @@ public async Task<long> ExecuteAsync(OperationContext operationContext, IReadBin
157157

158158
public async Task<long> ExecuteAsync(OperationContext operationContext, RetryableReadContext context)
159159
{
160-
var operation = CreateOperation(context);
160+
var operation = CreateOperation(operationContext, context);
161161
var document = await operation.ExecuteAsync(operationContext, context).ConfigureAwait(false);
162162
return document["n"].ToInt64();
163163
}
164164

165165
private IDisposable BeginOperation() => EventContext.BeginOperation("count");
166166

167-
private ReadCommandOperation<BsonDocument> CreateOperation(RetryableReadContext context)
167+
private ReadCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, RetryableReadContext context)
168168
{
169-
var command = CreateCommand(context.Channel.ConnectionDescription, context.Binding.Session);
169+
var command = CreateCommand(operationContext, context.Binding.Session, context.Channel.ConnectionDescription);
170170
return new ReadCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings)
171171
{
172172
RetryRequested = _retryRequested // might be overridden by retryable read context

src/MongoDB.Driver/Core/Operations/CreateIndexesOperation.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -93,7 +93,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
9393
using (var channel = channelSource.GetChannel(operationContext))
9494
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
9595
{
96-
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
96+
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription);
9797
return operation.Execute(operationContext, channelBinding);
9898
}
9999
}
@@ -105,12 +105,12 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
105105
using (var channel = await channelSource.GetChannelAsync(operationContext).ConfigureAwait(false))
106106
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
107107
{
108-
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
108+
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription);
109109
return await operation.ExecuteAsync(operationContext, channelBinding).ConfigureAwait(false);
110110
}
111111
}
112112

113-
internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
113+
internal BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
114114
{
115115
var maxWireVersion = connectionDescription.MaxWireVersion;
116116
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
@@ -123,7 +123,7 @@ internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescri
123123
{
124124
{ "createIndexes", _collectionNamespace.CollectionName },
125125
{ "indexes", new BsonArray(_requests.Select(request => request.CreateIndexDocument())) },
126-
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
126+
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue && !operationContext.IsRootContextTimeoutConfigured() },
127127
{ "writeConcern", writeConcern, writeConcern != null },
128128
{ "comment", _comment, _comment != null },
129129
{ "commitQuorum", () => _commitQuorum.ToBsonValue(), _commitQuorum != null }
@@ -132,10 +132,10 @@ internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescri
132132

133133
private IDisposable BeginOperation() => EventContext.BeginOperation(null, "createIndexes");
134134

135-
private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session, ConnectionDescription connectionDescription)
135+
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
136136
{
137137
var databaseNamespace = _collectionNamespace.DatabaseNamespace;
138-
var command = CreateCommand(session, connectionDescription);
138+
var command = CreateCommand(operationContext, session, connectionDescription);
139139
var resultSerializer = BsonDocumentSerializer.Instance;
140140
return new WriteCommandOperation<BsonDocument>(databaseNamespace, command, resultSerializer, _messageEncoderSettings);
141141
}

src/MongoDB.Driver/Core/Operations/DistinctOperation.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -111,7 +111,7 @@ public IAsyncCursor<TValue> Execute(OperationContext operationContext, IReadBind
111111
using (BeginOperation())
112112
using (var context = RetryableReadContext.Create(operationContext, binding, _retryRequested))
113113
{
114-
var operation = CreateOperation(context);
114+
var operation = CreateOperation(operationContext, context);
115115
var result = operation.Execute(operationContext, context);
116116

117117
binding.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
@@ -127,7 +127,7 @@ public async Task<IAsyncCursor<TValue>> ExecuteAsync(OperationContext operationC
127127
using (BeginOperation())
128128
using (var context = await RetryableReadContext.CreateAsync(operationContext, binding, _retryRequested).ConfigureAwait(false))
129129
{
130-
var operation = CreateOperation(context);
130+
var operation = CreateOperation(operationContext, context);
131131
var result = await operation.ExecuteAsync(operationContext, context).ConfigureAwait(false);
132132

133133
binding.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
@@ -136,15 +136,15 @@ public async Task<IAsyncCursor<TValue>> ExecuteAsync(OperationContext operationC
136136
}
137137
}
138138

139-
public BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
139+
public BsonDocument CreateCommand(OperationContext operationContext, ICoreSession session, ConnectionDescription connectionDescription)
140140
{
141141
var readConcern = ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern);
142142
return new BsonDocument
143143
{
144144
{ "distinct", _collectionNamespace.CollectionName },
145145
{ "key", _fieldName },
146146
{ "query", _filter, _filter != null },
147-
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
147+
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue && !operationContext.IsRootContextTimeoutConfigured() },
148148
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
149149
{ "comment", _comment, _comment != null },
150150
{ "readConcern", readConcern, readConcern != null }
@@ -153,9 +153,9 @@ public BsonDocument CreateCommand(ConnectionDescription connectionDescription, I
153153

154154
private IDisposable BeginOperation() => EventContext.BeginOperation("distinct");
155155

156-
private ReadCommandOperation<DistinctResult> CreateOperation(RetryableReadContext context)
156+
private ReadCommandOperation<DistinctResult> CreateOperation(OperationContext operationContext, RetryableReadContext context)
157157
{
158-
var command = CreateCommand(context.Channel.ConnectionDescription, context.Binding.Session);
158+
var command = CreateCommand(operationContext, context.Binding.Session, context.Channel.ConnectionDescription);
159159
var serializer = new DistinctResultDeserializer(_valueSerializer);
160160

161161
return new ReadCommandOperation<DistinctResult>(_collectionNamespace.DatabaseNamespace, command, serializer, _messageEncoderSettings)

0 commit comments

Comments
 (0)