aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-05-03 17:19:35 -0400
committerNick Mathewson <nickm@torproject.org>2011-05-03 17:19:35 -0400
commit865d53be5484ef06ca4a5103d9ebca343e19f7d4 (patch)
tree6cf1970fedf5f628f04f65c9f6af383af6cf0249 /src
parent97a55b80894b2816f435a1ee06a2549f0da72939 (diff)
parentcb6c909664f97f751fe78c0aa3205a9042760c53 (diff)
downloadtor-865d53be5484ef06ca4a5103d9ebca343e19f7d4.tar
tor-865d53be5484ef06ca4a5103d9ebca343e19f7d4.tar.gz
Merge remote-tracking branch 'origin/maint-0.2.2'
Conflicts: src/or/microdesc.c
Diffstat (limited to 'src')
-rw-r--r--src/or/microdesc.c45
-rw-r--r--src/or/microdesc.h2
-rw-r--r--src/test/test_microdesc.c4
3 files changed, 35 insertions, 16 deletions
diff --git a/src/or/microdesc.c b/src/or/microdesc.c
index 521ee0709..5d3f1c372 100644
--- a/src/or/microdesc.c
+++ b/src/or/microdesc.c
@@ -214,6 +214,8 @@ microdescs_add_list_to_cache(microdesc_cache_t *cache,
if (md2->last_listed < md->last_listed)
md2->last_listed = md->last_listed;
microdesc_free(md);
+ if (where != SAVED_NOWHERE)
+ cache->bytes_dropped += size;
continue;
}
@@ -243,14 +245,7 @@ microdescs_add_list_to_cache(microdesc_cache_t *cache,
if (f)
finish_writing_to_file(open_file); /*XXX Check me.*/
- {
- size_t old_content_len =
- cache->cache_content ? cache->cache_content->size : 0;
- if (cache->journal_len > 16384 + old_content_len &&
- cache->journal_len > old_content_len / 2) {
- microdesc_cache_rebuild(cache);
- }
- }
+ microdesc_cache_rebuild(cache, 0/* only as needed */);
{
networkstatus_t *ns = networkstatus_get_latest_consensus();
@@ -318,6 +313,9 @@ microdesc_cache_reload(microdesc_cache_t *cache)
}
log_notice(LD_DIR, "Reloaded microdescriptor cache. Found %d descriptors.",
total);
+
+ microdesc_cache_clean(cache, 0, 0);
+
return 0;
}
@@ -368,11 +366,30 @@ microdesc_cache_clean(microdesc_cache_t *cache, time_t cutoff, int force)
}
}
+static int
+should_rebuild_md_cache(microdesc_cache_t *cache)
+{
+ const size_t old_len =
+ cache->cache_content ? cache->cache_content->size : 0;
+ const size_t journal_len = cache->journal_len;
+ const size_t dropped = cache->bytes_dropped;
+
+ if (journal_len < 16384)
+ return 0; /* Don't bother, not enough has happened yet. */
+ if (dropped > (journal_len + old_len) / 3)
+ return 1; /* We could save 1/3 or more of the currently used space. */
+ if (journal_len > old_len / 2)
+ return 1; /* We should append to the regular file */
+
+ return 0;
+}
+
/** Regenerate the main cache file for <b>cache</b>, clear the journal file,
* and update every microdesc_t in the cache with pointers to its new
- * location. */
+ * location. If <b>force</b> is true, do this unconditionally. If
+ * <b>force</b> is false, do it only if we expect to save space on disk. */
int
-microdesc_cache_rebuild(microdesc_cache_t *cache)
+microdesc_cache_rebuild(microdesc_cache_t *cache, int force)
{
open_file_t *open_file;
FILE *f;
@@ -382,12 +399,14 @@ microdesc_cache_rebuild(microdesc_cache_t *cache)
off_t off = 0;
int orig_size, new_size;
- log_info(LD_DIR, "Rebuilding the microdescriptor cache...");
-
/* Remove dead descriptors */
microdesc_cache_clean(cache, 0/*cutoff*/, 0/*force*/);
- /* Calculate starting disk usage */
+ if (!force && !should_rebuild_md_cache(cache))
+ return 0;
+
+ log_info(LD_DIR, "Rebuilding the microdescriptor cache...");
+
orig_size = (int)(cache->cache_content ? cache->cache_content->size : 0);
orig_size += (int)cache->journal_len;
diff --git a/src/or/microdesc.h b/src/or/microdesc.h
index d96d34a77..c967742aa 100644
--- a/src/or/microdesc.h
+++ b/src/or/microdesc.h
@@ -23,7 +23,7 @@ smartlist_t *microdescs_add_list_to_cache(microdesc_cache_t *cache,
int no_save);
void microdesc_cache_clean(microdesc_cache_t *cache, time_t cutoff, int force);
-int microdesc_cache_rebuild(microdesc_cache_t *cache);
+int microdesc_cache_rebuild(microdesc_cache_t *cache, int force);
int microdesc_cache_reload(microdesc_cache_t *cache);
void microdesc_cache_clear(microdesc_cache_t *cache);
diff --git a/src/test/test_microdesc.c b/src/test/test_microdesc.c
index 5b14cdb3e..5a61c5dad 100644
--- a/src/test/test_microdesc.c
+++ b/src/test/test_microdesc.c
@@ -160,7 +160,7 @@ test_md_cache(void *data)
tt_str_op(smartlist_get(md3->family, 0), ==, "nodeX");
/* Now rebuild the cache! */
- tt_int_op(microdesc_cache_rebuild(mc), ==, 0);
+ tt_int_op(microdesc_cache_rebuild(mc, 1), ==, 0);
tt_int_op(md1->saved_location, ==, SAVED_IN_CACHE);
tt_int_op(md2->saved_location, ==, SAVED_IN_CACHE);
@@ -208,7 +208,7 @@ test_md_cache(void *data)
md3 = NULL; /* it's history now! */
/* rebuild again, make sure it stays gone. */
- microdesc_cache_rebuild(mc);
+ microdesc_cache_rebuild(mc, 1);
tt_ptr_op(md1, ==, microdesc_cache_lookup_by_digest256(mc, d1));
tt_ptr_op(md2, ==, microdesc_cache_lookup_by_digest256(mc, d2));
tt_ptr_op(NULL, ==, microdesc_cache_lookup_by_digest256(mc, d3));