[CDRIVER-2809] Pass non-const event structs to APM callbacks Created: 29/Aug/18  Updated: 16/Oct/23

Status: Backlog
Project: C Driver
Component/s: libmongoc
Affects Version/s: None
Fix Version/s: 2.0.0

Type: Improvement Priority: Major - P3
Reporter: A. Jesse Jiryu Davis Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: HTML File patch    
Epic Link: CDRIVER-4742

 Description   

The event structs passed to SDAM monitoring callbacks are const, e.g.:

typedef void (*mongoc_apm_topology_changed_cb_t) (
   const mongoc_apm_topology_changed_t *event); 

Furthermore, the event accessors give const pointers:

MONGOC_EXPORT (const mongoc_topology_description_t *)
mongoc_apm_topology_changed_get_previous_description (
   const mongoc_apm_topology_changed_t *event);

However, mongoc_topology_description_has_readable_server / writable server take non-const mongoc_topology_description_t pointers, because they must perform server selection which is a non-const operation on the topology description.

Calling mongoc_topology_description_has_readable_server / writable_server on the topology description provided to an SDAM monitoring callback is a documented use case in the SDAM monitoring spec, it shouldn't require the user to cast away const.

We can de-const some types without breaking compatibility.

Since we provide no public setter functions for any event structs, we could de-const them all without changing semantics significantly, and keep all the callback signatures consistent. Let's consider doing that.



 Comments   
Comment by Githook User [ 10/Sep/18 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'email': 'jesse@mongodb.com', 'username': 'ajdavis'}

Message: CDRIVER-2809 topology listeners must cast away const
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/92be0ec81882a9706fbf2303042564e93d9baed0

Comment by A. Jesse Jiryu Davis [ 07/Sep/18 ]

This can't be done until 2.0, since changing the type of the callback functions is backward-breaking. Let's say I updated this typedef in mongoc-apm.h to make the pointer to the event struct non-const:

typedef void (*mongoc_apm_topology_changed_cb_t) (
   mongoc_apm_topology_changed_t *event);

If I have code compatible with the previous typedef, like this:

/* note the "const" qualifier */
static void
topology_changed (const mongoc_apm_topology_changed_t *event);
 
int main() {
   mongoc_apm_callbacks_t *cbs = mongoc_apm_callbacks_new ();
   /* imagine that 
   mongoc_apm_set_topology_changed_cb (cbs, topology_changed);
}

... then the typedef change in the driver results in this warning from Clang:

warning: incompatible pointer types passing 'void (const mongoc_apm_topology_changed_t *)' (aka 'void (const struct _mongoc_apm_topology_changed_t *)') to parameter of type 'mongoc_apm_topology_changed_cb_t' (aka 'void (*)(struct _mongoc_apm_topology_changed_t *)') [-Wincompatible-pointer-types]
   mongoc_apm_set_topology_changed_cb (cbs, topology_changed);

For now, users will have to cast away const to call mongoc_topology_description_has_readable_server / writable server. I'll note this in the docs.

Generated at Wed Feb 07 21:16:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.