Skip to content

Commit

Permalink
Add admin tool to decode any thrift binary into JOSN (#4634)
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng authored Nov 16, 2021
1 parent 5ccad58 commit 9d40c45
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tools/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,20 @@ func newDBCommands() []cli.Command {
AdminDBClean(c)
},
},
{
Name: "decode_thrift",
Usage: "decode thrift object of HEX, print into JSON if the HEX data is matching with any supported struct",
Flags: []cli.Flag{
cli.StringFlag{
Name: FlagInputWithAlias,
EnvVar: "Input",
Usage: "HEX input of the binary data, you may get from database query like SELECT HEX(...) FROM ...",
},
},
Action: func(c *cli.Context) {
AdminDBDataDecodeThrift(c)
},
},
}
}

Expand Down
34 changes: 34 additions & 0 deletions tools/cli/adminDBCleanCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
package cli

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -31,13 +33,45 @@ import (
"github.com/urfave/cli"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/codec"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/reconciliation/entity"
"github.com/uber/cadence/common/reconciliation/invariant"
"github.com/uber/cadence/common/reconciliation/store"
"github.com/uber/cadence/service/worker/scanner/executions"
)

// AdminDBDataDecodeThrift is the command to decode thrift binary into JSON
func AdminDBDataDecodeThrift(c *cli.Context) {
input := getRequiredOption(c, FlagInput)

encoder := codec.NewThriftRWEncoder()
data, err := hex.DecodeString(input)
if err != nil {
ErrorAndExit("input is not a valid hex string", err)
}
found := false
for typeName, t := range decodingTypes {
err = encoder.Decode(data, t)
if err == nil {
// encoding back to confirm
data2, err := encoder.Encode(t)
if err != nil {
ErrorAndExit("cannot encode back to confirm", err)
}
if bytes.Compare(data, data2) == 0 {
fmt.Printf("=======Decode into type %v ========\n", typeName)
fmt.Println(anyToString(t, true, 0))
found = true
}
}
}
if !found {
ErrorAndExit("input data cannot be decoded into any struct", nil)
}

}

// AdminDBClean is the command to clean up unhealthy executions.
// Input is a JSON stream provided via STDIN or a file.
func AdminDBClean(c *cli.Context) {
Expand Down
58 changes: 58 additions & 0 deletions tools/cli/supportedDecodingTypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// The MIT License (MIT)
//
// Copyright (c) 2021 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 cli

import (
"github.com/uber/cadence/.gen/go/config"
"github.com/uber/cadence/.gen/go/history"
"github.com/uber/cadence/.gen/go/replicator"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/.gen/go/sqlblobs"
"github.com/uber/cadence/common/codec"
)

var decodingTypes = map[string]codec.ThriftObject{
"shared.History": &shared.History{},
"shared.HistoryEvent": &shared.HistoryEvent{},
"shared.Memo": &shared.Memo{},
"shared.ResetPoints": &shared.ResetPoints{},
"shared.BadBinaries": &shared.BadBinaries{},
"shared.VersionHistories": &shared.VersionHistories{},
"replicator.FailoverMarkers": &replicator.FailoverMarkers{},
"history.ProcessingQueueStates": &history.ProcessingQueueStates{},
"config.DynamicConfigBlob": &config.DynamicConfigBlob{},
"sqlblobs.ShardInfo": &sqlblobs.ShardInfo{},
"sqlblobs.DomainInfo": &sqlblobs.DomainInfo{},
"sqlblobs.HistoryTreeInfo": &sqlblobs.HistoryTreeInfo{},
"sqlblobs.WorkflowExecutionInfo": &sqlblobs.WorkflowExecutionInfo{},
"sqlblobs.ActivityInfo": &sqlblobs.ActivityInfo{},
"sqlblobs.ChildExecutionInfo": &sqlblobs.ChildExecutionInfo{},
"sqlblobs.SignalInfo": &sqlblobs.SignalInfo{},
"sqlblobs.RequestCancelInfo": &sqlblobs.RequestCancelInfo{},
"sqlblobs.TimerInfo": &sqlblobs.TimerInfo{},
"sqlblobs.TaskInfo": &sqlblobs.TaskInfo{},
"sqlblobs.TaskListInfo": &sqlblobs.TaskListInfo{},
"sqlblobs.TransferTaskInfo": &sqlblobs.TransferTaskInfo{},
"sqlblobs.TimerTaskInfo": &sqlblobs.TimerTaskInfo{},
"sqlblobs.ReplicationTaskInfo": &sqlblobs.ReplicationTaskInfo{},
}

0 comments on commit 9d40c45

Please sign in to comment.