Join Chander Dhall for an in-depth discussion in this video Create a stateful service, part of Microservices and Azure Service Fabric Basics for Developers.
- [Instructor] Now that we've seen how to create a stateless app, or a stateless service, what we would like to do next is add another service, in this case it needs to be a stateful service. So go to Linkedin, in this case that's your application project, and we're going to hit Add, and then New Service Fabric Service. And then pick Stateful. In this case, I could have called it Stateful App or Service, it really doesn't matter, hit Enter. As you can see, it's a very similar structure.
So you can go to PackageRoot, and you have Config, where you have your configuration Settings, and then you have ServiceManifest.xml, or you still have the StatefulSvcType, the "Code" and the Version, and the Exe. Program.cs, a very similar structure. You're registering the service Async, and then you use ServiceEventSource, and then execute the method ServiceTypeRegistered, which takes in a CurrentProcess().Id, and then typeof(StatefulSvc), and then the Name property.
By the way, ServiceEventSource is a class, and that's right here, if you are interested to check the source code. And then we have the StatefulSvc. Which is really an instance of the class that's created for every single node out there. What we are interested to look at is the RunAsync method. In this case, they have done a simple example for you already, which is they have a reliable dictionary, which is of the type <string, long>>, where key is the string and then long is the value, and it's called myDictionary.
So when you take myDictionary, we still have the same cancellationToken here. But what they are trying to do is, they are trying to get a value for a key, which is called "Counter". So if that exists, then all we have to do, is print that value, and then increment that value. Now what's different about this service? If you notice, in the previous case it was really how many times the loop is called, and then you keep showing that particular value. However, in this case what you have is you are keeping the state in your dictionary, which in this case is called myDictionary, and that dictionary has the value.
And if it is really stateful what should happen is that that state needs to be replicated across all the nodes. So what happens if we start the solution again? So once the application is up and running, you can right-click your Local Cluster, and say Manage Local Cluster. Once you do that, you can go to Applications, under LinkedinType. You can see that now we have two different applications. So if I were to click this, now I also have a stateful application.
If I were to go back into my Diagnostic Events, you can see we have two different things going on, one is working, which is coming from stateless service, and the second one is Current Counter Value, which is coming from a stateful service. Going back to our ServiceFabric Explorer, I can go into either Nodes, and I can see I have Node_0, which has a StatefulSvcPkg. It also has the Code Package. Then it also has the Replicas. So what happens if I were to go and kill this? I'm going to go to the Node, and go to Actions, and say Deactivate (pause).
The explorer wants me to copy the name of the node to make sure that I know what I'm doing, and I'm going to hit Deactivate. It says Node deactivation started. Now it will take some time, so it's really happening. And behind the scenes, a cancellation token has been paused to that particular instance, and now you see that this changed to Disabled as I was talking, and I can do the same thing here with Node_1, which doesn't really supposed to have anything. And then you have Node_2, which also has my StatefulSvcPkg, and Node_3 has the same thing, and we have Node_4.
Let's go ahead and kill the second node, say Deactivate. So this is going to deactivate Node_2, now we have two of them that are actually getting deactivated. As you can see, the status has changed to Disabling, and after some time it will be disabled. Now that we have two of the nodes disabled, we can still come back to our Diagnostic Events window, and we still see that the Counter Current Value is still increasing. And how is that really happening? So that proves that ServiceFabric is behind the scenes taking care of the value, replicating it to different nodes and giving you the state that it promised you.
Let's try to kill the third one too, and if I Deactivate number three, then we're only left with Node_4. Just to make sure, we can try to go to Node_1 and see if it still has anything. It doesn't look like it. The package is only running on Node_4 now. Node_3 is disabled, Node_2 is still disabling itself. And then we have Node_4. So we can see that it's still working. That tells you that it has a state, it's replicated it, and it also is taking care of failover for other nodes.
Because what if the other nodes were upgrading? If they had a (mumbles) upgrading, they will be paused, and then they will come back up. But your state is preserved. Another benefit of the Diagnostic Event is that if you click on one of these, it will actually give you a little bit more details. So I would like to pause this, and then open one of these events. So you can see, we have a timestamp, and we have a provider name. We also have a message, and this message is coming from the stateful app we just built, and we have the process ID, and a lot of other information that might come in handy.
So in this case, we have the serviceName. We also have the node number, which is this case is Node_4, and then we also have the replicaOrInstanceId, and then we have the "partitionId". So this can tell you, in cases you need to know which of the nodes is not working. So for example, some replica or instance stops responding and you're not expecting that, you can also find out which are the ones that are actually responding.
- Reviewing microservices vs. monolithic architecture
- Reviewing microservices and Azure Service Fabric basics
- Programming model architecture
- Creating a stateless service and a stateful service
- Creating a cluster in Azure
- Adding security to a cluster
- Finalizing cluster creation
- Deploying to an Azure cluster
- Debugging an application remotely