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

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b7604a7c2d HDDS-17290. Ensure key is not committed if thread 
interrupted (#12142)
1b7604a7c2d is described below

commit 1b7604a7c2d0254dc4a2e381227557521462fd26
Author: fmorg-git <[email protected]>
AuthorDate: Tue Nov 24 06:52:15 2026 -0800

    HDDS-17290. Ensure key is not committed if thread interrupted (#12142)
---
 .../hadoop/ozone/client/io/KeyOutputStream.java    |  5 ++
 .../hadoop/ozone/client/TestOzoneClient.java       | 69 ++++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/io/KeyOutputStream.java
 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/io/KeyOutputStream.java
index 8ad70ff0308..702da19aa0f 100644
--- 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/io/KeyOutputStream.java
+++ 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/io/KeyOutputStream.java
@@ -651,6 +651,11 @@ private void closeInternal() throws IOException {
       for (CheckedRunnable<IOException> preCommit : preCommits) {
         preCommit.run();
       }
+      // Do not commit when the thread is interrupted.
+      if (Thread.currentThread().isInterrupted()) {
+        throw new InterruptedIOException(
+            "Commit of key " + blockOutputStreamEntryPool.getKeyName() + " 
aborted: thread interrupted");
+      }
       blockOutputStreamEntryPool.commitKey(offset);
     } finally {
       blockOutputStreamEntryPool.cleanup();
diff --git 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/TestOzoneClient.java
 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/TestOzoneClient.java
index c96fe8bfc5b..4da0abd514e 100644
--- 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/TestOzoneClient.java
+++ 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/TestOzoneClient.java
@@ -20,6 +20,8 @@
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.hadoop.hdds.client.ReplicationFactor.ONE;
 import static org.apache.ozone.test.GenericTestUtils.getTestStartTime;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -27,10 +29,14 @@
 
 import jakarta.annotation.Nonnull;
 import java.io.IOException;
+import java.io.InterruptedIOException;
 import java.time.Instant;
 import java.util.HashMap;
 import java.util.UUID;
+import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.conf.StorageUnit;
+import org.apache.hadoop.crypto.CryptoCodec;
+import org.apache.hadoop.crypto.CryptoOutputStream;
 import org.apache.hadoop.hdds.client.ECReplicationConfig;
 import org.apache.hadoop.hdds.client.ReplicationConfigValidator;
 import org.apache.hadoop.hdds.client.ReplicationType;
@@ -220,6 +226,69 @@ public void testPutKeyWithECReplicationConfig() throws 
IOException {
     }
   }
 
+  @Test
+  public void testInterruptedCloseOfEncryptedKeyDoesNotCommit() throws 
IOException {
+    final OzoneBucket bucket = getOzoneBucket();
+    final String keyName = UUID.randomUUID().toString();
+    final byte[] previous = "previous value".getBytes(UTF_8);
+    final byte[] value = "sample value".getBytes(UTF_8);
+    writeKey(bucket, keyName, previous);
+
+    try (OzoneOutputStream out = bucket.createKey(keyName, value.length);
+         // RpcClient wraps the key stream this way for a key in an encrypted 
bucket
+         CryptoOutputStream cryptoOut = new CryptoOutputStream(
+             out.getKeyOutputStream(), CryptoCodec.getInstance(new 
OzoneConfiguration()), new byte[16], new byte[16])) {
+      // The value stays in the crypto buffer until close.
+      cryptoOut.write(value);
+      Thread.currentThread().interrupt();
+      try {
+        
assertThatThrownBy(cryptoOut::close).isInstanceOf(InterruptedIOException.class);
+      } finally {
+        // Also clears the flag so it cannot affect subsequent tests.
+        assertThat(Thread.interrupted()).isTrue();
+      }
+    }
+
+    assertKeyContent(bucket, keyName, previous);
+  }
+
+  @Test
+  public void testCloseAfterInterruptedWriteDoesNotCommit() throws IOException 
{
+    final OzoneBucket bucket = getOzoneBucket();
+    final String keyName = UUID.randomUUID().toString();
+    final byte[] previous = "previous value".getBytes(UTF_8);
+    final byte[] value = "sample value".getBytes(UTF_8);
+    writeKey(bucket, keyName, previous);
+
+    try (OzoneOutputStream out = bucket.createKey(keyName, value.length)) {
+      Thread.currentThread().interrupt();
+      try {
+        assertThatThrownBy(() -> 
out.write(value)).isInstanceOf(InterruptedIOException.class);
+        
assertThatThrownBy(out::close).isInstanceOf(InterruptedIOException.class);
+      } finally {
+        assertThat(Thread.interrupted()).isTrue();
+      }
+    }
+
+    assertKeyContent(bucket, keyName, previous);
+    // Once the interrupt is cleared, the key can be written again.
+    writeKey(bucket, keyName, value);
+    assertKeyContent(bucket, keyName, value);
+  }
+
+  private static void writeKey(OzoneBucket bucket, String keyName, byte[] 
content) throws IOException {
+    try (OzoneOutputStream out = bucket.createKey(keyName, content.length)) {
+      out.write(content);
+    }
+  }
+
+  private static void assertKeyContent(OzoneBucket bucket, String keyName, 
byte[] content) throws IOException {
+    assertEquals(content.length, bucket.getKey(keyName).getDataSize(), "data 
size is unexpected");
+    try (OzoneInputStream in = bucket.readKey(keyName)) {
+      assertThat(IOUtils.toByteArray(in)).isEqualTo(content);
+    }
+  }
+
   private OzoneBucket getOzoneBucket() throws IOException {
     String volumeName = UUID.randomUUID().toString();
     String bucketName = UUID.randomUUID().toString();


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

Reply via email to