Skip to content

Commit

Permalink
Optimize to check if block exists
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Oct 24, 2024
1 parent dd33759 commit b993bbf
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions server/gc/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ typedef struct VerifyData {
SeafRepo *repo;
gint64 truncate_time;
gboolean traversed_head;
GHashTable *exist_blocks;
} VerifyData;

static int
Expand All @@ -25,11 +26,9 @@ check_blocks (VerifyData *data, const char *file_id)
}

for (i = 0; i < seafile->n_blocks; ++i) {
if (!seaf_block_manager_block_exists (seaf->block_mgr,
repo->store_id,
repo->version,
seafile->blk_sha1s[i]))
g_message ("Block %s is missing.\n", seafile->blk_sha1s[i]);
if (!g_hash_table_lookup(data->exist_blocks, seafile->blk_sha1s[i])) {
seaf_message ("Block %s is missing.\n", seafile->blk_sha1s[i]);
}
}

seafile_unref (seafile);
Expand Down Expand Up @@ -136,6 +135,18 @@ verify_virtual_repos (VerifyData *data)

}

static gboolean
collect_exist_blocks (const char *store_id, int version,
const char *block_id, void *vdata)
{
GHashTable *exist_blocks = vdata;
char *copy = g_strdup (block_id);

g_hash_table_replace (exist_blocks, copy, copy);

return TRUE;
}

static int
verify_repo (SeafRepo *repo)
{
Expand All @@ -147,10 +158,22 @@ verify_repo (SeafRepo *repo)
data.repo = repo;
data.truncate_time = seaf_repo_manager_get_repo_truncate_time (repo->manager,
repo->id);
data.exist_blocks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
ret = seaf_block_manager_foreach_block (seaf->block_mgr,
repo->store_id, repo->version,
collect_exist_blocks,
data.exist_blocks);
if (ret < 0) {
seaf_warning ("Failed to collect existing blocks for repo %.8s, stop GC.\n\n",
repo->id);
g_hash_table_destroy (data.exist_blocks);
return ret;
}

branches = seaf_branch_manager_get_branch_list (seaf->branch_mgr, repo->id);
if (branches == NULL) {
seaf_warning ("[GC] Failed to get branch list of repo %s.\n", repo->id);
g_hash_table_destroy (data.exist_blocks);
return -1;
}

Expand All @@ -172,11 +195,13 @@ verify_repo (SeafRepo *repo)
g_list_free (branches);

if (ret < 0) {
g_hash_table_destroy (data.exist_blocks);
return ret;
}

ret = verify_virtual_repos (&data);

g_hash_table_destroy (data.exist_blocks);
return ret;
}

Expand All @@ -198,13 +223,18 @@ verify_repos (GList *repo_id_list)
if (!repo)
continue;

seaf_message ("Start to verify repo %s\n", repo->id);
if (repo->is_corrupted) {
seaf_warning ("Repo %s is corrupted.\n", repo->id);
} else {
ret = verify_repo (repo);
if (ret < 0) {
seaf_warning ("Failed to verify repo %s\n", repo->id);
seaf_repo_unref (repo);
continue;
}
seaf_message ("Verify repo %s success\n", repo->id);
seaf_repo_unref (repo);
if (ret < 0)
break;
}
}

Expand Down

0 comments on commit b993bbf

Please sign in to comment.