提交 1c3673a3 编写于 作者: Y youyong205

Merge branch 'biz' of github.com:dianping/cat into biz

......@@ -97,6 +97,10 @@ public class ClientConfigManager implements LogEnabled {
}
}
public int getTaggedTransactionCacheSize() {
return 1024;
}
public void initialize(File configFile) throws Exception {
ClientConfig globalConfig = null;
ClientConfig clientConfig = null;
......
......@@ -2,7 +2,10 @@ package com.dianping.cat.message.internal;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
......@@ -18,6 +21,7 @@ import com.dianping.cat.configuration.ClientConfigManager;
import com.dianping.cat.configuration.NetworkInterfaceManager;
import com.dianping.cat.configuration.client.entity.Domain;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.TaggedTransaction;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.io.MessageSender;
import com.dianping.cat.message.io.TransportManager;
......@@ -53,6 +57,8 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
private TransactionHelper m_validator = new TransactionHelper();
private Map<String, TaggedTransaction> m_taggedTransactions;
private Logger m_logger;
@Override
......@@ -64,6 +70,20 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
}
}
@Override
public void bind(String tag, String title) {
TaggedTransaction t = m_taggedTransactions.get(tag);
if (t != null) {
MessageTree tree = getThreadLocalMessageTree();
if (tree != null) {
t.start();
t.bind(tag, tree.getMessageId(), title);
}
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
......@@ -161,6 +181,18 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
} catch (IOException e) {
throw new InitializationException("Error while initializing MessageIdFactory!", e);
}
// initialize the tagged transaction cache
final int size = m_configManager.getTaggedTransactionCacheSize();
m_taggedTransactions = new LinkedHashMap<String, TaggedTransaction>(size * 4 / 3 + 1, 0.75f, true) {
private static final long serialVersionUID = 1L;
@Override
protected boolean removeEldestEntry(Entry<String, TaggedTransaction> eldest) {
return size() >= size;
}
};
}
@Override
......@@ -240,6 +272,12 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
if (ctx != null) {
ctx.start(transaction, forked);
if (transaction instanceof TaggedTransaction) {
TaggedTransaction tt = (TaggedTransaction) transaction;
m_taggedTransactions.put(tt.getTag(), tt);
}
} else if (m_firstMessage) {
m_firstMessage = false;
m_logger.warn("CAT client is not enabled because it's not initialized yet");
......@@ -434,7 +472,10 @@ public class DefaultMessageManager extends ContainerHolder implements MessageMan
}
private void markAsRunAway(Transaction parent, DefaultTaggedTransaction transaction) {
transaction.addData("RunAway");
if (!transaction.hasChildren()) {
transaction.addData("RunAway");
}
transaction.setStatus(Message.SUCCESS);
transaction.setStandalone(true);
transaction.complete();
......
......@@ -62,7 +62,9 @@ public class DefaultTaggedTransaction extends DefaultTransaction implements Tagg
public void start() {
MessageTree tree = getManager().getThreadLocalMessageTree();
tree.setParentMessageId(m_parentMessageId);
tree.setRootMessageId(m_rootMessageId);
if (tree != null && tree.getRootMessageId() == null) {
tree.setParentMessageId(m_parentMessageId);
tree.setRootMessageId(m_rootMessageId);
}
}
}
......@@ -88,4 +88,14 @@ public interface MessageManager {
*/
public void start(Transaction transaction, boolean forked);
/**
* Binds the current message tree to the transaction tagged with <code>tag</code>.
*
* @param tag
* tag name of the tagged transaction
* @param title
* title shown in the logview
*/
public void bind(String tag, String title);
}
\ No newline at end of file
......@@ -11,9 +11,7 @@ import org.unidal.helper.Threads;
import com.dianping.cat.Cat;
import com.dianping.cat.message.ForkedTransaction;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.TaggedTransaction;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.spi.MessageTree;
public class MultiThreadingTest {
@After
......@@ -44,12 +42,12 @@ public class MultiThreadingTest {
@Test
public void testTaggedTransaction() throws Exception {
Transaction t = Cat.newTransaction("TaggedRoot3", "Root");
TaggedTransaction t1 = Cat.newTaggedTransaction("TaggedChild", "Child1", "Tag1");
TaggedTransaction t2 = Cat.newTaggedTransaction("TaggedChild", "Child2", "Tag2");
Transaction t = Cat.newTransaction("TaggedRoot", "Root");
Cat.newTaggedTransaction("TaggedChild", "Child1", "Tag1");
Cat.newTaggedTransaction("TaggedChild", "Child2", "Tag2");
Threads.forGroup().start(new TaggedThread(t1, 500, "Tag1"));
Threads.forGroup().start(new TaggedThread(t2, 100, "Tag2"));
Threads.forGroup().start(new TaggedThread(500, "Tag1"));
Threads.forGroup().start(new TaggedThread(100, "Tag2"));
TimeUnit.MILLISECONDS.sleep(200);
......@@ -58,14 +56,11 @@ public class MultiThreadingTest {
}
static class TaggedThread extends Thread {
private TaggedTransaction m_transaction;
private int m_timeout;
private String m_tag;
public TaggedThread(TaggedTransaction t, int timeout, String tag) {
m_transaction = t;
public TaggedThread(int timeout, String tag) {
m_timeout = timeout;
m_tag = tag;
}
......@@ -73,15 +68,12 @@ public class MultiThreadingTest {
@Override
public void run() {
Transaction t = Cat.newTransaction("TaggedThread", m_tag);
MessageTree tree = Cat.getManager().getThreadLocalMessageTree();
m_transaction.start();
try {
TimeUnit.MILLISECONDS.sleep(m_timeout);
t.setStatus(Message.SUCCESS);
m_transaction.bind(m_tag, tree.getMessageId(), "child");
Cat.getManager().bind(m_tag, "Child Tagged Thread");
} catch (Exception e) {
Cat.logError(e);
t.setStatus(e);
......
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<domain id="Cat2"/>
<domain id="Cat"/>
</config>
......@@ -6,6 +6,9 @@ A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
E RemoteCall Next 0
T URL MyPage 0 k1=v1
t URL MyPage
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
......@@ -13,12 +16,19 @@ A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
E RemoteCall Next 0
T URL MyPage 0 k1=v1
t URL MyPage
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
A URL0 MyPage 0
A URL1 MyPage 0
E RemoteCall Next 0
T URL MyPage 0 k1=v1
t URL MyPage
t URL1 MyPage
t URL2 MyPage
E Event Name0 0
......@@ -26,16 +36,37 @@ E Event Name1 0
E Event Name2 0
E Event Name3 0
E Event Name4 0
T URL2 MyPage 0
T URL1 MyPage 0
E RemoteCall Next 0
T URL MyPage 0 k1=v1
t URL MyPage
t URL1 MyPage
t URL2 MyPage
E Event Name5 0
E Event Name6 0
E Event Name7 0
E Event Name8 0
E Event Name9 0
T URL2 MyPage 0
T URL1 MyPage 0
E RemoteCall Next 0
T URL MyPage 0 k1=v1
t URL MyPage
t URL1 MyPage
t URL2 MyPage
E Event Name10 0
E Event Name11 0
E Event Name12 0
E Event Name13 0
E Event Name14 0
T URL2 MyPage 0
T URL1 MyPage 0
E RemoteCall Next 0
T URL MyPage 0 k1=v1
t URL MyPage
t URL1 MyPage
t URL2 MyPage
E Event Name15 0
E Event Name16 0
E Event Name17 0
......@@ -43,4 +74,5 @@ E Event Name18 0
E Event Name19 0
T URL2 MyPage unset
T URL1 MyPage unset
E TruncatedTransaction TotalDuration 0
T URL MyPage 0 k1=v1
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册