Skip to content

NCL-7800 Build Artifact dependency graph #4242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* JBoss, Home of Professional Open Source.
* Copyright 2014-2022 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.pnc.common.graph;

import lombok.extern.slf4j.Slf4j;
import org.jboss.pnc.common.util.TriConsumer;
import org.jboss.util.graph.Graph;
import org.jboss.util.graph.Vertex;

import java.util.Collection;
import java.util.function.Function;

/**
* @author Patrik Korytár <[email protected]>
*/
@Slf4j
public class WeightedGraphBuilder<T, S> {

private Function<S, T> nodeSupplier;

private Function<T, Collection<VertexNeighbor<S>>> dependencySupplier;

private Function<T, Collection<VertexNeighbor<S>>> dependantSupplier;

public WeightedGraphBuilder(
Function<S, T> nodeSupplier,
Function<T, Collection<VertexNeighbor<S>>> dependencySupplier,
Function<T, Collection<VertexNeighbor<S>>> dependantSupplier) {
this.nodeSupplier = nodeSupplier;
this.dependencySupplier = dependencySupplier;
this.dependantSupplier = dependantSupplier;
}

private T getNode(S id) {
return nodeSupplier.apply(id);
}

private Collection<VertexNeighbor<S>> getDependencies(T node) {
return dependencySupplier.apply(node);
}

private Collection<VertexNeighbor<S>> getDependants(T node) {
return dependantSupplier.apply(node);
}

public void buildGraph(Graph<T> graph, S nodeId, int depthLimit) {
buildDependencyGraph(graph, nodeId, depthLimit);
buildDependantGraph(graph, nodeId, depthLimit);
}

private void buildDependencyGraph(Graph<T> graph, S nodeId, int depthLimit) {
buildGraphNode(graph, nodeId, depthLimit, this::getDependencies, (vertex, dependencyVertex, cost) -> {
log.trace("Creating new dependency edge from {} to {}.", vertex, dependencyVertex);
graph.addEdge(vertex, dependencyVertex, cost);
});
}

private void buildDependantGraph(Graph<T> graph, S nodeId, int depthLimit) {
buildGraphNode(graph, nodeId, depthLimit, this::getDependants, (vertex, dependantVertex, cost) -> {
log.trace("Creating new dependant edge from {} to {}.", dependantVertex, vertex);
graph.addEdge(dependantVertex, vertex, cost);
});
}

private Vertex<T> buildGraphNode(
Graph<T> graph,
S nodeId,
int depthLimit,
Function<T, Collection<VertexNeighbor<S>>> neighborSupplier,
TriConsumer<Vertex<T>, Vertex<T>, Integer> edgeAdder) {
T node = getNode(nodeId);
Vertex<T> vertex = getOrCreateGraphVertex(graph, nodeId, node);

if (depthLimit <= 0) {
return vertex;
}

for (VertexNeighbor<S> vertexNeighbor : neighborSupplier.apply(node)) {
S neighborId = vertexNeighbor.getNeighborId();

Vertex<T> neighborVertex = getGraphVertex(graph, neighborId);
if (neighborVertex == null) {
neighborVertex = buildGraphNode(graph, neighborId, depthLimit - 1, neighborSupplier, edgeAdder);
}

edgeAdder.accept(vertex, neighborVertex, vertexNeighbor.getCost());
}

return vertex;
}

private Vertex<T> getOrCreateGraphVertex(Graph<T> graph, S nodeId, T node) {
Vertex<T> vertex = getGraphVertex(graph, nodeId);
if (vertex == null) {
vertex = new NameUniqueVertex<>(nodeId.toString(), node);
graph.addVertex(vertex);
}

return vertex;
}

private Vertex<T> getGraphVertex(Graph<T> graph, S nodeId) {
return graph.findVertexByName(nodeId.toString());
}
}
26 changes: 26 additions & 0 deletions common/src/main/java/org/jboss/pnc/common/util/TriConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* JBoss, Home of Professional Open Source.
* Copyright 2014-2022 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.pnc.common.util;

/**
* @author Patrik Korytár &lt;[email protected]&gt;
*/
@FunctionalInterface
public interface TriConsumer<T, U, V> {
void accept(T t, U u, V v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.jboss.pnc.api.enums.AlignmentPreference;
import org.jboss.pnc.datastore.repositories.internal.AbstractRepository;
import org.jboss.pnc.model.Artifact;
import org.jboss.pnc.model.Artifact_;
import org.jboss.pnc.model.Base32LongID;
import org.jboss.pnc.model.BuildConfigSetRecord_;
import org.jboss.pnc.model.BuildConfigurationAudited;
Expand All @@ -41,10 +43,15 @@
import javax.inject.Inject;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Date;
Expand Down Expand Up @@ -316,6 +323,41 @@ public List<Base32LongID> queryIdsWithPredicates(Predicate<BuildRecord>... predi
return queryIdsWithPredicates((r) -> r.get(BuildRecord_.id), predicates);
}

public List<Tuple> getImplicitDependencies(Base32LongID buildId) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> query = cb.createQuery(Tuple.class);

Root<BuildRecord> builds = query.from(BuildRecord.class);
Join<BuildRecord, Artifact> dependencyArtifacts = builds.join(BuildRecord_.dependencies);
Join<Artifact, BuildRecord> dependencyBuilds = dependencyArtifacts.join(Artifact_.buildRecord);

query.multiselect(
dependencyBuilds.get(BuildRecord_.id),
cb.countDistinct(dependencyArtifacts.get(Artifact_.id)).as(Integer.class));
query.where(cb.equal(builds.get(BuildRecord_.id), buildId));
query.groupBy(dependencyBuilds.get(BuildRecord_.id));

return entityManager.createQuery(query).getResultList();
}

@Override
public List<Tuple> getImplicitDependants(Base32LongID buildId) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> query = cb.createQuery(Tuple.class);

Root<BuildRecord> builds = query.from(BuildRecord.class);
Join<BuildRecord, Artifact> builtArtifacts = builds.join(BuildRecord_.builtArtifacts);
Join<Artifact, BuildRecord> dependentBuilds = builtArtifacts.join(Artifact_.dependantBuildRecords);

query.multiselect(
dependentBuilds.get(BuildRecord_.id),
cb.countDistinct(builtArtifacts.get(Artifact_.id)).as(Integer.class));
query.where(cb.equal(builds.get(BuildRecord_.id), buildId));
query.groupBy(dependentBuilds.get(BuildRecord_.id));

return entityManager.createQuery(query).getResultList();
}

@Override
protected void joinFetch(Root<BuildRecord> root) {
root.fetch(BuildRecord_.user, JoinType.LEFT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ public void initiliazeBuildRecordDemoData() {
.executionRootVersion("7.0.3")
.temporaryBuild(false)
.dependency(importedArtifact1)
.dependency(builtArtifact10)
.build();

nextId = Sequence.nextBase32Id();
Expand Down
2 changes: 2 additions & 0 deletions dto/src/main/java/org/jboss/pnc/dto/response/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* Edge in graph of objects.
Expand All @@ -32,6 +33,7 @@
@Getter
@EqualsAndHashCode
@AllArgsConstructor
@ToString
@Builder(builderClassName = "Builder")
@JsonDeserialize(builder = Edge.Builder.class)
public class Edge<T> {
Expand Down
2 changes: 2 additions & 0 deletions dto/src/main/java/org/jboss/pnc/dto/response/Vertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* Vertex in graph of objects.
Expand All @@ -32,6 +33,7 @@
@Getter
@EqualsAndHashCode
@AllArgsConstructor
@ToString
@Builder(builderClassName = "Builder")
@JsonDeserialize(builder = Vertex.Builder.class)
public class Vertex<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.pnc.auth.KeycloakServiceClient;
import org.jboss.pnc.common.graph.VertexNeighbor;
import org.jboss.pnc.common.graph.WeightedGraphBuilder;
import org.jboss.pnc.common.scm.ScmException;
import org.jboss.pnc.common.graph.GraphBuilder;
import org.jboss.pnc.common.graph.GraphUtils;
Expand Down Expand Up @@ -77,6 +79,7 @@
import org.jboss.pnc.spi.exception.MissingDataException;
import org.jboss.pnc.spi.exception.RemoteRequestException;
import org.jboss.pnc.spi.exception.ValidationException;
import org.jboss.util.graph.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -85,6 +88,7 @@
import javax.ejb.EJBAccessException;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.Tuple;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -747,6 +751,38 @@ public Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(
return new Page<>(pageIndex, pageSize, totalPages, count, content);
}

@Override
public Graph<Build> getImplicitDependencyGraph(String buildId, Integer depthLimit) {
org.jboss.util.graph.Graph<Build> implicitDependencyGraph = createImplicitDependencyGraph(buildId, depthLimit);

return GraphDtoBuilder.from(implicitDependencyGraph, Build.class, Vertex::getData);
}

private org.jboss.util.graph.Graph<Build> createImplicitDependencyGraph(String buildId, Integer depthLimit) {
var graph = new org.jboss.util.graph.Graph<Build>();
var graphBuilder = new WeightedGraphBuilder<Build, String>(this::getSpecific, node -> {
var tuples = buildRecordRepository
.getImplicitDependencies(buildMapper.getIdMapper().toEntity(node.getId()));
return parseVertexNeighborsFromTuples(tuples);
}, node -> {
var tuples = buildRecordRepository.getImplicitDependants(buildMapper.getIdMapper().toEntity(node.getId()));
return parseVertexNeighborsFromTuples(tuples);
});

graphBuilder.buildGraph(graph, buildId, depthLimit);
return graph;
}

private List<VertexNeighbor<String>> parseVertexNeighborsFromTuples(List<Tuple> tuples) {
return tuples.stream()
.map(
tuple -> VertexNeighbor.<String> builder()
.neighborId(tuple.get(0, Base32LongID.class).toString())
.cost(tuple.get(1, Integer.class))
.build())
.collect(Collectors.toList());
}

private DefaultPageInfo toPageInfo(BuildPageInfo buildPageInfo) {
return new DefaultPageInfo(
buildPageInfo.getPageIndex() * buildPageInfo.getPageSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.jboss.pnc.dto.response.Page;
import org.jboss.pnc.dto.response.RunningBuildCount;
import org.jboss.pnc.dto.response.SSHCredentials;
import org.jboss.pnc.enums.BuildStatus;
import org.jboss.pnc.facade.validation.EmptyEntityException;
import org.jboss.pnc.model.Base32LongID;

Expand Down Expand Up @@ -116,4 +115,6 @@ Page<BuildRecordInsights> getAllBuildRecordInsightsNewerThanTimestamp(
int pageIndex,
int pageSize,
Date lastupdatetime);

Graph<Build> getImplicitDependencyGraph(String buildId, Integer depthLimit);
}
Loading