nastra commented on code in PR #9852:
URL: https://github.com/apache/iceberg/pull/9852#discussion_r1758467635


##########
hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveViewCommits.java:
##########
@@ -0,0 +1,442 @@
+/*
+ * 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.iceberg.hive;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyBoolean;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hadoop.fs.Path;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.CommitStateUnknownException;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.view.BaseView;
+import org.apache.iceberg.view.View;
+import org.apache.iceberg.view.ViewMetadata;
+import org.apache.thrift.TException;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+/** Test Hive locks and Hive errors and retry during commits. */
+public class TestHiveViewCommits {
+
+  private static final String VIEW_NAME = "test_iceberg_view";
+  private static final String DB_NAME = "hivedb";
+  private static final Namespace NS = Namespace.of(DB_NAME);
+  private static final Schema SCHEMA =
+      new Schema(
+          5,
+          required(3, "id", Types.IntegerType.get(), "unique ID"),
+          required(4, "data", Types.StringType.get()));
+  private static final TableIdentifier VIEW_IDENTIFIER = 
TableIdentifier.of(NS, VIEW_NAME);
+
+  @RegisterExtension
+  protected static final HiveMetastoreExtension HIVE_METASTORE_EXTENSION =
+      HiveMetastoreExtension.builder().withDatabase(DB_NAME).build();
+
+  private View view;
+  private Path viewLocation;
+
+  private static HiveCatalog catalog;
+
+  @BeforeAll
+  public static void initCatalog() {
+    catalog =
+        (HiveCatalog)
+            CatalogUtil.loadCatalog(
+                HiveCatalog.class.getName(),
+                CatalogUtil.ICEBERG_CATALOG_TYPE_HIVE,
+                ImmutableMap.of(
+                    CatalogProperties.CLIENT_POOL_CACHE_EVICTION_INTERVAL_MS,
+                    String.valueOf(TimeUnit.SECONDS.toMillis(10))),
+                HIVE_METASTORE_EXTENSION.hiveConf());
+  }
+
+  @BeforeEach
+  public void createTestView() {
+    view =
+        catalog
+            .buildView(VIEW_IDENTIFIER)
+            .withSchema(SCHEMA)
+            .withDefaultNamespace(NS)
+            .withQuery("hive", "select * from ns.tbl")
+            .create();
+    viewLocation = new Path(view.location());
+  }
+
+  @AfterEach
+  public void dropTestView() throws IOException {
+    
viewLocation.getFileSystem(HIVE_METASTORE_EXTENSION.hiveConf()).delete(viewLocation,
 true);
+    catalog.dropView(VIEW_IDENTIFIER);
+  }
+
+  @Test
+  public void testSuppressUnlockExceptions() {
+    HiveViewOperations ops = (HiveViewOperations) ((BaseView) 
view).operations();
+    ViewMetadata metadataV1 = ops.current();
+    assertThat(ops.current().properties()).hasSize(0);
+
+    view.updateProperties().set("k1", "v1").commit();
+    ops.refresh();
+    ViewMetadata metadataV2 = ops.current();
+    assertThat(ops.current().properties()).hasSize(1).containsEntry("k1", 
"v1");
+
+    HiveViewOperations spyOps = spy(ops);
+
+    AtomicReference<HiveLock> lockRef = new AtomicReference<>();
+
+    when(spyOps.lockObject())
+        .thenAnswer(
+            i -> {
+              HiveLock lock = (HiveLock) i.callRealMethod();
+              lockRef.set(lock);
+              return lock;
+            });
+
+    try {
+      spyOps.commit(metadataV2, metadataV1);
+      HiveLock spyLock = spy(lockRef.get());
+      doThrow(new RuntimeException()).when(spyLock).unlock();
+    } finally {
+      lockRef.get().unlock();
+    }
+
+    ops.refresh();
+
+    // the commit must succeed
+    assertThat(ops.current().properties()).hasSize(0);
+  }
+
+  /**
+   * Pretends we throw an error while persisting, and not found with check 
state, commit state
+   * should be treated as unknown, because in reality the persisting may still 
succeed, just not yet
+   * by the time of checking.
+   */
+  @Test
+  public void testThriftExceptionUnknownStateIfNotInHistoryFailureOnCommit()
+      throws TException, InterruptedException {
+    HiveViewOperations ops = (HiveViewOperations) ((BaseView) 
view).operations();
+    assertThat(ops.current().properties()).hasSize(0);
+
+    view.updateProperties().set("k1", "v1").commit();
+    ops.refresh();
+    ViewMetadata metadataV2 = ops.current();
+    assertThat(ops.current().properties()).hasSize(1).containsEntry("k1", 
"v1");
+
+    HiveViewOperations spyOps = spy(ops);
+
+    failCommitAndThrowException(spyOps);
+
+    ops.refresh();
+
+    assertThat(ops.current()).as("Current metadata should not have 
changed").isEqualTo(metadataV2);
+    assertThat(metadataFileExists(metadataV2)).as("Current metadata should 
still exist").isTrue();
+    assertThat(metadataFileCount(ops.current()))
+        .as(
+            "New metadata files should still exist, new location not in 
history but"
+                + " the commit may still succeed")
+        .isEqualTo(2);
+  }
+
+  /** Pretends we throw an error while persisting that actually does commit 
serverside. */
+  @Test
+  public void testThriftExceptionSuccessOnCommit() throws TException, 
InterruptedException {

Review Comment:
   please update this one to 
   ```
   @Test
     public void testThriftExceptionSuccessOnCommit() throws TException, 
InterruptedException {
       HiveViewOperations ops = (HiveViewOperations) ((BaseView) 
view).operations();
   
       ViewMetadata metadataV1 = ops.current();
       assertThat(metadataV1.properties()).hasSize(0);
   
       view.updateProperties().set("k1", "v1").commit();
       ops.refresh();
       ViewMetadata metadataV2 = ops.current();
       assertThat(metadataV2.properties()).hasSize(1).containsEntry("k1", "v1");
   
       HiveViewOperations spyOps = spy(ops);
   
       // Simulate a communication error after a successful commit
       commitAndThrowException(ops, spyOps);
       // Shouldn't throw because the commit succeeds even though an exception 
is thrown
       spyOps.commit(metadataV2, metadataV1);
   
       assertThat(ops.current()).as("Current metadata should have not 
changed").isEqualTo(metadataV2);
       assertThat(metadataFileExists(ops.current()))
           .as("Current metadata file should still exist")
           .isTrue();
     }
   ```



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to