blob: 71daef67c9d1518546a1d312b6495bb7c7b182cd (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <vespa/vespalib/stllike/string.h>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <vector>
namespace proton {
/**
* Track document db main state and validate that state transitions follow
* legal edges.
*
* Note that SHUTDOWN state can be entered from almost any state.
*/
class DDBState
{
public:
enum class State {
CONSTRUCT,
LOAD,
REPLAY_TRANSACTION_LOG,
REDO_REPROCESS,
APPLY_LIVE_CONFIG,
REPROCESS,
ONLINE,
SHUTDOWN,
DEAD
};
enum class ConfigState {
OK,
NEED_RESTART
};
private:
std::atomic<State> _state;
std::atomic<ConfigState> _configState;
using Mutex = std::mutex;
using Guard = std::lock_guard<Mutex>;
using GuardLock = std::unique_lock<Mutex>;
Mutex _lock; // protects state transition
std::condition_variable _cond;
static std::vector<vespalib::string> _stateNames;
static std::vector<vespalib::string> _configStateNames;
void set_state(State state) noexcept { _state.store(state, std::memory_order_release); }
public:
DDBState();
~DDBState();
/**
* Try to enter LOAD state. Fail and return false if document db is
* being shut down.
*/
bool enterLoadState();
bool enterReplayTransactionLogState();
bool enterRedoReprocessState();
bool enterApplyLiveConfigState();
bool enterReprocessState();
bool enterOnlineState();
void enterShutdownState();
void enterDeadState();
State getState() const noexcept { return _state.load(std::memory_order_acquire); }
static vespalib::string getStateString(State state);
bool getClosed() const noexcept {
State state(getState());
return state >= State::SHUTDOWN;
}
bool getAllowReconfig() const noexcept {
State state(getState());
return state >= State::APPLY_LIVE_CONFIG && state < State::SHUTDOWN;
}
bool getAllowPrune() const noexcept {
State state(getState());
return state == State::ONLINE;
}
static bool getDelayedConfig(ConfigState state) noexcept {
return state != ConfigState::OK;
}
bool getDelayedConfig() const noexcept {
ConfigState state(getConfigState());
return getDelayedConfig(state);
}
bool get_load_done() const noexcept {
State state(getState());
return state >= State::REPLAY_TRANSACTION_LOG;
}
void clearDelayedConfig();
ConfigState getConfigState() const noexcept { return _configState.load(std::memory_order_relaxed); }
static vespalib::string getConfigStateString(ConfigState configState);
void setConfigState(ConfigState newConfigState);
void waitForOnlineState();
};
} // namespace proton
|