Skip to content

Commit 56e3191

Browse files
authored
Enforce and apply Google Java Style formatting (#416)
Uses [fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin) to automatically (locally) apply [Google Java Style](https://google.github.io/styleguide/javaguide.html) formatting via the [google-java-format](https://github.com/google/google-java-format) plugin during `mvn compile` time. During CI time, it checks for incorrect formatting on files *prior* to compile to automatically reject failing commits so that reviewers have fewer things to nit over and code style is more standardized. - [x] Those file changes are initially omitted in order to validate this part and make the PR easier to review. Will commit those once initial review is done. This is not *yet* enforced by a git pre-commit however, so developers will have to add the changes that the `compile` stage applied after the fact. Additionally, it adds settings for [VSCode](https://code.visualstudio.com/docs/java/java-linting#_applying-formatter-settings) to apply these formattings during editing. - [ ] Similar settings for Eclipse are currently missing. Followup PR can also exclude this squashed PR from the `git blame` analysis using a [`.git-blame-ignore-revs` file](https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view) Closes #406
1 parent f1194b0 commit 56e3191

File tree

456 files changed

+41964
-41021
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

456 files changed

+41964
-41021
lines changed

.github/workflows/maven.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ jobs:
5050
cache: 'maven'
5151
distribution: 'temurin'
5252

53+
- name: Check formatting
54+
run: mvn -B fmt:check --file pom.xml
55+
56+
- name: Compile with Maven
57+
run: mvn -B compile test-compile --file pom.xml
58+
5359
- name: Test with Maven
5460
run: mvn -B test --file pom.xml
5561

@@ -605,6 +611,7 @@ jobs:
605611
- name: Build the benchbase docker image with all profiles using the dev image
606612
env:
607613
SKIP_TESTS: 'false'
614+
DO_FORMAT_CHECKS: 'true'
608615
run: |
609616
./docker/benchbase/build-full-image.sh
610617
- name: Run a basic test from the docker image against postgres test DB

.vscode/extensions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
"huntertran.auto-markdown-toc",
66
"vscjava.vscode-java-pack"
77
]
8-
}
8+
}

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.watcherExclude": {
3+
"**/target": true
4+
},
5+
"java.format.settings.profile": "GoogleStyle",
6+
"java.format.enabled": true
7+
}

CONTRIBUTING.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ We welcome all contributions! Please open a [pull request](https://github.com/cm
1515
- [IDE](#ide)
1616
- [Adding a new DBMS](#adding-a-new-dbms)
1717
- [Java Development Notes](#java-development-notes)
18-
- [Avoid var keyword](#avoid-var-keyword)
18+
- [Code Style](#code-style)
1919
- [Compiler Warnings](#compiler-warnings)
20+
- [Avoid var keyword](#avoid-var-keyword)
2021
- [Alternatives to arrays of generics](#alternatives-to-arrays-of-generics)
22+
- [this-escape warnings](#this-escape-warnings)
2123

2224
<!-- /TOC -->
2325

@@ -31,6 +33,14 @@ Please see the existing MySQL and PostgreSQL code for an example.
3133

3234
## Java Development Notes
3335

36+
### Code Style
37+
38+
To allow reviewers to focus more on code content and not style nits, [PR #416](https://github.com/cmu-db/benchbase/pulls/416) added support for auto formatting code at compile time according to [Google Java Style](https://google.github.io/styleguide/javaguide.html) using [google-java-format](https://github.com/google/google-java-format) and [fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin) Maven plugins.
39+
40+
Be sure to commit and include these changes in your PRs when submitting them so that the CI pipeline passes.
41+
42+
Additionally, this formatting style is included in the VSCode settings files for this repo.
43+
3444
### Compiler Warnings
3545

3646
In an effort to enforce clean, safe, maintainable code, [PR #413](https://github.com/cmu-db/benchbase/pull/413) enabled the `-Werror` and `-Xlint:all` options for the `javac` compiler.

docker/benchbase/common-env.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export BENCHBASE_PROFILE="${BENCHBASE_PROFILE:-postgres}"
77
export CLEAN_BUILD="${CLEAN_BUILD:-true}" # true, pre, post, false
88
# Whether to run the test target during build.
99
export SKIP_TESTS="${SKIP_TESTS:-false}"
10+
# Whether to do format checks during build (mostly for CI).
11+
export DO_FORMAT_CHECKS="${DO_FORMAT_CHECKS:-false}"
1012

1113
# Setting this allows us to easily tag and publish the image name in our CI pipelines or locally.
1214
CONTAINER_REGISTRY_NAME="${CONTAINER_REGISTRY_NAME:-}"

docker/benchbase/devcontainer/build-in-container.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set -eu -o pipefail
1010
BENCHBASE_PROFILES="${BENCHBASE_PROFILES:-cockroachdb mariadb mysql oracle phoenix postgres spanner sqlite sqlserver}"
1111
CLEAN_BUILD="${CLEAN_BUILD:-true}" # true, false, pre, post
1212
SKIP_TESTS="${SKIP_TESTS:-false}"
13+
DO_FORMAT_CHECKS="${DO_FORMAT_CHECKS:-false}"
1314

1415
cd /benchbase
1516
mkdir -p results
@@ -95,6 +96,12 @@ else
9596
echo "WARNING: Unhandled SKIP_TESTS mode: '$SKIP_TESTS'" >&2
9697
fi
9798

99+
# Pre format checks are mostly for CI.
100+
# Else, things are reformatted during compile.
101+
if [ "${DO_FORMAT_CHECKS:-}" == 'true' ]; then
102+
mvn -B --file pom.xml fmt:check
103+
fi
104+
98105
# Fetch resources serially to work around mvn races with downloading the same
99106
# file in multiple processes (mvn uses *.part instead of use tmpfile naming).
100107
for profile in ${BENCHBASE_PROFILES}; do

docker/benchbase/run-dev-image.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ docker run ${INTERACTIVE_ARGS:-} --rm \
3636
--env BENCHBASE_PROFILES="$BENCHBASE_PROFILES" \
3737
--env CLEAN_BUILD="$CLEAN_BUILD" \
3838
--env SKIP_TESTS="$SKIP_TESTS" \
39+
--env DO_FORMAT_CHECKS="$DO_FORMAT_CHECKS" \
3940
--env EXTRA_MAVEN_ARGS="${EXTRA_MAVEN_ARGS:-}" \
4041
--user "$CONTAINERUSER_UID:$CONTAINERUSER_GID" \
4142
-v "$MAVEN_CONFIG:/home/containeruser/.m2" \

pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,25 @@
323323
<artifactId>janino</artifactId>
324324
<version>3.1.11</version>
325325
</dependency>
326-
327326
</dependencies>
328327

329328
<build>
330329
<directory>${buildDirectory}</directory>
331330
<plugins>
331+
<plugin>
332+
<!-- format/check code via google style format -->
333+
<groupId>com.spotify.fmt</groupId>
334+
<artifactId>fmt-maven-plugin</artifactId>
335+
<version>2.21.1</version>
336+
<executions>
337+
<execution>
338+
<goals>
339+
<goal>check</goal>
340+
<goal>format</goal>
341+
</goals>
342+
</execution>
343+
</executions>
344+
</plugin>
332345
<plugin>
333346
<groupId>org.apache.maven.plugins</groupId>
334347
<artifactId>maven-compiler-plugin</artifactId>

src/main/java/com/oltpbenchmark/BenchmarkState.java

Lines changed: 69 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,109 +18,99 @@
1818
package com.oltpbenchmark;
1919

2020
import com.oltpbenchmark.types.State;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
23-
2421
import java.util.concurrent.CountDownLatch;
2522
import java.util.concurrent.atomic.AtomicInteger;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2625

2726
public final class BenchmarkState {
2827

29-
private static final Logger LOG = LoggerFactory.getLogger(BenchmarkState.class);
28+
private static final Logger LOG = LoggerFactory.getLogger(BenchmarkState.class);
3029

31-
private final long testStartNs;
32-
private final CountDownLatch startBarrier;
33-
private final AtomicInteger notDoneCount;
34-
private volatile State state = State.WARMUP;
30+
private final long testStartNs;
31+
private final CountDownLatch startBarrier;
32+
private final AtomicInteger notDoneCount;
33+
private volatile State state = State.WARMUP;
3534

36-
/**
37-
* @param numThreads number of threads involved in the test: including the
38-
* master thread.
39-
*/
40-
public BenchmarkState(int numThreads) {
41-
startBarrier = new CountDownLatch(numThreads);
42-
notDoneCount = new AtomicInteger(numThreads);
35+
/**
36+
* @param numThreads number of threads involved in the test: including the master thread.
37+
*/
38+
public BenchmarkState(int numThreads) {
39+
startBarrier = new CountDownLatch(numThreads);
40+
notDoneCount = new AtomicInteger(numThreads);
4341

42+
testStartNs = System.nanoTime();
43+
}
4444

45-
testStartNs = System.nanoTime();
46-
}
45+
// Protected by this
4746

48-
// Protected by this
47+
public long getTestStartNs() {
48+
return testStartNs;
49+
}
4950

50-
public long getTestStartNs() {
51-
return testStartNs;
51+
public State getState() {
52+
synchronized (this) {
53+
return state;
5254
}
55+
}
5356

54-
public State getState() {
55-
synchronized (this) {
56-
return state;
57-
}
57+
/** Wait for all threads to call this. Returns once all the threads have entered. */
58+
public void blockForStart() {
59+
60+
startBarrier.countDown();
61+
try {
62+
startBarrier.await();
63+
} catch (InterruptedException e) {
64+
throw new RuntimeException(e);
5865
}
66+
}
5967

60-
/**
61-
* Wait for all threads to call this. Returns once all the threads have
62-
* entered.
63-
*/
64-
public void blockForStart() {
68+
public void startMeasure() {
69+
state = State.MEASURE;
70+
}
6571

72+
public void startColdQuery() {
73+
state = State.COLD_QUERY;
74+
}
6675

67-
startBarrier.countDown();
68-
try {
69-
startBarrier.await();
70-
} catch (InterruptedException e) {
71-
throw new RuntimeException(e);
72-
}
73-
}
76+
public void startHotQuery() {
77+
state = State.MEASURE;
78+
}
7479

75-
public void startMeasure() {
76-
state = State.MEASURE;
77-
}
80+
public void signalLatencyComplete() {
81+
state = State.LATENCY_COMPLETE;
82+
}
7883

79-
public void startColdQuery() {
80-
state = State.COLD_QUERY;
81-
}
84+
public void ackLatencyComplete() {
85+
state = State.MEASURE;
86+
}
8287

83-
public void startHotQuery() {
84-
state = State.MEASURE;
85-
}
88+
public void signalError() {
89+
// A thread died, decrement the count and set error state
90+
notDoneCount.decrementAndGet();
91+
state = State.ERROR;
92+
}
8693

87-
public void signalLatencyComplete() {
88-
state = State.LATENCY_COMPLETE;
89-
}
94+
public void startCoolDown() {
95+
state = State.DONE;
9096

91-
public void ackLatencyComplete() {
92-
state = State.MEASURE;
93-
}
97+
// The master thread must also signal that it is done
98+
signalDone();
99+
}
94100

95-
public void signalError() {
96-
// A thread died, decrement the count and set error state
97-
notDoneCount.decrementAndGet();
98-
state = State.ERROR;
99-
}
101+
/** Notify that this thread has entered the done state. */
102+
public int signalDone() {
100103

101-
public void startCoolDown() {
102-
state = State.DONE;
104+
int current = notDoneCount.decrementAndGet();
103105

104-
// The master thread must also signal that it is done
105-
signalDone();
106+
if (LOG.isDebugEnabled()) {
107+
LOG.debug(String.format("%d workers are not done. Waiting until they finish", current));
106108
}
107-
108-
/**
109-
* Notify that this thread has entered the done state.
110-
*/
111-
public int signalDone() {
112-
113-
int current = notDoneCount.decrementAndGet();
114-
115-
if (LOG.isDebugEnabled()) {
116-
LOG.debug(String.format("%d workers are not done. Waiting until they finish", current));
117-
}
118-
if (current == 0) {
119-
// We are the last thread to notice that we are done: wake any
120-
// blocked workers
121-
this.state = State.EXIT;
122-
}
123-
return current;
109+
if (current == 0) {
110+
// We are the last thread to notice that we are done: wake any
111+
// blocked workers
112+
this.state = State.EXIT;
124113
}
125-
126-
}
114+
return current;
115+
}
116+
}

0 commit comments

Comments
 (0)