Skip to content

Agent Mesh

fix(policy): add pg_advisory_lock to prevent concurrent db migrations race condition and fix router local port-forwarding addresses

Antonio Ojea8f5bfedb17c5
diff --git a/charts/sam-mesh/templates/router-statefulset.yaml b/charts/sam-mesh/templates/router-statefulset.yaml
index f6f520f0..cef28bf6 100644
--- a/charts/sam-mesh/templates/router-statefulset.yaml
+++ b/charts/sam-mesh/templates/router-statefulset.yaml
@@ -47,7 +47,13 @@ spec:
- "--control-plane=http://{{ include "sam-mesh.fullname" . }}-control-plane:8080"
- "--listen=/ip4/0.0.0.0/tcp/4501"
- "--listen=/ip4/0.0.0.0/udp/4501/quic-v1"
+ {{- if .Values.router.externalAddrs }}
+ {{- range .Values.router.externalAddrs }}
+ - "--external-addr={{ . }}"
+ {{- end }}
+ {{- else }}
- "--external-addr=/dns4/{{ include "sam-mesh.fullname" . }}-router/tcp/4501"
+ {{- end }}
- "--keys-path=/data/router.key"
- "--allow-loopback"
- "--log-level={{ .Values.router.logLevel }}"
diff --git a/development/kind/run.sh b/development/kind/run.sh
index 3237c4ef..d550ad0c 100755
--- a/development/kind/run.sh
+++ b/development/kind/run.sh
@@ -165,6 +165,8 @@ echo "== Deploying SAM Mesh via Helm =="
--set router.service.type=NodePort \
--set router.service.nodePort=30401 \
--set router.useOidcToken=true \
+ --set router.externalAddrs[0]="/dns4/sam-mesh-router/tcp/4501" \
+ --set router.externalAddrs[1]="/ip4/127.0.0.1/tcp/4001" \
--set console.service.type=NodePort \
--set console.service.nodePort=30081 \
--set dex.enabled=true
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index d54bc36f..e446e8a0 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -267,6 +267,16 @@ var migrations = []migration{
}
func (s *SQLStore) initSchema() error {
+ if s.isPostgres() {
+ // Acquire an advisory lock to serialize migrations across concurrent replicas
+ if _, err := s.db.Exec("SELECT pg_advisory_lock(7345892)"); err != nil {
+ return fmt.Errorf("failed to acquire migration advisory lock: %w", err)
+ }
+ defer func() {
+ _, _ = s.db.Exec("SELECT pg_advisory_unlock(7345892)")
+ }()
+ }
+
// Create schema_migrations table
createMigrationsTable := `CREATE TABLE IF NOT EXISTS schema_migrations (version INTEGER PRIMARY KEY)`
if _, err := s.db.Exec(createMigrationsTable); err != nil {