1
© 2010 VMware Inc. All rights reserved
Google AppEngine
Recommended Text:
Programming Google App Engine
Dan Sandersons O’Reilly
2
AppEngine
! Web application hosting environment ! Highly scalable ! Automatic scaling • AppEngine allocates resources to the application as needed
! Fault tolerant
3
Components
! Run time environment • Java
• Python • PHP
• Go
! Datastore • Object database
! Scalable services • URL Fetch
• Mail • Image manipulation
• Extensible Messaging and Presence Protocol (XMPP) • Task queue (asynchronous processing)
4
Runtime Environment
! Stateless • Springs into existence to handle a request
• May go away after request is over • Requests routed independently to some server in infrastructure
! Sandboxing • Read only access to local file system • Limits the amount of resources per request
• Memory and CPU time • Application that use a lot of CPU of memory are throttled
• No new thread creation
• No direct networking • No awareness of other applications running on the same host
2
5
Eclipse Google Plugin
! Create AppEngine projects ! Debug project locally ! Upload projects to AppEngine
6
Directory Structure
7
AppEngine Console
! https://appengine.google.com/
8
Application Configuration
! war/WEB-INF/appengine-web.xml
! AppEngine URL • http://[{version}.]{appname}.appspot.com/
• http://1.ece1779.appspot.com • http://ece1779.appspot.com
3
9
Logging
! AppEngine logs all requests • Data, time, client IP, URL, referring URL user-agent, HTTP status code
• CPU time, response size
! Applications can also log events
10
Request Handling
! Requests are short: 30 sec max.
11
Static File Configuration
! Two kinds of files • Static: deployed on static servers
• Resource: deployed on application servers
! Default policy • Static: all files in WAR except contents of WEB-INF and JSPs
• Resources: all files in WAR
! Customize policy in war/WEB-INF/appengine-web.xml <resource-files>
<exclude path=“/images/**” /> <resource-files>
<static-files> <exclude path=“/**.xml” /> <include path=“sitemap.xml” expiration=“2d” />
</static-files>
12
Integration with Google Accounts
! Possible to use Google Accounts to handle user authentication ! Can detect whether the current user has signed in ! Redirect the user to the appropriate sign-in page to sign in ! App can access the user's email address ! Detect whether the current user is an administrator • Simplify implementation of admin-only areas of the app.
import com.google.appengine.api.users.User; import com.google.appengine.api.users.UserService; import com.google.appengine.api.users.UserServiceFactory;
UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); user.getEmail(); user.getUserId();
4
13
Security Configuration
! Configured in war/WEB-INF/web.xml ! HTTPS
! User authorization with Google Accounts • <role-name> can be either * or admin
14
Frontend Performance
! Can configure in management console ! Administration->Application Settings->Performance • Frontend Instance Class
• Idle instances • # of idle instances kept in standby
• Pending latency • Latency before starting new instance to handle waiting requests
15
Backend Performance
! Special App Engine instances ! Have no request deadlines, higher memory and CPU limits ! Persistent state across requests ! Started automatically ! Can run continously for long periods ! Individually addressable • http://[instance].[backend_name].[your_app_id].appspot.com
16
Bankend Configuration
! war/WEB-INF/backends.xml
! Administer • AppEngine management console
• appcfg.sh command line tool (part of the Java SDK)
5
17
Storage
! AppEngine Datastore • a NoSQL schemaless object datastore, with a query engine and atomic
transactions.
! Google Cloud SQL • Managed relational database
• https://developers.google.com/cloud-sql/
! Google Cloud Storage • Scalable, highly available storage service for objects and files up to terabytes
• https://developers.google.com/storage/
18
Datastore
! Highly scalable ! Fault tolerant ! Distributed ! Resembles a object database ! Data stored as entities • Similar to a RDB tuple
! Entity consists of: • Kind
• Similar to a RDB table, immutable
• Key • Immutable • Similar to primary key
• Properties • Similar to table columns • Different entities of the same kind may store different properties • A property can store multiple values at once
19
Data Types
20
Key
! Uniquely identifies object in Datastore ! Immutable ! Consists of: • Application’s ID (hidden from view)
• Kind • Entity ID, one of:
• key name - set by application • ID - set automatically by datastore if no name is given
! Key can be store as a property value ! Retrieving a entity by it’s key is faster than a datastore query
6
21
Datastore Low Level Interface
! Key creation: Key k = KeyFactory.createKey(“Entity”,”alphabeta”);
String k_str = KeyFactory.keyToString(k); Key k = KeyFactory.stringToKey(k_str);
! Store entity: DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Entity e1 = new Entity(k);
e1.setProperty("prop1", 1);
ds.put(e1);
! Retrieve entity: Entity result = ds.get(e1.getKey());
22
Java Persistence API (JPA)
! Standard Java API for persisting Java objects ! Supported by AppEngine Datastore ! Annotations define entity kind, id and which properties to persist • By default JPA persists builtin Java types (e.g., String, Date) • @Basic persists a non-standard Java type property • @Transient marks property as non-persistent
23
Java Persistence API (JPA)
! J2EE standard ! Supported by Google Datastore ! Mappings • kinds -> classes
• entities -> objects • fields -> properties
! Data types supported by either • Primitive Java data type (e.g., String, Int) • Application-defined data type (e.g., Preferences)
24
Key Generation
! JPA annotations key name
ID
7
25
EntityManagerFactory
! Object that interacts with the Datastore
26
Saving an Entity
27
Fetching/Deleting an Entity
Recommended