Skip to content

Commit

Permalink
Use a new const for the max length
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Feb 27, 2024
1 parent c0012c5 commit 91a733e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
17 changes: 9 additions & 8 deletions go/vt/vttablet/tabletmanager/vreplication/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (

const (
vreplicationLogTableName = "vreplication_log"
// This comes from the fact that the message column in the vreplication_log table is of type TEXT.
// See: go/vt/sidecardb/schema/vreplication/vreplication_log.sql
// https://dev.mysql.com/doc/refman/en/string-type-syntax.html and
// https://dev.mysql.com/doc/refman/en/storage-requirements.html#data-types-storage-reqs-strings
maxVReplicationLogMessageLen = 65535
)

const (
Expand Down Expand Up @@ -99,16 +104,12 @@ func insertLog(dbClient *vdbClient, typ string, vreplID int32, state, message st
query = fmt.Sprintf("update %s.vreplication_log set count = count + 1 where id = %d", sidecar.GetIdentifier(), id)
} else {
buf := sqlparser.NewTrackedBuffer(nil)
// The message column is a TEXT field and thus has a max length of 64KiB (2^16-1) so we truncate that if needed.
// See: https://dev.mysql.com/doc/refman/en/string-type-syntax.html and
// https://dev.mysql.com/doc/refman/en/storage-requirements.html#data-types-storage-reqs-strings
// We perform the truncation 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.
maxMessageLen := 65535
// 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.
truncationStr := fmt.Sprintf(" ... %s ... ", sqlparser.TruncationText)
if len(message) > maxMessageLen {
if len(message) > maxVReplicationLogMessageLen {
mid := (len(message) / 2) - len(truncationStr)
for mid > (maxMessageLen / 2) {
for mid > (maxVReplicationLogMessageLen / 2) {
mid = mid / 2
}
tail := (len(message) - (mid + len(truncationStr))) + 1
Expand Down
9 changes: 4 additions & 5 deletions go/vt/vttablet/tabletmanager/vreplication/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestInsertLogTruncation(t *testing.T) {
vrID := int32(1)
typ := "Testing"
state := binlogdatapb.VReplicationWorkflowState_Error.String()
maxMessageLen := 65535
truncationStr := fmt.Sprintf(" ... %s ... ", sqlparser.TruncationText)

insertStmtf := "insert into _vt.vreplication_log(vrepl_id, type, state, message) values(%d, '%s', '%s', %s)"
Expand All @@ -61,10 +60,10 @@ func TestInsertLogTruncation(t *testing.T) {
message: "Simple message that doesn't need to be truncated " + strings.Repeat("b", 64000) + " cuz it's not quite too long",
},
{
message: "Message that is just barely short enough " + strings.Repeat("c", maxMessageLen-(len("Message that is just barely short enough ")+len(" so it doesn't get truncated"))) + " so it doesn't get truncated",
message: "Message that is just barely short enough " + strings.Repeat("c", maxVReplicationLogMessageLen-(len("Message that is just barely short enough ")+len(" so it doesn't get truncated"))) + " so it doesn't get truncated",
},
{
message: "Message that is just barely too long " + strings.Repeat("d", maxMessageLen-(len("Message that is just barely too long ")+len(" so it gets truncated"))+1) + " so it gets truncated",
message: "Message that is just barely too long " + strings.Repeat("d", maxVReplicationLogMessageLen-(len("Message that is just barely too long ")+len(" so it gets truncated"))+1) + " so it gets truncated",
expectTruncation: true,
},
{
Expand All @@ -81,7 +80,7 @@ func TestInsertLogTruncation(t *testing.T) {
var messageOut string
if tc.expectTruncation {
mid := (len(tc.message) / 2) - len(truncationStr)
for mid > (maxMessageLen / 2) {
for mid > (maxVReplicationLogMessageLen / 2) {
mid = mid / 2
}
tail := (len(tc.message) - (mid + len(truncationStr))) + 1
Expand All @@ -92,7 +91,7 @@ func TestInsertLogTruncation(t *testing.T) {
} else {
messageOut = tc.message
}
require.LessOrEqual(t, len(messageOut), 65535)
require.LessOrEqual(t, len(messageOut), maxVReplicationLogMessageLen)
dbClient.ExpectRequest(fmt.Sprintf(insertStmtf, vrID, typ, state, encodeString(messageOut)), &sqltypes.Result{}, nil)
insertLog(vdbClient, typ, vrID, state, tc.message)
dbClient.Wait()
Expand Down

0 comments on commit 91a733e

Please sign in to comment.