Java Code Examples for com.dianping.cat.message.Transaction#addData()

The following examples show how to use com.dianping.cat.message.Transaction#addData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CatInvokeTrace.java    From octo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected void serverSendInCodec(TraceParam traceParam, RpcInvocation invocation) {
    TraceTimeline timeline = traceParam.getTraceTimeline();
    Transaction transaction = Cat.newTransactionWithDuration(DORADO_SERVICE, traceParam.getSpanName(),
            System.currentTimeMillis() - traceParam.getStartTimestamp());
    try {
        serverLogEvent(traceParam, invocation);
        transaction.addData(TRACE_ID, traceParam.getTraceId());
        transaction.addData(CatEventType.SERVICE_PHASECOST.type, timeline.genProviderAllPhaseCost());

        if (traceParam.getThrowable() != null) {
            Cat.logErrorWithCategory(DORADO_SERVICE + "." + traceParam.getRemoteAppkey(), traceParam.getThrowable());
            transaction.setStatus(traceParam.getThrowable());
        } else {
            transaction.setStatus(Transaction.SUCCESS);
        }
    } catch (Exception e) {
        Cat.logErrorWithCategory(DORADO_SERVICE + "." + traceParam.getRemoteAppkey(), e);
        transaction.setStatus(e);
    } finally {
        transaction.complete();
    }
}
 
Example 2
Source File: CatInvokeTrace.java    From octo-rpc with Apache License 2.0 6 votes vote down vote up
@Override
protected void serverSendInFilter(TraceParam traceParam, RpcInvocation invocation) {
    Transaction transaction = (Transaction) traceParam.getAttachment(TRANSACTION);
    if (transaction == null) {
        logger.warn("ServerSide: Request {} won't do cat report, cause no start transaction info.", traceParam.getSpanName());
        return;
    }
    try {
        serverLogEvent(traceParam, invocation);
        transaction.addData(TRACE_ID, traceParam.getTraceId());

        if (traceParam.getThrowable() == null) {
            transaction.setStatus(Transaction.SUCCESS);
        } else {
            Cat.logErrorWithCategory(DORADO_SERVICE + "." + traceParam.getRemoteAppkey(), traceParam.getThrowable());
            transaction.setStatus(traceParam.getThrowable());
        }
    } catch (Exception e) {
        Cat.logErrorWithCategory(DORADO_SERVICE + "." + traceParam.getRemoteAppkey(), e);
        transaction.setStatus(e);
    } finally {
        transaction.complete();
    }
}
 
Example 3
Source File: CatController.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * Cat.newTransaction 与Cat.getProducer().newTransaction 区别在于 一个是重新生成一个transation  和获取当前线程绑定的transaction“
 *
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/cycletransation", method = RequestMethod.GET)
public String newtransation() throws Exception {
	Transaction t = Cat.getProducer().newTransaction("TransactionTest", "Cat.getProducer()");
	Cat.getProducer().logEvent("eventType1", "1", Message.SUCCESS, "");
	Cat.getProducer().logEvent("eventType1", "2", Message.SUCCESS, "");
	Transaction t2 = Cat.getProducer().newTransaction("TransactionTest-1", "child transaction 1");
	Cat.getProducer().logEvent("eventType2-1", "2-1", Message.SUCCESS, "");
	Cat.getProducer().logEvent("eventType2-2", "2-2", Message.SUCCESS, "");
	t2.addData("tChild transaction-1");
	t2.setStatus(Message.SUCCESS);
	t2.complete();
	Transaction t3 = Cat.getProducer().newTransaction("TransactionTest-2", "child transaction 2");
	Cat.getProducer().logEvent("eventType3-1", "3-1", Message.SUCCESS, "");
	Cat.getProducer().logEvent("eventType3-2", "3-2", Message.SUCCESS, "");
	t3.addData("Child transaction-2");
	t3.setStatus(Message.SUCCESS);
	// 休眠3s 验证时间
	Thread.sleep(4000);
	t3.complete();
	t.addData(" Parent transaction");
	t.setStatus(Message.SUCCESS);
	t.complete();
	return "";
}
 
Example 4
Source File: ConsumerLeaseHolder.java    From hermes with Apache License 2.0 6 votes vote down vote up
private long loadNewLeasesAssignedByOtherMetaservers(long lastRunTime) throws DalException {
	Transaction transaction = Cat.newTransaction(CatConstants.TYPE_LEASE_DIRTY_LOAD, "Consumer");
	int count = 0;
	long maxLodedTime = -1L;
	try {
		List<ConsumerLease> changes = m_leasesDao.listLatestChanges(new Date(lastRunTime), Networks.forIp()
		      .getLocalHostAddress(), ConsumerLeaseEntity.READSET_FULL);
		if (changes != null && !changes.isEmpty()) {
			count = changes.size();
			maxLodedTime = loadExistingLeases(changes);
		}
		transaction.setStatus(Transaction.SUCCESS);
		return maxLodedTime;
	} catch (Exception e) {
		transaction.setStatus(e);
		throw e;
	} finally {
		transaction.addData("count", count);
		transaction.complete();
	}
}
 
Example 5
Source File: LeaseResource.java    From hermes with Apache License 2.0 6 votes vote down vote up
private LeaseAcquireResponse proxyBrokerLeaseRequestIfNecessary(HttpServletRequest req, String uri,
      Map<String, String> params, Object payload, Transaction tx) {
	if (m_clusterStateHolder.getRole() == Role.LEADER) {
		return null;
	} else {
		if (!isFromAnotherMetaServer(req)) {
			HostPort leader = m_clusterStateHolder.getLeader();
			if (leader != null) {
				tx.addData("dest", leader.getHost() + ":" + leader.getPort());
				return proxyPass(leader.getHost(), leader.getPort(), uri, params, payload);
			} else {
				return new LeaseAcquireResponse(false, null, m_systemClockService.now()
				      + PROXY_PASS_FAIL_DELAY_TIME_MILLIS);
			}
		} else {
			return new LeaseAcquireResponse(false, null, m_systemClockService.now() + PROXY_PASS_FAIL_DELAY_TIME_MILLIS);
		}
	}
}
 
Example 6
Source File: LeaseResource.java    From hermes with Apache License 2.0 6 votes vote down vote up
private LeaseAcquireResponse proxyConsumerLeaseRequestIfNecessary(HttpServletRequest req, String topic, String uri,
      Map<String, String> params, Object payload, Transaction tx) {

	Map<String, ClientContext> responsors = m_metaServerAssignmentHolder.getAssignment(topic);

	if (responsors != null && !responsors.isEmpty()) {
		ClientContext responsor = responsors.values().iterator().next();
		if (responsor != null) {
			if (m_config.getMetaServerHost().equals(responsor.getIp())
			      && m_config.getMetaServerPort() == responsor.getPort()) {
				return null;
			} else {
				if (!isFromAnotherMetaServer(req)) {
					tx.addData("dest", responsor.getIp() + ":" + responsor.getPort());
					return proxyPass(responsor.getIp(), responsor.getPort(), uri, params, payload);
				} else {
					return new LeaseAcquireResponse(false, null, m_systemClockService.now()
					      + PROXY_PASS_FAIL_DELAY_TIME_MILLIS);
				}
			}
		}
	}
	return new LeaseAcquireResponse(false, null, m_systemClockService.now() + NO_ASSIGNMENT_DELAY_TIME_MILLIS);

}
 
Example 7
Source File: BrokerLeaseHolder.java    From hermes with Apache License 2.0 6 votes vote down vote up
private long loadNewLeasesAssignedByOtherMetaservers(long lastRunTime) throws DalException {
	Transaction transaction = Cat.newTransaction(CatConstants.TYPE_LEASE_DIRTY_LOAD, "Broker");
	int count = 0;
	long maxLodedTime = -1L;
	try {
		List<BrokerLease> changes = m_leasesDao.listLatestChanges(new Date(lastRunTime), Networks.forIp()
		      .getLocalHostAddress(), BrokerLeaseEntity.READSET_FULL);
		if (changes != null && !changes.isEmpty()) {
			count = changes.size();
			maxLodedTime = loadExistingLeases(changes);
		}
		transaction.setStatus(Transaction.SUCCESS);
		return maxLodedTime;
	} catch (Exception e) {
		transaction.setStatus(e);
		throw e;
	} finally {
		transaction.addData("count", count);
		transaction.complete();
	}
}
 
Example 8
Source File: CatUtil.java    From hermes with Apache License 2.0 6 votes vote down vote up
public static void logElapse(String type, String name, long startTimestamp, int count,
      List<Pair<String, String>> datas, String status) {
	if (count > 0) {
		Transaction latencyT = Cat.newTransaction(type, name);
		long delta = System.currentTimeMillis() - startTimestamp;

		if (latencyT instanceof DefaultTransaction) {
			((DefaultTransaction) latencyT).setDurationStart(System.nanoTime() - delta * 1000000L);
		}
		latencyT.addData("*count", count);
		if (datas != null && !datas.isEmpty()) {
			for (Pair<String, String> data : datas) {
				latencyT.addData(data.getKey(), data.getValue());
			}
		}
		latencyT.setStatus(status);
		latencyT.complete();
	}
}
 
Example 9
Source File: BaseMessageListener.java    From hermes with Apache License 2.0 6 votes vote down vote up
private void setOnMessageStartTime(ConsumerMessage<T> msg) {
	if (msg instanceof BaseConsumerMessageAware) {
		BaseConsumerMessage<?> baseMsg = ((BaseConsumerMessageAware<?>) msg).getBaseConsumerMessage();
		long now = System.currentTimeMillis();
		baseMsg.setOnMessageStartTimeMills(now);

		Transaction latencyT = null;
		if (!msg.isResend()) {
			latencyT = Cat.newTransaction( //
			      CatConstants.TYPE_MESSAGE_CONSUME_LATENCY, msg.getTopic() + ":" + m_groupId);
		} else {
			latencyT = Cat.newTransaction( //
			      CatConstants.TYPE_MESSAGE_CONSUME_RESEND_LATENCY, msg.getTopic() + ":" + m_groupId);
		}
		long delta = System.currentTimeMillis() - baseMsg.getBornTime();

		if (latencyT instanceof DefaultTransaction) {
			((DefaultTransaction) latencyT).setDurationStart(System.nanoTime() - delta * 1000000L);
		}

		latencyT.addData("key", msg.getRefKey());
		latencyT.setStatus(Transaction.SUCCESS);
		latencyT.complete();
	}
}
 
Example 10
Source File: CatAop.java    From javabase with Apache License 2.0 5 votes vote down vote up
private Object aroundCache(CatMethodCache catMethodTransaction, ProceedingJoinPoint joinPoint) throws Throwable {
    //注解有值的优先以注解的值
    String type = catMethodTransaction.type();
    String name = catMethodTransaction.name();
    Object result = null;
    Transaction transaction = null;
    //不让cat异常导致业务异常
    try {
        transaction = Cat.getProducer().newTransaction(type, name);
    } catch (Exception e) {
        log.error("Cat.getProducer().newTransaction Error", e);
    }
    try {
        log.info("大众点评cat拦截 CatMethodCache :type="+type+";name="+name);
        result = joinPoint.proceed();
        if (transaction != null) {
            transaction.setStatus(Transaction.SUCCESS);
            if(joinPoint.getArgs().length>0)
            transaction.addData("key="+joinPoint.getArgs()[0]+";"+"value="+ JSONObject.toJSON(result==null?new Object():result).toString());
        }
    } catch (Throwable throwable) {
        if (transaction != null) transaction.setStatus(throwable);
        log.error("aroundTransaction exception", throwable);
        throw throwable;
    } finally {
        if (transaction != null)
            transaction.complete();
    }
    return result;
}
 
Example 11
Source File: DefaultMessageQueueFlusher.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public long flush(int maxMsgCount) {
	long maxPurgedSelectorOffset = purgeExpiredMsgs();
	long maxSavedSelectorOffset = Long.MIN_VALUE;

	int msgCount = 0;

	List<PendingMessageWrapper> todos = new ArrayList<>();
	if (!m_pendingMessages.isEmpty()) {
		PendingMessageWrapper msg = m_pendingMessages.poll();
		todos.add(msg);
		msgCount = msg.getBatch().getMsgSeqs().size();

		PendingMessageWrapper nextMsg = null;
		while ((nextMsg = m_pendingMessages.peek()) != null) {
			int nextPendingMsgCount = nextMsg.getBatch().getMsgSeqs().size();
			if (msgCount + nextPendingMsgCount > maxMsgCount) {
				break;
			}
			todos.add(m_pendingMessages.poll());
			msgCount += nextPendingMsgCount;
		}
	}

	if (!todos.isEmpty()) {
		Transaction catTx = Cat.newTransaction(CatConstants.TYPE_MESSAGE_BROKER_FLUSH, m_topic + "-" + m_partition);
		catTx.addData("count", msgCount);

		maxSavedSelectorOffset = appendMessageSync(todos);

		catTx.setStatus(Transaction.SUCCESS);
		catTx.complete();
	}

	return Math.max(maxPurgedSelectorOffset, maxSavedSelectorOffset);
}
 
Example 12
Source File: DefaultSendMessageResultMonitor.java    From hermes with Apache License 2.0 5 votes vote down vote up
private void tracking(SendMessageCommandV6 sendMessageCommand) {
	if (m_config.isCatEnabled()) {
		Transaction t = Cat.newTransaction(CatConstants.TYPE_MESSAGE_PRODUCE_ACKED, sendMessageCommand.getTopic());
		for (List<ProducerMessage<?>> msgs : sendMessageCommand.getProducerMessages()) {
			for (ProducerMessage<?> msg : msgs) {
				MessageTree tree = Cat.getManager().getThreadLocalMessageTree();

				String msgId = msg.getDurableSysProperty(CatConstants.SERVER_MESSAGE_ID);
				String parentMsgId = msg.getDurableSysProperty(CatConstants.CURRENT_MESSAGE_ID);
				String rootMsgId = msg.getDurableSysProperty(CatConstants.ROOT_MESSAGE_ID);

				tree.setMessageId(msgId);
				tree.setParentMessageId(parentMsgId);
				tree.setRootMessageId(rootMsgId);

				long duration = System.currentTimeMillis() - msg.getBornTime();
				String type = duration > 2000L ? CatConstants.TYPE_MESSAGE_PRODUCE_ELAPSE_LARGE
				      : CatConstants.TYPE_MESSAGE_PRODUCE_ELAPSE;
				CatUtil.logElapse(type, msg.getTopic(), msg.getBornTime(), 1,
				      Arrays.asList(new Pair<String, String>("key", msg.getKey())), Transaction.SUCCESS);
				ProducerStatusMonitor.INSTANCE.sendSuccess(msg.getTopic(), msg.getPartition(), duration);
			}
		}
		t.addData("*count", sendMessageCommand.getMessageCount());
		t.setStatus(Transaction.SUCCESS);
		t.complete();
	}
}
 
Example 13
Source File: CatInvokeTrace.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected void asyncClientRecv(TraceParam traceParam, RpcInvocation invocation) {
    ForkableTransaction forkableTransaction = (ForkableTransaction) traceParam.getAttachment(ASYNC_FORKABLE_TRANSACTION);

    ForkedTransaction forkedTransaction = null;
    Transaction transaction = null;
    try {
        if (forkableTransaction != null) {
            forkedTransaction = forkableTransaction.doFork();
        }
        transaction = Cat.newTransactionWithDuration(DORADO_CALL, traceParam.getSpanName(),
                System.currentTimeMillis() - traceParam.getStartTimestamp());
        clientLogEvent(traceParam, invocation);
        if (traceParam.getThrowable() != null) {
            Cat.logErrorWithCategory(DORADO_CALL + "." + traceParam.getRemoteAppkey(), traceParam.getThrowable());
            transaction.setStatus(traceParam.getThrowable());
        } else {
            transaction.setStatus(Transaction.SUCCESS);
        }

        transaction.addData(TRACE_ID, traceParam.getTraceId());
        TraceTimeline timeline = traceParam.getTraceTimeline();
        if (timeline.isEnable()) {
            transaction.addData(CatEventType.INVOKER_PHASECOST.type, timeline.genInvokerAllPhaseCost());
        }
    } catch (Exception e) {
        if (transaction != null) {
            Cat.logErrorWithCategory(DORADO_CALL + "." + traceParam.getRemoteAppkey(), e);
            transaction.setStatus(e);
        }
    } finally {
        if (transaction != null) {
            transaction.complete();
        }
        if (forkedTransaction != null) {
            forkedTransaction.close();
        }
    }
}
 
Example 14
Source File: CatInvokeTrace.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected void syncClientRecv(TraceParam traceParam, RpcInvocation invocation) {
    Transaction transaction = (Transaction) traceParam.getAttachment(TRANSACTION);
    if (transaction == null) {
        logger.warn("ClientSide: Request {} won't do cat report, cause no start transaction info.", traceParam.getSpanName());
        return;
    }
    try {
        clientLogEvent(traceParam, invocation);
        if (traceParam.getThrowable() != null) {
            Cat.logErrorWithCategory(DORADO_CALL + "." + traceParam.getRemoteAppkey(), traceParam.getThrowable());
            transaction.setStatus(traceParam.getThrowable());
        } else {
            transaction.setStatus(Transaction.SUCCESS);
        }

        transaction.addData(TRACE_ID, traceParam.getTraceId());
        TraceTimeline timeline = traceParam.getTraceTimeline();
        if (timeline.isEnable()) {
            transaction.addData(CatEventType.INVOKER_PHASECOST.type, timeline.genInvokerAllPhaseCost());
        }
    } catch (Exception e) {
        Cat.logErrorWithCategory(DORADO_CALL + "." + traceParam.getRemoteAppkey(), e);
        transaction.setStatus(e);
    } finally {
        transaction.complete();
    }
}
 
Example 15
Source File: BaseMessageListener.java    From hermes with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(List<ConsumerMessage<T>> msgs) {
	if (msgs != null && !msgs.isEmpty()) {
		String topic = msgs.get(0).getTopic();

		for (ConsumerMessage<T> msg : msgs) {
			Transaction t = Cat.newTransaction(CatConstants.TYPE_MESSAGE_CONSUMED, topic + ":" + m_groupId);
			MessageTree tree = Cat.getManager().getThreadLocalMessageTree();

			if (msg instanceof PropertiesHolderAware) {
				PropertiesHolder holder = ((PropertiesHolderAware) msg).getPropertiesHolder();
				String rootMsgId = holder.getDurableSysProperty(CatConstants.ROOT_MESSAGE_ID);
				String parentMsgId = holder.getDurableSysProperty(CatConstants.CURRENT_MESSAGE_ID);

				tree.setRootMessageId(rootMsgId);
				tree.setParentMessageId(parentMsgId);
			}

			try {
				t.addData("topic", topic);
				t.addData("key", msg.getRefKey());
				t.addData("groupId", m_groupId);

				setOnMessageStartTime(msg);
				onMessage(msg);
				setOnMessageEndTime(msg);
				// by design, if nacked, no effect
				msg.ack();

				String ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
				Cat.logEvent("Consumer:" + ip, msg.getTopic() + ":" + m_groupId, Event.SUCCESS, "key=" + msg.getRefKey());
				Cat.logEvent("Message:" + topic, "Consumed:" + ip, Event.SUCCESS, "key=" + msg.getRefKey());
				Cat.logMetricForCount(msg.getTopic());
				t.setStatus(MessageStatus.SUCCESS.equals(msg.getStatus()) ? Transaction.SUCCESS : "FAILED-WILL-RETRY");
			} catch (Exception e) {
				Cat.logError(e);
				t.setStatus(e);
				log.error("Exception occurred while calling onMessage.", e);
				msg.nack();
			} finally {
				t.complete();
			}
		}

	}
}
 
Example 16
Source File: BrokerMessageSender.java    From hermes with Apache License 2.0 4 votes vote down vote up
public void send() {
	boolean selectorSendSuccess = false;
	long cmdSelectorOffset = 0;
	SendMessageCommandV6 cmd = null;
	int batchSize = m_config.getBrokerSenderBatchSize();

	try {
		TaskQueue taskQueue = m_taskQueues.get(taskQueueKey);
		cmd = taskQueue.pop(batchSize);

		if (cmd != null) {
			selectorSendSuccess = true;
			cmdSelectorOffset = cmd.getSelectorOffset();

			if (!cmd.getProducerMessages().isEmpty()) {

				int produceTimeoutSeconds = m_config.getProduceTimeoutSeconds(cmd.getTopic());

				if (produceTimeoutSeconds > 0
				      && System.currentTimeMillis() - cmd.getBornTime() > produceTimeoutSeconds * 1000) {
					for (Pair<SettableFuture<SendResult>, ProducerMessage<?>> pair : cmd.getFutureAndMessagePair()) {
						MessageSendException exception = new MessageSendException("Send timeout.", pair.getValue());
						notifySendFail(pair.getKey(), exception);
					}

					Transaction t = Cat.newTransaction(CatConstants.TYPE_MESSAGE_PRODUCE_TIMEOUT, cmd.getTopic());
					t.addData("*count", cmd.getMessageCount());
					t.setStatus(Transaction.SUCCESS);
					t.complete();
				} else {
					SendMessageResult sendResult = sendMessagesToBroker(cmd);

					if (!sendResult.isSuccess() && !sendResult.isShouldSkip()) {
						selectorSendSuccess = false;
						taskQueue.push(cmd);
					}
					tracking(cmd, sendResult);
				}
			}
		}
	} catch (Exception e) {
		log.error("Unexpected exception when send cmd to broker", e);
		selectorSendSuccess = false;
	} finally {

		State state;
		if (selectorSendSuccess) {
			state = State.GotAndSuccessfullyProcessed;
		} else {
			if (cmd == null) {
				state = State.GotNothing;
			} else {
				state = State.GotButErrorInProcessing;
			}
		}
		TriggerResult triggerResult = new TriggerResult(state, new long[] { cmdSelectorOffset });

		m_selectorManager.reRegister(taskQueueKey, callbackContext, triggerResult, new FixedExpireTimeHolder(
		      Long.MAX_VALUE), new SelectorCallback() {

			@Override
			public void onReady(CallbackContext innerCtx) {
				new SendTask(taskQueueKey, innerCtx).send();
			}
		});
	}
}
 
Example 17
Source File: CatController.java    From javabase with Apache License 2.0 3 votes vote down vote up
/**
 * Transaction
 a).transaction适合记录跨越系统边界的程序访问行为,比如远程调用,数据库调用,也适合执行时间较长的业务逻辑监控
 b).某些运行期单元要花费一定时间完成工作, 内部需要其他处理逻辑协助, 我们定义为Transaction.
 c).Transaction可以嵌套(如http请求过程中嵌套了sql处理).
 d).大部分的Transaction可能会失败, 因此需要一个结果状态码.
 e).如果Transaction开始和结束之间没有其他消息产生, 那它就是Atomic Transaction(合并了起始标记).
 Event
 a). Event用来记录次数,表名单位时间内消息发生次数,比如记录系统异常,它和transaction相比缺少了时间的统计,开销比transaction要小
 
 type和name的组合要满足全局唯一性
    Transaction type有 "URL", "SQL", "Email", "Exec"等
    Event type有 "Info", "Warn", "Error"
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/transation", method = RequestMethod.GET)
public String transation() throws Exception {
	// Transaction的type
	Transaction t = Cat.newTransaction("TransactionTest", "findorder");
	// 3.Event Event用来记录次数,表名单位时间内消息发生次数,比如记录系统异常,它和transaction相比缺少了时间的统计,开销比transaction要小
	Cat.logEvent("info", "transation", Message.SUCCESS, "参数1");
	Cat.logEvent("error", "transation", Message.SUCCESS, "参数1");
	t.addData("Transaction测试");
	t.setStatus(Message.SUCCESS);
	t.complete();
	return "";
}