Search in sources :

Example 1 with RoutedLocalAsyncSender

use of info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender 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 RoutedLocalAsyncSender

use of info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender in project xian by happyyangyuan.

the class BroadcastSender method asyncSend.

@Override
protected void asyncSend() throws UnitOfflineException, UnitUndefinedException {
    List<UnitInstance> list = UnitRouter.singleton.allInstances(Unit.fullName(unitRequest.getContext().getGroup(), unitRequest.getContext().getUnit()));
    UnitMeta.Broadcast broadcast = list.get(0).getPayload().getMeta().getBroadcast();
    final CountDownLatch latch = new CountDownLatch(list.size());
    Collection<Object> piledUpOutput = new ConcurrentLinkedQueue<>();
    final NotifyHandler tmpHandler = new NotifyHandler() {

        protected void handle(UnitResponse output) {
            if (!broadcast.isSuccessDataOnly()) {
                piledUpOutput.add(output);
            } else if (output.succeeded()) {
                piledUpOutput.add(output.getData());
            }
            latch.countDown();
        }
    };
    for (UnitInstance unitInstance : list) {
        if (unitInstance.getNodeId().equals(LocalNodeManager.LOCAL_NODE_ID)) {
            // we must not share the same unitRequest object while sending request concurrently.
            UnitRequest clonedUnitRequest = CloneUtil.cloneBean(unitRequest, UnitRequest.class);
            /*clonedUnitRequest.getContext().setDestinationNodeId(unitInstance.getNodeId()); no need  */
            new RoutedLocalAsyncSender(clonedUnitRequest, tmpHandler).send();
        } else {
            LOG.debug("In order not to share the same unit request object,we clone a new request");
            UnitRequest clonedRequest = CloneUtil.cloneBean(unitRequest, UnitRequest.class);
            clonedRequest.getContext().setDestinationNodeId(unitInstance.getNodeId());
            LocalNodeManager.send(clonedRequest, tmpHandler);
        }
    }
    if (broadcast.isAsync()) {
        callback.callback(UnitResponse.success());
    } else {
        try {
            if (!latch.await(broadcast.getTimeoutInMilli(), TimeUnit.MILLISECONDS)) {
                LOG.error(new TimeoutException());
                callback.callback(UnitResponse.error(Group.CODE_TIME_OUT, piledUpOutput, "Time out while waiting for all the units to response, the data is only part of the result. "));
            } else {
                callback.callback(UnitResponse.success(piledUpOutput));
            }
        } catch (InterruptedException e) {
            callback.callback(UnitResponse.exception(e));
        }
    }
}
Also used : RoutedLocalAsyncSender(info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender) NotifyHandler(info.xiancloud.core.NotifyHandler) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) UnitMeta(info.xiancloud.core.UnitMeta) CountDownLatch(java.util.concurrent.CountDownLatch) UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

UnitInstance (info.xiancloud.core.distribution.service_discovery.UnitInstance)2 RoutedLocalAsyncSender (info.xiancloud.core.message.sender.local.RoutedLocalAsyncSender)2 NotifyHandler (info.xiancloud.core.NotifyHandler)1 UnitMeta (info.xiancloud.core.UnitMeta)1 UnitOfflineException (info.xiancloud.core.distribution.exception.UnitOfflineException)1 UnitUndefinedException (info.xiancloud.core.distribution.exception.UnitUndefinedException)1 UnitRequest (info.xiancloud.core.message.UnitRequest)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 ArrayList (java.util.ArrayList)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1