Skip to content

feat(go): implement binary reader/writer#2986

Merged
hubcio merged 17 commits intoapache:masterfrom
chengxilo:refactor/go-binary-reader
Mar 23, 2026
Merged

feat(go): implement binary reader/writer#2986
hubcio merged 17 commits intoapache:masterfrom
chengxilo:refactor/go-binary-reader

Conversation

@chengxilo
Copy link
Contributor

@chengxilo chengxilo commented Mar 20, 2026

Which issue does this PR close?

N/A

Rationale

Iggy, when read/write binary, use little endian. When there is a string, it has a prefix provide its length, and the actually string body. So instead of using the binary.LittleEndian.Uint32(payload[processIDPos : processIDPos+4]) and use position ++ , why not just wrap everything in a reader/writer to make it simple and clear.

As the Stats deserialize function need to be updated, I think this change would be helpful. #2964 (comment)

Here is a example that shows the difference:
Before:

func (stats *TcpStats) Deserialize(payload []byte) error {
stats.ProcessId = binary.LittleEndian.Uint32(payload[processIDPos : processIDPos+4])
stats.CpuUsage = math.Float32frombits(binary.LittleEndian.Uint32(payload[cpuUsagePos : cpuUsagePos+4]))
stats.TotalCpuUsage = math.Float32frombits(binary.LittleEndian.Uint32(payload[totalCpuUsagePos : totalCpuUsagePos+4]))
stats.MemoryUsage = binary.LittleEndian.Uint64(payload[memoryUsagePos : memoryUsagePos+8])
stats.TotalMemory = binary.LittleEndian.Uint64(payload[totalMemoryPos : totalMemoryPos+8])
stats.AvailableMemory = binary.LittleEndian.Uint64(payload[availableMemoryPos : availableMemoryPos+8])
stats.RunTime = binary.LittleEndian.Uint64(payload[runTimePos : runTimePos+8])
stats.StartTime = binary.LittleEndian.Uint64(payload[startTimePos : startTimePos+8])
stats.ReadBytes = binary.LittleEndian.Uint64(payload[readBytesPos : readBytesPos+8])
stats.WrittenBytes = binary.LittleEndian.Uint64(payload[writtenBytesPos : writtenBytesPos+8])
stats.MessagesSizeBytes = binary.LittleEndian.Uint64(payload[messagesSizeBytesPos : messagesSizeBytesPos+8])
stats.StreamsCount = binary.LittleEndian.Uint32(payload[streamsCountPos : streamsCountPos+4])
stats.TopicsCount = binary.LittleEndian.Uint32(payload[topicsCountPos : topicsCountPos+4])
stats.PartitionsCount = binary.LittleEndian.Uint32(payload[partitionsCountPos : partitionsCountPos+4])
stats.SegmentsCount = binary.LittleEndian.Uint32(payload[segmentsCountPos : segmentsCountPos+4])
stats.MessagesCount = binary.LittleEndian.Uint64(payload[messagesCountPos : messagesCountPos+8])
stats.ClientsCount = binary.LittleEndian.Uint32(payload[clientsCountPos : clientsCountPos+4])
stats.ConsumerGroupsCount = binary.LittleEndian.Uint32(payload[consumerGroupsCountPos : consumerGroupsCountPos+4])
position := consumerGroupsCountPos + 4
hostnameLength := int(binary.LittleEndian.Uint32(payload[position : position+4]))
stats.Hostname = string(payload[position+4 : position+4+hostnameLength])
position += 4 + hostnameLength
osNameLength := int(binary.LittleEndian.Uint32(payload[position : position+4]))
stats.OsName = string(payload[position+4 : position+4+osNameLength])
position += 4 + osNameLength
osVersionLength := int(binary.LittleEndian.Uint32(payload[position : position+4]))
stats.OsVersion = string(payload[position+4 : position+4+osVersionLength])
position += 4 + osVersionLength
kernelVersionLength := int(binary.LittleEndian.Uint32(payload[position : position+4]))
stats.KernelVersion = string(payload[position+4 : position+4+kernelVersionLength])
return nil
}

After

func (s *Stats) UnmarshalBinary(payload []byte) error {
	r := codec.newReader(payload)
	s.ProcessId = r.u32()
	s.CpuUsage = r.f32()
	s.TotalCpuUsage = r.f32()
	s.MemoryUsage = r.u64()
	s.TotalMemory = r.u64()
	s.AvailableMemory = r.u64()
	s.RunTime = r.u64()
	s.StartTime = r.u64()
	s.ReadBytes = r.u64()
	s.WrittenBytes = r.u64()
	s.MessagesSizeBytes = r.u64()
	s.StreamsCount = r.u32()
	s.TopicsCount = r.u32()
	s.PartitionsCount = r.u32()
	s.SegmentsCount = r.u32()
	s.MessagesCount = r.u64()
	s.ClientsCount = r.u32()
	s.ConsumerGroupsCount = r.u32()
	s.Hostname = r.u32LenStr()
	s.OsName = r.u32LenStr()
	s.OsVersion = r.u32LenStr()
	s.KernelVersion = r.u32LenStr()
	return r.Err()
}

Example of writer

func buildStatsPayload(
	processId uint32,
	cpuUsage, totalCpuUsage float32,
	memoryUsage, totalMemory, availableMemory uint64,
	runTime, startTime uint64,
	readBytes, writtenBytes, messagesSizeBytes uint64,
	streamsCount, topicsCount, partitionsCount, segmentsCount uint32,
	messagesCount uint64,
	clientsCount, consumerGroupsCount uint32,
	hostname, osName, osVersion, kernelVersion, iggyServerVersion string,
	iggyServerSemver *uint32,
	cacheMetrics []CacheMetrics,
	threadsCount uint32,
	freeDiskSpace, totalDiskSpace uint64,
) []byte {
	w := codec.NewWriter()
	w.U32(processId)
	w.F32(cpuUsage)
	w.F32(totalCpuUsage)
	w.U64(memoryUsage)
	w.U64(totalMemory)
	w.U64(availableMemory)
	w.U64(runTime)
	w.U64(startTime)
	w.U64(readBytes)
	w.U64(writtenBytes)
	w.U64(messagesSizeBytes)
	w.U32(streamsCount)
	w.U32(topicsCount)
	w.U32(partitionsCount)
	w.U32(segmentsCount)
	w.U64(messagesCount)
	w.U32(clientsCount)
	w.U32(consumerGroupsCount)
	w.U32LenStr(hostname)
	w.U32LenStr(osName)
	w.U32LenStr(osVersion)
	w.U32LenStr(kernelVersion)
	w.U32LenStr(iggyServerVersion)

	if iggyServerSemver != nil {
		w.U32(*iggyServerSemver)
	}

	w.U32(uint32(len(cacheMetrics)))
	for _, cm := range cacheMetrics {
		w.U32(cm.StreamId)
		w.U32(cm.TopicId)
		w.U32(cm.PartitionId)
		w.U64(cm.Hits)
		w.U64(cm.Misses)
		w.F32(cm.HitRatio)
	}

	if iggyServerSemver != nil {
		w.U32(threadsCount)
		w.U64(freeDiskSpace)
		w.U64(totalDiskSpace)
	}

	return w.Bytes()
}

What changed?

A reader for byte slice is created.

Local Execution

  • Passed
  • Pre-commit hooks ran

AI Usage

  1. smart Claude Sonnet 4.6 and my rotten brain
  2. entire implementation, manually modified some part to make sure the error output readablity.
  3. Reviewed the tests and code, actually modified some logic of reading binary and works well.
  4. yes

@codecov
Copy link

codecov bot commented Mar 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.89%. Comparing base (707ab24) to head (9239889).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2986      +/-   ##
============================================
- Coverage     71.98%   71.89%   -0.09%     
  Complexity      930      930              
============================================
  Files          1122     1120       -2     
  Lines         93496    93030     -466     
  Branches      71019    70377     -642     
============================================
- Hits          67303    66888     -415     
+ Misses        23623    23582      -41     
+ Partials       2570     2560      -10     
Flag Coverage Δ
go 38.68% <100.00%> (+2.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
foreign/go/internal/codec/reader.go 100.00% <100.00%> (ø)
foreign/go/internal/codec/writer.go 100.00% <100.00%> (ø)

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chengxilo chengxilo requested a review from ryankert01 March 20, 2026 17:21
@chengxilo chengxilo marked this pull request as draft March 21, 2026 04:44
@chengxilo chengxilo changed the title feat(go): implement a binary reader feat(go): implement binary reader/writer Mar 21, 2026
@chengxilo chengxilo marked this pull request as ready for review March 21, 2026 06:17
@chengxilo chengxilo marked this pull request as draft March 21, 2026 18:58
@chengxilo chengxilo force-pushed the refactor/go-binary-reader branch 2 times, most recently from 47e3eb7 to ea27dc3 Compare March 22, 2026 16:55
@chengxilo chengxilo force-pushed the refactor/go-binary-reader branch 2 times, most recently from b052dbc to 4b454b5 Compare March 22, 2026 17:59
@chengxilo chengxilo force-pushed the refactor/go-binary-reader branch from 4b454b5 to 669233f Compare March 22, 2026 18:01
@chengxilo chengxilo marked this pull request as ready for review March 22, 2026 18:25
@chengxilo chengxilo requested a review from hubcio March 22, 2026 19:07
Copy link
Member

@ryankert01 ryankert01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg

@hubcio hubcio merged commit 6180d88 into apache:master Mar 23, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants