Skip to content

Commit 4b4c0e7

Browse files
authored
Merge pull request #3119 from gnawf/add-diffing-stopper
Add manual stop on algorithm
2 parents aa46bc2 + 53e8416 commit 4b4c0e7

5 files changed

Lines changed: 64 additions & 21 deletions

File tree

‎src/main/java/graphql/schema/diffing/DiffImpl.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
@Internal
2323
public class DiffImpl {
2424

25-
private static MappingEntry LAST_ELEMENT = new MappingEntry();
26-
private SchemaGraph completeSourceGraph;
27-
private SchemaGraph completeTargetGraph;
28-
private FillupIsolatedVertices.IsolatedVertices isolatedVertices;
25+
private static final MappingEntry LAST_ELEMENT = new MappingEntry();
26+
private final SchemaGraph completeSourceGraph;
27+
private final SchemaGraph completeTargetGraph;
28+
private final FillupIsolatedVertices.IsolatedVertices isolatedVertices;
29+
private final SchemaDiffingRunningCheck runningCheck;
2930

3031
private static class MappingEntry {
3132
public boolean siblingsFinished;
@@ -67,14 +68,14 @@ public OptimalEdit(List<Mapping> mappings, List<List<EditOperation>> listOfEditO
6768
}
6869
}
6970

70-
public DiffImpl(SchemaGraph completeSourceGraph, SchemaGraph completeTargetGraph, FillupIsolatedVertices.IsolatedVertices isolatedVertices) {
71+
public DiffImpl(SchemaGraph completeSourceGraph, SchemaGraph completeTargetGraph, FillupIsolatedVertices.IsolatedVertices isolatedVertices, SchemaDiffingRunningCheck runningCheck) {
7172
this.completeSourceGraph = completeSourceGraph;
7273
this.completeTargetGraph = completeTargetGraph;
7374
this.isolatedVertices = isolatedVertices;
75+
this.runningCheck = runningCheck;
7476
}
7577

7678
OptimalEdit diffImpl(Mapping startMapping, List<Vertex> relevantSourceList, List<Vertex> relevantTargetList) throws Exception {
77-
7879
int graphSize = relevantSourceList.size();
7980

8081
ArrayList<EditOperation> initialEditOperations = new ArrayList<>();
@@ -122,7 +123,10 @@ OptimalEdit diffImpl(Mapping startMapping, List<Vertex> relevantSourceList, List
122123
relevantTargetList
123124
);
124125
}
126+
127+
runningCheck.check();
125128
}
129+
126130
return optimalEdit;
127131
}
128132

‎src/main/java/graphql/schema/diffing/FillupIsolatedVertices.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@
4141

4242
@Internal
4343
public class FillupIsolatedVertices {
44+
private final SchemaDiffingRunningCheck runningCheck;
4445

45-
SchemaGraph sourceGraph;
46-
SchemaGraph targetGraph;
47-
IsolatedVertices isolatedVertices;
46+
private final SchemaGraph sourceGraph;
47+
private final SchemaGraph targetGraph;
48+
private final IsolatedVertices isolatedVertices;
4849

49-
private BiMap<Vertex, Vertex> toRemove = HashBiMap.create();
50+
private final BiMap<Vertex, Vertex> toRemove = HashBiMap.create();
5051

51-
static Map<String, List<VertexContextSegment>> typeContexts = new LinkedHashMap<>();
52+
final static Map<String, List<VertexContextSegment>> typeContexts = new LinkedHashMap<>();
5253

5354
static {
5455
typeContexts.put(SCHEMA, schemaContext());
@@ -710,7 +711,8 @@ public boolean filter(Vertex argument, SchemaGraph schemaGraph) {
710711
}
711712

712713

713-
public FillupIsolatedVertices(SchemaGraph sourceGraph, SchemaGraph targetGraph) {
714+
public FillupIsolatedVertices(SchemaGraph sourceGraph, SchemaGraph targetGraph, SchemaDiffingRunningCheck runningCheck) {
715+
this.runningCheck = runningCheck;
714716
this.sourceGraph = sourceGraph;
715717
this.targetGraph = targetGraph;
716718
this.isolatedVertices = new IsolatedVertices();
@@ -830,6 +832,7 @@ private void calcPossibleMappingImpl(
830832
Set<Vertex> usedSourceVertices,
831833
Set<Vertex> usedTargetVertices,
832834
String typeNameForDebug) {
835+
runningCheck.check();
833836

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

925-
928+
public IsolatedVertices getIsolatedVertices() {
929+
return isolatedVertices;
930+
}
926931
}

‎src/main/java/graphql/schema/diffing/SchemaDiffing.java‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@
1515

1616
@Internal
1717
public class SchemaDiffing {
18-
19-
18+
private final SchemaDiffingRunningCheck runningCheck = new SchemaDiffingRunningCheck();
2019

2120
SchemaGraph sourceGraph;
2221
SchemaGraph targetGraph;
2322

23+
/**
24+
* Tries to stop the algorithm from execution ASAP by throwing a
25+
* {@link SchemaDiffingCancelledException}.
26+
*/
27+
public void stop() {
28+
runningCheck.stop();
29+
}
30+
2431
public List<EditOperation> diffGraphQLSchema(GraphQLSchema graphQLSchema1, GraphQLSchema graphQLSchema2) throws Exception {
2532
sourceGraph = new SchemaGraphFactory("source-").createGraph(graphQLSchema1);
2633
targetGraph = new SchemaGraphFactory("target-").createGraph(graphQLSchema2);
@@ -32,7 +39,7 @@ public EditOperationAnalysisResult diffAndAnalyze(GraphQLSchema graphQLSchema1,
3239
targetGraph = new SchemaGraphFactory("target-").createGraph(graphQLSchema2);
3340
DiffImpl.OptimalEdit optimalEdit = diffImpl(sourceGraph, targetGraph);
3441
EditOperationAnalyzer editOperationAnalyzer = new EditOperationAnalyzer(graphQLSchema1, graphQLSchema1, sourceGraph, targetGraph);
35-
return editOperationAnalyzer.analyzeEdits(optimalEdit.listOfEditOperations.get(0),optimalEdit.mappings.get(0));
42+
return editOperationAnalyzer.analyzeEdits(optimalEdit.listOfEditOperations.get(0), optimalEdit.mappings.get(0));
3643
}
3744

3845
public DiffImpl.OptimalEdit diffGraphQLSchemaAllEdits(GraphQLSchema graphQLSchema1, GraphQLSchema graphQLSchema2) throws Exception {
@@ -45,9 +52,9 @@ public DiffImpl.OptimalEdit diffGraphQLSchemaAllEdits(GraphQLSchema graphQLSchem
4552
private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targetGraph) throws Exception {
4653
int sizeDiff = targetGraph.size() - sourceGraph.size();
4754
System.out.println("graph diff: " + sizeDiff);
48-
FillupIsolatedVertices fillupIsolatedVertices = new FillupIsolatedVertices(sourceGraph, targetGraph);
55+
FillupIsolatedVertices fillupIsolatedVertices = new FillupIsolatedVertices(sourceGraph, targetGraph, runningCheck);
4956
fillupIsolatedVertices.ensureGraphAreSameSize();
50-
FillupIsolatedVertices.IsolatedVertices isolatedVertices = fillupIsolatedVertices.isolatedVertices;
57+
FillupIsolatedVertices.IsolatedVertices isolatedVertices = fillupIsolatedVertices.getIsolatedVertices();
5158

5259
assertTrue(sourceGraph.size() == targetGraph.size());
5360
// if (sizeDiff != 0) {
@@ -60,7 +67,8 @@ private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targe
6067
editorialCostForMapping(fixedMappings, sourceGraph, targetGraph, result);
6168
return new DiffImpl.OptimalEdit(singletonList(fixedMappings), singletonList(result), result.size());
6269
}
63-
DiffImpl diffImpl = new DiffImpl(sourceGraph, targetGraph, isolatedVertices);
70+
71+
DiffImpl diffImpl = new DiffImpl(sourceGraph, targetGraph, isolatedVertices, runningCheck);
6472
List<Vertex> nonMappedSource = new ArrayList<>(sourceGraph.getVertices());
6573
nonMappedSource.removeAll(fixedMappings.getSources());
6674
// for(Vertex vertex: nonMappedSource) {
@@ -74,6 +82,7 @@ private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targe
7482
List<Vertex> nonMappedTarget = new ArrayList<>(targetGraph.getVertices());
7583
nonMappedTarget.removeAll(fixedMappings.getTargets());
7684

85+
runningCheck.check();
7786
sortListBasedOnPossibleMapping(nonMappedSource, isolatedVertices);
7887

7988
// the non mapped vertices go to the end
@@ -141,6 +150,4 @@ private List<EditOperation> calcEdgeOperations(Mapping mapping) {
141150
}
142151
return result;
143152
}
144-
145-
146153
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package graphql.schema.diffing;
2+
3+
import graphql.Internal;
4+
5+
@Internal
6+
public class SchemaDiffingCancelledException extends RuntimeException {
7+
SchemaDiffingCancelledException(boolean byInterrupt) {
8+
super("Schema diffing job was cancelled by " + (byInterrupt ? "thread interrupt" : "stop call"));
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package graphql.schema.diffing;
2+
3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
5+
class SchemaDiffingRunningCheck {
6+
private final AtomicBoolean wasStopped = new AtomicBoolean(false);
7+
8+
void check() {
9+
if (wasStopped.get()) {
10+
throw new SchemaDiffingCancelledException(false);
11+
}
12+
}
13+
14+
void stop() {
15+
wasStopped.set(true);
16+
}
17+
}

0 commit comments

Comments
 (0)