|
29 | 29 | import java.util.HashSet;
|
30 | 30 | import java.util.List;
|
31 | 31 | import java.util.Map;
|
| 32 | +import java.util.Objects; |
32 | 33 | import java.util.Optional;
|
33 | 34 | import java.util.Set;
|
| 35 | +import java.util.concurrent.atomic.AtomicBoolean; |
34 | 36 | import java.util.stream.Collectors;
|
35 | 37 | import org.apache.iceberg.BaseMetadataTable;
|
36 | 38 | import org.apache.iceberg.BaseTable;
|
| 39 | +import org.apache.iceberg.BaseTransaction; |
| 40 | +import org.apache.iceberg.DataOperations; |
37 | 41 | import org.apache.iceberg.MetadataUpdate;
|
38 | 42 | import org.apache.iceberg.PartitionSpec;
|
| 43 | +import org.apache.iceberg.Schema; |
| 44 | +import org.apache.iceberg.Snapshot; |
39 | 45 | import org.apache.iceberg.SortOrder;
|
40 | 46 | import org.apache.iceberg.Table;
|
41 | 47 | import org.apache.iceberg.TableMetadata;
|
42 | 48 | import org.apache.iceberg.TableOperations;
|
| 49 | +import org.apache.iceberg.Transaction; |
43 | 50 | import org.apache.iceberg.UpdateRequirement;
|
44 | 51 | import org.apache.iceberg.catalog.Catalog;
|
45 | 52 | import org.apache.iceberg.catalog.Namespace;
|
|
71 | 78 | import org.apache.iceberg.rest.responses.LoadTableResponse;
|
72 | 79 | import org.apache.iceberg.rest.responses.LoadViewResponse;
|
73 | 80 | import org.apache.iceberg.rest.responses.UpdateNamespacePropertiesResponse;
|
| 81 | +import org.apache.iceberg.types.Types; |
| 82 | +import org.apache.iceberg.util.PropertyUtil; |
| 83 | +import org.apache.iceberg.util.Tasks; |
74 | 84 | import org.apache.polaris.core.auth.PolarisAuthorizableOperation;
|
75 | 85 | import org.apache.polaris.core.auth.PolarisAuthorizer;
|
76 | 86 | import org.apache.polaris.core.config.FeatureConfiguration;
|
@@ -754,7 +764,188 @@ public LoadTableResponse updateTable(
|
754 | 764 | if (isStaticFacade(catalog)) {
|
755 | 765 | throw new BadRequestException("Cannot update table on static-facade external catalogs.");
|
756 | 766 | }
|
757 |
| - return CatalogHandlers.updateTable(baseCatalog, tableIdentifier, applyUpdateFilters(request)); |
| 767 | + // TODO: pending discussion if table property is right way, or a writer specific knob is |
| 768 | + // required. |
| 769 | + return updateTableWithRollback(baseCatalog, tableIdentifier, applyUpdateFilters(request)); |
| 770 | + } |
| 771 | + |
| 772 | + private static TableMetadata create(TableOperations ops, UpdateTableRequest request) { |
| 773 | + request.requirements().forEach((requirement) -> requirement.validate(ops.current())); |
| 774 | + Optional<Integer> formatVersion = |
| 775 | + request.updates().stream() |
| 776 | + .filter((update) -> update instanceof MetadataUpdate.UpgradeFormatVersion) |
| 777 | + .map((update) -> ((MetadataUpdate.UpgradeFormatVersion) update).formatVersion()) |
| 778 | + .findFirst(); |
| 779 | + TableMetadata.Builder builder = |
| 780 | + (TableMetadata.Builder) |
| 781 | + formatVersion |
| 782 | + .map(TableMetadata::buildFromEmpty) |
| 783 | + .orElseGet(TableMetadata::buildFromEmpty); |
| 784 | + request.updates().forEach((update) -> update.applyTo(builder)); |
| 785 | + ops.commit((TableMetadata) null, builder.build()); |
| 786 | + return ops.current(); |
| 787 | + } |
| 788 | + |
| 789 | + // TODO: Clean this up when CatalogHandler become extensible. |
| 790 | + // Copy of CatalogHandler#update |
| 791 | + private static LoadTableResponse updateTableWithRollback( |
| 792 | + Catalog catalog, TableIdentifier ident, UpdateTableRequest request) { |
| 793 | + Schema EMPTY_SCHEMA = new Schema(new Types.NestedField[0]); |
| 794 | + TableMetadata finalMetadata; |
| 795 | + if (isCreate(request)) { |
| 796 | + Transaction transaction = |
| 797 | + catalog.buildTable(ident, EMPTY_SCHEMA).createOrReplaceTransaction(); |
| 798 | + if (!(transaction instanceof BaseTransaction)) { |
| 799 | + throw new IllegalStateException( |
| 800 | + "Cannot wrap catalog that does not produce BaseTransaction"); |
| 801 | + } |
| 802 | + |
| 803 | + BaseTransaction baseTransaction = (BaseTransaction) transaction; |
| 804 | + finalMetadata = create(baseTransaction.underlyingOps(), request); |
| 805 | + } else { |
| 806 | + Table table = catalog.loadTable(ident); |
| 807 | + if (!(table instanceof BaseTable)) { |
| 808 | + throw new IllegalStateException("Cannot wrap catalog that does not produce BaseTable"); |
| 809 | + } |
| 810 | + |
| 811 | + TableOperations ops = ((BaseTable) table).operations(); |
| 812 | + finalMetadata = commit(ops, request); |
| 813 | + } |
| 814 | + |
| 815 | + return LoadTableResponse.builder().withTableMetadata(finalMetadata).build(); |
| 816 | + } |
| 817 | + |
| 818 | + static TableMetadata commit(TableOperations ops, UpdateTableRequest request) { |
| 819 | + AtomicBoolean isRetry = new AtomicBoolean(false); |
| 820 | + |
| 821 | + try { |
| 822 | + Tasks.foreach(new TableOperations[] {ops}) |
| 823 | + .retry(4) |
| 824 | + .exponentialBackoff(100L, 60000L, 1800000L, (double) 2.0F) |
| 825 | + .onlyRetryOn(CommitFailedException.class) |
| 826 | + .run( |
| 827 | + (taskOps) -> { |
| 828 | + TableMetadata base = isRetry.get() ? taskOps.refresh() : taskOps.current(); |
| 829 | + isRetry.set(true); |
| 830 | + // My prev pr : https://github.com/apache/iceberg/pull/5888 |
| 831 | + // Taking this feature behind a table property presently. |
| 832 | + boolean rollbackCompaction = |
| 833 | + PropertyUtil.propertyAsBoolean( |
| 834 | + taskOps.current().properties(), |
| 835 | + "rollback.compaction.on-conflicts.enabled", |
| 836 | + false); |
| 837 | + // otherwise create a metadataUpdate to remove the snapshots we had |
| 838 | + // applied our rollback requests first |
| 839 | + TableMetadata.Builder metadataBuilder = TableMetadata.buildFrom(base); |
| 840 | + TableMetadata newBase = base; |
| 841 | + try { |
| 842 | + request.requirements().forEach((requirement) -> requirement.validate(base)); |
| 843 | + } catch (CommitFailedException e) { |
| 844 | + if (!rollbackCompaction) { |
| 845 | + throw new ValidationFailureException(e); |
| 846 | + } |
| 847 | + // Since snapshot has already been created at the client end. |
| 848 | + // Nothing much can be done, we can move this |
| 849 | + // to writer specific thing, but it would be cool if catalog does this for us. |
| 850 | + // Inspect that the requirements states that snapshot |
| 851 | + // ref needs to be asserted this usually means in the update section |
| 852 | + // it has addSnapshot and setSnapshotRef |
| 853 | + UpdateRequirement.AssertRefSnapshotID addSnapshot = null; |
| 854 | + int found = 0; |
| 855 | + for (UpdateRequirement requirement : request.requirements()) { |
| 856 | + // there should be only add snapshot request |
| 857 | + if (requirement instanceof UpdateRequirement.AssertRefSnapshotID) { |
| 858 | + ++found; |
| 859 | + addSnapshot = (UpdateRequirement.AssertRefSnapshotID) requirement; |
| 860 | + } |
| 861 | + } |
| 862 | + |
| 863 | + if (found != 1) { |
| 864 | + // TODO: handle this case, find min snapshot id, to rollback to give it creates |
| 865 | + // lineage |
| 866 | + // lets not complicate things rn |
| 867 | + throw new ValidationFailureException(e); |
| 868 | + } |
| 869 | + |
| 870 | + Long parentSnapshotId = addSnapshot.snapshotId(); |
| 871 | + // so we will first check all the snapshots on the top of |
| 872 | + // base on which the snapshot we want to commit is of type REPLACE ops. |
| 873 | + Long parentToRollbackTo = ops.current().currentSnapshot().snapshotId(); |
| 874 | + List<MetadataUpdate> updateToRemoveSnapshot = new ArrayList<>(); |
| 875 | + while (!Objects.equals(parentToRollbackTo, parentSnapshotId)) { |
| 876 | + Snapshot snap = ops.current().snapshot(parentToRollbackTo); |
| 877 | + if (!DataOperations.REPLACE.equals(snap.operation())) { |
| 878 | + break; |
| 879 | + } |
| 880 | + updateToRemoveSnapshot.add( |
| 881 | + new MetadataUpdate.RemoveSnapshot(snap.snapshotId())); |
| 882 | + parentToRollbackTo = snap.parentId(); |
| 883 | + } |
| 884 | + |
| 885 | + MetadataUpdate.SetSnapshotRef ref = null; |
| 886 | + // find the SetRefName snapshot update |
| 887 | + for (MetadataUpdate update : request.updates()) { |
| 888 | + if (update instanceof MetadataUpdate.SetSnapshotRef) { |
| 889 | + ++found; |
| 890 | + ref = (MetadataUpdate.SetSnapshotRef) update; |
| 891 | + } |
| 892 | + } |
| 893 | + |
| 894 | + if (found != 1 || (!Objects.equals(parentToRollbackTo, parentSnapshotId))) { |
| 895 | + // nothing can be done as this implies there was a non replace |
| 896 | + // snapshot in between or there is more than setRef ops, we don't know where |
| 897 | + // to go. |
| 898 | + throw new ValidationFailureException(e); |
| 899 | + } |
| 900 | + |
| 901 | + // first we should also set back the ref we wanted to set, back to the base |
| 902 | + // on which the current update is based on. |
| 903 | + metadataBuilder.setBranchSnapshot(parentSnapshotId, ref.name()); |
| 904 | + |
| 905 | + // apply the remove snapshots update in the current metadata. |
| 906 | + // NOTE: we need to setRef to parent first and then apply remove as the remove |
| 907 | + // will drop. The tags / branch which don't have reference. |
| 908 | + // NOTE: we can skip removing the now orphan base. Its not a hard requirement. |
| 909 | + // just something good to do, and not leave for Remove Orphans. |
| 910 | + updateToRemoveSnapshot.forEach((update -> update.applyTo(metadataBuilder))); |
| 911 | + // Ref rolled back update correctly to snapshot to be committed parent now. |
| 912 | + newBase = metadataBuilder.build(); |
| 913 | + } |
| 914 | + |
| 915 | + // double check if the requirements passes now. |
| 916 | + try { |
| 917 | + TableMetadata baseWithRemovedSnaps = newBase; |
| 918 | + request |
| 919 | + .requirements() |
| 920 | + .forEach((requirement) -> requirement.validate(baseWithRemovedSnaps)); |
| 921 | + } catch (CommitFailedException e) { |
| 922 | + throw new ValidationFailureException(e); |
| 923 | + } |
| 924 | + |
| 925 | + TableMetadata.Builder newMetadataBuilder = TableMetadata.buildFrom(newBase); |
| 926 | + request.updates().forEach((update) -> update.applyTo(newMetadataBuilder)); |
| 927 | + TableMetadata updated = newMetadataBuilder.build(); |
| 928 | + // always commit this |
| 929 | + taskOps.commit(base, updated); |
| 930 | + }); |
| 931 | + } catch (ValidationFailureException e) { |
| 932 | + throw e.wrapped(); |
| 933 | + } |
| 934 | + |
| 935 | + return ops.current(); |
| 936 | + } |
| 937 | + |
| 938 | + private static class ValidationFailureException extends RuntimeException { |
| 939 | + private final CommitFailedException wrapped; |
| 940 | + |
| 941 | + private ValidationFailureException(CommitFailedException cause) { |
| 942 | + super(cause); |
| 943 | + this.wrapped = cause; |
| 944 | + } |
| 945 | + |
| 946 | + public CommitFailedException wrapped() { |
| 947 | + return this.wrapped; |
| 948 | + } |
758 | 949 | }
|
759 | 950 |
|
760 | 951 | public LoadTableResponse updateTableForStagedCreate(
|
|
0 commit comments