SlideShare a Scribd company logo
Parallel processing with
      Spring Batch
     Lessons learned
Morten Andersen-Gott
•   Manager at Accenture Norway
•   30 years old
•   Been using Spring Batch since 1.0M2 (2007)
•   Member of JavaZone program committee
    – http://tinyurl.com/javaever
    – http://tinyurl.com/thestreaming
    – http://tinyurl.com/ladyjava




     mortenag
     www.github.com/magott
     www.andersen-gott.com
A BoF focusing on the problems

•   Functional background for the batch
•   Short introduction to Spring Batch
•   Even shorter on Hibernate
•   The problems
•   The problems
•   The problems
Norwegian Public Service
                            Pension Fund (SPK)
• Norway’s main provider of public occupational
  pensions
• Also provide housing loans and insurance schemes
• Membership of the Norwegian Public Service Pension
  Fund is obligatory for government employees
• Stats
   –   950,000 members across 1600 organisations
   –   Approx 138,000 receive a retirement pension
   –   Approx 58,000 receive a disability pension
   –   950,000 members have total accrued pension entitlements
       in the Norwegian Public Service Pension Fund of 339
       billion kroner.
Background
• Every year the parliament sets the basic amount
  of the national insurance
• This amount is a constant used in calculation of all
  benefits
• When the basic amount is changed, all benefits
  must be recalculated
• It’s more complex than a constant in an algorithm
   – Rules are result of political games the last 50 years
   – Complex rules
   – Lots of exemptions
Execution time requirements

• SPK’s recalculation batch must run
  – After the basic amount is set
  – After the Labour and Welfare Administration
    has done it’s calculation
  – Before the pensions are due next month
• Window of 1 week
  – Will ideally only run during weekends
  – Can not run while case workers are doing
    their job
Spring Batch

• Framework for developing batch
  applications
• Implements batch semantics
  – Steps, Chunks, Stop, Restart, Skips, Retries
  – Partitioning, Multithreading, Parallel steps
A step…
The components
• ItemReader
  – read() returns one row at the time
  – Step is completed once read()returns null
• ItemProcessor
  – process(item) item is return value from read()
  – Business logic goes here
  – Items can be filtered out by returning null
• ItemWriter
  – Write(list) list of items returned from
    process()
The code
<job id=”foo”>
   <step id="fileImport">
     <tasklet>
          <chunk commit-interval="10"
            reader=“reader"
            processor=“processor"
            writer=“writer“/>
     </tasklet>
   </step>
</job>
<bean id=“reader" class="...">
<bean id=“processor" class="...">
<bean id=“writer" class="...">
Chunk
• A chunk is a unit of work
  – Executes within a transaction
  – Size is defined by number of items read
• A step is divided into chunks by the
  framework
• When x is the chunk size
  – read() is called x times
  – process() is called x times
  – write is called once with a list where list.size()==x
    (minus filtered items)
Scaling
Multi-threaded step
Id   Name

1    Paul

2    John

3    Lisa




                                     Execution
               Reader




                                       Step
4    Simon
              Processor      T1..*
5    Rick       Writer

6    Julia

7    Olivia

8    Scott

9    Martin
Partitioned Step
Id   Name




                                       Execution
               Reader




                                         Step
1    Paul                       T1
              Processor
2    John       Writer

3    Lisa




                                       Execution
               Reader




                                         Step
4    Simon
              Processor         T2
5    Rick       Writer

6    Julia




                                       Execution
7    Olivia    Reader




                                         Step
              Processor         T3
8    Scott      Writer
9    Martin
Hibernate 101

• Proxy
• Session cache
• Flushing
  – Queries
  – Commit
The pension recalculation batch
- Stored procedure
Step 1
                  Populate staging table


                  - Read from staging
                  - Fetch additional data
Step 2   Rules    - Call out to rules engine
         engine   - Store result as pending
                  pensions



                  - Loop through pending
Step 3
                  pensions and activate


                  - Create manual tasks
Step 4
                  for failed items
Staging table

• A batch processing pattern
• A simple table with a identity column,
  functional key and processing status
• Restart is easy
  – Select soc_sec from staging where
    processed=false
• An easy way to get parallel processing
  capabilities
Our staging table(s)
Reader

           Updates
         family status


         Processor
Step 2                   ILog

                                Sybase


           Writer
Hibernate in the reader

• Using a separate statefull session in
  reader
  – Not bound to active transaction
  – Is never flushed
  – clear() is called before commit
• Entities are written to database using a
  (different) transaction bound session
  – Used by processor and writer
Parallel batch processing with spring batch   slideshare
Problems?
Problems?
What happened?

org.hibernate.HibernateException: Illegal
  attempt to associate a collection with two
  open sessions
Why

• Family.errors and Family.persons are
  attached to reader’s session
• Attempting to attach them to transaction
  bound session
• Hibernate will have non of that!
Problems?
The solution?
Stateless sessions
Hibernate in the reader
                                (Second attempt)


• Let’s try stateless session
• Default behavior in Spring Batch’s
  Hibernate ItemReaders
  – useSateless=”true”
• LEFT JOIN FETCH for eager loading
  collections
  – Avoiding LazyLoadingExceptions
Hibernate in the reader
            (Second attempt)
Problems?
Problems?
What happened?

• org.hibernate.HibernateException: cannot
  simultaneously fetch multiple bags
Why

• Hibernate is unable to resolve the
  Cartesian product
  – Throws exception to avoid duplicates
You are using it wrong!
Curing the
                    Problems?
you are using it wrong
syndrome
Hibernate in the reader
                               (The third attempt)

• Examine the object graph
• Replace List with Set
  – Only one eagerly loaded collection may be of
    type list List
• This works…
  – ..for a while
  – We’ll revisit Hibernate later…
Exception resilience

• New demands sneak in
  – The batch should not abort
     • Not under any circumstance
• The batch should deal with
  – Missing or functionally corrupt data
  – Programming errors
  – ...
Pseudo code
try{
  //do data and business operations
}catch(Exception e){
   //Add error to staging & continue
   family.addError(createError(e));
}
Problems?
Problems?
Overzealous exception handling

an assertion failure occurred (this may
  indicate a bug in Hibernate, but is more
  likely due to unsafe use of the session)
Overzealous exception handeling
                               The solution

• Some exception MUST result in a rollback
  – Ex. StaleStateException
• Configure the framework to do retry/skip
  for these
• Only catch exceptions you know you can
  handle in a meaningful way
  – Nothing new here
  – Do not succumb to crazy requirements
Time to turn on parallelization

• We chose partitioned over mutli-threaded
  step
  – No need for a thread safe reader
    • Step scope
    • Each partition get a new instance of reader
  – Page lock contentions are less likely
    • Row 1 in partition 1 not adjacent to row 1 in
      partition 2
Deadlocks

• Legacy database using page locking
• Normalized database
• Relevant data for one person is spread
  across a number of tables
• Different threads will access same data
  pages
• Deadlocks will occur
Page locking
ID   NAME
1    Paul       T1
2    John       T2 waiting
3    Simon
4    Scott     T1 waiting

5    Lisa      T2
6    Jack
7    Nina
8    Linda      T3
DeadlockLoserDataAccessException
Retry to the rescue
<step id=”step2">
 <tasklet>
   <chunk reader="reader" processor="processor" writer="writer"
      commit-interval="10" retry-limit="10">
         <retryable-exception-classes>
            <include class=”…DeadlockLoserDataAccessException"/>
         </retryable-exception-classes>
   </chunk>
  </tasklet>
</step>
The mystery exception
Problems?
Two weeks later..
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
#42
Problems?
Retry
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
Id=42




StaleState
Exception
What do we do?
What we should have done weeks ago
We ditch Hibernate
…well, almost anyway
Removing Hibernate from
                                   reader

• ItemReader is re-configured to use JDBC
• Fetches primary key from family staging
  table
• ItemProcesser fetches staging object
  graph
  – Uses primary key to fetch graph with
    hibernate
• Primary keys are immutable and stateless
Parallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch   slideshare
End result

•   Performance requirement was 48 hrs
•   Completed in 16 hrs
•   Used 12 threads
•   C-version used 1 thread and ran for 1 week
    – Stopped each morning, started each evening
• A batch that scales with the infrastructure
    – Number of threads is configurable in .properties
What we would have done
                                differently

• Switched from partioned to multi-threaded
  step
  – All work is shared among threads
  – All threads will run until batch completes
  – Avoid idle threads towards the end
  – With partitioning some partitions finished well
    before others
Recommendations
• Do not use Hibernate in the ItemReader
• Test parallelization early
• Monitor your SQLs
  – Frequently called
  – Long running
• Become friends with your DBA
• There is no reason to let Java be the bottle
  neck
  – Increase thread count until DB becomes the
    bottle neck
Thanks!



@mortenag
www.github.com/magott
www.andersen-gott.com
Pro tip: @BatchSize
• If one lazily loaded entity is fetched, they
  are all fetched – in one query
Ad

Recommended

PDF
Unlocking the Power of Apache Flink: An Introduction in 4 Acts
HostedbyConfluent
 
PDF
Domain Driven Design và Event Driven Architecture
IT Expert Club
 
PPTX
Evening out the uneven: dealing with skew in Flink
Flink Forward
 
PDF
Sapo Microservices Architecture
Khôi Nguyễn Minh
 
PPTX
Unifying Stream, SWL and CEP for Declarative Stream Processing with Apache Flink
DataWorks Summit/Hadoop Summit
 
PPTX
Introduction to Storm
Chandler Huang
 
PDF
Deploying IPv6 on OpenStack
Vietnam Open Infrastructure User Group
 
PDF
[OpenInfra Days Korea 2018] (Track 2) Neutron LBaaS 어디까지 왔니? - Octavia 소개
OpenStack Korea Community
 
PPTX
Spring batch
Chandan Kumar Rana
 
PDF
Batch and Stream Graph Processing with Apache Flink
Vasia Kalavri
 
PPTX
One sink to rule them all: Introducing the new Async Sink
Flink Forward
 
PPTX
Introduction to Zabbix - Company, Product, Services and Use Cases
Zabbix
 
PDF
Mind the App: How to Monitor Your Kafka Streams Applications | Bruno Cadonna,...
HostedbyConfluent
 
PDF
Percona server for MySQL 제품 소개
NeoClova
 
PPTX
Using Queryable State for Fun and Profit
Flink Forward
 
PDF
Spring Batch Introduction (and Bitbucket Project)
Guillermo Daniel Salazar
 
PPT
Step-by-Step Introduction to Apache Flink
Slim Baltagi
 
PPTX
itlchn 20 - Kien truc he thong chung khoan - Phan 1
IT Expert Club
 
PPTX
Building a Unified Logging Layer with Fluentd, Elasticsearch and Kibana
Mushfekur Rahman
 
PDF
Zabbix monitoring in 5 pictures
Nicola Mauri
 
PDF
MyRocks Deep Dive
Yoshinori Matsunobu
 
PPTX
Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition!
Michel Schudel
 
PPTX
Hibernate jpa
Lhouceine OUHAMZA
 
PPTX
WebLogic Stability; Detect and Analyse Stuck Threads
Maarten Smeets
 
PDF
Spring mvc
Hamid Ghorbani
 
PPTX
From cache to in-memory data grid. Introduction to Hazelcast.
Taras Matyashovsky
 
PDF
Cassandra Introduction & Features
DataStax Academy
 
PDF
Networking in Java with NIO and Netty
Constantine Slisenka
 
PDF
Groovy concurrency
Alex Miller
 
PDF
Design & Develop Batch Applications in Java/JEE
Naresh Chintalcheru
 

More Related Content

What's hot (20)

PPTX
Spring batch
Chandan Kumar Rana
 
PDF
Batch and Stream Graph Processing with Apache Flink
Vasia Kalavri
 
PPTX
One sink to rule them all: Introducing the new Async Sink
Flink Forward
 
PPTX
Introduction to Zabbix - Company, Product, Services and Use Cases
Zabbix
 
PDF
Mind the App: How to Monitor Your Kafka Streams Applications | Bruno Cadonna,...
HostedbyConfluent
 
PDF
Percona server for MySQL 제품 소개
NeoClova
 
PPTX
Using Queryable State for Fun and Profit
Flink Forward
 
PDF
Spring Batch Introduction (and Bitbucket Project)
Guillermo Daniel Salazar
 
PPT
Step-by-Step Introduction to Apache Flink
Slim Baltagi
 
PPTX
itlchn 20 - Kien truc he thong chung khoan - Phan 1
IT Expert Club
 
PPTX
Building a Unified Logging Layer with Fluentd, Elasticsearch and Kibana
Mushfekur Rahman
 
PDF
Zabbix monitoring in 5 pictures
Nicola Mauri
 
PDF
MyRocks Deep Dive
Yoshinori Matsunobu
 
PPTX
Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition!
Michel Schudel
 
PPTX
Hibernate jpa
Lhouceine OUHAMZA
 
PPTX
WebLogic Stability; Detect and Analyse Stuck Threads
Maarten Smeets
 
PDF
Spring mvc
Hamid Ghorbani
 
PPTX
From cache to in-memory data grid. Introduction to Hazelcast.
Taras Matyashovsky
 
PDF
Cassandra Introduction & Features
DataStax Academy
 
PDF
Networking in Java with NIO and Netty
Constantine Slisenka
 
Spring batch
Chandan Kumar Rana
 
Batch and Stream Graph Processing with Apache Flink
Vasia Kalavri
 
One sink to rule them all: Introducing the new Async Sink
Flink Forward
 
Introduction to Zabbix - Company, Product, Services and Use Cases
Zabbix
 
Mind the App: How to Monitor Your Kafka Streams Applications | Bruno Cadonna,...
HostedbyConfluent
 
Percona server for MySQL 제품 소개
NeoClova
 
Using Queryable State for Fun and Profit
Flink Forward
 
Spring Batch Introduction (and Bitbucket Project)
Guillermo Daniel Salazar
 
Step-by-Step Introduction to Apache Flink
Slim Baltagi
 
itlchn 20 - Kien truc he thong chung khoan - Phan 1
IT Expert Club
 
Building a Unified Logging Layer with Fluentd, Elasticsearch and Kibana
Mushfekur Rahman
 
Zabbix monitoring in 5 pictures
Nicola Mauri
 
MyRocks Deep Dive
Yoshinori Matsunobu
 
Battle Of The Microservice Frameworks: Micronaut versus Quarkus edition!
Michel Schudel
 
Hibernate jpa
Lhouceine OUHAMZA
 
WebLogic Stability; Detect and Analyse Stuck Threads
Maarten Smeets
 
Spring mvc
Hamid Ghorbani
 
From cache to in-memory data grid. Introduction to Hazelcast.
Taras Matyashovsky
 
Cassandra Introduction & Features
DataStax Academy
 
Networking in Java with NIO and Netty
Constantine Slisenka
 

Similar to Parallel batch processing with spring batch slideshare (20)

PDF
Groovy concurrency
Alex Miller
 
PDF
Design & Develop Batch Applications in Java/JEE
Naresh Chintalcheru
 
KEY
Modern Java Concurrency
Ben Evans
 
KEY
Spring Batch Behind the Scenes
Joshua Long
 
PPTX
Spring batch
Deepak Kumar
 
PPTX
Java Batch
Software Infrastructure
 
PDF
Java EE 7 Batch processing in the Real World
Roberto Cortez
 
PDF
Spring batch overivew
Chanyeong Choi
 
PPT
Spring Batch Introduction
Tadaya Tsuyukubo
 
ODP
Pick up the low-hanging concurrency fruit
Vaclav Pech
 
PDF
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Arun Gupta
 
PDF
The Future of Futures - A Talk About Java 8 CompletableFutures
Haim Yadid
 
PPTX
Concurrent talk
rahulrevo
 
PDF
A Deep Dive into Query Execution Engine of Spark SQL
Databricks
 
KEY
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
KEY
Modern Java Concurrency (Devoxx Nov/2011)
Martijn Verburg
 
PDF
Exploring Terracotta
Alex Miller
 
PDF
Concurrency for dummies
Azrul MADISA
 
PDF
Batch Applications for the Java Platform
Sivakumar Thyagarajan
 
PPTX
Spring batch introduction
Alex Fernandez
 
Groovy concurrency
Alex Miller
 
Design & Develop Batch Applications in Java/JEE
Naresh Chintalcheru
 
Modern Java Concurrency
Ben Evans
 
Spring Batch Behind the Scenes
Joshua Long
 
Spring batch
Deepak Kumar
 
Java EE 7 Batch processing in the Real World
Roberto Cortez
 
Spring batch overivew
Chanyeong Choi
 
Spring Batch Introduction
Tadaya Tsuyukubo
 
Pick up the low-hanging concurrency fruit
Vaclav Pech
 
Batch Applications for Java Platform 1.0: Java EE 7 and GlassFish
Arun Gupta
 
The Future of Futures - A Talk About Java 8 CompletableFutures
Haim Yadid
 
Concurrent talk
rahulrevo
 
A Deep Dive into Query Execution Engine of Spark SQL
Databricks
 
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
Modern Java Concurrency (Devoxx Nov/2011)
Martijn Verburg
 
Exploring Terracotta
Alex Miller
 
Concurrency for dummies
Azrul MADISA
 
Batch Applications for the Java Platform
Sivakumar Thyagarajan
 
Spring batch introduction
Alex Fernandez
 
Ad

Recently uploaded (20)

PDF
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
PDF
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
PDF
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
PDF
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
PPTX
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
PDF
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
PDF
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
PDF
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
PDF
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
PPTX
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
PDF
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
PDF
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Mastering AI Workflows with FME by Mark Döring
Safe Software
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
 
Ad

Parallel batch processing with spring batch slideshare

  • 1. Parallel processing with Spring Batch Lessons learned
  • 2. Morten Andersen-Gott • Manager at Accenture Norway • 30 years old • Been using Spring Batch since 1.0M2 (2007) • Member of JavaZone program committee – http://tinyurl.com/javaever – http://tinyurl.com/thestreaming – http://tinyurl.com/ladyjava mortenag www.github.com/magott www.andersen-gott.com
  • 3. A BoF focusing on the problems • Functional background for the batch • Short introduction to Spring Batch • Even shorter on Hibernate • The problems • The problems • The problems
  • 4. Norwegian Public Service Pension Fund (SPK) • Norway’s main provider of public occupational pensions • Also provide housing loans and insurance schemes • Membership of the Norwegian Public Service Pension Fund is obligatory for government employees • Stats – 950,000 members across 1600 organisations – Approx 138,000 receive a retirement pension – Approx 58,000 receive a disability pension – 950,000 members have total accrued pension entitlements in the Norwegian Public Service Pension Fund of 339 billion kroner.
  • 5. Background • Every year the parliament sets the basic amount of the national insurance • This amount is a constant used in calculation of all benefits • When the basic amount is changed, all benefits must be recalculated • It’s more complex than a constant in an algorithm – Rules are result of political games the last 50 years – Complex rules – Lots of exemptions
  • 6. Execution time requirements • SPK’s recalculation batch must run – After the basic amount is set – After the Labour and Welfare Administration has done it’s calculation – Before the pensions are due next month • Window of 1 week – Will ideally only run during weekends – Can not run while case workers are doing their job
  • 7. Spring Batch • Framework for developing batch applications • Implements batch semantics – Steps, Chunks, Stop, Restart, Skips, Retries – Partitioning, Multithreading, Parallel steps
  • 9. The components • ItemReader – read() returns one row at the time – Step is completed once read()returns null • ItemProcessor – process(item) item is return value from read() – Business logic goes here – Items can be filtered out by returning null • ItemWriter – Write(list) list of items returned from process()
  • 10. The code <job id=”foo”> <step id="fileImport"> <tasklet> <chunk commit-interval="10" reader=“reader" processor=“processor" writer=“writer“/> </tasklet> </step> </job> <bean id=“reader" class="..."> <bean id=“processor" class="..."> <bean id=“writer" class="...">
  • 11. Chunk • A chunk is a unit of work – Executes within a transaction – Size is defined by number of items read • A step is divided into chunks by the framework • When x is the chunk size – read() is called x times – process() is called x times – write is called once with a list where list.size()==x (minus filtered items)
  • 13. Multi-threaded step Id Name 1 Paul 2 John 3 Lisa Execution Reader Step 4 Simon Processor T1..* 5 Rick Writer 6 Julia 7 Olivia 8 Scott 9 Martin
  • 14. Partitioned Step Id Name Execution Reader Step 1 Paul T1 Processor 2 John Writer 3 Lisa Execution Reader Step 4 Simon Processor T2 5 Rick Writer 6 Julia Execution 7 Olivia Reader Step Processor T3 8 Scott Writer 9 Martin
  • 15. Hibernate 101 • Proxy • Session cache • Flushing – Queries – Commit
  • 17. - Stored procedure Step 1 Populate staging table - Read from staging - Fetch additional data Step 2 Rules - Call out to rules engine engine - Store result as pending pensions - Loop through pending Step 3 pensions and activate - Create manual tasks Step 4 for failed items
  • 18. Staging table • A batch processing pattern • A simple table with a identity column, functional key and processing status • Restart is easy – Select soc_sec from staging where processed=false • An easy way to get parallel processing capabilities
  • 20. Reader Updates family status Processor Step 2 ILog Sybase Writer
  • 21. Hibernate in the reader • Using a separate statefull session in reader – Not bound to active transaction – Is never flushed – clear() is called before commit • Entities are written to database using a (different) transaction bound session – Used by processor and writer
  • 24. What happened? org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
  • 25. Why • Family.errors and Family.persons are attached to reader’s session • Attempting to attach them to transaction bound session • Hibernate will have non of that!
  • 28. Hibernate in the reader (Second attempt) • Let’s try stateless session • Default behavior in Spring Batch’s Hibernate ItemReaders – useSateless=”true” • LEFT JOIN FETCH for eager loading collections – Avoiding LazyLoadingExceptions
  • 29. Hibernate in the reader (Second attempt)
  • 31. What happened? • org.hibernate.HibernateException: cannot simultaneously fetch multiple bags
  • 32. Why • Hibernate is unable to resolve the Cartesian product – Throws exception to avoid duplicates
  • 33. You are using it wrong!
  • 34. Curing the Problems? you are using it wrong syndrome
  • 35. Hibernate in the reader (The third attempt) • Examine the object graph • Replace List with Set – Only one eagerly loaded collection may be of type list List • This works… – ..for a while – We’ll revisit Hibernate later…
  • 36. Exception resilience • New demands sneak in – The batch should not abort • Not under any circumstance • The batch should deal with – Missing or functionally corrupt data – Programming errors – ...
  • 37. Pseudo code try{ //do data and business operations }catch(Exception e){ //Add error to staging & continue family.addError(createError(e)); }
  • 39. Overzealous exception handling an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
  • 40. Overzealous exception handeling The solution • Some exception MUST result in a rollback – Ex. StaleStateException • Configure the framework to do retry/skip for these • Only catch exceptions you know you can handle in a meaningful way – Nothing new here – Do not succumb to crazy requirements
  • 41. Time to turn on parallelization • We chose partitioned over mutli-threaded step – No need for a thread safe reader • Step scope • Each partition get a new instance of reader – Page lock contentions are less likely • Row 1 in partition 1 not adjacent to row 1 in partition 2
  • 42. Deadlocks • Legacy database using page locking • Normalized database • Relevant data for one person is spread across a number of tables • Different threads will access same data pages • Deadlocks will occur
  • 43. Page locking ID NAME 1 Paul T1 2 John T2 waiting 3 Simon 4 Scott T1 waiting 5 Lisa T2 6 Jack 7 Nina 8 Linda T3
  • 45. Retry to the rescue <step id=”step2"> <tasklet> <chunk reader="reader" processor="processor" writer="writer" commit-interval="10" retry-limit="10"> <retryable-exception-classes> <include class=”…DeadlockLoserDataAccessException"/> </retryable-exception-classes> </chunk> </tasklet> </step>
  • 57. #42
  • 67. What do we do?
  • 68. What we should have done weeks ago
  • 70. Removing Hibernate from reader • ItemReader is re-configured to use JDBC • Fetches primary key from family staging table • ItemProcesser fetches staging object graph – Uses primary key to fetch graph with hibernate • Primary keys are immutable and stateless
  • 73. End result • Performance requirement was 48 hrs • Completed in 16 hrs • Used 12 threads • C-version used 1 thread and ran for 1 week – Stopped each morning, started each evening • A batch that scales with the infrastructure – Number of threads is configurable in .properties
  • 74. What we would have done differently • Switched from partioned to multi-threaded step – All work is shared among threads – All threads will run until batch completes – Avoid idle threads towards the end – With partitioning some partitions finished well before others
  • 75. Recommendations • Do not use Hibernate in the ItemReader • Test parallelization early • Monitor your SQLs – Frequently called – Long running • Become friends with your DBA • There is no reason to let Java be the bottle neck – Increase thread count until DB becomes the bottle neck
  • 77. Pro tip: @BatchSize • If one lazily loaded entity is fetched, they are all fetched – in one query

Editor's Notes

  • #4: trials and tribulationsfairy tale
  • #5: Offers a lot of different benefits for employees or former employees of the public sector in NorwayPlays a major part in the personal economy for a lot of Norwegian citizens
  • #6: To reflect changes in consumer price indexBenefits
  • #7: Labour and Wellfare Administration provides the minimum pensions for all Norwegian citizensWe rely on the calculations from the Wellfare Administration in our batch
  • #9: 2 loopsOuter: Loops while read returns dataInner: Loops over read+process according to commit-interval
  • #15: Data is partitioned before step executesThreads are processing isolated data setsExamples of partitioning strategiesOne file per partitionPartitioning a tablePartition 1: Row 1 – Row 1000Partition 2: Row 1001 – Row 2000Partition 3: Row 2001 – Row 3000
  • #48: Indicates that session contained stale dataThrown when a version number or timestamp check failsAlso occurs if we try delete or update a row that does not existStarted doing the numbers, how much can we do within 48 hours with a single thread?Weknewthattwothreadswould never accessthe same row. Same page, sure, but never same row.
  • #62: The ItemReader is forward-onlyWe can’t roll back a cursor next()Spring batch stores all read items in a retry cache until transaction is committedUses the cache for retries
  • #68: Hibernatethrows an exceptionwhenrealitydoes not match itsexpectations