Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Go_at_MongoDB.pdf
Search
Wisdom
February 18, 2015
50
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Go_at_MongoDB.pdf
nycgolang meetup
Wisdom
February 18, 2015
More Decks by Wisdom
See All by Wisdom
Deployment Strategies at Codecademy
waomuya
0
41
Error Handling at Spring
waomuya
0
48
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
500
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Visualization
eitanlees
153
17k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Code Review Best Practice
trishagee
74
20k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Believing is Seeing
oripsolob
1
260
Fireside Chat
paigeccino
43
4.1k
The Curse of the Amulet
leimatthew05
3
16k
Transcript
None
MONGODB TOOLS Wisdom Omuya, Software Engineer Making the Move to
Golang
PORT OF DEPARTURE
WHAT IS MONGODB - Document-oriented database - JSON-like data -
Open-source
WHAT ARE MONGODB TOOLS? Several command line tools: - bsondump
- mongofiles - mongooplog - mongodump - mongorestore - mongoimport - mongoexport - mongotop - mongostat
MOTIVATION - Looser coupling - Shorter release cycle - Performance
improvements
FERRYING THE TOOLS
CASE STUDY - MONGOIMPORT - Import text data into MongoDB
- Accepted formats include JSON, CSV, and TSV
MONGOIMPORT ARCHITECTURE
MONGOIMPORT PIPELINE 1 chanSize := opts.BulkBufferSize * opts.MaxInsertThreads 2 3
rawChan := make(chan bson.Raw, opts.NumDecoders) 4 docChan := make(chan bson.D, chanSize) 5 6 7 errChan := make(chan error) 8 9 // read and process from the input reader 10 go func() { 11 errChan <- Decode(opts.NumDecoders, rawChan, docChan) 12 }() 13 14 15 // insert documents into the target database 16 go func() { 17 errChan <- Insert(docChan) 18 }() 19 20 return channelQuorumError(errChan, 2)
TYING ‘EM UP 1 func channelQuorumError(ch <-chan error, q int)
error { 2 3 for i := 0; i < q; i++ { 4 if err := <-ch; err != nil { 5 return err 6 } 7 } 8 9 return nil 10 }
TRAVEL VAGARIES - Transportation Mode - Booty Protection - Load
Limits
TRANSPORTATION MODE 1 import "gopkg.in/mgo/bson" 2 3 type M map[string]interface{}
4 5 bson.M{"a": 1, "b": true} 1 import "gopkg.in/mgo/bson" 2 3 type D []DocElem 4 5 type DocElem struct { 6 Name string 7 Value interface{} 8 } 9 10 bson.D{{"a", 1}, {"b", true}}
BATTLE LESSON 1 “MVDS - Minimum Viable Data Structure” go
tool pprof mongoimport cpu.prof
BOOTY PROTECTION - 1 1 func Decode(numDecoders int, rawChan chan
bson.Raw, docChan chan bson.D) error { 2 var decodeWorkers []DecodeWorker 3 wg := &sync.WaitGroup{} 4 for i := 0; i < numDecoders; i++ { 5 decoder := &DecodeWorker{ 6 inChan: rawChan, 7 outChan: docChan, 8 } 9 decodeWorkers = append(decodeWorkers, decoder) 10 wg.Add(1) 11 go func() { 12 defer wg.Done() 13 err := decoder.processDocuments(); 14 if err != nil { 15 return err 16 } 17 }() 18 } 19 wg.Wait()
BOOTY PROTECTION - 2 1 var decodeWorkers []*DecodeWorker 2 wg
:= &sync.WaitGroup{} 3 mt := &sync.Mutex{} 4 decodeTomb := &tomb.Tomb{} 5 for i := 0; i < numDecoders; i++ { 6 decoder := &DecodeWorker{ 7 inChan: rawChan, 8 outChan: docChan, 9 tomb: decodeTomb, 10 } 11 decodeWorkers = append(decodeWorkers, decoder) 13 wg.Add(1) 14 go func() { 15 defer wg.Done() 16 err := decoder.processDocuments() 17 mt.Lock() 18 defer mt.Unlock() 19 if err != nil { 20 retErr = appendErr(retErr, err) 21 decoder.tomb.Kill(err) 22 } 23 }() 24 } 25 wg.Wait()
BOOTY PROTECTION - 3 1 var decodeWorkers []*DecodeWorker 2 wg
:= &sync.WaitGroup{} 3 mt := &sync.Mutex{} 4 decodeTomb := &tomb.Tomb{} 5 for i := 0; i < numDecoders; i++ { 6 decoder := &DecodeWorker{ 7 inChan: rawChan, 8 outChan: docChan, 9 tomb: decodeTomb, 10 } 11 decodeWorkers = append(decodeWorkers, decoder) 12 wg.Add(1) 13 go func(dw DecodeWorker) { 14 defer wg.Done() 15 err := dw.processDocuments() 16 mt.Lock() 17 defer mt.Unlock() 18 if err != nill { 19 retErr = err 20 dw.tomb.Kill(err) 21 } 22 }(*decoder) 23 } 24 wg.Wait() 25 return retErr
LESSON 2 “Continuously test your product using a race build”
go build -race mongoimport.go
LOAD LIMITS 1 import "gopkg.in/mgo/bson" 2 chanSize := opts.BufferSize*opts.NumInserters 3
docChan := make(chan bson.D, chanSize) 1 import "gopkg.in/mgo/bson" 2 3 docChan := make(chan bson.Raw, InsertBufferSize)
BATTLE LESSON 3 “Buffered channels are great - provided you
don’t run out of memory”
SEAFARER’S SURVIVAL KIT - Tomb: https://github.com/go-tomb/tomb/tree/v2 - Sync: http://golang.org/pkg/sync -
Pprof: https://blog.golang.org/profiling-go- programs - Race Detector: http://blog.golang.org/race- detector
PORT OF ARRIVAL
QUESTIONS Try out our tools at: https://github.com/mongodb/mongo- tools/