aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/vespa/documentapi/messagebus/policies/roundrobinpolicy.cpp
blob: 1bc562103cdeceba39ed50328c62c0c095160060 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "roundrobinpolicy.h"
#include <vespa/documentapi/messagebus/documentprotocol.h>
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/error.h>

namespace documentapi {

RoundRobinPolicy::CacheEntry::CacheEntry() :
    _offset(0),
    _generation(),
    _recipients()
{}

RoundRobinPolicy::RoundRobinPolicy(const string &) :
    _lock(),
    _cache()
{}

RoundRobinPolicy::~RoundRobinPolicy() = default;

void
RoundRobinPolicy::select(mbus::RoutingContext &ctx)
{
    mbus::Hop hop = getRecipient(ctx);
    if (hop.hasDirectives()) {
        mbus::Route route = ctx.getRoute();
        route.setHop(0, hop);
        ctx.addChild(route);
    } else {
        mbus::EmptyReply::UP reply(new mbus::EmptyReply());
        reply->addError(mbus::Error(mbus::ErrorCode::NO_ADDRESS_FOR_SERVICE,
                                    "None of the configured recipients are currently available."));
        ctx.setReply(std::move(reply));
    }
}

void
RoundRobinPolicy::merge(mbus::RoutingContext &context)
{
    DocumentProtocol::merge(context);
}

string
RoundRobinPolicy::getCacheKey(const mbus::RoutingContext &ctx) const
{
    string ret;
    for (uint32_t i = 0; i < ctx.getNumRecipients(); ++i) {
        ret.append(ctx.getRecipient(i).getHop(0).toString());
        ret.append(" ");
    }
    return ret;
}

mbus::Hop
RoundRobinPolicy::getRecipient(mbus::RoutingContext &ctx)
{
    std::lock_guard guard(_lock);
    CacheEntry &entry = update(ctx);
    if (entry._recipients.empty()) {
        return mbus::Hop();
    }
    if (++entry._offset >= entry._recipients.size()) {
        entry._offset = 0;
    }
    return entry._recipients[entry._offset];
}

RoundRobinPolicy::CacheEntry &
RoundRobinPolicy::update(mbus::RoutingContext &ctx)
{
    uint32_t upd = ctx.getMirror().updates();
    CacheEntry &entry = _cache.insert(std::map<string, CacheEntry>::value_type(getCacheKey(ctx), CacheEntry())).first->second;
    if (entry._generation != upd) {
        entry._generation = upd;
        entry._recipients.clear();
        for (uint32_t i = 0; i < ctx.getNumRecipients(); ++i)
        {
            slobrok::api::IMirrorAPI::SpecList entries = ctx.getMirror().lookup(ctx.getRecipient(i).getHop(0).toString());
            for (slobrok::api::IMirrorAPI::SpecList::iterator it = entries.begin();
                 it != entries.end(); ++it)
            {
                entry._recipients.push_back(mbus::Hop::parse(it->first));
            }
        }
    }
    return entry;
}

}