timothyw553 commented on code in PR #19101:
URL: https://github.com/apache/iceberg/pull/19101#discussion_r4707842336


##########
core/src/test/java/org/apache/iceberg/io/TestFileIOParser.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.io;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.StringWriter;
+import org.apache.iceberg.TestCatalogUtil.TestFileIONoArg;
+import org.apache.iceberg.TestCatalogUtil.TestFileIOWithStorageCredentials;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.JsonUtil;
+import org.junit.jupiter.api.Test;
+
+class TestFileIOParser {
+
+  @Test
+  void nullCheck() throws Exception {
+    assertThatThrownBy(() -> FileIOParser.toJson(null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Invalid FileIO: null");
+
+    StringWriter writer = new StringWriter();
+    try (JsonGenerator generator = JsonUtil.factory().createGenerator(writer)) 
{
+      assertThatThrownBy(() -> FileIOParser.toJson(null, generator))
+          .isInstanceOf(IllegalArgumentException.class)
+          .hasMessage("Invalid FileIO: null");
+
+      assertThatThrownBy(() -> FileIOParser.toJson(new 
TestFileIOWithStorageCredentials(), null))
+          .isInstanceOf(IllegalArgumentException.class)
+          .hasMessage("Invalid JSON generator: null");
+    }
+
+    assertThatThrownBy(() -> FileIOParser.fromJson((JsonNode) null, null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse FileIO from null object");
+  }
+
+  @Test
+  void invalidJson() throws Exception {
+    JsonNode arrayNode = JsonUtil.mapper().readTree("[]");
+    assertThatThrownBy(() -> FileIOParser.fromJson(arrayNode, null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse FileIO from non-object: []");
+
+    assertThatThrownBy(() -> FileIOParser.fromJson("{}", null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing string: io-impl");
+
+    assertThatThrownBy(() -> FileIOParser.fromJson("{\"io-impl\": \"foo\"}", 
null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse missing map: properties");
+
+    String invalidArrayJson =
+        String.format(
+            
"{\"io-impl\":\"%s\",\"properties\":{},\"storage-credentials\":\"invalid\"}",
+            TestFileIOWithStorageCredentials.class.getName());
+    assertThatThrownBy(() -> FileIOParser.fromJson(invalidArrayJson, null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse storage credentials from non-array: 
\"invalid\"");
+
+    String nullCredentialsJson =
+        String.format(
+            
"{\"io-impl\":\"%s\",\"properties\":{},\"storage-credentials\":null}",
+            TestFileIOWithStorageCredentials.class.getName());
+    assertThatThrownBy(() -> FileIOParser.fromJson(nullCredentialsJson, null))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse storage credentials from non-array: null");
+  }
+
+  @Test
+  void unsupportedProperties() {
+    assertThatThrownBy(() -> FileIOParser.toJson(new TestFileIONoArg()))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage(
+            String.format(
+                "Cannot serialize FileIO: %s does not expose configuration 
properties",
+                TestFileIONoArg.class.getName()));
+  }
+
+  @Test
+  void roundTripWithoutCredentials() {
+    TestFileIOWithStorageCredentials fileIO = new 
TestFileIOWithStorageCredentials();
+    fileIO.initialize(ImmutableMap.of("k1", "v1", "k2", "v2"));
+
+    String json = FileIOParser.toJson(fileIO);
+    assertThat(json).doesNotContain("storage-credentials");
+
+    try (FileIO deserialized = FileIOParser.fromJson(json)) {
+      
assertThat(deserialized).isInstanceOf(TestFileIOWithStorageCredentials.class);
+      assertThat(deserialized.properties()).isEqualTo(fileIO.properties());
+      assertThat(((SupportsStorageCredentials) 
deserialized).credentials()).isEmpty();
+    }
+  }
+
+  @Test
+  void roundTripWithCredentials() {
+    TestFileIOWithStorageCredentials fileIO = new 
TestFileIOWithStorageCredentials();
+    fileIO.initialize(ImmutableMap.of("k1", "v1", "k2", "v2"));
+
+    StorageCredential cred1 =
+        StorageCredential.create(
+            "s3://bucket1",
+            ImmutableMap.of("s3.access-key-id", "key1", 
"s3.secret-access-key", "secret1"));
+    StorageCredential cred2 =
+        StorageCredential.create(
+            "gs://bucket2",
+            ImmutableMap.of("gcs.oauth2.token", "token2", 
"gcs.oauth2.token-expires-at", "1000"));
+    fileIO.setCredentials(ImmutableList.of(cred1, cred2));
+
+    String json = FileIOParser.toJson(fileIO, true);

Review Comment:
   nit: only checking `contains("storage-credentials")` wouldn't catch 
unintended changes to the serialized JSON format if the writer and reader 
change together. could we also assert the full `expectedJson` like 
`TestStorageCredentialParser` does?
   ```diff
   -    String json = FileIOParser.toJson(fileIO, true);
   -    assertThat(json).contains("storage-credentials");
   +    String json = FileIOParser.toJson(fileIO);
   +    assertThat(json).isEqualTo(expectedJson);
   ```



##########
core/src/main/java/org/apache/iceberg/io/FileIOParser.java:
##########
@@ -73,9 +90,25 @@ public static FileIO fromJson(String json, Object conf) {
   }
 
   public static FileIO fromJson(JsonNode json, Object conf) {
+    Preconditions.checkArgument(json != null, "Cannot parse FileIO from null 
object");
     Preconditions.checkArgument(json.isObject(), "Cannot parse FileIO from 
non-object: %s", json);
     String impl = JsonUtil.getString(FILE_IO_IMPL, json);
     Map<String, String> properties = JsonUtil.getStringMap(PROPERTIES, json);
-    return CatalogUtil.loadFileIO(impl, properties, conf);
+
+    List<StorageCredential> credentials = ImmutableList.of();
+    if (json.has(STORAGE_CREDENTIALS)) {
+      JsonNode credentialsNode = json.get(STORAGE_CREDENTIALS);
+      Preconditions.checkArgument(
+          credentialsNode != null && credentialsNode.isArray(),
+          "Cannot parse storage credentials from non-array: %s",
+          credentialsNode);
+      ImmutableList.Builder<StorageCredential> builder = 
ImmutableList.builder();
+      for (JsonNode credentialNode : credentialsNode) {
+        builder.add(StorageCredentialParser.fromJson(credentialNode));
+      }
+      credentials = builder.build();
+    }
+
+    return CatalogUtil.loadFileIO(impl, properties, conf, credentials);

Review Comment:
   to my understanding, passing credentials here lets S3 and GCS `FileIO` 
schedule background credential refresh, while callers like Flink's metadata 
reader close the row iterator without closing the restored `FileIO`. could that 
keep completed tasks' `FileIO` objects and refresh threads alive in 
long-running jobs? we shouldn't just call `io.close()` unconditionally in 
`rows()` since driver-side tasks share `table.io()`, so should we track 
ownership for parser-created `FileIO` instances and close them when the 
consumer finishes or fails?



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