Skip to content

Commit 44e5969

Browse files
authored
Merge pull request #17 from threefoldtech/temp-cleanup
cleanup: flush temporary namespace when possible
2 parents f1eba86 + 62ed95e commit 44e5969

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/cache.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ static int zdbfs_cache_block_restore(zdbfs_t *fs, inocache_t *cache, blockcache_
255255
zdbfs_lowdebug("cache: block offloaded restored, %lu bytes read", block->blocksize);
256256
zdbfs_zdb_reply_free(reply);
257257

258+
// delete that block from temporary namespace
259+
if(zdb_del(fs->tempctx, block->offid) != 0) {
260+
zdbfs_debug("[-] cache: temporary: could not delete key: %u\n", block->offid);
261+
return 0;
262+
}
263+
264+
// ensure blockid is not set anymore
265+
block->offid = 0;
266+
258267
return 0;
259268
}
260269

@@ -583,13 +592,51 @@ int zdbfs_cache_release(fuse_req_t req, inocache_t *cache) {
583592
return 1;
584593
}
585594

595+
size_t zdbfs_cache_temp_cleanup(zdbfs_t *fs) {
596+
zdb_nsinfo_t *temp;
597+
598+
if(!(temp = zdb_nsinfo(fs->tempctx, fs->opts->temp_ns)))
599+
return 0;
600+
601+
if(temp->nextid > 32 && temp->entries == 1) {
602+
zdbfs_lowdebug("cache: temporary namespace not empty and not in use: %lu", temp->nextid);
603+
604+
zdb_reply_t *reply;
605+
redisReply *zreply;
606+
607+
zdbfs_lowdebug("cache: %s: backing up temporary namespace header", fs->opts->temp_ns);
608+
if(!(reply = zdb_get(fs->tempctx, 0))) {
609+
zdbfs_critical("cache: delegate: could not get header: %s", fs->tempctx->errstr);
610+
return 1;
611+
}
612+
613+
zdbfs_lowdebug("cache: %s: flushing temporary namespace", fs->opts->temp_ns);
614+
zdb_flush(fs->tempctx);
615+
616+
zdbfs_lowdebug("cache: %s: restoring temporary namespace header", fs->opts->temp_ns);
617+
618+
if(!(zreply = redisCommand(fs->tempctx, "SET %b %b", NULL, 0, reply->value, reply->length))) {
619+
zdbfs_critical("inode: temporary reset: %s", fs->tempctx->errstr);
620+
return 1;
621+
}
622+
623+
zdbfs_zdb_reply_free(reply);
624+
freeReplyObject(zreply);
625+
}
626+
627+
return 1;
628+
}
629+
586630
size_t zdbfs_cache_sync(zdbfs_t *fs) {
587631
size_t cleared = 0;
588632

589633
// runtime cache disabled
590634
if(!zdbfs_cache_enabled(fs))
591635
return 0;
592636

637+
// check if the temporary namespace needs to be reset
638+
zdbfs_cache_temp_cleanup(fs);
639+
593640
// checking each cache entries and check
594641
// if entry were added few time ago or more
595642
//

src/zdb.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ int zdb_nsnew(redisContext *remote, char *namespace) {
6767
return 0;
6868
}
6969

70+
int zdb_flush(redisContext *remote) {
71+
const char *argv[] = {"FLUSH"};
72+
redisReply *reply;
73+
74+
zdbfs_debug("[+] zdb: flush: namespace: %p\n", remote);
75+
76+
if(!(reply = redisCommandArgv(remote, 1, argv, NULL))) {
77+
zdbfs_critical("zdb: flush: %p: %s", remote, remote->errstr);
78+
return 1;
79+
}
80+
81+
if(strcmp(reply->str, "OK") != 0) {
82+
zdbfs_error("zdb: flush: %p: %s", remote, reply->str);
83+
freeReplyObject(reply);
84+
return 1;
85+
}
86+
87+
freeReplyObject(reply);
88+
89+
return 0;
90+
}
91+
7092
int zdb_nsset(redisContext *remote, char *namespace, char *setting, char *value) {
7193
const char *argv[] = {"NSSET", namespace, setting, value};
7294
int argc = 4;
@@ -101,6 +123,30 @@ static size_t zdb_nsinfo_sizeval(char *buffer, char *entry) {
101123
return strtoumax(match, NULL, 10);
102124
}
103125

126+
static size_t zdb_nsinfo_internal_id(char *buffer, char *entry) {
127+
char *match;
128+
char numbuf[64];
129+
130+
if(!(match = strstr(buffer, entry)))
131+
return 0;
132+
133+
match += strlen(entry) + 2;
134+
135+
memset(numbuf, 0x00, sizeof(numbuf));
136+
sprintf(numbuf, "0x");
137+
138+
// FIXME: loop ? better way ?
139+
// convert 0xaabbccdd to 0xddccbbaa
140+
memcpy(numbuf + 2, match + 8, 2);
141+
memcpy(numbuf + 4, match + 6, 2);
142+
memcpy(numbuf + 6, match + 4, 2);
143+
memcpy(numbuf + 8, match + 2, 2);
144+
145+
// return strtoul(match, NULL, 0);
146+
return strtoul(numbuf, NULL, 0);
147+
}
148+
149+
104150
static int zdb_locked(redisReply *reply) {
105151
if(reply->type != REDIS_REPLY_ERROR)
106152
return 0;
@@ -140,6 +186,7 @@ zdb_nsinfo_t *zdb_nsinfo(redisContext *remote, char *namespace) {
140186

141187
nsinfo->entries = zdb_nsinfo_sizeval(reply->str, "entries");
142188
nsinfo->datasize = zdb_nsinfo_sizeval(reply->str, "data_size_bytes");
189+
nsinfo->nextid = zdb_nsinfo_internal_id(reply->str, "next_internal_id");
143190

144191
freeReplyObject(reply);
145192

src/zdb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
typedef struct zdb_nsinfo_t {
77
size_t entries;
88
size_t datasize;
9+
size_t nextid;
910

1011
} zdb_nsinfo_t;
1112

1213
int zdbfs_zdb_connect(zdbfs_t *fs);
1314
void zdbfs_zdb_free(zdbfs_t *fs);
1415

16+
int zdb_flush(redisContext *remote);
1517
zdb_reply_t *zdb_get(redisContext *remote, uint32_t id);
1618
uint32_t zdb_set(redisContext *remote, uint32_t id, const void *buffer, size_t length);
1719
int zdb_del(redisContext *remote, uint32_t id);

0 commit comments

Comments
 (0)