Skip to content

Commit 2930ecc

Browse files
authored
HDDS-10791. Duplicated instanceof checks in OzoneOutputStream (#9370)
1 parent fe33021 commit 2930ecc

2 files changed

Lines changed: 169 additions & 25 deletions

File tree

hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/io/OzoneOutputStream.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,36 +139,28 @@ public OmMultipartCommitUploadPartInfo getCommitUploadPartInfo() {
139139
public OutputStream getOutputStream() {
140140
return outputStream;
141141
}
142-
142+
143143
public KeyOutputStream getKeyOutputStream() {
144-
if (outputStream instanceof KeyOutputStream) {
145-
return ((KeyOutputStream) outputStream);
146-
} else if (outputStream instanceof CryptoOutputStream) {
147-
OutputStream wrappedStream =
148-
((CryptoOutputStream) outputStream).getWrappedStream();
149-
if (wrappedStream instanceof KeyOutputStream) {
150-
return ((KeyOutputStream) wrappedStream);
151-
}
152-
} else if (outputStream instanceof CipherOutputStreamOzone) {
153-
OutputStream wrappedStream =
154-
((CipherOutputStreamOzone) outputStream).getWrappedStream();
155-
if (wrappedStream instanceof KeyOutputStream) {
156-
return ((KeyOutputStream)wrappedStream);
157-
}
158-
}
159-
// Otherwise return null.
160-
return null;
144+
OutputStream base = unwrap(outputStream);
145+
return base instanceof KeyOutputStream ? (KeyOutputStream) base : null;
161146
}
162147

163148
@Override
164149
public Map<String, String> getMetadata() {
165-
if (outputStream instanceof CryptoOutputStream) {
166-
return ((KeyMetadataAware)((CryptoOutputStream) outputStream)
167-
.getWrappedStream()).getMetadata();
168-
} else if (outputStream instanceof CipherOutputStreamOzone) {
169-
return ((KeyMetadataAware)((CipherOutputStreamOzone) outputStream)
170-
.getWrappedStream()).getMetadata();
150+
OutputStream base = unwrap(outputStream);
151+
if (base instanceof KeyMetadataAware) {
152+
return ((KeyMetadataAware) base).getMetadata();
153+
}
154+
throw new IllegalStateException(
155+
"OutputStream is not KeyMetadataAware: " + base.getClass());
156+
}
157+
158+
private static OutputStream unwrap(OutputStream out) {
159+
if (out instanceof CryptoOutputStream) {
160+
return ((CryptoOutputStream) out).getWrappedStream();
161+
} else if (out instanceof CipherOutputStreamOzone) {
162+
return ((CipherOutputStreamOzone) out).getWrappedStream();
171163
}
172-
return ((KeyMetadataAware) outputStream).getMetadata();
164+
return out;
173165
}
174166
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hadoop.ozone.client.io;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
import java.io.IOException;
27+
import java.io.OutputStream;
28+
import java.util.Collections;
29+
import java.util.Map;
30+
import org.apache.hadoop.crypto.CryptoOutputStream;
31+
import org.junit.jupiter.api.Test;
32+
33+
/**
34+
* Unit tests for OzoneOutputStream covering metadata and unwrap behavior.
35+
*/
36+
public class TestOzoneOutputStream {
37+
38+
/**
39+
* Fake KeyOutputStream implementation for testing.
40+
* Uses the package-private KeyOutputStream() constructor.
41+
*/
42+
private static class FakeKeyOutputStream extends KeyOutputStream
43+
implements KeyMetadataAware {
44+
45+
private final Map<String, String> metadata;
46+
47+
FakeKeyOutputStream(Map<String, String> metadata) {
48+
super(); // VisibleForTesting constructor
49+
this.metadata = metadata;
50+
}
51+
52+
@Override
53+
public Map<String, String> getMetadata() {
54+
return metadata;
55+
}
56+
57+
@Override
58+
public void flush() {
59+
// avoid KeyOutputStream.flush() using null semaphore
60+
}
61+
62+
@Override
63+
public void close() {
64+
// avoid real close logic touching internal state
65+
}
66+
}
67+
68+
/**
69+
* Minimal fake CipherOutputStreamOzone wrapper for testing unwrap().
70+
*/
71+
private static class FakeCipherOutputStreamOzone
72+
extends CipherOutputStreamOzone {
73+
74+
FakeCipherOutputStreamOzone(OutputStream out) {
75+
super(out);
76+
}
77+
78+
@Override
79+
public OutputStream getWrappedStream() {
80+
return super.getWrappedStream();
81+
}
82+
}
83+
84+
/**
85+
* test Plain KeyOutputStream and getMetadata() (no encryption).
86+
*/
87+
@Test
88+
public void testPlainKeyOutputStream() throws IOException {
89+
Map<String, String> metadata = Collections.singletonMap("k1", "v1");
90+
91+
FakeKeyOutputStream key = new FakeKeyOutputStream(metadata);
92+
try (OzoneOutputStream ozone = new OzoneOutputStream(key, null)) {
93+
assertNotNull(ozone.getKeyOutputStream());
94+
assertEquals("v1", ozone.getMetadata().get("k1"));
95+
}
96+
}
97+
98+
/**
99+
* test getKeyOutputStream and getMetadata() wrapped inside CryptoOutputStream.
100+
*/
101+
@Test
102+
public void testCryptoWrapped() throws IOException {
103+
Map<String, String> metadata = Collections.singletonMap("k1", "v1");
104+
105+
FakeKeyOutputStream key = new FakeKeyOutputStream(metadata);
106+
107+
// Mockito mock for CryptoOutputStream: we only care that
108+
// 1) it is an instance of CryptoOutputStream
109+
// 2) getWrappedStream() returns the underlying KeyOutputStream
110+
CryptoOutputStream crypto = mock(CryptoOutputStream.class);
111+
when(crypto.getWrappedStream()).thenReturn(key);
112+
113+
try (OzoneOutputStream ozone = new OzoneOutputStream(crypto, null)) {
114+
assertNotNull(ozone.getKeyOutputStream());
115+
assertEquals("v1", ozone.getMetadata().get("k1"));
116+
}
117+
}
118+
119+
/**
120+
* test getKeyOutputStream and getMetadata() wrapped inside CipherOutputStreamOzone.
121+
*/
122+
@Test
123+
public void testCipherWrapped() throws IOException {
124+
Map<String, String> metadata = Collections.singletonMap("k1", "v1");
125+
126+
FakeKeyOutputStream key = new FakeKeyOutputStream(metadata);
127+
FakeCipherOutputStreamOzone cipher =
128+
new FakeCipherOutputStreamOzone(key);
129+
130+
try (OzoneOutputStream ozone = new OzoneOutputStream(cipher, null)) {
131+
assertNotNull(ozone.getKeyOutputStream());
132+
assertEquals("v1", ozone.getMetadata().get("k1"));
133+
}
134+
}
135+
136+
/**
137+
* test for Non-KeyMetadataAware stream verify that exception is thrown here.
138+
*/
139+
@Test
140+
public void testNonKeyMetadataAwareThrows() throws IOException {
141+
OutputStream nonMeta = new OutputStream() {
142+
@Override
143+
public void write(int b) {
144+
145+
}
146+
};
147+
148+
try (OzoneOutputStream ozone = new OzoneOutputStream(nonMeta, null)) {
149+
assertThrows(IllegalStateException.class, ozone::getMetadata);
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)