aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/frt/frtconfigresponsev3.cpp
blob: 70d6456d433bf6daaddbf3ea5f089e3dba6728cd (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "frtconfigresponsev3.h"
#include "compressioninfo.h"
#include <vespa/fnet/frt/values.h>
#include <vespa/vespalib/data/simple_buffer.h>

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

using namespace vespalib;
using namespace vespalib::slime;
using namespace vespalib::slime::convenience;
using namespace config::protocol;
using namespace config::protocol::v2;
using namespace config::protocol::v3;

namespace config {

std::string make_json(const Slime &slime, bool compact) {
    vespalib::SimpleBuffer buf;
    vespalib::slime::JsonFormat::encode(slime, buf, compact);
    return buf.get().make_string();
}

class V3Payload : public Payload
{
public:
    explicit V3Payload(Slime::UP data) noexcept
        : _data(std::move(data))
    {
    }

    const Inspector & getSlimePayload() const override {
        return _data->get();
    }
private:
    Slime::UP _data;
};

const vespalib::string FRTConfigResponseV3::RESPONSE_TYPES = "sx";

FRTConfigResponseV3::FRTConfigResponseV3(FRT_RPCRequest * request)
    : SlimeConfigResponse(request)
{
}

const vespalib::string &
FRTConfigResponseV3::getResponseTypes() const
{
    return RESPONSE_TYPES;
}

ConfigValue
FRTConfigResponseV3::readConfigValue() const
{
    vespalib::string xxhash64(_data->get()[RESPONSE_CONFIG_XXHASH64].asString().make_string());
    CompressionInfo info;
    info.deserialize(_data->get()[RESPONSE_COMPRESSION_INFO]);
    auto slime = std::make_unique<Slime>();
    DecompressedData data(decompress(((*_returnValues)[1]._data._buf), ((*_returnValues)[1]._data._len), info.compressionType, info.uncompressedSize));
    if (data.memRef.size > 0) {
        size_t consumedSize = JsonFormat::decode(data.memRef, *slime);
        if (consumedSize == 0) {
            std::string json(make_json(*slime, true));
            LOG(error, "Error decoding JSON. Consumed size: %lu, uncompressed size: %u, compression type: %s, assumed uncompressed size(%u), compressed size: %u, slime(%s)", consumedSize, data.size, compressionTypeToString(info.compressionType).c_str(), info.uncompressedSize, ((*_returnValues)[1]._data._len), json.c_str());
            LOG_ABORT("Error decoding JSON");
        }
    }
    if (LOG_WOULD_LOG(spam)) {
        LOG(spam, "read config value xxhash64(%s), payload size: %lu", xxhash64.c_str(), data.memRef.size);
    }
    return ConfigValue(std::make_shared<V3Payload>(std::move(slime)), xxhash64);
}

} // namespace config