Skip to content
This repository has been archived by the owner. It is now read-only.
Browse files
Sync _security objects on interactive request
We've known for some time that the _security object can occasionally
diverge across a cluster. This ends up leading to intermittent client
errors as they access different _security settings depending on which
node happens to be handling the request.

This patch works by opening the _security object for all copies of all
shards in the cluster and then checks to see if they are the same object
or not. If there is a single version across the cluster then all is
well. If two versions exist, and the minority are an empty object, then
the majority version is set on all shards in the cluster. Otherwise an
error is logged for human intervention.
  • Loading branch information
davisp committed Jan 28, 2012
1 parent 49614f1 commit ba252b99c4d80ebede30ed7dcbf32797df162d1e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
@@ -16,6 +16,7 @@

-export([start/0, stop/0, restart/0, nodes/0, node_info/2, shards/1, shards/2,
choose_shards/2, n/1, dbname/1, ushards/1]).
-export([sync_security/0, sync_security/1]).
-export([compare_nodelists/0, compare_shards/1]).

@@ -128,6 +129,12 @@ live_shards(DbName) ->
Nodes = [node()|erlang:nodes()],
[S || #shard{node=Node} = S <- shards(DbName), lists:member(Node, Nodes)].

sync_security() ->

sync_security(Db) ->

-spec choose_shards(DbName::iodata(), Options::list()) -> [#shard{}].
choose_shards(DbName, Options) when is_list(DbName) ->
choose_shards(list_to_binary(DbName), Options);
@@ -0,0 +1,78 @@
% Copyright 2010 Cloudant
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.


-export([go/0, go/1]).

go() ->
{ok, Dbs} = fabric:all_dbs(),
lists:foreach(fun handle_db/1, Dbs).

go(DbName) when is_binary(DbName) ->

handle_db(DbName) ->
case get_all_security(DbName) of
{ok, SecObjs} ->
case is_ok(SecObjs) of
ok ->
{fixable, SecObj} ->
twig:log(info, "Sync security object for ~p: ~p", [DbName, SecObj]),
case fabric:set_security(DbName, SecObj) of
ok -> ok;
Error ->
twig:log(err, "Error setting security object in ~p: ~p",
[DbName, Error])
broken ->
twig:log(err, "Bad security object in ~p: ~p", [DbName, SecObjs])
Error ->
twig:log(err, "Error getting security objects for ~p: ~p", [
DbName, Error])

get_all_security(DbName) ->
case fabric:get_all_security(DbName) of
{ok, SecObjs} ->
SecObjsDict = lists:foldl(fun({_, SO}, Acc) ->
dict:update_counter(SO, 1, Acc)
end, dict:new(), SecObjs),
{ok, dict:to_list(SecObjsDict)};
Error ->

is_ok(SecObjs) when length(SecObjs) == 1 ->
is_ok(SecObjs) when length(SecObjs) > 2 ->
is_ok(SecObjs0) ->
EmptyCount = proplists:get_value({[]}, SecObjs0, undefined),
SecObjs = proplists:delete({[]}, SecObjs0),
case length(SecObjs) == 1 of
true ->
[{SecObj, Count}] = SecObjs,
case Count > EmptyCount of
true ->
{fixable, SecObj};
false ->
false ->

0 comments on commit ba252b9

Please sign in to comment.