/
open.go
220 lines (194 loc) · 5.96 KB
/
open.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"runtime/pprof"
"strings"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/mgo/v3"
"github.com/juju/names/v5"
"github.com/juju/worker/v3"
"github.com/juju/juju/controller"
"github.com/juju/juju/state/cloudimagemetadata"
)
// Register the state tracker as a new profile.
var profileTracker = pprof.NewProfile("juju/state/tracker")
// OpenParams contains the parameters for opening the state database.
type OpenParams struct {
// Clock is the clock used for time-related operations.
Clock clock.Clock
// ControllerTag is the tag of the controller.
ControllerTag names.ControllerTag
// ControllerModelTag is the tag of the controller model.
ControllerModelTag names.ModelTag
// MongoSession is the mgo.Session to use for storing and
// accessing state data. The caller remains responsible
// for closing this session; Open will copy it.
MongoSession *mgo.Session
// NewPolicy, if non-nil, returns a policy which will be used to
// validate and modify behaviour of certain operations in state.
NewPolicy NewPolicyFunc
// RunTransactionObserver, if non-nil, is a function that will
// be called after mgo/txn transactions are run, successfully
// or not.
RunTransactionObserver RunTransactionObserverFunc
// InitDatabaseFunc, if non-nil, is a function that will be called
// just after the state database is opened.
InitDatabaseFunc InitDatabaseFunc
// MaxTxnAttempts is defaulted by OpenStatePool if otherwise not set.
MaxTxnAttempts int
// WatcherPollInterval is defaulted by the TxnWatcher if otherwise not set.
WatcherPollInterval time.Duration
}
// Validate validates the OpenParams.
func (p OpenParams) Validate() error {
if p.Clock == nil {
return errors.NotValidf("nil Clock")
}
if p.ControllerTag == (names.ControllerTag{}) {
return errors.NotValidf("empty ControllerTag")
}
if p.ControllerModelTag == (names.ModelTag{}) {
return errors.NotValidf("empty ControllerModelTag")
}
if p.MongoSession == nil {
return errors.NotValidf("nil MongoSession")
}
return nil
}
// OpenController connects to the server with the given parameters, waits for it
// to be initialized, and returns a new Controller instance.
//
// OpenController returns unauthorizedError if access is unauthorized.
func OpenController(args OpenParams) (*Controller, error) {
pool, err := OpenStatePool(args)
if err != nil {
return nil, errors.Trace(err)
}
return &Controller{
pool: pool,
ownsPool: true,
}, nil
}
func open(
controllerTag names.ControllerTag,
controllerModelTag names.ModelTag,
session *mgo.Session,
initDatabase InitDatabaseFunc,
controllerConfig *controller.Config,
newPolicy NewPolicyFunc,
clock clock.Clock,
runTransactionObserver RunTransactionObserverFunc,
maxTxnAttempts int,
) (*State, error) {
st, err := newState(controllerTag,
controllerModelTag,
controllerModelTag,
session,
newPolicy,
clock,
runTransactionObserver,
maxTxnAttempts)
if err != nil {
return nil, errors.Trace(err)
}
if initDatabase != nil {
if err := initDatabase(session, controllerModelTag.Id(), controllerConfig); err != nil {
session.Close()
return nil, errors.Trace(err)
}
logger.Debugf("mongodb initialised")
}
return st, nil
}
// newState creates an incomplete *State, with no running workers.
// You must startWorkers() the returned *State before it will function correctly.
// modelTag is used to filter all queries and transactions.
//
// newState takes responsibility for the supplied *mgo.Session, and will
// close it if it cannot be returned under the aegis of a *State.
func newState(
controllerTag names.ControllerTag,
modelTag, controllerModelTag names.ModelTag,
session *mgo.Session,
newPolicy NewPolicyFunc,
clock clock.Clock,
runTransactionObserver RunTransactionObserverFunc,
maxTxnAttempts int,
) (_ *State, err error) {
defer func() {
if err != nil {
session.Close()
}
}()
info, err := session.BuildInfo()
if err != nil {
return nil, errors.Trace(err)
}
if len(info.VersionArray) < 1 || info.VersionArray[0] < 4 {
parts := make([]string, len(info.VersionArray))
for i, v := range info.VersionArray {
parts[i] = fmt.Sprintf("%d", v)
}
mongoVers := "this mongo version"
if len(parts) > 0 {
mongoVers = fmt.Sprintf("mongo version %v", strings.Join(parts, "."))
}
return nil, errors.Errorf("%v does not support server side transactions", mongoVers)
}
mongodb := session.DB(jujuDB)
db := &database{
raw: mongodb,
schema: allCollections(),
modelUUID: modelTag.Id(),
runTransactionObserver: runTransactionObserver,
clock: clock,
maxTxnAttempts: maxTxnAttempts,
}
// Create State.
st := &State{
stateClock: clock,
modelTag: modelTag,
controllerModelTag: controllerModelTag,
session: session,
database: db,
newPolicy: newPolicy,
runTransactionObserver: runTransactionObserver,
maxTxnAttempts: maxTxnAttempts,
}
if newPolicy != nil {
st.policy = newPolicy(st)
}
// Record this State instance with the global tracker.
profileTracker.Add(st, 1)
st.controllerTag = controllerTag
logger.Infof("creating cloud image metadata storage")
st.CloudImageMetadataStorage = cloudimagemetadata.NewStorage(
cloudimagemetadataC,
&environMongo{st},
)
return st, nil
}
// Close the connection to the database.
func (st *State) Close() (err error) {
defer errors.DeferredAnnotatef(&err, "closing state failed")
if err := st.stopWorkers(); err != nil {
return errors.Trace(err)
}
st.session.Close()
logger.Debugf("closed state without error")
// Remove the reference.
profileTracker.Remove(st)
return nil
}
func (st *State) stopWorkers() (err error) {
if st.workers != nil {
if err := worker.Stop(st.workers); err != nil {
return errors.Annotatef(err, "failed to stop workers")
}
}
return nil
}