Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/hb_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ do_write_message(Bin, Store, Opts) when is_binary(Bin) ->
% Return the path.
Path = generate_binary_path(Bin, Opts),
hb_store:write(Store, Path, Bin),
%lists:map(fun(ID) -> hb_store:make_link(Store, Path, ID) end, AllIDs),
{ok, Path};
do_write_message(List, Store, Opts) when is_list(List) ->
do_write_message(
Expand Down
37 changes: 34 additions & 3 deletions src/hb_store_lmdb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ write(Opts, PathParts, Value) when is_list(PathParts) ->
% Convert to binary
PathBin = to_path(PathParts),
write(Opts, PathBin, Value);
write(Opts, Path, Value) ->
#{ <<"db">> := DBInstance } = find_env(Opts),
write(#{<<"name">> := Name} = Opts, Path, Value) ->
#{ <<"db">> := DBInstance, <<"env">> := Env } = find_env(Opts),
?event({elmdb_write, {db, DBInstance}, {path, Path}, {value, Value}}),
case elmdb:put(DBInstance, Path, Value) of
ok -> ok;
ok ->
sample_size_metrics(Env, Name),
ok;
{error, Type, Description} ->
?event(
error,
Expand Down Expand Up @@ -624,6 +626,25 @@ sample_metrics(Name, StartTime, Type) ->
miss -> ok
end.

%% @doc Sample LMDB size metrics on roughly 1/1024 writes (writable stores only).
%%
%% Uses the low bits of monotonic_time as a cheap random-ish gate so the
%% env_stat NIF call does not run on every write. Only called from the
%% writable write/3 clause, so read-only stores are never sampled.
sample_size_metrics(Env, Name) ->
case erlang:monotonic_time() band 1023 of
0 ->
case elmdb:env_stat(Env) of
{ok, MapSize, UsedBytes} ->
prometheus_gauge:set(hb_store_lmdb_used_bytes, [Name], UsedBytes),
prometheus_gauge:set(hb_store_lmdb_map_size_bytes, [Name], MapSize);
_ ->
ok
end;
_ ->
ok
end.

init_prometheus() ->
hb_prometheus:declare(histogram, [
{name, hb_store_lmdb_duration_seconds},
Expand All @@ -636,6 +657,16 @@ init_prometheus() ->
{labels, [name]},
{help, "LMDB name requested"}
]),
hb_prometheus:declare(gauge, [
{name, hb_store_lmdb_used_bytes},
{labels, [store_name]},
{help, "Approximate LMDB used bytes (high-water mark based on last_pgno)"}
]),
hb_prometheus:declare(gauge, [
{name, hb_store_lmdb_map_size_bytes},
{labels, [store_name]},
{help, "Configured LMDB map_size limit in bytes"}
]),
ok.

%% @doc Test suite demonstrating basic store operations.
Expand Down