Fine Tuning

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Fine Tuning as PDF for free.

More details

  • Words: 3,171
  • Pages: 23
Professional Open Source™

Performance Tuning Fine Tuning JBoss and J2EE Applications

© JBoss, Inc. 2003-2005.

8/2/2005

1

Frequent Performance Problems Professional Open Source™

Too much network communication – In poorly collocated designs

Contention on a scarce resource – Low number of HTTP connections – Low JDBC connection in connection pool

Overloading a scarce resource – Not caching static HTTP content – Unnecessary DB queries – Unneccessary roundtrips to database

JVM bottlenecks – Virtual machine heap and GC configuration

© JBoss, Inc. 2003-2005.

2

1

Professional Open Source™

Web tier tuning

© JBoss, Inc. 2003-2005.

8/2/2005

3

HTTP/1.1 Connector tweaking Professional Open Source™

Common newbie error: Not enough HTTP threads Tomcat limits number of concurrent web requests – Keep a minimal pool of threads: minProcessors (4.1) and minSpareThreads (5.0) – Allow more concurrent connections: maxProcessors (4.1) and maxThreads (5.0) – Many other factors, obviously: the worst is to allow too many HTTP connections that couldn't complete in a satisfactory time

Keeping connection open too long – ConnectionTimeout, disableUploadTimeout and maxKeepAliveRequests allow configuring HTTP/1.1 keepalive

© JBoss, Inc. 2003-2005.

4

2

Tomcat Connectors Professional Open Source™

deploy/jbossweb-tomcat5x.sar/server.xml deploy/jbossweb-tomcat5x.sar/server.xml <Service <Servicename="jboss.web“ name="jboss.web“className="org.jboss.web.tomcat.tc5.StandardService"> className="org.jboss.web.tomcat.tc5.StandardService"> --> disableUploadTimeout="true"/>

5

© JBoss, Inc. 2003-2005.

Use Embedded Tomcat Professional Open Source™

3-Tiered architecture doesn’t mean different servers! Embed service implementations to a single process as much as possible – For fail-over/load-balancing, replicate entire processes rather than single service instances

Tomcat Tomcat

EJB EJB

© JBoss, Inc. 2003-2005.

Tomcat Tomcat

Tomcat Tomcat

EJB EJB

Tomcat Tomcat

Tomcat Tomcat

Tomcat Tomcat

EJB EJB

EJB EJB

EJB EJB

– Web tier is often tightly couple with EJB tier, ensure the communication is inprocess and as fast as possible – Identical nodes are easier to manage as well

6

3

Efficient Static Content Access Professional Open Source™

Apache is generally better at serving static content – Serve images, static HTML from Apache – Serve dynamic content like JSPs/Servlets from Tomcat/JBoss – Apache/Tomcat via mod_jk) • AJP protocol

AJP

mod_jk mod_jk

HTTP

7

© JBoss, Inc. 2003-2005.

Tomcat Static Resource Cache Professional Open Source™

Improves significantly the performance for static file serving The parameters are set on the Context element – cacheMaxSize: Global size of the cache in KB. By default, it is set to 10 MB. The maximum size of a cached resource is the maximum size divided by 20 (so 512 KB by default). – cacheTTL: After the specified amount of milliseconds (5000 by default), the cache entry will be revalidated. Increase this value if the load is spread over a large amount of resources. – cachingAllowed: Set to false to disable caching.

© JBoss, Inc. 2003-2005.

8

4

JSP Optimization Professional Open Source™

JSP page compiler is in development mode by default – To increase performance you can: • Reduce the amount of logging • Remove ”is-modified” check on JSP pages on every request • Precompile your JSP pages

deploy/jbossweb-tomcat5x.sar/conf/web.xml deploy/jbossweb-tomcat5x.sar/conf/web.xml
9

© JBoss, Inc. 2003-2005.

JSP Optimization Professional Open Source™

deploy/jbossweb-tomcat5x.sar/conf/web.xml deploy/jbossweb-tomcat5x.sar/conf/web.xml <servlet> <servlet> <servlet-name>jsp <servlet-name>jsp <servlet-class>org.apache.jasper.servlet.JspServlet <servlet-class>org.apache.jasper.servlet.JspServlet <param-name>development <param-name>development <param-value>false <param-value>false 3 3

Other options that may affect performance: – genStrAsCharArray, internally Tomcat uses character array objects instead of String objects – may be faster in some cases – trimSpaces, removes white space from responses

© JBoss, Inc. 2003-2005.

10

5

Professional Open Source™

Connection Pool Tuning

© JBoss, Inc. 2003-2005.

8/2/2005

11

Database Connection Tuning Professional Open Source™

Sample datasource: <jndi-name>GenericDS <jndi-name>GenericDS [jdbc: [jdbc:url urlfor foruse usewith withDriver Driverclass] class] [fully [fullyqualified qualifiedclass classname nameof ofjava.sql.Driver java.sql.Driverimplementation] implementation] <user-name>x <user-name>x <password>y <password>y --> x type="java.lang.String">x UTF-8 name="char.encoding">UTF-8 TRANSACTION_SERIALIZABLE TRANSACTION_SERIALIZABLE <min-pool-size>5 <min-pool-size>5 <max-pool-size>100 <max-pool-size>100 5000 5000 15 15 <prepared-statement-cache-size>100 <prepared-statement-cache-size>100

© JBoss, Inc. 2003-2005.

12

6

Database Connection Tuning Professional Open Source™

Configuration Options – Set the appropriate database connection pool size • Depending on how many concurrent requests you’re handling – E.g. how many concurrent requests are coming through web tier? – How many of those requests need database access?

• Try the prepared statement cache for performance • Try switching track-statements to false for performance

13

© JBoss, Inc. 2003-2005.

Professional Open Source™

Entity Load Tuning

© JBoss, Inc. 2003-2005.

8/2/2005

14

7

Entity Load Tuning Professional Open Source™

Lazily-load unneeded data – static fetch policies

Eagerly-load needed data and relationships – EJB-QL fetch joins

Avoid unnecessary updates – flush mode – bulk updates/deletes

15

© JBoss, Inc. 2003-2005.

Fetch policies Professional Open Source™

Lazily load relationships – Don’t pull in entire object graph

Lazily load large properties – You may not always need them @Entity @Entitypublic publicclass classAccount Accountimplements implementsSerializable Serializable{{ … … @OneToMany @OneToMany(mappedBy (mappedBy==“account”, “account”,fetch=LAZY) fetch=LAZY) public publicCollection CollectiongetCustomers() getCustomers(){{ return returncustomers; customers; }} @Lob(fetch=LAZY, @Lob(fetch=LAZY,type=BLOB) type=BLOB) public publicbyte[] byte[]getJPEG() getJPEG(){{ return returnjpeg; jpeg; }} }}

© JBoss, Inc. 2003-2005.

16

8

Fetch Joins Professional Open Source™

Access Account entity from previous slide private privatevoid voidoutputAccounts() outputAccounts()throws throwsIOException IOException{{ String Stringtable table==""; "
"; List Listaccts accts==entityManager.createQuery(“from entityManager.createQuery(“fromAccount Accounta”).getResultList(); a”).getResultList(); for for(Iterator (Iteratoritit==accts.iterator(); accts.iterator();it.hasNext();) it.hasNext();){{ Account Accountacct acct==(Account)it.next(); (Account)it.next(); System.out.println(“cc#:” System.out.println(“cc#:”++acct.getCreditCardNumber()); acct.getCreditCardNumber()); for for(Customer (Customercc::acct.getCustomers()) acct.getCustomers()){{ 2 System.out.println(“\t” System.out.println(“\t”++c.getName()); c.getName()); }} }}

1

17

© JBoss, Inc. 2003-2005.

Loading Scenario: SQL Professional Open Source™

Arrow 1 – EJB 3.0 executes the following query: SELECT id, ccn, … FROM account 1

Arrow 2 - the following queries are executed: SELECT name, address FROM customer WHERE acct_id = 1

2

SELECT name, address FROM customer WHERE acct_id = 2

3

SELECT name, address FROM customer WHERE acct_id = 3

4

SELECT name, address FROM customer WHERE acct_id = 4

5

This is called the “n+1” problem

© JBoss, Inc. 2003-2005.

18

9

Fetch Joins Professional Open Source™

Use a fetch join to eagerly load relationship private privatevoid voidoutputAccounts() outputAccounts()throws throwsIOException IOException{{ String Stringtable table=="
"; "
"; List Listaccts accts==entityManager.createQuery(“from entityManager.createQuery(“fromAccount Accountaaleft leftjoin joinfetch fetcha.customers”) a.customers”) .getResultList(); .getResultList(); for for(Iterator (Iteratoritit==accts.iterator(); accts.iterator();it.hasNext();) it.hasNext();){{ Account Accountacct acct==(Account)it.next(); (Account)it.next(); System.out.println(“cc#:” System.out.println(“cc#:”++acct.getCreditCardNumber()); acct.getCreditCardNumber()); for for(Customer (Customercc::acct.getCustomers()) acct.getCustomers()){{ System.out.println(“\t” System.out.println(“\t”++c.getName()); c.getName()); }} }}

19

© JBoss, Inc. 2003-2005.

Loading Scenario: SQL Professional Open Source™

EJB 3.0 executes the following query: SELECT a.id, a.ccn, c.name, … FROM account LEFT JOIN customer c ON a.id = c.acct_id

1

Instead of N + 1 queries only one query loads all accounts and related customers

© JBoss, Inc. 2003-2005.

20

10

Professional Open Source™

Entity Caching

© JBoss, Inc. 2003-2005.

8/2/2005

21

Caching Professional Open Source™

The cache is used whenever – the application performs a lookup by identifier with getReference() or find() – JBoss EJB3/Hibernate resolves an association lazily – (we may also cache queries)

The cache type can be – transaction scope cache – process scope cache – cluster scope cache

The EJB3/Hibernate caching system has several layers, i.e. a cache miss at the transaction level might be followed by a lookup at the process- or cluster level and only then produce a database hit.

© JBoss, Inc. 2003-2005.

22

11

The Hibernate cache architecture Professional Open Source™

Hibernate has a two-level cache architecture: First-level First-level Cache Cache

Session

Cache Concurrency Strategy

Query Cache

Cache Provider Cache Implementation (Physical Cache Regions) Second-level Second-level Cache Cache

23

© JBoss, Inc. 2003-2005.

The Session cache Professional Open Source™

The EntityManager/Hibernate Session is the first-level cache – always enabled, can't be turned off (mandatory for transaction integrity)

“I get an OutOfMemoryException when I load 500 000 objects?” Solution: – Use Query API to define pagination – Query.setMaxResults – Query.setFirstResult

The session cache is a cache of object instances – the second-level cache is a cache of data only (objects are copied out of and into the second-level cache)

© JBoss, Inc. 2003-2005.

24

12

The second-level cache Professional Open Source™

Process or cluster scope caching – makes data visible in concurrent transactions – this can cause problems if the ORM instance has non-exclusive access to the data – it is expensive to ensure consistency of cache and database, and respect transaction isolation

Non-exclusive data access – in clustered applications (use a cluster cache) – with shared legacy data (define cache expiry and transaction isolation)

EJB3/Hibernate will not know when shared data has been updated, but you may implement a trigger notification system (difficult).

25

© JBoss, Inc. 2003-2005.

Cache candidates Professional Open Source™

Especially good classes for caching represent – – – –

data that is shared between many users data that changes rarely non-critical data (i.e. content-management data) data that is local to the application and not shared

Potentially bad classes for caching are – – – –

data that is owned by a particular user data that is updated often financial data data that is shared with legacy applications

Reference data is an excellent candidate for caching with expiry: – a small number of instances (< 1000) – each instance referenced by many other instances – instances are rarely (or never) updated

© JBoss, Inc. 2003-2005.

26

13

Hibernate cache concurrency strategies Professional Open Source™

transactional ➔

Available in managed environments (appserver), full isolation up to repeatable read. Use this strategy for read-mostly data where it is critical to prevent stale data in concurrent transactions, in the rare case of an update.

read-write ➔

Maintains read-commited isolation, only in non-cluster environments, uses a timestamp mechanism, use it for read-mostly data.

nonstrict-read-write ➔

Makes no guarantee of consistency, uses an expiry timeout.

read-only ➔

Useable for data that never changes, use it for reference data

27

© JBoss, Inc. 2003-2005.

Hibernate built-in cache providers Professional Open Source™

EHCache ➔

Default, simple process cache for a single VM, supports query caching.

OpenSymphony OSCache ➔

Single process cache, rich set of expiration options and query caching.

SwarmCache ➔

Cluster-only cache system with invalidation, no query cache support.

JBossCache ➔

Fully transactional replicated cache, supports query caching.

Not every Cache Provider can use every Concurrency Strategy!

© JBoss, Inc. 2003-2005.

28

14

Cache compatibility matrix Professional Open Source™

Concurrency Strategy Cache Provider

read-only

non-strict read-write

read-write

EHCache

X

X

X

OSCache

X

X

X

SwarmCache

X

X

JBossCache

X

transactional

X

Clustered Cache

29

© JBoss, Inc. 2003-2005.

Cache configuration Professional Open Source™

Different data requires different cache policies, so the second-level cache is granular The Account is a good candidate for a read-write cache – small number of instances – updated rarely and shared by many units of work – a read committed isolation level is good enough

Caching all instances of Account, in persistence.xml <entity-manager> CRM <properties> <property name=“hibernate.ejb.classcache.com.acme.Account” value=“read-write/>

© JBoss, Inc. 2003-2005.

30

15

Caching associted entities Professional Open Source™

We can also cache associated entities of Account: <entity-manager> CRM <properties> <property name=“hibernate.ejb.collectioncache.com.acme.Account.customers” value=“read-write”/>

The cache will now hold the identifier values of all associated instances, not the customer entities!

31

© JBoss, Inc. 2003-2005.

Query cache Professional Open Source™

Query cache will cache result sets. – Keyed by query string/parameters, value is result set

Invalidated if any table of query is updated The query cache is not enabled by default! – persistence.xml property – set hibernate.cache.use_query_cache = true

Use the Hibernate Query interface to enable caching of a particular queryjavax.persistence.Query query = entityManager.createQuery(...); org.hiberante.ejb.QueryImpl hs = (QueryImpl)query; org.hibernate.Query hbQuery = hs.getHibernateQuery(); hbQuery.setCacheable(true);

© JBoss, Inc. 2003-2005.

32

16

Professional Open Source™

Configuring the Virtual Machine

© JBoss, Inc. 2003-2005.

8/2/2005

33

How Garbage Collectors Work Professional Open Source™

We will look at the different garbage collector implementations available in Sun’s JDK 1.3.1 and 1.4.2 versions. – You should update to JDK 1.4.2 if you’re running a multiple CPU machine to take an advantage of parallelism in garbage collection.

Generational and parallel garbage collection algorithms

© JBoss, Inc. 2003-2005.

34

17

How Garbage Collectors Work Professional Open Source™

What is garbage? – An object that cannot be reached from any pointer in a running program.

Simplistic garbage collection algorithm: Iterate over all reachable objects marking them; everything left over is garbage. – Inefficient with large memory heaps; potentially very large number of object instances that need to be iterated over (increases GC pauses) – Without compacting you end up with fragmented memory. – Ignores the fact that different object collections have different lifetime: • Some objects only live a few milliseconds at a time (e.g. local variables, iterators, etc.) • Some objects may live days, months or years at a time (assuming you are using high quality server such as JBoss)

35

© JBoss, Inc. 2003-2005.

Generational Garbage Collector Professional Open Source™

Generational GC accommodates object pools based on object life time. Object pools may use different garbage collection algorithms. – Allows you to tune your GC based on the application profile. – Most applications have high “infant mortality”; objects live for a short time and do not survive the first garbage collection.

Virtual Space

Tenured Generation Pool

Permanent

Spaces

Young Generation Pool (Eden)

Survivor

Maximum Young Generation Pool Size (when full, a minor collection will occur)

Virtual Space

Maximum Heap Size (-xmx) © JBoss, Inc. 2003-2005.

36

18

Eden Professional Open Source™

When a young generation pool fills up it is garbage collected. – This is a minor garbage collection. – Young generation uses fast copy collector. – Full (slow) garbage collection is less frequent. – Live objects are copied to a survivor space. – Dead objects are then easily removed from Eden. – Objects that survive several minor collections are moved from survivor space to tenured generation pool. – After minor collection Eden is empty.

37

© JBoss, Inc. 2003-2005.

Fine Tuning Garbage Collection with Generations Professional Open Source™

The default GC parameters are effective for small applications. They are not optimal for most server applications. – Most server applications are tuned for throughput -- the total time your JVM spends executing your application and not running the garbage collector. – Client applications are usually concerned with pause times -- how long is the noticeable pause the “stop-the-world” garbage collection algorithms inflict.

You most often want to minimize full garbage collections (slow, attempt to defragment heap) and keep as much of your object instances in Eden as possible – This gives you the best throughput, minor collections (Eden) are fast.

© JBoss, Inc. 2003-2005.

38

19

GC algorithms Professional Open Source™

Copying garbage collector used for Eden. – – – –

Effective Moves all live objects to survivor space. If survivor space fills up, floods over to tenured generation pool. The remaining objects in Eden are dead and can be reclaimed very quickly. – Requires larger memory footprint since copying needs space.

Mark-compact used for tenured generation pool. – Objects are not moved, no need for extra memory allocation. – Compacting is much slower than copying. – Used for major garbage collections (when reclaiming short lived objects does not free enough memory).

39

© JBoss, Inc. 2003-2005.

Configuring Generational Segments Professional Open Source™

-XX:NewRatio= – ratio between young and tenured object pool size 1: (young pool is at maximum 1/n of total heap size)

-XX:NewSize, -XX:MaxNewSize – bind a fixed size for the young generation pool, allows more fine grained granularity than NewRatio setting.

Remember that the worst case scenario is a young generation pool full of live objects. If there is not enough memory in the tenured pool to hold these, a full GC will occur. – Therefore young generation pool size larger than half the maximum heap size is usually counter productive.

© JBoss, Inc. 2003-2005.

40

20

Parallel Garbage Collection Professional Open Source™

Most JVM’s implement “stop-the-world” garbage collection algorithms. – All running threads are halted. – Garbage collection is run in a “stopped world”.

If you have more than one CPU available it makes sense for all of them to participate in sweeping the world clean. In JDK 1.4.2 there are two new parallel collectors – Throughput collector (parallel copy collector) – Low pause collector (parallel mark and sweep collector)

“Parallel Garbage Collection for Shared Memory Multiprocessors”, Flood, Detlefs, Shavit, Zhang, USENIX JVM Conference, April 2001

41

© JBoss, Inc. 2003-2005.

Throughput Collector Professional Open Source™

– Parallel version of the copy collector in JDK 1.3.1 – Efficient for systems with more than two CPUs – Uses several threads to execute the garbage collection Default Copy Collector

Parallel Copy Collector

Stop the World

Stop the World GC Time GC Time

GC Thread Application Thread

Enable with -XX:+UseParallelGC -XX:ParallelGCThreads= to control the number of GC threads © JBoss, Inc. 2003-2005.

42

21

Concurrent Low Pause Collector Professional Open Source™

Collects tenured generation concurrently with application threads (a.k.a. “mostly CMS Collector concurrent” mark and sweep or CMS) 1. Stop the World

– Enable with XX:+UseConcMarkSweepGC – By default the CMS implementation does not compact the tenured pool. This can potentially lead to a fragmentation problem. • -XX:+UseCMSCompactAtFullCollection

– Combine with parallel GC on young generation for maximum throughput • -XX:+UseParNewGC

Generational Mostly-Concurrent Garbage Collector, David Detlefs, Tony Printezis, TR-2000-88, http://research.sun.com/research/techrep/2000/

(initial mark of all objects reachable from root)

2. Concurrent Marking

(mark transitive closure of reachable objects -- not guaranteed due to concurrent mutator threads)

3. Stop the World (remark to guarantee final transitive closure)

4. Concurrently sweep unmarked objects

GC Thread Application Thread

43

© JBoss, Inc. 2003-2005.

Other Considerations with Garbage Collection Professional Open Source™

System.gc() always does a major collection negating any benefits from collecting Eden only. – Disable with -XX:+DisableExplicitGC option!!

RMI subsystem forces a major collection once a minute (for DGC) – Frequency can be controlled with system property java

-Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 (once an hour)

Default thread stack size on Linux can be very large (several megabytes) – Each thread allocates 8 MB memory, 10 threads use 80MB memory! – Ulimit –s

Object pooling forces instances to tenured pool out of reach of the fast copy collector – Negates the generational memory allocation in VM © JBoss, Inc. 2003-2005.

44

22

Conclusion Professional Open Source™

Biggest bang for your buck – Make sure you have enough connections (HTTP/DB) – Reduce round trips to database • Prepared Statement Caching • Fetch joining • Entity and Query caches • Database will be the cause of 90% of your woes

Minor optimizations – GC setters usually not the problem, but you may need to tweak under heavy, heavy load

© JBoss, Inc. 2003-2005.

45

23

Related Documents

Fine Tuning
November 2019 17
Tuning
November 2019 15
Tuning
June 2020 9
Fine Arts
October 2019 43
Fine Wines
October 2019 23
Fine Anno
October 2019 34