Skip to content

Commit d283803

Browse files
jmikolakevinAlbs
andauthored
CDRIVER-3909 topology description copy/destroy methods (#785)
* Rename mongoc_topology_description_destroy to cleanup * Use consistent language in server description copy/destroy docs * CDRIVER-3909 topology description copy/destroy methods Co-authored-by: Kevin Albertson <[email protected]>
1 parent 6553e01 commit d283803

13 files changed

+193
-17
lines changed

src/libmongoc/doc/mongoc_server_description_destroy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ Parameters
1919
Description
2020
-----------
2121

22-
Clean up all memory associated with the server description. Does nothing if ``description`` is NULL.
22+
Frees all resources associated with the server description. Does nothing if ``description`` is NULL.

src/libmongoc/doc/mongoc_server_description_new_copy.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ Parameters
2020
Description
2121
-----------
2222

23-
This function copies the given server description and returns a new server description object. The caller is responsible for destroying the new copy.
23+
Performs a deep copy of ``description``.
2424

2525
Returns
2626
-------
2727

28-
A copy of the original server description.
29-
28+
Returns a newly allocated copy of ``description`` that should be freed with :symbol:`mongoc_server_description_destroy()` when no longer in use. Returns NULL if ``description`` is NULL.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
:man_page: mongoc_topology_description_destroy
2+
3+
mongoc_topology_description_destroy()
4+
=====================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
mongoc_topology_description_destroy (mongoc_topology_description_t *description);
13+
14+
Parameters
15+
----------
16+
17+
* ``description``: A :symbol:`mongoc_topology_description_t`.
18+
19+
Description
20+
-----------
21+
22+
Frees all resources associated with the topology description. Does nothing if ``description`` is NULL.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:man_page: mongoc_topology_description_new_copy
2+
3+
mongoc_topology_description_new_copy()
4+
======================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
mongoc_topology_description_t *
12+
mongoc_topology_description_new_copy (
13+
const mongoc_topology_description_t *description);
14+
15+
Parameters
16+
----------
17+
18+
* ``description``: A :symbol:`mongoc_topology_description_t`.
19+
20+
Description
21+
-----------
22+
23+
Performs a deep copy of ``description``.
24+
25+
Returns
26+
-------
27+
28+
Returns a newly allocated copy of ``description`` that should be freed with :symbol:`mongoc_topology_description_destroy()` when no longer in use. Returns NULL if ``description`` is NULL.

src/libmongoc/doc/mongoc_topology_description_t.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Synopsis
1515
``mongoc_topology_description_t`` is an opaque type representing the driver's knowledge of the MongoDB server or servers it is connected to.
1616
Its API conforms to the `SDAM Monitoring Specification <https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-monitoring.rst>`_.
1717

18-
Applications receive a temporary reference to a ``mongoc_topology_description_t`` as a parameter to an SDAM Monitoring callback. See :doc:`Introduction to Application Performance Monitoring <application-performance-monitoring>`.
18+
Applications receive a temporary reference to a ``mongoc_topology_description_t`` as a parameter to an SDAM Monitoring callback that must not be destroyed. See :doc:`Introduction to Application Performance Monitoring <application-performance-monitoring>`.
1919

2020
.. only:: html
2121

@@ -26,8 +26,10 @@ Applications receive a temporary reference to a ``mongoc_topology_description_t`
2626
:titlesonly:
2727
:maxdepth: 1
2828

29+
mongoc_topology_description_destroy
2930
mongoc_topology_description_get_servers
3031
mongoc_topology_description_has_readable_server
3132
mongoc_topology_description_has_writable_server
33+
mongoc_topology_description_new_copy
3234
mongoc_topology_description_type
3335

src/libmongoc/src/mongoc/mongoc-topology-description-apm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ _mongoc_topology_description_monitor_opening (mongoc_topology_description_t *td)
132132
prev_sd = mongoc_server_description_new_copy (sd);
133133
BSON_ASSERT (prev_sd);
134134
if (td->apm_callbacks.topology_changed) {
135-
mongoc_topology_description_destroy (prev_td);
135+
mongoc_topology_description_cleanup (prev_td);
136136
_mongoc_topology_description_copy_to (td, prev_td);
137137
}
138138
sd->type = MONGOC_SERVER_LOAD_BALANCER;
@@ -144,7 +144,7 @@ _mongoc_topology_description_monitor_opening (mongoc_topology_description_t *td)
144144
}
145145

146146
if (prev_td) {
147-
mongoc_topology_description_destroy (prev_td);
147+
mongoc_topology_description_cleanup (prev_td);
148148
bson_free (prev_td);
149149
}
150150
}

src/libmongoc/src/mongoc/mongoc-topology-description-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ _mongoc_topology_description_copy_to (const mongoc_topology_description_t *src,
7373
mongoc_topology_description_t *dst);
7474

7575
void
76-
mongoc_topology_description_destroy (
76+
mongoc_topology_description_cleanup (
7777
mongoc_topology_description_t *description);
7878

7979
void

src/libmongoc/src/mongoc/mongoc-topology-description.c

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ mongoc_topology_description_init (mongoc_topology_description_t *description,
104104
*
105105
* Deep-copy @src to an uninitialized topology description @dst.
106106
* @dst must not already point to any allocated resources. Clean
107-
* up with mongoc_topology_description_destroy.
107+
* up with mongoc_topology_description_cleanup.
108108
*
109109
* WARNING: @dst's rand_seed is not initialized.
110110
*
@@ -163,12 +163,46 @@ _mongoc_topology_description_copy_to (const mongoc_topology_description_t *src,
163163
EXIT;
164164
}
165165

166+
/*
167+
*-------------------------------------------------------------------------
168+
*
169+
* mongoc_topology_description_new_copy --
170+
*
171+
* Allocates a new topology description and deep-copies @description to it
172+
* using _mongoc_topology_description_copy_to.
173+
*
174+
* Returns:
175+
* A copy of a topology description that you must destroy with
176+
* mongoc_topology_description_destroy, or NULL if @description is NULL.
177+
*
178+
* Side effects:
179+
* None.
180+
*
181+
*-------------------------------------------------------------------------
182+
*/
183+
mongoc_topology_description_t *
184+
mongoc_topology_description_new_copy (
185+
const mongoc_topology_description_t *description)
186+
{
187+
mongoc_topology_description_t *copy;
188+
189+
if (!description) {
190+
return NULL;
191+
}
192+
193+
copy = (mongoc_topology_description_t *) bson_malloc0 (sizeof (*copy));
194+
195+
_mongoc_topology_description_copy_to (description, copy);
196+
197+
return copy;
198+
}
199+
166200
/*
167201
*--------------------------------------------------------------------------
168202
*
169-
* mongoc_topology_description_destroy --
203+
* mongoc_topology_description_cleanup --
170204
*
171-
* Destroy allocated resources within @description
205+
* Destroy allocated resources within @description but don't free it.
172206
*
173207
* Returns:
174208
* None.
@@ -179,7 +213,7 @@ _mongoc_topology_description_copy_to (const mongoc_topology_description_t *src,
179213
*--------------------------------------------------------------------------
180214
*/
181215
void
182-
mongoc_topology_description_destroy (mongoc_topology_description_t *description)
216+
mongoc_topology_description_cleanup (mongoc_topology_description_t *description)
183217
{
184218
ENTRY;
185219

@@ -198,6 +232,37 @@ mongoc_topology_description_destroy (mongoc_topology_description_t *description)
198232
EXIT;
199233
}
200234

235+
/*
236+
*--------------------------------------------------------------------------
237+
*
238+
* mongoc_topology_description_destroy --
239+
*
240+
* Destroy allocated resources within @description and free
241+
* @description.
242+
*
243+
* Returns:
244+
* None.
245+
*
246+
* Side effects:
247+
* None.
248+
*
249+
*--------------------------------------------------------------------------
250+
*/
251+
void
252+
mongoc_topology_description_destroy (mongoc_topology_description_t *description)
253+
{
254+
ENTRY;
255+
256+
if (!description) {
257+
EXIT;
258+
}
259+
260+
mongoc_topology_description_cleanup (description);
261+
bson_free (description);
262+
263+
EXIT;
264+
}
265+
201266
/* find the primary, then stop iterating */
202267
static bool
203268
_mongoc_topology_description_has_primary_cb (void *item, void *ctx /* OUT */)
@@ -1984,7 +2049,7 @@ mongoc_topology_description_handle_hello (
19842049
&sd->topology_version, &incoming_topology_version) == 1) {
19852050
TRACE ("%s", "topology version is strictly less. Skipping.");
19862051
if (prev_td) {
1987-
mongoc_topology_description_destroy (prev_td);
2052+
mongoc_topology_description_cleanup (prev_td);
19882053
bson_free (prev_td);
19892054
}
19902055
return;
@@ -2070,7 +2135,7 @@ mongoc_topology_description_handle_hello (
20702135
}
20712136

20722137
if (prev_td) {
2073-
mongoc_topology_description_destroy (prev_td);
2138+
mongoc_topology_description_cleanup (prev_td);
20742139
bson_free (prev_td);
20752140
}
20762141

src/libmongoc/src/mongoc/mongoc-topology-description.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,25 @@ BSON_BEGIN_DECLS
2929

3030
typedef struct _mongoc_topology_description_t mongoc_topology_description_t;
3131

32+
MONGOC_EXPORT (void)
33+
mongoc_topology_description_destroy (
34+
mongoc_topology_description_t *description);
35+
36+
MONGOC_EXPORT (mongoc_topology_description_t *)
37+
mongoc_topology_description_new_copy (
38+
const mongoc_topology_description_t *description);
39+
3240
MONGOC_EXPORT (bool)
3341
mongoc_topology_description_has_readable_server (
3442
mongoc_topology_description_t *td, const mongoc_read_prefs_t *prefs);
43+
3544
MONGOC_EXPORT (bool)
3645
mongoc_topology_description_has_writable_server (
3746
mongoc_topology_description_t *td);
47+
3848
MONGOC_EXPORT (const char *)
3949
mongoc_topology_description_type (const mongoc_topology_description_t *td);
50+
4051
MONGOC_EXPORT (mongoc_server_description_t **)
4152
mongoc_topology_description_get_servers (
4253
const mongoc_topology_description_t *td, size_t *n);

src/libmongoc/src/mongoc/mongoc-topology.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ mongoc_topology_destroy (mongoc_topology_t *topology)
567567
_mongoc_topology_description_monitor_closed (&topology->description);
568568

569569
mongoc_uri_destroy (topology->uri);
570-
mongoc_topology_description_destroy (&topology->description);
570+
mongoc_topology_description_cleanup (&topology->description);
571571
mongoc_topology_scanner_destroy (topology->scanner);
572572

573573
/* If we are single-threaded, the client will try to call

src/libmongoc/tests/json-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ test_server_selection_logic_cb (bson_t *test)
543543

544544
DONE:
545545
mongoc_read_prefs_destroy (read_prefs);
546-
mongoc_topology_description_destroy (&topology);
546+
mongoc_topology_description_cleanup (&topology);
547547
_mongoc_array_destroy (&selected_servers);
548548
}
549549

src/libmongoc/tests/test-mongoc-dns.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ test_srv_polling_mocked (void *unused)
579579
_mongoc_host_list_destroy_all (hosts);
580580
ASSERT_CAPTURED_LOG ("topology", MONGOC_LOG_LEVEL_ERROR, "Invalid host");
581581

582-
mongoc_topology_description_destroy (&td);
582+
mongoc_topology_description_cleanup (&td);
583583
mongoc_uri_destroy (uri);
584584
}
585585

src/libmongoc/tests/test-mongoc-topology-description.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,52 @@ test_topology_version_equal (void)
208208
mongoc_uri_destroy (uri);
209209
}
210210

211+
static void
212+
test_topology_description_new_copy (void)
213+
{
214+
mongoc_uri_t *uri;
215+
mongoc_topology_t *topology;
216+
mongoc_topology_description_t *td, *td_copy;
217+
mongoc_server_description_t *sd_a;
218+
mongoc_server_description_t *sd_c;
219+
mongoc_server_description_t **sds;
220+
size_t n;
221+
222+
uri = mongoc_uri_new ("mongodb://a,b,c");
223+
topology = mongoc_topology_new (uri, true /* single-threaded */);
224+
td = &topology->description;
225+
226+
td_copy = mongoc_topology_description_new_copy (td);
227+
228+
/* servers "a" and "c" are mongos, but "b" remains unknown */
229+
sd_a = _sd_for_host (td, "a");
230+
mongoc_topology_description_handle_hello (
231+
td, sd_a->id, tmp_bson ("{'ok': 1, 'msg': 'isdbgrid'}"), 100, NULL);
232+
233+
sd_c = _sd_for_host (td, "c");
234+
mongoc_topology_description_handle_hello (
235+
td, sd_c->id, tmp_bson ("{'ok': 1, 'msg': 'isdbgrid'}"), 100, NULL);
236+
237+
/* td was copied before original was updated */
238+
sds = mongoc_topology_description_get_servers (td_copy, &n);
239+
ASSERT_CMPSIZE_T ((size_t) 0, ==, n);
240+
241+
mongoc_server_descriptions_destroy_all (sds, n);
242+
mongoc_topology_description_destroy (td_copy);
243+
244+
td_copy = mongoc_topology_description_new_copy (td);
245+
246+
mongoc_topology_destroy (topology);
247+
mongoc_uri_destroy (uri);
248+
249+
/* td was copied after original was updated, but before it was destroyed */
250+
sds = mongoc_topology_description_get_servers (td_copy, &n);
251+
ASSERT_CMPSIZE_T ((size_t) 2, ==, n);
252+
253+
mongoc_server_descriptions_destroy_all (sds, n);
254+
mongoc_topology_description_destroy (td_copy);
255+
}
256+
211257
void
212258
test_topology_description_install (TestSuite *suite)
213259
{
@@ -221,4 +267,7 @@ test_topology_description_install (TestSuite *suite)
221267
TestSuite_Add (suite,
222268
"/TopologyDescription/topology_version_equal",
223269
test_topology_version_equal);
270+
TestSuite_Add (suite,
271+
"/TopologyDescription/new_copy",
272+
test_topology_description_new_copy);
224273
}

0 commit comments

Comments
 (0)