Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash slotname, if it exceeds 63 characters in streams #2808

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/cluster/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -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_%x", constants.EventStreamSourceSlotPrefix, sha1.Sum([]byte(name)))
}
return name
}

func (c *Cluster) getStreamConnection(database, user, appId string) zalandov1.Connection {
Expand Down
24 changes: 24 additions & 0 deletions pkg/cluster/streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}