diff --git a/go/vt/vttablet/tabletmanager/vreplication/utils.go b/go/vt/vttablet/tabletmanager/vreplication/utils.go index 789c034bfa3..a7af5c483cf 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/utils.go +++ b/go/vt/vttablet/tabletmanager/vreplication/utils.go @@ -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 ( @@ -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 diff --git a/go/vt/vttablet/tabletmanager/vreplication/utils_test.go b/go/vt/vttablet/tabletmanager/vreplication/utils_test.go index cf296d5fb2c..1982edd20d5 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/utils_test.go +++ b/go/vt/vttablet/tabletmanager/vreplication/utils_test.go @@ -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)" @@ -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, }, { @@ -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 @@ -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()