-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathNeo4jGraphService.java
More file actions
417 lines (347 loc) · 15.5 KB
/
Neo4jGraphService.java
File metadata and controls
417 lines (347 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package com.linkedin.metadata.graph.neo4j;
import com.codahale.metrics.Timer;
import com.datahub.util.Statement;
import com.datahub.util.exception.RetryLimitReached;
import com.google.common.collect.ImmutableMap;
import com.linkedin.common.urn.Urn;
import com.linkedin.metadata.graph.Edge;
import com.linkedin.metadata.graph.GraphService;
import com.linkedin.metadata.models.registry.LineageRegistry;
import com.linkedin.metadata.graph.RelatedEntitiesResult;
import com.linkedin.metadata.graph.RelatedEntity;
import com.linkedin.metadata.query.filter.Condition;
import com.linkedin.metadata.query.filter.ConjunctiveCriterionArray;
import com.linkedin.metadata.query.filter.CriterionArray;
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.query.filter.RelationshipDirection;
import com.linkedin.metadata.query.filter.RelationshipFilter;
import com.linkedin.metadata.utils.metrics.MetricUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.exceptions.Neo4jException;
@Slf4j
public class Neo4jGraphService implements GraphService {
private static final int MAX_TRANSACTION_RETRY = 3;
private final LineageRegistry _lineageRegistry;
private final Driver _driver;
private SessionConfig _sessionConfig;
public Neo4jGraphService(@Nonnull LineageRegistry lineageRegistry, @Nonnull Driver driver) {
this(lineageRegistry, driver, SessionConfig.defaultConfig());
}
public Neo4jGraphService(@Nonnull LineageRegistry lineageRegistry, @Nonnull Driver driver, @Nonnull SessionConfig sessionConfig) {
this._lineageRegistry = lineageRegistry;
this._driver = driver;
this._sessionConfig = sessionConfig;
}
@Override
public LineageRegistry getLineageRegistry() {
return _lineageRegistry;
}
public void addEdge(@Nonnull final Edge edge) {
log.debug(String.format("Adding Edge source: %s, destination: %s, type: %s",
edge.getSource(),
edge.getDestination(),
edge.getRelationshipType()));
final String sourceType = edge.getSource().getEntityType();
final String destinationType = edge.getDestination().getEntityType();
final List<Statement> statements = new ArrayList<>();
// Add/Update source & destination node first
statements.add(getOrInsertNode(edge.getSource()));
statements.add(getOrInsertNode(edge.getDestination()));
// Add/Update relationship
final String mergeRelationshipTemplate =
"MATCH (source:%s {urn: $sourceUrn}),(destination:%s {urn: $destinationUrn}) MERGE (source)-[r:%s]->(destination) SET r = $properties";
final String statement =
String.format(mergeRelationshipTemplate, sourceType, destinationType, edge.getRelationshipType());
final Map<String, Object> paramsMerge = new HashMap<>();
paramsMerge.put("sourceUrn", edge.getSource().toString());
paramsMerge.put("destinationUrn", edge.getDestination().toString());
paramsMerge.put("properties", new HashMap<>());
statements.add(buildStatement(statement, paramsMerge));
executeStatements(statements);
}
@Nonnull
public RelatedEntitiesResult findRelatedEntities(
@Nullable final List<String> sourceTypes,
@Nonnull final Filter sourceEntityFilter,
@Nullable final List<String> destinationTypes,
@Nonnull final Filter destinationEntityFilter,
@Nonnull final List<String> relationshipTypes,
@Nonnull final RelationshipFilter relationshipFilter,
final int offset,
final int count) {
log.debug(
String.format("Finding related Neo4j nodes sourceType: %s, sourceEntityFilter: %s, destinationType: %s, ",
sourceTypes, sourceEntityFilter, destinationTypes)
+ String.format(
"destinationEntityFilter: %s, relationshipTypes: %s, relationshipFilter: %s, ",
destinationEntityFilter, relationshipTypes, relationshipFilter)
+ String.format(
"offset: %s, count: %s",
offset, count)
);
if (sourceTypes != null && sourceTypes.isEmpty() || destinationTypes != null && destinationTypes.isEmpty()) {
return new RelatedEntitiesResult(offset, 0, 0, Collections.emptyList());
}
final String srcCriteria = filterToCriteria(sourceEntityFilter).trim();
final String destCriteria = filterToCriteria(destinationEntityFilter).trim();
final String edgeCriteria = relationshipFilterToCriteria(relationshipFilter);
final RelationshipDirection relationshipDirection = relationshipFilter.getDirection();
String matchTemplate = "MATCH (src %s)-[r%s %s]-(dest %s)%s";
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate = "MATCH (src %s)<-[r%s %s]-(dest %s)%s";
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate = "MATCH (src %s)-[r%s %s]->(dest %s)%s";
}
final String returnNodes = String.format("RETURN dest, type(r)"); // Return both related entity and the relationship type.
final String returnCount = "RETURN count(*)"; // For getting the total results.
String relationshipTypeFilter = "";
if (relationshipTypes.size() > 0) {
relationshipTypeFilter = ":" + StringUtils.join(relationshipTypes, "|");
}
String whereClause = computeEntityTypeWhereClause(sourceTypes, destinationTypes);
// Build Statement strings
String baseStatementString =
String.format(matchTemplate, srcCriteria, relationshipTypeFilter, edgeCriteria, destCriteria, whereClause);
log.info(baseStatementString);
final String resultStatementString = String.format("%s %s SKIP $offset LIMIT $count", baseStatementString, returnNodes);
final String countStatementString = String.format("%s %s", baseStatementString, returnCount);
// Build Statements
final Statement resultStatement = new Statement(resultStatementString, ImmutableMap.of("offset", offset, "count", count));
final Statement countStatement = new Statement(countStatementString, Collections.emptyMap());
// Execute Queries
final List<RelatedEntity> relatedEntities = runQuery(resultStatement).list(record ->
new RelatedEntity(
record.values().get(1).asString(), // Relationship Type
record.values().get(0).asNode().get("urn").asString())); // Urn TODO: Validate this works against Neo4j.
final int totalCount = runQuery(countStatement).single().get(0).asInt();
return new RelatedEntitiesResult(offset, relatedEntities.size(), totalCount, relatedEntities);
}
private String computeEntityTypeWhereClause(@Nonnull final List<String> sourceTypes,
@Nonnull final List<String> destinationTypes) {
String whereClause = "";
Boolean hasSourceTypes = sourceTypes != null && !sourceTypes.isEmpty();
Boolean hasDestTypes = destinationTypes != null && !destinationTypes.isEmpty();
if (hasSourceTypes && hasDestTypes) {
whereClause = String.format(" WHERE %s AND %s",
sourceTypes.stream().map(type -> "src:" + type).collect(Collectors.joining(" OR ")),
destinationTypes.stream().map(type -> "dest:" + type).collect(Collectors.joining(" OR "))
);
} else if (hasSourceTypes) {
whereClause = String.format(" WHERE %s",
sourceTypes.stream().map(type -> "src:" + type).collect(Collectors.joining(" OR "))
);
} else if (hasDestTypes) {
whereClause = String.format(" WHERE %s",
destinationTypes.stream().map(type -> "dest:" + type).collect(Collectors.joining(" OR "))
);
}
return whereClause;
}
public void removeNode(@Nonnull final Urn urn) {
log.debug(String.format("Removing Neo4j node with urn: %s", urn));
// also delete any relationship going to or from it
final String matchTemplate = "MATCH (node {urn: $urn}) DETACH DELETE node";
final String statement = String.format(matchTemplate);
final Map<String, Object> params = new HashMap<>();
params.put("urn", urn.toString());
runQuery(buildStatement(statement, params)).consume();
}
public void removeEdgesFromNode(
@Nonnull final Urn urn,
@Nonnull final List<String> relationshipTypes,
@Nonnull final RelationshipFilter relationshipFilter) {
log.debug(String.format("Removing Neo4j edge types from node with urn: %s, types: %s, filter: %s",
urn,
relationshipTypes,
relationshipFilter));
// also delete any relationship going to or from it
final RelationshipDirection relationshipDirection = relationshipFilter.getDirection();
String matchTemplate = "MATCH (src {urn: $urn})-[r%s]-(dest) DELETE r";
if (relationshipDirection == RelationshipDirection.INCOMING) {
matchTemplate = "MATCH (src {urn: $urn})<-[r%s]-(dest) DELETE r";
} else if (relationshipDirection == RelationshipDirection.OUTGOING) {
matchTemplate = "MATCH (src {urn: $urn})-[r%s]->(dest) DELETE r";
}
String relationshipTypeFilter = "";
if (relationshipTypes.size() > 0) {
relationshipTypeFilter = ":" + StringUtils.join(relationshipTypes, "|");
}
final String statement = String.format(matchTemplate, relationshipTypeFilter);
final Map<String, Object> params = new HashMap<>();
params.put("urn", urn.toString());
runQuery(buildStatement(statement, params)).consume();
}
public void removeNodesMatchingLabel(@Nonnull String labelPattern) {
log.debug(String.format("Removing Neo4j nodes matching label %s", labelPattern));
final String matchTemplate =
"MATCH (n) WHERE any(l IN labels(n) WHERE l=~'%s') DETACH DELETE n";
final String statement = String.format(matchTemplate, labelPattern);
final Map<String, Object> params = new HashMap<>();
runQuery(buildStatement(statement, params)).consume();
}
@Override
public void configure() {
// Do nothing
}
@Override
public void clear() {
removeNodesMatchingLabel(".*");
}
// visible for testing
@Nonnull
Statement buildStatement(@Nonnull String queryTemplate, @Nonnull Map<String, Object> params) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String k = entry.getKey();
Object v = entry.getValue();
params.put(k, toPropertyValue(v));
}
return new Statement(queryTemplate, params);
}
@Nonnull
private Object toPropertyValue(@Nonnull Object obj) {
if (obj instanceof Urn) {
return obj.toString();
}
return obj;
}
@AllArgsConstructor
@Data
private static final class ExecutionResult {
private long tookMs;
private int retries;
}
/**
* Executes a list of statements with parameters in one transaction.
*
* @param statements List of statements with parameters to be executed in order
*/
private synchronized ExecutionResult executeStatements(@Nonnull List<Statement> statements) {
int retry = 0;
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
Exception lastException;
try (final Session session = _driver.session(_sessionConfig)) {
do {
try {
session.writeTransaction(tx -> {
for (Statement statement : statements) {
tx.run(statement.getCommandText(), statement.getParams());
}
return 0;
});
lastException = null;
break;
} catch (Neo4jException e) {
lastException = e;
}
} while (++retry <= MAX_TRANSACTION_RETRY);
}
if (lastException != null) {
throw new RetryLimitReached(
"Failed to execute Neo4j write transaction after " + MAX_TRANSACTION_RETRY + " retries", lastException);
}
stopWatch.stop();
return new ExecutionResult(stopWatch.getTime(), retry);
}
/**
* Runs a query statement with parameters and return StatementResult.
*
* @param statement a statement with parameters to be executed
* @return list of elements in the query result
*/
@Nonnull
private Result runQuery(@Nonnull Statement statement) {
log.debug(String.format("Running Neo4j query %s", statement.toString()));
try (Timer.Context ignored = MetricUtils.timer(this.getClass(), "runQuery").time()) {
return _driver.session(_sessionConfig).run(statement.getCommandText(), statement.getParams());
}
}
@Nonnull
private static String toCriterionWhereString(@Nonnull String key, @Nonnull Object value) {
if (ClassUtils.isPrimitiveOrWrapper(value.getClass())) {
return key + " = " + value;
}
return key + " = \"" + value.toString() + "\"";
}
// Returns "key:value" String, if value is not primitive, then use toString() and double quote it
@Nonnull
private static String toCriterionString(@Nonnull String key, @Nonnull Object value) {
if (ClassUtils.isPrimitiveOrWrapper(value.getClass())) {
return key + ":" + value;
}
return key + ":\"" + value.toString() + "\"";
}
/**
* Converts {@link RelationshipFilter} to neo4j query criteria, filter criterion condition requires to be EQUAL.
*
* @param filter Query relationship filter
* @return Neo4j criteria string
*/
@Nonnull
private static String relationshipFilterToCriteria(@Nonnull RelationshipFilter filter) {
return disjunctionToCriteria(filter.getOr());
}
/**
* Converts {@link Filter} to neo4j query criteria, filter criterion condition requires to be EQUAL.
*
* @param filter Query Filter
* @return Neo4j criteria string
*/
@Nonnull
private static String filterToCriteria(@Nonnull Filter filter) {
return disjunctionToCriteria(filter.getOr());
}
private static String disjunctionToCriteria(final ConjunctiveCriterionArray disjunction) {
if (disjunction.size() > 1) {
// TODO: Support disjunctions (ORs).
throw new UnsupportedOperationException("Neo4j query filter only supports 1 set of conjunction criteria");
}
final CriterionArray criterionArray = disjunction.size() > 0 ? disjunction.get(0).getAnd() : new CriterionArray();
return criterionToString(criterionArray);
}
/**
* Converts {@link CriterionArray} to neo4j query string.
*
* @param criterionArray CriterionArray in a Filter
* @return Neo4j criteria string
*/
@Nonnull
private static String criterionToString(@Nonnull CriterionArray criterionArray) {
if (!criterionArray.stream().allMatch(criterion -> Condition.EQUAL.equals(criterion.getCondition()))) {
throw new RuntimeException("Neo4j query filter only support EQUAL condition " + criterionArray);
}
final StringJoiner joiner = new StringJoiner(",", "{", "}");
criterionArray.forEach(criterion -> joiner.add(toCriterionString(criterion.getField(), criterion.getValue())));
return joiner.length() <= 2 ? "" : joiner.toString();
}
/**
* Gets Node based on Urn, if not exist, creates placeholder node.
*/
@Nonnull
private Statement getOrInsertNode(@Nonnull Urn urn) {
final String nodeType = urn.getEntityType();
final String mergeTemplate = "MERGE (node:%s {urn: $urn}) RETURN node";
final String statement = String.format(mergeTemplate, nodeType);
final Map<String, Object> params = new HashMap<>();
params.put("urn", urn.toString());
return buildStatement(statement, params);
}
}