Skip to content

Commit 7fa9355

Browse files
committed
feat(lazyfree): can free replicaKeysWithExpire dict async
Signed-off-by: Scut-Corgis <[email protected]>
1 parent a06cf15 commit 7fa9355

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ long long emptyData(int dbnum, int flags, void(callback)(hashtable *)) {
682682
/* Empty the database structure. */
683683
removed = emptyDbStructure(server.db, dbnum, async, callback);
684684

685-
if (dbnum == -1) flushReplicaKeysWithExpireList();
685+
if (dbnum == -1) flushReplicaKeysWithExpireList(async);
686686

687687
if (with_functions) {
688688
serverAssert(dbnum == -1);
@@ -1796,7 +1796,7 @@ void swapMainDbWithTempDb(serverDb **tempDb) {
17961796
}
17971797

17981798
trackingInvalidateKeysOnFlush(1);
1799-
flushReplicaKeysWithExpireList();
1799+
flushReplicaKeysWithExpireList(1);
18001800
}
18011801

18021802
/* SWAPDB db1 db2 */

src/expire.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,13 @@ size_t getReplicaKeyWithExpireCount(void) {
644644
* but it is not worth it since anyway race conditions using the same set
645645
* of key names in a writable replica and in its primary will lead to
646646
* inconsistencies. This is just a best-effort thing we do. */
647-
void flushReplicaKeysWithExpireList(void) {
647+
void flushReplicaKeysWithExpireList(int async) {
648648
if (replicaKeysWithExpire) {
649-
dictRelease(replicaKeysWithExpire);
649+
if (async) {
650+
freeReplicaKeysWithExpireAsync(replicaKeysWithExpire);
651+
} else {
652+
dictRelease(replicaKeysWithExpire);
653+
}
650654
replicaKeysWithExpire = NULL;
651655
}
652656
}

src/expire.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum activeExpiryType {
5151
typedef struct client client;
5252
typedef struct serverObject robj;
5353
typedef struct serverDb serverDb;
54+
typedef struct dict dict;
5455

5556
/* return the relevant expiration policy based on the current server state and the provided flags.
5657
* FLAGS can indicate either:
@@ -64,8 +65,9 @@ int convertExpireArgumentToUnixTime(client *c, robj *arg, long long basetime, in
6465
void activeExpireCycle(int type);
6566
void expireReplicaKeys(void);
6667
void rememberReplicaKeyWithExpire(serverDb *db, robj *key);
67-
void flushReplicaKeysWithExpireList(void);
68+
void flushReplicaKeysWithExpireList(int async);
6869
size_t getReplicaKeyWithExpireCount(void);
6970
bool timestampIsExpired(mstime_t when);
71+
void freeReplicaKeysWithExpireAsync(dict *replica_keys_with_expire);
7072

7173
#endif

src/lazyfree.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ void lazyFreeReplicationBacklogRefMem(void *args[]) {
8585
atomic_fetch_add_explicit(&lazyfreed_objects, len, memory_order_relaxed);
8686
}
8787

88+
/* Release the replicaKeysWithExpire dict. */
89+
void lazyFreeReplicaKeysWithExpire(void *args[]) {
90+
dict *replica_keys_with_expire = args[0];
91+
dictRelease(replica_keys_with_expire);
92+
}
93+
8894
/* Return the number of currently pending objects to free. */
8995
size_t lazyfreeGetPendingObjectsCount(void) {
9096
size_t aux = atomic_load_explicit(&lazyfree_objects, memory_order_relaxed);
@@ -260,3 +266,13 @@ void freeReplicationBacklogRefMemAsync(list *blocks, rax *index) {
260266
raxFree(index);
261267
}
262268
}
269+
270+
271+
/* Free replicaKeysWithExpire dict, if the dict is huge enough, free it in async way. */
272+
void freeReplicaKeysWithExpireAsync(dict *replica_keys_with_expire) {
273+
if (dictSize(replica_keys_with_expire) > LAZYFREE_THRESHOLD) {
274+
bioCreateLazyFreeJob(lazyFreeReplicaKeysWithExpire,1,replica_keys_with_expire);
275+
} else {
276+
dictRelease(replica_keys_with_expire);
277+
}
278+
}

0 commit comments

Comments
 (0)