Anatomy of a Proxy Server - Percona · 5 What is a Database Proxy? A Database Proxy is a...

34
Anatomy of a Proxy Server MaxScale Internals Ivan Zoratti - Percona Live Europe - September 2015

Transcript of Anatomy of a Proxy Server - Percona · 5 What is a Database Proxy? A Database Proxy is a...

Anatomy of a Proxy ServerMaxScale Internals

Ivan Zoratti - Percona Live Europe - September 2015

2

Meet ScaleDB Inc.

• Database company based in Menlo Park

• Developers ofIDC - Intelligent Data Cluster andUDE - Universal Data Engine

• Main focus on Streaming Data, Time Series and Real Time Analytics, all in one Engine

• Put it simply,Big Data on MySQL

• Try the Free version: www.scaledb.com

A Bit of “Externals”

4

What is a Database Proxy?

A Database Proxy is a specialised Proxy Server

5

What is a Database Proxy?

A Database Proxy is a specialised Proxy Server

• A Database Proxy is an intermediary for requests from client applications or other databases

• A Database Proxy• Receives a request• Evaluates the request and acts accordingly

• Connects to a database, directs the request to a database server, discards the request, logs the request etc.

• Follows up on future events associated to the request• Receives data from a database server, prepares and send a response etc.

6

What is MaxScale?

MaxScale is an event-based, I/O dispatcher that acts as a database proxy.

MaxScale can be used to capture, analyze, log, transform, reroute incoming requests from a frontend (client) to a backend (server) and the outcoming results from the backend to the frontend

• Client / Frontend - One or more applications, web servers, database servers etc.• Server / Backend - One or more database servers, proxy servers, message brokers etc.

7

MaxScale - Prehistory and History

MySQLProxy

8

MaxScale - Prehistory and History

MySQLProxy

9

MaxScale - Prehistory and History

Apache-basedprototypeMySQL

Proxy

10

MaxScale - Prehistory and History

Apache-basedprototypeMySQL

Proxy

11

MaxScale - Prehistory and History

Apache-basedprototypeMySQL

Proxy

epoll architecture & MaxScale 0.1

12

MaxScale Modern Days

• Active Project at MariaDB Corporation (renamed MariaDB MaxScale)• https://github.com/mariadb-corporation/MaxScale• Used in production in many companies (including Booking.com), MariaDB and

Percona customers• More projects are on going at ScaleDB and other companies

Current version: 1.2

• Packaged for Ubuntu/Debian and CentOS/RH• Running as a service• Many modules already available

MaxScale Architecture and Implementation

14

The MaxScale Pentagon

15

MaxScale Modules

• Routers: ReadConn Route, ReadWrite Split, Schema Router, Master/Master

• Monitors: MySQL Replication, MySQL Cluster/NDB, Galera• Filters: Firewall, Query Log, Query Routing, RabbitMQ, Query Re-

write, Tee, Query Top• Authentication: MySQL Authentication (up to MySQL 5.6)• Protocol: MySQL Client, MySQL Server

16

MaxScale and MySQL Replication

17

MaxScale and Galera

18

MaxScale Master/Master

19

MaxScale Cop :-)

20

MaxScale Binlog Server

MaxScale Internals

22

MaxScale Design

The Three Golden Rules

1. Never wait for a response2. Network sockets must be non-

blocking3. Do not use mutexes that cause

I/O threads queueing

23

MaxScale Design

• NOT a database• “Object Orientated”• Pure multi-threading• Non Preemptive• Event-driven I/O model

• Events are queued for processing by a thread

• Implementation of IOCP• epoll abstraction• Self contained, isolated

plugins

The Three Golden Rules

1. Never wait for a response2. Network sockets must be non-

blocking3. Do not use mutexes that cause

I/O threads queueing

24

Event Model Implementation

• epoll Implementation of event abstraction• poll_init > epoll_create• poll_add_dcb• poll_remove_dcb• poll_waitevents > polling and event dispatching with timeout bias• pollq > choose an event to be executed by a thread• poll_add_event_to_dcb

• The event queue is implemented within the DCB structure (DCBEVENTQ)• Next/Previous event list• Set of pending events• Set of processing event• Processing flag• Event queue lock

• Event process:• Lock dcb->evq structure• Copy pending events to processing events• Set processing flag• Unlock dcb->evq structure

25

Functions & Classes

• spinlock• acquire - wait for spinlock to

be available• acquire_nowait• release• Not to be used when

making system calls

26

Functions & Classes

• spinlock• acquire - wait for spinlock to

be available• acquire_nowait• release• Not to be used when

making system calls

27

Functions & Classes

• spinlock• atomic_add

• Basis for spinlock• Add signed value

and return the previous value

28

Functions & Classes

• spinlock• atomic_add• GWBUF

• Sharing buffers between different threads without copies

• Chopping buffers up without copy• Creating lists of buffers for

read/write (scatter/gather approach)

29

DCB - Descriptor Control Block

• One DCB per connection• DCB holds the connection state

• Registered with epoll• Buffer queues

• DCB->writeq• DCB->delayq• DCB->dcb_readqueue

Operations

• connect - Create a connection to the specified server using a specific protocol for the session

• read - Read data from a socket• write - Data write entry point (generic)• close - Remove the DCB from the poll set• write_ready - EPOLLOUT handler for the socket,

indicates that the socket is ready to send more data• error - EPOLLERR handler for the socket• hangup - EPOLLHUP handler for the socket• accept - Accept handler for listener socket only

30

Plugins - An Example

Filter Plugin (QLA)

31

Plugins - An Example

Filter Plugin (QLA)

32

Plugins - An Example

Filter Plugin (QLA)

33

The Future of MaxScale

• Memory Allocation and optimisation• Authentication Plugin• Parser Plugin (and optimisation)• Protocol Plugins• Router Plugins

Thank You!

Images● MySQL 2007 Logo - Courtesy of Oracle Corporation● ScaleBase Logo - Courtesy of ScaleArc● Jan Kneschke, Massimiliano Pinto, Massimo Brignoli, Mark Riddoch - Courtesy of Twitter and Percona● Jean-Francois Gagne Presentation - http://www.slideshare.net/JeanFranoisGagn/binlog-servers-at-bookingcom?qid=1fdc1af3-5658-400e-

8a9c-964ad40319c1&v=qf1&b=&from_search=3 ● Caffe Trieste - Christopher Michel - https://www.flickr.com/photos/cmichel67/17596540570/in/photostream/