aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/persistence/persistenceutil.cpp
blob: 4bee5df4d04782fd164ffd40e0eb141c09f85e84 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "persistenceutil.h"
#include <vespa/config/config.h>
#include <vespa/config/helper/configgetter.hpp>

#include <vespa/log/bufferedlogger.h>
LOG_SETUP(".persistence.util");

namespace storage {
namespace {
    std::string generateName(void* p) {
        std::ostringstream ost;
        ost << "PersistenceUtil(" << p << ")";
        return ost.str();
    }
}

MessageTracker::MessageTracker(FileStorThreadMetrics::Op& metric,
                               framework::Clock& clock)
    : _sendReply(true),
      _metric(metric),
      _result(api::ReturnCode::OK),
      _timer(clock)
{
    _metric.count.inc();
}

MessageTracker::~MessageTracker()
{
    if (_reply.get() && _reply->getResult().success()) {
        _metric.latency.addValue(_timer.getElapsedTimeAsDouble());
    }
}

void
MessageTracker::fail(const ReturnCode& result)
{
    _result = result;
    LOG(debug, "Failing operation with error: %s", _result.toString().c_str());
}

void
MessageTracker::generateReply(api::StorageCommand& cmd)
{
    if (!_sendReply) {
        return;
    }

    if (!_reply.get()) {
        _reply.reset(cmd.makeReply().release());
        _reply->setResult(_result);
    }

    if (!_reply->getResult().success()) {
        _metric.failed.inc();
        LOGBP(debug, "Failed to handle command %s: %s",
              cmd.toString().c_str(),
              _result.toString().c_str());
    }
}

PersistenceUtil::PersistenceUtil(
        const config::ConfigUri & configUri,
        ServiceLayerComponentRegister& compReg,
        FileStorHandler& fileStorHandler,
        FileStorThreadMetrics& metrics,
        uint16_t partition,
        spi::PersistenceProvider& provider)
    : _config(*config::ConfigGetter<vespa::config::content::StorFilestorConfig>::getConfig(configUri.getConfigId(), configUri.getContext())),
      _compReg(compReg),
      _component(compReg, generateName(this)),
      _fileStorHandler(fileStorHandler),
      _partition(partition),
      _nodeIndex(_component.getIndex()),
      _metrics(metrics),
      _bucketFactory(_component.getBucketIdFactory()),
      _repo(_component.getTypeRepo()),
      _spi(provider)
{
}

PersistenceUtil::~PersistenceUtil() { }

void
PersistenceUtil::updateBucketDatabase(const document::Bucket &bucket, const api::BucketInfo& i)
{
    // Update bucket database
    StorBucketDatabase::WrappedEntry entry(getBucketDatabase(bucket.getBucketSpace()).get(bucket.getBucketId(),
                                                                                          "env::updatebucketdb"));
    if (entry.exist()) {
        api::BucketInfo info = i;

        // Don't override last modified unless this is the first bucket
        // info reading.
        if (entry->info.getLastModified() != 0) {
            info.setLastModified(entry->info.getLastModified());
        }
        entry->setBucketInfo(info);
        entry.write();
    } else {
        LOG(debug, "Bucket(%s).getBucketInfo: Bucket does not exist.", bucket.getBucketId().toString().c_str());
    }
}

uint16_t
PersistenceUtil::getPreferredAvailableDisk(const document::Bucket &bucket) const
{
    return _component.getPreferredAvailablePartition(bucket);
}

PersistenceUtil::LockResult
PersistenceUtil::lockAndGetDisk(const document::Bucket &bucket,
                                StorBucketDatabase::Flag flags)
{
    // To lock the bucket, we need to ensure that we don't conflict with
    // bucket disk move command. First we fetch current disk index from
    // bucket DB. When we attempt to lock that lock. And lastly we check
    // the bucket DB again to verify that the bucket is still on that
    // disk after locking it, or we will have to retry on new disk.
    LockResult result;
    result.disk = getPreferredAvailableDisk(bucket);

    while (true) {
        // This function is only called in a context where we require exclusive
        // locking (split/join). Refactor if this no longer the case.
        std::shared_ptr<FileStorHandler::BucketLockInterface> lock(
                _fileStorHandler.lock(bucket, result.disk, api::LockingRequirements::Exclusive));

        // TODO disks are no longer used in practice, can we safely discard this?
        // Might need it for synchronization purposes if something has taken the
        // disk lock _and_ the bucket lock...?
        StorBucketDatabase::WrappedEntry entry(getBucketDatabase(bucket.getBucketSpace()).get(
                bucket.getBucketId(), "join-lockAndGetDisk-1", flags));
        if (entry.exist() && entry->disk != result.disk) {
            result.disk = entry->disk;
            continue;
        }

        result.lock = lock;
        return result;
    }
}

void
PersistenceUtil::setBucketInfo(MessageTracker& tracker, const document::Bucket &bucket)
{
    api::BucketInfo info = getBucketInfo(bucket, _partition);

    static_cast<api::BucketInfoReply&>(*tracker.getReply()).
        setBucketInfo(info);

    updateBucketDatabase(bucket, info);
}

api::BucketInfo
PersistenceUtil::getBucketInfo(const document::Bucket &bucket, int disk) const
{
    if (disk == -1) {
        disk = _partition;
    }

    spi::BucketInfoResult response =
        _spi.getBucketInfo(spi::Bucket(bucket, spi::PartitionId(disk)));

    return convertBucketInfo(response.getBucketInfo());
}

api::BucketInfo
PersistenceUtil::convertBucketInfo(const spi::BucketInfo& info) const
{
   return api::BucketInfo(info.getChecksum(),
                          info.getDocumentCount(),
                          info.getDocumentSize(),
                          info.getEntryCount(),
                          info.getUsedSize(),
                          info.isReady(),
                          info.isActive(), 0);
}

uint32_t
PersistenceUtil::convertErrorCode(const spi::Result& response)
{
    switch (response.getErrorCode()) {
    case spi::Result::NONE:
        return 0;
    case spi::Result::TIMESTAMP_EXISTS:
        return api::ReturnCode::TIMESTAMP_EXIST;
    case spi::Result::TRANSIENT_ERROR:
    case spi::Result::FATAL_ERROR:
        return mbus::ErrorCode::APP_TRANSIENT_ERROR;
    case spi::Result::RESOURCE_EXHAUSTED:
        return api::ReturnCode::NO_SPACE;
    case spi::Result::PERMANENT_ERROR:
    default:
        return mbus::ErrorCode::APP_FATAL_ERROR;
    }

    return 0;
}

void
PersistenceUtil::shutdown(const std::string& reason)
{
    _component.requestShutdown(reason);
}

} // storage