migration to replicaset connection
Some checks failed
ci/woodpecker/push/ledger Pipeline is pending
ci/woodpecker/push/nats Pipeline is pending
ci/woodpecker/push/notification Pipeline is pending
ci/woodpecker/push/payments_orchestrator Pipeline is pending
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline failed
ci/woodpecker/push/bump_version unknown status
ci/woodpecker/push/frontend Pipeline failed

This commit is contained in:
Stephan D
2025-11-24 19:10:07 +01:00
parent cd79355e69
commit 72271cfc9a
11 changed files with 204 additions and 36 deletions

View File

@@ -2,7 +2,10 @@ package mongo
import ( import (
"context" "context"
"fmt"
"net"
"os" "os"
"strings"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/tech/sendico/pkg/auth" "github.com/tech/sendico/pkg/auth"
@@ -28,33 +31,40 @@ import (
"github.com/tech/sendico/pkg/mservice" "github.com/tech/sendico/pkg/mservice"
mutil "github.com/tech/sendico/pkg/mutil/config" mutil "github.com/tech/sendico/pkg/mutil/config"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref" "go.mongodb.org/mongo-driver/mongo/readpref"
"go.uber.org/zap" "go.uber.org/zap"
) )
// Config represents configuration // Config represents configuration
type Config struct { type Config struct {
Port *string `mapstructure:"port"` Port *string `mapstructure:"port"`
PortEnv *string `mapstructure:"port_env"` PortEnv *string `mapstructure:"port_env"`
User *string `mapstructure:"user"` User *string `mapstructure:"user"`
UserEnv *string `mapstructure:"user_env"` UserEnv *string `mapstructure:"user_env"`
PasswordEnv string `mapstructure:"password_env"` PasswordEnv string `mapstructure:"password_env"`
Database *string `mapstructure:"database"` Database *string `mapstructure:"database"`
DatabaseEnv *string `mapstructure:"database_env"` DatabaseEnv *string `mapstructure:"database_env"`
Host *string `mapstructure:"host"` Host *string `mapstructure:"host"`
HostEnv *string `mapstructure:"host_env"` HostEnv *string `mapstructure:"host_env"`
AuthSource *string `mapstructure:"auth_source,omitempty"` Hosts []string `mapstructure:"hosts,omitempty"`
AuthSourceEnv *string `mapstructure:"auth_source_env,omitempty"` HostsEnvPrefix *string `mapstructure:"hosts_env_prefix,omitempty"`
AuthMechanism *string `mapstructure:"auth_mechanism,omitempty"` HostsEnvPrefixEnv *string `mapstructure:"hosts_env_prefix_env,omitempty"`
AuthMechanismEnv *string `mapstructure:"auth_mechanism_env,omitempty"` PortsEnvPrefix *string `mapstructure:"ports_env_prefix,omitempty"`
ReplicaSet *string `mapstructure:"replica_set,omitempty"` PortsEnvPrefixEnv *string `mapstructure:"ports_env_prefix_env,omitempty"`
ReplicaSetEnv *string `mapstructure:"replica_set_env,omitempty"` URI *string `mapstructure:"uri,omitempty"`
Enforcer *auth.Config `mapstructure:"enforcer"` URIEnv *string `mapstructure:"uri_env,omitempty"`
AuthSource *string `mapstructure:"auth_source,omitempty"`
AuthSourceEnv *string `mapstructure:"auth_source_env,omitempty"`
AuthMechanism *string `mapstructure:"auth_mechanism,omitempty"`
AuthMechanismEnv *string `mapstructure:"auth_mechanism_env,omitempty"`
ReplicaSet *string `mapstructure:"replica_set,omitempty"`
ReplicaSetEnv *string `mapstructure:"replica_set_env,omitempty"`
Enforcer *auth.Config `mapstructure:"enforcer"`
} }
type DBSettings struct { type DBSettings struct {
Host string Host string
Hosts []string
Port string Port string
User string User string
Password string Password string
@@ -62,6 +72,7 @@ type DBSettings struct {
AuthSource string AuthSource string
AuthMechanism string AuthMechanism string
ReplicaSet string ReplicaSet string
URI string
} }
func newProtectedDB[T any]( func newProtectedDB[T any](
@@ -87,6 +98,19 @@ func Config2DBSettings(logger mlogger.Logger, config *Config) *DBSettings {
p.AuthSource = mutil.GetConfigValue(logger, "auth_source", "auth_source_env", config.AuthSource, config.AuthSourceEnv) p.AuthSource = mutil.GetConfigValue(logger, "auth_source", "auth_source_env", config.AuthSource, config.AuthSourceEnv)
p.AuthMechanism = mutil.GetConfigValue(logger, "auth_mechanism", "auth_mechanism_env", config.AuthMechanism, config.AuthMechanismEnv) p.AuthMechanism = mutil.GetConfigValue(logger, "auth_mechanism", "auth_mechanism_env", config.AuthMechanism, config.AuthMechanismEnv)
p.ReplicaSet = mutil.GetConfigValue(logger, "replica_set", "replica_set_env", config.ReplicaSet, config.ReplicaSetEnv) p.ReplicaSet = mutil.GetConfigValue(logger, "replica_set", "replica_set_env", config.ReplicaSet, config.ReplicaSetEnv)
p.URI = mutil.GetConfigValue(logger, "uri", "uri_env", config.URI, config.URIEnv)
hostPrefix := mutil.GetConfigValue(logger, "hosts_env_prefix", "hosts_env_prefix_env", config.HostsEnvPrefix, config.HostsEnvPrefixEnv)
portPrefix := mutil.GetConfigValue(logger, "ports_env_prefix", "ports_env_prefix_env", config.PortsEnvPrefix, config.PortsEnvPrefixEnv)
if hostPrefix == "" && p.ReplicaSet != "" {
hostPrefix = "MONGO_HOSTS_"
}
if portPrefix == "" && p.ReplicaSet != "" {
portPrefix = "MONGO_PORTS_"
}
p.Hosts = collectReplicaHosts(config.Hosts, p.ReplicaSet, p.Port, hostPrefix, portPrefix)
return p return p
} }
@@ -101,21 +125,19 @@ func decodeConfig(logger mlogger.Logger, settings model.SettingsT) (*Config, *DB
} }
func dialMongo(logger mlogger.Logger, dbSettings *DBSettings) (*mongo.Client, error) { func dialMongo(logger mlogger.Logger, dbSettings *DBSettings) (*mongo.Client, error) {
cred := options.Credential{ opts := buildOptions(dbSettings)
AuthMechanism: dbSettings.AuthMechanism,
AuthSource: dbSettings.AuthSource,
Username: dbSettings.User,
Password: dbSettings.Password,
}
dbURI := buildURI(dbSettings)
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(dbURI).SetAuth(cred)) client, err := mongo.Connect(context.Background(), opts)
if err != nil { if err != nil {
logger.Error("Unable to connect to database", zap.Error(err)) logger.Error("Unable to connect to database", zap.Error(err))
return nil, err return nil, err
} }
logger.Info("Connected successfully", zap.String("uri", dbURI)) if dbSettings.URI != "" {
logger.Info("Connected successfully", zap.Bool("uri_provided", true))
} else {
logger.Info("Connected successfully", zap.Strings("hosts", opts.Hosts), zap.String("replica_set", dbSettings.ReplicaSet))
}
if err := client.Ping(context.Background(), readpref.Primary()); err != nil { if err := client.Ping(context.Background(), readpref.Primary()); err != nil {
logger.Error("Unable to ping database", zap.Error(err)) logger.Error("Unable to ping database", zap.Error(err))
@@ -199,6 +221,70 @@ func (db *DB) TransactionFactory() transaction.Factory {
return transactionimp.CreateFactory(db.client) return transactionimp.CreateFactory(db.client)
} }
func collectReplicaHosts(configuredHosts []string, replicaSet, defaultPort, hostPrefix, portPrefix string) []string {
normalize := func(host, port string) string {
host = strings.TrimSpace(host)
port = strings.TrimSpace(port)
if host == "" {
return ""
}
// If host already has a port, keep it; otherwise apply provided/default port.
if _, _, err := net.SplitHostPort(host); err == nil {
return host
}
if port != "" {
return net.JoinHostPort(host, port)
}
if defaultPort != "" {
return net.JoinHostPort(host, defaultPort)
}
return host
}
appendHost := func(list []string, host, port string) []string {
if normalized := normalize(host, port); normalized != "" {
return append(list, normalized)
}
return list
}
var hosts []string
for _, h := range configuredHosts {
hosts = appendHost(hosts, h, "")
}
if replicaSet == "" || hostPrefix == "" {
return hosts
}
index := 0
for {
hostEnv := os.Getenv(fmt.Sprintf("%s%d", hostPrefix, index))
portEnv := ""
if portPrefix != "" {
portEnv = os.Getenv(fmt.Sprintf("%s%d", portPrefix, index))
}
if hostEnv == "" && index == 0 {
hostEnv = os.Getenv(fmt.Sprintf("%s%d", hostPrefix, 1))
if portPrefix != "" {
portEnv = os.Getenv(fmt.Sprintf("%s%d", portPrefix, 1))
}
if hostEnv == "" {
break
}
index = 1
} else if hostEnv == "" {
break
}
hosts = appendHost(hosts, hostEnv, portEnv)
index++
}
return hosts
}
func (db *DB) Permissions() auth.Provider { func (db *DB) Permissions() auth.Provider {
return db return db
} }

View File

@@ -1,22 +1,49 @@
package mongo package mongo
import ( import (
"net/url" "net"
"strings"
"go.mongodb.org/mongo-driver/mongo/options"
) )
func buildURI(s *DBSettings) string { func buildOptions(s *DBSettings) *options.ClientOptions {
u := &url.URL{ opts := options.Client()
Scheme: "mongodb",
Host: s.Host, if s.URI != "" {
Path: "/" + url.PathEscape(s.Database), // /my%20db return opts.ApplyURI(s.URI)
}
hosts := make([]string, 0, len(s.Hosts)+1)
for _, h := range s.Hosts {
if trimmed := strings.TrimSpace(h); trimmed != "" {
hosts = append(hosts, trimmed)
}
}
if len(hosts) == 0 && s.Host != "" {
host := s.Host
if _, _, err := net.SplitHostPort(host); err != nil && s.Port != "" {
host = net.JoinHostPort(host, s.Port)
}
hosts = append(hosts, host)
}
if len(hosts) > 0 {
opts.SetHosts(hosts)
} }
q := url.Values{}
if s.ReplicaSet != "" { if s.ReplicaSet != "" {
q.Set("replicaSet", s.ReplicaSet) opts.SetReplicaSet(s.ReplicaSet)
} }
u.RawQuery = q.Encode() cred := options.Credential{
AuthMechanism: s.AuthMechanism,
AuthSource: s.AuthSource,
Username: s.User,
Password: s.Password,
}
opts.SetAuth(cred)
return u.String() return opts
} }

View File

@@ -8,6 +8,13 @@ MONGO_REPLICA_SET=sendico-rs
MONGO_AUTH_SOURCE=admin MONGO_AUTH_SOURCE=admin
MONGO_DATABASE=sendico MONGO_DATABASE=sendico
MONGO_ARCH=linux/arm64 MONGO_ARCH=linux/arm64
MONGO_HOSTS_0=sendico_db1
MONGO_PORTS_0=27017
MONGO_HOSTS_1=sendico_db2
MONGO_PORTS_1=27017
MONGO_HOSTS_2=sendico_db3
MONGO_PORTS_2=27017
PERMISSION_MODEL=/app/env/permissions_model.conf PERMISSION_MODEL=/app/env/permissions_model.conf
PERMISSION_COLLECTION=permissions PERMISSION_COLLECTION=permissions
PERMISSION_TIMEOUT=5 PERMISSION_TIMEOUT=5

View File

@@ -37,6 +37,12 @@ services:
MONGO_PASSWORD: ${MONGO_PASSWORD} MONGO_PASSWORD: ${MONGO_PASSWORD}
MONGO_AUTH_SOURCE: ${MONGO_AUTH_SOURCE} MONGO_AUTH_SOURCE: ${MONGO_AUTH_SOURCE}
MONGO_REPLICA_SET: ${MONGO_REPLICA_SET} MONGO_REPLICA_SET: ${MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
PERMISSION_MODEL: ${PERMISSION_MODEL} PERMISSION_MODEL: ${PERMISSION_MODEL}
PERMISSION_COLLECTION: ${PERMISSION_COLLECTION} PERMISSION_COLLECTION: ${PERMISSION_COLLECTION}
PERMISSION_TIMEOUT: ${PERMISSION_TIMEOUT} PERMISSION_TIMEOUT: ${PERMISSION_TIMEOUT}

View File

@@ -25,6 +25,12 @@ services:
FEES_MONGO_PASSWORD: ${FEES_MONGO_PASSWORD} FEES_MONGO_PASSWORD: ${FEES_MONGO_PASSWORD}
FEES_MONGO_AUTH_SOURCE: ${FEES_MONGO_AUTH_SOURCE} FEES_MONGO_AUTH_SOURCE: ${FEES_MONGO_AUTH_SOURCE}
FEES_MONGO_REPLICA_SET: ${FEES_MONGO_REPLICA_SET} FEES_MONGO_REPLICA_SET: ${FEES_MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
FEES_GRPC_PORT: ${FEES_GRPC_PORT} FEES_GRPC_PORT: ${FEES_GRPC_PORT}
FEES_METRICS_PORT: ${FEES_METRICS_PORT} FEES_METRICS_PORT: ${FEES_METRICS_PORT}
NATS_URL: ${NATS_URL} NATS_URL: ${NATS_URL}

View File

@@ -33,6 +33,12 @@ services:
CHAIN_GATEWAY_MONGO_PASSWORD: ${CHAIN_GATEWAY_MONGO_PASSWORD} CHAIN_GATEWAY_MONGO_PASSWORD: ${CHAIN_GATEWAY_MONGO_PASSWORD}
CHAIN_GATEWAY_MONGO_AUTH_SOURCE: ${CHAIN_GATEWAY_MONGO_AUTH_SOURCE} CHAIN_GATEWAY_MONGO_AUTH_SOURCE: ${CHAIN_GATEWAY_MONGO_AUTH_SOURCE}
CHAIN_GATEWAY_MONGO_REPLICA_SET: ${CHAIN_GATEWAY_MONGO_REPLICA_SET} CHAIN_GATEWAY_MONGO_REPLICA_SET: ${CHAIN_GATEWAY_MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
NATS_URL: ${NATS_URL} NATS_URL: ${NATS_URL}
NATS_HOST: ${NATS_HOST} NATS_HOST: ${NATS_HOST}
NATS_PORT: ${NATS_PORT} NATS_PORT: ${NATS_PORT}

View File

@@ -25,6 +25,12 @@ services:
FX_MONGO_PASSWORD: ${FX_MONGO_PASSWORD} FX_MONGO_PASSWORD: ${FX_MONGO_PASSWORD}
FX_MONGO_AUTH_SOURCE: ${FX_MONGO_AUTH_SOURCE} FX_MONGO_AUTH_SOURCE: ${FX_MONGO_AUTH_SOURCE}
FX_MONGO_REPLICA_SET: ${FX_MONGO_REPLICA_SET} FX_MONGO_REPLICA_SET: ${FX_MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
FX_INGESTOR_METRICS_PORT: ${FX_INGESTOR_METRICS_PORT} FX_INGESTOR_METRICS_PORT: ${FX_INGESTOR_METRICS_PORT}
command: ["--config.file", "/app/config.yml"] command: ["--config.file", "/app/config.yml"]
ports: ports:

View File

@@ -25,6 +25,12 @@ services:
FX_MONGO_PASSWORD: ${FX_MONGO_PASSWORD} FX_MONGO_PASSWORD: ${FX_MONGO_PASSWORD}
FX_MONGO_AUTH_SOURCE: ${FX_MONGO_AUTH_SOURCE} FX_MONGO_AUTH_SOURCE: ${FX_MONGO_AUTH_SOURCE}
FX_MONGO_REPLICA_SET: ${FX_MONGO_REPLICA_SET} FX_MONGO_REPLICA_SET: ${FX_MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
FX_ORACLE_GRPC_PORT: ${FX_ORACLE_GRPC_PORT} FX_ORACLE_GRPC_PORT: ${FX_ORACLE_GRPC_PORT}
FX_ORACLE_METRICS_PORT: ${FX_ORACLE_METRICS_PORT} FX_ORACLE_METRICS_PORT: ${FX_ORACLE_METRICS_PORT}
NATS_URL: ${FX_NATS_URL} NATS_URL: ${FX_NATS_URL}

View File

@@ -25,6 +25,12 @@ services:
LEDGER_MONGO_PASSWORD: ${LEDGER_MONGO_PASSWORD} LEDGER_MONGO_PASSWORD: ${LEDGER_MONGO_PASSWORD}
LEDGER_MONGO_AUTH_SOURCE: ${LEDGER_MONGO_AUTH_SOURCE} LEDGER_MONGO_AUTH_SOURCE: ${LEDGER_MONGO_AUTH_SOURCE}
LEDGER_MONGO_REPLICA_SET: ${LEDGER_MONGO_REPLICA_SET} LEDGER_MONGO_REPLICA_SET: ${LEDGER_MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
LEDGER_GRPC_PORT: ${LEDGER_GRPC_PORT} LEDGER_GRPC_PORT: ${LEDGER_GRPC_PORT}
LEDGER_METRICS_PORT: ${LEDGER_METRICS_PORT} LEDGER_METRICS_PORT: ${LEDGER_METRICS_PORT}
NATS_URL: ${NATS_URL} NATS_URL: ${NATS_URL}

View File

@@ -41,6 +41,12 @@ services:
MONGO_PASSWORD: ${MONGO_PASSWORD} MONGO_PASSWORD: ${MONGO_PASSWORD}
MONGO_AUTH_SOURCE: ${MONGO_AUTH_SOURCE} MONGO_AUTH_SOURCE: ${MONGO_AUTH_SOURCE}
MONGO_REPLICA_SET: ${MONGO_REPLICA_SET} MONGO_REPLICA_SET: ${MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
PERMISSION_MODEL: ${PERMISSION_MODEL} PERMISSION_MODEL: ${PERMISSION_MODEL}
PERMISSION_COLLECTION: ${PERMISSION_COLLECTION} PERMISSION_COLLECTION: ${PERMISSION_COLLECTION}
PERMISSION_TIMEOUT: ${PERMISSION_TIMEOUT} PERMISSION_TIMEOUT: ${PERMISSION_TIMEOUT}

View File

@@ -25,6 +25,12 @@ services:
PAYMENTS_MONGO_PASSWORD: ${PAYMENTS_MONGO_PASSWORD} PAYMENTS_MONGO_PASSWORD: ${PAYMENTS_MONGO_PASSWORD}
PAYMENTS_MONGO_AUTH_SOURCE: ${PAYMENTS_MONGO_AUTH_SOURCE} PAYMENTS_MONGO_AUTH_SOURCE: ${PAYMENTS_MONGO_AUTH_SOURCE}
PAYMENTS_MONGO_REPLICA_SET: ${PAYMENTS_MONGO_REPLICA_SET} PAYMENTS_MONGO_REPLICA_SET: ${PAYMENTS_MONGO_REPLICA_SET}
MONGO_HOSTS_0: ${MONGO_HOSTS_0}
MONGO_PORTS_0: ${MONGO_PORTS_0}
MONGO_HOSTS_1: ${MONGO_HOSTS_1}
MONGO_PORTS_1: ${MONGO_PORTS_1}
MONGO_HOSTS_2: ${MONGO_HOSTS_2}
MONGO_PORTS_2: ${MONGO_PORTS_2}
NATS_URL: ${NATS_URL} NATS_URL: ${NATS_URL}
NATS_HOST: ${NATS_HOST} NATS_HOST: ${NATS_HOST}
NATS_PORT: ${NATS_PORT} NATS_PORT: ${NATS_PORT}