Exemplo n.º 1
0
        public void Send(IMessageTree tree)
        {
            lock (_mQueue)
            {
                if (_mQueue.Count < _maxQueueSize)
                {
                    _mQueue.Enqueue(tree);
                }
                else
                {
                    // throw it away since the queue is full
                    _mErrors++;

                    if (_mStatistics != null)
                    {
                        _mStatistics.OnOverflowed(tree);
                    }

                    if (_mErrors % 100 == 0)
                    {
                        PureCat.Util.Logger.Warn("Can't send message to cat-server due to queue's full! Count: " + _mErrors);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void AsynchronousSendTask(object o)
        {
            IMessageTree tree = null;

            while (_mActive)
            {
                while (_mQueue.Count == 0 || _mActiveConnection == null || !_mActiveConnection.Connected)
                {
                    if (_mActiveConnection != null && !_mActiveConnection.Connected)
                    {
                        LoggerManager.Warn("AsynchronousSendTask中,Socket关闭");
                    }
                    Thread.Sleep(5);
                }
                lock (_mQueue)
                {
                    tree = _mQueue.Dequeue();
                }
                try
                {
                    SendInternal(tree);
                }
                catch (Exception t)
                {
                    LoggerManager.Error("Error when sending message over TCP socket! Error: {0}", t);
                }
            }
        }
Exemplo n.º 3
0
        public void Send(IMessageTree tree)
        {
            lock (_queue)
            {
                if (_queue.Count < _maxQueueSize)
                {
                    _queue.Enqueue(tree);
                }
                else
                {
                    // throw it away since the queue is full
                    Interlocked.Increment(ref _errors);

                    if (_statistics != null)
                    {
                        _statistics.OnOverflowed(tree);
                    }

                    if (Interlocked.Read(ref _errors) % 100 == 0)
                    {
                        Logger.Warn("Can't send message to cat-server due to queue's full! Count: " + Interlocked.Read(ref _errors));
                    }
                }
            }
        }
Exemplo n.º 4
0
 public void Send(IMessageTree tree)
 {
     if (tree == null)
         return;
     if (IsAtomicMessage(tree))
     {
         if (_atomicTress.Count < _maxQueueSize)
         {
             _atomicTress.Enqueue(tree);
         }
         else
         {
             LogQueueFullInfo("AtomicMessage");
         }
     }
     else
     {
         if (_queue.Count < _maxQueueSize)
         {
             _queue.Enqueue(tree);
         }
         else
         {
             LogQueueFullInfo("Message");
         }
     }
 }
Exemplo n.º 5
0
        // added by dingning
        public static void LogRemoteCallClient(Context context)
        {
            IMessageTree tree      = GetManager().ThreadLocalMessageTree;
            string       messageId = tree.MessageId;

            if (messageId == null)
            {
                messageId      = CreateMessageId();
                tree.MessageId = messageId;
            }

            string childId = CreateMessageId();

            LogEvent(CatConstants.TYPE_REMOTE_CALL, "", CatConstants.SUCCESS, childId);

            string root = tree.RootMessageId;

            if (root == null)
            {
                root = messageId;
            }

            context.AddProperty(Context.ROOT, root);
            context.AddProperty(Context.PARENT, messageId);
            context.AddProperty(Context.CHILD, childId);
        }
Exemplo n.º 6
0
            ///<summary>
            ///  return true means the transaction has been flushed.
            ///</summary>
            ///<param name="manager"> </param>
            ///<param name="transaction"> </param>
            ///<returns> true if message is flushed, false otherwise </returns>
            public bool End(DefaultMessageManager manager, ITransaction transaction)
            {
                if (_mStack.Count != 0)
                {
                    ITransaction current = _mStack.Pop();

                    if (transaction == current)
                    {
                        ValidateTransaction(manager, _mStack.Count == 0 ? null : _mStack.Peek(), current);
                    }
                    else
                    {
                        while (transaction != current && _mStack.Count != 0)
                        {
                            ValidateTransaction(manager, _mStack.Peek(), current);
                            current = _mStack.Pop();
                        }
                    }


                    if (_mStack.Count == 0)
                    {
                        IMessageTree tree = _mTree.Copy();
                        _mTree.MessageId = null;
                        _mTree.Message   = null;

                        manager.Flush(tree);
                        return(true);
                    }
                }
                return(false);
            }
Exemplo n.º 7
0
        public IForkedTransaction NewForkedTransaction(string type, string name)
        {
            // this enable CAT client logging cat message without explicit setup
            if (!_mManager.HasContext())
            {
                _mManager.Setup();
            }

            if (_mManager.CatEnabled)
            {
                IMessageTree tree = _mManager.ThreadLocalMessageTree;

                if (tree.MessageId == null)
                {
                    tree.MessageId = CreateMessageId();
                }

                IForkedTransaction transaction = new DefaultForkedTransaction(type, name, _mManager);

                if (_mManager is DefaultMessageManager)
                {
                    ((DefaultMessageManager)_mManager).LinkAsRunAway(transaction);
                }
                _mManager.Start(transaction, true);
                return(transaction);
            }
            else
            {
                return(NullMessage.FORKEDTRANSACTION);
            }
        }
Exemplo n.º 8
0
        public IList <User> FindAll()
        {
            HeaderContext headercontext = HeaderOperater.GetServiceWcfHeader(OperationContext.Current);
            ITransaction  t             = Cat.GetProducer().NewTransaction("Data", "call wcf");
            IMessageTree  tree          = Cat.GetManager().ThreadLocalMessageTree;

            tree.RootMessageId   = headercontext.RootID;
            tree.ParentMessageId = headercontext.CorrelationState;
            tree.MessageId       = headercontext.ParentID;

            List <User> list = new List <User>();

            for (int i = 0; i < 30; i++)
            {
                User obj = new User();
                obj.UserID   = i + 1;
                obj.UserName = "******" + (i + 1);

                list.Add(obj);
            }

            Cat.GetProducer().LogEvent("Data In.Server", "FindAll", "0", "Success");
            t.Status = "B";
            t.Complete();

            return(list);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 消息树构建 由插件调用 应用不需要关心这个方法
        /// </summary>
        /// <returns></returns>
        public static RemotionCallContext logRemoteCallClient()
        {
            IMessageTree tree      = Jasmine.GetManager().GetMessageTree(CallContextManager.MessageTreeId);
            String       messageId = tree.MessageId;

            if (messageId == null)
            {
                messageId      = Jasmine.GetProducer().CreateMessageId();
                tree.MessageId = messageId;
            }

            String childId = Jasmine.GetProducer().CreateMessageId();

            Jasmine.Event(JasmineConstants.TYPE_REMOTE_CALL, "", "0", childId);

            String root = tree.RootMessageId;

            if (root == null)
            {
                root = messageId;
            }

            return(new RemotionCallContext()
            {
                ChildMessageId = childId,
                ParentMessageId = messageId,
                RootMessageId = root
            });

            //ctx.AddProperty(CatContext.ROOT, root);
            //ctx.AddProperty(CatContext.PARENT, messageId);
            //ctx.AddProperty(CatContext.CHILD, childId);
        }
Exemplo n.º 10
0
        protected internal void DecodeHeader(ChannelBuffer buf, IMessageTree tree)
        {
            BufferHelper helper          = _mBufferHelper;
            String       id              = helper.Read(buf, TAB);
            String       domain          = helper.Read(buf, TAB);
            String       hostName        = helper.Read(buf, TAB);
            String       ipAddress       = helper.Read(buf, TAB);
            String       threadGroupName = helper.Read(buf, TAB);
            String       threadId        = helper.Read(buf, TAB);
            String       threadName      = helper.Read(buf, TAB);
            String       messageId       = helper.Read(buf, TAB);
            String       parentMessageId = helper.Read(buf, TAB);
            String       rootMessageId   = helper.Read(buf, TAB);
            String       sessionToken    = helper.Read(buf, LF);

            if (ID.Equals(id))
            {
                tree.Domain          = domain;
                tree.HostName        = hostName;
                tree.IpAddress       = ipAddress;
                tree.ThreadGroupName = threadGroupName;
                tree.ThreadId        = threadId;
                tree.ThreadName      = threadName;
                tree.MessageId       = messageId;
                tree.ParentMessageId = parentMessageId;
                tree.RootMessageId   = rootMessageId;
                tree.SessionToken    = sessionToken;
            }
            else
            {
                throw new Exception("Unrecognized id(" + id + ") for plain text message codec!");
            }
        }
Exemplo n.º 11
0
        protected internal void DecodeHeader(ChannelBuffer buf, IMessageTree tree)
        {
            BufferHelper helper = _mBufferHelper;
            String id = helper.Read(buf, TAB);
            String domain = helper.Read(buf, TAB);
            String hostName = helper.Read(buf, TAB);
            String ipAddress = helper.Read(buf, TAB);
            String threadGroupName = helper.Read(buf, TAB);
            String threadId = helper.Read(buf, TAB);
            String threadName = helper.Read(buf, TAB);
            String messageId = helper.Read(buf, TAB);
            String parentMessageId = helper.Read(buf, TAB);
            String rootMessageId = helper.Read(buf, TAB);
            String sessionToken = helper.Read(buf, LF);

            if (ID.Equals(id))
            {
                tree.Domain = domain;
                tree.HostName = hostName;
                tree.IpAddress = ipAddress;
                tree.ThreadGroupName = threadGroupName;
                tree.ThreadId = threadId;
                tree.ThreadName = threadName;
                tree.MessageId = messageId;
                tree.ParentMessageId = parentMessageId;
                tree.RootMessageId = rootMessageId;
                tree.SessionToken = sessionToken;
            }
            else
            {
                throw new Exception("Unrecognized id(" + id + ") for plain text message codec!");
            }
        }
Exemplo n.º 12
0
 public void Send(IMessageTree tree)
 {
     if (tree == null)
     {
         return;
     }
     if (IsAtomicMessage(tree))
     {
         if (_atomicTress.Count < _maxQueueSize)
         {
             _atomicTress.Enqueue(tree);
         }
         else
         {
             LogQueueFullInfo("AtomicMessage");
         }
     }
     else
     {
         if (_queue.Count < _maxQueueSize)
         {
             _queue.Enqueue(tree);
         }
         else
         {
             LogQueueFullInfo("Message");
         }
     }
 }
Exemplo n.º 13
0
            ///<summary>
            ///  return true means the transaction has been flushed.
            ///</summary>
            ///<param name="manager"> </param>
            ///<param name="transaction"> </param>
            ///<returns> true if message is flushed, false otherwise </returns>
            public bool End(DefaultMessageManager manager, ITransaction transaction)
            {
                if (_mStack.Count != 0)
                {
                    ITransaction current = _mStack.Pop();
                    while (transaction != current && _mStack.Count != 0)
                    {
                        current = _mStack.Pop();
                    }
                    if (transaction != current)
                        throw new Exception("没找到对应的Transaction.");

                    if (_mStack.Count == 0)
                    {
                        ValidateTransaction(current);

                        IMessageTree tree = _mTree.Copy();
                        _mTree.MessageId = null;
                        _mTree.Message = null;
                        manager.Flush(tree);
                        return true;
                    }
                    return false;
                }

                throw new Exception("Stack为空, 没找到对应的Transaction.");
            }
Exemplo n.º 14
0
 public void Send(IMessageTree tree)
 {
     if (isAtomicMessage(tree))
     {
         if (_mAtomicTrees.Count >= _mClientConfig.MaxQueueSize)
         {
             LogQueueOverflow(tree);
         }
         else if (_mAtomicTrees.EstimatedByteSize >= _mClientConfig.MaxQueueByteSize)
         {
             LogQueueBytesOverflow(tree);
         }
         else
         {
             // if (_mAtomicTrees.Count < _mClientConfig.MaxQueueSize && _mAtomicTrees.EstimatedByteSize < _mClientConfig.MaxQueueByteSize)
             _mAtomicTrees.Enqueue(tree);
         }
     }
     else
     {
         if (_mQueue.Count >= _mClientConfig.MaxQueueSize)
         {
             LogQueueOverflow(tree);
         }
         else if (_mQueue.EstimatedByteSize >= _mClientConfig.MaxQueueByteSize)
         {
             LogQueueBytesOverflow(tree);
         }
         else
         {
             // if (_mQueue.Count < _mClientConfig.MaxQueueSize && _mQueue.EstimatedByteSize < _mClientConfig.MaxQueueByteSize)
             _mQueue.Enqueue(tree);
         }
     }
 }
Exemplo n.º 15
0
        protected internal int EncodeHeader(IMessageTree tree, ChannelBuffer buf)
        {
            BufferHelper helper = _mBufferHelper;
            int          count  = 0;

            count += helper.Write(buf, ID);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.Domain);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.HostName);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.IpAddress);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ThreadGroupName);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ThreadId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ThreadName);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.MessageId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ParentMessageId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.RootMessageId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.SessionToken);
            count += helper.Write(buf, LF);

            return(count);
        }
Exemplo n.º 16
0
        private void SendInternal(IMessageTree tree)
        {
            if (_mActiveChannel != null && _mActiveChannel.Connected)
            {
                // Re-use the existing buffer.
                buf.Reset();

                _mCodec.Encode(tree, buf);

                // Nullify reference as early as possible
                tree.Message = null;

                byte[] data = buf.ToArray();

                _mActiveChannel.Client.Send(data);

                if (_mStatistics != null)
                {
                    _mStatistics.OnBytes(data.Length);
                }
            }
            else
            {
                tree.Message = null;
                Logger.Warn("SendInternal中,Socket关闭");
            }
        }
Exemplo n.º 17
0
        public void Send(IMessageTree tree)
        {
            lock (_mQueue)
            {
                if (_mQueue.Count < 100000)
                {
                    _mQueue.Add(tree);
                }
                else
                {
                    // throw it away since the queue is full
                    _mErrors ++;

                    if (_mStatistics != null)
                    {
                        _mStatistics.OnOverflowed(tree);
                    }

                    if (_mErrors%100 == 0)
                    {
                        Logger.Warn("Can't send message to cat-server due to queue's full! Count: " + _mErrors);
                    }
                }
            }
        }
Exemplo n.º 18
0
        public ITaggedTransaction NewTaggedTransaction(string type, string name, string tag)
        {
            // this enable CAT client logging cat message without explicit setup
            if (!_mManager.HasContext())
            {
                _mManager.Setup();
            }

            if (_mManager.CatEnabled)
            {
                IMessageTree tree = _mManager.ThreadLocalMessageTree;

                if (tree.MessageId == null)
                {
                    tree.MessageId = CreateMessageId();
                }

                ITaggedTransaction transaction = new DefaultTaggedTransaction(type, name, tag, _mManager);

                _mManager.Start(transaction, true);
                return(transaction);
            }
            else
            {
                return(NullMessage.TAGGEDTRANSACTION);
            }
        }
Exemplo n.º 19
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string filename = Server.MapPath("~/config/catclient.xml");
                Cat.Initialize(filename);
                ITransaction t    = Cat.GetProducer().NewTransaction("URL", "demo.aspx");
                IMessageTree tree = Cat.GetManager().ThreadLocalMessageTree;

                //创建传递的上下文信息
                HeaderContext context = new HeaderContext();
                context.AppName          = "";
                context.CorrelationState = tree.MessageId;
                context.RootID           = tree.RootMessageId == null ? tree.MessageId : tree.RootMessageId;
                context.ParentID         = Cat.GetManager().CreateMessageId();
                context.Ip = "";
                HeaderOperater.SetClientWcfHeader(context);
                Cat.GetProducer().LogEvent("URL", "Call", "0", "Call Start...");
                Cat.GetProducer().LogEvent("RemoteCall", "PigeonRequest", "0", context.ParentID);

                IUserBll     bll  = WcfClient.GetProxy <IUserBll>();
                IList <User> list = bll.FindAll();

                Cat.GetProducer().LogEvent("URL", "Call", "0", "Call End...");

                //注:上下文信息必须在创建后清除
                HeaderOperater.ClearClientWcfHeader();
                t.Status = "A";
                t.Complete();

                this.GridView1.DataSource = list;
                this.GridView1.DataBind();
            }
        }
Exemplo n.º 20
0
        public virtual void Decode(ChannelBuffer buf, IMessageTree tree)
        {
            DecodeHeader(buf, tree);

            if (buf.ReadableBytes() > 0)
            {
                DecodeMessage(buf, tree);
            }
        }
Exemplo n.º 21
0
        public virtual void Decode(ChannelBuffer buf, IMessageTree tree)
        {
            DecodeHeader(buf, tree);

            if (buf.ReadableBytes() > 0)
            {
                DecodeMessage(buf, tree);
            }
        }
Exemplo n.º 22
0
        public void Start()
        {
            IMessageTree tree = MessageManager.ThreadLocalMessageTree;

            if (tree != null && tree.RootMessageId == null)
            {
                tree.ParentMessageId = _parentMessageId;
                tree.RootMessageId   = _rootMessageId;
            }
        }
Exemplo n.º 23
0
        public virtual void Decode(ChannelBuffer buf, IMessageTree tree)
        {
            // buf.ReadInt();// read body length

            DecodeHeader(buf, tree);

            if (buf.ReadableBytes() > 0)
            {
                DecodeMessage(buf, tree);
            }
        }
Exemplo n.º 24
0
        private bool IsAtomicMessage(IMessageTree tree)
        {
            var message = tree.Message;

            if (message is ITransaction)
            {
                var type = message.Type;
                return(type.StartsWith("Cache.") || type.StartsWith("SQL"));
            }
            return(true);
        }
Exemplo n.º 25
0
        public virtual void Decode(ChannelBuffer buf, IMessageTree tree)
        {

            // buf.ReadInt();// read body length

            DecodeHeader(buf, tree);

            if (buf.ReadableBytes() > 0)
            {
                DecodeMessage(buf, tree);
            }
        }
Exemplo n.º 26
0
        internal void Flush(IMessageTree tree)
        {
            if (_mSender != null)
            {
                _mSender.Send(tree);

                if (_mStatistics != null)
                {
                    _mStatistics.OnSending(tree);
                }
            }
        }
Exemplo n.º 27
0
        public DefaultTaggedTransaction(string type, string name, string tag, IMessageManager manager)
            : base(type, name, manager)
        {
            _mTag      = tag;
            Standalone = false;
            IMessageTree tree = manager.ThreadLocalMessageTree;

            if (tree != null)
            {
                _mRootMessageId   = tree.RootMessageId;
                _mParentMessageId = tree.MessageId;
            }
        }
Exemplo n.º 28
0
            public void TruncateAndFlush(Context ctx, long timestamp)
            {
                IMessageTree tree = ctx.Tree;
                ThreadSafeStack <ITransaction> stack = ctx.Stack;
                IMessage message = tree.Message;

                if (message is DefaultTransaction)
                {
                    string             id      = tree.MessageId;
                    string             rootId  = tree.RootMessageId;
                    string             childId = _mManager.NextMessageId();
                    DefaultTransaction source  = (DefaultTransaction)message;
                    DefaultTransaction target  = new DefaultTransaction(source.Type, source.Name, _mManager);

                    target.Timestamp        = source.Timestamp;
                    target.DurationInMicros = source.DurationInMicros;
                    target.AddData(source.Data);
                    target.Status = CatConstants.SUCCESS;

                    MigrateMessage(stack, source, target, 1);

                    int reducedByteSize = 0;
                    foreach (ITransaction transaction in stack)
                    {
                        DefaultTransaction tran = (DefaultTransaction)transaction;
                        tran.Timestamp   = timestamp;
                        reducedByteSize += transaction.EstimateByteSize();
                    }

                    DefaultEvent next = new DefaultEvent("RemoteCall", "Next");
                    next.AddData(childId);
                    next.Status = CatConstants.SUCCESS;
                    target.AddChild(next);

                    IMessageTree t = tree.Copy();

                    t.Message = target;

                    ctx.Tree.MessageId       = childId;
                    ctx.Tree.ParentMessageId = id;

                    ctx.Tree.RootMessageId = (rootId != null ? rootId : id);

                    ctx._mLength = stack.Count;
                    // Update estimated byte size of the truncated tree to be the total size of all on-stack transactions.
                    ctx.Tree.EstimatedByteSize = reducedByteSize;

                    ctx._mTotalDurationInMicros = ctx._mTotalDurationInMicros + target.DurationInMicros;
                    _mManager.Flush(t);
                }
            }
Exemplo n.º 29
0
        public virtual void Encode(IMessageTree tree, ChannelBuffer buf)
        {
            int count = 0;

            buf.WriteInt(0); // place-holder
            count += EncodeHeader(tree, buf);

            if (tree.Message != null)
            {
                count += EncodeMessage(tree.Message, buf);
            }

            buf.SetInt(0, count);
        }
Exemplo n.º 30
0
        public void AsynchronousSendTask(object state)
        {
            var i = (int)state;

            while (true)
            {
                if (_active)
                {
                    try
                    {
                        TcpClient activeChannel = null;
                        var       connPoolList  = _connPool.ToList();

                        if (connPoolList.Count != 0)
                        {
                            Interlocked.Exchange(ref activeChannel, connPoolList[i % connPoolList.Count].Value);
                        }
                        else
                        {
                            Thread.Sleep(100);
                            continue;
                        }
                        while (_queue.Count == 0 || activeChannel == null || !activeChannel.Connected)
                        {
                            Thread.Sleep(500);
                            Interlocked.Exchange(ref activeChannel, connPoolList[i % connPoolList.Count].Value);
                        }

                        IMessageTree tree = null;

                        if (_queue.TryDequeue(out tree))
                        {
                            if (tree != null)
                            {
                                SendInternal(tree, activeChannel);
                                tree.Message = null;
                            }
                        }
                    }
                    catch (Exception t)
                    {
                        Logger.Error("Error when sending message over TCP socket! Error: {0}", t);
                    }
                }
                else
                {
                    Thread.Sleep(5 * 1000);
                }
            }
        }
Exemplo n.º 31
0
        public virtual void Encode(IMessageTree tree, ChannelBuffer buf)
        {
            int count = 0;

            buf.WriteInt(0); // place-holder
            count += EncodeHeader(tree, buf);

            if (tree.Message != null)
            {
                count += EncodeMessage(tree.Message, buf);
            }

            buf.SetInt(0, count);
        }
Exemplo n.º 32
0
        public void Bind(string tag, string title)
        {
            ITaggedTransaction t = _mTaggedTransactions.Get(tag);

            if (t != null)
            {
                IMessageTree tree = ThreadLocalMessageTree;
                if (null != tree)
                {
                    t.Start();
                    t.Bind(tag, tree.MessageId, title);
                }
            }
        }
Exemplo n.º 33
0
            private void TruncateAndFlush(DefaultMessageManager manager, long timestamp)
            {
                IMessageTree         tree    = _mTree;
                Stack <ITransaction> stack   = _mStack;
                IMessage             message = tree.Message;

                if (message is DefaultTransaction)
                {
                    if (tree.MessageId == null)
                    {
                        tree.MessageId = manager.NextMessageId();
                    }

                    string rootId  = tree.RootMessageId;
                    string childId = manager.NextMessageId();

                    DefaultTransaction source = message as DefaultTransaction;
                    DefaultTransaction target = new DefaultTransaction(source.Type, source.Name, manager);
                    target.Timestamp        = source.Timestamp;
                    target.DurationInMicros = source.DurationInMicros;
                    target.AddData(source.Data);
                    target.Status = PureCatConstants.SUCCESS;

                    MigrateMessage(manager, stack, source, target, 1);

                    var list = stack.ToList();

                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        DefaultTransaction tran = list[i] as DefaultTransaction;
                        tran.Timestamp        = timestamp;
                        tran.DurationInMicros = -1;
                    }

                    IEvent next = new DefaultEvent(PureCatConstants.TYPE_REMOTE_CALL, "Next");
                    next.AddData(childId);
                    next.Status = PureCatConstants.SUCCESS;
                    target.AddChild(next);

                    IMessageTree t = tree.Copy();

                    t.Message = target;

                    _mTree.MessageId       = childId;
                    _mTree.ParentMessageId = tree.MessageId;
                    _mTree.RootMessageId   = rootId ?? tree.MessageId;

                    manager.Flush(t);
                }
            }
Exemplo n.º 34
0
 /// <summary>
 ///   添加Event和Heartbeat
 /// </summary>
 /// <param name="manager"> </param>
 /// <param name="message"> </param>
 public void Add(DefaultMessageManager manager, IMessage message)
 {
     if ((_mStack.Count == 0))
     {
         IMessageTree tree = _mTree.Copy();
         tree.MessageId = manager.NextMessageId();
         tree.Message = message;
         manager.Flush(tree);
     }
     else
     {
         ITransaction entry = _mStack.Peek();
         entry.AddChild(message);
     }
 }
Exemplo n.º 35
0
        internal void Flush(IMessageTree tree)
        {
            if (tree.MessageId == null)
            {
                tree.MessageId = NextMessageId();
            }
            if (_sender != null)
            {
                _sender.Send(tree);

                Reset();

                _statistics?.OnSending();
            }
        }
Exemplo n.º 36
0
        private void LogQueueBytesOverflow(IMessageTree tree)
        {
            if (_mStatistics != null)
            {
                _mStatistics.OnBytesOverflowed(tree);
            }

            // throw it away since the queue is full
            Interlocked.Add(ref _mErrors, 1);

            if (_mErrors % 10000 == 0)
            {
                Logger.Warn("Can't send message to cat-server because max queue byte size is reached ! Count: " + _mErrors);
            }
        }
Exemplo n.º 37
0
        private void SendInternal(IMessageTree tree)
        {
            if (_mLastChannel != null)
            {
                try
                {
                    Logger.Warn("SendInternal中,_mLastChannel关闭");
                    _mLastChannel.Close();
                }
                catch
                {
                    // ignore it
                }

                _mLastChannel = null;
            }

            if (_mActiveChannel != null && _mActiveChannel.Connected)
            {
                ChannelBuffer buf = new ChannelBuffer(8192);

                _mCodec.Encode(tree, buf);

                byte[] data = buf.ToArray();

                _mActiveChannel.Client.Send(data);

                if (_mStatistics != null)
                {
                    _mStatistics.OnBytes(data.Length);
                }
            }
            else
            {
                Logger.Warn("SendInternal中,Socket关闭");
            }
        }
Exemplo n.º 38
0
            public Context(String domain, String hostName, String ipAddress)
            {
                _mTree = new DefaultMessageTree();
                _mStack = new Stack<ITransaction>();

                Thread thread = Thread.CurrentThread;
                String groupName = Thread.GetDomain().FriendlyName;

                _mTree.ThreadGroupName = groupName;
                _mTree.ThreadId = thread.ManagedThreadId.ToString(CultureInfo.InvariantCulture);
                _mTree.ThreadName = thread.Name;

                _mTree.Domain = domain;
                _mTree.HostName = hostName;
                _mTree.IpAddress = ipAddress;
            }
Exemplo n.º 39
0
        internal void Flush(IMessageTree tree)
        {
            if (_mSender != null)
            {
                _mSender.Send(tree);

                if (_mStatistics != null)
                {
                    _mStatistics.OnSending(tree);
                }
            }
        }
Exemplo n.º 40
0
        internal void Flush(IMessageTree tree)
        {
            if (tree.MessageId == null)
            {
                tree.MessageId = NextMessageId();
            }
            if (_sender != null)
            {
                _sender.Send(tree);

                Reset();

                if (_statistics != null)
                {
                    _statistics.OnSending();
                }
            }
        }
Exemplo n.º 41
0
 public void OnOverflowed(IMessageTree tree)
 {
     Overflowed++;
 }
Exemplo n.º 42
0
        protected internal void DecodeMessage(ChannelBuffer buf, IMessageTree tree)
        {
            Stack<ITransaction> stack = new Stack<ITransaction>();
            IMessage parent = DecodeLine(buf, null, stack, tree);

            tree.Message = parent;
            tree.EstimatedByteSize = parent.EstimateByteSize();

            while (buf.ReadableBytes() > 0)
            {
                IMessage message = DecodeLine(buf, (ITransaction) parent, stack, tree);

                if (message is ITransaction)
                {
                    parent = message;
                }
                else
                {
                    break;
                }
            }
        }
Exemplo n.º 43
0
        private bool isAtomicMessage(IMessageTree tree) {
            IMessage message = tree.Message;

		    if (message is ITransaction) {
                String type = message.Type;

                return (type != null &&
                    (type.StartsWith("Cache.") || type.StartsWith("Redis") || type.StartsWith("Memcached")
                    || "SQL" == type));
		    } else {
			    return true;
		    }
        }
Exemplo n.º 44
0
        protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent,
                                               Stack<ITransaction> stack, IMessageTree tree)
        {
            BufferHelper helper = _mBufferHelper;
            char identifier = (char)buf.ReadByte();
            string timestamp = helper.Read(buf, TAB);
            string type = helper.Read(buf, TAB);
            string name = helper.Read(buf, TAB);
            switch (identifier)
            {
                case 't':
                    IMessage transaction = new DefaultTransaction(type, name, null);

                    helper.Read(buf, LF); // get rid of line feed
                    transaction.Timestamp = _mDateHelper.Parse(timestamp);

                    if (parent != null)
                    {
                        parent.AddChild(transaction);
                    }

                    stack.Push(parent);
                    return transaction;
                case 'A':
                    DefaultTransaction tran = new DefaultTransaction(type, name, null);
                    string status = helper.Read(buf, TAB);
                    string duration = helper.Read(buf, TAB);
                    string data = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    tran.Timestamp = _mDateHelper.Parse(timestamp);
                    tran.Status = status;
                    tran.AddData(data);

                    long d = long.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer);

                    tran.DurationInMicros = d;

                    if (parent != null)
                    {
                        parent.AddChild(tran);
                        return parent;
                    }
                    return tran;
                case 'T':
                    string transactionStatus = helper.Read(buf, TAB);
                    string transactionDuration = helper.Read(buf, TAB);
                    string transactionData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    parent.Status = transactionStatus;
                    parent.AddData(transactionData);

                    long transactionD = long.Parse(transactionDuration.Substring(0, transactionDuration.Length - 2), NumberStyles.Integer);

                    parent.DurationInMicros = transactionD;

                    return stack.Pop();
                case 'E':
                    DefaultEvent evt = new DefaultEvent(type, name);
                    string eventStatus = helper.Read(buf, TAB);
                    string eventData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    evt.Timestamp = _mDateHelper.Parse(timestamp);
                    evt.Status = eventStatus;
                    evt.AddData(eventData);

                    if (parent != null)
                    {
                        parent.AddChild(evt);
                        return parent;
                    }
                    return evt;
                case 'M':
                    DefaultMetric metric = new DefaultMetric(type, name);
                    string metricStatus = helper.Read(buf, TAB);
                    string metricData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF);
                    metric.Timestamp = _mDateHelper.Parse(timestamp);
                    metric.Status = metricStatus;
                    metric.AddData(metricData);

                    if (parent != null)
                    {
                        parent.AddChild(metric);
                        return parent;
                    }
                    return metric;
                case 'L':
                    DefaultTrace trace = new DefaultTrace(type, name);
                    string traceStatus = helper.Read(buf, TAB);
                    string traceData = helper.Read(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    trace.Timestamp = _mDateHelper.Parse(timestamp);
                    trace.Status = traceStatus;
                    trace.AddData(traceData);

                    if (parent != null)
                    {
                        parent.AddChild(trace);
                        return parent;
                    }
                    return trace;
                case 'H':
                    DefaultHeartbeat heartbeat = new DefaultHeartbeat(type, name);
                    string heartbeatStatus = helper.Read(buf, TAB);
                    string heartbeatData = helper.ReadRaw(buf, TAB);

                    helper.Read(buf, LF); // get rid of line feed
                    heartbeat.Timestamp = _mDateHelper.Parse(timestamp);
                    heartbeat.Status = heartbeatStatus;
                    heartbeat.AddData(heartbeatData);

                    if (parent != null)
                    {
                        parent.AddChild(heartbeat);
                        return parent;
                    }
                    return heartbeat;
            }

            Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf);
            //throw new Exception("Unknown identifier int name"); //java版的抛出异常

            // unknown message, ignore it
            return parent;
        }
Exemplo n.º 45
0
        private void LogQueueBytesOverflow(IMessageTree tree)
        {
            if (_mStatistics != null)
            {
                _mStatistics.OnBytesOverflowed(tree);
            }

            // throw it away since the queue is full
            Interlocked.Add(ref _mErrors, 1);

            if (_mErrors % 10000 == 0)
            {
                Logger.Warn("Can't send message to cat-server because max queue byte size is reached ! Count: " + _mErrors);
            }
        }
Exemplo n.º 46
0
 public void Send(IMessageTree tree)
 {
     if (isAtomicMessage(tree))
     {
         if (_mAtomicTrees.Count >= _mClientConfig.MaxQueueSize)
         {
             LogQueueOverflow(tree);
         }
         else if (_mAtomicTrees.EstimatedByteSize >= _mClientConfig.MaxQueueByteSize)
         {
             LogQueueBytesOverflow(tree);
         }
         else
         {
             // if (_mAtomicTrees.Count < _mClientConfig.MaxQueueSize && _mAtomicTrees.EstimatedByteSize < _mClientConfig.MaxQueueByteSize)
             _mAtomicTrees.Enqueue(tree);
         }
     }
     else
     {
         if (_mQueue.Count >= _mClientConfig.MaxQueueSize)
         {
             LogQueueOverflow(tree);
         }
         else if (_mQueue.EstimatedByteSize >= _mClientConfig.MaxQueueByteSize)
         {
             LogQueueBytesOverflow(tree);
         }
         else
         {
             // if (_mQueue.Count < _mClientConfig.MaxQueueSize && _mQueue.EstimatedByteSize < _mClientConfig.MaxQueueByteSize)
             _mQueue.Enqueue(tree);
         }
     }
 }
Exemplo n.º 47
0
        private void SendInternal(IMessageTree tree)
        {
            if (_mActiveChannel != null && _mActiveChannel.Connected)
            {
                // Re-use the existing buffer.
                buf.Reset();

                _mCodec.Encode(tree, buf);

                // Nullify reference as early as possible
                tree.Message = null;

                byte[] data = buf.ToArray();

                _mActiveChannel.Client.Send(data);

                if (_mStatistics != null)
                {
                    _mStatistics.OnBytes(data.Length);
                }
            }
            else
            {
                tree.Message = null;
                Logger.Warn("SendInternal中,Socket关闭");
            }
        }
Exemplo n.º 48
0
        private bool IsAtomicMessage(IMessageTree tree)
        {
            var message = tree.Message;

            if (message is ITransaction)
            {
                var type = message.Type;
                return type.StartsWith("Cache.") || type.StartsWith("SQL");
            }
            return true;
        }
Exemplo n.º 49
0
        protected internal IMessage DecodeLine(ChannelBuffer buf, ITransaction parent,
                                               Stack<ITransaction> stack, IMessageTree tree)
        {
            BufferHelper helper = _mBufferHelper;
            byte identifier = buf.ReadByte();
            String timestamp = helper.Read(buf, TAB);
            String type = helper.Read(buf, TAB);
            String name = helper.Read(buf, TAB);

            if (identifier == 'E')
            {
                IMessage evt = new DefaultEvent(type, name);
                String status = helper.Read(buf, TAB);
                String data = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                evt.Timestamp = _mDateHelper.Parse(timestamp);
                evt.Status = status;
                evt.AddData(data);

                if (parent != null)
                {
                    parent.AddChild(evt);
                    tree.EstimatedByteSize += evt.EstimateByteSize();
                    return parent;
                }
                return evt;
            }
            if (identifier == 'M')
            {
                DefaultMetric metric = new DefaultMetric(type, name);
                String status = helper.Read(buf, TAB);
                String data = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                metric.Timestamp = _mDateHelper.Parse(timestamp);
                metric.Status = status;
                metric.AddData(data);

                if (parent != null)
                {
                    parent.AddChild(metric);
                    tree.EstimatedByteSize += metric.EstimateByteSize();
                    return parent;
                }
                return metric;
            }
            if (identifier == 'H')
            {
                IMessage heartbeat = new DefaultHeartbeat(type, name);
                String status0 = helper.Read(buf, TAB);
                String data1 = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                heartbeat.Timestamp = _mDateHelper.Parse(timestamp);
                heartbeat.Status = status0;
                heartbeat.AddData(data1);

                if (parent != null)
                {
                    parent.AddChild(heartbeat);
                    tree.EstimatedByteSize += heartbeat.EstimateByteSize();
                    return parent;
                }
                return heartbeat;
            }
            if (identifier == 't')
            {
                IMessage transaction = new DefaultTransaction(type, name,
                                                              null);

                helper.Read(buf, LF); // get rid of line feed
                transaction.Timestamp = _mDateHelper.Parse(timestamp);

                if (parent != null)
                {
                    parent.AddChild(transaction);
                }

                stack.Push(parent);
                return transaction;
            }
            if (identifier == 'A')
            {
                ITransaction transaction2 = new DefaultTransaction(type, name, null);
                String status3 = helper.Read(buf, TAB);
                String duration = helper.Read(buf, TAB);
                String data4 = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                transaction2.Timestamp = _mDateHelper.Parse(timestamp);
                transaction2.Status = status3;
                transaction2.AddData(data4);

                long d = Int64.Parse(duration.Substring(0, duration.Length - 2), NumberStyles.Integer);
                transaction2.DurationInMicros = d;

                if (parent != null)
                {
                    parent.AddChild(transaction2);
                    tree.EstimatedByteSize += transaction2.EstimateByteSize();
                    return parent;
                }
                return transaction2;
            }
            if (identifier == 'T')
            {
                String status5 = helper.Read(buf, TAB);
                String duration6 = helper.Read(buf, TAB);
                String data7 = helper.ReadRaw(buf, TAB);

                helper.Read(buf, LF); // get rid of line feed
                parent.Status = status5;
                parent.AddData(data7);

                long d8 = Int64.Parse(
                    duration6.Substring(0, duration6.Length - 2),
                    NumberStyles.Integer);
                parent.DurationInMicros = d8;
                tree.EstimatedByteSize += parent.EstimateByteSize();
                return stack.Pop();
            }
            Logger.Error("Unknown identifier(" + identifier + ") of message: " + buf);

            // unknown message, ignore it
            return parent;
        }
Exemplo n.º 50
0
 public void OnSending(IMessageTree tree)
 {
     Produced++;
 }
Exemplo n.º 51
0
            public Context(DefaultMessageManager manager, String domain)
            {
                _mManager = manager;

                _mTree = new DefaultMessageTree();
                _mStack = new Stack<ITransaction>();

                Thread thread = Thread.CurrentThread;
                String groupName = Thread.GetDomain().FriendlyName;

                _mTree.ThreadGroupName = groupName;
                _mTree.ThreadId = thread.ManagedThreadId.ToString();
                _mTree.ThreadName = thread.Name;

                _mTree.Domain = domain;
                // _mTree.HostName = hostName;
                // _mTree.IpAddress = ipAddress;

                _mLength = 1;

                _mKnownExceptions = new HashSet<Exception>();
            }
Exemplo n.º 52
0
        private void SendInternal(IMessageTree tree, TcpClient activeChannel)
        {

            if (activeChannel != null && activeChannel.Connected)
            {
                var buf = new ChannelBuffer(8192);

                _codec.Encode(tree, buf);

                var data = buf.ToArray();

                activeChannel.Client.Send(data);

                if (_statistics != null)
                {
                    _statistics.OnBytes(data.Length);
                }
            }
            else
            {
                Logger.Warn("SendInternal中,Socket关闭");
            }
        }
Exemplo n.º 53
0
        protected internal int EncodeHeader(IMessageTree tree, ChannelBuffer buf)
        {
            BufferHelper helper = _mBufferHelper;
            int count = 0;

            count += helper.Write(buf, ID);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.Domain);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.HostName);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.IpAddress);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ThreadGroupName);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ThreadId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ThreadName);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.MessageId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.ParentMessageId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.RootMessageId);
            count += helper.Write(buf, TAB);
            count += helper.Write(buf, tree.SessionToken);
            count += helper.Write(buf, LF);

            return count;
        }