forked from OpenAtomFoundation/pikiwidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_transaction.cc
309 lines (284 loc) · 9.02 KB
/
pika_transaction.cc
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2018-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include <memory>
#include "include/pika_transaction.h"
#include "include/pika_admin.h"
#include "include/pika_client_conn.h"
#include "include/pika_list.h"
#include "include/pika_rm.h"
#include "include/pika_server.h"
#include "include/pika_transaction.h"
#include "src/pstd/include/scope_record_lock.h"
extern std::unique_ptr<PikaServer> g_pika_server;
extern std::unique_ptr<PikaReplicaManager> g_pika_rm;
void MultiCmd::Do() {
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (conn == nullptr || client_conn == nullptr) {
res_.SetRes(CmdRes::kErrOther, name());
return;
}
if (client_conn->IsInTxn()) {
res_.SetRes(CmdRes::kErrOther, "ERR MULTI calls can not be nested");
return;
}
client_conn->SetTxnStartState(true);
res_.SetRes(CmdRes::kOk);
}
void MultiCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, name());
return;
}
}
void ExecCmd::Do() {
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
std::vector<CmdRes> res_vec = {};
std::vector<std::shared_ptr<std::string>> resp_strs;
for (size_t i = 0; i < cmds_.size(); ++i) {
resp_strs.emplace_back(std::make_shared<std::string>());
}
auto resp_strs_iter = resp_strs.begin();
std::for_each(cmds_.begin(), cmds_.end(), [&client_conn, &res_vec, &resp_strs_iter](CmdInfo& each_cmd_info) {
each_cmd_info.cmd_->SetResp(*resp_strs_iter++);
auto& cmd = each_cmd_info.cmd_;
auto& db = each_cmd_info.db_;
auto sync_db = each_cmd_info.sync_db_;
cmd->res() = {};
if (cmd->name() == kCmdNameFlushall) {
auto flushall = std::dynamic_pointer_cast<FlushallCmd>(cmd);
flushall->FlushAllWithoutLock();
client_conn->SetAllTxnFailed();
} else if (cmd->name() == kCmdNameFlushdb) {
auto flushdb = std::dynamic_pointer_cast<FlushdbCmd>(cmd);
flushdb->FlushAllDBsWithoutLock();
if (cmd->res().ok()) {
cmd->res().SetRes(CmdRes::kOk);
}
client_conn->SetTxnFailedFromDBs(each_cmd_info.db_->GetDBName());
} else {
cmd->Do();
if (cmd->res().ok() && cmd->is_write()) {
cmd->DoBinlog();
auto db_keys = cmd->current_key();
for (auto& item : db_keys) {
item = cmd->db_name().append(item);
}
client_conn->SetTxnFailedFromKeys(db_keys);
}
}
res_vec.emplace_back(cmd->res());
});
res_.AppendArrayLen(res_vec.size());
for (auto &r : res_vec) {
res_.AppendStringRaw(r.message());
}
}
void ExecCmd::Execute() {
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (client_conn == nullptr) {
res_.SetRes(CmdRes::kErrOther, name());
return;
}
if (!client_conn->IsInTxn()) {
res_.SetRes(CmdRes::kErrOther, "ERR EXEC without MULTI");
return;
}
if (IsTxnFailedAndSetState()) {
client_conn->ExitTxn();
return;
}
SetCmdsVec();
Lock();
Do();
Unlock();
ServeToBLrPopWithKeys();
list_cmd_.clear();
client_conn->ExitTxn();
}
void ExecCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, name());
return;
}
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (client_conn == nullptr) {
res_.SetRes(CmdRes::kErrOther, name());
return;
}
}
bool ExecCmd::IsTxnFailedAndSetState() {
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (client_conn->IsTxnInitFailed()) {
res_.SetRes(CmdRes::kTxnAbort, "Transaction discarded because of previous errors.");
return true;
}
if (client_conn->IsTxnWatchFailed()) {
res_.AppendStringLen(-1);
return true;
}
return false;
}
void ExecCmd::Lock() {
g_pika_server->DBLockShared();
std::for_each(lock_db_.begin(), lock_db_.end(), [](auto& need_lock_db) {
need_lock_db->DBLock();
});
if (is_lock_rm_dbs_) {
g_pika_rm->DBLock();
}
std::for_each(r_lock_dbs_.begin(), r_lock_dbs_.end(), [this](auto& need_lock_db) {
if (lock_db_keys_.count(need_lock_db) != 0) {
pstd::lock::MultiRecordLock record_lock(need_lock_db->LockMgr());
record_lock.Lock(lock_db_keys_[need_lock_db]);
}
need_lock_db->DBLockShared();
});
}
void ExecCmd::Unlock() {
std::for_each(r_lock_dbs_.begin(), r_lock_dbs_.end(), [this](auto& need_lock_db) {
if (lock_db_keys_.count(need_lock_db) != 0) {
pstd::lock::MultiRecordLock record_lock(need_lock_db->LockMgr());
record_lock.Unlock(lock_db_keys_[need_lock_db]);
}
need_lock_db->DBUnlockShared();
});
if (is_lock_rm_dbs_) {
g_pika_rm->DBUnlock();
}
std::for_each(lock_db_.begin(), lock_db_.end(), [](auto& need_lock_db) {
need_lock_db->DBUnlock();
});
g_pika_server->DBUnlockShared();
}
void ExecCmd::SetCmdsVec() {
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(GetConn());
auto cmd_que = client_conn->GetTxnCmdQue();
while (!cmd_que.empty()) {
auto cmd = cmd_que.front();
auto cmd_db = client_conn->GetCurrentTable();
auto db = g_pika_server->GetDB(cmd_db);
auto sync_db = g_pika_rm->GetSyncMasterDBByName(DBInfo(cmd->db_name()));
cmds_.emplace_back(cmd, db, sync_db);
if (cmd->name() == kCmdNameSelect) {
cmd->Do();
} else if (cmd->name() == kCmdNameFlushdb) {
is_lock_rm_dbs_ = true;
lock_db_.emplace(g_pika_server->GetDB(cmd_db));
} else if (cmd->name() == kCmdNameFlushall) {
is_lock_rm_dbs_ = true;
for (const auto& db_item : g_pika_server->GetDB()) {
lock_db_.emplace(db_item.second);
}
} else {
r_lock_dbs_.emplace(db);
if (lock_db_keys_.count(db) == 0) {
lock_db_keys_.emplace(db, std::vector<std::string>{});
}
auto cmd_keys = cmd->current_key();
lock_db_keys_[db].insert(lock_db_keys_[db].end(), cmd_keys.begin(), cmd_keys.end());
if (cmd->name() == kCmdNameLPush || cmd->name() == kCmdNameRPush) {
list_cmd_.insert(list_cmd_.end(), cmds_.back());
}
}
cmd_que.pop();
}
}
void ExecCmd::ServeToBLrPopWithKeys() {
for (auto each_list_cmd : list_cmd_) {
auto push_keys = each_list_cmd.cmd_->current_key();
//PS: currently, except for blpop/brpop, there are three cmds inherited from BlockingBaseCmd: lpush, rpush, rpoplpush
//For rpoplpush which has 2 keys(source and receiver), push_keys[0] fetchs the receiver, push_keys[1] fetchs the source.(see RpopLpushCmd::current_key()
auto push_key = push_keys[0];
if (auto push_list_cmd = std::dynamic_pointer_cast<BlockingBaseCmd>(each_list_cmd.cmd_);
push_list_cmd != nullptr) {
push_list_cmd->TryToServeBLrPopWithThisKey(push_key, each_list_cmd.db_);
}
}
}
void WatchCmd::Do() {
auto mp = std::map<storage::DataType, storage::Status>{};
for (const auto& key : keys_) {
auto type_count = db_->storage()->IsExist(key, &mp);
if (type_count > 1) {
res_.SetRes(CmdRes::CmdRet::kErrOther, "EXEC WATCH watch key must be unique");
return;
}
mp.clear();
}
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (client_conn == nullptr) {
res_.SetRes(CmdRes::kErrOther, name());
return;
}
if (client_conn->IsInTxn()) {
res_.SetRes(CmdRes::CmdRet::kErrOther, "ERR WATCH inside MULTI is not allowed");
return;
}
client_conn->AddKeysToWatch(db_keys_);
res_.SetRes(CmdRes::kOk);
}
void WatchCmd::Execute() {
Do();
}
void WatchCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, name());
return;
}
size_t pos = 1;
while (pos < argv_.size()) {
keys_.emplace_back(argv_[pos]);
db_keys_.push_back(db_name() + argv_[pos++]);
}
}
void UnwatchCmd::Do() {
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (client_conn == nullptr) {
res_.SetRes(CmdRes::kErrOther, name());
return;
}
if (client_conn->IsTxnExecing()) {
res_.SetRes(CmdRes::CmdRet::kOk);
return ;
}
client_conn->RemoveWatchedKeys();
if (client_conn->IsTxnWatchFailed()) {
client_conn->SetTxnWatchFailState(false);
}
res_.SetRes(CmdRes::CmdRet::kOk);
}
void UnwatchCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, name());
return;
}
}
void DiscardCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, name());
return;
}
}
void DiscardCmd::Do() {
auto conn = GetConn();
auto client_conn = std::dynamic_pointer_cast<PikaClientConn>(conn);
if (client_conn == nullptr) {
res_.SetRes(CmdRes::kErrOther, name());
return;
}
if (!client_conn->IsInTxn()) {
res_.SetRes(CmdRes::kErrOther, "ERR DISCARD without MULTI");
return;
}
client_conn->ExitTxn();
res_.SetRes(CmdRes::CmdRet::kOk);
}