Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Plain db import with Go

Plain db import with Go

golang.tokyo#2での発表資料です。

Seiji Takahashi

December 12, 2016
Tweet

More Decks by Seiji Takahashi

Other Decks in Technology

Transcript

  1. timakin/gopli • Plain DB import tool with toml configuration. •

    Without complex replication settings. 
 $ gopli sync -f production -t local -c config/repli.toml
  2. طଘͷํ๏ • ͔͍͋ͬͨshell • mysqlpump • maxbube/mydumper • mysqldump͕γϯάϧεϨουͳͷʹରͯ͠ɺ
 ্ه̎ͭ͸ϚϧνεϨου

    • উखʹઃఆܾΊͱ͍ͯ೚ҙͷλΠϛϯάʹ
 ίϚϯυΛࢥ͍ग़ͣ͞ʹར༻ग़དྷΔ΋ͷ͕ͳ͍…
  3. σʔλͷͱ͖ͬͯํ • ςʔϒϧҰཡͷऔಘ • goroutineΛར༻ͭͭ͠ɺςʔϒϧ͝ͱʹλϒ۠੾ΓͷSELECT݁ՌΛ tmpϑΝΠϧͱͯ͠txtʹอଘ • schema_migrations, repli_chk౳ͷςʔϒϧ͸ϒϥοΫϦετͱͯ͠আ֎ •

    ஫ೖઌͷDBͷςʔϒϧΛҰ୴DELETE͢Δ • LOAD DATA LOCAL INFILEͰσʔλΛϦετΞɻ͜ͷࡍʹ΋֤ςʔϒϧ ʹಉ࣌ʹσʔλΛೖΕΔͨΊʹgoroutineΛར༻
  4. toml configuration • TOML࠾༻͠·ͨ͠ɻ
 ؀ڥ͝ͱͷ઀ଓઃఆ౳ɻ [database] [database.local] host = "localhost"

    management_system = "mysql" name = "app_development" user = "root" password = "" [database.staging] host = "xxx.xxx.xxx.xxx" management_system = "mysql" name = "app_staging" user = "root" password = "" [database.production] host = "yyy.yyy.yyy.yyy" management_system = "mysql" name = "app_production" user = "root" password = ""
  5. mysql΁ͷ઀ଓ • go-mysql-driver/mysqlܦ༝ͰͷϦϞʔτ΁ͷ઀ଓ͸constͰܾΊΒΕͯΔ maxPacketSizeΛ௒͑Δͱଈࢮ͠·ͨ͠ͷͰ࢖͑ͣɻ • mysql -h Ͱͷ઀ଓ΋ࢼΈ·͕ࣦͨ͠ഊɻgo-mysql-driver/mysqlΈͨ͘ Keep-Aliveͯ͘͠Εͳ͍ͷ͔ɺଈࢮ͠·ͨ͠ɻ •

    Ͳ͏΍ΒϩʔΧϧͷmysqlΫϥΠΞϯτʹ઀ଓॲཧΛ೚ͤͯgoroutineΛ ࢖͓͏ͱͯ͠΋ɺίωΫγϣϯ਺͕૿͑ΔͷͰࢮɻɹ • sshͰ઀ଓͨ͠ޙɺsession.RunͰ௚ʹΫΤϦΛ࣮ߦ͢ΔखஈΛऔΕ͹ɺί ωΫγϣϯΛ̍ͭுΓͭͭηογϣϯΛ૿΍ͤΔ & ఆ਺ͷ੍໿Λ֎ͤΔͷ Ͱɺฒྻ࣮ߦͰ͖Δ༷ࢠɻʢਏ͍ʣ
  6. goroutineΛ࢖͏৔໘ sem := make(chan int, MaxFetchSession) var wg sync.WaitGroup for

    _, table := range tables { wg.Add(1) go func(table string) { sem <- 1 defer wg.Done() defer func() { <-sem }() session, err := conn.NewSession() if err != nil { panic("Failed to create session: " + err.Error()) } defer session.Close() var fetchTableStdoutBuf bytes.Buffer session.Stdout = &fetchTableStdoutBuf fetchRowsCmd := fmt.Sprintf(SelectTablesCmd, srcDBConf.User, srcDBConf.Password, srcDBConf.Name, table) err = session.Run(fetchRowsCmd) MySQL͸1ΫΤϦΛΑ͠ͳʹϚ ϧνίΞͰ΍ͬͯ͘ΕΔ࢓૊Έ ͕ͳ͍ͷͰɺμϯϓ/ϦετΞΛ goroutineͰฒྻԽ͢Δ͜ͱͰɺ
 γϡοͱσʔλΛऔͬͯ͜Ε· ͢ɻ