Architecture of Popular

download Architecture of Popular

of 33

description

Architecture of Popular

Transcript of Architecture of Popular

  • 2007 JavaOneSM Conference | Session TS-4856 |

    TS-4856

    Architecture of PopularObject-Relational MappingProviders

    Craig RussellMitesh MeswaniLarry White

  • 2007 JavaOneSM Conference | Session TS-4856 | 2

    Goal

    How do architectures of Object-Relational Mapping (ORM) frameworks affect performance?

  • 2007 JavaOneSM Conference | Session TS-4856 | 3

    Agenda

    Concepts of ORMMajor User-Visible ComponentsMajor Internal ComponentsPerformance MethodologyKey Findings/RecommendationsCall to Action

  • 2007 JavaOneSM Conference | Session TS-4856 | 4

    Concepts of ORM Business Logic

    Persistence aware Uses persistence API

    Persist, remove, query Domain Objects

    Plain old Java objects No persistence code

    Persistence Provider Implements persistence API

    Mapping Metadata Domain to database

    Database

    Business Logic

    Domain Objects

    PersistenceProvider

    Mapping Metadata

  • 2007 JavaOneSM Conference | Session TS-4856 | 5

    Agenda

    Concepts of ORMMajor User-Visible ComponentsMajor Internal ComponentsPerformance MethodologyKey Findings/RecommendationsCall to Action

  • 2007 JavaOneSM Conference | Session TS-4856 | 6

    Persistent Class

    The domain object persisted in database Instances might be persistent (or not)

  • 2007 JavaOneSM Conference | Session TS-4856 | 7

    Persistence Unit

    Singleton pattern for VM (JNDI) DataSource definition Metadata for mapping to database

    Entity classes Rows, columns, primary/foreign keys

  • 2007 JavaOneSM Conference | Session TS-4856 | 8

    Persistence Context

    Represents a collection of entities managed bya persistence provider on behalf of a single user

    For each persistent identity, there is a unique entity instance in a given persistence context

    For exampleEntities managed by Java Persistence API (JPA) EntityManager or Java Data Objects (JDO) PersistenceManager

  • 2007 JavaOneSM Conference | Session TS-4856 | 9

    Query API

    Provides query using domain model artifacts Entity classes Persistent fields/properties Relationships

    Persistence provider translates to SQL Or underlying native query language (QL) One domain query translates to one native QL

  • 2007 JavaOneSM Conference | Session TS-4856 | 10

    Second Level Cache

    Managed in persistence unit Contains entities used at least once by any

    persistence context in a persistence unit For each persistent identity, contains the

    unique entity instance last updated in a persistence context

    Cached entities can become stale

  • 2007 JavaOneSM Conference | Session TS-4856 | 11

    Connection Pooling

    Access to database requires a connection Connections are expensive to acquire Connection is transactional (or not) Connections can be serially reused

  • 2007 JavaOneSM Conference | Session TS-4856 | 12

    Field vs. Property Persistence

    Persistent fields Fields behave according to Java platform access

    modifiers Private, protected

    Persistence provider uses reflection or byte code Enhancement to access fields

    Persistent properties JavaBeans architecture style get/set methods

    Both user contract and persistence provider No validation

    Violates separation of concerns

  • 2007 JavaOneSM Conference | Session TS-4856 | 13

    Byte-code Enhancement

    Class byte codes modified for efficiency Direct access to fields without reflection Automatic change tracking Lazy field loading

    Requires changes to deployment Dynamic instrumentation via Java technology agent

    [Java Platform, Standard Edition (Java SE platform)] Dynamic instrumentation via transformer

    [Java Platform, Enterprise Edition (Java EE platform)] Static enhancement provides more options

    a.k.a. weaving, transforming

  • 2007 JavaOneSM Conference | Session TS-4856 | 14

    Agenda

    Concepts of ORMMajor User-Visible ComponentsMajor Internal ComponentsPerformance MethodologyKey Findings/RecommendationsCall to Action

  • 2007 JavaOneSM Conference | Session TS-4856 | 15

    Major Internal Components

    Persistence unit DataSource (Connection Factory)

    Statement cache, query cache Persistent class metadata Second-level cache

    Persistence context Entity maps/lists

    Transactional entities: map Modified entities: list

    SQL generator/resultSet handler

  • 2007 JavaOneSM Conference | Session TS-4856 | 16

    Internal Architecture

    Persistence Unit

    Data Access SQL Generation ResultSet Processing Statement Caching Connection Pooling

    Persistence Context

    Modified List

    Cache Manager

    Mapping Metadata

    Connection/ Statement

    PoolID

    Map2nd Level Cache

    Transactional Cache

  • 2007 JavaOneSM Conference | Session TS-4856 | 17

    Lazy Loading/Change Detection

    OpenJPA uses byte code enhancement JPOX uses byte code enhancement TopLink essentials uses byte code enhancement

    Reflection for change detection Hibernate uses reflection for change detection

    Doesn't load lazily with field persistence

  • 2007 JavaOneSM Conference | Session TS-4856 | 18

    Agenda

    Concepts of ORMMajor User-Visible ComponentsMajor Internal ComponentsPerformance MethodologyKey Findings/RecommendationsCall to Action

  • 2007 JavaOneSM Conference | Session TS-4856 | 19

    Can we really do that?Performance Methodology

    Performance is difficult to measure (technically) Choose a workload Choose a configuration Choose products to measure Measure, verify, repeat (until exhaustion)

    Performance is difficult to measure (legally) Restrictions, covenants, licenses Open Source Changes Everything

    Well almost

  • 2007 JavaOneSM Conference | Session TS-4856 | 20

    How do we do that?Performance Methodology

    TheorizePredict relative performance Qualitative understanding of architecture

    Caching, data access, change detection Conduct experiments Analyze and correlate with measurements Modify workload and repeat

  • 2007 JavaOneSM Conference | Session TS-4856 | 21

    Why should we NOT do that?Performance Methodology

    All workloads are different Workloads are composites of atomic use cases

    Typically 5 to 15 use cases Find, query, retrieve object graph, update, delete

    Vary mix and correlation eBay is different from amazon

  • 2007 JavaOneSM Conference | Session TS-4856 | 22

    Why should we NOT do that?Performance Methodology

    Micro benchmarks are trivial Workloads are composites Each individual operation is trivial but in aggregate

    represents real work Quantity has a quality all its own

  • 2007 JavaOneSM Conference | Session TS-4856 | 23

    Why should we NOT do that?Performance Methodology

    Why should you trust a vendor? ORM frameworks are open source

    We dont need to trust vendors The community can engage this problem Users can modify to suit their requirements

  • 2007 JavaOneSM Conference | Session TS-4856 | 24

    Measuring Performance

    Workload driver Operations (create, read, update, query, navigate) Mix (e.g. 10%, 60%, 5%, 20%, 5%) Timing requirements (90% percentile within 2 seconds)

    Operations implementation Use persistence adapter to do work Translate operation to adapter API

    Scale number of parallel threads Measure operations per second Increase parallelism until metric decreases

  • 2007 JavaOneSM Conference | Session TS-4856 | 25

    DIY Open Source Project

    Allows users to run their own workloads Sample workloads and configurations provided URL http://diy.dev.java.net Suggestions and contributions encouraged

  • 2007 JavaOneSM Conference | Session TS-4856 | 26

    Connection Pooling Performance

    40 50 60 70 80 90 100 110 120 13065707580859095

    100105110115120125130135140

    No PoolPool

    Users

    Op

    era

    tio

    ns p

    er

    se

    co

    nd

    Hibernate Connection Pooling

  • 2007 JavaOneSM Conference | Session TS-4856 | 27

    TopLink Essentials Field vs. Property

    50 100 150 200 250 300 350100

    125

    150

    175

    200

    225

    250

    275

    300

    325

    350

    375

    FieldProperty

    Users

    Ops p

    er

    second

    Field vs. Property

  • 2007 JavaOneSM Conference | Session TS-4856 | 28

    Use Second Level Cache

    0 25 50 75 100 12520

    40

    60

    80

    100

    120

    140

    160

    180

    200

    220

    No CacheCache

    Users

    Op

    era

    tio

    ns p

    er

    se

    co

    nd

    OpenJPA Second Level Cache

  • 2007 JavaOneSM Conference | Session TS-4856 | 29

    Use Byte-code Enhancement

    50 100 150 200 250 300 350150

    175

    200

    225

    250

    275

    300

    325

    350

    375

    EnhancedNot Enhanced

    Users

    Op

    era

    tio

    ns p

    er

    se

    co

    nd

    TopLink Essentials Enhancement

  • 2007 JavaOneSM Conference | Session TS-4856 | 30

    And recommendationsKey Findings

    Use second-level cache Use connection pooling Use byte-code enhancement Use field persistence

  • 2007 JavaOneSM Conference | Session TS-4856 | 31

    Call to Action

    Performance tuning is required For all but the most trivial applications

    Its much too hard to tune persistence Performance monitoring should be automatic

    Should have to turn it off if you dont want it Should enable access strategy tuning

    Part of every database access Take a look at DIY Project

  • 2007 JavaOneSM Conference | Session TS-4856 | 32

    Q&ACraig RussellMitesh MeswaniLarry White

    http://diy.dev.java.net

  • 2007 JavaOneSM Conference | Session TS-4856 |

    TS-4856

    Architecture of PopularObject-Relational MappingProviders

    Craig RussellMitesh MeswaniLarry White