diff options
author | Marcos José Grillo Ramírez <marcos.grillo@mongodb.com> | 2020-04-24 12:53:56 +0200 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-04-27 14:28:37 +0000 |
commit | 2ea63cd234669d3cfe09292f6f978f7bf768d0d6 (patch) | |
tree | 7ed8519b435d968323a878bceb3cab52685420a2 /src/mongo/db | |
parent | 7fa85b78aab68e1658d1902d43077449e532b1f1 (diff) | |
download | mongo-2ea63cd234669d3cfe09292f6f978f7bf768d0d6.tar.gz |
SERVER-47477 Report failures on refresh and remove in sessions collection sharded
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/SConscript | 22 | ||||
-rw-r--r-- | src/mongo/db/logical_session_cache_factory_mongod.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/sessions_collection_config_server.h | 2 | ||||
-rw-r--r-- | src/mongo/db/sessions_collection_sharded.cpp | 211 | ||||
-rw-r--r-- | src/mongo/db/sessions_collection_sharded.h | 83 |
5 files changed, 5 insertions, 315 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index 186c683abfa..561122013e4 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -1333,22 +1333,6 @@ env.Library( ) env.Library( - target='sessions_collection_sharded', - source=[ - 'sessions_collection_sharded.cpp', - ], - LIBDEPS=[ - '$BUILD_DIR/mongo/base', - 'logical_session_id', - 'sessions_collection', - ], - LIBDEPS_PRIVATE=[ - '$BUILD_DIR/mongo/s/sharding_api', - 'sessions_collection_rs', - ], -) - -env.Library( target='sessions_collection_config_server', source=[ 'sessions_collection_config_server.cpp', @@ -1356,9 +1340,9 @@ env.Library( LIBDEPS=[ '$BUILD_DIR/mongo/s/client/sharding_client', '$BUILD_DIR/mongo/s/coreshard', + '$BUILD_DIR/mongo/s/sessions_collection_sharded', 'dbdirectclient', - 'sessions_collection', - 'sessions_collection_sharded', + 'sessions_collection' ], LIBDEPS_PRIVATE=[ '$BUILD_DIR/mongo/s/sharding_router_api', @@ -1431,12 +1415,12 @@ envWithAsio.Library( 'logical_session_cache_factory_mongod.cpp', ], LIBDEPS=[ + '$BUILD_DIR/mongo/s/sessions_collection_sharded', 'logical_session_cache', 'logical_session_cache_impl', 'service_liaison_mongod', 'sessions_collection_config_server', 'sessions_collection_rs', - 'sessions_collection_sharded', 'sessions_collection_standalone', ], LIBDEPS_PRIVATE=[ diff --git a/src/mongo/db/logical_session_cache_factory_mongod.cpp b/src/mongo/db/logical_session_cache_factory_mongod.cpp index 0bde403ab26..d95858462ef 100644 --- a/src/mongo/db/logical_session_cache_factory_mongod.cpp +++ b/src/mongo/db/logical_session_cache_factory_mongod.cpp @@ -40,8 +40,8 @@ #include "mongo/db/session_catalog_mongod.h" #include "mongo/db/sessions_collection_config_server.h" #include "mongo/db/sessions_collection_rs.h" -#include "mongo/db/sessions_collection_sharded.h" #include "mongo/db/sessions_collection_standalone.h" +#include "mongo/s/sessions_collection_sharded.h" namespace mongo { diff --git a/src/mongo/db/sessions_collection_config_server.h b/src/mongo/db/sessions_collection_config_server.h index 2deeac82549..9f09df55117 100644 --- a/src/mongo/db/sessions_collection_config_server.h +++ b/src/mongo/db/sessions_collection_config_server.h @@ -29,8 +29,8 @@ #pragma once -#include "mongo/db/sessions_collection_sharded.h" #include "mongo/platform/mutex.h" +#include "mongo/s/sessions_collection_sharded.h" namespace mongo { diff --git a/src/mongo/db/sessions_collection_sharded.cpp b/src/mongo/db/sessions_collection_sharded.cpp deleted file mode 100644 index df1bb8c54dd..00000000000 --- a/src/mongo/db/sessions_collection_sharded.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#include "mongo/platform/basic.h" - -#include "mongo/db/sessions_collection_sharded.h" - -#include "mongo/db/matcher/extensions_callback_noop.h" -#include "mongo/db/operation_context.h" -#include "mongo/db/query/canonical_query.h" -#include "mongo/db/query/query_request.h" -#include "mongo/db/sessions_collection_rs.h" -#include "mongo/rpc/get_status_from_command_result.h" -#include "mongo/rpc/op_msg.h" -#include "mongo/rpc/op_msg_rpc_impls.h" -#include "mongo/s/catalog_cache.h" -#include "mongo/s/client/shard.h" -#include "mongo/s/client/shard_registry.h" -#include "mongo/s/grid.h" -#include "mongo/s/query/cluster_find.h" -#include "mongo/s/write_ops/batch_write_exec.h" -#include "mongo/s/write_ops/batched_command_request.h" -#include "mongo/s/write_ops/batched_command_response.h" -#include "mongo/s/write_ops/cluster_write.h" -#include "mongo/util/assert_util.h" - -namespace mongo { - -namespace { - -BSONObj lsidQuery(const LogicalSessionId& lsid) { - return BSON(LogicalSessionRecord::kIdFieldName << lsid.toBSON()); -} - -} // namespace - -std::vector<LogicalSessionId> SessionsCollectionSharded::_groupSessionIdsByOwningShard( - OperationContext* opCtx, const LogicalSessionIdSet& sessions) { - auto routingInfo = uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo( - opCtx, NamespaceString::kLogicalSessionsNamespace)); - auto cm = routingInfo.cm(); - - uassert(ErrorCodes::NamespaceNotSharded, - str::stream() << "Collection " << NamespaceString::kLogicalSessionsNamespace - << " is not sharded", - cm); - - std::multimap<ShardId, LogicalSessionId> sessionIdsByOwningShard; - for (const auto& session : sessions) { - sessionIdsByOwningShard.emplace( - cm->findIntersectingChunkWithSimpleCollation(session.getId().toBSON()).getShardId(), - session); - } - - std::vector<LogicalSessionId> sessionIdsGroupedByShard; - sessionIdsGroupedByShard.reserve(sessions.size()); - for (auto& session : sessionIdsByOwningShard) { - sessionIdsGroupedByShard.emplace_back(std::move(session.second)); - } - - return sessionIdsGroupedByShard; -} - -std::vector<LogicalSessionRecord> SessionsCollectionSharded::_groupSessionRecordsByOwningShard( - OperationContext* opCtx, const LogicalSessionRecordSet& sessions) { - auto routingInfo = uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo( - opCtx, NamespaceString::kLogicalSessionsNamespace)); - auto cm = routingInfo.cm(); - - uassert(ErrorCodes::NamespaceNotSharded, - str::stream() << "Collection " << NamespaceString::kLogicalSessionsNamespace - << " is not sharded", - cm); - - std::multimap<ShardId, LogicalSessionRecord> sessionsByOwningShard; - for (const auto& session : sessions) { - sessionsByOwningShard.emplace( - cm->findIntersectingChunkWithSimpleCollation(session.getId().toBSON()).getShardId(), - session); - } - - std::vector<LogicalSessionRecord> sessionRecordsGroupedByShard; - sessionRecordsGroupedByShard.reserve(sessions.size()); - for (auto& session : sessionsByOwningShard) { - sessionRecordsGroupedByShard.emplace_back(std::move(session.second)); - } - - return sessionRecordsGroupedByShard; -} - -void SessionsCollectionSharded::setupSessionsCollection(OperationContext* opCtx) { - checkSessionsCollectionExists(opCtx); -} - -void SessionsCollectionSharded::checkSessionsCollectionExists(OperationContext* opCtx) { - uassert(ErrorCodes::ShardingStateNotInitialized, - "sharding state is not yet initialized", - Grid::get(opCtx)->isShardingInitialized()); - - // If the collection doesn't exist, fail. Only the config servers generate it. - const auto routingInfo = uassertStatusOK( - Grid::get(opCtx)->catalogCache()->getShardedCollectionRoutingInfoWithRefresh( - opCtx, NamespaceString::kLogicalSessionsNamespace)); - - uassert( - ErrorCodes::NamespaceNotFound, "config.system.sessions does not exist", routingInfo.cm()); -} - -void SessionsCollectionSharded::refreshSessions(OperationContext* opCtx, - const LogicalSessionRecordSet& sessions) { - auto send = [&](BSONObj toSend) { - auto opMsg = - OpMsgRequest::fromDBAndBody(NamespaceString::kLogicalSessionsNamespace.db(), toSend); - auto request = BatchedCommandRequest::parseUpdate(opMsg); - - BatchedCommandResponse response; - BatchWriteExecStats stats; - - ClusterWriter::write(opCtx, request, &stats, &response); - return response.toStatus(); - }; - - _doRefresh(NamespaceString::kLogicalSessionsNamespace, - _groupSessionRecordsByOwningShard(opCtx, sessions), - send); -} - -void SessionsCollectionSharded::removeRecords(OperationContext* opCtx, - const LogicalSessionIdSet& sessions) { - auto send = [&](BSONObj toSend) { - auto opMsg = - OpMsgRequest::fromDBAndBody(NamespaceString::kLogicalSessionsNamespace.db(), toSend); - auto request = BatchedCommandRequest::parseDelete(opMsg); - - BatchedCommandResponse response; - BatchWriteExecStats stats; - - ClusterWriter::write(opCtx, request, &stats, &response); - return response.toStatus(); - }; - - _doRemove(NamespaceString::kLogicalSessionsNamespace, - _groupSessionIdsByOwningShard(opCtx, sessions), - send); -} - -LogicalSessionIdSet SessionsCollectionSharded::findRemovedSessions( - OperationContext* opCtx, const LogicalSessionIdSet& sessions) { - - auto send = [&](BSONObj toSend) -> BSONObj { - auto qr = uassertStatusOK(QueryRequest::makeFromFindCommand( - NamespaceString::kLogicalSessionsNamespace, toSend, false)); - - const boost::intrusive_ptr<ExpressionContext> expCtx; - auto cq = uassertStatusOK( - CanonicalQuery::canonicalize(opCtx, - std::move(qr), - expCtx, - ExtensionsCallbackNoop(), - MatchExpressionParser::kBanAllSpecialFeatures)); - - // Do the work to generate the first batch of results. This blocks waiting to get responses - // from the shard(s). - std::vector<BSONObj> batch; - CursorId cursorId; - cursorId = ClusterFind::runQuery(opCtx, *cq, ReadPreferenceSetting::get(opCtx), &batch); - - rpc::OpMsgReplyBuilder replyBuilder; - CursorResponseBuilder::Options options; - options.isInitialResponse = true; - CursorResponseBuilder firstBatch(&replyBuilder, options); - for (const auto& obj : batch) { - firstBatch.append(obj); - } - firstBatch.done(cursorId, NamespaceString::kLogicalSessionsNamespace.ns()); - - return replyBuilder.releaseBody(); - }; - - return _doFindRemoved(NamespaceString::kLogicalSessionsNamespace, - _groupSessionIdsByOwningShard(opCtx, sessions), - send); -} - -} // namespace mongo diff --git a/src/mongo/db/sessions_collection_sharded.h b/src/mongo/db/sessions_collection_sharded.h deleted file mode 100644 index c43c1b67fa6..00000000000 --- a/src/mongo/db/sessions_collection_sharded.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#pragma once - -#include <memory> - -#include "mongo/db/logical_session_id.h" -#include "mongo/db/sessions_collection.h" -#include "mongo/util/time_support.h" - -namespace mongo { - -class OperationContext; - -/** - * Accesses the sessions collection for mongos and shard servers. - */ -class SessionsCollectionSharded : public SessionsCollection { -public: - /** - * Only ensures that the sessions collection exists, is sharded and has the proper indexes, but - * doesn't do any configuration on its own. This is left to the config server's implementation - * in SessionsCollectionConfigServer. - */ - void setupSessionsCollection(OperationContext* opCtx) override; - - /** - * Checks if the sessions collection exists. Does not check if the index exists in the sharded - * version of this function. - */ - void checkSessionsCollectionExists(OperationContext* opCtx) final; - - void refreshSessions(OperationContext* opCtx, const LogicalSessionRecordSet& sessions) override; - - void removeRecords(OperationContext* opCtx, const LogicalSessionIdSet& sessions) override; - - LogicalSessionIdSet findRemovedSessions(OperationContext* opCtx, - const LogicalSessionIdSet& sessions) override; - -private: - /** - * These two methods use the sharding routing metadata to do a best effort attempt at grouping - * the specified set of sessions by the shards, which have the records for these sessions. This - * is done as an attempt to avoid broadcast queries. - * - * The reason it is 'best effort' is because it makes no attempt at checking whether the routing - * table is up-to-date and just picks up whatever was most recently fetched from the config - * server, which could be stale. - */ - std::vector<LogicalSessionId> _groupSessionIdsByOwningShard( - OperationContext* opCtx, const LogicalSessionIdSet& sessions); - std::vector<LogicalSessionRecord> _groupSessionRecordsByOwningShard( - OperationContext* opCtx, const LogicalSessionRecordSet& sessions); -}; - -} // namespace mongo |