@@ -12,6 +12,7 @@ import (
1212 "github.com/NVIDIA/aistore/api/apc"
1313 "github.com/NVIDIA/aistore/cmn"
1414 "github.com/NVIDIA/aistore/cmn/atomic"
15+ "github.com/NVIDIA/aistore/cmn/debug"
1516 "github.com/NVIDIA/aistore/cmn/mono"
1617 "github.com/NVIDIA/aistore/cmn/nlog"
1718 "github.com/NVIDIA/aistore/core/meta"
@@ -21,17 +22,18 @@ import (
2122// auto-deactivates after a timeout;
2223// negative timeout (see `tout` below) => never deactivate
2324
24- // [TODO] shared-DM:
25- // - apc.HdrActiveDM
26- // - dmToggle.on()
27- // - extra checks on then target side (unregIf, etc.)
25+ // non-primary action
26+ const (
27+ nonpBit = uint64 (1 ) << 63
28+ nonpMask = ^ nonpBit
29+ )
2830
2931type (
3032 streamsToggle struct {
31- hdrActive string // apc.HdrActiveEC, ...
32- actOn string // apc.ActOpenEC, ...
33- actOff string // apc.ActCloseEC, ...
34- last atomic.Int64 // mono-time of last positive refresh
33+ hdrActive string // apc.HdrActiveEC, ...
34+ actOn string // apc.ActOpenEC, ...
35+ actOff string // apc.ActCloseEC, ...
36+ last atomic.Uint64 // mono-time of last positive refresh
3537 }
3638
3739 ecToggle struct {
@@ -56,10 +58,34 @@ func (f *dmToggle) init() {
5658 f .actOff = apc .ActCloseSDM
5759}
5860
59- func (* dmToggle ) timeout () time.Duration { return cmn .SharedStreamsDflt }
61+ func (* dmToggle ) timeout () time.Duration { return cmn .SharedStreamsDflt } // see also: sharedDM.getActive
6062
6163func (f * streamsToggle ) isActive (h http.Header ) bool { _ , ok := h [f .hdrActive ]; return ok }
62- func (f * streamsToggle ) setActive (now int64 ) { f .last .Store (now ) }
64+
65+ func (f * streamsToggle ) setActive (now int64 ) {
66+ f .last .Store (uint64 (now ))
67+ }
68+
69+ // non-primary: executed user call requiring SDM
70+ func (f * streamsToggle ) nonpSetActive (now int64 ) {
71+ debug .Assert (now > 0 , "invalid timestamp: " , now )
72+ f .last .Store ((uint64 (now ) & nonpMask ) | nonpBit )
73+ }
74+
75+ // non-primary: fast-keepalive => primary
76+ func (f * streamsToggle ) nonpResetActive () (uint64 , bool ) {
77+ last := f .last .Load ()
78+ if last & nonpBit == 0 {
79+ return last , false
80+ }
81+ last = f .last .Swap (0 )
82+ return last , true
83+ }
84+
85+ // non-primary: fast-keepalive => primary
86+ func (f * streamsToggle ) nonpUndo (last uint64 ) {
87+ _ = f .last .CAS (0 , last )
88+ }
6389
6490// target => primary keep-alive
6591func (f * streamsToggle ) recvKalive (p * proxy , hdr http.Header , now int64 , tout time.Duration ) {
@@ -70,7 +96,7 @@ func (f *streamsToggle) recvKalive(p *proxy, hdr http.Header, now int64, tout ti
7096 if tout < 0 {
7197 return
7298 }
73- last := f .last .Load ()
99+ last := int64 ( f .last .Load () & nonpMask )
74100 if last == 0 || time .Duration (now - last ) < tout {
75101 return
76102 }
@@ -80,7 +106,10 @@ func (f *streamsToggle) recvKalive(p *proxy, hdr http.Header, now int64, tout ti
80106// primary => target keep-alive
81107func (f * streamsToggle ) respKalive (hdr http.Header , now int64 , tout time.Duration ) {
82108 if tout > 0 {
83- if last := f .last .Load (); last != 0 && time .Duration (now - last ) < tout {
109+ v := f .last .Load ()
110+ last := int64 (v & nonpMask )
111+ debug .Assert (int64 (v ) >= 0 , f .hdrActive , " invalid timestamp: " , v )
112+ if last != 0 && time .Duration (now - last ) < tout {
84113 hdr .Set (f .hdrActive , "true" )
85114 }
86115 }
@@ -96,7 +125,7 @@ func (f *streamsToggle) on(p *proxy, tout time.Duration) error {
96125 }
97126 var (
98127 now = mono .NanoTime ()
99- last = f .last .Load ()
128+ last = int64 ( f .last .Load () & nonpMask )
100129 )
101130 if last != 0 && time .Duration (now - last ) < cmn .SharedStreamsNack {
102131 return nil
@@ -109,7 +138,8 @@ func (f *streamsToggle) on(p *proxy, tout time.Duration) error {
109138}
110139
111140func (f * streamsToggle ) off (p * proxy , last int64 ) {
112- if ! f .last .CAS (last , 0 ) {
141+ debug .Assert (last > 0 , f .hdrActive , " invalid timestamp: " , last )
142+ if ! f .last .CAS (uint64 (last ), 0 ) {
113143 return
114144 }
115145
0 commit comments