-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-5864 Do not parse BSON in mongoc_server_description_new_copy
#1835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9da57f8
do not parse BSON in `mongoc_server_description_new_copy`
kevinAlbs 5d84208
remove unnecessary atomic modification of `round_trip_time_msec`
kevinAlbs ca372a3
add tests for copying
kevinAlbs 392f83e
remove unnecessary `bson_init`
kevinAlbs 2c84319
add debug assertions
kevinAlbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,11 +485,9 @@ mongoc_server_description_update_rtt (mongoc_server_description_t *server, int64 | |
return; | ||
} | ||
if (server->round_trip_time_msec == MONGOC_RTT_UNSET) { | ||
mcommon_atomic_int64_exchange (&server->round_trip_time_msec, rtt_msec, mcommon_memory_order_relaxed); | ||
server->round_trip_time_msec = rtt_msec; | ||
} else { | ||
mcommon_atomic_int64_exchange (&server->round_trip_time_msec, | ||
(int64_t) (ALPHA * rtt_msec + (1 - ALPHA) * server->round_trip_time_msec), | ||
mcommon_memory_order_relaxed); | ||
server->round_trip_time_msec = (int64_t) (ALPHA * rtt_msec + (1 - ALPHA) * server->round_trip_time_msec); | ||
} | ||
} | ||
|
||
|
@@ -768,48 +766,96 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd, | |
mongoc_server_description_t * | ||
mongoc_server_description_new_copy (const mongoc_server_description_t *description) | ||
{ | ||
mongoc_server_description_t *copy; | ||
#define COPY_FIELD(FIELD) \ | ||
if (1) { \ | ||
copy->FIELD = description->FIELD; \ | ||
} else \ | ||
(void) 0 | ||
|
||
|
||
#define COPY_BSON_FIELD(FIELD) \ | ||
if (1) { \ | ||
bson_copy_to (&description->FIELD, ©->FIELD); \ | ||
} else \ | ||
(void) 0 | ||
|
||
// COPY_INTERNAL_BSON_FIELD copies a `bson_t` that references data in `last_hello_response`. | ||
#define COPY_INTERNAL_BSON_FIELD(FIELD) \ | ||
if (1) { \ | ||
if (!bson_empty (&description->FIELD)) { \ | ||
ptrdiff_t offset = bson_get_data (&description->FIELD) - bson_get_data (&description->last_hello_response); \ | ||
MONGOC_DEBUG_ASSERT (offset >= 0); \ | ||
const uint8_t *data = bson_get_data (©->last_hello_response) + offset; \ | ||
uint32_t len = description->FIELD.len; \ | ||
MONGOC_DEBUG_ASSERT (offset + len <= copy->last_hello_response.len); \ | ||
bson_init_static (©->FIELD, data, len); \ | ||
} else { \ | ||
bson_init (©->FIELD); \ | ||
} \ | ||
} else \ | ||
(void) 0 | ||
|
||
// COPY_INTERNAL_STRING_FIELD copies a `const char*` that references data in `last_hello_response`. | ||
#define COPY_INTERNAL_STRING_FIELD(FIELD) \ | ||
if (1) { \ | ||
if (description->FIELD) { \ | ||
ptrdiff_t offset = (char *) description->FIELD - (char *) bson_get_data (&description->last_hello_response); \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a good place for an asan-only assertion too, since it would require a heavy strlen check. |
||
MONGOC_DEBUG_ASSERT (offset >= 0); \ | ||
copy->FIELD = (char *) bson_get_data (©->last_hello_response) + offset; \ | ||
MONGOC_DEBUG_ASSERT (offset + strlen (description->FIELD) <= copy->last_hello_response.len); \ | ||
} else { \ | ||
copy->FIELD = NULL; \ | ||
} \ | ||
} else \ | ||
(void) 0 | ||
|
||
|
||
if (!description) { | ||
return NULL; | ||
} | ||
|
||
copy = BSON_ALIGNED_ALLOC0 (mongoc_server_description_t); | ||
|
||
copy->id = description->id; | ||
copy->opened = description->opened; | ||
memcpy (©->host, &description->host, sizeof (copy->host)); | ||
copy->round_trip_time_msec = MONGOC_RTT_UNSET; | ||
mongoc_server_description_t *copy = BSON_ALIGNED_ALLOC (mongoc_server_description_t); | ||
|
||
COPY_FIELD (id); | ||
COPY_FIELD (host); | ||
COPY_FIELD (round_trip_time_msec); | ||
COPY_FIELD (last_update_time_usec); | ||
COPY_BSON_FIELD (last_hello_response); | ||
COPY_FIELD (has_hello_response); | ||
COPY_FIELD (hello_ok); | ||
copy->connection_address = copy->host.host_and_port; | ||
bson_init (©->last_hello_response); | ||
bson_init (©->hosts); | ||
bson_init (©->passives); | ||
bson_init (©->arbiters); | ||
bson_init (©->tags); | ||
bson_init (©->compressors); | ||
bson_copy_to (&description->topology_version, ©->topology_version); | ||
bson_oid_copy (&description->service_id, ©->service_id); | ||
copy->server_connection_id = description->server_connection_id; | ||
|
||
if (description->has_hello_response) { | ||
/* calls mongoc_server_description_reset */ | ||
int64_t last_rtt_ms = | ||
mcommon_atomic_int64_fetch (&description->round_trip_time_msec, mcommon_memory_order_relaxed); | ||
mongoc_server_description_handle_hello ( | ||
copy, &description->last_hello_response, last_rtt_ms, &description->error); | ||
} else { | ||
mongoc_server_description_reset (copy); | ||
/* preserve the original server description type, which is manually set | ||
* for a LoadBalancer server */ | ||
copy->type = description->type; | ||
} | ||
COPY_INTERNAL_STRING_FIELD (me); | ||
COPY_FIELD (opened); | ||
COPY_INTERNAL_STRING_FIELD (set_name); | ||
COPY_FIELD (error); | ||
COPY_FIELD (type); | ||
COPY_FIELD (min_wire_version); | ||
COPY_FIELD (max_wire_version); | ||
COPY_FIELD (max_msg_size); | ||
COPY_FIELD (max_bson_obj_size); | ||
COPY_FIELD (max_write_batch_size); | ||
COPY_FIELD (session_timeout_minutes); | ||
COPY_INTERNAL_BSON_FIELD (hosts); | ||
COPY_INTERNAL_BSON_FIELD (passives); | ||
COPY_INTERNAL_BSON_FIELD (arbiters); | ||
COPY_INTERNAL_BSON_FIELD (tags); | ||
COPY_INTERNAL_STRING_FIELD (current_primary); | ||
COPY_FIELD (set_version); | ||
COPY_FIELD (election_id); | ||
COPY_FIELD (last_write_date_ms); | ||
COPY_INTERNAL_BSON_FIELD (compressors); | ||
// `topology_version` does not refer to data in `last_hello_response`. It needs to outlive `last_hello_response`. | ||
COPY_BSON_FIELD (topology_version); | ||
COPY_FIELD (generation); | ||
copy->_generation_map_ = mongoc_generation_map_copy (mc_tpl_sd_generation_map_const (description)); | ||
COPY_FIELD (service_id); | ||
COPY_FIELD (server_connection_id); | ||
|
||
/* Preserve the error */ | ||
memcpy (©->error, &description->error, sizeof copy->error); | ||
#undef COPY_INTERNAL_STRING_FIELD | ||
#undef COPY_INTERNAL_BSON_FIELD | ||
#undef COPY_BSON_FIELD | ||
#undef COPY_FIELD | ||
|
||
copy->generation = description->generation; | ||
copy->_generation_map_ = mongoc_generation_map_copy (mc_tpl_sd_generation_map_const (description)); | ||
return copy; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is a performance patch, but it might still be worth bounds-checking 'offset' here? This might be a good place for a debug-only or asan-only assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added debug-only assertions with
MONGOC_DEBUG_ASSERT