Skip to content

Commit 4675104

Browse files
rakhiagrRakhi Agrawal
andauthored
[EGG Migration] NPE fix for dual read (#556)
* Bug fix * Checkstyle --------- Co-authored-by: Rakhi Agrawal <rakagrawal@linkedin.com>
1 parent 08935b6 commit 4675104

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

restli-resources/src/main/java/com/linkedin/metadata/restli/BaseAspectRoutingResource.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ protected Task<VALUE> get(@Nonnull KEY id, @QueryParam(PARAM_ASPECTS) @Optional
119119
// The assumption is main GMS must have this entity.
120120
// If entity only has routing aspect, resourceNotFoundException will be thrown.
121121
final URN urn = toUrn(id);
122+
BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO = getShadowReadLocalDAO();
122123
if (!getLocalDAO().exists(urn)) {
123-
if (getShadowReadLocalDAO() != null && getShadowReadLocalDAO().exists(urn)) {
124+
if (shadowReadLocalDAO != null && shadowReadLocalDAO.exists(urn)) {
124125
// If the entity exists in shadow DAO, we can return an empty value.
125126
log.warn("Entity {} exists in shadow DAO but not in local DAO. Ignoring shadow-only data.", urn);
126127
}
@@ -571,15 +572,16 @@ private List<INTERNAL_ASPECT_UNION> getInternalAspectsFromLocalDao(URN urn, Set<
571572
.map(aspectClass -> new AspectKey<>(aspectClass, urn, LATEST_VERSION))
572573
.collect(Collectors.toSet());
573574

574-
if (getShadowReadLocalDAO() == null) {
575+
BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO = getShadowReadLocalDAO();
576+
if (shadowReadLocalDAO == null) {
575577
return getLocalDAO().get(keys)
576578
.values()
577579
.stream()
578580
.filter(java.util.Optional::isPresent)
579581
.map(aspect -> ModelUtils.newAspectUnion(_internalAspectUnionClass, aspect.get()))
580582
.collect(Collectors.toList());
581583
}
582-
return getInternalAspectsWithShadowComparison(keys);
584+
return getInternalAspectsWithShadowComparison(keys, shadowReadLocalDAO);
583585
}
584586

585587
/**
@@ -588,12 +590,13 @@ private List<INTERNAL_ASPECT_UNION> getInternalAspectsFromLocalDao(URN urn, Set<
588590
* @param keys Aspect keys to be retrieved from shadow DAO
589591
* @return A list of internal aspects.
590592
*/
591-
private List<INTERNAL_ASPECT_UNION> getInternalAspectsWithShadowComparison(Set<AspectKey<URN, ? extends RecordTemplate>> keys) {
593+
private List<INTERNAL_ASPECT_UNION> getInternalAspectsWithShadowComparison(
594+
Set<AspectKey<URN, ? extends RecordTemplate>> keys, BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO) {
592595

593596
Map<AspectKey<URN, ? extends RecordTemplate>, java.util.Optional<? extends RecordTemplate>> localResults =
594597
getLocalDAO().get(keys);
595598
Map<AspectKey<URN, ? extends RecordTemplate>, java.util.Optional<? extends RecordTemplate>> shadowResults =
596-
getShadowReadLocalDAO().get(keys);
599+
shadowReadLocalDAO.get(keys);
597600

598601
return keys.stream()
599602
.map(key -> {

restli-resources/src/main/java/com/linkedin/metadata/restli/BaseEntityResource.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ protected Task<VALUE> get(@Nonnull KEY id, @QueryParam(PARAM_ASPECTS) @Optional
234234

235235
return RestliUtils.toTask(() -> {
236236
final URN urn = toUrn(id);
237+
BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO = getShadowReadLocalDAO();
237238
if (!getLocalDAO().exists(urn)) {
238-
if (getShadowReadLocalDAO() != null && getShadowReadLocalDAO().exists(urn)) {
239+
if (shadowReadLocalDAO != null && shadowReadLocalDAO.exists(urn)) {
239240
log.warn("Entity {} exists in shadow DAO but not in local DAO. Ignoring shadow-only data.", urn);
240241
}
241242
throw RestliUtils.resourceNotFoundException();
@@ -571,7 +572,8 @@ public Task<ASSET> getAsset(@ActionParam(PARAM_URN) @Nonnull String urnString,
571572
return RestliUtils.toTask(() -> {
572573
final URN urn = parseUrnParam(urnString);
573574

574-
if (getShadowReadLocalDAO() == null) {
575+
BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO = getShadowReadLocalDAO();
576+
if (shadowReadLocalDAO == null) {
575577
if (!getLocalDAO().exists(urn)) {
576578
throw RestliUtils.resourceNotFoundException();
577579
}
@@ -589,7 +591,7 @@ public Task<ASSET> getAsset(@ActionParam(PARAM_URN) @Nonnull String urnString,
589591

590592
return ModelUtils.newAsset(_assetClass, urn, aspects);
591593
}
592-
return getAssetWithShadowComparison(urn, aspectNames);
594+
return getAssetWithShadowComparison(urn, aspectNames, shadowReadLocalDAO);
593595
});
594596
} catch (ModelValidationException e) {
595597
throw RestliUtils.invalidArgumentsException(e.getMessage());
@@ -610,10 +612,10 @@ public Task<ASSET> getAsset(@ActionParam(PARAM_URN) @Nonnull String urnString,
610612
* @return an asset assembled from the resolved aspects
611613
* @throws RestLiServiceException if the entity does not exist in the local DAO
612614
*/
613-
private ASSET getAssetWithShadowComparison(@Nonnull URN urn, @Nullable String[] aspectNames) {
615+
private ASSET getAssetWithShadowComparison(@Nonnull URN urn, @Nullable String[] aspectNames, BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO) {
614616

615617
if (!getLocalDAO().exists(urn)) {
616-
if (getShadowReadLocalDAO().exists(urn)) {
618+
if (shadowReadLocalDAO.exists(urn)) {
617619
log.warn("Entity {} exists in shadow DAO but not in local DAO. Ignoring shadow-only data.", urn);
618620
}
619621
throw RestliUtils.resourceNotFoundException();
@@ -628,7 +630,7 @@ private ASSET getAssetWithShadowComparison(@Nonnull URN urn, @Nullable String[]
628630
Map<AspectKey<URN, ? extends RecordTemplate>, java.util.Optional<? extends RecordTemplate>> localResults =
629631
getLocalDAO().get(keys);
630632
Map<AspectKey<URN, ? extends RecordTemplate>, java.util.Optional<? extends RecordTemplate>> shadowResults =
631-
getShadowReadLocalDAO().get(keys);
633+
shadowReadLocalDAO.get(keys);
632634

633635
// Collect aspects
634636
List<UnionTemplate> aspects = new ArrayList<>();
@@ -1230,7 +1232,8 @@ private Map<URN, List<UnionTemplate>> getUrnAspectMap(@Nonnull Collection<URN> u
12301232
final Map<URN, List<UnionTemplate>> urnAspectsMap =
12311233
urns.stream().collect(Collectors.toMap(Function.identity(), urn -> new ArrayList<>()));
12321234

1233-
if (getShadowReadLocalDAO() == null) {
1235+
BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO = getShadowReadLocalDAO();
1236+
if (shadowReadLocalDAO == null) {
12341237
if (isInternalModelsEnabled) {
12351238
getLocalDAO().get(keys)
12361239
.forEach((key, aspect) -> aspect.ifPresent(metadata -> urnAspectsMap.get(key.getUrn())
@@ -1242,22 +1245,22 @@ private Map<URN, List<UnionTemplate>> getUrnAspectMap(@Nonnull Collection<URN> u
12421245
}
12431246
return urnAspectsMap;
12441247
} else {
1245-
return getUrnAspectMapFromShadowDao(urns, keys, isInternalModelsEnabled);
1248+
return getUrnAspectMapFromShadowDao(urns, keys, isInternalModelsEnabled, shadowReadLocalDAO);
12461249
}
12471250
}
12481251

12491252
@Nonnull
12501253
private Map<URN, List<UnionTemplate>> getUrnAspectMapFromShadowDao(
12511254
@Nonnull Collection<URN> urns,
12521255
@Nonnull Set<AspectKey<URN, ? extends RecordTemplate>> keys,
1253-
boolean isInternalModelsEnabled) {
1256+
boolean isInternalModelsEnabled,
1257+
@Nonnull BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowReadLocalDAO) {
12541258

12551259
Map<AspectKey<URN, ? extends RecordTemplate>, java.util.Optional<? extends RecordTemplate>> localResults =
12561260
getLocalDAO().get(keys);
12571261

1258-
BaseLocalDAO<INTERNAL_ASPECT_UNION, URN> shadowDao = getShadowReadLocalDAO();
12591262
Map<AspectKey<URN, ? extends RecordTemplate>, java.util.Optional<? extends RecordTemplate>> shadowResults =
1260-
shadowDao.get(keys);
1263+
shadowReadLocalDAO.get(keys);
12611264

12621265
final Map<URN, List<UnionTemplate>> urnAspectsMap =
12631266
urns.stream().collect(Collectors.toMap(Function.identity(), urn -> new ArrayList<>()));

0 commit comments

Comments
 (0)