From b572b42a0f757e282a4b063ee75c5d21b25d55bd Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 20 Sep 2023 09:21:54 +0000 Subject: Refactor code to make object lifetime easier to follow. --- .../searchcore/proton/matchengine/matchengine.cpp | 62 +++++++++++----------- .../searchcore/proton/matchengine/matchengine.h | 1 + 2 files changed, 33 insertions(+), 30 deletions(-) (limited to 'searchcore') diff --git a/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.cpp b/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.cpp index 5d5ddd62c25..c3f52dafdd6 100644 --- a/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.cpp +++ b/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.cpp @@ -123,43 +123,45 @@ MatchEngine::search(SearchRequest::Source request, SearchClient &client) return performSearch(std::move(request)); } +std::unique_ptr +MatchEngine::doSearch(const search::engine::SearchRequest & searchRequest) { + // 3 is the minimum level required for backend tracing. + searchRequest.setTraceLevel(trace::Level::lookup(searchRequest.propertiesMap.modelOverrides(), + searchRequest.trace().getLevel()), 3); + ISearchHandler::SP searchHandler; + auto threadBundle = _threadBundlePool.getBundle(); + { // try to find the match handler corresponding to the specified search doc type + DocTypeName docTypeName(searchRequest); + std::lock_guard guard(_lock); + searchHandler = _handlers.getHandler(docTypeName); + } + std::unique_ptr ret; + if (searchHandler) { + ret = searchHandler->match(searchRequest, threadBundle.bundle()); + } else { + HandlerMap::Snapshot snapshot; + { + std::lock_guard guard(_lock); + snapshot = _handlers.snapshot(); + } + ret = (snapshot.valid()) + ? snapshot.get()->match(searchRequest, threadBundle.bundle()) // use the first handler + : std::make_unique(); + } + if (searchRequest.expired()) { + vespalib::Issue::report("search request timed out; results may be incomplete"); + } + return ret; +} + std::unique_ptr MatchEngine::performSearch(SearchRequest::Source req) { auto my_issues = std::make_unique(); auto capture_issues = vespalib::Issue::listen(*my_issues); - auto ret = std::make_unique(); - const SearchRequest * searchRequest = req.get(); - if (searchRequest) { - // 3 is the minimum level required for backend tracing. - searchRequest->setTraceLevel(trace::Level::lookup(searchRequest->propertiesMap.modelOverrides(), - searchRequest->trace().getLevel()), 3); - ISearchHandler::SP searchHandler; - vespalib::SimpleThreadBundle::UP threadBundle = _threadBundlePool.obtain(); - { // try to find the match handler corresponding to the specified search doc type - DocTypeName docTypeName(*searchRequest); - std::lock_guard guard(_lock); - searchHandler = _handlers.getHandler(docTypeName); - } - if (searchHandler) { - ret = searchHandler->match(*searchRequest, *threadBundle); - } else { - HandlerMap::Snapshot snapshot; - { - std::lock_guard guard(_lock); - snapshot = _handlers.snapshot(); - } - if (snapshot.valid()) { - ret = snapshot.get()->match(*searchRequest, *threadBundle); // use the first handler - } - } - _threadBundlePool.release(std::move(threadBundle)); - if (searchRequest->expired()) { - vespalib::Issue::report("search request timed out; results may be incomplete"); - } - } + auto ret = (searchRequest) ? doSearch(*searchRequest) : std::make_unique(); ret->request = req.release(); if (_forward_issues) { ret->my_issues = std::move(my_issues); diff --git a/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.h b/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.h index 2979213979f..52ca419c50c 100644 --- a/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.h +++ b/searchcore/src/vespa/searchcore/proton/matchengine/matchengine.h @@ -28,6 +28,7 @@ private: std::atomic _nodeUp; std::atomic _nodeMaintenance; + std::unique_ptr doSearch(const search::engine::SearchRequest & searchRequest); public: /** * Convenience typedefs. -- cgit v1.2.3