2014-02-14 11:45:53 +08:00
Apache Kafka
=================
2019-04-23 02:58:25 +08:00
See our [web site ](https://kafka.apache.org ) for details on the project.
2011-08-02 07:41:24 +08:00
2019-11-22 00:06:06 +08:00
You need to have [Java ](http://www.oracle.com/technetwork/java/javase/downloads/index.html ) installed.
2016-01-27 07:33:48 +08:00
2024-11-28 05:23:17 +08:00
We build and test Apache Kafka with 17 and 23. The `release` parameter in javac and scalac is set to `11` for the clients
and streams modules, and `17` for the broker and tools, ensuring compatibility with their respective minimum Java versions.
2018-10-29 02:31:39 +08:00
2024-10-07 01:34:38 +08:00
Scala 2.13 is the only supported version in Apache Kafka.
2014-09-24 00:06:17 +08:00
2017-03-21 17:55:46 +08:00
### Build a jar and run it ###
2016-09-15 01:38:40 +08:00
./gradlew jar
2011-08-02 07:41:24 +08:00
2020-08-12 10:23:58 +08:00
Follow instructions in https://kafka.apache.org/quickstart
2011-08-02 07:41:24 +08:00
2017-03-21 17:55:46 +08:00
### Build source jar ###
2014-04-19 04:10:34 +08:00
./gradlew srcJar
2017-03-21 17:55:46 +08:00
### Build aggregated javadoc ###
2016-05-19 07:44:31 +08:00
./gradlew aggregatedJavadoc
2017-03-21 17:55:46 +08:00
### Build javadoc and scaladoc ###
2014-04-19 04:10:34 +08:00
./gradlew javadoc
2016-05-19 07:44:31 +08:00
./gradlew javadocJar # builds a javadoc jar for each module
2014-04-19 04:10:34 +08:00
./gradlew scaladoc
2016-05-19 07:44:31 +08:00
./gradlew scaladocJar # builds a scaladoc jar for each module
./gradlew docsJar # builds both (if applicable) javadoc and scaladoc jars for each module
2014-04-19 04:10:34 +08:00
2017-03-21 17:55:46 +08:00
### Run unit/integration tests ###
2024-10-06 14:09:24 +08:00
./gradlew test # runs both unit and integration tests
2017-03-21 17:55:46 +08:00
./gradlew unitTest
./gradlew integrationTest
2024-10-06 14:09:24 +08:00
./gradlew quarantinedTest # runs the quarantined tests
2017-03-21 17:55:46 +08:00
### Force re-running tests without code change ###
2024-10-06 14:09:24 +08:00
./gradlew test --rerun-tasks
./gradlew unitTest --rerun-tasks
./gradlew integrationTest --rerun-tasks
2014-02-14 11:45:53 +08:00
2017-03-21 17:55:46 +08:00
### Running a particular unit/integration test ###
2018-11-30 08:04:39 +08:00
./gradlew clients:test --tests RequestResponseTest
2014-02-14 11:45:53 +08:00
2024-11-06 10:49:37 +08:00
### Repeatedly running a particular unit/integration test with specific times by setting N ###
N=500; I=0; while [ $I -lt $N ] & & ./gradlew clients:test --tests RequestResponseTest --rerun --fail-fast; do (( I=$I+1 )); echo "Completed run: $I"; sleep 1; done
2022-03-24 11:09:05 +08:00
2017-03-21 17:55:46 +08:00
### Running a particular test method within a unit/integration test ###
2015-10-19 13:45:27 +08:00
./gradlew core:test --tests kafka.api.ProducerFailureHandlingTest.testCannotSendToInternalTopic
2022-02-21 14:48:24 +08:00
./gradlew clients:test --tests org.apache.kafka.clients.MetadataTest.testTimeToNextUpdate
2015-01-26 11:15:51 +08:00
2017-03-21 17:55:46 +08:00
### Running a particular unit/integration test with log4j output ###
2024-12-14 01:14:31 +08:00
By default, there will be only small number of logs output while testing. You can adjust it by changing the `log4j2.yml` file in the module's `src/test/resources` directory.
2015-12-16 02:55:24 +08:00
2024-12-14 01:14:31 +08:00
For example, if you want to see more logs for clients project tests, you can modify [the line ](https://github.com/apache/kafka/blob/trunk/clients/src/test/resources/log4j2.yml#L35 ) in `clients/src/test/resources/log4j2.yml`
to `level: INFO` and then run:
2024-03-27 01:06:47 +08:00
./gradlew cleanTest clients:test --tests NetworkClientTest
And you should see `INFO` level logs in the file under the `clients/build/test-results/test` directory.
2014-03-01 05:53:37 +08:00
2020-02-06 08:41:28 +08:00
### Specifying test retries ###
2024-11-06 11:09:09 +08:00
Retries are disabled by default, but you can set maxTestRetryFailures and maxTestRetries to enable retries.
The following example declares -PmaxTestRetries=1 and -PmaxTestRetryFailures=3 to enable a failed test to be retried once, with a total retry limit of 3.
2020-02-06 08:41:28 +08:00
2024-10-06 14:09:24 +08:00
./gradlew test -PmaxTestRetries=1 -PmaxTestRetryFailures=3
2024-11-06 11:09:09 +08:00
The quarantinedTest task also has no retries by default, but you can set maxQuarantineTestRetries and maxQuarantineTestRetryFailures to enable retries, similar to the test task.
2024-10-06 14:09:24 +08:00
2024-11-06 11:09:09 +08:00
./gradlew quarantinedTest -PmaxQuarantineTestRetries=3 -PmaxQuarantineTestRetryFailures=20
2024-10-06 14:09:24 +08:00
See [Test Retry Gradle Plugin ](https://github.com/gradle/test-retry-gradle-plugin ) for and [build.yml ](.github/workflows/build.yml ) more details.
2020-02-06 08:41:28 +08:00
2016-04-01 01:07:54 +08:00
### Generating test coverage reports ###
2017-02-11 01:15:12 +08:00
Generate coverage reports for the whole project:
2021-05-12 23:13:03 +08:00
./gradlew reportCoverage -PenableTestCoverage=true -Dorg.gradle.parallel=false
2016-04-01 01:07:54 +08:00
2017-02-11 01:15:12 +08:00
Generate coverage for a single module, i.e.:
2021-05-12 23:13:03 +08:00
./gradlew clients:reportCoverage -PenableTestCoverage=true -Dorg.gradle.parallel=false
2017-02-11 01:15:12 +08:00
2014-02-14 11:45:53 +08:00
### Building a binary release gzipped tar ball ###
2018-10-29 02:31:39 +08:00
./gradlew clean releaseTarGz
2015-12-16 02:55:24 +08:00
The release file can be found inside `./core/build/distributions/` .
2014-02-08 05:48:04 +08:00
2020-04-16 04:29:35 +08:00
### Building auto generated messages ###
Sometimes it is only necessary to rebuild the RPC auto-generated message data when switching between branches, as they could
fail due to code changes. You can just run:
./gradlew processMessages processTestMessages
2022-10-18 21:12:25 +08:00
### Running a Kafka broker in KRaft mode
2024-03-27 11:11:09 +08:00
Using compiled files:
2022-10-18 21:12:25 +08:00
KAFKA_CLUSTER_ID="$(./bin/kafka-storage.sh random-uuid)"
2024-12-08 01:06:51 +08:00
./bin/kafka-storage.sh format --standalone -t $KAFKA_CLUSTER_ID -c config/kraft/reconfig-server.properties
2024-11-08 03:58:21 +08:00
./bin/kafka-server-start.sh config/kraft/reconfig-server.properties
2022-10-18 21:12:25 +08:00
2024-03-27 11:11:09 +08:00
Using docker image:
docker run -p 9092:9092 apache/kafka:3.7.0
2014-02-14 11:45:53 +08:00
### Cleaning the build ###
./gradlew clean
### Running a task for a specific project ###
2015-12-16 02:55:24 +08:00
This is for `core` , `examples` and `clients`
2014-02-14 11:45:53 +08:00
./gradlew core:jar
./gradlew core:test
2014-02-08 05:48:04 +08:00
2020-07-25 04:30:20 +08:00
Streams has multiple sub-projects, but you can run all the tests:
./gradlew :streams:testAll
2014-02-14 11:45:53 +08:00
### Listing all gradle tasks ###
./gradlew tasks
2014-02-08 05:48:04 +08:00
2014-02-14 11:45:53 +08:00
### Building IDE project ####
2015-12-16 02:55:24 +08:00
*Note that this is not strictly necessary (IntelliJ IDEA has good built-in support for Gradle projects, for example).*
2014-02-14 11:45:53 +08:00
./gradlew eclipse
./gradlew idea
2014-02-08 05:48:04 +08:00
2017-02-10 10:37:32 +08:00
The `eclipse` task has been configured to use `${project_dir}/build_eclipse` as Eclipse's build directory. Eclipse's default
build directory (`${project_dir}/bin`) clashes with Kafka's scripts directory and we don't use Gradle's build directory
to avoid known issues with this configuration.
2017-08-12 03:19:28 +08:00
### Publishing the streams quickstart archetype artifact to maven ###
For the Streams archetype project, one cannot use gradle to upload to maven; instead the `mvn deploy` command needs to be called at the quickstart folder:
cd streams/quickstart
mvn deploy
Please note for this to work you should create/update user maven settings (typically, `${USER_HOME}/.m2/settings.xml` ) to assign the following variables
< settings xmlns = "http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
< servers >
...
< server >
< id > apache.snapshots.https< / id >
< username > ${maven_username}< / username >
< password > ${maven_password}< / password >
< / server >
< server >
< id > apache.releases.https< / id >
< username > ${maven_username}< / username >
< password > ${maven_password}< / password >
< / server >
...
< / servers >
...
2022-03-24 02:07:02 +08:00
### Installing specific projects to the local Maven repository ###
2022-10-24 23:55:48 +08:00
./gradlew -PskipSigning=true :streams:publishToMavenLocal
2022-03-24 02:07:02 +08:00
2014-02-14 11:45:53 +08:00
### Building the test jar ###
./gradlew testJar
2014-02-08 05:48:04 +08:00
2017-03-01 07:04:02 +08:00
### Running code quality checks ###
2018-09-11 04:14:00 +08:00
There are two code quality analysis tools that we regularly run, spotbugs and checkstyle.
2017-03-01 07:04:02 +08:00
2017-03-21 17:55:46 +08:00
#### Checkstyle ####
2017-03-01 07:04:02 +08:00
Checkstyle enforces a consistent coding style in Kafka.
You can run checkstyle using:
2024-06-03 04:02:07 +08:00
./gradlew checkstyleMain checkstyleTest spotlessCheck
2011-08-02 07:41:24 +08:00
2017-03-01 07:04:02 +08:00
The checkstyle warnings will be found in `reports/checkstyle/reports/main.html` and `reports/checkstyle/reports/test.html` files in the
2019-12-18 23:19:17 +08:00
subproject build directories. They are also printed to the console. The build will fail if Checkstyle fails.
2024-12-02 21:14:30 +08:00
For experiments (or regression testing purposes) add `-PcheckstyleVersion=X.y.z` switch (to override project-defined checkstyle version).
2017-03-01 07:04:02 +08:00
2024-06-03 04:02:07 +08:00
#### Spotless ####
2024-10-18 10:54:02 +08:00
The import order is a part of static check. please call `spotlessApply` to optimize the imports of Java codes before filing pull request.
2024-06-03 04:02:07 +08:00
./gradlew spotlessApply
2018-09-11 04:14:00 +08:00
#### Spotbugs ####
Spotbugs uses static analysis to look for bugs in the code.
You can run spotbugs using:
2017-03-01 07:04:02 +08:00
2018-09-11 04:14:00 +08:00
./gradlew spotbugsMain spotbugsTest -x test
2017-03-01 07:04:02 +08:00
2018-09-11 04:14:00 +08:00
The spotbugs warnings will be found in `reports/spotbugs/main.html` and `reports/spotbugs/test.html` files in the subproject build
directories. Use -PxmlSpotBugsReport=true to generate an XML report instead of an HTML one.
2015-10-13 09:26:38 +08:00
2020-12-11 09:56:52 +08:00
### JMH microbenchmarks ###
We use [JMH ](https://openjdk.java.net/projects/code-tools/jmh/ ) to write microbenchmarks that produce reliable results in the JVM.
See [jmh-benchmarks/README.md ](https://github.com/apache/kafka/blob/trunk/jmh-benchmarks/README.md ) for details on how to run the microbenchmarks.
2022-10-18 02:27:49 +08:00
### Dependency Analysis ###
The gradle [dependency debugging documentation ](https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html ) mentions using the `dependencies` or `dependencyInsight` tasks to debug dependencies for the root project or individual subprojects.
Alternatively, use the `allDeps` or `allDepInsight` tasks for recursively iterating through all subprojects:
./gradlew allDeps
./gradlew allDepInsight --configuration runtimeClasspath --dependency com.fasterxml.jackson.core:jackson-databind
These take the same arguments as the builtin variants.
### Determining if any dependencies could be updated ###
./gradlew dependencyUpdates
2016-01-09 07:13:14 +08:00
### Common build options ###
2017-04-21 18:29:22 +08:00
The following options should be set with a `-P` switch, for example `./gradlew -PmaxParallelForks=1 test` .
2016-01-09 07:13:14 +08:00
2017-08-04 06:24:05 +08:00
* `commitId` : sets the build commit ID as .git/HEAD might not be correct if there are local commits added for build purposes.
2017-04-21 18:29:22 +08:00
* `mavenUrl` : sets the URL of the maven deployment repository (`file://path/to/repo` can be used to point to a local repository).
2022-02-09 03:03:19 +08:00
* `maxParallelForks` : maximum number of test processes to start in parallel. Defaults to the number of processors available to the JVM.
* `maxScalacThreads` : maximum number of worker threads for the scalac backend. Defaults to the lowest of `8` and the number of processors
available to the JVM. The value must be between 1 and 16 (inclusive).
2020-09-02 06:12:10 +08:00
* `ignoreFailures` : ignore test failures from junit
2017-04-21 18:29:22 +08:00
* `showStandardStreams` : shows standard out and standard error of the test JVM(s) on the console.
* `skipSigning` : skips signing of artifacts.
* `testLoggingEvents` : unit test events to be logged, separated by comma. For example `./gradlew -PtestLoggingEvents=started,passed,skipped,failed test` .
2018-09-11 04:14:00 +08:00
* `xmlSpotBugsReport` : enable XML reports for spotBugs. This also disables HTML reports as only one can be enabled at a time.
2022-02-09 03:03:19 +08:00
* `maxTestRetries` : maximum number of retries for a failing test case.
2020-02-06 08:41:28 +08:00
* `maxTestRetryFailures` : maximum number of test failures before retrying is disabled for subsequent tests.
2020-06-19 22:32:53 +08:00
* `enableTestCoverage` : enables test coverage plugins and tasks, including bytecode enhancement of classes required to track said
coverage. Note that this introduces some overhead when running tests and hence why it's disabled by default (the overhead
varies, but 15-20% is a reasonable estimate).
MINOR: Reuse gradle daemon for scala compilation by default (#12280)
The time time to compile was reduced from 5.3 seconds to 0.9 seconds by
changing the keep alive mode from `SESSION` to `DAEMON`. We change the
default to the latter and allow the former to be set via a `keepAliveMode`
property.
Also update the Jenkins build to use `keepAliveMode="session"` instead of
`--no-daemon`.
./gradlew compileTestScala -PkeepAliveMode="daemon" --info:
> Task :streams:streams-scala:compileTestScala
> Watching 928 directories to track changes
> Caching disabled for task ':streams:streams-scala:compileTestScala' because:
> Build cache is disabled
> Task ':streams:streams-scala:compileTestScala' is not up-to-date because:
> Input property 'source' file /home/ijuma/src/kafka/streams/streams-scala/src/test/scala/org/apache/kafka/streams/scala/kstream/JoinedTest.scala has changed.
> Watching 920 directories to track changes
> Watching 919 directories to track changes
> Compiling with Zinc Scala compiler.
> Prepared Zinc Scala inputs: 0.004 secs
> compiling 16 Scala sources to /home/ijuma/src/kafka/streams/streams-scala/build/classes/scala/test ...
> done compiling
> Completed Scala compilation: 0.925 secs
./gradlew compileTestScala -PkeepAliveMode="session" --info
> Task :streams:streams-scala:compileTestScala
> Watching 928 directories to track changes
> Caching disabled for task ':streams:streams-scala:compileTestScala' because:
> Build cache is disabled
> Task ':streams:streams-scala:compileTestScala' is not up-to-date because:
> Input property 'source' file /home/ijuma/src/kafka/streams/streams-scala/src/test/scala/org/apache/kafka/streams/scala/kstream/JoinedTest.scala has changed.
> Watching 920 directories to track changes
> Watching 919 directories to track changes
> Starting process 'Gradle Worker Daemon 3'. Working directory: /home/ijuma/.gradle/workers Command: /usr/java/jdk-17.0.4.1+1/bin/java -Xss4m -XX:+UseParallelGC --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.nio.file=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.regex=ALL-UNNAMED --add-opens=java.base/java.util.stream=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED @/home/ijuma/.gradle/.tmp/gradle-worker-classpath3305983246500958050txt -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Worker Daemon 3'
> Successfully started process 'Gradle Worker Daemon 3'
> Started Gradle worker daemon (0.244 secs) with fork options DaemonForkOptions{executable=/usr/java/jdk-17.0.4.1+1/bin/java, minHeapSize=null, maxHeapSize=2048m, jvmArgs=[-Xss4m, -XX:+UseParallelGC, --add-opens=java.base/java.io=ALL-UNNAMED, --add-opens=java.base/java.lang=ALL-UNNAMED, --add-opens=java.base/java.nio=ALL-UNNAMED, --add-opens=java.base/java.nio.file=ALL-UNNAMED, --add-opens=java.base/java.util=ALL-UNNAMED, --add-opens=java.base/java.util.concurrent=ALL-UNNAMED, --add-opens=java.base/java.util.regex=ALL-UNNAMED, --add-opens=java.base/java.util.stream=ALL-UNNAMED, --add-opens=java.base/java.text=ALL-UNNAMED, --add-opens=java.base/java.time=ALL-UNNAMED, --add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED], keepAliveMode=SESSION}.
> Compiling with Zinc Scala compiler.
> Prepared Zinc Scala inputs: 0.187 secs
> compiling 16 Scala sources to /home/ijuma/src/kafka/streams/streams-scala/build/classes/scala/test ...
> done compiling
> Completed Scala compilation: 5.307 secs
See https://github.com/gradle/gradle/issues/20579 for more details.
Reviewers: Manikumar Reddy <manikumar.reddy@gmail.com>
2022-12-01 21:33:43 +08:00
* `keepAliveMode` : configures the keep alive mode for the Gradle compilation daemon - reuse improves start-up time. The values should
be one of `daemon` or `session` (the default is `daemon` ). `daemon` keeps the daemon alive until it's explicitly stopped while
`session` keeps it alive until the end of the build session. This currently only affects the Scala compiler, see
https://github.com/gradle/gradle/pull/21034 for a PR that attempts to do the same for the Java compiler.
2021-02-23 13:11:17 +08:00
* `scalaOptimizerMode` : configures the optimizing behavior of the scala compiler, the value should be one of `none` , `method` , `inline-kafka` or
`inline-scala` (the default is `inline-kafka` ). `none` is the scala compiler default, which only eliminates unreachable code. `method` also
includes method-local optimizations. `inline-kafka` adds inlining of methods within the kafka packages. Finally, `inline-scala` also
includes inlining of methods within the scala library (which avoids lambda allocations for methods like `Option.exists` ). `inline-scala` is
only safe if the Scala library version is the same at compile time and runtime. Since we cannot guarantee this for all cases (for example, users
may depend on the kafka jar for integration tests where they may include a scala library with a different version), we don't enable it by
default. See https://www.lightbend.com/blog/scala-inliner-optimizer for more details.
2016-01-09 07:13:14 +08:00
2019-06-14 21:21:38 +08:00
### Running system tests ###
See [tests/README.md ](tests/README.md ).
2014-12-05 21:37:11 +08:00
### Running in Vagrant ###
See [vagrant/README.md ](vagrant/README.md ).
2014-02-14 11:45:53 +08:00
### Contribution ###
2013-07-11 23:06:43 +08:00
2014-09-15 01:16:07 +08:00
Apache Kafka is interested in building the community; we would welcome any thoughts or [patches ](https://issues.apache.org/jira/browse/KAFKA ). You can reach us [on the Apache mailing lists ](http://kafka.apache.org/contact.html ).
2013-07-11 23:06:43 +08:00
To contribute follow the instructions here:
KAFKA-15183: Add more controller, loader, snapshot emitter metrics (#14010)
Implement some of the metrics from KIP-938: Add more metrics for
measuring KRaft performance.
Add these metrics to QuorumControllerMetrics:
kafka.controller:type=KafkaController,name=TimedOutBrokerHeartbeatCount
kafka.controller:type=KafkaController,name=EventQueueOperationsStartedCount
kafka.controller:type=KafkaController,name=EventQueueOperationsTimedOutCount
kafka.controller:type=KafkaController,name=NewActiveControllersCount
Create LoaderMetrics with these new metrics:
kafka.server:type=MetadataLoader,name=CurrentMetadataVersion
kafka.server:type=MetadataLoader,name=HandleLoadSnapshotCount
Create SnapshotEmitterMetrics with these new metrics:
kafka.server:type=SnapshotEmitter,name=LatestSnapshotGeneratedBytes
kafka.server:type=SnapshotEmitter,name=LatestSnapshotGeneratedAgeMs
Reviewers: Ron Dagostino <rndgstn@gmail.com>
2023-07-25 12:13:58 +08:00
* https://kafka.apache.org/contributing.html