-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathpool.go
100 lines (82 loc) · 2.4 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
2020 © Postgres.ai
*/
package resources
import (
"path"
"sync"
"time"
"gitlab.com/postgres-ai/database-lab/v3/pkg/util"
)
// PoolStatus represents a pool status.
type PoolStatus string
const (
// ActivePool defines an active pool status.
ActivePool PoolStatus = "active"
// RefreshingPool defines the status of a pool when data retrieval in progress.
RefreshingPool PoolStatus = "refreshing"
// EmptyPool defines the status of an inactive pool.
EmptyPool PoolStatus = "empty"
)
// Pool describes a storage pool.
type Pool struct {
Name string
Mode string
DSA time.Time
PoolDirName string
MountDir string
CloneSubDir string
DataSubDir string
SocketSubDir string
ObserverSubDir string
mu sync.RWMutex
status PoolStatus
}
// NewPool creates a new Pool.
func NewPool(name string) *Pool {
return &Pool{Name: name, status: EmptyPool}
}
// IsEmpty checks if Pool is empty.
func (p *Pool) IsEmpty() bool {
return p.Name == "" && p.Mode == ""
}
// SetDSA sets a dataStateAt value.
func (p *Pool) SetDSA(dsa time.Time) {
p.DSA = dsa
}
// DataDir returns a path to the data directory of the storage pool.
func (p *Pool) DataDir() string {
return path.Join(p.MountDir, p.PoolDirName, p.DataSubDir)
}
// SocketDir returns a path to the sockets directory of the storage pool.
func (p *Pool) SocketDir() string {
return path.Join(p.MountDir, p.PoolDirName, p.SocketSubDir)
}
// ObserverDir returns a path to the observer directory of the storage pool.
func (p *Pool) ObserverDir(port uint) string {
return path.Join(p.ClonePath(port), p.ObserverSubDir)
}
// ClonesDir returns a path to the clones directory of the storage pool.
func (p *Pool) ClonesDir() string {
return path.Join(p.MountDir, p.PoolDirName, p.CloneSubDir)
}
// ClonePath returns a path to the initialized clone directory.
func (p *Pool) ClonePath(port uint) string {
return path.Join(p.MountDir, p.PoolDirName, p.CloneSubDir, util.GetCloneName(port), p.DataSubDir)
}
// SocketCloneDir returns a path to the socket clone directory.
func (p *Pool) SocketCloneDir(name string) string {
return path.Join(p.SocketDir(), name)
}
// Status gets the pool status.
func (p *Pool) Status() PoolStatus {
p.mu.RLock()
defer p.mu.RUnlock()
return p.status
}
// SetStatus sets a status to the pool.
func (p *Pool) SetStatus(status PoolStatus) {
p.mu.Lock()
p.status = status
p.mu.Unlock()
}