Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HADOOP-13500. Synchronizing iteration of Configuration properties object #3775

Merged
merged 2 commits into from Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2978,11 +2978,13 @@ public Iterator<Map.Entry<String, String>> iterator() {
// methods that allow non-strings to be put into configurations are removed,
// we could replace properties with a Map<String,String> and get rid of this
// code.
Map<String,String> result = new HashMap<String,String>();
for(Map.Entry<Object,Object> item: getProps().entrySet()) {
if (item.getKey() instanceof String &&
item.getValue() instanceof String) {
Properties props = getProps();
Map<String, String> result = new HashMap<>();
synchronized (props) {
for (Map.Entry<Object, Object> item : props.entrySet()) {
if (item.getKey() instanceof String && item.getValue() instanceof String) {
result.put((String) item.getKey(), (String) item.getValue());
}
}
}
return result.entrySet().iterator();
Expand Down
Expand Up @@ -38,13 +38,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import static java.util.concurrent.TimeUnit.*;

Expand Down Expand Up @@ -2687,4 +2689,31 @@ private static Configuration checkCDATA(byte[] bytes) {
assertEquals(" prefix >cdata\nsuffix ", conf.get("cdata-whitespace"));
return conf;
}

@Test
public void testConcurrentModificationDuringIteration() throws InterruptedException {
Configuration configuration = new Configuration();
new Thread(() -> {
while (true) {
configuration.set(String.valueOf(Math.random()), String.valueOf(Math.random()));
}
}).start();

AtomicBoolean exceptionOccurred = new AtomicBoolean(false);

new Thread(() -> {
while (true) {
try {
configuration.iterator();
} catch (final ConcurrentModificationException e) {
exceptionOccurred.set(true);
break;
}
}
}).start();

Thread.sleep(1000); //give enough time for threads to run

assertFalse("ConcurrentModificationException occurred", exceptionOccurred.get());
}
}