Closed omercelikceng closed 1 year ago
@omercelikceng This is indeed a tricky one. AOT requires these need to be set at build time. The generated output is based on the config at build time. Applying such optimizations early implies the following restrictions:
See this for more info: https://docs.spring.io/spring-framework/reference/core/aot.html#aot.bestpractices Thanks, @onobc, for pointing this out.
Moreover, this is not just a config-server-only issue. Any property (via CLI, env variables, etc.) that changes the application context at runtime will have the same issue.
After chatting with the team internally, we concluded that this is certainly a restriction in place by the AOT engine. The best we can recommend is putting as much config as possible at build time for initializing the binder context. This is not ideal, but please let us know how it goes.
Thank you very much for your explanations @sobychacko . I try to contribute to your community as much as I can. I want to say some things. Actually, we can fetching configuration from config-server. Wouldn't it be more useful if we went for another solution instead of creating a class for each binder in your source code? Since I don't know the code as much as you do, I might be giving a ridiculous idea. I understand the issue is related to AOT. But I thought we could solve it by making changes to the code. I will also try to spend more time review into your code.
Wouldn't it be more useful if we went for another solution instead of creating a class for each binder in your source code?
Not sure I follow. In Spring Cloud Stream, we create child contexts for the binders in order to support multi-binder-based applications. This is fundamental to the binder bootstrapping architecture and hard to change. Is this what you meant?
Yes. I guess the code architecture is hard to change. However, it will be very difficult for developers to build the code for every change I make in the configuration (Example: topicName). I think it is important to find a solution to this issue.
I'm not as knowledgeable about the subject as you are. So forgive me if I have any funny thoughts.
No worries. Let us chat more so that we can understand your requirements. Are we still talking about the original concerns raised in this issue, i.e., properties provided via config-server and therefore it cannot work at runtime in an AOT context?
Yes. Let me try to explain to you what I tried.
There is no problem in fetching data from the Config server at runtime in a spring native project.
However, in a native spring project, spring cloud obliges me to specify stream binders at build time. This time, I will have to rebuild the code every time the binder configuration changes. This is a very difficult development process for me. I'm wondering if the binder creation mechanism can be modified to find a solution to this.
Also, I didn't look too closely. Do I need to specify other configurations at build time? For example; Do configurations under bindings need to be specified at build time?
All information that needs to be run in AOT and native mode must be provided at the build time so that the AOT engine can build the context. Please take a look at the docs I linked above for details. Any Spring context modifying operations are not permitted when running AOT mode as everything needed to run in AOT mode is already created at build time.
Thank you @sobychacko. I will do more research on native and read the resources you provided. Maybe I'll come back after I learn more. 😊
@sobychacko Would you like me to close the issue because I could not explain the problem clearly or for a similar reason?
@omercelikceng If you want to close the issue, that is fine. I think you explained the problem really well. The thing is that because of the AOT limitations, it is very hard to achieve a solution in this case. If you want to close this issue and ponder on an alternative solution, that is fine with us. We can always link this issue if you send a PR related to this.
@sobychacko I'm glad I was able to explain the problem well. I will work on this and to open a pull request. Thanks for be interested.
@sobychacko . I re-read all of our conversations today. I realized that I couldn't explain myself well. If it's okay for you, I'll try to open a pull request.
What I am trying to suggest is; we are using spring prototype beans for these cases. I know that for AOT runtime code generation is not possible. But instead of generating specific classes for each binder at the build time, if we use a generic prototype bean and create a different instance for each binder during runtime, it will be both suitable for AOT and JIT. By this way, may be the fundamental bootstrapping architecture will not be changed too much, and still we have different instances for binders at the runtime.
@omercelikceng the PRs are always welcomed. so you don't have to ask ;)
But based on my understanding of what you are proposing, it will be a fundamental change in the architecture of s-c-stream as @sobychacko pointed out earlier. Regardless, going through this effort would probably help you as well to understand what we are dealing with.
That said, there are few things that the greater s-c-cloud team is working on, specifically to deal with AOT and Config server.
Feel free to close this issue if you want to
Got it @olegz. Thank you very much to both you and @sobychacko for your explanations. You know the project better than me and you have much better experience than me. I have infinite respect for you, @sobychacko and spring cloud stream team. I just wanted to talk about this issue because I think it's important. But I appreciate your suggestions and will definitely take into consideration. Instead of spending effort on this issue, I will try to solve other issues. Thank you very much again.
Hello,
I have a spring application running using Spring Cloud Stream Function(Spring Native). I am consuming some data using cloud function. I generated a native docker image with spring-boot-maven-plugin.
Additionally, I read spring cloud stream properties from config-server. This is exactly where my problem starts. Actually, I have no problem reading properties from config-server. However, for native (AOT), it seems like I have to specify spring cloud stream binders at build time. But I want to get the binder configurations from config server during application startup as I have been doing before native.
Case 1:
If I configure the application to connect to the config-server while building the project, the code runs without any problem.
Related application.yml
What I see in your code is that If I build as shown above (fetching from config server during build time), the following classes are generated for all binders correctly. Then, afterwards there is no problem in getting these classes from applicationContext in runtime and use them(DefaultBinderFactory - getBinderInstance- this.binderChildContextInitializers.get(configurationName).initialize(binderProducingContext)). But unfortunately, I don't have the chance to access config server in build time. I did this for testing purposes. I can't work like this in Production.
Case 2:
Another option is to disable property reading from the config server at build time.
In this case, I enable config-server from docker-compose.yml to fetch the properties from the config server at runtime. If I do it this way, the relevant classes for the binders are not generated that are supposed to be generated in build time. Then I get an error because these binders cannot be found in the runtime.
Case 1 - Classes Generated After Code Build (Config.Enabled = True)
Case 2 - Classes Generated After Code Build (Config.Enabled = False)
I am getting the error here. Because the binderChildContextInitializers instance in DefaultBinderFactory was not initialized with the relevant binders at build time. It seems like this is a must to fill the binderChildContextInitializers in native build.
Error :
I kind of get that these should be initialized during build time but how can we get over this situation (fetching binders during application startup)? Recently, I have been opening pull requests for problems caused by Spring Native. If you direct me, I can make the development and open a pull request.