introduction to google appengine development in java philippe beaudoin (track sponsor)

45
Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Upload: percival-baldwin

Post on 23-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Introduction to Google AppEngine

Development in Java

Philippe Beaudoin

(Track Sponsor)

Page 2: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Overview

What is Google AppEngineHit the ground running: Your first GAE appThe datastoreTask QueuesOverview of other services

?

Page 3: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

What is Google AppEngine

Fly me to the cloud, baby!Fly me to the cloud, baby!

PaaSPaaS(Platform as a Service)(Platform as a Service)

?

?

Page 4: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

• Platform features– Not Infrastructure as a Service (i.e. Amazon AWS)– Sandboxed– NoSQL distributed datastore + memcache– Many services (task queues, image manipulation…)

• APIs for…– Java (And other JVM languages)– Python– Go (beta)

?

Page 5: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

• Drawbacks– Not all of Java is available (sandbox)– Only communicate via HTTP/HTTPS + email– Read-only file system– Must finish processing within 30 seconds• Except for new backends

• Advantages– It will scale!– Pay only for what you need– Super-easy and fast deployment

?

Page 6: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Billing model• Free!– 500Mb storage– 5M page views/month

• Billing enabled

?

Page 7: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Admin console

?

Page 8: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Hit the ground running:Your first GAE app

?

Page 9: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 10: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 11: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 12: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 13: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

The datastore

• NoSQL (Roughly: a fancy hashmap)• Optimistic concurrency– No locking: try first, fail later if things go wrong

• Multitenancy• Memcache (Use it!)• Default Java API: JDO or JPA– Get cover!

• Much better: Objectify (OSS project) ?

Page 14: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 15: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 16: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Transactions

• Each entity has an entity group• By default, each entity is in a group of its own• Transactions are restricted to one entity group

By default, transactions are restricted to a

single entity!

?

Page 17: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Everything in one Entity Group?

• Bad idea!– AppEngine limits the number of requests per

second on a given entity group.– Contention: every request wants the same group

• So design your entity groups carefully– The default (each entity in its group) often works

?

Page 18: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Entity groups design

• Decide them when designing your data model– Address and phone have coupled transactions?

Same group!

• Grouping is a bit weird: choose one entity as the parent of the other.– Not necessarily a “real” parent-child relationship– No “cascading deletes”

?

Page 19: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

The components

?

Page 20: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Persistable Entities

• Basic Java types (String, int, etc.)• Date• Blob (binary)• Email, GeoPt, PhoneNumber, etc.• Key<AnotherEntity>• List<>, Set<>, or arrays of the above• @Embedded AnyPersistableEntity• @Serialized AnySerializableEntity

?

Page 21: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

?

Page 22: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Accessing entitiesput(), get(), query(), delete()

?

Page 23: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Optimization

• Get/put/delete multiple keys at once• Asynchronous fetches for concurrent queries• Fetch only keys when needed

?

Page 24: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Using the memcache

• Simply annotate the entity with @Cached!

• Optimized for read-intensive apps– Write-through

• Caches negative results to• Caches get(), put() and delete(), not query().• Can go out-of-sync in some situations– Not for entities requiring rigorous data integrity

?

Page 25: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Useful trick: counters

• Remember: AppEngine limits the number of requests per second on a given entity group.

• A counter many “read-increment-write”!• The trick: a sharded counter– Create many counters (not @Cached!)– Pick one randomly, increment it– At the end, collect them all and sum them up

• Why it works? AppEngine has very fast read!• Same idea: sharded lists

?

Page 26: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Queries

• NoSQL: only allows efficient queries– No table scans– No in-memory sorts– No joins (almost)

• Queries only allowed on indexed properties• Allow “merge-join” queries:

Horn

4 legsSpotted

Cows!

?

Page 27: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Indexes

• List of entities sorted by a given field– Allows fast dichotomic search

• By default all fields are indexed– @Indexed, @Unindexed control that– Use on fields or classes

• Manual indexes– Specify more than one field to sort• Ex: Sort by gender then by age

– Needed for some complex queries

?

Page 28: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Complex queries

• income > 100000 and age < 30– Not allowed! Inequalities on two fields

• gender = f and age > 18 and age < 25– Allowed! Two inequalities on the same field– Needs a manual index (equality + inequality)• Sort by gender, then by age

• all, sorted by decreasing age– Allowed, needs a manual index (reverse sort)

?

Page 29: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Optimization

• Index only the fields you need• Index only the values you need!– @Unindexed(IfFalse.class) boolean admin;

• Try to limit manual indexes• Avoid manual indexes on many List<>

fields!• Exploding indexes!

?

Page 30: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Useful trick: full text search

• Full text search not available out-of-the-box• Build a KeywordIndex table– KeywordIndex entity is a [keyword, entityId] pair– Normalize keywords (uppercase, no diacritics…)

• When searching for a prefix:– ofy().query(Customer.class).filter("keyword >=",

prefix).filter("keyword <=", prefix + "\ufffd").list();

?

Page 31: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Task queues

• Work outside of a user request– Still need to be initiated by a user request

• Organize works in small discrete tasks– Again: split it up to make it scalable

• 10 minutes deadline (instead of 30s)• Examples:– email notifications– schema migration

?

Page 32: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Push queue vs Pull queue

• Push queue– Meant to be consumed by your AppEngine app– Dispatching managed by AppEngine

• Pull queue new!

– Meant to be consumed externally (REST)• or on an AppEngine backend

– Consume the tasks when you’re ready– Your task consumer must scale!

?

Page 33: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Configuring a Pull QueueIn queue.xml

<bucket-size>

<queue>

<max-concurrent-requests>

<rate>

?

Page 34: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Enqueing a task

Or: getQueue(name)

?

Page 35: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Mapper API

• The “map” part of “mapreduce”• Run a task on all the entities of a given type in

the datastore• Just configure the MapReduceServlet

?

Page 36: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Configure a new mapper

?

Page 37: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

The mapper class

?

Page 38: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Launching the mapper

?

Page 39: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Overview of other services

• URL Fetch• Mail / XMPP (Google Talk)• OAuth, OpenID, Google Accounts• Image manipulation• Channel API• Blobstore

?

Page 40: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

URL Fetch

• Access other web resources (REST Apis, etc.)– Only http/https– Maximum deadline of 10 seconds– Synchronous or asynchronous

• Use standard java.net.URLConnection• Low-level AppEngine API has more features

?

Page 41: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Channel API

• Enables COMET on AppEngine– Only for javascript clients

?

Page 42: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Channel API

?

Page 43: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Blobstore

• Objects up to 2 Gb• Useful for videos or large images• User use a form to upload to the blobstore• Can serve back the blob later

?

Page 44: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Conclusion

• Platform-as-a-Service– Learn the platform– Constraints are often opportunities– Work with it, not against

• Still in active development– No https on custom domain

• A great way to learn what is needed to build a scalable app.– Try your hand at the free version!

Page 45: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)

Hands on?

Tomorrow, 11am, CamstasiaGoogle Web Toolkit and the Model View Google Web Toolkit and the Model View

Presenter Architecture Presenter Architecture

[email protected]

@philbeaudoinhttp://arcbees.com