Skip to content

Commit

Permalink
Track type for id column for messages
Browse files Browse the repository at this point in the history
This tracks the explicit type for the id column so that we can use that
properly when binding variables. If we don't bind the right type, we end
up depending on implicit MySQL type casting behavior which isn't
something we should do.

Making it explicit here is much more robust.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Oct 21, 2024
1 parent 5276ec4 commit 6602de0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions go/vt/vttablet/tabletserver/messager/message_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ type messageManager struct {
ackQuery *sqlparser.ParsedQuery
postponeQuery *sqlparser.ParsedQuery
purgeQuery *sqlparser.ParsedQuery

// idType is the type of the id column in the message table.
idType sqltypes.Type
}

// newMessageManager creates a new message manager.
Expand All @@ -259,6 +262,7 @@ func newMessageManager(tsv TabletService, vs VStreamer, table *schema.Table, pos
purgeTicks: timer.NewTimer(table.MessageInfo.PollInterval),
postponeSema: postponeSema,
messagesPending: true,
idType: table.MessageInfo.IDType,
}
mm.cond.L = &mm.mu

Expand Down Expand Up @@ -856,7 +860,7 @@ func (mm *messageManager) GenerateAckQuery(ids []string) (string, map[string]*qu
}
for _, id := range ids {
idbvs.Values = append(idbvs.Values, &querypb.Value{
Type: querypb.Type_VARBINARY,
Type: mm.idType,
Value: []byte(id),
})
}
Expand All @@ -874,7 +878,7 @@ func (mm *messageManager) GeneratePostponeQuery(ids []string) (string, map[strin
}
for _, id := range ids {
idbvs.Values = append(idbvs.Values, &querypb.Value{
Type: querypb.Type_VARBINARY,
Type: mm.idType,
Value: []byte(id),
})
}
Expand Down
8 changes: 8 additions & 0 deletions go/vt/vttablet/tabletserver/schema/load_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ func loadMessageInfo(ta *Table, comment string, collationEnv *collations.Environ
ta.MessageInfo.Fields = getDefaultMessageFields(ta.Fields, hiddenCols)
}

ta.MessageInfo.IDType = sqltypes.VarBinary
for _, field := range ta.MessageInfo.Fields {
if field.Name == "id" {
ta.MessageInfo.IDType = field.Type
break
}
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions go/vt/vttablet/tabletserver/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"
"time"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/log"

"vitess.io/vitess/go/vt/sqlparser"
Expand Down Expand Up @@ -125,6 +126,9 @@ type MessageInfo struct {
// MaxBackoff specifies the longest duration message manager
// should wait before rescheduling a message
MaxBackoff time.Duration

// IDType specifies the type of the ID column
IDType sqltypes.Type
}

// NewTable creates a new Table.
Expand Down

0 comments on commit 6602de0

Please sign in to comment.