diff --git a/syncer.c b/syncer.c index 23cdfa7..7e7666b 100644 --- a/syncer.c +++ b/syncer.c @@ -350,12 +350,29 @@ sync_gather (GNode *root, uint64_t timestamp) pthread_rwlock_unlock (&paths_lock); } +void +sync_write_tree (sync_partner *sp, GNode *data) +{ + if (APTERYX_NUM_NODES (data) > 0) + { + char *next_path = NULL; + + if (asprintf (&next_path, "%s:/", sp->socket) > 0) + { + free (data->data); + data->data = next_path; + apteryx_set_tree (data); + } + } +} + bool resync () { /* Called under a lock */ GList *iter; - GNode *data = NULL; + GNode *new_data = NULL; + GNode *sync_data = NULL; static uint64_t last_sync_local = 0; uint64_t local_ts = apteryx_timestamp ("/"); @@ -363,66 +380,37 @@ resync () for (iter = partners; iter; iter = iter->next) { sync_partner *sp = iter->data; + /* Sync the entire tree to new partners */ if (sp->new_joiner) { - if (data == NULL) + if (new_data == NULL) { - data = APTERYX_NODE (NULL, strdup ("/")); + new_data = APTERYX_NODE (NULL, strdup ("/")); /* Get everything */ - sync_gather (data, 0); - } - if (APTERYX_NUM_NODES (data) > 0) - { - char *next_path = NULL; - - if (asprintf (&next_path, "%s:/", sp->socket) > 0) - { - free (data->data); - data->data = next_path; - apteryx_set_tree (data); - } + sync_gather (new_data, 0); } + sync_write_tree (sp, new_data); + sp->new_joiner = false; } - } - - if (data) - { - apteryx_free_tree (data); - data = NULL; - } - - /* Grab all the data that has been added since the last update... */ - data = APTERYX_NODE (NULL, strdup ("/")); - sync_gather (data, last_sync_local); - - if (APTERYX_NUM_NODES (data) > 0) - { - for (iter = partners; iter; iter = iter->next) + else { - sync_partner *sp = iter->data; - if (!sp->new_joiner) + /* Sync changes since the last timestamp to existing partners */ + if (sync_data == NULL) { - char *next_path = NULL; - - if (asprintf (&next_path, "%s:/", sp->socket) > 0) - { - free (data->data); - data->data = next_path; - apteryx_set_tree (data); - } - } - else - { - /* These ones will have got all the data above */ - sp->new_joiner = false; + sync_data = APTERYX_NODE (NULL, strdup ("/")); + sync_gather (sync_data, last_sync_local); } + sync_write_tree (sp, sync_data); } } - if (data) + if (new_data) + { + apteryx_free_tree (new_data); + } + if (sync_data) { - apteryx_free_tree (data); - data = NULL; + apteryx_free_tree (sync_data); } last_sync_local = local_ts;