登录
首页 >  Golang >  Go问答

数据丢失问题的goroutine

来源:stackoverflow

时间:2024-03-06 11:12:28 323浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《数据丢失问题的goroutine》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

我正在编写一个 aws lambda 代码来查询 rds 表、将其转换为 json 并返回它。但除了 sql 查询返回的记录之外,我没有看到 json 中的所有记录。假设我从表中查询 1500 条记录,但每次 json 中只有 1496 到 1500 条记录(少 0-5 条记录)。我怀疑我把 sync.waitgroup 弄乱了。

下面是 sql server 查询

select top 1500 * from imbookingapp.dbo.ba_contact__c
where contactid > 0

下面是我的代码

// convert the rows object to slice of objects for every row
func parserow(rows *sql.rows, totalcolumns int) []string {

    receiver := make([]string, totalcolumns)

    is := make([]interface{}, len(receiver))
    for i := range is {
        is[i] = &receiver[i]
    }

    err := rows.scan(is...)

    if err != nil {
        fmt.println("error reading rows: " + err.error())
    }

    totalrecordsinparserowfunction++
    return receiver
}

// query the given table and return json response
func querytable(conn *sql.db, query string) (string, error) {

    // query table
    rows, err := conn.query(query)
    if err != nil {
        fmt.println("database error:", err)
        return "", errors.new("database error:" + err.error())
    }

    println("rows:", rows)
    defer rows.close()

    // get the column names
    columns, err := rows.columns()
    // fmt.println("columns", columns)
    if err != nil {
        fmt.println("database error:", err)
        return "", errors.new("database error:" + err.error())
    }

    totalcolumns := len(columns)
    var resp []map[string]string // declare the type of final response which will be used to create json
    var waitgroup sync.waitgroup

    // iterate over all the rows returned
    for rows.next() {

        waitgroup.add(1)
        totalrecordscount++
        row := parserow(rows, totalcolumns)

        go func() {
            // create a map of the row
            resprow := map[string]string{} // declare the type of each row of response
            for count := range row {
                resprow[columns[count]] = row[count]
            }
            // fmt.println("\n\nresprow", resprow)

            resp = append(resp, resprow)
            totalrecordsappendedcount++
            waitgroup.done()
        }()
    }
    waitgroup.wait()

    // if no rows are returned
    if len(resp) == 0 {
        fmt.println("message: no records are available")
        return "", errors.new("message: no records are available")
    }

    // create json
    respjson, _ := json.marshal(resp)
    fmt.println("response", string(respjson))

    fmt.println("\n--------------summary---------------")
    fmt.println("totalrecordsinparserowfunction", totalrecordsinparserowfunction)
    fmt.println("totalrecordscount", totalrecordscount)
    fmt.println("totalrecordsappendedcount", totalrecordsappendedcount)
    fmt.println("object length", len(resp))

    return string(respjson), nil // return json

}

下面是我得到的输出摘要

--------------Summary---------------
TotalRecordsInParseRowfunction 1500
TotalRecordsCount 1500
TotalRecordsAppendedCount 1500
Object Length 1496

解决方案


你的代码很活泼。多个 goroutine 在没有任何互斥的情况下写入 resp,因此您会丢失数据。

您可以在其周围添加互斥锁-解锁。然而,goroutine 中的代码并不保证有自己的 goroutine,因为它只是一个简单的映射添加。在 goroutine 中处理该代码会容易得多,并且可能会运行得更快,而无需 goroutine 调度开销。除非您计划在该 goroutine 中添加更多逻辑,否则我建议您将其删除。

这里有一些关于可能发生的情况的更多信息:首先,在当前版本的 go 中,只有当该 goroutine 调用某些库函数时,该 goroutine 才会屈服于其他 goroutine。查看代码,您的 goroutine 不太可能会屈服。由于您已经观察到数据丢失(这意味着存在竞争条件),因此您可能拥有多个内核。

比赛在这里:

resp = append(resp, respRow)

如果没有互斥,一个 goroutine 可能会查看 resp,发现它可以写入其 nth 元素。另一个 goroutine(在单独的核心上运行)可以执行相同的操作,并成功写入。但第一个 goroutine 仍然认为该元素为空,因此覆盖它,并更新 resp。发生这种情况时,您会丢失一个元素。

如果你在这段代码中添加互斥,你实际上会强制所有 goroutine 顺序运行,因为它们实际上并没有做任何其他事情。另外,由于 goroutine 执行顺序是随机的,因此您最终会得到随机排序的 resp。简而言之,这是您应该顺序执行代码的实例之一。

终于介绍完啦!小伙伴们,这篇关于《数据丢失问题的goroutine》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>