Search in sources :

Example 1 with UnitOfflineException

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

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

the class UnitRouter method firstInstance.

@Override
public UnitInstance firstInstance(String fullUnitName) throws UnitUndefinedException, UnitOfflineException {
    UnitInstance instance = localInstance(fullUnitName);
    if (instance != null)
        return instance;
    newestDefinition(fullUnitName);
    /*Constant.SYSTEM_DAO_GROUP_NAME.concat("."))) {
            List<UnitInstance> mappedInstances = allInstances(fullUnitName);
            instance = mappedInstances.isEmpty() ? null : mappedInstances.get(0);
        } else*/
    if (UnitDiscovery.singleton != null)
        instance = UnitDiscovery.singleton.firstInstance(fullUnitName);
    else {
    // service discovery(zk) is optional plugin, we shouldn't depend on it.
    // Service discovery is disabled currently, use local single node mode.
    }
    if (instance == null)
        throw new UnitOfflineException(fullUnitName);
    return instance;
}
Also used : UnitOfflineException(info.xiancloud.core.distribution.exception.UnitOfflineException) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance)

Example 3 with UnitOfflineException

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

the class UnitRouter method loadBalancedInstance.

public UnitInstance loadBalancedInstance(String unitFullName) throws UnitOfflineException, UnitUndefinedException {
    UnitInstance instance = localInstance(unitFullName);
    if (instance != null)
        return instance;
    newestDefinition(unitFullName);
    /*  do not delete this remark
        here is for old virtual dao unit, not used any more.
        if (unitFullName.startsWith(Constant.SYSTEM_DAO_GROUP_NAME.concat("."))) {
            List<UnitInstance> mappedInstances = allInstances(unitFullName);
            instance = mappedInstances.get(new Random().nextInt(mappedInstances.size()));
        } else
        */
    if (UnitDiscovery.singleton != null)
        instance = UnitDiscovery.singleton.lb(unitFullName);
    else {
    // service discovery(zk) is optional plugin, we shouldn't depend on it.
    // Service discovery is disabled currently, use local single node mode.
    }
    if (instance == null)
        throw new UnitOfflineException(unitFullName);
    return instance;
}
Also used : UnitOfflineException(info.xiancloud.core.distribution.exception.UnitOfflineException) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance)

Example 4 with UnitOfflineException

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

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

the class UnitRouter method allInstances.

/**
 * @return 返回包含指定服务的在线的客户端列表;
 * 注意:这里不再对结果做了排序,如果是由于一致性哈希算法的需要,则 Let the code which uses constant hash do the sorting。
 */
public List<UnitInstance> allInstances(String fullUnitName) throws UnitOfflineException, UnitUndefinedException {
    // for teasing.
    newestDefinition(fullUnitName);
    List<UnitInstance> instances;
    if (UnitDiscovery.singleton != null)
        instances = UnitDiscovery.singleton.all(fullUnitName);
    else {
        // service discovery(zk) is optional plugin, we shouldn't depend on it.
        // Service discovery is disabled currently, use local single node mode.
        instances = new ArrayList<>();
        instances.add(localInstance(fullUnitName));
    }
    if (instances.isEmpty())
        throw new UnitOfflineException(fullUnitName);
    /*instances.sort(Comparator.comparing(UnitInstance::getNodeId));
        * Let the code which uses constant hash do the sorting*/
    return instances;
}
Also used : UnitOfflineException(info.xiancloud.core.distribution.exception.UnitOfflineException) UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance)

Aggregations

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