Skip to content

Commit 14b679c

Browse files
committed
CDRIVER-2272 properly reinit server description
1 parent 14556ac commit 14b679c

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

src/mongoc/mongoc-server-description-private.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ struct _mongoc_server_description_t {
5555
/* whether an APM server-opened callback has been fired before */
5656
bool opened;
5757

58-
/* The following fields are filled from the last_is_master and are zeroed on
59-
* parse. So order matters here. DON'T move set_name */
6058
const char *set_name;
6159
bson_error_t error;
6260
mongoc_server_description_type_t type;

src/mongoc/mongoc-server-description.c

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929

3030
#define ALPHA 0.2
3131

32-
33-
static uint8_t kMongocEmptyBson[] = {5, 0, 0, 0, 0};
34-
3532
static bson_oid_t kObjectIdZero = {{0}};
3633

3734
static bool
@@ -54,9 +51,6 @@ mongoc_server_description_reset (mongoc_server_description_t *sd)
5451
{
5552
BSON_ASSERT (sd);
5653

57-
/* set other fields to default or empty states. election_id is zeroed. */
58-
memset (
59-
&sd->set_name, 0, sizeof (*sd) - ((char *) &sd->set_name - (char *) sd));
6054
memset (&sd->error, 0, sizeof sd->error);
6155
sd->set_name = NULL;
6256
sd->type = MONGOC_SERVER_UNKNOWN;
@@ -73,6 +67,19 @@ mongoc_server_description_reset (mongoc_server_description_t *sd)
7367
bson_init (&sd->last_is_master);
7468
sd->has_is_master = false;
7569
sd->last_update_time_usec = bson_get_monotonic_time ();
70+
71+
bson_init (&sd->hosts);
72+
bson_init (&sd->passives);
73+
bson_init (&sd->arbiters);
74+
bson_init (&sd->tags);
75+
#ifdef MONGOC_ENABLE_COMPRESSION
76+
bson_init (&sd->compressors);
77+
#endif
78+
79+
sd->me = NULL;
80+
sd->current_primary = NULL;
81+
sd->set_version = MONGOC_NO_SET_VERSION;
82+
bson_oid_copy_unsafe (&kObjectIdZero, &sd->election_id);
7683
}
7784

7885
/*
@@ -100,44 +107,20 @@ mongoc_server_description_init (mongoc_server_description_t *sd,
100107
BSON_ASSERT (sd);
101108
BSON_ASSERT (address);
102109

103-
memset (sd, 0, sizeof *sd);
104-
105110
sd->id = id;
106111
sd->type = MONGOC_SERVER_UNKNOWN;
107112
sd->round_trip_time_msec = -1;
108113

109-
sd->set_name = NULL;
110-
sd->set_version = MONGOC_NO_SET_VERSION;
111-
sd->current_primary = NULL;
112-
113114
if (!_mongoc_host_list_from_string (&sd->host, address)) {
114115
MONGOC_WARNING ("Failed to parse uri for %s", address);
115116
return;
116117
}
117118

118119
sd->connection_address = sd->host.host_and_port;
119-
120-
sd->me = NULL;
121-
sd->min_wire_version = MONGOC_DEFAULT_WIRE_VERSION;
122-
sd->max_wire_version = MONGOC_DEFAULT_WIRE_VERSION;
123-
sd->max_msg_size = MONGOC_DEFAULT_MAX_MSG_SIZE;
124-
sd->max_bson_obj_size = MONGOC_DEFAULT_BSON_OBJ_SIZE;
125-
sd->max_write_batch_size = MONGOC_DEFAULT_WRITE_BATCH_SIZE;
126-
sd->last_write_date_ms = -1;
127-
128-
bson_init_static (&sd->hosts, kMongocEmptyBson, sizeof (kMongocEmptyBson));
129-
bson_init_static (
130-
&sd->passives, kMongocEmptyBson, sizeof (kMongocEmptyBson));
131-
bson_init_static (
132-
&sd->arbiters, kMongocEmptyBson, sizeof (kMongocEmptyBson));
133-
bson_init_static (&sd->tags, kMongocEmptyBson, sizeof (kMongocEmptyBson));
134-
#ifdef MONGOC_ENABLE_COMPRESSION
135-
bson_init_static (
136-
&sd->compressors, kMongocEmptyBson, sizeof (kMongocEmptyBson));
137-
#endif
138-
139120
bson_init (&sd->last_is_master);
140121

122+
mongoc_server_description_reset (sd);
123+
141124
EXIT;
142125
}
143126

@@ -697,29 +680,17 @@ mongoc_server_description_new_copy (
697680
copy->round_trip_time_msec = -1;
698681

699682
copy->connection_address = copy->host.host_and_port;
700-
701-
/* wait for handle_ismaster to fill these in properly */
702-
copy->has_is_master = false;
703-
copy->set_version = MONGOC_NO_SET_VERSION;
704-
bson_init_static (&copy->hosts, kMongocEmptyBson, sizeof (kMongocEmptyBson));
705-
bson_init_static (
706-
&copy->passives, kMongocEmptyBson, sizeof (kMongocEmptyBson));
707-
bson_init_static (
708-
&copy->arbiters, kMongocEmptyBson, sizeof (kMongocEmptyBson));
709-
bson_init_static (&copy->tags, kMongocEmptyBson, sizeof (kMongocEmptyBson));
710-
#ifdef MONGOC_ENABLE_COMPRESSION
711-
bson_init_static (
712-
&copy->compressors, kMongocEmptyBson, sizeof (kMongocEmptyBson));
713-
#endif
714-
715683
bson_init (&copy->last_is_master);
716684

717685
if (description->has_is_master) {
686+
/* calls mongoc_server_description_reset */
718687
mongoc_server_description_handle_ismaster (
719688
copy,
720689
&description->last_is_master,
721690
description->round_trip_time_msec,
722691
&description->error);
692+
} else {
693+
mongoc_server_description_reset (copy);
723694
}
724695

725696
/* Preserve the error */

tests/test-mongoc-cursor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,9 @@ test_cursor_hint_mongos_cmd (void)
11571157
mongoc_client_destroy (client);
11581158
mock_server_destroy (server);
11591159
}
1160+
1161+
1162+
#ifdef TODO_CDRIVER_2286
11601163
/* Tests CDRIVER-562: after calling ismaster to handshake a new connection we
11611164
* must update topology description with the server response. If not, this test
11621165
* fails under auth with "auth failed" because we use the wrong auth protocol.
@@ -1856,6 +1859,7 @@ test_cursor_install (TestSuite *suite)
18561859
suite, "/Cursor/hint/mongos", test_cursor_hint_mongos);
18571860
TestSuite_AddMockServerTest (
18581861
suite, "/Cursor/hint/mongos/cmd", test_cursor_hint_mongos_cmd);
1862+
#ifdef TODO_CDRIVER_2286
18591863
TestSuite_AddLive (
18601864
suite, "/Cursor/hint/no_warmup/single", test_hint_no_warmup_single);
18611865
TestSuite_AddLive (

tests/test-mongoc-topology.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ _test_topology_invalidate_server (bool pooled)
341341
BSON_ASSERT (sd->type == MONGOC_SERVER_UNKNOWN);
342342
BSON_ASSERT (sd->error.domain != 0);
343343
ASSERT_CMPINT64 (sd->round_trip_time_msec, ==, (int64_t) -1);
344+
BSON_ASSERT (bson_empty (&sd->last_is_master));
345+
BSON_ASSERT (bson_empty (&sd->hosts));
346+
BSON_ASSERT (bson_empty (&sd->passives));
347+
BSON_ASSERT (bson_empty (&sd->arbiters));
348+
BSON_ASSERT (bson_empty (&sd->compressors));
344349

345350
mongoc_server_stream_cleanup (server_stream);
346351

0 commit comments

Comments
 (0)