aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageapi/mbusprot/storageprotocol.cpp
blob: 5c443537881533af0ed1b87323377a73a05b0e83 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "storageprotocol.h"
#include "serializationhelper.h"
#include "storagecommand.h"
#include "storagereply.h"
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/document/util/stringutil.h>
#include <vespa/document/bucket/fixed_bucket_spaces.h>
#include <cassert>
#include <sstream>

#include <vespa/log/bufferedlogger.h>
LOG_SETUP(".storage.api.mbusprot.protocol");

namespace storage::mbusprot {

mbus::string StorageProtocol::NAME = "StorageProtocol";

StorageProtocol::StorageProtocol(const std::shared_ptr<const document::DocumentTypeRepo> repo)
    : _serializer7_0(repo)
{
}

StorageProtocol::~StorageProtocol() = default;

mbus::IRoutingPolicy::UP
StorageProtocol::createPolicy(const mbus::string&, const mbus::string&) const
{
    return mbus::IRoutingPolicy::UP();
}

namespace {
vespalib::Version version7_0(7, 41, 19);
}

static mbus::Blob
encodeMessage(const ProtocolSerialization & serializer,
              const mbus::Routable & routable,
              const StorageMessage & message,
              const vespalib::Version & serializerVersion,
              const vespalib::Version & actualVersion)
{
    mbus::Blob blob(serializer.encode(*message.getInternalMessage()));

    if (LOG_WOULD_LOG(spam)) {
        std::ostringstream messageStream;
        document::StringUtil::printAsHex(messageStream, blob.data(), blob.size());

        LOG(spam, "Encoded message of protocol %s type %s using "
            "%s serialization as version is %s:\n%s",
            routable.getProtocol().c_str(),
            message.getInternalMessage()->getType().toString().c_str(),
            serializerVersion.toString().c_str(),
            actualVersion.toString().c_str(),
            messageStream.str().c_str());
    }

    return blob;
}


mbus::Blob
StorageProtocol::encode(const vespalib::Version& version,
                        const mbus::Routable& routable) const
{
    const StorageMessage & message(dynamic_cast<const StorageMessage &>(routable));

    try {
        assert(message.getInternalMessage());
        if (version < version7_0) {
            LOGBP(error,
                  "Cannot encode message on version %s."
                  "Minimum version is %s. Cannot serialize %s.",
                  version.toString().c_str(),
                  version7_0.toString().c_str(),
                  message.getInternalMessage()->toString().c_str());

            return mbus::Blob(0);
        }
        return encodeMessage(_serializer7_0, routable, message, version7_0, version);
    } catch (std::exception & e) {
        LOGBP(warning, "Failed to encode %s storage protocol message %s: %s",
              version.toString().c_str(),
              message.getInternalMessage()->toString().c_str(),
              e.what());
    }

    return mbus::Blob(0);
}

static mbus::Routable::UP
decodeMessage(const ProtocolSerialization & serializer,
              mbus::BlobRef data,
              const api::MessageType & type,
              const vespalib::Version & serializerVersion,
              const vespalib::Version & actualVersion)
{
    if (LOG_WOULD_LOG(spam)) {
        std::ostringstream messageStream;
        document::StringUtil::printAsHex(messageStream, data.data(), data.size());

        LOG(spam,
            "Decoding %s of version %s "
            "using %s decoder from:\n%s",
            type.toString().c_str(),
            actualVersion.toString().c_str(),
            serializerVersion.toString().c_str(),
            messageStream.str().c_str());
    }

    if (type.isReply()) {
        return std::make_unique<StorageReply>(data, serializer);
    } else {
        auto command = serializer.decodeCommand(data);
        if (command && command->getInternalMessage()) {
            command->getInternalMessage()->setApproxByteSize(data.size());
        }
        return mbus::Routable::UP(command.release());
    }
}

mbus::Routable::UP
StorageProtocol::decode(const vespalib::Version & version,
                        mbus::BlobRef data) const
{
    try {
        document::ByteBuffer buf(data.data(), data.size());
        auto & type = api::MessageType::get(
            static_cast<api::MessageType::Id>(SerializationHelper::getInt(buf)));

        StorageMessage::UP message;
        if (version < version7_0) {
            LOGBP(error,
                  "Cannot decode message on version %s. Minimum version is %s.",
                  version.toString().c_str(),
                  version7_0.toString().c_str());
            return mbus::Routable::UP();
        }
        return decodeMessage(_serializer7_0, data, type, version7_0, version);
    } catch (std::exception & e) {
        std::ostringstream ost;
        ost << "Failed to decode " << version.toString() << " messagebus "
            << "storage protocol message: " << e.what() << "\n";
        document::StringUtil::printAsHex(ost, data.data(), data.size());
        LOGBP(warning, "%s", ost.str().c_str());
    }
    return mbus::Routable::UP();
}

}