Skip to content

Commit 92be0ec

Browse files
committed
CDRIVER-2809 topology listeners must cast away const
1 parent 6ea7737 commit 92be0ec

5 files changed

+37
-1
lines changed

src/libmongoc/doc/application-performance-monitoring.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ This example program prints something like:
8484
8585
topology opening
8686
topology changed: Unknown -> ReplicaSetNoPrimary
87+
secondary UNAVAILABLE
88+
primary UNAVAILABLE
8789
server opening: localhost:27017
8890
server opening: localhost:27018
8991
localhost:27017 heartbeat started
@@ -94,6 +96,8 @@ This example program prints something like:
9496
topology changed: ReplicaSetNoPrimary -> ReplicaSetWithPrimary
9597
new servers:
9698
RSPrimary localhost:27017
99+
secondary UNAVAILABLE
100+
primary AVAILABLE
97101
localhost:27019 heartbeat started
98102
localhost:27018 heartbeat succeeded: { ... reply ... }
99103
server changed: localhost:27018 Unknown -> RSSecondary
@@ -103,6 +107,8 @@ This example program prints something like:
103107
new servers:
104108
RSPrimary localhost:27017
105109
RSSecondary localhost:27018
110+
secondary AVAILABLE
111+
primary AVAILABLE
106112
localhost:27019 heartbeat succeeded: { ... reply ... }
107113
server changed: localhost:27019 Unknown -> RSSecondary
108114
topology changed: ReplicaSetWithPrimary -> ReplicaSetWithPrimary
@@ -113,6 +119,8 @@ This example program prints something like:
113119
RSPrimary localhost:27017
114120
RSSecondary localhost:27018
115121
RSSecondary localhost:27019
122+
secondary AVAILABLE
123+
primary AVAILABLE
116124
topology closed
117125
118126
Events:
@@ -126,7 +134,7 @@ This example program prints something like:
126134
heartbeat succeeded: 3
127135
heartbeat failed: 0
128136
129-
The driver discovers the third member, "localhost:27019", and adds it to the topology.
137+
The driver connects to the mongods on ports 27017 and 27018, which were specified in the URI, and determines which is primary. It also discovers the third member, "localhost:27019", and adds it to the topology.
130138

131139
.. only:: html
132140

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use this function in a topology-changed callback registered with :symbol:`mongoc_apm_set_topology_changed_cb`. For historical reasons, the :symbol:`mongoc_topology_description_t` passed to the callback is a const pointer, you must cast away const to pass the pointer to |td-func|.

src/libmongoc/doc/mongoc_topology_description_has_readable_server.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Determines if the topology has a readable server available.
1616
Servers are filtered by the given read preferences only if the driver is connected to a replica set, otherwise the read preferences are ignored.
1717
This function uses the driver's current knowledge of the state of the MongoDB server or servers it is connected to; it does no I/O and it does not block.
1818

19+
.. |td-func| replace:: ``mongoc_topology_description_has_readable_server``
20+
21+
.. include:: includes/cast-away-td-const.txt
22+
1923
Parameters
2024
----------
2125

src/libmongoc/doc/mongoc_topology_description_has_writable_server.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Synopsis
1414
1515
Determines if the topology has a writable server available, such as a primary, mongos, or standalone. This function uses the driver's current knowledge of the state of the MongoDB server or servers it is connected to; it does no I/O and it does not block.
1616

17+
.. |td-func| replace:: ``mongoc_topology_description_has_writable_server``
18+
19+
.. include:: includes/cast-away-td-const.txt
20+
1721
Parameters
1822
----------
1923

src/libmongoc/examples/example-sdam-monitoring.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ topology_changed (const mongoc_apm_topology_changed_t *event)
7676
mongoc_server_description_t **new_sds;
7777
size_t n_new_sds;
7878
size_t i;
79+
mongoc_read_prefs_t *prefs;
7980

8081
context = (stats_t *) mongoc_apm_topology_changed_get_context (event);
8182
context->topology_changed_events++;
@@ -107,6 +108,24 @@ topology_changed (const mongoc_apm_topology_changed_t *event)
107108
}
108109
}
109110

111+
prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
112+
113+
/* it is safe, and unfortunately necessary, to cast away const here */
114+
if (mongoc_topology_description_has_readable_server (
115+
(mongoc_topology_description_t *) new_td, prefs)) {
116+
printf (" secondary AVAILABLE\n");
117+
} else {
118+
printf (" secondary UNAVAILABLE\n");
119+
}
120+
121+
if (mongoc_topology_description_has_writable_server (
122+
(mongoc_topology_description_t *) new_td)) {
123+
printf (" primary AVAILABLE\n");
124+
} else {
125+
printf (" primary UNAVAILABLE\n");
126+
}
127+
128+
mongoc_read_prefs_destroy (prefs);
110129
mongoc_server_descriptions_destroy_all (prev_sds, n_prev_sds);
111130
mongoc_server_descriptions_destroy_all (new_sds, n_new_sds);
112131
}

0 commit comments

Comments
 (0)