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