Share
Embed
Splitting DB on "Rails"
Presented on RejectKaigi 2018
https://speee.connpass.com/event/84915/
https://gist.github.com/izumin5210/8ebbf32a54dcb142d31cde9254ccd336
izumin5210
May 12, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
92 slides
-
Splitting DB on "Rails" How to split DB efficiently with
Ruby "magic" RejectKaigi 2018 12.May.2018 - @izumin5210 -
izumin5210 Engineer at Wantedly, Inc. Wantedly People ‣ Web Application
Engineer - Profile Data Strategy Group ‣ Interests in developer productivity on microservices Gopher, Rubyist, JavaScripter and Androider -
©2018 Wantedly, Inc. Background WHY did we need to split
the database? -
©2018 Wantedly, Inc. Background History of our apps 2012 ~
2016.01 ~ 2016.11 ~ -
©2018 Wantedly, Inc. Background History of our apps 2012 ~
2016.01 ~ 2016.11 ~ Monolithic Rails app -
©2018 Wantedly, Inc. Background History of our apps 2012 ~
2016.01 ~ 2016.11 ~ Monolithic Rails app 2 Rails apps (Main, Accounts) -
©2018 Wantedly, Inc. Background History of our apps The Go
gopher © Renée French, The Ruby logo © Yukihiro Matsumoto 2012 ~ 2016.01 ~ 2016.11 ~ Monolithic Rails app 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) -
©2018 Wantedly, Inc. Background Problems of our apps Monolithic Rails
app 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) profiles, working_histories, ... companies messages ... Main DB Read / Write -
©2018 Wantedly, Inc. Background Problems of our apps Monolithic Rails
app 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) profiles, working_histories, ... companies messages ... Main DB Read / Write GET on Kubernetes cluster -
©2018 Wantedly, Inc. Background Problems of our apps Monolithic Rails
app 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) profiles, working_histories, ... companies messages ... Main DB Read / Write GET on Kubernetes cluster -
©2018 Wantedly, Inc. Background Problems of our apps Monolithic Rails
app 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) profiles, working_histories, ... companies messages ... Main DB Read / Write GET on Kubernetes cluster ‣ ͦͦ͜͜େن ࡀ͘Β͍ ىಈ$*ʢଞʹൺΕʣ͘ͳ͖ͬͯͨ ‣ ଞͷαʔϏεͰ͔ͭ͏ʮϓϩϑΟʔϧใʯ͜͜ʹՃ͢Δͷ͔ʁ ‣ ͜ͷαʔϏεࣗମ͕ѻ͏ϢʔβɾτϥϑΟοΫ͕ͦͦଟ͍ ͳΔ͘ͳΒ͜͜Λܦ༝ͤͣϓϩϑΟʔϧΛऔಘ͍ͨ͠ -
©2018 Wantedly, Inc. Background Ideal architecture Monolithic Rails app 2
Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) Main DB Read / Write on Kubernetes cluster -
©2018 Wantedly, Inc. Background Ideal architecture Monolithic Rails app 2
Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Read / Write GET / UPDATE on Kubernetes cluster Profile DB Read / Write -
©2018 Wantedly, Inc. Background Ideal architecture Monolithic Rails app 2
Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Read / Write GET / UPDATE on Kubernetes cluster Profile DB Read / Write ‣ ͦͦ͜͜େن DPNNJUT JTTVFTBOEQVMMSFRT SPVUJOHT -
©2018 Wantedly, Inc. Background Ideal architecture Monolithic Rails app 2
Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Read / Write on Kubernetes cluster Profile DB Read / Write GET / UPDATE -
©2018 Wantedly, Inc. Background Ideal architecture Monolithic Rails app 2
Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Read / Write GET / UPDATE on Kubernetes cluster Profile DB Read / Write "DUJWF3FDPSE͕͑ͳ͘ͳΔ ͜͜ͷҠߦ͕Ұ൪Ωπ͍ -
©2018 Wantedly, Inc. Background Ideal architecture Monolithic Rails app 2
Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Read / Write on Kubernetes cluster Profile DB Read / Write GET / UPDATE ͍͖ͳΓཧΛٻΊΔͷݫ͍͠ʜ ஈ֊తʹҠߦͰ͖ͳ͍͔ʁ -
©2018 Wantedly, Inc. Background Ideal architecture - intermediate Monolithic Rails
app Main DB Profile DB Read / Write Read / Write 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) GET on Kubernetes cluster -
©2018 Wantedly, Inc. Background Ideal architecture - intermediate Monolithic Rails
app Main DB Profile DB Read / Write Read / Write 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) GET on Kubernetes cluster ͱΓ͋͑ͣ%#Λ͚Δͱ͜Ζ͔Β "DUJWF3FDPSEΛ͍ଓ͚ΒΕΔ -
©2018 Wantedly, Inc. Background Ideal architecture - intermediate ‣ ͦͦ͜͜େن
DPNNJUT JTTVFTBOEQVMMSFRT SPVUJOHT -
©2018 Wantedly, Inc. )PSJ[POUBM7FSUJDBM w *%˓˓˓˓Ҏ߱ͬͪ͜ͷ%#ʹʙ͜ͷลΓͷςʔϒϧΛผ%#ʹʙ w ࠓճޙऀ
'VODUJPO%PNBJO w 'VODUJPOFHಈըΞοϓϩʔυʹؔ࿈͢ΔςʔϒϧΛʙ w %PNBJOFHϒϩάαʔϏεʹؔ࿈͢ΔςʔϒϧΛʙ DB splitting -
©2018 Wantedly, Inc. )PSJ[POUBM7FSUJDBM 'VODUJPO%PNBJO w 'VODUJPOFHಈըΞοϓϩʔυʹؔ࿈͢ΔςʔϒϧΛʙ w
%PNBJOFHϒϩάαʔϏεʹؔ࿈͢ΔςʔϒϧΛʙ w $PSFEPNBJO αʔϏεͷ͋ΒΏΔͱ͜ΖͰར༻͞ΕΔɼαʔϏεͷ֩ʹ͍ۙςʔϒϧΛʙ FH6TFS "DDPVOU 1SPpMF FUD DB splitting -
©2018 Wantedly, Inc. ίΞΛΓग़͢͠͞ ‣ Ͳ͜·ͰΓग़͔͢ Abelongs_to :userAͳϞσϧɼ͍ͭ͋͘Δʁ ‣
Ͳ͏ͬͯΓग़͔͢ αʔϏεͷ͍ͨΔͱ͜ΖͰར༻͞Ε͍ͯΔ मਖ਼ՕॴɾӨڹൣғͷѲࠔ ‣ ίΞΛΓग़͍ͨ͠ͱߟ͑Δͷଟ͘ͷ߹ผͷαʔϏεΛ࡞͍ͬͯΔνʔϜ DB splitting -
©2018 Wantedly, Inc. ίΞΛΓग़͢͠͞ ‣ Ͳ͜·ͰΓग़͔͢ ‣ Ͳ͏ͬͯΓग़͔͢ ‣ ίΞΛΓग़͍ͨ͠ͱߟ͑Δͷଟ͘ͷ߹ผͷαʔϏεΛ࡞͍ͬͯΔνʔϜ
DB splitting ޮతɾ࣮֬ʹίΞυϝΠϯΓग़͠ΛΓ͛ΔͨΊͷϨʔϧ͕ඞཁ -
©2018 Wantedly, Inc. Splitting DB on "Rails" How to split
DB efficiently with Ruby "magic" -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ "ϢʔβϓϩϑΟʔϧ 1SPpMFNPEFM class Profile
< ApplicationRecord # ... end -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ class Profile < ApplicationRecord
has_many :working_histories end class WorkingHistory < ApplicationRecord belongs_to :company end class Company < ApplicationRecord end class Post < ApplicationRecord has_one :profile end -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ class Profile < ApplicationRecord
has_many :working_histories end class WorkingHistory < ApplicationRecord belongs_to :company end class Company < ApplicationRecord end class Post < ApplicationRecord has_one :profile end ย͍ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ class Profile < ApplicationRecord
has_many :working_histories end class WorkingHistory < ApplicationRecord belongs_to :company end class Company < ApplicationRecord end class Post < ApplicationRecord has_one :profile end ยΘΕ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ class Profile < ApplicationRecord
has_many :working_histories end class WorkingHistory < ApplicationRecord belongs_to :company end class Company < ApplicationRecord end class Post < ApplicationRecord has_one :profile end ย͍ͷย͍ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ # activerecord/lib/active_record/associations.rb in rails/rails
def has_many(name, scope = nil, **options, &extension) reflection = Builder::HasMany.build(self, name, scope, options, &extension) Reflection.add_reflection self, name, reflection end -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ # activerecord/lib/active_record/associations.rb in rails/rails
def has_many(name, scope = nil, **options, &extension) reflection = Builder::HasMany.build(self, name, scope, options, &extension) Reflection.add_reflection self, name, reflection end -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ [2] pry(main)> Profile.methods.grep(/reflect/) =>
[:_reflections, :aggregate_reflections, :aggregate_reflections=, :aggregate_reflections?, :_reflections=, :_reflections?, :reflect_on_aggregation, :reflect_on_all_aggregations, :_reflect_on_association, :reflect_on_all_associations, :clear_reflections_cache, :reflections, :reflect_on_association, :reflect_on_all_autosave_associations] -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ [1] pry(main)> Profile.methods.grep(/reflect/) =>
[:_reflections, :aggregate_reflections, :aggregate_reflections=, :aggregate_reflections?, :_reflections=, :_reflections?, :reflect_on_aggregation, :reflect_on_all_aggregations, :_reflect_on_association, :reflect_on_all_associations, :clear_reflections_cache, :reflections, :reflect_on_association, :reflect_on_all_autosave_associations] -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ [2] pry(main)> Profile.reflect_on_all_associations.map(&:table_name).uniq =>
["users", # ... "academic_records", "working_histories", # ... "posts", #... ] ย͍Ұཡ͕औΕͨʂ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ ApplicationRecord.descendants .reject(&:abstract_class?) .flat_map(&:reflect_on_all_associations) .select
{ |r| r.table_name == 'profiles' } .map(&:active_record) .map(&:table_name) ยΘΕҰཡಉ͡Α͏ʹͯ͠औΕΔʂ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ ͋ΔϞσϧʹؔ࿈ΛͭϞσϧ ‣ Areflect_on_all_associationsAΛར༻
͋ΔϞσϧ͕ؔ࿈Λ࣋ͭϞσϧΛऔಘͰ͖Δ ٯ͖Εย͍ɼยΘΕؚΊ ͯ͢ݟ͔ͭΔʂ from = ApplicationRecord.descendants .reject(&:abstract_class?) .flat_map(&:reflect_on_all_associations) .select { |r| r.table_name == 'profiles' } to = Profile.reflect_on_all_associations (from + to).map(&:table_name) -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ ApplicationRecord.descendants .reject(&:abstract_class?) .flat_map(&:reflect_on_all_associations) .select
{ |r| r.table_name == 'profiles' } .map(&:active_record) .map(&:table_name) .uniq [ "users", "posts", "academic_records", # ... "working_histories", # ... ] # => 18 tables -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ ApplicationRecord.descendants .reject(&:abstract_class?) .flat_map(&:reflect_on_all_associations) .select
{ |r| r.table_name == 'profiles' } .map(&:active_record) .map(&:table_name) .uniq [ "users", "posts", "academic_records", # ... "working_histories", # ... ] # => 18 tables ͜ͷΜϓϩϑΟʔϧ ʢ6*తʹϓϩϑΟʔϧը໘ʹग़ͯ͘Δใʣ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ ApplicationRecord.descendants .reject(&:abstract_class?) .flat_map(&:reflect_on_all_associations) .select
{ |r| r.table_name == 'profiles' } .map(&:active_record) .map(&:table_name) .uniq [ "users", "posts", "academic_records", # ... "working_histories", # ... ] # => 18 tables ͜ΕΒϓϩϑΟʔϧʁ ʢQPTUTϢʔβߘςʔϒϧʣ -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ from = ApplicationRecord.descendants .reject(&:abstract_class?)
.flat_map(&:reflect_on_all_associations) .select { |r| r.table_name == 'profiles' } to = Profile.reflect_on_all_associations (from + to) .map(&:table_name) .reject { |n| excluded_table_names.include? n } ผυϝΠϯͬΆ͍ͷΓग़͠ର͔Β֎͢ ʢࠓճͩͱձࣾใϢʔβߘ͕֘ʣ [ "academic_records", # ... "working_histories", # ... ] -
©2018 Wantedly, Inc. DB splitting Ͳ͜·ͰΓग़͔͢ 2 1SPpMFʹؔ࿈Λ࣋ͭNPEFMͲΕʁ " Areflect_on_all_associationsAͰػցతʹ୳͢
ผυϝΠϯͬΆ͍ͷআ֎ ࣍ɼ࣍ͷBTTPDJBUJPO͘Β͍·ͰݟͯɼαʔϏεʹৄ͍͠ਓͱ͑߹Θͤ͢Δ ˞࣮ࡍʹͬͨεΫϦϓτIUUQTHJTUHJUIVCDPNJ[VNJOFCCGBEDCEDEFDDE -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true end class Profile < ApplicationRecord # ... end class WorkingHistory < ApplicationRecord # ... end -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class ProfileDb::Base < ApplicationRecord
self.abstract_class = true db_config = YAML::load(ERB.new(IO.read('config/profile_database.yml')).result) establish_connection(db_config[Rails.env]) end class ProfileDb::Profile < ProfileDb::Base # ... end class ProfileDb::WorkingHistory < ProfileDb::Base # ... end "DUJWF3FDPSEͰଓઌΛม͑ΔΑ͋͘Δύλʔϯ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class ProfileDb::Base < ApplicationRecord
self.abstract_class = true db_config = YAML::load(ERB.new(IO.read('config/profile_database.yml')).result) establish_connection(db_config[Rails.env]) end class ProfileDb::Profile < ProfileDb::Base # ... end class ProfileDb::WorkingHistory < ProfileDb::Base # ... end ৽͘͠ϕʔεΫϥεΛ࡞ͬͯɼܧঝͤ͞Δ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class ProfileDb::Base < ApplicationRecord
self.abstract_class = true db_config = YAML::load(ERB.new(IO.read('config/profile_database.yml')).result) establish_connection(db_config[Rails.env]) end class ProfileDb::Profile < ProfileDb::Base # ... end class ProfileDb::WorkingHistory < ProfileDb::Base # ... end ϞδϡʔϧΛΔʁ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class ProfileRecord < ApplicationRecord
self.abstract_class = true db_config = YAML::load(ERB.new(IO.read('config/profile_database.yml')).result) establish_connection(db_config[Rails.env]) end class Profile < ProfileRecord # ... end class WorkingHistory < ProfileRecord # ... end ϞδϡʔϧΛΒͳ͍ʁ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ .PEVMFΛΔʁ ‣ Δʁ
ίʔυతʹʮҟͳΔυϝΠϯͰ͋Δ͜ͱͷද໌ʯ Ahas_manyAͷڍಈͱ͔ؾΛ͚ͭΔඞཁΞϦ ‣ Βͳ͍ʁ ϕʔεΫϥεม͑Δ͚ͩͳͷͰɼมߋ͘͢ͳ͍ ίʔυ্Ͱҧ͏%#Λࢀর͍ͯ͠Δ͜ͱ͕ෆ໌ྎ class ProfileDb::WorkingHistory < ProfileDb::Base # ... end # or class WorkingHistory < ProfileRecord # ... end -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ .PEVMFΛΔʁ ‣ Δʁ
ίʔυతʹʮҟͳΔυϝΠϯͰ͋Δ͜ͱͷද໌ʯ Ahas_manyAͷڍಈͱ͔ؾΛ͚ͭΔඞཁΞϦ ‣ ࠓճΒͳ͍ʂ ϕʔεΫϥεม͑Δ͚ͩͳͷͰɼมߋ͘͢ͳ͍ ίʔυ্Ͱҧ͏%#Λࢀর͍ͯ͠Δ͜ͱ͕ෆ໌ྎ class ProfileDb::WorkingHistory < ProfileDb::Base # ... end # or class WorkingHistory < ProfileRecord # ... end -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class User < ApplicationRecord
has_one :profile end class Profile < ProfileRecord has_many :working_histories end class WorkingHistory < ProfileRecord end ͔Μ͖ʜʁ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ class User < ApplicationRecord
has_one :profile end class Profile < ProfileRecord has_many :working_histories end class WorkingHistory < ProfileRecord end ͜ΕԿߟ͑ͣʹಈ͘ͷ͔ʁ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ User.joins(profile: :working_histories).to_a User.preload(profile: :working_histories).to_a
User.eager_load(profile: :working_histories).to_a User.includes(profile: :working_histories).to_a User.includes(:profile).where(profile: { location: 'Tokyo' }).to_a ΫΠζಈ͘ͷͲΕͰ͠ΐ͏ʢprofilesͱworking_histories͕ҧ͏%#ʣ -
©2018 Wantedly, Inc. DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ User.joins(profile: :working_histories).to_a User.preload(profile: :working_histories).to_a
User.eager_load(profile: :working_histories).to_a User.includes(profile: :working_histories).to_a User.includes(:profile).where(profile: { location: 'Tokyo' }).to_a %#Λ·͍ͨͩ+0*/͕Δͱμϝ 1SFMPBEJOHʹؔͯ͠ࢀর͢Δ%#͕ҧ͍ͬͯͯਖ਼͘͠ಈ͘ ˞ݕূϦϙδτϦIUUQTHJUIVCDPNBXBLJBEPVCMFEC 3BJMTͰಈ࡞֬ೝ -
©2018 Wantedly, Inc. User.joins(profile: :working_histories).to_a User.preload(profile: :working_histories).to_a User.eager_load(profile: :working_histories).to_a User.includes(profile:
:working_histories).to_a User.includes(:profile).where(profile: { location: 'Tokyo' }).to_a DB splitting ࠷খͷมߋͰࡁ·ͨ͢Ίʹ %#Λ·͍ͨͩ+0*/͕Δͱμϝ 1SFMPBEJOHʹؔͯ͠ࢀর͢Δ%#͕ҧ͍ͬͯͯਖ਼͘͠ಈ͘ ٯʹݴ͏ͱɼ+0*/͑͞ճආͰ͖ΕͪΌΜͱಈ࡞͢Δʂ -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ ΞάϨογϒʹϩάऩू ඞࡴPQFODMBTT ‣ "DUJWF3FDPSEʹɼΫΤϦ࣮ߦ࣌ʹઈରݺΕΔϝιου͕ଘࡏ͢Δ
ͦͷλΠϛϯάͰ+0*/͞Εͦ͏ͳΒϩάΛ͢ DPOUSPMMFS BDUJPO DBMMFS ؔ࿈͍ͯ͠Δςʔϒϧ FUD ‣ Ͳͷςʔϒϧ͕KPJOTJODMVEFTFBHFS@MPBE͞Ε͔ͨΫΤϦϏϧμ͕͍ͬͯΔ -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ caller_app = caller.find do
|c| !c.to_s.start_with?(Bundler.bundle_path.to_s) && c.to_s.start_with?(Rails.root.to_s) end info = { caller: caller_app || "", table: table.name, joins: flatten_recursively(joins_values), left_outer_joins: flatten_recursively(left_outer_joins_values), eager_load: flatten_recursively(eager_load_values), includes: flatten_recursively(includes_values), joined_includes: flatten_recursively(joined_includes_values), references_eager_loaded: flatten_recursively(references_eager_loaded_tables), request_controller: $request_controller, request_action: $request_action, } TD.event.post("join_queries", info) +0*/MPHHFS -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ caller_app = caller.find do
|c| !c.to_s.start_with?(Bundler.bundle_path.to_s) && c.to_s.start_with?(Rails.root.to_s) end info = { caller: caller_app || "", table: table.name, joins: flatten_recursively(joins_values), left_outer_joins: flatten_recursively(left_outer_joins_values), eager_load: flatten_recursively(eager_load_values), includes: flatten_recursively(includes_values), joined_includes: flatten_recursively(joined_includes_values), references_eager_loaded: flatten_recursively(references_eager_loaded_tables), request_controller: $request_controller, request_action: $request_action, } TD.event.post("join_queries", info) +0*/MPHHFS ݺͼग़͠ݩΞϓϦέʔγϣϯίʔυΛ୳͢ʢOPUHFNʣ -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ caller_app = caller.find do
|c| !c.to_s.start_with?(Bundler.bundle_path.to_s) && c.to_s.start_with?(Rails.root.to_s) end info = { caller: caller_app || "", table: table.name, joins: flatten_recursively(joins_values), left_outer_joins: flatten_recursively(left_outer_joins_values), eager_load: flatten_recursively(eager_load_values), includes: flatten_recursively(includes_values), joined_includes: flatten_recursively(joined_includes_values), references_eager_loaded: flatten_recursively(references_eager_loaded_tables), request_controller: $request_controller, request_action: $request_action, } TD.event.post("join_queries", info) +0*/MPHHFS KPJOT FBHFS@MPBE ͷҾϦετ -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ caller_app = caller.find do
|c| !c.to_s.start_with?(Bundler.bundle_path.to_s) && c.to_s.start_with?(Rails.root.to_s) end info = { caller: caller_app || "", table: table.name, joins: flatten_recursively(joins_values), left_outer_joins: flatten_recursively(left_outer_joins_values), eager_load: flatten_recursively(eager_load_values), includes: flatten_recursively(includes_values), joined_includes: flatten_recursively(joined_includes_values), references_eager_loaded: flatten_recursively(references_eager_loaded_tables), request_controller: $request_controller, request_action: $request_action, } TD.event.post("join_queries", info) +0*/MPHHFS def flatten_recursively(arr) arr.flat_map do |i| case i when Hash then hash_to_array(i) when Array then flatten_recursively(i) when Arel::Nodes::Node then i.to_sql when ActiveRecord::Associations::JoinDependency then join_dependency_to_table_names(i) else i.to_s end end end def hash_to_array(h) h.keys + (h.values.flat_map { |v| case v when Hash then hash_to_array(v) when Array then flatten_recursively(v) when Arel::Nodes::Node then v.to_sql when ActiveRecord::Associations::JoinDependency then join_dependency_to_table_names(v) else v.to_s end }) end def join_dependency_to_table_names(jd) jd.aliases.instance_variable_get(:@tables).map { |a| a.table.table_name } end flatten_recursively IVNBOSFBEBCMFͳTUSJOHʹ͢ΔͨΊͷIFMQFS -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ caller_app = caller.find do
|c| !c.to_s.start_with?(Bundler.bundle_path.to_s) && c.to_s.start_with?(Rails.root.to_s) end info = { caller: caller_app || "", table: table.name, joins: flatten_recursively(joins_values), left_outer_joins: flatten_recursively(left_outer_joins_values), eager_load: flatten_recursively(eager_load_values), includes: flatten_recursively(includes_values), joined_includes: flatten_recursively(joined_includes_values), references_eager_loaded: flatten_recursively(references_eager_loaded_tables), request_controller: $request_controller, request_action: $request_action, } TD.event.post("join_queries", info) +0*/MPHHFS 6OJDPSOͩͬͨͷͰࡶʹάϩʔόϧมʹ ʢDBMMFS͕Θ͔Ε֓Ͷͳ͍ʣ -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ caller_app = caller.find do
|c| !c.to_s.start_with?(Bundler.bundle_path.to_s) && c.to_s.start_with?(Rails.root.to_s) end info = { caller: caller_app || "", table: table.name, joins: flatten_recursively(joins_values), left_outer_joins: flatten_recursively(left_outer_joins_values), eager_load: flatten_recursively(eager_load_values), includes: flatten_recursively(includes_values), joined_includes: flatten_recursively(joined_includes_values), references_eager_loaded: flatten_recursively(references_eager_loaded_tables), request_controller: $request_controller, request_action: $request_action, } TD.event.post("join_queries", info) +0*/MPHHFS 5SFBTVSF%BUBʹه -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ select caller_line, collect_set(table) as
base_tables, collect_set(t) as joined_tables_or_queries, collect_set(class) as classes, collect_set(request) as requests, collect_set(caller) as callers from ( select *, substring_index(caller, ':', 2) as caller_line, concat(request_controller, '#', request_action) as request from join_queries lateral view explode(joins) tmp as t where td_time_range(time, '2017-11-08 00:00:00', '2017-11-15 00:00:00') and ( ( table not in ('profiles', ..., 'working_histories', ...) and ( t = 'profile' or instr(t, 'profiles') != 0 -- ... or t = 'working_history' or instr(t, 'working_histories') != 0 -- ... ) ) or -- ... ) order by time desc ) queries group by caller_line ‣ ͬ͟ͱूܭ͢Δ DBMMFSͰHSPVQCZ +0*/ઌͱݩͷͲͪΒ͔͚ͩʹׂର͕ೖ͍ͬͯΔΫΤϦ ‣ ଟগࡶͰ0, ޡݕग़࠷ѱͳ͍ʢࣗͷνΣοΫର͕૿͑Δ͚ͩʣ ࿙Ε͕ແ͍Α͏ʹؾΛ͚ͭͯ42-ॻ͘ -
©2018 Wantedly, Inc. DB splitting ӨڹՕॴΛݟಀ͞ͳ͍ ‣ ूܭ݁ՌΛ$47ग़ྗˠBXLͱ͔ͰνΣοΫϘοΫεʹ ‣ ͋ͱػցతʹ௵͍ͯ͘͠
includes preload joins eager_load FHASELECT idAͱAWHERE id in (?, ?, ...)AʹΘ͚Δ ‣ ͍͍ͩͨࠜੑͱ3VCZྗͰ͚ͩղܾͰ͖Δ ͋ͱɼ΄Μͷগ͠ͷ42-ྗ -
©2018 Wantedly, Inc. DB splitting ·ͱΊͲ͏ͬͯΓग़͔͢ ࢀর͢Δ%#Λม͑Δʹʜ ϕʔεΫϥε͚ͩมߋ͢Ε0,ʂ ϕʔεΫϥεͷมߋ͚ͩͰਖ਼͘͠ಈ࡞ͤ͞ΔͨΊʹʜ %#Λ·͙ͨ+0*/͚࣮ͩ֬ʹճආ͢Δʂ
%#Λ·͙ͨ+0*/Λճආ͢ΔͨΊʹʜ ΫΤϦͷϩάΛऔͬͯɼ͋Ϳͳ͍+0*/Λ୳ͯ͠௵͢ʂ -
©2018 Wantedly, Inc. DB splitting ·ͱΊͲ͏ͬͯΓग़͔͢ ࢀর͢Δ%#Λม͑Δʹʜ ϕʔεΫϥε͚ͩมߋ͢Ε0,ʂ ϕʔεΫϥεͷมߋ͚ͩͰਖ਼͘͠ಈ࡞ͤ͞ΔͨΊʹʜ %#Λ·͙ͨ+0*/͚࣮ͩ֬ʹճආ͢Δʂ
%#Λ·͙ͨ+0*/Λճආ͢ΔͨΊʹʜ ΫΤϦͷϩάΛऔͬͯɼ͋Ϳͳ͍+0*/Λ୳ͯ͠௵͢ʂ 3VCZͷྗͰ γεςϚνοΫʹղܾͰ͖ͨ -
©2018 Wantedly, Inc. Migration process ϕʔεΫϥεΛ࡞ɾܧঝ PROFILE_DATABASE_URL.BJO%#ʹ͚ͨ $*্ͰPROFILE_DATABASE_URLΛҧ͏%#ʹ
͚ͯςετ௨͢ ͖ͬ͞ͷνΣοΫϘοΫεΛຒΊ͍ͯ͘ ProfileRecord ApplicationRecord MainDB ProfileRecord ApplicationRecord MainDB Profile DB Local / CI QA / Prod -
©2018 Wantedly, Inc. Migration process ςετ͕ͱ͓Δ 2"Γସ͑ J
ԼهͷҠߦϓϩηεͷϦϋʔαϧ JJ όάνΣοΫ 1SPEΓସ͑ J શαʔϏεΛϝϯςφϯεϞʔυʹ JJ .BJO%#ͷϦʔυϨϓϦΧΛঢ֨ JJJ 1SPpMF3FDPSEͷ͖Λม͑Δ JW αʔϏεΠϯ ProfileRecord ApplicationRecord MainDB ProfileRecord ApplicationRecord MainDB Profile DB Local / CI QA / Prod -
©2018 Wantedly, Inc. Migration process ςετ͕ͱ͓Δ 2"Γସ͑ J
ԼهͷҠߦϓϩηεͷϦϋʔαϧ JJ όάνΣοΫ 1SPEΓସ͑ J શαʔϏεΛϝϯςφϯεϞʔυʹ JJ .BJO%#ͷϦʔυϨϓϦΧΛঢ֨ JJJ 1SPpMF3FDPSEͷ͖Λม͑Δ JW αʔϏεΠϯ ProfileRecord ApplicationRecord MainDB ProfileRecord ApplicationRecord MainDB Profile DB Local / CI QA / Prod ϓϩϑΟʔϧใͷσʔλҠߦ ϦεΫίετߴ͍ͷͰແఀࢭҠߦ͠ͳ͍ -
©2018 Wantedly, Inc. Future works ŘŵŠŠ Monolithic Rails app Main
DB Profile DB Read / Write Read / Write 2 Rails apps (Main, Accounts) Microservices (Rails, Golang, Python) GET on Kubernetes cluster -
©2018 Wantedly, Inc. Future works Monolithic Rails app 2 Rails
apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Profile DB Read / Write Read / Write Read Update -
©2018 Wantedly, Inc. Future works Monolithic Rails app 2 Rails
apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Profile DB Read / Write Read / Write Read Update ॻ͖ࠐΈͯ͢QSPpMFTFSWJDFʹΑͤΔ ʢσʔλͷ߹ੑΛ࣮֬ʹʣ -
©2018 Wantedly, Inc. Future works Monolithic Rails app 2 Rails
apps (Main, Accounts) Microservices (Rails, Golang, Python) ? User/Profile service Main DB Profile DB Read / Write Read / Write Read Update ಡΈࠐΈʹؔͯ͠ڐ༰͢Δ ʢ"DUJWF3FDPSEΛണ͕͢ίετ͕େ͖͗͢Δʣ