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

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

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

using search::transactionlog::TransLogClient;

namespace proton {


TransactionLogManagerBase::TransactionLogManagerBase(
        const vespalib::string &tlsSpec, const vespalib::string &domainName) :
    _tlc(tlsSpec),
    _tlcSession(),
    _domainName(domainName),
    _replayLock(),
    _replayCond(),
    _replayDone(false),
    _replayStarted(false),
    _replayStartTime(0)
{
}

TransactionLogManagerBase::~TransactionLogManagerBase() = default;

TransactionLogManagerBase::StatusResult
TransactionLogManagerBase::init()
{
    TransLogClient::Session::UP session = _tlc.open(_domainName);
    if (session.get() == NULL) {
        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.get() == NULL) {
            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;
    FastOS_Time timer;
    timer.SetNow();
    _replayStartTime = timer.MilliSecs();
}

void
TransactionLogManagerBase::markReplayStarted()
{
    std::lock_guard<std::mutex> guard(_replayLock);
    _replayStarted = true;
}

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.get() != NULL) {
        _tlcSession->close();
    }
    // Delay destruction until replay is not active.
    waitForReplayDone();
    if (_tlcSession.get() != NULL) {
        _tlcSession->clear();
    }
}

TransLogClient::Visitor::UP TransactionLogManagerBase::createTlcVisitor(
        TransLogClient::Session::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 {
    FastOS_Time timer;
    timer.SetMilliSecs(_replayStartTime);
    doLogReplayComplete(_domainName, static_cast<int64_t>(timer.MilliSecsToNow()));
}

} // namespace proton