code: func (s *State) PersistState() error {
  	s.mu.Lock()
  	defer s.mu.Unlock()
  
  	if s.readState != nil {
  		if !statefile.StatesMarshalEqual(s.state, s.readState) {
  			s.serial++
  		}
  	} else {
  		// We might be writing a new state altogether, but before we do that
  		// we'll check to make sure there isn't already a snapshot present
  		// that we ought to be updating.
  		err := s.refreshState()
  		if err != nil {
  			return fmt.Errorf("failed checking for existing remote state: %s", err)
  		}
  		if s.lineage == "" { // indicates that no state snapshot is present yet
  			lineage, err := uuid.GenerateUUID()
  			if err != nil {
  				return fmt.Errorf("failed to generate initial lineage: %v", err)
  			}
  			s.lineage = lineage
  			s.serial = 0
  		}
  	}
  
  	f := statefile.New(s.state, s.lineage, s.serial)
  
  	var buf bytes.Buffer
  	err := statefile.Write(f, &buf)
  	if err != nil {
  		return err
  	}
  
  	err = s.Client.Put(buf.Bytes())
  	if err != nil {
  		return err
  	}
  
  	// After we've successfully persisted, what we just wrote is our new
  	// reference state until someone calls RefreshState again.
  	s.readState = s.state.DeepCopy()
  	return nil
  }
docs: // statemgr.Persister impl.