mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
cockroachdb: add high-availability support (#12965)
This commit adds high-availability support to the CockroachDB backend. The locking strategy implemented is heavily influenced from the very similar Postgres backend.
This commit is contained in:
@@ -18,7 +18,8 @@ import (
|
||||
|
||||
type Config struct {
|
||||
docker.ServiceURL
|
||||
TableName string
|
||||
TableName string
|
||||
HATableName string
|
||||
}
|
||||
|
||||
var _ docker.ServiceConfig = &Config{}
|
||||
@@ -29,11 +30,11 @@ func prepareCockroachDBTestContainer(t *testing.T) (func(), *Config) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tableName := os.Getenv("CR_TABLE")
|
||||
if tableName == "" {
|
||||
tableName = defaultTableName
|
||||
return func() {}, &Config{
|
||||
ServiceURL: *s,
|
||||
TableName: "vault." + defaultTableName,
|
||||
HATableName: "vault." + defaultHATableName,
|
||||
}
|
||||
return func() {}, &Config{*s, "vault." + tableName}
|
||||
}
|
||||
|
||||
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
||||
@@ -74,14 +75,10 @@ func connectCockroachDB(ctx context.Context, host string, port int) (docker.Serv
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tableName := os.Getenv("CR_TABLE")
|
||||
if tableName == "" {
|
||||
tableName = defaultTableName
|
||||
}
|
||||
|
||||
return &Config{
|
||||
ServiceURL: *docker.NewServiceURL(u),
|
||||
TableName: database + "." + tableName,
|
||||
ServiceURL: *docker.NewServiceURL(u),
|
||||
TableName: database + "." + defaultTableName,
|
||||
HATableName: database + "." + defaultHATableName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -89,26 +86,56 @@ func TestCockroachDBBackend(t *testing.T) {
|
||||
cleanup, config := prepareCockroachDBTestContainer(t)
|
||||
defer cleanup()
|
||||
|
||||
hae := os.Getenv("CR_HA_ENABLED")
|
||||
if hae == "" {
|
||||
hae = "true"
|
||||
}
|
||||
|
||||
// Run vault tests
|
||||
logger := logging.NewVaultLogger(log.Debug)
|
||||
|
||||
b, err := NewCockroachDBBackend(map[string]string{
|
||||
b1, err := NewCockroachDBBackend(map[string]string{
|
||||
"connection_url": config.URL().String(),
|
||||
"table": config.TableName,
|
||||
"ha_table": config.HATableName,
|
||||
"ha_enabled": hae,
|
||||
}, logger)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create new backend: %v", err)
|
||||
}
|
||||
|
||||
b2, err := NewCockroachDBBackend(map[string]string{
|
||||
"connection_url": config.URL().String(),
|
||||
"table": config.TableName,
|
||||
"ha_table": config.HATableName,
|
||||
"ha_enabled": hae,
|
||||
}, logger)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create new backend: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
truncate(t, b)
|
||||
truncate(t, b1)
|
||||
truncate(t, b2)
|
||||
}()
|
||||
|
||||
physical.ExerciseBackend(t, b)
|
||||
truncate(t, b)
|
||||
physical.ExerciseBackend_ListPrefix(t, b)
|
||||
truncate(t, b)
|
||||
physical.ExerciseTransactionalBackend(t, b)
|
||||
physical.ExerciseBackend(t, b1)
|
||||
truncate(t, b1)
|
||||
physical.ExerciseBackend_ListPrefix(t, b1)
|
||||
truncate(t, b1)
|
||||
physical.ExerciseTransactionalBackend(t, b1)
|
||||
truncate(t, b1)
|
||||
|
||||
ha1, ok1 := b1.(physical.HABackend)
|
||||
ha2, ok2 := b2.(physical.HABackend)
|
||||
if !ok1 || !ok2 {
|
||||
t.Fatalf("CockroachDB does not implement HABackend")
|
||||
}
|
||||
|
||||
if ha1.HAEnabled() && ha2.HAEnabled() {
|
||||
logger.Info("Running ha backend tests")
|
||||
physical.ExerciseHABackend(t, ha1, ha2)
|
||||
}
|
||||
}
|
||||
|
||||
func truncate(t *testing.T, b physical.Backend) {
|
||||
@@ -117,6 +144,12 @@ func truncate(t *testing.T, b physical.Backend) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to drop table: %v", err)
|
||||
}
|
||||
if crdb.haEnabled {
|
||||
_, err = crdb.client.Exec("TRUNCATE TABLE " + crdb.haTable)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to drop table: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDBTable(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user