@@ -236,34 +236,44 @@ const (
236236 GtidFormatTagged
237237)
238238
239+ // Decode the number of sids (source identifiers) and if it is using
240+ // tagged GTIDs or classic (non-tagged) GTIDs.
241+ //
242+ // Note that each gtid tag increases the sidno here, so a single UUID
243+ // might turn up multiple times if there are multipl tags.
244+ //
245+ // see also:
246+ // decode_nsids_format in mysql/mysql-server
247+ // https://github.com/mysql/mysql-server/blob/61a3a1d8ef15512396b4c2af46e922a19bf2b174/sql/rpl_gtid_set.cc#L1363-L1378
239248func decodeSid (data []byte ) (format GtidFormat , sidnr uint64 ) {
240- if data [7 ] == 1 {
249+ gtidinfo := make ([]byte , 8 )
250+ copy (gtidinfo , data [:8 ])
251+
252+ if gtidinfo [7 ] == 1 {
241253 format = GtidFormatTagged
242254 }
243255
244256 if format == GtidFormatTagged {
245257 sid_mask := []byte {0 , 255 , 255 , 255 , 255 , 255 , 255 , 0 }
246258
247259 // Apply the mask
248- for i , _ := range data [:8 ] {
249- data [i ] &= sid_mask [i ]
260+ for i , _ := range gtidinfo [:8 ] {
261+ gtidinfo [i ] &= sid_mask [i ]
250262 }
251- data = append (data , 0 )
263+ gtidinfo = append (gtidinfo , 0 )
252264
253265 // sidnr
254- sidnr = binary .LittleEndian .Uint64 (data [1 :])
266+ sidnr = binary .LittleEndian .Uint64 (gtidinfo [1 :])
255267 return
256268 }
257- sidnr = binary .LittleEndian .Uint64 (data )
269+ sidnr = binary .LittleEndian .Uint64 (gtidinfo )
258270 return
259271}
260272
261273func (e * PreviousGTIDsEvent ) Decode (data []byte ) error {
262274 pos := 0
263275
264- gtidinfo := make ([]byte , 8 )
265- copy (gtidinfo , data [:8 ])
266- format , uuidCount := decodeSid (gtidinfo )
276+ format , uuidCount := decodeSid (data )
267277 pos += 8
268278
269279 previousGTIDSets := make ([]string , uuidCount )
@@ -276,7 +286,7 @@ func (e *PreviousGTIDsEvent) Decode(data []byte) error {
276286 if format == GtidFormatTagged {
277287 tagLength := int (data [pos ]) / 2
278288 pos += 1
279- if tagLength > 0 {
289+ if tagLength > 0 { // 0 == no tag, >0 == tag
280290 isTag = true
281291 tag = string (data [pos : pos + tagLength ])
282292 pos += tagLength
0 commit comments