aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/blueprintresolver.cpp
blob: 2a12867dd3328250a6bba5da24f10c05622fe108 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "blueprintresolver.h"
#include "blueprintfactory.h"
#include "featurenameparser.h"
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <stack>
#include <cassert>
#include <set>
#include <thread>

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

using vespalib::make_string_short::fmt;
using vespalib::ThreadStackExecutor;
using vespalib::makeLambdaTask;

namespace search::fef {

namespace {

constexpr int MAX_TRACE_SIZE = BlueprintResolver::MAX_TRACE_SIZE;
constexpr int TRACE_SKIP_POS = 10;

using Accept = Blueprint::AcceptInput;

bool is_compatible(bool is_object, Accept accept_type) {
    return ((accept_type == Accept::ANY) ||
            ((accept_type == Accept::OBJECT) == (is_object)));
}

const char *type_str(bool is_object) {
    return (is_object ? "object" : "number");
}

const char *accept_type_str(Accept accept_type) {
    switch (accept_type) {
    case Accept::NUMBER: return "number";
    case Accept::OBJECT: return "object";
    case Accept::ANY:    return "any";
    }
    return "(not reached)";
}

struct Compiler : public Blueprint::DependencyHandler {
    using ExecutorSpec = BlueprintResolver::ExecutorSpec;
    using ExecutorSpecList = BlueprintResolver::ExecutorSpecList;
    using FeatureRef = BlueprintResolver::FeatureRef;
    using FeatureMap = BlueprintResolver::FeatureMap;

    struct Frame {
        ExecutorSpec spec;
        const FeatureNameParser &parser;
        Frame(Blueprint::SP blueprint, const FeatureNameParser &parser_in)
            : spec(std::move(blueprint)), parser(parser_in) {}
    };
    using Stack = std::vector<Frame>;

    struct FrameGuard {
        Stack &stack;
        explicit FrameGuard(Stack &stack_in) : stack(stack_in) {}
        ~FrameGuard() {
            stack.back().spec.blueprint->detach_dependency_handler();
            stack.pop_back();
        }
    };

    const BlueprintFactory  &factory;
    const IIndexEnvironment &index_env;
    Stack                    resolve_stack;
    ExecutorSpecList        &spec_list;
    FeatureMap              &feature_map;
    std::set<vespalib::string> setup_set;
    std::set<vespalib::string> failed_set;
    const char *min_stack;
    const char *max_stack;

    Compiler(const BlueprintFactory &factory_in,
             const IIndexEnvironment &index_env_in,
             ExecutorSpecList &spec_list_out,
             FeatureMap &feature_map_out)
        : factory(factory_in),
          index_env(index_env_in),
          resolve_stack(),
          spec_list(spec_list_out),
          feature_map(feature_map_out),
          setup_set(),
          failed_set(),
          min_stack(nullptr),
          max_stack(nullptr) {}
    ~Compiler();

    void probe_stack() {
        const char c = 'X';
        min_stack = (min_stack == nullptr) ? &c : std::min(min_stack, &c);
        max_stack = (max_stack == nullptr) ? &c : std::max(max_stack, &c);
    }

    int stack_usage() const {
        return (max_stack - min_stack);
    }

    Frame &self() { return resolve_stack.back(); }
    bool failed() const { return !failed_set.empty(); }

    vespalib::string make_trace(bool skip_self) {
        vespalib::string trace;
        auto pos = resolve_stack.rbegin();
        auto end = resolve_stack.rend();
        if ((pos != end) && skip_self) {
            ++pos;
        }
        size_t i = 0;
        size_t n = (end - pos);
        for (; (pos != end); ++pos, ++i) {
            failed_set.insert(pos->parser.featureName());
            bool should_trace = (n <= MAX_TRACE_SIZE);
            should_trace |= (i < TRACE_SKIP_POS);
            should_trace |= ((end - pos) < (MAX_TRACE_SIZE - TRACE_SKIP_POS));
            if (should_trace) {
                trace += fmt("  ... needed by rank feature '%s'\n", pos->parser.featureName().c_str());
            } else if (i == TRACE_SKIP_POS) {
                trace += fmt("  (skipped %zu entries)\n", (n - MAX_TRACE_SIZE) + 1);
            }
        }
        return trace;
    }

    FeatureRef fail(const vespalib::string &feature_name, const vespalib::string &reason, bool skip_self = false) {
        if (failed_set.count(feature_name) == 0) {
            failed_set.insert(feature_name);
            auto trace = make_trace(skip_self);
            if (trace.empty()) {
                LOG(warning, "invalid rank feature '%s': %s", feature_name.c_str(), reason.c_str());
            } else {
                LOG(warning, "invalid rank feature '%s': %s\n%s", feature_name.c_str(), reason.c_str(), trace.c_str());
            }
        }
        probe_stack();
        return FeatureRef();
    }

    void fail_self(const vespalib::string &reason) {
        fail(self().parser.featureName(), reason, true);
    }

    FeatureRef verify_type(const FeatureNameParser &parser, FeatureRef ref, Accept accept_type) {
        const auto &spec = spec_list[ref.executor];
        bool is_object = spec.output_types[ref.output].is_object();
        if (!is_compatible(is_object, accept_type)) {
            return fail(parser.featureName(),
                        fmt("output '%s' has wrong type: was %s, expected %s",
                            parser.output().c_str(), type_str(is_object), accept_type_str(accept_type)));
        }
        probe_stack();
        return ref;
    }

    void setup_executor(const FeatureNameParser &parser) {
        if (setup_set.count(parser.executorName()) == 0) {
            setup_set.insert(parser.executorName());
            if (Blueprint::SP blueprint = factory.createBlueprint(parser.baseName())) {
                resolve_stack.emplace_back(std::move(blueprint), parser);
                FrameGuard frame_guard(resolve_stack);
                self().spec.blueprint->setName(parser.executorName());
                self().spec.blueprint->attach_dependency_handler(*this);
                if (!self().spec.blueprint->setup(index_env, parser.parameters())) {
                    fail_self("invalid parameters");
                }
                if (parser.output().empty() && self().spec.output_types.empty()) {
                    fail_self("has no output value");
                }
                spec_list.push_back(self().spec); // keep all feature_map refs valid
            } else {
                fail(parser.featureName(), fmt("unknown basename: '%s'", parser.baseName().c_str()));
            }
        }
    }

    FeatureRef resolve_feature(const vespalib::string &feature_name, Accept accept_type) {
        auto parser = std::make_unique<FeatureNameParser>(feature_name);
        if (!parser->valid()) {
            return fail(feature_name, "malformed name");
        }
        if (failed_set.count(parser->featureName()) > 0) {
            return fail(parser->featureName(), "already failed");
        }
        auto old_feature = feature_map.find(parser->featureName());
        if (old_feature != feature_map.end()) {
            return verify_type(*parser, old_feature->second, accept_type);
        }
        if ((resolve_stack.size() + 1) > BlueprintResolver::MAX_DEP_DEPTH) {
            return fail(parser->featureName(), "dependency graph too deep");
        }
        for (const Frame &frame: resolve_stack) {
            if (frame.parser.executorName() == parser->executorName()) {
                return fail(parser->featureName(), "dependency cycle detected");
            }
        }
        setup_executor(*parser);
        auto new_feature = feature_map.find(parser->featureName());
        if (new_feature != feature_map.end()) {
            return verify_type(*parser, new_feature->second, accept_type);
        }
        return fail(parser->featureName(), fmt("unknown output: '%s'", parser->output().c_str()));
    }

    std::optional<FeatureType> resolve_input(const vespalib::string &feature_name, Accept accept_type) override {
        assert(self().spec.output_types.empty()); // require: 'resolve inputs' before 'define outputs'
        auto ref = resolve_feature(feature_name, accept_type);
        if (!ref.valid()) {
            // fail silently here to avoid mutiple traces for the same root error
            failed_set.insert(self().parser.featureName());
            return std::nullopt;
        }
        self().spec.inputs.push_back(ref);
        return spec_list[ref.executor].output_types[ref.output];
    }

    void define_output(const vespalib::string &output_name, FeatureType type) override {
        vespalib::string feature_name = self().parser.executorName();
        if (!output_name.empty()) {
            feature_name.push_back('.');
            feature_name.append(output_name);
        }
        FeatureRef output_ref(spec_list.size(), self().spec.output_types.size());
        if (output_ref.output == 0) {
            feature_map.emplace(self().parser.executorName(), output_ref);
        }
        feature_map.emplace(feature_name, output_ref);
        self().spec.output_types.push_back(std::move(type));
    }

    void fail(const vespalib::string &msg) override {
        fail_self(msg);
    }
};

Compiler::~Compiler() = default;

} // namespace search::fef::<unnamed>

BlueprintResolver::ExecutorSpec::ExecutorSpec(Blueprint::SP blueprint_in)
    : blueprint(std::move(blueprint_in)),
      inputs(),
      output_types()
{ }

BlueprintResolver::ExecutorSpec::~ExecutorSpec() = default;
BlueprintResolver::~BlueprintResolver() = default;

BlueprintResolver::BlueprintResolver(const BlueprintFactory &factory,
                                     const IIndexEnvironment &indexEnv)
    : _factory(factory),
      _indexEnv(indexEnv),
      _seeds(),
      _executorSpecs(),
      _featureMap(),
      _seedMap()
{
}

void
BlueprintResolver::addSeed(vespalib::stringref feature)
{
    _seeds.emplace_back(feature);
}

bool
BlueprintResolver::compile()
{
    assert(_executorSpecs.empty()); // only one compilation allowed
    Compiler compiler(_factory, _indexEnv, _executorSpecs, _featureMap);
    auto compile_task = makeLambdaTask([&]() {
                                   compiler.probe_stack();
                                   for (const auto &seed: _seeds) {
                                       auto ref = compiler.resolve_feature(seed, Blueprint::AcceptInput::ANY);
                                       if (compiler.failed()) {
                                           return;
                                       }
                                       _seedMap.emplace(FeatureNameParser(seed).featureName(), ref);
                                   }
                               });
    ThreadStackExecutor executor(1, 8_Mi);
    executor.execute(std::move(compile_task));
    executor.sync();
    executor.shutdown();
    size_t stack_usage = compiler.stack_usage();
    if (stack_usage > (128_Ki)) {
        LOG(warning, "high stack usage: %zu bytes", stack_usage);
    }
    return !compiler.failed();
}

const BlueprintResolver::ExecutorSpecList &
BlueprintResolver::getExecutorSpecs() const
{
    return _executorSpecs;
}

const BlueprintResolver::FeatureMap &
BlueprintResolver::getFeatureMap() const
{
    return _featureMap;
}

const BlueprintResolver::FeatureMap &
BlueprintResolver::getSeedMap() const
{
    return _seedMap;
}

}