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: avoid turning huge filepaths to atoms #18

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 9 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
matrix:
otp: [23.2, 24.0.2]
otp: ['26.2.5', '27.1']
rebar3: ['3.22.1']
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4.2.1
with:
submodules: recursive
- uses: gleam-lang/setup-erlang@v1.1.2
- name: Setup Erlang/OTP
uses: erlef/setup-beam@v1.17.5
with:
otp-version: ${{ matrix.otp }}
rebar3-version: ${{ matrix.rebar3 }}
- run: |
make
- name: Archive CT Logs
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4.4.3
with:
name: ct-logs
name: ct-logs-${{ matrix.otp }}
path: _build/test/
retention-days: 1
2 changes: 1 addition & 1 deletion src/replayq.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, replayq,
[{description, "A Disk Queue for Log Replay in Erlang"},
{vsn, "0.3.7"},
{vsn, "git"},
{registered, []},
{applications,
[kernel,
Expand Down
2 changes: 1 addition & 1 deletion src/replayq.appup.src
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%% -*- mode: erlang -*-
{"0.3.8",
{VSN,
[ {<<".*">>, [ {load_module, replayq, brutal_purge, soft_purge, []} ]} ],
[ {<<".*">>, [ {load_module, replayq, brutal_purge, soft_purge, []} ]} ]
}.
12 changes: 10 additions & 2 deletions src/replayq.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
default_marshaller/1,
default_stop_before_func/2]).

-ifdef(TEST).
-export([committer_process_name/1]).
-endif.

-export_type([config/0, q/0, ack_ref/0, sizer/0, marshaller/0]).

-define(NOTHING_TO_ACK, nothing_to_ack).
Expand Down Expand Up @@ -515,13 +519,17 @@ ensure_deleted(Filename) ->
%% The committer writes consumer's acked segmeng number + item ID
%% to a file. The file is only read at start/restart.
spawn_committer(ReaderSegno, Dir) ->
Name = iolist_to_binary(filename:join([Dir, committer])),
%% register a name to avoid having two committers spawned for the same dir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a lazy implementation to guard bugs in caller modules.
e.g. open without close.

we can maybe find a better way for this exclusiveness check.
e.g. put pid in app env, and assert if pid is alive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a named process sounds like a simple implementation supported by the vm itself, with automatic cleanup of the name in case the committer terminates.

what would be the motivation to get rid of this mechanism?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in the latest commit

RegName = binary_to_atom(Name, utf8),
RegName = committer_process_name(Dir),
Pid = erlang:spawn_link(fun() -> committer_loop(ReaderSegno, Dir) end),
true = erlang:register(RegName, Pid),
Pid.

committer_process_name(Dir) ->
Name = iolist_to_binary(filename:join([Dir, committer])),
NameBin = binary:encode_hex(erlang:md5(Name)),
binary_to_atom(NameBin, utf8).

committer_loop(ReaderSegno, Dir) ->
receive
?COMMIT(Segno0, Id0, false) ->
Expand Down
13 changes: 12 additions & 1 deletion test/replayq_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ test_corrupted_segment(BadBytes) ->

comitter_crash_test() ->
Dir = ?DIR,
ComitterName = binary_to_atom(iolist_to_binary(filename:join([Dir, committer])), utf8),
ComitterName = replayq:committer_process_name(Dir),
Config = #{dir => Dir, seg_bytes => 1000},
_ = replayq:open(Config),
erlang:process_flag(trap_exit, true),
Expand All @@ -388,6 +388,17 @@ comitter_crash_test() ->
ok
end.

%% Checks that our spawned committer can register a name for itself when using filepaths
%% larger than 255 bytes.
huge_filepath_test() ->
Dir0 = ?DIR,
Dir = filename:join(Dir0, binary:copy(<<"a">>, 255)),
Config = #{dir => Dir, seg_bytes => 1000},
_ = replayq:open(Config),
CommitterName = replayq:committer_process_name(Dir),
?assert(is_process_alive(whereis(CommitterName))),
ok.

is_in_mem_test_() ->
[ {"mem queue", fun() ->
Q = replayq:open(#{mem_only => true}),
Expand Down