-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpika_partition.h
169 lines (138 loc) · 3.81 KB
/
pika_partition.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
// 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.
#ifndef PIKA_PARTITION_H_
#define PIKA_PARTITION_H_
#include "blackwidow/blackwidow.h"
#include "blackwidow/backupable.h"
#include "include/pika_binlog.h"
class Cmd;
struct BgSaveInfo {
bool bgsaving;
time_t start_time;
std::string s_start_time;
std::string path;
uint32_t filenum;
uint64_t offset;
BgSaveInfo() : bgsaving(false), filenum(0), offset(0) {}
void Clear() {
bgsaving = false;
path.clear();
filenum = 0;
offset = 0;
}
};
enum ReplState {
kNoConnect = 0,
kTryConnect = 1,
kTryDBSync = 2,
kWaitDBSync = 3,
kWaitReply = 4,
kConnected = 5,
kError = 6
};
// debug only
const std::string ReplStateMsg[] = {
"kNoConnect",
"kTryConnect",
"kTryDBSync",
"kWaitDBSync",
"kWaitReply",
"kConnected",
"kError"
};
class Partition : public std::enable_shared_from_this<Partition> {
public:
Partition(const std::string& table_name,
uint32_t partition_id,
const std::string& table_db_path,
const std::string& table_log_path,
const std::string& table_trash_path);
virtual ~Partition();
std::string GetTableName() const;
uint32_t GetPartitionId() const;
std::string GetPartitionName() const;
std::shared_ptr<Binlog> logger() const;
std::shared_ptr<blackwidow::BlackWidow> db() const;
void DoCommand(Cmd* const cmd);
void Compact(const blackwidow::DataType& type);
void DbRWLockWriter();
void DbRWLockReader();
void DbRWUnLock();
void RecordLock(const std::string& key);
void RecordUnLock(const std::string& key);
void SetBinlogIoError(bool error);
bool IsBinlogIoError();
bool GetBinlogOffset(BinlogOffset* const boffset);
bool SetBinlogOffset(const BinlogOffset& boffset);
ReplState State();
void SetReplState(const ReplState& state);
bool FullSync();
void SetFullSync(bool full_sync);
void PrepareRsync();
bool TryUpdateMasterOffset();
bool ChangeDb(const std::string& new_path);
void Leave();
void Close();
void MoveToTrash();
// BgSave use;
bool IsBgSaving();
void BgSavePartition();
BgSaveInfo bgsave_info();
// FlushDB & FlushSubDB use
bool FlushDB();
bool FlushSubDB(const std::string& db_name);
// Purgelogs use
bool PurgeLogs(uint32_t to = 0, bool manual = false);
void ClearPurge();
private:
std::string table_name_;
uint32_t partition_id_;
std::string db_path_;
std::string log_path_;
std::string trash_path_;
std::string bgsave_sub_path_;
std::string dbsync_path_;
std::string partition_name_;
bool opened_;
std::shared_ptr<Binlog> logger_;
std::atomic<bool> binlog_io_error_;
pthread_rwlock_t db_rwlock_;
slash::RecordMutex mutex_record_;
std::shared_ptr<blackwidow::BlackWidow> db_;
pthread_rwlock_t state_rwlock_; // protect partition status below
ReplState repl_state_;
bool full_sync_;
/*
* BgSave use
*/
static void DoBgSave(void* arg);
bool RunBgsaveEngine();
bool InitBgsaveEnv();
bool InitBgsaveEngine();
void ClearBgsave();
void FinishBgsave();
BgSaveInfo bgsave_info_;
slash::Mutex bgsave_protector_;
blackwidow::BackupEngine* bgsave_engine_;
/*
* Purgelogs use
*/
static void DoPurgeLogs(void* arg);
bool PurgeFiles(uint32_t to, bool manual);
bool GetBinlogFiles(std::map<uint32_t, std::string>& binlogs);
std::atomic<bool> purging_;
/*
* No allowed copy and copy assign
*/
Partition(const Partition&);
void operator=(const Partition&);
};
struct PurgeArg {
std::shared_ptr<Partition> partition;
uint32_t to;
bool manual;
bool force; // Ignore the delete window
};
#endif