summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/include/wsrep/reporter.hpp
blob: 3e8c70005ccf6324bf467424c932606e2c7eba9e (plain)
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 (C) 2021 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

/** @file info.hpp
 *
 * Interface to report application status to external programs
 * via JSON file.
 */

#ifndef WSREP_REPORTER_HPP
#define WSREP_REPORTER_HPP

#include "mutex.hpp"
#include "server_state.hpp"

#include <string>
#include <deque>

namespace wsrep
{
    class reporter
    {
    public:
        reporter(mutex&             mutex,
                 const std::string& file_name,
                 size_t             max_msg);

        virtual ~reporter();

        void report_state(enum server_state::state state);

        /**
         * Report progres in the form of a JSON string (all values integers):
         * {
         *     "from":       FROM,       // from wsrep API state number
         *     "to":         TO,         // to wsrep API state number
         *     "total":      TOTAL,      // total work to do
         *     "done":       DONE,       // work already done
         *     "indefinite": INDEFINITE  // indefinite value of work constant
         * }
         */
        void report_progress(const std::string& json);

        /**
         * Report provider event.
         * {
         *     "status": "Status string",
         *     "message": "Message from the provider"
         * }
         */
        void report_event(const std::string& json);

        enum log_level
        {
            error,
            warning
        };

        // undefined timestamp value
        static double constexpr undefined = 0.0;

        void report_log_msg(log_level, const std::string& msg,
                            double timestamp = undefined);

    private:
        enum substates {
            s_disconnected_disconnected,
            s_disconnected_initializing,
            s_disconnected_initialized,
            s_connected_waiting, // to become joiner
            s_joining_initialized,
            s_joining_sst,
            s_joining_initializing,
            s_joining_ist,
            s_joined_syncing,
            s_synced_running,
            s_donor_sending,
            s_disconnecting_disconnecting,
            substates_max
        };

        wsrep::mutex&       mutex_;
        std::string const   file_name_;
        std::string         progress_;
        char*               template_;
        substates           state_;
        bool                initialized_;

        typedef struct {
            double tstamp;
            std::string msg;
        } log_msg;

        std::deque<log_msg> err_msg_;
        std::deque<log_msg> warn_msg_;
        std::deque<log_msg> events_;
        size_t const        max_msg_;

        static void write_log_msg(std::ostream& os,
                                  const log_msg& msg);
        static void write_event(std::ostream& os,
                                const log_msg& msg);
        static void write_array(std::ostream& os, const std::string& label,
                                const std::deque<log_msg>& events,
                                void (*element_writer)(std::ostream& os,
                                                       const log_msg& msg));
        substates substate_map(enum server_state::state state);
        float     progress_map(float progress) const;
        void      write_file(double timestamp);

        // make uncopyable
        reporter(const wsrep::reporter&);
        void operator=(const wsrep::reporter&);
    }; /* reporter */

} /* wsrep */

#endif /* WSREP_REPORTER_HPP */