|
21 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 22 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
23 | 23 |
|
| 24 | +import jakarta.annotation.Nonnull; |
24 | 25 | import jakarta.ws.rs.core.Response; |
25 | 26 | import jakarta.ws.rs.core.SecurityContext; |
26 | 27 | import java.security.Principal; |
27 | 28 | import java.time.Clock; |
28 | 29 | import java.time.Instant; |
| 30 | +import java.util.HashSet; |
29 | 31 | import java.util.List; |
30 | 32 | import java.util.Map; |
31 | 33 | import java.util.Set; |
|
43 | 45 | import org.apache.polaris.core.auth.PolarisAuthorizerImpl; |
44 | 46 | import org.apache.polaris.core.context.CallContext; |
45 | 47 | import org.apache.polaris.core.context.RealmContext; |
| 48 | +import org.apache.polaris.core.entity.PolarisBaseEntity; |
| 49 | +import org.apache.polaris.core.entity.PolarisEntity; |
| 50 | +import org.apache.polaris.core.entity.PolarisEntityConstants; |
| 51 | +import org.apache.polaris.core.entity.PolarisEntitySubType; |
| 52 | +import org.apache.polaris.core.entity.PolarisEntityType; |
46 | 53 | import org.apache.polaris.core.entity.PrincipalEntity; |
47 | 54 | import org.apache.polaris.core.entity.PrincipalRoleEntity; |
48 | 55 | import org.apache.polaris.core.persistence.MetaStoreManagerFactory; |
49 | 56 | import org.apache.polaris.core.persistence.PolarisMetaStoreManager; |
| 57 | +import org.apache.polaris.core.persistence.dao.entity.BaseResult; |
| 58 | +import org.apache.polaris.core.persistence.dao.entity.CreateCatalogResult; |
50 | 59 | import org.apache.polaris.core.persistence.dao.entity.EntityResult; |
| 60 | +import org.apache.polaris.core.persistence.transactional.TransactionalMetaStoreManagerImpl; |
51 | 61 | import org.apache.polaris.core.secrets.UnsafeInMemorySecretsManager; |
52 | 62 | import org.apache.polaris.service.TestServices; |
53 | 63 | import org.apache.polaris.service.admin.PolarisAdminService; |
@@ -276,4 +286,66 @@ public void testCannotAssignFederatedEntities() { |
276 | 286 | () -> polarisAdminService.assignPrincipalRole(principal.getName(), role.getName())) |
277 | 287 | .isInstanceOf(ValidationException.class); |
278 | 288 | } |
| 289 | + |
| 290 | + /** Simulates the case when a catalog is dropped after being found while listing all catalogs. */ |
| 291 | + @Test |
| 292 | + public void testCatalogNotReturnedWhenDeletedAfterListBeforeGet() { |
| 293 | + TestPolarisMetaStoreManager metaStoreManager = new TestPolarisMetaStoreManager(); |
| 294 | + PolarisCallContext callContext = setupCallContext(metaStoreManager); |
| 295 | + PolarisAdminService polarisAdminService = |
| 296 | + setupPolarisAdminService(metaStoreManager, callContext); |
| 297 | + |
| 298 | + CreateCatalogResult catalog1 = |
| 299 | + metaStoreManager.createCatalog( |
| 300 | + callContext, |
| 301 | + new PolarisBaseEntity( |
| 302 | + PolarisEntityConstants.getNullId(), |
| 303 | + metaStoreManager.generateNewEntityId(callContext).getId(), |
| 304 | + PolarisEntityType.CATALOG, |
| 305 | + PolarisEntitySubType.NULL_SUBTYPE, |
| 306 | + PolarisEntityConstants.getRootEntityId(), |
| 307 | + "my-catalog-1"), |
| 308 | + List.of()); |
| 309 | + CreateCatalogResult catalog2 = |
| 310 | + metaStoreManager.createCatalog( |
| 311 | + callContext, |
| 312 | + new PolarisBaseEntity( |
| 313 | + PolarisEntityConstants.getNullId(), |
| 314 | + metaStoreManager.generateNewEntityId(callContext).getId(), |
| 315 | + PolarisEntityType.CATALOG, |
| 316 | + PolarisEntitySubType.NULL_SUBTYPE, |
| 317 | + PolarisEntityConstants.getRootEntityId(), |
| 318 | + "my-catalog-2"), |
| 319 | + List.of()); |
| 320 | + |
| 321 | + metaStoreManager.setFakeEntityNotFoundIds(Set.of(catalog1.getCatalog().getId())); |
| 322 | + List<PolarisEntity> catalogs = polarisAdminService.listCatalogs(); |
| 323 | + assertThat(catalogs.size()).isEqualTo(1); |
| 324 | + assertThat(catalogs.getFirst().getId()).isEqualTo(catalog2.getCatalog().getId()); |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * Intended to be a delegate to TransactionalMetaStoreManagerImpl with the ability to inject |
| 329 | + * faults. Currently, you can force loadEntity() to return ENTITY_NOT_FOUND for a set of entity |
| 330 | + * IDs. |
| 331 | + */ |
| 332 | + public static class TestPolarisMetaStoreManager extends TransactionalMetaStoreManagerImpl { |
| 333 | + private Set<Long> fakeEntityNotFoundIds = new HashSet<>(); |
| 334 | + |
| 335 | + public void setFakeEntityNotFoundIds(Set<Long> ids) { |
| 336 | + fakeEntityNotFoundIds = new HashSet<>(ids); |
| 337 | + } |
| 338 | + |
| 339 | + @Override |
| 340 | + public @Nonnull EntityResult loadEntity( |
| 341 | + @Nonnull PolarisCallContext callCtx, |
| 342 | + long entityCatalogId, |
| 343 | + long entityId, |
| 344 | + @Nonnull PolarisEntityType entityType) { |
| 345 | + if (fakeEntityNotFoundIds.contains(entityId)) { |
| 346 | + return new EntityResult(BaseResult.ReturnStatus.ENTITY_NOT_FOUND, ""); |
| 347 | + } |
| 348 | + return super.loadEntity(callCtx, entityCatalogId, entityId, entityType); |
| 349 | + } |
| 350 | + } |
279 | 351 | } |
0 commit comments