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
2023-09-26 16:29:30 +08:00
We build and test Apache Kafka with Java 8, 11, 17 and 21. We set the `release` parameter in javac and scalac
2020-05-06 13:20:29 +08:00
to `8` to ensure the generated binaries are compatible with Java 8 or higher (independently of the Java version
2024-01-20 06:08:07 +08:00
used for compilation). Java 8 support project-wide has been deprecated since Apache Kafka 3.0, Java 11 support for
the broker and tools has been deprecated since Apache Kafka 3.7 and removal of both is planned for Apache Kafka 4.0 (
see [KIP-750 ](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=181308223 ) and
[KIP-1013 ](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=284789510 ) for more details).
2018-10-29 02:31:39 +08:00
2021-07-16 06:29:18 +08:00
Scala 2.12 and 2.13 are supported and 2.13 is used by default. Scala 2.12 support has been deprecated since
Apache Kafka 3.0 and will be removed in Apache Kafka 4.0 (see [KIP-751 ](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=181308218 )
for more details). See below for how to use a specific Scala version or all of the supported Scala versions.
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 ###
./gradlew test # runs both unit and integration tests
./gradlew unitTest
./gradlew integrationTest
### Force re-running tests without code change ###
2023-03-06 10:53:49 +08:00
./gradlew test --rerun
./gradlew unitTest --rerun
./gradlew integrationTest --rerun
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
2022-03-24 11:09:05 +08:00
### Repeatedly running a particular unit/integration test ###
2023-03-06 10:53:49 +08:00
I=0; while ./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 ###
2015-12-16 02:55:24 +08:00
Change the log4j setting in either `clients/src/test/resources/log4j.properties` or `core/src/test/resources/log4j.properties`
2018-11-30 08:04:39 +08:00
./gradlew clients:test --tests RequestResponseTest
2014-03-01 05:53:37 +08:00
2020-02-06 08:41:28 +08:00
### Specifying test retries ###
By default, each failed test is retried once up to a maximum of five retries per test run. Tests are retried at the end of the test task. Adjust these parameters in the following way:
./gradlew test -PmaxTestRetries=1 -PmaxTestRetryFailures=5
See [Test Retry Gradle Plugin ](https://github.com/gradle/test-retry-gradle-plugin ) for more details.
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
KAFKA_CLUSTER_ID="$(./bin/kafka-storage.sh random-uuid)"
./bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
./bin/kafka-server-start.sh config/kraft/server.properties
2021-03-30 06:39:10 +08:00
### Running a Kafka broker in ZooKeeper mode
2021-03-20 07:42:37 +08:00
./bin/zookeeper-server-start.sh config/zookeeper.properties
./bin/kafka-server-start.sh config/server.properties
2014-02-14 11:45:53 +08:00
### Cleaning the build ###
./gradlew clean
2020-01-07 02:51:01 +08:00
### Running a task with one of the Scala versions available (2.12.x or 2.13.x) ###
2020-09-08 22:11:43 +08:00
*Note that if building the jars with a version other than 2.13.x, you need to set the `SCALA_VERSION` variable or change it in `bin/kafka-run-class.sh` to run the quick start.*
2018-10-29 02:31:39 +08:00
You can pass either the major version (eg 2.12) or the full version (eg 2.12.7):
./gradlew -PscalaVersion=2.12 jar
./gradlew -PscalaVersion=2.12 test
./gradlew -PscalaVersion=2.12 releaseTarGz
2015-12-16 02:55:24 +08:00
2019-06-23 00:26:14 +08:00
### Running a task with all the scala versions enabled by default ###
2015-12-16 02:55:24 +08:00
2020-04-20 10:24:21 +08:00
Invoke the `gradlewAll` script followed by the task(s):
2018-10-29 02:31:39 +08:00
2020-04-20 10:24:21 +08:00
./gradlewAll test
./gradlewAll jar
./gradlewAll releaseTarGz
2014-02-14 11:45:53 +08:00
### 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.
2021-10-18 16:19:05 +08:00
### Publishing the jar for all versions of Scala and for all projects to maven ###
2021-03-05 03:22:22 +08:00
The recommended command is:
./gradlewAll publish
For backwards compatibility, the following also works:
2020-04-20 10:24:21 +08:00
./gradlewAll uploadArchives
2014-02-08 05:48:04 +08:00
2016-09-18 06:01:32 +08:00
Please note for this to work you should create/update `${GRADLE_USER_HOME}/gradle.properties` (typically, `~/.gradle/gradle.properties` ) and assign the following variables
2014-02-08 05:48:04 +08:00
2014-02-14 11:45:53 +08:00
mavenUrl=
mavenUsername=
mavenPassword=
signing.keyId=
signing.password=
signing.secretKeyRingFile=
2014-02-08 05:48:04 +08:00
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 ALL the jars to the local Maven repository ###
The recommended command to build for both Scala 2.12 and 2.13 is:
2021-03-05 03:22:22 +08:00
./gradlewAll publishToMavenLocal
For backwards compatibility, the following also works:
2020-04-20 10:24:21 +08:00
./gradlewAll install
2015-05-30 05:50:45 +08:00
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
If needed, you can specify the Scala version with `-PscalaVersion=2.13` .
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:
2015-02-03 13:36:21 +08:00
./gradlew checkstyleMain checkstyleTest
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.
2017-03-01 07:04:02 +08:00
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