Search in sources :

Example 1 with UnitUndefinedException

use of info.xiancloud.core.distribution.exception.UnitUndefinedException in project xian by happyyangyuan.

the class XhashSender method asyncSend.

@Override
protected void asyncSend() {
    String group = unitRequest.getContext().getGroup(), unit = unitRequest.getContext().getUnit();
    try {
        List<UnitInstance> unitInstances = UnitRouter.singleton.allInstances(Unit.fullName(group, unit));
        List<String> clientIds = new ArrayList<>();
        for (UnitInstance clientInfo : unitInstances) {
            clientIds.add(clientInfo.getNodeId());
        }
        String[] xhashNames = unitInstances.get(0).getPayload().getInput().getXhashNames();
        // sorting is for constant-hash requirement.
        Collections.sort(clientIds);
        String clientId = new Shard<>(clientIds).getShardInfo(xhashString(xhashNames));
        if (clientId.equals(LocalNodeManager.LOCAL_NODE_ID)) {
            new RoutedLocalAsyncSender(unitRequest, callback).send();
        } else {
            unitRequest.getContext().setDestinationNodeId(clientId);
            LocalNodeManager.send(unitRequest, callback);
        }
    } catch (UnitOfflineException | UnitUndefinedException e) {
        LOG.error("代码写错了吧? 进入xhashSender的前提就是unit在线!", e);
        callback.callback(UnitResponse.exception(e));
    }
}
Also used : RoutedLocalAsyncSender(info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender) UnitOfflineException(info.xiancloud.core.distribution.exception.UnitOfflineException) ArrayList(java.util.ArrayList) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) UnitUndefinedException(info.xiancloud.core.distribution.exception.UnitUndefinedException)

Example 2 with UnitUndefinedException

use of info.xiancloud.core.distribution.exception.UnitUndefinedException in project xian by happyyangyuan.

the class ReceiveAndBroadcast method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    if (msg.getContext().isRouted()) {
        return execute0(msg);
    } else {
        List<UnitInstance> list = new ArrayList<>();
        String application = msg.get("application", String.class);
        List<UnitInstance> unitInstances;
        try {
            unitInstances = UnitRouter.singleton.allInstances(Unit.fullName(getGroupName(), getUnitName()));
        } catch (UnitOfflineException | UnitUndefinedException e) {
            throw new RuntimeException(e);
        }
        if (StringUtil.isEmpty(application) || ALL.equals(application)) {
            list.addAll(unitInstances);
        } else {
            for (UnitInstance clientInfo : unitInstances) {
                if (clientInfo.getName().equals(msg.getString("application"))) {
                    list.add(clientInfo);
                }
            }
        }
        CountDownLatch latch = new CountDownLatch(list.size());
        List<Object> piledUpOutput = new ArrayList<>();
        for (UnitInstance clientInfo : list) {
            LocalNodeManager.send(new UnitRequest().setContext(RequestContext.create().setGroup(getGroupName()).setUnit(getUnitName()).setDestinationNodeId(clientInfo.getNodeId())), new NotifyHandler() {

                protected void handle(UnitResponse unitResponse) {
                    LOG.info("对" + clientInfo.getNodeId() + "执行" + getName() + "操作完毕");
                    if (!successDataOnly()) {
                        piledUpOutput.add(unitResponse);
                    } else if (unitResponse.succeeded()) {
                        piledUpOutput.add(unitResponse.getData());
                    }
                    latch.countDown();
                }
            });
        }
        if (async()) {
            return UnitResponse.success();
        } else {
            try {
                latch.await(timeoutInMilli(), TimeUnit.MILLISECONDS);
                return UnitResponse.success(piledUpOutput);
            } catch (InterruptedException e) {
                return UnitResponse.exception(e);
            }
        }
    }
}
Also used : UnitOfflineException(info.xiancloud.core.distribution.exception.UnitOfflineException) ArrayList(java.util.ArrayList) NotifyHandler(info.xiancloud.core.NotifyHandler) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) CountDownLatch(java.util.concurrent.CountDownLatch) UnitUndefinedException(info.xiancloud.core.distribution.exception.UnitUndefinedException) UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse)

Example 3 with UnitUndefinedException

use of info.xiancloud.core.distribution.exception.UnitUndefinedException 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 4 with UnitUndefinedException

use of info.xiancloud.core.distribution.exception.UnitUndefinedException in project xian by happyyangyuan.

the class AbstractLocalAsyncSender method asyncSend.

@Override
protected void asyncSend() {
    ThreadPoolManager.execute(() -> {
        /*IdManager.makeSureMsgId(unitRequest.getContext()); No need, because super asyncSender has made sure msg id.*/
        UnitResponse unitResponse;
        long start = System.nanoTime();
        Unit unit = LocalUnitsManager.getLocalUnit(unitRequest.getContext().getGroup(), unitRequest.getContext().getUnit());
        if (unit == null) {
            unitResponse = new UnitUndefinedException(unitRequest.getContext().getGroup(), unitRequest.getContext().getUnit()).toUnitResponse();
        } else {
            Set<Input.Obj> required = getRequired(unit, unitRequest);
            if (!required.isEmpty()) {
                String[] requiredParamNames = required.stream().map(Input.Obj::getName).collect(Collectors.toList()).toArray(new String[0]);
                LackParamException lackParamException = new LackParamException(unitRequest.getContext().getGroup(), unitRequest.getContext().getUnit(), requiredParamNames);
                unitResponse = UnitResponse.error(Group.CODE_LACK_OF_PARAMETER, lackParamException.getLacedParams(), lackParamException.getMessage());
            } else {
                try {
                    unitResponse = unit.execute(unitRequest);
                } catch (Throwable anyExceptionCaughtHere) {
                    unitResponse = UnitResponse.exception(anyExceptionCaughtHere);
                }
                if (unitResponse == null) {
                    unitResponse = UnitResponse.failure(null, "Null response is returned from: " + Unit.fullName(unitRequest.getContext().getGroup(), unitRequest.getContext().getUnit()));
                    LOG.error(unitResponse);
                }
            }
        }
        fillResponseContext(unitResponse.getContext());
        UnitResponse finalResponse = unitResponse;
        long cost = (System.nanoTime() - start) / 1000000;
        JSONObject logJson = new JSONObject() {

            {
                put("group", unitRequest.getContext().getGroup());
                put("unit", unitRequest.getContext().getUnit());
                put(Constant.COST, cost);
                put("unitRequest", originalMap);
                put("unitResponse", finalResponse);
                put("type", "unit");
            }
        };
        LOG.info(logJson.toJSONString());
        callback.callback(finalResponse);
    });
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse) LackParamException(info.xiancloud.core.message.LackParamException) UnitUndefinedException(info.xiancloud.core.distribution.exception.UnitUndefinedException)

Aggregations

UnitUndefinedException (info.xiancloud.core.distribution.exception.UnitUndefinedException)4 UnitOfflineException (info.xiancloud.core.distribution.exception.UnitOfflineException)3 UnitInstance (info.xiancloud.core.distribution.service_discovery.UnitInstance)3 UnitRequest (info.xiancloud.core.message.UnitRequest)2 UnitResponse (info.xiancloud.core.message.UnitResponse)2 ArrayList (java.util.ArrayList)2 JSONObject (com.alibaba.fastjson.JSONObject)1 NotifyHandler (info.xiancloud.core.NotifyHandler)1 LackParamException (info.xiancloud.core.message.LackParamException)1 RoutedLocalAsyncSender (info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender)1 CountDownLatch (java.util.concurrent.CountDownLatch)1