Skip to content

Commit

Permalink
Check synced repo head commit ids in batch.
Browse files Browse the repository at this point in the history
  • Loading branch information
killing committed Nov 7, 2017
1 parent 5fc440f commit b50b513
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 8 deletions.
2 changes: 2 additions & 0 deletions common/common.h
Expand Up @@ -29,6 +29,8 @@

#define CURRENT_REPO_VERSION 1

#define CURRENT_SYNC_PROTO_VERSION 2

/* For compatibility with the old protocol, use an UUID for signature.
* Listen manager on the server will use the new block tx protocol if it
* receives this signature as "token".
Expand Down
128 changes: 124 additions & 4 deletions daemon/http-tx-mgr.c
Expand Up @@ -2196,6 +2196,126 @@ http_tx_manager_unlock_file (HttpTxManager *manager,
return ret;
}

static char *
repo_id_list_to_json (GList *repo_id_list)
{
json_t *array = json_array();
GList *ptr;
char *repo_id;

for (ptr = repo_id_list; ptr; ptr = ptr->next) {
repo_id = ptr->data;
json_array_append_new (array, json_string(repo_id));
}

char *data = json_dumps (array, JSON_COMPACT);
if (!data) {
seaf_warning ("Failed to dump json.\n");
json_decref (array);
return NULL;
}

json_decref (array);
return data;
}

static GHashTable *
repo_head_commit_map_from_json (const char *json_str, gint64 len)
{
json_t *object;
json_error_t jerror;
GHashTable *ret;

object = json_loadb (json_str, (size_t)len, 0, &jerror);
if (!object) {
seaf_warning ("Failed to load json: %s\n", jerror.text);
return NULL;
}

ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

void *iter = json_object_iter (object);
const char *key;
json_t *value;
while (iter) {
key = json_object_iter_key (iter);
value = json_object_iter_value (iter);
if (json_typeof(value) != JSON_STRING) {
seaf_warning ("Bad json object format when parsing head commit id map.\n");
g_hash_table_destroy (ret);
goto out;
}
g_hash_table_replace (ret, g_strdup (key), g_strdup(json_string_value(value)));
iter = json_object_iter_next (object, iter);
}

out:
json_decref (object);
return ret;
}

GHashTable *
http_tx_manager_get_head_commit_ids (HttpTxManager *manager,
const char *host,
gboolean use_fileserver_port,
GList *repo_id_list)
{
HttpTxPriv *priv = seaf->http_tx_mgr->priv;
ConnectionPool *pool;
Connection *conn;
CURL *curl;
char *url;
char *req_content = NULL;
gint64 req_size;
int status;
char *rsp_content = NULL;
gint64 rsp_size;
GHashTable *map = NULL;

pool = find_connection_pool (priv, host);
if (!pool) {
seaf_warning ("Failed to create connection pool for host %s.\n", host);
return NULL;
}

conn = connection_pool_get_connection (pool);
if (!conn) {
seaf_warning ("Failed to get connection to host %s.\n", host);
return NULL;
}

curl = conn->curl;

if (!use_fileserver_port)
url = g_strdup_printf ("%s/seafhttp/repo/head-commits-multi/", host);
else
url = g_strdup_printf ("%s/repo/head-commits-multi/", host);

req_content = repo_id_list_to_json (repo_id_list);
req_size = strlen(req_content);

if (http_post (curl, url, NULL, req_content, req_size,
&status, &rsp_content, &rsp_size, TRUE, NULL) < 0) {
conn->release = TRUE;
goto out;
}

if (status != HTTP_OK) {
seaf_warning ("Bad response code for POST %s: %d\n", url, status);
goto out;
}

map = repo_head_commit_map_from_json (rsp_content, rsp_size);

out:
g_free (url);
connection_pool_return_connection (pool, conn);
/* returned by json_dumps(). */
free (req_content);
g_free (rsp_content);
return map;
}

static gboolean
remove_task_help (gpointer key, gpointer value, gpointer user_data)
{
Expand Down Expand Up @@ -3419,8 +3539,8 @@ http_upload_thread (void *vdata)
goto out;
}

seaf_message ("Upload with HTTP sync protocol version %d.\n",
task->protocol_version);
/* seaf_message ("Upload with HTTP sync protocol version %d.\n", */
/* task->protocol_version); */

transition_state (task, task->state, HTTP_TASK_RT_STATE_CHECK);

Expand Down Expand Up @@ -4284,8 +4404,8 @@ http_download_thread (void *vdata)
goto out;
}

seaf_message ("Download with HTTP sync protocol version %d.\n",
task->protocol_version);
/* seaf_message ("Download with HTTP sync protocol version %d.\n", */
/* task->protocol_version); */

transition_state (task, task->state, HTTP_TASK_RT_STATE_CHECK);

Expand Down
6 changes: 6 additions & 0 deletions daemon/http-tx-mgr.h
Expand Up @@ -286,6 +286,12 @@ http_tx_manager_unlock_file (HttpTxManager *manager,
const char *repo_id,
const char *path);

GHashTable *
http_tx_manager_get_head_commit_ids (HttpTxManager *manager,
const char *host,
gboolean use_fileserver_port,
GList *repo_id_list);

int
http_tx_task_download_file_blocks (HttpTxTask *task, const char *file_id);

Expand Down
25 changes: 25 additions & 0 deletions daemon/repo-mgr.c
Expand Up @@ -7874,6 +7874,31 @@ seaf_repo_manager_get_repo_list (SeafRepoManager *manager, int start, int limit)
return repo_list;
}

GList *
seaf_repo_manager_get_repo_id_list_by_server (SeafRepoManager *manager, const char *server_url)
{
GList *repo_id_list = NULL;
GHashTableIter iter;
SeafRepo *repo;
gpointer key, value;

if (pthread_rwlock_rdlock (&manager->priv->lock) < 0) {
seaf_warning ("[repo mgr] failed to lock repo cache.\n");
return NULL;
}
g_hash_table_iter_init (&iter, manager->priv->repo_hash);

while (g_hash_table_iter_next (&iter, &key, &value)) {
repo = value;
if (!repo->delete_pending && g_strcmp0 (repo->server_url, server_url) == 0)
repo_id_list = g_list_prepend (repo_id_list, g_strdup(repo->id));
}

pthread_rwlock_unlock (&manager->priv->lock);

return repo_id_list;
}

typedef struct {
SeafRepo *repo;
CheckoutTask *task;
Expand Down
3 changes: 3 additions & 0 deletions daemon/repo-mgr.h
Expand Up @@ -245,6 +245,9 @@ seaf_repo_manager_repo_exists (SeafRepoManager *manager, const gchar *id);
GList*
seaf_repo_manager_get_repo_list (SeafRepoManager *mgr, int start, int limit);

GList *
seaf_repo_manager_get_repo_id_list_by_server (SeafRepoManager *mgr, const char *server_url);

GList *
seaf_repo_manager_list_garbage_repos (SeafRepoManager *mgr);

Expand Down

0 comments on commit b50b513

Please sign in to comment.