Implementing parallel rating

Through configuration, you can implement parallel rating in a line of business. You can use commercial property, which implements parallel rating, as a model.

Can this policy line be rated in parallel?

Consider parallel rating for lines of business with policies that typically include a large number of coverables, and where generating quotes takes a noticeable amount of time. Parallel rating iterates over coverables in parallel using multiple threads to generate costs for each of the coverables.

The coverable object that will be rated in parallel must meet the following requirements:

  • The coverable must be EffDated
  • The coverable must not share data with other coverables that will be rated in parallel, or
  • Data shared among the coverables must be immutable or accessed in a thread-safe manner

Consider the size of the coverable when deciding which coverable to rate in parallel. If the coverable is large, then parallel rating requires fewer threads but may result in threads waiting idly for threads to finish rating other large coverables.

Ensuring thread safety

Because parallel rating makes use of multiple threads, the code must be thread-safe.

The implementation of parallel rating uses the following general coding guidelines for thread safety:
  • The PolicyLine variable is ThreadLocal so that each thread has its own instance of the PolicyLine.
  • Because bundles are not thread-safe, each thread works on its own separate bundle, and this bundle does not write to the database.
    • For parallel rating using entities, access to in-memory objects related to rating, including CostDataMap and WorksheetContainer, must be thread-safe.
    • For parallel rating using DTOs, the rating threads do not have access to a bundle. Consequentially, access to CostDataMap, WorksheetContainer, and other entities is deferred until rating is complete.
The AbstractParallelRatingEngineBase class implements thread safety, and the CPRatingEngine class calls the methods in this class.
  • The rating engine code has been reordered for thread-safety. Some tasks have been moved to the preRateStep method which is called before the code which runs in parallel. Other tasks have been moved to postRateStep method which is called after the code which runs in parallel.
  • The rating engine for the line of business calls the rateInParallel method, passing in a list of coverables along with the block of code that rates each coverable.
  • The rateInParallel method, spawn threads. Each thread rates a coverable by executing the block of code on the coverable. These threads are managed by a thread pool.
  • For parallel rating using entities, if the server is in development mode, the checkOriginalBundleState method verifies that the original coverables have not been modified.

Logging

Parallel rating logs general messages at INFO level and more detailed messages at the DEBUG level. Logging at the INFO level includes general information about actions taken by the rating engine. Logging at the DEBUG level includes timing for the overall rate as well as details about how long individual threads took to rate coverables.