Skip to content

Commit 82a294f

Browse files
authored
CDRIVER-835 remove mongoc_client_kill_cursor (#1936)
* remove `mongoc_client_kill_cursor` * remove unused code for `OP_KILL_CURSORS`
1 parent 0b3bb26 commit 82a294f

File tree

10 files changed

+65
-464
lines changed

10 files changed

+65
-464
lines changed

NEWS

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ Instead, the names must be prefixed with the parent directory: `mongoc/mongoc.h`
6161
#include <mongoc/mongoc.h>
6262
```
6363

64+
### `mongoc_client_kill_cursor`
65+
66+
`mongoc_client_kill_cursor` is removed. It did not accept a server identifier. It was only reliable when connected to a single server.
67+
68+
Sending [killCursors](https://www.mongodb.com/docs/manual/reference/command/killCursors/) is not typically needed. `mongoc_cursor_t` manages the cursor lifetime. If needed, use a generic command helper to manually send a `killCursors` command:
69+
70+
```c
71+
bson_t *cmd = BCON_NEW ("killCursors", "coll", "cursors", "[", BCON_INT64 (cursor_id), "]");
72+
bool ok = mongoc_client_command_simple_with_server_id (client, "db", cmd, NULL, server_id, NULL, &error);
73+
if (!ok) {
74+
printf ("Failed to send 'killCursors': %s\n", error.message);
75+
}
76+
bson_destroy (cmd);
77+
```
6478

6579

6680
libmongoc 1.30.2

build/generate-future-functions.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@
201201
param("bson_ptr", "reply"),
202202
param("bson_error_ptr", "error")]),
203203

204-
future_function("void",
205-
"mongoc_client_kill_cursor",
206-
[param("mongoc_client_ptr", "client"),
207-
param("int64_t", "cursor_id")]),
208-
209204
future_function("mongoc_change_stream_ptr",
210205
"mongoc_client_watch",
211206
[param("mongoc_client_ptr", "client"),
@@ -573,7 +568,7 @@ def future_function_name(fn):
573568
# E.g. future_cursor_next().
574569
return 'future' + fn.name[len('mongoc'):]
575570
else:
576-
# E.g. future__mongoc_client_kill_cursor().
571+
# E.g. future_mongoc_client_command_simple().
577572
return 'future_' + fn.name
578573

579574

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 3 additions & 273 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,6 @@
7979
#define MONGOC_LOG_DOMAIN "client"
8080

8181

82-
static void
83-
_mongoc_client_op_killcursors (mongoc_cluster_t *cluster,
84-
mongoc_server_stream_t *server_stream,
85-
int64_t cursor_id,
86-
int64_t operation_id,
87-
const char *db,
88-
const char *collection);
89-
9082
static void
9183
_mongoc_client_killcursors_command (mongoc_cluster_t *cluster,
9284
mongoc_server_stream_t *server_stream,
@@ -2098,6 +2090,8 @@ _mongoc_client_kill_cursor (mongoc_client_t *client,
20982090
ENTRY;
20992091

21002092
BSON_ASSERT_PARAM (client);
2093+
BSON_ASSERT_PARAM (db);
2094+
BSON_ASSERT_PARAM (collection);
21012095
BSON_ASSERT (cursor_id);
21022096

21032097
/* don't attempt reconnect if server unavailable, and ignore errors */
@@ -2108,209 +2102,14 @@ _mongoc_client_kill_cursor (mongoc_client_t *client,
21082102
return;
21092103
}
21102104

2111-
if (db && collection) {
2112-
_mongoc_client_killcursors_command (&client->cluster, server_stream, cursor_id, db, collection, cs);
2113-
} else {
2114-
_mongoc_client_op_killcursors (&client->cluster, server_stream, cursor_id, operation_id, db, collection);
2115-
}
2105+
_mongoc_client_killcursors_command (&client->cluster, server_stream, cursor_id, db, collection, cs);
21162106

21172107
mongoc_server_stream_cleanup (server_stream);
21182108

21192109
EXIT;
21202110
}
21212111

21222112

2123-
static void
2124-
_mongoc_client_monitor_op_killcursors (mongoc_cluster_t *cluster,
2125-
mongoc_server_stream_t *server_stream,
2126-
int64_t cursor_id,
2127-
int64_t operation_id,
2128-
const char *db,
2129-
const char *collection)
2130-
{
2131-
bson_t doc;
2132-
mongoc_apm_command_started_t event;
2133-
2134-
ENTRY;
2135-
2136-
mongoc_client_t *client = cluster->client;
2137-
const mongoc_log_and_monitor_instance_t *log_and_monitor = &client->topology->log_and_monitor;
2138-
2139-
if (!log_and_monitor->apm_callbacks.started) {
2140-
return;
2141-
}
2142-
2143-
bson_init (&doc);
2144-
_mongoc_client_prepare_killcursors_command (cursor_id, collection, &doc);
2145-
mongoc_apm_command_started_init (&event,
2146-
&doc,
2147-
db,
2148-
"killCursors",
2149-
cluster->request_id,
2150-
operation_id,
2151-
&server_stream->sd->host,
2152-
server_stream->sd->id,
2153-
&server_stream->sd->service_id,
2154-
server_stream->sd->server_connection_id,
2155-
NULL,
2156-
log_and_monitor->apm_context);
2157-
2158-
log_and_monitor->apm_callbacks.started (&event);
2159-
mongoc_apm_command_started_cleanup (&event);
2160-
bson_destroy (&doc);
2161-
2162-
EXIT;
2163-
}
2164-
2165-
2166-
static void
2167-
_mongoc_client_monitor_op_killcursors_succeeded (mongoc_cluster_t *cluster,
2168-
int64_t duration,
2169-
mongoc_server_stream_t *server_stream,
2170-
int64_t cursor_id,
2171-
int64_t operation_id,
2172-
const char *db)
2173-
{
2174-
bson_t doc;
2175-
bson_array_builder_t *cursors_unknown;
2176-
mongoc_apm_command_succeeded_t event;
2177-
2178-
ENTRY;
2179-
2180-
mongoc_client_t *client = cluster->client;
2181-
const mongoc_log_and_monitor_instance_t *log_and_monitor = &client->topology->log_and_monitor;
2182-
2183-
if (!log_and_monitor->apm_callbacks.succeeded) {
2184-
EXIT;
2185-
}
2186-
2187-
/* fake server reply to killCursors command: {ok: 1, cursorsUnknown: [42]} */
2188-
bson_init (&doc);
2189-
bson_append_int32 (&doc, "ok", 2, 1);
2190-
bson_append_array_builder_begin (&doc, "cursorsUnknown", 14, &cursors_unknown);
2191-
bson_array_builder_append_int64 (cursors_unknown, cursor_id);
2192-
bson_append_array_builder_end (&doc, cursors_unknown);
2193-
2194-
mongoc_apm_command_succeeded_init (&event,
2195-
duration,
2196-
&doc,
2197-
"killCursors",
2198-
db,
2199-
cluster->request_id,
2200-
operation_id,
2201-
&server_stream->sd->host,
2202-
server_stream->sd->id,
2203-
&server_stream->sd->service_id,
2204-
server_stream->sd->server_connection_id,
2205-
false,
2206-
log_and_monitor->apm_context);
2207-
2208-
log_and_monitor->apm_callbacks.succeeded (&event);
2209-
2210-
mongoc_apm_command_succeeded_cleanup (&event);
2211-
bson_destroy (&doc);
2212-
}
2213-
2214-
2215-
static void
2216-
_mongoc_client_monitor_op_killcursors_failed (mongoc_cluster_t *cluster,
2217-
int64_t duration,
2218-
mongoc_server_stream_t *server_stream,
2219-
const bson_error_t *error,
2220-
int64_t operation_id,
2221-
const char *db)
2222-
{
2223-
bson_t doc;
2224-
mongoc_apm_command_failed_t event;
2225-
2226-
ENTRY;
2227-
2228-
mongoc_client_t *client = cluster->client;
2229-
const mongoc_log_and_monitor_instance_t *log_and_monitor = &client->topology->log_and_monitor;
2230-
2231-
if (!log_and_monitor->apm_callbacks.failed) {
2232-
EXIT;
2233-
}
2234-
2235-
/* fake server reply to killCursors command: {ok: 0} */
2236-
bson_init (&doc);
2237-
bson_append_int32 (&doc, "ok", 2, 0);
2238-
2239-
mongoc_apm_command_failed_init (&event,
2240-
duration,
2241-
"killCursors",
2242-
db,
2243-
error,
2244-
&doc,
2245-
cluster->request_id,
2246-
operation_id,
2247-
&server_stream->sd->host,
2248-
server_stream->sd->id,
2249-
&server_stream->sd->service_id,
2250-
server_stream->sd->server_connection_id,
2251-
false,
2252-
log_and_monitor->apm_context);
2253-
2254-
log_and_monitor->apm_callbacks.failed (&event);
2255-
2256-
mongoc_apm_command_failed_cleanup (&event);
2257-
bson_destroy (&doc);
2258-
}
2259-
2260-
2261-
static void
2262-
_mongoc_client_op_killcursors (mongoc_cluster_t *cluster,
2263-
mongoc_server_stream_t *server_stream,
2264-
int64_t cursor_id,
2265-
int64_t operation_id,
2266-
const char *db,
2267-
const char *collection)
2268-
{
2269-
BSON_ASSERT_PARAM (cluster);
2270-
BSON_ASSERT_PARAM (server_stream);
2271-
BSON_OPTIONAL_PARAM (db);
2272-
BSON_OPTIONAL_PARAM (collection);
2273-
2274-
const bool has_ns = db && collection;
2275-
const int64_t started = bson_get_monotonic_time ();
2276-
2277-
mcd_rpc_message *const rpc = mcd_rpc_message_new ();
2278-
2279-
{
2280-
int32_t message_length = 0;
2281-
2282-
message_length += mcd_rpc_header_set_message_length (rpc, 0);
2283-
message_length += mcd_rpc_header_set_request_id (rpc, ++cluster->request_id);
2284-
message_length += mcd_rpc_header_set_response_to (rpc, 0);
2285-
message_length += mcd_rpc_header_set_op_code (rpc, MONGOC_OP_CODE_KILL_CURSORS);
2286-
2287-
message_length += sizeof (int32_t); // ZERO
2288-
message_length += mcd_rpc_op_kill_cursors_set_cursor_ids (rpc, &cursor_id, 1);
2289-
2290-
mcd_rpc_message_set_length (rpc, message_length);
2291-
}
2292-
2293-
if (has_ns) {
2294-
_mongoc_client_monitor_op_killcursors (cluster, server_stream, cursor_id, operation_id, db, collection);
2295-
}
2296-
2297-
bson_error_t error;
2298-
const bool res = mongoc_cluster_legacy_rpc_sendv_to_server (cluster, rpc, server_stream, &error);
2299-
2300-
if (has_ns) {
2301-
if (res) {
2302-
_mongoc_client_monitor_op_killcursors_succeeded (
2303-
cluster, bson_get_monotonic_time () - started, server_stream, cursor_id, operation_id, db);
2304-
} else {
2305-
_mongoc_client_monitor_op_killcursors_failed (
2306-
cluster, bson_get_monotonic_time () - started, server_stream, &error, operation_id, db);
2307-
}
2308-
}
2309-
2310-
mcd_rpc_message_destroy (rpc);
2311-
}
2312-
2313-
23142113
static void
23152114
_mongoc_client_killcursors_command (mongoc_cluster_t *cluster,
23162115
mongoc_server_stream_t *server_stream,
@@ -2343,75 +2142,6 @@ _mongoc_client_killcursors_command (mongoc_cluster_t *cluster,
23432142
}
23442143

23452144

2346-
/*
2347-
*--------------------------------------------------------------------------
2348-
*
2349-
* mongoc_client_kill_cursor --
2350-
*
2351-
* Destroy a cursor on the server.
2352-
*
2353-
* NOTE: this is only reliable when connected to a single mongod or
2354-
* mongos. If connected to a replica set, the driver attempts to
2355-
* kill the cursor on the primary. If connected to multiple mongoses
2356-
* the kill-cursors message is sent to a *random* mongos.
2357-
*
2358-
* If no primary, mongos, or standalone server is known, return
2359-
* without attempting to reconnect.
2360-
*
2361-
* Returns:
2362-
* None.
2363-
*
2364-
* Side effects:
2365-
* None.
2366-
*
2367-
*--------------------------------------------------------------------------
2368-
*/
2369-
2370-
void
2371-
mongoc_client_kill_cursor (mongoc_client_t *client, int64_t cursor_id)
2372-
{
2373-
BSON_ASSERT_PARAM (client);
2374-
2375-
mongoc_topology_t *const topology = BSON_ASSERT_PTR_INLINE (client)->topology;
2376-
mongoc_server_description_t const *selected_server;
2377-
mongoc_read_prefs_t *read_prefs;
2378-
bson_error_t error;
2379-
uint32_t server_id = 0;
2380-
mc_shared_tpld td = mc_tpld_take_ref (topology);
2381-
2382-
read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
2383-
2384-
if (!mongoc_topology_compatible (td.ptr, NULL, &error)) {
2385-
MONGOC_ERROR ("Could not kill cursor: %s", error.message);
2386-
mc_tpld_drop_ref (&td);
2387-
mongoc_read_prefs_destroy (read_prefs);
2388-
return;
2389-
}
2390-
2391-
/* see if there's a known writable server - do no I/O or retries */
2392-
selected_server = mongoc_topology_description_select (td.ptr,
2393-
MONGOC_SS_WRITE,
2394-
read_prefs,
2395-
NULL /* chosen read mode */,
2396-
NULL /* deprioritized servers */,
2397-
topology->local_threshold_msec);
2398-
2399-
if (selected_server) {
2400-
server_id = selected_server->id;
2401-
}
2402-
2403-
if (server_id) {
2404-
_mongoc_client_kill_cursor (
2405-
client, server_id, cursor_id, 0 /* operation_id */, NULL /* db */, NULL /* collection */, NULL /* session */);
2406-
} else {
2407-
MONGOC_INFO ("No server available for mongoc_client_kill_cursor");
2408-
}
2409-
2410-
mongoc_read_prefs_destroy (read_prefs);
2411-
mc_tpld_drop_ref (&td);
2412-
}
2413-
2414-
24152145
char **
24162146
mongoc_client_get_database_names (mongoc_client_t *client, bson_error_t *error)
24172147
{

src/libmongoc/src/mongoc/mongoc-client.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ mongoc_client_get_uri (const mongoc_client_t *client);
120120
MONGOC_EXPORT (void)
121121
mongoc_client_set_stream_initiator (mongoc_client_t *client, mongoc_stream_initiator_t initiator, void *user_data);
122122

123-
124-
BSON_DEPRECATED ("mongoc_client_kill_cursor is deprecated")
125-
MONGOC_EXPORT (void) mongoc_client_kill_cursor (mongoc_client_t *client, int64_t cursor_id);
126-
127-
128123
MONGOC_EXPORT (bool)
129124
mongoc_client_command_simple (mongoc_client_t *client,
130125
const char *db_name,

0 commit comments

Comments
 (0)