dimas-b commented on code in PR #6789:
URL: https://github.com/apache/iceberg/pull/6789#discussion_r1122203463


##########
nessie/src/test/java/org/apache/iceberg/nessie/TestMultipleClients.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.nessie;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.AbstractMap;
+import java.util.Collections;
+import org.apache.commons.io.FileUtils;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.projectnessie.client.ext.NessieClientFactory;
+import org.projectnessie.client.ext.NessieClientUri;
+
+public class TestMultipleClients extends BaseTestIceberg {
+
+  private static final String BRANCH = "multiple-clients-test";
+  private static final Schema schema =
+      new Schema(Types.StructType.of(required(1, "id", 
Types.LongType.get())).fields());
+
+  public TestMultipleClients() {
+    super(BRANCH);
+  }
+
+  // another client that connects to the same nessie server.
+  NessieCatalog anotherCatalog;
+
+  @Override
+  @BeforeEach
+  public void beforeEach(NessieClientFactory clientFactory, @NessieClientUri 
URI nessieUri)
+      throws IOException {
+    super.beforeEach(clientFactory, nessieUri);
+    anotherCatalog = initCatalog(branch);
+  }
+
+  @Override
+  @AfterEach
+  public void afterEach() throws Exception {
+    dropTables(catalog);
+    dropTables(anotherCatalog);

Review Comment:
   Why bother? The super class will delete/reset all Nessie data. Table files 
are located in a temp. dir.
   
   If the intention is to remove table's files, wouldn't it be preferable to do 
that in the base class and the FS level for all tests after resetting Nessie?



##########
nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java:
##########
@@ -194,7 +195,7 @@ protected TableOperations newTableOps(TableIdentifier 
tableIdentifier) {
         ContentKey.of(
             
org.projectnessie.model.Namespace.of(tableIdentifier.namespace().levels()),
             tr.getName()),
-        client.withReference(tr.getReference(), tr.getHash()),
+        refreshedClient().withReference(tr.getReference(), tr.getHash()),

Review Comment:
   I believe in this case, since the client goes into `NessieTableOperations`, 
it's reference should be loaded immediately and made immutable regardless of 
whether the commit hash is specified or not. Currently, the reference will be 
loaded on first use, which may happen a lot later. WDYT?



##########
nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java:
##########
@@ -310,19 +312,28 @@ public Configuration getConf() {
 
   @VisibleForTesting
   String currentHash() {
-    return client.getRef().getHash();
+    return refreshedClient().getRef().getHash();
   }
 
   @VisibleForTesting
   String currentRefName() {
-    return client.getRef().getName();
+    return refreshedClient().getRef().getName();
   }
 
   @VisibleForTesting
   FileIO fileIO() {
     return fileIO;
   }
 
+  private NessieIcebergClient refreshedClient() {
+    try {
+      client.refresh();

Review Comment:
   Since this method does not return a new client instance (compared to, e.g., 
`NessieIcebergClient.withReference()`), using it to get the client for further 
actions looks a bit misleading to me. Why not simply call `client.refresh()` 
where we need to?



##########
nessie/src/test/java/org/apache/iceberg/nessie/TestMultipleClients.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.nessie;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.AbstractMap;
+import java.util.Collections;
+import org.apache.commons.io.FileUtils;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.projectnessie.client.ext.NessieClientFactory;
+import org.projectnessie.client.ext.NessieClientUri;
+
+public class TestMultipleClients extends BaseTestIceberg {
+
+  private static final String BRANCH = "multiple-clients-test";
+  private static final Schema schema =
+      new Schema(Types.StructType.of(required(1, "id", 
Types.LongType.get())).fields());
+
+  public TestMultipleClients() {
+    super(BRANCH);
+  }
+
+  // another client that connects to the same nessie server.
+  NessieCatalog anotherCatalog;
+
+  @Override
+  @BeforeEach
+  public void beforeEach(NessieClientFactory clientFactory, @NessieClientUri 
URI nessieUri)
+      throws IOException {
+    super.beforeEach(clientFactory, nessieUri);
+    anotherCatalog = initCatalog(branch);
+  }
+
+  @Override
+  @AfterEach
+  public void afterEach() throws Exception {
+    dropTables(catalog);
+    dropTables(anotherCatalog);
+
+    super.afterEach();
+    anotherCatalog.close();
+  }
+
+  @Test
+  public void testListNamespaces() {
+    catalog.createNamespace(Namespace.of("db1"), Collections.emptyMap());
+    
Assertions.assertThat(catalog.listNamespaces()).containsExactlyInAnyOrder(Namespace.of("db1"));
+
+    // another client creates a namespace with the same nessie server
+    anotherCatalog.createNamespace(Namespace.of("db2"), 
Collections.emptyMap());
+    Assertions.assertThat(anotherCatalog.listNamespaces())
+        .containsExactlyInAnyOrder(Namespace.of("db1"), Namespace.of("db2"));
+
+    Assertions.assertThat(catalog.listNamespaces())
+        .containsExactlyInAnyOrder(Namespace.of("db1"), Namespace.of("db2"));
+  }
+
+  @Test
+  public void testLoadNamespaceMetadata() {
+    catalog.createNamespace(Namespace.of("namespace1"), 
Collections.emptyMap());
+    Assertions.assertThat(catalog.listNamespaces())
+        .containsExactlyInAnyOrder(Namespace.of("namespace1"));
+
+    // another client adds a metadata to the same namespace
+    anotherCatalog.setProperties(Namespace.of("namespace1"), 
Collections.singletonMap("k1", "v1"));
+    AbstractMap.SimpleEntry<String, String> entry = new 
AbstractMap.SimpleEntry<>("k1", "v1");
+    
Assertions.assertThat(anotherCatalog.loadNamespaceMetadata(Namespace.of("namespace1")))
+        .containsExactly(entry);
+
+    
Assertions.assertThat(catalog.loadNamespaceMetadata(Namespace.of("namespace1")))
+        .containsExactly(entry);
+  }
+
+  @Test
+  public void testListTables() {
+    catalog.createTable(TableIdentifier.parse("foo.tbl1"), schema);
+    Assertions.assertThat(catalog.listTables(Namespace.of("foo")))
+        .containsExactlyInAnyOrder(TableIdentifier.parse("foo.tbl1"));
+
+    // another client creates a table with the same nessie server
+    anotherCatalog.createTable(TableIdentifier.parse("foo.tbl2"), schema);
+    Assertions.assertThat(anotherCatalog.listTables(Namespace.of("foo")))
+        .containsExactlyInAnyOrder(
+            TableIdentifier.parse("foo.tbl1"), 
TableIdentifier.parse("foo.tbl2"));
+
+    Assertions.assertThat(catalog.listTables(Namespace.of("foo")))
+        .containsExactlyInAnyOrder(
+            TableIdentifier.parse("foo.tbl1"), 
TableIdentifier.parse("foo.tbl2"));
+  }
+
+  @Test
+  public void testCommits() {
+    TableIdentifier identifier = TableIdentifier.parse("foo.tbl1");
+    catalog.createTable(identifier, schema);
+    Table tableFromCatalog = catalog.loadTable(identifier);
+    tableFromCatalog.updateSchema().addColumn("x1", 
Types.LongType.get()).commit();
+
+    Table tableFromAnotherCatalog = anotherCatalog.loadTable(identifier);
+    tableFromAnotherCatalog.updateSchema().addColumn("x2", 
Types.LongType.get()).commit();
+
+    tableFromCatalog.updateSchema().addColumn("x3", 
Types.LongType.get()).commit();
+    tableFromAnotherCatalog.updateSchema().addColumn("x4", 
Types.LongType.get()).commit();
+
+    
Assertions.assertThat(catalog.loadTable(identifier).schema().columns()).hasSize(5);
+    
Assertions.assertThat(anotherCatalog.loadTable(identifier).schema().columns()).hasSize(5);
+  }
+
+  @Test
+  public void testConcurrentCommitsWithRefresh() {
+    TableIdentifier identifier = TableIdentifier.parse("foo.tbl1");
+    catalog.createTable(identifier, schema);
+
+    String hashBefore = catalog.currentHash();
+
+    TableOperations ops1 = catalog.newTableOps(identifier);
+    TableMetadata current1 =

Review Comment:
   nit: `current1` -> `metadata1`



##########
nessie/src/test/java/org/apache/iceberg/nessie/TestMultipleClients.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.nessie;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.AbstractMap;
+import java.util.Collections;
+import org.apache.commons.io.FileUtils;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.projectnessie.client.ext.NessieClientFactory;
+import org.projectnessie.client.ext.NessieClientUri;
+
+public class TestMultipleClients extends BaseTestIceberg {
+
+  private static final String BRANCH = "multiple-clients-test";
+  private static final Schema schema =
+      new Schema(Types.StructType.of(required(1, "id", 
Types.LongType.get())).fields());
+
+  public TestMultipleClients() {
+    super(BRANCH);
+  }
+
+  // another client that connects to the same nessie server.
+  NessieCatalog anotherCatalog;
+
+  @Override
+  @BeforeEach
+  public void beforeEach(NessieClientFactory clientFactory, @NessieClientUri 
URI nessieUri)
+      throws IOException {
+    super.beforeEach(clientFactory, nessieUri);
+    anotherCatalog = initCatalog(branch);
+  }
+
+  @Override
+  @AfterEach
+  public void afterEach() throws Exception {
+    dropTables(catalog);
+    dropTables(anotherCatalog);
+
+    super.afterEach();

Review Comment:
   Why override `afterEach`? This test class can have its own `@AfterEach` 
method for closing the catalog.



##########
nessie/src/test/java/org/apache/iceberg/nessie/TestMultipleClients.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.nessie;
+
+import static org.apache.iceberg.types.Types.NestedField.required;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.AbstractMap;
+import java.util.Collections;
+import org.apache.commons.io.FileUtils;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableOperations;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.CommitFailedException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Types;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.projectnessie.client.ext.NessieClientFactory;
+import org.projectnessie.client.ext.NessieClientUri;
+
+public class TestMultipleClients extends BaseTestIceberg {
+
+  private static final String BRANCH = "multiple-clients-test";
+  private static final Schema schema =
+      new Schema(Types.StructType.of(required(1, "id", 
Types.LongType.get())).fields());
+
+  public TestMultipleClients() {
+    super(BRANCH);
+  }
+
+  // another client that connects to the same nessie server.
+  NessieCatalog anotherCatalog;
+
+  @Override
+  @BeforeEach
+  public void beforeEach(NessieClientFactory clientFactory, @NessieClientUri 
URI nessieUri)
+      throws IOException {
+    super.beforeEach(clientFactory, nessieUri);
+    anotherCatalog = initCatalog(branch);
+  }
+
+  @Override
+  @AfterEach
+  public void afterEach() throws Exception {
+    dropTables(catalog);
+    dropTables(anotherCatalog);
+
+    super.afterEach();
+    anotherCatalog.close();
+  }
+
+  @Test
+  public void testListNamespaces() {
+    catalog.createNamespace(Namespace.of("db1"), Collections.emptyMap());
+    
Assertions.assertThat(catalog.listNamespaces()).containsExactlyInAnyOrder(Namespace.of("db1"));
+
+    // another client creates a namespace with the same nessie server
+    anotherCatalog.createNamespace(Namespace.of("db2"), 
Collections.emptyMap());
+    Assertions.assertThat(anotherCatalog.listNamespaces())
+        .containsExactlyInAnyOrder(Namespace.of("db1"), Namespace.of("db2"));
+
+    Assertions.assertThat(catalog.listNamespaces())
+        .containsExactlyInAnyOrder(Namespace.of("db1"), Namespace.of("db2"));
+  }
+
+  @Test
+  public void testLoadNamespaceMetadata() {
+    catalog.createNamespace(Namespace.of("namespace1"), 
Collections.emptyMap());
+    Assertions.assertThat(catalog.listNamespaces())
+        .containsExactlyInAnyOrder(Namespace.of("namespace1"));
+
+    // another client adds a metadata to the same namespace
+    anotherCatalog.setProperties(Namespace.of("namespace1"), 
Collections.singletonMap("k1", "v1"));
+    AbstractMap.SimpleEntry<String, String> entry = new 
AbstractMap.SimpleEntry<>("k1", "v1");
+    
Assertions.assertThat(anotherCatalog.loadNamespaceMetadata(Namespace.of("namespace1")))
+        .containsExactly(entry);
+
+    
Assertions.assertThat(catalog.loadNamespaceMetadata(Namespace.of("namespace1")))
+        .containsExactly(entry);
+  }
+
+  @Test
+  public void testListTables() {
+    catalog.createTable(TableIdentifier.parse("foo.tbl1"), schema);
+    Assertions.assertThat(catalog.listTables(Namespace.of("foo")))
+        .containsExactlyInAnyOrder(TableIdentifier.parse("foo.tbl1"));
+
+    // another client creates a table with the same nessie server
+    anotherCatalog.createTable(TableIdentifier.parse("foo.tbl2"), schema);
+    Assertions.assertThat(anotherCatalog.listTables(Namespace.of("foo")))
+        .containsExactlyInAnyOrder(
+            TableIdentifier.parse("foo.tbl1"), 
TableIdentifier.parse("foo.tbl2"));
+
+    Assertions.assertThat(catalog.listTables(Namespace.of("foo")))
+        .containsExactlyInAnyOrder(
+            TableIdentifier.parse("foo.tbl1"), 
TableIdentifier.parse("foo.tbl2"));
+  }
+
+  @Test
+  public void testCommits() {
+    TableIdentifier identifier = TableIdentifier.parse("foo.tbl1");
+    catalog.createTable(identifier, schema);
+    Table tableFromCatalog = catalog.loadTable(identifier);
+    tableFromCatalog.updateSchema().addColumn("x1", 
Types.LongType.get()).commit();
+
+    Table tableFromAnotherCatalog = anotherCatalog.loadTable(identifier);
+    tableFromAnotherCatalog.updateSchema().addColumn("x2", 
Types.LongType.get()).commit();
+
+    tableFromCatalog.updateSchema().addColumn("x3", 
Types.LongType.get()).commit();
+    tableFromAnotherCatalog.updateSchema().addColumn("x4", 
Types.LongType.get()).commit();
+
+    
Assertions.assertThat(catalog.loadTable(identifier).schema().columns()).hasSize(5);
+    
Assertions.assertThat(anotherCatalog.loadTable(identifier).schema().columns()).hasSize(5);
+  }
+
+  @Test
+  public void testConcurrentCommitsWithRefresh() {
+    TableIdentifier identifier = TableIdentifier.parse("foo.tbl1");
+    catalog.createTable(identifier, schema);
+
+    String hashBefore = catalog.currentHash();
+
+    TableOperations ops1 = catalog.newTableOps(identifier);
+    TableMetadata current1 =
+        
TableMetadata.buildFrom(ops1.current()).setProperties(ImmutableMap.of("k1", 
"v1")).build();
+
+    // commit should succeed
+    TableOperations ops2 = catalog.newTableOps(identifier);
+    TableMetadata current2 =

Review Comment:
   nit: `current2` -> `metadata2`



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to