Skip to content

Commit

Permalink
remove node scorer service invoke (#4659)
Browse files Browse the repository at this point in the history
  • Loading branch information
peacewong authored Jun 19, 2023
1 parent 4e75276 commit 466f398
Showing 1 changed file with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.linkis.manager.label.entity.Feature;
import org.apache.linkis.manager.label.entity.InheritableLabel;
import org.apache.linkis.manager.label.entity.Label;
import org.apache.linkis.manager.label.score.LabelScoreServiceInstance;
import org.apache.linkis.manager.label.score.NodeLabelScorer;
import org.apache.linkis.manager.label.service.NodeLabelService;
import org.apache.linkis.manager.label.utils.LabelUtils;
Expand Down Expand Up @@ -359,62 +360,49 @@ private Map<ScoreServiceInstance, List<Label<?>>> getScoredNodeMapsByLabels(
instances = instanceLabels.keySet();
}

Map<ScoreServiceInstance, List<Label<?>>> matchInstancesAndLabels = new HashMap<>();

// Get the out-degree relations ( Node -> Label )
Map<ServiceInstance, List<PersistenceLabel>> outNodeDegree =
labelManagerPersistence.getLabelRelationsByServiceInstance(new ArrayList<>(instances));
// outNodeDegree cannot be empty
if (!outNodeDegree.isEmpty()) {
Set<String> necessaryLabelKeys =
necessaryLabels.stream().map(PersistenceLabel::getLabelKey).collect(Collectors.toSet());

// Rebuild in-degree relations
inNodeDegree.clear();
List<ServiceInstance> removeNodes = new ArrayList<>();
outNodeDegree.forEach(
(node, iLabels) -> {
// The core tag must be exactly the same
if (!necessaryLabels.isEmpty()) {
Set<String> coreLabelKeys =
iLabels.stream()
.map(ManagerUtils::persistenceLabelToRealLabel)
.filter(l -> l.getFeature() == Feature.CORE)
.map(Label::getLabelKey)
.collect(Collectors.toSet());
if (necessaryLabelKeys.containsAll(coreLabelKeys)
&& coreLabelKeys.size() == necessaryLabelKeys.size()) {
iLabels.forEach(
label -> {
inNodeDegree.computeIfAbsent(label, (k) -> new ArrayList<>()).add(node);
});
} else {
removeNodes.add(node);
if (null == necessaryLabels || necessaryLabels.isEmpty()) {
outNodeDegree.forEach(
(node, iLabels) -> {
matchInstancesAndLabels.put(
new LabelScoreServiceInstance(node),
iLabels.stream().map(label -> (Label<?>) label).collect(Collectors.toList()));
});
} else {
outNodeDegree.forEach(
(node, iLabels) -> {
// The core tag must be exactly the same
if (!necessaryLabels.isEmpty()) {
Set<String> coreLabelKeys =
iLabels.stream()
.map(ManagerUtils::persistenceLabelToRealLabel)
.filter(l -> l.getFeature() == Feature.CORE)
.map(Label::getLabelKey)
.collect(Collectors.toSet());
if (necessaryLabelKeys.containsAll(coreLabelKeys)
&& coreLabelKeys.size() == necessaryLabelKeys.size()) {
matchInstancesAndLabels.put(
new LabelScoreServiceInstance(node),
iLabels.stream().map(label -> (Label<?>) label).collect(Collectors.toList()));
}
}
}
});

// Remove nodes with mismatched labels
if (!removeNodes.isEmpty() && removeNodes.size() == outNodeDegree.size()) {
logger.info(
"The entered labels {} do not match the labels of the node itself", necessaryLabels);
});
}

removeNodes.forEach(outNodeDegree::remove);
Map<ScoreServiceInstance, List<PersistenceLabel>> calculate =
nodeLabelScorer.calculate(inNodeDegree, outNodeDegree, labels);

Map<ScoreServiceInstance, List<Label<?>>> resultMap =
calculate.entrySet().stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
e ->
e.getValue().stream()
.map(label -> (Label<?>) label)
.collect(Collectors.toList())));

return resultMap;
}
return new HashMap<>();
// Remove nodes with mismatched labels
if (matchInstancesAndLabels.isEmpty()) {
logger.info(
"The entered labels {} do not match the labels of the node itself", necessaryLabels);
}
return matchInstancesAndLabels;
}

private int tryToAddLabel(PersistenceLabel persistenceLabel) {
Expand Down

0 comments on commit 466f398

Please sign in to comment.