Search in sources :

Example 16 with UnitRequest

use of info.xiancloud.core.message.UnitRequest in project xian by happyyangyuan.

the class AbstractBodyNotRequiredAsyncForwarder method bodyParams.

@Override
protected UnitRequest bodyParams(String body, Map<String, String> headerIgnored) throws ReqBodyParseFailure {
    JSONObject fromBodyMap = parseBody(body);
    UnitRequest controllerRequest = UnitRequest.create();
    controllerRequest.setArgMap(fromBodyMap);
    return controllerRequest;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitRequest(info.xiancloud.core.message.UnitRequest)

Example 17 with UnitRequest

use of info.xiancloud.core.message.UnitRequest in project xian by happyyangyuan.

the class CachedGlobalHttpSessionCountMonitor method execute0.

@Override
public Object execute0() {
    int totalSessionCount = 0;
    try {
        for (UnitInstance clientInfo : UnitRouter.singleton.allInstances(Unit.fullName("httpServer", "cachedLocalHttpSessionMonitor"))) {
            int localSessionCount;
            UnitRequest request = UnitRequest.create("httpServer", "cachedLocalHttpSessionMonitor");
            request.getContext().setDestinationNodeId(clientInfo.getNodeId());
            List<Integer> counts = SyncXian.call(request, 5 * 1000).dataToTypedList(Integer.class);
            totalSessionCount = MathUtil.sum(counts);
        }
    } catch (UnitOfflineException | UnitUndefinedException e) {
        LOG.error(e);
        totalSessionCount = -1;
    }
    LOG.info("当前缓存的session总数量为:" + totalSessionCount);
    return UnitResponse.success(totalSessionCount);
}
Also used : UnitOfflineException(info.xiancloud.core.distribution.exception.UnitOfflineException) UnitRequest(info.xiancloud.core.message.UnitRequest) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) UnitUndefinedException(info.xiancloud.core.distribution.exception.UnitUndefinedException)

Example 18 with UnitRequest

use of info.xiancloud.core.message.UnitRequest in project xian by happyyangyuan.

the class AppTransaction method commitAndCloseTogether.

@Override
public void commitAndCloseTogether() throws SQLException {
    // 发送远程请求,将其他数据库事务提交和关闭
    JSONArray clientIds = TransactionalCache.clear();
    if (clientIds != null) {
        for (int i = 0; i < clientIds.size(); i++) {
            final String clientId = clientIds.getString(i);
            if (!LocalNodeManager.LOCAL_NODE_ID.equals(clientId)) {
                UnitRequest request = UnitRequest.create(CommitAndCloseTogetherUnit.class);
                request.getContext().setDestinationNodeId(clientId);
                LocalNodeManager.send(request, new NotifyHandler() {

                    protected void handle(UnitResponse unitResponse) {
                        LOG.info(clientId + "的事务已经提交");
                    }
                });
            }
        }
    }
    // 提交和关闭自己的数据库事务
    connection.commit();
    connection.close();
    // 清除本地事务
    localTransMap.remove(transactionId);
}
Also used : UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) JSONArray(com.alibaba.fastjson.JSONArray) NotifyHandler(info.xiancloud.core.NotifyHandler)

Example 19 with UnitRequest

use of info.xiancloud.core.message.UnitRequest in project xian by happyyangyuan.

the class JedisTestDistributedLock method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    int number = msg.get("number", int.class, 10000);
    final CountDownLatch startCountDownLatch = new CountDownLatch(1);
    final CountDownLatch finishCountDownLatch = new CountDownLatch(number);
    final List<Long> consumeTimes = new CopyOnWriteArrayList<>();
    for (int i = 0; i < number; i++) {
        int _i = i;
        ThreadPoolManager.execute(() -> {
            try {
                startCountDownLatch.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            long startTime = System.nanoTime();
            // Redis Lock
            DistributedLockSynchronizer.call("QPS_" + _i, 3, () -> {
                return null;
            }, 3);
            // ZK Lock
            // try
            // {
            // Synchronizer.call("QPS_" + _i, () -> {
            // return null;
            // }, 3L);
            // }
            // catch (Exception e)
            // {
            // LOG.error(e);
            // }
            finishCountDownLatch.countDown();
            long endTime = System.nanoTime();
            consumeTimes.add(endTime - startTime);
        });
    }
    try {
        startCountDownLatch.countDown();
        finishCountDownLatch.await();
    } catch (Exception e) {
        LOG.error(e);
    }
    long totalConsumeTime = 0;
    for (long consumeTime : consumeTimes) totalConsumeTime += consumeTime;
    long avgConsumeTime = totalConsumeTime / number;
    LongSummaryStatistics intSummaryStatistics = consumeTimes.stream().collect(Collectors.summarizingLong(value -> value));
    String log = String.format("QPS, 加锁解锁, 任务数量: %s, 累计耗时: %s, %s, 平均耗时:%s, %s, %s", number, totalConsumeTime, totalConsumeTime / 1000000, avgConsumeTime, avgConsumeTime / 1000000, intSummaryStatistics);
    LOG.info(log);
    UnitResponse unitResponseObject = SyncXian.call("cache", "cacheKeys", new JSONObject() {

        {
            put("pattern", "LOCK_QPS_*");
        }
    });
    if (unitResponseObject.succeeded() && unitResponseObject.getData() != null) {
        Set<String> keys = unitResponseObject.getData();
        LOG.info(String.format("分布锁剩余数量: %s", keys.size()));
    }
    SyncXian.call("diyMonitor", "jedisLockMonitor", new JSONObject() {

        {
        }
    });
    return UnitResponse.success(log);
}
Also used : Group(info.xiancloud.core.Group) Unit(info.xiancloud.core.Unit) SyncXian(info.xiancloud.core.message.SyncXian) UnitRequest(info.xiancloud.core.message.UnitRequest) CacheGroup(info.xiancloud.cache.service.CacheGroup) Set(java.util.Set) UnitResponse(info.xiancloud.core.message.UnitResponse) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Input(info.xiancloud.core.Input) ThreadPoolManager(info.xiancloud.core.thread_pool.ThreadPoolManager) DistributedLockSynchronizer(info.xiancloud.core.support.cache.lock.DistributedLockSynchronizer) JSONObject(com.alibaba.fastjson.JSONObject) LOG(info.xiancloud.core.util.LOG) UnitMeta(info.xiancloud.core.UnitMeta) LongSummaryStatistics(java.util.LongSummaryStatistics) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LongSummaryStatistics(java.util.LongSummaryStatistics) JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 20 with UnitRequest

use of info.xiancloud.core.message.UnitRequest in project xian by happyyangyuan.

the class CacheSetAddUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.get("key", String.class);
    Set members = (Set) msg.getArgMap().get("members");
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        Long result = 0L;
        if (members != null && !members.isEmpty()) {
            result = Redis.call(cacheConfigBean, jedis -> {
                String[] _members = new String[members.size()];
                Iterator<Object> iterator = members.iterator();
                int i = 0;
                while (iterator.hasNext()) {
                    _members[i] = FormatUtil.formatValue(iterator.next());
                    i++;
                }
                return jedis.sadd(key, _members);
            });
        }
        return UnitResponse.success(result);
    } catch (Throwable e) {
        return UnitResponse.exception(e);
    }
}
Also used : Group(info.xiancloud.core.Group) Unit(info.xiancloud.core.Unit) Input(info.xiancloud.core.Input) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean) Iterator(java.util.Iterator) Redis(info.xiancloud.cache.redis.Redis) UnitRequest(info.xiancloud.core.message.UnitRequest) CacheGroup(info.xiancloud.cache.service.CacheGroup) Set(java.util.Set) FormatUtil(info.xiancloud.cache.redis.util.FormatUtil) UnitResponse(info.xiancloud.core.message.UnitResponse) UnitMeta(info.xiancloud.core.UnitMeta) Set(java.util.Set) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean) Iterator(java.util.Iterator)

Aggregations

UnitRequest (info.xiancloud.core.message.UnitRequest)24 UnitResponse (info.xiancloud.core.message.UnitResponse)17 JSONObject (com.alibaba.fastjson.JSONObject)9 NotifyHandler (info.xiancloud.core.NotifyHandler)8 Unit (info.xiancloud.core.Unit)7 UnitMeta (info.xiancloud.core.UnitMeta)6 CacheGroup (info.xiancloud.cache.service.CacheGroup)5 Group (info.xiancloud.core.Group)5 Input (info.xiancloud.core.Input)5 JSONArray (com.alibaba.fastjson.JSONArray)4 Redis (info.xiancloud.cache.redis.Redis)4 CacheConfigBean (info.xiancloud.core.support.cache.CacheConfigBean)4 UnitInstance (info.xiancloud.core.distribution.service_discovery.UnitInstance)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 MessageType (info.xiancloud.core.distribution.MessageType)2 UnitOfflineException (info.xiancloud.core.distribution.exception.UnitOfflineException)2 UnitUndefinedException (info.xiancloud.core.distribution.exception.UnitUndefinedException)2 DefaultLocalAsyncSender (info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender)2 ThreadPoolManager (info.xiancloud.core.thread_pool.ThreadPoolManager)2 LOG (info.xiancloud.core.util.LOG)2