MySQL Document Store: Under the Hood

26
MYSQL DOCUMENT STORE UNDER THE HOOD Jan Kneschke, Architect, MySQL Middleware and Clients MySQL Innovation Day, April 2016 mailto:[email protected]

Transcript of MySQL Document Store: Under the Hood

Page 1: MySQL Document Store: Under the Hood

MYSQL DOCUMENT STOREUNDER THE HOOD

Jan Kneschke,

Architect, MySQL Middleware and Clients

MySQL Innovation Day, April 2016

mailto:[email protected]

Page 2: MySQL Document Store: Under the Hood

WHAT TO EXPECTWhat is MySQL Document StoreFoundations of the X ProtocolHow X Plugin maps CRUD to SQL

Page 3: MySQL Document Store: Under the Hood

DOCUMENT STOREA Document is

typedstructured as a treeno schema enforced

'_id': 1, 'name': 'Milk', 'properties': 'volume_l': 1, 'amount': 32

Page 4: MySQL Document Store: Under the Hood

DOCUMENT STOREStore provides

fast retrievaloptimizations for updatesoperations

Find, Update, Modify, DeleteProjectionsFiltersAggregation

Page 5: MySQL Document Store: Under the Hood

MYSQL DOCUMENT STOREpart of MySQL 5.7.12X DevAPI

MySQL Shell, Connector/J, C/.net and C/node.jsX Protocol

implemented by X Pluginusing stable interfaces from MySQL 5.7 GA

SQL serviceJSON support

Page 6: MySQL Document Store: Under the Hood

MYSQL 5.7'S JSON SUPPORTMySQL 5.7.8 added:

JSON datatypeJSON functions likeJSON_EXTRACT(), JSON_OBJECT(), ...

Generated Virtual/Stored ColumnsFunction Indexes

based on JSON datatype and functions

Page 7: MySQL Document Store: Under the Hood

MYSQL 5.7'S JSON SUPPORTEXAMPLES

CREATE TABLE product ( doc JSON, _id CHAR(16) AS (JSON_EXTRACT(doc, "$._id") STORED), PRIMARY KEY(_id));

INSERT INTO product (doc) VALUES ( " '_id': 1, 'name': 'Milk', 'properties': 'volume_l': 1, 'amount': 32 ");

SELECT JSON_OBJECT("amount", JSON_EXTRACT(doc, "$.properties.amount")) FROM product WHERE JSON_EXTRACT(doc, '$.name') == "Milk"

Page 8: MySQL Document Store: Under the Hood

X DEVAPIworking with Documents and Tablesfluent API

prod = sess.getSchema("prod") res = prod.users. find("$.name = 'Milk'"). fields(["name", "properties"])

Page 9: MySQL Document Store: Under the Hood

A NEW PROTOCOLThe common set of queries:

CRUD == many small PK readsqueries are independentplenty of data

sharding

Page 10: MySQL Document Store: Under the Hood

X PROTOCOLoptimize for common operations

no manatory handshakereduced message sizes

pipeliningexpectations

notifications

Page 11: MySQL Document Store: Under the Hood

X PROTOCOLClient

Client

Server

Server

Negotiation

Conn.GetCap()

Conn.Caps("tls": ...)

Authentication

Sess.AuthStart(mech="MySQL41")

Sess.AuthContinue

Sess.AuthContinue

Sess.AuthOk

Statements

Page 12: MySQL Document Store: Under the Hood

X PROTOCOLClient

Client

Server

Server

Authentication

Sess.AuthStart(mech="MySQL41")

Sess.AuthContinue

Sess.AuthContinue

Sess.AuthOk

Statements

Page 13: MySQL Document Store: Under the Hood

REDUCED MESSAGE SIZESmessage ColumnMeta optional name string = 1; optional orig_name string = 2; optional catalog string = 3; // ...

Note

don’t send orig_name if it is equal to namedon’t send catalog if it is “def”

Page 14: MySQL Document Store: Under the Hood

QUERY TIMEClient Network Server

stage time

network path latency 1ms

exectime 0.1ms

Page 15: MySQL Document Store: Under the Hood

CLASSIC REQUEST/RESPONSEClient

Client

Server

Server

Sql.StmtExecute("INSERT INTO ... (1)")

exec

Sql.StmtExecuteOk()

Sql.StmtExecute("INSERT INTO ... (2)")

exec

Sql.StmtExecuteOk()

total: 4x path + 2x exectime = 4.2ms

Page 16: MySQL Document Store: Under the Hood

PIPELININGClient

Client

Server

Server

packetSql.StmtExecute("INSERT INTO ... (1)")

Sql.StmtExecute("INSERT INTO ... (2)")

exec, exec

packetSql.StmtExecuteOk()

Sql.StmtExecuteOk()

total: 2x path + 2x exectime = 2.2ms

Page 17: MySQL Document Store: Under the Hood

MANAGING EXPECTATIONSPipelining makes error handling harder

Assumptions are usually wrongFail early? Fail late?

StatementReplication Lag?Max Execution Time

Page 18: MySQL Document Store: Under the Hood

MANAGING EXPECTATIONSFAIL EARLY

Client

Client

Server

Server

Expect.Open([+no_error])

Sql.StmtExecute("INSERT INTO ... (1)")

Sql.StmtExecute("INSERT INTO ... (1)")

Sql.StmtExecute("INSERT INTO ... (2)")

Expect.Close()

Ok()

Sql.StmtExecuteOk()

Error("Dup Key")

Error("Expectation Failed")

Ok()

Page 19: MySQL Document Store: Under the Hood

MANAGING EXPECTATIONS IIINESTING

Expect.Open([+no_error]) PrepStmt.Prepare(id=1, "INSERT INTO ... (?)")

Expect.Open([+no_error]) PrepStmt.Execute(id=1, values=[1]) PrepStmt.Execute(id=1, values=[2]) Expect.Close()

PrepStmt.Close(id=1) Expect.Close()

Page 20: MySQL Document Store: Under the Hood

MANAGING EXPECTATIONS IVNESTING, PYTHON STYLE

try: prep = prepare(..) try: prep.execute([1]) prep.execute([1]) finally: prep.close() except Exception: pass

Page 21: MySQL Document Store: Under the Hood

NOTIFICATIONSserver to clientcan appear at any timelocal events belong to current statement

warningsstatus variable changes

Page 22: MySQL Document Store: Under the Hood

NOTIFICATIONSGLOBAL EVENTS

async notification channelglobal events

server going downconnection timed outmembership changed: node le group

foundation for message bus, pub/sub, ...

Page 23: MySQL Document Store: Under the Hood

BUILDING BLOCKSProtobuf for serializationSASL methods for authentication

Simple Authentication Security LayerPLAIN over TLSMYSQL41

TLS for encryption

Page 24: MySQL Document Store: Under the Hood

PROTOBUFmessage Find required Collection collection = 2;

optional DataModel data_model = 3; repeated Projection projection = 4; optional Mysqlx.Expr.Expr criteria = 5; repeated Mysqlx.Datatypes.Scalar args = 11; optional Limit limit = 6; repeated Order order = 7; repeated Mysqlx.Expr.Expr grouping = 8; optional Mysqlx.Expr.Expr grouping_criteria = 9; ;

Page 25: MySQL Document Store: Under the Hood

HOW TO WRITE YOUR OWN CLIENTSpec:

Message Def:

Protobuf

http://dev.mysql.com/doc/internals/en/x-protocol.html

https://github.com/mysql/mysql-server/tree/5.7/rapid/plugin/x/protocol

$ protoc ­I ­­python_out=... .../mysql.proto

Page 26: MySQL Document Store: Under the Hood

Questions, please.

Q&A