forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadPreference.cs
344 lines (311 loc) · 12.3 KB
/
ReadPreference.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Copyright 2013-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.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
using MongoDB.Shared;
namespace MongoDB.Driver
{
/// <summary>
/// Represents a read preference.
/// </summary>
public sealed class ReadPreference : IEquatable<ReadPreference>
{
#region static
// static fields
private static readonly TagSet[] __emptyTagSetsArray = new TagSet[0];
private static readonly ReadPreference __nearest = new ReadPreference(ReadPreferenceMode.Nearest);
private static readonly ReadPreference __primary = new ReadPreference(ReadPreferenceMode.Primary);
private static readonly ReadPreference __primaryPreferred = new ReadPreference(ReadPreferenceMode.PrimaryPreferred);
private static readonly ReadPreference __secondary = new ReadPreference(ReadPreferenceMode.Secondary);
private static readonly ReadPreference __secondaryPreferred = new ReadPreference(ReadPreferenceMode.SecondaryPreferred);
// static properties
/// <summary>
/// Gets an instance of ReadPreference that represents a Nearest read preference.
/// </summary>
/// <value>
/// An instance of ReadPreference that represents a Nearest read preference.
/// </value>
public static ReadPreference Nearest
{
get { return __nearest; }
}
/// <summary>
/// Gets an instance of ReadPreference that represents a Primary read preference.
/// </summary>
/// <value>
/// An instance of ReadPreference that represents a Primary read preference.
/// </value>
public static ReadPreference Primary
{
get { return __primary; }
}
/// <summary>
/// Gets an instance of ReadPreference that represents a PrimaryPreferred read preference.
/// </summary>
/// <value>
/// An instance of ReadPreference that represents a PrimaryPreferred read preference.
/// </value>
public static ReadPreference PrimaryPreferred
{
get { return __primaryPreferred; }
}
/// <summary>
/// Gets an instance of ReadPreference that represents a Secondary read preference.
/// </summary>
/// <value>
/// An instance of ReadPreference that represents a Secondary read preference.
/// </value>
public static ReadPreference Secondary
{
get { return __secondary; }
}
/// <summary>
/// Gets an instance of ReadPreference that represents a SecondaryPreferred read preference.
/// </summary>
/// <value>
/// An instance of ReadPreference that represents a SecondaryPreferred read preference.
/// </value>
public static ReadPreference SecondaryPreferred
{
get { return __secondaryPreferred; }
}
// public static methods
/// <summary>
/// Creates a new ReadPreference instance from a BsonDocument.
/// </summary>
/// <param name="document">The document.</param>
/// <returns>A ReadPreference.</returns>
public static ReadPreference FromBsonDocument(BsonDocument document)
{
ReadPreferenceMode mode = ReadPreferenceMode.Primary;
TimeSpan? maxStaleness = null;
List<TagSet> tagSets = null;
ReadPreferenceHedge hedge = null;
foreach (var element in document)
{
switch (element.Name)
{
case "mode":
mode = (ReadPreferenceMode)Enum.Parse(typeof(ReadPreferenceMode), element.Value.AsString, ignoreCase: true);
break;
case "tags":
case "tagSets":
tagSets = new List<TagSet>();
foreach (var tagsDocument in element.Value.AsBsonArray.Cast<BsonDocument>())
{
var tags = new List<Tag>();
foreach (var tagElement in tagsDocument)
{
var tag = new Tag(tagElement.Name, tagElement.Value.AsString);
tags.Add(tag);
}
var tagSet = new TagSet(tags);
tagSets.Add(tagSet);
}
break;
case "maxStaleness":
maxStaleness = element.Value.BsonType switch
{
BsonType.String => TimeSpanParser.Parse(element.Value.AsString),
_ => TimeSpan.FromSeconds(element.Value.ToDouble())
};
break;
case "maxStalenessSeconds":
maxStaleness = TimeSpan.FromSeconds(element.Value.ToDouble());
break;
case "hedge":
var hedgeEnabled = element.Value.AsBsonDocument["enabled"].AsBoolean;
hedge = new ReadPreferenceHedge(hedgeEnabled);
break;
default:
throw new ArgumentException($"Invalid element in ReadPreference document: {element.Name}.");
}
}
return new ReadPreference(mode, tagSets, maxStaleness, hedge);
}
#endregion
// fields
private readonly ReadPreferenceHedge _hedge;
private readonly TimeSpan? _maxStaleness;
private readonly ReadPreferenceMode _mode;
private readonly IReadOnlyList<TagSet> _tagSets;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="ReadPreference" /> class.
/// </summary>
/// <param name="mode">The read preference mode.</param>
/// <param name="tagSets">The tag sets.</param>
/// <param name="maxStaleness">The maximum staleness.</param>
/// <param name="hedge">The hedge.</param>
public ReadPreference(
ReadPreferenceMode mode,
IEnumerable<TagSet> tagSets = null,
TimeSpan? maxStaleness = null,
ReadPreferenceHedge hedge = null)
{
var tagSetsArray = tagSets == null ? __emptyTagSetsArray : tagSets.ToArray();
if (tagSetsArray.Length > 0)
{
Ensure.That(mode != ReadPreferenceMode.Primary, "TagSets cannot be used with ReadPreferenceMode Primary.", nameof(tagSets));
}
if (maxStaleness.HasValue)
{
Ensure.IsInfiniteOrGreaterThanZero(maxStaleness.Value, nameof(maxStaleness));
if (maxStaleness.Value > TimeSpan.Zero)
{
Ensure.That(maxStaleness.Value.Ticks % TimeSpan.TicksPerMillisecond == 0, "MaxStaleness must not have fractional seconds.", nameof(maxStaleness));
}
Ensure.That(mode != ReadPreferenceMode.Primary, "MaxStaleness cannot be used with ReadPreferenceMode Primary.", nameof(maxStaleness));
}
if (hedge != null)
{
Ensure.That(mode != ReadPreferenceMode.Primary, "Hedged reads cannot be used with ReadPreferenceMode Primary.", nameof(hedge));
}
_mode = mode;
_tagSets = tagSetsArray;
_maxStaleness = maxStaleness;
_hedge = hedge;
}
// properties
/// <summary>
/// Gets the hedge.
/// </summary>
public ReadPreferenceHedge Hedge => _hedge;
/// <summary>
/// Gets the maximum staleness.
/// </summary>
/// <value>
/// The maximum staleness.
/// </value>
public TimeSpan? MaxStaleness
{
get { return _maxStaleness; }
}
/// <summary>
/// Gets the read preference mode.
/// </summary>
/// <value>
/// The read preference mode.
/// </value>
public ReadPreferenceMode ReadPreferenceMode
{
get { return _mode; }
}
/// <summary>
/// Gets the tag sets.
/// </summary>
/// <value>
/// The tag sets.
/// </value>
public IReadOnlyList<TagSet> TagSets
{
get { return _tagSets; }
}
// methods
/// <inheritdoc/>
public bool Equals(ReadPreference other)
{
if (other == null)
{
return false;
}
return
object.Equals(_hedge, other._hedge) &&
_maxStaleness.Equals(other._maxStaleness) &&
_mode.Equals(other._mode) &&
_tagSets.SequenceEqual(other.TagSets);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return Equals(obj as ReadPreference);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return new Hasher()
.Hash(_hedge)
.Hash(_maxStaleness)
.Hash(_mode)
.HashElements(_tagSets)
.GetHashCode();
}
/// <inheritdoc/>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("{ Mode : ");
sb.Append(_mode.ToString());
if (_tagSets.Count > 0)
{
sb.Append(", TagSets : [");
sb.Append(string.Join(", ", _tagSets));
sb.Append("]");
}
if (_maxStaleness.HasValue)
{
sb.Append(", MaxStaleness : ");
sb.Append(TimeSpanParser.ToString(_maxStaleness.Value));
}
if (_hedge != null)
{
sb.Append(", Hedge : ");
sb.Append(_hedge.ToBsonDocument().ToJson());
}
sb.Append(" }");
return sb.ToString();
}
/// <summary>
/// Returns a new instance of ReadPreference with some values changed.
/// </summary>
/// <param name="hedge">The hedge.</param>
/// <returns>A new instance of ReadPreference.</returns>
public ReadPreference With(ReadPreferenceHedge hedge)
{
return new ReadPreference(_mode, _tagSets, _maxStaleness, hedge);
}
/// <summary>
/// Returns a new instance of ReadPreference with some values changed.
/// </summary>
/// <param name="mode">The read preference mode.</param>
/// <returns>A new instance of ReadPreference.</returns>
public ReadPreference With(ReadPreferenceMode mode)
{
return new ReadPreference(mode, _tagSets, _maxStaleness, _hedge);
}
/// <summary>
/// Returns a new instance of ReadPreference with some values changed.
/// </summary>
/// <param name="tagSets">The tag sets.</param>
/// <returns>A new instance of ReadPreference.</returns>
public ReadPreference With(IEnumerable<TagSet> tagSets)
{
return new ReadPreference(_mode, tagSets, _maxStaleness, _hedge);
}
/// <summary>
/// Returns a new instance of ReadPreference with some values changed.
/// </summary>
/// <param name="maxStaleness">The maximum staleness.</param>
/// <returns>A new instance of ReadPreference.</returns>
public ReadPreference With(TimeSpan? maxStaleness)
{
return new ReadPreference(_mode, _tagSets, maxStaleness, _hedge);
}
}
}