You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/mongo/read_preference.ex
+77-70Lines changed: 77 additions & 70 deletions
Original file line number
Diff line number
Diff line change
@@ -4,134 +4,141 @@ defmodule Mongo.ReadPreference do
4
4
@moduledoc~S"""
5
5
Determines which servers are considered suitable for read operations
6
6
7
-
A read preference consists of a mode and optional `tag_sets`, max_staleness_ms, and `hedge`.
7
+
A read preference consists of a mode and optional `tags`, max_staleness_ms, and `hedge`.
8
8
The mode prioritizes between primaries and secondaries to produce either a single suitable server or a list of candidate servers.
9
-
If tag_sets and maxStalenessSeconds are set, they determine which candidate servers are eligible for selection.
9
+
If tags and maxStalenessSeconds are set, they determine which candidate servers are eligible for selection.
10
10
If hedge is set, it configures how server hedged reads are used.
11
11
12
12
The default mode is `:primary`.
13
-
The default tag_sets is a list with an empty tag set: [{}].
13
+
The default tags is a list with an empty tag set: [{}].
14
14
The default max_staleness_ms is unset.
15
15
The default hedge is unset.
16
16
17
17
## mode
18
18
19
19
* `:primary` Only an available primary is suitable.
20
-
* `:secondary` All secondaries (and only secondaries) are candidates, but only eligible candidates (i.e. after applying tag_sets and maxStalenessSeconds) are suitable.
20
+
* `:secondary` All secondaries (and only secondaries) are candidates, but only eligible candidates (i.e. after applying tags and maxStalenessSeconds) are suitable.
21
21
* `:primary_preferred` If a primary is available, only the primary is suitable. Otherwise, all secondaries are candidates,
22
22
but only eligible secondaries are suitable.
23
23
* `:secondary_preferred` All secondaries are candidates. If there is at least one eligible secondary, only eligible secondaries are suitable.
24
24
Otherwise, when there are no eligible secondaries, the primary is suitable.
25
25
* `:nearest` The primary and all secondaries are candidates, but only eligible candidates are suitable.
26
26
27
27
"""
28
-
@typet::%{
29
-
mode:
30
-
:primary
31
-
|:secondary
32
-
|:primary_preferred
33
-
|:secondary_preferred
34
-
|:nearest,
35
-
tag_sets: [%{String.t()=>String.t()}],
36
-
max_staleness_ms: non_neg_integer,
37
-
hedge: BSON.document()
38
-
}
39
28
40
29
@primary%{
41
30
mode: :primary,
42
-
tag_sets: [],
31
+
tags: [],
43
32
max_staleness_ms: 0
44
33
}
45
34
46
-
defprimary(map\\nil)
35
+
@doc"""
36
+
Merge default values to the read preferences and converts deprecated tag_sets to tags
37
+
"""
38
+
defmerge_defaults(%{tag_sets: tags}=map)do
39
+
map=
40
+
map
41
+
|>Map.delete(:tag_sets)
42
+
|>Map.put(:tags,tags)
43
+
44
+
Map.merge(@primary,map)
45
+
end
47
46
48
-
defprimary(map)whenis_map(map)do
47
+
defmerge_defaults(map)whenis_map(map)do
49
48
Map.merge(@primary,map)
50
49
end
51
50
52
-
defprimary(_),do: @primary
51
+
defmerge_defaults(_other)do
52
+
@primary
53
+
end
53
54
54
55
@doc"""
55
56
Add read preference to the cmd
56
57
"""
57
58
defadd_read_preference(cmd,opts)do
58
59
caseKeyword.get(opts,:read_preference)do
59
-
nil->cmd
60
-
pref->cmd++["$readPreference": pref]
60
+
nil->
61
+
cmd
62
+
63
+
pref->
64
+
cmd++["$readPreference": pref]
61
65
end
62
66
end
63
67
64
68
@doc"""
65
-
From the specs:
66
-
67
-
Use of slaveOk
68
-
69
-
There are two usages of slaveOK:
70
-
71
-
* A driver query parameter that predated read preference modes and tag set lists.
72
-
* A wire protocol flag on OP_QUERY operations
73
-
69
+
Converts the preference to the mongodb format for replica sets
0 commit comments