My sql tutorial-oscon-2012

142
Copyright © 2011 Oracle Corp. <Insert Picture Here> Welcome to OSCON 2012 1 MySQL Cluster and NoSQL If you want to run a node in a multi-node cluster: Pick a seat where there is a network cable Plug in Turn off your firewall (or at least allow ports 1186 & 2186) Point your browser to http://uplink/ OR, to run a one-node cluster contained entirely on your own laptop, sit away from the network cables and use the conference wireless. 1

description

 

Transcript of My sql tutorial-oscon-2012

Page 1: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

<Insert Picture Here>

Welcome to OSCON 2012

1

MySQL Cluster and NoSQL

• If you want to run a node in a multi-node cluster:• Pick a seat where there is a network cable• Plug in• Turn off your firewall (or at least allow ports 1186 & 2186)• Point your browser to http://uplink/

• OR, to run a one-node cluster contained entirely on your own laptop, sit away from the network cables and use the conference wireless.

1

Page 2: My sql tutorial-oscon-2012

<Insert Picture Here>

MySQL Cluster and NoSQLJohn David DuncanCraig L RussellMySQL Cluster EngineeringOracle Corp

2

Page 3: My sql tutorial-oscon-2012

Everyone needs the tutorial files!

3

• oscon-2012-mysql-nosql.tar – or

• oscon-2012-mysql-nosql.zip– or

• oscon-2012-mysql-nosql-MSDOS.zip

• From USB stick• From uplink• From github

git clone git://github.com/jdduncan/oscon-2012-mysql-nosql.git

3

Page 4: My sql tutorial-oscon-2012

Platform Census Time

4

4

Page 5: My sql tutorial-oscon-2012

MySQL Cluster and NoSQL

• There are 3 handouts– README for Java / Twitter development exercise– MySQL pathname reference– Memcache reference card (2-sided)

• If you still need MySQL Cluster 7.2.6 and Cache::Memcached, they are on the USB sticks – go get ‘em

• If you want to be in a multi-node cluster with other people, find a network cable

5

5

Page 6: My sql tutorial-oscon-2012

<Insert Picture Here>

Big Picture Agenda

• First half: deploy cluster – Some lectures– Some lab - run a cluster

• Second half: develop apps– Some lectures - Memcached & Java– Some lab - you write the code

6

6

Page 7: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Presenter's "Baggage"

• Former Systems Administrator, web developer, MySQL DBA

• Joined MySQL AB in 2004

Image used under a Creative Commons license from Flickr user Digiart2001

7

J.D. Duncan

7

Page 8: My sql tutorial-oscon-2012

Presenter's "Baggage"

• Systems Architect focused on Enterprise Java

• Specification Lead for Java Data Objects

• Architect of Container-Managed Persistence

• Likes SQL well enough• Likes well-designed APIs

even moreImage used under a Creative Commons license from Flickr user Digiart2001

8

Craig L. Russell

8

Page 9: My sql tutorial-oscon-2012

<Insert Picture Here>

9

1:30 - 3:00 P.M Agenda

• Seating & Introductions (10 min.)• MySQL Cluster Essentials (JD - 25 min.)• Hands-on MySQL Cluster setup (35 min.)• API Overviews (20 min.)

–ClusterJ (Craig)–Memcache (JD)–Node.js Connector Project

9

Page 10: My sql tutorial-oscon-2012

<Insert Picture Here>

MySQL Cluster Essentials

10

10

Page 11: My sql tutorial-oscon-2012

What is a database?

11

• It’s a computer system that manages data ...in a useful way.

11

Page 12: My sql tutorial-oscon-2012

How do database systems make themselves useful?

12

• Persistent storage– in “the system of record”

• Indexed retrieval– the database gets big, but retrieval time remains small

• Central enforcement of data integrity– Every Social Security Number has 9 digits– Every order has a billing address

• Maintain an illusion of consistency– isolating a transcation other concurrent changes

12

Page 13: My sql tutorial-oscon-2012

How is the data structured?

• Key : Value– Hash table (or memcache):

• lookups only• no scans

– Ordered Index (or binary tree):• Lookups and scans both.

• Key : Structured Value – redis: common data structures used in application code – mongodb: structured documents, possible secondary indexes– dynamo: schema-free tables

• Relational (Tables with a schema)

13

13

Page 14: My sql tutorial-oscon-2012

Document Databases

• Examples: MongoDB, CouchDB• Expected lookup:

– “Fetch this patient’s record” ✓• Unexepected lookup:

– “Who took Accutane after 1990?” x

14

14

Page 15: My sql tutorial-oscon-2012

Relational Databases

• In a truly normalized relational schema, no table is more central than any other.

• “Fetch this patient’s record” ✓

• “Did more of our patients get the flu in 2008 or in 2009?” ✓

• “Which doctor prescribes the most prednisone?” ✓

15

15

Page 16: My sql tutorial-oscon-2012

But what about schema changes?

16

16

Page 17: My sql tutorial-oscon-2012

But what about schema changes?

17

They need to be online operations.

17

Page 18: My sql tutorial-oscon-2012

In the last few minutes we actually covered a lot of important theory.

• But here’s one more bit: consistency vs. availability.• Look at MySQL 4.0, circa 2000

18

Master

Slave

Slave

Slave

18

Page 19: My sql tutorial-oscon-2012

What happens when a link goes down?

19

Master

Slave

Slave

Slave

• CAP (Eric Brewer): You have to choose. You can’t have both. Commercial databases usually chose C, but MySQL replication chose A.

19

Page 20: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

<Insert Picture Here>

Now We Are Ready

20

MySQL Cluster

20

Page 21: My sql tutorial-oscon-2012

21

3

Data Nodes

Node Group 1

F1

F3

F3

F1

Nod

e 1

Nod

e 2

Node Group 2

F2

F4

F4

F2N

ode

3N

ode

4

NDB Cluster Ericsson, 1997

• Hardware Design – Inexpensive x86 hardware– "Shared nothing" clustering

• Software Design– 99.999% availability– Guaranteed consistency– Parallel execution– Commit at speed of

network (rather than disk)

21

Page 22: My sql tutorial-oscon-2012

NDB Cluster: Where does it fit?

• Consistent or Available?– Availability is important. NDB is designed to remain running if

one node in a node group fails. – But consistency is even more important. If all nodes in a

node group fail, the database will shut itself down.• Relational? Yes.

– Hashed primary key for data distribution– Secondary Unique indexes– Ordered indexes for scans– Online schema changes.

• SQL? No.– C++ NDB API for object-relational application development.

22

22

Page 23: My sql tutorial-oscon-2012

2003: MySQL + NDB Cluster = MySQL Cluster

• MySQL:– built for cheap linux boxes– free, popular, revolutionary, fast, easy to use– proven ability to run with different back-end data stores, e.g.

MyISAM and InnoDB.– MySQL replication has asynchronous, best-effort design that

prioritizes availability

• NDB:– built for cheap linux boxes– high performance and availability– had no support for SQL, or ODBC, or JDBC

23

23

Page 24: My sql tutorial-oscon-2012

The Basics

24

• Management server– read the one central configuration file– allow other nodes to join – distribute the configuration to them– get the cluster up & running

• Data Node– ndbd (single-threaded) or ndbmtd (multi-threaded)– Stores data and indexes– Manage all transactions and operations for API nodes

• API Node– Join the cluster as a member node with node id– e.g. mysqld, memcached, JVM running ClusterJ application

• Client– MySQL or Memcache client; not a member of the cluster

24

Page 25: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

NDB Data Nodes

API Nodes

Node Group 1 Node Group 2

Big Picture

25

25

Page 26: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

NOT a shared-disk architecture

26

Application Application Application

DBMSServer

Instance

DBMSServer

Instance

DBMSServer

Instance

Disk

26

Page 27: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

“Shared-Nothing” Architecture

27

Application Application Application

DBMSServer

Instance

DBMSServer

Instance

DBMSServer

Instance

DiskDisk Disk

27

Page 28: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp. 28

• Data changes take place on all the servers in coordination • The ”two-phase commit” (2PC) is the most commonly used protocol

– The changes are only committed after they’ve been succesfully executed on all servers

– TC (Transaction Coordinator) manages 2PC– LQH (Local Query Handler) at each data node manages local

transaction

Data Node 1LQH

Data Node 2 LQH

API

TC

Synchronous data replication

28

Page 29: My sql tutorial-oscon-2012

29

Heartbeat Circle

DBNode 1

Nodes organizedin a logical circle

Heartbeat messagessent to next node in circle

DBNode 2

DBNode 3

DBNode 4

29

Page 30: My sql tutorial-oscon-2012

30

Heartbeat Failure Example

DBNode 1

DBNode 2

DBNode 3

DBNode 4

x

Node 1 fails to sendheartbeats three consecutive times

It is declared as dead and removedfrom the cluster

30

Page 31: My sql tutorial-oscon-2012

31

Network Partitioning

• The servers are organised in a network• What happens if the servers lose contact to each other?

• Neither side can continue as this would lead to inconsistency

Server

Server

Server

Server

“Split brain”

Server

31

Page 32: My sql tutorial-oscon-2012

32

Using an Arbitrator

• In order to handle an even number of servers an arbitrator is added

• When a majority is needed by a set of servers, the arbitrator “votes” for that set, making it a majority

• The arbitrator must not vote for several different subsets and the arbitrator must be known to all servers

• By using an arbitrator the two server system becomes more fault tolerant than a single server system

Server Server

Arbitrator

32

Page 33: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Who Uses MySQL Cluster?Until 2010: Telco Networks & Network Equipment Makers

33

33

Page 34: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Who Uses MySQL Cluster?Since 2010: Web & Online gaming

34

• Session management• Shopping carts• Game play• Internal site monitoring

34

Page 35: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

MySQL Cluster TodayACID Compliant Relational Database

SQL & NoSQL interfaces

Write-Scalable & Real-TimeDistributed, auto-partitioning (sharding), multi-master

99.999% Availability Shared-nothing, integrated clustering & sub-second recovery, local & geographic replication, on-line operations

Low TCOOpen-source, management & monitoring tools, scale-out on commodity hardware

35

35

Page 36: My sql tutorial-oscon-2012

Online Maintenance

36

• Online schema changes– ALTER TABLE ...

• Online backup– START BACKUP ...

• Online storage addition– e.g. from 4 data nodes to 6

36

Page 37: My sql tutorial-oscon-2012

The long road to SQL JOIN performance

• Initial integration (2004)– Move the data to the query– ... one row at a time– ... one loop iteration in a nested-loop join = 1 network trip

• 2005 - 2009 – Improve efficiency ...– within the existing framework of the MySQL optimizer– e.g. Batch Key Access, push-down filters

• MySQL Cluster 7.2 (2011)– Adaptive Query Localization– Algorithmic changes to optimizer– Push the majority of the query to the data nodes

37

37

Page 38: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Results: Adaptive Query Localization in MySQL Cluster 7.2

Before: 48.68 sec

mysql> SELECT COUNT(*) FROM residents, postcodes, towns WHERE residents.postcode=postcodes.postcode AND postcodes.town=towns.town AND towns.county="Berkshire";

+----------+ | COUNT(*) | +----------+ | 40001 | +----------+

• After: 2.02 sec

38

38

Page 39: My sql tutorial-oscon-2012

Tour

39

39

Page 40: My sql tutorial-oscon-2012

Tour Stop 1: Config File

40

• A single central file for the whole cluster• Loaded by the management server on --initial• And then stored in cache for future management server

restarts• Sections:

– [MGM]– [DB DEFAULT]– [NDBD]– [API]

40

Page 41: My sql tutorial-oscon-2012

Tour Stop: Management Server

41

[MGM]NodeId=1datadir=/Users/jdd/sandbox/ndbhostname=localhost

% ndb_mgmd -f config-file-path

41

Page 42: My sql tutorial-oscon-2012

Tour Stop: Data Nodes

42

[DB DEFAULT]NoOfReplicas=1TimeBetweenGlobalCheckpoints=12000HeartbeatIntervalDbApi=15000DataMemory=180MIndexMemory=60M

42

Page 43: My sql tutorial-oscon-2012

Tour Stop: Data Nodes

43

[NDBD]NodeId=2

% ndbd -c mgm_host:mgm_port

43

Page 44: My sql tutorial-oscon-2012

Tour Stop: API Nodes

44

[API][API][API][API][API][API][API]

44

Page 45: My sql tutorial-oscon-2012

Tour Stop: API Nodes

45

• mysqld, memcached, java, etc.– Java API nodes connect to clusters based on Java connection

properties– mysqld connects to cluster based on my.cnf file– memcached connects based on command-line options

45

Page 46: My sql tutorial-oscon-2012

Tour Stop: Management & Monitoring

46

• From the mysql server– SQL for table creation, etc.– ndbinfo schema

• e.g. SELECT * FROM ndbinfo.memoryusage;– SHOW ENGINE NDB STATUS;

• Management Clients– ndb_mgm

• Toolsets– From Oracle: MySQL Cluster Manager– Third party tools: severalnines.com

46

Page 47: My sql tutorial-oscon-2012

<Insert Picture Here>

MySQL Cluster Hands-On Lab

47

47

Page 48: My sql tutorial-oscon-2012

<Insert Picture Here>

MySQL Cluster Hands-On Lab

48

• Solo Clusters– Your config file is here:– run-cluster/ndb/solo-cluster.ini

• Multi-Node Clusters– http://uplink/– 1 managment server per cluster– 2, 4, or 6 data nodes per cluster– Others can run API nodes – MGM and NDB nodes register with the web app– Then the web app generates a config file for the management

server and you (mgm server person) download it and save it in run-cluster/ndb/

48

Page 49: My sql tutorial-oscon-2012

<Insert Picture Here>

MySQL Cluster Hands-On Lab

49

• Open 4 or more terminal windows

• Set some pathnames– Check your handout!

–export PATH=/usr/local/mysql/bin:$PATH– (or whatever is correct for your platform)– cd into oscon-2012-mysql-nosql

49

Page 50: My sql tutorial-oscon-2012

Start your management servers!

• Wireless:•cd run-cluster/ndb•ndb_mgmd --initial -f solo-cluster.ini --configdir=.

50

• Wired (MGM person only!): •save cluster.ini from uplink •cd run-cluster/ndb•ndb_mgmd --initial -f cluster.ini --configir=.

50

Page 51: My sql tutorial-oscon-2012

What’s going on with this cluster?

• Start a management client!• (And leave it running in its window)• Wireless:•ndb_mgm -c localhost:1186

• Wired:•Where is your management server?•http://uplink should tell you•ndb_mgm -c box-xx:1186

51

51

Page 52: My sql tutorial-oscon-2012

Start your data nodes!

52

• You are still in run-cluster/ndb• And you remember your connectstring ...• So:

ndbd -c <connectstring>

52

Page 53: My sql tutorial-oscon-2012

What’s going on with this cluster?

53

ndb_mgm> all statusNode 2: starting (Last completed phase 2) ...

ndb_mgm> showCluster Configuration---------------------

53

Page 54: My sql tutorial-oscon-2012

Configure a MySQL server

54

• Create a my.cnf file– cd run-cluster/sql– ./create-config.sh– ls (you should see my.cnf)

• Wireless: leave it as-is• Wired: edit the connectstring

54

Page 55: My sql tutorial-oscon-2012

Here’s what my own my.cnf looks like

55

[mysqld]ndbclusteruser=jdddatadir=/Users/jdd/[...]/run-cluster/sqlndb-connectstring=localhost:1186

55

Page 56: My sql tutorial-oscon-2012

Configure a MySQL server

56

cd oscon-2012-mysql-nosql. pathnames.shcd run-cluster/sql$MYSQL_INSTALL_DIR/scripts/msyql_install_db \ --datadir=. \ --basedir=$MYSQL_INSTALL_DIR

56

Page 57: My sql tutorial-oscon-2012

Configure a MySQL server

57

Installing MySQL system tables...OKFilling help tables...OK...

57

Page 58: My sql tutorial-oscon-2012

Now start the mysqld

58

mysqld_safe --defaults-file=./my.cnf

mysqld_safe Starting mysqld daemon with databases from ...

58

Page 59: My sql tutorial-oscon-2012

MySQL Client

59

$ mysql -u root

Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2

59

Page 60: My sql tutorial-oscon-2012

MySQL Client

60

mysql> select * From ndbinfo.nodes;

+---------+--------+---------+-------------+-------------------+| node_id | uptime | status | start_phase | config_generation |+---------+--------+---------+-------------+-------------------+| 2 | 3453 | STARTED | 0 | 1 |+---------+--------+---------+-------------+-------------------+1 row in set (0.00 sec)

60

Page 61: My sql tutorial-oscon-2012

Create some tables (from the shell)

61

• We need to run two SQL scripts:• One called ndb_memcache_metadata.sql in$MYSQL_HOME/share/memcache-api/

• Plus java/labs/schema/create.sql from oscon-2012-mysql-nosql

% mysql -u root < filename

61

Page 62: My sql tutorial-oscon-2012

MySQL Client

62

mysql> use test

mysql> show tables ;

+----------------+| Tables_in_test |+----------------+| hashtags || tweets |+----------------+

62

Page 63: My sql tutorial-oscon-2012

Make a backup!

63

ndb_mgm> start backup

Waiting for completed, this may take several minutesNode 2: Backup 1 started from node 1Node 2: Backup 1 started from node 1 completed StartGCP: 351 StopGCP: 354 #Records: 2058 #LogRecords: 0 Data: 51728 bytes Log: 0 bytes

63

Page 64: My sql tutorial-oscon-2012

Things you might do if we had more time

64

• Set up cluster-to-cluster replication • uses a designated mysql server in each cluster

• Online alter table operations• ALTER ONLINE TABLE x ADD i int NULL;• (note that some ALTER operations can be done

online and some others cannot)• Rolling Restart• Expand a cluster online (add nodes)

64

Page 65: My sql tutorial-oscon-2012

<Insert Picture Here>

API Overviews

• ClusterJ (Craig, 10 min.)• Memcache (JD, 5 min.)• Node.JS project (JD & Craig, 10 min.)• Break time

65

65

Page 66: My sql tutorial-oscon-2012

<Insert Picture Here>

ClusterJ OverviewCraig L RussellArchitect, Oracle Corp.

66

Page 67: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ

• High Performance, Light Weight, Easy to Use Direct Connection– In the style of Hibernate / JPA / JDO– Insert, delete, find by key, update, simple query

• Shared Data storage with:– MySQL server– Native C++ applications– Other ClusterJ, memcached applications

• Domain Object Model DataMapper pattern– Data is represented as domain objects– Domain objects are separate from business logic– Domain objects are mapped to database tables

67

Page 68: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ as NoSQL Store

• Key/value Store– Arbitrary text storage (String <=> CLOB)– Arbitrary data storage (byte[ ] <=> BLOB)

• Document Store– JSON, XML, etc.

• Session Store– key = session id– value = serialized session state

• Back end for REST plugin

68

Page 69: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Domain Object Model Mapping

• Tables map to Persistent Interfaces / Classes• Columns map to Persistent Properties

– column names default to property name• Rows map to Persistent Instances• Annotations on Interfaces / Classes customize mappings• User chooses which to write:

– User interface (ClusterJ then generates implementation class)– Persistent class (ClusterJ provides base implementation class)

69

Page 70: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ – Generated Class@PersistenceCapable(table="tweets")public interface Tweets {

String getId(); void setId(String id);

String getAuthor(); void setAuthor(String value);

Date getTime_stamp(); void setTime_stamp(Date value);

String getTweet(); void setTweet(String value);}

70

Page 71: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ – Dynamic Objectpublic class Tweets extends DynamicObject {

public String table() { return "tweets";

String getId() { return (String)get(0); } void setId(String value) { set(0, value); }...// other fields and behavior}

71

Page 72: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Numeric Column Mapping• Java boolean, Boolean

– BIT(1)• Java byte, Byte

– BIT(1) to BIT(8)– TINYINT

• Java short, Short– BIT(1) to BIT(16)– SMALLINT– YEAR

• Java int, Integer– BIT(1) to BIT(32)– INT

• Java long, Long– BIT(1) to BIT(64)– BIGINT– BIGUNSIGNED

• Java float, Float– FLOAT

• Java double, Double– DOUBLE

• Java BigDecimal– NUMERIC– DECIMAL

• Java BigInteger– NUMERIC– DECIMAL

72

Page 73: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Date Column Mapping

• Java util Date– DATETIME– TIMESTAMP– TIME– DATE

• Java sql Date– DATE

• Java sql Time– TIME

• Java sql Timestamp– DATETIME– TIMESTAMP

73

Page 74: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Variable Size Column Mapping

• Java String– CHAR– VARCHAR– TEXT

• Java byte[ ]– BINARY– VARBINARY– BLOB

74

Page 75: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Features

• Character Set Translation (all MySQL charsets)• Automatic detection of primary keys, indexes• Compound Primary Keys• Ordered (btree) indexes• Unique (hash) indexes• Automatic use of partition key• Multi-threaded applications

75

Page 76: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Interfaces

• SessionFactory– Instance per connection to cluster

• Session– Instance per "user"

• Transaction– Instance per Session

• Query– Multiple instances per Session

76

Page 77: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ User View

SessionFactory Configuration Properties

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

77

Page 78: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Limitations• No Relationships

– primitive types only• No Multi-table inheritance

– single table per persistent interface or class• No joins in queries

– column comparisons and boolean operators• No Table creation

– user needs to create tables and indexes• No Lazy Loading

– entire record is loaded at one time, including LOBs

78

Page 79: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Requirements

• Java 1.5 or 1.6– Compile-time jar:

• clusterj-api– Run-time jar:

• clusterj– Java Native Library

• ndbclient• MySQL Cluster 7.1 or higher

– includes jars and native libraries as above

79

Page 80: My sql tutorial-oscon-2012

<Insert Picture Here>

NDB Memcache API Overview

80

Page 81: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Memcached Overview:Two levels of hashing

Memcache

httpd memcached

memcached

memcachedmemcache key

PHP/Perl

friends:12389

hash key to find data

hash key to pick server

81

81

Page 82: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Expected Latency & Throughput

memcache

httpd

PHP/Perl

MySQL Slave

1,000s of operations/sec.~ 2 ms round trip

10,000s of operations/sec.

~ 200 µs round trip

mysql

memcached

82

82

Page 83: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Goals • Access stored data directly from memcache clients

– Memcached perspective: • MySQL Cluster is a write-scalable, replicated data store

– with reliable in-memory storage,– plus on-disk storage when data is too big for

memory.– MySQL Cluster perspective:

• memcache is a high performance API– providing easy access to in-memory data,– plus an extra layer of caching when data is on disk.

83

83

Page 84: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Memcache Protocol & Commands

84

see handout!

84

Page 85: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Anatomy of a Memcache key

user:1248

the prefix the database key

85

85

Page 86: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

A key-prefix mapping

Memcache key

prefixCluster Container Cache

Policy

86

86

Page 87: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

keyprefix Cluster Con-

tainerCache Policy

ServerRole

ID

A memcache server role

keyprefix Cluster Con-

tainerCache Policy

keyprefix Cluster Con-

tainerCache Policy

keyprefix Cluster Con-

tainerCache Policy

87

87

Page 88: My sql tutorial-oscon-2012

<Insert Picture Here>

Mynode PreviewCraig L RussellArchitect, Oracle Corp.

88

Page 89: My sql tutorial-oscon-2012

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

89

89

Page 90: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode: Node.js modulefor MySQL

• Advance Planning – availability not announced• Domain Object Model DataMapper pattern

– Domain objects are mapped to database tables• High Performance, Light Weight, Easy to Use Direct Connection

– Asynchronous API– Insert, delete, find by key, update, simple query

• Shared Data storage with:– MySQL server– Native C++ applications– Other Mynode, ClusterJ, memcached applications

90

Page 91: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Domain Object Model Mapping

• Tables map to Persistent Classes• Columns map to Persistent Properties

– column names default to property name• Rows map to Persistent Instances• Annotations customize mappings

– column names– table names– null value handling

91

Page 92: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode – Mapped Class

function Tweets(author, tweet) {

this.id = randomUUID();

this.author = author;

this.tweet = tweet;

this.time_stamp = new Date();

};

92

Page 93: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode – Insert

var tweet = new Tweet('clr', 'having a great #oscon');

insertTweet(tweet, function()...);

function insertTweet(tweet, callback) {

session.insert('tweets', tweet, function(err) { if (err) throw … callback(); }

};

93

Page 94: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode – Find by Primary Key

function getTweet(id, callback) {

session.find('tweets', id, function(err, tweet) { if (err) throw … callback(tweet); }

};

94

Page 95: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode Features

• Character Set Translation (all MySQL charsets)• Automatic detection of primary keys, indexes• Compound Primary Keys• Automatic use of partition key• Multi-threaded applications

95

Page 96: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode Classes

• Mynode– SessionFactory factory– Establishes connection to database

• SessionFactory– Instance per connection to database

• Session– Instance per "user"

96

Page 97: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode User View

SessionFactory Configuration Properties

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

97

Page 98: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Mynode Requirements

• JavaScript• Node.js

– Run-time module:• mynode

– Java Native Library• ndbclient

• MySQL Cluster 7.2 or higher

98

Page 99: My sql tutorial-oscon-2012

One more thing before the break ...

99

DBNode 1

Heartbeat messagessent to next node in circle

DBNode 2

DBNode 3

DBNode 4

99

Page 100: My sql tutorial-oscon-2012

You want to shut down?

100

mysqladmin -u root shutdown MySQL Servers:

Management and Data Nodes:

ndb_mgm> shutdown

100

Page 101: My sql tutorial-oscon-2012

<Insert Picture Here>

MySQL Cluster and NoSQLMid-Session Break

101

Page 102: My sql tutorial-oscon-2012

<Insert Picture Here>

102

3:30 - 5:00 P.M Agenda

–SQL DDL Lecture–Restart your clusters–Labs

– Java Insert–Time for Coding– Memcache Insert– Time for Coding– Java Query– Time for Coding

–Wrap-up with Q&A

102

Page 103: My sql tutorial-oscon-2012

<Insert Picture Here>

103

Data Definition,SQL,and NDB Indexes

103

Page 104: My sql tutorial-oscon-2012

<Insert Picture Here>

Data Definition: Distribution Keys

104

• Every table has a distribution key. The MD5 hash of the distribution key determines which fragment a row belongs to.

• When an API node knows the distribution key of an interesting row, it can take advantage of this by choosing the primary fragment as TC. (ClusterJ, MySQL, and Memcached all do this automatically).

104

Page 105: My sql tutorial-oscon-2012

Data Definition: Ordered Indexes

105

• An ordered index is an in-memory T-Tree index

• It is partitioned in exactly the same way as the main table. – i.e.: each data node has its own ordered index

covering its own fragments of the table

• This in-memory T-Tree contains direct pointers to the in-memory rows of the main table.

• The index is not stored on disk between restarts; it is rebuilt during node startup.

105

Page 106: My sql tutorial-oscon-2012

Data Definition: The SQL primary key

106

• By default, MySQL Cluster does two things with the primary key of a table:–Uses it as the distribution key–Creates an ordered index named PRIMARY.

• This means a table’s primary key can be used both for lookups and for scans

• You can override the default behavior:–PARTITION BY HASH (col1, col2) to get a

different distribution key–PRIMARY KEY USING HASH to skip building the

ordered index

106

Page 107: My sql tutorial-oscon-2012

<Insert Picture Here>

Data Definition: Unique Indexes

107

• A unique index is a secondary hash index.• Used to enforce a unique constraint• Used for lookups, but not scans• Implemented as a hidden table:

– key: the unique index =>– value: the primary key of the main table

• Because it’s really an independent table, it has its own independent distribution across fragments.

107

Page 108: My sql tutorial-oscon-2012

Data Definition: Indexes

108

• Data rows can be stored either in-memory, or on disk (in a tablespace).

• Data stored in memory is still durable, because the data nodes checkpoint it to disk

• Indexes, however, are always in memory. Never on disk.

• Therefore, a cluster must have enough memory for all its indexes.

108

Page 109: My sql tutorial-oscon-2012

Data Definition: Tweets

109

CREATE TABLE tweets ( id varchar(36) primary key, tweet varchar(140), time_stamp timestamp, author varchar(15), index(time_stamp), index(author))ENGINE=ndb;

109

Page 110: My sql tutorial-oscon-2012

Data Definition: Hashtags

110

CREATE TABLE hashtags ( hashtag varchar(20), tweet_id varchar(36), time_stamp timestamp, author varchar(15), primary key(hashtag, tweet_id))ENGINE=ndb;

110

Page 111: My sql tutorial-oscon-2012

<Insert Picture Here>

111

Restart clusters!

ndb_mgmd --configdir=.

ndbd -c <connectstring>

ndb_mgm -c <connectstring>

111

Page 112: My sql tutorial-oscon-2012

<Insert Picture Here>

ClusterJ APICraig L RussellArchitect, Oracle Corp.

112

Page 113: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ – Generated Class@PersistenceCapable(table="tweets")public interface Tweets {

String getId(); void setId(String id);

String getAuthor(); void setAuthor(String value);

Date getTime_stamp(); void setTime_stamp(Date value);

String getTweet(); void setTweet(String value);}

113

Page 114: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ – Dynamic Objectpublic class Tweets extends DynamicObject {

public String table() { return "tweets";

String getId() { return (String)get(0); } void setId(String value) { set(0, value); }...// other fields and behavior}

114

Page 115: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Numeric Column Mapping• Java boolean, Boolean

– BIT(1)• Java byte, Byte

– BIT(1) to BIT(8)– TINYINT

• Java short, Short– BIT(1) to BIT(16)– SMALLINT– YEAR

• Java int, Integer– BIT(1) to BIT(32)– INT

• Java long, Long– BIT(1) to BIT(64)– BIGINT– BIGUNSIGNED

• Java float, Float– FLOAT

• Java double, Double– DOUBLE

• Java BigDecimal– NUMERIC– DECIMAL

• Java BigInteger– NUMERIC– DECIMAL

115

Page 116: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Date Column Mapping

• Java util Date– DATETIME– TIMESTAMP– TIME– DATE

• Java sql Date– DATE

• Java sql Time– TIME

• Java sql Timestamp– DATETIME– TIMESTAMP

116

Page 117: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Variable Size Column Mapping

• Java String– CHAR– VARCHAR– TEXT

• Java byte[ ]– BINARY– VARBINARY– BLOB

117

Page 118: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Interfaces

• SessionFactory– Instance per connection to cluster

• Session– Instance per "user"

• Transaction– Instance per Session

• Query– Multiple instances per Session

118

Page 119: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ User View

SessionFactory Configuration Properties

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

Session andTransaction

Domain Object Domain

Object

Domain Object

Domain Object

119

Page 120: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

SessionFactory• Configured by properties

– com.mysql.clusterj.connectstring (the only really important property) – com.mysql.clusterj.connect.retries– com.mysql.clusterj.connect.delay– com.mysql.clusterj.connect.timeout.before– com.mysql.clusterj.connect.timeout.after– com.mysql.clusterj.max.transactions

• One SessionFactory per cluster per JVM– Connection poooling (multiple TCP connections per SessionFactory)

120

Page 121: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ExampleSessionFactory sessionFactory;Properties props = new Properties();

void initProps() {props.put(PROPERTY_CLUSTER_CONNECTSTRING, "localhost:1186");props.put(PROPERTY_CLUSTER_DATABASE, "personnel");}public init(Properties props) { initProps(); sessionFactory = ClusterJHelper .getSessionFactory(props);}

121

Page 122: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Session

• Persistent Instance Factory• Lifecycle Management

– persist() // record must not already exist– remove() // record must already exist

• Update Management– update() // no need to read first– write() // record will be overwritten if already exists

• Find by ID• Query Factory

122

Page 123: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Example InsertSession session;void getSession() { session = sessionFactory.getSession();}Tweets createTweet(String author, String message) { Tweets tweet = session.newInstance(Tweets.class); tweet.setId(UUID.randomUUID().toString(); tweet.setAuthor(author); tweet.setTweet(message); tweet.setTime_stamp(new Date()); session.persist(tweet); return tweet;}

123

Page 124: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Example FindTweets findTweet(String id) { Tweets tweet = session.find(Tweets.class, id); return tweet;}

124

Page 125: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

Transaction

• Optionally used to delimit transactions– begin()– commit()– rollback()

• Optionally mark a transaction for rollback– setRollbackOnly()– getRollbackOnly()

• If not used, every operation is in its own transaction

125

Page 126: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ExampleTransaction transaction;

void getTransaction() { transaction = session.getTransaction();}

void createTweets() { getTransaction(); transaction.begin(); createTweet("amos", "#oscon rocks"); createTweet("barb", "sw33t tutorial"); createTweet("chuck", "lets #partay"); createTweet("dave", "just one mo hour"); transaction.commit();}

126

Page 127: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Exercises

• Create schema for tweets, hashtags– create.sql in labs/schema

• Write domain models for Tweets, Hashtags– skeletons in labs/model

• Write Insert.java program– skeleton in labs/test/Insert.java

• Compile-time jar: clusterj-api-7.2.6.jar• Run-time jar: clusterj-7.2.6.jar• Java Native Library: ndbclient

127

Page 128: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Exercises

• Create schema for tweets, hashtags– create.sql in labs/schema

• Write domain models for Tweets, Hashtags– skeletons in labs/model

• Write Insert.java program– skeleton in labs/test/Insert.java

• Compile-time jar: clusterj-api-7.2.6.jar• Run-time jar: clusterj-7.2.6.jar• Java Native Library: ndbclient

128

Page 129: My sql tutorial-oscon-2012

<Insert Picture Here>

Memcache Insert

129

Page 130: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

A key-prefix mapping

Memcache key

prefixCluster Container Cache

Policy

130

130

Page 131: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Memcache Config

131

• Generate a containers record for the tweets table• value columns in the containers record are

separated by commas

• Generate a key-prefix mapping for role 0

• Start a memcached server

cd run-cluster/ndb./start-memcached.sh

131

Page 132: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Start Memcached

132

$ memcached -E /usr/local/mysql/lib/ndb_engine.so -e connectstring="localhost:1186"

15-Jul-2012 23:51:46 PDT NDB Memcache 5.5.22-ndb-7.2.6 started [NDB 7.2.6; MySQL 5.5.22]Contacting primary management server (localhost:1186) ... Connected to "localhost:1186" as node id 4.Retrieved 4 key prefixes for server role "default_role".The default behavior is that: GET uses NDB only SET uses NDB only DELETE uses NDB only.The 3 explicitly defined key prefixes are "b:" (demo_table_large), "mc:" () and "t:" (demo_table_tabs)Server started with 4 threads.Priming the pump ... Connected to "localhost:1186" as node id 5.Scheduler: using 2 connections to cluster 0Scheduler: starting for 1 cluster; c0,f0,g1,t1done [0.656 sec].

132

Page 133: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Memcache Exercises

133

• In perl/labs/ • 1. config.sql: create metadata for app. • 2. tweets.pl: insert tweets using memcached• 3. counter.sql: revise app to count tweets per user

133

Page 134: My sql tutorial-oscon-2012

Copyright © 2011 Oracle Corp.

Memcache tips

134

• perldoc Cache::Memcached• The file in labs/ already connects to a server for you.• In an actual Memcache command, the 3 fields of the

value should be separated by tab characters (“\t”)

134

Page 135: My sql tutorial-oscon-2012

<Insert Picture Here>

ClusterJ QueryCraig L RussellArchitect, Oracle Corp.

135

Page 136: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Query

• Builder pattern• Similar to JPA criteria query• Predicates compare column values to parameters:

– equal, lessEqual, greaterEqual, lessThan, greaterThan, between, in– comparison with null

• Combine predicates using binary and unary operators:– or, and, not

• Execution is optimized to use indexes– primary or unique key lookup– ordered scan for complete or partial keys– table scan if no index can be used

136

Page 137: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Query Interfaces• Query

– the "executable" part– Parameters and limits are bound to this instance

• QueryBuilder– used to build queries

• QueryDomainType– represents the user's domain type for queries

• PredicateOperand– represents either a query parameter or a query property

• Predicate– represents a comparison between a property and a parameter– can be combined using "and", "or", and "not" operators

137

Page 138: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Query Optimizer

• Optimizer looks for indexes• PRIMARY key all columns equal• PRIMARY key leading columns equal• Unique (hash) key equal• Ordered (btree) key equal, greater, less• Table scan if no indexes are usable• All terms used for filter• After parameters are bound, ask for query plan:

– Query.explain()– Keys: Query.SCAN_TYPE, Query.INDEX_USED

138

Page 139: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

QueryDomainType<Tweets> qdtTweets = builder.createQueryDefinition(Tweets.class) ;

Predicate eqAuthor = qdtTweets.get("author") .eq(qemp.param("pauthor"));Predicate gtTime_stamp = qdtTweets.get("time_stamp") .greaterEqual(qemp.param("ptime_stamp"));

qdtTweets.where(eqAuthor.and(gtTime_stamp));

Query query = session.createQuery(qdtTweets) ;

query.setParameter("pauthor", getAuthor());query.setParameter("ptime_stamp", getTime());

List<Tweets> results = query.getResultList() ;

Query Example

139

Page 140: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Query Exercises

• Write Find.java program– Skeleton in labs/test/Find.java– Query by author– Query by hashtag– Query by date range– Query by combining above criteria

140

Page 141: My sql tutorial-oscon-2012

Copyright © 2012 Oracle Corp.

ClusterJ Query Extra Credit

• Write Delete.java program– skeleton in labs/test/Delete.java– Delete by author– Delete by hashtag– Delete by date range– Delete by combining above criteria

141

Page 142: My sql tutorial-oscon-2012

<Insert Picture Here>

Final Questions & Answers

142