tarantool / jepsen.tarantool

Jepsen tests for Tarantool
https://www.tarantool.io/en/
Other
7 stars 0 forks source link

Rewrite operations of a client in register workload to SQL #31

Closed ligurio closed 4 years ago

ligurio commented 4 years ago

register was a first experience in a jepsen testing it contains two operations (write and read) written using Lua. Although both can be implemented using SQL.

https://github.com/tarantool/jepsen.tarantool/blob/master/src/tarantool/register.clj

  (invoke! [this test op]
     (case (:f op)
       :read (assoc op
                    :type  :ok
                    :value (cl/read-v-by-k conn 1))
       :write (do (let [con (cl/open (jepsen/primary test) test)]
                   (cl/write-v-by-k con 1 (:value op)))
                   (assoc op :type :ok))
       :cas (let [[old new] (:value op)
                  con (cl/open (jepsen/primary test) test)]
                  (assoc op :type (if (cl/compare-and-set con 1 old new)
                                   :ok
                                   :fail)))))

  (teardown! [this test])
      ;(j/execute! conn ["DROP TABLE jepsen"]))

https://github.com/tarantool/jepsen.tarantool/blob/master/src/tarantool/client.clj

(defn read-v-by-k
  "Reads the current value of a key."
  [conn k]
  (first (vals (first (j/execute! conn ["SELECT _READ(?, 'JEPSEN')" k])))))

(defn write-v-by-k
  "Writes the current value of a key."
  [conn k v]
  (j/execute! conn ["SELECT _WRITE(?, ?, 'JEPSEN')"
                    k v]))

(defn compare-and-set
  [conn id old new]
  (first (vals (first (j/execute! conn ["SELECT _CAS(?, ?, ?, 'JEPSEN')"
                                        id old new])))))

https://github.com/tarantool/jepsen.tarantool/blob/master/resources/tarantool/jepsen.lua


--[[ Function implements an WRITE operation, which takes a key and value
and sets the key to the value if and only if the key is already exists, and
insert value if it is absent.
Example: SELECT _WRITE(1, 3, 'JEPSEN')
]]
box.schema.func.create('_WRITE',
   {language = 'LUA',
    returns = 'integer',
    body = [[function (id, value, table)
             box.space[table]:upsert({id, value}, {{'=', 1, 1}, {'=', 2, value}})
             return value
             end]],
    is_sandboxed = false,
    param_list = {'integer', 'integer', 'string'},
    exports = {'LUA', 'SQL'},
    is_deterministic = true})

--[[ Function implements an READ operation, which takes a key and returns a
value.
Example: SELECT _READ(1, 'JEPSEN')
]]
box.schema.func.create('_READ',
   {language = 'LUA',
    returns = 'integer',
    body = [[function (id, table)
             box.begin()
             local tuple = box.space[table]:get{id}
             if tuple then
                 return tuple[2]
             end
             box.commit()
             return nil
             end]],
    is_sandboxed = false,
    param_list = {'integer', "string"},
    exports = {'LUA', 'SQL'},
    is_deterministic = true})