Files
tasks/tools/tdbg/decode_commands.go
Alex ShtinandGitHub 917cd4e446 tdbg: fallback to encode history to text if JSON encoding failed (#6591)
## What changed?
<!-- Describe what has changed in this PR -->
`tdbg`: fallback to encode history to text if JSON encoding failed.

## Why?
<!-- Tell your future self why have you made these changes -->
Under some weird circumstances JSON encoding might not work but it is
still useful to see as much data as we can. Text marshaling seems always
work.

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
Run `tdbg` for problematic blobs with wrong durations.

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
No risks.

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->
No.

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
No.
2024-10-02 15:13:24 -07:00

143 lines
4.1 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tdbg
import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
"go.temporal.io/server/common/codec"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func AdminDecodeProto(c *cli.Context) error {
protoType := c.String(FlagProtoType)
var protoData []byte
var err error
binaryFile := c.String(FlagBinaryFile)
if binaryFile != "" {
protoData, err = os.ReadFile(binaryFile)
if err != nil {
return fmt.Errorf("unable to read binary file %s: %s", binaryFile, err)
}
}
if protoData == nil {
hexData := c.String(FlagHexData)
hexFile := c.String(FlagHexFile)
if hexData == "" && hexFile != "" {
hexBytes, err := os.ReadFile(hexFile)
if err != nil {
return fmt.Errorf("unable to read hex file %s: %s", hexFile, err)
}
hexData = string(hexBytes)
}
hexData = strings.TrimPrefix(hexData, "0x")
if hexData != "" {
protoData, err = hex.DecodeString(hexData)
if err != nil {
cutLen := 10
dots := "..."
if len(hexData) <= cutLen {
cutLen = len(hexData)
dots = ""
}
return fmt.Errorf("unable to decode hex data %s%s: %s", hexData[:cutLen], dots, err)
}
}
}
if protoData == nil {
return fmt.Errorf("missing required parameter data flag")
}
messageType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(protoType))
if err != nil {
return fmt.Errorf("unable to find %s type: %w", protoType, err)
}
message := messageType.New().Interface()
if err = proto.Unmarshal(protoData, message); err != nil {
return fmt.Errorf("unable to unmarshal to %s", protoType)
}
encoder := codec.NewJSONPBIndentEncoder(" ")
json, err := encoder.Encode(message)
if err != nil {
err := fmt.Errorf("unable to encode to JSON: %s", err)
text, terr := prototext.Marshal(message)
if terr != nil {
return err
}
fmt.Fprintln(c.App.Writer, err)
fmt.Fprintln(c.App.Writer, "marshal to text:")
json = text
}
fmt.Fprintln(c.App.Writer, string(json))
return nil
}
func AdminDecodeBase64(c *cli.Context) error {
base64Data := c.String(FlagBase64Data)
base64File := c.String(FlagBase64File)
if base64Data == "" && base64File != "" {
base64Bytes, err := os.ReadFile(base64File)
if err != nil {
return fmt.Errorf("unable to read base64 file %s: %s", base64File, err)
}
base64Data = string(base64Bytes)
}
if base64Data == "" {
return fmt.Errorf("no data flag is specified")
}
data, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
cutLen := 10
dots := "..."
if len(base64Data) <= cutLen {
cutLen = len(base64Data)
dots = ""
}
return fmt.Errorf("unable to decode base64 data %s%s: %s", base64Data[:cutLen], dots, err)
}
fmt.Fprintln(c.App.Writer)
fmt.Fprintln(c.App.Writer, string(data))
return nil
}