summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul J. Davis <paul.joseph.davis@gmail.com>2017-07-12 15:50:17 -0500
committerPaul J. Davis <paul.joseph.davis@gmail.com>2017-07-12 16:43:52 -0500
commitd3e06625015cac765620ec11eb3fea518fb14016 (patch)
treea622b1aef9725c63ad31e763022f02ed487802cb
parent348accf5e8c1e0cdcae4803efc3bad2c7149d362 (diff)
downloadcouchdb-d3e06625015cac765620ec11eb3fea518fb14016.tar.gz
Prevent a terrible race condition
Looking into #649 I realized there's a pretty terrible race condition if an index is compacted quickly followed by an index update. Since we don't check the index updater message it would be possible for us to swap out a compaction change, followed by immediately resetting to the new state from the index updater. This would be bad as we'd possibly end up with a situation where our long lived index would be operating on a file that no longer existed on disk.
-rw-r--r--src/couch_index/src/couch_index.erl49
1 files changed, 31 insertions, 18 deletions
diff --git a/src/couch_index/src/couch_index.erl b/src/couch_index/src/couch_index.erl
index 9da928dac..604d5038c 100644
--- a/src/couch_index/src/couch_index.erl
+++ b/src/couch_index/src/couch_index.erl
@@ -230,24 +230,37 @@ handle_cast({new_state, NewIdxState}, State) ->
mod=Mod,
idx_state=OldIdxState
} = State,
- assert_signature_match(Mod, OldIdxState, NewIdxState),
- CurrSeq = Mod:get(update_seq, NewIdxState),
- Args = [
- Mod:get(db_name, NewIdxState),
- Mod:get(idx_name, NewIdxState),
- CurrSeq
- ],
- couch_log:debug("Updated index for db: ~s idx: ~s seq: ~B", Args),
- Rest = send_replies(State#st.waiters, CurrSeq, NewIdxState),
- case State#st.committed of
- true -> erlang:send_after(commit_delay(), self(), commit);
- false -> ok
- end,
- {noreply, State#st{
- idx_state=NewIdxState,
- waiters=Rest,
- committed=false
- }};
+ OldFd = Mod:get(fd, OldIdxState),
+ NewFd = Mod:get(fd, NewIdxState),
+ case NewFd == OldFd of
+ true ->
+ assert_signature_match(Mod, OldIdxState, NewIdxState),
+ CurrSeq = Mod:get(update_seq, NewIdxState),
+ Args = [
+ Mod:get(db_name, NewIdxState),
+ Mod:get(idx_name, NewIdxState),
+ CurrSeq
+ ],
+ couch_log:debug("Updated index for db: ~s idx: ~s seq: ~B", Args),
+ Rest = send_replies(State#st.waiters, CurrSeq, NewIdxState),
+ case State#st.committed of
+ true -> erlang:send_after(commit_delay(), self(), commit);
+ false -> ok
+ end,
+ {noreply, State#st{
+ idx_state=NewIdxState,
+ waiters=Rest,
+ committed=false
+ }};
+ false ->
+ Fmt = "Ignoring update from old indexer for db: ~s idx: ~s",
+ Args = [
+ Mod:get(db_name, NewIdxState),
+ Mod:get(idx_name, NewIdxState)
+ ],
+ couch_log:warning(Fmt, Args),
+ {noreply, State}
+ end;
handle_cast({update_error, Error}, State) ->
send_all(State#st.waiters, Error),
{noreply, State#st{waiters=[]}};