aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/transactionlogmanagerbase.cpp
blob: 55e82051de6ebb47c39b48093f24e4edb8b42328 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "transactionlogmanagerbase.h"
#include <vespa/searchlib/transactionlog/translogclient.h>
#include <vespa/vespalib/util/stringfmt.h>

#include <vespa/log/log.h>
LOG_SETUP(".proton.server.transactionlogmanagerbase");

using search::transactionlog::client::Visitor;

namespace proton {


TransactionLogManagerBase::TransactionLogManagerBase(FNET_Transport & transport,
                                                     const vespalib::string &tlsSpec,
                                                     const vespalib::string &domainName) :
    _tlc(std::make_unique<TransLogClient>(transport, tlsSpec)),
    _tlcSession(),
    _domainName(domainName),
    _replayLock(),
    _replayCond(),
    _replayDone(false),
    _replayStarted(false),
    _replayStopWatch()
{
}

TransactionLogManagerBase::~TransactionLogManagerBase() = default;

TransactionLogManagerBase::StatusResult
TransactionLogManagerBase::init()
{
    std::unique_ptr<Session> session = _tlc->open(_domainName);
    if ( ! session) {
        if (!_tlc->create(_domainName)) {
            vespalib::string str = vespalib::make_string(
                    "Failed creating domain '%s' on TLS '%s'",
                    _domainName.c_str(), _tlc->getRPCTarget().c_str());
            throw std::runtime_error(str);
        }
        LOG(debug, "Created domain '%s' on TLS '%s'",
            _domainName.c_str(), _tlc->getRPCTarget().c_str());
        session = _tlc->open(_domainName);
        if ( ! session) {
            vespalib::string str = vespalib::make_string(
                    "Could not open session for domain '%s' on TLS '%s'",
                    _domainName.c_str(), _tlc->getRPCTarget().c_str());
            throw std::runtime_error(str);
        }
    }
    LOG(debug, "Opened domain '%s' on TLS '%s'",
        _domainName.c_str(), _tlc->getRPCTarget().c_str());
    StatusResult res;
    if (!session->status(res.serialBegin, res.serialEnd, res.count)) {
        vespalib::string str = vespalib::make_string(
                "Could not get status from session with domain '%s' on TLS '%s'",
                _domainName.c_str(), _tlc->getRPCTarget().c_str());
        throw std::runtime_error(str);
    }
    LOG(debug,
        "Status for domain '%s': serialBegin(%" PRIu64 "), serialEnd(%" PRIu64 "), count(%zu)",
        _domainName.c_str(), res.serialBegin, res.serialEnd, res.count);
    _tlcSession = std::move(session);
    return res;
}

void
TransactionLogManagerBase::internalStartReplay()
{
    std::lock_guard<std::mutex> guard(_replayLock);
    _replayStarted = true;
    _replayDone = false;
    _replayStopWatch = vespalib::Timer();
}

void
TransactionLogManagerBase::changeReplayDone()
{
    std::lock_guard<std::mutex> guard(_replayLock);
    _replayDone = true;
    _replayCond.notify_all();
}

void
TransactionLogManagerBase::waitForReplayDone() const
{
    std::unique_lock<std::mutex> guard(_replayLock);
    while (_replayStarted && !_replayDone) {
        _replayCond.wait(guard);
    }
}

void
TransactionLogManagerBase::close()
{
    if (_tlcSession) {
        _tlcSession->close();
    }
    // Delay destruction until replay is not active.
    waitForReplayDone();
    if (_tlcSession) {
        _tlcSession->clear();
    }
}

std::unique_ptr<Visitor>
TransactionLogManagerBase::createTlcVisitor(Callback &callback) {
    return _tlc->createVisitor(_domainName, callback);
}

bool
TransactionLogManagerBase::getReplayDone() const {
    std::lock_guard<std::mutex> guard(_replayLock);
    return _replayDone;
}

bool
TransactionLogManagerBase::isDoingReplay() const {
    std::lock_guard<std::mutex> guard(_replayLock);
    return _replayStarted && !_replayDone;
}

void
TransactionLogManagerBase::logReplayComplete() const {
    doLogReplayComplete(_domainName, _replayStopWatch.elapsed());
}

const vespalib::string &
TransactionLogManagerBase::getRpcTarget() const {
    return _tlc->getRPCTarget();
}

} // namespace proton