Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement delete devices #299

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ func NewClient(
opt = append(opt, core.WithErr(errors))
}
oc := core.NewClient(opt...)
pollInterval := time.Second * 10
if cacheExpiration/2 > pollInterval {
pollInterval = cacheExpiration / 2
}
client := Client{
client: oc,
app: app,
deviceCache: NewDeviceCache(cacheExpiration, time.Minute, errors),
deviceCache: NewDeviceCache(cacheExpiration, pollInterval, errors),
observeResourceCache: kitSync.NewMap(),
deviceOwner: deviceOwner,
subscriptions: make(map[string]subscription),
Expand Down
17 changes: 0 additions & 17 deletions client/deleteDevice.go

This file was deleted.

28 changes: 28 additions & 0 deletions client/deleteDevices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

import (
"context"
"fmt"
)

func (c *Client) DeleteDevice(ctx context.Context, deviceID string) bool {
devs := c.DeleteDevices(ctx, []string{deviceID})
return len(devs) > 0
}

// DeleteDevices deletes a device from the cache. If deviceIDFilter is empty, all devices are deleted.
func (c *Client) DeleteDevices(ctx context.Context, deviceIDFilter []string) []string {
devs := c.deviceCache.LoadAndDeleteDevices(deviceIDFilter)
if len(devs) == 0 {
return nil
}
deviceIDs := make([]string, 0, len(devs))
for _, d := range devs {
deviceIDs = append(deviceIDs, d.DeviceID())
err := d.Close(ctx)
if err != nil {
c.errors(fmt.Errorf("can't close device %v during deleting device from the cache: %w", d.DeviceID(), err))
}
}
return deviceIDs
}
29 changes: 9 additions & 20 deletions client/deleteDevice_test.go → client/deleteDevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,36 +191,32 @@ func TestClientDeleteDevice(t *testing.T) {
deviceID string
}
tests := []struct {
name string
args args
want bool
wantErr bool
name string
args args
want bool
}{
{
name: "not found",
args: args{
addDevice: nil,
deviceID: "not-found",
},
want: false,
wantErr: false,
want: false,
},
{
name: "found",
args: args{
addDevice: addDirectDeviceToCache,
deviceID: "found",
},
want: true,
wantErr: false,
want: true,
},
{
name: "try to delete twice",
args: args{
deviceID: "found",
},
want: false,
wantErr: false,
want: false,
},
{
name: "delete device with resource observation",
Expand Down Expand Up @@ -250,8 +246,7 @@ func TestClientDeleteDevice(t *testing.T) {
checkForSkip: func(ctx context.Context, t *testing.T, c *Client, deviceID string) {
_, links, err := c.GetDevice(ctx, deviceID)
require.NoError(t, err)
ok, err := c.DeleteDevice(ctx, deviceID)
require.NoError(t, err)
ok := c.DeleteDevice(ctx, deviceID)
require.True(t, ok)

res := links.GetResourceLinks(resources.ResourceType)
Expand Down Expand Up @@ -333,8 +328,7 @@ func TestClientDeleteDevice(t *testing.T) {
err := c.DisownDevice(ctx, deviceID)
require.NoError(t, err)
}()
ok, err := c.DeleteDevice(ctx, deviceID)
require.NoError(t, err)
ok := c.DeleteDevice(ctx, deviceID)
require.True(t, ok)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -347,12 +341,7 @@ func TestClientDeleteDevice(t *testing.T) {
if tt.args.addDevice != nil {
testCtx = tt.args.addDevice(ctx, t, c, tt.args.deviceID)
}
got, err := c.DeleteDevice(testCtx, tt.args.deviceID)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
got := c.DeleteDevice(testCtx, tt.args.deviceID)
require.Equal(t, tt.want, got)
if tt.args.cleanUp != nil {
tt.args.cleanUp(testCtx, t, c, tt.args.deviceID)
Expand Down
31 changes: 25 additions & 6 deletions client/deviceCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ func NewDeviceCache(deviceExpiration, pollInterval time.Duration, errors func(er
}

// This function loads the device from the cache and deletes it from the cache. To cleanup the device you have to call device.Close.
func (c *DeviceCache) LoadAndDeleteDevice(ctx context.Context, deviceID string) (*core.Device, bool) {
d := c.devicesCache.Load(deviceID)
if d == nil {
func (c *DeviceCache) LoadAndDeleteDevice(deviceID string) (*core.Device, bool) {
devs := c.LoadAndDeleteDevices([]string{deviceID})
if len(devs) == 0 {
return nil, false
}
dev := d.Data().(*core.Device)
c.devicesCache.Delete(deviceID)
return dev, true
return devs[0], true
}

func (c *DeviceCache) GetDevice(deviceID string) (*core.Device, bool) {
Expand Down Expand Up @@ -184,6 +182,27 @@ func (c *DeviceCache) GetDevicesFoundByIP() map[string]string {
return devices
}

func (c *DeviceCache) LoadAndDeleteDevices(deviceIDFilter []string) []*core.Device {
devices := make([]*core.Device, 0, len(deviceIDFilter))
if len(deviceIDFilter) == 0 {
for _, val := range c.devicesCache.PullOutAll() {
d := val.(*core.Device)
devices = append(devices, d)
}
return devices
}
for _, deviceID := range deviceIDFilter {
lubo-svk marked this conversation as resolved.
Show resolved Hide resolved
val := c.devicesCache.Load(deviceID)
if val == nil {
continue
}
c.devicesCache.Delete(deviceID)
d := val.Data().(*core.Device)
devices = append(devices, d)
}
return devices
}

func (c *DeviceCache) Close(ctx context.Context) error {
var errors []error
if c.closed.CompareAndSwap(false, true) {
Expand Down
6 changes: 3 additions & 3 deletions client/deviceCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func TestDeviceCacheContentHandling(t *testing.T) {
require.True(t, found)
require.False(t, expiration.IsZero())

dev, removed := cache.LoadAndDeleteDevice(context.TODO(), device2ID)
dev, removed := cache.LoadAndDeleteDevice(device2ID)
require.True(t, removed)
err := dev.Close(context.TODO())
require.NoError(t, err)
dev, removed = cache.LoadAndDeleteDevice(context.TODO(), device2ID)
dev, removed = cache.LoadAndDeleteDevice(device2ID)
require.False(t, removed)
require.Nil(t, dev)

Expand All @@ -77,7 +77,7 @@ func TestDeviceCacheContentHandling(t *testing.T) {
require.True(t, found)
require.False(t, expiration.IsZero())

dev, removed = cache.LoadAndDeleteDevice(context.TODO(), device1ID)
dev, removed = cache.LoadAndDeleteDevice(device1ID)
require.True(t, removed)
err = dev.Close(context.TODO())
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion client/disownDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *Client) DisownDevice(ctx context.Context, deviceID string, opts ...Comm
return err
}
defer func() {
dev, ok := c.deviceCache.LoadAndDeleteDevice(ctx, d.DeviceID())
dev, ok := c.deviceCache.LoadAndDeleteDevice(d.DeviceID())
if ok {
dev.Close(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion client/getDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func getLinksDevice(ctx context.Context, dev *core.Device, disableUDPEndpoints b
// come back online
func deleteDeviceNotFoundByIP(ctx context.Context, deviceCache *DeviceCache, dev *core.Device) {
if dev.FoundByIP() == "" {
deviceCache.LoadAndDeleteDevice(ctx, dev.DeviceID())
deviceCache.LoadAndDeleteDevice(dev.DeviceID())
}
dev.Close(ctx)
}
Expand Down
6 changes: 2 additions & 4 deletions client/getDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ func TestClientGetDeviceByIP(t *testing.T) {
require.NotEmpty(t, got.Details.(*device.Device).ProtocolIndependentID)
got.Details.(*device.Device).ProtocolIndependentID = ""
require.Equal(t, tt.want, got)
ok, err := c.DeleteDevice(ctx, got.ID)
require.NoError(t, err)
ok := c.DeleteDevice(ctx, got.ID)
require.True(t, ok)

// we should not be able to remove the device second time
ok, err = c.DeleteDevice(ctx, got.ID)
ok = c.DeleteDevice(ctx, got.ID)
require.False(t, ok)
require.NoError(t, err)
})
}
}
2 changes: 1 addition & 1 deletion client/getDevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (h *discoveryHandler) Handle(ctx context.Context, newdev *core.Device) {
dev, _ := h.deviceCache.UpdateOrStoreDeviceWithExpiration(newdev)
links, err := getLinksDevice(ctx, dev, h.disableUDPEndpoints)
if err != nil {
dev, ok := h.deviceCache.LoadAndDeleteDevice(ctx, dev.DeviceID())
dev, ok := h.deviceCache.LoadAndDeleteDevice(dev.DeviceID())
if ok {
dev.Close(ctx)
}
Expand Down
6 changes: 3 additions & 3 deletions client/ownDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Client) OwnDevice(ctx context.Context, deviceID string, opts ...OwnOpti
return c.deviceOwner.OwnDevice(ctx, deviceID, cfg.otmTypes, cfg.discoveryConfiguration, c.ownDeviceWithSigners, cfg.opts...)
}

func (c *Client) updateCache(ctx context.Context, d *core.Device, oldDeviceID string) {
func (c *Client) updateCache(d *core.Device, oldDeviceID string) {
if d.DeviceID() == oldDeviceID {
return
}
Expand All @@ -44,7 +44,7 @@ func (c *Client) updateCache(ctx context.Context, d *core.Device, oldDeviceID st
}
// remove device from key oldDeviceID
// we don't need to close it because it is already stored on new deviceID position
_, _ = c.deviceCache.LoadAndDeleteDevice(ctx, oldDeviceID)
_, _ = c.deviceCache.LoadAndDeleteDevice(oldDeviceID)
}

func (c *Client) ownDeviceWithSigners(ctx context.Context, deviceID string, otmClient []otm.Client, discoveryConfiguration core.DiscoveryConfiguration, opts ...core.OwnOption) (string, error) {
Expand All @@ -71,7 +71,7 @@ func (c *Client) ownDeviceWithSigners(ctx context.Context, deviceID string, otmC
if err != nil {
return "", err
}
c.updateCache(ctx, d, deviceID)
c.updateCache(d, deviceID)

return d.DeviceID(), nil
}