You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2025/05/29 13:44:19 UTC

(couchdb) branch auto-delete-3 updated (f35bbd231 -> 013ad352e)

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a change to branch auto-delete-3
in repository https://gitbox.apache.org/repos/asf/couchdb.git


    from f35bbd231 PropEr test
     new ba86d20ab remove n=1 erlang proper test
     new 013ad352e PropCheck test for drop_seq against 3 node cluster

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .gitignore                                         |   2 +
 Makefile                                           |   7 +
 mix.exs                                            |   5 +-
 .../test/eunit/fabric_drop_seq_prop_tests.erl      | 248 ---------------------
 test/elixir/lib/utils.ex                           |   2 +-
 test/elixir/test/drop_seq_statem_test.exs          | 232 +++++++++++++++++++
 6 files changed, 245 insertions(+), 251 deletions(-)
 delete mode 100644 src/fabric/test/eunit/fabric_drop_seq_prop_tests.erl
 create mode 100644 test/elixir/test/drop_seq_statem_test.exs


(couchdb) 02/02: PropCheck test for drop_seq against 3 node cluster

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch auto-delete-3
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 013ad352e885d7a06b658fa9fa3d438181520f6d
Author: Robert Newson <rn...@apache.org>
AuthorDate: Wed May 28 14:24:02 2025 +0100

    PropCheck test for drop_seq against 3 node cluster
---
 .gitignore                                |   2 +
 Makefile                                  |   7 +
 mix.exs                                   |   5 +-
 test/elixir/lib/utils.ex                  |   2 +-
 test/elixir/test/drop_seq_statem_test.exs | 232 ++++++++++++++++++++++++++++++
 5 files changed, 245 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index 080a7dd6f..4d4918a73 100644
--- a/.gitignore
+++ b/.gitignore
@@ -78,6 +78,8 @@ src/unicode_util_compat/
 src/file_system/
 src/rebar3/
 src/erlfmt/
+src/libgraph/
+src/propcheck/
 src/*.erl
 tmp/
 
diff --git a/Makefile b/Makefile
index df2163d54..317188fe0 100644
--- a/Makefile
+++ b/Makefile
@@ -254,6 +254,13 @@ elixir-cluster-with-quorum: elixir-init devclean
 		--degrade-cluster 1 \
 		--no-eval 'mix test --trace --only with_quorum_test $(EXUNIT_OPTS)'
 
+.PHONY: elixir-cluster
+elixir-cluster: export MIX_ENV=integration
+elixir-cluster: elixir-init devclean
+#   @rm -f _build/propcheck.ctex
+	@dev/run -n 3 -q -a adm:pass \
+		--no-eval 'mix test --trace --only with_cluster $(EXUNIT_OPTS)'
+
 .PHONY: elixir
 # target: elixir - Run Elixir-based integration tests
 elixir: export MIX_ENV=integration
diff --git a/mix.exs b/mix.exs
index 4f0cb4ba3..86fff4227 100644
--- a/mix.exs
+++ b/mix.exs
@@ -71,7 +71,7 @@ defmodule CouchDBTest.Mixfile do
   end
 
   # Run "mix help compile.app" to learn about applications.
-  def application, do: [applications: [:logger, :httpotion]]
+  def application, do: [applications: [:logger, :httpotion, :propcheck]]
 
   # Specifies which paths to compile per environment.
   defp elixirc_paths(:test), do: ["test/elixir/lib", "test/elixir/test/support"]
@@ -85,7 +85,8 @@ defmodule CouchDBTest.Mixfile do
       {:httpotion, ">= 3.2.0", only: [:dev, :test, :integration], runtime: false},
       {:excoveralls, "~> 0.18.3", only: :test},
       {:ibrowse, path: path("ibrowse"), override: true},
-      {:credo, "~> 1.7.11", only: [:dev, :test, :integration], runtime: false}
+      {:credo, "~> 1.7.11", only: [:dev, :test, :integration], runtime: false},
+      {:propcheck, ">= 1.5.0", only: [:dev, :test, :integration]},
     ]
 
     extra_deps = [:b64url, :jiffy, :jwtf, :meck, :mochiweb]
diff --git a/test/elixir/lib/utils.ex b/test/elixir/lib/utils.ex
index 3ecf878e7..15e321240 100644
--- a/test/elixir/lib/utils.ex
+++ b/test/elixir/lib/utils.ex
@@ -58,4 +58,4 @@ defmodule Couch.Test.Utils do
       end)
   end
 
-end
\ No newline at end of file
+end
diff --git a/test/elixir/test/drop_seq_statem_test.exs b/test/elixir/test/drop_seq_statem_test.exs
new file mode 100644
index 000000000..84505bd08
--- /dev/null
+++ b/test/elixir/test/drop_seq_statem_test.exs
@@ -0,0 +1,232 @@
+defmodule DropSeqStateM do
+  use PropCheck, default_opts: &PropCheck.TestHelpers.config/0
+  use PropCheck.StateM
+  use CouchTestCase
+
+  # alias Couch.Test.Utils
+  # import Utils
+
+  @moduletag capture_log: true
+
+  # expected to pass in all three cluster scenarios
+  @moduletag :with_cluster
+  @moduletag :without_quorum_test
+  @moduletag :with_quorum_test
+
+  property "drop_seq works fine", start_size: 5, max_size: 100, numtests: 2000 do
+    forall cmds <- more_commands(100, commands(__MODULE__)) do
+      trap_exit do
+        db_name = random_db_name()
+        {:ok, _} = create_db(db_name, query: %{n: 3, q: 4})
+        r = run_commands(__MODULE__, cmds, [{:dbname, db_name}])
+        {history, state, result} = r
+        delete_db(db_name)
+
+        (result == :ok)
+        |> when_fail(
+          IO.puts("""
+          Commands: #{inspect(cmds, pretty: true)}
+          History: #{inspect(history, pretty: true)}
+          State: #{inspect(state, pretty: true)}
+          Result: #{inspect(result, pretty: true)}
+          """)
+        )
+      end
+    end
+  end
+
+  require Record
+
+  Record.defrecord(:state,
+    docs: [],
+    deleted_docs: [],
+    current_seq: 0,
+    peer_checkpoint_seq: nil,
+    drop_seq: nil,
+    drop_count: 0
+  )
+
+  def initial_state() do
+    state()
+  end
+
+  @max_doc_ids 10
+  @doc_ids 1..@max_doc_ids |> Enum.map(&"doc-#{&1}")
+
+  def doc_id, do: oneof(@doc_ids)
+
+  def command(_state) do
+    oneof([
+      {:call, __MODULE__, :update_document, [{:var, :dbname}, doc_id()]},
+      {:call, __MODULE__, :delete_document, [{:var, :dbname}, doc_id()]},
+      {:call, __MODULE__, :update_peer_checkpoint, [{:var, :dbname}]},
+      {:call, __MODULE__, :update_drop_seq, [{:var, :dbname}]},
+      {:call, __MODULE__, :compact_db, [{:var, :dbname}]},
+      {:call, __MODULE__, :changes, [{:var, :dbname}]}
+    ])
+  end
+
+  def get_document(db_name, doc_id) do
+    resp = Couch.get("/#{db_name}/#{doc_id}")
+
+    case resp.status_code do
+      200 ->
+        {:ok, resp.body}
+
+      404 ->
+        {:not_found, resp.body["reason"]}
+    end
+  end
+
+  def update_document(db_name, doc_id) do
+    case get_document(db_name, doc_id) do
+      {:ok, doc} ->
+        resp = Couch.put("/#{db_name}/#{doc_id}", body: doc)
+        assert resp.status_code == 201
+
+      {:not_found, _} ->
+        resp = Couch.put("/#{db_name}/#{doc_id}", body: %{})
+        assert resp.status_code == 201
+    end
+    sync_shards(db_name)
+  end
+
+  def delete_document(db_name, doc_id) do
+    case get_document(db_name, doc_id) do
+      {:ok, doc} ->
+        rev = doc["_rev"]
+        resp = Couch.delete("/#{db_name}/#{doc_id}?rev=#{rev}")
+        assert resp.status_code == 200
+
+      {:not_found, _} ->
+        :ok
+    end
+    sync_shards(db_name)
+  end
+
+  def update_peer_checkpoint(db_name) do
+    resp = Couch.get("/#{db_name}")
+    assert resp.status_code == 200
+    update_seq = resp.body["update_seq"]
+
+    resp =
+      Couch.put("/#{db_name}/_local/peer-checkpoint-foo",
+        body: %{
+          update_seq: update_seq
+        }
+      )
+
+    assert resp.status_code == 201
+  end
+
+  def update_drop_seq(db_name) do
+    resp = Couch.post("/#{db_name}/_update_drop_seq")
+    assert resp.status_code == 201
+    resp.body
+  end
+
+  def compact_db(db_name) do
+    compact(db_name)
+    # try to avoid seeing pre-compact state of shards immediately after
+    # compactor pids exit
+    :timer.sleep(1000)
+  end
+
+  def changes(db_name) do
+    resp = Couch.get("/#{db_name}/_changes")
+    assert resp.status_code == 200
+
+    List.foldl(resp.body["results"], {[], []}, fn change, {doc_ids, del_doc_ids} ->
+      if change["deleted"] do
+        {doc_ids, Enum.sort([change["id"] | del_doc_ids])}
+      else
+        {Enum.sort([change["id"] | doc_ids]), del_doc_ids}
+      end
+    end)
+  end
+
+  def sync_shards(db_name) do
+    resp = Couch.post("/#{db_name}/_sync_shards")
+    assert resp.status_code == 202
+    :timer.sleep(1000)
+  end
+
+  def precondition(s, {:call, _, :update_document, [_db_name, doc_id]}) do
+    not doc_exists(s, doc_id)
+  end
+
+  def precondition(s, {:call, _, :delete_document, [_db_name, doc_id]}) do
+    doc_exists(s, doc_id)
+  end
+
+  def precondition(_, _) do
+    true
+  end
+
+  def next_state(s, _v, {:call, _, :update_document, [_db_name, doc_id]}) do
+    state(s,
+      current_seq: state(s, :current_seq) + 1,
+      docs: Enum.sort([doc_id | state(s, :docs)]),
+      deleted_docs: List.keydelete(state(s, :deleted_docs), doc_id, 0)
+    )
+  end
+
+  def next_state(s, _v, {:call, _, :delete_document, [_db_name, doc_id]}) do
+    state(s,
+      current_seq: state(s, :current_seq) + 1,
+      docs: List.delete(state(s, :docs), doc_id),
+      deleted_docs:
+        Enum.sort([{doc_id, state(s, :current_seq) + 1} | state(s, :deleted_docs)])
+    )
+  end
+
+  def next_state(s, _v, {:call, _, :update_peer_checkpoint, [_db_name]}) do
+    state(s, peer_checkpoint_seq: state(s, :current_seq))
+  end
+
+  def next_state(s, _v, {:call, _, :update_drop_seq, [_db_name]}) do
+    # we'll drop all tombstones if _update_drop_seq is called when there
+    # are no peer checkpoint docs as the only peers are the shard syncs
+    # which update automatically
+    drop_seq =
+      if state(s, :peer_checkpoint_seq) == nil,
+        do: state(s, :current_seq),
+        else: state(s, :peer_checkpoint_seq)
+
+    state(s, drop_seq: drop_seq)
+  end
+
+  def next_state(s, _v, {:call, _, :compact_db, [_db_name]}) do
+    {keep_docs, drop_docs} =
+      Enum.split_with(state(s, :deleted_docs), fn {_, seq} ->
+        state(s, :drop_seq) == nil or seq > state(s, :drop_seq)
+      end)
+
+    state(s,
+      deleted_docs: keep_docs,
+      drop_count: state(s, :drop_count) + length(drop_docs)
+    )
+  end
+
+  def next_state(s, _v, {:call, _, :changes, [_db_name]}) do
+    s
+  end
+
+  def postcondition(s, {:call, _, :changes, [_db_name]}, {doc_ids, del_doc_ids}) do
+    doc_ids == doc_ids(s) and del_doc_ids == deleted_doc_ids(s)
+  end
+
+  def postcondition(_, _, _), do: true
+
+  def doc_exists(s, doc_id), do: doc_id in state(s, :docs)
+
+  def deleted_doc_exists(s, doc_id) do
+    List.keymember?(state(s, :deleted_docs), doc_id, 0)
+  end
+
+  def doc_ids(s), do: state(s, :docs)
+
+  def deleted_doc_ids(s) do
+    Enum.map(state(s, :deleted_docs), fn {doc_id, _} -> doc_id end)
+  end
+end


(couchdb) 01/02: remove n=1 erlang proper test

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch auto-delete-3
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit ba86d20ab593796e2caf70f8f2ad432b51d7302f
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu May 29 12:25:53 2025 +0100

    remove n=1 erlang proper test
---
 .../test/eunit/fabric_drop_seq_prop_tests.erl      | 248 ---------------------
 1 file changed, 248 deletions(-)

diff --git a/src/fabric/test/eunit/fabric_drop_seq_prop_tests.erl b/src/fabric/test/eunit/fabric_drop_seq_prop_tests.erl
deleted file mode 100644
index ac5ae1e9a..000000000
--- a/src/fabric/test/eunit/fabric_drop_seq_prop_tests.erl
+++ /dev/null
@@ -1,248 +0,0 @@
-% 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.
-
--module(fabric_drop_seq_prop_tests).
-
--behaviour(proper_statem).
-
--include_lib("couch/include/couch_eunit_proper.hrl").
-
--export([
-    initial_state/0,
-    command/1,
-    precondition/2,
-    postcondition/3,
-    next_state/3
-]).
-
--export([
-    get_document/2,
-    changes/1,
-    update_document/2,
-    delete_document/2,
-    update_peer_checkpoint/1,
-    update_drop_seq/1,
-    compact/1
-]).
-
--include_lib("couch/include/couch_db.hrl").
--include_lib("couch/include/couch_eunit.hrl").
-
--type docid() :: binary().
--type seq() :: non_neg_integer().
-
--record(state, {
-    docs :: [{docid()}],
-    deleted_docs :: [{docid(), seq()}],
-    current_seq :: seq(),
-    peer_checkpoint_seq :: seq(),
-    drop_seq :: seq(),
-    drop_count :: non_neg_integer()
-}).
-
-property_test_() ->
-    ?EUNIT_QUICKCHECK(10000, 2000).
-
-prop_drop_seq() ->
-    ?FORALL(
-        Cmds,
-        commands(?MODULE),
-        ?TRAPEXIT(
-            begin
-                {DbName, Ctx} = setup(),
-                {History, State, Result} = run_commands(?MODULE, Cmds, [{dbname, DbName}]),
-                teardown(DbName, Ctx),
-                ?WHENFAIL(
-                    io:format(
-                        "~nHistory: ~p~n~nState: ~p~n~nResult: ~p~n~n",
-                        [History, State, Result]
-                    ),
-                    aggregate(command_names(Cmds), Result =:= ok)
-                )
-            end
-        )
-    ).
-
-setup() ->
-    Ctx = test_util:start_couch([fabric]),
-    DbName = ?tempdb(),
-    ok = fabric:create_db(DbName, [{q, 4}, {n, 1}]),
-    {DbName, Ctx}.
-
-teardown(DbName, Ctx) ->
-    ok = fabric:delete_db(DbName, [?ADMIN_CTX]),
-    test_util:stop_couch(Ctx).
-
-initial_state() ->
-    #state{
-        docs = [],
-        deleted_docs = [],
-        current_seq = 0,
-        peer_checkpoint_seq = undefined,
-        drop_seq = undefined,
-        drop_count = 0
-    }.
-
-command(S) ->
-    HasDocs = (S#state.docs =/= []),
-    frequency(
-        [
-            {4, {call, ?MODULE, update_document, [{var, dbname}, docid()]}},
-            {1, {call, ?MODULE, get_document, [{var, dbname}, docid()]}},
-            {1, {call, ?MODULE, changes, [{var, dbname}]}},
-            {1, {call, ?MODULE, update_peer_checkpoint, [{var, dbname}]}},
-            {1, {call, ?MODULE, update_drop_seq, [{var, dbname}]}},
-            {1, {call, ?MODULE, compact, [{var, dbname}]}}
-        ] ++
-            [{4, {call, ?MODULE, delete_document, [{var, dbname}, docid()]}} || HasDocs]
-    ).
-
-get_document(DbName, DocId) ->
-    fabric:open_doc(DbName, DocId, [?ADMIN_CTX]).
-
-changes(DbName) ->
-    Acc0 = {[], []},
-    Callback = fun
-        ({change, {Change}}, {DocIds, DelDocIds}) ->
-            Id = couch_util:get_value(id, Change),
-            Deleted = couch_util:get_value(deleted, Change),
-            if
-                Deleted ->
-                    {ok, {DocIds, lists:merge([Id], DelDocIds)}};
-                true ->
-                    {ok, {lists:merge([Id], DocIds), DelDocIds}}
-            end;
-        (_Else, Acc) ->
-            {ok, Acc}
-    end,
-    fabric:changes(DbName, Callback, Acc0, []).
-
-update_document(DbName, DocId) ->
-    update_document(DbName, DocId, false).
-
-delete_document(DbName, DocId) ->
-    update_document(DbName, DocId, true).
-
-update_document(DbName, DocId, Deleted) ->
-    case fabric:open_doc(DbName, DocId, [?ADMIN_CTX]) of
-        {ok, Doc} ->
-            {ok, _} = fabric:update_doc(DbName, Doc#doc{deleted = Deleted}, [?ADMIN_CTX]);
-        {not_found, _} when not Deleted ->
-            {ok, _} = fabric:update_doc(DbName, #doc{id = DocId}, [?ADMIN_CTX]);
-        {not_found, _} ->
-            ok
-    end.
-
-update_peer_checkpoint(DbName) ->
-    {ok, DbInfo} = fabric:get_db_info(DbName),
-    UpdateSeq = couch_util:get_value(update_seq, DbInfo),
-    Doc = #doc{id = <<"_local/peer-checkpoint-foo">>, body = {[{<<"update_seq">>, UpdateSeq}]}},
-    {ok, _} = fabric:update_doc(
-        DbName,
-        Doc,
-        [?ADMIN_CTX]
-    ).
-
-update_drop_seq(DbName) ->
-    {ok, _} = fabric_drop_seq:go(DbName).
-
-compact(DbName) ->
-    ok = fabric:compact(DbName),
-    wait_for_compaction_to_finish(DbName).
-
-wait_for_compaction_to_finish(DbName) ->
-    {ok, DbInfo} = fabric:get_db_info(DbName),
-    CompactRunning = couch_util:get_value(compact_running, DbInfo),
-    if
-        CompactRunning ->
-            timer:sleep(500),
-            wait_for_compaction_to_finish(DbName);
-        true ->
-            timer:sleep(500),
-            ok
-    end.
-
-precondition(S, {call, _, update_document, [_DbName, DocId]}) ->
-    not doc_exists(S, DocId);
-precondition(S, {call, _, delete_document, [_DbName, DocId]}) ->
-    doc_exists(S, DocId);
-precondition(_, _) ->
-    true.
-
-next_state(S, _V, {call, _, update_document, [_DbName, DocId]}) ->
-    S#state{
-        current_seq = S#state.current_seq + 1,
-        docs = lists:merge([DocId], S#state.docs),
-        deleted_docs = lists:keydelete(DocId, 1, S#state.deleted_docs)
-    };
-next_state(S, _V, {call, _, delete_document, [_DbName, DocId]}) ->
-    S#state{
-        current_seq = S#state.current_seq + 1,
-        docs = lists:delete(DocId, S#state.docs),
-        deleted_docs = lists:merge([{DocId, S#state.current_seq + 1}], S#state.deleted_docs)
-    };
-next_state(S, _V, {call, _, update_peer_checkpoint, [_DbName]}) ->
-    S#state{
-        peer_checkpoint_seq = S#state.current_seq
-    };
-next_state(S, _V, {call, _, update_drop_seq, [_DbName]}) ->
-    S#state{
-        drop_seq = S#state.peer_checkpoint_seq
-    };
-next_state(S, _V, {call, _, compact, [_DbName]}) ->
-    {KeepDocs, DropDocs} = lists:partition(
-        fun({_, Seq}) ->
-            S#state.drop_seq == undefined orelse Seq > S#state.drop_seq
-        end,
-        S#state.deleted_docs
-    ),
-    S#state{
-        deleted_docs = KeepDocs,
-        drop_count = S#state.drop_count + length(DropDocs)
-    };
-next_state(S, _Res, _Call) ->
-    S.
-
-postcondition(S, {call, _, get_document, [_DbName, DocId]}, {ok, _Doc}) ->
-    doc_exists(S, DocId) andalso not deleted_doc_exists(S, DocId);
-postcondition(S, {call, _, get_document, [_DbName, DocId]}, {not_found, deleted}) ->
-    not doc_exists(S, DocId) andalso deleted_doc_exists(S, DocId);
-postcondition(S, {call, _, get_document, [_DbName, DocId]}, {not_found, missing}) ->
-    not doc_exists(S, DocId) andalso not deleted_doc_exists(S, DocId);
-postcondition(S, {call, _, changes, [_DbName]}, {ok, {DocIds, DelDocIds}}) ->
-    same_list(DocIds, doc_ids(S)) andalso same_list(DelDocIds, deleted_doc_ids(S));
-postcondition(_, _, _) ->
-    true.
-
-docid() ->
-    elements([<<"doc", (integer_to_binary(N))/binary>> || N <- lists:seq(1, 10)]).
-
-docid(Bin) when is_binary(Bin) ->
-    Bin;
-docid(Tuple) when is_tuple(Tuple) ->
-    element(1, Tuple).
-
-doc_exists(S, DocId) ->
-    lists:member(DocId, S#state.docs).
-
-deleted_doc_exists(S, DocId) ->
-    lists:keymember(DocId, 1, S#state.deleted_docs).
-
-doc_ids(S) ->
-    S#state.docs.
-deleted_doc_ids(S) ->
-    [docid(T) || T <- S#state.deleted_docs].
-
-same_list(ListA, ListB) ->
-    SortedA = lists:sort(ListA),
-    SortedB = lists:sort(ListB),
-    SortedA == SortedB.