You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by gi...@git.apache.org on 2017/04/30 19:52:25 UTC

[GitHub] davisp commented on issue #501: Fix error on race condition in mem3 startup

davisp commented on issue #501: Fix error on race condition in mem3 startup
URL: https://github.com/apache/couchdb/pull/501#issuecomment-298252919
 
 
   I think the bigger issue here is that `mem3_util:ensure_exists/1` is racey in that if multiple concurrent clients attempt to ensure a missing database exists simultaneously only one of them will succeed. A better approach would be to allow for an expected file_exists return and for the client that receives it to try re-opening a second time. Something along these lines:
   
   ```diff
   diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
   index 4e1b5fd7d..deb2c6640 100644
   --- a/src/mem3/src/mem3_util.erl
   +++ b/src/mem3/src/mem3_util.erl
   @@ -224,11 +224,21 @@ shard_info(DbName) ->
    ensure_exists(DbName) when is_list(DbName) ->
        ensure_exists(list_to_binary(DbName));
    ensure_exists(DbName) ->
   -    case couch_db:open(DbName, [nologifmissing, sys_db | [?ADMIN_CTX]]) of
   -    {ok, Db} ->
   -        {ok, Db};
   -    _ ->
   -        couch_server:create(DbName, [?ADMIN_CTX])
   +    Options = [nologifmissing, sys_db, ?ADMIN_CTX],
   +    case couch_db:open(DbName, Options) of
   +        {ok, Db} ->
   +            {ok, Db};
   +        _ ->
   +            case couch_server:create(DbName, Options) of
   +                {ok, Db} ->
   +                    {ok, Db};
   +                file_exists ->
   +                    % Try opening again in case there was a race to
   +                    % create the database.
   +                    couch_db:open(DbName, Options);
   +                Error ->
   +                    Error
   +            end
        end.
   ```
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services