aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/ddbstate.cpp
blob: 868c1fa1240fcb65a6036975cde1c81aed71afdc (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "ddbstate.h"
#include <cassert>

namespace proton {

std::vector<vespalib::string> DDBState::_stateNames =
{
    "CONSTRUCT",
    "LOAD",
    "REPLAY_TRANSACTION_LOG",
    "REDO_REPROCESS",
    "APPLY_LIVE_CONFIG",
    "REPROCESS",
    "ONLINE",
    "SHUTDOWN",
    "DEAD",
};

std::vector<vespalib::string> DDBState::_configStateNames =
{
    "OK",
    "NEED_RESTART"
};

DDBState::DDBState()
    : _state(State::CONSTRUCT),
      _configState(ConfigState::OK),
      _lock(),
      _cond()
{
}


DDBState::~DDBState() = default;


bool
DDBState::enterLoadState()
{
    Guard guard(_lock);
    if (getClosed()) {
        return false;
    }
    assert(getState() == State::CONSTRUCT);
    set_state(State::LOAD);
    return true;
}

    
bool
DDBState::enterReplayTransactionLogState()
{
    Guard guard(_lock);
    if (getClosed()) {
        return false;
    }
    assert(getState() == State::LOAD);
    set_state(State::REPLAY_TRANSACTION_LOG);
    return true;
}


bool
DDBState::enterRedoReprocessState()
{
    Guard guard(_lock);
    if (getClosed()) {
        return false;
    }
    assert(getState() == State::REPLAY_TRANSACTION_LOG);
    set_state(State::REDO_REPROCESS);
    return true;
}


bool
DDBState::enterApplyLiveConfigState()
{
    Guard guard(_lock);
    if (getClosed()) {
        return false;
    }
    State state(getState());
    assert(state == State::REPLAY_TRANSACTION_LOG ||
           state == State::REDO_REPROCESS);
    set_state(State::APPLY_LIVE_CONFIG);
    return true;
}


bool
DDBState::enterReprocessState()
{
    Guard guard(_lock);
    if (getClosed()) {
        return false;
    }
    assert(getState() == State::APPLY_LIVE_CONFIG);
    set_state(State::REPROCESS);
    return true;
}

bool
DDBState::enterOnlineState()
{
    Guard guard(_lock);
    if (getClosed()) {
        return false;
    }
    assert(getState() == State::REPROCESS);
    set_state(State::ONLINE);
    _cond.notify_all();
    return true;
}


void
DDBState::enterShutdownState()
{
    Guard guard(_lock);
    // Shutdown can be initiated before online state was reached
    if (getClosed()) {
        return;
    }
    set_state(State::SHUTDOWN);
    _cond.notify_all();
}

void
DDBState::enterDeadState()
{
    Guard guard(_lock);
    if (getState() == State::DEAD) {
        return;
    }
    assert(getState() == State::SHUTDOWN);
    set_state(State::DEAD);
    _cond.notify_all();
}


void
DDBState::setConfigState(ConfigState newConfigState)
{
    Guard guard(_lock);
    _configState.store(newConfigState, std::memory_order_relaxed);
}


void
DDBState::clearDelayedConfig()
{
    setConfigState(ConfigState::OK);
}


vespalib::string
DDBState::getStateString(State state)
{
    return _stateNames[static_cast<unsigned int>(state)];
}


vespalib::string
DDBState::getConfigStateString(ConfigState configState)
{
    return _configStateNames[static_cast<unsigned int>(configState)];
}


void
DDBState::waitForOnlineState()
{
    GuardLock lk(_lock);
    _cond.wait(lk, [this] { return this->getState() >= State::ONLINE; } );
}


} // namespace proton