Exemplo n.º 1
0
 public TcpMessageSender(AbstractClientConfig clientConfig, IMessageStatistics statistics, MessageIdFactory messageIdFactory)
 {
     _mClientConfig    = clientConfig;
     _mStatistics      = statistics;
     _mActive          = true;
     _mQueue           = new BlockingThreadSafeQueue <IMessageTree>();
     _mAtomicTrees     = new BlockingThreadSafeQueue <IMessageTree>(MAX_ATOMIC_MESSAGES);
     _mCodec           = new PlainTextMessageCodec();
     _messageIdFactory = messageIdFactory;
 }
Exemplo n.º 2
0
 public TcpMessageSender(AbstractClientConfig clientConfig, IMessageStatistics statistics, MessageIdFactory messageIdFactory)
 {
     _mClientConfig = clientConfig;
     _mStatistics = statistics;
     _mActive = true;
     _mQueue = new BlockingThreadSafeQueue<IMessageTree>();
     _mAtomicTrees = new BlockingThreadSafeQueue<IMessageTree>(MAX_ATOMIC_MESSAGES);
     _mCodec = new PlainTextMessageCodec();
     _messageIdFactory = messageIdFactory;
 }
Exemplo n.º 3
0
        private bool ShouldMerge(BlockingThreadSafeQueue <IMessageTree> trees)
        {
            IMessageTree tree;

            trees.TryPeek(out tree, true);

            if (tree != null)
            {
                long firstTime = tree.Message.Timestamp;

                // 30 sec
                const int maxDuration = 1000 * 30;

                if (MilliSecondTimer.CurrentTimeMicros() / 1000 - firstTime > maxDuration ||
                    trees.Count >= MAX_ATOMIC_MESSAGES ||
                    trees.Count >= _mClientConfig.MaxQueueSize ||
                    trees.EstimatedByteSize >= _mClientConfig.MaxQueueByteSize)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        private IMessageTree MergeTree(BlockingThreadSafeQueue <IMessageTree> trees)
        {
            int max = MAX_ATOMIC_MESSAGES - 1;
            DefaultTransaction tran = new DefaultTransaction("_CatMergeTree", "_CatMergeTree", null);

            tran.Status = CatConstants.SUCCESS;
            tran.SetCompleted(true);

            IMessageTree first;

            trees.TryPeek(out first, false);

            // Usually this should not happen, because it is in the same thread with ShouldMerge()
            if (first == null)
            {
                return(null);
            }

            // Set merge tree start time to the first massage's timestamp
            tran.Timestamp = first.Message.Timestamp;

            long lastTimestamp     = 0;
            long lastDuration      = 0;
            int  estimatedByteSize = 0;

            while (max >= 0)
            {
                IMessageTree tree;
                trees.TryDequeue(out tree, false);

                if (tree != null)
                {
                    tran.AddChild(tree.Message);
                    estimatedByteSize += tree.EstimatedByteSize;

                    if (first != tree)
                    {
                        _messageIdFactory.Reuse(tree.MessageId);
                    }

                    lastTimestamp = tree.Message.Timestamp;
                    if (tree.Message is DefaultTransaction)
                    {
                        lastDuration = ((DefaultTransaction)tree.Message).DurationInMillis;
                    }
                    else
                    {
                        lastDuration = 0;
                    }
                }

                if (tree == null || max == 0)
                {
                    // Set merge tree end time to the last massage's end time
                    tran.DurationInMillis = (lastTimestamp - tran.Timestamp + lastDuration);
                    break;
                }
                max--;
            }
            ((DefaultMessageTree)first).Message = tran;
            estimatedByteSize      += tran.EstimateByteSize();
            first.EstimatedByteSize = estimatedByteSize;
            return(first);
        }
Exemplo n.º 5
0
        private IMessageTree MergeTree(BlockingThreadSafeQueue<IMessageTree> trees)
        {
		    int max = MAX_ATOMIC_MESSAGES - 1;
		    DefaultTransaction tran = new DefaultTransaction("_CatMergeTree", "_CatMergeTree", null);
		    tran.Status = CatConstants.SUCCESS;
		    tran.SetCompleted(true);
            
            IMessageTree first;
            trees.TryPeek(out first, false);

            // Usually this should not happen, because it is in the same thread with ShouldMerge()
            if (first == null)
            {
                return null;
            }

            // Set merge tree start time to the first massage's timestamp
            tran.Timestamp = first.Message.Timestamp;

            long lastTimestamp = 0;
            long lastDuration = 0;
            int estimatedByteSize = 0;
		    while (max >= 0) {
			    IMessageTree tree;
                trees.TryDequeue(out tree, false);

                if (tree != null)
                {
                    tran.AddChild(tree.Message);
                    estimatedByteSize += tree.EstimatedByteSize;

                    if (first!=tree)
                    {
                        _messageIdFactory.Reuse(tree.MessageId);
                    }

                    lastTimestamp = tree.Message.Timestamp;
                    if (tree.Message is DefaultTransaction)
                    {
                        lastDuration = ((DefaultTransaction)tree.Message).DurationInMillis;
                    }
                    else
                    {
                        lastDuration = 0;
                    }
                }

			    if (tree == null || max == 0) {
                    // Set merge tree end time to the last massage's end time
                    tran.DurationInMillis = (lastTimestamp - tran.Timestamp + lastDuration);
                    break;
			    }
			    max--;
		    }
		    ((DefaultMessageTree) first).Message = tran;
            estimatedByteSize += tran.EstimateByteSize();
            first.EstimatedByteSize = estimatedByteSize;
		    return first;
	    }
Exemplo n.º 6
0
        private bool ShouldMerge(BlockingThreadSafeQueue<IMessageTree> trees)
        {
            IMessageTree tree;
            trees.TryPeek(out tree, true);

            if (tree != null)
            {
                long firstTime = tree.Message.Timestamp;

                // 30 sec
                const int maxDuration = 1000 * 30;

                if (MilliSecondTimer.UnixNowMilliSeconds() - firstTime > maxDuration 
                    || trees.Count >= MAX_ATOMIC_MESSAGES 
                    || trees.Count >= _mClientConfig.MaxQueueSize
                    || trees.EstimatedByteSize >= _mClientConfig.MaxQueueByteSize)
                {
                    return true;
                }
            }
            return false;
        }