azureazure-service-fabricservice-fabric-statelessservice-fabric-actor

Dynamically creating services in an service fabric application


This is a bit descriptive so please bear with me. :)

In the application that I'm trying to build, there are distinct functionalities of product. Users can choose to opt-in for functionality A, B, D but not C. The way I'm building this, is that each of the distinct functionality is a Service (stateless, I'm thinking of storing the data in Azure SQL DBs and exposing REST APIs from each service). Bundled all services together is an ApplicationType. For each customer tenant (consider this as an shared account of a group of users) that is created, I'm thinking of creating a new concrete instance of registered ApplicationType using a TenantManagementService and calling client.ApplicationManager.CreateApplicationAsync() on a FabricClient instance so that I can have a dedicated application instance running on my nodes for that tenant. However, as I mentioned, a tenant can choose to opt-in only for specific functionality which is mapped to a subset of services. If a tenant chooses only service A of my Application, rest of the service instances corresponding to features B, C, D shouldn't be idly running on the nodes.

I thought of creating actors for each service, but the services I'm creating are stateless and I'd like to have multiple instances of them actively running on multiple nodes to load balance rather than having idle replicas of stateful services.

Similar to what I'm doing with application types, i.e., spawning application types as a new tenant registers, can I spawn/delete services as and when a tenant wants to opt-in/out of product features?

Here's what I've tried: I tried setting InstanceCount 0 for the services at when packaging my application. In my ApplicationParameters XML files:

<Parameters>
    <Parameter Name="FeatureAService_InstanceCount" Value="0" />
    <Parameter Name="FeatureBService_InstanceCount" Value="0" />
</Parameters>

However, Service Fabric Explorer cribs when instantiating the application out of such application type. The error is this: zero service instance count error

But on the other hand, when a service is deployed on the fabric, it gives me an option to delete it specifically, so this scenario should be valid. enter image description here

Any suggestions are welcome!

EDIT: My requirement is similar to the approach mentioned by anderso in here - https://stackoverflow.com/a/35248349/1842699, However, the problem that I'm specifically trying to solve is to upload create an application instance with one or more packaged services having zero instance count!


Solution

  • @uplnCloud

    I hope I understand everything right.

    Your situation is the following:

    If I get it right then this is supported out of the box.

    First of all you should remove <DefaultServices /> section from the ApplicationManifest.xml. This will instruct Service Fabric to don't create services automatically with the application.

    Now the algorithm is the following:

    1. Create application using FabricClient.ApplicationManager.CreateApplicationAsync()
    2. For each required feature create a new corresponding Service using FabricClient.ServiceManager.CreateServiceAsync() (you need to specify the Application name of newly created Application)

    Also note that CreateServiceAsync() accepts ServiceDescriptor that you can configure all service related parameters - starting from partitioning schema and ending up with instance count.