20
Spinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei([email protected] ) Cheng Hao([email protected] )

Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei([email protected])

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Spinach: An Ad-hoc Query Engine on Top of Spark SQL

Shi Mingfei([email protected])Cheng Hao([email protected])

Page 2: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

• Spark Contributor & Alluxio(Tachyon) PMC

• Big data technology team at Intel• Active in Spark and related projects

• Focus on IA Optimization for Big data

About me & our team

Page 3: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Spinach

• Overview

• Getting starts

• Implementation & design principles

• Micro benchmark result

• Future plan

Outline

Page 4: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Why & How to accelerate SQL querieswith Spark SQL?

Page 5: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Current solution

• Tungsten• (Offheap) Data oriented Memory Management• Cache-aware computation• Code Generation

• Tungsten II• Whole Stage Code Generation• Vectorization

Page 6: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Challenge from Ad-hoc Query

• Full table scan• No index support

• Data access from cold storage• From external storage / services• Wake cache management limited by RDD cache

Page 7: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Spinach for Ad-hoc Query

Hive Table Parquet JSON ORCRedis

Connector

Cassandra Connector

HBaseConnector

Data Source API

Cache Layer

Storage Layer

Computing Engine

OSS

Page 8: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Spinach for Ad-hoc Query

Hive Table Parquet JSON ORCRedis

Connector

Cassandra Connector

HBaseConnector

Data Source API

Cache Layer

Storage Layer

Fine-grained Data Cache management

No additional 3rd Service required

Customized Indices Supported

Data Cached in Off-heap Memory(No GC Overhead)

Computing Engine

OSS

Page 9: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

How to use Spinach?

1. Start the Spark SQL Shell and Load the Spinach Package$SPARK_HOME/bin/spark-sql --jars spinach-0.1.jar

2. Create a Data Source Table with Spinach Format spark-sql> CREATE TABLE src(a INT, b STRING, value INT) USING org.apache.spark.sql.execution.datasources.spinach;

spark-sql> INSERT INTO TABLE src SELECT key1, key2, value FROM xxx;

3. Add Index Support for the Data Source Tablespark-sql>CREATE INDEX idx_1 ON src (a);

4. Ad-hoc Query by enabling the indices automaticallyspark-sql> SELECT MAX(value) FROM src WHERE a > 100 AND a <= 120 AND b=‘spinach’;

spark-sql> CREATE INDEX idx_2 ON src (a, b);

spark-sql> SELECT MAX(value) FROM src WHERE a>=100 AND b=‘spinach’;

spark-sql> DROP INDEX idx_2;

spark-sql> SELECT MAX(value) FROM src WHERE a>=100;

Automatically trigger theindex idx_1

Automatically trigger the index idx_2(TBD)

Trigger the index idx_1, but return with too many records, automatically bypass the index and fall back to full table scan

Page 10: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Spinach Implementation(Spark level)

• DDL Statement Extension• Create / Add Index (Parser & Logical Node / Physical Execution)

• Drop Index (Parser & Logical Node / Physical Execution)

• Data Source Extension• Implements the HadoopFSRelation interface (Support Partition &

File Status Caching)

• Abbr. (“spn”) for Spinach Data Source in Data Frame API

• Enable the extensions• SpinachContext (SQLContext)

• Make SQLContext configurable in ThriftServer / SparkSQL Shell / Spark-shell

• Spark Executor HeartBeat extenstions(manage cache meta)

df.write.format(“spn”).save(“/path/to/spinach_test”)

sqlContext.read.format(“spn”).load(“/path/to/spinach_test”)

Page 11: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Spinach Data Format

Column(Fiber) #1

Column(Fiber) #N

Row Group #1

RowGroup Meta

...

...

Row Group #N

File Meta Data

Index Meta Data

...

Index Node(Fiber) #1

Index Node(Fiber) #N

Data Files

Index FilesSpinach Meta File

File Entries

Version Info

Index Entries

DataType

• Fibers (the minimum unit for caching / loading / eviction)• Index Fibers• Data Fibers (Columnar based)

• Spinach Meta (1 File per Table / Partition)• Data Schema• Data File Statistic / Entries• Indices Entries

• Data File (N Files)• RowGroups• Fibers in Each Row Group• File Meta

• Index File (N * M Files)• N is the number of Data Files• M is the number of Indices• Index Meta• Index Fibers

Page 12: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Fiber & Index

二狗子

王五

李四

2

5

4

张三

赵六

钱多多

3

6

7

18

25

53

34

17

18

UID Age Name

Row 1

Row 2

Row 3

Row 4

Row 5

Row 6

2 5

2 3 4

1 4 3

5 6 7

2 5 6

INDEX of UID

17 25

17 18 18

5 1 6

25 34 53

2 4 3

INDEX of Age

Row ID

Row ID

Row Group

Fibers are in the red boxes

Page 13: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Memory Layout for Data Cache

On Heap

(HDFS / S3 / OSS / ...)

Off Heap Column(Fiber) #1

...

...

File #1...

Data Fiber #1(File #1)

Data Fiber #M(File #1)

map

map

Column(Fiber) #1

...

...

File #M

map...

IndexFiber #1

...

...

Index #1

Column(Fiber) #1

...

...

File ...

IndexFiber #1(IDX #1)

IndexFiber #1

...

...

Index #M

IndexFiber #M(IDX #1)

map

map

IndexFiber #1

...

...

Index...

(Memory)

...

Fiber Cache Manager

(Executor Process)

Page 14: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

File Format

• Spinach Meta• Describe the data schema and statistic info

• Describe how the indices are organized

• Different cache strategy for fast accessing

• Data Fiber• Columnar Storage

• Aim to fully compatible with Spark SQL Data Types (nested data types are TBD)

• Vectorization friendly in the offheap Memory, without any encoding

• Data Type aware encoder/decoder in the storage layer

• Decouple with concrete data format to support more columnar storage based format like ORC, Parquet (TBD)

• Index Fiber (Sort based)• Row(index keys) based Storage

• MySQL like B+ Tree Implementation

• Separate the files for index & data, for better managing the indices efficiently, and decouple with the data format.

Page 15: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Architecture Overview

Meta Data

FiberCacheManager

Executor 1

Index

HDFS / S3 / OSS / ...

FiberCacheManager

Executor N

SpinachContext (Driver)

FiberSensor

FiberCacheManager

Executor ...

Spark as a Service

• Fiber Cache Manager• Resides in each executor process• Manage an off-heap memory pool & Data Loading & Evicting Strategy• Update the fiber cache statistic info periodically with Fiber Sensor via executor

heartbeat RPC.

Page 16: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Architecture Overview

Meta Data

FiberCacheManager

Executor 1

Index

HDFS / S3 / OSS / ...

FiberCacheManager

Executor N

SpinachContext (Driver)

FiberSensor

FiberCacheManager

Executor ...

Spark as a Service

• Fiber Sensor• Resides in Spark Driver Process• Global Fiber Cache Distribution statistic info• Fiber(index/data) cache-aware for preferred data location in tasks assignment

Page 17: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Micro-benchmark

• Demo Micro-benchmark probably very different when data value patterns are different*• Full table scan:

• df.selectExpr("count(str1)", "count(int1)", "count(str2)").show• Range Key Scan: ()

• df.filter("str2 >= 'China-6234567' and str2 <= 'China-6234596'").selectExpr("count(str1)", "sum(int1)").show

Hardware• 1 Master + 3 Slaves• Xeon E5-2699 v3• 256GB / node• 8x1TB SATA / node• 10Gb network

Data• ~100GB Uncompressed• Record Count: 1.3B

Page 18: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Summary

• Spark as a service

• Data Source Extension

• User Defined Indices

• Off-heap Data Cache

• Fined-grained Cache Management

• Vectorization Friendly

Page 19: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Future Plan

• More Index type

• Better encoder/decoder

• Nested data type

• Support other columnar storage based data formats (ORC/Parquet)

• More Flexible Fiber Caching & Evicting strategy

• Optimize task assignment algorithm(preferred location)

• Open Source (POC stage)

Page 20: Spinach: An Ad-hoc Query Engine on Top of Spark …files.meetup.com/16395762/meetup_9_spinach-meetup0.pdfSpinach: An Ad-hoc Query Engine on Top of Spark SQL Shi Mingfei(mingfei.shi@intel.com)

Thanks