Skip to content

CXX-2785 do not apply readConcern or writeConcern to search index commands #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 252 additions & 0 deletions data/index-management/searchIndexIgnoresReadWriteConcern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
{
"description": "search index operations ignore read and write concern",
"schemaVersion": "1.4",
"createEntities": [
{
"client": {
"id": "client0",
"useMultipleMongoses": false,
"uriOptions": {
"readConcernLevel": "local",
"w": 1
},
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "database0"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "collection0"
}
}
],
"runOnRequirements": [
{
"minServerVersion": "7.0.0",
"topologies": [
"replicaset",
"load-balanced",
"sharded"
],
"serverless": "forbid"
}
],
"tests": [
{
"description": "createSearchIndex ignores read and write concern",
"operations": [
{
"name": "createSearchIndex",
"object": "collection0",
"arguments": {
"model": {
"definition": {
"mappings": {
"dynamic": true
}
}
}
},
"expectError": {
"isError": true,
"errorContains": "Search index commands are only supported with Atlas"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"createSearchIndexes": "collection0",
"indexes": [
{
"definition": {
"mappings": {
"dynamic": true
}
}
}
],
"$db": "database0",
"writeConcern": {
"$$exists": false
},
"readConcern": {
"$$exists": false
}
}
}
}
]
}
]
},
{
"description": "createSearchIndex ignores read and write concern",
"operations": [
{
"name": "createSearchIndexes",
"object": "collection0",
"arguments": {
"models": []
},
"expectError": {
"isError": true,
"errorContains": "Search index commands are only supported with Atlas"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"createSearchIndexes": "collection0",
"indexes": [],
"$db": "database0",
"writeConcern": {
"$$exists": false
},
"readConcern": {
"$$exists": false
}
}
}
}
]
}
]
},
{
"description": "dropSearchIndex ignores read and write concern",
"operations": [
{
"name": "dropSearchIndex",
"object": "collection0",
"arguments": {
"name": "test index"
},
"expectError": {
"isError": true,
"errorContains": "Search index commands are only supported with Atlas"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"dropSearchIndex": "collection0",
"name": "test index",
"$db": "database0",
"writeConcern": {
"$$exists": false
},
"readConcern": {
"$$exists": false
}
}
}
}
]
}
]
},
{
"description": "listSearchIndexes ignores read and write concern",
"operations": [
{
"name": "listSearchIndexes",
"object": "collection0",
"expectError": {
"isError": true,
"errorContains": "Search index commands are only supported with Atlas"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"aggregate": "collection0",
"pipeline": [
{
"$listSearchIndexes": {}
}
],
"writeConcern": {
"$$exists": false
},
"readConcern": {
"$$exists": false
}
}
}
}
]
}
]
},
{
"description": "updateSearchIndex ignores the read and write concern",
"operations": [
{
"name": "updateSearchIndex",
"object": "collection0",
"arguments": {
"name": "test index",
"definition": {}
},
"expectError": {
"isError": true,
"errorContains": "Search index commands are only supported with Atlas"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"updateSearchIndex": "collection0",
"name": "test index",
"definition": {},
"$db": "database0",
"writeConcern": {
"$$exists": false
},
"readConcern": {
"$$exists": false
}
}
}
}
]
}
]
}
]
}
3 changes: 2 additions & 1 deletion data/index-management/test_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ createSearchIndex.json
createSearchIndexes.json
dropSearchIndex.json
listSearchIndexes.json
updateSearchIndex.json
updateSearchIndex.json
searchIndexIgnoresReadWriteConcern.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ inline namespace v_noabi {
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

struct collection_deleter {
void operator()(mongoc_collection_t* ptr) noexcept {
libmongoc::collection_destroy(ptr);
}
};

using collection_ptr = std::unique_ptr<mongoc_collection_t, collection_deleter>;

// `copy_and_apply_default_rw_concerns` copies the mongoc_collection_t and applies a default
// readConcern and writeConcern. Used to prevent sending a readConcern or writeConcern.
static collection_ptr copy_and_apply_default_rw_concerns(mongoc_collection_t* coll) {
auto* wc_default = libmongoc::write_concern_new();
auto* rc_default = libmongoc::read_concern_new();
auto coll_copy = libmongoc::collection_copy(coll);

mongoc_collection_set_read_concern(coll_copy, rc_default);
mongoc_collection_set_write_concern(coll_copy, wc_default);

libmongoc::read_concern_destroy(rc_default);
libmongoc::write_concern_destroy(wc_default);

return collection_ptr(coll_copy);
}

class search_index_view::impl {
public:
impl(mongoc_collection_t* collection, mongoc_client_t* client)
Expand Down Expand Up @@ -56,8 +80,9 @@ class search_index_view::impl {

libbson::scoped_bson_t opts_bson(opts_doc.view());

auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
return libmongoc::collection_aggregate(
_coll, mongoc_query_flags_t(), stages.bson(), opts_bson.bson(), rp_ptr);
coll_copy.get(), mongoc_query_flags_t(), stages.bson(), opts_bson.bson(), rp_ptr);
}

std::string create_one(const client_session* session, const search_index_model& model) {
Expand Down Expand Up @@ -107,8 +132,9 @@ class search_index_view::impl {
libbson::scoped_bson_t command_bson{command};
libbson::scoped_bson_t opts_bson{opts_doc.view()};

auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
auto result = libmongoc::collection_write_command_with_opts(
_coll, command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
coll_copy.get(), command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);

if (!result) {
throw_exception<operation_exception>(reply.steal(), error);
Expand All @@ -133,8 +159,9 @@ class search_index_view::impl {
libbson::scoped_bson_t opts_bson{opts_doc.view()};
bson_error_t error;

auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
bool result = libmongoc::collection_write_command_with_opts(
_coll, command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
coll_copy.get(), command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);

const uint32_t serverErrorNamespaceNotFound = 26;
if (error.domain == MONGOC_ERROR_QUERY && error.code == serverErrorNamespaceNotFound) {
Expand Down Expand Up @@ -167,8 +194,9 @@ class search_index_view::impl {
libbson::scoped_bson_t opts_bson{opts_doc.view()};
bson_error_t error;

auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
bool result = libmongoc::collection_write_command_with_opts(
_coll, command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
coll_copy.get(), command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);

if (!result) {
throw_exception<operation_exception>(reply.steal(), error);
Expand Down
31 changes: 31 additions & 0 deletions src/mongocxx/test/search_index_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,36 @@ TEST_CASE("atlas search indexes prose tests", "") {

std::cout << "drop one supress namespace not found SUCCESS" << std::endl;
}

SECTION("create and list search indexes with non-default readConcern and writeConcern") {
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());

// Apply non-default write concern.
auto nondefault_wc = mongocxx::write_concern();
nondefault_wc.nodes(1);
coll.write_concern(nondefault_wc);

// Apply non-default read concern.
auto nondefault_rc = mongocxx::read_concern();
nondefault_rc.acknowledge_level(mongocxx::read_concern::level::k_local);
coll.read_concern(nondefault_rc);

auto siv = coll.search_indexes();
auto name = "test-search-index-case6";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model = search_index_model(name, definition.view());

REQUIRE(siv.create_one(name, definition.view()) == "test-search-index-case6");

assert_soon([&siv, &model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model, false);
});

std::cout << "create and list search indexes with non-default readConcern and writeConcern "
"SUCCESS"
<< std::endl;
}
}
} // namespace