aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/ranking_assets_builder.cpp
blob: 93539233bad6086e2e342c82e2e88049120170c4 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "ranking_assets_builder.h"
#include "onnx_models.h"
#include "ranking_constants.h"
#include "ranking_expressions.h"
#include <vespa/config/common/exceptions.h>
#include <vespa/config/file_acquirer/file_acquirer.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/time.h>
#include <thread>

#include <vespa/log/log.h>
LOG_SETUP(".fef.ranking_assets_builder");


using vespa::config::search::core::OnnxModelsConfig;
using vespa::config::search::core::RankingConstantsConfig;
using vespa::config::search::core::RankingExpressionsConfig;
using vespalib::make_string_short::fmt;

namespace search::fef {

constexpr vespalib::duration file_resolve_timeout = 60min;

RankingAssetsBuilder::RankingAssetsBuilder(FNET_Transport* transport, const vespalib::string& file_distributor_connection_spec)
    : _file_acquirer(),
      _time_box(vespalib::to_s(file_resolve_timeout), 5)
{
    if (transport != nullptr && file_distributor_connection_spec != "") {
        _file_acquirer = std::make_unique<config::RpcFileAcquirer>(*transport, file_distributor_connection_spec);
    }
}

RankingAssetsBuilder::~RankingAssetsBuilder() = default;

vespalib::string
RankingAssetsBuilder::resolve_file(const vespalib::string& desc, const vespalib::string& fileref)
{
    vespalib::string file_path;
    LOG(debug, "Waiting for file acquirer (%s, ref='%s')", desc.c_str(), fileref.c_str());
    while (_time_box.hasTimeLeft() && (file_path == "")) {
        file_path = _file_acquirer->wait_for(fileref, _time_box.timeLeft());
        if (file_path == "") {
            std::this_thread::sleep_for(100ms);
        }
    }
    LOG(debug, "Got file path from file acquirer: '%s' (%s, ref='%s')", file_path.c_str(), desc.c_str(), fileref.c_str());
    if (file_path == "") {
        throw config::ConfigTimeoutException(fmt("could not get file path from file acquirer for %s (ref=%s)",
                                                 desc.c_str(), fileref.c_str()));
    }
    return file_path;
}

std::shared_ptr<const OnnxModels>
RankingAssetsBuilder::build(const OnnxModelsConfig& config)
{
    OnnxModels::Vector models;
    if (_file_acquirer) {
        for (const auto& rc : config.model) {
            auto desc = fmt("name='%s'", rc.name.c_str());
            vespalib::string file_path = resolve_file(desc, rc.fileref);
            models.emplace_back(rc.name, file_path);
            OnnxModels::configure(rc, models.back());
        }
    }
    return std::make_shared<OnnxModels>(std::move(models));
}

std::shared_ptr<const RankingConstants>
RankingAssetsBuilder::build(const RankingConstantsConfig& config)
{
    RankingConstants::Vector constants;
    if (_file_acquirer) {
        for (const auto& rc : config.constant) {
            auto desc = fmt("name='%s', type='%s'", rc.name.c_str(), rc.type.c_str());
            vespalib::string file_path = resolve_file(desc, rc.fileref);
            constants.emplace_back(rc.name, rc.type, file_path);
        }
    }
    return std::make_shared<RankingConstants>(constants);
}

std::shared_ptr<const RankingExpressions>
RankingAssetsBuilder::build(const RankingExpressionsConfig& config)
{
    RankingExpressions expressions;
    if (_file_acquirer) {
        for (const auto& rc : config.expression) {
            auto desc = fmt("name='%s'", rc.name.c_str());
            vespalib::string filePath = resolve_file(desc, rc.fileref);
            expressions.add(rc.name, filePath);
        }
    }
    return std::make_shared<RankingExpressions>(std::move(expressions));
}

}