summaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/frt/frtsource.cpp
blob: 1cb514dcd4dba14e5be2a93628c12eb183c17396 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "frtconfigrequest.h"
#include "frtconfigresponse.h"
#include "frtsource.h"
#include "frtconfigagent.h"
#include "connectionfactory.h"
#include "connection.h"
#include "frtconfigrequestfactory.h"
#include <cassert>

#include <vespa/log/log.h>
LOG_SETUP(".config.frt.frtsource");

namespace config {

class GetConfigTask : public FNET_Task {
public:
    GetConfigTask(FNET_Scheduler * scheduler, FRTSource * source)
        : FNET_Task(scheduler),
          _source(source)
    {
    }
    ~GetConfigTask() {
        Kill();
    }
    void PerformTask() override {
        _source->getConfig();
    }
private:
    FRTSource * _source;
};

FRTSource::FRTSource(std::shared_ptr<ConnectionFactory> connectionFactory, const FRTConfigRequestFactory & requestFactory,
                     std::unique_ptr<ConfigAgent> agent, const ConfigKey & key)
    : _connectionFactory(std::move(connectionFactory)),
      _requestFactory(requestFactory),
      _agent(std::move(agent)),
      _currentRequest(),
      _key(key),
      _lock(),
      _task(std::make_unique<GetConfigTask>(_connectionFactory->getScheduler(), this)),
      _closed(false)
{
    LOG(spam, "New source!");
}

FRTSource::~FRTSource()
{
    LOG(spam, "Destructing source");
    close();
}

void
FRTSource::getConfig()
{
    vespalib::duration serverTimeout = _agent->getTimeout();
    vespalib::duration clientTimeout = serverTimeout + 5s; // The additional 5 seconds is the time allowed for the server to respond after serverTimeout has elapsed.
    Connection * connection = _connectionFactory->getCurrent();
    if (connection == nullptr) {
        LOG(warning, "No connection available - bad config ?");
        return;
    }
    const ConfigState & state(_agent->getConfigState());
 //   LOG(debug, "invoking request with md5 %s, gen %" PRId64 ", servertimeout(%" PRId64 "), client(%f)", state.md5.c_str(), state.generation, serverTimeout, clientTimeout);


    std::unique_ptr<FRTConfigRequest> request = _requestFactory.createConfigRequest(_key, connection, state, serverTimeout);
    FRT_RPCRequest * req = request->getRequest();

    _currentRequest = std::move(request);
    connection->invoke(req, clientTimeout, this);
}


void
FRTSource::RequestDone(FRT_RPCRequest * request)
{
    if (request->GetErrorCode() == FRTE_RPC_ABORT) {
        LOG(debug, "request aborted, stopping");
        return;
    }
    assert(_currentRequest);
    // If this was error from FRT side and nothing to do with config, notify
    // connection about the error.
    if (request->IsError()) {
        _currentRequest->setError(request->GetErrorCode());
    }
    _agent->handleResponse(*_currentRequest, _currentRequest->createResponse(request));
    LOG(spam, "Calling schedule");
    scheduleNextGetConfig();
}

void
FRTSource::close()
{
    {
        std::lock_guard guard(_lock);
        if (_closed)
            return;
        LOG(spam, "Killing task");
        _task->Kill();
    }
    LOG(spam, "Aborting");
    if (_currentRequest.get() != NULL)
        _currentRequest->abort();
    LOG(spam, "Syncing");
    _connectionFactory->syncTransport();
    _currentRequest.reset(0);
    LOG(spam, "closed");
}

void
FRTSource::scheduleNextGetConfig()
{
    std::lock_guard guard(_lock);
    if (_closed)
        return;
    double sec = vespalib::to_s(_agent->getWaitTime());
    LOG(debug, "Scheduling task in %f seconds", sec);
    _task->Schedule(sec);
    LOG(debug, "Done scheduling task");
}

void
FRTSource::reload(int64_t generation)
{
    (void) generation;
}

} // namespace config