Move the 'dirty' flag for the guards to a global again

It makes more sense to have a single dirty flag, since we always
regenerate the whole state file when we save it.
This commit is contained in:
Nick Mathewson 2016-11-23 15:48:10 -05:00
parent bce0f79252
commit e56bc1e5de
2 changed files with 16 additions and 22 deletions

View File

@ -145,6 +145,10 @@
static smartlist_t *guard_contexts = NULL; static smartlist_t *guard_contexts = NULL;
static guard_selection_t *curr_guard_context = NULL; static guard_selection_t *curr_guard_context = NULL;
/** A value of 1 means that at least one context has changed,
* and those changes need to be flushed to disk. */
static int entry_guards_dirty = 0;
static const node_t *choose_random_entry_impl(guard_selection_t *gs, static const node_t *choose_random_entry_impl(guard_selection_t *gs,
cpath_build_state_t *state, cpath_build_state_t *state,
int for_directory, int for_directory,
@ -2027,7 +2031,6 @@ entry_guards_update_guards_in_state(or_state_t *state)
(*nextline)->value = entry_guard_encode_for_state(guard); (*nextline)->value = entry_guard_encode_for_state(guard);
nextline = &(*nextline)->next; nextline = &(*nextline)->next;
} SMARTLIST_FOREACH_END(guard); } SMARTLIST_FOREACH_END(guard);
gs->dirty = 0;
} SMARTLIST_FOREACH_END(gs); } SMARTLIST_FOREACH_END(gs);
config_free_lines(state->Guard); config_free_lines(state->Guard);
@ -3685,11 +3688,11 @@ entry_guards_parse_state_for_guard_selection(
smartlist_free(gs->chosen_entry_guards); smartlist_free(gs->chosen_entry_guards);
} }
gs->chosen_entry_guards = new_entry_guards; gs->chosen_entry_guards = new_entry_guards;
gs->dirty = 0;
/* XXX hand new_entry_guards to this func, and move it up a /* XXX hand new_entry_guards to this func, and move it up a
* few lines, so we don't have to re-dirty it */ * few lines, so we don't have to re-dirty it */
if (remove_obsolete_entry_guards(gs, now)) if (remove_obsolete_entry_guards(gs, now))
gs->dirty = 1; entry_guards_dirty = 1;
} }
digestmap_free(added_by, tor_free_); digestmap_free(added_by, tor_free_);
return *msg ? -1 : 0; return *msg ? -1 : 0;
@ -3704,12 +3707,16 @@ entry_guards_parse_state_for_guard_selection(
int int
entry_guards_parse_state(or_state_t *state, int set, char **msg) entry_guards_parse_state(or_state_t *state, int set, char **msg)
{ {
entry_guards_dirty = 0;
int r1 = entry_guards_load_guards_from_state(state, set); int r1 = entry_guards_load_guards_from_state(state, set);
int r2 = entry_guards_parse_state_for_guard_selection( int r2 = entry_guards_parse_state_for_guard_selection(
get_guard_selection_by_name("legacy", 1), get_guard_selection_by_name("legacy", 1),
state, set, msg); state, set, msg);
entry_guards_dirty = 0;
if (r1 < 0 || r2 < 0) { if (r1 < 0 || r2 < 0) {
if (msg && *msg == NULL) { if (msg && *msg == NULL) {
*msg = tor_strdup("parsing error"); //xxxx prop271 should we try harder? *msg = tor_strdup("parsing error"); //xxxx prop271 should we try harder?
@ -3737,7 +3744,7 @@ entry_guards_changed_for_guard_selection(guard_selection_t *gs)
tor_assert(gs != NULL); tor_assert(gs != NULL);
gs->dirty = 1; entry_guards_dirty = 1;
if (get_options()->AvoidDiskWrites) if (get_options()->AvoidDiskWrites)
when = time(NULL) + SLOW_GUARD_STATE_FLUSH_TIME; when = time(NULL) + SLOW_GUARD_STATE_FLUSH_TIME;
@ -3764,26 +3771,23 @@ entry_guards_changed(void)
* Otherwise, free the EntryGuards piece of <b>state</b> and create * Otherwise, free the EntryGuards piece of <b>state</b> and create
* a new one out of the global entry_guards list, and then mark * a new one out of the global entry_guards list, and then mark
* <b>state</b> dirty so it will get saved to disk. * <b>state</b> dirty so it will get saved to disk.
*
* XXX this should get totally redesigned around storing multiple
* entry guard contexts. For the initial refactor we'll just
* always use the current default. Fix it as soon as we actually
* have any way that default can change.
*/ */
void void
entry_guards_update_state(or_state_t *state) entry_guards_update_state(or_state_t *state)
{ {
config_line_t **next, *line; config_line_t **next, *line;
entry_guards_dirty = 0;
// Handles all non-legacy guard info. // Handles all non-legacy guard info.
entry_guards_update_guards_in_state(state); entry_guards_update_guards_in_state(state);
entry_guards_dirty = 0;
guard_selection_t *gs = get_guard_selection_by_name("legacy", 0); guard_selection_t *gs = get_guard_selection_by_name("legacy", 0);
if (!gs) if (!gs)
return; // nothign to save. return; // nothign to save.
tor_assert(gs->chosen_entry_guards != NULL); tor_assert(gs->chosen_entry_guards != NULL);
if (!gs->dirty)
return;
config_free_lines(state->EntryGuards); config_free_lines(state->EntryGuards);
next = &state->EntryGuards; next = &state->EntryGuards;
@ -3854,7 +3858,7 @@ entry_guards_update_state(or_state_t *state)
} SMARTLIST_FOREACH_END(e); } SMARTLIST_FOREACH_END(e);
if (!get_options()->AvoidDiskWrites) if (!get_options()->AvoidDiskWrites)
or_state_mark_dirty(get_or_state(), 0); or_state_mark_dirty(get_or_state(), 0);
gs->dirty = 0; entry_guards_dirty = 0;
} }
/** If <b>question</b> is the string "entry-guards", then dump /** If <b>question</b> is the string "entry-guards", then dump

View File

@ -212,16 +212,6 @@ struct guard_selection_s {
*/ */
char *name; char *name;
/**
* A value of 1 means that guard_selection_t structures have changed
* and those changes need to be flushed to disk.
*
* XXX prop271 we don't know how to flush multiple guard contexts to
* disk yet; fix that as soon as any way to change the default exists,
* or at least make sure this gets set on change.
*/
int dirty;
/** /**
* A value of 1 means that primary_entry_guards is up-to-date; 0 * A value of 1 means that primary_entry_guards is up-to-date; 0
* means we need to recalculate it before using primary_entry_guards * means we need to recalculate it before using primary_entry_guards