You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "dongdongwcpp (via GitHub)" <gi...@apache.org> on 2023/03/06 06:49:03 UTC
[GitHub] [incubator-kvrocks] dongdongwcpp opened a new issue, #1298: race condition for subkey compaction thread and Expire command
dongdongwcpp opened a new issue, #1298:
URL: https://github.com/apache/incubator-kvrocks/issues/1298
### Search before asking
- [X] I had searched in the [issues](https://github.com/apache/incubator-kvrocks/issues) and found no similar issues.
### Version
all version had the same problem.
### Minimal reproduce step
### step 1:slowdown compacton thread@ src/storage/compact_filter.cc
```c++
bool SubKeyFilter::IsMetadataExpired(const InternalKey &ikey, const Metadata &metadata) const {
if (metadata.Type() == kRedisString // metadata key was overwrite by set command
|| metadata.Expired() || ikey.GetVersion() != metadata.version) {
std::cout <<"exp:"<< metadata.expire << ",mk:"<< ikey.GetKey().ToString() << "=>" << ikey.GetSubKey().ToString() <<std::endl;
cached_key_.clear(); // meta TTL will be update in another thread,so don't cache it
usleep(100000); // slow down
return true;
}
return false;
}
```
### step 2: slow down for update TTL after check expired@ src/storage/redis_db.cc
```c++
rocksdb::Status Database::Expire(const Slice &user_key, int timestamp) {
....
if (metadata.Expired()) {
return rocksdb::Status::NotFound(kErrMsgKeyExpired);
}
...
sleep(2);
....
}
```
### step 3: a unit test @ tests/cppunit/t_metadata_test.cc
```c++
#include <random>
#include <thread>
using namespace std;
std::string GenRandomStr(size_t len){
static constexpr const char* DEFAULT_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int64_t now = rocksdb::Env::Default()->NowNanos();
static std::default_random_engine random{static_cast<unsigned>(now)};
static std::mt19937 random_generator(random());
std::string rand_str_out = std::string(DEFAULT_CHARSET);
while(len > rand_str_out.length()) rand_str_out +=rand_str_out ;
std::shuffle(rand_str_out.begin(), rand_str_out.end(), random_generator);
return rand_str_out.substr(0,len);
}
TEST_F(RedisTypeTest, ExpireBug) {
bool start_compact = false;
bool compact_started = false;
bool stop_running = false;
std::thread compact_thread([&](){
while(!stop_running){
if(start_compact){
rocksdb::CompactRangeOptions compact_opts;
compact_opts.change_level = true;
compact_started = true;
auto s = storage_->GetDB()->CompactRange(compact_opts,nullptr,nullptr);// compaction subkey,default cf only
ASSERT_TRUE(s.ok()) << s.ToString();
}else{
// waiting to start compact
usleep(10000);
}
}
});
int uk_count = 50;
auto cfs = storage_->GetCFHandles();
std::vector<FieldValue> mk_field_values;
std::string mk = GenRandomStr(10);
set<string> field_no_dup ;
for(int j=0;j<uk_count;j++){
string field = GenRandomStr(200);
field_no_dup.insert(field);
mk_field_values.push_back(FieldValue{field,"0000"});
}
EXPECT_EQ(field_no_dup.size(),uk_count);
int ret = 0;
rocksdb::Status s = hash->MSet(mk, mk_field_values, false, &ret);
EXPECT_TRUE(s.ok() && uk_count == ret);
int64_t now;
rocksdb::Env::Default()->GetCurrentTime(&now);
int ttl_exp = now + 5;
hash->Expire(mk,ttl_exp);
for(auto&item:*cfs){
rocksdb::FlushOptions fs;
auto s = storage_->GetDB()->Flush(fs,item);
cout << "flush done for:"<< item->GetName() <<",res:" << s.ToString()<<endl;
}
sleep(2);
start_compact = true;
cout << "start compact " <<endl;
while(!compact_started ){
usleep(10);
}
//part field will be expired..
bool lease_TTL_successed = false;
rocksdb::Env::Default()->GetCurrentTime(&now);
s = hash->Expire(mk,(int)(now+100));
if(s.ok()){
lease_TTL_successed = true;
}
if(!lease_TTL_successed){
start_compact = false;
cout << "try another round"<<endl;
return;
}
sleep(2);
vector<FieldValue> vret;
s = hash->GetAll(mk,&vret);
if(s.ok()){
EXPECT_EQ(vret.size(),uk_count);
}else{
cout << "mk:"<< mk << " is gone"<<endl;
}
stop_running = true;
compact_thread.join();
}
```
### step 4 compile and run:
`make unittest && rm -rf testsdb/&& ./unittest --gtest_filter=RedisTypeTest.ExpireBug`
### What did you expect to see?
no race condition, the key either expired or kept
### What did you see instead?
a race condition between the Expire command and the compaction thread leads to the deletion of certain fields/members from a hash/zset/set/list。

Specifically,this issue occurs when a key's TTL was expiring while the Expire command's Exprire checking passed and the same time,the `SubKeyFilter(SubKeyFilter::IsMetadataExpired)` removes some subkey.

now the key's TTL is updated success but some field/membert were disappeared.
### Anything Else?
a lazy delete of the subkey may by plus 1 min when a compaction thread checking meta's Expred for subkey may help.
### Are you willing to submit a PR?
- [X] I'm willing to submit a PR!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-kvrocks] git-hulk closed issue #1298: race condition between subkey compaction thread and Expire command
Posted by "git-hulk (via GitHub)" <gi...@apache.org>.
git-hulk closed issue #1298: race condition between subkey compaction thread and Expire command
URL: https://github.com/apache/incubator-kvrocks/issues/1298
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-kvrocks] git-hulk commented on issue #1298: race condition between subkey compaction thread and Expire command
Posted by "git-hulk (via GitHub)" <gi...@apache.org>.
git-hulk commented on issue #1298:
URL: https://github.com/apache/incubator-kvrocks/issues/1298#issuecomment-1455547899
@dongdongwcpp Thanks for your feedback. Agree that lazy deletion can fix this corner case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org