<commit_before>% @copyright 2015 Zuse Institute Berlin

%   Licensed under the Apache License, Version 2.0 (the "License");
%   you may not use this file except in compliance with the License.
%   You may obtain a copy of the License at
%
%       http://www.apache.org/licenses/LICENSE-2.0
%
%   Unless required by applicable law or agreed to in writing, software
%   distributed under the License is distributed on an "AS IS" BASIS,
%   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%   See the License for the specific language governing permissions and
%   limitations under the License.

%% @author <NAME> <<EMAIL>>
%% @doc    Unit tests for the mnesia recovery.
%% @end
%% @version $Id$
-module(recover_mnesia_SUITE).

-include("scalaris.hrl").
-include("unittest.hrl").

-author('<EMAIL>').
-vsn('$Id$').

-compile(export_all).

-define(CLOSE, close).
-define(EQ, ==).

all() -> [
  tester_ring_8,
  tester_with_data].

suite() -> [ {timetrap, {seconds, 60}} ].

init_per_suite(Config) ->
  unittest_helper:init_per_suite(Config).

end_per_suite(Config) ->
  unittest_helper:end_per_suite(Config).

-ifdef(PRBR_MNESIA).
init_per_testcase(_TestCase, Config) ->
  % make sure ring is stopped, previous test may have run into timeout
  unittest_helper:stop_ring(),
  application:stop(mnesia),
  file:delete("/home/tanguy/scalaris-dev/data/recover_mnesia_SUITE_ct@127.0.0.1/schema.DAT"),

  {priv_dir, PrivDir} = lists:keyfind(priv_dir, 1, Config),
  unittest_helper:make_ring(8, [{config, [{log_path, PrivDir},
                                {leases, true}]}]),
  unittest_helper:check_ring_size_fully_joined(8),
  Config.
-else.
init_per_testcase(_TestCase, _Config) -> {skip, "db_mnesia not set -> skipping test SUITE"}.
-endif.

end_per_testcase(_TestCase, Config) ->
  Tabs = lists:delete(schema, mnesia:system_info(tables)),
  unittest_helper:stop_ring(),
  application:stop(mnesia),
  [ok = file:delete("/home/tanguy/scalaris-dev/data/recover_mnesia_SUITE_ct@127.0.0.1/"++atom_to_list(X)++".DCD")||X<-Tabs],
  ok = file:delete("/home/tanguy/scalaris-dev/data/recover_mnesia_SUITE_ct@127.0.0.1/schema.DAT"),
  Config.

rw_suite_runs(N) ->
  erlang:min(N, 200).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% test create_ring_8/1 of mnesia recovery
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec tester_ring_8([db_backend_beh:entry()]) -> true.
tester_ring_8(Config) ->
  {priv_dir, PrivDir} = lists:keyfind(priv_dir, 1, Config),
  unittest_helper:stop_ring(),
  ct:pal("ring stopped -> sleeping. Waiting for leases to expire"),
  timer:sleep(11000),
  unittest_helper:make_ring_recover( [{config, [{log_path, PrivDir},
                                      {leases, true},
                                      {start_type, recover}]}]),
  util:wait_for(fun admin:check_leases/0, 10000),
  true.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% test with_data/1 write data to KV DBs and ensure data integrity after recovery
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tester_with_data(Config) ->
  % writting data to KV
  [kv_on_cseq:write(integer_to_list(X),X)||X<-lists:seq(1,100)],
  {priv_dir, PrivDir} = lists:keyfind(priv_dir, 1, Config),
  unittest_helper:stop_ring(),
  ct:pal("ring stopped -> sleeping. Waiting for leases to expire"),
  timer:sleep(11000),
  unittest_helper:make_ring_recover( [{config, [{log_path, PrivDir},
    {leases, true},
    {start_type, recover}]}]),
  util:wait_for(fun admin:check_leases/0, 10000),
  % ring restored -> checking KV data integrity
  [{ok, X} = kv_on_cseq:read(integer_to_list(X))||X<-lists:seq(1,100)],
  true.

<commit_msg>recover_SUITE: include groups to recover n times in a raw<commit_after>% @copyright 2015 Zuse Institute Berlin

%   Licensed under the Apache License, Version 2.0 (the "License");
%   you may not use this file except in compliance with the License.
%   You may obtain a copy of the License at
%
%       http://www.apache.org/licenses/LICENSE-2.0
%
%   Unless required by applicable law or agreed to in writing, software
%   distributed under the License is distributed on an "AS IS" BASIS,
%   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%   See the License for the specific language governing permissions and
%   limitations under the License.

%% @author <NAME> <<EMAIL>>
%% @doc    Unit tests for the mnesia recovery.
%% @end
%% @version $Id$
-module(recover_mnesia_SUITE).

-include("scalaris.hrl").
-include("unittest.hrl").

-author('<EMAIL>').
-vsn('$Id$').

-compile(export_all).

-define(NUM_EXECUTIONS, 5).
-define(CLOSE, close).

all() -> [
  {group, make_ring_group},
  {group, recover_data_group}].
groups() ->
  [
    {make_ring_group, [sequence], [tester_ring_8]},
    {recover_data_group, [sequence, {repeat, ?NUM_EXECUTIONS}], [tester_write, tester_read]}
  ].

suite() -> [ {timetrap, {seconds, 60}} ].

-ifdef(PRBR_MNESIA).
init_per_suite(Config) ->
  unittest_helper:init_per_suite(Config).
-else.
init_per_suite(_TestCase, _Config) -> {skip, "db_mnesia not set -> skipping test SUITE"}.
-endif.

end_per_suite(Config) ->
  unittest_helper:end_per_suite(Config).

init_per_group(Group, Config) ->
  % stop ring from previous test case (it may have run into a timeout)
  unittest_helper:stop_ring(),
  application:stop(mnesia),
  WorkingDir = string:sub_string(os:cmd(pwd), 1, string:len(os:cmd(pwd))-1)++
    "/../data/recover_mnesia_SUITE_ct@127.0.0.1/",
  file:delete(WorkingDir ++ "schema.DAT"),

  {priv_dir, PrivDir} = lists:keyfind(priv_dir, 1, Config),
  unittest_helper:make_ring(8, [{config, [{log_path, PrivDir},
    {leases, true}]}]),
  unittest_helper:check_ring_size_fully_joined(8),
  unittest_helper:init_per_group(Group, Config).

end_per_group(Group, Config) ->
  WorkingDir = string:sub_string(os:cmd(pwd), 1, string:len(os:cmd(pwd))-1)++
    "/../data/recover_mnesia_SUITE_ct@127.0.0.1/",
  Tabs = lists:delete(schema, mnesia:system_info(tables)),
  unittest_helper:stop_ring(),
  application:stop(mnesia),
  [ok = file:delete(WorkingDir ++ atom_to_list(X)++".DCD")||X<-Tabs],
  ok = file:delete(WorkingDir ++ "schema.DAT"),
  unittest_helper:end_per_group(Group, Config).

init_per_testcase(_TestCase, Config) ->
  Config.

end_per_testcase(_TestCase, _Config) ->
  ok.

rw_suite_runs(N) ->
  erlang:min(N, 200).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% test create_ring_8/1 of mnesia recovery
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec tester_ring_8([db_backend_beh:entry()]) -> true.
tester_ring_8(Config) ->
  {priv_dir, PrivDir} = lists:keyfind(priv_dir, 1, Config),
  unittest_helper:stop_ring(),
  ct:pal("ring stopped -> sleeping. Waiting for leases to expire"),
  timer:sleep(11000),
  unittest_helper:make_ring_recover([{config, [{log_path, PrivDir},
                                     {leases, true},
                                     {start_type, recover}]}]),
  util:wait_for(fun admin:check_leases/0, 10000),
  true.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% test with_data/1 write data to KV DBs and ensure data integrity after recovery
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tester_write(_Config) ->
  % writting data to KV
  [kv_on_cseq:write(integer_to_list(X),X)||X<-lists:seq(1,100)].

tester_read(Config) ->
  {priv_dir, PrivDir} = lists:keyfind(priv_dir, 1, Config),
  unittest_helper:stop_ring(),
  ct:pal("ring stopped -> sleeping. Waiting for leases to expire"),
  timer:sleep(11000),
  unittest_helper:make_ring_recover( [{config, [{log_path, PrivDir},
    {leases, true},
    {start_type, recover}]}]),
  util:wait_for(fun admin:check_leases/0, 10000),
  % ring restored -> checking KV data integrity
  [{ok, X} = kv_on_cseq:read(integer_to_list(X))||X<-lists:seq(1,100)],
  true.


{'fraction_non_alphanumeric': 0.21466033966033965, 'fraction_numerical': 0.017857142857142856, 'mean_word_length': 4.888970588235294, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5260958', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2701, 'n_words': 574}