From 8b03db1c9c036f8bc9760cd02beab41d088f79f2 Mon Sep 17 00:00:00 2001 From: Johannes Knauft Date: Wed, 27 Nov 2024 02:36:17 +0100 Subject: [PATCH 1/2] Hash slotname, if it exceeds 63 characters in streams --- pkg/cluster/streams.go | 7 ++++++- pkg/cluster/streams_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pkg/cluster/streams.go b/pkg/cluster/streams.go index 3d9cbae11..1a58daaf9 100644 --- a/pkg/cluster/streams.go +++ b/pkg/cluster/streams.go @@ -2,6 +2,7 @@ package cluster import ( "context" + "crypto/sha1" "encoding/json" "fmt" "reflect" @@ -288,7 +289,11 @@ func getOutboxTable(tableName string, idColumn *string) zalandov1.EventStreamTab } func getSlotName(dbName, appId string) string { - return fmt.Sprintf("%s_%s_%s", constants.EventStreamSourceSlotPrefix, dbName, strings.Replace(appId, "-", "_", -1)) + name := fmt.Sprintf("%s_%s_%s", constants.EventStreamSourceSlotPrefix, dbName, strings.Replace(appId, "-", "_", -1)) + if len(name) > 63 { + name = fmt.Sprintf("%s_%s", constants.EventStreamSourceSlotPrefix, sha1.Sum([]byte(name))) + } + return name } func (c *Cluster) getStreamConnection(database, user, appId string) zalandov1.Connection { diff --git a/pkg/cluster/streams_test.go b/pkg/cluster/streams_test.go index 92d28663e..4edbf558e 100644 --- a/pkg/cluster/streams_test.go +++ b/pkg/cluster/streams_test.go @@ -752,3 +752,27 @@ func patchPostgresqlStreams(t *testing.T, cluster *Cluster, pgSpec *acidv1.Postg return streams } + +func TestSlotNameWithinMaxLength(t *testing.T) { + dbName := "testdb" + appId := "test-app" + expected := constants.EventStreamSourceSlotPrefix + "_testdb_test_app" + result := getSlotName(dbName, appId) + assert.Equal(t, expected, result) +} + +func TestSlotNameExceedsMaxLength(t *testing.T) { + dbName := "testdb" + appId := "this-is-a-very-long-application-id-that-will-exceed-the-maximum-length" + expected := constants.EventStreamSourceSlotPrefix + "_5a300d179c894b672b35bac212eab875d4c4145a" + result := getSlotName(dbName, appId) + assert.Equal(t, expected, result) +} + +func TestSlotNameWithHyphens(t *testing.T) { + dbName := "testdb" + appId := "test-app-with-hyphens" + expected := constants.EventStreamSourceSlotPrefix + "_testdb_test_app_with_hyphens" + result := getSlotName(dbName, appId) + assert.Equal(t, expected, result) +} From 6c1f75e7c11102a4e00a6a70e9f3f1a601bf5a11 Mon Sep 17 00:00:00 2001 From: Johannes Knauft Date: Wed, 27 Nov 2024 02:53:58 +0100 Subject: [PATCH 2/2] Fix test --- pkg/cluster/streams.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cluster/streams.go b/pkg/cluster/streams.go index 1a58daaf9..398e020d7 100644 --- a/pkg/cluster/streams.go +++ b/pkg/cluster/streams.go @@ -291,7 +291,7 @@ func getOutboxTable(tableName string, idColumn *string) zalandov1.EventStreamTab func getSlotName(dbName, appId string) string { name := fmt.Sprintf("%s_%s_%s", constants.EventStreamSourceSlotPrefix, dbName, strings.Replace(appId, "-", "_", -1)) if len(name) > 63 { - name = fmt.Sprintf("%s_%s", constants.EventStreamSourceSlotPrefix, sha1.Sum([]byte(name))) + name = fmt.Sprintf("%s_%x", constants.EventStreamSourceSlotPrefix, sha1.Sum([]byte(name))) } return name }