Skip to content

Commit bdf9fbe

Browse files
Muhamad Azmyzaibon
andauthored
Use next id from client (#764)
* Use next id from client Co-authored-by: Christophe de Carvalho <[email protected]>
1 parent eeb2e06 commit bdf9fbe

File tree

7 files changed

+23
-42
lines changed

7 files changed

+23
-42
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/shirou/gopsutil v2.19.11+incompatible
3131
github.com/stretchr/testify v1.5.1
3232
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae
33-
github.com/threefoldtech/tfexplorer v0.2.7-0.20200429090008-f462f9a8d76c
33+
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505141628-4d7a0c749784
3434
github.com/threefoldtech/zbus v0.1.3
3535
github.com/urfave/cli v1.22.3
3636
github.com/vishvananda/netlink v1.0.0

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,10 @@ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG
557557
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae h1:vgGSvdW5Lqg+I1aZOlG32uyE6xHpLdKhZzcTEktz5wM=
558558
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae/go.mod h1:quDq6Se6jlGwiIKia/itDZxqC5rj6/8OdFyMMAwTxCs=
559559
github.com/threefoldtech/tfexplorer v0.2.5/go.mod h1:TugylEDgMNKk4ZIzee9PpVmIcLNLaRLdiXuhHTt/AR0=
560-
github.com/threefoldtech/tfexplorer v0.2.7-0.20200429090008-f462f9a8d76c h1:5tx6tw6P2uzQCCAAsjubA7p4ozKJXV0TMedIPCbGDgk=
561-
github.com/threefoldtech/tfexplorer v0.2.7-0.20200429090008-f462f9a8d76c/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
560+
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505125555-7d20b5212c0e h1:HqNiMBz0RpYg8RBDQl67soy2WPjAWHCBPzb7jl1Dtxc=
561+
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505125555-7d20b5212c0e/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
562+
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505141628-4d7a0c749784 h1:CCyZTGGV7BVVIGDduxLtebFsVHxJt6i2Z7rq6ostfmU=
563+
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505141628-4d7a0c749784/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
562564
github.com/threefoldtech/zbus v0.1.3 h1:18DnIzximRbATle5ZdZz0i84n/bCYB8k/gkhr2dXayc=
563565
github.com/threefoldtech/zbus v0.1.3/go.mod h1:ZtiRpcqzEBJetVQDsEbw0p48h/AF3O1kf0tvd30I0BU=
564566
github.com/threefoldtech/zos v0.2.4-rc2/go.mod h1:7A2oflcmSVsHFC4slOcydWgJyFBMFMH9wsaTRv+CnTA=

pkg/provision/explorer/source.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ func NewPoller(cl *client.Client, inputConv provision.ReservationConverterFunc,
3030
}
3131

3232
// Poll implements provision.ReservationPoller
33-
func (r *Poller) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservation, error) {
33+
func (r *Poller) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservation, uint64, error) {
3434

35-
list, err := r.wl.Workloads(nodeID.Identity(), from)
35+
list, lastID, err := r.wl.Workloads(nodeID.Identity(), from)
3636
if err != nil {
37-
return nil, fmt.Errorf("error while retrieving workloads from explorer: %w", err)
37+
return nil, 0, fmt.Errorf("error while retrieving workloads from explorer: %w", err)
3838
}
3939

4040
result := make([]*provision.Reservation, 0, len(list))
4141
for _, wl := range list {
4242
r, err := r.inputConv(wl)
4343
if err != nil {
44-
return nil, err
44+
return nil, 0, err
4545
}
4646

4747
result = append(result, r)
@@ -54,5 +54,5 @@ func (r *Poller) Poll(nodeID pkg.Identifier, from uint64) ([]*provision.Reservat
5454
})
5555
}
5656

57-
return result, nil
57+
return result, lastID, nil
5858
}

pkg/provision/primitives/converter.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ func WorkloadToProvisionType(w workloads.ReservationWorkload) (*provision.Reserv
256256
if err != nil {
257257
return nil, err
258258
}
259-
case nil:
260-
// no content, no conversion
261259
default:
262260
return nil, fmt.Errorf("unknown workload type (%s) (%T)", w.Type.String(), tmp)
263261
}

pkg/provision/reservation.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import (
1515
// ReservationType type
1616
type ReservationType string
1717

18-
const (
19-
//NOOPReservation type
20-
NOOPReservation ReservationType = "noop"
21-
)
22-
2318
// Reservation struct
2419
type Reservation struct {
2520
// ID of the reservation

pkg/provision/source.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type ReservationPoller interface {
2727
// from is the used as a filter to which reservation to use as
2828
// reservation.ID >= from. So a client to the Poll method should make
2929
// sure to call it with the last (MAX) reservation ID he receieved.
30-
Poll(nodeID pkg.Identifier, from uint64) ([]*Reservation, error)
30+
Poll(nodeID pkg.Identifier, from uint64) (reservations []*Reservation, lastID uint64, err error)
3131
}
3232

3333
// PollSource does a long poll on address to get new and to be deleted
@@ -50,7 +50,6 @@ type pollSource struct {
5050

5151
func (s *pollSource) Reservations(ctx context.Context) <-chan *Reservation {
5252
ch := make(chan *Reservation)
53-
5453
// on the first run we will get all the reservation
5554
// ever made to this know, to make sure we provision
5655
// everything at boot
@@ -65,39 +64,26 @@ func (s *pollSource) Reservations(ctx context.Context) <-chan *Reservation {
6564
on = time.Now().Add(s.maxSleep)
6665
log.Info().Uint64("next", next).Msg("Polling for reservations")
6766

68-
res, err := s.store.Poll(pkg.StrIdentifier(s.nodeID), next)
67+
res, lastID, err := s.store.Poll(pkg.StrIdentifier(s.nodeID), next)
6968
if err != nil && err != ErrPollEOS {
7069
// if this is not a temporary error, then skip the reservation entirely
7170
// and try to get the next one
7271
if shouldRetry(err) {
7372
log.Error().Err(err).Uint64("next", next).Msg("failed to get reservation, retry same")
7473
} else {
7574
log.Error().Err(err).Uint64("next", next).Msg("failed to get reservation")
76-
next++
75+
next = lastID + 1
7776
}
7877
continue
7978
}
8079

80+
next = lastID + 1
81+
8182
select {
8283
case <-ctx.Done():
8384
return
8485
default:
8586
for _, r := range res {
86-
current, _, err := r.SplitID()
87-
if err != nil {
88-
log.Warn().Err(err).Str("id", r.ID).Msg("skipping reservation")
89-
continue
90-
}
91-
if current >= next {
92-
next = current + 1
93-
}
94-
95-
if r.Type == NOOPReservation {
96-
// special type of reservation that does nothing
97-
// we just ignore it here.
98-
continue
99-
}
100-
10187
ch <- r
10288
}
10389
}

pkg/provision/source_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type TestPollSource struct {
1515
mock.Mock
1616
}
1717

18-
func (s *TestPollSource) Poll(nodeID pkg.Identifier, from uint64) ([]*Reservation, error) {
18+
func (s *TestPollSource) Poll(nodeID pkg.Identifier, from uint64) ([]*Reservation, uint64, error) {
1919
returns := s.Called(nodeID, from)
20-
return returns.Get(0).([]*Reservation), returns.Error(1)
20+
return returns.Get(0).([]*Reservation), uint64(returns.Get(1).(int)), returns.Error(2)
2121
}
2222

2323
func TestHTTPReservationSource(t *testing.T) {
@@ -32,7 +32,7 @@ func TestHTTPReservationSource(t *testing.T) {
3232
Return([]*Reservation{
3333
&Reservation{ID: "1-1"},
3434
&Reservation{ID: "1-2"},
35-
}, ErrPollEOS)
35+
}, 1, ErrPollEOS)
3636

3737
reservations := []*Reservation{}
3838
for res := range chn {
@@ -60,13 +60,13 @@ func TestHTTPReservationSourceMultiple(t *testing.T) {
6060
Return([]*Reservation{
6161
&Reservation{ID: "1-1"},
6262
&Reservation{ID: "2-1"},
63-
}, nil) // return nil error so it tries again
63+
}, 2, nil) // return nil error so it tries again
6464

6565
store.On("Poll", nodeID, uint64(3)).
6666
Return([]*Reservation{
6767
&Reservation{ID: "3-1"},
6868
&Reservation{ID: "4-1"},
69-
}, ErrPollEOS)
69+
}, 6, ErrPollEOS)
7070

7171
reservations := []*Reservation{}
7272
for res := range chn {
@@ -86,9 +86,9 @@ type TestTrackSource struct {
8686
Calls []int64
8787
}
8888

89-
func (s *TestTrackSource) Poll(nodeID pkg.Identifier, from uint64) ([]*Reservation, error) {
89+
func (s *TestTrackSource) Poll(nodeID pkg.Identifier, from uint64) ([]*Reservation, uint64, error) {
9090
if s.ID == s.Max {
91-
return nil, ErrPollEOS
91+
return nil, 0, ErrPollEOS
9292
}
9393

9494
s.Calls = append(s.Calls, time.Now().Unix())
@@ -101,7 +101,7 @@ func (s *TestTrackSource) Poll(nodeID pkg.Identifier, from uint64) ([]*Reservati
101101
&Reservation{
102102
ID: fmt.Sprint(s.ID, "-", "0"),
103103
},
104-
}, nil
104+
}, s.ID, nil
105105
}
106106

107107
func TestHTTPReservationSourceSleep(t *testing.T) {

0 commit comments

Comments
 (0)