private IMessageTree MergeTree() { var max = PureCatConstants.MAX_CHILD_NUMBER; var tran = new DefaultTransaction("_CatMergeTree", "_CatMergeTree"); if (!_atomicTress.TryDequeue(out IMessageTree first)) { return(null); } tran.Status = PureCatConstants.SUCCESS; tran.Complete(); tran.AddChild(first.Message); tran.Timestamp = first.Message.Timestamp; long lastTimestamp = 0; long lastDuration = 0; while (max-- >= 0) { if (!_atomicTress.TryDequeue(out IMessageTree tree)) { tran.DurationInMillis = (lastTimestamp - tran.Timestamp + lastDuration); break; } lastTimestamp = tree.Message.Timestamp; var message = tree.Message as DefaultTransaction; lastDuration = message?.DurationInMillis ?? 0; tran.AddChild(tree.Message); } first.Message = tran; return(first); }
private void MarkAsNotCompleted(DefaultTransaction transaction) { IEvent notCompleteEvent = new DefaultEvent("CAT", "BadInstrument") { Status = "TransactionNotCompleted" }; notCompleteEvent.Complete(); transaction.AddChild(notCompleteEvent); transaction.Complete(); }
public void markAsNotCompleted(DefaultTransaction transaction) { DefaultEvent evt = new DefaultEvent("cat", "BadInstrument"); evt.Status = "TransactionNotCompleted"; evt.SetCompleted(true); transaction.AddChild(evt); transaction.SetCompleted(true); }
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); } }
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); } }
private void TruncateAndFlush(long timestamp) { var tree = _mTree; var stack = _mStack; var message = tree.Message; if (message is DefaultTransaction) { var id = tree.MessageId ?? _manager.NextMessageId(); var rootId = tree.RootMessageId; var childId = _manager.NextMessageId(); var source = message as DefaultTransaction; var 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(stack, source, target, 1); var list = stack.ToList(); for (var i = list.Count - 1; i >= 0; i--) { var tran = list[i] as DefaultTransaction; if (tran != null) { tran.Timestamp = timestamp; tran.DurationInMicros = -1; } } var next = new DefaultEvent(PureCatConstants.TYPE_REMOTE_CALL, "Next"); next.AddData(childId); next.Status = PureCatConstants.SUCCESS; target.AddChild(next); var t = tree.Copy(); t.Message = target; _mTree.MessageId = childId; _mTree.ParentMessageId = id; _mTree.RootMessageId = rootId ?? tree.MessageId; _manager.Flush(t); } }
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); }