spring-cloud-function icon indicating copy to clipboard operation
spring-cloud-function copied to clipboard

Refactor Azure adapter to make sure it takes into account Azure invocation model

Open olegz opened this issue 6 years ago • 8 comments

Summary

  • Azure creates a new instance of handler on each invocation forcing us to cache in a static variable Spring Application Context to avoid its reinitialisation
  • Spring Cloud Function may have several functions in its context and each invocation will only use cached version of Spring's AC if requested function is the same, otherwise reinitialisation will still be triggered
  • There is also invocation id that comes with each request and may be important to some users, hence we must provide a mechanism to communicate such id to the function (e.g., Message header)

See more in the discussion - https://github.com/spring-cloud/spring-cloud-function/issues/425

olegz avatar Dec 18 '19 14:12 olegz

Another side effect of this issue is that logging only works on a cold start, when the same function is called multiple times in a row, logs are only captured the first time. This is due to the fact that each invocation generates a new context, yet the initial context is what is available in the handler.

jdwilkin avatar Mar 04 '20 22:03 jdwilkin

After hours debuging my code, finally I could address the root of all my problems: the static non thread-safe variable thisInitializer in the AzureSpringBootRequestHandler class.

The problems caused in my application are: 1 - Too many connections exception throwed by the Postgres (due to Spring reinitialization); 2 - ClassCastException thrown by the AzureSpringBootRequestHandler.handleRequest method (due the non thread-safe implementation). It occurs when two distinct functions with incompatible input/output types are called at the same time; 3 - Loss of logs sent to ExecutionContext.getLogger (due the context reinitialization).

My application is going to production soon, so I think I can't wait for the version 3.1.

Please, can anyone help me with some questions: 1 - When the new version (3.1) will be released? 2 - Is this bug already fiexed? If so, in which branch? 3 - How can I contribute to fix this issue?

EDIT: I've fixed the AbstractSpringFrunctionAdapterInitializer and AzureSpringBootRequestHandler in my application. I can commit the changes made, but I need directions about how I should do that in the spring-cloud-function repository. Obs: my implementation can cause breaking changes because the ExecutionContext will no more be autowired by the Spring Container, since it's not a Singleton, but a brand new object for every request.

tonyfarney avatar Oct 18 '20 01:10 tonyfarney

I’m facing the same issue and I’m glad someone else also got the same results and opened a bug. Thank you guys.

By the way, are there any plans to release 3.1 soon? This seems to be an important enhancement for applications relying on Azure Function Apps + SpringBoot. :)

EDIT: I’d be glad if you could confirm my understanding: a Function App with many Functions that are part of it would have only one SpringApplication instance initialized, is that correct?

glauber-md avatar Dec 10 '20 12:12 glauber-md

After hours debuging my code, finally I could address the root of all my problems: the static non thread-safe variable thisInitializer in the AzureSpringBootRequestHandler class.

The problems caused in my application are: 1 - Too many connections exception throwed by the Postgres (due to Spring reinitialization); 2 - ClassCastException thrown by the AzureSpringBootRequestHandler.handleRequest method (due the non thread-safe implementation). It occurs when two distinct functions with incompatible input/output types are called at the same time; 3 - Loss of logs sent to ExecutionContext.getLogger (due the context reinitialization).

My application is going to production soon, so I think I can't wait for the version 3.1.

Please, can anyone help me with some questions: 1 - When the new version (3.1) will be released? 2 - Is this bug already fiexed? If so, in which branch? 3 - How can I contribute to fix this issue?

EDIT: I've fixed the AbstractSpringFrunctionAdapterInitializer and AzureSpringBootRequestHandler in my application. I can commit the changes made, but I need directions about how I should do that in the spring-cloud-function repository. Obs: my implementation can cause breaking changes because the ExecutionContext will no more be autowired by the Spring Container, since it's not a Singleton, but a brand new object for every request.

This is huge. We're currently upgrading from 3.0.x to 3.2 due to some vulnerabilities 3rd party dependencies. Can you help to quote me the official document for this? I can't seem to find it. Thanks!

EthanNguyenDev avatar May 25 '22 01:05 EthanNguyenDev

After hours debuging my code, finally I could address the root of all my problems: the static non thread-safe variable thisInitializer in the AzureSpringBootRequestHandler class. The problems caused in my application are: 1 - Too many connections exception throwed by the Postgres (due to Spring reinitialization); 2 - ClassCastException thrown by the AzureSpringBootRequestHandler.handleRequest method (due the non thread-safe implementation). It occurs when two distinct functions with incompatible input/output types are called at the same time; 3 - Loss of logs sent to ExecutionContext.getLogger (due the context reinitialization). My application is going to production soon, so I think I can't wait for the version 3.1. Please, can anyone help me with some questions: 1 - When the new version (3.1) will be released? 2 - Is this bug already fiexed? If so, in which branch? 3 - How can I contribute to fix this issue? EDIT: I've fixed the AbstractSpringFrunctionAdapterInitializer and AzureSpringBootRequestHandler in my application. I can commit the changes made, but I need directions about how I should do that in the spring-cloud-function repository. Obs: my implementation can cause breaking changes because the ExecutionContext will no more be autowired by the Spring Container, since it's not a Singleton, but a brand new object for every request.

This is huge. We're currently upgrading from 3.0.x to 3.2 due to some vulnerabilities 3rd party dependencies. Can you help to quote me the official document for this? I can't seem to find it. Thanks!

I am not sure what documentation you are looking for... please check that one: https://cloud.spring.io/spring-cloud-function/reference/html/azure.html

tonyfarney avatar May 25 '22 03:05 tonyfarney

After hours debuging my code, finally I could address the root of all my problems: the static non thread-safe variable thisInitializer in the AzureSpringBootRequestHandler class. The problems caused in my application are: 1 - Too many connections exception throwed by the Postgres (due to Spring reinitialization); 2 - ClassCastException thrown by the AzureSpringBootRequestHandler.handleRequest method (due the non thread-safe implementation). It occurs when two distinct functions with incompatible input/output types are called at the same time; 3 - Loss of logs sent to ExecutionContext.getLogger (due the context reinitialization). My application is going to production soon, so I think I can't wait for the version 3.1. Please, can anyone help me with some questions: 1 - When the new version (3.1) will be released? 2 - Is this bug already fiexed? If so, in which branch? 3 - How can I contribute to fix this issue? EDIT: I've fixed the AbstractSpringFrunctionAdapterInitializer and AzureSpringBootRequestHandler in my application. I can commit the changes made, but I need directions about how I should do that in the spring-cloud-function repository. Obs: my implementation can cause breaking changes because the ExecutionContext will no more be autowired by the Spring Container, since it's not a Singleton, but a brand new object for every request.

This is huge. We're currently upgrading from 3.0.x to 3.2 due to some vulnerabilities 3rd party dependencies. Can you help to quote me the official document for this? I can't seem to find it. Thanks!

I am not sure what documentation you are looking for... please check that one: https://cloud.spring.io/spring-cloud-function/reference/html/azure.html

Thanks for sharing the docs.

We expect migration guide in official Microsoft documentation at least for that. AzureSpringBootRequestHandler also gives us access to Spring ApplicationContext via getContext().getBean("beanName"), this's no longer supported with FunctionInvoker.

This's the latest Spring Cloud docs BTW https://docs.spring.io/spring-cloud-function/docs/current/reference/html/azure.html

EthanNguyenDev avatar May 27 '22 02:05 EthanNguyenDev

Okay, I understand the issues you are facing. Unfortunately I can not help you. I didn't find any update guide either :(

tonyfarney avatar May 27 '22 04:05 tonyfarney

Okay, I understand the issues you are facing. Unfortunately I can not help you. I didn't find any update guide either :(

Thanks! I managed to refactor the code & it's now working. Just a bit of a surprise such a change didn't get documented in Official Microsoft Azure Functions Java developer guide

EthanNguyenDev avatar May 27 '22 10:05 EthanNguyenDev