29
29
30
30
static uint8_t kMongocEmptyBson [] = { 5 , 0 , 0 , 0 , 0 };
31
31
32
+ static bson_oid_t kObjectIdZero = { 0 };
33
+
32
34
/* Destroy allocated resources within @description, but don't free it */
33
35
void
34
36
mongoc_server_description_cleanup (mongoc_server_description_t * sd )
@@ -45,7 +47,7 @@ mongoc_server_description_reset (mongoc_server_description_t *sd)
45
47
{
46
48
BSON_ASSERT (sd );
47
49
48
- /* set other fields to default or empty states */
50
+ /* set other fields to default or empty states. election_id is zeroed. */
49
51
memset (& sd -> set_name , 0 , sizeof (* sd ) - ((char * )& sd -> set_name - (char * )sd ));
50
52
sd -> set_name = NULL ;
51
53
sd -> type = MONGOC_SERVER_UNKNOWN ;
@@ -191,6 +193,25 @@ mongoc_server_description_has_rs_member(mongoc_server_description_t *server,
191
193
return false;
192
194
}
193
195
196
+ /*
197
+ *--------------------------------------------------------------------------
198
+ *
199
+ * mongoc_server_description_has_election_id --
200
+ *
201
+ * Did this server's ismaster response have an "electionId" field?
202
+ *
203
+ * Returns:
204
+ * True if the server description's electionId is set.
205
+ *
206
+ *--------------------------------------------------------------------------
207
+ */
208
+
209
+ bool
210
+ mongoc_server_description_has_election_id (mongoc_server_description_t * description )
211
+ {
212
+ return 0 != bson_oid_compare (& description -> election_id , & kObjectIdZero );
213
+ }
214
+
194
215
/*
195
216
*--------------------------------------------------------------------------
196
217
*
@@ -234,13 +255,7 @@ mongoc_server_description_host (mongoc_server_description_t *description)
234
255
*
235
256
* mongoc_server_description_set_state --
236
257
*
237
- * Change the state of this server.
238
- *
239
- * Returns:
240
- * true, false
241
- *
242
- * Side effects:
243
- * None
258
+ * Set the server description's server type.
244
259
*
245
260
*--------------------------------------------------------------------------
246
261
*/
@@ -252,6 +267,31 @@ mongoc_server_description_set_state (mongoc_server_description_t *description,
252
267
}
253
268
254
269
270
+ /*
271
+ *--------------------------------------------------------------------------
272
+ *
273
+ * mongoc_server_description_set_election_id --
274
+ *
275
+ * Set the election_id of this server. Copies the given ObjectId or,
276
+ * if it is NULL, zeroes description's election_id.
277
+ *
278
+ * Side effects:
279
+ * None.
280
+ *
281
+ *--------------------------------------------------------------------------
282
+ */
283
+ void
284
+ mongoc_server_description_set_election_id (mongoc_server_description_t * description ,
285
+ const bson_oid_t * election_id )
286
+ {
287
+ if (election_id ) {
288
+ bson_oid_copy_unsafe (election_id , & description -> election_id );
289
+ } else {
290
+ bson_oid_copy_unsafe (& kObjectIdZero , & description -> election_id );
291
+ }
292
+ }
293
+
294
+
255
295
/*
256
296
*-------------------------------------------------------------------------
257
297
*
@@ -260,11 +300,8 @@ mongoc_server_description_set_state (mongoc_server_description_t *description,
260
300
* Calculate this server's rtt calculation using an exponentially-
261
301
* weighted moving average formula.
262
302
*
263
- * Returns:
264
- * None.
265
- *
266
303
* Side effects:
267
- * Changes this server description's rtt .
304
+ * None .
268
305
*
269
306
*-------------------------------------------------------------------------
270
307
*/
@@ -351,6 +388,9 @@ mongoc_server_description_handle_ismaster (
351
388
} else if (strcmp ("setName" , bson_iter_key (& iter )) == 0 ) {
352
389
if (! BSON_ITER_HOLDS_UTF8 (& iter )) goto failure ;
353
390
sd -> set_name = bson_iter_utf8 (& iter , NULL );
391
+ } else if (strcmp ("electionId" , bson_iter_key (& iter )) == 0 ) {
392
+ if (! BSON_ITER_HOLDS_OID (& iter )) goto failure ;
393
+ mongoc_server_description_set_election_id (sd , bson_iter_oid (& iter ));
354
394
} else if (strcmp ("secondary" , bson_iter_key (& iter )) == 0 ) {
355
395
if (! BSON_ITER_HOLDS_BOOL (& iter )) goto failure ;
356
396
is_secondary = bson_iter_bool (& iter );
0 commit comments