Remove redundant type parameters and enforce some other style and inspection rules#5980
Remove redundant type parameters and enforce some other style and inspection rules#5980leventov merged 30 commits intoapache:masterfrom asdf2014:various_changes
Conversation
There was a problem hiding this comment.
nit: Please make this final.
There was a problem hiding this comment.
Can be Collections.singletonList.
There was a problem hiding this comment.
Can be Collections.emptyList().
There was a problem hiding this comment.
Please break the line per comma like below:
binder,
Key.get(MetadataStorageConnectorConfig.class),
new MetadataStorageConnectorConfig()
{
}
There was a problem hiding this comment.
There is a corresponding inspection in IntelliJ, could you assign it ERROR severity (you could do it via IntelliJ interface; the change should appear in https://github.com/apache/incubator-druid/blob/master/.idea/inspectionProfiles/Druid.xml) so such problems are caught by CI.
There was a problem hiding this comment.
Yes. Indeed, in this way, such a similar work will be more effective. By the way, can we learn from Alibaba's Java coding specification project P3C. Although the specifications in it are not completely correct... For example, i just submitted the issue about the pre-allocated size toArray method to P3C a few days ago. 😅
There was a problem hiding this comment.
Already add ToArrayCallWithZeroLengthArrayArgument specification into profile.
There was a problem hiding this comment.
Because, File.toURL() is deprecated.
There was a problem hiding this comment.
Please add this method to our forbidden-apis file: https://github.com/apache/incubator-druid/blob/master/codestyle/druid-forbidden-apis.txt. When adding, please check that it works by intentionally leaving one call to this method in the code, and see if mvn clean install -DskipTests fails locally. For syntax, see https://github.com/policeman-tools/forbidden-apis/wiki/SignaturesSyntax.
There was a problem hiding this comment.
Boolean.parseBoolean returns a primitive
There was a problem hiding this comment.
There is no need for the .booleanValue() method, cuz Java will automatically unbox Boolean primitive after JDK 5+.
There was a problem hiding this comment.
I mean there is no need to get a boxed Boolean either, Boolean.parseBoolean() is a direct equivalent which returns a primitive.
There was a problem hiding this comment.
Also worth catching this consistently, via IntelliJ inspection
There was a problem hiding this comment.
There was a problem hiding this comment.
PS: The origin intellij inspection about string equals is SimplifiableEqualsExpression
There was a problem hiding this comment.
I think you could catch those things merely using a regex, [a-z][a-zA-Z0-9_]*\.equals\((\"|[A-Z_]+\)) (intended to catch javaVar.equals("string") and javaVar.equals(STRING_CONSTANT)). You could add this regex to the checkstyle config: https://github.com/apache/incubator-druid/blob/master/codestyle/checkstyle.xml#L168
There was a problem hiding this comment.
If you wish you could also fix all occurrences of this, via IntelliJ inspection "Too few arguments of Arrays.asList()", and prohibit it on CI level. There are hundreds of such issues in the codebase, but since there is an automatic fix in IntelliJ, it could be even not very hard to do this.
There was a problem hiding this comment.
Okay, I will add this situation to the Intellij inspection configuration file. In this way, each newly added developer will notice this coding specification without having to read some sort of HOW TO CONTRIBUTE documents. 👍
There was a problem hiding this comment.
Already add ArraysAsListWithZeroOrOneArgument specification into profile.
…eArgument into inspection profile
|
job-402722329 and job-402722330 both failed... job-402722329: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project druid-processing: ExecutionException The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Command was /bin/sh -c cd /home/travis/build/apache/incubator-druid/processing && /usr/lib/jvm/java-8-oracle/jre/bin/java -Xmx768m -Duser.language=en -Duser.country=US -Dfile.encoding=UTF-8 -Duser.timezone=UTC -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager -Ddruid.indexing.doubleStorage=double -jar /home/travis/build/apache/incubator-druid/processing/target/surefire/surefirebooter678641966742689650.jar /home/travis/build/apache/incubator-druid/processing/target/surefire/surefire1266228946009469426tmp /home/travis/build/apache/incubator-druid/processing/target/surefire/surefire_14722988346361641729tmpIt seems that job-402722330: CuratorDruidCoordinatorTest.testMoveSegment:369 » test timed out after 60000 ...After PR#5978, sometimes the timeout value is still not enough. |
…ease the timeout value for testMoveSegment testcase
|
The last commit is not related to this PR. Please make it as another one. |
|
Hi, @jihoonson . Already roll back the latest commit. I will create a independent PR for it. But, if the travis happens |
|
@asdf2014 sure, I'll restart failed jobs if they're transient. BTW, the size of this PR looks suddenly increased. What commit has increased the size? |
|
Under the advice of @leventov . After adding |
|
@asdf2014 thanks. I'll take another look. |
|
@jihoonson You are welcome. The amount of code is large and very troublesome. May have to trouble you. 👍 |
|
Hi, @jihoonson . jobs-403482349 failed again.. Could you please rebuild it? |
|
@asdf2014 restarted the test. Will take a look again. |
|
@jihoonson Thanks a lot :D |
|
@jihoonson It works. Thx again. 👍 |
|
@leventov PTAL.
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
public class ToArrayBenchmark {
@Param({"1", "100", "1000", "5000", "10000", "100000"})
private int n;
private final List<Object> list = new ArrayList<>();
@Setup
public void populateList() {
for (int i = 0; i < n; i++) {
list.add(0);
}
}
@Benchmark
public Object[] preSize() {
return list.toArray(new Object[n]);
}
@Benchmark
public Object[] resize() {
return list.toArray(new Object[0]);
}
/*
Integer List:
Benchmark (n) Mode Cnt Score Error Units
ToArrayBenchmark.preSize 1 avgt 3 41.552 ± 108.030 ns/op
ToArrayBenchmark.preSize 100 avgt 3 216.449 ± 799.501 ns/op
ToArrayBenchmark.preSize 1000 avgt 3 2087.965 ± 6027.778 ns/op
ToArrayBenchmark.preSize 5000 avgt 3 9098.358 ± 14603.493 ns/op
ToArrayBenchmark.preSize 10000 avgt 3 24204.199 ± 121468.232 ns/op
ToArrayBenchmark.preSize 100000 avgt 3 188183.618 ± 369455.090 ns/op
ToArrayBenchmark.resize 1 avgt 3 18.987 ± 36.449 ns/op
ToArrayBenchmark.resize 100 avgt 3 265.549 ± 1125.008 ns/op
ToArrayBenchmark.resize 1000 avgt 3 1560.713 ± 2922.186 ns/op
ToArrayBenchmark.resize 5000 avgt 3 7804.810 ± 8333.390 ns/op
ToArrayBenchmark.resize 10000 avgt 3 24791.026 ± 78459.936 ns/op
ToArrayBenchmark.resize 100000 avgt 3 158891.642 ± 56055.895 ns/op
Object List:
Benchmark (n) Mode Cnt Score Error Units
ToArrayBenchmark.preSize 1 avgt 3 36.306 ± 96.612 ns/op
ToArrayBenchmark.preSize 100 avgt 3 52.372 ± 84.159 ns/op
ToArrayBenchmark.preSize 1000 avgt 3 449.807 ± 215.692 ns/op
ToArrayBenchmark.preSize 5000 avgt 3 2080.172 ± 2003.726 ns/op
ToArrayBenchmark.preSize 10000 avgt 3 4657.937 ± 8432.624 ns/op
ToArrayBenchmark.preSize 100000 avgt 3 51980.829 ± 46920.314 ns/op
ToArrayBenchmark.resize 1 avgt 3 16.747 ± 85.131 ns/op
ToArrayBenchmark.resize 100 avgt 3 43.803 ± 28.704 ns/op
ToArrayBenchmark.resize 1000 avgt 3 404.681 ± 132.986 ns/op
ToArrayBenchmark.resize 5000 avgt 3 1972.649 ± 174.691 ns/op
ToArrayBenchmark.resize 10000 avgt 3 4021.440 ± 1114.212 ns/op
ToArrayBenchmark.resize 100000 avgt 3 44204.167 ± 76714.850 ns/op
*/
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.include(ToArrayBenchmark.class.getSimpleName())
.forks(1)
.warmupIterations(1)
.measurementIterations(3)
.threads(1)
.build();
new Runner(opt).run();
}
}Tips: Full code is here.
|
|
|
Hi, @leventov . It has been fixed. BTW, jobs-405249198 failed because vm crashed. Would you please help me rebuild it once? |
…ck the level of ToArrayCallWithZeroLengthArrayArgument as WARNING until Youtrack fix it
|
@leventov It seems that both travis and teamcity have succeeded. Any other good suggestions? |
| <property name="message" value="Use java.lang.Primitive.BYTES instead."/> | ||
| </module> | ||
|
|
||
| <module name="Regexp"> |
There was a problem hiding this comment.
Please add a comment that this regex should be replaced with an IntelliJ inspection when teamcity.jetbrains.com updates to at least IntelliJ 2018.1 (currently it uses 2017.2)
| Sequence<Result<TopNResultValue>> queryResult = theRunner.run( | ||
| QueryPlus.wrap(query), | ||
| Maps.<String, Object>newHashMap() | ||
| Maps.newHashMap() |
There was a problem hiding this comment.
Did you remove redundant type arguments in the whole codebase? If so, you could try to change the level of the corresponding IntelliJ inspection ("Redundant type arguments") to ERROR.
| { | ||
| return Arrays.<AggregatorFactory>asList(new DistinctCountAggregatorFactory(fieldName, fieldName, bitMapFactory)); | ||
| return Collections.singletonList( | ||
| new DistinctCountAggregatorFactory(fieldName, fieldName, bitMapFactory)); |
There was a problem hiding this comment.
It's not formatted properly, the closing ); should be on the next line (or the whole expression on a single line).
| public List<AggregatorFactory> getRequiredColumns() | ||
| { | ||
| return Arrays.<AggregatorFactory>asList(new TimestampAggregatorFactory(fieldName, fieldName, timeFormat, comparator, initValue)); | ||
| return Collections.singletonList( |
|
Hi, @leventov . Thanks for your review. The |
leventov
left a comment
There was a problem hiding this comment.
Thanks a lot for this contribution, cleaning up the source is important but nobody usually does this.
|
You are welcome. In the process, I also learned a lot. 👍 |
|
@asdf2014 would you update the PR description to include additional changes? E.g., Added new inspection/checkstyle rules. |
|
Hi, @jihoonson . Thanks for you comments. Added. |
|
Thanks @asdf2014! The latest change looks good to me. One side comment: usually, you can expect your PR can be reviewed faster as your PR contains less changes because it's much easier to review. It would be great if you can split your PRs if possible in the future. |
|
@jihoonson Thanks for your suggestion about the |



Various changes:
toArray()with pre-sized array argument for better performance;Module/Runnable/Accumulator;Set#addAllinstead offorloop;Collections.<...>singletonListinstead ofArrays.<...>asListfor single element situation;file.toURI().toURL()instead offile.toURL();Collections.<StorageLocationConfig>emptyList()instead ofArrays.<StorageLocationConfig>asList();ArraysAsListWithZeroOrOneArgument,RedundantTypeArgumentsandToArrayCallWithZeroLengthArrayArgument;String#equalsandtoArray(new Object[0])into checkstyle config file.