首页 > 解决方案 > sql 数据仓库批量复制期间的恐慌

问题描述

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

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

错误是

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

标签: azuregosql-data-warehouse

解决方案


The panic provides useful information:

panic: runtime error: slice bounds out of range

and

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

So, the panicking line may well be here:

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

There might be an off-by-one bug in looping through the buffer, causing the out of range error.

The project has been updated recently. I would just submit an issue on the project.


推荐阅读