Skip to content

Commit 9c3e912

Browse files
committed
Add primary node discovery
Early versions of tests always first node as a primary in a cluster. With automated leader election leader can be on any node in clustera, so discovery of primary instance has been added. It consists of SQL function `_LEADER()` implemented in Lua, ``` unix/:/var/run/tarantool/jepsen.control> box.execute([[SELECT _LEADER()]]) --- - metadata: - name: COLUMN_1 type: string rows: - ['89.208.84.154'] ... ``` `primary` function that connects to desired node and gets IP address of primary instance using `_LEADER()` function and `primaries` function that gather information about primaries on every node in a cluster. Closes #43 Closes #17
1 parent 4cba2aa commit 9c3e912

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

resources/tarantool/jepsen.lua

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ single_mode = %TARANTOOL_SINGLE_MODE%
22

33
if single_mode then
44
box.cfg {
5-
listen = 3301;
5+
listen = '%TARANTOOL_IP_ADDRESS%:3301';
66
log_level = 6;
77
log_nonblock = false;
88
too_long_threshold = 0.5;
@@ -12,7 +12,7 @@ box.cfg {
1212
}
1313
else
1414
box.cfg {
15-
listen = 3301;
15+
listen = '%TARANTOOL_IP_ADDRESS%:3301';
1616
replication = { %TARANTOOL_REPLICATION% };
1717
read_only = %TARANTOOL_IS_READ_ONLY%;
1818
replication_synchro_quorum = 2;
@@ -76,6 +76,31 @@ box.schema.func.create('_UPSERT',
7676
param_list = {'integer', 'integer', 'string'},
7777
exports = {'LUA', 'SQL'},
7878
is_deterministic = true})
79+
80+
--[[ Function returns IP address of a node where current leader
81+
of synchronous cluster with enabled Raft consensus protocol is started.
82+
Returns nil when Raft is disabled. Example: SELECT _LEADER()
83+
]]
84+
box.schema.func.create('_LEADER',
85+
{language = 'LUA',
86+
returns = 'string',
87+
body = [[function()
88+
local leader_id = box.info.election.leader
89+
if leader_id == 0 or leader_id == nil then
90+
return nil
91+
end
92+
local leader_upstream = box.info.replication[leader_id].upstream
93+
if leader_upstream == nil then
94+
return string.match(box.info.listen, '(.+):[0-9]+')
95+
end
96+
local leader_ip_address = string.match(leader_upstream.peer, '[A-z]+@(.+):[0-9]+')
97+
98+
return leader_ip_address
99+
end]],
100+
is_sandboxed = false,
101+
param_list = {},
102+
exports = {'LUA', 'SQL'},
103+
is_deterministic = true})
79104
end
80105

81106
box.once('jepsen', bootstrap)

src/tarantool/client.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(:require [clojure.string :as str]
44
[clojure.tools.logging :refer [info warn]]
55
[next.jdbc :as j]
6+
[next.jdbc.sql :as sql]
67
[dom-top.core :as dt]
78
[next.jdbc.connection :as connection]))
89

@@ -56,6 +57,13 @@
5657
(info :caught-cause (.cause (:rollback (ex-data e#))))
5758
(throw e#))))))
5859

60+
(defn primary
61+
[node]
62+
(let [conn (open node test)
63+
leader (:COLUMN_1 (first (sql/query conn ["SELECT _LEADER()"])))]
64+
;(assert leader)
65+
leader))
66+
5967
(defmacro with-txn-aborts
6068
"Aborts body on rollbacks."
6169
[op & body]

0 commit comments

Comments
 (0)