Signed-off-by: Lei Jitang <leijitang@huawei.com>
| ... | ... |
@@ -82,9 +82,6 @@ type Graph struct {
|
| 82 | 82 |
imageMutex imageMutex // protect images in driver. |
| 83 | 83 |
retained *retainedLayers |
| 84 | 84 |
tarSplitDisabled bool |
| 85 |
- |
|
| 86 |
- parentRefs map[string]int |
|
| 87 |
- parentRefsMutex sync.Mutex |
|
| 88 | 85 |
} |
| 89 | 86 |
|
| 90 | 87 |
// file names for ./graph/<ID>/ |
| ... | ... |
@@ -115,11 +112,10 @@ func NewGraph(root string, driver graphdriver.Driver) (*Graph, error) {
|
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
graph := &Graph{
|
| 118 |
- root: abspath, |
|
| 119 |
- idIndex: truncindex.NewTruncIndex([]string{}),
|
|
| 120 |
- driver: driver, |
|
| 121 |
- retained: &retainedLayers{layerHolders: make(map[string]map[string]struct{})},
|
|
| 122 |
- parentRefs: make(map[string]int), |
|
| 118 |
+ root: abspath, |
|
| 119 |
+ idIndex: truncindex.NewTruncIndex([]string{}),
|
|
| 120 |
+ driver: driver, |
|
| 121 |
+ retained: &retainedLayers{layerHolders: make(map[string]map[string]struct{})},
|
|
| 123 | 122 |
} |
| 124 | 123 |
|
| 125 | 124 |
// Windows does not currently support tarsplit functionality. |
| ... | ... |
@@ -287,13 +283,6 @@ func (graph *Graph) Register(img *image.Image, layerData io.Reader) (err error) |
| 287 | 287 |
return err |
| 288 | 288 |
} |
| 289 | 289 |
graph.idIndex.Add(img.ID) |
| 290 |
- |
|
| 291 |
- graph.parentRefsMutex.Lock() |
|
| 292 |
- if img.Parent != "" {
|
|
| 293 |
- graph.parentRefs[img.Parent]++ |
|
| 294 |
- } |
|
| 295 |
- graph.parentRefsMutex.Unlock() |
|
| 296 |
- |
|
| 297 | 290 |
return nil |
| 298 | 291 |
} |
| 299 | 292 |
|
| ... | ... |
@@ -356,10 +345,6 @@ func (graph *Graph) Delete(name string) error {
|
| 356 | 356 |
if err != nil {
|
| 357 | 357 |
return err |
| 358 | 358 |
} |
| 359 |
- img, err := graph.Get(id) |
|
| 360 |
- if err != nil {
|
|
| 361 |
- return err |
|
| 362 |
- } |
|
| 363 | 359 |
tmp, err := graph.mktemp() |
| 364 | 360 |
graph.idIndex.Delete(id) |
| 365 | 361 |
if err == nil {
|
| ... | ... |
@@ -374,16 +359,6 @@ func (graph *Graph) Delete(name string) error {
|
| 374 | 374 |
} |
| 375 | 375 |
// Remove rootfs data from the driver |
| 376 | 376 |
graph.driver.Remove(id) |
| 377 |
- |
|
| 378 |
- graph.parentRefsMutex.Lock() |
|
| 379 |
- if img.Parent != "" {
|
|
| 380 |
- graph.parentRefs[img.Parent]-- |
|
| 381 |
- if graph.parentRefs[img.Parent] == 0 {
|
|
| 382 |
- delete(graph.parentRefs, img.Parent) |
|
| 383 |
- } |
|
| 384 |
- } |
|
| 385 |
- graph.parentRefsMutex.Unlock() |
|
| 386 |
- |
|
| 387 | 377 |
// Remove the trashed image directory |
| 388 | 378 |
return os.RemoveAll(tmp) |
| 389 | 379 |
} |
| ... | ... |
@@ -401,11 +376,9 @@ func (graph *Graph) Map() map[string]*image.Image {
|
| 401 | 401 |
// The walking order is undetermined. |
| 402 | 402 |
func (graph *Graph) walkAll(handler func(*image.Image)) {
|
| 403 | 403 |
graph.idIndex.Iterate(func(id string) {
|
| 404 |
- img, err := graph.Get(id) |
|
| 405 |
- if err != nil {
|
|
| 404 |
+ if img, err := graph.Get(id); err != nil {
|
|
| 406 | 405 |
return |
| 407 |
- } |
|
| 408 |
- if handler != nil {
|
|
| 406 |
+ } else if handler != nil {
|
|
| 409 | 407 |
handler(img) |
| 410 | 408 |
} |
| 411 | 409 |
}) |
| ... | ... |
@@ -433,10 +406,7 @@ func (graph *Graph) ByParent() map[string][]*image.Image {
|
| 433 | 433 |
|
| 434 | 434 |
// HasChildren returns whether the given image has any child images. |
| 435 | 435 |
func (graph *Graph) HasChildren(img *image.Image) bool {
|
| 436 |
- graph.parentRefsMutex.Lock() |
|
| 437 |
- refCount := graph.parentRefs[img.ID] |
|
| 438 |
- graph.parentRefsMutex.Unlock() |
|
| 439 |
- return refCount > 0 |
|
| 436 |
+ return len(graph.ByParent()[img.ID]) > 0 |
|
| 440 | 437 |
} |
| 441 | 438 |
|
| 442 | 439 |
// Retain keeps the images and layers that are in the pulling chain so that |
| ... | ... |
@@ -454,14 +424,13 @@ func (graph *Graph) Release(sessionID string, layerIDs ...string) {
|
| 454 | 454 |
// A head is an image which is not the parent of another image in the graph. |
| 455 | 455 |
func (graph *Graph) Heads() map[string]*image.Image {
|
| 456 | 456 |
heads := make(map[string]*image.Image) |
| 457 |
+ byParent := graph.ByParent() |
|
| 457 | 458 |
graph.walkAll(func(image *image.Image) {
|
| 458 | 459 |
// If it's not in the byParent lookup table, then |
| 459 | 460 |
// it's not a parent -> so it's a head! |
| 460 |
- graph.parentRefsMutex.Lock() |
|
| 461 |
- if _, exists := graph.parentRefs[image.ID]; !exists {
|
|
| 461 |
+ if _, exists := byParent[image.ID]; !exists {
|
|
| 462 | 462 |
heads[image.ID] = image |
| 463 | 463 |
} |
| 464 |
- graph.parentRefsMutex.Unlock() |
|
| 465 | 464 |
}) |
| 466 | 465 |
return heads |
| 467 | 466 |
} |