Skip to content

Commit 6f1ab95

Browse files
YARN-9128. Use SerializationUtils from apache commons to serialize / deserialize ResourceMappings. Contributed by Zoltan Siegl
1 parent 35f093f commit 6f1ab95

File tree

2 files changed

+128
-22
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src

2 files changed

+128
-22
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/container/ResourceMappings.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@
1818

1919
package org.apache.hadoop.yarn.server.nodemanager.containermanager.container;
2020

21-
import java.io.ByteArrayInputStream;
22-
import java.io.ByteArrayOutputStream;
21+
import org.apache.commons.lang3.SerializationException;
22+
import org.apache.commons.lang3.SerializationUtils;
23+
2324
import java.io.IOException;
24-
import java.io.ObjectInputStream;
25-
import java.io.ObjectOutputStream;
2625
import java.io.Serializable;
2726
import java.util.ArrayList;
2827
import java.util.Collections;
2928
import java.util.HashMap;
3029
import java.util.List;
3130
import java.util.Map;
3231

33-
import org.apache.commons.io.IOUtils;
34-
3532
/**
3633
* This class is used to store assigned resource to a single container by
3734
* resource types.
@@ -91,32 +88,23 @@ public void updateAssignedResources(List<Serializable> list) {
9188
@SuppressWarnings("unchecked")
9289
public static AssignedResources fromBytes(byte[] bytes)
9390
throws IOException {
94-
ObjectInputStream ois = null;
95-
List<Serializable> resources;
91+
final List<Serializable> resources;
9692
try {
97-
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
98-
ois = new ObjectInputStream(bis);
99-
resources = (List<Serializable>) ois.readObject();
100-
} catch (ClassNotFoundException e) {
93+
resources = SerializationUtils.deserialize(bytes);
94+
} catch (SerializationException e) {
10195
throw new IOException(e);
102-
} finally {
103-
IOUtils.closeQuietly(ois);
10496
}
10597
AssignedResources ar = new AssignedResources();
10698
ar.updateAssignedResources(resources);
10799
return ar;
108100
}
109101

110102
public byte[] toBytes() throws IOException {
111-
ObjectOutputStream oos = null;
112-
byte[] bytes;
103+
final byte[] bytes;
113104
try {
114-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
115-
oos = new ObjectOutputStream(bos);
116-
oos.writeObject(resources);
117-
bytes = bos.toByteArray();
118-
} finally {
119-
IOUtils.closeQuietly(oos);
105+
bytes = SerializationUtils.serialize((Serializable) resources);
106+
} catch (SerializationException e) {
107+
throw new IOException(e);
120108
}
121109
return bytes;
122110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.yarn.server.nodemanager.containermanager.container;
19+
20+
import com.google.common.collect.ImmutableList;
21+
import org.apache.commons.io.IOUtils;
22+
import org.apache.hadoop.yarn.server.nodemanager.api.deviceplugin.Device;
23+
import org.junit.Assert;
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.ObjectOutputStream;
30+
import java.io.Serializable;
31+
import java.util.List;
32+
33+
public class TestResourceMappings {
34+
35+
private static final ResourceMappings.AssignedResources testResources =
36+
new ResourceMappings.AssignedResources();
37+
38+
@BeforeClass
39+
public static void setup() {
40+
testResources.updateAssignedResources(ImmutableList.of(
41+
Device.Builder.newInstance()
42+
.setId(0)
43+
.setDevPath("/dev/hdwA0")
44+
.setMajorNumber(256)
45+
.setMinorNumber(0)
46+
.setBusID("0000:80:00.0")
47+
.setHealthy(true)
48+
.build(),
49+
Device.Builder.newInstance()
50+
.setId(1)
51+
.setDevPath("/dev/hdwA1")
52+
.setMajorNumber(256)
53+
.setMinorNumber(0)
54+
.setBusID("0000:80:00.1")
55+
.setHealthy(true)
56+
.build()
57+
));
58+
}
59+
60+
@Test
61+
public void testSerializeAssignedResourcesWithSerializationUtils() {
62+
try {
63+
byte[] serializedString = testResources.toBytes();
64+
65+
ResourceMappings.AssignedResources deserialized =
66+
ResourceMappings.AssignedResources.fromBytes(serializedString);
67+
68+
Assert.assertEquals(testResources.getAssignedResources(),
69+
deserialized.getAssignedResources());
70+
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
Assert.fail(String.format("Serialization of test AssignedResources " +
74+
"failed with %s", e.getMessage()));
75+
}
76+
}
77+
78+
@Test
79+
public void testAssignedResourcesCanDeserializePreviouslySerializedValues() {
80+
try {
81+
byte[] serializedString = toBytes(testResources.getAssignedResources());
82+
83+
ResourceMappings.AssignedResources deserialized =
84+
ResourceMappings.AssignedResources.fromBytes(serializedString);
85+
86+
Assert.assertEquals(testResources.getAssignedResources(),
87+
deserialized.getAssignedResources());
88+
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
Assert.fail(String.format("Deserialization of test AssignedResources " +
92+
"failed with %s", e.getMessage()));
93+
}
94+
}
95+
96+
/**
97+
* This was the legacy way to serialize resources. This is here for
98+
* backward compatibility to ensure that after YARN-9128 we can still
99+
* deserialize previously serialized resources.
100+
*
101+
* @param resources the list of resources
102+
* @return byte array representation of the resource
103+
* @throws IOException
104+
*/
105+
private byte[] toBytes(List<Serializable> resources) throws IOException {
106+
ObjectOutputStream oos = null;
107+
byte[] bytes;
108+
try {
109+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
110+
oos = new ObjectOutputStream(bos);
111+
oos.writeObject(resources);
112+
bytes = bos.toByteArray();
113+
} finally {
114+
IOUtils.closeQuietly(oos);
115+
}
116+
return bytes;
117+
}
118+
}

0 commit comments

Comments
 (0)