Upload
smartnews-inc
View
11.949
Download
2
Embed Size (px)
Citation preview
Building a Sustainable Data Platform on AWS
Takumi Sakamoto 2016.01.27
Takumi Sakamoto @takus š = ā½ ā š·
Mentioned by @jeffbarr
https://twitter.com/jeffbarr/status/649575575787454464
http://www.slideshare.net/smartnews/smart-newss-journey-into-microservices
AWS Case Study
http://aws.amazon.com/solutions/case-studies/smartnews/
Data Platform at SmartNews
What is SmartNews?
ā¢ News Discovery App
ā¢ Launched in 2012
ā¢ 15M+ Downloads in World Wide
https://www.smartnews.com/en/
Our Mission
the world's quality information?
the people who need it?
How?
Machine Learning
URLs Found
Structure Analysis
Semantics Analysis
Importance Estimation
Diversification
Internet
100,000+ /day
1000+ /day
Feedback
DeliverTrending Stories
Data Platform Use Cases
ā¢ Product development
ā¢ track KPI such as DAU and MAU
ā¢ A/B test for new feature, on-boarding, etc...
ā¢ ad-hoc analysis
ā¢ Provide data to applications
ā¢ realtime re-ranking news articles
ā¢ CTR prediction of Ads system
ā¢ dashboard service for media partners
Data & Its Numbers
ā¢ User activities
ā¢ ~100 GBs per day (compressed)
ā¢ 60+ record types
ā¢ User demographics or configurations etc...
ā¢ 15M+ records
ā¢ Articles metadata
ā¢ 100K+ records per day
Sustainable Data Platform?
Sustainable Data Platform
ā¢ Provide a reliable and scalable "Lambda Architecture"
ā¢ Minimize both operation & running cost
ā¢ Be open to uncertain future
Lambda Architecture
http://lambda-architecture.net/
Why Sustainable?
ā¢ Do a lot with a few engineers
ā¢ no one is a full-time maintainer
ā¢ avoid to waste too much time
ā¢ Empower brilliant engineers in SmartNews
ā¢ everything should be as self-serve as possible
ā¢ don't ask for permission, beg for forgiveness
System Design
Ī» Architecture at SmartNews
Input Batch Serving
Speed
Output
Design Principles
ā¢ Decoupled "Computation" and "Storage" layers
ā¢ multiple consumers can use the same data
ā¢ run consumers on Spot Instances
ā¢ prevent serious data lost with minimum effort
ā¢ Use the right tool for the job
ā¢ leverage AWS managed service as possible
ā¢ fill in the missing pieces by Presto & PipelineDB
An Example
Amazon EMRAMI 3.x
Amazon S3
Amazon EMRHive
GeneralUsers
ApplicationEngineer
I wanna upgrade hive
Ad Engineer
I wanna combine news data with
ad data
Amazon EMRAMI 4.x
Amazon EMRSpark
Weāre satisfied with current
version
DataScientist
I wanna test my algorithm with the
latest spark
Batch Layer Run multiple EMR clusters for each usages
Kinesis Stream
Sparkon EMR
AWSLambda
DataScientist
I wanna consume streaming data by
Spark
ApplicationEngineer
I wanna add a streaming monitor
by Lambda
Speed Layer Consume the same data for each usages
ā¢ AWS managed services ā¢ Replicated data into Multiple AZs ā¢ High availability
Input Data
Collect Events by Fluentd
ā¢ Forwarder (running on each instances)
ā¢ store JSON events to S3
ā¢ forward events to aggregators
ā¢ collect metrics and post them to Datadog
ā¢ Aggregator
ā¢ input events into Kinesis & PipelineDB
ā¢ other reporting tasks (not mentioned today)
Forwards to S3
<source> @type tail format json path /data/log/user_activity.log pos_file /data/log/pos/user_activity.pos tag smartnews.user_activity time_key timestamp </source>
<match smartnews.user_activity> @type copy <store> @type relabel @label @s3 </store> <store> @type forward @label @forward </store> </match>
@include conf.d/s3.conf @include conf.d/forward.conf
<label @s3> <% node[:td_agent][:s3].each do |c| -%> <match <%= c[:tag] %>> @id s3.<%= c[:tag] %> @type s3 ... path fluentd/<%= node[:env] %>/<%= node[:role] %>/<%= c[:tag] %> time_slice_format dt=%Y-%m-%d/hh=%H time_key timestamp include_time_key time_as_epoch reduced_redundancy true format json utc buffer_chunk_limit 2048m </match> <% end -%> </label>
td-agent.conf conf.d/s3.conf
Capture DynamoDB Streams
<source> type dynamodb_streams stream_arn YOUR_DDB_STREAMS_ARN pos_file /path/to/table.pos fetch_interval 1 fetch_size 100 </source>
https://github.com/takus/fluent-plugin-dynamodb-streams
DynamoDB DynamoDBStreams
Amazon S3
AWSLambda
Fluentd
Recommended Practices
ā¢ Make configuration simple as possible
ā¢ fluentd can cover everything, but shouldn't
ā¢ keep stateless
ā¢ Use v0.12 or later
ā¢ "Filter" : better performance
ā¢ "Label": eliminate 'output_tag' configuration
Monitor Fluentd Status
ā¢ Monitor traffic volume & retry count by Datadog
ā¢ Datadog's fluentd integration
ā¢ fluent-plugin-flowcounter
ā¢ fluent-plugin-dogstatsd
Archive to Amazon S3
ā¢ I have 2 recommended settings
ā¢ versioning
ā¢ enable to recover from human error
ā¢ lifecycle policy
ā¢ minify storage cost
Archives to IA or Gracier xx days after the creation date
Keep previous versions xx days Save you in the future!!
Batch Layer
Various ETL Tasks
ā¢ Extract
ā¢ dump MySQL records by Embulk
ā¢ make files on S3 readable to Hive
ā¢ Transform
ā¢ transform text files into columnar files (RCFile, ORC)
ā¢ generate features for machine learning
ā¢ aggregate records (by country, by channel)
ā¢ Load
ā¢ load aggregated metrics into Amazon Aurora
Hive
ā¢ Most popular project on Hadoop ecosystem
ā¢ famous for its lovely logo :)
ā¢ HiveQL and MapReduce
ā¢ convert SQL-like query into MR jobs
ā¢ Not adopt Tez engine yet
ā¢ Amazon EMR doesn't support now
ā¢ limited improvement to our queries
How to process JSON?
A. Transform into columnar table periodically
ā¢ required converting job
ā¢ better performance
B. Use JSON-SerDe for temporary analysis
ā¢ easy way for querying raw json text files
ā¢ required to "drop table" for change schema
ā¢ performance is not good
Transform Tables-- Make S3 files readable by Hive ALTER TABLE raw_activities ADD IF NOT EXISTS PARTITION (dt='${DATE}', hh='${HOUR}');
-- Transform text files into columnar files (Flatten JSON) INSERT OVERWRITE TABLE activities PARTITION (dt='${DATE}', action) SELECT user_id, timestamp, os, country, data, action FROM raw_activities LATERAL VIEW json_tuple( raw_activities.json, 'userId','timestamp','platform','country','action','data' ) a as user_id, timestamp, os, country, action, data WHERE dt = '${DATE}' CLUSTER BY os, country, action, user_id ;
JSON-SerDe
-- Define table with SERDE CREATE TABLE json_table ( country string, languages array<string>, religions map<string,array<int>> ) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe' STORED AS TEXTFILE;
-- Result: 10 SELECT religions['catholic'][0] FROM json_table;
cf. hive-ruby-scripting
-- Define your ruby (JRuby) script SET rb.script= require 'json'
def parse (json) j = JSON.load(json) j['profile']['attribute1'] end ;
-- Use the script in HQL SELECT rb_exec('&parse', json) FROM user;
https://github.com/gree/hive-ruby-scripting
Spark
http://www.slideshare.net/smartnews/aws-meetupapache-spark-on-emr
Self-Serve via AWS CLI
# Create EMR clusters that runs Hive & Spark & Ganglia
aws emr create-cluster \
--name "My Cluster" \
--release-label emr-4.2.0 \
--applications Name=Hive Name=Spark Name=GANGLIA \
--ec2-attributes KeyName=myKey \
--instance-type c3.4xlarge \
--instance-count 4 \
--use-default-roles
Minimize expenses
ā¢ Use Spot Instances as possible
ā¢ typically discount 50-90%
ā¢ select instance type with stable price
ā¢ C3 families spike often :(
ā¢ Dynamic cluster resizing
ā¢ x2 capacity during daily batch job
ā¢ 1/2 capacity during midnight
Handle Data Dependencies
Typical Anti-Pattern
5 * * * * app hive -f query_1.hql 15 * * * * app hive -f query_2.hql 30 * * * * app hive -f query_3.hql 0 * * * * app hive -f query_4.hql 1 * * * * app hive -f query_5.hql
Workflow Management
ā¢ Define dependencies
ā¢ task E is executed after finishing task C and task D
ā¢ Scheduling
ā¢ task A is kicked after 09:00 AM
ā¢ throttle concurrent running of the same task
ā¢ Monitoring
ā¢ notification in failure
ā¢ task C must finish before 01:00 PM (SLA)
cf. http://www.slideshare.net/taroleo/workflow-hacks-1-dots-tokyo
Airflow
ā¢ A workflow management systems
ā¢ define workflow by Python
ā¢ built in shiny UI & CLI
ā¢ pluggable architecture
http://nerds.airbnb.com/airflow/
Define Tasks
dag = DAG('tutorial', default_args=default_args)
t1 = BashOperator( task_id='print_date', bash_command='date', dag=dag)
t2 = BashOperator( task_id='sleep', bash_command='sleep 5', retries=3, dag=dag)
t3 = BashOperator( task_id='templated', bash_command=""" {% for i in range(5) %} echo "{{ ds }}" echo "{{ macros.ds_add(ds, 7)}}" echo "{{ params.my_param }}" {% endfor %} """, params={'my_param': 'Parameter I passed in'}, dag=dag)
t2.set_upstream(t1) t3.set_upstream(t1)
Task
Dependencies
Python code
DAG
Workflow as Code
Deploy codes automatically after merging into master
Visualize Dependencies
What is done or not?
Alerting to Slackā¢ SLA Violation
ā¢ task A should be done till 00:00 PM
ā¢ other team's task K has dependency into task A
ā¢ Output validation failure
ā¢ stop the following tasks if the output is doubtful
Retry from Web UI
Once clear histories, airflow scheduler back fill the histories
Retry from CLI
// Clear some histories from 2016-01-01
airflow clear etl_smartnews \
--task_regex user_ \
--downstream \
--start_date 2016-01-01
// Backfill uncompleted tasks
airflow backfill etl_smartnews \
--start_date 2016-01-01
Check Rendered Query
How Long Each Tasks?
Pluggable Architecture
ā¢ Built-in plugins
ā¢ operator: bash, hive, preto, mysql
ā¢ transfer: hive_to_mysql
ā¢ sensor: wait_hive_partition, wait_s3_file
ā¢ Written our own plugin
ā¢ mysql_partition
Examplesuser_sensor = S3KeySensor(
task_id='wait_user',
bucket_name='smartnews',
bucket_key='user/dt={{ ds }}/dump.csv',
)
etl = HiveOperator(
task_id="task1",
hql="INSERT OVERWRITE INTO...."
)
etl.set_upstream(user_sensor)
import = HiveToMySqlTransfer(
task_id=name,
mysql_preoperator="DELETE FROM %s WHERE date = '{{ ds }}'" % table,
sql="SELECT country, count(*) FROM %s" % table,
mysql_table=table
)
import.set_upstream(etl)
Wait a S3 file creation
After the file is created,
Run ETL Query
After that,
Import into MySQL
Serving Layer
Provides batch views in low-latency and ad-hoc way
Presto
ā¢ A distributed SQL query engine
ā¢ join multiple data sources (Hive + MySQL)
ā¢ support standard ANSI SQL
ā¢ designed to handle TBs or PBs scale data
cf. http://www.slideshare.net/frsyuki/presto-hadoop-conference-japan-2014
Presto Architecture
Amazon S3 Kinesis Stream
AmazonRDS
AmazonAurora
PrestoWorker
PrestoWorker
PrestoWorker
PrestoWorker
PrestoWorker
PrestoWorker
PrestoCoordinator
Client
1. Query with Standard SQL
4. Scan data concurrently5. Aggregate data without disk I/O6. Return result to client
2. Generate execution plan3. Dispatch tasks into multiple workers
Amazon EMR(Hive Metastore)
Provides Hive table metadata(S3 access only)
ā» https://github.com/qubole/presto-kinesis
ā»
Why Presto?
ā¢ Join multiple data sources
ā¢ skip large parts of ETL process
ā¢ enable to merge Hive/MySQL/Kinesis/PipelineDB
ā¢ Low latency
ā¢ ~30s to scan billions records in S3
ā¢ Low maintenance cost
ā¢ stateless, and easy to integrate with Auto Scaling
Use case: A/B Test-- Suppose that this table exists DESC hive.default.user_activities; user_id bigint action varchar abtest array<map<varchar, bigint>> url varchar
-- Summarize page view per A/B Test identifier -- for comparing two algorithms v1 & v2 SELECT dt, t['behaviorId'], count(*) as pv FROM hive.default.user_activities CROSS JOIN UNNEST(abtest) AS t (t) WHERE dt like '2016-01-%' AND action = 'viewArticle' AND t['definitionId'] = 163 GROUP BY dt, t['behaviorId'] ORDER BY dt ;
2015-12-01 | algorithm_v1 | 40000 2015-12-01 | algorithm_v2 | 62000
Use case: Troubleshoot
-- Store access logs to S3, and query to them -- Summarize access & 95pct response time by SQL SELECT from_unixtime(timestamp), count(*) as access, approx_percentile(reqtime, 0.95) as pct95_reqtime FROM hive.default.access_log WHERE dt = '2015-11-04' AND hh = '13' AND role = 'xxx' GROUP BY timestamp ORDER BY timestamp ;
2015-11-04 22:00:00.000 | 6377 | 0.522 2015-11-04 22:00:01.000 | 3580 | 0.422
Scheduled Auto Scaling
$ aws autoscaling describe-scheduled-actions { "ScheduledUpdateGroupActions": [ { "DesiredCapacity": 2, "AutoScalingGroupName": "presto-worker-prd", "Recurrence": "59 14 * * *", "ScheduledActionName": "scalein-2359-jst" }, { "DesiredCapacity": 20, "AutoScalingGroupName": "presto-worker-prd", "Recurrence": "45 0 * * 1-5", "ScheduledActionName": "scaleout-0945-jst" } ] }
Presto Covers Everything? No!
ā¢ Fixed system on Amazon Aurora (or other RDB)
ā¢ provides KPI for products & business
ā¢ require high availability & low latency
ā¢ has no flexibility
ā¢ Ad-hoc system on Presto
ā¢ provides access to all dataset on data platform
ā¢ require high scalability
ā¢ has flexibility (join various data sources)
Why Fixed vs Ad-hoc?
ā¢ Difficulties on the Ad-hoc only solution
ā¢ difficult to prevent heavy queries
ā¢ large distinct count exhausts computing resources
ā¢ decrease presto maintainability
Output Data
Chartioā¢ Dashboard as A Service
ā¢ helps businesses analyze and track their critical data
ā¢ one of AWS partners (ā»)
ā¢ Combine multiple data sources at one dashboard ā¢ Presto, MySQL, Redshift, BigQuery, Elasticsearch ... ā¢ enable to join BigQuery + MySQL internally
ā¢ Easy to use for every one ā¢ everyone can make their own dashboard ā¢ write SQL directly / generate query by drag & drop
ā» http://www.aws-partner-directory.com/PartnerDirectory/PartnerDetail?id=8959
Creating dashboard1. Building query (Drag&Drop / SQL)
2. Add step (filterćsortćmodify)
3. Select visualize way (tablećgraph)
Examples
Why Chartio?
ā¢ Chartio saves a lot of engineering resources
ā¢ before
ā¢ maintain in-house dashboard written by rails
ā¢ everyone got tired to maintain it
ā¢ after
ā¢ everyone can build their own dashboard easily
ā¢ Chartio's UI is cool
ā¢ very important factor for dashboard tool
Missing Pieces of Chartio
ā¢ No programable API provides
ā¢ need to edit dashboard / chart manually
ā¢ No rollback feature
ā¢ all changes are recorded, but not rollback to the
previous state
ā¢ work around : clone => edit => rename
Speed Layer
Why Speed is Matter?
Todayās News is Wrapping
Tomorrowās Fish and Chips
ā Yesterday's News
http://www.personalchefapproach.com/tomorrows-fish-n-chips-wrapper/
How News Behaves?
https://gdsdata.blog.gov.uk/2013/10/22/the-half-life-of-news/
Use cases
ā¢ Re-rank news articles by user feedback
ā¢ track user's positive/negative signal
ā¢ consider gender, age, location, interests
ā¢ Realtime article monitoring
ā¢ detect high bounce rate (may be broken?)
ā¢ make realtime reporting dashboard for A/B test
Realtime Re-Ranking
ref. Stream å¦ē (Spark Streaming + Kinesis) ćØ Offline å¦ē (Hive) ć®ēµ±å www.slideshare.net/smartnews/stremspark-streaming-kinesisofflinehive
Amazon CloudSearch
SearchAPI
APIGateway
Kinesis Stream
Amazon S3
Amazon EMR
Amazon S3 Amazon EMR
DynamoDB
RealtimeFeedback
Re-rankArticles
ArticleMetadata
UserInterests
UserBehaviors
Offline Proceesby Hive / Spark
Realtime Monitoring
APIGateway
Stream
Continuous View
Continuous View
Continuous View
Discard raw record soon afterconsumed by Continuous View
Incrementallyupdated in realtime
PipelineDB Chartio
AWSLambda
Slack
Access Continuous Viewby PostgreSQL Client
Record
ā»1
ā»1 Use cron on 26 Feb. 2016 Migrate it soon after supporting VPC
PipelineDB
ā¢ OSS & enterprise streaming SQL database
ā¢ PostgreSQL compatible
ā¢ connect to Chartio š
ā¢ join stream to normal PostgreSQL table
ā¢ Support probabilistic data structures
ā¢ e.g. HyperLogLog
https://www.pipelinedb.com/ http://developer.smartnews.com/blog/2015/09/09/20150907pipelinedb/
Continuous View
-- Calculate unique users seen per media each day -- Using only a constant amount of space (HyperLogLog) CREATE CONTINUOUS VIEW uniques AS SELECT day(arrival_timestamp), substring(url from '.*://([^/]*)') as hostname, COUNT(DISTINCT user_id::integer) FROM activity_stream GROUP BY day,hostname;
-- How many impressions have we served in the last five minutes? CREATE CONTINUOUS VIEW imps WITH (max_age = '5 minutes') AS SELECT COUNT(*) FROM imps_stream;
-- What are the 90th, 95th, 99th percentiles of request latency? CREATE CONTINUOUS VIEW latency AS SELECT percentile_cont(array[90, 95, 99]) WITHIN GROUP (ORDER BY latency::integer) FROM latency_stream;
Summary
Sustainable Data Platform
ā¢ build a reliable and scalable lambda architecture
ā¢ minimize operation & running cost
ā¢ be open to uncertain future
My Wishlist to AWS
ā¢ Support Reduced Redundancy Storage (RRS) on EMR
ā¢ Faster EMR Launch
ā¢ Set TTL to DynamoDB records
ā¢ Auto-scale Kinesis Stream
ā¢ Launch Kinesis Analytics in Tokyo region
Thank you!!