Permalink
Browse files

Implement o_sync for real

Use the bitcask NIFs to make a couch btree use proper O_SYNC
The sync option named just plain "sync" will sync on every write.
  • Loading branch information...
1 parent 15e9fa3 commit 46fa8dee1e971ab0c6f29741b02ad67a1670222f @krestenkrab committed Apr 25, 2011
Showing with 18 additions and 2 deletions.
  1. +2 −1 src/riak_btree_backend.app.src
  2. +16 −1 src/riak_btree_backend.erl
@@ -14,8 +14,9 @@
%% Strategies available for syncing data to disk:
%% * none - let the O/S decide
+ %% * sync - call file:sync for every each write
%% * o_sync - use the O_SYNC flag to sync each write
- %% * {seconds, N} - call bitcask:sync/1 every N seconds
+ %% * {seconds, N} - call file:sync/1 every N seconds
%%
%% Note that for the {seconds, N} strategy, it is up to the
%% API caller to execute the call on the interval. This config
View
@@ -86,6 +86,7 @@ initstate(BtreeFileName, Config) ->
case couch_file:open(BtreeFileName, [sys_db]) of
{ok, Fd} -> %% open existing file
+ maybe_set_osync(Fd, Config),
{ok, #db_header{local_docs_btree_state = HeaderBtree}} =
couch_file:read_header(Fd),
{ok, Bt} = couch_btree:open(HeaderBtree, Fd, []),
@@ -97,6 +98,7 @@ initstate(BtreeFileName, Config) ->
case couch_file:open(BtreeFileName ++ ".save", [sys_db]) of
{ok, Fd} -> %% open existing file
+ maybe_set_osync(Fd, Config),
file:rename(BtreeFileName ++ ".save", BtreeFileName),
{ok, #db_header{local_docs_btree_state = HeaderBtree}} =
couch_file:read_header(Fd),
@@ -106,6 +108,7 @@ initstate(BtreeFileName, Config) ->
{error, enoent} ->
case couch_file:open(BtreeFileName, [create,sys_db]) of
{ok, Fd} ->
+ maybe_set_osync(Fd, Config),
Header = #db_header{},
ok = couch_file:write_header(Fd, Header),
{ok, Bt} = couch_btree:open(nil, Fd, []),
@@ -125,6 +128,14 @@ initstate(BtreeFileName, Config) ->
end
.
+maybe_set_osync(Fd,Config) ->
+ case get_opt(sync_strategy, Config) of
+ o_sync ->
+ ok = couch_file:set_osync(Fd);
+ _ ->
+ ok
+ end.
+
get_opt(Key, #state{config=Config}) ->
get_opt(Key, Config);
get_opt(Key, Opts) ->
@@ -186,7 +197,7 @@ commit_data(#btree{fd = Fd}, Bt2, State) ->
#db_header{local_docs_btree_state =
couch_btree:get_state(Bt2)}),
case get_opt(sync_strategy, State) of
- o_sync ->
+ sync ->
couch_file:sync(Fd);
_ ->
ok
@@ -405,6 +416,10 @@ maybe_schedule_sync(Ref) when is_reference(Ref) ->
schedule_sync(Ref, SyncIntervalMs);
{ok, none} ->
ok;
+ {ok, sync} ->
+ ok;
+ {ok, o_sync} ->
+ ok;
BadStrategy ->
error_logger:info_msg("Ignoring invalid riak_btree sync strategy: ~p\n",
[BadStrategy]),

0 comments on commit 46fa8de

Please sign in to comment.