SlideShare a Scribd company logo
1 of 77
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
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..
#42
Problems?
Retry
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
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

More Related Content

What's hot

Spring batch for large enterprises operations
Spring batch for large enterprises operations Spring batch for large enterprises operations
Spring batch for large enterprises operations Ignasi González
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquareApigee | Google Cloud
 
SpringBoot with MyBatis, Flyway, QueryDSL
SpringBoot with MyBatis, Flyway, QueryDSLSpringBoot with MyBatis, Flyway, QueryDSL
SpringBoot with MyBatis, Flyway, QueryDSLSunghyouk Bae
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Yoshifumi Kawai
 
Git flow for daily use
Git flow for daily useGit flow for daily use
Git flow for daily useMediacurrent
 
HBase Tales From the Trenches - Short stories about most common HBase operati...
HBase Tales From the Trenches - Short stories about most common HBase operati...HBase Tales From the Trenches - Short stories about most common HBase operati...
HBase Tales From the Trenches - Short stories about most common HBase operati...DataWorks Summit
 
Monitoring, the Prometheus Way - Julius Voltz, Prometheus
Monitoring, the Prometheus Way - Julius Voltz, Prometheus Monitoring, the Prometheus Way - Julius Voltz, Prometheus
Monitoring, the Prometheus Way - Julius Voltz, Prometheus Docker, Inc.
 
Logging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & KibanaLogging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & KibanaAmazee Labs
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringScyllaDB
 
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git민태 김
 
Initiation à Express js
Initiation à Express jsInitiation à Express js
Initiation à Express jsAbdoulaye Dieng
 

What's hot (20)

Spring Batch 2.0
Spring Batch 2.0Spring Batch 2.0
Spring Batch 2.0
 
Spring batch for large enterprises operations
Spring batch for large enterprises operations Spring batch for large enterprises operations
Spring batch for large enterprises operations
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at Square
 
SpringBoot with MyBatis, Flyway, QueryDSL
SpringBoot with MyBatis, Flyway, QueryDSLSpringBoot with MyBatis, Flyway, QueryDSL
SpringBoot with MyBatis, Flyway, QueryDSL
 
React js
React jsReact js
React js
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)
 
Prometheus monitoring
Prometheus monitoringPrometheus monitoring
Prometheus monitoring
 
Git flow for daily use
Git flow for daily useGit flow for daily use
Git flow for daily use
 
HBase Tales From the Trenches - Short stories about most common HBase operati...
HBase Tales From the Trenches - Short stories about most common HBase operati...HBase Tales From the Trenches - Short stories about most common HBase operati...
HBase Tales From the Trenches - Short stories about most common HBase operati...
 
Monitoring, the Prometheus Way - Julius Voltz, Prometheus
Monitoring, the Prometheus Way - Julius Voltz, Prometheus Monitoring, the Prometheus Way - Julius Voltz, Prometheus
Monitoring, the Prometheus Way - Julius Voltz, Prometheus
 
WebAssembly Overview
WebAssembly OverviewWebAssembly Overview
WebAssembly Overview
 
Spring batch
Spring batchSpring batch
Spring batch
 
Jenkins
JenkinsJenkins
Jenkins
 
Logging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & KibanaLogging with Elasticsearch, Logstash & Kibana
Logging with Elasticsearch, Logstash & Kibana
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 
Git and git flow
Git and git flowGit and git flow
Git and git flow
 
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
 
Tomcat
TomcatTomcat
Tomcat
 
Initiation à Express js
Initiation à Express jsInitiation à Express js
Initiation à Express js
 
Git cheatsheet
Git cheatsheetGit cheatsheet
Git cheatsheet
 

Similar to Parallel batch processing with spring batch slideshare

Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Ryan Cuprak
 
2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwordsNitay Joffe
 
Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Guillermo Daniel Salazar
 
2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users GroupNitay Joffe
 
Asynchronous Awesome
Asynchronous AwesomeAsynchronous Awesome
Asynchronous AwesomeFlip Sasser
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the WildTomer Gabel
 
Parallel programming
Parallel programmingParallel programming
Parallel programmingSwain Loda
 
Optimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant callerOptimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant callerAllineaSoftware
 
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionCassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionDataStax Academy
 
Cassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionCassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionDataStax Academy
 
Cassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionCassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionDataStax Academy
 
Eureka Moment UKLUG
Eureka Moment UKLUGEureka Moment UKLUG
Eureka Moment UKLUGPaul Withers
 
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelSilicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelDaniel Coupal
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraJon Haddad
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Yuta Iwama
 
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACPerformance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACKristofferson A
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to ProductionJBUG London
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to ProductionC2B2 Consulting
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplainedshannomc
 

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

Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)
 
2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords
 
Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)
 
2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group
 
Asynchronous Awesome
Asynchronous AwesomeAsynchronous Awesome
Asynchronous Awesome
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 
Parallel programming
Parallel programmingParallel programming
Parallel programming
 
Optimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant callerOptimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant caller
 
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionCassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
 
Cassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionCassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in Production
 
Cassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionCassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in Production
 
Eureka Moment UKLUG
Eureka Moment UKLUGEureka Moment UKLUG
Eureka Moment UKLUG
 
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelSilicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016
 
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACPerformance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to Production
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to Production
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplained
 

Recently uploaded

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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
  • 22.
  • 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>
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57. #42
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 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
  • 71.
  • 72.
  • 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

  1. trials and tribulationsfairy tale
  2. 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
  3. To reflect changes in consumer price indexBenefits
  4. Labour and Wellfare Administration provides the minimum pensions for all Norwegian citizensWe rely on the calculations from the Wellfare Administration in our batch
  5. 2 loopsOuter: Loops while read returns dataInner: Loops over read+process according to commit-interval
  6. 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
  7. 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.
  8. 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
  9. Hibernatethrows an exceptionwhenrealitydoes not match itsexpectations