Tool runner bus: long-lived loops read their settings once, as they start
A hearing's loops, SeatTake's heartbeat and followGrants' retry loop outlive the call that started them and read the package's settings on every turn; a test that sets a setting back as it ends then raced the loop (the race detector: streams_test.go:232's cleanup write of RebindEvery against the rebind loop's read, in TestAConsumerNamedBeforeItExistsIsBoundOnceItDoes at -count=30). Each loop now takes RebindEvery, NakDelay, EventHeartbeat, WorkHeartbeat, GrantsRetry, GrantsRetryMost, GrantsPatience and StateTimeout once, as consumes already took IdleHeartbeat and its tick hook. Builds: design 47 H H-A-363 (fix-a-bug-met-twice: a goroutine outliving its settings)
This commit is contained in:
@@ -427,29 +427,33 @@ func (c *Conn) issue(module string, m Membership) {
|
|||||||
// right after its boot, a timeout), is said once and read again, with a growing wait, until it can be or the
|
// right after its boot, a timeout), is said once and read again, with a growing wait, until it can be or the
|
||||||
// connection closes: Follow runs once per module, so nothing else would ever read it again.
|
// connection closes: Follow runs once per module, so nothing else would ever read it again.
|
||||||
func (c *Conn) followGrants(module string) {
|
func (c *Conn) followGrants(module string) {
|
||||||
if c.watchGrants(module, true) {
|
// Read here, not in the loop: the loop outlives Follow, and a test sets them back as it ends (design 47 H, H-A-363).
|
||||||
|
patience, timeout := GrantsPatience, StateTimeout
|
||||||
|
if c.watchGrants(module, true, patience, timeout) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
wait, most := GrantsRetry, GrantsRetryMost
|
||||||
go func() {
|
go func() {
|
||||||
wait := GrantsRetry
|
|
||||||
for !c.nc.IsClosed() {
|
for !c.nc.IsClosed() {
|
||||||
time.Sleep(wait)
|
time.Sleep(wait)
|
||||||
if c.nc.IsClosed() {
|
if c.nc.IsClosed() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c.watchGrants(module, false) {
|
if c.watchGrants(module, false, patience, timeout) {
|
||||||
c.Logf("[mesh-tools] the grants part's memberships are read again; %s on %s follows its key", module, c.node)
|
c.Logf("[mesh-tools] the grants part's memberships are read again; %s on %s follows its key", module, c.node)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if wait *= 2; wait > GrantsRetryMost {
|
if wait *= 2; wait > most {
|
||||||
wait = GrantsRetryMost
|
wait = most
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// watchGrants is one attempt of followGrants: true once the key is watched. A failure is said when say is true.
|
// watchGrants is one attempt of followGrants: true once the key is watched. A failure is said when say is true. Its
|
||||||
func (c *Conn) watchGrants(module string, say bool) bool {
|
// bounds, patience for the key's direct get and timeout for the bucket and its current value, are GrantsPatience and
|
||||||
|
// StateTimeout as they stood when followGrants began.
|
||||||
|
func (c *Conn) watchGrants(module string, say bool, patience, timeout time.Duration) bool {
|
||||||
logf := func(format string, a ...any) {
|
logf := func(format string, a ...any) {
|
||||||
if say {
|
if say {
|
||||||
c.Logf(format, a...)
|
c.Logf(format, a...)
|
||||||
@@ -459,7 +463,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
|
|||||||
// First the key's direct get, which the account is granted alone: a bus that refuses it, or a state not there yet,
|
// First the key's direct get, which the account is granted alone: a bus that refuses it, or a state not there yet,
|
||||||
// answers at once, where binding the bucket would wait out its bound on a refusal.
|
// answers at once, where binding the bucket would wait out its bound on a refusal.
|
||||||
bucket := membership.GrantsBucket
|
bucket := membership.GrantsBucket
|
||||||
probe, err := c.nc.Request("$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+key, nil, GrantsPatience)
|
probe, err := c.nc.Request("$JS.API.DIRECT.GET.KV_"+bucket+".$KV."+bucket+"."+key, nil, patience)
|
||||||
if err != nil || probe.Header.Get("Status") == "503" {
|
if err != nil || probe.Header.Get("Status") == "503" {
|
||||||
why := "it did not answer"
|
why := "it did not answer"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -473,7 +477,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), StateTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
kv, err := js.KeyValue(ctx, bucket)
|
kv, err := js.KeyValue(ctx, bucket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -506,7 +510,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
|
|||||||
}
|
}
|
||||||
c.issue(module, m)
|
c.issue(module, m)
|
||||||
}
|
}
|
||||||
timer := time.NewTimer(StateTimeout)
|
timer := time.NewTimer(timeout)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
for current := true; current; {
|
for current := true; current; {
|
||||||
select {
|
select {
|
||||||
@@ -522,7 +526,7 @@ func (c *Conn) watchGrants(module string, say bool) bool {
|
|||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
_ = w.Stop()
|
_ = w.Stop()
|
||||||
logf("[mesh-tools] %s's key %s of the grants part's memberships did not answer in %s; %s has no membership "+
|
logf("[mesh-tools] %s's key %s of the grants part's memberships did not answer in %s; %s has no membership "+
|
||||||
"until it can be", module, key, StateTimeout, module)
|
"until it can be", module, key, timeout, module)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -279,6 +279,8 @@ func (c *Conn) SeatTake(module, consumer string, deliver func(Work) error) (func
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("binding %s's worker %s on %s: %w", module, w.Consumer, w.Stream, err)
|
return nil, fmt.Errorf("binding %s's worker %s on %s: %w", module, w.Consumer, w.Stream, err)
|
||||||
}
|
}
|
||||||
|
// Read once, as the take starts: its loop outlives SeatTake, and a test sets it back as it ends (design 47 H, H-A-363).
|
||||||
|
heartbeat := WorkHeartbeat
|
||||||
reading, err := bound.Consume(func(msg jetstream.Msg) {
|
reading, err := bound.Consume(func(msg jetstream.Msg) {
|
||||||
if !json.Valid(msg.Data()) {
|
if !json.Valid(msg.Data()) {
|
||||||
_ = msg.Term()
|
_ = msg.Term()
|
||||||
@@ -292,7 +294,7 @@ func (c *Conn) SeatTake(module, consumer string, deliver func(Work) error) (func
|
|||||||
// that died, not one that is slow.
|
// that died, not one that is slow.
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
tick := time.NewTicker(WorkHeartbeat)
|
tick := time.NewTicker(heartbeat)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|||||||
@@ -106,12 +106,18 @@ type hearing struct {
|
|||||||
kick chan struct{}
|
kick chan struct{}
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
unhear func()
|
unhear func()
|
||||||
|
|
||||||
|
// rebind, nakDelay and heartbeat are RebindEvery, NakDelay and EventHeartbeat as they stood when the hearing was
|
||||||
|
// made: its loops outlive the call that started them, so they never read a setting again, which a test may set
|
||||||
|
// back as it ends (design 47 H, H-A-363: a goroutine outliving its settings).
|
||||||
|
rebind, nakDelay, heartbeat time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHearing(c *Conn, js jetstream.JetStream, module string, deliver func(Envelope) error) *hearing {
|
func newHearing(c *Conn, js jetstream.JetStream, module string, deliver func(Envelope) error) *hearing {
|
||||||
return &hearing{c: c, js: js, module: module, deliver: deliver,
|
return &hearing{c: c, js: js, module: module, deliver: deliver,
|
||||||
bound: map[HeardIssued]jetstream.ConsumeContext{}, made: map[HeardIssued]time.Time{}, said: map[HeardIssued]bool{}, why: map[HeardIssued]error{},
|
bound: map[HeardIssued]jetstream.ConsumeContext{}, made: map[HeardIssued]time.Time{}, said: map[HeardIssued]bool{}, why: map[HeardIssued]error{},
|
||||||
kick: make(chan struct{}, 1), done: make(chan struct{})}
|
kick: make(chan struct{}, 1), done: make(chan struct{}),
|
||||||
|
rebind: RebindEvery, nakDelay: NakDelay, heartbeat: EventHeartbeat}
|
||||||
}
|
}
|
||||||
|
|
||||||
// bind reads one durable consumer by name, and on failure says what it was doing. Errors the bus gives
|
// bind reads one durable consumer by name, and on failure says what it was doing. Errors the bus gives
|
||||||
@@ -158,7 +164,7 @@ func (h *hearing) handle(msg jetstream.Msg) {
|
|||||||
done, stopped := make(chan struct{}), make(chan struct{})
|
done, stopped := make(chan struct{}), make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(stopped)
|
defer close(stopped)
|
||||||
tick := time.NewTicker(EventHeartbeat)
|
tick := time.NewTicker(h.heartbeat)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -186,7 +192,7 @@ func (h *hearing) handle(msg jetstream.Msg) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = msg.NakWithDelay(NakDelay)
|
_ = msg.NakWithDelay(h.nakDelay)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = msg.Ack()
|
_ = msg.Ack()
|
||||||
@@ -203,7 +209,7 @@ func (h *hearing) start() {
|
|||||||
h.nudge()
|
h.nudge()
|
||||||
})
|
})
|
||||||
go func() {
|
go func() {
|
||||||
tick := time.NewTicker(RebindEvery)
|
tick := time.NewTicker(h.rebind)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -353,7 +359,7 @@ func (h *hearing) reconcile() {
|
|||||||
// Said once, not at every attempt; said again when it is bound.
|
// Said once, not at every attempt; said again when it is bound.
|
||||||
if !h.said[heard] {
|
if !h.said[heard] {
|
||||||
h.c.Logf("[mesh-tools] %s does not hear %s yet: %s its consumer %s failed: %v; tried again every %s",
|
h.c.Logf("[mesh-tools] %s does not hear %s yet: %s its consumer %s failed: %v; tried again every %s",
|
||||||
h.module, heard.Stream, doing, heard.Consumer, err, RebindEvery)
|
h.module, heard.Stream, doing, heard.Consumer, err, h.rebind)
|
||||||
h.said[heard] = true
|
h.said[heard] = true
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user