summaryrefslogtreecommitdiffstats
path: root/storageapi/src/vespa/storageapi/messageapi/returncode.h
blob: 3bdcfc885de30c5242e14a76dbd308e35c74cfc3 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class storage::api::ReturnCode
 * @ingroup messageapi
 *
 * @brief Class for representing return values from the processing chain
 *
 * @version $Id$
 */

#pragma once

#include <vespa/vespalib/util/printable.h>
#include <vespa/document/util/serializable.h>
#include <vespa/documentapi/messagebus/documentprotocol.h>
#include <iosfwd>

namespace document {
    class ByteBuffer;
}

namespace storage {
namespace api {

class ReturnCode : public document::Deserializable,
                   public vespalib::Printable {
public:
    typedef documentapi::DocumentProtocol Protocol;

    /** Return status codes */
    enum Result {
        OK                            = mbus::ErrorCode::NONE,

        EXISTS                        = Protocol::ERROR_EXISTS,

        NOT_READY                     = Protocol::ERROR_NODE_NOT_READY,
        WRONG_DISTRIBUTION            = Protocol::ERROR_WRONG_DISTRIBUTION,
        REJECTED                      = Protocol::ERROR_REJECTED,
        ABORTED                       = Protocol::ERROR_ABORTED,
        BUCKET_NOT_FOUND              = Protocol::ERROR_BUCKET_NOT_FOUND,
        BUCKET_DELETED                = Protocol::ERROR_BUCKET_DELETED,
        TIMESTAMP_EXIST               = Protocol::ERROR_TIMESTAMP_EXIST,
        STALE_TIMESTAMP               = Protocol::ERROR_STALE_TIMESTAMP,
        TEST_AND_SET_CONDITION_FAILED = Protocol::ERROR_TEST_AND_SET_CONDITION_FAILED,

        // Wrong use
        UNKNOWN_COMMAND               = Protocol::ERROR_UNKNOWN_COMMAND,
        NOT_IMPLEMENTED               = Protocol::ERROR_NOT_IMPLEMENTED,
        ILLEGAL_PARAMETERS            = Protocol::ERROR_ILLEGAL_PARAMETERS,
        IGNORED                       = Protocol::ERROR_IGNORED,
        UNPARSEABLE                   = Protocol::ERROR_UNPARSEABLE,

        // Network failure
        NOT_CONNECTED                 = Protocol::ERROR_NOT_CONNECTED,
        TIMEOUT                       = mbus::ErrorCode::TIMEOUT,
        BUSY                          = Protocol::ERROR_BUSY,

        // Disk operations
        NO_SPACE                      = Protocol::ERROR_NO_SPACE,
        DISK_FAILURE                  = Protocol::ERROR_DISK_FAILURE,
        IO_FAILURE                    = Protocol::ERROR_IO_FAILURE,

        // Don't know what happened (catch-all)
        INTERNAL_FAILURE              = Protocol::ERROR_INTERNAL_FAILURE
    };

private:
    Result _result;
    vespalib::string _message;
    void onDeserialize(const document::DocumentTypeRepo &repo, document::ByteBuffer& buffer) override;
    void onSerialize(document::ByteBuffer& buffer) const override;

public:
    ReturnCode();
    explicit ReturnCode(Result result, const vespalib::stringref & msg = "");
    ReturnCode(const document::DocumentTypeRepo &repo,
               document::ByteBuffer& buffer);
    ReturnCode(const ReturnCode &);
    ReturnCode & operator = (const ReturnCode &);
    ReturnCode(ReturnCode &&) = default;
    ReturnCode & operator = (ReturnCode &&);
    ~ReturnCode();

    ReturnCode* clone() const override { return new ReturnCode(*this); }

    size_t getSerializedSize() const override;

    const vespalib::string& getMessage() const { return _message; }
    void setMessage(const vespalib::stringref & message) { _message = message; }

    Result getResult() const { return _result; }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    /**
     * Translate from status code to human-readable string
     * @param result Status code returned from getResult()
     */
    static vespalib::string getResultString(Result result);

    bool failed() const { return (_result != OK); }
    bool success() const { return (_result == OK); }

    bool operator==(Result res) const { return _result == res; }
    bool operator!=(Result res) const { return _result != res; }
    bool operator==(const ReturnCode& code) const
        { return _result == code._result && _message == code._message; }
    bool operator!=(const ReturnCode& code) const
        { return _result != code._result || _message != code._message; }

    // To avoid lots of code matching various return codes in storage, we define
    // some functions they can use to match those codes that corresponds to what
    // they want to match.

    bool isBusy() const;
    bool isNodeDownOrNetwork() const;
    bool isCriticalForMaintenance() const;
    bool isCriticalForVisitor() const;
    bool isCriticalForVisitorDispatcher() const;
    bool isShutdownRelated() const;
    bool isBucketDisappearance() const;
    bool isNonCriticalForIntegrityChecker() const;
};


} // api
} // storage