From 3c3d2230ca85e96840310ba3accac1e3553ac522 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 3 Dec 2019 16:32:42 +0100 Subject: [PATCH] HA: Populate environment table --- ha/ha.go | 20 +++++++++++++++++++- ha/ha_test.go | 27 +++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/ha/ha.go b/ha/ha.go index 4b271e35..dfc48e0e 100644 --- a/ha/ha.go +++ b/ha/ha.go @@ -6,6 +6,7 @@ import ( "bytes" "encoding/hex" "errors" + "fmt" "github.com/Icinga/icingadb/connection" "github.com/Icinga/icingadb/supervisor" "github.com/go-redis/redis" @@ -53,11 +54,13 @@ var mysqlObservers = struct { updateIcingadbInstanceById prometheus.Observer updateIcingadbInstanceByEnvironmentId prometheus.Observer insertIntoIcingadbInstance prometheus.Observer + insertIntoEnvironment prometheus.Observer selectIdHeartbeatFromIcingadbInstanceByEnvironmentId prometheus.Observer }{ connection.DbIoSeconds.WithLabelValues("mysql", "update icingadb_instance by id"), connection.DbIoSeconds.WithLabelValues("mysql", "update icingadb_instance by environment_id"), connection.DbIoSeconds.WithLabelValues("mysql", "insert into icingadb_instance"), + connection.DbIoSeconds.WithLabelValues("mysql", "insert into environment"), connection.DbIoSeconds.WithLabelValues("mysql", "select id, heartbeat from icingadb_instance where environment_id = ourEnvID"), } @@ -109,6 +112,10 @@ func (h *HA) getInstance() (bool, uuid.UUID, int64, error) { func (h *HA) StartHA(chEnv chan *Environment) { env := h.waitForEnvironment(chEnv) + err := h.setAndInsertEnvironment(env) + if err != nil { + h.super.ChErr <- fmt.Errorf("Could not insert environment into MySQL: %s", err.Error()) + } h.logger = log.WithFields(log.Fields{ "context": "HA", @@ -138,10 +145,21 @@ func (h *HA) waitForEnvironment(chEnv chan *Environment) *Environment { return &Environment{} } - h.super.EnvId = env.ID return env } +func (h *HA) setAndInsertEnvironment(env *Environment) error { + h.super.EnvId = env.ID + + _, err := h.super.Dbw.SqlExec( + mysqlObservers.insertIntoEnvironment, + "REPLACE INTO environment(id, name) VALUES (?, ?)", + env.ID, env.Name, + ) + + return err +} + func (h *HA) checkResponsibility(env *Environment) { found, _, beat, err := h.getInstance() if err != nil { diff --git a/ha/ha_test.go b/ha/ha_test.go index ca5a0dcd..6d8a42ce 100644 --- a/ha/ha_test.go +++ b/ha/ha_test.go @@ -7,6 +7,7 @@ import ( "github.com/Icinga/icingadb/config/testbackends" "github.com/Icinga/icingadb/connection" "github.com/Icinga/icingadb/supervisor" + "github.com/Icinga/icingadb/utils" "github.com/go-redis/redis" "github.com/google/uuid" log "github.com/sirupsen/logrus" @@ -130,8 +131,8 @@ func TestHA_waitForEnvironment(t *testing.T) { wg.Add(1) go func() { - ha.waitForEnvironment(chEnv) - assert.Equal(t, []byte("my.env"), ha.super.EnvId) + env := ha.waitForEnvironment(chEnv) + assert.Equal(t, []byte("my.env"), env.ID) wg.Done() }() @@ -139,6 +140,28 @@ func TestHA_waitForEnvironment(t *testing.T) { wg.Wait() } +func TestHA_setAndInsertEnvironment(t *testing.T) { + ha := createTestingHA(t, testbackends.RedisTestAddr) + + env := Environment{ + ID: utils.EncodeChecksum(utils.Checksum("herp")), + Name: "herp", + } + + err := ha.setAndInsertEnvironment(&env) + require.NoError(t, err, "setAndInsertEnvironment should not return an error") + + rows, err := ha.super.Dbw.SqlFetchAll( + mysqlTestObserver, + "SELECT name from environment where id = ? LIMIT 1", + ha.super.EnvId, + ) + + require.NoError(t, err, "There was an unexpected SQL error") + assert.Equal(t, 1, len(rows), "There should be a row inserted") + assert.Equal(t, env.Name, rows[0][0], "name must match") +} + func TestHA_runHA(t *testing.T) { ha := createTestingHA(t, testbackends.RedisTestAddr) ha.heartbeatTimer = time.NewTimer(10 * time.Second)