SlideShare a Scribd company logo
1 of 18
Download to read offline
appengine/datastore
パッケージを読む
〜または OSS への contribute について〜
1
自己紹介
名前 陣内 靖(じんのうち やすし)
アカウント @delphinus (GitHub) @delphinus35 (Twitter)
所属 株式会社ディー・エヌ・エー(DeNA)
AppEngine 歴 2年くらい
ハマってるもの Splatoon2。Switch 最近買った。
オクト・エキスパンジョン今日出ましたね!
2
今日のお題
対象者
● GAE/Go SE で Datastore 使ったことのある人。
● あんまり OSS に contribute とかしたことない人。
内容
● appengine/datastore パッケージのコードリーディング。
● BatchSize というニッチなメソッドを実装する際にやったこと。
○ https://github.com/golang/appengine/pull/90
3
一度に全 Entity を取得したい!
構造体のプロパティ追加・削除したいときとか……
Entity の数が多いとお金かかるからあんまりやりたくないのだけども。
(KeysOnly なら無料)
全部じゃなくても数千 Entity〜 の結果が変えるクエリなら同じ影響あり。
4
普通に GetAll してみると……(その1)
5
普通に GetAll してみると……(その2)
なんだか効率が悪い?
「問題」も指摘されている……
6
指摘を詳しく見てみると……
このリクエストを処理中に datastore.next() へのリモート プロシージャ コールが
249 回実行されました。クエリのバッチサイズとして 20 を指定していたことが原因と
考えられます。
いや、そんな指定した覚えはないしバッチサイズって何……?
クエリのバッチサイズの値を増やし、datastore.next() のコール回数を減らしてくださ
い。
どうやって? というか datastore.next() って何者?
7
appengine/datastore でクエリを処理するには
通常、クエリを発行した後に Entity を取り出すには2つの方法があります。
● q.GetAll(ctx, &entities) で一気に全部取り出す。
○ 変数 entities に全部突っ込まれる。
● t := q.Run() した後 t.Next() で一個ずつ取り出す。
○ メモリを余り消費しないので扱う Entity が多い場合はこちらが安心。
○ とはいえ、まさか一個取り出す度に Datastore にアクセスする訳ではないよね……?
■ ⇒読んでみるとなかなかスマートなロジックになっていた。
8
q.GetAll = q.Run + t.Next
実際のソースを読んでみると、両者は同じことを行っていることが分かります。
func GetAll(...) ([]*Key, error) {
...
for t := q.Run(c); ; {
k, e, err := t.next()
...
}
...
}
9
t.next() は何をやっている?
Datastore へのアクセスを極力少なくするようになっています。ソースはここ。
func (t *Iterator) next() (...) {
...
for (必要なときだけ真) {
callNext() ... // ←実際に Datastore へ RPC してるヤツ
}
// 予め上で取得しておいた Pool から 目当ての Entity を取り出す
return ...
}
10
callNext() が Datastore にアクセスする
ということが分かります。ソースはここ(修正前)。
ここで呼んでいる RPC が最初に出てきた datastore.next()。
一度に Get する数がバッチサイズだったのです。
でもバッチサイズを指定するための適当なメソッドがない……
11
ハックしてバッチサイズを指定可能にする
なんとレスポンスが
2分の1に!
12
初 contribute
これは他にも便利に思う人が多そう。
ひとまず issue 立ててみる。誰かこれで実装してくれるかな……
……と思ったけど、これくらいなら実装してみよう。
そもそもこんなマイナーなの誰も実装してくれないし。
⇒おそるおそる PR してみた。
13
BatchSize の指定の仕方
こんな風に指定できたら便利そう。
// BatchSize に適当な値を指定
q := datastore.NewQuery(“Hoge”).BatchSize(500)
// もちろん他の条件を追加しても OK
q := datastore.NewQuery(“Hoge”).BatchSize(500).
Filter(“Age >=”, 20).Order(“Age”)
コード例はここに置いています。
● https://github.com/delphinus/datastore-batchsize-test
14
ここからが長かった……
https://github.com/golang/appengine/pull/90
PR したのは 2017年9月。その後1ヶ月間、完全に放置される。
催促してみるが、またもや放置。
4ヵ月経ってレビューコメントが付くが、今度は僕が放置(気付かなかった)。
なんやかんやあってマージされたのは今年の5月。
15
結論:OSS への contribute は気長にね!
16
まとめ
動作が分からないときはソースを読もう!
なんかおかしいと思ったら気軽に contribute しよう!
無視されてもめげるな! それが通常運行だ!
17
18

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

appengine/datastore パッケージを読む