-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathondemand_agentmanager.go
More file actions
171 lines (136 loc) · 4.03 KB
/
ondemand_agentmanager.go
File metadata and controls
171 lines (136 loc) · 4.03 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package gocbcorex
import (
"context"
"crypto/tls"
"errors"
"sync"
"sync/atomic"
"go.uber.org/zap"
)
// OnDemandAgentManagerReconfigureOptions is the set of options available for reconfiguring an AgentManager.
type OnDemandAgentManagerReconfigureOptions struct {
TLSConfig *tls.Config
Authenticator Authenticator
}
// OnDemandAgentManagerOptions is the set of options available for creating a new OnDemandAgentManager.
type OnDemandAgentManagerOptions struct {
Logger *zap.Logger
TLSConfig *tls.Config
Authenticator Authenticator
SeedConfig SeedConfig
CompressionConfig CompressionConfig
ConfigPollerConfig ConfigPollerConfig
HTTPConfig HTTPConfig
DisableMetrics bool
}
// OnDemandAgentManager is responsible for managing a collection of Agent instances.
type OnDemandAgentManager struct {
opts OnDemandAgentManagerOptions
clusterAgent *Agent
fastMap atomic.Pointer[map[string]*Agent]
slowMap map[string]*Agent
slowLock sync.Mutex
stateLock sync.Mutex
closed bool
}
// CreateOnDemandAgentManager creates a new OnDemandAgentManager.
func CreateOnDemandAgentManager(ctx context.Context, opts OnDemandAgentManagerOptions) (*OnDemandAgentManager, error) {
m := &OnDemandAgentManager{
opts: opts,
slowMap: make(map[string]*Agent),
}
clusterAgent, err := m.makeAgent(ctx, "")
if err != nil {
return nil, err
}
m.clusterAgent = clusterAgent
return m, nil
}
func (m *OnDemandAgentManager) makeAgent(ctx context.Context, bucketName string) (*Agent, error) {
return CreateAgent(ctx, AgentOptions{
Logger: m.opts.Logger,
TLSConfig: m.opts.TLSConfig,
Authenticator: m.opts.Authenticator,
SeedConfig: m.opts.SeedConfig,
CompressionConfig: m.opts.CompressionConfig,
ConfigPollerConfig: m.opts.ConfigPollerConfig,
HTTPConfig: m.opts.HTTPConfig,
BucketName: bucketName,
DisableMetrics: m.opts.DisableMetrics,
})
}
// Reconfigure reconfigures the AgentManager, and underling Agent instances.
func (m *OnDemandAgentManager) Reconfigure(opts OnDemandAgentManagerReconfigureOptions) error {
return errors.New("not yet supported")
}
// GetClusterAgent returns the Agent which is not bound to any bucket.
func (m *OnDemandAgentManager) GetClusterAgent() (*Agent, error) {
m.stateLock.Lock()
if m.closed {
m.stateLock.Unlock()
return nil, errors.New("agent manager closed")
}
m.stateLock.Unlock()
return m.clusterAgent, nil
}
// GetBucketAgent returns the Agent bound to a specific bucket, creating a new Agent as required.
func (m *OnDemandAgentManager) GetBucketAgent(ctx context.Context, bucketName string) (*Agent, error) {
m.stateLock.Lock()
if m.closed {
m.stateLock.Unlock()
return nil, errors.New("agent manager closed")
}
m.stateLock.Unlock()
fastMap := m.fastMap.Load()
if fastMap != nil {
agent, ok := (*fastMap)[bucketName]
if ok {
return agent, nil
}
}
agent, err := m.getBucketAgentSlow(ctx, bucketName)
if err != nil {
return nil, err
}
return agent, nil
}
func (m *OnDemandAgentManager) getBucketAgentSlow(ctx context.Context, bucketName string) (*Agent, error) {
m.slowLock.Lock()
defer m.slowLock.Unlock()
if agent, ok := m.slowMap[bucketName]; ok {
return agent, nil
}
agent, err := m.makeAgent(ctx, bucketName)
if err != nil {
return nil, err
}
m.slowMap[bucketName] = agent
fastMap := make(map[string]*Agent, len(m.slowMap))
for name, agent := range m.slowMap {
fastMap[name] = agent
}
m.fastMap.Store(&fastMap)
return agent, nil
}
// Close closes the AgentManager and all underlying Agent instances.
func (m *OnDemandAgentManager) Close() error {
m.stateLock.Lock()
m.opts.Logger.Debug("Closing")
m.closed = true
m.fastMap.Store(nil)
firstErr := m.clusterAgent.Close()
m.slowLock.Lock()
agents := m.slowMap
m.slowMap = make(map[string]*Agent)
m.slowLock.Unlock()
m.clusterAgent = nil
m.stateLock.Unlock()
for _, agent := range agents {
err := agent.Close()
if err != nil && firstErr == nil {
firstErr = err
}
}
m.opts.Logger.Debug("Closed")
return firstErr
}