Skip to content

Instantly share code, notes, and snippets.

@kevinAlbs
Last active March 10, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinAlbs/0438b175d6f5c89581145c053488b5f9 to your computer and use it in GitHub Desktop.
Save kevinAlbs/0438b175d6f5c89581145c053488b5f9 to your computer and use it in GitHub Desktop.
Requires internals of libmongoc, replace example-client.c
#include <mongoc/mongoc.h>
#include <stdio.h>
#include <stdlib.h>
#include <mongoc/mongoc-cursor-private.h>
#include <mongoc/mongoc-client-private.h>
#include <mongoc/mongoc-topology-private.h>
int
main (int argc, char *argv[])
{
mongoc_client_pool_t *pool;
mongoc_uri_t *uri;
mongoc_init();
uri = mongoc_uri_new ("mongodb://localhost:27017/?retryWrites=false&retryReads=false");
pool = mongoc_client_pool_new (uri);
mongoc_client_t *client = mongoc_client_pool_pop (pool);
bson_t empty_doc = BSON_INITIALIZER;
mongoc_cursor_t *cursor;
mongoc_collection_t *collection;
mongoc_read_concern_t *rc;
const bson_t *doc;
bson_error_t error;
collection = mongoc_client_get_collection (client, "test", "test");
rc = mongoc_read_concern_new ();
mongoc_read_concern_set_level (rc, MONGOC_READ_CONCERN_LEVEL_MAJORITY);
mongoc_collection_set_read_concern (collection, rc);
printf ("initializing agg, this ties cursor to the server, but does not send the command\n");
cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &empty_doc, NULL /* opts */, NULL /* read_prefs */);
if (mongoc_cursor_error (cursor, &error)) {
printf ("agg error: %s\n", error.message);
}
printf ("simulating a server being marked as UNKNOWN\n");
mongoc_topology_invalidate_server (client->topology, cursor->server_id, &error);
printf ("done\n");
printf ("sending aggregate command\n");
mongoc_cursor_next (cursor, &doc);
if (mongoc_cursor_error (cursor, &error)) {
printf ("error: %s\n", error.message);
}
bson_destroy (&empty_doc);
mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (collection);
mongoc_client_pool_push (pool, client);
mongoc_client_pool_destroy (pool);
mongoc_uri_destroy (uri);
mongoc_cleanup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment