Skip to content

Commit 08d2227

Browse files
committed
Fix race in writes
1 parent e5f8e77 commit 08d2227

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/AsyncWorkQueue.h

+9
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ class AsyncWorkQueue
3737
void shutdown();
3838

3939
void abandonThreads();
40+
41+
void waitForEmpty() {
42+
for (;;) {
43+
std::unique_lock<std::mutex> l(m_mutex);
44+
if (m_workqueue.empty())
45+
return;
46+
sched_yield();
47+
}
48+
}
4049
};

src/debug.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ void computeDatasetDigest(unsigned char *final) {
298298

299299
memset(final,0,20); /* Start with a clean result */
300300

301+
// For test reliabilty ensure all writes are applied
302+
if (g_pserver->m_pstorageFactory) {
303+
g_pserver->asyncwriteworkqueue->waitForEmpty();
304+
}
305+
301306
for (j = 0; j < cserver.dbnum; j++) {
302307
redisDb *db = g_pserver->db[j];
303308

src/replication.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,12 @@ int startBgsaveForReplication(int mincapa) {
13241324
serverLog(LL_NOTICE,"Starting BGSAVE for SYNC with target: %s",
13251325
socket_target ? "replicas sockets" : "disk");
13261326

1327+
// When FLASH is enabled we have a seperate responsibility of ensuring the write queue is flushed otherwise
1328+
// the data won't match the repl buffer
1329+
if (g_pserver->m_pstorageFactory) {
1330+
g_pserver->asyncwriteworkqueue->waitForEmpty();
1331+
}
1332+
13271333
rdbSaveInfo rsi, *rsiptr;
13281334
rsiptr = rdbPopulateSaveInfo(&rsi);
13291335
/* Only do rdbSave* when rsiptr is not NULL,

src/storage/rocksdb.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,12 @@ StorageToken* RocksDBStorageProvider::begin_endWriteBatch(struct aeEventLoop *el
344344
tok->tspbatch = std::move(m_spbatch);
345345
tok->tspdb = m_spdb;
346346
m_spbatch = nullptr;
347-
m_lock.unlock();
348-
(*m_pfactory->m_wwqueue)->AddWorkFunction([this, el,callback,tok]{
347+
// This has a race with future writes so async is disabled
348+
//(*m_pfactory->m_wwqueue)->AddWorkFunction([this, el,callback,tok]{
349349
tok->tspdb->Write(WriteOptions(),tok->tspbatch.get()->GetWriteBatch());
350350
aePostFunction(el,callback,tok);
351-
});
351+
//});
352+
m_lock.unlock();
352353

353354
return tok;
354355
}

0 commit comments

Comments
 (0)