Skip to content

Add counter test #29

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

Merged
merged 1 commit into from
Oct 1, 2020
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
88 changes: 88 additions & 0 deletions src/tarantool/counter.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
(ns tarantool.counter
"Incrementing and decrementing a counter."
(:require [jepsen [client :as client]
[checker :as checker]
[generator :as gen]
[independent :as independent]]
[jepsen.checker.timeline :as timeline]
[clojure.tools.logging :refer [debug info warn]]
[next.jdbc :as j]
[next.jdbc.sql :as sql]
[tarantool.client :as cl]
[jepsen.core :as jepsen]
[knossos.model :as model]
[knossos.op :as op]))

(def table-name "counter")

(defrecord CounterClient [conn]
client/Client

(open! [this test node]
(let [conn (cl/open node test)]
(assert conn)
(assoc this :conn conn :node node)))

(setup! [this test node]
(let [conn (cl/open node test)]
(assert conn)
(when (= node (jepsen/primary test))
(cl/with-conn-failure-retry conn
(j/execute! conn [(str "CREATE TABLE IF NOT EXISTS " table-name
" (id INT NOT NULL PRIMARY KEY,
count INT NOT NULL)")]))
(sql/insert! conn table-name {:id 0 :count 0}))
(assoc this :conn conn :node node)))

(invoke! [this test op]
(cl/with-error-handling op
(cl/with-txn-aborts op
(case (:f op)
:add (do (j/execute! conn
[(str "UPDATE " table-name " SET count = count + ? WHERE id = 0") (:value op)])
(assoc op :type :ok))

:read (let [value (:COUNT
(first (sql/query conn
[(str "SELECT count FROM " table-name " WHERE id = 0")])))]
(assoc op :type :ok :value value))))))

(teardown! [_ test]
(cl/with-conn-failure-retry conn
(j/execute! conn [(str "DROP TABLE IF EXISTS " table-name)])))

(close! [_ test]))

(def add {:type :invoke :f :add :value 1})
(def sub {:type :invoke :f :add :value -1})
(def r {:type :invoke :f :read})

(defn with-op-index
"Append :op-index integer to every operation emitted by the given generator.
Value starts at 1 and increments by 1 for every subsequent emitted operation."
[gen]
(let [ctr (atom 0)]
(gen/map (fn add-op-index [op]
(assoc op :op-index (swap! ctr inc)))
gen)))

(defn workload-inc
[opts]
{:client (CounterClient. nil)
:generator (->> (repeat 100 add)
(cons r)
gen/mix
(gen/delay 1/10)
(with-op-index))
:checker (checker/compose
{:timeline (timeline/html)
:counter (checker/counter)})})

(defn workload-dec
[opts]
(assoc (workload-inc opts)
:generator (->> (take 100 (cycle [add sub]))
(cons r)
gen/mix
(gen/delay 1/10)
(with-op-index))))
5 changes: 4 additions & 1 deletion src/tarantool/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
[jepsen.os.ubuntu :as ubuntu]
[tarantool [db :as db]
[register :as register]
[sets :as sets]]))
[sets :as sets]
[counter :as counter]]))

(def workloads
"A map of workload names to functions that can take opts and construct
Expand All @@ -36,6 +37,8 @@
Or, for some special cases where nemeses and workloads are coupled, we return
a keyword here instead."
{:set sets/workload
:counter-inc counter/workload-inc
:counter-dec counter/workload-dec
;:bank bank/workload
;:bank-index bank/index-workload
;:g2 g2/workload
Expand Down