ZenQ icon indicating copy to clipboard operation
ZenQ copied to clipboard

How I get the sequence number from a ring ZenQ

Open aslupin opened this issue 2 years ago • 5 comments

Hello. I have more questions from this https://github.com/alphadose/ZenQ/issues/10. Assume I created three or more ZenQ 3 rings/instances and want to balance the size of ZenQ when the producer delivers a message by selecting the ring with the lowest sequence. Could you suggest to me how I can retrieve the length of a ring or the most recent sequence from ZenQ?

FYI. I'm new to LMAX Disruptor. Please excuse any misunderstanding.

aslupin avatar Jan 04 '24 04:01 aslupin

you can get the sequence number from the writerIndex variable in the ZenQ struct https://github.com/alphadose/ZenQ/blob/main/zenq.go#L87

var sequenceNumber uint32 = ZenQ.writerIndex.Load()

alphadose avatar Jan 04 '24 05:01 alphadose

I got it but this writerIndex variable didn't export to use in an external package. Could you provide a method to return this, maybe something like Length() or SequenceNumber()?

aslupin avatar Jan 04 '24 08:01 aslupin

@aslupin I am a bit occupied currently, hence the patch with Length() function will take some time

In the meantime you can use

package main

import (
	"fmt"
	"reflect"
	"strings"

	"github.com/alphadose/zenq/v2"
)

type payload struct {
	alpha int
	beta  string
}

func main() {
	zq := zenq.New[payload](100)
	for i := 0; i < 100; i++ {
		zq.Write(payload{1, "2"})
	}
	k := strings.Split(fmt.Sprintf("%v", reflect.ValueOf(*zq).FieldByName("writerIndex")), " ")
	fmt.Println(strings.Trim(k[len(k)-1], "}"))
}

output

100

alphadose avatar Jan 04 '24 09:01 alphadose

@aslupin I have added Size() function in https://github.com/alphadose/ZenQ/releases/tag/v2.8.3

here is the commit https://github.com/alphadose/ZenQ/commit/43a1241a8670b32266ece6404f6c2b7785bc6b9e

alphadose avatar Jan 19 '24 05:01 alphadose

Cool!, Thank you for your help

aslupin avatar Jan 21 '24 06:01 aslupin