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));
}
}
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;
}
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;
}
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);
}
}
}
}
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;
}
Aggregations