Skip to content

Commit bf3ff58

Browse files
committed
feat: more robust ConvertBase64ToHex and centralize the logic
put in utils and use in reader Signed-off-by: machichima <nary12321@gmail.com>
1 parent dd8a77f commit bf3ff58

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

historyserver/pkg/historyserver/reader.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package historyserver
33
import (
44
"bufio"
55
"context"
6-
"encoding/base64"
76
"encoding/json"
87
"errors"
98
"fmt"
@@ -14,9 +13,7 @@ import (
1413
"sort"
1514
"strings"
1615

17-
"github.com/emicklei/go-restful/v3"
1816
eventtypes "github.com/ray-project/kuberay/historyserver/pkg/eventserver/types"
19-
"github.com/ray-project/kuberay/historyserver/pkg/utils"
2017
"github.com/sirupsen/logrus"
2118

2219
"github.com/ray-project/kuberay/historyserver/pkg/utils"
@@ -44,29 +41,6 @@ func filterAnsiEscapeCodes(content []byte) []byte {
4441
return ansiEscapePattern.ReplaceAll(content, []byte(""))
4542
}
4643

47-
// decodeBase64ToHex converts an ID to hex format.
48-
// Handles both cases:
49-
// 1. Already hex format - returns as-is
50-
// 2. Base64-encoded - decodes to hex
51-
// It tries RawURLEncoding first (Ray's default), falling back to StdEncoding if that fails.
52-
func decodeBase64ToHex(id string) (string, error) {
53-
// Check if already hex (only [0-9a-f])
54-
if matched, _ := regexp.MatchString("^[0-9a-f]+$", id); matched {
55-
return id, nil
56-
}
57-
58-
// Try base64 decode
59-
idBytes, err := base64.RawURLEncoding.DecodeString(id)
60-
if err != nil {
61-
// Try standard Base64 if URL-safe fails
62-
idBytes, err = base64.StdEncoding.DecodeString(id)
63-
if err != nil {
64-
return "", fmt.Errorf("failed to decode Base64 ID: %w", err)
65-
}
66-
}
67-
return fmt.Sprintf("%x", idBytes), nil
68-
}
69-
7044
func (s *ServerHandler) listClusters(limit int) []utils.ClusterInfo {
7145
// Initial continuation marker
7246
logrus.Debugf("Prepare to get list clusters info ...")
@@ -244,7 +218,7 @@ func (s *ServerHandler) resolvePidLogFilename(clusterNameID, sessionID, nodeID s
244218
}
245219

246220
// Convert to hex if not already is
247-
nodeIDHex, err := decodeBase64ToHex(nodeID)
221+
nodeIDHex, err := utils.ConvertBase64ToHex(nodeID)
248222
if err != nil {
249223
return "", "", fmt.Errorf("failed to decode node_id: %w", err)
250224
}
@@ -388,13 +362,13 @@ func (s *ServerHandler) resolveActorLogFilename(clusterNameID, sessionID, actorI
388362
// Returns (nodeIDHex, filename, error).
389363
func (s *ServerHandler) findWorkerLogFile(clusterNameID, sessionID, nodeID, workerID, suffix string) (string, string, error) {
390364
// Convert to hex if not already is
391-
nodeIDHex, err := decodeBase64ToHex(nodeID)
365+
nodeIDHex, err := utils.ConvertBase64ToHex(nodeID)
392366
if err != nil {
393367
return "", "", fmt.Errorf("failed to decode node_id: %w", err)
394368
}
395369

396370
// Convert Base64 worker_id to hex
397-
workerIDHex, err := decodeBase64ToHex(workerID)
371+
workerIDHex, err := utils.ConvertBase64ToHex(workerID)
398372
if err != nil {
399373
return "", "", fmt.Errorf("failed to decode worker_id: %w", err)
400374
}
@@ -500,7 +474,7 @@ func (s *ServerHandler) ipToNodeId(rayClusterNameID, sessionID, nodeIP string) (
500474
}
501475

502476
// Convert to hex if not already is
503-
nodeIDHex, err := decodeBase64ToHex(nodeIDBytes)
477+
nodeIDHex, err := utils.ConvertBase64ToHex(nodeIDBytes)
504478
if err != nil {
505479
logrus.Warnf("Failed to decode node_id %s: %v", nodeIDBytes, err)
506480
continue

historyserver/pkg/utils/utils.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path"
10+
"regexp"
1011
"strings"
1112
"time"
1213

@@ -243,15 +244,27 @@ func GetRayNodeID() (string, error) {
243244
return "", fmt.Errorf("timeout --node_id= not found")
244245
}
245246

246-
func ConvertBase64ToHex(input string) (string, error) {
247-
bytes, err := base64.StdEncoding.DecodeString(input)
248-
if err != nil {
249-
return input, err
247+
// ConvertBase64ToHex converts an ID to hex format.
248+
// Handles both cases:
249+
// 1. Already hex format - returns as-is
250+
// 2. Base64-encoded - decodes to hex
251+
// It tries RawURLEncoding first (Ray's default), falling back to StdEncoding if that fails.
252+
func ConvertBase64ToHex(id string) (string, error) {
253+
// Check if already hex (only [0-9a-f])
254+
if matched, _ := regexp.MatchString("^[0-9a-f]+$", id); matched {
255+
return id, nil
250256
}
251257

252-
hexStr := hex.EncodeToString(bytes)
253-
254-
return hexStr, nil
258+
// Try base64 decode
259+
idBytes, err := base64.RawURLEncoding.DecodeString(id)
260+
if err != nil {
261+
// Try standard Base64 if URL-safe fails
262+
idBytes, err = base64.StdEncoding.DecodeString(id)
263+
if err != nil {
264+
return "", fmt.Errorf("failed to decode Base64 ID: %w", err)
265+
}
266+
}
267+
return fmt.Sprintf("%x", idBytes), nil
255268
}
256269

257270
// BuildClusterSessionKey constructs the key used to identify a specific cluster session.

0 commit comments

Comments
 (0)