Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cache sizing is critical for performance. This change guesses at a

reasonable setting for the WiredTiger cache size at runtime.  This cache
is shared across all vnodes regarless of how many are active at any
given time.  The algorithm is: max(1GB, 1/3 (RAM - Beam RSS size)). We don't
enable direct_io on purpose and data will be double buffered in WiredTiger's
cache and the filesystem buffer cache.  This turns out to be faster than
direct I/O despite wasting a bit of RAM.
  • Loading branch information...
commit 1d6dfee19245ce1d02162b8f2889c16cac057b37 1 parent efdeb70
Gregory Burd gburd authored
44 src/riak_kv_wterl_backend.erl
@@ -24,6 +24,8 @@
24 24 -behavior(temp_riak_kv_backend).
25 25 -author('Steve Vinoski <steve@basho.com>').
26 26
  27 +-compile([{parse_transform, lager_transform}]).
  28 +
27 29 %% KV Backend API
28 30 -export([api_version/0,
29 31 capabilities/1,
@@ -111,7 +113,7 @@ start(Partition, Config) ->
111 113 {logging, true},
112 114 {transactional, true},
113 115 {session_max, SessionMax},
114   - {cache_size, "2GB"},
  116 + {cache_size, size_cache(Config)},
115 117 {sync, false}
116 118 %% {verbose,
117 119 %% ["block", "shared_cache", "ckpt", "evict",
@@ -135,8 +137,7 @@ start(Partition, Config) ->
135 137 session=SRef,
136 138 partition=Partition}};
137 139 {error, ConnReason}=ConnError ->
138   - lager:error("Failed to start wterl backend: ~p\n",
139   - [ConnReason]),
  140 + lager:error("Failed to start wterl backend: ~p\n", [ConnReason]),
140 141 ConnError
141 142 end;
142 143 Error ->
@@ -476,6 +477,43 @@ fetch_status(Cursor, {ok, Stat}, Acc) ->
476 477 [What,Val|_] = [binary_to_list(B) || B <- binary:split(Stat, [<<0>>], [global])],
477 478 fetch_status(Cursor, wterl:cursor_next_value(Cursor), [{What,Val}|Acc]).
478 479
  480 +size_cache(Config) ->
  481 + Size =
  482 + case app_helper:get_prop_or_env(cache_size, Config, wterl) of
  483 + {ok, Value} ->
  484 + Value;
  485 + undefined ->
  486 + RunningApps = application:which_applications(),
  487 + FinalGuess =
  488 + case proplists:is_defined(sasl, RunningApps) andalso
  489 + proplists:is_defined(os_mon, RunningApps) of
  490 + true ->
  491 + Memory = memsup:get_system_memory_data(),
  492 + TotalRAM = proplists:get_value(system_total_memory, Memory),
  493 + FreeRAM = proplists:get_value(free_memory, Memory),
  494 + UsedByBeam = proplists:get_value(total, erlang:memory()),
  495 + Target = ((TotalRAM - UsedByBeam) div 3),
  496 + FirstGuess = (Target - (Target rem (1024 * 1024))),
  497 + SecondGuess =
  498 + case FirstGuess > FreeRAM of
  499 + true -> FreeRAM - (FreeRAM rem (1024 * 1024));
  500 + _ -> FirstGuess
  501 + end,
  502 + case SecondGuess < 1073741824 of %% < 1GB?
  503 + true -> "1GB";
  504 + false ->
  505 + ThirdGuess = SecondGuess div (1024 * 1024),
  506 + integer_to_list(ThirdGuess) ++ "MB"
  507 + end;
  508 + false ->
  509 + "1GB"
  510 + end,
  511 + application:set_env(wt, cache_size, FinalGuess),
  512 + lager:warning("Using best-guess cache size of ~p for WiredTiger storage backend.", [FinalGuess]),
  513 + FinalGuess
  514 + end,
  515 + Size.
  516 +
479 517 %% ===================================================================
480 518 %% EUnit tests
481 519 %% ===================================================================
4 src/temp_riak_kv_backend.erl
@@ -272,7 +272,9 @@ empty_check({Backend, State}) ->
272 272 }.
273 273
274 274 setup({BackendMod, Config}) ->
275   - %% Start the backend
  275 + lager:start(),
  276 + application:start(sasl),
  277 + application:start(os_mon),
276 278 {ok, S} = BackendMod:start(42, Config),
277 279 {BackendMod, S}.
278 280

0 comments on commit 1d6dfee

Please sign in to comment.
Something went wrong with that request. Please try again.