Skip to content

Conversation

@will-sh
Copy link
Contributor

@will-sh will-sh commented Jan 20, 2024

What changes were proposed in this pull request?

HDDS-10151: Replace single-use Random objects with RandomUtils in test classes

Please describe your PR in detail:
Replace single-use Random objects by RandomUtils or similar. This is a code optimization task aimed at improving the efficiency of the codebase.

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-10151

How was this patch tested?

Each test class are manually tested using
mvn test -Dtest=ClassName
All the tests passed after the code change.
For the Abstract classes, tests passed for the sub-classes that inherit from the superclasses after code change, for example:
TestOzoneContractLegacy extends AbstractOzoneContractTest extends AbstractContractSeekTest
TestOFSWithOMRatis extends AbstractRootedOzoneFileSystemTest

Note

RandomUtils cannot directly set a seed.
In the below test classes (TestDeletedBlockLog.java, TestLeaderChoosePolicy.java, TestDeletedBlocksTxnShell.java)
We don't see much benefit to set a seed, so Random is replaced by RandomUtils

The test class TestMultipartObjectGet.java also doesn't really need to be secure so the SecureRandom() is replaced by RandomUtils

Copy link
Contributor

@adoroszlai adoroszlai left a comment

Choose a reason for hiding this comment

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

Thanks @will-sh for the patch. The change mostly looks good, see two items inline.

Comment on lines 62 to 65
final byte[] bytes = new byte[1 << 10];
for (int i = 0; i < 1000; i++) {
random.nextBytes(bytes);
checkBytes(bytes, random.nextInt(bytes.length));
RandomUtils.nextBytes(bytes.length);
checkBytes(bytes, RandomUtils.nextInt(0, bytes.length));
Copy link
Contributor

Choose a reason for hiding this comment

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

nextBytes returns the byte array, which is ignored here. So bytes is the same in each iteration instead of having random content.

Since this results in a new array in each iteration, we can remove the original bytes array.

      final int len = 1 << 10;
      for (int i = 0; i < 1000; i++) {
        checkBytes(RandomUtils.nextBytes(len), RandomUtils.nextInt(0, len));
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, it is corrected.

import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.RandomUtils;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: please try to keep alphabetical order, move to:

import com.google.common.collect.ImmutableSet;
import org.apache.hadoop.hdds.client.ECReplicationConfig;

(the same minor issue applies to some of the other files)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@adoroszlai adoroszlai changed the title HDDS-10151: Replace single-use Random objects with RandomUtils in test classes HDDS-10151. Replace single-use Random objects with RandomUtils in test classes Jan 21, 2024
@adoroszlai adoroszlai merged commit 7d2864c into apache:master Jan 21, 2024
@adoroszlai
Copy link
Contributor

Thanks @will-sh for updating the patch.

@will-sh will-sh deleted the HDDS-10151 branch January 21, 2024 16:18
@will-sh
Copy link
Contributor Author

will-sh commented Jan 21, 2024

Thanks for the review @adoroszlai

Tejaskriya pushed a commit to Tejaskriya/ozone that referenced this pull request Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants