forked from OpenAtomFoundation/pikiwidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_cache_load_thread.cc
223 lines (199 loc) · 7.09 KB
/
pika_cache_load_thread.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
// Copyright (c) 2023-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 <glog/logging.h>
#include "include/pika_cache_load_thread.h"
#include "include/pika_server.h"
#include "include/pika_cache.h"
#include "pstd/include/scope_record_lock.h"
extern PikaServer* g_pika_server;
PikaCacheLoadThread::PikaCacheLoadThread(int zset_cache_start_direction, int zset_cache_field_num_per_key)
: should_exit_(false)
, loadkeys_cond_()
, async_load_keys_num_(0)
, waitting_load_keys_num_(0)
, zset_cache_start_direction_(zset_cache_start_direction)
, zset_cache_field_num_per_key_(zset_cache_field_num_per_key)
{
set_thread_name("PikaCacheLoadThread");
}
PikaCacheLoadThread::~PikaCacheLoadThread() {
{
std::lock_guard lq(loadkeys_mutex_);
should_exit_ = true;
loadkeys_cond_.notify_all();
}
StopThread();
}
void PikaCacheLoadThread::Push(const char key_type, std::string& key, const std::shared_ptr<DB>& db) {
std::unique_lock lq(loadkeys_mutex_);
std::unique_lock lm(loadkeys_map_mutex_);
if (CACHE_LOAD_QUEUE_MAX_SIZE < loadkeys_queue_.size()) {
// 5s to print logs once
static uint64_t last_log_time_us = 0;
if (pstd::NowMicros() - last_log_time_us > 5000000) {
LOG(WARNING) << "PikaCacheLoadThread::Push waiting...";
last_log_time_us = pstd::NowMicros();
}
return;
}
if (loadkeys_map_.find(key) == loadkeys_map_.end()) {
std::tuple<const char, std::string, const std::shared_ptr<DB>> ktuple = std::make_tuple(key_type, key, db);
loadkeys_queue_.push_back(ktuple);
loadkeys_map_[key] = std::string("");
loadkeys_cond_.notify_all();
}
}
bool PikaCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr<DB>& db) {
std::string value;
int64_t ttl = -1;
rocksdb::Status s = db->storage()->GetWithTTL(key, &value, &ttl);
if (!s.ok()) {
LOG(WARNING) << "load kv failed, key=" << key;
return false;
}
std::string CachePrefixKeyK = PCacheKeyPrefixK + key;
db->cache()->WriteKVToCache(CachePrefixKeyK, value, ttl);
return true;
}
bool PikaCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr<DB>& db) {
int32_t len = 0;
db->storage()->HLen(key, &len);
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
LOG(WARNING) << "can not load key, because item size:" << len
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
return false;
}
std::vector<storage::FieldValue> fvs;
int64_t ttl = -1;
rocksdb::Status s = db->storage()->HGetallWithTTL(key, &fvs, &ttl);
if (!s.ok()) {
LOG(WARNING) << "load hash failed, key=" << key;
return false;
}
std::string CachePrefixKeyH = PCacheKeyPrefixH + key;
db->cache()->WriteHashToCache(CachePrefixKeyH, fvs, ttl);
return true;
}
bool PikaCacheLoadThread::LoadList(std::string& key, const std::shared_ptr<DB>& db) {
uint64_t len = 0;
db->storage()->LLen(key, &len);
if (len <= 0 || CACHE_VALUE_ITEM_MAX_SIZE < len) {
LOG(WARNING) << "can not load key, because item size:" << len
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
return false;
}
std::vector<std::string> values;
int64_t ttl = -1;
rocksdb::Status s = db->storage()->LRangeWithTTL(key, 0, -1, &values, &ttl);
if (!s.ok()) {
LOG(WARNING) << "load list failed, key=" << key;
return false;
}
std::string CachePrefixKeyL = PCacheKeyPrefixL + key;
db->cache()->WriteListToCache(CachePrefixKeyL, values, ttl);
return true;
}
bool PikaCacheLoadThread::LoadSet(std::string& key, const std::shared_ptr<DB>& db) {
int32_t len = 0;
db->storage()->SCard(key, &len);
if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) {
LOG(WARNING) << "can not load key, because item size:" << len
<< " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE;
return false;
}
std::vector<std::string> values;
int64_t ttl = -1;
rocksdb::Status s = db->storage()->SMembersWithTTL(key, &values, &ttl);
if (!s.ok()) {
LOG(WARNING) << "load set failed, key=" << key;
return false;
}
std::string CachePrefixKeyS = PCacheKeyPrefixS + key;
db->cache()->WriteSetToCache(CachePrefixKeyS, values, ttl);
return true;
}
bool PikaCacheLoadThread::LoadZset(std::string& key, const std::shared_ptr<DB>& db) {
int32_t len = 0;
int start_index = 0;
int stop_index = -1;
db->storage()->ZCard(key, &len);
if (0 >= len) {
return false;
}
uint64_t cache_len = 0;
std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key;
db->cache()->CacheZCard(CachePrefixKeyZ, &cache_len);
if (cache_len != 0) {
return true;
}
if (zset_cache_start_direction_ == cache::CACHE_START_FROM_BEGIN) {
if (zset_cache_field_num_per_key_ <= len) {
stop_index = zset_cache_field_num_per_key_ - 1;
}
} else if (zset_cache_start_direction_ == cache::CACHE_START_FROM_END) {
if (zset_cache_field_num_per_key_ <= len) {
start_index = len - zset_cache_field_num_per_key_;
}
}
std::vector<storage::ScoreMember> score_members;
int64_t ttl = -1;
rocksdb::Status s = db->storage()->ZRangeWithTTL(key, start_index, stop_index, &score_members, &ttl);
if (!s.ok()) {
LOG(WARNING) << "load zset failed, key=" << key;
return false;
}
db->cache()->WriteZSetToCache(CachePrefixKeyZ, score_members, ttl);
return true;
}
bool PikaCacheLoadThread::LoadKey(const char key_type, std::string& key, const std::shared_ptr<DB>& db) {
pstd::lock::ScopeRecordLock record_lock(db->LockMgr(), key);
switch (key_type) {
case 'k':
return LoadKV(key, db);
case 'h':
return LoadHash(key, db);
case 'l':
return LoadList(key, db);
case 's':
return LoadSet(key, db);
case 'z':
return LoadZset(key, db);
default:
LOG(WARNING) << "PikaCacheLoadThread::LoadKey invalid key type : " << key_type;
return false;
}
}
void *PikaCacheLoadThread::ThreadMain() {
LOG(INFO) << "PikaCacheLoadThread::ThreadMain Start";
while (!should_exit_) {
std::deque<std::tuple<const char, std::string, const std::shared_ptr<DB>>> load_keys;
{
std::unique_lock lq(loadkeys_mutex_);
waitting_load_keys_num_ = loadkeys_queue_.size();
while (!should_exit_ && loadkeys_queue_.size() <= 0) {
loadkeys_cond_.wait(lq);
}
if (should_exit_) {
return nullptr;
}
for (int i = 0; i < CACHE_LOAD_NUM_ONE_TIME; ++i) {
if (!loadkeys_queue_.empty()) {
load_keys.push_back(loadkeys_queue_.front());
loadkeys_queue_.pop_front();
}
}
}
for (auto iter = load_keys.begin(); iter != load_keys.end(); ++iter) {
if (LoadKey(std::get<0>(*iter), std::get<1>(*iter), std::get<2>(*iter))) {
++async_load_keys_num_;
} else {
LOG(WARNING) << "PikaCacheLoadThread::ThreadMain LoadKey: " << std::get<1>(*iter) << " failed !!!";
}
std::unique_lock lm(loadkeys_map_mutex_);
loadkeys_map_.erase(std::get<1>(*iter));
}
}
return nullptr;
}