Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ KafScale is built on the assumption that broker state is a historical constraint

In scope:
- Kafka wire protocol for core produce, fetch, and consumer group APIs
- Nontransactional idempotent producers with durable retry deduplication
- Immutable S3-backed segment storage
- Stateless broker pods
- etcd-backed metadata, offsets, and consumer group state
Expand Down Expand Up @@ -159,13 +160,12 @@ git clone https://github.com/scalytics/kaf-mirror.git && cd kaf-mirror
mkdir -p bin
go build -o bin/kaf-mirror ./cmd/kaf-mirror
go build -o bin/mirror-cli ./cmd/mirror-cli
KAFMIRROR_DISABLE_IDEMPOTENT_WRITES=true ./bin/kaf-mirror
./bin/kaf-mirror
```

KafScale does not implement `InitProducerId`, so idempotent writes must be off
when KafScale is the target. The environment variable above does that for the
whole process. On first start kaf-mirror creates `admin@localhost` and prints a
random password to its log (`logs/kaf-mirror.log`).
KafScale supports nontransactional idempotent producers through both the broker
and Kubernetes proxy. Leave transactions disabled. See [producer guarantees](docs/protocol.md#idempotent-producers).
On first start kaf-mirror creates `admin@localhost` and prints a random password to its log (`logs/kaf-mirror.log`).

**3. Add both clusters and start a job**

Expand Down
39 changes: 39 additions & 0 deletions cmd/broker/committed_fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2026 Alexander Alten (novatechflow), NovaTechflow (novatechflow.com).
// This project is supported and financed by Scalytics, Inc. (www.scalytics.io).
//
// Licensed 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.

package main

import "encoding/binary"

// limitFetchToStableOffset fences a read at the durable offset captured for this
// response. A concurrent flush or a snap across a retained-data gap can make Read
// return newer batches. Never expose a batch that crosses the captured boundary.
func limitFetchToStableOffset(records []byte, stable int64) []byte {
end := 0
for len(records)-end >= 61 {
batch := records[end:]
length := int64(int32(binary.BigEndian.Uint32(batch[8:12]))) + 12
if length < 61 || length > int64(len(batch)) {
break
}
base := int64(binary.BigEndian.Uint64(batch[:8]))
delta := int64(int32(binary.BigEndian.Uint32(batch[23:27])))
if base < 0 || delta < 0 || base >= stable || delta >= stable-base {
break
}
end += int(length)
}
return records[:end]
}
86 changes: 86 additions & 0 deletions cmd/broker/committed_fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2026 Alexander Alten (novatechflow), NovaTechflow (novatechflow.com).
// This project is supported and financed by Scalytics, Inc. (www.scalytics.io).
//
// Licensed 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.

package main

import (
"context"
"testing"

"github.com/KafScale/platform/v2/pkg/metadata"
"github.com/KafScale/platform/v2/pkg/protocol"
"github.com/twmb/franz-go/pkg/kmsg"
)

func TestReadCommittedExcludesBufferedTail(t *testing.T) {
ctx := context.Background()
h := newFlushDisabledHandler(metadata.NewInMemoryStore(defaultMetadata()))
t.Cleanup(h.coordinator.Stop)
produce := &kmsg.ProduceRequest{Version: 9, Acks: -1, Topics: []kmsg.ProduceRequestTopic{{Topic: "orders", Partitions: []kmsg.ProduceRequestTopicPartition{{Records: testBatchBytes(0, 0, 1)}}}}}
wire, err := h.Handle(ctx, &protocol.RequestHeader{APIKey: 0, APIVersion: 9}, produce)
if err != nil {
t.Fatal(err)
}
if p := decodeKmsgResponse(t, 9, wire, kmsg.NewPtrProduceResponse).Topics[0].Partitions[0]; p.ErrorCode != 0 {
t.Fatal(p)
}
fetch := func(isolation int8, offset int64) kmsg.FetchResponseTopicPartition {
t.Helper()
req := kmsg.NewPtrFetchRequest()
req.Version, req.IsolationLevel, req.MaxBytes = 4, isolation, 1<<20
req.Topics = []kmsg.FetchRequestTopic{{Topic: "orders", Partitions: []kmsg.FetchRequestTopicPartition{{FetchOffset: offset, PartitionMaxBytes: 1 << 20}}}}
wire, err := h.Handle(ctx, &protocol.RequestHeader{APIKey: 1, APIVersion: 4}, req)
if err != nil {
t.Fatal(err)
}
part := decodeKmsgResponse(t, 4, wire, kmsg.NewPtrFetchResponse).Topics[0].Partitions[0]
if part.ErrorCode != 0 {
t.Fatal(part)
}
return part
}
if p := fetch(0, 0); len(p.RecordBatches) == 0 || p.HighWatermark != 1 || p.LastStableOffset != 0 {
t.Fatalf("uncommitted buffered read: %+v", p)
}
if p := fetch(1, 0); len(p.RecordBatches) != 0 || p.HighWatermark != 0 || p.LastStableOffset != 0 {
t.Fatalf("committed exposed buffered tail: %+v", p)
}
if p := fetch(1, 1); len(p.RecordBatches) != 0 || p.LastStableOffset != 0 {
t.Fatalf("position in volatile tail must wait: %+v", p)
}
log, err := h.getPartitionLog(ctx, "orders", 0)
if err != nil {
t.Fatal(err)
}
if err := log.Flush(ctx); err != nil {
t.Fatal(err)
}
if p := fetch(1, 0); len(p.RecordBatches) == 0 || p.HighWatermark != 1 || p.LastStableOffset != 1 || len(p.AbortedTransactions) != 0 {
t.Fatalf("committed did not advance after flush: %+v", p)
}
}

func TestCommittedFetchStableBoundary(t *testing.T) {
first, second := testBatchBytes(0, 1, 2), testBatchBytes(2, 0, 1)
records := append(append([]byte(nil), first...), second...)
for _, tc := range []struct {
stable int64
want int
}{{0, 0}, {1, 0}, {2, len(first)}, {3, len(records)}} {
if got := limitFetchToStableOffset(records, tc.stable); len(got) != tc.want {
t.Fatalf("stable %d: got %d bytes, want %d", tc.stable, len(got), tc.want)
}
}
}
57 changes: 30 additions & 27 deletions cmd/broker/fetch_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,36 @@ func TestFetchCompatibilityWireVersions(t *testing.T) {
t.Fatal(resp)
}
for version := int16(4); version <= 13; version++ {
t.Run(fmt.Sprint(version), func(t *testing.T) {
req := kmsg.NewPtrFetchRequest()
req.Version, req.ReplicaID, req.MinBytes, req.MaxBytes = version, -1, 1, 1<<20
req.Topics = []kmsg.FetchRequestTopic{{Topic: "orders", TopicID: topicID, Partitions: []kmsg.FetchRequestTopicPartition{{Partition: 0, FetchOffset: 0, PartitionMaxBytes: 1 << 20}}}}
formatter := kmsg.NewRequestFormatter(kmsg.FormatterClientID("compatibility"))
framed := formatter.AppendRequest(nil, req, 42)
header, parsed, err := protocol.ParseRequest(framed[4:])
if err != nil {
t.Fatal(err)
}
decoded, ok := parsed.(*kmsg.FetchRequest)
if !ok || decoded.Version != version || decoded.ReplicaID != -1 || decoded.MinBytes != 1 || decoded.MaxBytes != 1<<20 || len(decoded.Topics) != 1 || len(decoded.Topics[0].Partitions) != 1 || decoded.Topics[0].Partitions[0].PartitionMaxBytes != 1<<20 {
t.Fatalf("Fetch v%d request round trip: %+v", version, parsed)
}
wire, err := h.Handle(ctx, header, parsed)
if err != nil {
t.Fatal(err)
}
resp := decodeKmsgResponse(t, version, wire, kmsg.NewPtrFetchResponse)
if len(resp.Topics) != 1 || len(resp.Topics[0].Partitions) != 1 {
t.Fatal(resp)
}
part := resp.Topics[0].Partitions[0]
if part.ErrorCode != 0 || part.HighWatermark != 1 || len(part.RecordBatches) == 0 {
t.Fatal(part)
}
})
for _, isolation := range []int8{0, 1} {
t.Run(fmt.Sprintf("v%d/isolation%d", version, isolation), func(t *testing.T) {
req := kmsg.NewPtrFetchRequest()
req.IsolationLevel = isolation
req.Version, req.ReplicaID, req.MinBytes, req.MaxBytes = version, -1, 1, 1<<20
req.Topics = []kmsg.FetchRequestTopic{{Topic: "orders", TopicID: topicID, Partitions: []kmsg.FetchRequestTopicPartition{{Partition: 0, FetchOffset: 0, PartitionMaxBytes: 1 << 20}}}}
formatter := kmsg.NewRequestFormatter(kmsg.FormatterClientID("compatibility"))
framed := formatter.AppendRequest(nil, req, 42)
header, parsed, err := protocol.ParseRequest(framed[4:])
if err != nil {
t.Fatal(err)
}
decoded, ok := parsed.(*kmsg.FetchRequest)
if !ok || decoded.Version != version || decoded.IsolationLevel != isolation || decoded.ReplicaID != -1 || decoded.MinBytes != 1 || decoded.MaxBytes != 1<<20 || len(decoded.Topics) != 1 || len(decoded.Topics[0].Partitions) != 1 || decoded.Topics[0].Partitions[0].PartitionMaxBytes != 1<<20 {
t.Fatalf("Fetch v%d request round trip: %+v", version, parsed)
}
wire, err := h.Handle(ctx, header, parsed)
if err != nil {
t.Fatal(err)
}
resp := decodeKmsgResponse(t, version, wire, kmsg.NewPtrFetchResponse)
if len(resp.Topics) != 1 || len(resp.Topics[0].Partitions) != 1 {
t.Fatal(resp)
}
part := resp.Topics[0].Partitions[0]
if part.ErrorCode != 0 || part.HighWatermark != 1 || part.LastStableOffset != 1 || len(part.AbortedTransactions) != 0 || len(part.RecordBatches) == 0 {
t.Fatal(part)
}
})
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/broker/fetch_offset_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func TestFetchOffsetReadFailuresDoNotResetOffsets(t *testing.T) {
if offset > 3 {
want = protocol.OFFSET_OUT_OF_RANGE
}
if part.ErrorCode != want || part.HighWatermark != 3 || part.LastStableOffset != 3 {
stable := int64(3)
if !flush {
stable = 0
} // buffered data is visible only to uncommitted reads
if part.ErrorCode != want || part.HighWatermark != 3 || part.LastStableOffset != stable {
t.Fatalf("recovered offset=%d: %+v", offset, part)
}
if offset < 3 {
Expand Down
Loading
Loading