Beispiel #1
0
            public RecoveryPlan(IDirectBuffer buffer, int offset)
            {
                decoder.Wrap(buffer, offset, RecoveryPlanDecoder.BLOCK_LENGTH, RecoveryPlanDecoder.SCHEMA_VERSION);

                lastLeadershipTermId      = decoder.LastLeadershipTermId();
                lastTermBaseLogPosition   = decoder.LastTermBaseLogPosition();
                lastTermPositionCommitted = decoder.LastTermPositionCommitted();
                lastTermPositionAppended  = decoder.LastTermPositionAppended();

                ReplayStep snapshot = null;

                termSteps = new List <ReplayStep>();
                int stepNumber = 0;

                var stepDecoder = decoder.Steps();

                for (var i = 0; i < stepDecoder.Count(); i++)
                {
                    if (0 == stepNumber && stepDecoder.EntryType() == ENTRY_TYPE_SNAPSHOT)
                    {
                        snapshot = new ReplayStep(stepDecoder);
                    }
                    else
                    {
                        termSteps.Add(new ReplayStep(stepDecoder));
                    }

                    stepNumber++;
                    stepDecoder = stepDecoder.Next();
                }

                snapshotStep = snapshot;
            }
Beispiel #2
0
        /// <summary>
        /// Create a recovery plan for the cluster that when the steps are replayed will bring the cluster back to the
        /// latest stable state.
        /// </summary>
        /// <param name="archive"> to lookup recording descriptors. </param>
        /// <returns> a new <seealso cref="RecoveryPlan"/> for the cluster. </returns>
        public virtual RecoveryPlan CreateRecoveryPlan(AeronArchive archive)
        {
            var steps = new List <ReplayStep>();

            var snapshotStep = PlanRecovery(steps, entries, archive);

            long lastLeadershipTermId      = -1;
            long lastLogPosition           = 0;
            long lastTermPositionCommitted = -1;
            long lastTermPositionAppended  = 0;

            if (null != snapshotStep)
            {
                lastLeadershipTermId      = snapshotStep.entry.leadershipTermId;
                lastLogPosition           = snapshotStep.entry.logPosition;
                lastTermPositionCommitted = snapshotStep.entry.termPosition;
                lastTermPositionAppended  = lastTermPositionCommitted;
            }

            int size = steps.Count;

            if (size > 0)
            {
                ReplayStep replayStep = steps[size - 1];
                Entry      entry      = replayStep.entry;

                lastLeadershipTermId      = entry.leadershipTermId;
                lastLogPosition           = entry.logPosition;
                lastTermPositionCommitted = entry.termPosition;
                lastTermPositionAppended  = replayStep.recordingStopPosition;
            }

            return(new RecoveryPlan(lastLeadershipTermId, lastLogPosition, lastTermPositionCommitted, lastTermPositionAppended, snapshotStep, steps));
        }
Beispiel #3
0
 public RecoveryPlan(
     long lastLeadershipTermId,
     long lastLogPosition,
     long lastTermPositionCommitted,
     long lastTermPositionAppended,
     ReplayStep snapshotStep,
     List <ReplayStep> termSteps)
 {
     this.lastLeadershipTermId      = lastLeadershipTermId;
     this.lastLogPosition           = lastLogPosition;
     this.lastTermPositionCommitted = lastTermPositionCommitted;
     this.lastTermPositionAppended  = lastTermPositionAppended;
     this.snapshotStep = snapshotStep;
     this.termSteps    = termSteps;
 }
Beispiel #4
0
        public override void Update(GameTime gameTime)
        {
            if (!pause)
            {
                if (IsFirst)
                {
                    gameCollection = (EntityCollection)rm.ReadInitPos(Game.GetService <GameFieldService>().GameField).Copy(null);
                    stepList       = rm.ReadGame(Game.GetService <GameFieldService>().GameField);
                    gameCollection = (EntityCollection)stepList.Find(x => x.stepNumb == k).collection.Copy(null);
                    queue          = new ActionsQueue(gameCollection, Game.GetService <GameFieldService>().GameField);

                    IsFirst = false;
                }
                else
                {
                    gameCollection.Update(gameTime);
                    if (queue.Size == 0)
                    {
                        if (k <= stepList.Count)
                        {
                            gameCollection.GlobalUpdate();
                            ReplayStep step = stepList.Find(x => x.stepNumb == k);
                            k++;
                            if (step.action != null)
                            {
                                step.action.action.execute(gameCollection.getSubmarines().Find(x => x.Number == step.action.subNumb), queue);
                            }
                        }
                    }
                    else
                    {
                        queue.nextAction(gameTime);
                        if (queue.Size == 0)
                        {
                            Game.GetService <GameFieldService>().GameField.coolNoise();
                            Game.GetService <GameFieldService>().GameField.calculateNoise();
                        }
                    }
                }
            }
            base.Update(gameTime);
        }
Beispiel #5
0
    public ReplayStep nextReplayStepEpisodeOrReopen()
    {
        ReplayStep rStep = readStep(ReplayCommandLine.RCL_Episode);

        if (rStep.command == ReplayCommandLine.RCL_Episode)
        {
            return(rStep);
        }

        while (rStep.command != ReplayCommandLine.RCL_Episode && rStep.command != ReplayCommandLine.RCL_End)
        {
            rStep = readStep(ReplayCommandLine.RCL_Episode, true);
        }

        if (rStep.command == ReplayCommandLine.RCL_End)
        {
            reopen();
            rStep = readStep(ReplayCommandLine.RCL_Episode);
        }

        return(rStep);
    }
Beispiel #6
0
        internal static ReplayStep PlanRecovery(List <ReplayStep> steps, List <Entry> entries, AeronArchive archive)
        {
            if (entries.Count == 0)
            {
                return(null);
            }

            int snapshotIndex = -1;

            for (int i = entries.Count - 1; i >= 0; i--)
            {
                Entry entry = entries[i];
                if (ENTRY_TYPE_SNAPSHOT == entry.type)
                {
                    snapshotIndex = i;
                }
            }

            ReplayStep      snapshotStep;
            RecordingExtent recordingExtent = new RecordingExtent();

            if (-1 != snapshotIndex)
            {
                Entry snapshot = entries[snapshotIndex];
                GetRecordingExtent(archive, recordingExtent, snapshot);

                snapshotStep = new ReplayStep(recordingExtent.startPosition, recordingExtent.stopPosition, snapshot);

                if (snapshotIndex - 1 >= 0)
                {
                    for (int i = snapshotIndex - 1; i >= 0; i--)
                    {
                        Entry entry = entries[i];
                        if (ENTRY_TYPE_TERM == entry.type)
                        {
                            GetRecordingExtent(archive, recordingExtent, snapshot);
                            long snapshotPosition = snapshot.logPosition + snapshot.termPosition;

                            if (recordingExtent.stopPosition == AeronArchive.NULL_POSITION ||
                                (entry.logPosition + recordingExtent.stopPosition) > snapshotPosition)
                            {
                                steps.Add(new ReplayStep(snapshot.termPosition, recordingExtent.stopPosition, entry));
                            }

                            break;
                        }
                    }
                }
            }
            else
            {
                snapshotStep = null;
            }

            for (int i = snapshotIndex + 1, length = entries.Count; i < length; i++)
            {
                Entry entry = entries[i];
                GetRecordingExtent(archive, recordingExtent, entry);

                steps.Add(new ReplayStep(recordingExtent.startPosition, recordingExtent.stopPosition, entry));
            }

            return(snapshotStep);
        }
Beispiel #7
0
    public ReplayStep readStep(ReplayCommandLine requiredCommand, bool force = false)
    {
        if (isSynchronized || force)
        {
            ReplayStep replayStep = new ReplayStep();
            string     line       = sr.ReadLine();

            if (line != null && line != "END")
            {
                if (line.Substring(0, 2).Equals("EP"))
                {
                    replayStep.command = ReplayCommandLine.RCL_Episode;

                    string[] parts = line.Split(':');
                    replayStep.epId = parts[1];

                    //return replayStep;
                }
                else if (line.Substring(0, 2).Equals("NA"))
                {
                    replayStep.command = ReplayCommandLine.RCL_NumberOfAgents;

                    string[] parts = line.Split(':');
                    replayStep.numberOfAgents = Int32.Parse(parts[1]);

                    //return replayStep;
                }
                else if (line.Substring(0, 2).Equals("BL"))
                {
                    replayStep.command = ReplayCommandLine.RCL_Blocks;

                    string[] parts          = line.Split(':');
                    string   positions      = parts[1];
                    string[] blocksPosParts = positions.Split(';');
                    for (int i = 0; i < blocksPosParts.Length; ++i)
                    {
                        string[] blocks = blocksPosParts[i].Split(',');
                        replayStep.blockMap.Add(Int32.Parse(blocks[0]), (Int32.Parse(blocks[1]) == 1 ? true : false));
                    }
                }
                else if (line.Substring(0, 2).Equals("IP"))
                {
                    replayStep.command = ReplayCommandLine.RCL_InitialPositions;

                    string[] parts         = line.Split(':');
                    string   positions     = parts[1];
                    string[] agentPosParts = positions.Split(';');
                    for (int i = 0; i < agentPosParts.Length; ++i)
                    {
                        string[]   agentPos = agentPosParts[i].Split(',');
                        Vector2Int pos      = new Vector2Int(Int32.Parse(agentPos[1]), Int32.Parse(agentPos[2]));
                        replayStep.agentInitPositionMap.Add(agentPos[0], pos);
                    }

                    //return replayStep;
                }
                else if (line.Substring(0, 2).Equals("AC"))
                {
                    replayStep.command = ReplayCommandLine.RCL_Actions;

                    string[] parts            = line.Split(':');
                    string   positions        = parts[1];
                    string[] agentActionParts = positions.Split(';');
                    for (int i = 0; i < agentActionParts.Length; ++i)
                    {
                        string[] agentAction = agentActionParts[i].Split(',');
                        replayStep.agentActionMap.Add(agentAction[0], Int32.Parse(agentAction[1]));
                    }

                    //return replayStep;
                }
                else if (line.Substring(0, 2).Equals("BO"))
                {
                    replayStep.command = ReplayCommandLine.RCL_BombPositions;

                    string[] parts            = line.Split(':');
                    string   positions        = parts[1];
                    string[] agentActionParts = positions.Split(';');

                    replayStep.bombIteration  = Int32.Parse(agentActionParts[0]);
                    replayStep.hasCreatedBomb = Int32.Parse(agentActionParts[1]) == 1 ? true : false;

                    if (replayStep.hasCreatedBomb)
                    {
                        for (int i = 2; i < agentActionParts.Length; ++i)
                        {
                            string[]   agentPos = agentActionParts[i].Split(',');
                            Vector2Int pos      = new Vector2Int(Int32.Parse(agentPos[0]), Int32.Parse(agentPos[1]));
                            replayStep.bombList.Add(pos);
                        }
                    }

                    //return replayStep;
                }
                else
                {
                    replayStep.command = ReplayCommandLine.RCL_Corrupted;
                    //return replayStep;
                }
            }
            else
            {
                replayStep.command = ReplayCommandLine.RCL_End;
                //return replayStep;
            }

            if (requiredCommand == replayStep.command)
            {
                isSynchronized = true;
            }
            else
            {
                isSynchronized = false;
            }

            lastLine = replayStep;
            return(replayStep);
        }
        else
        {
            if (requiredCommand == lastLine.command)
            {
                isSynchronized = true;
            }

            return(lastLine);
        }
    }
 public void addOnlineReplayStep(ReplayStep step)
 {
     this.replaySteps.Add(step);
     Count++;
 }