Skip to content

Commit 26f4454

Browse files
authored
Fixes statement name generation for CP Flink (#3242)
1 parent 1fcf15f commit 26f4454

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

internal/flink/command_statement_create_onprem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *command) newStatementCreateCommandOnPrem() *cobra.Command {
4949

5050
func (c *command) statementCreateOnPrem(cmd *cobra.Command, args []string) error {
5151
// Flink statement name can be automatically generated or provided by the user
52-
name := types.GenerateStatementName()
52+
name := types.GenerateStatementNameForOnPrem()
5353
if len(args) == 1 {
5454
name = args[0]
5555
}

pkg/flink/internal/store/store_onprem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *StoreOnPrem) ProcessStatement(statement string) (*types.ProcessedStatem
6060
return result, sErr
6161
}
6262

63-
statementName := s.Properties.GetOrDefault(config.KeyStatementName, types.GenerateStatementName())
63+
statementName := s.Properties.GetOrDefault(config.KeyStatementName, types.GenerateStatementNameForOnPrem())
6464
if len(statementName) > 45 { // on-prem name length limit
6565
statementName = statementName[0:45]
6666
}

pkg/flink/internal/store/store_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func (s *StoreTestSuite) TestGenerateStatementName() {
5555
}
5656
}
5757

58+
func (s *StoreTestSuite) TestGenerateStatementNameForOnPrem() {
59+
statementRegex := `^cli-\d{8}-\d{6}-[a-f0-9]{24}$`
60+
for i := 0; i < 10; i++ {
61+
s.Require().Regexp(statementRegex, types.GenerateStatementNameForOnPrem())
62+
}
63+
}
64+
5865
func TestStoreProcessLocalStatement(t *testing.T) {
5966
// Create new stores
6067
stores := make([]types.StoreInterface, 2)

pkg/flink/types/statement.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package types
22

33
import (
4+
"crypto/rand"
5+
"encoding/hex"
46
"fmt"
57
"time"
68

@@ -14,3 +16,17 @@ func GenerateStatementName() string {
1416
id := uuid.New().String()
1517
return fmt.Sprintf("%s-%s-%s-%s", clientName, date, localTime, id)
1618
}
19+
20+
func GenerateStatementNameForOnPrem() string {
21+
clientName := "cli"
22+
timeNow := time.Now()
23+
date := timeNow.Format("20060102") // 8 chars
24+
localTime := timeNow.Format("150405") // 6 chars
25+
// 12 random bytes => 24 hex chars
26+
b := make([]byte, 12)
27+
if _, err := rand.Read(b); err != nil {
28+
panic(fmt.Sprintf("unable to generate random bytes for statement name: %v", err))
29+
}
30+
randomHex := hex.EncodeToString(b)
31+
return fmt.Sprintf("%s-%s-%s-%s", clientName, date, localTime, randomHex)
32+
}

0 commit comments

Comments
 (0)