forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_server.h
422 lines (367 loc) · 12.6 KB
/
pika_server.h
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (c) 2015-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.
#ifndef PIKA_SERVER_H_
#define PIKA_SERVER_H_
#include <sys/statfs.h>
#include <memory>
#include "slash/include/slash_mutex.h"
#include "slash/include/slash_status.h"
#include "slash/include/slash_string.h"
#include "pink/include/bg_thread.h"
#include "pink/include/thread_pool.h"
#include "pink/include/pink_pubsub.h"
#include "blackwidow/blackwidow.h"
#include "blackwidow/backupable.h"
#include "include/pika_conf.h"
#include "include/pika_table.h"
#include "include/pika_binlog.h"
#include "include/pika_define.h"
#include "include/pika_monitor_thread.h"
#include "include/pika_rsync_service.h"
#include "include/pika_dispatch_thread.h"
#include "include/pika_repl_client.h"
#include "include/pika_repl_server.h"
#include "include/pika_auxiliary_thread.h"
#include "include/pika_client_processor.h"
#include "include/pika_statistic.h"
using slash::Status;
using slash::Slice;
/*
static std::set<std::string> MultiKvCommands {kCmdNameDel,
kCmdNameMget, kCmdNameKeys, kCmdNameMset,
kCmdNameMsetnx, kCmdNameExists, kCmdNameScan,
kCmdNameScanx, kCmdNamePKScanRange, kCmdNamePKRScanRange,
kCmdNameRPopLPush, kCmdNameZUnionstore, kCmdNameZInterstore,
kCmdNameSUnion, kCmdNameSUnionstore, kCmdNameSInter,
kCmdNameSInterstore, kCmdNameSDiff, kCmdNameSDiffstore,
kCmdNameSMove, kCmdNameBitOp, kCmdNamePfAdd,
kCmdNamePfCount, kCmdNamePfMerge, kCmdNameGeoAdd,
kCmdNameGeoPos, kCmdNameGeoDist, kCmdNameGeoHash,
kCmdNameGeoRadius, kCmdNameGeoRadiusByMember};
*/
static std::set<std::string> ConsensusNotSupportCommands {
kCmdNameMsetnx, kCmdNameScan, kCmdNameKeys,
kCmdNameScanx, kCmdNamePKScanRange, kCmdNamePKRScanRange,
kCmdNameRPopLPush, kCmdNameZUnionstore, kCmdNameZInterstore,
kCmdNameSUnion, kCmdNameSUnionstore, kCmdNameSInter,
kCmdNameSInterstore, kCmdNameSDiff, kCmdNameSDiffstore,
kCmdNameSMove, kCmdNameBitOp, kCmdNamePfAdd,
kCmdNamePfCount, kCmdNamePfMerge, kCmdNameGeoAdd,
kCmdNameGeoPos, kCmdNameGeoDist, kCmdNameGeoHash,
kCmdNameGeoRadius, kCmdNameGeoRadiusByMember, kCmdNamePKPatternMatchDel,
kCmdNameSlaveof, kCmdNameDbSlaveof, kCmdNameMset,
kCmdNameMget};
static std::set<std::string> ShardingModeNotSupportCommands {
kCmdNameMsetnx, kCmdNameScan, kCmdNameKeys,
kCmdNameScanx, kCmdNamePKScanRange, kCmdNamePKRScanRange,
kCmdNameRPopLPush, kCmdNameZUnionstore, kCmdNameZInterstore,
kCmdNameSUnion, kCmdNameSUnionstore, kCmdNameSInter,
kCmdNameSInterstore, kCmdNameSDiff, kCmdNameSDiffstore,
kCmdNameSMove, kCmdNameBitOp, kCmdNamePfAdd,
kCmdNamePfCount, kCmdNamePfMerge, kCmdNameGeoAdd,
kCmdNameGeoPos, kCmdNameGeoDist, kCmdNameGeoHash,
kCmdNameGeoRadius, kCmdNameGeoRadiusByMember, kCmdNamePKPatternMatchDel,
kCmdNameSlaveof, kCmdNameDbSlaveof};
extern PikaConf *g_pika_conf;
enum TaskType {
kCompactAll,
kCompactStrings,
kCompactHashes,
kCompactSets,
kCompactZSets,
kCompactList,
kResetReplState,
kPurgeLog,
kStartKeyScan,
kStopKeyScan,
kBgSave,
};
class PikaServer {
public:
PikaServer();
~PikaServer();
/*
* Server init info
*/
bool ServerInit();
void Start();
void Exit();
std::string host();
int port();
time_t start_time_s();
std::string master_ip();
int master_port();
int role();
bool readonly(const std::string& table, const std::string& key);
bool ConsensusCheck(const std::string& table_name, const std::string& key);
int repl_state();
std::string repl_state_str();
bool force_full_sync();
void SetForceFullSync(bool v);
void SetDispatchQueueLimit(int queue_limit);
blackwidow::BlackwidowOptions bw_options();
/*
* Table use
*/
void InitTableStruct();
Status AddTableStruct(std::string table_name, uint32_t num);
Status DelTableStruct(std::string table_name);
std::shared_ptr<Table> GetTable(const std::string& table_name);
std::set<uint32_t> GetTablePartitionIds(const std::string& table_name);
bool IsBgSaving();
bool IsKeyScaning();
bool IsCompacting();
bool IsTableExist(const std::string& table_name);
bool IsTablePartitionExist(const std::string& table_name, uint32_t partition_id);
bool IsCommandSupport(const std::string& command);
bool IsTableBinlogIoError(const std::string& table_name);
Status DoSameThingSpecificTable(const TaskType& type, const std::set<std::string>& tables = {});
/*
* Partition use
*/
void PreparePartitionTrySync();
void PartitionSetMaxCacheStatisticKeys(uint32_t max_cache_statistic_keys);
void PartitionSetSmallCompactionThreshold(uint32_t small_compaction_threshold);
bool GetTablePartitionBinlogOffset(const std::string& table_name,
uint32_t partition_id,
BinlogOffset* const boffset);
std::shared_ptr<Partition> GetPartitionByDbName(const std::string& db_name);
std::shared_ptr<Partition> GetTablePartitionById(
const std::string& table_name,
uint32_t partition_id);
std::shared_ptr<Partition> GetTablePartitionByKey(
const std::string& table_name,
const std::string& key);
Status DoSameThingEveryPartition(const TaskType& type);
/*
* Master use
*/
void BecomeMaster();
void DeleteSlave(int fd); //conn fd
int32_t CountSyncSlaves();
int32_t GetSlaveListString(std::string& slave_list_str);
int32_t GetShardingSlaveListString(std::string& slave_list_str);
bool TryAddSlave(const std::string& ip, int64_t port, int fd,
const std::vector<TableStruct>& table_structs);
slash::Mutex slave_mutex_; // protect slaves_;
std::vector<SlaveItem> slaves_;
/*
* Slave use
*/
void SyncError();
void RemoveMaster();
bool SetMaster(std::string& master_ip, int master_port);
/*
* Slave State Machine
*/
bool ShouldMetaSync();
void FinishMetaSync();
bool MetaSyncDone();
void ResetMetaSyncStatus();
bool AllPartitionConnectSuccess();
bool LoopPartitionStateMachine();
void SetLoopPartitionStateMachine(bool need_loop);
int GetMetaSyncTimestamp();
void UpdateMetaSyncTimestamp();
bool IsFirstMetaSync();
void SetFirstMetaSync(bool v);
/*
* PikaClientProcessor Process Task
*/
void ScheduleClientPool(pink::TaskFunc func, void* arg);
void ScheduleClientBgThreads(pink::TaskFunc func, void* arg, const std::string& hash_str);
// for info debug
size_t ClientProcessorThreadPoolCurQueueSize();
/*
* BGSave used
*/
void BGSaveTaskSchedule(pink::TaskFunc func, void* arg);
/*
* PurgeLog used
*/
void PurgelogsTaskSchedule(pink::TaskFunc func, void* arg);
/*
* Flushall & Flushdb used
*/
void PurgeDir(const std::string& path);
void PurgeDirTaskSchedule(void (*function)(void*), void* arg);
/*
* DBSync used
*/
void DBSync(const std::string& ip, int port,
const std::string& table_name,
uint32_t partition_id);
void TryDBSync(const std::string& ip, int port,
const std::string& table_name,
uint32_t partition_id, int32_t top);
void DbSyncSendFile(const std::string& ip, int port,
const std::string& table_name,
uint32_t partition_id);
std::string DbSyncTaskIndex(const std::string& ip, int port,
const std::string& table_name,
uint32_t partition_id);
/*
* Keyscan used
*/
void KeyScanTaskSchedule(pink::TaskFunc func, void* arg);
/*
* Client used
*/
void ClientKillAll();
int ClientKill(const std::string &ip_port);
int64_t ClientList(std::vector<ClientInfo> *clients = nullptr);
/*
* Monitor used
*/
bool HasMonitorClients();
void AddMonitorMessage(const std::string &monitor_message);
void AddMonitorClient(std::shared_ptr<PikaClientConn> client_ptr);
/*
* Slowlog used
*/
void SlowlogTrim();
void SlowlogReset();
uint32_t SlowlogLen();
void SlowlogObtain(int64_t number, std::vector<SlowlogEntry>* slowlogs);
void SlowlogPushEntry(const PikaCmdArgsType& argv, int32_t time, int64_t duration);
/*
* Statistic used
*/
void ResetStat();
uint64_t ServerQueryNum();
uint64_t ServerCurrentQps();
uint64_t accumulative_connections();
void incr_accumulative_connections();
void ResetLastSecQuerynum();
void UpdateQueryNumAndExecCountTable(
const std::string& table_name,
const std::string& command, bool is_write);
std::unordered_map<std::string, uint64_t> ServerExecCountTable();
QpsStatistic ServerTableStat(const std::string& table_name);
std::unordered_map<std::string, QpsStatistic> ServerAllTableStat();
/*
* Slave to Master communication used
*/
int SendToPeer();
void SignalAuxiliary();
Status TriggerSendBinlogSync();
/*
* PubSub used
*/
int PubSubNumPat();
int Publish(const std::string& channel, const std::string& msg);
int UnSubscribe(std::shared_ptr<pink::PinkConn> conn,
const std::vector<std::string>& channels,
const bool pattern,
std::vector<std::pair<std::string, int>>* result);
void Subscribe(std::shared_ptr<pink::PinkConn> conn,
const std::vector<std::string>& channels,
const bool pattern,
std::vector<std::pair<std::string, int>>* result);
void PubSubChannels(const std::string& pattern,
std::vector<std::string>* result);
void PubSubNumSub(const std::vector<std::string>& channels,
std::vector<std::pair<std::string, int>>* result);
// info debug use
void ServerStatus(std::string* info);
friend class Cmd;
friend class InfoCmd;
friend class PkClusterAddSlotsCmd;
friend class PkClusterDelSlotsCmd;
friend class PkClusterAddTableCmd;
friend class PkClusterDelTableCmd;
friend class PikaReplClientConn;
friend class PkClusterInfoCmd;
private:
/*
* TimingTask use
*/
void DoTimingTask();
void AutoCompactRange();
void AutoPurge();
void AutoDeleteExpiredDump();
void AutoKeepAliveRSync();
std::string host_;
int port_;
time_t start_time_s_;
blackwidow::BlackwidowOptions bw_options_;
void InitBlackwidowOptions();
std::atomic<bool> exit_;
/*
* Table used
*/
std::atomic<SlotState> slot_state_;
pthread_rwlock_t tables_rw_;
std::map<std::string, std::shared_ptr<Table>> tables_;
/*
* CronTask used
*/
bool have_scheduled_crontask_;
struct timeval last_check_compact_time_;
/*
* Communicate with the client used
*/
int worker_num_;
PikaClientProcessor* pika_client_processor_;
PikaDispatchThread* pika_dispatch_thread_;
/*
* Slave used
*/
std::string master_ip_;
int master_port_;
int repl_state_;
int role_;
int last_meta_sync_timestamp_;
bool first_meta_sync_;
bool loop_partition_state_machine_;
bool force_full_sync_;
pthread_rwlock_t state_protector_; //protect below, use for master-slave mode
/*
* Bgsave used
*/
pink::BGThread bgsave_thread_;
/*
* Purgelogs use
*/
pink::BGThread purge_thread_;
/*
* DBSync used
*/
slash::Mutex db_sync_protector_;
std::unordered_set<std::string> db_sync_slaves_;
/*
* Keyscan used
*/
pink::BGThread key_scan_thread_;
/*
* Monitor used
*/
PikaMonitorThread* pika_monitor_thread_;
/*
* Rsync used
*/
PikaRsyncService* pika_rsync_service_;
/*
* Pubsub used
*/
pink::PubSubThread* pika_pubsub_thread_;
/*
* Communication used
*/
PikaAuxiliaryThread* pika_auxiliary_thread_;
/*
* Slowlog used
*/
uint64_t slowlog_entry_id_;
pthread_rwlock_t slowlog_protector_;
std::list<SlowlogEntry> slowlog_list_;
/*
* Statistic used
*/
Statistic statistic_;
PikaServer(PikaServer &ps);
void operator =(const PikaServer &ps);
};
#endif