aboutsummaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/vespa/searchvisitor/searchenvironment.cpp
blob: 5f4db0fe2d9f16121c7ca35f224c72c5c0ff29e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "searchenvironment.h"
#include "search_environment_snapshot.h"
#include <vespa/vespalib/stllike/hash_map.hpp>
#include <vespa/searchsummary/config/config-juniperrc.h>

#include <vespa/log/log.h>
LOG_SETUP(".visitor.instance.searchenvironment");

using search::docsummary::JuniperProperties;
using vsm::VSMAdapter;

namespace streaming {

__thread SearchEnvironment::EnvMap * SearchEnvironment::_localEnvMap = nullptr;

SearchEnvironment::Env::Env(const config::ConfigUri& configUri, const Fast_NormalizeWordFolder& wf)
    : _configId(configUri.getConfigId()),
      _configurer(std::make_unique<config::SimpleConfigRetriever>(createKeySet(configUri.getConfigId()), configUri.getContext()), this),
      _vsmAdapter(std::make_unique<VSMAdapter>(_configId, wf)),
      _rankManager(std::make_unique<RankManager>(_vsmAdapter.get())),
      _snapshot()
{
    
    _configurer.start();
}

config::ConfigKeySet
SearchEnvironment::Env::createKeySet(const vespalib::string & configId)
{
    config::ConfigKeySet set;
    set.add<vespa::config::search::vsm::VsmfieldsConfig,
            vespa::config::search::SummaryConfig,
            vespa::config::search::vsm::VsmsummaryConfig,
            vespa::config::search::summary::JuniperrcConfig,
            vespa::config::search::RankProfilesConfig>(configId);
    return set;
}

void
SearchEnvironment::Env::configure(const config::ConfigSnapshot & snapshot)
{
    vsm::VSMConfigSnapshot snap(_configId, snapshot);
    _vsmAdapter->configure(snap);
    _rankManager->configure(snap);
    auto se_snapshot = std::make_shared<const SearchEnvironmentSnapshot>(*_rankManager, *_vsmAdapter);
    std::lock_guard guard(_lock);
    std::swap(se_snapshot, _snapshot);
}

std::shared_ptr<const SearchEnvironmentSnapshot>
SearchEnvironment::Env::get_snapshot()
{
    std::lock_guard guard(_lock);
    return _snapshot;
}

SearchEnvironment::Env::~Env()
{
    _configurer.close();
}

SearchEnvironment::SearchEnvironment(const config::ConfigUri & configUri) :
    VisitorEnvironment(),
    _envMap(),
    _configUri(configUri)
{
}


SearchEnvironment::~SearchEnvironment()
{
    std::lock_guard guard(_lock);
    _threadLocals.clear();
}

SearchEnvironment::Env &
SearchEnvironment::getEnv(const vespalib::string & searchCluster)
{
    config::ConfigUri searchClusterUri(_configUri.createWithNewId(searchCluster));
    if (_localEnvMap == nullptr) {
        EnvMapUP envMap = std::make_unique<EnvMap>();
        _localEnvMap = envMap.get();
        std::lock_guard guard(_lock);
        _threadLocals.emplace_back(std::move(envMap));
    }
    EnvMap::iterator localFound = _localEnvMap->find(searchCluster);
    if (localFound == _localEnvMap->end()) {
        std::lock_guard guard(_lock);
        EnvMap::iterator found = _envMap.find(searchCluster);
        if (found == _envMap.end()) {
            LOG(debug, "Init VSMAdapter with config id = '%s'", searchCluster.c_str());
            Env::SP env = std::make_shared<Env>(searchClusterUri, _wordFolder);
            _envMap[searchCluster] = std::move(env);
            found = _envMap.find(searchCluster);
        }
        _localEnvMap->insert(*found);
        localFound = _localEnvMap->find(searchCluster);
    }
    return *localFound->second;
}

void
SearchEnvironment::clear_thread_local_env_map()
{
    _localEnvMap = nullptr;
}

std::shared_ptr<const SearchEnvironmentSnapshot>
SearchEnvironment::get_snapshot(const vespalib::string& search_cluster)
{
    return getEnv(search_cluster).get_snapshot();
}

}