// snapshot exports the current running state of the vserver.
775903
func (v *vserver) snapshot() *seesaw.Vserver {
	if v.config == nil {
		return nil
	}
	sv := &seesaw.Vserver{
		Name:    v.config.Name,
		Entries: make([]*seesaw.VserverEntry, 0, len(v.config.Entries)),
		Host: seesaw.Host{
			Hostname: v.config.Hostname,
			IPv4Addr: v.config.IPv4Addr,
			IPv4Mask: v.config.IPv4Mask,
			IPv6Addr: v.config.IPv6Addr,
			IPv6Mask: v.config.IPv6Mask,
		},
		FWM:           make(map[seesaw.AF]uint32),
		Services:      make(map[seesaw.ServiceKey]*seesaw.Service, len(v.services)),
		OverrideState: v.vserverOverride.State(),
		Enabled:       v.enabled,
		ConfigEnabled: v.config.Enabled,
		Warnings:      v.config.Warnings,
	}
	for _, ve := range v.config.Entries {
		sv.Entries = append(sv.Entries, ve.Snapshot())
	}
	sv.FWM[seesaw.IPv4] = v.fwm[seesaw.IPv4]
	sv.FWM[seesaw.IPv6] = v.fwm[seesaw.IPv6]
	for _, s := range v.services {
		ss := s.snapshot()
		sv.Services[ss.ServiceKey] = ss
	}
	return sv
}
311702
func snapshots(srv *clonesrv_t) (err error) {

	msg, err := srv.snapshot.RecvMessage(0)
	if err != nil {
		return
	}
	identity := msg[0]

	//  Request is in second frame of message
	request := msg[1]
	if request != "ICANHAZ?" {
		err = errors.New("E: bad request, aborting")
		return
	}
	subtree := msg[2]

	//  Send state socket to client
	for _, kvmsg := range srv.kvmap {
		if key, _ := kvmsg.GetKey(); strings.HasPrefix(key, subtree) {
			srv.snapshot.Send(identity, zmq.SNDMORE)
			kvmsg.Send(srv.snapshot)
		}
	}

	//  Now send END message with sequence number
	log.Println("I: sending shapshot =", srv.sequence)
	srv.snapshot.Send(identity, zmq.SNDMORE)
	kvmsg := kvmsg.NewKvmsg(srv.sequence)
	kvmsg.SetKey("KTHXBAI")
	kvmsg.SetBody(subtree)
	kvmsg.Send(srv.snapshot)

	return
}
491295
func SnapshotT(t TestingT, i ...interface{}) {
	t.Helper()
	Global.SnapshotT(t, i...)
}
1287706
func (s *Store) Snapshot() (raft.FSMSnapshot, error) {
	s.mu.Lock()
	defer s.mu.Unlock()

	buf, err := json.Marshal(s.data)
	if err != nil {
		return nil, err
	}
	return &raftSnapshot{data: buf}, nil
}
796098
func (f *FSM) Snapshot() (raft.FSMSnapshot, error) {
	f.mu.Lock()

	entries := make(map[SnapshotKey]SnapshotEntry)
	for _, group := range f.groups {
		for key, entry := range group {
			entries[SnapshotKey{
				Namespace: key.Namespace,
				ModelUUID: key.ModelUUID,
				Lease:     key.Lease,
			}] = SnapshotEntry{
				Holder:   entry.holder,
				Start:    entry.start,
				Duration: entry.duration,
			}
		}
	}

	pinned := make(map[SnapshotKey][]string)
	for key, entities := range f.pinned {
		if entities.IsEmpty() {
			continue
		}
		pinned[SnapshotKey{
			Namespace: key.Namespace,
			ModelUUID: key.ModelUUID,
			Lease:     key.Lease,
		}] = entities.SortedValues()
	}

	f.mu.Unlock()

	return &Snapshot{
		Version:    SnapshotVersion,
		Entries:    entries,
		Pinned:     pinned,
		GlobalTime: f.globalTime,
	}, nil
}
1242020
func (api *ServerAPI) GetVNCSnapshot(serverID int64, body *sacloud.VNCSnapshotRequest) (*sacloud.VNCSnapshotResponse, error) {
	var (
		method = "GET"
		uri    = fmt.Sprintf("%s/%d/vnc/snapshot", api.getResourceURL(), serverID)
		res    = &sacloud.VNCSnapshotResponse{}
	)
	err := api.baseAPI.request(method, uri, body, res)
	if err != nil {
		return nil, err
	}
	return res, nil
}
1192989
func (c *instanceCache) getSnapshot() *allInstancesSnapshot {
	c.mutex.Lock()
	defer c.mutex.Unlock()

	return c.snapshot
}
1317203
func (f *Facade) Snapshot(ctx datastore.Context, serviceID, message string, tags []string, snapshotSpacePercent int) (string, error) {
	defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.Snapshot"))
	// Do not DFSLock here, ControlPlaneDao does that

	logger := plog.WithFields(logrus.Fields{
		"serviceid": serviceID,
	})

	tenantID, err := f.GetTenantID(ctx, serviceID)
	if err != nil {
		logger.WithError(err).Debug("Could not get tenant id of service")
		return "", err
	}
	logger = logger.WithField("tenantid", tenantID)
	if err := f.lockTenant(ctx, tenantID); err != nil {
		logger.WithError(err).Debug("Could not lock tenant")
		return "", err
	}
	defer f.retryUnlockTenant(ctx, tenantID, nil, time.Second)
	logger.Info("Got tenant lock")
	svcs, err := f.GetServices(ctx, dao.ServiceRequest{TenantID: tenantID})
	if err != nil {
		logger.WithError(err).Debug("Could not get services under tenant")
		return "", err
	}
	imagesMap := make(map[string]struct{})
	images := make([]string, 0)
	serviceids := []string{}
	servicesToPause := []*service.Service{}
	poolmap := make(map[string]bool)
	var pausedServiceIds []string
	for i, _ := range svcs {
		svc := &svcs[i]

		// only pause services that have access to the dfs
		hasDFS, ok := poolmap[svc.PoolID]
		if !ok {
			p := &pool.ResourcePool{}
			err := f.poolStore.Get(ctx, pool.Key(svc.PoolID), p)
			if err != nil {
				logger.WithField("poolid", svc.PoolID).WithError(err).Error("Could not get resource pool")
				return "", err
			}
			hasDFS = p.HasDfsAccess()
			poolmap[svc.PoolID] = hasDFS
		}

		if hasDFS {
			if svc.DesiredState == int(service.SVCRun) {
				servicesToPause = append(servicesToPause, svc)
				pausedServiceIds = append(pausedServiceIds, svc.ID)
			}
			serviceids = append(serviceids, svc.ID)
		}

		if svc.ImageID != "" {
			if _, ok := imagesMap[svc.ImageID]; !ok {
				imagesMap[svc.ImageID] = struct{}{}
				images = append(images, svc.ImageID)
			}
		}
	}
	// Pause the services that need pausing in a batch
	if _, err := scheduleServices(f, servicesToPause, ctx, tenantID, service.SVCPause, false); err != nil {
		logger.WithError(err).Debug("Could not pause services for snapshot")
		return "", err
	}

	defer func() {
		// Refresh service objects in case something has changed (like current state)
		servicesToPause = f.GetServicesForScheduling(ctx, pausedServiceIds)
		scheduleServices(f, servicesToPause, ctx, tenantID, service.SVCRun, false)
	}()

	// Wait for the paused services to reach the paused state (and other services to reach stopped)
	if err := f.WaitService(ctx, service.SVCPause, f.dfs.Timeout(), false, serviceids...); err != nil {
		logger.WithError(err).Debug("Could not wait for services to pause during snapshot")
		return "", err
	}
	logger.Infof("Services are now paused for snapshot")
	data := dfs.SnapshotInfo{
		SnapshotInfo: &volume.SnapshotInfo{
			TenantID: tenantID,
			Message:  message,
			Tags:     tags,
		},
		Services: svcs,
		Images:   images,
	}
	snapshotID, err := f.dfs.Snapshot(data, snapshotSpacePercent)
	if err != nil {
		logger.WithError(err).Debug("Could not snapshot disk and images for tenant")
		return "", err
	}
	logger.WithField("snapshotid", snapshotID).Info("Successfully captured application data and created snapshot")
	return snapshotID, nil
}
1209992
func Snapshot(name string, opts *SnapshotOpts) error {
	cmd := newCmd("snapshot")
	if opts != nil {
		cmd = addOpt(cmd, opts.Recurse, "-r")
		cmd = addProperties(cmd, opts.SetProperties)
	}
	cmd = addNonEmpty(cmd, name)
	return zExecNoOut(cmd)
}
248162
func (kvm *Clone) Snapshot(wr io.Writer) error {
	kvm.mu.RLock()
	defer kvm.mu.RUnlock()
	data, err := json.Marshal(kvm.keys)
	if err != nil {
		return err
	}
	if _, err := wr.Write(data); err != nil {
		return err
	}
	return nil
}
806530
func (s *ExpDecaySample) Snapshot() Sample {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	vals := s.values.Values()
	values := make([]int64, len(vals))
	for i, v := range vals {
		values[i] = v.v
	}
	return &SampleSnapshot{
		count:  s.count,
		values: values,
	}
}
1357838
func (e *CombinedEtcdClient) Snapshot(version *EtcdVersion, snapshotFile string) error {
	if version.Major != 3 {
		return fmt.Errorf("etcd 3.x required but got version '%s'", version)
	}
	return e.runEtcdctlCommand(version,
		"--endpoints", e.endpoint(),
		"snapshot", "save", snapshotFile,
	)
}
1098787
func (m *collection) Snapshot() (rv Snapshot, err error) {
	m.m.Lock()
	rv = reuseSnapshot(m.latestSnapshot)
	if rv == nil { // No cached snapshot.
		rv, err = m.newSnapshotLOCKED()
		if err == nil {
			// collection holds 1 ref count for its cached snapshot copy.
			m.latestSnapshot = reuseSnapshot(rv)
		}
	}
	m.m.Unlock()

	return
}
1291336
func (s *StateStore) Snapshot() (*StateSnapshot, error) {
	snap := &StateSnapshot{
		StateStore: StateStore{
			logger: s.logger,
			config: s.config,
			db:     s.db.Snapshot(),
		},
	}
	return snap, nil
}
906874
func (r *Reservoir) Snapshot() []int64 {
	r.m.Lock()
	defer r.m.Unlock()
	s := atomic.SwapInt64(&r.size, 0)
	v := make([]int64, min(int(s), len(r.values)))
	copy(v, r.values)
	r.values = make([]int64, cap(r.values))
	sorted(v)
	return v
}
751968
func (s *server) saveSnapshot() error {
	if s.pendingSnapshot == nil {
		return errors.New("pendingSnapshot.is.nil")
	}

	// Write snapshot to disk.
	if err := s.pendingSnapshot.save(); err != nil {
		return err
	}

	// Swap the current and last snapshots.
	tmp := s.snapshot
	s.snapshot = s.pendingSnapshot

	// Delete the previous snapshot if there is any change
	if tmp != nil && !(tmp.LastIndex == s.snapshot.LastIndex && tmp.LastTerm == s.snapshot.LastTerm) {
		tmp.remove()
	}
	s.pendingSnapshot = nil

	return nil
}
409519
func (h *StandardHistogram) Snapshot() Histogram {
	return &HistogramSnapshot{sample: h.sample.Snapshot().(*SampleSnapshot)}
}