<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thanan Sapthamrong on Medium]]></title>
        <description><![CDATA[Stories by Thanan Sapthamrong on Medium]]></description>
        <link>https://medium.com/@artthananstr?source=rss-c4d8e7a33ff8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nuRB4hBYGTlu5Cur9HEBew.jpeg</url>
            <title>Stories by Thanan Sapthamrong on Medium</title>
            <link>https://medium.com/@artthananstr?source=rss-c4d8e7a33ff8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 18:01:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@artthananstr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How we monitor thousands of Spark data pipelines]]></title>
            <link>https://medium.com/@artthananstr/how-we-monitor-thousands-of-spark-data-pipelines-a918c7c7916a?source=rss-c4d8e7a33ff8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a918c7c7916a</guid>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[data-engineer]]></category>
            <category><![CDATA[apache-spark]]></category>
            <category><![CDATA[data-engineering]]></category>
            <dc:creator><![CDATA[Thanan Sapthamrong]]></dc:creator>
            <pubDate>Tue, 02 May 2023 10:47:32 GMT</pubDate>
            <atom:updated>2023-05-04T07:15:30.525Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RSpODuROEBb-dci6AlJE3w.png" /></figure><p>Hi, I’m Thanan, a Senior Data Platform Engineer at LINE MAN Wongnai (LMWN). The responsibility of the Data Platform Engineer is to ensure the reliability and availability of data platforms and services, and also make an improvement to the overall system as well. So, this blog will take you to see one of the improvement journeys that we implemented to monitor thousands of data pipelines running on Apache Spark.</p><h3>Overview of Batch Data Pipelines</h3><p>Let’s talk about the overall batch data pipeline system. In 2023, we are mainly using Apache Airflow as the main workflow orchestrator to support pipelines from different parties such as data engineers, data scientists, and analytics engineers. Currently, there are 3 Airflow Instances as following</p><ol><li>DAP Airflow is the Airflow that our data engineers and data scientists mostly use for ETL stuff. The data is collected from several internal and external sources such as databases from go-live applications in our company, object storage from GCS and AWS, Google Sheets from the business sides, and 3rd party APIs. We also use this Airflow to support the pipelines for creating the precomputed table as well (a table that uses another table(s) to generate new data by joining and/or aggregation)</li><li>DS Airflow is the Airflow that our data scientists use for data science works only such as feature engineering, model training &amp; prediction, finding insights (data exploration and analysis)</li><li>DBT Airflow is the Airflow for our analytics engineers. They will focus on working with SQL to create the precomputed tables. The pipelines are built on top of DBT (Data Build Tool) in order to leverage the feature of data modeling reusability.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L52ARk9tlpU73E7-dSXbfw.png" /><figcaption>Overall Batch Data Pipelines of LINE MAN Wongnai in 2023</figcaption></figure><p>Based on this picture, we have <strong>83.9% of DAGs</strong> (Direct Acyclic Graph), or <strong>386 DAGs </strong>that process the data through Apache Spark from every Airflow platform. In addition, if we count the total Airflow Task Instance. we will end up with a total number of <strong>2,560 Task Instances</strong>. This comes to the conclusion that right now, we have <strong>2,560 Spark data pipelines</strong> in our data platform.</p><h3>So, What’s the problem (s) ?</h3><p>A large number of data pipelines also comes with a big need for data pipeline maintenance and monitoring as well. If we don’t take care of them well and don’t have a good methodology to monitor the health of these pipelines. It will definitely affect our business stakeholders in just a few hours. Thinking about this case, we have pipelines that start running at midnight every day, and one of them got failure or was delayed. This will result in making the downstream pipelines got issues too. Finally, the business guys could take action slower than before or make a worse decision due to wrong data / missing data.</p><p>So, getting back to our topic. What’s happening with data pipelines between Airflow and Spark? What are the issues that could trouble the data pipelines? The workflow between Airflow and Spark is briefed below</p><ol><li>Airflow starts sending jobs to the Spark cluster through spark-submit CLI or Livy REST API</li><li>The Spark cluster receives the jobs and jobs start running</li><li>Airflow keeps waiting for running results from Spark</li><li>When the Spark jobs finish (or fail), Airflow collects the log.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x8lsV7AGbYrU1IBLyjWu0Q.png" /><figcaption>Pipeline Flows Between Apache Airflow and Apache Spark</figcaption></figure><p>Based on this workflow, the key problem is the lacking of Pipeline Observability. We don’t know what actually happens with our Spark jobs. Do they work well or just workable? We can only observe the issues from the Airflow side only by the errors that are sent to our Slack monitoring channel. As the screenshot below, we will know that this pipeline failed. Then, we will fix the error based on the Airflow log or Spark Web UI by adjusting the Spark configuration or fixing the data bugs. However, we still don’t know if is there any pipeline that is going to have a problem soon or how many pipelines that have room for improvement. If we know this, It would help us a lot in order to reduce the risk of data delay or get rid of common error patterns, and we will also have an opportunity to speed up our pipelines as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*gXyS-oZ6RgkeQuWeiweoyg.png" /><figcaption>Airflow Error when Spark Job Failed</figcaption></figure><p>The current process to speed up or fix the errors is required our engineers to look up the Spark Web UI for each pipeline. The Spark Web UI also has its own limitation that it has only 14 days of retention. As I mentioned to you before that we have thousands of pipelines, It is difficult and time-consuming to find out which pipelines need fixing and improvement.</p><p>The common issues that we have found from Spark data pipelines so far are below</p><p><strong>Data Skew</strong> — Due to Spark’s working mechanism being based on the cluster and it has many workers help process data parallelly. If one worker spends more time than the others. It could relate to the Data Skew issue. For example, as in the picture, we have 200 Spark tasks to be computed. 199 tasks are already completed within 10 minutes (assume), and there is one task left that spends 47 hours and it is still running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*H_mSJUS9_G58x17IX5CdYg.png" /><figcaption>Data Skew in Apache Spark ref: <a href="https://www.clairvoyant.ai/blog/optimizing-the-skew-in-spark">https://www.clairvoyant.ai/blog/optimizing-the-skew-in-spark</a></figcaption></figure><p><strong>Data Spill </strong>— This issue happens when the execution memory of the Spark worker is not enough, and Spark will first try to use the other parts of the memory block. If the memory is still not enough. Spark will use the disk to store the data instead. This results in having more spending time reading/writing data to disk. As we know that RAM is always faster than Disk. So, this issue can be also another problem that could degrade our pipeline’s performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HUNPK9gBc1iKWnprel1wrg.png" /><figcaption>Memory Spill and Disk Spill in Spark Web UI</figcaption></figure><p><strong>Over/Under Resource Utilization </strong>— When we have lots of jobs, it takes time to find out which jobs request how much of resources in terms of CPU core and memory. Some jobs might request a large number of resources. In fact, it can use just a few resources to fit its own job, and we can still get the same pipeline runtime. The one major issue when requesting lots of resources is the total resource capacity will be insufficient at that time. This can also lead to data delay too since the other pipelines have to wait for the available resources.</p><p>So, how we can know that these issues happened with our pipelines? Let’s go to the next section</p><h3>The Solution, Spark Listener</h3><p>We decide to use <strong>Spark Listener</strong> to solve the problems. But first, what’s Spark Listener?</p><blockquote>Spark Listener is an interface that provides developer API in order to listen to Spark callback events. We can make use of Spark callback events to collect statistics of each event, and export useful statistics to somewhere else for furthur analysis, monitoring purpose, etc. Spark Web UI also uses this listener to export the data to display on the web UI as well.</blockquote><p>Therefore, we decided to implement our own Spark Listener based on Java in order to export statistics of every Spark pipeline in our data platforms. We will mainly focus on the statistics calculation of Spark Task and Spark Stage. But what are Job, Stage, and Task in Spark? Let’s see the below explanation.</p><ol><li>Spark Job — When we call actions like save, collect, count, or functions that retrieve results from Spark. The Spark Driver will create a Job. Jobs will be able to run in parallel.</li><li>Spark Stage — In every Spark Job, It can contain many stages. Each stage contains transformation logic such as filter, join, sort, repartition, etc.</li><li>Spark Task — It’s an execution unit in Spark (the smallest unit in Spark) that is a part of the Spark Stage. One Spark Stage can comprise many tasks. Spark Executor computes this unit in parallel. Each unit computes the partitioned data in memory.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*_hooZh0d4Sj-p6p83xmSgg.png" /><figcaption>Relationship of Spark Job, Spark Stage, and Stage Task (ref: <a href="https://stackoverflow.com/questions/42263270/what-is-the-concept-of-application-job-stage-and-task-in-spark">https://stackoverflow.com/questions/42263270/what-is-the-concept-of-application-job-stage-and-task-in-spark</a>)</figcaption></figure><p>The workflow of this Spark Listener starts from collecting data in each Spark callback event and processing the data into useful statistics for each Spark Stage. The examples of information that we collect in each Spark Stage are a list of Spark configuration, how much data spilled in memory and disk, percentile in different range values (P50/P75/P90) of runtime spent in Spark Tasks, total written bytes and records, Error (if pipeline failed), and skewness of every runtime of Spark Tasks in each Spark Stage. This will be calculated until the pipeline is finished. Finally, the listener combines all the statistics of every Spark Stage and sends the data to the DTP Interval Server through REST API.</p><blockquote>DTP Internal Server is one of another service implemented by our platform team. This service is for platform logging purpose. The service is a server that receives requests via REST API and pass the payload to Apache Kafka. Then the data will be synced into Hive table every 2 hours.</blockquote><p>Getting back to the skewness calculation of each Spark Stage, there are 3 methods used for skewness measurement as described below</p><ol><li>Standard Deviation (SD) — If the runtime of A Spark Task exceeds the 2*SD or 3*SD of the SD of its Spark Stage. We count this as a skew task.</li><li><a href="https://www.wallstreetmojo.com/skewness-formula/"><em>Skewness Formula</em></a><em> — </em>This is straight forward method. This method measures a skewness of distribution from given input values and returns a numeric value.</li><li><em>Max(Spark Task Runtime) — P90(Spark Task Runtime) &gt; 10 minutes </em>—If the duration between the maximum task’s runtime and the task’s runtime at P90 is more than 10 minutes. We also count this as skew as well.</li></ol><p>We use many methods to find the skewness due to we really don’t know what are the suitable methods for our collected data. Each method has also its own drawback. For example, the third method won’t be able to detect skewness if the runtime at P90 and maximum runtime do not exceed the threshold.</p><p>When we have the data stored in the Hive table. Now, we are ready to find insight through querying and we can also calculate the SLO too. Moreover, we don’t have to worry about retention anymore. It can store a long period of data for several years.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PkIqjW3zZSkyXFN5gARyDQ.png" /><figcaption>Conceptual Flow of Our Spark Listener</figcaption></figure><p>During we deployed the listener, we also add the extra Spark Configurations from every Airflow platform. Thus, we will also use this to trace back to the source pipeline’s logic.</p><ol><li><em>spark.dap.source</em> — where is the source platform or source client that submits jobs</li><li>s<em>park.dap.platformEnv — Environment of the source platform or source client</em></li><li><em>spark.dap.appId</em> — The source pipeline that contains Spark source code logic. We use Airflow macro to fill this information by using this format <em>dag_id — task_id — run_id</em></li></ol><p>Example of spark-submit command when we use the Spark Listener, together with extra Spark configuration</p><pre>spark-submit \<br>--conf \&quot;spark.extraListeners=dap.LMWNSparkListener\&quot; \<br>--conf \&quot;spark.jars=dap-spark-listener-1.0.0.jar\&quot; \<br>--conf \&quot;spark.dap.source=dap-airflow\&quot; \<br>--conf \&quot;spark.dap.platformEnv=production\&quot; \<br>--conf \&quot;spark.dap.appId=wongnai_import--transform_and_update_wn_restaurant--scheduled__2023-04-13T02:00:00+00:00\&quot; \<br>script.py</pre><h3>Let’s Get into Monitoring</h3><p>It’s almost the final part. This section will take you on a tour of how we monitor our Spark pipelines. First of all, we set the SLOs (Service Level Objectives) as below.</p><ol><li>Runtime — How many pipelines have runtime exceeds the time limit? The time limit is based on the cron expression of Airflow. For example, the runtime of the @hourly pipeline should be less than 70% of 1 hour or 42 minutes, and the runtime of the <em>30 */2 * * *</em> pipeline should be less than 1 hour.</li><li>Skew — We classify skew pipelines by combining the answers from 3 skewness detection methods (same as the concept of ensemble technique in data science). If one of the answers tells us that this pipeline is skew. The number of this SLO will be reduced.</li><li>Spill — This SLO covers the area of spill in memory and disk. Even if a pipeline has spill in memory only 0.1GB. We will also count it as a spill pipeline.</li><li>Failed Apps — the last SLO is about failure. It’s easy to understand. If a pipeline has a failure. The SLO reduces. That’s it.</li></ol><p>As the below picture, these pie charts represent the 4 SLOs. In addition, we also use the data from <em>spark.dap.appId</em> to join with the DAG’s metadata in order to get the DAG’s tier. DAG’s tier is a priority of DAG defined by our data engineers which also represents the data’s priority as well. When we separate the SLOs by DAG’s tier. We can have a view of priority. For example, tier-1 needs to be fixed first, follow by tier-2, tier-3, and tier-4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6FLpi5U8rLi_bCPjZ0rqow.png" /><figcaption>SLOs of Spark Batch Data Pipelines</figcaption></figure><p>In the snapshot of our SLOs, we faced a low number of SLO in the runtime aspect. There are lots of spills that happened with our pipelines. On the other hand, the skew SLO is almost close to 100%. For the SLO of Failed Apps, it should be 100% for the ideal case which means that we got no error at all. In fact, it does not. Hence, how can we improve and increase these numbers? this brings us to the second monitoring part which is more detailed at the DAG level and Task Instance level.</p><p>The first case is the pivot table that displays the list of DAGs whose runtime exceeds the time limit and counts them in a daily manner. You will see that there is one pipeline that has a very very red color and occurs almost every day. This is a pipeline that runs every 30 minutes. Its runtime is close to the next DAG run e.g. 28–29 mins which is why the runtime exceeds. It should be less than 70% of a half hour or 21 minutes. However, this shows that it’s the tier-3 pipeline. So, we can focus on runtime’s improvement in tier-1 pipelines first due to it has more impact on business stakeholders.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yHU7Au_lIZ7Zn06YnTwFoA.png" /><figcaption>Pivot Table of Pipelines Exceed Time Limit per Day</figcaption></figure><p>The second one is still based on the pivot table. The below table shows the list of DAGs together with their Task Instances that have spill issues, whether it’s from memory or disk.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4TADcRpejl7ZISpB8clcYA.png" /><figcaption>The Pivot Table of Pipelines has the Spill Issue</figcaption></figure><p>The third table is the summary of errors of each DAG and Task Instance. We can know how many same errors happen with the specific pipeline. Moreover, we can also see the full error and use YARN Application Id to search in Spark History Server for more debugging purposes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x1sM6-eSx7EC9nlnLCyIBA.png" /><figcaption>Pivot Table of Failure Reason Summary per DAG and Task Instance</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oXPxlpfHWJ7BGYTwhkMLMw.png" /><figcaption>A Table of Failure Reason</figcaption></figure><p>For the skew pipelines, since we collect the data at Spark Stage granularity, we can see which id of Spark Stage and Spark Job has skew. Then, there is YARN Application Id for tracing back the issues in Spark History Server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pF5m-WtS8uOcqv42WKCqOQ.png" /><figcaption>A Table of Pipelines that have Spark Skew Stages</figcaption></figure><p>Besides the table visualizations, we also have a graph that helps us find which pipelines use lots of resources but produce few output data. As the below figure, the scatter plot shows that there are several pipelines that use 100 Spark executors to write data less than 5GB. Some of them can only use 5 Spark executors to fit their job and runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KURa42epqppxd2hAN-kMvg.png" /><figcaption>Scatter Plot between the number of executors and written bytes</figcaption></figure><p>Lastly, we can find pipelines that write the non-optimal files to HDFS. This issue is a case that a pipeline writes too big file(s). The file size should be close to the HDFS Block Size. In our case, it is 256MB. So, if we write too huge file(s). It can lead to spill issues, Out-of-Memory issues, and slow down the runtime due to we gather the distributed data in the Spark cluster into only a few workers. So, one worker may not be able to handle that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sZsxkZ_GmK_aKDzv06heHQ.png" /><figcaption>List of DAGs and Task Instances that produce non-optimal file sizes</figcaption></figure><h3>Example Results after We have Monitoring</h3><p>Let’s see the real improvement case after we had the monitoring</p><p>1st Case — This case is one of our pipelines that runs on a daily basis. The pipeline executes a query through SparkSQL to create a precomputed table. The pipeline runs smoothly every day without any retry. The issue is the pipeline has a skew issue, and it takes more than 2 hours to finish. We detected this issue from the above pivot table and we changed one line of code of Spark’s join strategy to enable the Broadcast Hash Join. Finally, the overall runtime has been reduced to less than 30 minutes approximately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WMmteSXz0loUgeLuSB0oZw.png" /><figcaption>Case#1 — Before and After We Have Monitoring</figcaption></figure><p>2nd Case — It’s our pipeline that has hourly DAG runs. We found that the pipeline has a lot of Airflow Retry in some runs, and the root cause is the insufficient resource of Spark workers and the job itself does not write the optimal file size to the HDFS. Therefore, this makes Airflow tries to run the same job 2–3 times in order to make it completed. Moreover, the retry mechanism works well and it does not reach the retry limit. Then, it has no error sent to our Slack channel at all. After we adjust the resources and apply the repartitioning mechanism to adjust the number of written files. It results that there is no second retry happens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bWYMfrWGGr9U5igNYCeffA.png" /><figcaption>Case#2 — Before and After We Have Monitoring</figcaption></figure><h3>Final Thought</h3><p>Coming to the final thought. After our team has the Spark Listener to help export the pipeline’s statistics and also has the Dashboards / Graphes to help us go direct to the points. Our team has started to fix the problems easier and faster. Not also fixed the known issues, but we also got the unknown issues like the second case as well. As the Data Platform Engineer, we got a solution that can support every Spark user from different teams at the same time. Going to the overview of the platform, we also have the SLOs to give us the overall scores of our platforms as well. So, the pipeline observability increases and it affects the end of the data consumer which is our business stakeholders can ensure their decision-making based on the data.</p><p>One last thing, we currently have <strong><em>8,000 Spark pipeline runs per day or 600 Spark pipeline runs per hour</em></strong><em>.</em> There are <strong><em>16 Terabytes</em></strong> of written data per day or translated into <strong><em>50 billion records</em></strong><em>.</em> With this scale of data engineering work, we still need talents to join our team, not just only Data Platform Engineers to ensure the stability and reliability of the platforms. But you can be Data Engineer to help manage our ETL pipelines and increase the data quality to support our company. Or if you are proficient at converting complex business contexts into SQL. You can join the Business Intelligence team. If you are interested. Please go to <a href="https://careers.lmwn.com/data-and-analytics">https://careers.lmwn.com/data-and-analytics</a></p><p>Thanks for your reading :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a918c7c7916a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we monitor thousands of Spark data pipelines (TH version)]]></title>
            <link>https://life.lmwn.com/how-we-monitor-thousands-of-spark-data-pipelines-f9b16b8463b?source=rss-c4d8e7a33ff8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9b16b8463b</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[engineering2023]]></category>
            <category><![CDATA[data-pipeline]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[apache-spark]]></category>
            <dc:creator><![CDATA[Thanan Sapthamrong]]></dc:creator>
            <pubDate>Tue, 25 Apr 2023 09:54:34 GMT</pubDate>
            <atom:updated>2023-11-19T10:14:41.028Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RSpODuROEBb-dci6AlJE3w.png" /></figure><p>สวัสดีครับ เราชื่ออาร์ท ตอนนี้เป็น Senior Data Platform Engineer อยู่ที่ LINE MAN Wongnai วันนี้เราจะมาเล่าว่าเรา monitor Spark pipeline กันยังไง แต่ก่อนอื่นขอแนะนำตำแหน่งตัวเองซักนิดนึง ….. ต้องเท้าความก่อนว่าเมื่อก่อน Data Engineer คือคนที่ดูแลทุกอย่างตั้งแต่ Data Pipeline, Data Infrastructure, Apache Airflow, Presto Cluster, Monitoring Tools, และอื่น ๆ อีกมากมาย ซึ่งมันเยอะมากจนทำให้การดูแลของเหล่านี้มันไม่ทั่วถึง ด้วยเหตุผลนี้บริษัทเลยเปิดตำแหน่งใหม่ขึ้นมาเป็น Data Platform Engineer ที่จะมาเข้ามาดู​ data platform โดยเฉพาะเลย หน้าที่หลักคือคล้าย ๆ Site Reliability Engineer แต่จะดูแค่ data platform and related services เท่านั้น และก็เราจะต้องทำยังไงก็ได้ให้ platform ไม่ล่ม น่าเชื่อถือ และทำ improvement ใน platform ให้ดียิ่งขึ้น วันนี้เราขอหยิบหนึ่งตัวอย่างของ improvement มาให้ดูกัน</p><h3><strong>Overview of Batch Data Pipelines</strong></h3><p>ขอเกริ่นถึงภาพรวมในปัจจุบันของ data pipeline ก่อน ตอนนี้เราเลือกใช้ Apache Airflow เป็น workflow orchestrator ตัวหลักในการช่วยทำ data pipeline ซึ่งตอนนี้เรามี Airflow ทั้งหมด 3 ตัว ได้แก่</p><ol><li>DAP Airflow เป็น Airflow ที่ Data Engineer กับ Data Scientist ใช้ทำ ETL pipeline จากฐานข้อมูลต่าง ๆ ในบริษัท ใช้ดึงข้อมูลจาก third party api ใช้ประมวลผลไฟล์จาก cloud object storage จาก GCP และ AWS รวมถึงการดึงข้อมูลจาก Google Sheets ด้วย นอกเหนือจากการทำ ETL จากข้อมูลภายนอก เรามี pipeline ที่ซัพพอร์ตการทำ precomputed table อีกด้วย (table ที่ aggregate ข้อมูลจากหลาย ๆ table เข้าด้วยกัน)</li><li>DS Airflow เป็น Airflow ที่ Data Scientist ใช้ในการ process data เพื่องานของ data scientist โดยเฉพาะ เช่น การทำ feature engineering, model training &amp; prediction, หรือการทำ analysis ทั่ว ๆ ไป</li><li>DBT Airflow เป็น Airflow ที่ Analytics Engineer ใช้ในการทำ precomputed table โดยเฉพาะ โดยหลังบ้านจะใช้ DBT (Data Build Tool) ในการช่วยจัดการ data modeling ทั้งหมด</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L52ARk9tlpU73E7-dSXbfw.png" /><figcaption>Overall Batch Data Pipelines of LINE MAN Wongnai in 2023</figcaption></figure><p>จาก Airflow ทั้ง 3 ตัวนี้ เรามี DAG (Direct Acyclic Graph) ที่ใช้ Apache Spark ในการเข้ามาช่วยประมวลผลข้อมูลเป็นจำนวนทั้งสิ้น <strong>83.9% </strong>จากจำนวน DAG ทั้งหมด หรือตีเป็น<strong> 386 DAGs </strong>ถ้านับเป็นจำนวน Task Instance ก็จะได้ทั้งหมด<strong> 2,560 Task Instances </strong>นั่นเอง พูดง่าย ๆ คือเรามี data pipelines ที่ทำงานด้วย Apache Spark ทั้งหมด<strong> 2,560 pipelines</strong> นั่นเอง</p><h3>So, What’s the problem (s) ?</h3><p>ดังนั้นเมื่อจำนวน pipeline มันมีมากขนาดนี้ การดูแล pipeline มันก็เริ่มยากขึ้นด้วย และถ้าเราดูแลไม่ดีหรือ take action ไม่ไวหรือไม่มีวิธีการ monitor มันก็กระทบไปถึงฝั่ง business ได้เลยในเวลาแค่ไม่กี่ชั่วโมง ตัวอย่างเช่น เรามี pipeline ที่เริ่มทำงานช่วงเที่ยงคืนเวลาไทย แล้วถ้ามี pipeline ซัก 1 ตัวทำงานช้ากว่าปกติหรือมีพังด้วยสาเหตุต่าง ๆ และมีอีกหลาย ๆ downstream pipeline ที่มารอข้อมูลจาก pipeline ตัวนี้เพื่อนำไปคำนวณต่อ ผลที่จะเกิดขึ้นก็คือ ข้อมูลจะถูกดีเลย์หรือไม่ก็ข้อมูลผิดไปเลย ซึ่งมันก็จะส่งผลกับฝั่ง business ที่แต่ละทีมก็จะ take action ได้ช้าหรือตัดสินใจผิดเพราะข้อมูลไม่ครบก็ได้</p><p>ดังนั้นเรามาดูภาพปัจจุบันกันต่อว่าสิ่งที่เกิดขึ้นใน Data Pipeline ระหว่าง Airflow กับ Spark มีอะไรเกิดขึ้นบ้าง โดยการทำงานคร่าว ๆ มีดังนี้</p><ol><li>Airflow ส่ง job เข้าไปหา Spark cluster ด้วย spark-submit cli หรือ Livy REST API</li><li>Airflow รอ Spark รันไปเรื่อย ๆ</li><li>พอ Spark รันเสร็จ ก็จะเก็บ log ต่าง ๆ กลับมา แต่ถ้าไม่เสร็จก็จะเก็บ error กลับมาแทน</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x8lsV7AGbYrU1IBLyjWu0Q.png" /><figcaption>Pipeline Flows Between Apache Airflow and Apache Spark</figcaption></figure><p>จาก flow นี้ ปัญหาจริง ๆ ที่เกิดขึ้นคือเราขาด <strong>Pipeline Observability</strong> เราไม่สามารถรับรู้ได้เลยว่า Spark pipeline ต่าง ๆ ทำงานดีไหม หรือแค่ทำงานได้ สิ่งที่เราจะรับรู้จาก Airflow จะมีแค่ error ที่ส่งเข้ามาใน Slack ว่า pipeline พัง แบบในรูปด้านล่าง พอปัญหาแบบนี้เกิดขึ้นปุ๊บ เราก็จะเข้าไปแก้ไขบัคหรือปรับ Spark Configuration ใน pipeline ให้มันทำงานต่อได้ แต่เราก็ยังไม่สามารถรับรู้ได้อยู่ดีว่า มี pipeline ไหนที่ใกล้จะมีปัญหาอีก หรือมี pipeline ไหนที่เราสามารถปรับปรุงเพื่อให้มันทำงานได้แบบไม่ต้องไปเสี่ยงให้มันเกิดปัญหาอีก หรือทำให้มันเร็วขึ้นกว่าเดิมได้ไหมเพื่อที่จะลดความเสี่ยงที่จะเกิดข้อมูลดีเลย์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*gXyS-oZ6RgkeQuWeiweoyg.png" /><figcaption>Airflow Error when Spark Job Failed</figcaption></figure><p>ซึ่งการจะเข้าไปปรับปรุงให้ pipeline มันทำงานเร็วขึ้น เราจะต้องเข้าไปดูที่ Spark Web UI ของแต่ละ pipeline เท่านั้น และตัว Spark Web UI เองก็มี retention ถึงแค่ 14 วัน และอย่างที่ได้เล่ามาว่าเรามี pipeline จำนวนถึงหลักพัน ดังนั้นการจะเข้าไปหาว่า pipeline ไหนควรได้รับการปรับปรุงบ้างจึงเป็นเรื่องที่ใช้เวลาเยอะมาก</p><p>ซึ่งปัญหาหลัก ๆ ที่เราพบบน Spark pipeline โดนส่วนมากมีดังนี้ ได้แก่</p><p><strong>Data Skew</strong> — ในเมื่อการทำงานของ Apache Spark มันทำงานกันเป็น cluster มี worker หลาย ๆ ตัวช่วยกันประมวลผลพร้อม ๆ กัน ถ้า worker ตัวใดตัวหนึ่งใช้เวลาทำงานเยอะกว่าเพื่อน แปลว่าเราอาจจะกำลังเจอปัญหา Data Skew เข้าให้แล้ว ดังตัวอย่างในรูป เรามี task ทั้งหมด 200 tasks ซึ่ง 199 tasks อาจจะทำงานเสร็จไปแล้วภายใน 10 นาที แต่มีอีก 1 task ที่ยังทำงานอยู่แล้วใช้เวลาไปแล้ว 47 ชั่วโมง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*H_mSJUS9_G58x17IX5CdYg.png" /><figcaption>Data Skew in Apache Spark ref: <a href="https://www.clairvoyant.ai/blog/optimizing-the-skew-in-spark">https://www.clairvoyant.ai/blog/optimizing-the-skew-in-spark</a></figcaption></figure><p><strong>Data Spill </strong>— เป็นปัญหาที่จะเกิดขึ้นเมื่อ execution memory ของ Spark worker ไม่พอ จนต้องใช้ memory ส่วนอื่น ๆ หรือถ้าไม่พออีก มันก็จะไปใช้ disk ในการเก็บ data แทน ซึ่งมันจะทำให้เสียเวลามากขึ้นในการที่ Spark จะไปเขียน/อ่านข้อมูลที่ disk อย่างที่เรารู้กันว่า RAM จะทำงานเร็วกว่า disk เสมอ ดังนั้นนี่จึงเป็นอีกหนึ่งปัญหาที่อาจจะทำให้ pipeline เราทำงานช้าลง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HUNPK9gBc1iKWnprel1wrg.png" /><figcaption>Memory Spill and Disk Spill in Spark Web UI</figcaption></figure><p><strong>Over/Under Resource Utilization </strong>— พอเรามี job จำนวนมาก เราเองไม่มีทางรู้ได้เลยว่า job ไหนขอ CPU core กับ memory เยอะไปหรือน้อยไป บาง job ขอ CPU ไปเยอะมาก แต่จริง ๆ แล้วอาจจะใช้แค่ 10 CPU cores ก็เพียงพอต่อการรันงานให้จบภายในเวลาที่กำหนดแล้ว ซึ่งเราจะรู้ก็ต่อเมื่อไปดูในโค้ดของเรา หรือ Spark Web UI อีกทั้งการขอ resource เยอะมาก ๆ มันก็ส่งผลกับ capacity ที่มีอยู่ อาจจะส่งผลให้มันไม่พอจนเกิด job ดีเลย์</p><p>จากปัญหาที่เล่ามาจะเห็นว่ามันค่อนข้างกระทบกับ pipeline โดยตรง และจากที่ทีมได้เจอมา บาง pipeline ใช้เวลาถึง 2 ชั่วโมง แต่พอหลังเจอและแก้ไป ใช้เวลาราว ๆ 20 นาที ดังนั้นขอนำไปสู่วิธีการที่เราใช้ตรวจเจอปัญหาที่เล่ามาเลย</p><h3>The Solution, Spark Listener</h3><p>เราจะใช้ Spark Listener มาแก้ปัญหาที่ว่า ก่อนอื่นเลย Spark Listener คืออะไร</p><blockquote>Spark Listener คือเป็น Developer API ของ Spark ตัวนึงที่โดยปกติแล้ว Spark จะส่ง callback event มาที่ Interface นี้ แล้วเราสามารถหยิบ event นี้ไปทำอะไรต่อก็ได้ ซึ่งตัว Spark Web UI เองก็ใช้ Spark Listener ในการช่วย export ข้อมูลต่าง ๆ ออกมาแสดงผลที่หน้า Web UI นั่นเอง</blockquote><p>ดังนั้นเราจะใช้เจ้านี่แหละ ช่วย export ข้อมูลต่าง ๆ ไม่ว่าจะเป็น job นี้ใช้ Spark Configuration อะไรบ้าง job เริ่มกี่โมง มี memory / disk spill หรือ data skew ไหม หรือ job นี้เขียน data ไปทั้งหมดกี่ไบต์</p><p>ซึ่งตัว Spark Listener ที่เราทำขึ้นมาเขียนด้วยภาษา Java และจะโฟกัสที่การคำนวณ Spark Task กับ Spark Stage เพื่อให้ได้ข้อมูลที่ต้องการ กับขออธิบายคร่าว ๆ ก่อนว่า Job, Stage, และ Task ใน Spark คืออะไร</p><ol><li>Spark Job — ทุกครั้งที่เราเรียกคำสั่ง <em>save, collect, count</em> หรืออะไรที่ทำการเรียก result จาก Spark ตัว Spark Driver จะสร้าง Job ขึ้นมา ซึ่งแต่ละ Job สามารถถูกประมวลแบบ parallel ได้พร้อมกัน</li><li>Spark Stage — ในหนึ่ง Spark Job สามารถประกอบไปด้วยหลาย ๆ Stage และในแต่ละ Stage จะเก็บ logic ของการ transformation ไว้ เช่น filter, join, sort, หรือทำการ exchange Spark partition กันใน Spark cluster</li><li>Spark Task — เป็น unit ที่เล็กที่สุดใน Spark ที่จะอยู่ใน Spark Stage อีกที ซึ่ง unit นี้จะเป็นส่วนที่ Spark worker จะนำไปประมวลผลจริง ๆ แล้ว</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*_hooZh0d4Sj-p6p83xmSgg.png" /><figcaption>Relationship of Spark Job, Spark Stage, and Stage Task (ref: <a href="https://stackoverflow.com/questions/42263270/what-is-the-concept-of-application-job-stage-and-task-in-spark">https://stackoverflow.com/questions/42263270/what-is-the-concept-of-application-job-stage-and-task-in-spark</a>)</figcaption></figure><p>การทำงานของ Spark Listener ตัวนี้จะเริ่มจากการไปเก็บข้อมูลตาม Spark event ต่าง ๆ นำไป pre-process และย่อยให้เหลือแต่สถิติของแต่ละ Spark Stage เช่น เราเก็บจำนวน memory / disk spill, Percentile (P50/P75/P90) ของเวลาที่ใช้ไปในแต่ละ Spark Task, จำนวน byte / record ที่เขียนลงไปที่ HDFS, Error ต่าง ๆ (ถ้าเกิด failed) รวมถึงคำนวณ skewness ของ Spark Task ในแต่ละ Stage ด้วย และพอ pipeline ทำงานเสร็จ เราก็จะให้ Spark Listener ของเราส่งข้อมูลทั้งหมดออกไปหา server ตัวนึงที่ชื่อว่า DTP Internal Server ผ่าน REST API</p><blockquote>DTP Internal Server เป็นอีกหนึ่ง Service ที่ภายในทีมจะใช้เพื่อเก็บ log ต่าง ๆ ผ่าน REST API และพอมันได้รับ request มาจาก client มันจะทำการส่งข้อมูลขึ้น Apache Kafka อีกทีนึง และข้อมูลจะถูก sync ลง Hive table ทุก ๆ 2 ชั่วโมง</blockquote><p>ขอย้อนกลับไปที่วิธีที่เราใช้คำนวณ skewness เราใช้ทั้งหมด 3 วิธีในการวัด skewness ของ Spark stage ได้แก่</p><ol><li><em>Standard Deviation</em> — ถ้าเวลาที่ใช้ไปของ task นั่นเกิน 2*SD หรือ 3*SD ก็จะบอกว่ามัน skew</li><li><a href="https://www.wallstreetmojo.com/skewness-formula/"><em>Skewness Formula</em></a> — ใช้สูตรทางสถิติเข้ามาช่วยเลย อันนี้สุดท้ายเราจะได้ค่ามาหนึ่งค่าเพื่อบอกว่า distribution ของเวลาที่ใช้ไปในแต่ละ Spark task เป็นอย่างไร</li><li><em>Max(Spark Task Runtime) — P90(Spark Task Runtime) &gt; 10 minutes </em>— พูดง่าย ๆ คือ ถ้าเกิด runtime ของ task สุดท้ายมันห่างกับ runtime ที่ P90 เกิน 10 นาที เราก็จะบอกว่ามัน skew นั่นเอง</li></ol><p>ที่เราเลือกใช้วิธีวัดทั้งหมด 3 วิธีเพราะเรายังไม่เห็น data จริง ๆ ว่าวิธีการไหนเหมาะสมสำหรับ data ที่เราเก็บมา เราเลยต้อง implement ทั้ง 3 วิธีไปก่อน และอย่างวิธีที่ 3 มันก็มีจุดบอดเช่นกัน ถ้าเกิด task ที่ P90 ใช้เวลาใกล้เคียงกับ task ที่ใช้เวลาเยอะสุด มันก็จะบอกว่าไม่ skew (ขอเคลมก่อนว่าผู้เขียนไม่ได้เชี่ยวชาญด้านสถิติมาก ถ้าเกิดผิดพลาดตรงนี้ ขออภัยไว้​ ณ ​ที่นี่ด้วย)</p><p>พอเรามีข้อมูลมาเก็บที่ Hive table แล้ว เราก็พร้อมที่จะ query ข้อมูลขึ้นมาแล้วเอาไปหา insight ต่าง ๆ รวมถึงสามารถนำไปคำนวณ SLO ได้ด้วย กับเราไม่ต้องห่วงเรื่อง retention ด้วย สามารถเก็บได้เป็นหลักปี (ใช้แค่ 1–3 เดือนก็เพียงพอต่อการใช้งานแล้ว)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PkIqjW3zZSkyXFN5gARyDQ.png" /><figcaption>Conceptual Flow of Our Spark Listener</figcaption></figure><p>เมื่อเรามี Spark Listener แล้ว เราจะเอาตัว Spark Listener ไปติดกับทุก ๆ pipeline ที่อยู่ตาม Airflow รวมถึงเราจะใส่ Spark Configuration เหล่านี้เพิ่มไปด้วย เพื่อเก็บไว้ใช้ทำ tracking กลับไปที่ตัว pipeline</p><ol><li><em>spark.dap.source</em> — ใช้บอกที่มาของ Spark job นี้ว่าโดนส่งมาจาก data platform หรือ client ไหน</li><li>s<em>park.dap.platformEnv — </em>ใช้บอก environment ของ data platform ที่ส่ง job เข้ามา</li><li><em>spark.dap.appId</em> — ใช้ระบุที่มาของ pipeline logic ซึ่งเรานำข้อมูลจาก Airflow macro มาใช้ และใช้เป็น format นี้ <em>dag_id — task_id — run_id</em></li></ol><p><em>ตัวอย่างใน spark-submit command ในการใช้ Spark Listener</em></p><pre>spark-submit \<br>--conf \&quot;spark.extraListeners=dap.LMWNSparkListener\&quot; \<br>--conf \&quot;spark.jars=dap-spark-listener-1.0.0.jar\&quot; \<br>--conf \&quot;spark.dap.source=dap-airflow\&quot; \<br>--conf \&quot;spark.dap.platformEnv=production\&quot; \<br>--conf \&quot;spark.dap.appId=wongnai_import--transform_and_update_wn_restaurant--scheduled__2023-04-13T02:00:00+00:00\&quot; \<br>script.py</pre><h3>Let’s Get into Monitoring</h3><p>มาถึงส่วนสุดท้ายแล้ว แล้วเรา monitor อะไรบ้าง อย่างแรกคือเราตั้ง SLO ขึ้นมาก่อนเลยโดย SLO ของเรามีทั้งหมด 4 ข้อ</p><ol><li>Runtime — มี pipeline จำนวนเท่าไหร่ที่มี runtime รันเกิดกว่า time limit ที่ตั้งไว้ ซึ่ง time limit เราคิดมาจาก cron ของ Airflow เช่น runtime ของ <em>@hourly</em> pipeline จะต้องไม่เกิน 70% ของ 1 ชั่วโมง หรือคิดเป็น 42 นาที หรือ runtime ของ <em>30 */2 * * *</em> pipeline จะต้องไม่เกิน 1 ชั่วโมงเป็นต้น</li><li>Skew — สำหรับ skew เราวัดมาจาก คำตอบที่ได้จาก skewness calculation จากทั้งหมด 3 วิธีที่ได้กล่าวไว้ และเอาคำตอบมารวมกัน ถ้ามีคำตอบใดคำตอบหนึ่งบอกว่ามัน skew เราก็จะนับว่า pipeline นี้ไม่ meet SLO (ทำคล้าย ๆ กับ ensemble method)</li><li>Spill — อันนี้ค่อนข้างตรงไปตรงมา ถ้า pipeline ไหนมี spill ไม่ว่าจะเกิดที่ memory หรือ disk เราก็จะนับว่า pipeline นี้ไม่ meet SLO ทันที</li><li>Failed Apps — SLO ตัวสุดท้ายคือ pipeline เรา failed เมื่อไหร่ ก็จะนับว่าไม่ meet SLO ทันที</li></ol><p>และนี่คือตัวอย่าง SLO จะเห็นว่ามันแบ่งเป็นตาม tier อีก ใช่แล้ว ในทุก ๆ pipeline จะมีการระบุความสำคัญเป็น tier อยู่ ไล่ความสำคัญตั้งแต่ tier-1, tier-2, tier-3, tier-4, tier-no (ไม่มี tier) เราเลยเอา tier ตรงนี้มาช่วย grouping ในแต่ละ SLO ด้วย ดังนั้นเราจะสามารถให้ความสำคัญกับการทำ pipeline tuning ได้ดีขึ้น</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6FLpi5U8rLi_bCPjZ0rqow.png" /><figcaption>SLOs of Spark Batch Data Pipelines</figcaption></figure><p>จากรูป SLO จะเห็นไว้ว่า Runtime เราค่อนข้างไม่เขียว และมี Spill ค่อนข้างเยอะมาก กลับกับในส่วนของ Skew จะเห็นว่ามันค่อนข้างจะเขียวเลย และก็เคสของ Failed Apps มันควรเป็น 100% ซึ่งหมายถึงไม่มี pipeline ไหนพังเลย แต่กลับมาที่โลกของความจริง มันก็ไม่ได้เป็นอย่างนั้น ดังนั้นคำถามคือ เราจะทำให้ตัวเลขเหล่านี้มันเพิ่มขึ้นยังไงได้บ้าง จึงเข้ามาสู่การ monitor อีกส่วนนึงคือเราสามารถดูเป็นราย pipeline ได้เลยว่า pipeline ไหนมีปัญหาบ้าง</p><p>ตัวอย่างแรกเลยคือเราทำ pivot table ที่ลิสต์ DAG ที่ใช้ runtime เกินกว่า time limit และเอามานับเป็นจำนวนครั้งต่อวัน จะเห็นว่ามีสีแดง ๆ อยู่อันนึงที่ runtime เกินตลอดเวลา (เป็น pipeline ที่ทำงานทุก ๆ 30 นาที) แต่เป็น pipeline tier-3 ซึ่งอาจจะไม่สำคัญมาก ยังปล่อยให้ delay ได้อยู่ (runtime จริงอาจจะใกล้กับรอบ DAG run มาก ๆ เช่น runtime 28–29 นาที) ถ้าเคสนี้เราก็ควรโฟกัสที่ pipeline tier-1 ก่อน เพราะ impact ต่อ business มากกว่า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yHU7Au_lIZ7Zn06YnTwFoA.png" /><figcaption>Pivot Table of Pipelines Exceed Time Limit per Day</figcaption></figure><p>ตัวอย่างที่สองยังเป็น pivot table เหมือนเดิม แต่เป็น pivot table ที่โชว์ลิสต์ของ DAG กับ Task Instance ที่เกิดปัญหา spill ทั้ง memory และ disk</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4TADcRpejl7ZISpB8clcYA.png" /><figcaption>Pivot Table of Pipelines have the Spill Issue</figcaption></figure><p>ตัวอย่างที่สามเป็น pivot table ที่สรุป error จาก Spark ว่าในแต่ละ pipeline ที่พังมันพังด้วยเหตุผลอะไร และก็สามารถดูเพิ่มเติมได้ว่า error เต็ม ๆ คืออะไร และ มี YARN Application Id ให้สามารถกลับไป search หาใน Spark History Server ได้เพื่อการ debugging เพิ่มเติม</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x1sM6-eSx7EC9nlnLCyIBA.png" /><figcaption>Pivot Table of Failure Reason Summary per DAG and Task Instance</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oXPxlpfHWJ7BGYTwhkMLMw.png" /><figcaption>A Table of Failure Reason</figcaption></figure><p>สำหรับ Skew pipeline เราสามารถดูได้เลยว่า Spark Stage ไหนที่มันเกิด Skew และสามารถย้อนกลับที่ไปดูที่ Spark Web UI ที่อยู่ใน Spark History Server ได้อีกด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pF5m-WtS8uOcqv42WKCqOQ.png" /><figcaption>A Table of Pipelines that have Spark Skew Stages</figcaption></figure><p>นอกเหนือจากนี้เรายังมีกราฟที่จะช่วยเราหาอีกว่าตอนนี้มี pipeline ไหนบ้างที่ใช้ resource เยอะ ๆ แต่เขียน data ออกไปนิดเดียว ดังในรูปข้างล่างนี้ ดังในรูปจะเห็นว่ามี pipeline อีกหลาย ๆ ตัวเลยที่ใช้จำนวน Spark executor ถึง 100 ตัว แต่เขียน data ออกไปไม่ถึง 5 กิกะไบต์ด้วยซ้ำ ใช่ครับทุกคนฟังไม่ผิด มีจำนวนเยอะประมาณนึงเลย TT ซึ่งจริง ๆ แล้ว pipeline เหล่านี้อาจจะใช้แค่ Spark executor แค่ 5 ตัวก็เพียงพอแล้วก็ได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KURa42epqppxd2hAN-kMvg.png" /><figcaption>Scatter Plot between the number of executors and written bytes</figcaption></figure><p>นอกเหนือจากนี้เรายังสามารถหา pipeline ที่เขียนไฟล์ ลง HDFS แบบไม่ optimal ได้ด้วย อย่างเช่น มี pipeline เขียนไฟล์ทั้งหมด 1 ไฟล์มีขนาด 2 กิกะไบต์ แบบนี้เรียกว่าไม่ optimal ซึ่งควรปรับให้เขียนทั้งหมด 8 ไฟล์แทน (1 ไฟล์ควรมีขนาดใกล้เคียงกับ HDFS Block Size ~256MB) ซึ่งตรงนี้ก็เป็นอีกหนึ่งจุดที่มักจะทำให้เกิดปัญหา spill นั่นเอง เพราะเราเขียนข้อมูลใหญ่เกินไปกว่าที่ execution memory ของ Spark worker จะรับไหว อีกทั้งยังทำให้ runtime สูงขึ้นด้วย เพราะ Spark รวบรวม data จากหลาย ๆ ที่เข้ามารวมที่เดียวกันก่อนเขียนไฟล์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sZsxkZ_GmK_aKDzv06heHQ.png" /><figcaption>List of DAGs and Task Instances that produce non-optimal file sizes</figcaption></figure><h3>Example Results after We have Monitoring</h3><p>เราลองมาดูตัวอย่างเคสจริงที่เราเจอจากการ monitoring และได้ทำการปรับปรุงแก้ไขไปแล้วกัน</p><p>เคสที่ 1 — เป็นเคสที่ pipeline ที่รันวันละครั้ง โดย pipeline จะรัน query ผ่าน SparkSQL เพื่อทำ precomputed table ซึ่ง pipeline นี้ย้ำว่าเป็น pipeline ที่<strong><em>รันผ่านทุกวันและไม่มีการ Retry แต่อย่างใด </em></strong>แต่มีปัญหา Data Skew เลยทำให้ runtime สูงเกินสองชั่วโมง ซึ่งเรามาตรวจเจอสิ่งนี้จาก pivot table ด้านบน เลยทำการปรับแก้ query โดยเปลี่ยน Join Strategy (เปลี่ยนโค้ดแค่ 1 บรรทัด) ไปใช้ Broadcast Hash Join แทน สุดท้าย runtime เลยเหลือไม่ถึง 25 นาที</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WMmteSXz0loUgeLuSB0oZw.png" /><figcaption>Case#1 — Before and After We Have Monitoring</figcaption></figure><p>เคสที่ 2 — เคสนี้เป็น pipeline ที่จะทำงานทุก ๆ ชั่วโมง โดยที่ปัญหามันคือ มันเกิดการ retry ที่ Airflow หลาย ๆ รอบ (ไม่ได้เจอทุกรอบ) สาเหตุเป็นเพราะ Spark worker มี resource ไม่พอ และ pipeline นี้เขียนไฟล์ลง HDFS แบบไม่ optimal มันเลยทำให้ Spark worker ตายจน Airflow ต้อง retry ตัวเองใหม่เรื่อย ๆ ถึง 2–3 รอบ จนมันผ่านเอง ซึ่งที่ผ่านยังไม่มี error จาก pipeline นี้ส่งลงไปที่ Slack ของทีมเลยเพราะ Airflow ยัง retry ตัวเองไม่ถึง threshold ที่ตั้งไว้ หลังจากการปรับ tuning ไป โดยได้ปรับ resource และได้ทำการ repartition ใน Spark ไปเพื่อปรับจำนวนไฟล์ก่อนจะเขียนลง HDFS ผลที่ได้ก็คือไม่มี Airflow retry เกิดขึ้นอีกเลย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bWYMfrWGGr9U5igNYCeffA.png" /><figcaption>Case#2 — Before and After We Have Monitoring</figcaption></figure><h3>Final Thought</h3><p>มาถึงสรุปแล้ว ก็หลังจากที่ทีมเราได้มี Spark Listener เข้ามาช่วย export ข้อมูลและมี Dashboard / Graph ในการช่วยชี้เป้าปัญหาต่าง ๆ เราก็เริ่มทำการแก้ไขปัญหาได้อย่างตรงจุดมากขึ้น รวดเร็ว และขุดปัญหาใต้พรมต่าง ๆ ที่เราไม่เคยรู้ และก็ยังสามารถ support ทุก ๆ ทีม ทุก ๆ platform ที่ใช้ Apache Spark ได้ในเวลาเดียวกันอีกทั้งเราก็มี SLO ด้วยที่ได้มาจากข้อมูลตรงนี้ ทำให้เราสามารถมั่นใจได้ว่า pipeline ทั้งหมดของเรายังทำงานได้ดี หรือถ้ายังไม่ meet SLO ก็ต้องทำ improvement กันไป ฮ่า ๆ ซึ่งทั้งหมดมันก็ส่งผลให้ pipeline เรามีคุณภาพ มี observability ที่ดีขึ้น และถ้าฝั่ง pipeline ดีขึ้น มันก็ส่งผลไปถึงปลายทางฝั่ง business ด้วยเช่นกัน business ก็จะมั่นใจการใช้ข้อมูลประกอบการตัดสินใจมากขึ้น</p><p>สุดท้ายนี้ขอแถมอีกนิดนึง ขายของกันรัว ๆ ในปัจจุบัน pipeline ของทีม Data ที่ทำงานอยู่บน Apache Spark มีจำนวนรวมราว ๆ <strong>8,000 pipeline runs ต่อวัน</strong> นับเป็นรายชั่วโมงคือราว ๆ<strong> 600 pipeline runs ต่อชั่วโมง</strong> และมีข้อมูลถูกเขียนลง HDFS เฉลี่ยแล้ว <strong>16 Terabytes ต่อวัน</strong> หรือตีเป็น record ได้ <strong>50 พันล้านแถว</strong> ก็ด้วย scale งานปัจจุบันเราจึงยังต้องการคนเก่ง ๆ เข้ามาร่วมทีมอีก ใครสนใจเข้ามาร่วมงานด้วยกันไม่ว่าจะเป็น Data Engineer ที่จะเข้ามาพัฒนา pipeline เหล่านี้ให้ดีขึ้น หรือ Data Platform Engineer ที่จะเข้ามาทำให้ data platform ของ LINE MAN Wongnai เสถียรขึ้น scalable มากขึ้น (เราไม่ได้มีแต่ ETL Infrastructure อย่างเดียวที่ต้องดูแล ยังมีอีกหลาย services ที่ยังไม่ได้พูดถึงในนี้ต้องดูแลอีกมากมาย) หรืออยากเข้ามาซัพพอร์ตฝั่ง Business Intelligence มาเขียน query เพื่อหา insight ของธุรกิจก็สามารถส่งใบสมัครงานเข้ามาได้ที่ <a href="https://careers.lmwn.com/data-and-analytics">https://careers.lmwn.com/data-and-analytics</a></p><p>ขอบคุณครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9b16b8463b" width="1" height="1" alt=""><hr><p><a href="https://life.lmwn.com/how-we-monitor-thousands-of-spark-data-pipelines-f9b16b8463b">How we monitor thousands of Spark data pipelines (TH version)</a> was originally published in <a href="https://life.lmwn.com/">Life@LINE MAN Wongnai</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>