rdblue commented on a change in pull request #666: BaseRewriteManifests should 
keep different manifest for different partitionSpec
URL: https://github.com/apache/incubator-iceberg/pull/666#discussion_r361016556
 
 

 ##########
 File path: core/src/main/java/org/apache/iceberg/BaseRewriteManifests.java
 ##########
 @@ -312,33 +311,62 @@ long getManifestTargetSizeBytes() {
   }
 
   class WriterWrapper {
-    private ManifestWriter writer;
-
-    synchronized void addEntry(ManifestEntry entry) {
-      if (writer == null) {
-        writer = newWriter();
-      } else if (writer.length() >= getManifestTargetSizeBytes()) {
-        close();
-        writer = newWriter();
+    private final Map<Integer, ManifestWriter> manifestWritersBySpecId = 
Collections.synchronizedMap(new HashMap<>());
+
+    synchronized void addEntry(ManifestEntry entry, int partitionSpecId) {
+      ManifestWriter manifestWriter = 
manifestWritersBySpecId.get(partitionSpecId);
+      if (manifestWriter == null) {
+        manifestWriter = newWriter(partitionSpecId);
+      } else if (manifestWriter.length() >= getManifestTargetSizeBytes()) {
+        close(partitionSpecId);
+        manifestWriter = newWriter(partitionSpecId);
       }
-      writer.existing(entry);
+      manifestWriter.existing(entry);
     }
 
-    private ManifestWriter newWriter() {
-      return new ManifestWriter(spec, 
manifestPath(manifestSuffix.getAndIncrement()), snapshotId());
+    private ManifestWriter newWriter(int partitionSpecId) {
+
+      if (manifestWritersBySpecId.containsKey(partitionSpecId)) {
+        // should not reach here, but checking to avoid resource leak.
+        throw new RuntimeException("ManifestWriter already exists for this 
partition-specId " + partitionSpecId);
 
 Review comment:
   I think we can avoid the need for this by doing the caching in the same 
method that creates writers. Instead of checking the writer map before calling 
this method, this method should handle it:
   
   ```java
   synchronized void addEntry(ManifestEntry entry, int partitionSpecId) {
     getWriter(partitionSpecId).existing(entry);
   }
   
   synchronized ManifestWriter getWriter(int partitionSpecId) {
     ManifestWriter writer = manifestWritersBySpecId.get(partitionSpecId);
     if (writer != null) {
       if (writer.length() < targetSizeBytes) {
         return writer;
       } else {
         close(writer);
       }
     }
   
     ...
     writer = new ManifestWriter(partitionSpec, outputFile, snapshotId());
     manifestWritersbySpecId.put(partitionSpecId, manifestWriter);
     return manifestWriter;
   }
   ```
   
   That removes the dependency between the methods, which is cleaner than an 
assertion.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to