nifi icon indicating copy to clipboard operation
nifi copied to clipboard

NIFI-10622 Fix Flaky Test

Open Kerr0220 opened this issue 2 years ago • 0 comments

Summary

NIFI-10622

Following the problem in the issue NIFI-10622, I looked through the test code and find that the checksum calculated by test peer is hard coded as "3057746557" in testTransferTwoFiles():

// Commit transaction        
final int flowFileSent = serverProtocol.commitTransferTransaction(peer, "3058746557"); 

Therefore, the problem must be caused by the other peer, where the checksum is calculated as another value.

Then, I dived into the codes where the checksum is calculated and found that this non-deterministic problem is caused by

// module: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-site-to-site
// file: remote/protocol/AbstractFlowFileServerProtocol.java
final DataPacket dataPacket = new StandardDataPacket(toSend.getAttributes(), in, toSend.getSize());

since the determination of getAttributed() is:

public Map<String, String> getAttributes() {
    return Collections.unmodifiableMap(this.attributes);
}

and the order of elements return by it is non-deterministic.

To minimize the influence, I sort the elements after retrieving them and store them in LinkedHashMap:

LinkedHashMap<String, String> attributes = new LinkedHashMap<>();
String[] keySet = toSend.getAttributes().keySet().toArray(new String[0]);
Arrays.sort(keySet);
for(String key: keySet){
    attributes.put(key, toSend.getAttributes().get(key));
}
final DataPacket dataPacket = new StandardDataPacket(attributes, in, toSend.getSize());

In this case, the checksum will always be calculated as 4171232842. So I changed the checksum in the test case to this value.

Finally, this test is not flaky and can pass NonDex's check.

Tracking

Please complete the following tracking steps prior to pull request creation.

Issue Tracking

Pull Request Tracking

  • [x] Pull Request title starts with Apache NiFi Jira issue number, such as NIFI-00000
  • [x] Pull Request commit message starts with Apache NiFi Jira issue number, as such NIFI-00000

Pull Request Formatting

  • [x] Pull Request based on current revision of the main branch
  • [x] Pull Request refers to a feature branch with one commit containing changes

Verification

Please indicate the verification steps performed prior to pull request creation.

Build

  • [ ] Build completed using mvn clean install -P contrib-check
    • [x] JDK 8
    • [ ] JDK 11
    • [ ] JDK 17

Licensing

  • [ ] New dependencies are compatible with the Apache License 2.0 according to the License Policy
  • [ ] New dependencies are documented in applicable LICENSE and NOTICE files

Documentation

  • [ ] Documentation formatting appears as expected in rendered files

Kerr0220 avatar Oct 11 '22 00:10 Kerr0220