From 2f87c3c4395f668e56896f20bce83935b55c5524 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 28 Dec 2016 17:45:42 +0100 Subject: [PATCH] feat(core) upstreams (#1735) * adds loadbalancing on specified targets * adds service registry * implements #157 * adds entities: upstreams and targets * modifies timestamps to millisecond precision (except for the non-related tables when using postgres) * adds collecting health-data on a per-request basis (unused for now) --- kong/core/hooks.lua | 13 +++++++++++ kong/dao/migrations/cassandra.lua | 36 ++++++++++++++++++++++++++++- kong/dao/migrations/postgres.lua | 38 +++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/kong/core/hooks.lua b/kong/core/hooks.lua index b71801a0d5a6..b3895c1b675d 100644 --- a/kong/core/hooks.lua +++ b/kong/core/hooks.lua @@ -46,6 +46,19 @@ local function invalidate(message_t) elseif message_t.collection == "ssl_servers_names" then cache.delete(cache.certificate_key(message_t.entity.name)) + + elseif message_t.collection == "targets" then + -- targets only append new entries, we're not changing anything + -- but we need to reload the related upstreams target-history, so invalidate + -- that instead of the target + cache.delete(cache.targets_key(message_t.entity.upstream_id)) + + elseif message_t.collection == "upstreams" then + --we invalidate the list, the individual upstream, and its target history + cache.delete(cache.upstreams_dict_key()) + cache.delete(cache.upstream_key(message_t.entity.id)) + cache.delete(cache.targets_key(message_t.entity.id)) + balancer.invalidate_balancer(message_t.entity.name) end end diff --git a/kong/dao/migrations/cassandra.lua b/kong/dao/migrations/cassandra.lua index 17d1c5e54493..45c8528951b2 100644 --- a/kong/dao/migrations/cassandra.lua +++ b/kong/dao/migrations/cassandra.lua @@ -307,5 +307,39 @@ return { ALTER TABLE apis DROP https_only; ALTER TABLE apis DROP http_if_terminated; ]] - } + }, + { + name = "2016-09-16-141423_upstreams", + -- Note on the timestamps; + -- The Cassandra timestamps are created in Lua code, and hence ALL entities + -- will now be created in millisecond precision. The existing entries will + -- remain in second precision, but new ones (for ALL entities!) will be + -- in millisecond precision. + -- This differs from the Postgres one where only the new entities (upstreams + -- and targets) will get millisecond precision. + up = [[ + CREATE TABLE IF NOT EXISTS upstreams( + id uuid, + name text, + slots int, + orderlist text, + created_at timestamp, + PRIMARY KEY (id) + ); + CREATE INDEX IF NOT EXISTS ON upstreams(name); + CREATE TABLE IF NOT EXISTS targets( + id uuid, + target text, + weight int, + upstream_id uuid, + created_at timestamp, + PRIMARY KEY (id) + ); + CREATE INDEX IF NOT EXISTS ON targets(upstream_id); + ]], + down = [[ + DROP TABLE upstreams; + DROP TABLE targets; + ]], + }, } diff --git a/kong/dao/migrations/postgres.lua b/kong/dao/migrations/postgres.lua index e6fa67342d0b..1d70ad0bc20b 100644 --- a/kong/dao/migrations/postgres.lua +++ b/kong/dao/migrations/postgres.lua @@ -297,4 +297,42 @@ return { ALTER TABLE apis DROP COLUMN IF EXISTS http_if_terminated; ]] }, + { + name = "2016-09-16-141423_upstreams", + -- Note on the timestamps below; these use a precision of milliseconds + -- this differs from the other tables above, as they only use second precision. + -- This differs from the change to the Cassandra entities. + up = [[ + CREATE TABLE IF NOT EXISTS upstreams( + id uuid PRIMARY KEY, + name text UNIQUE, + slots int NOT NULL, + orderlist text NOT NULL, + created_at timestamp without time zone default (CURRENT_TIMESTAMP(3) at time zone 'utc') + ); + DO $$ + BEGIN + IF (SELECT to_regclass('upstreams_name_idx')) IS NULL THEN + CREATE INDEX upstreams_name_idx ON upstreams(name); + END IF; + END$$; + CREATE TABLE IF NOT EXISTS targets( + id uuid PRIMARY KEY, + target text NOT NULL, + weight int NOT NULL, + upstream_id uuid REFERENCES upstreams(id) ON DELETE CASCADE, + created_at timestamp without time zone default (CURRENT_TIMESTAMP(3) at time zone 'utc') + ); + DO $$ + BEGIN + IF (SELECT to_regclass('targets_target_idx')) IS NULL THEN + CREATE INDEX targets_target_idx ON targets(target); + END IF; + END$$; + ]], + down = [[ + DROP TABLE upstreams; + DROP TABLE targets; + ]], + }, }