public String GetNextId() { string id; _mReusedIds.TryDequeue(out id, false); if (null != id) { return(id); } long timestamp = Timestamp; if (timestamp != _mTimestamp) { _mIndex = 0; _mTimestamp = timestamp; _mTimestampStr = timestamp.ToString(); } int index = Interlocked.Increment(ref _mIndex); //StringBuilder sb = new StringBuilder(_mDomain.Length + 32); //sb.Append(_mDomain); //sb.Append('-'); //sb.Append(_mIpAddress); //sb.Append('-'); //sb.Append(timestamp); //sb.Append('-'); //sb.Append(index); //return sb.ToString(); id = String.Join("-", new string[] { _mDomain, _mIpAddress, _mTimestampStr, index.ToString() }); SaveMark(false); return(id); }
public void AsynchronousSendTask(object o) { try { while (true) { if (_mActive) { if (_mActiveChannel == null || !_mActiveChannel.Connected) { Logger.Warn("TCP connection is not available." + _mActiveChannel); Thread.Sleep(CatConstants.TCP_CHECK_INTERVAL); continue; } try { IMessageTree tree; _mQueue.TryDequeue(out tree, true); if (tree != null) { SendInternal(tree); tree = null; } ; } catch (Exception ex) { Cat.lastException = ex; } } else { Thread.Sleep(CatConstants.TCP_CHECK_INTERVAL); } } } catch (Exception ex) { Cat.lastException = ex; } }
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); }
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; }