self-healing databases

37
Self- Healing Databases MySQL Miniconf, linux.conf.au 2007 Jonathan Oxer <[email protected]> managing schema updates in the field or, “peddling drugs to Sakila”

Upload: jonathan-oxer

Post on 18-Dec-2014

1.046 views

Category:

Technology


2 download

DESCRIPTION

Talk by Jonathan Oxer at the MySQL Miniconf run as part of linux.conf.au 2007. Outlines a technique for managing schema updates in the field. More information at http://jon.oxer.com.au/talks/id/56

TRANSCRIPT

Page 1: Self-Healing Databases

Self-HealingDatabases

MySQL Miniconf, linux.conf.au 2007Jonathan Oxer <[email protected]>

managing schema updates in the fieldor, “peddling drugs to Sakila”

Page 2: Self-Healing Databases

The Problem

Self-Healing Databases Jonathan Oxer <[email protected]>

Applications are not static.

New versions mean schema changes.

App / schema mismatches are bad.

Page 3: Self-Healing Databases
Page 4: Self-Healing Databases

The Problem

Self-Healing Databases Jonathan Oxer <[email protected]>

Applications are not static.

New versions mean schema changes.

App / schema mismatches are bad.

Page 5: Self-Healing Databases

The Problem

Self-Healing Databases Jonathan Oxer <[email protected]>

Applications are not static.

New versions mean schema changes.

App / schema mismatches are bad.

Schema changes mean pain.

Page 6: Self-Healing Databases
Page 7: Self-Healing Databases

The Problem

Self-Healing Databases Jonathan Oxer <[email protected]>

Applications are not static.

New versions mean schema changes.

App / schema mismatches are bad.

Schema changes mean pain.

Page 8: Self-Healing Databases

Obvious Solution

Self-Healing Databases Jonathan Oxer <[email protected]>

Update scripts

Page 9: Self-Healing Databases

Update Scripts

Self-Healing Databases Jonathan Oxer <[email protected]>

Run manuallyX

Page 10: Self-Healing Databases

Update Scripts

Self-Healing Databases Jonathan Oxer <[email protected]>

Statically definedX

Page 11: Self-Healing Databases

Self-Healing Databases Jonathan Oxer <[email protected]>

A better way?

Page 12: Self-Healing Databases

Self-Healing Databases Jonathan Oxer <[email protected]>

Self-Healing

Databases

Page 13: Self-Healing Databases

Reasons For Change

Self-Healing Databases Jonathan Oxer <[email protected]>

New tables required.

New columns required.

Alterations to columns.

Alterations to contents of tables.

Page 14: Self-Healing Databases

Failure Modes

Self-Healing Databases Jonathan Oxer <[email protected]>

New tables required.

New columns required.

Alterations to columns.

Alterations to contents of tables.

“Unknown table”

“ Unknown column”

?

?

Page 15: Self-Healing Databases

Self-Healing Databases Jonathan Oxer <[email protected]>

Reactive,not

proactive

Page 16: Self-Healing Databases

Smart Error Trapping

Self-Healing Databases Jonathan Oxer <[email protected]>

1. Run queries blindly.

2. Detect failure conditions.

3. Fix them.

4. Profit!

Page 17: Self-Healing Databases

But...

Self-Healing Databases Jonathan Oxer <[email protected]>

...if you don't have a dbabstraction layer you're

stuffed!

Page 18: Self-Healing Databases

Build, Borrow or Steal

Self-Healing Databases Jonathan Oxer <[email protected]>

One central query executor

Page 19: Self-Healing Databases

MySQL Errors

Self-Healing Databases Jonathan Oxer <[email protected]>

MySQL has built-in error reporting: use it!

In PHP:$errno = mysql_errno($link);$error = mysql_error($link);

Specify the link or you'll get the valuefrom the last opened connection, notthe last error from your connection.

Page 20: Self-Healing Databases

MySQL Errors

Self-Healing Databases Jonathan Oxer <[email protected]>

Check for specific errors, such as:

1146: Table doesn't exist 1054: Unknown column

dev.mysql.com/doc/refman/5.0/en/error-handling.html

Page 21: Self-Healing Databases

Missing Table

Self-Healing Databases Jonathan Oxer <[email protected]>

● Store reference schemas in app● Trap “1146” errors● Examine error to determine table name● Load reference schema● Create table● Rerun original query● Return result

The user never even notices a glitch :-)

Page 22: Self-Healing Databases

Missing Table

Self-Healing Databases Jonathan Oxer <[email protected]>

Embed reference schemas into your app.

[modulename]/sql/articles.sql:

CREATE TABLE `articles` (`Serial` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`Title` VARCHAR( 255 ) NOT NULL ,`Article` TEXT NOT NULL) ENGINE = MYISAM ;

Page 23: Self-Healing Databases

Missing Column

Self-Healing Databases Jonathan Oxer <[email protected]>

● Record schema changes in “alter” file● Trap “unknown column” errors● Load and execute alter file● Rerun original query● Return result

No harm, no foul.

Page 24: Self-Healing Databases

Missing Column

Self-Healing Databases Jonathan Oxer <[email protected]>

Make execution of “alter” file idempotent.

[modulename]/sql/alter.php:

if (!$dbase->field_exists(“news”, “Modified”)){

$s = “ALTER TABLE news ADD `Modified` TIMESTAMP NOT NULL”;

$dbase->query($s);}

Page 25: Self-Healing Databases

Self-Healing Databases Jonathan Oxer <[email protected]>

That's notall, folks!

Page 26: Self-Healing Databases

Problem:

Self-Healing Databases Jonathan Oxer <[email protected]>

Multiple moduleinstances requiredata partitioning

Page 27: Self-Healing Databases

Solution:

Self-Healing Databases Jonathan Oxer <[email protected]>

Three-tierdynamic table

naming scheme

Page 28: Self-Healing Databases

Dynamic Table Names

Self-Healing Databases Jonathan Oxer <[email protected]>

1: Module instance2: Module name3: Specific table

hotstuff_news_articleshotstuff_news_comments

Page 29: Self-Healing Databases

Benefits

Self-Healing Databases Jonathan Oxer <[email protected]>

Storage of schema withmodule: error handler candeduce path from table.

Upgrade of tables when youdon't know their name.

Page 30: Self-Healing Databases

Schema Templates

Self-Healing Databases Jonathan Oxer <[email protected]>

Placeholders in reference schemas

[modulename]/sql/articles.sql:

CREATE TABLE `articles` (`Serial` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`Title` VARCHAR( 255 ) NOT NULL ,`Article` TEXT NOT NULL) ENGINE = MYISAM ;

Page 31: Self-Healing Databases

Schema Templates

Self-Healing Databases Jonathan Oxer <[email protected]>

Placeholders in reference schemas

[modulename]/sql/articles.sql:

CREATE TABLE <articles> (`Serial` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`Title` VARCHAR( 255 ) NOT NULL ,`Article` TEXT NOT NULL) ENGINE = MYISAM ;

Page 32: Self-Healing Databases

“Alter” Templates

Self-Healing Databases Jonathan Oxer <[email protected]>

Make table names in “alter” file dynamic[modulename]/sql/alter.php:

if (!$dbase->field_exists(“articles”, “Modified”)){

$s = “ALTER TABLE `articles` ADD `Modified` TIMESTAMP NOT NULL”;

$dbase->query($s);}

Page 33: Self-Healing Databases

“Alter” Templates

Self-Healing Databases Jonathan Oxer <[email protected]>

Make table names in “alter” file dynamic[modulename]/sql/alter.php:$articles = $instance.'_news_articles';if (!$dbase->field_exists($articles, “Modified”)){

$s = “ALTER TABLE $articles ADD `Modified` TIMESTAMP NOT NULL”;

$dbase->query($s);}

Page 34: Self-Healing Databases

Benefits

Self-Healing Databases Jonathan Oxer <[email protected]>

Stop caring about:

● App / schema mismatches● Knowing what tables are called● Telling users to run upgrade scripts

Page 35: Self-Healing Databases

Is This A Fairy Tale?

Self-Healing Databases Jonathan Oxer <[email protected]>

Technique in production use in theSiteBuilder web application frameworkand modules for more than 6 years:

● 1 million lines of PHP● 96 modules● 6,829 SQL statements● 793 embedded table schemas

Page 36: Self-Healing Databases

Is This A Fairy Tale?

Self-Healing Databases Jonathan Oxer <[email protected]>

Deployments include the Siemensintranet which has over 2,500dynamically managed tables.

Page 37: Self-Healing Databases

Self-Healing Databases

Thankyou :-)These slides: jon.oxer.com.au/talks

Shameless plug: www.sitebuilder.com.au

Contact: Jonathan Oxer <[email protected]>