Example #1
0
        protected override void OnReceive(object message)
        {
            switch (message)
            {
            case Storage storage:
                FileManagement.writeFile(storage);
                break;

            case WriteState state:
                FileManagement.writeStateMachineFile(state.nodeName, state.state);
                break;
            }
        }
Example #2
0
 protected void loadDataFromDisk()
 {
     // If we have persistent data on disk, get it
     if (FileManagement.checkFileExists(Self.Path.Name))
     {
         Storage storage = FileManagement.readFile(Self.Path.Name);
         if (storage != null)
         {
             this.currentTerm         = storage.currentTerm;
             this.votedForCandidateId = storage.votedFor;
             this.logEntries          = new Log(storage.entries);
             this.commitIndex         = storage.commitIndex;
             while (this.commitIndex > this.lastApplied)
             {
                 this.updateLastApplied();
             }
         }
     }
 }
Example #3
0
        static async Task Main(string[] args)
        {
            //Remove all the json files
            for (int i = 0; i < 5; i++)
            {
                string nodeName = string.Concat("Node-", i + 1);
                if (!FileManagement.checkFileExists(nodeName))
                {
                    continue;
                }
                FileManagement.deleteFile(nodeName);
                FileManagement.deleteStateFile(nodeName);
            }

            Console.WriteLine("Starting actor...");

            var sys = ActorSystem.Create("system");

            IActorRef supervisor   = sys.ActorOf(Props.Create <Supervisor>(), "super");
            IActorRef storageActor = sys.ActorOf(Props.Create <StorageActor>(), "storage");

            // Create the nodes
            IActorRef        raftNode      = null;
            IActorRef        frontEndNode  = null;
            List <IActorRef> raftNodes     = new List <IActorRef>();
            List <IActorRef> frontEndNodes = new List <IActorRef>();
            int clusterSize = 5;

            for (int i = 1; i <= clusterSize; i++)
            {
                raftNode = (IActorRef)(await supervisor.Ask((Props.Create <RaftServer>(i, raftNodes, storageActor, sys), string.Concat("Node-", i))));
                raftNodes.Add(raftNode);

                frontEndNode = (IActorRef)(await supervisor.Ask((Props.Create <TicketingFrontEnd>(raftNode, raftNodes, frontEndNodes), string.Concat("Client-", i))));
                frontEndNodes.Add(frontEndNode);
            }

            // IActorRef clientActor = sys.ActorOf(Props.Create<ClientActor>(raftNodes[0], raftNodes), "client");
            IActorRef routerActor = sys.ActorOf(Props.Create <Router>(frontEndNodes, raftNodes), "router");

            bool done = false;

            while (!done)
            {
                var input = Console.ReadLine();
                switch (input)
                {
                case "kill":
                    foreach (IActorRef server in raftNodes)
                    {
                        server.Tell(new KillLeader());
                    }
                    break;

                case "poison":
                    foreach (IActorRef server in raftNodes)
                    {
                        server.Tell(new PoisonLeader());
                    }
                    break;

                case "end":
                    done = true;
                    break;

                case "read":

                    break;

                case "":
                    foreach (IActorRef node in raftNodes)
                    {
                        //clientActor.Tell(new UnstableReadCommand(raftNode));
                        node.Tell(new PrintStateMachine());
                    }
                    break;
                    // default:
                    //     allNodes[0].Tell(new ClientRequest(input), clientActor);
                    //     break;
                }
            }

            //Remove all the json files
            foreach (IActorRef actor in raftNodes)
            {
                FileManagement.deleteFile(actor.Path.Name);
                FileManagement.deleteStateFile(actor.Path.Name);
            }

            await sys.Terminate();
        }