Github user kocolosk commented on a diff in the pull request:
https://github.com/apache/couchdb-mem3/pull/19#discussion_r59487957
--- Diff: src/mem3_sync_event_listener.erl ---
@@ -51,35 +68,136 @@ handle_event(NodesDb, updated, #state{nodes = NodesDb}
= St) ->
Nodes = mem3:nodes(),
Live = nodes(),
[mem3_sync:push(NodesDb, N) || N <- Nodes, lists:member(N, Live)],
- {ok, St};
+ maybe_push_shards(St);
handle_event(ShardsDb, updated, #state{shards = ShardsDb} = St) ->
mem3_sync:push(ShardsDb, mem3_sync:find_next_node()),
- {ok, St};
+ maybe_push_shards(St);
handle_event(UsersDb, updated, #state{users = UsersDb} = St) ->
mem3_sync:push(UsersDb, mem3_sync:find_next_node()),
- {ok, St};
+ maybe_push_shards(St);
handle_event(<<"shards/", _/binary>> = ShardName, updated, St) ->
- try mem3:shards(mem3:dbname(ShardName)) of
- Shards ->
- Targets = [S || #shard{node=N, name=Name} = S <- Shards,
- N =/= node(), Name =:= ShardName],
- Live = nodes(),
- [mem3_sync:push(ShardName,N) || #shard{node=N} <- Targets,
- lists:member(N, Live)]
- catch error:database_does_not_exist ->
- ok
- end,
- {ok, St};
+ Buckets = bucket_shard(ShardName, St#state.buckets),
+ maybe_push_shards(St#state{buckets=Buckets});
handle_event(<<"shards/", _:18/binary, _/binary>> = ShardName, deleted,
St) ->
mem3_sync:remove_shard(ShardName),
- {ok, St};
+ maybe_push_shards(St);
handle_event(_DbName, _Event, St) ->
- {ok, St}.
+ maybe_push_shards(St).
+handle_cast({set_frequency, Frequency}, St) ->
+ #state{delay = Delay, buckets = Buckets0} = St,
+ Buckets1 = rebucket_shards(Delay, Frequency, Buckets0),
+ maybe_push_shards(St#state{frequency=Frequency, buckets=Buckets1});
+handle_cast({set_delay, Delay}, St) ->
+ #state{frequency = Frequency, buckets = Buckets0} = St,
+ Buckets1 = rebucket_shards(Delay, Frequency, Buckets0),
+ maybe_push_shards(St#state{delay=Delay, buckets=Buckets1});
handle_cast(Msg, St) ->
couch_log:notice("unexpected cast to mem3_sync_event_listener: ~p",
[Msg]),
- {ok, St}.
+ maybe_push_shards(St).
+handle_info(timeout, St) ->
+ maybe_push_shards(St);
handle_info(Msg, St) ->
couch_log:notice("unexpected info to mem3_sync_event_listener: ~p",
[Msg]),
+ maybe_push_shards(St).
+
+handle_config_change("mem3", "sync_delay", Delay0, _, St) ->
+ try list_to_integer(Delay0) of
+ Delay1 ->
+ couch_event_listener:cast(
+ ?MODULE,
+ {set_delay, Delay1}
+ )
+ catch error:badarg ->
+ couch_log:warning(
+ "ignoring bad value for mem3.sync_delay: ~p",
+ [Delay0]
+ )
+ end,
+ {ok, St};
+handle_config_change("mem3", "sync_frequency", Frequency0, _, St) ->
+ try list_to_integer(Frequency0) of
+ Frequency1 ->
+ couch_event_listener:cast(
+ ?MODULE,
+ {set_frequency, Frequency1}
+ )
+ catch error:badarg ->
+ couch_log:warning(
+ "ignoring bad value for mem3.sync_frequency: ~p",
+ [Frequency0]
+ )
+ end,
+ {ok, St};
+handle_config_change(_, _, _, _, St) ->
{ok, St}.
+
+handle_config_terminate(_, stop, _) -> ok;
+handle_config_terminate(_Server, _Reason, St) ->
+ Fun = fun() ->
+ timer:sleep(5000),
+ config:listen_for_changes(?MODULE, St)
+ end,
+ spawn(Fun).
+
+bucket_shard(ShardName, [B|Bs]=Buckets0) ->
+ case waiting(ShardName, Buckets0) of
+ true -> Buckets0;
+ false -> [sets:add_element(ShardName, B)|Bs]
+ end.
+
+waiting(_, []) ->
+ false;
+waiting(ShardName, [B|Bs]) ->
+ case sets:is_element(ShardName, B) of
+ true -> true;
+ false -> waiting(ShardName, Bs)
+ end.
+
+rebucket_shards(Frequency, Delay, Buckets0) ->
+ case (Delay div Frequency + 1) - length(Buckets0) of
+ 0 ->
+ Buckets0;
+ N when N < 0 ->
+ %% Reduce the number of buckets by merging the last N + 1
together
+ {ToMerge, [B0|Buckets1]} = lists:split(abs(N), Buckets0),
+ B1 = lists:foldl(fun(A, B) -> sets:union(A, B) end, B0,
ToMerge),
--- End diff --
Use `sets:union/1` and skip the `fold`?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---