Skip to content

Commit

Permalink
Unify truncation location and indicator btwn vreplication & binlogplayer
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Feb 28, 2024
1 parent 021f060 commit 793c7a1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
5 changes: 5 additions & 0 deletions go/vt/binlog/binlogplayer/binlog_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/textutil"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
Expand All @@ -66,6 +67,10 @@ var (
BlplTransaction = "Transaction"
// BlplBatchTransaction is the key for the stats map.
BlplBatchTransaction = "BatchTransaction"

// Truncate values in the middle to preserve the end of the message which
// typically contains the error text.
TruncationLocation = textutil.TruncationLocationMiddle
)

var TruncationIndicator = fmt.Sprintf(" ... %s ... ", sqlparser.TruncationText)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/binlog/binlogplayer/dbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func LogError(msg string, err error) {

// LimitString truncates string to specified size
func LimitString(s string, limit int) string {
ts, err := textutil.TruncateText(s, limit, textutil.TruncationLocationMiddle, "...")
ts, err := textutil.TruncateText(s, limit, TruncationLocation, TruncationIndicator)
if err != nil { // Fallback to simple truncation
if len(s) <= limit {
return s
Expand Down
5 changes: 1 addition & 4 deletions go/vt/vttablet/tabletmanager/vreplication/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ import (

const (
vreplicationLogTableName = "vreplication_log"
// Truncate values in the middle to preserve the end of the message which typically contains the
// error text.
truncationLocation = textutil.TruncationLocationMiddle
// This comes from the fact that the message column in the vreplication_log table is of type TEXT.
maxVReplicationLogMessageLen = 65535
)
Expand Down Expand Up @@ -109,7 +106,7 @@ func insertLog(dbClient *vdbClient, typ string, vreplID int32, state, message st
// We perform the truncation, if needed, in the middle of the message as the end of the message is likely to
// be the most important part as it often explains WHY we e.g. failed to execute an INSERT in the workflow.
if len(message) > maxVReplicationLogMessageLen {
message, err = textutil.TruncateText(message, maxVReplicationLogMessageLen, truncationLocation, binlogplayer.TruncationIndicator)
message, err = textutil.TruncateText(message, maxVReplicationLogMessageLen, binlogplayer.TruncationLocation, binlogplayer.TruncationIndicator)
if err != nil {
log.Errorf("Could not insert vreplication_log record because we failed to truncate the message: %v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vttablet/tabletmanager/vreplication/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestInsertLogTruncation(t *testing.T) {
err error
)
if tc.expectTruncation {
messageOut, err = textutil.TruncateText(tc.message, maxVReplicationLogMessageLen, truncationLocation, binlogplayer.TruncationIndicator)
messageOut, err = textutil.TruncateText(tc.message, maxVReplicationLogMessageLen, binlogplayer.TruncationLocation, binlogplayer.TruncationIndicator)
require.NoError(t, err)
require.True(t, strings.HasPrefix(messageOut, tc.message[:1024])) // Confirm we still have the same beginning
require.True(t, strings.HasSuffix(messageOut, tc.message[len(tc.message)-1024:])) // Confirm we still have the same end
Expand Down

0 comments on commit 793c7a1

Please sign in to comment.