SQL 数据仓库大容量复制期间出现死机



我正在使用go-mssql驱动程序将数据写入Azure SQL Datawarehouse。

在使用批量复制功能写入一些数据时,我随机抛出恐慌(至少我无法可靠地复制此问题(。

错误是

panic: runtime error: slice bounds out of range
goroutine 56 [running]:
github.com/denisenkom/go-mssqldb.(*tdsBuffer).Write(0xc420614800, 0xc420547d40, 0x2, 0x8, 0x0, 0x0, 0x0)
/Users/thihara/workspace/golang/src/favmed.unfuddle.com/src/github.com/denisenkom/go-mssqldb/buf.go:93 +0x194
encoding/binary.Write(0xd2f320, 0xc420614800, 0xd3b500, 0xda4e70, 0x8ea680, 0xa2cfb4, 0x8, 0x0)
/usr/local/go/src/encoding/binary/binary.go:354 +0x188
github.com/denisenkom/go-mssqldb.(*Bulk).Done(0xc420506600, 0xc420691550, 0xda4e70, 0x10)
/Users/thihara/workspace/golang/src/favmed.unfuddle.com/src/github.com/denisenkom/go-mssqldb/bulkcopy.go:219 +0xae
github.com/denisenkom/go-mssqldb.(*copyin).Exec(0xc4204a6bc0, 0xda4e70, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/Users/thihara/workspace/golang/src/favmed.unfuddle.com/src/github.com/denisenkom/go-mssqldb/bulkcopy_sql.go:73 +0x1cb
database/sql.ctxDriverStmtExec(0xd38d60, 0xc42001a070, 0xd38fe0, 0xc4204a6bc0, 0xda4e70, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/usr/local/go/src/database/sql/ctxutil.go:78 +0x176
database/sql.resultFromStatement(0xd38d60, 0xc42001a070, 0xd36720, 0xc4205e95f0, 0xc42099c580, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/usr/local/go/src/database/sql/sql.go:2109 +0x184
database/sql.(*Stmt).ExecContext(0xc4202f87e0, 0xd38d60, 0xc42001a070, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/database/sql/sql.go:2085 +0x223
database/sql.(*Stmt).Exec(0xc4202f87e0, 0x0, 0x0, 0x0, 0xd352e0, 0xc420421180, 0x0, 0x0)
/usr/local/go/src/database/sql/sql.go:2097 +0x65

有人知道发生了什么吗?

我认为这可能与并发性有关,但通过仅运行一个 go 例程(最初是 3 个 go 例程(排除了它。

出于某种原因,这似乎发生在某些批次插入中,而不是全部。

这也与批量大小无关。我有一批 1500 行通过,而一批 1161 行因这种恐慌而失败。

编辑:

我通过将我的插入物手动批处理到 1000 秒来解决这个问题。

但是,这不是一个非常实用的解决方案,因为吞吐量将变得非常有限。

代码 :

func (w Writer) Write(schema string, data []Datum) error {
txn, err := w.connection.Begin()
if err != nil {
return err
}
fullTableName := fmt.Sprintf("[%s].[%s]", schema, TABLE_NAME)
//Create a prepared statement, BulkOptions can be configured to handle the insert limits if necessary.
stmt, err := txn.Prepare(mssql.CopyIn(fullTableName, mssql.BulkOptions{RowsPerBatch: 100000}, "unit_did", "chassis_did",
"gateway_did", "location_code", "data_type", "value", "last_detected_timestamp", "sample_timestamp", "db_timestamp"))
if err != nil {
return err
}
defer stmt.Close()
// Prepare the values to be inserted.
for _, value := range data {
_, err = stmt.Exec(value.UnitDID, value.ChassisDID, value.GatewayDID, value.LocationCode, value.DataType,
value.Value, value.LastDetectedTimestamp, value.SampleTimestamp, value.DbTimestamp)
// Fail the transaction if an error is encountered.
if err != nil {
txn.Rollback()
return err
}
}
// Execute the query against the database and fail the transaction if there's an error.
_, err = stmt.Exec()
if err != nil {
txn.Rollback()
return err
}
return txn.Commit()
}

为此提出了一个 github 问题,测试显示将问题外壳化。

https://github.com/denisenkom/go-mssqldb/issues/370

panic 提供了有用的信息:

panic: runtime error: slice bounds out of range

goroutine 56 [running]:
github.com/denisenkom/go-mssqldb.(*tdsBuffer).Write(0xc420614800, 0xc420547d40, 0x2, 0x8, 0x0, 0x0, 0x0)
/Users/thihara/workspace/golang/src/favmed.unfuddle.com/src/github.com/denisenkom/go-mssqldb/buf.go:93 +0x194

所以,恐慌的台词很可能在这里:

https://github.com/denisenkom/go-mssqldb/blob/master/buf.go#L93

循环遍历缓冲区时可能存在逐个错误,从而导致超出范围错误。

该项目最近已更新。我只想提交一个关于该项目的问题。

最新更新