Skip to content

Share routing rules among all requests#211

Open
deigote wants to merge 1 commit intolyft:masterfrom
deigote:share-routing-rules-among-all-queries
Open

Share routing rules among all requests#211
deigote wants to merge 1 commit intolyft:masterfrom
deigote:share-routing-rules-among-all-queries

Conversation

@deigote
Copy link

@deigote deigote commented Sep 26, 2023

Small performance improvement plus it very likely closes #166 (it solved it for us at least).

By placing the Rules creation inside the lambda, the previous implementation required to read the routing rules config file for each request, adding unnecessary latency. More importantly, it was not thread-safe, as the rulesEngine and ruleFactory were shared among all requests, only the Rules creation wasn't.

The ruleFactory is of class MVELRuleFactory, which is stateful - it has RuleDefinitionReader reader and ParserContext parserContext as private members. Creating the Rules changes that state, so it should not be shared between concurrent threads. Rules OTOH is a read-only class after it's created (it's just iterated over by the DefaultRulesEngine). Given that the rules are the same for all requests, and that the Rules usage is thread-safe, it makes sense to share it across all of them.

Since deploying this our profiling tool stopped showing the RoutingGroupSelector (which was consuming around 1-2% before) and we stopped seeing the error described in #166 .

the previous implementation required to read the routing rules config file for each request, adding unnecessary latency. More importantly, it was done in a non-thread-safe way, as the `rulesEngine` and `ruleFactory` were shared among all requests, but the `Rules` creation was not. \

Since the class MVELRuleFactory is stateful (as it has `RuleDefinitionReader reader` and `ParserContext parserContext` as private members), it should not be shared between concurrent threads. `Rules` OTOH is a read-only class after it's created (it's just iterated over by the `DefaultRulesEngine`). Given that the rules are the same for all requests, and that the `Rules` usage is thread-safe, it makes sense to share it across all of them.\

We have deployed this code in production at Datadog and our APM profiling tool shows a reduction of around ~1-2%, plus we stopped seeing the error described in lyft#166
@awdavidson
Copy link

I believe this change will require the following test to be updated: https://github.com/deigote/presto-gateway/blob/share-routing-rules-among-all-queries/gateway-ha/src/test/java/com/lyft/data/gateway/ha/router/TestRoutingGroupSelector.java#L85

Given the change in the rules file, the gateway would need restarting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Random "Error opening rules configuration file" when using Routing Rule Engine

2 participants