Skip to content

Commit

Permalink
Merge pull request #7019 from nustiueudinastea/concurrent-backups-map2
Browse files Browse the repository at this point in the history
Concurrent backups map
  • Loading branch information
zachmu authored Nov 20, 2023
2 parents 4cffade + 5aed4d1 commit 517e867
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 38 deletions.
4 changes: 2 additions & 2 deletions go/cmd/dolt/commands/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func printBackups(dEnv *env.DoltEnv, apr *argparser.ArgParseResults) errhand.Ver
return errhand.BuildDError("Unable to get backups from the local directory").AddCause(err).Build()
}

for _, r := range backups {
for _, r := range backups.Snapshot() {
if apr.Contains(cli.VerboseFlag) {
paramStr := make([]byte, 0)
if len(r.Params) > 0 {
Expand Down Expand Up @@ -256,7 +256,7 @@ func syncBackup(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgParseR
return errhand.BuildDError("Unable to get backups from the local directory").AddCause(err).Build()
}

b, ok := backups[backupName]
b, ok := backups.Get(backupName)
if !ok {
return errhand.BuildDError("error: unknown backup: '%s' ", backupName).Build()
}
Expand Down
29 changes: 16 additions & 13 deletions go/libraries/doltcore/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,7 @@ func createRepoState(fs filesys.Filesys) (*RepoState, error) {
// deep copy remotes and backups ¯\_(ツ)_/¯ (see commit c59cbead)
if repoState != nil {
repoState.Remotes = repoState.Remotes.DeepCopy()

backups := make(map[string]Remote, len(repoState.Backups))
for n, r := range repoState.Backups {
backups[n] = r
}
repoState.Backups = backups
repoState.Backups = repoState.Backups.DeepCopy()
}

return repoState, rsErr
Expand Down Expand Up @@ -863,7 +858,7 @@ func (dEnv *DoltEnv) GetRemotes() (*concurrentmap.Map[string, Remote], error) {

// CheckRemoteAddressConflict checks whether any backups or remotes share the given URL. Returns the first remote if multiple match.
// Returns NoRemote and false if none match.
func CheckRemoteAddressConflict(absUrl string, remotes *concurrentmap.Map[string, Remote], backups map[string]Remote) (Remote, bool) {
func CheckRemoteAddressConflict(absUrl string, remotes *concurrentmap.Map[string, Remote], backups *concurrentmap.Map[string, Remote]) (Remote, bool) {
if remotes != nil {
var rm *Remote
remotes.Iter(func(key string, value Remote) bool {
Expand All @@ -878,9 +873,17 @@ func CheckRemoteAddressConflict(absUrl string, remotes *concurrentmap.Map[string
}
}

for _, r := range backups {
if r.Url == absUrl {
return r, true
if backups != nil {
var rm *Remote
backups.Iter(func(key string, value Remote) bool {
if value.Url == absUrl {
rm = &value
return false
}
return true
})
if rm != nil {
return *rm, true
}
}
return NoRemote, false
Expand Down Expand Up @@ -910,7 +913,7 @@ func (dEnv *DoltEnv) AddRemote(r Remote) error {
return dEnv.RepoState.Save(dEnv.FS)
}

func (dEnv *DoltEnv) GetBackups() (map[string]Remote, error) {
func (dEnv *DoltEnv) GetBackups() (*concurrentmap.Map[string, Remote], error) {
if dEnv.RSLoadErr != nil {
return nil, dEnv.RSLoadErr
}
Expand All @@ -919,7 +922,7 @@ func (dEnv *DoltEnv) GetBackups() (map[string]Remote, error) {
}

func (dEnv *DoltEnv) AddBackup(r Remote) error {
if _, ok := dEnv.RepoState.Backups[r.Name]; ok {
if _, ok := dEnv.RepoState.Backups.Get(r.Name); ok {
return ErrBackupAlreadyExists
}

Expand Down Expand Up @@ -976,7 +979,7 @@ func (dEnv *DoltEnv) RemoveRemote(ctx context.Context, name string) error {
}

func (dEnv *DoltEnv) RemoveBackup(ctx context.Context, name string) error {
backup, ok := dEnv.RepoState.Backups[name]
backup, ok := dEnv.RepoState.Backups.Get(name)
if !ok {
return ErrBackupNotFound
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/env/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func createTestEnv(isInitialized bool, hasLocalConfig bool) (*DoltEnv, *filesys.
initialDirs = append(initialDirs, doltDataDir)

mainRef := ref.NewBranchRef(DefaultInitBranch)
repoState := &RepoState{Head: ref.MarshalableRef{Ref: mainRef}, Remotes: concurrentmap.New[string, Remote]()}
repoState := &RepoState{Head: ref.MarshalableRef{Ref: mainRef}, Remotes: concurrentmap.New[string, Remote](), Backups: concurrentmap.New[string, Remote]()}
repoStateData, err := json.Marshal(repoState)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/env/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (m MemoryRepoState) SetCWBHeadRef(_ context.Context, r ref.MarshalableRef)
}

func (m MemoryRepoState) GetRemotes() (*concurrentmap.Map[string, Remote], error) {
return &concurrentmap.Map[string, Remote]{}, nil
return concurrentmap.New[string, Remote](), nil
}

func (m MemoryRepoState) AddRemote(r Remote) error {
Expand All @@ -232,7 +232,7 @@ func (m MemoryRepoState) TempTableFilesDir() (string, error) {
return os.TempDir(), nil
}

func (m MemoryRepoState) GetBackups() (map[string]Remote, error) {
func (m MemoryRepoState) GetBackups() (*concurrentmap.Map[string, Remote], error) {
panic("cannot get backups on in memory database")
}

Expand Down
25 changes: 17 additions & 8 deletions go/libraries/doltcore/env/repo_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type RepoStateReader interface {
CWBHeadRef() (ref.DoltRef, error)
CWBHeadSpec() (*doltdb.CommitSpec, error)
GetRemotes() (*concurrentmap.Map[string, Remote], error)
GetBackups() (map[string]Remote, error)
GetBackups() (*concurrentmap.Map[string, Remote], error)
GetBranches() (map[string]BranchConfig, error)
}

Expand Down Expand Up @@ -71,7 +71,7 @@ type BranchConfig struct {
type RepoState struct {
Head ref.MarshalableRef `json:"head"`
Remotes *concurrentmap.Map[string, Remote] `json:"remotes"`
Backups map[string]Remote `json:"backups"`
Backups *concurrentmap.Map[string, Remote] `json:"backups"`
Branches map[string]BranchConfig `json:"branches"`
// |staged|, |working|, and |merge| are legacy fields left over from when Dolt repos stored this info in the repo
// state file, not in the DB directly. They're still here so that we can migrate existing repositories forward to the
Expand All @@ -86,7 +86,7 @@ type RepoState struct {
type repoStateLegacy struct {
Head ref.MarshalableRef `json:"head"`
Remotes *concurrentmap.Map[string, Remote] `json:"remotes"`
Backups map[string]Remote `json:"backups"`
Backups *concurrentmap.Map[string, Remote] `json:"backups"`
Branches map[string]BranchConfig `json:"branches"`
Staged string `json:"staged,omitempty"`
Working string `json:"working,omitempty"`
Expand All @@ -112,7 +112,7 @@ type mergeState struct {
}

func (rs *repoStateLegacy) toRepoState() *RepoState {
return &RepoState{
newRS := &RepoState{
Head: rs.Head,
Remotes: rs.Remotes,
Backups: rs.Backups,
Expand All @@ -121,6 +121,15 @@ func (rs *repoStateLegacy) toRepoState() *RepoState {
working: rs.Working,
merge: rs.Merge,
}

if newRS.Remotes == nil {
newRS.Remotes = concurrentmap.New[string, Remote]()
}
if newRS.Backups == nil {
newRS.Backups = concurrentmap.New[string, Remote]()
}

return newRS
}

func (rs *repoStateLegacy) save(fs filesys.ReadWriteFS) error {
Expand Down Expand Up @@ -162,7 +171,7 @@ func CloneRepoState(fs filesys.ReadWriteFS, r Remote) (*RepoState, error) {
working: hashStr,
Remotes: remotes,
Branches: make(map[string]BranchConfig),
Backups: make(map[string]Remote),
Backups: concurrentmap.New[string, Remote](),
}

err := rs.Save(fs)
Expand All @@ -184,7 +193,7 @@ func CreateRepoState(fs filesys.ReadWriteFS, br string) (*RepoState, error) {
Head: ref.MarshalableRef{Ref: headRef},
Remotes: concurrentmap.New[string, Remote](),
Branches: make(map[string]BranchConfig),
Backups: make(map[string]Remote),
Backups: concurrentmap.New[string, Remote](),
}

err = rs.Save(fs)
Expand Down Expand Up @@ -224,9 +233,9 @@ func (rs *RepoState) RemoveRemote(r Remote) {
}

func (rs *RepoState) AddBackup(r Remote) {
rs.Backups[r.Name] = r
rs.Backups.Set(r.Name, r)
}

func (rs *RepoState) RemoveBackup(r Remote) {
delete(rs.Backups, r.Name)
rs.Backups.Delete(r.Name)
}
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
sess, db.RevisionQualifiedName(),
concurrentmap.New[string, env.Remote](),
map[string]env.BranchConfig{},
map[string]env.Remote{})
concurrentmap.New[string, env.Remote]())
ws, err := sess.WorkingSet(ctx, db.RevisionQualifiedName())
if err != nil {
return nil, false, err
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dprocedures/dolt_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func syncBackupViaName(ctx *sql.Context, dbData env.DbData, sess *dsess.DoltSess
return err
}

b, ok := backups[backupName]
b, ok := backups.Get(backupName)
if !ok {
return fmt.Errorf("error: unknown backup: '%s'; %v", backupName, backups)
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dsess/database_session_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type InitialDbState struct {
DbData env.DbData
Remotes *concurrentmap.Map[string, env.Remote]
Branches map[string]env.BranchConfig
Backups map[string]env.Remote
Backups *concurrentmap.Map[string, env.Remote]

// If err is set, this InitialDbState is partially invalid, but may be
// usable to initialize a database at a revision specifier, for
Expand Down
18 changes: 9 additions & 9 deletions go/libraries/doltcore/sqle/dsess/session_state_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type SessionStateAdapter struct {
session *DoltSession
dbName string
remotes *concurrentmap.Map[string, env.Remote]
backups map[string]env.Remote
backups *concurrentmap.Map[string, env.Remote]
branches map[string]env.BranchConfig
}

Expand All @@ -45,7 +45,7 @@ var _ env.RepoStateReader = SessionStateAdapter{}
var _ env.RepoStateWriter = SessionStateAdapter{}
var _ env.RootsProvider = SessionStateAdapter{}

func NewSessionStateAdapter(session *DoltSession, dbName string, remotes *concurrentmap.Map[string, env.Remote], branches map[string]env.BranchConfig, backups map[string]env.Remote) SessionStateAdapter {
func NewSessionStateAdapter(session *DoltSession, dbName string, remotes *concurrentmap.Map[string, env.Remote], branches map[string]env.BranchConfig, backups *concurrentmap.Map[string, env.Remote]) SessionStateAdapter {
if branches == nil {
branches = make(map[string]env.BranchConfig)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s SessionStateAdapter) GetRemotes() (*concurrentmap.Map[string, env.Remote
return s.remotes, nil
}

func (s SessionStateAdapter) GetBackups() (map[string]env.Remote, error) {
func (s SessionStateAdapter) GetBackups() (*concurrentmap.Map[string, env.Remote], error) {
return s.backups, nil
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func (s SessionStateAdapter) AddRemote(remote env.Remote) error {
}

func (s SessionStateAdapter) AddBackup(backup env.Remote) error {
if _, ok := s.backups[backup.Name]; ok {
if _, ok := s.backups.Get(backup.Name); ok {
return env.ErrBackupAlreadyExists
}

Expand All @@ -170,7 +170,7 @@ func (s SessionStateAdapter) AddBackup(backup env.Remote) error {
return fmt.Errorf("%w: '%s' -> %s", env.ErrRemoteAddressConflict, bac.Name, bac.Url)
}

s.backups[backup.Name] = backup
s.backups.Set(backup.Name, backup)
repoState.AddBackup(backup)
return repoState.Save(fs)
}
Expand Down Expand Up @@ -202,11 +202,11 @@ func (s SessionStateAdapter) RemoveRemote(_ context.Context, name string) error
}

func (s SessionStateAdapter) RemoveBackup(_ context.Context, name string) error {
backup, ok := s.backups[name]
backup, ok := s.backups.Get(name)
if !ok {
return env.ErrBackupNotFound
}
delete(s.backups, backup.Name)
s.backups.Delete(backup.Name)

fs, err := s.session.Provider().FileSystemForDatabase(s.dbName)
if err != nil {
Expand All @@ -218,12 +218,12 @@ func (s SessionStateAdapter) RemoveBackup(_ context.Context, name string) error
return err
}

backup, ok = repoState.Backups[name]
backup, ok = repoState.Backups.Get(name)
if !ok {
// sanity check
return env.ErrBackupNotFound
}
delete(repoState.Backups, name)
repoState.Backups.Delete(name)
return repoState.Save(fs)
}

Expand Down

0 comments on commit 517e867

Please sign in to comment.