From bb46eeb58d31ef4b3c9456f19f0353b403553653 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Thu, 28 Oct 2021 13:07:30 +0700 Subject: [PATCH 1/3] fixes dmsg showing 00000 after successful reconnection --- pkg/visor/dmsgtracker/dmsg_tracker.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pkg/visor/dmsgtracker/dmsg_tracker.go b/pkg/visor/dmsgtracker/dmsg_tracker.go index 18fcdfdc3..d81325c9d 100644 --- a/pkg/visor/dmsgtracker/dmsg_tracker.go +++ b/pkg/visor/dmsgtracker/dmsg_tracker.go @@ -36,8 +36,8 @@ type DmsgTracker struct { ctrl *dmsgctrl.Control // dmsg ctrl } -// NewDmsgTracker creates a new DmsgTracker. -func NewDmsgTracker(ctx context.Context, dmsgC *dmsg.Client, pk cipher.PubKey) (dt *DmsgTracker, err error) { +// newDmsgTracker creates a new DmsgTracker. +func newDmsgTracker(ctx context.Context, dmsgC *dmsg.Client, pk cipher.PubKey) (dt *DmsgTracker, err error) { conn, err := dmsgC.DialStream(ctx, dmsg.Addr{PK: pk, Port: skyenv.DmsgCtrlPort}) if err != nil { return nil, err @@ -193,7 +193,7 @@ func (dtm *Manager) MustGet(ctx context.Context, pk cipher.PubKey) (DmsgClientSu return e.sum, nil } - dt, err := NewDmsgTracker(ctx, dtm.dc, pk) + dt, err := dtm.mustEstablishTracker(pk) if err != nil { return DmsgClientSummary{}, err } @@ -214,17 +214,29 @@ func (dtm *Manager) Get(pk cipher.PubKey) (DmsgClientSummary, bool) { return dtm.get(pk) } +// mustEstablishTracker creates / re-creates tracker when dmsgTrackerMap entry got deleted, and reconnected. +func (dtm *Manager) mustEstablishTracker(pk cipher.PubKey) (*DmsgTracker, error) { + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dtm.updateTimeout)) + defer cancel() + return newDmsgTracker(ctx, dtm.dc, pk) +} + // GetBulk obtains bulk dmsg client summaries. func (dtm *Manager) GetBulk(pks []cipher.PubKey) []DmsgClientSummary { dtm.mx.Lock() defer dtm.mx.Unlock() + var err error out := make([]DmsgClientSummary, 0, len(pks)) for _, pk := range pks { dt, ok := dtm.dm[pk] if !ok { - continue + dt, err = dtm.mustEstablishTracker(pk) + if err != nil { + dtm.log.WithError(err).Infoln("failed to re-create dmsgtracker client") + continue + } } out = append(out, dt.sum) } From 216b01e7cc6f8d1ed65e9b5fea898fdf03b6a22e Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Thu, 28 Oct 2021 13:27:00 +0700 Subject: [PATCH 2/3] fixes test --- pkg/visor/dmsgtracker/dmsg_tracker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/visor/dmsgtracker/dmsg_tracker_test.go b/pkg/visor/dmsgtracker/dmsg_tracker_test.go index 6dd5c2361..54f0f8fcc 100644 --- a/pkg/visor/dmsgtracker/dmsg_tracker_test.go +++ b/pkg/visor/dmsgtracker/dmsg_tracker_test.go @@ -38,7 +38,7 @@ func TestDmsgTracker_Update(t *testing.T) { // arrange: tracking client cT, err := env.NewClient(&conf) require.NoError(t, err) - dt, err := NewDmsgTracker(context.TODO(), cT, cL.LocalPK()) + dt, err := newDmsgTracker(context.TODO(), cT, cL.LocalPK()) require.NoError(t, err) // act: attempt update From 2f82f2f4ea18eaf65342f9942338916653e7c4c5 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Thu, 28 Oct 2021 13:40:33 +0700 Subject: [PATCH 3/3] remove redundant code --- pkg/visor/dmsgtracker/dmsg_tracker.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/visor/dmsgtracker/dmsg_tracker.go b/pkg/visor/dmsgtracker/dmsg_tracker.go index d81325c9d..78d004949 100644 --- a/pkg/visor/dmsgtracker/dmsg_tracker.go +++ b/pkg/visor/dmsgtracker/dmsg_tracker.go @@ -179,9 +179,6 @@ func (dtm *Manager) updateAllTrackers(ctx context.Context, dts map[cipher.PubKey // MustGet obtains a DmsgClientSummary of the client of given pk. // If one is not found internally, a new tracker stream is to be established, returning error on failure. func (dtm *Manager) MustGet(ctx context.Context, pk cipher.PubKey) (DmsgClientSummary, error) { - ctx, cancel := context.WithDeadline(ctx, time.Now().Add(dtm.updateTimeout)) - defer cancel() - dtm.mx.Lock() defer dtm.mx.Unlock()