Cat 跨线程之 ForkedTransaction 用法和原理分析

https://www.cnblogs.com/ucarinc/p/7838154.html

子线程消息树和父线程消息树,联系起来的方法就是

在DefaultForkedTransaction的构造方法中,将父线程消息树的id,设置为子线程消息树的parentId,

子线程消息树,就和父线程消息树联系起来了。

 

 

package com.dianping.cat.message.internal;

import com.dianping.cat.Cat;
import com.dianping.cat.message.ForkedTransaction;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.unidal.helper.Threads;

import java.io.File;
import java.util.concurrent.TimeUnit;

public class MultiThreadingTest {
    @After
    public void after() {
    }

    @Before
    public void before() {
        Cat.initialize(new File("/data/appdatas/cat/client.xml"));
    }

    @Test
    public void testForkedTransaction() throws Exception {
        Transaction t = Cat.newTransaction("ForkedRoot", "Root");
        ForkedTransaction t1 = Cat.newForkedTransaction("ForkedChild", "Child1"); //在当前消息树中埋点桥接节点
        Threads.forGroup().start(new TimedThread(t1, 100)); //与子线程共享这个对象
        TimeUnit.MILLISECONDS.sleep(200);
        t.setStatus(Message.SUCCESS);
        t.complete();
    }

    static class TimedThread extends Thread {
        private ForkedTransaction m_transaction;

        private int m_timeout;

        public TimedThread(ForkedTransaction t, int timeout) {
            m_transaction = t;
            m_timeout = timeout;
        }

        @Override
        public void run() {
            m_transaction.fork();
            try {
                TimeUnit.MILLISECONDS.sleep(m_timeout);
                Cat.logEvent("TimedThread", "Timeout." + m_timeout);
                m_transaction.setStatus(Message.SUCCESS);
            } catch (Exception e) {
                Cat.logError(e);
                m_transaction.setStatus(e);
            } finally {
                m_transaction.complete();
            }
        }
    }
}

模型图

代码和流程分析

new一个ForkT在干嘛?
1.初始化当前线程的消息树根节点消息Id
2.创建子线程中的事务,并设置自己的根节点和父节点的消息Id
3.在消息树的当前节点加入一个额外的event节点,并且把子线程的msgId记录下来

linkasRunAway的具体逻辑

子线程的fork在干嘛?

1.在子线程中初始化本线程的消息树

2. 把forkT的消息id作为子线程的根节点消息id,这样就可以通过runway event节点找到子线程的消息树

注意  m_forkedMessageId , m_rootMessageId, m_parentMessageId 这三个参数。

 

        在父线程中创建ForkedTransaction, m_manager 是父线程的manager。
        父线程manager传入到DefaultForkedTransaction的构造方法中

	public ForkedTransaction newForkedTransaction(String type, String name) {
		// this enable CAT client logging cat message without explicit setup
		if (!m_manager.hasContext()) {
			m_manager.setup();
		}

		MessageTree tree = m_manager.getThreadLocalMessageTree();

		if (tree.getMessageId() == null) {
			tree.setMessageId(createMessageId());
		}
        
        //在父线程中创建ForkedTransaction, m_manager 是父线程的manager。
        //父线程manager传入到DefaultForkedTransaction的构造方法中
		DefaultForkedTransaction transaction = new DefaultForkedTransaction(type, name, m_manager);

		if (m_manager instanceof DefaultMessageManager) {
			((DefaultMessageManager) m_manager).linkAsRunAway(transaction);
		}
		m_manager.start(transaction, true);
		return transaction;
	}

 

在DefaultForkedTransaction的构造方法中,将父线程消息树的id,设置为子线程消息树的parentId,

子线程消息树,就和父线程消息树联系起来了。

public class DefaultForkedTransaction extends DefaultTransaction implements ForkedTransaction {
	private String m_rootMessageId;

	private String m_parentMessageId;

	private String m_forkedMessageId;

	public DefaultForkedTransaction(String type, String name, MessageManager manager) {
        //manager是父线程的manager
		super(type, name, manager);

		setStandalone(false);

        //tree是父线程消息树
		MessageTree tree = manager.getThreadLocalMessageTree();

		if (tree != null) {
            //父线程消息树的rootManagerId
			m_rootMessageId = tree.getRootMessageId();
            //父线程消息节点的messageId
			m_parentMessageId = tree.getMessageId();

			// Detach parent transaction and this forked transaction, by calling linkAsRunAway(), at this earliest moment,
			// so that thread synchronization is not needed at all between them in the future.    
            //子线程消息树Id
			m_forkedMessageId = Cat.createMessageId();
		}
	}

	@Override
	public void fork() {
		MessageManager manager = getManager();

		manager.setup();
		manager.start(this, false);

		MessageTree tree = manager.getThreadLocalMessageTree();

		if (tree != null) {
			// Override tree.messageId to be forkedMessageId of current forked transaction, which is created in the parent
			// thread.
			tree.setMessageId(m_forkedMessageId);
			tree.setRootMessageId(m_rootMessageId == null ? m_parentMessageId : m_rootMessageId);
			tree.setParentMessageId(m_parentMessageId);
		}
	}

	@Override
	public String getForkedMessageId() {
		return m_forkedMessageId;
	}
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值