SlideShare una empresa de Scribd logo
1 de 59
Descargar para leer sin conexión
Copyright © 2009-2017 eureka, inc. All rights reserved.
golang.tokyo #7 Wizard
Takuma Morikawa / eureka, Inc.
2017-07-03
Who?
●
-
-
-
Agenda
● 1. About Us
● 2. What Lead Us to Create This?
● 3. How to Use It?
CONFIDENTIAL
About Us - Pairs
https://eure.jp/pairs-5million-thanks/
About Us - Couples
About Us - IAC/Match Group
2017.01
2016.01
2016.09
2015.05
Our History
"Couples" gets over 4 million
downloads.
"Pairs" gets over 5 million user
and 32 million matchings.
Junya Ishibashi is elected CEO,
succeeding Yu Akasaka, who
retained chairman title.
IAC/InterActiveCorp acquires
eureka.
2014.06
2014.05
2013.10
2012.10
2011.09
2010.08
2008.11
"Pairs" gets over 1 million user
and 3.3 million matchings.
eureka launches communication
app for couple, "Couples".
eureka launches online-dating
service "Pairs" in Taiwan.
eureka launches online-dating
service "Pairs" in Japan.
eureka starts smartphone and
Facebook app services.
eureka starts blog marketing
services.
eureka is founded in Tokyo.
Today’s Talk...
Today’s talk...
Today’s talk...
* Gopher designed by Hinako Sakawa - Original Gopher designed by Renee French.
https://github.com/ohina
What’s Wizard?
● https://github.com/evalphobia/wizard
● Database Partitioning/Sharding Library
- Vertical partitioning
- Horizontal sharding
- Inspired by mixed_gauge (https://github.com/taiki45/mixed_gauge)
- Hash Slots
- Clusters
- Extra features
- Read only flag
- Transaction Management
- Query to all shards
- Wizard is NOT ORM
● Supported ORM
- xorm (https://github.com/go-xorm/xorm)
2. What Lead Us to Create This?
One day, on very sunny day morning...
あるよく晴れた日の朝...
Mr. Me (alias)
私さん(仮名)
ʕº̫͡ºʔ
Note: All characters and stories are fictitious examples
※ 登場人物・話は全て架空の例です
Mr. Boss (alias)
ボス(仮名)
ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
One day, on very sunny day morning… (2014, Winter)
ʕº̫͡ºʔ< "Awesome weather today, huh!"
(うーん、今日もいい天気)
ʕº̫͡ºʔ< "Nothing beats Golang on a day like this."
(こんな日はGoに限る)
ʕº̫͡ºʔ< "Alright, let’s write some codes."
(よーし、今日も仕事しちゃうぞ)
"Hi, Mr. Me" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(私さん)
"I have something to tell you..." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(ちょっと話があるんですが...)
One day, on very sunny day morning… (2014, Winter)
One day, on very sunny day morning… (2014, Winter)
ʕº̫͡ºʔ< "What's the matter?"
(え、なんでしょうか)
ʕº̫͡ºʔo0O (Something smells fishey...)
(これは危険なニオイがするぞ...)
One day, on very sunny day morning… (2014, Winter)
"About the next golang project," >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(Goフルスクラッチに際して)
"We needs some kind of database shardings,
right?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(DBシャーディング必要じゃないですか?)
One day, on very sunny day morning… (2014, Winter)
ʕº̫͡ºʔ< "Ah, well..."
(あー、そうですね。。。)
ʕº̫͡ºʔ< "But there isn’t such library in golang."
(でもちょっと探してみた限りシャーディング対応したライブラリなさそうなんですよ
ね。)
ʕº̫͡ºʔ< "I think it’s better to do it using some
middleware for MySQL"
(やっぱミドルウェア側でやるしかないんじゃないですかね)
One day, on very sunny day morning… (2014, Winter)
"Hmmmmm, whatever" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(ふーん)
"You should fix the details and   
add it to the schedule." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(まあいいけどちゃんと詳細決めて見積もり入れておいてくださいよ)
One day, on very sunny day morning… (2014, Winter)
ʕº̫͡ºʔ< "Roger, sir!"
(ラジャー!)
"..." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(He replies cheerfully, but never keeps a schedule.)
O0oʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(返事だけはいつも調子いいからけどスケジュール全く守らないからな、
こいつ...)
3 Months Later...
三ヶ月後...
3 Months Later (Spring)
"Hi, Mr. Me" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(私さん)
"What’s going on the sharding?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(シャーディングの件どうなってます?)
3 Months Later (Spring)
ʕº̫͡ºʔ< "Ah, sharding! Well..."
(あーその件ですね。。。)
"Did you consider about it seriously?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(ちゃんと考えてました?)
ʕº̫͡ºʔo0O (he-he! Gotcha!)
(ふはは、かかったなバカめ!)
3 Months Later (Spring)
ʕº̫͡ºʔ< "Let’s try to use Vitess by Youtube."
(YoutubeのVitess使いましょう)
"What?!?!" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(は?!?!)
ʕº̫͡ºʔ< "Trust yourself."
(我々なら出来ますよ。自分を信じましょう。)
3 Months Later (Spring)
"Are you sure?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(信じましょうって...)
"It’s just released, right?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(まだ出たばっかですよね?)
"Moreover, don’t you think that   
you should maintain it yourself?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(それにあなた、自分で構築・運用する気ないですよね?)
3 Months Later (Spring)
"If use it, you should do it from beginning to the
end." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(Vitess採用するならあなた運用対応してくださいよ)
"I’ll catch you even if you escape." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(決めるだけ決めて逃げたらどこまでも追いかけますからね)
"Right?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(それでもいいですか?)
3 Months Later (Spring)
ʕº̫͡ºʔo0O (OMG...)
(そんなばかな...)
ʕº̫͡ºʔ< "Just kidding. I have another plan."
(いや、冗談です。実は本当の案があるんです。)
ʕº̫͡ºʔ< "We’ll write golang library for sharding."
(我々でGo言語のライブラリ作ります。)
3 Months Later (Spring)
"Alright. Should’ve said it in the first place" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(はじめからそう言ってください)
"We have 3 months to release," >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(3ヶ月後にはリリースですから)
"You don’t have much time to sleep. Just write code."
>ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(寝てる暇はありませんよ、さあ書きましょう)
3 Months Later (Spring)
ʕº̫͡ºʔ< "..."
(...)
ʕº̫͡ºʔo0O (Fxxk!)
(ちきしょー!)
Another 3 Months Later...
さらに三ヶ月後...
Another 3 Months Later (Summer)
ʕº̫͡ºʔ< "Phew,"
(ふー)
ʕº̫͡ºʔ< "The release has been postponed and this
library is almost about complete."
(リリースも延期になったし、なんとかライブラリも仕上がりそうだ)
Another 3 Months Later (Summer)
"Hi, Mr. Me" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(私さん)
"I have a good news" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(嬉しいお知らせがあります)
"You must be surprised." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(きっと驚きますよ?)
Another 3 Months Later (Summer)
ʕº̫͡ºʔ< "What’s the news?"
(どうしたんですか?)
"Listen," >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(実は...)
"AWS Aurora is just released" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(AWS Auroraがリリースされました)
Another 3 Months Later (Summer)
ʕº̫͡ºʔ< "What?!"
(え???)
ʕº̫͡ºʔ (Checking release infomation of AWS Aurora)
(AWS Auroraのリリース情報をチェックする)
Another 3 Months Later (Summer)
https://aws.amazon.com/blogs/aws/now-available-amazon-aurora/
Another 3 Months Later (Summer)
https://aws.amazon.com/blogs/aws/now-available-amazon-aurora/
NO MORE SHARDING!!
Another 3 Months Later (Summer)
"So we don’t need shardings anymore." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(これでもうシャーディングは不要ですね)
"What a peaceful world." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
(いい世の中になりました。)
Another 3 Months Later (Summer)
ʕº̫͡ºʔ< "..."
ʕº̫͡ºʔo0O (...)
ʕº̫͡ºʔo0O (I’ll go home and sleep, seriously.)
(今日はもう帰って寝よう...)
Another 3 Months Later (Summer)
Chapter 1.
“Tales of Sharding”
~ Fin ~
3. How to use Wizard?
How to use Wizard?
https://github.com/evalphobia/wizard
How to use Wizard?
https://github.com/evalphobia/wizard
See README!!
How to use Wizard?
Chapter 2.
“How to use”
~ Fin ~
How to use Wizard? (Setup Engine)
● 1. Initialize New Engine
- xorm.NewEngine(“mysql”, “user:pass@tcp(0.0.0.0:3306)/dbname”)
● 2. Create New Cluster
- cluster := wizard.NewCluster(engine) // <= register master db
- cluster.RegisterSlave(engine2) // <= register slave db
● 3. Add cluster to new wizard struct
- w := wizard.NewWizard()
- w.SetDefault(cluster)
- ormEngine = wizardxorm.New(w)
How to use Wizard? (Get Session)
● 4. Get Session from wizard
- session, err := ormEngine.UseMasterSession(ctx, User{})
● first arg is identifier.
○ Use same identifier, you can get same session.
○ Suit for a series of transaction during the request.
○ e.g.) context.Context, *http.Request
○ Anything is OK!
● second arg is struct for table.
● 5. Execute SQL
- session.Sql("SELECT 1")
Wizard
What’s Wizard/Cluster/Engine?
Cluster (User)
DB
Master
M
DB
Slave1
S
DB
Slave2
S
(Engine)
(Engine)
(Engine)
Session
Cluster (Article)
DB
Master
M
DB
Slave1
S
DB
Slave2
S
(Engine)
(Engine)
(Engine)
Shard Cluster
(Favorite)
Shard #1 SSM
Shard #2 SSM
Shard #3 SSM
①
②
③
④
Code: Setup Engine
import (
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/evalphobia/wizard"
wizardORM "github.com/evalphobia/wizard/orm/xorm"
)
Code: Setup Engine
var ormEngine wizardORM.ORM
// Initialize database. (Ignore errors for simpleness)
func initNormal() {
// xorm's engine(DB handler)
xormEngine, _ := xorm.NewEngine("mysql", "user:pass@tcp(0.0.0.0:3306)/dbname")
// Create DB Cluster (argument is master DB)
cluster := wizard.NewCluster(xormEngine)
// Create wizard (DB and session handler)
w := wizard.NewWizard()
w.SetDefault(cluster) // set default cluster
ormEngine = wizardxorm.New(w)
}
Code: Execute SQL #1 - select to user’s table
// Get User from database.
func sqlGetUser(ctx context.Context, userID int64) (User, error) {
session, _ := ormEngine.UseMasterSession(ctx, &User{})
session.And("user_id = ?", id)
session.ForUpdate() // Lock the row
user := &User{}
_, _ = session.Get(user) //
return user
}
// => SELECT * FROM user WHERE user_id = userID FOR UPDATE;
Code: Execute SQL #2 - update user’s table
// Update user’s point
func sqlUpdateUserPoint(ctx context.Context, userID int64, point int64) error {
session, _ := ormEngine.UseMasterSession(ctx, &User{})
session.Cols("point")
session.And("id = ?", userID)
return session.Update(&User{
Point: point,
})
}
// => UPDATE user SET point = point WHERE user_id = userID;
Code: Execute SQL #3 - update campaign table
// Update campaign status of the user
func sqlUpdateCampaignStatus(ctx context.Context, userID int64) error {
session, _ := ormEngine.UseMasterSession(ctx, &Campaign{})
session.Cols("status")
session.And("user_id = ?", userID)
return session.Update(&Campaign{
Status: "end",
})
}
// => UPDATE campaign SET status = "end" WHERE user_id = userID;
Code: Execute SQL - wrap up
// UPDATE user’s point and complete campaign.
func ExecSQL(ctx context.Context) {
ormEngine.SetAutoTransaction(ctx, true) // start transaction when SQL is executed
userID := 1
u, _ := sqlGetUser(ctx, userID) // => BEGIN and SELECT
_ = sqlUpdateUserPoint(ctx, u.UserID, u.Point + 1) // => UPDATE user
_ = sqlUpdateCampaignStatus(ctx, u.UserID) // => UPDATE campaign
orm.SetAutoTransaction(ctx, false)
err = ormEngine.CommitAll(ctx) // COMMIT
ormEngine.CloseAll(ctx) // Close all sessions
}
How to use Shard? (Setup Engine)
● 1. Initialize New Engine
- xorm.NewEngine(“mysql”, “user:pass@tcp(0.0.0.0:3306)/dbname”)
● 2. Create New Clusters
- baseCluster := wizard.NewCluster(engine)
- shardCluster01 := wizard.NewCluster(shardEngineA01)
- shardCluster01.RegisterSlave(shardEngineA02)
- shardCluster02 := wizard.NewCluster(shardEngineB01)
- shardCluster02.RegisterSlave(shardEngineB02)
How to use Shard? (Setup Shard Clusters)
● 3. Add clusters to new wizard struct
- w := wizard.NewWizard()
- w.SetDefault(baseCluster)
- // ↑add normal cluster
- shards := w.CreateShardCluster(&UserFavorite{}, 1021)
- shards.RegisterShard(0, 510, shardCluster01)
- shards.RegisterShard(511, 1020, shardCluster02)
- // ↑add shards cluster
- w.RegisterTables(shards, &UserLogin{}, ...)
- // ↑add sharded tables
- ormEngine = wizardxorm.New(w)
How to use Shard? (Get Session)
● 4. Get Session from wizard
- session, err := ormEngine.UseMasterSessionByKey(ctx, User{}, key)
● first arg is identifier.
● second arg is struct for table.
● third ard is shard key.
○ e.g. user_id
● 5. Execute SQL
- session.Sql("SELECT 1")
- // ↑ executed on one shard
- ormEngine.FindParallelByCondition([]*User, condition)
- // ↑ executed on all shards
Wrap up
● Wizard is DB sharding/session management library
● Only supports xorm
- Send PR!
● eureka use Wizard for production.
● Hard to test...
- Data
- DB instances
- clusters of MySQL/PostgreSQL/etc...
CONFIDENTIAL
Thank you
:)
Thank you :)
* Gopher designed by Hinako Sakawa - Original Gopher designed by Renee French.
https://github.com/ohina

Más contenido relacionado

Similar a golang.tokyo #7 Wizard (Database Sharding Library for golang)

Elasticsearch sur Azure : Make sense of your (BIG) data !
Elasticsearch sur Azure : Make sense of your (BIG) data !Elasticsearch sur Azure : Make sense of your (BIG) data !
Elasticsearch sur Azure : Make sense of your (BIG) data !Microsoft
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++Mike Acton
 
CPANTS: Kwalitative website and its tools
CPANTS: Kwalitative website and its toolsCPANTS: Kwalitative website and its tools
CPANTS: Kwalitative website and its toolscharsbar
 
Crab: A Python Framework for Building Recommender Systems
Crab: A Python Framework for Building Recommender Systems Crab: A Python Framework for Building Recommender Systems
Crab: A Python Framework for Building Recommender Systems Marcel Caraciolo
 
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...MongoDB
 
Martin Splitt "A short history of the web"
Martin Splitt "A short history of the web"Martin Splitt "A short history of the web"
Martin Splitt "A short history of the web"Fwdays
 
Running a Realtime Stats Service on MySQL
Running a Realtime Stats Service on MySQLRunning a Realtime Stats Service on MySQL
Running a Realtime Stats Service on MySQLKazuho Oku
 
Using MongoDB and a Relational Database at MongoDB Day
Using MongoDB and a Relational Database at MongoDB DayUsing MongoDB and a Relational Database at MongoDB Day
Using MongoDB and a Relational Database at MongoDB Dayhayesdavis
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4alexsaves
 
Getting started with amazon aurora - Toronto
Getting started with amazon aurora - TorontoGetting started with amazon aurora - Toronto
Getting started with amazon aurora - TorontoAmazon Web Services
 
MongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsMongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsServer Density
 
MongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social PlatformMongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social PlatformManish Pandit
 
Improve your SQL workload with observability
Improve your SQL workload with observabilityImprove your SQL workload with observability
Improve your SQL workload with observabilityOVHcloud
 
Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009pauldix
 
200603ash.pdf Performance Tuning Oracle DB
200603ash.pdf Performance Tuning Oracle DB200603ash.pdf Performance Tuning Oracle DB
200603ash.pdf Performance Tuning Oracle DBcookie1969
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsIgnacio Martín
 

Similar a golang.tokyo #7 Wizard (Database Sharding Library for golang) (20)

Spark Meetup
Spark MeetupSpark Meetup
Spark Meetup
 
Storm Anatomy
Storm AnatomyStorm Anatomy
Storm Anatomy
 
Elasticsearch sur Azure : Make sense of your (BIG) data !
Elasticsearch sur Azure : Make sense of your (BIG) data !Elasticsearch sur Azure : Make sense of your (BIG) data !
Elasticsearch sur Azure : Make sense of your (BIG) data !
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
 
Sprockets
SprocketsSprockets
Sprockets
 
Os Krug
Os KrugOs Krug
Os Krug
 
CPANTS: Kwalitative website and its tools
CPANTS: Kwalitative website and its toolsCPANTS: Kwalitative website and its tools
CPANTS: Kwalitative website and its tools
 
Crab: A Python Framework for Building Recommender Systems
Crab: A Python Framework for Building Recommender Systems Crab: A Python Framework for Building Recommender Systems
Crab: A Python Framework for Building Recommender Systems
 
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
Ensuring High Availability for Real-time Analytics featuring Boxed Ice / Serv...
 
Martin Splitt "A short history of the web"
Martin Splitt "A short history of the web"Martin Splitt "A short history of the web"
Martin Splitt "A short history of the web"
 
Running a Realtime Stats Service on MySQL
Running a Realtime Stats Service on MySQLRunning a Realtime Stats Service on MySQL
Running a Realtime Stats Service on MySQL
 
Using MongoDB and a Relational Database at MongoDB Day
Using MongoDB and a Relational Database at MongoDB DayUsing MongoDB and a Relational Database at MongoDB Day
Using MongoDB and a Relational Database at MongoDB Day
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4
 
Getting started with amazon aurora - Toronto
Getting started with amazon aurora - TorontoGetting started with amazon aurora - Toronto
Getting started with amazon aurora - Toronto
 
MongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & AnalyticsMongoDB: Optimising for Performance, Scale & Analytics
MongoDB: Optimising for Performance, Scale & Analytics
 
MongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social PlatformMongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social Platform
 
Improve your SQL workload with observability
Improve your SQL workload with observabilityImprove your SQL workload with observability
Improve your SQL workload with observability
 
Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009Synchronous Reads Asynchronous Writes RubyConf 2009
Synchronous Reads Asynchronous Writes RubyConf 2009
 
200603ash.pdf Performance Tuning Oracle DB
200603ash.pdf Performance Tuning Oracle DB200603ash.pdf Performance Tuning Oracle DB
200603ash.pdf Performance Tuning Oracle DB
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
 

Más de Takuma Morikawa

Gunosy Beer Bash #05 pairs
Gunosy Beer Bash #05 pairsGunosy Beer Bash #05 pairs
Gunosy Beer Bash #05 pairsTakuma Morikawa
 
eureka go ライブラリの話
eureka go ライブラリの話eureka go ライブラリの話
eureka go ライブラリの話Takuma Morikawa
 
pairsでのAngularJS x TypeScript x e2e @めぐすた#1
pairsでのAngularJS x TypeScript x e2e @めぐすた#1 pairsでのAngularJS x TypeScript x e2e @めぐすた#1
pairsでのAngularJS x TypeScript x e2e @めぐすた#1 Takuma Morikawa
 
会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版
会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版
会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版Takuma Morikawa
 

Más de Takuma Morikawa (7)

Gunosy Beer Bash #05 pairs
Gunosy Beer Bash #05 pairsGunosy Beer Bash #05 pairs
Gunosy Beer Bash #05 pairs
 
Golang oss libraries
Golang oss librariesGolang oss libraries
Golang oss libraries
 
Microservices on pairs
Microservices on pairsMicroservices on pairs
Microservices on pairs
 
eureka go ライブラリの話
eureka go ライブラリの話eureka go ライブラリの話
eureka go ライブラリの話
 
pairsでのAngularJS x TypeScript x e2e @めぐすた#1
pairsでのAngularJS x TypeScript x e2e @めぐすた#1 pairsでのAngularJS x TypeScript x e2e @めぐすた#1
pairsでのAngularJS x TypeScript x e2e @めぐすた#1
 
golintを使おう
golintを使おうgolintを使おう
golintを使おう
 
会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版
会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版
会員数180万人のマッチングサービスpairsの 急成長を支える技術基盤 ディレクターズカット版
 

Último

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Último (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

golang.tokyo #7 Wizard (Database Sharding Library for golang)

  • 1. Copyright © 2009-2017 eureka, inc. All rights reserved. golang.tokyo #7 Wizard Takuma Morikawa / eureka, Inc. 2017-07-03
  • 3. Agenda ● 1. About Us ● 2. What Lead Us to Create This? ● 3. How to Use It?
  • 5. About Us - Pairs https://eure.jp/pairs-5million-thanks/
  • 6. About Us - Couples
  • 7. About Us - IAC/Match Group
  • 8. 2017.01 2016.01 2016.09 2015.05 Our History "Couples" gets over 4 million downloads. "Pairs" gets over 5 million user and 32 million matchings. Junya Ishibashi is elected CEO, succeeding Yu Akasaka, who retained chairman title. IAC/InterActiveCorp acquires eureka. 2014.06 2014.05 2013.10 2012.10 2011.09 2010.08 2008.11 "Pairs" gets over 1 million user and 3.3 million matchings. eureka launches communication app for couple, "Couples". eureka launches online-dating service "Pairs" in Taiwan. eureka launches online-dating service "Pairs" in Japan. eureka starts smartphone and Facebook app services. eureka starts blog marketing services. eureka is founded in Tokyo.
  • 11. Today’s talk... * Gopher designed by Hinako Sakawa - Original Gopher designed by Renee French. https://github.com/ohina
  • 12. What’s Wizard? ● https://github.com/evalphobia/wizard ● Database Partitioning/Sharding Library - Vertical partitioning - Horizontal sharding - Inspired by mixed_gauge (https://github.com/taiki45/mixed_gauge) - Hash Slots - Clusters - Extra features - Read only flag - Transaction Management - Query to all shards - Wizard is NOT ORM ● Supported ORM - xorm (https://github.com/go-xorm/xorm)
  • 13. 2. What Lead Us to Create This?
  • 14. One day, on very sunny day morning... あるよく晴れた日の朝...
  • 15. Mr. Me (alias) 私さん(仮名) ʕº̫͡ºʔ Note: All characters and stories are fictitious examples ※ 登場人物・話は全て架空の例です Mr. Boss (alias) ボス(仮名) ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢
  • 16. One day, on very sunny day morning… (2014, Winter) ʕº̫͡ºʔ< "Awesome weather today, huh!" (うーん、今日もいい天気) ʕº̫͡ºʔ< "Nothing beats Golang on a day like this." (こんな日はGoに限る) ʕº̫͡ºʔ< "Alright, let’s write some codes." (よーし、今日も仕事しちゃうぞ)
  • 17. "Hi, Mr. Me" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (私さん) "I have something to tell you..." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (ちょっと話があるんですが...) One day, on very sunny day morning… (2014, Winter)
  • 18. One day, on very sunny day morning… (2014, Winter) ʕº̫͡ºʔ< "What's the matter?" (え、なんでしょうか) ʕº̫͡ºʔo0O (Something smells fishey...) (これは危険なニオイがするぞ...)
  • 19. One day, on very sunny day morning… (2014, Winter) "About the next golang project," >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (Goフルスクラッチに際して) "We needs some kind of database shardings, right?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (DBシャーディング必要じゃないですか?)
  • 20. One day, on very sunny day morning… (2014, Winter) ʕº̫͡ºʔ< "Ah, well..." (あー、そうですね。。。) ʕº̫͡ºʔ< "But there isn’t such library in golang." (でもちょっと探してみた限りシャーディング対応したライブラリなさそうなんですよ ね。) ʕº̫͡ºʔ< "I think it’s better to do it using some middleware for MySQL" (やっぱミドルウェア側でやるしかないんじゃないですかね)
  • 21. One day, on very sunny day morning… (2014, Winter) "Hmmmmm, whatever" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (ふーん) "You should fix the details and    add it to the schedule." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (まあいいけどちゃんと詳細決めて見積もり入れておいてくださいよ)
  • 22. One day, on very sunny day morning… (2014, Winter) ʕº̫͡ºʔ< "Roger, sir!" (ラジャー!) "..." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (He replies cheerfully, but never keeps a schedule.) O0oʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (返事だけはいつも調子いいからけどスケジュール全く守らないからな、 こいつ...)
  • 24. 3 Months Later (Spring) "Hi, Mr. Me" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (私さん) "What’s going on the sharding?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (シャーディングの件どうなってます?)
  • 25. 3 Months Later (Spring) ʕº̫͡ºʔ< "Ah, sharding! Well..." (あーその件ですね。。。) "Did you consider about it seriously?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (ちゃんと考えてました?) ʕº̫͡ºʔo0O (he-he! Gotcha!) (ふはは、かかったなバカめ!)
  • 26. 3 Months Later (Spring) ʕº̫͡ºʔ< "Let’s try to use Vitess by Youtube." (YoutubeのVitess使いましょう) "What?!?!" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (は?!?!) ʕº̫͡ºʔ< "Trust yourself." (我々なら出来ますよ。自分を信じましょう。)
  • 27. 3 Months Later (Spring) "Are you sure?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (信じましょうって...) "It’s just released, right?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (まだ出たばっかですよね?) "Moreover, don’t you think that    you should maintain it yourself?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (それにあなた、自分で構築・運用する気ないですよね?)
  • 28. 3 Months Later (Spring) "If use it, you should do it from beginning to the end." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (Vitess採用するならあなた運用対応してくださいよ) "I’ll catch you even if you escape." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (決めるだけ決めて逃げたらどこまでも追いかけますからね) "Right?" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (それでもいいですか?)
  • 29. 3 Months Later (Spring) ʕº̫͡ºʔo0O (OMG...) (そんなばかな...) ʕº̫͡ºʔ< "Just kidding. I have another plan." (いや、冗談です。実は本当の案があるんです。) ʕº̫͡ºʔ< "We’ll write golang library for sharding." (我々でGo言語のライブラリ作ります。)
  • 30. 3 Months Later (Spring) "Alright. Should’ve said it in the first place" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (はじめからそう言ってください) "We have 3 months to release," >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (3ヶ月後にはリリースですから) "You don’t have much time to sleep. Just write code." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (寝てる暇はありませんよ、さあ書きましょう)
  • 31. 3 Months Later (Spring) ʕº̫͡ºʔ< "..." (...) ʕº̫͡ºʔo0O (Fxxk!) (ちきしょー!)
  • 32. Another 3 Months Later... さらに三ヶ月後...
  • 33. Another 3 Months Later (Summer) ʕº̫͡ºʔ< "Phew," (ふー) ʕº̫͡ºʔ< "The release has been postponed and this library is almost about complete." (リリースも延期になったし、なんとかライブラリも仕上がりそうだ)
  • 34. Another 3 Months Later (Summer) "Hi, Mr. Me" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (私さん) "I have a good news" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (嬉しいお知らせがあります) "You must be surprised." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (きっと驚きますよ?)
  • 35. Another 3 Months Later (Summer) ʕº̫͡ºʔ< "What’s the news?" (どうしたんですか?) "Listen," >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (実は...) "AWS Aurora is just released" >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (AWS Auroraがリリースされました)
  • 36. Another 3 Months Later (Summer) ʕº̫͡ºʔ< "What?!" (え???) ʕº̫͡ºʔ (Checking release infomation of AWS Aurora) (AWS Auroraのリリース情報をチェックする)
  • 37. Another 3 Months Later (Summer) https://aws.amazon.com/blogs/aws/now-available-amazon-aurora/
  • 38. Another 3 Months Later (Summer) https://aws.amazon.com/blogs/aws/now-available-amazon-aurora/ NO MORE SHARDING!!
  • 39. Another 3 Months Later (Summer) "So we don’t need shardings anymore." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (これでもうシャーディングは不要ですね) "What a peaceful world." >ʕ̡̢̡ʘ̅͟͜͡ ʘ̲̅ ʔ̢̡̢ (いい世の中になりました。)
  • 40. Another 3 Months Later (Summer) ʕº̫͡ºʔ< "..." ʕº̫͡ºʔo0O (...) ʕº̫͡ºʔo0O (I’ll go home and sleep, seriously.) (今日はもう帰って寝よう...)
  • 41. Another 3 Months Later (Summer) Chapter 1. “Tales of Sharding” ~ Fin ~
  • 42. 3. How to use Wizard?
  • 43. How to use Wizard? https://github.com/evalphobia/wizard
  • 44. How to use Wizard? https://github.com/evalphobia/wizard See README!!
  • 45. How to use Wizard? Chapter 2. “How to use” ~ Fin ~
  • 46. How to use Wizard? (Setup Engine) ● 1. Initialize New Engine - xorm.NewEngine(“mysql”, “user:pass@tcp(0.0.0.0:3306)/dbname”) ● 2. Create New Cluster - cluster := wizard.NewCluster(engine) // <= register master db - cluster.RegisterSlave(engine2) // <= register slave db ● 3. Add cluster to new wizard struct - w := wizard.NewWizard() - w.SetDefault(cluster) - ormEngine = wizardxorm.New(w)
  • 47. How to use Wizard? (Get Session) ● 4. Get Session from wizard - session, err := ormEngine.UseMasterSession(ctx, User{}) ● first arg is identifier. ○ Use same identifier, you can get same session. ○ Suit for a series of transaction during the request. ○ e.g.) context.Context, *http.Request ○ Anything is OK! ● second arg is struct for table. ● 5. Execute SQL - session.Sql("SELECT 1")
  • 48. Wizard What’s Wizard/Cluster/Engine? Cluster (User) DB Master M DB Slave1 S DB Slave2 S (Engine) (Engine) (Engine) Session Cluster (Article) DB Master M DB Slave1 S DB Slave2 S (Engine) (Engine) (Engine) Shard Cluster (Favorite) Shard #1 SSM Shard #2 SSM Shard #3 SSM ① ② ③ ④
  • 49. Code: Setup Engine import ( _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" "github.com/evalphobia/wizard" wizardORM "github.com/evalphobia/wizard/orm/xorm" )
  • 50. Code: Setup Engine var ormEngine wizardORM.ORM // Initialize database. (Ignore errors for simpleness) func initNormal() { // xorm's engine(DB handler) xormEngine, _ := xorm.NewEngine("mysql", "user:pass@tcp(0.0.0.0:3306)/dbname") // Create DB Cluster (argument is master DB) cluster := wizard.NewCluster(xormEngine) // Create wizard (DB and session handler) w := wizard.NewWizard() w.SetDefault(cluster) // set default cluster ormEngine = wizardxorm.New(w) }
  • 51. Code: Execute SQL #1 - select to user’s table // Get User from database. func sqlGetUser(ctx context.Context, userID int64) (User, error) { session, _ := ormEngine.UseMasterSession(ctx, &User{}) session.And("user_id = ?", id) session.ForUpdate() // Lock the row user := &User{} _, _ = session.Get(user) // return user } // => SELECT * FROM user WHERE user_id = userID FOR UPDATE;
  • 52. Code: Execute SQL #2 - update user’s table // Update user’s point func sqlUpdateUserPoint(ctx context.Context, userID int64, point int64) error { session, _ := ormEngine.UseMasterSession(ctx, &User{}) session.Cols("point") session.And("id = ?", userID) return session.Update(&User{ Point: point, }) } // => UPDATE user SET point = point WHERE user_id = userID;
  • 53. Code: Execute SQL #3 - update campaign table // Update campaign status of the user func sqlUpdateCampaignStatus(ctx context.Context, userID int64) error { session, _ := ormEngine.UseMasterSession(ctx, &Campaign{}) session.Cols("status") session.And("user_id = ?", userID) return session.Update(&Campaign{ Status: "end", }) } // => UPDATE campaign SET status = "end" WHERE user_id = userID;
  • 54. Code: Execute SQL - wrap up // UPDATE user’s point and complete campaign. func ExecSQL(ctx context.Context) { ormEngine.SetAutoTransaction(ctx, true) // start transaction when SQL is executed userID := 1 u, _ := sqlGetUser(ctx, userID) // => BEGIN and SELECT _ = sqlUpdateUserPoint(ctx, u.UserID, u.Point + 1) // => UPDATE user _ = sqlUpdateCampaignStatus(ctx, u.UserID) // => UPDATE campaign orm.SetAutoTransaction(ctx, false) err = ormEngine.CommitAll(ctx) // COMMIT ormEngine.CloseAll(ctx) // Close all sessions }
  • 55. How to use Shard? (Setup Engine) ● 1. Initialize New Engine - xorm.NewEngine(“mysql”, “user:pass@tcp(0.0.0.0:3306)/dbname”) ● 2. Create New Clusters - baseCluster := wizard.NewCluster(engine) - shardCluster01 := wizard.NewCluster(shardEngineA01) - shardCluster01.RegisterSlave(shardEngineA02) - shardCluster02 := wizard.NewCluster(shardEngineB01) - shardCluster02.RegisterSlave(shardEngineB02)
  • 56. How to use Shard? (Setup Shard Clusters) ● 3. Add clusters to new wizard struct - w := wizard.NewWizard() - w.SetDefault(baseCluster) - // ↑add normal cluster - shards := w.CreateShardCluster(&UserFavorite{}, 1021) - shards.RegisterShard(0, 510, shardCluster01) - shards.RegisterShard(511, 1020, shardCluster02) - // ↑add shards cluster - w.RegisterTables(shards, &UserLogin{}, ...) - // ↑add sharded tables - ormEngine = wizardxorm.New(w)
  • 57. How to use Shard? (Get Session) ● 4. Get Session from wizard - session, err := ormEngine.UseMasterSessionByKey(ctx, User{}, key) ● first arg is identifier. ● second arg is struct for table. ● third ard is shard key. ○ e.g. user_id ● 5. Execute SQL - session.Sql("SELECT 1") - // ↑ executed on one shard - ormEngine.FindParallelByCondition([]*User, condition) - // ↑ executed on all shards
  • 58. Wrap up ● Wizard is DB sharding/session management library ● Only supports xorm - Send PR! ● eureka use Wizard for production. ● Hard to test... - Data - DB instances - clusters of MySQL/PostgreSQL/etc...
  • 59. CONFIDENTIAL Thank you :) Thank you :) * Gopher designed by Hinako Sakawa - Original Gopher designed by Renee French. https://github.com/ohina