Github user banjiewen commented on a diff in the pull request:
https://github.com/apache/couchdb-mem3/pull/19#discussion_r59493169
--- 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.
--- End diff --
I'm not sure that's quite the right analysis - given the fixed-rate flow of
sets through the bucket list any database updated more frequently than
`mem3.sync_frequency` is on average equally likely to result in nine
`is_element` checks as it is to result in one (or three, etc).
Assuming that's true, then keeping the newest bucket at the head would be
preferable given its high mutation rates - we only need to split off the last
element once every `mem3.sync_frequency` milliseconds, but may mutate the head
much more frequently in a cluster with a great number of active databases.
---
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.
---