adnanhemani commented on code in PR #3293:
URL: https://github.com/apache/polaris/pull/3293#discussion_r2674041099
##########
runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/AbstractIcebergCatalogTest.java:
##########
@@ -2360,13 +2362,15 @@ public void testEventsAreEmitted() {
table.updateProperties().set(key, valOld).commit();
table.updateProperties().set(key, valNew).commit();
- var beforeRefreshEvent =
-
testPolarisEventListener.getLatest(IcebergRestCatalogEvents.BeforeRefreshTableEvent.class);
-
Assertions.assertThat(beforeRefreshEvent.tableIdentifier()).isEqualTo(TestData.TABLE);
+ PolarisEvent beforeRefreshEvent =
+
testPolarisEventListener.getLatest(PolarisEventType.BEFORE_REFRESH_TABLE);
+
Assertions.assertThat(beforeRefreshEvent.attribute(EventAttributes.TABLE_IDENTIFIER))
Review Comment:
Commenting in case you forgot to make this change :)
##########
runtime/service/src/main/java/org/apache/polaris/service/events/listeners/PolarisPersistenceEventListener.java:
##########
@@ -23,29 +23,45 @@
import java.util.Map;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.rest.responses.LoadTableResponse;
+import org.apache.polaris.core.admin.model.Catalog;
import org.apache.polaris.core.auth.PolarisPrincipal;
-import org.apache.polaris.core.entity.PolarisEvent;
-import org.apache.polaris.service.events.CatalogsServiceEvents;
-import org.apache.polaris.service.events.IcebergRestCatalogEvents;
+import org.apache.polaris.service.events.EventAttributes;
+import org.apache.polaris.service.events.PolarisEvent;
public abstract class PolarisPersistenceEventListener implements
PolarisEventListener {
- // TODO: Ensure all events (except RateLimiter ones) call `processEvent`
-
@Override
- public void
onAfterCreateTable(IcebergRestCatalogEvents.AfterCreateTableEvent event) {
- TableMetadata tableMetadata = event.loadTableResponse().tableMetadata();
- PolarisEvent polarisEvent =
- new PolarisEvent(
- event.catalogName(),
+ public void onEvent(PolarisEvent event) {
+ switch (event.type()) {
+ case AFTER_CREATE_TABLE -> handleAfterCreateTable(event);
+ case AFTER_CREATE_CATALOG -> handleAfterCreateCatalog(event);
+ default -> {
+ // Other events not handled by this listener
+ }
+ }
+ }
+
+ private void handleAfterCreateTable(PolarisEvent event) {
+ LoadTableResponse loadTableResponse =
+ event.requiredAttribute(EventAttributes.LOAD_TABLE_RESPONSE);
+ TableMetadata tableMetadata = loadTableResponse.tableMetadata();
+ String catalogName = event.requiredAttribute(EventAttributes.CATALOG_NAME);
+ Namespace namespace = event.requiredAttribute(EventAttributes.NAMESPACE);
+ String tableName = event.requiredAttribute(EventAttributes.TABLE_NAME);
+
+ org.apache.polaris.core.entity.PolarisEvent polarisEvent =
Review Comment:
nit: not sure why we had to write the full class hierarchy here. Was there a
change?
##########
runtime/service/src/main/java/org/apache/polaris/service/events/AllowedAttributeTypes.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.service.events;
+
+import com.google.common.reflect.TypeToken;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.rest.RESTMessage;
+import org.apache.iceberg.view.ViewMetadata;
+import org.apache.polaris.core.admin.model.AddGrantRequest;
+import org.apache.polaris.core.admin.model.Catalog;
+import org.apache.polaris.core.admin.model.CatalogRole;
+import org.apache.polaris.core.admin.model.CreatePrincipalRoleRequest;
+import org.apache.polaris.core.admin.model.GrantResource;
+import org.apache.polaris.core.admin.model.Principal;
+import org.apache.polaris.core.admin.model.PrincipalRole;
+import org.apache.polaris.core.admin.model.RevokeGrantRequest;
+import org.apache.polaris.core.admin.model.UpdateCatalogRequest;
+import org.apache.polaris.core.admin.model.UpdateCatalogRoleRequest;
+import org.apache.polaris.core.admin.model.UpdatePrincipalRequest;
+import org.apache.polaris.core.admin.model.UpdatePrincipalRoleRequest;
+import org.apache.polaris.core.entity.PolarisPrivilege;
+import org.apache.polaris.service.types.AttachPolicyRequest;
+import org.apache.polaris.service.types.CommitViewRequest;
+import org.apache.polaris.service.types.CreateGenericTableRequest;
+import org.apache.polaris.service.types.CreatePolicyRequest;
+import org.apache.polaris.service.types.DetachPolicyRequest;
+import org.apache.polaris.service.types.GenericTable;
+import org.apache.polaris.service.types.GetApplicablePoliciesResponse;
+import org.apache.polaris.service.types.LoadPolicyResponse;
+import org.apache.polaris.service.types.NotificationRequest;
+import org.apache.polaris.service.types.UpdatePolicyRequest;
+
+/** Whitelist of types allowed for event attributes. */
+final class AllowedAttributeTypes {
+ private AllowedAttributeTypes() {}
+
+ static final Set<Class<?>> ALLOWED_TYPES =
+ Set.of(
+ // Primitives
+ String.class,
+ Boolean.class,
+ Number.class,
+ // Iceberg types
+ RESTMessage.class,
+ Namespace.class,
+ TableIdentifier.class,
+ TableMetadata.class,
+ ViewMetadata.class,
+ // Polaris admin model types
+ Catalog.class,
+ Principal.class,
+ PrincipalRole.class,
+ CatalogRole.class,
+ GrantResource.class,
+ UpdatePrincipalRequest.class,
+ CreatePrincipalRoleRequest.class,
+ UpdatePrincipalRoleRequest.class,
+ UpdateCatalogRequest.class,
+ UpdateCatalogRoleRequest.class,
+ AddGrantRequest.class,
+ RevokeGrantRequest.class,
+ PolarisPrivilege.class,
+ // Polaris service types
+ CommitViewRequest.class,
+ GenericTable.class,
+ CreateGenericTableRequest.class,
+ CreatePolicyRequest.class,
+ UpdatePolicyRequest.class,
+ LoadPolicyResponse.class,
+ AttachPolicyRequest.class,
+ DetachPolicyRequest.class,
+ GetApplicablePoliciesResponse.class,
+ NotificationRequest.class);
+
+ private static final Set<Class<?>> COLLECTION_TYPES = Set.of(List.class,
Set.class, Map.class);
+
+ static boolean isAllowed(TypeToken<?> type) {
+ Class<?> rawType = type.getRawType();
+ if (COLLECTION_TYPES.contains(rawType)) {
+ for (var typeParam : rawType.getTypeParameters()) {
+ TypeToken<?> resolvedType = type.resolveType(typeParam);
+ if (!isSubtypeOfAllowedType(resolvedType.getRawType())) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return isSubtypeOfAllowedType(rawType);
+ }
+
+ private static boolean isSubtypeOfAllowedType(Class<?> rawType) {
+ return ALLOWED_TYPES.stream().anyMatch(t -> t.isAssignableFrom(rawType));
Review Comment:
Not sure if this is too nit-picky - but maybe something like this to ensure
we don't waste a lot of cycles going through this check for the same handful of
classes?
```
private static final ClassValue<Boolean> IS_ALLOWED =
new ClassValue<>() {
@Override
protected Boolean computeValue(Class<?> type) {
for (Class<?> allowed : ALLOWED_TYPES) {
if (allowed.isAssignableFrom(type)) {
return true;
}
}
return false;
}
};
private static boolean isSubtypeOfAllowedType(Class<?> rawType) {
return IS_ALLOWED.get(rawType);
}
```
##########
runtime/service/src/main/java/org/apache/polaris/service/events/PolarisEvent.java:
##########
@@ -18,10 +18,66 @@
*/
package org.apache.polaris.service.events;
-/** Represents an event emitted by Polaris. */
-public interface PolarisEvent {
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
- PolarisEventType type();
+/**
+ * Represents an event emitted by Polaris. Events have a type, metadata, and a
map of typed
+ * attributes. Use {@link #builder(PolarisEventType, PolarisEventMetadata)} to
create instances.
+ */
+public record PolarisEvent(
Review Comment:
I think @adutra's idea is likely better. Let's go with it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]