mirror of
https://github.com/Icinga/icingadb.git
synced 2026-06-08 16:34:29 -04:00
HA: Populate environment table
This commit is contained in:
parent
62aaa8a365
commit
3c3d2230ca
2 changed files with 44 additions and 3 deletions
20
ha/ha.go
20
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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue