// ToGarbage transitions a pod from abortedPrepared -> garbage or prepared -> garbage
// This method refreshes the pod state.
420709
func (p *Pod) ToGarbage() error {
	if !p.isAbortedPrepare && !p.isPrepared {
		return fmt.Errorf("bug: only failed prepare or prepared pods may transition to garbage")
	}

	if err := os.Rename(p.Path(), p.garbagePath()); err != nil {
		return err
	}

	df, err := os.Open(garbageDir(p.dataDir))
	if err != nil {
		return err
	}
	defer df.Close()
	if err := df.Sync(); err != nil {
		return err
	}

	p.isAbortedPrepare = false
	p.isPrepared = false
	p.isGarbage = true

	return nil
}
1018584
func (gc *GarbageCollector) deleteOwnerRefJSONMergePatch(item *node, ownerUIDs ...types.UID) ([]byte, error) {
	accessor, err := gc.getMetadata(item.identity.APIVersion, item.identity.Kind, item.identity.Namespace, item.identity.Name)
	if err != nil {
		return nil, err
	}
	expectedObjectMeta := ObjectMetaForPatch{}
	expectedObjectMeta.ResourceVersion = accessor.GetResourceVersion()
	refs := accessor.GetOwnerReferences()
	for _, ref := range refs {
		var skip bool
		for _, ownerUID := range ownerUIDs {
			if ref.UID == ownerUID {
				skip = true
				break
			}
		}
		if !skip {
			expectedObjectMeta.OwnerReferences = append(expectedObjectMeta.OwnerReferences, ref)
		}
	}
	return json.Marshal(objectForPatch{expectedObjectMeta})
}
1008496
func emptyExitedGarbage(gracePeriod time.Duration) error {
	if err := pkgPod.WalkPods(getDataDir(), pkgPod.IncludeExitedGarbageDir, func(p *pkgPod.Pod) {
		gp := p.Path()
		st := &syscall.Stat_t{}
		if err := syscall.Lstat(gp, st); err != nil {
			if err != syscall.ENOENT {
				stderr.PrintE(fmt.Sprintf("unable to stat %q, ignoring", gp), err)
			}
			return
		}

		if expiration := time.Unix(st.Ctim.Unix()).Add(gracePeriod); time.Now().After(expiration) {
			if err := p.ExclusiveLock(); err != nil {
				return
			}
			stdout.Printf("Garbage collecting pod %q", p.UUID)

			deletePod(p)
		} else {
			stderr.Printf("pod %q not removed: still within grace period (%s)", p.UUID, gracePeriod)
		}
	}); err != nil {
		return err
	}

	return nil
}
469307
func (e *Environment) ensurePrepared(c context.Context) {
	e.prepareOnce.Do(func() {
		e.processCacheData = caching.NewProcessCacheData()
		e.globalSettings = settings.New(gaesettings.Storage{})
		if e.Prepare != nil {
			e.Prepare(c)
		}
	})
}
1298254
func loadGopkgLock(rawToml []byte) pinnedPkgs {
	lock := struct {
		Projects []struct {
			Name     string `toml:"name"`
			Revision string `toml:"revision"`
			// There are other fields, but we don't use them
		} `toml:"projects"`
		// There are other fields, but we don't use them
	}{}
	err := toml.Unmarshal(rawToml, &lock)
	if err != nil {
		return nil
	}

	pkgs := make(pinnedPkgs, 0, len(lock.Projects))
	for _, p := range lock.Projects {
		pkgs = append(pkgs, pinnedPkg{Pkg: p.Name + "/", Rev: p.Revision})
	}
	sort.Sort(pkgs)
	return pkgs
}
156131
func (s *baggageSetter) setBaggage(span *Span, key, value string) {
	var truncated bool
	var prevItem string
	restriction := s.restrictionManager.GetRestriction(span.serviceName(), key)
	if !restriction.KeyAllowed() {
		s.logFields(span, key, value, prevItem, truncated, restriction.KeyAllowed())
		s.metrics.BaggageUpdateFailure.Inc(1)
		return
	}
	if len(value) > restriction.MaxValueLength() {
		truncated = true
		value = value[:restriction.MaxValueLength()]
		s.metrics.BaggageTruncate.Inc(1)
	}
	prevItem = span.context.baggage[key]
	s.logFields(span, key, value, prevItem, truncated, restriction.KeyAllowed())
	span.context = span.context.WithBaggageItem(key, value)
	s.metrics.BaggageUpdateSuccess.Inc(1)
}
164894
func (h *hawkularSink) pushToCache(key string, hash uint64) {
	h.regLock.Lock()
	h.expReg[key] = &expiringItem{
		hash: hash,
		ttl:  h.runId,
	}
	h.regLock.Unlock()
}
556056
func (c SpanContext) WithBaggageItem(key, val string) SpanContext {
	var newBaggage map[string]string
	if c.Baggage == nil {
		newBaggage = map[string]string{key: val}
	} else {
		newBaggage = make(map[string]string, len(c.Baggage)+1)
		for k, v := range c.Baggage {
			newBaggage[k] = v
		}
		newBaggage[key] = val
	}
	// Use positional parameters so the compiler will help catch new fields.
	return SpanContext{c.TraceID, c.SpanID, c.Sampled, newBaggage}
}
1349700
func (p *Pod) garbagePath() string {
	return filepath.Join(garbageDir(p.dataDir), p.UUID.String())
}
75661
func (wg *WaitGroup) Go(cb func()) {
	wg.wg.Add(1)
	go func() {
		cb()
		wg.wg.Done()
	}()
}
204940
func renameExited() error {
	if err := pkgPod.WalkPods(getDataDir(), pkgPod.IncludeRunDir, func(p *pkgPod.Pod) {
		if p.State() == pkgPod.Exited {
			stderr.Printf("moving pod %q to garbage", p.UUID)
			if err := p.ToExitedGarbage(); err != nil && err != os.ErrNotExist {
				stderr.PrintE("rename error", err)
			}
		}
	}); err != nil {
		return err
	}

	return nil
}
638883
func (h *BrandsHandler) GTG() gtg.Status {
	statusCheck := func() gtg.Status {
		return gtgCheck(h.Checker)
	}
	return gtg.FailFastParallelCheck([]gtg.StatusChecker{statusCheck})()
}
994166
func collapseAggregateErrors(errors []error) error {
	if len(errors) == 0 {
		return nil
	}
	if len(errors) == 1 {
		return errors[0]
	}

	allNoMatchErrors := true
	for _, err := range errors {
		allNoMatchErrors = allNoMatchErrors && IsNoMatchError(err)
	}
	if allNoMatchErrors {
		return errors[0]
	}

	return utilerrors.NewAggregate(errors)
}
868748
func (q *LinkedBlockingDeque) ToSlice() []interface{} {
	q.lock.Lock()
	defer q.lock.Unlock()
	a := make([]interface{}, q.count)
	for p, k := q.first, 0; p != nil; p, k = p.next, k+1 {
		a[k] = p.item
	}
	return a
}
1018445
func deletePod(p *pkgPod.Pod) bool {
	podState := p.State()
	if podState != pkgPod.ExitedGarbage && podState != pkgPod.Garbage && podState != pkgPod.ExitedDeleting {
		stderr.Errorf("non-garbage pod %q (status %q), skipped", p.UUID, p.State())
		return false
	}

	if podState == pkgPod.ExitedGarbage {
		s, err := imagestore.NewStore(storeDir())
		if err != nil {
			stderr.PrintE("cannot open store", err)
			return false
		}
		defer s.Close()

		ts, err := treestore.NewStore(treeStoreDir(), s)
		if err != nil {
			stderr.PrintE("cannot open store", err)
			return false
		}

		if globalFlags.Debug {
			stage0.InitDebug()
		}

		if newMount, err := mountPodStage1(ts, p); err == nil {
			if err = stage0.GC(p.Path(), p.UUID, globalFlags.LocalConfigDir); err != nil {
				stderr.PrintE(fmt.Sprintf("problem performing stage1 GC on %q", p.UUID), err)
			}
			// Linux <4.13 allows an overlay fs to be mounted over itself, so let's
			// unmount it here to avoid problems when running stage0.MountGC
			if p.UsesOverlay() && newMount {
				stage1Mnt := common.Stage1RootfsPath(p.Path())
				if err := syscall.Unmount(stage1Mnt, 0); err != nil && err != syscall.EBUSY {
					stderr.PrintE("error unmounting stage1", err)
				}
			}
		} else {
			stderr.PrintE("skipping stage1 GC", err)
		}

		// unmount all leftover mounts
		if err := stage0.MountGC(p.Path(), p.UUID.String()); err != nil {
			stderr.PrintE(fmt.Sprintf("GC of leftover mounts for pod %q failed", p.UUID), err)
			return false
		}
	}

	// remove the rootfs first; if this fails (eg. due to busy mountpoints), pod manifest
	// is left in place and clean-up can be re-tried later.
	rootfsPath, err := p.Stage1RootfsPath()
	if err == nil {
		if e := os.RemoveAll(rootfsPath); e != nil {
			stderr.PrintE(fmt.Sprintf("unable to remove pod rootfs %q", p.UUID), e)
			return false
		}
	}

	// finally remove all remaining pieces
	if err := os.RemoveAll(p.Path()); err != nil {
		stderr.PrintE(fmt.Sprintf("unable to remove pod %q", p.UUID), err)
		return false
	}

	return true
}
511270
func (i *iNode) copyToGen(gen *generation, ctrie *Ctrie) *iNode {
	nin := &iNode{gen: gen}
	main := gcasRead(i, ctrie)
	atomic.StorePointer(
		(*unsafe.Pointer)(unsafe.Pointer(&nin.main)), unsafe.Pointer(main))
	return nin
}
442693
func IsAggregate(err error) bool {
	_, ok := Unwrap(err).(Aggregate)
	return ok
}