2016-11-09 4 views
-1

짧은 이야기로, 3 대의 AWS 지역에 여러 서버가 있습니다. Node로 작성된 서버를 줄이기 위해 Go에서 다시 작성했습니다. 기쁜 소식과 놀랍게도 1 Go 서버는 CPU 사용률이 낮고 ELB 대기 시간이 적은 노드 서버 10 개를 처리 할 수 ​​있습니다. 이것은 굉장합니다. 그러나, 나는 그들이 너무 빨리 롤 생각할 수 있습니다. 우리는 데이터를 기록하는 데 사용되는 단일 MongoDB 서버를 가지고 있습니다. 들어오는 모든 요청은 특정 방식으로 기록되며이 서버로 전송됩니다. MongoDB 서버가 충돌하기 시작한 2 개의 Go 서버를 추가 한 이후. 이 문제를 해결하기위한 노력에서내 Go 서버가 내 Mongo 서버를 죽이고 있다고 생각합니다.

역 추적

2016-11-09T03:06:41.240-0500 I JOURNAL [journal writer] warning couldn't write to/rename file /data/data/db/journal/prealloc.2: Couldn't open directory '/data/data/db/journal' for flushing: errno:24 Too many open files 
2016-11-09T03:06:41.240-0500 I JOURNAL [journal writer] warning exception opening journal file couldn't open file /data/data/db/journal/j._213 for writing errno:9 Bad file descriptor 
2016-11-09T03:06:41.240-0500 I JOURNAL [journal writer] error exception in dur::journal couldn't open file /data/data/db/journal/j._213 for writing errno:9 Bad file descriptor 
2016-11-09T03:06:41.242-0500 F JOURNAL [journal writer] dbexception in journalWriterThread causing immediate shutdown: 13516 couldn't open file /data/data/db/journal/j._213 for writing errno:9 Bad file descriptor 
2016-11-09T03:06:41.242-0500 I -  [journal writer] Invariant failure false src/mongo/db/storage/mmap_v1/dur_journal_writer.cpp 258 
2016-11-09T03:06:41.246-0500 I JOURNAL [durability] warning couldn't write to/rename file /data/data/db/journal/prealloc.2: couldn't open file /data/data/db/journal/prealloc.2 for writing errno:9 Bad file descriptor 
2016-11-09T03:06:41.246-0500 F JOURNAL [durability] dbexception in durThread causing immediate shutdown: 13516 couldn't open file /data/data/db/journal/j._213 for writing errno:9 Bad file descriptor 
2016-11-09T03:06:41.246-0500 I -  [durability] Invariant failure false src/mongo/db/storage/mmap_v1/dur.cpp 862 
2016-11-09T03:06:41.246-0500 I CONTROL [journal writer] 
0xf51949 0xef1671 0xed6192 0xd17613 0xf9f9b4 0x7fd09697d184 0x7fd09544337d 
----- BEGIN BACKTRACE ----- 
{"backtrace":[{"b":"400000","o":"B51949"},{"b":"400000","o":"AF1671"},{"b":"400000","o":"AD6192"},{"b":"400000","o":"917613"},{"b":"400000","o":"B9F9B4"},{"b":"7FD096975000","o":"8184"},{"b":"7FD095349000","o":"FA37D"}],"processInfo":{ "mongodbVersion" : "3.0.3", "gitVersion" : "b40106b36eecd1b4407eb1ad1af6bc60593c6105", "uname" : { "sysname" : "Linux", "release" : "3.13.0-54-generic", "version" : "#91-Ubuntu SMP Tue May 26 19:15:08 UTC 2015", "machine" : "x86_64" }, "somap" : [ { "elfType" : 2, "b" : "400000", "buildId" : "F56F80CB96B4DBFC070BEB0ADAC7D6B274BFC6B1" }, { "b" : "7FFF14FD0000", "elfType" : 3, "buildId" : "1C0D0A18FF043EED9EE11DB5E5E90A3F74729341" }, { "b" : "7FD096975000", "path" : "/lib/x86_64-linux-gnu/libpthread.so.0", "elfType" : 3, "buildId" : "31E9F21AE8C10396171F1E13DA15780986FA696C" }, { "b" : "7FD096716000", "path" : "/lib/x86_64-linux-gnu/libssl.so.1.0.0", "elfType" : 3, "buildId" : "74864DB9D5F69D39A67E4755012FB6573C469B3D" }, { "b" : "7FD09633A000", "path" : "/lib/x86_64-linux-gnu/libcrypto.so.1.0.0", "elfType" : 3, "buildId" : "AAE7CFF8351B730830BDBCE0DCABBE06574B7144" }, { "b" : "7FD096132000", "path" : "/lib/x86_64-linux-gnu/librt.so.1", "elfType" : 3, "buildId" : "E2A6DD5048A0A051FD61043BDB69D8CC68192AB7" }, { "b" : "7FD095F2E000", "path" : "/lib/x86_64-linux-gnu/libdl.so.2", "elfType" : 3, "buildId" : "DA9B8C234D0FE9FD8CAAC8970A7EC1B6C8F6623F" }, { "b" : "7FD095C2A000", "path" : "/usr/lib/x86_64-linux-gnu/libstdc++.so.6", "elfType" : 3, "buildId" : "76190E922AF7457D078F75C9B15FA184E83EB506" }, { "b" : "7FD095924000", "path" : "/lib/x86_64-linux-gnu/libm.so.6", "elfType" : 3, "buildId" : "D144258E614900B255A31F3FD2283A878670D5BC" }, { "b" : "7FD09570E000", "path" : "/lib/x86_64-linux-gnu/libgcc_s.so.1", "elfType" : 3, "buildId" : "36311B4457710AE5578C4BF00791DED7359DBB92" }, { "b" : "7FD095349000", "path" : "/lib/x86_64-linux-gnu/libc.so.6", "elfType" : 3, "buildId" : "CF699A15CAAE64F50311FC4655B86DC39A479789" }, { "b" : "7FD096B93000", "path" : "/lib64/ld-linux-x86-64.so.2", "elfType" : 3, "buildId" : "D0F537904076D73F29E4A37341F8A449E2EF6CD0" } ] }} 
mongod(_ZN5mongo15printStackTraceERSo+0x29) [0xf51949] 
mongod(_ZN5mongo10logContextEPKc+0xE1) [0xef1671] 
mongod(_ZN5mongo15invariantFailedEPKcS1_j+0xB2) [0xed6192] 
mongod(_ZN5mongo3dur13JournalWriter20_journalWriterThreadEv+0x953) [0xd17613] 
mongod(+0xB9F9B4) [0xf9f9b4] 
libpthread.so.0(+0x8184) [0x7fd09697d184] 
libc.so.6(clone+0x6D) [0x7fd09544337d] 

나는 그 기계에 ulimit를 증가했다. 그러나 뭔가가 프로그래밍 측면에서도 수행 될 수 있다고 생각합니다. 여기

다음
package mongo 

import (
    "time" 

    "gopkg.in/mgo.v2" 
) 

var (
    // TagDB ... 
    TagDB DataStore 
    // LogDB ... 
    LogDB DataStore 
) 

func init() { 
    TagDB.ConnectToTagserver() 
    LogDB.ConnectToLogServer() 
} 

// DataStore containing a pointer to a mgo session 
type DataStore struct { 
    Session *mgo.Session 
} 

// ConnectToTagserver is a helper method that connections to pubgears' tagserver 
// database 
func (ds *DataStore) ConnectToTagserver() { 
    mongoDBDialInfo := &mgo.DialInfo{ 
     Addrs: []string{"some_IP"}, 
     Timeout: 60 * time.Second, 
     Database: "tagserver", 
    } 
    sess, err := mgo.DialWithInfo(mongoDBDialInfo) 
    if err != nil { 
     panic(err) 
    } 
    sess.SetMode(mgo.Monotonic, true) 
    TagDB.Session = sess 
} 


// database SERVER IN QUESTION 
func (ds *DataStore) ConnectToLogServer() { 
    mongoDBDialInfo := &mgo.DialInfo{ 
     Addrs: []string{"some_IP"}, 
     Timeout: 60 * time.Second, 
     Database: "nginx_logs", 
    } 
    sess, err := mgo.DialWithInfo(mongoDBDialInfo) 
    if err != nil { 
     println(1) 
     panic(err) 
    } 
    sess.SetMode(mgo.Monotonic, true) 
    LogDB.Session = sess 
} 

// Close is a helper method that ensures the session is properly terminated 
func (ds *DataStore) Close() { 
    ds.Session.Close() 
} 

내 경로 내에서

package models 

import (
    "errors" 
    "fmt" 
    "strconv" 
    "time" 

    "gopkg.in/mgo.v2/bson" 

    "./mongo" 
) 

// RawRequests ... 
type RawRequests struct { 
    TagServer string   `json:"tag_server"` 
    Server  string   `json:"server"` 
    Slug  string   `json:"slug"` 
    Zone  string   `json:"zone"` 
    Size  string   `json:"size"` 
    Network  string   `json:"network"` 
    TagHash  string   `json:"tag_hash"` 
    Extra  string   `json:"extra"` 
    Logged  time.Time   `json:"logged"` 
    Date  string   `json:"date"` 
    Hour  int    `json:"hour"` 
    QueryParams map[string]string `json:"query_params"` 
} 

// RawTagRequests ... 
type RawTagRequests struct { 
    TagServer string   `json:"tag_server"` 
    Server  string   `json:"server"` 
    Slug  string   `json:"slug"` 
    Zone  string   `json:"zone"` 
    Size  string   `json:"size"` 
    Network  string   `json:"network"` 
    TagHash  string   `json:"tag_hash"` 
    Extra  string   `json:"extra"` 
    Logged  time.Time   `json:"logged"` 
    Date  string   `json:"date"` 
    Hour  int    `json:"hour"` 
    QueryParams map[string]string `json:"query_params"` 
    ChainNext string   `json:"chain_next"` 
} 

// LogRequest ... 
func LogRequest(tagServer string, server string, slug string, zone string, size string, network string, extra string, tagHash string, queryParams map[string]string) error { 
    dbsession := mongo.LogDB.Session.Copy() 
    defer dbsession.Close() 
    logCollection := dbsession.DB("nginx_logs").C("raw_requests") 
    d := time.Now() 
    hour, _, _ := d.Clock() 
    year, month, day, _ := formatDate(d.Year(), int(d.Month()), d.Day()) 
    date := fmt.Sprintf("%s-%s-%s", year, month, day) 
    return logCollection.Insert(bson.M{"tag_server": tagServer, "server": server, "slug": slug, "zone": zone, "size": size, "network": network, "tag_hash": tagHash, "extra": extra, "logged": time.Now(), "date": date, "hour": hour, "query_params": queryParams}) 
} 

// LogTagRequests ... 
func LogTagRequests(tagServer string, server string, slug string, zone string, size string, network string, extra string, tagHash string, queryParams map[string]string, chainHash string) error { 
    dbsession := mongo.LogDB.Session.Copy() 
    defer dbsession.Close() 
    logCollection := dbsession.DB("nginx_logs").C("raw_tag_requests") 
    d := time.Now() 
    hour, _, _ := d.Clock() 
    year, month, day, _ := formatDate(d.Year(), int(d.Month()), d.Day()) 
    date := fmt.Sprintf("%s-%s-%s", year, month, day) 
    return logCollection.Insert(bson.M{"tag_server": tagServer, "server": server, "slug": slug, "zone": zone, "size": size, "network": network, "tag_hash": tagHash, "extra": extra, "logged": time.Now(), "date": date, "hour": hour, "query_params": queryParams, "chain_hash": chainHash}) 
} 

func formatDate(year int, month int, day int) (y string, m string, d string, err error) { 
    var tmonth, tday, tyear string 
    tyear = strconv.Itoa(year) 
    // convert the month and year and replace with the correct number of digits 
    if month < 10 { 
     tmonth = "0" + strconv.Itoa(int(month)) 
    } else { 
     tmonth = strconv.Itoa(int(month)) 
    } 
    if day < 10 { 
     tday = "0" + strconv.Itoa(int(day)) 
    } else { 
     tday = strconv.Itoa(int(day)) 
    } 
    if len(tmonth) == 2 && len(tday) == 2 && len(tyear) == 4 { 
     return tyear, tmonth, tday, nil 
    } 
    return tyear, tmonth, tday, errors.New("One of the values passed in does not meet the time format requirement month-day-year --> 1991-01-15") 
} 

마지막으로 해당 패키지를 사용하는 파일 내 몽고 패키지를, 난 그저 내 메서드를 호출 go LogRequest(tagServer, host, site, channel, adSize, network, tagExtra, tagHash, queryParams)

+1

열린 소켓의 수를 줄이기 위해 세션을 복사/닫기 대신 호출하는 것이 좋습니다. 이것은 "너무 많은 파일 열기"의 원인 일 수 있습니다. 나는 문서가 스레드 안전하다고 세션에서 읽었습니다. – squiguy

+0

나는 그것들을 복사함으로써 그것들을 재사용하고 있다고 생각했다. 작은 예제를 제공 할 수 있습니까? @squiguy – reticentroot

+0

복제본과 복사의 차이점은 무엇입니까? 귀하의 의견은 내 검색어를 안내하고 그것이 내가 우연히 만난 것입니다. – reticentroot

답변

1
  1. 당신은 요청 - 응답주기마다 한 번만 사본을 사용해야합니다. 모델을 호출 할 때마다이를 사용하면 많은 소켓을 소비하게됩니다. 각 모델 호출은 루틴을 종료하는 동안 소켓을 열고 닫습니다 (지연을 사용하여). 동일한 요청 - 응답주기에서 db에 대한 여러 호출간에 복사 된 세션을 다시 사용하십시오.
  2. 로그와 코드 스 니펫에서 각 요청 - 응답 사이클에서 CRUD를 한 번만 수행하도록 모델 루틴을 호출하지 않는 한 1을 처리해야하는 것처럼 보입니다.
  3. Copy는 부모 세션과 동일한 소켓을 사용하는 동안 부모 세션의 인증 자격 증명을 유지하면서 새 소켓을 사용합니다 (복제 된 세션을 사용하는 동안 부모/자식 중 하나는 소켓을 기다려야합니다. 다른 사람은 그것으로 끝난다.). 따라서 서버에 대한 여러 요청을 병렬로 처리하려면 사본 당 요청 처리기를 사용하는 것이 더 좋습니다. (https://godoc.org/gopkg.in/mgo.v2#Session.Clone)
  4. 기계에서 ulimit을 증가시킨 후 즉시 반영되도록하십시오 - http://lzone.de/blog/Apply-changes-to-limits.conf-immediately.
+0

그래서 코드를 수정했습니다. 각 요청의 맨 위에 세션 객체를 인스턴스화하고 세션의 복제를 메소드에 전달하여 사용합니다. 나는이 루틴을 이동 루틴으로 돌리고 있기 때문에 세션을 복사하지 않았고 mongo 호출은 삽입이므로 리턴 데이터를 기다릴 필요가 없습니다. 조언 주셔서 감사합니다 :-) – reticentroot