Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.polaris.core.policy;
package org.apache.polaris.core.policy.content;

/** A marker interface for policy content */
public interface PolicyContent {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.polaris.core.policy.validator;
package org.apache.polaris.core.policy.content;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PolicyValidatorUtil {
public class PolicyContentUtil {
public static final ObjectMapper MAPPER = configureMapper();

private static ObjectMapper configureMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.polaris.core.policy.validator;
package org.apache.polaris.core.policy.content;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import org.apache.polaris.core.policy.validator.InvalidPolicyException;

public class StrictBooleanDeserializer extends JsonDeserializer<Boolean> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,27 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.polaris.core.policy.validator.datacompaction;
package org.apache.polaris.core.policy.content.maintenance;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Strings;
import java.util.Map;
import java.util.Set;
import org.apache.polaris.core.policy.PolicyContent;
import org.apache.polaris.core.policy.content.PolicyContent;
import org.apache.polaris.core.policy.content.StrictBooleanDeserializer;
import org.apache.polaris.core.policy.validator.InvalidPolicyException;
import org.apache.polaris.core.policy.validator.PolicyValidatorUtil;
import org.apache.polaris.core.policy.validator.StrictBooleanDeserializer;

public class DataCompactionPolicyContent implements PolicyContent {
private static final String DEFAULT_POLICY_SCHEMA_VERSION = "2025-02-03";
private static final Set<String> POLICY_SCHEMA_VERSIONS = Set.of(DEFAULT_POLICY_SCHEMA_VERSION);

public abstract class BaseMaintenancePolicyContent implements PolicyContent {
@JsonDeserialize(using = StrictBooleanDeserializer.class)
private Boolean enable;

private String version;
private Map<String, String> config;

@JsonCreator
public DataCompactionPolicyContent(
public BaseMaintenancePolicyContent(
@JsonProperty(value = "enable", required = true) boolean enable) {
this.enable = enable;
}
Expand Down Expand Up @@ -69,29 +65,21 @@ public void setConfig(Map<String, String> config) {
this.config = config;
}

public static DataCompactionPolicyContent fromString(String content) {
if (Strings.isNullOrEmpty(content)) {
throw new InvalidPolicyException("Policy is empty");
static void validateVersion(
String content,
BaseMaintenancePolicyContent policy,
String defaultVersion,
Set<String> allVersions) {
if (policy == null) {
throw new InvalidPolicyException("Invalid policy: " + content);
}

try {
DataCompactionPolicyContent policy =
PolicyValidatorUtil.MAPPER.readValue(content, DataCompactionPolicyContent.class);
if (policy == null) {
throw new InvalidPolicyException("Invalid policy");
}

if (Strings.isNullOrEmpty(policy.getVersion())) {
policy.setVersion(DEFAULT_POLICY_SCHEMA_VERSION);
}

if (!POLICY_SCHEMA_VERSIONS.contains(policy.getVersion())) {
throw new InvalidPolicyException("Invalid policy version: " + policy.getVersion());
}
if (Strings.isNullOrEmpty(policy.getVersion())) {
policy.setVersion(defaultVersion);
}

return policy;
} catch (Exception e) {
throw new InvalidPolicyException(e);
if (!allVersions.contains(policy.getVersion())) {
throw new InvalidPolicyException("Invalid policy version: " + policy.getVersion());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.core.policy.content.maintenance;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import java.util.Set;
import org.apache.polaris.core.policy.content.PolicyContentUtil;
import org.apache.polaris.core.policy.validator.InvalidPolicyException;

public class DataCompactionPolicyContent extends BaseMaintenancePolicyContent {
private static final String DEFAULT_POLICY_SCHEMA_VERSION = "2025-02-03";
private static final Set<String> POLICY_SCHEMA_VERSIONS = Set.of(DEFAULT_POLICY_SCHEMA_VERSION);

@JsonCreator
public DataCompactionPolicyContent(
@JsonProperty(value = "enable", required = true) boolean enable) {
super(enable);
}

public static DataCompactionPolicyContent fromString(String content) {
if (Strings.isNullOrEmpty(content)) {
throw new InvalidPolicyException("Policy is empty");
}

DataCompactionPolicyContent policy;
try {
policy = PolicyContentUtil.MAPPER.readValue(content, DataCompactionPolicyContent.class);
} catch (Exception e) {
throw new InvalidPolicyException(e);
}

validateVersion(content, policy, DEFAULT_POLICY_SCHEMA_VERSION, POLICY_SCHEMA_VERSIONS);

return policy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.core.policy.content.maintenance;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import java.util.Set;
import org.apache.polaris.core.policy.content.PolicyContentUtil;
import org.apache.polaris.core.policy.validator.InvalidPolicyException;

public class MetadataCompactionPolicyContent extends BaseMaintenancePolicyContent {
private static final String DEFAULT_POLICY_SCHEMA_VERSION = "2025-02-03";
private static final Set<String> POLICY_SCHEMA_VERSIONS = Set.of(DEFAULT_POLICY_SCHEMA_VERSION);

@JsonCreator
public MetadataCompactionPolicyContent(
@JsonProperty(value = "enable", required = true) boolean enable) {
super(enable);
}

public static MetadataCompactionPolicyContent fromString(String content) {
if (Strings.isNullOrEmpty(content)) {
throw new InvalidPolicyException("Policy is empty");
}

MetadataCompactionPolicyContent policy;
try {
policy = PolicyContentUtil.MAPPER.readValue(content, MetadataCompactionPolicyContent.class);
} catch (Exception e) {
throw new InvalidPolicyException(e);
}

validateVersion(content, policy, DEFAULT_POLICY_SCHEMA_VERSION, POLICY_SCHEMA_VERSIONS);

return policy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.core.policy.content.maintenance;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import java.util.List;
import java.util.Set;
import org.apache.polaris.core.policy.content.PolicyContentUtil;
import org.apache.polaris.core.policy.validator.InvalidPolicyException;

public class OrphanFileRemovalPolicyContent extends BaseMaintenancePolicyContent {
private static final String DEFAULT_POLICY_SCHEMA_VERSION = "2025-02-03";
private static final Set<String> POLICY_SCHEMA_VERSIONS = Set.of(DEFAULT_POLICY_SCHEMA_VERSION);

@JsonProperty(value = "max_orphan_file_age_in_days")
private int maxOrphanFileAgeInDays;

private List<String> locations;

@JsonCreator
public OrphanFileRemovalPolicyContent(
@JsonProperty(value = "enable", required = true) boolean enable) {
super(enable);
}

public int getMaxOrphanFileAgeInDays() {
return maxOrphanFileAgeInDays;
}

public void setMaxOrphanFileAgeInDays(int maxOrphanFileAgeInDays) {
this.maxOrphanFileAgeInDays = maxOrphanFileAgeInDays;
}

public List<String> getLocations() {
return locations;
}

public void setLocations(List<String> locations) {
this.locations = locations;
}

public static OrphanFileRemovalPolicyContent fromString(String content) {
if (Strings.isNullOrEmpty(content)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of repeating this everywhere, perhaps we can push this check up into the caller -- PolicyValidators is a good narrow waist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd nice to keep this parsing method in each individual class, so that they can be invoked separately, not only for validation use cases, but also for a general parsing or constructor use cases. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that they can be invoked separately makes sense. Maybe a supertype could contain some of this logic then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was trying to do that per your comment here, #1238 (comment). fromString are static methods, which cannot be overridden. The only thing we could do for static functions is to having another static function to group common logic. Let me refactor a bit.

throw new InvalidPolicyException("Policy is empty");
}

OrphanFileRemovalPolicyContent policy;
try {
policy = PolicyContentUtil.MAPPER.readValue(content, OrphanFileRemovalPolicyContent.class);
} catch (Exception e) {
throw new InvalidPolicyException(e);
}

validateVersion(content, policy, DEFAULT_POLICY_SCHEMA_VERSION, POLICY_SCHEMA_VERSIONS);

int maxAge = policy.getMaxOrphanFileAgeInDays();
if (maxAge < 0) {
throw new InvalidPolicyException(
"Invalid max_orphan_file_age_in_days: "
+ maxAge
+ ". It must be greater than or equal to 0");
}

return policy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.core.policy.content.maintenance;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import java.util.Set;
import org.apache.polaris.core.policy.content.PolicyContentUtil;
import org.apache.polaris.core.policy.validator.InvalidPolicyException;

public class SnapshotRetentionPolicyContent extends BaseMaintenancePolicyContent {
private static final String DEFAULT_POLICY_SCHEMA_VERSION = "2025-02-03";
private static final Set<String> POLICY_SCHEMA_VERSIONS = Set.of(DEFAULT_POLICY_SCHEMA_VERSION);

@JsonCreator
public SnapshotRetentionPolicyContent(
@JsonProperty(value = "enable", required = true) boolean enable) {
super(enable);
}

public static SnapshotRetentionPolicyContent fromString(String content) {
if (Strings.isNullOrEmpty(content)) {
throw new InvalidPolicyException("Policy is empty");
}

SnapshotRetentionPolicyContent policy;
try {
policy = PolicyContentUtil.MAPPER.readValue(content, SnapshotRetentionPolicyContent.class);
} catch (Exception e) {
throw new InvalidPolicyException(e);
}

validateVersion(content, policy, DEFAULT_POLICY_SCHEMA_VERSION, POLICY_SCHEMA_VERSIONS);

return policy;
}
}
Loading