Configuring multiple DI service providers within the same cluster
We're working on configuring Orleans in an environment where we expect that we will want to deploy multiple independent grain collections within the same cluster. On it's own, it seems like this is a non-issue. We can set up multiple grain collections with differently-versioned dependencies as long as the grain assemblies end up in different subfolders of the Applications folder, as documented here.
On the other hand, the setup of dependency injection doesn't seem to play as nicely with our use case. It seems like there must be a single Startup.ConfigureServices method across all grain collections, and all grains will be instantiated using the single produced IServiceProvider. From there, it doesn't seem possible to have the IServiceProvider produce ServiceA v1.1 when creating a grain from one collection while producing ServiceA v1.2 when creating a grain from another collection.
This would presumably work if the grain collections were split into multiple clusters or were forced to take on the same versions of their dependencies, but both of those at least feel like non-starters.
We're new to Orleans, so we could be missing something, but it's not possible for different grain collections to be configured to use different IServiceProvider instances, right? If not, is there a good way that this problem has been solved or sidestepped in the past?
I'm also curious about this. It looks like Orleans is just loading the GrainCollection DLLs using Assembly.LoadFrom(), which implies they are all in the same AppDomain. I had initially thought each grain collection would be in its own AppDomain to support loading the same assembly with different versions. Does this mean the only way differently-versioned assemblies can work is with binding redirects to the latest version?
This is undesirable for us, as there may be breaking incompatibilities between the assembly versions. With our current service-oriented architecture, teams are able to upgrade (and test) their services' dependencies on their own schedule.
How much of a challenge would it be to host each grain collection in a separate AppDomain, each with its own service provider container? Even if Orleans makes direct method invocations for co-located grains, all of the method parameter types and return types must be serializable already, so I don't think this would be a huge issue across domains. .
You can configure your cluster to use different IServiceProvider in different node in the same cluster.
see DependencyInjectionGrainTests as an example.
But if you want to deploy different grain collection in the same node and use different IServiceProvider, I believe we don't support that yet.
Does this help with your situation there ? @dbcates
@lee-alexander Hosting each grain collection in a separate AppDomain is definitely one way to solve this problem. But it also brings complication regarding to serialization between AppDomains. I'd like to further understand the use case here before going too far in this topic. And also if you want to host different micro services in the same host, orleans is going in the direction of coreclr compatible so you can deploy orleans in docker.
@xiazen We would like our grain collections to be as independent as possible from one another. They will be in separate VS solutions with different dependencies managed by different teams throughout the organization. However, ideally they are all deployed into the same Orleans instance.
Since the only interactions between grain collections should be grains passing serializable messages around, the AppDomain boundary shouldn't be a huge issue, right?
What's the downside of deploy those grain collection into different instances?
And another concern of grain collection with different version of dependency is compatibility between different dependency version. For example, their message sending to another AppDomain may not be serialized correctly due to different dependency version. How do you plan to handle that?
Today with our ASP.NET MVC apps, we must make sure that each service's HTTP API stays backwards compatible until all consumers (other services or JS) have had a chance to upgrade their calls. However, the implementation details of the services are free to change at any time. We would like to take a similar approach with our grain interfaces and DTOs, which would be exposed with the GrainInterfaces NuGet package (per grain collection). This way, it wouldn't matter if the grains dependencies were different, since they would only be communicating with DTOs we've ensured are backwards compatible.
With different Orleans instances (nodes?) per GrainCollection, we would have to think about how to scale each node based on each GrainCollection's performance requirements, and the grain is no longer the single unit of distribution. We would like to let Orleans handle all scaling for us.
I think I understand your use case here. Let me bring this up to the team . Will come back to you tomorrow.
There's complication of us enable this as an feature. One case I can think of right now is how to guide users to use this new feature correctly, regarding of dependency version compatibility/incompatibility in different AppDomain. You know how to use it since you instantiate this discussion. But others may not . :)
And also another case, is that coreclr doesn't support AppDomain, so I'm not sure what would need to be played there.
Thanks @xiazen , we look forward to hearing the team's thoughts!
I agree that it would be an advanced feature and possibly disabled by default. There could be a build flag to eliminate the AppDomain creation #if CORECLR, and then downstream code would just behave as if the feature were disabled.
We do believe that this feature is essential to us adopting Orleans more widely at our company. With ~15 teams on their own development/testing/release schedules, forcing a single set of dependencies would likely be very difficult. As such any support here would be greatly appreciated!
It's also, do we want to support this feature in coreclr version? If yes, what replacement of AppDomain will we use.
And also, does this different AppDomain share the same scheduler? Or each AppDomain has their own scheduler? How does it affect perf. How do orleans scale in this scenario? So many questions. lol
But I do think this is a solid use case, sent an email to the team to gather response. Sorry was sick this morning so I missed our team morning standup to gether opinion.
@xiazen, just to call it out as you shop this idea around, beyond dependency management, I also suspect that the multi-AppDomain approach may have the benefit of isolating faults to specific grain collections. At least in my testing, it looks like an errant grain hitting an OutOfMemoryException makes that whole silo unresponsive for a period of time, which is a bit of a drawback.
And we definitely understand that this is complex! You and the rest of the Orleans team likely have a better understanding of the implications for scheduling, performance, scaling, and portability to .NET Core than we do, so we're definitely interested in getting your thoughts. (And we hope you feel better soon as well!)
@dbcates Are you thinking to deploy a single cluster and within that multiple collections from different teams? This looks like an enterprise deployment scenario to me and I was thinking if the requirement for isolation is stronger than just a separate AppDomain. The separation requirement might "bleed" into storage side too.
@veikkoeeva, the primary reason that we want our grain collections to be isolated is the ability for development teams to update their dependencies independently. The isolated grain collections are related, though -- they are all the same product, and we would like them all to be able to communicate with one another. The concern is that the development scale is large enough that the isolation that we have with microservices today is beneficial, and we don't want to lose that with Orleans.
We do not think we need more stringent requirements for storage isolation.
@veikkoeeva for more context on our particular use case see https://github.com/dotnet/orleans/issues/2224
Edit: lol I see you were the one who replied to that issue originally
@dbcates You mentioned each team can have its own release schedule. Does this mean each team needs the ability to continue deploy their new release to production without restarting the whole Orleans application? since currently, you need to stop the silo, deploy new version of Orleans application and then start it again.
If you are interested in continue deployment to production env without interfere current production service of my teammate is working on it now. Not a easy problem, we are working on a prototype. :)
And as regards to AppDomain solution, it is absolutely one solution. But also there's many problems to solve in this. For example, scaling, assembly loading to different AppDomains, scheduling and storage isolation.
Maybe it is faster that we can take another direction. Instead of building these features into Orleans, We can build a deployment and hosting solution on top of Orleans, for enterprise customers like you. For example, still deploy grains from different teams to different Orleans node, but we have a solution for you to automate scale up or down those nodes based on its perf. This also solve the problem of one team's grain sabotage the whole silo and affect other team's service.
I think this may be faster to take us there to support enterprise customers. This is my opinion currently.
@xiazen We coordinate once a week full deployments across all teams, in which case we'll likely follow the advice given in the Orleans docs ("To upgrade to a new version of grain code, you need to provision and start a new deployment, switch incoming traffic to it, and then shut down the old deployment"). This is how we handle deployments of our ASP.NET MVC apps today (blue-green deployments).
However, when bug fixes need to be hot deployed, we can currently just replace any one micro-service with a new build in place on production. This seems like it would also be possible with separate AppDomains in Orleans, or with a deployment strategy like your teammate is working on. Though I wonder how this is possible without separate AppDomains since assemblies cannot be unloaded from an AppDomain once loaded. Would you just restart individual silos with the new code one by one, keeping the cluster as a whole alive?
An enterprise hosting/deployment solution on top of Orleans does sound like it could suit our needs here if the AppDomain solution proves to be too complex. Having the nodes automatically scale up/down would mostly keep the grain as the single unit of distribution. Though we do host fully on-premise so we wouldn't be able to use anything tied into Azure or other cloud providers.
Would you just restart individual silos with the new code one by one, keeping the cluster as a whole alive?
Yes we would upgrade individual silo one by one and keep the cluster as a whole alive. That's the current direction.
Yes I'm with you here, if we do an hosting/deployment solution, we need to take account of host fully on-premise case.
@dbcates @lee-alexander When you "grain collections", this sounds to me like a "microservice", based on the reasons for their existence that you mentioned: owned and managed by different team, independently versioned, etc. Is this a correct understanding?
If that's the case, then trying to put multiple microservices into a single silos cluster seems like a wrong approach to me. App domains provide very little isolation, and have a number of issues, some of which @xiazen already mentioned.
IMO deploying different microservices as different silos clusters is the right approach here. Since silos are just processes, multiple microservices can easily share a physical cluster. Running silos with different microservices as separate processes from different folders on same servers will solve independent deployment, dependency management, and isolation in a straightforward way. I'd like to understand your aversion to this approach.
One downside of the approach is that the microservices will have to talk to each other via their respective frontends (presumably HTTP) or queues. There's a desire to enable direct calls to grains in another cluster as part of #467, but it hasn't been done yet.
@sergeybykov Yes I would definitely classify them as microservices. I suppose our aversions to this approach are:
- Having to scale each cluster independently based on varying performance needs. But we have to do this today with our MVC apps so we aren't any worse off with Orleans.
- Not being able to treat the system as a whole as an object-oriented design. We have to expose HTTP endpoints for each service. #467 sounds like it would solve this, assuming that this works cluster to cluster and not just client (MVC) to cluster. Do you have any estimate on where that issue fits on the roadmap?
Having to scale each cluster independently based on varying performance needs. But we have to do this today with our MVC apps so we aren't any worse off with Orleans.
The simplest approach is not to scale them independently, and have an instance (a silo) of each of the microservices on each of the physical nodes. That way you'll scale your solution as a whole without worrying about optimal size for each of the microservices.
Not being able to treat the system as a whole as an object-oriented design. We have to expose HTTP endpoints for each service. #467 sounds like it would solve this, assuming that this works cluster to cluster and not just client (MVC) to cluster.
#467 with cluster to cluster calls will help blur the line between microservices. I'm not sure how desirable that is, but it should be possible at that point.
Do you have any estimate on where that issue fits on the roadmap?
@ReubenBond started thinking about some design options. If we come up with a reasonable non-confusing programming model, I'd like to see this in 2.0.