Search in sources :

Example 61 with UnitResponse

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

the class App method main.

public static void main(String[] args) {
    Map<String, Object> params = new HashMap<>();
    params.put("content", "消息接口测试");
    UnitResponse result = SyncXian.call("wxcp", "wxcpMessage", params);
    System.out.println(result.toJSONString());
}
Also used : HashMap(java.util.HashMap) UnitResponse(info.xiancloud.core.message.UnitResponse)

Example 62 with UnitResponse

use of info.xiancloud.core.message.UnitResponse 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 63 with UnitResponse

use of info.xiancloud.core.message.UnitResponse 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)

Example 64 with UnitResponse

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

the class RpcClientUnitHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, JSONObject response) throws Exception {
    try {
        MsgIdHolder.set(response.getString("$msgId"));
        logRpcFly(response, ctx);
        String ssid = response.remove("$ssid").toString();
        NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
        LocalNodeManager.handleMap.invalidate(ssid);
        if (handler == null) {
            LOG.error(String.format("ssid=%s的消息没有找到对应的notifyHandler!整个消息内容=%s,", ssid, response), new Throwable());
            return;
        }
        UnitResponse responseUnitResponse = UnitResponse.create(response);
        ThreadPoolManager.execute(() -> handler.callback(responseUnitResponse));
    /*try {
                不再需要单独检测线程池被销毁而使用独立线程了,threadPoolManager已支持
            } catch (RejectedExecutionException threadPoolAlreadyShutdown) {
                LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
                new Thread(() -> handler.callback(responseUnitResponse)).start();
            }*/
    } catch (Throwable e) {
        LOG.error(e);
    } finally {
        MsgIdHolder.clear();
    }
}
Also used : UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler)

Example 65 with UnitResponse

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

the class App method pauseClusterService.

/**
 * 暂停服务更新
 */
static void pauseClusterService() {
    // description
    // Man.serial();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("clusterId", "cls-768pakpq");
    params.put("serviceName", "my-mongodb");
    UnitResponse result = SyncXian.call("qcloudContainerService", "pauseClusterService", params);
    System.out.println(result.getData().toString());
}
Also used : HashMap(java.util.HashMap) UnitResponse(info.xiancloud.core.message.UnitResponse) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

UnitResponse (info.xiancloud.core.message.UnitResponse)106 JSONObject (com.alibaba.fastjson.JSONObject)74 HashMap (java.util.HashMap)22 UnitRequest (info.xiancloud.core.message.UnitRequest)17 NotifyHandler (info.xiancloud.core.NotifyHandler)16 JSONArray (com.alibaba.fastjson.JSONArray)9 Unit (info.xiancloud.core.Unit)6 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 CountDownLatch (java.util.concurrent.CountDownLatch)5 Redis (info.xiancloud.cache.redis.Redis)4 CacheConfigBean (info.xiancloud.core.support.cache.CacheConfigBean)4 ThreadPoolManager (info.xiancloud.core.thread_pool.ThreadPoolManager)3 LOG (info.xiancloud.core.util.LOG)3 Set (java.util.Set)3 Test (org.junit.Test)3 MessageType (info.xiancloud.core.distribution.MessageType)2 UnitUndefinedException (info.xiancloud.core.distribution.exception.UnitUndefinedException)2