Skip to content

Commit 7bf8462

Browse files
committed
Added typeCode to BasePersistence.lookupEntity
This is mainly needed for the upcoming NoSQL implementation. Also, this change harmonizes the API parameters across all lookup methods. This change induces changes in the MetaStore Manager API and related classes. In most top-level call sites the required extra information is already available. A minor inefficiency exists in PolarisAdminService, but it will only manifest in backends that do rely on the new parameter to perform lookups, plus the affected use case is not expected to be frequent (Admin API queries to obtain existing grants for a catalog role).
1 parent 4de1d33 commit 7bf8462

File tree

27 files changed

+267
-96
lines changed

27 files changed

+267
-96
lines changed

extension/persistence/eclipselink/src/main/java/org/apache/polaris/extension/persistence/impl/eclipselink/PolarisEclipseLinkMetaStoreSessionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public void deleteAll(@Nonnull PolarisCallContext callCtx) {
351351
/** {@inheritDoc} */
352352
@Override
353353
public @Nullable PolarisBaseEntity lookupEntity(
354-
@Nonnull PolarisCallContext callCtx, long catalogId, long entityId) {
354+
@Nonnull PolarisCallContext callCtx, long catalogId, long entityId, int typeCode) {
355355
return ModelEntity.toEntity(this.store.lookupEntity(localSession.get(), catalogId, entityId));
356356
}
357357

polaris-core/src/main/java/org/apache/polaris/core/persistence/BasePersistence.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ void deleteAllEntityGrantRecords(
153153
* @param callCtx call context
154154
* @param catalogId catalog id or NULL_ID
155155
* @param entityId entity id
156+
* @param typeCode the PolarisEntityType code of the entity to lookup
156157
* @return null if the entity was not found, else the retrieved entity.
157158
*/
158159
@Nullable
159160
PolarisBaseEntity lookupEntity(
160-
@Nonnull PolarisCallContext callCtx, long catalogId, long entityId);
161+
@Nonnull PolarisCallContext callCtx, long catalogId, long entityId, int typeCode);
161162

162163
/**
163164
* Lookup an entity given its catalogId, parentId, typeCode, and name.

polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisMetaStoreManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ DropEntityResult dropEntityIfExists(
285285
* @param entityId the id of the entity to load
286286
*/
287287
@Nonnull
288-
EntityResult loadEntity(@Nonnull PolarisCallContext callCtx, long entityCatalogId, long entityId);
288+
EntityResult loadEntity(
289+
@Nonnull PolarisCallContext callCtx,
290+
long entityCatalogId,
291+
long entityId,
292+
@Nonnull PolarisEntityType entityType);
289293

290294
/**
291295
* Fetch a list of tasks to be completed. Tasks
@@ -327,7 +331,10 @@ ChangeTrackingResult loadEntitiesChangeTracking(
327331
*/
328332
@Nonnull
329333
ResolvedEntityResult loadResolvedEntityById(
330-
@Nonnull PolarisCallContext callCtx, long entityCatalogId, long entityId);
334+
@Nonnull PolarisCallContext callCtx,
335+
long entityCatalogId,
336+
long entityId,
337+
PolarisEntityType entityType);
331338

332339
/**
333340
* Load a resolved entity, i.e. an entity definition and associated grant records, from the

polaris-core/src/main/java/org/apache/polaris/core/persistence/TransactionWorkspaceMetaStoreManager.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ public ChangeTrackingResult loadEntitiesChangeTracking(
303303

304304
@Override
305305
public EntityResult loadEntity(
306-
@Nonnull PolarisCallContext callCtx, long entityCatalogId, long entityId) {
306+
@Nonnull PolarisCallContext callCtx,
307+
long entityCatalogId,
308+
long entityId,
309+
PolarisEntityType entityType) {
307310
callCtx.getDiagServices().fail("illegal_method_in_transaction_workspace", "loadEntity");
308311
return null;
309312
}
@@ -320,13 +323,15 @@ public ScopedCredentialsResult getSubscopedCredsForEntity(
320323
@Nonnull PolarisCallContext callCtx,
321324
long catalogId,
322325
long entityId,
326+
PolarisEntityType entityType,
323327
boolean allowListOperation,
324328
@Nonnull Set<String> allowedReadLocations,
325329
@Nonnull Set<String> allowedWriteLocations) {
326330
return delegate.getSubscopedCredsForEntity(
327331
callCtx,
328332
catalogId,
329333
entityId,
334+
entityType,
330335
allowListOperation,
331336
allowedReadLocations,
332337
allowedWriteLocations);
@@ -337,6 +342,7 @@ public ValidateAccessResult validateAccessToLocations(
337342
@Nonnull PolarisCallContext callCtx,
338343
long catalogId,
339344
long entityId,
345+
PolarisEntityType entityType,
340346
@Nonnull Set<PolarisStorageActions> actions,
341347
@Nonnull Set<String> locations) {
342348
callCtx
@@ -347,7 +353,10 @@ public ValidateAccessResult validateAccessToLocations(
347353

348354
@Override
349355
public ResolvedEntityResult loadResolvedEntityById(
350-
@Nonnull PolarisCallContext callCtx, long entityCatalogId, long entityId) {
356+
@Nonnull PolarisCallContext callCtx,
357+
long entityCatalogId,
358+
long entityId,
359+
PolarisEntityType entityType) {
351360
callCtx
352361
.getDiagServices()
353362
.fail("illegal_method_in_transaction_workspace", "loadResolvedEntityById");

polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/EntityCache.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ && isNewer(existingCacheEntry, existingCacheEntryByName)) {
292292
// try to load it
293293
refreshedCacheEntry =
294294
this.polarisMetaStoreManager.loadResolvedEntityById(
295-
callContext, entityCatalogId, entityId);
295+
callContext, entityCatalogId, entityId, entityType);
296296
if (refreshedCacheEntry.isSuccess()) {
297297
entity = refreshedCacheEntry.getEntity();
298298
grantRecords = refreshedCacheEntry.getEntityGrantRecords();
@@ -361,7 +361,10 @@ && isNewer(existingCacheEntry, existingCacheEntryByName)) {
361361
* entity, either as found in the cache or loaded from the backend
362362
*/
363363
public @Nullable EntityCacheLookupResult getOrLoadEntityById(
364-
@Nonnull PolarisCallContext callContext, long entityCatalogId, long entityId) {
364+
@Nonnull PolarisCallContext callContext,
365+
long entityCatalogId,
366+
long entityId,
367+
PolarisEntityType entityType) {
365368

366369
// if it exists, we are set
367370
ResolvedPolarisEntity entry = this.getEntityById(entityId);
@@ -374,7 +377,8 @@ && isNewer(existingCacheEntry, existingCacheEntryByName)) {
374377

375378
// load it
376379
ResolvedEntityResult result =
377-
polarisMetaStoreManager.loadResolvedEntityById(callContext, entityCatalogId, entityId);
380+
polarisMetaStoreManager.loadResolvedEntityById(
381+
callContext, entityCatalogId, entityId, entityType);
378382

379383
// not found, exit
380384
if (!result.isSuccess()) {

polaris-core/src/main/java/org/apache/polaris/core/persistence/resolver/Resolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ private ResolvedPolarisEntity resolveById(
10271027
if (this.cache != null) {
10281028
// get or load by name
10291029
EntityCacheLookupResult lookupResult =
1030-
this.cache.getOrLoadEntityById(this.polarisCallContext, catalogId, entityId);
1030+
this.cache.getOrLoadEntityById(this.polarisCallContext, catalogId, entityId, entityType);
10311031

10321032
// if not found, return null
10331033
if (lookupResult == null) {
@@ -1049,7 +1049,7 @@ private ResolvedPolarisEntity resolveById(
10491049
// If no cache, load directly from metastore manager.
10501050
ResolvedEntityResult result =
10511051
polarisMetaStoreManager.loadResolvedEntityById(
1052-
this.polarisCallContext, catalogId, entityId);
1052+
this.polarisCallContext, catalogId, entityId, entityType);
10531053
if (!result.isSuccess()) {
10541054
// not found
10551055
return null;

polaris-core/src/main/java/org/apache/polaris/core/persistence/transactional/AbstractTransactionalPersistence.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public PolarisBaseEntity lookupEntityByName(
6868

6969
// lookup the entity, should be there
7070
PolarisBaseEntity entity =
71-
lookupEntity(callCtx, entityActiveRecord.getCatalogId(), entityActiveRecord.getId());
71+
lookupEntity(
72+
callCtx,
73+
entityActiveRecord.getCatalogId(),
74+
entityActiveRecord.getId(),
75+
entityActiveRecord.getTypeCode());
7276
callCtx
7377
.getDiagServices()
7478
.checkNotNull(

polaris-core/src/main/java/org/apache/polaris/core/persistence/transactional/PolarisMetaStoreManagerImpl.java

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private void dropEntity(
328328
// load the grantee (either a catalog/principal role or a principal) and increment its grants
329329
// version
330330
PolarisBaseEntity granteeEntity =
331-
ms.lookupEntity(callCtx, grantee.getCatalogId(), grantee.getId());
331+
ms.lookupEntity(callCtx, grantee.getCatalogId(), grantee.getId(), grantee.getTypeCode());
332332
callCtx
333333
.getDiagServices()
334334
.checkNotNull(granteeEntity, "grantee_not_found", "grantee={}", grantee);
@@ -341,7 +341,8 @@ private void dropEntity(
341341
// we also need to invalidate the grants on that securable so that we can reload them.
342342
// load the securable and increment its grants version
343343
PolarisBaseEntity securableEntity =
344-
ms.lookupEntity(callCtx, securable.getCatalogId(), securable.getId());
344+
ms.lookupEntity(
345+
callCtx, securable.getCatalogId(), securable.getId(), securable.getTypeCode());
345346
callCtx
346347
.getDiagServices()
347348
.checkNotNull(securableEntity, "securable_not_found", "securable={}", securable);
@@ -404,7 +405,7 @@ private void revokeGrantRecord(
404405

405406
// load the grantee and increment its grants version
406407
PolarisBaseEntity refreshGrantee =
407-
ms.lookupEntity(callCtx, grantee.getCatalogId(), grantee.getId());
408+
ms.lookupEntity(callCtx, grantee.getCatalogId(), grantee.getId(), grantee.getTypeCode());
408409
callCtx
409410
.getDiagServices()
410411
.checkNotNull(
@@ -418,7 +419,8 @@ private void revokeGrantRecord(
418419
// we also need to invalidate the grants on that securable so that we can reload them.
419420
// load the securable and increment its grants version
420421
PolarisBaseEntity refreshSecurable =
421-
ms.lookupEntity(callCtx, securable.getCatalogId(), securable.getId());
422+
ms.lookupEntity(
423+
callCtx, securable.getCatalogId(), securable.getId(), securable.getTypeCode());
422424
callCtx
423425
.getDiagServices()
424426
.checkNotNull(
@@ -460,7 +462,7 @@ private void revokeGrantRecord(
460462

461463
// check if that catalog has already been created
462464
PolarisBaseEntity refreshCatalog =
463-
ms.lookupEntity(callCtx, catalog.getCatalogId(), catalog.getId());
465+
ms.lookupEntity(callCtx, catalog.getCatalogId(), catalog.getId(), catalog.getTypeCode());
464466

465467
// if found, probably a retry, simply return the previously created catalog
466468
if (refreshCatalog != null) {
@@ -822,7 +824,8 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
822824

823825
// check if that catalog has already been created
824826
PolarisBaseEntity refreshPrincipal =
825-
ms.lookupEntity(callCtx, principal.getCatalogId(), principal.getId());
827+
ms.lookupEntity(
828+
callCtx, principal.getCatalogId(), principal.getId(), principal.getTypeCode());
826829

827830
// if found, probably a retry, simply return the previously created principal
828831
if (refreshPrincipal != null) {
@@ -950,7 +953,12 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
950953
@Nonnull String oldSecretHash) {
951954
// if not found, the principal must have been dropped
952955
EntityResult loadEntityResult =
953-
loadEntity(callCtx, ms, PolarisEntityConstants.getNullId(), principalId);
956+
loadEntity(
957+
callCtx,
958+
ms,
959+
PolarisEntityConstants.getNullId(),
960+
principalId,
961+
PolarisEntityType.PRINCIPAL.getCode());
954962
if (loadEntityResult.getReturnStatus() != BaseResult.ReturnStatus.SUCCESS) {
955963
return null;
956964
}
@@ -1061,7 +1069,8 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
10611069
callCtx.getDiagServices().checkNotNull(entity.getName(), "unexpected_null_entity_name");
10621070

10631071
// first, check if the entity has already been created, in which case we will simply return it
1064-
PolarisBaseEntity entityFound = ms.lookupEntity(callCtx, entity.getCatalogId(), entity.getId());
1072+
PolarisBaseEntity entityFound =
1073+
ms.lookupEntity(callCtx, entity.getCatalogId(), entity.getId(), entity.getTypeCode());
10651074
if (entityFound != null) {
10661075
// probably the client retried, simply return it
10671076
return new EntityResult(entityFound);
@@ -1158,7 +1167,7 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
11581167

11591168
// lookup the entity, cannot be null
11601169
PolarisBaseEntity entityRefreshed =
1161-
ms.lookupEntity(callCtx, entity.getCatalogId(), entity.getId());
1170+
ms.lookupEntity(callCtx, entity.getCatalogId(), entity.getId(), entity.getTypeCode());
11621171
callCtx
11631172
.getDiagServices()
11641173
.checkNotNull(entityRefreshed, "unexpected_entity_not_found", "entity={}", entity);
@@ -1280,7 +1289,11 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
12801289

12811290
// find the entity to rename
12821291
PolarisBaseEntity refreshEntityToRename =
1283-
ms.lookupEntity(callCtx, entityToRename.getCatalogId(), entityToRename.getId());
1292+
ms.lookupEntity(
1293+
callCtx,
1294+
entityToRename.getCatalogId(),
1295+
entityToRename.getId(),
1296+
entityToRename.getTypeCode());
12841297

12851298
// if this entity was not found, return failure. Not expected here because it was
12861299
// resolved successfully (see above)
@@ -1389,7 +1402,8 @@ public Map<String, String> deserializeProperties(PolarisCallContext callCtx, Str
13891402

13901403
// first find the entity to drop
13911404
PolarisBaseEntity refreshEntityToDrop =
1392-
ms.lookupEntity(callCtx, entityToDrop.getCatalogId(), entityToDrop.getId());
1405+
ms.lookupEntity(
1406+
callCtx, entityToDrop.getCatalogId(), entityToDrop.getId(), entityToDrop.getTypeCode());
13931407

13941408
// if this entity was not found, return failure
13951409
if (refreshEntityToDrop == null) {
@@ -1921,14 +1935,15 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
19211935
callCtx, () -> this.loadEntitiesChangeTracking(callCtx, ms, entityIds));
19221936
}
19231937

1924-
/** Refer to {@link #loadEntity(PolarisCallContext, long, long)} */
1938+
/** Refer to {@link #loadEntity(PolarisCallContext, long, long, PolarisEntityType)} */
19251939
private @Nonnull EntityResult loadEntity(
19261940
@Nonnull PolarisCallContext callCtx,
19271941
@Nonnull TransactionalPersistence ms,
19281942
long entityCatalogId,
1929-
long entityId) {
1943+
long entityId,
1944+
int entityTypeCode) {
19301945
// this is an easy one
1931-
PolarisBaseEntity entity = ms.lookupEntity(callCtx, entityCatalogId, entityId);
1946+
PolarisBaseEntity entity = ms.lookupEntity(callCtx, entityCatalogId, entityId, entityTypeCode);
19321947
return (entity != null)
19331948
? new EntityResult(entity)
19341949
: new EntityResult(BaseResult.ReturnStatus.ENTITY_NOT_FOUND, null);
@@ -1937,13 +1952,17 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
19371952
/** {@inheritDoc} */
19381953
@Override
19391954
public @Nonnull EntityResult loadEntity(
1940-
@Nonnull PolarisCallContext callCtx, long entityCatalogId, long entityId) {
1955+
@Nonnull PolarisCallContext callCtx,
1956+
long entityCatalogId,
1957+
long entityId,
1958+
@Nonnull PolarisEntityType entityType) {
19411959
// get metastore we should be using
19421960
TransactionalPersistence ms = callCtx.getMetaStore();
19431961

19441962
// need to run inside a read transaction
19451963
return ms.runInReadTransaction(
1946-
callCtx, () -> this.loadEntity(callCtx, ms, entityCatalogId, entityId));
1964+
callCtx,
1965+
() -> this.loadEntity(callCtx, ms, entityCatalogId, entityId, entityType.getCode()));
19471966
}
19481967

19491968
/** Refer to {@link #loadTasks(PolarisCallContext, String, int)} */
@@ -2011,6 +2030,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
20112030
@Nonnull PolarisCallContext callCtx,
20122031
long catalogId,
20132032
long entityId,
2033+
PolarisEntityType entityType,
20142034
boolean allowListOperation,
20152035
@Nonnull Set<String> allowedReadLocations,
20162036
@Nonnull Set<String> allowedWriteLocations) {
@@ -2024,7 +2044,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
20242044
"allowed_locations_to_subscope_is_required");
20252045

20262046
// reload the entity, error out if not found
2027-
EntityResult reloadedEntity = loadEntity(callCtx, catalogId, entityId);
2047+
EntityResult reloadedEntity = loadEntity(callCtx, catalogId, entityId, entityType);
20282048
if (reloadedEntity.getReturnStatus() != BaseResult.ReturnStatus.SUCCESS) {
20292049
return new ScopedCredentialsResult(
20302050
reloadedEntity.getReturnStatus(), reloadedEntity.getExtraInformation());
@@ -2067,6 +2087,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
20672087
@Nonnull PolarisCallContext callCtx,
20682088
long catalogId,
20692089
long entityId,
2090+
PolarisEntityType entityType,
20702091
@Nonnull Set<PolarisStorageActions> actions,
20712092
@Nonnull Set<String> locations) {
20722093
// get meta store we should be using
@@ -2077,7 +2098,7 @@ private PolarisEntityResolver resolveSecurableToRoleGrant(
20772098
!actions.isEmpty() && !locations.isEmpty(),
20782099
"locations_and_operations_privileges_are_required");
20792100
// reload the entity, error out if not found
2080-
EntityResult reloadedEntity = loadEntity(callCtx, catalogId, entityId);
2101+
EntityResult reloadedEntity = loadEntity(callCtx, catalogId, entityId, entityType);
20812102
if (reloadedEntity.getReturnStatus() != BaseResult.ReturnStatus.SUCCESS) {
20822103
return new ValidateAccessResult(
20832104
reloadedEntity.getReturnStatus(), reloadedEntity.getExtraInformation());
@@ -2129,15 +2150,16 @@ public Map<String, String> getInternalPropertyMap(
21292150
return deserializeProperties(callCtx, internalPropStr);
21302151
}
21312152

2132-
/** {@link #loadResolvedEntityById(PolarisCallContext, long, long)} */
2153+
/** {@link #loadResolvedEntityById(PolarisCallContext, long, long, PolarisEntityType)} */
21332154
private @Nonnull ResolvedEntityResult loadResolvedEntityById(
21342155
@Nonnull PolarisCallContext callCtx,
21352156
@Nonnull TransactionalPersistence ms,
21362157
long entityCatalogId,
2137-
long entityId) {
2158+
long entityId,
2159+
int typeCode) {
21382160

21392161
// load that entity
2140-
PolarisBaseEntity entity = ms.lookupEntity(callCtx, entityCatalogId, entityId);
2162+
PolarisBaseEntity entity = ms.lookupEntity(callCtx, entityCatalogId, entityId, typeCode);
21412163

21422164
// if entity not found, return null
21432165
if (entity == null) {
@@ -2161,16 +2183,22 @@ public Map<String, String> getInternalPropertyMap(
21612183
/** {@inheritDoc} */
21622184
@Override
21632185
public @Nonnull ResolvedEntityResult loadResolvedEntityById(
2164-
@Nonnull PolarisCallContext callCtx, long entityCatalogId, long entityId) {
2186+
@Nonnull PolarisCallContext callCtx,
2187+
long entityCatalogId,
2188+
long entityId,
2189+
PolarisEntityType entityType) {
21652190
// get metastore we should be using
21662191
TransactionalPersistence ms = callCtx.getMetaStore();
21672192

21682193
// need to run inside a read transaction
21692194
return ms.runInReadTransaction(
2170-
callCtx, () -> this.loadResolvedEntityById(callCtx, ms, entityCatalogId, entityId));
2195+
callCtx,
2196+
() ->
2197+
this.loadResolvedEntityById(
2198+
callCtx, ms, entityCatalogId, entityId, entityType.getCode()));
21712199
}
21722200

2173-
/** {@link #loadResolvedEntityById(PolarisCallContext, long, long)} */
2201+
/** {@link #loadResolvedEntityById(PolarisCallContext, long, long, PolarisEntityType)} */
21742202
private @Nonnull ResolvedEntityResult loadResolvedEntityByName(
21752203
@Nonnull PolarisCallContext callCtx,
21762204
@Nonnull TransactionalPersistence ms,
@@ -2292,7 +2320,7 @@ public Map<String, String> getInternalPropertyMap(
22922320
// load the entity if something changed
22932321
final PolarisBaseEntity entity;
22942322
if (entityVersion != entityVersions.getEntityVersion()) {
2295-
entity = ms.lookupEntity(callCtx, entityCatalogId, entityId);
2323+
entity = ms.lookupEntity(callCtx, entityCatalogId, entityId, entityType.getCode());
22962324

22972325
// if not found, return null
22982326
if (entity == null) {

0 commit comments

Comments
 (0)