Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup CI tests against official Kafka broker #1

Merged
merged 12 commits into from
Sep 5, 2024
11 changes: 9 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ jobs:
with:
go-version: 1.22.x

- name: Install Kafka
run: |
wget --progress=dot:giga https://dlcdn.apache.org/kafka/3.8.0/kafka_2.13-3.8.0.tgz
tar -xzf kafka_2.13-3.8.0.tgz
sudo mv kafka_2.13-3.8.0/ /usr/local/kafka
export PATH=$PATH:/usr/local/kafka/bin

- name: Set up Python
uses: actions/setup-python@v1
with:
Expand All @@ -32,7 +39,7 @@ jobs:
with:
go-version: 1.22.x

- uses: dominikh/[email protected].0
- uses: dominikh/[email protected].1
with:
version: "2023.1"
version: "2024.1"
install-go: false
2 changes: 1 addition & 1 deletion internal/kafka_executable/kafka_executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewKafkaExecutable(stageHarness *test_case_harness.TestCaseHarness) *KafkaE

func (b *KafkaExecutable) Run(args ...string) error {
b.args = args
if b.args == nil || len(b.args) == 0 {
if len(b.args) == 0 {
b.logger.Infof("$ ./%s", path.Base(b.executable.Path))
} else {
var log string
Expand Down
14 changes: 12 additions & 2 deletions internal/stages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"os"
"regexp"
"testing"

tester_utils_testing "github.com/codecrafters-io/tester-utils/testing"
Expand All @@ -11,7 +12,7 @@ func TestStages(t *testing.T) {
os.Setenv("CODECRAFTERS_RANDOM_SEED", "1234567890")

testCases := map[string]tester_utils_testing.TesterOutputTestCase{
"transactions_pass": {
"base_stages_pass": {
UntilStageSlug: "cm4",
CodePath: "./test_helpers/pass_all",
ExpectedExitCode: 0,
Expand All @@ -22,7 +23,16 @@ func TestStages(t *testing.T) {

tester_utils_testing.TestTesterOutput(t, testerDefinition, testCases)
}

func normalizeTesterOutput(testerOutput []byte) []byte {
replacements := map[string][]*regexp.Regexp{
"": {regexp.MustCompile(`Failed to connect to broker .*`)},
}

for replacement, regexes := range replacements {
for _, regex := range regexes {
testerOutput = regex.ReplaceAll(testerOutput, []byte(replacement))
}
}

return testerOutput
}
5 changes: 0 additions & 5 deletions internal/test_helpers/fixtures/base/pass
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Debug = true
[stage-8] Running tests for Stage #8: cm4
[stage-8] $ ./your_program.sh
[stage-8] Connecting to broker at: localhost:9092
[your_program] Starting Kafka using KRaft (Kafka Raft Metadata)
[stage-8] Connection to broker at localhost:9092 successful
[stage-8] ✓ Correlation ID: -713223315
[stage-8] ✓ ErrorCode: 0
Expand All @@ -27,7 +26,6 @@ Debug = true
[stage-6] $ ./your_program.sh
[stage-6] Connecting to broker at: localhost:9092
[stage-6] Connection to broker at localhost:9092 successful
[your_program] Starting Kafka using KRaft (Kafka Raft Metadata)
[stage-6] ✓ Correlation ID: -198843362
[stage-6] ✓ API version 16 is supported for FETCH
[stage-6] Test passed.
Expand All @@ -48,7 +46,6 @@ Debug = true
[stage-4] $ ./your_program.sh
[stage-4] Connecting to broker at: localhost:9092
[stage-4] Connection to broker at localhost:9092 successful
[your_program] Starting Kafka using KRaft (Kafka Raft Metadata)
[stage-4] ✓ Correlation ID: -488288628
[stage-4] ✓ ErrorCode: 35 (UNSUPPORTED_VERSION)
[stage-4] Test passed.
Expand All @@ -59,7 +56,6 @@ Debug = true
[stage-3] $ ./your_program.sh
[stage-3] Connecting to broker at: localhost:9092
[stage-3] Connection to broker at localhost:9092 successful
[your_program] Starting Kafka using KRaft (Kafka Raft Metadata)
[stage-3] ✓ Correlation ID: 1743371062
[stage-3] Test passed.
[stage-3] Terminating program
Expand All @@ -68,7 +64,6 @@ Debug = true
[stage-2] Running tests for Stage #2: nv3
[stage-2] $ ./your_program.sh
[stage-2] Connecting to broker at: localhost:9092
[your_program] Starting Kafka using KRaft (Kafka Raft Metadata)
[stage-2] Connection to broker at localhost:9092 successful
[stage-2] ✓ Correlation ID: 7
[stage-2] Test passed.
Expand Down
132 changes: 132 additions & 0 deletions internal/test_helpers/pass_all/kraft.server.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# This configuration file is intended for use in KRaft mode, where
# Apache ZooKeeper is not present.
#

############################# Server Basics #############################

# The role of this server. Setting this puts us in KRaft mode
process.roles=broker,controller

# The node id associated with this instance's roles
node.id=1

# The connect string for the controller quorum
controller.quorum.voters=1@localhost:9093

############################# Socket Server Settings #############################

# The address the socket server listens on.
# Combined nodes (i.e. those with `process.roles=broker,controller`) must list the controller listener here at a minimum.
# If the broker listener is not defined, the default listener will use a host name that is equal to the value of java.net.InetAddress.getCanonicalHostName(),
# with PLAINTEXT listener name, and port 9092.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://:9092,CONTROLLER://:9093

# Name of listener used for communication between brokers.
inter.broker.listener.name=PLAINTEXT

# Listener name, hostname and port the broker will advertise to clients.
# If not set, it uses the value for "listeners".
advertised.listeners=PLAINTEXT://localhost:9092

# A comma-separated list of the names of the listeners used by the controller.
# If no explicit mapping set in `listener.security.protocol.map`, default will be using PLAINTEXT protocol
# This is required if running in KRaft mode.
controller.listener.names=CONTROLLER

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma separated list of directories under which to store log files
log.dirs=/tmp/kraft-combined-logs

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
# 1. Durability: Unflushed data may be lost if you are not using replication.
# 2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
# 3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000
13 changes: 5 additions & 8 deletions internal/test_helpers/pass_all/your_program.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/bin/sh
echo "Starting Kafka using KRaft (Kafka Raft Metadata)"
cd /tmp
rm -rf /tmp/kafka-logs /tmp/zookeeper /tmp/kraft-combined-logs
cd /Users/ryang/Developer/tmp/kafka/kafka_2.13-3.8.0
# For fresh start
# 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 --override log.dirs=/Users/ryang/Developer/work/course-testers/kafka-tester/internal/test_helpers/pass_all/kraft-combined-logs > /dev/null 2>&1
# echo "Starting Kafka using KRaft (Kafka Raft Metadata)"

SCRIPT_DIR=$(dirname "$(realpath "$0")")

/usr/local/kafka/bin/kafka-server-start.sh $SCRIPT_DIR/kraft.server.properties --override log.dirs=$SCRIPT_DIR/kraft-combined-logs > /dev/null 2>&1
1 change: 1 addition & 0 deletions protocol/api/api_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func DecodeApiVersionsHeader(response []byte, version int16) (*ResponseHeader, e
return &responseHeader, nil
}

// DecodeApiVersionsHeaderAndResponse decodes the header and response
// If an error is encountered while decoding, the returned objects are nil
func DecodeApiVersionsHeaderAndResponse(response []byte, version int16) (*ResponseHeader, *ApiVersionsResponse, error) {
decoder := decoder.RealDecoder{}
Expand Down
7 changes: 4 additions & 3 deletions protocol/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ func (b *Broker) ConnectWithRetries(executable *kafka_executable.KafkaExecutable
}

// Don't print errors in the first second
if retries > 2 {
logger.Infof("Failed to connect to broker at %s, retrying in 1s", b.addr)
}
// ToDo: fixtures fail
// if retries > 2 {
// logger.Infof("Failed to connect to broker at %s, retrying in 1s", b.addr)
// }

retries += 1
time.Sleep(1000 * time.Millisecond)
Expand Down
1 change: 1 addition & 0 deletions protocol/decoder/real_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,5 +473,6 @@ func (rd *RealDecoder) FormatDetailedError(message string) error {
lines = append(lines, receivedByteString.FormatWithHighlightedOffset(offset, "error", "Received: ", suffix))
lines = append(lines, message)

//lint:ignore SA1006 we are okay with this
return fmt.Errorf(strings.Join(lines, "\n"))
}
1 change: 1 addition & 0 deletions protocol/encoder/real_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (re *RealEncoder) PutCompactString(in string) {
func (re *RealEncoder) PutNullableCompactString(in *string) {
if in == nil {
re.PutInt8(0)
return
}
re.PutCompactString(*in)
}
Expand Down
6 changes: 6 additions & 0 deletions staticcheck.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
checks = [
"all",
"-ST1005", # We're okay with capitalized errors
"-ST1000", # We're okay with no package comments
"-ST1003", # We're okay with bad package names (we've got too many already)
]
Loading