Search in sources :

Example 1 with UnitInstance

use of info.xiancloud.core.distribution.service_discovery.UnitInstance 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 UnitInstance

use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.

the class UnitRegistrationBridge method unitInstance.

static UnitInstance unitInstance(UnitProxy unitProxy, NodeStatus nodeStatus) {
    UnitInstance unitInstance = new UnitInstance();
    unitInstance.setPayload(UnitProxy.create(unitProxy));
    unitInstance.setRegistrationTimestamp(nodeStatus.getInitTime());
    unitInstance.setPort(nodeStatus.getPort());
    unitInstance.setName(Unit.fullName(unitProxy));
    unitInstance.setEnabled(true);
    unitInstance.setAddress(nodeStatus.getHost());
    unitInstance.setUnitInstanceIdBean(new UnitInstanceIdBean(Unit.fullName(unitProxy), nodeStatus.getNodeId()));
    return unitInstance;
}
Also used : UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) UnitInstanceIdBean(info.xiancloud.core.distribution.service_discovery.UnitInstanceIdBean)

Example 3 with UnitInstance

use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.

the class UnitRegistrationBridge method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    UnitProxy unitProxy = msg.get("unit", UnitProxy.class);
    NodeStatus nodeStatus = msg.get("nodeStatus", NodeStatus.class);
    UnitInstance unitInstance = unitInstance(unitProxy, nodeStatus);
    try {
        UnitDiscovery.singleton.register(unitInstance);
        return UnitResponse.success();
    } catch (Exception e) {
        LOG.error(e);
        return UnitResponse.failure(null, "registration failed.");
    }
}
Also used : UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) UnitProxy(info.xiancloud.core.distribution.UnitProxy) NodeStatus(info.xiancloud.core.distribution.NodeStatus)

Example 4 with UnitInstance

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

use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.

the class UnitRouter method localInstance.

public UnitInstance localInstance(String fullUnitName) {
    Unit localUnit = LocalUnitsManager.getLocalUnit(fullUnitName);
    if (localUnit != null) {
        // 本地有,就直接用即可
        UnitInstance unitInstance = new UnitInstance();
        unitInstance.setPayload(UnitProxy.create(localUnit));
        unitInstance.setRegistrationTimestamp(LocalNodeManager.singleton.getSimpleStatus().getInitTime());
        unitInstance.setPort(Node.RPC_PORT);
        unitInstance.setName(fullUnitName);
        unitInstance.setEnabled(true);
        unitInstance.setAddress(EnvUtil.getLocalIp());
        unitInstance.setId(new UnitInstanceIdBean(fullUnitName, LocalNodeManager.LOCAL_NODE_ID).getUnitInstanceId());
        return unitInstance;
    }
    return null;
}
Also used : UnitInstance(info.xiancloud.core.distribution.service_discovery.UnitInstance) UnitInstanceIdBean(info.xiancloud.core.distribution.service_discovery.UnitInstanceIdBean) Unit(info.xiancloud.core.Unit)

Aggregations

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