ããã«ã¡ã¯ãMercari Corporate Products Teamã®ã¨ã³ã¸ãã¢ã®@yuki.watanabeã§ãã
ãã®è¨äºã¯ãMercari Advent Calendar 2024 ã®21æ¥ç®ã®è¨äºã§ãã
ã¯ããã«
ç¾å¨ãå
製ã®ä¼è¨ä»è¨³ã·ã¹ãã ã®éçºã«æºãã£ã¦ãã¾ãããã®ã·ã¹ãã ã«ã¯æ§ã
ãªãããå¦çãå®è£
ããã¦ããã®ã§ãããBigQueryã¸ã¯ã¨ãªããã¼ã¿ãæ½åºããããã®ãããã§èª¤æ¤åºã®åé¡ãããã¾ãããæ¬è¨äºã§ã¯ãã®åé¡ã«å¯¾ãã¦æ¤è¨ããè¤æ°ã®ã½ãªã¥ã¼ã·ã§ã³ã¨çµæçã«ã©ã®æ¹æ³ãæ¡ç¨ããã®ãã«ã¤ãã¦ç´¹ä»ãã¾ãã
ãããå¦çã®èª²é¡ã解決ããéã®åèã«ãã¦ããã ããã¨å¹¸ãã§ãã
ãªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã«ã¤ãã¦
ä¼è¨ä»è¨³ã·ã¹ãã ã«ããããã¼ã¿ã®æµã
ã¾ããä¼è¨ä»è¨³ã·ã¹ãã (å³ã®Accounting System)ã«ããããã¼ã¿ã®æµããç´¹ä»ãã¾ããã客ãã¾ãã¡ã«ã«ãªãã¡ã«ãã¤ã使ç¨ããå ´åãåå¼å
容ã«å¿ãã¦æ§ã
ãªMicroservicesãå¦çãè¡ããééã«é¢ãããã¼ã¿ãããå ´åã¯ãä¼è¨ä»è¨³ã·ã¹ãã ã®Pub/Subã«éä¿¡ãã¾ããä¼è¨ä»è¨³ã·ã¹ãã ã§ã¯Cloud Functionsã§ããªãã¼ã·ã§ã³ãè¡ããSpannerã®accounting_dataãã¼ãã«ã¸ç»é²ãã¾ãã
次ã«ãåMicroservicesã¯ä¼è¨ä»è¨³ã·ã¹ãã ã®Pub/Subã«éä¿¡æ¸ã¿ã®ãã¼ã¿ã«ã¤ãã¦ãä¼è¨ä»è¨³ã·ã¹ãã ã®ãªã³ã³ãµã¤ã«ç¨APIã¸éä¿¡ãã¾ãããã®APIã¯å¾è¿°ãããªã³ã³ãµã¤ã«ã¨å¼ã°ããçªåå¦çãè¡ããçµæãSpannerã®reconciliationãã¼ãã«ã¸ç»é²ãããã®ã§ãKubernetesä¸ã®gRPC Serverã®APIã¨ãã¦å®è£
ããã¦ãã¾ãã
Spannerã¸ç»é²ããããã¼ã¿(accounting_dataã reconciliation)ã¯ãCloud Composerã¨Dataflowãç¨ãã¦ãBigQueryã¸1æ¥ã«1度差åãåæãã¦ãã¾ãã
ãªã³ã³ãµã¤ã«ã¯ä¼è¨ãã¼ã¿ã®ç¢ºãããããæ¤è¨¼ããä»çµã¿
ãªã³ã³ãµã¤ã«ã¨ã¯ãä¼è¨ä»è¨³ã·ã¹ãã ã¨ä¼è¨ãã¼ã¿ã®éãå
ã¨ãªãMicroserviceéã®ãã¼ã¿ã®çªåå¦çã®ãã¨ãæãã¾ããMicroserviceã¯ãã¼ã¿ãã¼ã¹ã«ç»é²ããä¼è¨ãã¼ã¿ããªã¯ã¨ã¹ããã¼ã¿ã«å«ãããªã³ã³ãµã¤ã«ç¨APIã¸ãªã¯ã¨ã¹ããã¾ããAPIã§ã¯ããªã¯ã¨ã¹ããã¼ã¿ã¨ä¼è¨ä»è¨³ã·ã¹ãã ã«ç»é²ãããä¼è¨ãã¼ã¿(accounting_data)ãçªåããçªåçµæãreconciliationãã¼ãã«ã®statusã«ã©ã ã«ä¿æãã¦ç»é²ãã¾ãããã®ãªã³ã³ãµã¤ã«ãéãã¦ãMicroserviceå´ã®ãã¼ã¿ã¨ä¼è¨ä»è¨³ã·ã¹ãã å´ã®ãã¼ã¿ãä¸è´ãã¦ãããã¨ãä¿è¨¼ãã¦ãã¾ãã
以ä¸ã¯reconciliationã«ç»é²ãããstatusã«ã©ã ã®å¤ã®ã¤ã¡ã¼ã¸ã§ãã
- çªåæå: status=âsuccessâ
- çªå失æ: status=âfailedâ
ãªã³ã³ãµã¤ã«æ¤è¨¼ç¨ãããã§ãªã³ã³ãµã¤ã«ã®ã¨ã©ã¼ããªããã確èªãã
çªåã失æãããã¼ã¿ã«ã¤ãã¦ã¯ãªã³ã³ãµã¤ã«ã®ã¨ã©ã¼ã¨èãããã¾ããããã§ããªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºç¨ã®ããããCronJobãç¨ãã¦å®è£ ãã¦ãã¾ãããã®CronJobã§ã¯1æ¥ã«1度BigQueryã¸ã¯ã¨ãªãããªã³ã³ãµã¤ã«ã¨ã©ã¼ã®ãã¼ã¿ãæ½åºãã¾ããã¨ã©ã¼ã®ãã¼ã¿ãåå¨ããå ´åã¯ãMicroservice Teamã¸å ±æããå度ã®ãªã³ã³ãµã¤ã«APIã¸ã®ãªã¯ã¨ã¹ããä¾é ¼ãã¦ãã¾ãã
Spannerã¨BigQueryã®åæã¿ã¤ã ã©ã°ã«ãã誤æ¤åº
ããããä¸è¨ã®ãããã«ã¯èª²é¡ãåå¨ãã¾ãããSpannerã«ã¯ãªã¢ã«ã¿ã¤ã ã«ãªã³ã³ãµã¤ã«çµæãç»é²ããã¦ãã¾ãããããããåç
§ãã¦ããBigQueryã«ã¯1æ¥ã«1度ããåæããã¾ããããã®Spannerã¨BigQueryã®åæã¿ã¤ã ã©ã°ã«ããããããã®å®è¡çµæã«ã¯èª¤æ¤åºã§ããå½é½æ§ã®ãã¼ã¿ãå«ã¾ãã¦ãã¾ããããSpannerã«ã¯çªåæ¸ã¿ã®ãã¼ã¿ãåå¨ããããBigQueryã«ã¯æªåæãã®ãã¼ã¿ã¯æ¬æ¥ã¯çªåãæåãã¦ãã¾ããããããã§ã¯çªåã失æãããã¼ã¿ã¨ãã¦æ¤åºããã¦ãã¾ããã
ãã®ããããããã«ãã£ã¦ãªã³ã³ãµã¤ã«ã¨ã©ã¼ã¨ãã¦æ½åºããããã¼ã¿ã«ã¤ãã¦ãã¨ã³ã¸ãã¢ããSpannerã«ã¯ã¨ãªããã¦æ¬å½ã«ãªã³ã³ãµã¤ã«ã®ã¨ã©ã¼ãããã®ãã©ããã調ã¹ããã¨ããæåã®éç¨ä½æ¥ãçºçãã¦ãã¾ããã
ä¼è¨ä»è¨³ã·ã¹ãã ã§ã¯ãä¼è¨ãã¼ã¿ãæ±ã£ã¦ããã¨ããç¹æ§ä¸ãæ¯æã®æåã«åæåã®ãã¼ã¿ã確å®ããããããããç· ãããå¿
è¦ã«ãªãã¾ããææ«ä»è¿ã«çºçãããªã³ã³ãµã¤ã«ã¨ã©ã¼ã¯éããã«éãå
ã§ããMicroservice Teamã«ãªã³ã³ãµã¤ã«ä¾é ¼ããããªã³ã³ãµã¤ã«ã¨ã©ã¼ã解æ¶ããªããã°ãªãã¾ãããããããä¸è¨ã®éç¨ä½æ¥ãçºçããå ´åããªã³ã³ãµã¤ã«ã¨ã©ã¼ã®æ¤åºãã解æ¶ã¾ã§ã«æ¥ãã¾ããã§ãã¾ããã¨ããããä¼è¨æ¥åã¸ã®å½±é¿ãåºã¦ãã¾ããã¨ãããã¾ããã
ãããã®éç¨èª²é¡ã®è§£æ¶ã®ããã«ã¯ãããªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã®èª¤æ¤åºãã¼ãã«ãããã¨ãããã¨ãå¿
è¦ã§ããã
Spanner Data Boostã®æ¡ç¨
æ¤è¨ããã½ãªã¥ã¼ã·ã§ã³
éç¨èª²é¡ã®è§£æ¶ã®ãããè¤æ°ã®ã½ãªã¥ã¼ã·ã§ã³ãæ¤è¨ãã¾ããã
1. Streamingã§SpannerããBigQueryã¸åæããæ¹æ³
ã¾ããSpannerã«ç»é²ããããã¼ã¿ãStreamingã§ãªã¢ã«ã¿ã¤ã ã«BigQueryã¸åæããæ¹æ³ãæ¤è¨ãã¾ãããDataflowã®Spanner change streams to BigQuery templateãªã©ãå©ç¨ãåæç¨ã®Jobãä½æãããã¨ã§ãæè¡çã«ã¯å®ç¾å¯è½ãªæ¹æ³ã§ã¯ããã¾ããStreamingã§ãªã¢ã«ã¿ã¤ã ã«åæã§ããã¨ãä¸è¨ä»¥å¤ã®èª²é¡ã®è§£æ¶ã«ãå½¹ç«ã¦ãããããã大ããªæ©æµãå¾ãããã§ããããä¸æ¹ã§ãStremingã®åæç¨ã®Jobãæ¡ç¨ããå ´åã¯ãåæã®ä¸å ·åãããå ´åã«ãåããªããã°ãªãã¾ãããä¾ãã°ãåæç¨Jobãåæ¢ãããBigQueryã¸äºéã§ãã¼ã¿ãç»é²ããããBigQueryã¸ã®ä¸é¨ã®ãã¼ã¿ã®ç»é²ã失æãããªã©ãèãããã¾ããããããä¸å ·åãçºçããå ´åã«ã¯ãæåéç¨ã§ãªã«ããªãããããããã¯ãªã«ããªç¨ã®ã·ã¹ãã ã®å®è£ ãå¿ è¦ã«ãªãã¾ãããåæã®å®è£ ã¨ãã®å¾ã®éç¨ã¾ã§å«ããå·¥æ°ãèæ ®ããã¨ãæ¢åã®èª²é¡ã«å¯¾ããã½ãªã¥ã¼ã·ã§ã³ã¨ãã¦ã¯é大ã ã¨èããæ¡ç¨ãè¦éãã¾ããã
2. Spannerã¨BigQueryã®åæé »åº¦ãå¢å ããæ¹æ³
次ã«ãSpannerããBigQueryã®åæé »åº¦ãå¢å ããæ¹æ³ãæ¤è¨ãã¾ãããç¾ç¶1æ¥ã«1度è¡ã£ã¦ããåæã2ã3åã«å¢å ããããã®å¾ã«ãªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ããããå®è¡ããæ¹æ³ã§ããããã¾ã§ã®æ¹æ³ã¨æ¯è¼ããå½é½æ§ã®ãã¼ã¿ãæ¸ãããã¨ã¯å¯è½ã ã£ããã¨æãã¾ããããããSpannerã«ç»é²ããã¦ããBigQueryæªé£æºã®ãã¼ã¿ã¯å¤å°ãªãã¨ãåå¨ããããããªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã®èª¤æ¤åºãã¼ãã«ãããã¨ã«ã¯åããªãã¨èããæ¡ç¨ãè¦éãã¾ããã
3. Spanner federated queriesã¨Spanner Data Boostãå©ç¨ããæ¹æ³
æçµçã«ãSpannerã®Data Boostãæ´»ç¨ãããã¨ã«ãã¾ãããä¸è¿°ã®éãããªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã§ã¯BigQueryã«ã¯ã¨ãªããã¦ãã¾ããããã®ã¯ã¨ãªãä¿®æ£ããBigQueryã®Spanner federated queriesã®æ©è½ã使ããSpannerã¸ã®ã¯ã¨ãªããã¦ãã¾ããSpannerã¸ã¯ã¨ãªããéã«ãSpannerã«ã¯ç»é²æ¸ã¿ã ãBigQueryã«ã¯æªåæã®ãã¼ã¿ããä½µãã¦åå¾ãããã¨ã§ãBigQueryã¨Spannerã®ãã¼ã¿ãã©ã¡ããèæ
®ãã¦ããªã³ã³ãµã¤ã«ã¨ã©ã¼ã®ãããã¼ã¿ã®ã¿ãæ½åºãããã¨ãã§ããããã«ãªã£ã¦ãã¾ãã
éè¦ãªç¹ã¨ãã¦ã¯ãSpanner federated queriesãå©ç¨ããéã«ãData Boostãæå¹åããã¨ãããã¨ã§ããData Boostã¯Spannerã®Primary Instanceã¸è² è·ãä¸ãããã¨ãªãSpannerã¸ã¯ã¨ãªãããã¨ãã§ããé常ã«ä¾¿å©ãªæ©è½ã§ãããªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã§ã¯ãSpannerã¸ç»é²ããã1æ¥åã®ãã¼ã¿ãåå¾ãã¾ããã1æ¥åã§ãããªãã®ã¬ã³ã¼ãæ°ã¨ãªããããããPrimary Instanceã¸ã¯ã¨ãªããå ´åã¯ããã©ã¼ãã³ã¹ã¸ã®å½±é¿ã¯é¿ãããã¾ãããããã§ãData Boostãæå¹åããã®åé¡ãåé¿ãã¦ãã¾ãã
ãã®æ¹æ³ãæ¡ç¨ããçç±ã¯ãããªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã®èª¤æ¤åºãã¼ãã«ãããã¨ããç®çãéæå¯è½ã§ããããã¤å®è£
å·¥æ°ã®è¦³ç¹ã§ãæ¢åã®ã¯ã¨ãªã®æ¹ä¿®ã®ç¯å²ã§æ©æ¥ã«å®ç¾ã§ãããã¨ãè¦è¾¼ã¾ããããã§ãã
BigQueryã¸ã®ã¯ã¨ãªæ¹ä¿®åå¾ã®ãµã³ãã«ã³ã¼ã
ãªã³ã³ãµã¤ã«ã¨ã©ã¼æ¤åºã®ãããã§ä½¿ç¨ãã¦ããBigQueryã¸ã®ã¯ã¨ãªã®æ¹ä¿®åå¾ã®ãµã³ãã«ã³ã¼ããè¨è¼ãã¾ãã
æ¹ä¿®åã®ã¯ã¨ãªãµã³ãã«
SELECT *
FROM example_dataset.reconciliation
WHERE status != 'success'
ã¾ããæ¹ä¿®åã®ã¯ã¨ãªã§ã¯BigQueryã®Datasetã§ããexample_datasetã®reconciliationãã¼ãã«ãFROMå¥ã«æå®ããstatusãsuccess以å¤ã®ã¬ã³ã¼ããæ½åºãã¦ãã¾ããã
æ¹ä¿®å¾ã®ã¯ã¨ãªãµã³ãã«
WITH spanner_reconciliation AS (
SELECT *
FROM EXTERNAL_QUERY('spanner_connection_example', """
SELECT *
FROM reconciliation
WHERE created >= TIMESTAMP(CURRENT_DATE("Asia/Tokyo"), "Asia/Tokyo")
""")
)
SELECT * FROM example_dataset.reconciliation
LEFT JOIN spanner_reconciliation ON reconciliation.id = spanner_reconciliation.id
WHERE status != 'success'
AND (spanner_reconciliation.id IS NULL OR spanner_reconciliation.status != 'success')
æ¹ä¿®å¾ã®ãã¤ã³ãã¯2ç¹ããã¾ãã1ç¹ç®ã¯ãWITHå¥ã§EXTERNAL_QUERYã®é¢æ°ãå©ç¨ãã¦ããç¹ã§ãã第1å¼æ°ã«Spannerãæå®ããBigQueryã®Connection IDãæå®ãã第2å¼æ°ã«ã¯ãã¯ã¨ãªå®è¡æ¥ã«ç»é²ãããreconciliationãã¼ãã«ã®ã¬ã³ã¼ããæ½åºããã¯ã¨ãªãæå®ãã¦ãã¾ãã
2ç¹ç®ã¯ãã¡ã¤ã³ã®ã¯ã¨ãªã®WHEREå¥ã®çµãè¾¼ã¿ã§ããFROMå¥ã«reconciliationãã¼ãã«ãæå®ãããã¨ã¯æ¹ä¿®åã¨åæ§ã§ãããå ãã¦WITHå¥ã§å®ç¾©ããspanner_reconciliationãã¼ãã«ãLEFT JOINããWHEREå¥ã§å©ç¨ãã¦ãã¾ããããã«ããããBigQueryã®reconciliationãã¼ãã«ã®ã¬ã³ã¼ãã§ã¨ã©ã¼ãçºçãã¦ããããã¤ãå®è¡æ¥ã«ç»é²ãããSpannerã®reconciliationãã¼ãã«ã®ã¬ã³ã¼ãã§ã¨ã©ã¼ãçºçãã¦ãããã¾ãã¯ã¬ã³ã¼ããåå¨ããªããæ¡ä»¶ã«è©²å½ããã¬ã³ã¼ãã®ã¿ãæ½åºã§ããããã«ãªã£ã¦ãã¾ãã
ã¾ã¨ã
ä¼è¨ä»è¨³ã·ã¹ãã ã®ãªã³ã³ãµã¤ã«æ¤è¨¼ç¨ãããã«ã¯ãSpannerã¨BigQueryã®åæã¿ã¤ã ã©ã°ã«ãã誤æ¤åºã®èª²é¡ãåå¨ãã¾ãããããã§ããããã§å®è¡ãã¦ããBigQueryã¸ã®ã¯ã¨ãªãæ¹ä¿®ããSpanner federated queriesã¨Data Boostãå©ç¨ãSpannerã¸ãã¯ã¨ãªãããã¨ã§ãBigQueryã¸æªåæã®ãã¼ã¿ãæ½åºããããã«ããåæã¿ã¤ã ã©ã°ã«ãã課é¡ã解決ãã¾ããã
æ¬è¨äºå·çæç¹ã§ãæ¹ä¿®çã®ãªãªã¼ã¹ãã2ã¶æç¨åº¦ãçµéãã¦ãã¾ããæ¹ä¿®åã¨æ¯è¼ããæã«10件ç¨åº¦çºçãã¦ããSpannerã®æåã¯ã¨ãªã«ããéç¨ä½æ¥ãã»ã¼ã¼ãã«ãªããªã©ã®å¹æãåºã¦ãã¾ãã
ã¾ãããã®çµé¨ãæ©éå¥ã®æ©ä¼ã«ãå½¹ç«ã¡ã¾ããããããããã§Spannerã¸ã¯ã¨ãªããå¦çããã¨ããå¤æ´ããã£ããã«ã¤ã³ããã¯ã¹ãå¹ããªããªã大å¹
ã«ããã©ã¼ãã³ã¹ãæªåãã¦ãã¾ãåé¡ããã£ãã®ã§ãããSpanner federated queriesã¨Data Boostãå©ç¨ãããã¨ã§ãããã解決ãããã¨ãã§ãã¾ããã
ä»å¾ãSpannerã¨BigQueryããã°ãã使ãç¶ãããã¨ãäºæ³ããããããSpanner federated queriesã¨Data Boostãå©ç¨ããã¢ããã¼ããæ§ã
ãªå ´é¢ã§æ´»ç¨ã§ãããã¨æãã¾ãã
ææ¥ã®è¨äºã¯ kimras ããã§ããå¼ãç¶ãã楽ãã¿ãã ããã
åèè³æ
- Data Boost Overview | Spanner | Google Cloud
- Connect to Spanner | BigQuery | Google Cloud
- Spanner federated queries | BigQuery | Google Cloud
- ãã¼ã¿æåã¢ããªã±ã¼ã·ã§ã³ãã¶ã¤ã³ âä¿¡é ¼æ§ãæ¡å¼µæ§ãä¿å®æ§ã®é«ãåæ£ã·ã¹ãã è¨è¨ã®åç
- ãã¼ã¿ã¨ã³ã¸ãã¢ãªã³ã°ã®åºç¤ âãã¼ã¿ããã¸ã§ã¯ãã§å¤±æããªãããã«