Repository: pingcap/tidb
File: pkg/resourcegroup/runaway/record.go (Go)

Code before:
		return
	}
	var leftRows [][]types.Datum
	for {
		sql := ""
		if sql, err = generator.NextSQL(leftRows, batchSize); err != nil {
			logutil.BgLogger().Error("delete system table failed", zap.String("table", tableName), zap.Error(err))
			return
		}
		// to remove
		if len(sql) == 0 {
			return
		}
		rows, sqlErr := ExecRCRestrictedSQL(rm.sysSessionPool, sql, nil)
		if sqlErr != nil {
			logutil.BgLogger().Error("delete system table failed", zap.String("table", tableName), zap.Error(err))
			return
		}
		if len(rows) == 0 {
			return
		}
		logutil.BgLogger().Info("start to delete the expired rows",
			zap.Int("rows", len(rows)),
			zap.Int("batch-size", batchSize),
			zap.Int("delete-size", deleteSize),
		)
		leftRows = make([][]types.Datum, len(rows))
		for i, row := range rows {
			leftRows[i] = row.GetDatumRow(tb.KeyColumnTypes)
		}
		for startIndex := 0; startIndex < len(leftRows); startIndex += deleteSize {
			endIndex := startIndex + deleteSize
			if endIndex > len(leftRows) {
				endIndex = len(leftRows)
			}
			delBatch := leftRows[startIndex:endIndex]
			sql, err := sqlbuilder.BuildDeleteSQL(tb, delBatch, expiredTime)
			if err != nil {
				logutil.BgLogger().Error(
					"build delete SQL failed when deleting system table",
					zap.Error(err),
					zap.String("table", tb.Schema.O+"."+tb.Name.O),
				)
				return
			}

			_, err = ExecRCRestrictedSQL(rm.sysSessionPool, sql, nil)
			if err != nil {
				logutil.BgLogger().Error(
					"delete SQL failed when deleting system table", zap.Error(err), zap.String("SQL", sql),
				)

Review comment (refactor):
Should also clean up the global variable `hasDeletedExpiredRows`

Code after:
			return
		}
		// to remove
		if len(sql) == 0 {
			return
		}
		rows, sqlErr := ExecRCRestrictedSQL(rm.sysSessionPool, sql, nil)
		if sqlErr != nil {
			logutil.BgLogger().Error("delete system table failed", zap.String("table", tableName), zap.Error(err))
			return
		}
		if len(rows) == 0 {
			return
		}
		logutil.BgLogger().Info("start to delete the expired rows",
			zap.Int("rows", len(rows)),
			zap.Int("batch-size", batchSize),
			zap.Int("delete-size", deleteSize),
		)
		leftRows = make([][]types.Datum, len(rows))
		for i, row := range rows {
			leftRows[i] = row.GetDatumRow(tb.KeyColumnTypes)
		}
		for startIndex := 0; startIndex < len(leftRows); startIndex += deleteSize {
			endIndex := startIndex + deleteSize
			if endIndex > len(leftRows) {
				endIndex = len(leftRows)
			}
			delBatch := leftRows[startIndex:endIndex]
			sql, err := sqlbuilder.BuildDeleteSQL(tb, delBatch, expiredTime)
			if err != nil {
				logutil.BgLogger().Error(
					"build delete SQL failed when deleting system table",
					zap.Error(err),
					zap.String("table", tb.Schema.O+"."+tb.Name.O),
				)
				return
			}

			_, err = ExecRCRestrictedSQL(rm.sysSessionPool, sql, nil)
			if err != nil {
				logutil.BgLogger().Error(
					"delete SQL failed when deleting system table", zap.Error(err), zap.String("SQL", sql),
				)
			}
		}
		logutil.BgLogger().Info("deleted expired rows",
			zap.Int("rows", len(rows)),
			zap.Int("batch-size", batchSize),
			zap.Int("delete-size", deleteSize),
		)