Add manual stop on schema diffing algorithm by gnawf · Pull Request #3119 · graphql-java/graphql-java · GitHub
Skip to content
Merged
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
16 changes: 10 additions & 6 deletions src/main/java/graphql/schema/diffing/DiffImpl.java
19 changes: 12 additions & 7 deletions src/main/java/graphql/schema/diffing/FillupIsolatedVertices.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@

@Internal
public class FillupIsolatedVertices {
private final SchemaDiffingRunningCheck runningCheck;

SchemaGraph sourceGraph;
SchemaGraph targetGraph;
IsolatedVertices isolatedVertices;
private final SchemaGraph sourceGraph;
private final SchemaGraph targetGraph;
private final IsolatedVertices isolatedVertices;

private BiMap<Vertex, Vertex> toRemove = HashBiMap.create();
private final BiMap<Vertex, Vertex> toRemove = HashBiMap.create();

static Map<String, List<VertexContextSegment>> typeContexts = new LinkedHashMap<>();
final static Map<String, List<VertexContextSegment>> typeContexts = new LinkedHashMap<>();

static {
typeContexts.put(SCHEMA, schemaContext());
Expand Down Expand Up @@ -710,7 +711,8 @@ public boolean filter(Vertex argument, SchemaGraph schemaGraph) {
}


public FillupIsolatedVertices(SchemaGraph sourceGraph, SchemaGraph targetGraph) {
public FillupIsolatedVertices(SchemaGraph sourceGraph, SchemaGraph targetGraph, SchemaDiffingRunningCheck runningCheck) {
this.runningCheck = runningCheck;
this.sourceGraph = sourceGraph;
this.targetGraph = targetGraph;
this.isolatedVertices = new IsolatedVertices();
Expand Down Expand Up @@ -830,6 +832,7 @@ private void calcPossibleMappingImpl(
Set<Vertex> usedSourceVertices,
Set<Vertex> usedTargetVertices,
String typeNameForDebug) {
runningCheck.check();

VertexContextSegment finalCurrentContext = contexts.get(contextIx);
Map<String, ImmutableList<Vertex>> sourceGroups = FpKit.filterAndGroupingBy(currentSourceVertices,
Expand Down Expand Up @@ -922,5 +925,7 @@ private void calcPossibleMappingImpl(
isolatedVertices.putPossibleMappings(possibleSourceVertices, possibleTargetVertices);
}


public IsolatedVertices getIsolatedVertices() {
return isolatedVertices;
}
}
23 changes: 15 additions & 8 deletions src/main/java/graphql/schema/diffing/SchemaDiffing.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@

@Internal
public class SchemaDiffing {


private final SchemaDiffingRunningCheck runningCheck = new SchemaDiffingRunningCheck();

SchemaGraph sourceGraph;
SchemaGraph targetGraph;

/**
* Tries to stop the algorithm from execution ASAP by throwing a
* {@link SchemaDiffingCancelledException}.
*/
public void stop() {
runningCheck.stop();
}

public List<EditOperation> diffGraphQLSchema(GraphQLSchema graphQLSchema1, GraphQLSchema graphQLSchema2) throws Exception {
sourceGraph = new SchemaGraphFactory("source-").createGraph(graphQLSchema1);
targetGraph = new SchemaGraphFactory("target-").createGraph(graphQLSchema2);
Expand All @@ -32,7 +39,7 @@ public EditOperationAnalysisResult diffAndAnalyze(GraphQLSchema graphQLSchema1,
targetGraph = new SchemaGraphFactory("target-").createGraph(graphQLSchema2);
DiffImpl.OptimalEdit optimalEdit = diffImpl(sourceGraph, targetGraph);
EditOperationAnalyzer editOperationAnalyzer = new EditOperationAnalyzer(graphQLSchema1, graphQLSchema1, sourceGraph, targetGraph);
return editOperationAnalyzer.analyzeEdits(optimalEdit.listOfEditOperations.get(0),optimalEdit.mappings.get(0));
return editOperationAnalyzer.analyzeEdits(optimalEdit.listOfEditOperations.get(0), optimalEdit.mappings.get(0));
}

public DiffImpl.OptimalEdit diffGraphQLSchemaAllEdits(GraphQLSchema graphQLSchema1, GraphQLSchema graphQLSchema2) throws Exception {
Expand All @@ -45,9 +52,9 @@ public DiffImpl.OptimalEdit diffGraphQLSchemaAllEdits(GraphQLSchema graphQLSchem
private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targetGraph) throws Exception {
int sizeDiff = targetGraph.size() - sourceGraph.size();
System.out.println("graph diff: " + sizeDiff);
FillupIsolatedVertices fillupIsolatedVertices = new FillupIsolatedVertices(sourceGraph, targetGraph);
FillupIsolatedVertices fillupIsolatedVertices = new FillupIsolatedVertices(sourceGraph, targetGraph, runningCheck);
fillupIsolatedVertices.ensureGraphAreSameSize();
FillupIsolatedVertices.IsolatedVertices isolatedVertices = fillupIsolatedVertices.isolatedVertices;
FillupIsolatedVertices.IsolatedVertices isolatedVertices = fillupIsolatedVertices.getIsolatedVertices();

assertTrue(sourceGraph.size() == targetGraph.size());
// if (sizeDiff != 0) {
Expand All @@ -60,7 +67,8 @@ private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targe
editorialCostForMapping(fixedMappings, sourceGraph, targetGraph, result);
return new DiffImpl.OptimalEdit(singletonList(fixedMappings), singletonList(result), result.size());
}
DiffImpl diffImpl = new DiffImpl(sourceGraph, targetGraph, isolatedVertices);

DiffImpl diffImpl = new DiffImpl(sourceGraph, targetGraph, isolatedVertices, runningCheck);
List<Vertex> nonMappedSource = new ArrayList<>(sourceGraph.getVertices());
nonMappedSource.removeAll(fixedMappings.getSources());
// for(Vertex vertex: nonMappedSource) {
Expand All @@ -74,6 +82,7 @@ private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targe
List<Vertex> nonMappedTarget = new ArrayList<>(targetGraph.getVertices());
nonMappedTarget.removeAll(fixedMappings.getTargets());

runningCheck.check();
sortListBasedOnPossibleMapping(nonMappedSource, isolatedVertices);

// the non mapped vertices go to the end
Expand Down Expand Up @@ -141,6 +150,4 @@ private List<EditOperation> calcEdgeOperations(Mapping mapping) {
}
return result;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package graphql.schema.diffing;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets make this Internal

import graphql.Internal;

@Internal
public class SchemaDiffingCancelledException extends RuntimeException {
SchemaDiffingCancelledException(boolean byInterrupt) {
super("Schema diffing job was cancelled by " + (byInterrupt ? "thread interrupt" : "stop call"));
}
}