Ejemplo n.º 1
0
func (s *session) doCommit() error {
	if s.txn == nil {
		return nil
	}
	defer func() {
		s.txn = nil
		s.sessionVars.SetStatusFlag(mysql.ServerStatusInTrans, false)
	}()
	if binloginfo.PumpClient != nil {
		prewriteValue := binloginfo.GetPrewriteValue(s, false)
		if prewriteValue != nil {
			prewriteData, err := prewriteValue.Marshal()
			if err != nil {
				return errors.Trace(err)
			}
			bin := &binlog.Binlog{
				Tp:            binlog.BinlogType_Prewrite,
				PrewriteValue: prewriteData,
			}
			s.txn.SetOption(kv.BinlogData, bin)
		}
	}
	if err := s.checkSchemaValid(); err != nil {
		err1 := s.txn.Rollback()
		if err1 != nil {
			log.Errorf("rollback txn failed, err:%v", err1)
		}
		return errors.Trace(err)
	}
	if err := s.txn.Commit(); err != nil {
		return errors.Trace(err)
	}
	return nil
}
Ejemplo n.º 2
0
func (t *Table) getMutation(ctx context.Context) *binlog.TableMutation {
	bin := binloginfo.GetPrewriteValue(ctx, true)
	for i := range bin.Mutations {
		if bin.Mutations[i].TableId == t.ID {
			return &bin.Mutations[i]
		}
	}
	idx := len(bin.Mutations)
	bin.Mutations = append(bin.Mutations, binlog.TableMutation{TableId: t.ID})
	return &bin.Mutations[idx]
}
Ejemplo n.º 3
0
func (s *session) finishTxn(rollback bool) error {
	// transaction has already been committed or rolled back
	if s.txn == nil {
		return nil
	}
	defer func() {
		s.ClearValue(executor.DirtyDBKey)
		s.txn = nil
		variable.GetSessionVars(s).SetStatusFlag(mysql.ServerStatusInTrans, false)
		binloginfo.ClearBinlog(s)
	}()

	if rollback {
		s.resetHistory()
		s.cleanRetryInfo()
		return s.txn.Rollback()
	}
	if binloginfo.PumpClient != nil {
		prewriteValue := binloginfo.GetPrewriteValue(s, false)
		if prewriteValue != nil {
			prewriteData, err := prewriteValue.Marshal()
			if err != nil {
				return errors.Trace(err)
			}
			bin := &binlog.Binlog{
				Tp:            binlog.BinlogType_Prewrite,
				PrewriteValue: prewriteData,
			}
			s.txn.SetOption(kv.BinlogData, bin)
		}
	}
	err := s.txn.Commit()
	if err != nil {
		if !variable.GetSessionVars(s).RetryInfo.Retrying && kv.IsRetryableError(err) {
			err = s.Retry()
		}
		if err != nil {
			log.Warnf("txn:%s, %v", s.txn, err)
			return errors.Trace(err)
		}
	}

	s.resetHistory()
	s.cleanRetryInfo()
	return nil
}