-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for conditional postcommit hooks #771
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
%% ------------------------------------------------------------------- | ||
%% | ||
%% Copyright (c) 2012-2013 Basho Technologies, Inc. All Rights Reserved. | ||
%% | ||
%% This file is provided to you 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 | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% 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. | ||
%% | ||
%% ------------------------------------------------------------------- | ||
-module(riak_kv_hooks). | ||
|
||
%% API | ||
-export([add_conditional_postcommit/1, | ||
del_conditional_postcommit/1, | ||
get_conditional_postcommit/2]). | ||
|
||
%% Exported for internal use by `riak_kv_sup' | ||
-export([create_table/0]). | ||
|
||
%% Types | ||
-type hook() :: {module(), atom()}. | ||
-type hook_type() :: conditional_postcommit. | ||
-type bucket() :: riak_object:bucket(). | ||
-type key() :: riak_object:key(). | ||
-type bucket_props() :: riak_kv_bucket:props(). | ||
|
||
%%%=================================================================== | ||
|
||
%% @doc | ||
%% Called by {@link riak_kv_sup} to create the public ETS table used to | ||
%% track registered hooks. Having `riak_kv_sup' own the table ensures | ||
%% that the table exists aslong as riak_kv is running. | ||
-spec create_table() -> ok. | ||
create_table() -> | ||
?MODULE = ets:new(?MODULE, [named_table, public, bag, | ||
{write_concurrency, true}, | ||
{read_concurrency, true}]), | ||
restore_state(), | ||
ok. | ||
|
||
%% @doc | ||
%% Add a global conditional postcommit hook that is called for each | ||
%% PUT operation. The hook is of the form `{Module, Fun}'. The specified | ||
%% function is called with the relevent bucket, key, and bucket properties | ||
%% at the time of the PUT operation and is expected to return `false' or | ||
%% a normal postcommit hook specification that should be invoked. | ||
-spec add_conditional_postcommit(hook()) -> ok. | ||
add_conditional_postcommit(Hook) -> | ||
add_hook(conditional_postcommit, Hook). | ||
|
||
%% @doc Remove a previously registered conditional postcommit hook | ||
-spec del_conditional_postcommit(hook()) -> ok. | ||
del_conditional_postcommit(Hook) -> | ||
del_hook(conditional_postcommit, Hook). | ||
|
||
%% @doc | ||
%% This function invokes each registered conditional postcommit | ||
%% hook. Each hook will return either `false' or a list of active | ||
%% hooks. This function then returns the combined list of active hooks. | ||
-spec get_conditional_postcommit({bucket(), key()}, bucket_props()) -> [any()]. | ||
get_conditional_postcommit({{BucketType, Bucket}, _Key}, BucketProps) -> | ||
Hooks = get_hooks(conditional_postcommit), | ||
ActiveHooks = | ||
[ActualHook || {Mod, Fun} <- Hooks, | ||
ActualHook <- [Mod:Fun(BucketType, Bucket, BucketProps)], | ||
ActualHook =/= false], | ||
lists:flatten(ActiveHooks); | ||
get_conditional_postcommit(_BKey, _BucketProps) -> | ||
%% For now, we only support typed buckets. | ||
[]. | ||
|
||
%%%=================================================================== | ||
|
||
-spec add_hook(hook_type(), hook()) -> ok. | ||
add_hook(Type, Hook) -> | ||
ets:insert(?MODULE, {Type, Hook}), | ||
save_state(), | ||
ok. | ||
|
||
-spec del_hook(hook_type(), hook()) -> ok. | ||
del_hook(Type, Hook) -> | ||
ets:delete_object(?MODULE, {Type, Hook}), | ||
save_state(), | ||
ok. | ||
|
||
-spec get_hooks(hook_type()) -> [hook()]. | ||
get_hooks(Type) -> | ||
[Hook || {_, Hook} <- ets:lookup(?MODULE, Type)]. | ||
|
||
%% Backup the current ETS state to the application environment just in case | ||
%% riak_kv_sup dies and the ETS table is lost. | ||
-spec save_state() -> ok. | ||
save_state() -> | ||
Hooks = ets:tab2list(?MODULE), | ||
ok = application:set_env(riak_kv, riak_kv_hooks, Hooks, infinity), | ||
ok. | ||
|
||
%% Restore registered hooks in the unlikely case that riak_kv_sup died and | ||
%% the ETS table was lost/recreated. | ||
-spec restore_state() -> ok. | ||
restore_state() -> | ||
case application:get_env(riak_kv, riak_kv_hooks) of | ||
undefined -> | ||
ok; | ||
{ok, Hooks} -> | ||
true = ets:insert_new(?MODULE, Hooks), | ||
ok | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other functions that take bucket type and bucket, including
get_conditional_postcommit
, typically pass them as a 2-tuple. If we were to extendget_conditional_postcommit
to untyped buckets I think we'd probably want to make this a 2-arity function.