-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathserver_description.ts
266 lines (233 loc) · 8.32 KB
/
server_description.ts
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
import { type Document, Long, type ObjectId } from '../bson';
import { type MongoError, MongoRuntimeError } from '../error';
import { arrayStrictEqual, compareObjectId, errorStrictEqual, HostAddress, now } from '../utils';
import type { ClusterTime } from './common';
import { ServerType } from './common';
const WRITABLE_SERVER_TYPES = new Set<ServerType>([
ServerType.RSPrimary,
ServerType.Standalone,
ServerType.Mongos,
ServerType.LoadBalancer
]);
const DATA_BEARING_SERVER_TYPES = new Set<ServerType>([
ServerType.RSPrimary,
ServerType.RSSecondary,
ServerType.Mongos,
ServerType.Standalone,
ServerType.LoadBalancer
]);
/** @public */
export interface TopologyVersion {
processId: ObjectId;
counter: Long;
}
/** @public */
export type TagSet = { [key: string]: string };
/** @internal */
export interface ServerDescriptionOptions {
/** An Error used for better reporting debugging */
error?: MongoError;
/** The average round trip time to ping this server (in ms) */
roundTripTime?: number;
/** The minimum round trip time to ping this server over the past 10 samples(in ms) */
minRoundTripTime?: number;
/** If the client is in load balancing mode. */
loadBalanced?: boolean;
}
/**
* The client's view of a single server, based on the most recent hello outcome.
*
* Internal type, not meant to be directly instantiated
* @public
*/
export class ServerDescription {
address: string;
type: ServerType;
hosts: string[];
passives: string[];
arbiters: string[];
tags: TagSet;
error: MongoError | null;
topologyVersion: TopologyVersion | null;
minWireVersion: number;
maxWireVersion: number;
roundTripTime: number;
minRoundTripTime: number;
lastUpdateTime: number;
lastWriteDate: number;
me: string | null;
primary: string | null;
setName: string | null;
setVersion: number | null;
electionId: ObjectId | null;
logicalSessionTimeoutMinutes: number | null;
// NOTE: does this belong here? It seems we should gossip the cluster time at the CMAP level
$clusterTime?: ClusterTime;
/**
* Create a ServerDescription
* @internal
*
* @param address - The address of the server
* @param hello - An optional hello response for this server
*/
constructor(
address: HostAddress | string,
hello?: Document,
options: ServerDescriptionOptions = {}
) {
if (address == null || address === '') {
throw new MongoRuntimeError('ServerDescription must be provided with a non-empty address');
}
this.address =
typeof address === 'string'
? HostAddress.fromString(address).toString() // Use HostAddress to normalize
: address.toString();
this.type = parseServerType(hello, options);
this.hosts = hello?.hosts?.map((host: string) => host.toLowerCase()) ?? [];
this.passives = hello?.passives?.map((host: string) => host.toLowerCase()) ?? [];
this.arbiters = hello?.arbiters?.map((host: string) => host.toLowerCase()) ?? [];
this.tags = hello?.tags ?? {};
this.minWireVersion = hello?.minWireVersion ?? 0;
this.maxWireVersion = hello?.maxWireVersion ?? 0;
this.roundTripTime = options?.roundTripTime ?? -1;
this.minRoundTripTime = options?.minRoundTripTime ?? 0;
this.lastUpdateTime = now();
this.lastWriteDate = hello?.lastWrite?.lastWriteDate ?? 0;
this.error = options.error ?? null;
// TODO(NODE-2674): Preserve int64 sent from MongoDB
this.topologyVersion = this.error?.topologyVersion ?? hello?.topologyVersion ?? null;
this.setName = hello?.setName ?? null;
this.setVersion = hello?.setVersion ?? null;
this.electionId = hello?.electionId ?? null;
this.logicalSessionTimeoutMinutes = hello?.logicalSessionTimeoutMinutes ?? null;
this.primary = hello?.primary ?? null;
this.me = hello?.me?.toLowerCase() ?? null;
this.$clusterTime = hello?.$clusterTime ?? null;
}
get hostAddress(): HostAddress {
return HostAddress.fromString(this.address);
}
get allHosts(): string[] {
return this.hosts.concat(this.arbiters).concat(this.passives);
}
/** Is this server available for reads*/
get isReadable(): boolean {
return this.type === ServerType.RSSecondary || this.isWritable;
}
/** Is this server data bearing */
get isDataBearing(): boolean {
return DATA_BEARING_SERVER_TYPES.has(this.type);
}
/** Is this server available for writes */
get isWritable(): boolean {
return WRITABLE_SERVER_TYPES.has(this.type);
}
get host(): string {
const chopLength = `:${this.port}`.length;
return this.address.slice(0, -chopLength);
}
get port(): number {
const port = this.address.split(':').pop();
return port ? Number.parseInt(port, 10) : 27017;
}
/**
* Determines if another `ServerDescription` is equal to this one per the rules defined
* in the {@link https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#serverdescription|SDAM spec}
*/
equals(other?: ServerDescription | null): boolean {
// Despite using the comparator that would determine a nullish topologyVersion as greater than
// for equality we should only always perform direct equality comparison
const topologyVersionsEqual =
this.topologyVersion === other?.topologyVersion ||
compareTopologyVersion(this.topologyVersion, other?.topologyVersion) === 0;
const electionIdsEqual =
this.electionId != null && other?.electionId != null
? compareObjectId(this.electionId, other.electionId) === 0
: this.electionId === other?.electionId;
return (
other != null &&
errorStrictEqual(this.error, other.error) &&
this.type === other.type &&
this.minWireVersion === other.minWireVersion &&
arrayStrictEqual(this.hosts, other.hosts) &&
tagsStrictEqual(this.tags, other.tags) &&
this.setName === other.setName &&
this.setVersion === other.setVersion &&
electionIdsEqual &&
this.primary === other.primary &&
this.logicalSessionTimeoutMinutes === other.logicalSessionTimeoutMinutes &&
topologyVersionsEqual
);
}
}
// Parses a `hello` message and determines the server type
export function parseServerType(hello?: Document, options?: ServerDescriptionOptions): ServerType {
if (options?.loadBalanced) {
return ServerType.LoadBalancer;
}
if (!hello || !hello.ok) {
return ServerType.Unknown;
}
if (hello.isreplicaset) {
return ServerType.RSGhost;
}
if (hello.msg && hello.msg === 'isdbgrid') {
return ServerType.Mongos;
}
if (hello.setName) {
if (hello.hidden) {
return ServerType.RSOther;
} else if (hello.isWritablePrimary) {
return ServerType.RSPrimary;
} else if (hello.secondary) {
return ServerType.RSSecondary;
} else if (hello.arbiterOnly) {
return ServerType.RSArbiter;
} else {
return ServerType.RSOther;
}
}
return ServerType.Standalone;
}
function tagsStrictEqual(tags: TagSet, tags2: TagSet): boolean {
const tagsKeys = Object.keys(tags);
const tags2Keys = Object.keys(tags2);
return (
tagsKeys.length === tags2Keys.length &&
tagsKeys.every((key: string) => tags2[key] === tags[key])
);
}
/**
* Compares two topology versions.
*
* 1. If the response topologyVersion is unset or the ServerDescription's
* topologyVersion is null, the client MUST assume the response is more recent.
* 1. If the response's topologyVersion.processId is not equal to the
* ServerDescription's, the client MUST assume the response is more recent.
* 1. If the response's topologyVersion.processId is equal to the
* ServerDescription's, the client MUST use the counter field to determine
* which topologyVersion is more recent.
*
* ```ts
* currentTv < newTv === -1
* currentTv === newTv === 0
* currentTv > newTv === 1
* ```
*/
export function compareTopologyVersion(
currentTv?: TopologyVersion | null,
newTv?: TopologyVersion | null
): 0 | -1 | 1 {
if (currentTv == null || newTv == null) {
return -1;
}
if (!currentTv.processId.equals(newTv.processId)) {
return -1;
}
// TODO(NODE-2674): Preserve int64 sent from MongoDB
const currentCounter = Long.isLong(currentTv.counter)
? currentTv.counter
: Long.fromNumber(currentTv.counter);
const newCounter = Long.isLong(newTv.counter) ? newTv.counter : Long.fromNumber(newTv.counter);
return currentCounter.compare(newCounter);
}