Skip to content

Commit

Permalink
refactor(db) simplify schema consensus wait connector API
Browse files Browse the repository at this point in the history
Drop `post_run_up_migrations` and `post_run_teardown_migrations` in
favor of a single `wait_for_schema_consensus`, since no distinction are
really made between `up` and `teardown` post-steps of migrations
anymore.
  • Loading branch information
thibaultcha committed Jan 28, 2019
1 parent c05d37f commit f87a82f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 55 deletions.
6 changes: 3 additions & 3 deletions kong/db/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ do
if run_up then
-- ensure schema consensus is reached before running DML queries
-- that could span all peers
ok, err = self.connector:post_run_up_migrations()
ok, err = self.connector:wait_for_schema_consensus()
if not ok then
self.connector:close()
return nil, prefix_err(self, err)
Expand Down Expand Up @@ -595,7 +595,7 @@ do
-- DML queries; if the next migration runs its up step, it will
-- run DDL queries against the same node, so no need to reach
-- schema consensus
ok, err = self.connector:post_run_teardown_migrations()
ok, err = self.connector:wait_for_schema_consensus()
if not ok then
self.connector:close()
return nil, prefix_err(self, err)
Expand All @@ -614,7 +614,7 @@ do
-- wait for schema consensus after the last migration has run
-- (only if `run_up`, since if not, we just called it from the
-- teardown step)
ok, err = self.connector:post_run_teardown_migrations()
ok, err = self.connector:wait_for_schema_consensus()
if not ok then
self.connector:close()
return nil, prefix_err(self, err)
Expand Down
72 changes: 26 additions & 46 deletions kong/db/strategies/cassandra/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ local CassandraConnector = {}
CassandraConnector.__index = CassandraConnector


local function wait_for_schema_consensus(self)
local conn = self:get_stored_connection()
if not conn then
error("no connection")
end

log.verbose("waiting for Cassandra schema consensus (%dms timeout)...",
self.cluster.max_schema_consensus_wait)

local ok, err = self.cluster:wait_schema_consensus(conn)

log.verbose("Cassandra schema consensus: %s",
ok and "reached" or "not reached")

if err then
return nil, "failed to wait for schema consensus: " .. err
end

return true
end


function CassandraConnector.new(kong_config)
local cluster_options = {
shm = "kong_cassandra",
Expand Down Expand Up @@ -253,6 +231,28 @@ function CassandraConnector:close()
end


function CassandraConnector:wait_for_schema_consensus()
local conn = self:get_stored_connection()
if not conn then
error("no connection")
end

log.verbose("waiting for Cassandra schema consensus (%dms timeout)...",
self.cluster.max_schema_consensus_wait)

local ok, err = self.cluster:wait_schema_consensus(conn)

log.verbose("Cassandra schema consensus: %s",
ok and "reached" or "not reached")

if err then
return nil, "failed to wait for schema consensus: " .. err
end

return true
end


function CassandraConnector:query(query, args, opts, operation)
if operation ~= nil and operation ~= "read" and operation ~= "write" then
error("operation must be 'read' or 'write', was: " .. tostring(operation), 2)
Expand Down Expand Up @@ -393,7 +393,7 @@ function CassandraConnector:reset()
end
end

ok, err = wait_for_schema_consensus(self)
ok, err = self:wait_for_schema_consensus()
if not ok then
self:setkeepalive()
return nil, err
Expand Down Expand Up @@ -482,7 +482,7 @@ function CassandraConnector:setup_locks(default_ttl, no_schema_consensus)
if not no_schema_consensus then
-- called from tests, ignored when called from bootstrapping, since
-- we wait for schema consensus as part of bootstrap
ok, err = wait_for_schema_consensus(self)
ok, err = self:wait_for_schema_consensus()
if not ok then
self:setkeepalive()
return nil, err
Expand Down Expand Up @@ -720,7 +720,7 @@ do
return nil, err
end

ok, err = wait_for_schema_consensus(self)
ok, err = self:wait_for_schema_consensus()
if not ok then
return nil, err
end
Expand All @@ -744,7 +744,7 @@ do

log("dropped '%s' keyspace", self.keyspace)

ok, err = wait_for_schema_consensus(self)
ok, err = self:wait_for_schema_consensus()
if not ok then
return nil, err
end
Expand Down Expand Up @@ -851,26 +851,6 @@ do
end


function CassandraConnector:post_run_up_migrations()
local ok, err = wait_for_schema_consensus(self)
if not ok then
return nil, err
end

return true
end


function CassandraConnector:post_run_teardown_migrations()
local ok, err = wait_for_schema_consensus(self)
if not ok then
return nil, err
end

return true
end


local function does_table_exist(self, table_name)
local cql

Expand Down
7 changes: 1 addition & 6 deletions kong/db/strategies/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,7 @@ function Connector:run_up_migration()
end


function Connector:post_run_up_migrations()
return true
end


function Connector:post_run_teardown_migrations()
function Connector:wait_for_schema_consensus()
return true
end

Expand Down

0 comments on commit f87a82f

Please sign in to comment.