gpt4 book ai didi

mongodb - 使用 mgo.Monotonic 从中学读取

转载 作者:IT老高 更新时间:2023-10-28 12:29:48 25 4
gpt4 key购买 nike

我正在尝试配置从 mongo 副本集的主节点和两个辅助节点读取以提供更好的负载平衡。 3 个节点中的每一个都位于具有 IP 地址的不同机器上:ip1、ip2、ip3。

我的 GoLang 站点,即 martini 网络服务器,有两个 url /insert/get:

package main

import (
"github.com/go-martini/martini"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"net/http"
)

const (
dialStr = "ip1:port1,ip2:port2,ip3:port3"
dbName = "test"
collectionName = "test"
elementsCount = 1000
)

var mainSessionForSave *mgo.Session

func ConnectToMongo() {
var err error
mainSessionForSave, err = mgo.Dial(dialStr)
mainSessionForSave.SetMode(mgo.Monotonic, true)
if err != nil {
panic(err)
}
}

func GetMgoSessionPerRequest() *mgo.Session {
var sessionPerRequest *mgo.Session
sessionPerRequest = mainSessionForSave.Copy()
return sessionPerRequest
}

func main() {
ConnectToMongo()
prepareMartini().Run()
}

type Element struct {
I int `bson:"I"`
}

func prepareMartini() *martini.ClassicMartini {
m := martini.Classic()
sessionPerRequest := GetMgoSessionPerRequest()
m.Get("/insert", func(w http.ResponseWriter, r *http.Request) {
for i := 0; i < elementsCount; i++ {
e := Element{I: i}
err := collection(sessionPerRequest).Insert(&e)
if err != nil {
panic(err)
}
}
w.Write([]byte("data inserted successfully"))
})
m.Get("/get", func(w http.ResponseWriter, r *http.Request) {
var element Element
const findI = 500
err := collection(sessionPerRequest).Find(bson.M{"I": findI}).One(&element)
if err != nil {
panic(err)
}
w.Write([]byte("get data successfully"))

})

return m
}

func collection(s *mgo.Session) *mgo.Collection {
return s.DB(dbName).C(collectionName)
}

我使用命令 go run site.go 运行这个 GoLang 站点并准备​​我请求的实验 http://localhost:3000/insert - 大约一分钟后我的测试数据被插入。

然后我开始测试从辅助节点和主节点读取在 attacker.go:

package main

import (
"fmt"
"time"

vegeta "github.com/tsenart/vegeta/lib"
)

func main() {

rate := uint64(4000) // per second
duration := 4 * time.Second
targeter := vegeta.NewStaticTargeter(&vegeta.Target{
Method: "GET",
URL: "http://localhost:3000/get",
})
attacker := vegeta.NewAttacker()

var results vegeta.Results
for res := range attacker.Attack(targeter, rate, duration) {
results = append(results, res)
}

metrics := vegeta.NewMetrics(results)
fmt.Printf("99th percentile: %s\n", metrics.Latencies.P99)
}

运行它go runattacker.go 我只是每秒请求 URL http://localhost:3000/get 4000 次。当攻击者工作时,我打开了我所有的 3 台服务器并运行 htop 命令来观察资源消耗。 PRIMARY 节点显示它处于高负载状态,CPU 大约为 80%。中学很平静。

为什么?

当我使用 mgo.Monotonic ...

mainSessionForSave.SetMode(mgo.Monotonic, true)

... 我希望从所有节点读取:ip1, ip2, ip3 并且我希望在相同负载和相同 CPU 消耗下观察所有节点。但事实并非如此。我配置错了什么?事实上 mgo.Monotonic 在我的情况下不起作用,我只从 PRIMARY 节点读取。

最佳答案

sessionPerRequest 只创建一次:prepareMartini 在服务器启动时调用,然后设置 sessionPerRequest。传递给 m.Get() 的闭包访问该变量。然后,在第一次写入之后(在您的测试设置期间),mgo will only access the primary :

Monotonic consistency will start reading from a slave if possible, so that the load is better distributed, and once the first write happens the connection is switched to the master.

(如果 mgo 在写入主节点后只是继续从辅助节点读取,则读取不一定反射(reflect)您刚刚进行的写入,这可能会很痛苦。切换到主节点应该只为您提供更新的数据,而不是您从辅助服务器获得的数据,从不旧,这保持了单调性。无论如何,理想情况下它应该是这样工作的;有关更多信息,请参阅下面的“未解决问题”链接。)

解决方案是将创建 session 推送到您的处理程序中,例如,删除 sessionPerRequest 并在每个处理程序顶部放置一些明确的内容,例如

coll := mainSessionForSave.Copy().DB(dbName).Collection(collName)

应根据 open issues with MongoDB consistency 阅读所有一致性 promise :现在,在网络分区期间,读取可以看到旧数据和稍后将回滚的写入,即使 mgo 正在尝试从主数据库读取。 (比较和设置没有这个问题,但当然这是一个更大更慢的操作。)同样值得仔细阅读这篇文章只是为了讨论一致性级别和描述不同的数据库行为可能如何体现在应用程序的最终用户。

关于mongodb - 使用 mgo.Monotonic 从中学读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com