示例#1
0
        public void Apply(LogEntry entry, Command cmd)
        {
            var betCmd = cmd as BetOnPonyCommand;
            if (betCmd != null)
            {
                var odd = OddsReference.AvailableOdds.FirstOrDefault(o => o.OddId == betCmd.OddId);
                if (odd != null)
                {
                    this.Bets.Add(new Bet() { Odd = odd, UserId = betCmd.UserId, AmountOfMoney = betCmd.AmountOfMoney });
                    cmd.CommandResult = new { Result = "Success" };
                }
                else
                {
                    // TODO: figure out what to do if the odd is invalid
                }
            }

            var getCmd = cmd as GetAllBetsCommand;
            if (getCmd != null)
            {
                cmd.CommandResult = this.Bets;
            }

            this.LastAppliedIndex = cmd.AssignedIndex;
        }
示例#2
0
		public byte[] Serialize(Command cmd)
		{
			using (var memoryStream = new MemoryStream())
			{
				using (var streamWriter = new StreamWriter(memoryStream))
				{
					_serializer.Serialize(streamWriter, cmd);
					streamWriter.Flush();
				}
				return memoryStream.ToArray();
			}
		}
		public void Apply(LogEntry entry, Command cmd)
		{
			if (LastAppliedIndex >= entry.Index)
				throw new InvalidOperationException("Already applied " + entry.Index);
			
			LastAppliedIndex = entry.Index;
			
			var dicCommand = cmd as DictionaryCommand;
			
			if (dicCommand != null) 
				dicCommand.Apply(Data);
		}
示例#4
0
		public void Apply(LogEntry entry, Command cmd)
		{
			var batch = cmd as OperationBatchCommand;
		    if (batch != null)
		    {
		        Apply(batch.Batch, cmd.AssignedIndex);
		        return;
		    }
		    var get = cmd as GetCommand;
		    if (get != null)
		    {
		        cmd.CommandResult = Read(get.Key);
		    }
            // have to apply even get command so we'll keep track of the last applied index
		    Apply(Enumerable.Empty<KeyValueOperation>(), cmd.AssignedIndex);
		}
示例#5
0
        public void AppendCommand(Command command)
        {
            var index = Engine.PersistentState.AppendToLeaderLog(command);
            _matchIndexes[Engine.Name] = index;
            _nextIndexes[Engine.Name] = index + 1;
            _lastContact[Engine.Name] = DateTime.UtcNow;

            if (Engine.CurrentTopology.QuorumSize == 1)
            {
                CommitEntries(null, index);
                command.Complete();

                return;
            }

            if (command.Completion != null)
                _pendingCommands.Enqueue(command);
        }
示例#6
0
		public long AppendToLeaderLog(Command command)
		{
			if (CurrentTerm == 0)
				throw new InvalidOperationException("Cannot append entries in term 0");
			using (var tx = _env.NewTransaction(TransactionFlags.ReadWrite))
			{
				var logs = tx.ReadTree(LogsTreeName);
				var terms = tx.ReadTree(EntryTermsTreeName);

				var nextEntryId = GetLastEntryNumber(logs, tx) + 1;
				var key = new Slice(EndianBitConverter.Big.GetBytes(nextEntryId));

				command.AssignedIndex = nextEntryId;
				var commandEntry = CommandSerializer.Serialize(command);
				logs.Add(key, commandEntry);
				terms.Add(key, BitConverter.GetBytes(CurrentTerm));

				var topologyChangeCommand = command as TopologyChangeCommand;
				if (topologyChangeCommand != null)
				{
					SetCurrentTopologyInternal(topologyChangeCommand.Requested, nextEntryId, tx);
				}

				tx.Commit();

				return nextEntryId;
			}
		}
示例#7
0
		protected virtual void OnCommitApplied(Command cmd)
		{
			var handler = CommitApplied;
			if (handler != null)
			{
				try
				{
					handler(cmd);
				}
				catch (Exception e)
				{
					_log.Error("Error on raising CommitApplied event", e);
				}
			}
		}
示例#8
0
		public void AppendCommand(Command command)
		{
			if (command == null) throw new ArgumentNullException("command");

			var leaderStateBehavior = StateBehavior as LeaderStateBehavior;
			if (leaderStateBehavior == null || leaderStateBehavior.State != RaftEngineState.Leader)
				throw new NotLeadingException("Command can be appended only on leader node. This node behavior type is " +
													StateBehavior.GetType().Name)
				{
					CurrentLeader = CurrentLeader
				};


			leaderStateBehavior.AppendCommand(command);
		}