adoroszlai commented on code in PR #7164:
URL: https://github.com/apache/ozone/pull/7164#discussion_r1805917753


##########
hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/OzoneClientUtils.java:
##########
@@ -280,4 +290,53 @@ public static void deleteSnapshot(ObjectStore objectStore,
           exception.getMessage());
     }
   }
+
+  public static void setReplication(OzoneConfiguration conf, OzoneBucket 
bucket,
+      String keyName, short replication) throws IOException {
+    OzoneKeyDetails keyDetails;
+    try {
+      keyDetails = bucket.getKey(keyName);
+    } catch (OMException ome) {
+      // if key does not exist, do nothing
+      if (ome.getResult() == KEY_NOT_FOUND) {
+        return;
+      }
+      throw ome;
+    }
+    ReplicationConfig newReplication =
+        resolveClientSideReplicationConfig(replication, null,
+            null, conf);
+    if (newReplication == null) {
+      throw new IOException(
+          "Replication factor of " + replication + " not supported");
+    }
+    if (newReplication.getReplication()
+        .equals(keyDetails.getReplicationConfig().getReplication())) {
+      return;
+    }
+    try (InputStream inputStream = readFile(bucket, keyName);
+        OzoneOutputStream outputStream = bucket.rewriteKey(keyName,
+            keyDetails.getDataSize(), keyDetails.getGeneration(),
+            newReplication, keyDetails.getMetadata())) {
+      int chunkSize = (int) conf.getStorageSize(OZONE_SCM_CHUNK_SIZE_KEY,
+          OZONE_SCM_CHUNK_SIZE_DEFAULT, StorageUnit.BYTES);
+      IOUtils.copyBytes(inputStream, outputStream, chunkSize);
+    }

Review Comment:
   If `KeyOutputStream` is not closed, the rewrite will not be completed:
   
   ```
   [ERROR] org.apache.hadoop.fs.ozone.TestOFS.testSetReplication  Time elapsed: 
0.592 s  <<< FAILURE!
   org.opentest4j.AssertionFailedError: expected: <THREE> but was: <ONE>
   ```
   
   @sadanand48 it may be useful to add a negative test like that.



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