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

Fixes a bug that caused concurrent field access of cloned, read-only structs with more than 5 members to be non-deterministic. #630

Merged
merged 2 commits into from
Nov 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/com/amazon/ion/impl/lite/IonStructLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ private void build_field_map()
_field_map.put(v._fieldName, ii); // this causes the map to have the largest index value stored
}
}

@Override
public void makeReadOnly() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to only do this for IonStructLite as opposed to IonContainerLite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do what, specifically? The field map is contained in IonStructLite.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if eager initialization is needed for IonContainerLite as well, but it seems the issue is isolated to IonStructLite

// Eagerly initialize the fields map to prevent potential data races https://github.com/amazon-ion/ion-java/issues/629
fieldMapIsActive(_child_count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name reads like a Boolean accessor.

super.makeReadOnly();
}

private void add_field(String fieldName, int newFieldIdx)
{
Integer idx = _field_map.get(fieldName);
Expand Down
29 changes: 29 additions & 0 deletions test/com/amazon/ion/CloneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import com.amazon.ion.system.IonSystemBuilder;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static com.amazon.ion.impl._Private_Utils.newSymbolToken;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -28,6 +32,7 @@
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CloneTest
Expand Down Expand Up @@ -261,6 +266,30 @@ public void modifyAfterCloneDoesNotChangeOriginal() {
assertNotEquals(original, clone);
}

@Test
public void readOnlyIonStructMultithreadedTest() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion—It would be nice to have a more descriptive name.

// See: https://github.com/amazon-ion/ion-java/issues/629
String ionStr = "{a:1,b:2,c:3,d:4,e:5,f:6}";

IonStruct ionValue = (IonStruct)system().singleValue(ionStr);
ionValue.makeReadOnly();

for (int i=0; i<100; i++) {
IonStruct clone = ionValue.clone();
clone.makeReadOnly();

List<CompletableFuture<Void>> waiting = new ArrayList<>();
for (int j = 0; j < 4; j++) {
waiting.add(CompletableFuture.runAsync(() -> {
for (int k = 0; k <= 100; k++) {
assertNotNull(clone.get("a"));
}
}));
}
waiting.forEach(CompletableFuture::join);
}
}

/**
* @return the singleton IonSystem
*/
Expand Down
Loading