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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "custompolicy.h"
#include "simpleprotocol.h"
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/routing/routingcontext.h>
#include <vespa/vespalib/text/stringtokenizer.h>
#include <vespa/log/log.h>
LOG_SETUP(".custompolicy");
namespace mbus {
CustomPolicy::CustomPolicy(bool selectOnRetry,
std::vector<uint32_t> consumableErrors,
std::vector<Route> routes) :
_selectOnRetry(selectOnRetry),
_consumableErrors(std::move(consumableErrors)),
_routes(std::move(routes))
{
}
CustomPolicy::~CustomPolicy() = default;
void
CustomPolicy::select(RoutingContext &context)
{
string str = "Selecting { ";
for (uint32_t i = 0; i < _routes.size(); ++i) {
str.append("'");
str.append(_routes[i].toString());
str.append("'");
if (i < _routes.size() - 1) {
str.append(", ");
}
}
str.append(" }.");
context.trace(1, str);
context.setSelectOnRetry(_selectOnRetry);
for (uint32_t consumableError : _consumableErrors) {
context.addConsumableError(consumableError);
}
context.addChildren(_routes);
}
void
CustomPolicy::merge(RoutingContext &context)
{
Reply::UP ret(new EmptyReply());
std::vector<string> routes;
for (RoutingNodeIterator it = context.getChildIterator();
it.isValid(); it.next())
{
routes.push_back(it.getRoute().toString());
const Reply &reply = it.getReplyRef();
for (uint32_t i = 0; i < reply.getNumErrors(); ++i) {
ret->addError(reply.getError(i));
}
}
context.setReply(std::move(ret));
string str = "Merged { ";
for (uint32_t i = 0; i < routes.size(); ++i) {
str.append("'");
str.append(routes[i]);
str.append("'");
if (i < _routes.size() - 1) {
str.append(", ");
}
}
str.append(" }.");
context.trace(1, str);
}
CustomPolicyFactory::CustomPolicyFactory(bool selectOnRetry) noexcept :
_selectOnRetry(selectOnRetry),
_consumableErrors()
{
}
CustomPolicyFactory::CustomPolicyFactory(bool selectOnRetry, uint32_t consumableError) :
_selectOnRetry(selectOnRetry),
_consumableErrors()
{
_consumableErrors.push_back(consumableError);
}
CustomPolicyFactory::CustomPolicyFactory(bool selectOnRetry, std::vector<uint32_t> consumableErrors) :
_selectOnRetry(selectOnRetry),
_consumableErrors(std::move(consumableErrors))
{
}
CustomPolicyFactory::~CustomPolicyFactory() = default;
IRoutingPolicy::UP
CustomPolicyFactory::create(const string ¶m)
{
string str = "{ ";
for (uint32_t i = 0; i < _consumableErrors.size(); ++i) {
str.append(ErrorCode::getName(_consumableErrors[i]));
if (i < _consumableErrors.size() - 1) {
str.append(", ");
}
}
str.append(" }");
LOG(info, "Creating custom policy; selectOnRetry = %d, consumableErrors = %s, param = '%s'.",
_selectOnRetry, str.c_str(), param.c_str());
return std::make_unique<CustomPolicy>(_selectOnRetry, _consumableErrors, parseRoutes(param));
}
std::vector<Route>
CustomPolicyFactory::parseRoutes(const string &str)
{
std::vector<Route> routes;
vespalib::StringTokenizer tokenizer(str, ",", "");
for (const auto & token : tokenizer) {
routes.emplace_back(Route::parse(token));
}
return routes;
}
} // namespace mbus
|