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

自社サービスのDjangoを 1.3から1.11(LTS)に アップグレードするまでの道のり

自社サービスのDjangoを 1.3から1.11(LTS)に アップグレードするまでの道のり

DjangoCongress JP 2019
https://djangocongress.jp/

Toshikazu Ohashi

May 18, 2019
Tweet

More Decks by Toshikazu Ohashi

Other Decks in Programming

Transcript

  1. 12 ELB NAT GW MYSQL Aurora EC2 EC2 ΠϕϯτऩूAPI EC2

    ΤϯυϢʔβAPI ؅ཧAPI EC2 όονॲཧ ؅ཧը໘ ഑৴γεςϜ ELB ELB
  2. ຊ೔ͷλʔήοτΰʔϧ w λʔήοτ w ݹ͍%KBOHPΛ࢖ͬͯࠔ͍ͬͯΔਓ w ۤ࿑࿩͕ฉ͖͍ͨਓ w ΰʔϧ w

    ࣮ྫΛ౿·͑ͯͷ࣍ͷ̎఺Λ఻͑Δ w ΞοϓάϨʔυதʹ৺͕͚ͯΑ͔ͬͨ͜ͱ w ΞοϓάϨʔυதʹ৺͕͚Δ΂͖ͩͬͨ͜ͱ 18
  3. ΞδΣϯμ  ͳͥΞοϓάϨʔυ͕ඞཁ͔ߟ͑Δ  ΰʔϧʹ͍͖ͳΓ޲͔Θͳ͍  ඞཁͳ΋ͷ͚ͩΛΞοϓάϨʔυ͢Δ  αΠΫϧΛߴ଎Խ͢Δ 

    %KBOHPͷΞοϓάϨʔυ಺༰Λཧղ͢Δ  पΓͷϓϩϑΣογϣφϧΛཔΔ  ΞοϓάϨʔυ͸ऴΘΓͰ͸ͳ͍ 20
  4. SEύʔςΟϥΠϒϥϦ͕ ରԠ͍ͯ͠ͳ͍ 31 ϥΠϒϥϦ ରԠόʔδϣϯ django-rest-framework Django ≧ 1.11 django-debug-toolbar

    Django ≧ 1.11 django-extensions Django ≧ 1.11 django-security Django ≧ 1.8 django-import-export Django ≧ 2.0
  5. ೥Ҏ্ͷࡀ݄ 41 Django 1.3.7 Django 1.11.18 2013/02/20 Release 2019/01/04 Release

    ϦϦʔεճ਺ ϚΠφʔόʔδϣϯΞοϓ: 8ճ ύον: 114ճ
  6. ໰୊͕͋ͬͨΒ੾Γ໭͠ 52 ELB EC2 1.3 EC2 1.3 EC2 1.3 EC2

    1.3 EC2 1.8 Τϥʔ͓ͯ͜͠ ͝ΊΜͳ͍
  7. Ͱ͸NJHSBUF͢Ε͹ 59 EC2 1.3 EC2 1.3 EC2 1.3 EC2 1.8

    MYSQL django.auth Table Django 1.8
  8. ϚΠάϨʔγϣϯ಺༰Λௐࠪ 62 auth [ ] 0001_initial [ ] 0002_alter_permission_name_max_length [

    ] 0003_alter_user_email_max_length [ ] 0004_alter_user_username_opts [ ] 0005_alter_user_last_login_null [ ] 0006_require_contenttypes_0002 $ python manage.py showmigrations
  9. ൃߦ42-Λௐࠪ͢Δ 63 BEGIN; CREATE TABLE `auth_permission` ( `id` integer AUTO_INCREMENT

    NOT NULL PRIMARY KEY, `name` varchar(50) NOT NULL, `content_type_id` integer NOT NULL, `codename` varchar(100) NOT NULL, UNIQUE (`content_type_id`, `codename`) ); ... $ python manage.py sqlmigrate auth 0001_initial
  10. ͪΐ͏Ͳ͍͍ͱ͜·Ͱ ϚΠάϨʔγϣϯ 64 auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length

    [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [ ] 0006_require_contenttypes_0002 $ python manage.py migrate auth 0001_initial --fake $ python manage.py migrate auth 0005_alter_user_last_login_null $ python manage.py showmigrations
  11. ࢒ͬͨ෦෼͸ શ෦ϦϦʔε͔ͯ͠Β 65 auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length

    [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [ ] 0006_require_contenttypes_0002
  12. 75 ELB NAT GW MYSQL Aurora EC2 EC2 EC2 ΤϯυϢʔβAPI

    ؅ཧAPI EC2 όονॲཧ ؅ཧը໘ ഑৴γεςϜ ELB ELB
  13. ؅ཧ"1*ʹ͸ چόʔδϣϯ͕͋ͬͨ 76 MYSQL ؅ཧAPI چόʔδϣϯ API &؅ཧը໘ چόʔδϣϯ σʔλ

    ݱόʔδϣϯ API ݱόʔδϣϯ σʔλ چ഑৴ γεςϜ ৽഑৴ γεςϜ چVerސ٬ ݱVerސ٬
  14. ݶΒΕͨҰ෦ͷސ٬ͷΈ͕ ࢖༻͍ͯͨ͠ 77 MYSQL ؅ཧAPI چόʔδϣϯ API &؅ཧը໘ چόʔδϣϯ σʔλ

    ݱόʔδϣϯ API ݱόʔδϣϯ σʔλ چ഑৴ γεςϜ ৽഑৴ γεςϜ چVerސ٬ ݱVerސ٬
  15. 86

  16. ࡟ݮͷ݁Ռ 97 Files Lines Code Comments Blanks ࡟ݮલ 431 71835

    57995 3798 10042 ࡟ݮޙ 293 50226 40782 2456 6988 ࠩ෼ -138 -21609 -17213 -1342 -3054 ࡟ݮޙͷൺ཰ 67.98% 69.92% 70.32% 64.67% 69.59% Pythonίʔυྔ͕70%ఔ౓·Ͱ཈͑ΒΕͨ
  17. ΞοϓάϨʔυͷ มߋ఺Λௐ΂Δ w ެࣜʹ͸֤%KBOHPόʔδϣϯͷ࠷ऴϦϦʔεϊʔτΛಡ Ίͱॻ͍ͯ͋Δ w %KBOHPͷ3FMFBTF/PUF͸ඇৗʹ༏ल w ֤όʔδϣϯΞοϓͷ಺༰Λաෆ଍ͳ͘໢ཏͰ͖Δ 108

    > Read the release notes for each ‘final’ release from the one after your current Django version, up to and including the version to which you plan to upgrade.
  18. ໰୊ൃੜ w Ͱ͸ൃੜ͠ͳ͍σουϩοΫ͕Ͱ͸ൃੜ͢Δ 114 OperationalError: (1213, 'Deadlock found when trying

    to get lock; try restarting transaction') w %#ͷΞΠιϨʔγϣϯϨϕϧ͕3&1&"5"#-&3&"%ͩ ͱ
  19. ࣮ࡍʹൃߦ͞Ε͍ͯΔΫΤϦ 117 BEGIN UPDATE `hoge` SET `foo` = 3, `bar`

    = 2310, WHERE `hoge_id` = 85524878 INSERT INTO `hoge` (`foo`, `bar`) VALUES (85524878, 3, 2310, 914) BEGIN UPDATE `hoge` SET `foo` = 3, `bar` = 2310, WHERE `hoge_id` = 85524879 INSERT INTO `hoge` (`foo`, `bar`) VALUES (85524879, 3, 2310, 914) SessionA SessionB DeadLock DeadLock
  20. ࣮ࡍʹൃߦ͞Ε͍ͯΔΫΤϦ 118 BEGIN UPDATE `hoge` SET `foo` = 3, `bar`

    = 2310, WHERE `hoge_id` = 85524878 INSERT INTO `hoge` (`foo`, `bar`) VALUES (85524878, 3, 2310, 914) BEGIN UPDATE `hoge` SET `foo` = 3, `bar` = 2310, WHERE `hoge_id` = 85524879 INSERT INTO `hoge` (`foo`, `bar`) VALUES (85524879, 3, 2310, 914) SessionA SessionB DeadLock DeadLock ৄ͘͠͸ωΫετΩʔϩοΫͰ ௐ΂ͯΈ͍ͯͩ͘͞
  21. .:42-ͷϩά͔Β ର৅ςʔϒϧΛ֬ೝ 121 mysql> SHOW ENGINE INNODB STATUS 2019-01-16 09:33:07

    2acb1339d700 *** (1) TRANSACTION: TRANSACTION 45525172536, ACTIVE 0 sec inserting mysql tables in use 1, locked 1 LOCK WAIT 14 lock struct(s), heap size 2936, 6 row lock(s), undo log entries 4 MySQL thread id 210690, OS thread handle 0x2acb433de700, query id 1591163 ec2-52-193-113-252.ap-northeast-1.compute.amazonaws.com 52.193.113.252 popinfo update INSERT INTO `delivery_indexedmessage` (`infobase_id`, `delivery_state`, `operator_id`, `shop_id`) VALUES (85524410, 3, 2310, 914) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 965 page no 274660 n bits 488 index `PRIMARY` of table `popinfo`.`delivery_indexedmessage` trx id 45525172536 lock_mode X insert intention waiting Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; *** (2) TRANSACTION: TRANSACTION 45525172535, ACTIVE 0 sec inserting mysql tables in use 1, locked 1 14 lock struct(s), heap size 2936, 6 row lock(s), undo log entries 4 MySQL thread id 210689, OS thread handle 0x2acb1339d700, query id 1591164 ec2-52-193-113-252.ap-northeast-1.compute.amazonaws.com 52.193.113.252 popinfo update INSERT INTO `delivery_indexedmessage` (`infobase_id`, `delivery_state`, `operator_id`, `shop_id`) VALUES (85524409, 3, 2310, 914) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 965 page no 274660 n bits 488 index `PRIMARY` of table `popinfo`.`delivery_indexedmessage` trx id 45525172535 lock_mode X Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 965 page no 274660 n bits 488 index `PRIMARY` of table `popinfo`.`delivery_indexedmessage` trx id 45525172535 lock_mode X insert intention waiting Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;;
  22. ൃߦ42-Λ֬ೝ 122 sudo tcpdump -i eth0 -s 0 -w -

    port 3306 | strings SELECT ... UPDATE ... Djangoαʔόʹೖͬͯtcpdump͔Βൃߦ͍ͯ͠ΔSQLΛऔಘ͢Δ
  23. ରԠํ๏ w ΧελϜϛυϧ΢ΣΞΛ௥Ճ͢Δ͜ͱͰɺҎલͷڍಈΛҡ ࣋Ͱ͖Δ 135 class LocationHeaderFix(object): def process_response(self, request,

    response): if 'Location' in response: response['Location'] = request.build_absolute_uri(response['Location']) return response
  24. มߋલ 140 ސ٬αʔό ؅ཧAPI Django 1.8 αʔό https://nyan.jp/add { "title":

    "ೣ" "contents": " " } OK contents = forms.CharField( required=True, )
  25. มߋޙ 142 ސ٬αʔό ؅ཧAPI Django 1.11 αʔό https://nyan.jp/add { "title":

    "ೣ" "contents": " " } όϦσʔγϣϯΤϥʔ Form { "title": "ೣ" "contents": "" }
  26. %KBOHPʹ͸ਪ঑͞ΕΔ 1ZUIPOͷόʔδϣϯ͕͋Δ w %KBOHPެࣜΛݟͯΈΔͱ্هͷදه w ݱঢ়1ZUIPOͳͷͰਪ঑όʔδϣϯͰ͸ͳ͍ 148 > Since newer

    versions of Python are often faster, have more features, and are better supported, all else being equal, we recommend that you use the latest 2.x.y or 3.x.y release. 8IBU1ZUIPOWFSTJPOTIPVME*VTFXJUI%KBOHP
  27. ΛͿΒԼ͛ͨ ΠϯελϯεͷϨΠςϯγ͕ٸ্ঢ 150 NAT GW MYSQL Aurora EC2 EC2 ΠϕϯτऩूAPI

    EC2 ΤϯυϢʔβAPI ؅ཧAPI ഑৴γεςϜ ELB ؅ཧը໘ ELB ELB
  28. /"5(BUFXBZܦ༝ͷ ௨৴ͷࢼߦճ਺͕ٸ্ঢ 151 NAT GW MYSQL Aurora EC2 EC2 ΠϕϯτऩूAPI

    EC2 ΤϯυϢʔβAPI ؅ཧAPI ഑৴γεςϜ ELB ؅ཧը໘ ELB ELB
  29. ਪଌ w ύογϒΦʔϓϯ %#αʔόଆ ͕઀ଓऴྃ࣌ʹιέοτ ΛΫϩʔζͰ͖͍ͯͳ͍ 4UBUF͕&TUBCMJTIFEͳ͍͠'*/ 8"*5  w

    Ϋϩʔζ͍ͯ͠ͳ͍ιέοτʹ઀ଓ͔ͨ͠Β͍͖ͳΓ "$,͕ฦΔ w "1*(BUFXBZ͕தܧ࣌ʹޡͬͨϙʔτʹ޲͚͍ͯΔ ͷͰ͸ʁ 157