Esempio n. 1
0
        // Placed object message payload into a segment from a buffer pool.  When this get's too big, older blocks will be purged
        private ArraySegment <byte> SerializeMessageIntoPooledSegment(MemoryMessageData queueMessage)
        {
            // serialize payload
            int size = SegmentBuilder.CalculateAppendSize(queueMessage.Payload);

            // get segment from current block
            ArraySegment <byte> segment;

            if (currentBuffer == null || !currentBuffer.TryGetSegment(size, out segment))
            {
                // no block or block full, get new block and try again
                currentBuffer = bufferPool.Allocate();
                //call EvictionStrategy's OnBlockAllocated method
                this.evictionStrategy.OnBlockAllocated(currentBuffer);
                // if this fails with clean block, then requested size is too big
                if (!currentBuffer.TryGetSegment(size, out segment))
                {
                    string errmsg = String.Format(CultureInfo.InvariantCulture,
                                                  "Message size is too big. MessageSize: {0}", size);
                    throw new ArgumentOutOfRangeException(nameof(queueMessage), errmsg);
                }
            }
            // encode namespace, offset, partitionkey, properties and payload into segment
            int writeOffset = 0;

            SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Payload);
            return(segment);
        }
Esempio n. 2
0
        public IBatchContainer GetBatchContainer(ref CachedMessage cachedMessage)
        {
            //Deserialize payload
            int readOffset = 0;
            ArraySegment <byte> payload = SegmentBuilder.ReadNextBytes(cachedMessage.Segment, ref readOffset);
            MemoryMessageData   message = MemoryMessageData.Create(cachedMessage.StreamGuid, cachedMessage.StreamNamespace, new ArraySegment <byte>(payload.ToArray()));

            return(new MemoryBatchContainer <TSerializer>(message, this.serializer));
        }
Esempio n. 3
0
 /// <summary>
 /// Enqueues an event data. If the current total count reaches the max limit. throws an exception.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public Task Enqueue(MemoryMessageData data)
 {
     if (eventQueue.Count >= maxEventCount)
     {
         throw new InvalidOperationException($"Can not enqueue since the count has reached its maximum of {maxEventCount}");
     }
     data.SequenceNumber = sequenceNumber++;
     eventQueue.Enqueue(data);
     return(Task.CompletedTask);
 }
 /// <summary>
 /// Enqueues an event data. If the current total count reaches the max limit. throws an exception.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public Task Enqueue(MemoryMessageData data)
 {
     if (eventQueue.Count >= maxEventCount)
     {
         throw new InvalidOperationException($"Can not enqueue since the count has reached its maximum of {maxEventCount}");
     }
     data.SequenceNumber = sequenceNumber++;
     eventQueue.Enqueue(data);
     return TaskDone.Done;
 }
Esempio n. 5
0
        private CachedMessage QueueMessageToCachedMessage(MemoryMessageData queueMessage, DateTime dequeueTimeUtc)
        {
            StreamPosition streamPosition = GetStreamPosition(queueMessage);

            return(new CachedMessage()
            {
                StreamId = streamPosition.StreamId,
                SequenceNumber = queueMessage.SequenceNumber,
                EnqueueTimeUtc = queueMessage.EnqueueTimeUtc,
                DequeueTimeUtc = dequeueTimeUtc,
                Segment = SerializeMessageIntoPooledSegment(queueMessage)
            });
        }
Esempio n. 6
0
        private CachedMessage QueueMessageToCachedMessage(MemoryMessageData queueMessage, DateTime dequeueTimeUtc)
        {
            StreamPosition streamPosition = GetStreamPosition(queueMessage);

            return(new CachedMessage()
            {
                StreamGuid = streamPosition.StreamIdentity.Guid,
                StreamNamespace = streamPosition.StreamIdentity.Namespace != null?string.Intern(streamPosition.StreamIdentity.Namespace) : null,
                                      SequenceNumber = queueMessage.SequenceNumber,
                                      EnqueueTimeUtc = queueMessage.EnqueueTimeUtc,
                                      DequeueTimeUtc = dequeueTimeUtc,
                                      Segment = SerializeMessageIntoPooledSegment(queueMessage)
            });
        }
 /// <summary>
 /// Writes a set of events to the queue as a single batch associated with the provided streamId.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="streamGuid"></param>
 /// <param name="streamNamespace"></param>
 /// <param name="events"></param>
 /// <param name="token"></param>
 /// <param name="requestContext"></param>
 /// <returns></returns>
 public async Task QueueMessageBatchAsync <T>(Guid streamGuid, string streamNamespace, IEnumerable <T> events, StreamSequenceToken token, Dictionary <string, object> requestContext)
 {
     try
     {
         var queueId = streamQueueMapper.GetQueueForStream(streamGuid, streamNamespace);
         ArraySegment <byte> bodyBytes = serializer.Serialize(new MemoryMessageBody(events.Cast <object>(), requestContext));
         var messageData = MemoryMessageData.Create(streamGuid, streamNamespace, bodyBytes);
         IMemoryStreamQueueGrain queueGrain = GetQueueGrain(queueId);
         await queueGrain.Enqueue(messageData);
     }
     catch (Exception exc)
     {
         logger.Error((int)ProviderErrorCode.MemoryStreamProviderBase_QueueMessageBatchAsync, "Exception thrown in MemoryAdapterFactory.QueueMessageBatchAsync.", exc);
         throw;
     }
 }
Esempio n. 8
0
 private StreamPosition GetStreamPosition(MemoryMessageData queueMessage)
 {
     return(new StreamPosition(queueMessage.StreamId,
                               new EventSequenceTokenV2(queueMessage.SequenceNumber)));
 }
Esempio n. 9
0
 private StreamPosition GetStreamPosition(MemoryMessageData queueMessage)
 {
     return(new StreamPosition(new StreamIdentity(queueMessage.StreamGuid, queueMessage.StreamNamespace),
                               new EventSequenceTokenV2(queueMessage.SequenceNumber)));
 }
Esempio n. 10
0
 public MemoryBatchContainer(MemoryMessageData messageData, TSerializer serializer)
 {
     this.serializer = serializer;
     MessageData     = messageData;
     realToken       = new EventSequenceToken(messageData.SequenceNumber);
 }
Esempio n. 11
0
 public MemoryBatchContainer(MemoryMessageData messageData)
 {
     serializer  = new TSerializer();
     MessageData = messageData;
     realToken   = new EventSequenceToken(messageData.SequenceNumber);
 }