Skip to content
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

fix: put memtable resizing behind genserver #93

Merged
merged 5 commits into from
Aug 8, 2021
Merged
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
13 changes: 8 additions & 5 deletions lib/august_db/commitlog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ defmodule CommitLog do
{:ok, device_out}
end

def handle_cast(:new, _) do
def handle_cast(:new, old_device_out) do
:ok = :file.close(old_device_out)
:ok = :file.delete(@log_file)
{:ok, device_out} = :file.open(@log_file, [:append])
{:noreply, device_out}
{:ok, new_device_out} = :file.open(@log_file, [:append, :raw])
{:noreply, new_device_out}
end

def handle_cast(:close, device_out) do
:ok = :file.close(device_out)
{:noreply, device_out}
end

Expand Down Expand Up @@ -96,8 +96,11 @@ defmodule CommitLog do
File.touch!(@log_file)
end

@doc """
Delete the old commit log and open a device to a new one
in raw,append mode.
"""
def new() do
GenServer.cast(CommitLogDevice, :close)
GenServer.cast(CommitLogDevice, :new)
end
end
33 changes: 12 additions & 21 deletions lib/august_db/memtable_sizer.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Memtable.Sizer do
use GenServer

@max_size_bytes 64 * 1024 * 1024
@max_size_bytes 1024 * 1024

defmodule State do
defstruct total_size: 0, sizes: %{}
Expand All @@ -15,7 +15,7 @@ defmodule Memtable.Sizer do
{:ok, state}
end

def handle_call({:resize, key, kv_size}, _from, state) do
def handle_cast({:resize, key, kv_size}, state) do
old_kv_size =
case Map.get(state.sizes, key) do
nil -> 0
Expand All @@ -26,8 +26,14 @@ defmodule Memtable.Sizer do

new_total_size = state.total_size + kv_size_diff

{:reply, new_total_size,
%State{total_size: new_total_size, sizes: Map.put(state.sizes, key, kv_size)}}
if new_total_size > @max_size_bytes do
IO.puts("Clearing Memtable with size #{new_total_size}B, max is #{@max_size_bytes}B")

Memtable.flush()
{:noreply, %State{total_size: 9, sizes: %{}}}
else
{:noreply, %State{total_size: new_total_size, sizes: Map.put(state.sizes, key, kv_size)}}
end
end

def handle_cast(:clear, _state) do
Expand All @@ -37,25 +43,10 @@ defmodule Memtable.Sizer do
def resize(key, value) when is_binary(key) and is_binary(value) do
kv_size = byte_size(key) + byte_size(value)

update(key, kv_size)
GenServer.cast(MemtableSizer, {:resize, key, kv_size})
end

def remove(key) when is_binary(key) do
update(key, byte_size(key))
end

defp update(key, kv_size) do
new_total_size = GenServer.call(MemtableSizer, {:resize, key, kv_size})

if new_total_size > @max_size_bytes do
IO.puts("Clearing Memtable with size #{new_total_size}B, max is #{@max_size_bytes}B")

Memtable.flush()
__MODULE__.clear()
end
end

def clear() do
GenServer.cast(MemtableSizer, :clear)
GenServer.cast(MemtableSizer, {:resize, key, byte_size(key)})
end
end
4 changes: 2 additions & 2 deletions test/AugustDB.jmx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<stringProp name="LoopController.loops">200</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">500</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">5</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
Expand All @@ -42,7 +42,7 @@
</ConfigTestElement>
<hashTree/>
<RandomVariableConfig guiclass="TestBeanGUI" testclass="RandomVariableConfig" testname="Random Key" enabled="true">
<stringProp name="maximumValue">4294967295</stringProp>
<stringProp name="maximumValue">2147483647</stringProp>
<stringProp name="minimumValue">1</stringProp>
<stringProp name="outputFormat"></stringProp>
<boolProp name="perThread">true</boolProp>
Expand Down