yihua commented on code in PR #894: URL: https://github.com/apache/incubator-xtable/pull/894#discussion_r4225249292
########## xtable-hudi-support/xtable-iceberg-pluggable-tf/src/main/java/org/apache/xtable/timeline/IcebergRollbackExecutor.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.xtable.timeline; + +import lombok.SneakyThrows; +import lombok.extern.log4j.Log4j2; + +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.InstantComparison; +import org.apache.hudi.common.table.timeline.dto.InstantDTO; + +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.TableIdentifier; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + +import org.apache.xtable.iceberg.IcebergConversionTarget; +import org.apache.xtable.iceberg.IcebergTableManager; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; + +@Log4j2 +public class IcebergRollbackExecutor { + private static final ObjectMapper MAPPER = + new ObjectMapper() + .registerModule(new JavaTimeModule()) + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) + .setSerializationInclusion(JsonInclude.Include.NON_NULL); + + private final HoodieTableMetaClient metaClient; + private final IcebergConversionTarget target; + private final IcebergTableManager tableManager; + + public IcebergRollbackExecutor(HoodieTableMetaClient metaClient, IcebergConversionTarget target) { + this.metaClient = metaClient; + this.target = target; + this.tableManager = + IcebergTableManager.of( + (org.apache.hadoop.conf.Configuration) metaClient.getStorageConf().unwrap()); + } + + @SneakyThrows + public void rollbackSnapshot(InternalTable internalTable, HoodieInstant instantToRollback) { + TableIdentifier tableIdentifier = + TableIdentifier.of(metaClient.getTableConfig().getTableName()); + if (tableManager.tableExists(null, tableIdentifier, metaClient.getBasePath().toString())) { + Table table = + tableManager.getTable(null, tableIdentifier, metaClient.getBasePath().toString()); + TableSyncMetadata syncMetadata = + TableSyncMetadata.fromJson( + table.currentSnapshot().summary().get(TableSyncMetadata.XTABLE_METADATA)) + .get(); + HoodieInstant latestHoodieInstantInIceberg = + InstantDTO.toInstant( + MAPPER.readValue(syncMetadata.getLatestTableOperationIdentifier(), InstantDTO.class), + metaClient.getInstantGenerator()); + if (latestHoodieInstantInIceberg.equals(instantToRollback)) { + // The instant to rollback is committed in iceberg, so rollback to previous snapshot. + // NOTE: This is equivalent to hudi restore and should be performed by killing all active + // writers. + target.beginSync(internalTable); + target.rollbackToSnapshotId(table.currentSnapshot().snapshotId()); Review Comment: Fixed. Rolling back the instant the current snapshot records now calls `rollbackTo(parentId)` and fails when there is no parent; `testsForRollbacks` asserts Iceberg plans exactly the base files Hudi kept and that each one exists on storage. -- 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]
