Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

fixbug: server.cleanThread无sleep导致mongo server负载过大 #15

Merged
merged 1 commit into from
Apr 11, 2018
Merged

fixbug: server.cleanThread无sleep导致mongo server负载过大 #15

merged 1 commit into from
Apr 11, 2018

Conversation

chuanbowen
Copy link

No description provided.

@ihacku
Copy link
Member

ihacku commented Apr 10, 2018

我补充一下背景
“mongo进程被kill N小时之后重启,发现server进程和mongo进程打满了CPU。”

@neargle
Copy link
Member

neargle commented Apr 10, 2018

感谢pr,可是我没搞懂为啥要在这里加。
这里ip数过多还会 sleep 一下, mongodb连不上的情况不应该会报 error 吗?

@ihacku ihacku requested a review from ywolf April 11, 2018 02:30
@chuanbowen
Copy link
Author

@neargle 感谢回复,一下是我对代码的理解:
cleanThread中,代码逻辑:
for {
查询离线72小时机器;
if 离线机器数 > 100:
sleep 1 minute
}

如果没有离线机器,那就是会持续query mongodb。

image

@neargle
Copy link
Member

neargle commented Apr 11, 2018

写了个脚本验证了一下:

package main

import (
	"log"
	"time"

	mgo "gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

var DB, _ = conn("127.0.0.1:27017", "agent")

func cleanThread() {
	client := DB.C("client")
	for {
		var offlineIPList []string
		log.Println(bson.M{"uptime": bson.M{"$lte": time.Now().Add(time.Hour * time.Duration(-72))}})
		err := client.Find(
			bson.M{"uptime": bson.M{"$lte": time.Now().Add(time.Hour * time.Duration(-72))}}).Distinct("ip", &offlineIPList)
		if err != nil {
			log.Println(err.Error())
		}
		if len(offlineIPList) >= 100 {
			time.Sleep(time.Second * 60)
			continue
		}
		for _, ip := range offlineIPList {
			log.Println(bson.M{"uptime": bson.M{"$lte": time.Now().Add(time.Hour * time.Duration(-72))}})
			err = DB.C("client").Remove(bson.M{"ip": ip})
			if err != nil {
				log.Println(err.Error())
			}
		}
	}
}

func conn(netloc string, dbname string) (*mgo.Database, error) {
	session, err := mgo.Dial(netloc)
	if err != nil {
		return nil, err
	}
	session.SetMode(mgo.Monotonic, true)
	db := session.DB(dbname)
	return db, nil
}

func main() {
	cleanThread()
}

虽然没有复现问题,但是这里确实需要sleep,感谢pr。

@neargle neargle merged commit dcc5dc1 into ysrc:master Apr 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants