mysql

package
v0.0.0-...-870b839 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 26 Imported by: 1

Documentation

Index

Constants

View Source
const (

	// DefaultWorkerCount is the default number of workers.
	DefaultWorkerCount = 16
	// DefaultMaxTxnRow is the default max number of rows in a transaction.
	DefaultMaxTxnRow = 256

	// BackoffBaseDelay indicates the base delay time for retrying.
	BackoffBaseDelay = 500 * time.Millisecond
	// BackoffMaxDelay indicates the max delay time for retrying.
	BackoffMaxDelay = 60 * time.Second
)

Variables

This section is empty.

Functions

func CheckIfBDRModeIsSupported

func CheckIfBDRModeIsSupported(ctx context.Context, db *sql.DB) (bool, error)

CheckIfBDRModeIsSupported checks if the downstream supports BDR mode.

func CheckIsTiDB

func CheckIsTiDB(ctx context.Context, db *sql.DB) bool

CheckIsTiDB checks if the downstream is TiDB.

func CreateMySQLDBConn

func CreateMySQLDBConn(ctx context.Context, dsnStr string) (*sql.DB, error)

CreateMySQLDBConn establishes a connection to a MySQL database and pings it to verify the connection. It takes a context for managing cancellation and timeouts, and a DSN (Data Source Name) string as input. The function returns a pointer to an sql.DB object, representing the connection, or an error if the connection fails.

func GenBasicDSN

func GenBasicDSN(sinkURI *url.URL, cfg *Config) (*dmysql.Config, error)

GenBasicDSN generates a basic DSN from the given config.

func GenerateDSN

func GenerateDSN(ctx context.Context, sinkURI *url.URL, cfg *Config, dbConnFactory ConnectionFactory) (dsnStr string, err error)

GenerateDSN generates the dsn with the given config. GenerateDSN uses the provided dbConnFactory to create a temporary connection to the downstream database specified by the sinkURI. This temporary connection is used to query important information from the downstream database, such as version, charset, and other relevant details. After the required information is retrieved, the temporary connection is closed. The retrieved data is then used to populate additional parameters into the Sink URI, refining the connection URL (dsnStr).

func GetTestDB

func GetTestDB(ctx context.Context, dbConfig *dmysql.Config, dbConnFactory ConnectionFactory) (*sql.DB, error)

GetTestDB checks and adjusts the password of the given DSN, it will return a DB instance opened with the adjusted password.

func IsSinkSafeMode

func IsSinkSafeMode(sinkURI *url.URL, replicaConfig *config.ReplicaConfig) (bool, error)

IsSinkSafeMode returns whether the sink is in safe mode.

func MockTestDB

func MockTestDB() (*sql.DB, error)

MockTestDB creates a mock mysql database connection.

func QueryMaxAllowedPacket

func QueryMaxAllowedPacket(ctx context.Context, db *sql.DB) (int64, error)

QueryMaxAllowedPacket gets the value of max_allowed_packet

func QueryMaxPreparedStmtCount

func QueryMaxPreparedStmtCount(ctx context.Context, db *sql.DB) (int, error)

QueryMaxPreparedStmtCount gets the value of max_prepared_stmt_count

func SetWriteSource

func SetWriteSource(ctx context.Context, cfg *Config, txn *sql.Tx) error

SetWriteSource sets write source for the transaction.

Types

type Config

type Config struct {
	WorkerCount            int
	MaxTxnRow              int
	MaxMultiUpdateRowCount int
	MaxMultiUpdateRowSize  int

	ReadTimeout    string
	WriteTimeout   string
	DialTimeout    string
	SafeMode       bool
	Timezone       string
	TLS            string
	ForceReplicate bool

	IsTiDB bool // IsTiDB is true if the downstream is TiDB
	// IsBDRModeSupported is true if the downstream is TiDB and write source is existed.
	// write source exists when the downstream is TiDB and version is greater than or equal to v6.5.0.
	IsWriteSourceExisted bool
	HasVectorType        bool // HasVectorType is true if the column is vector type

	SourceID        uint64
	BatchDMLEnable  bool
	MultiStmtEnable bool
	CachePrepStmts  bool
	// contains filtered or unexported fields
}

Config is the configs for MySQL backend.

func NewConfig

func NewConfig() *Config

NewConfig returns the default mysql backend config.

func (*Config) Apply

func (c *Config) Apply(
	serverTimezone string,
	changefeedID model.ChangeFeedID,
	sinkURI *url.URL,
	replicaConfig *config.ReplicaConfig,
) (err error)

Apply applies the sink URI parameters to the config.

type ConnectionFactory

type ConnectionFactory func(ctx context.Context, dsnStr string) (*sql.DB, error)

ConnectionFactory is a function type that takes a context and a DSN (Data Source Name) string as input parameters, and returns a pointer to an sql.DB object and an error. This function type is typically used to create and configure database connections.

type DBConnectionFactory

type DBConnectionFactory struct{}

DBConnectionFactory is an implementation of the IDBConnectionFactory interface, designed for use in normal system operations where only a single method of creating database connections is required.

In regular workflows, both the temporary connection (used for querying information like version and charset) and the standard connection (used for subsequent operations) can be created in the same manner. Therefore, DBConnectionFactory provides a unified approach by implementing the IDBConnectionFactory interface and using the same CreateMySQLDBConn function for both CreateTemporaryConnection and CreateStandardConnection.

This struct simplifies the process for scenarios where unit testing is not a concern. As long as you are not writing unit tests and do not need to mock different types of connections, using DBConnectionFactory is perfectly suitable for establishing the necessary database connections.

func (*DBConnectionFactory) CreateStandardConnection

func (d *DBConnectionFactory) CreateStandardConnection(ctx context.Context, dsnStr string) (*sql.DB, error)

CreateStandardConnection creates the standard database connection that will be used by the system for ongoing operations. This connection is based on the refined Sink URI containing the necessary parameters gathered from the temporary connection.

func (*DBConnectionFactory) CreateTemporaryConnection

func (d *DBConnectionFactory) CreateTemporaryConnection(ctx context.Context, dsnStr string) (*sql.DB, error)

CreateTemporaryConnection creates a temporary database connection used to query essential information from the downstream database, such as version and charset. This connection is intended to be short-lived and will be closed after the necessary information is retrieved.

type DBConnectionFactoryForTest

type DBConnectionFactoryForTest struct {
	// contains filtered or unexported fields
}

DBConnectionFactoryForTest is a utility implementation of the IDBConnectionFactory interface designed to simplify the process of writing unit tests that require different methods for creating database connections.

Instead of implementing the IDBConnectionFactory interface from scratch in every unit test, DBConnectionFactoryForTest allows developers to easily set up custom connection creation methods. The SetTemporaryConnectionFactory and SetStandardConnectionFactory methods allow you to define how the temporary and standard connections should be created during testing.

Once these methods are set, the system will automatically invoke CreateTemporaryConnection and CreateStandardConnection to establish the respective connections during the connection creation process. This approach provides flexibility and convenience in unit testing scenarios, where different connection behaviors need to be mocked or tested separately.

func NewDBConnectionFactoryForTest

func NewDBConnectionFactoryForTest() *DBConnectionFactoryForTest

NewDBConnectionFactoryForTest creates a new instance of DBConnectionFactoryForTest with a predefined temporary connection creation method. This method is initialized to use a mock database connection, which is commonly required in the current unit tests. By setting up the temporary connection creation logic directly in this constructor, it eliminates the need to repeatedly call SetTemporaryConnectionFactory to set up the temporary connection factory in each test. This streamlines the setup process for unit tests that require a mock temporary connection.

func (*DBConnectionFactoryForTest) CreateStandardConnection

func (d *DBConnectionFactoryForTest) CreateStandardConnection(ctx context.Context, dsnStr string) (*sql.DB, error)

CreateStandardConnection creates the standard database connection during testing using the connection factory set by SetStandardConnectionFactory. If no factory has been set, it returns an error. This method supports custom connection behavior during the standard connection phase in unit tests.

func (*DBConnectionFactoryForTest) CreateTemporaryConnection

func (d *DBConnectionFactoryForTest) CreateTemporaryConnection(ctx context.Context, dsnStr string) (*sql.DB, error)

CreateTemporaryConnection creates a temporary database connection during testing using the connection factory set by SetTemporaryConnectionFactory. If no factory has been set, it returns an error. This method allows for customized connection logic during the temporary connection phase in unit tests.

func (*DBConnectionFactoryForTest) SetStandardConnectionFactory

func (d *DBConnectionFactoryForTest) SetStandardConnectionFactory(f ConnectionFactory)

SetStandardConnectionFactory sets the connection factory that will be used to create the standard connection during testing. This provides the ability to mock the connection behavior specifically for the standard connection phase.

func (*DBConnectionFactoryForTest) SetTemporaryConnectionFactory

func (d *DBConnectionFactoryForTest) SetTemporaryConnectionFactory(f ConnectionFactory)

SetTemporaryConnectionFactory sets the connection factory that will be used to create the temporary connection during testing. This allows for custom behavior during unit tests when different connection logic is required.

type IDBConnectionFactory

type IDBConnectionFactory interface {
	CreateTemporaryConnection(ctx context.Context, dsnStr string) (*sql.DB, error)
	CreateStandardConnection(ctx context.Context, dsnStr string) (*sql.DB, error)
}

IDBConnectionFactory is an interface designed specifically to facilitate unit testing in scenarios where connections to downstream databases are required.

In the process of creating a downstream database connection based on a Sink URI, a temporary connection is first established. This temporary connection is used to query information from the downstream database, such as version, charset, etc. After retrieving this information, the temporary connection is closed. The retrieved data is then used to populate additional parameters into the Sink URI, and a more refined Sink URI is used to establish the final, standard connection that the Sink will use for subsequent operations.

During normal system operation, it's perfectly acceptable to create both of these connections in the same manner. However, in the context of unit testing, where it's not feasible to start an actual downstream database, we need to mock these connections. Since both connections will send SQL requests, the unit tests require mocking two different connections to handle each phase of the connection creation process.

This interface addresses this issue by providing two distinct methods: CreateTemporaryConnection, for creating the temporary connection, and CreateStandardConnection, for creating the standard, persistent connection. By using these separate methods, the interface allows for greater flexibility in mocking and testing, ensuring that the two connections can be created differently as needed during unit tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址