private void insertOrAppend() { Date now = DateUtil.now(); DubboMethodDayIP day = new DubboMethodDayIP( "LZY5", "com.lvmama.test.soa.monitor.DubboMethodDayIPServiceTest", "testInsertOrAppend", "111.333.555.777", "222.444.666.888", DateUtil.trimToDay(now)); day.setSuccessTimes(10L); day.setFailTimes(1L); day.setElapsedAvg(BigDecimal.valueOf(2L)); day.setElapsedMax(5L); day.setSuccessTimesDetail( DateUtil.HHmm(now) + " " + day.getSuccessTimes() + StringUtil.getLineSeparator()); day.setFailTimesDetail( DateUtil.HHmm(now) + " " + day.getFailTimes() + StringUtil.getLineSeparator()); day.setElapsedTotalDetail( DateUtil.HHmm(now) + " " + day.getElapsedAvg().multiply(BigDecimal.valueOf(day.getSuccessTimes())) + StringUtil.getLineSeparator()); day.setElapsedMaxDetail( DateUtil.HHmm(now) + " " + day.getElapsedMax() + StringUtil.getLineSeparator()); for (int i = 1; i <= 10; i++) { dubboMethodDayIPService.insertOrAppend(day); dubboMethodDayIPDao.insert(day); } }
public static void updateProviderCache(DubboMethodMinuteIP minute) { String appName = minute.getAppName(); String service = minute.getService(); String consumerIP = minute.getConsumerIP(); String providerIP = minute.getProviderIP(); Date time = DateUtil.trimToDay(minute.getTime()); String key = "ServiceDayIPCache.updateProviderCache_" + appName + "_" + service + "_" + consumerIP + "_" + providerIP + "_" + DateUtil.format(time); AtomicReference<DubboServiceDayIP> dayReference = null; try { providerLock.readLock().lock(); dayReference = PROVIDER_CACHE.get(key); } finally { providerLock.readLock().unlock(); } if (dayReference == null) { dayReference = new AtomicReference<DubboServiceDayIP>(); DubboServiceDayIP day = new DubboServiceDayIP(appName, service, consumerIP, providerIP, time); day.setElapsedAvg(BigDecimal.ZERO); day.setFailTimes(0L); day.setSuccessTimes(0L); day.setElapsedMax(0L); dayReference.set(day); try { providerLock.writeLock().lock(); if (PROVIDER_CACHE.get(key) == null) { PROVIDER_CACHE.put(key, dayReference); } else { dayReference = PROVIDER_CACHE.get(key); } } finally { providerLock.writeLock().unlock(); } } while (true) { DubboServiceDayIP dayOld = dayReference.get(); DubboServiceDayIP dayUpdated = new DubboServiceDayIP(appName, service, consumerIP, providerIP, time); if (minute.getSuccessTimes() != null && minute.getSuccessTimes() != 0) { dayUpdated.setElapsedAvg( (dayOld .getElapsedAvg() .multiply(new BigDecimal(dayOld.getSuccessTimes())) .add(minute.getElapsedTotal())) .divide( new BigDecimal(dayOld.getSuccessTimes() + minute.getSuccessTimes()), 4, BigDecimal.ROUND_HALF_UP)); dayUpdated.setSuccessTimes(dayOld.getSuccessTimes() + minute.getSuccessTimes()); } else { dayUpdated.setElapsedAvg(dayOld.getElapsedAvg()); } if (minute.getSuccessTimes() != null) { dayUpdated.setSuccessTimes(dayOld.getSuccessTimes() + minute.getSuccessTimes()); } else { dayUpdated.setSuccessTimes(dayOld.getSuccessTimes()); } if (minute.getFailTimes() != null) { dayUpdated.setFailTimes(dayOld.getFailTimes() + minute.getFailTimes()); } else { dayUpdated.setFailTimes(dayOld.getFailTimes()); } if (minute.getElapsedMax() != null) { dayUpdated.setElapsedMax(Math.max(dayOld.getElapsedMax(), minute.getElapsedMax())); } else { dayUpdated.setElapsedMax(dayOld.getElapsedMax()); } dayUpdated.setElapsedMaxDetail( DubboDetailUtil.mergeDetailToStr( dayOld.getElapsedMaxDetail(), DateUtil.HHmm(minute.getTime()) + " " + minute.getElapsedMax(), true)); dayUpdated.setElapsedTotalDetail( DubboDetailUtil.mergeDetailToStr( dayOld.getElapsedTotalDetail(), DateUtil.HHmm(minute.getTime()) + " " + minute.getElapsedTotal(), false)); dayUpdated.setSuccessTimesDetail( DubboDetailUtil.mergeDetailToStr( dayOld.getSuccessTimesDetail(), DateUtil.HHmm(minute.getTime()) + " " + minute.getSuccessTimes(), false)); dayUpdated.setFailTimesDetail( DubboDetailUtil.mergeDetailToStr( dayOld.getFailTimesDetail(), DateUtil.HHmm(minute.getTime()) + " " + minute.getFailTimes(), false)); if (dayReference.compareAndSet(dayOld, dayUpdated)) { break; } } }