Slide 1

Slide 1 text

. Drools Trouble-Shooting ● Agenda ● Unexpected rule firing ● Property Reactivity ● Debugging Spreadsheet ● Control the flow of rule execution ● Performance issues Demo projects can be found in https://github.com/tkobayas/kie-live They run with Drools 7.52.0.Final : When you develop/maintain a project, please use the latest version as possible!

Slide 2

Slide 2 text

. What's wrong with this rule?

Slide 3

Slide 3 text

. My rule is looping! (recap) ● Use EventListener to understand what's going on ● DebugRuleRuntimeEventListener ● DebugAgendaEventListener ● You can write your custom listeners This Rule is activated Firing this Rule This Object is updated This Rule is activated again!

Slide 4

Slide 4 text

. My rule is looping! (recap) ● no-loop ● Only prevents a loop which is triggered by the rule itself ● lock-on-active ● Once the rule is fired, it's no longer fired regardless of the origin of re-evaluation

Slide 5

Slide 5 text

. Property reactivity

Slide 6

Slide 6 text

. Property reactivity (recap) Why this doesn't loop?

Slide 7

Slide 7 text

. Property reactivity (recap) Why this doesn't loop? Because Drools Engine analyzes rules: 1) This rule only tests item 2) item is not changed

Slide 8

Slide 8 text

. Property reactivity (previous example) This rule loops Because Drools Engine knows: 1) This rule tests discount 2) discount is changed

Slide 9

Slide 9 text

. Property reactivity (recap) Drools Engine doesn't know which property is tested LOOP! blackbox method

Slide 10

Slide 10 text

. Property reactivity (recap) Don't loop! This @watch tells : Re-evaluate this Pattern only when item is changed

Slide 11

Slide 11 text

. Property reactivity ● Since Drools 7, property reactivity is enabled by default ● Be careful when you migrate from Drools 6 ● Property reactivity is good for performance and you can write rules more naturally. ● If you hit a strange behavior and doubt property reactivity, temporarily disable it so you can confirm the difference. kmodule.xml

Slide 12

Slide 12 text

. Debugging Spreadsheet

Slide 13

Slide 13 text

. Debugging Spreadsheet (recap) [main] ERROR Unable to build KieBaseModel:defaultKieBase [10,15]: [ERR 101] Line 10:15 no viable alternative at input '' [10,35]: [ERR 101] Line 10:35 no viable alternative at input '' [0,0]: Parser returned a null Package This message means, "A wrong word/character here!" Line# and Column# in the generated DRL (Note: \t is 1 char)

Slide 14

Slide 14 text

. Debugging Spreadsheet (recap) ● drools.dump.dir ● SpreadsheetCompiler

Slide 15

Slide 15 text

. How to control the flow of rules? First evaluation group Second evaluation group Second evaluation group Final evaluation group

Slide 16

Slide 16 text

. How to control the flow of rules? ● Split into multiple KieBases ● Good for maintenance/debug if your kbase is large ● Salience ● Most convenient but too much use could be messy ● Agenda Group ● Standard approach ● Ruleflow Group ● Good combination with BPMN ● Control Fact ● Flexible

Slide 17

Slide 17 text

. Performance issues: ● Set up your benchmark for repeatable testing ● JMH (https://github.com/openjdk/jmh) is recommended for fine tuning ● Enable GC logging ● Narrow down the bottle-neck ● Rule execution itself? ● kie-server payload marshalling? ● Application logic? ● External system access in RHS? (e.g. DB, Web Service)

Slide 18

Slide 18 text

. The first response is slow ● KieBase loading is slow ● Try executable model ● just add dependency "drools-model-compiler", Then build a kjar (mvn clean install) Kogito uses executable model!

Slide 19

Slide 19 text

. The first response is slow ● First "fireAllRules" invocation is slow -> it gradually gets faster ● Various internal caching ● MvelConstraint is slow at first. Then MvelConstraint turns into a Java class dynamically so it will gets faster ● JVM Jitting ● Try executable model ● doesn't have Mvel related perf issues ● Warm up KieBase ● Create a dummy KieSession. Insert dummy facts into a ksession and fire ● Improvement depends on your rules. If your application is sensitive about the first response, it's worth trying

Slide 20

Slide 20 text

. Rule execution is slow (Best Practice) ● Watch out for Cross Product If you insert 1000 Customer facts and 1000 Product facts, 1000*1000 combination will be evaluated!

Slide 21

Slide 21 text

. Rule execution is slow (Best Practice) ● List the most restrictive rule conditions first ● Use simple field constraints as possible (No eval)

Slide 22

Slide 22 text

. Rule execution is slow ● But reviewing lots of rules is hard... drools-metric helps your analysis Note: drools-metric is not for production environment. Use it in your test environment to analyze a bottleneck

Slide 23

Slide 23 text

. Rule execution is slow (recap) ● Find bottle-neck Node from drools-metric logs Node name How many time the condition is evaluated elapsed time (micro second)

Slide 24

Slide 24 text

. Rule execution is slow (recap) ● ReteDumper.dumpAssociatedRulesRete() shows which rule is associated with the problematic Node Check this rule!

Slide 25

Slide 25 text

. Rule execution is slow (recap) ● Fix rules following the best practice ● For detailed explanation, please read: ● https://github.com/tkobayas/MetricLogUtils/wiki/How-to-use-Me tricLogUtils

Slide 26

Slide 26 text

. Community ● Zulip chat ● https://kie.zulipchat.com/ ● #drools ● Google group ● https://groups.google.com/g/drools-usage ● https://groups.google.com/g/drools-setup