From 0ec817ae9f4d62278d8e2e5aa599504c99bc3205 Mon Sep 17 00:00:00 2001 From: Gregory Burd Date: Mon, 11 Mar 2013 12:59:31 -0400 Subject: [PATCH] Restructure the way we create/merge default configuration setttings with user supplied settings. Move cache estimate to its own function. --- src/riak_kv_wiredtiger_backend.erl | 40 ++++++++++----------- src/temp_riak_kv_backend.erl | 2 +- src/wt_conn.erl | 57 ++++++++++++++++++------------ 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/riak_kv_wiredtiger_backend.erl b/src/riak_kv_wiredtiger_backend.erl index 059f938..6ecea43 100644 --- a/src/riak_kv_wiredtiger_backend.erl +++ b/src/riak_kv_wiredtiger_backend.erl @@ -2,7 +2,7 @@ %% %% riak_kv_wiredtiger_backend: Use WiredTiger for Riak/KV storage %% -%% Copyright (c) 2012-2013 Basho Technologies, Inc. All Rights Reserved. +%% Copyright (c) 2012 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 @@ -83,31 +83,11 @@ capabilities(_, _) -> start(Partition, Config0) -> %% Get the data root directory case app_helper:get_prop_or_env(data_root, Config0, wt) of - <<"">> -> - lager:error("Failed to startup WiredTiger: data_root is not valid"), - {error, data_root_unset}; - [] -> - lager:error("Failed to startup WiredTiger: data_root is empty"), - {error, data_root_unset}; undefined -> lager:error("Failed to startup WiredTiger: data_root is not set"), {error, data_root_unset}; DataRoot -> - Config = lists:keydelete(data_root, 1, Config0), - CacheSize = - case proplists:get_value(cache_size, Config) of - undefined -> - case application:get_env(wt, cache_size) of - {ok, Value} -> - Value; - _ -> - SizeEst = best_guess_at_a_reasonable_cache_size(64), - %% lager:warning("Using estimated best cache size of ~p for WiredTiger backend.", [SizeEst]), - SizeEst - end; - Value -> - Value - end, + Config = lists:keydelete(data_root, 1, Config0), AppStarted = case application:start(wt) of ok -> @@ -120,6 +100,7 @@ start(Partition, Config0) -> end, case AppStarted of ok -> + CacheSize = size_cache(64, Config), ConnectionOpts = [Config, {create, true}, @@ -475,6 +456,21 @@ fetch_status(Cursor, {ok, Stat}, Acc) -> [What,Val|_] = [binary_to_list(B) || B <- binary:split(Stat, [<<0>>], [global])], fetch_status(Cursor, wt:cursor_next_value(Cursor), [{What,Val}|Acc]). +size_cache(ChunkSize, Config) -> + case proplists:get_value(cache_size, Config) of + undefined -> + case application:get_env(wt, cache_size) of + {ok, Value} -> + Value; + _ -> + SizeEst = best_guess_at_a_reasonable_cache_size(ChunkSize), + %% lager:warning("Using estimated best cache size of ~p for WiredTiger backend.", [SizeEst]), + SizeEst + end; + Value -> + Value + end. + best_guess_at_a_reasonable_cache_size(ChunkSizeInMB) -> RunningApps = application:which_applications(), case proplists:is_defined(sasl, RunningApps) andalso diff --git a/src/temp_riak_kv_backend.erl b/src/temp_riak_kv_backend.erl index 38e41fe..fab168f 100644 --- a/src/temp_riak_kv_backend.erl +++ b/src/temp_riak_kv_backend.erl @@ -272,7 +272,7 @@ empty_check({Backend, State}) -> }. setup({BackendMod, Config}) -> - {ok, S} = BackendMod:start(0, Config), + {ok, S} = BackendMod:start(42, Config), {BackendMod, S}. cleanup({BackendMod, S}) -> diff --git a/src/wt_conn.erl b/src/wt_conn.erl index 82a364c..ffee504 100644 --- a/src/wt_conn.erl +++ b/src/wt_conn.erl @@ -2,7 +2,7 @@ %% %% wt_conn: manage a connection to WiredTiger %% -%% Copyright (c) 2012-2013 Basho Technologies, Inc. All Rights Reserved. +%% Copyright (c) 2012 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 @@ -26,6 +26,7 @@ -ifdef(TEST). -include_lib("eunit/include/eunit.hrl"). +-compile(export_all). -endif. %% API @@ -83,26 +84,7 @@ init([]) -> {ok, #state{}}. handle_call({open, Dir, Config, Caller}, _From, #state{conn=undefined}=State) -> - OptsA = - case proplists:get_bool(create, Config) of - false -> [{create, false}]; - _ -> [{create, true}] - end, - OptsB = - case proplists:is_defined(shared_cache, Config) of - true -> - []; - false -> - [config_value(cache_size, Config, "512MB")] - end, - OptsC = - case proplists:is_defined(session_max, Config) of - true -> - []; - false -> - [config_value(session_max, Config, 100)] - end, - Opts = lists:merge([OptsA, OptsB, OptsC, Config]), + Opts = tailor_config(Config), {Reply, NState} = case wt:conn_open(Dir, wt:config_to_bin(Opts)) of {ok, ConnRef}=OK -> @@ -192,6 +174,35 @@ do_close(ConnRef) -> config_value(Key, Config, Default) -> {Key, app_helper:get_prop_or_env(Key, Config, wt, Default)}. +%% @private +map_cfg([], Acc) -> + Acc; +map_cfg([Fun|T], Acc) -> + map_cfg(T, Fun(Acc)). + +tailor_config(Config) -> + map_cfg([fun (Acc) -> + case proplists:is_defined(create, Acc) of + false -> [{create, true} | Acc]; + true -> Acc + end + end, + fun (Acc) -> + case proplists:is_defined(shared_cache, Acc) of + false -> + [config_value(cache_size, Acc, "512MB") | Acc]; + true -> + Acc + end + end, + fun (Acc) -> + case proplists:is_defined(session_max, Acc) of + false -> + [config_value(session_max, Acc, 100) | Acc]; + true -> + Acc + end + end], Config). -ifdef(TEST). @@ -233,14 +244,14 @@ simple_test_() -> end}]}. open_one() -> - {ok, Ref} = open("test/wt-backend", [{create,true},{session_max, 20}]), + {ok, Ref} = open("test/wt-backend", [{create, true},{session_max, 20}]), true = is_open(), close(Ref), false = is_open(), ok. open_and_wait(Pid) -> - {ok, Ref} = open("test/wt-backend", [{create,true}]), + {ok, Ref} = open("test/wt-backend", [{create, true}]), Pid ! open, receive close ->