What's New in the PHP Driver

40
Hannes Magnusson , 10gen [email protected] @ bjori November 15th , 2012 What’s new in the PHP Driver (1.3.0) Thursday, 15 November 12

description

Interested in learning more about MongoDB? Sign up for MongoSV, the largest annual user conference dedicated to MongoDB. Learn more at MongoSV.com

Transcript of What's New in the PHP Driver

Page 1: What's New in the PHP Driver

Hannes Magnusson, [email protected]

@bjoriNovember 15th, 2012

What’s new in thePHP Driver (1.3.0)

Thursday, 15 November 12

Page 2: What's New in the PHP Driver

• Hannes / @bjori• Icelandic

– Oslo, Norway– London, England– San Francisco, USA

• PHP Contributor • PHP Driver Engineer

» @10gen

About me

3

Thursday, 15 November 12

Page 3: What's New in the PHP Driver

Agenda

• Removed not-so-cool stuff• New cool stuff

• Connection handling / Replica Sets– Write Concerns– Read Preferences

Thursday, 15 November 12

Page 4: What's New in the PHP Driver

Removed stuff

Thursday, 15 November 12

Page 5: What's New in the PHP Driver

Pooling

• Bye bye!• MongoPool::*()• Mongo->poolDebug();• Mongo->get/setPoolSize();

– Replaced by one persistent connection per• host:port;replicaSetName;credentials;pid combo

6

Thursday, 15 November 12

Page 6: What's New in the PHP Driver

Deprecated

• Mongo->connectUtil()• Mongo->get/setSlaveOkay()• Mongo->last/prev/reset/forceError()• Mongo->switchSlave()

7

Thursday, 15 November 12

Page 7: What's New in the PHP Driver

New stuff

Thursday, 15 November 12

Page 8: What's New in the PHP Driver

MongoClient !!

• Acknowledged writes by default! \o/\o/• No deprecated methods

– Old Mongo class now extends MongoClient and adds back the old behaviour and methods

9

Thursday, 15 November 12

Page 9: What's New in the PHP Driver

• $mongoCollection->aggregate()• $mongoCollection->findAndModify()• TTL Collections

10

$mongoCollection->ensureIndex( array("dateField" => 1), array("expireAfterSeconds" => 3600));

Thursday, 15 November 12

Page 10: What's New in the PHP Driver

Aggregation Framework

11

$ops = array(    array(        '$project' => array(            "author" => 1,            "tags"   => 1,        )    ),    array('$unwind' => '$tags'),    array(        '$group' => array(            "_id" => array("tags" => '$tags'),            "authors" => array('$addToSet' => '$author'),        ),    ),);$results = $collection->aggregate($ops);

Thursday, 15 November 12

Page 11: What's New in the PHP Driver

New cool stuff

• ReadPreferences• WriteConcerns• Connection handling

12

Thursday, 15 November 12

Page 12: What's New in the PHP Driver

What does the driver do?

Its responsibilitiesConnection handling/failover

Thursday, 15 November 12

Page 13: What's New in the PHP Driver

The Driver

• Full C level client library, no external dependencies– Connection handling– Memory usage reported by PHP (almost)– Connection handling– MongoDB Wire protocol (OP_[CRUD], KILL_CURSOR)– Connection handling– Authentication– Connection handling

• BSON serialization• Connection handling• Command wrappers/helpers• Connection handling

14

Thursday, 15 November 12

Page 14: What's New in the PHP Driver

Replica Sets

Establishing connections

Thursday, 15 November 12

Page 15: What's New in the PHP Driver

Replica Set – Configuration Options

> conf = { _id : "mySet", members : [ {_id : 0, host : "Node1”, tags : {"dc": "us"}}, {_id : 1, host : "Node2", priority : 20, tags : {"dc": "is"}}, {_id : 2, host : "Node3”, priority : 30, tags : {"dc": "is"}}, {_id : 3, host : "Node4", hidden : true}, {_id : 4, host : "Node5", hidden : true, slaveDelay : 3600} ]}

> rs.initiate(conf)

Thursday, 15 November 12

Page 16: What's New in the PHP Driver

Replica Set – Configuration Options

> conf = { _id : "mySet", members : [ {_id : 0, host : "Node1”, tags : {"dc": "us"}}, {_id : 1, host : "Node2", priority : 20, tags : {"dc": "is"}}, {_id : 2, host : "Node3”, priority : 30, tags : {"dc": "is"}}, {_id : 3, host : "Node4", hidden : true}, {_id : 4, host : "Node5", hidden : true, slaveDelay : 3600} ]}

> rs.initiate(conf)

Primary DC

Thursday, 15 November 12

Page 17: What's New in the PHP Driver

Replica Set – Configuration Options

> conf = { _id : "mySet", members : [ {_id : 0, host : "Node1”, tags : {"dc": "us"}}, {_id : 1, host : "Node2", priority : 20, tags : {"dc": "is"}}, {_id : 2, host : "Node3”, priority : 30, tags : {"dc": "is"}}, {_id : 3, host : "Node4", hidden : true}, {_id : 4, host : "Node5", hidden : true, slaveDelay : 3600} ]}

> rs.initiate(conf)

Secondary DCDefault priority = 1

Thursday, 15 November 12

Page 18: What's New in the PHP Driver

Replica Set – Configuration Options

> conf = { _id : "mySet", members : [ {_id : 0, host : "Node1”, tags : {"dc": "us"}}, {_id : 1, host : "Node2", priority : 20, tags : {"dc": "is"}}, {_id : 2, host : "Node3”, priority : 30, tags : {"dc": "is"}}, {_id : 3, host : "Node4", hidden : true}, {_id : 4, host : "Node5", hidden : true, slaveDelay : 3600} ]}

> rs.initiate(conf)

Analytics node

Thursday, 15 November 12

Page 19: What's New in the PHP Driver

Replica Set – Configuration Options

> conf = { _id : "mySet", members : [ {_id : 0, host : "Node1”, tags : {"dc": "us"}}, {_id : 1, host : "Node2", priority : 20, tags : {"dc": "is"}}, {_id : 2, host : "Node3”, priority : 30, tags : {"dc": "is"}}, {_id : 3, host : "Node4", hidden : true}, {_id : 4, host : "Node5", hidden : true, slaveDelay : 3600} ]}

> rs.initiate(conf)

Backup node

Thursday, 15 November 12

Page 20: What's New in the PHP Driver

Replica Set - Creation

Node 1 Node 2

Node 3Node 4 Node 5

Thursday, 15 November 12

Page 21: What's New in the PHP Driver

Replica Set - Initialize

Node 1 Node 2

Node 3

RS Init

Node 4 Node 5

Thursday, 15 November 12

Page 22: What's New in the PHP Driver

Replica Set - Initializing

Node 1 Node 2

Node 3

HeartbeatElection

Node 4 Node 5

Thursday, 15 November 12

Page 23: What's New in the PHP Driver

Replica Set - Initialized

Node 1Secondary

Node 2Secondary

Node 3Primary

Node 4(hidden)

Node 5(hidden)

Thursday, 15 November 12

Page 24: What's New in the PHP Driver

Replica Set - Driver view

Node 1Secondary

Node 2Secondary

Node 3Primary

Driver

Thursday, 15 November 12

Page 25: What's New in the PHP Driver

Connecting to Replica Set

• Connect to Node1– send `ismaster` then `ping`

• Connect to Node3...• Topology discovery

– Match hostnames & Replica Set names

– Verify the seedlist

– Discover more nodes

– Connect to Node2...26

<?php$servers = "mongodb://Node1,Node3";$options = array( "replicaSet" => "mySet",);$m = new MongoClient($servers, $options);?>

Thursday, 15 November 12

Page 26: What's New in the PHP Driver

Connections

• Each unique connection string• host:port;replicaSetName;user/pass/db;pid

• Once per process• Closed & Cleaned up during shutdown

– Please don’t close connections manually :)

27

Thursday, 15 November 12

Page 27: What's New in the PHP Driver

Read Preference

Secondary readsPrioritise servers / datacenters

Thursday, 15 November 12

Page 28: What's New in the PHP Driver

Replica Set - Default only primary

Node 1Secondary

Node 2Secondary

Node 3Primary

Driver

Thursday, 15 November 12

Page 29: What's New in the PHP Driver

Read Preferences

• Only read from primary• Read from secondaries• Read from specific secondaries• Only read from secondaries when primary

down• Don’t care, just the nearest one• Preferably node with a given tag

30

Thursday, 15 November 12

Page 30: What's New in the PHP Driver

Read Preferences

• Modes (readPreference)– primary, primaryPreferred– secondary, secondaryPreferred– nearest

• Tag Sets (readPreferenceTags)– Uses Replica Set tags– Passed Tag is used to find matching members

31

Thursday, 15 November 12

Page 31: What's New in the PHP Driver

Detecting failures / failovers

• Runs `ping` every 5 seconds (mongo.ping_interval)

• Runs `ismaster` every 60 seconds (mongo.is_master_interval)

• Server/connection failure/force close– rs.stepDown()– service mongod stop– Query failure– ...

32

Thursday, 15 November 12

Page 32: What's New in the PHP Driver

Read Preference +

Failovers

No primary available

Thursday, 15 November 12

Page 33: What's New in the PHP Driver

Replica Set - all well

Node 1Secondary

Node 2Secondary

Node 3Primary

Driver

Thursday, 15 November 12

Page 34: What's New in the PHP Driver

Replica Set

Node 1Secondary

Node 2Secondary

Node 3Primary

Driver

Thursday, 15 November 12

Page 35: What's New in the PHP Driver

Read Preferences

<?php$servers = "mongodb://Node1,Node2,Node3";$options = array( "replicaSet" => "mySet", "readPreference" => "primaryPreferred", "readPreferenceTagS" => "dc:is",);$m = new MongoClient($servers, $options);?>

36

• Read from Primary if available• Otherwise from a secondary in Iceland

Thursday, 15 November 12

Page 36: What's New in the PHP Driver

WriteConcerns

How much do you love your data?(not actually new, but still cool)

Thursday, 15 November 12

Page 37: What's New in the PHP Driver

Write Preference

• Unacknowledged (w=0)• Acknowledged (w=1) • Wait for replication (w=2+)• Wait for replication to tagset (w=string) • Wait for journal sync (j=1)

Thursday, 15 November 12

Page 38: What's New in the PHP Driver

Write Preference

<?php

$examples = $mongo->test->example;$data = array("Hello" => "World");$examples->insert($data, array("w" => 2));$examples->insert($data, array("w" => "is"));

?>

• w=majority• (replicate to the majority of the RS)

Thursday, 15 November 12

Page 39: What's New in the PHP Driver

New Connection handlingRead Preferences

Acknowledged writes by default

RC2 released last Monday...RC3 this coming Monday...Final .. 2weeks?

Thursday, 15 November 12

Page 40: What's New in the PHP Driver

@mongodb

conferences, appearances, and meetupshttp://www.10gen.com/events

http://bit.ly/mongofb Facebook | Twitter | LinkedIn

http://linkd.in/joinmongo

download at mongodb.org

We’re Hiring [email protected] @bjori

Thursday, 15 November 12