Skip to content

Commit

Permalink
Minor changes from self review
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 4296f8a commit 1f80864
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
9 changes: 6 additions & 3 deletions go/textutil/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,17 @@ func Title(s string) string {
s)
}

// TruncateText truncates the provided text to the specified length using the
// provided indicator in place of the truncated text in the specified location
// of the original.
func TruncateText(text string, maxLen int, location TruncationLocation, truncationIndicator string) (string, error) {
if len(truncationIndicator)+2 >= maxLen {
return "", fmt.Errorf("the truncation indicator is too long for the provided text")
}
ol := len(text)
if ol <= maxLen {
return text, nil
}
if len(truncationIndicator)+2 >= maxLen {
return "", fmt.Errorf("the truncation indicator is too long for the provided text")
}
switch location {
case TruncationLocationMiddle:
prefix := int((float64(maxLen) * 0.5) - float64(len(truncationIndicator)))
Expand Down
1 change: 1 addition & 0 deletions go/textutil/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func TestTruncateText(t *testing.T) {
} else {
require.NoError(t, err)
require.Equal(t, tt.want, val)
require.LessOrEqual(t, len(val), tt.maxLen)
}
})
}
Expand Down
7 changes: 5 additions & 2 deletions go/vt/vttablet/tabletmanager/vreplication/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ 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
// This comes from the fact that the message column in the vreplication table is varbinary(1000).
maxVReplicationMessageLen = 1000
)

// vrepliationLogTruncationStr is the string that is used to indicate that a message has been
// truncated, in the middle, before being inserted into the vreplication_log table.
// truncated before being inserted into one of the vreplication sidecar tables.
var vreplicationLogTruncationStr = fmt.Sprintf(" ... %s ... ", sqlparser.TruncationText)

const (
Expand Down Expand Up @@ -111,7 +114,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, textutil.TruncationLocationMiddle, vreplicationLogTruncationStr)
message, err = textutil.TruncateText(message, maxVReplicationLogMessageLen, truncationLocation, vreplicationLogTruncationStr)
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, textutil.TruncationLocationMiddle, vreplicationLogTruncationStr)
messageOut, err = textutil.TruncateText(tc.message, maxVReplicationLogMessageLen, truncationLocation, vreplicationLogTruncationStr)
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
2 changes: 1 addition & 1 deletion go/vt/vttablet/tabletmanager/vreplication/vreplicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func (vr *vreplicator) readSettings(ctx context.Context, dbClient *vdbClient) (s
}

func (vr *vreplicator) setMessage(message string) (err error) {
message, err = textutil.TruncateText(message, maxVReplicationMessageLen, textutil.TruncationLocationMiddle, vreplicationLogTruncationStr)
message, err = textutil.TruncateText(message, maxVReplicationMessageLen, truncationLocation, vreplicationLogTruncationStr)
if err != nil {
return err
}
Expand Down

0 comments on commit 1f80864

Please sign in to comment.