This is an automated email from the ASF dual-hosted git repository.

etudenhoefner pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new 0627042839 Core: Make InMemoryFileIO map shared across instances 
(#9722)
0627042839 is described below

commit 062704283918ae843d0e40b3f279f1482f0835e5
Author: Drew Gallardo <[email protected]>
AuthorDate: Fri Feb 16 03:46:22 2024 -0800

    Core: Make InMemoryFileIO map shared across instances (#9722)
---
 .../org/apache/iceberg/inmemory/InMemoryFileIO.java | 10 +++++-----
 .../apache/iceberg/inmemory/TestInMemoryFileIO.java | 21 ++++++++++++++++++++-
 .../org/apache/iceberg/rest/TestRESTCatalog.java    |  7 ++++++-
 3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/inmemory/InMemoryFileIO.java 
b/core/src/main/java/org/apache/iceberg/inmemory/InMemoryFileIO.java
index 056929a201..19208a1e0c 100644
--- a/core/src/main/java/org/apache/iceberg/inmemory/InMemoryFileIO.java
+++ b/core/src/main/java/org/apache/iceberg/inmemory/InMemoryFileIO.java
@@ -28,22 +28,22 @@ import 
org.apache.iceberg.relocated.com.google.common.collect.Maps;
 
 public class InMemoryFileIO implements FileIO {
 
-  private final Map<String, byte[]> inMemoryFiles = Maps.newConcurrentMap();
+  private static final Map<String, byte[]> IN_MEMORY_FILES = 
Maps.newConcurrentMap();
   private boolean closed = false;
 
   public void addFile(String location, byte[] contents) {
     Preconditions.checkState(!closed, "Cannot call addFile after calling 
close()");
-    inMemoryFiles.put(location, contents);
+    IN_MEMORY_FILES.put(location, contents);
   }
 
   public boolean fileExists(String location) {
-    return inMemoryFiles.containsKey(location);
+    return IN_MEMORY_FILES.containsKey(location);
   }
 
   @Override
   public InputFile newInputFile(String location) {
     Preconditions.checkState(!closed, "Cannot call newInputFile after calling 
close()");
-    byte[] contents = inMemoryFiles.get(location);
+    byte[] contents = IN_MEMORY_FILES.get(location);
     if (null == contents) {
       throw new NotFoundException("No in-memory file found for location: %s", 
location);
     }
@@ -59,7 +59,7 @@ public class InMemoryFileIO implements FileIO {
   @Override
   public void deleteFile(String location) {
     Preconditions.checkState(!closed, "Cannot call deleteFile after calling 
close()");
-    if (null == inMemoryFiles.remove(location)) {
+    if (null == IN_MEMORY_FILES.remove(location)) {
       throw new NotFoundException("No in-memory file found for location: %s", 
location);
     }
   }
diff --git 
a/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryFileIO.java 
b/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryFileIO.java
index 5f9ec3fbf2..12f5bf84cc 100644
--- a/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryFileIO.java
+++ b/core/src/test/java/org/apache/iceberg/inmemory/TestInMemoryFileIO.java
@@ -21,17 +21,18 @@ package org.apache.iceberg.inmemory;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.UUID;
 import org.apache.iceberg.exceptions.AlreadyExistsException;
 import org.apache.iceberg.exceptions.NotFoundException;
 import org.assertj.core.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 public class TestInMemoryFileIO {
-  String location = "s3://foo/bar.txt";
 
   @Test
   public void testBasicEndToEnd() throws IOException {
     InMemoryFileIO fileIO = new InMemoryFileIO();
+    String location = randomLocation();
     Assertions.assertThat(fileIO.fileExists(location)).isFalse();
 
     OutputStream outputStream = fileIO.newOutputFile(location).create();
@@ -66,6 +67,7 @@ public class TestInMemoryFileIO {
 
   @Test
   public void testCreateNoOverwrite() {
+    String location = randomLocation();
     InMemoryFileIO fileIO = new InMemoryFileIO();
     fileIO.addFile(location, "hello world".getBytes());
     Assertions.assertThatExceptionOfType(AlreadyExistsException.class)
@@ -74,6 +76,7 @@ public class TestInMemoryFileIO {
 
   @Test
   public void testOverwriteBeforeAndAfterClose() throws IOException {
+    String location = randomLocation();
     byte[] oldData = "old data".getBytes();
     byte[] newData = "new data".getBytes();
 
@@ -108,4 +111,20 @@ public class TestInMemoryFileIO {
     inputStream.close();
     Assertions.assertThat(new String(buf)).isEqualTo("new data");
   }
+
+  @Test
+  public void testFilesAreSharedAcrossMultipleInstances() {
+    String location = randomLocation();
+    InMemoryFileIO fileIO = new InMemoryFileIO();
+    fileIO.addFile(location, "hello world".getBytes());
+
+    InMemoryFileIO fileIO2 = new InMemoryFileIO();
+    Assertions.assertThat(fileIO2.fileExists(location))
+        .isTrue()
+        .as("Files should be shared across all InMemoryFileIO instances");
+  }
+
+  private String randomLocation() {
+    return "s3://foo/" + UUID.randomUUID();
+  }
 }
diff --git a/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java 
b/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java
index f196787d56..34b3e22222 100644
--- a/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java
+++ b/core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java
@@ -180,7 +180,12 @@ public class TestRESTCatalog extends 
CatalogTests<RESTCatalog> {
     restCatalog.initialize(
         "prod",
         ImmutableMap.of(
-            CatalogProperties.URI, httpServer.getURI().toString(), 
"credential", "catalog:12345"));
+            CatalogProperties.URI,
+            httpServer.getURI().toString(),
+            CatalogProperties.FILE_IO_IMPL,
+            "org.apache.iceberg.inmemory.InMemoryFileIO",
+            "credential",
+            "catalog:12345"));
   }
 
   @SuppressWarnings("unchecked")

Reply via email to