瀏覽代碼

return prune data when context canceled

Signed-off-by: allencloud <allen.sun@daocloud.io>
allencloud 8 年之前
父節點
當前提交
87b4dc2002
共有 4 個文件被更改,包括 23 次插入13 次删除
  1. 1 1
      api/server/backend/build/backend.go
  2. 10 3
      builder/fscache/fscache.go
  3. 1 1
      builder/fscache/fscache_test.go
  4. 11 8
      daemon/prune.go

+ 1 - 1
api/server/backend/build/backend.go

@@ -70,7 +70,7 @@ func (b *Backend) Build(ctx context.Context, config backend.BuildConfig) (string
 
 // PruneCache removes all cached build sources
 func (b *Backend) PruneCache(ctx context.Context) (*types.BuildCachePruneReport, error) {
-	size, err := b.fsCache.Prune()
+	size, err := b.fsCache.Prune(ctx)
 	if err != nil {
 		return nil, errors.Wrap(err, "failed to prune build cache")
 	}

+ 10 - 3
builder/fscache/fscache.go

@@ -154,8 +154,8 @@ func (fsc *FSCache) DiskUsage() (int64, error) {
 }
 
 // Prune allows manually cleaning up the cache
-func (fsc *FSCache) Prune() (uint64, error) {
-	return fsc.store.Prune()
+func (fsc *FSCache) Prune(ctx context.Context) (uint64, error) {
+	return fsc.store.Prune(ctx)
 }
 
 // Close stops the gc and closes the persistent db
@@ -396,12 +396,19 @@ func (s *fsCacheStore) DiskUsage() (int64, error) {
 }
 
 // Prune allows manually cleaning up the cache
-func (s *fsCacheStore) Prune() (uint64, error) {
+func (s *fsCacheStore) Prune(ctx context.Context) (uint64, error) {
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	var size uint64
 
 	for id, snap := range s.sources {
+		select {
+		case <-ctx.Done():
+			logrus.Debugf("Cache prune operation cancelled, pruned size: %d", size)
+			// when the context is cancelled, only return current size and nil
+			return size, nil
+		default:
+		}
 		if len(snap.refs) == 0 {
 			ss, err := snap.getSize()
 			if err != nil {

+ 1 - 1
builder/fscache/fscache_test.go

@@ -97,7 +97,7 @@ func TestFSCache(t *testing.T) {
 	assert.Equal(t, s, int64(8))
 
 	// prune deletes everything
-	released, err := fscache.Prune()
+	released, err := fscache.Prune(context.TODO())
 	assert.Nil(t, err)
 	assert.Equal(t, released, uint64(8))
 

+ 11 - 8
daemon/prune.go

@@ -74,8 +74,8 @@ func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.
 	for _, c := range allContainers {
 		select {
 		case <-ctx.Done():
-			logrus.Warnf("ContainersPrune operation cancelled: %#v", *rep)
-			return rep, ctx.Err()
+			logrus.Debugf("ContainersPrune operation cancelled: %#v", *rep)
+			return rep, nil
 		default:
 		}
 
@@ -121,7 +121,7 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg
 	pruneVols := func(v volume.Volume) error {
 		select {
 		case <-ctx.Done():
-			logrus.Warnf("VolumesPrune operation cancelled: %#v", *rep)
+			logrus.Debugf("VolumesPrune operation cancelled: %#v", *rep)
 			return ctx.Err()
 		default:
 		}
@@ -153,6 +153,9 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg
 	}
 
 	err = daemon.traverseLocalVolumes(pruneVols)
+	if err == context.Canceled {
+		return rep, nil
+	}
 
 	return rep, err
 }
@@ -303,8 +306,7 @@ deleteImagesLoop:
 	}
 
 	if canceled {
-		logrus.Warnf("ImagesPrune operation cancelled: %#v", *rep)
-		return nil, ctx.Err()
+		logrus.Debugf("ImagesPrune operation cancelled: %#v", *rep)
 	}
 
 	return rep, nil
@@ -320,6 +322,7 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
 	l := func(nw libnetwork.Network) bool {
 		select {
 		case <-ctx.Done():
+			// context cancelled
 			return true
 		default:
 		}
@@ -370,7 +373,7 @@ func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters fil
 	for _, nw := range networks {
 		select {
 		case <-ctx.Done():
-			return rep, ctx.Err()
+			return rep, nil
 		default:
 			if nw.Ingress {
 				// Routing-mesh network removal has to be explicitly invoked by user
@@ -427,8 +430,8 @@ func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Ar
 
 	select {
 	case <-ctx.Done():
-		logrus.Warnf("NetworksPrune operation cancelled: %#v", *rep)
-		return nil, ctx.Err()
+		logrus.Debugf("NetworksPrune operation cancelled: %#v", *rep)
+		return rep, nil
 	default:
 	}