-
Notifications
You must be signed in to change notification settings - Fork 495
/
mongo.go
494 lines (419 loc) · 15.1 KB
/
mongo.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mongo
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/mgo/v3"
"github.com/juju/replicaset/v3"
"github.com/juju/retry"
"github.com/juju/utils/v3"
"github.com/juju/juju/core/base"
"github.com/juju/juju/core/network"
coreos "github.com/juju/juju/core/os"
"github.com/juju/juju/packaging"
"github.com/juju/juju/packaging/dependency"
"github.com/juju/juju/service/common"
"github.com/juju/juju/service/snap"
"github.com/juju/juju/service/systemd"
)
var logger = loggo.GetLogger("juju.mongo")
// StorageEngine represents the storage used by mongo.
type StorageEngine string
const (
// JujuDbSnap is the snap of MongoDB that Juju uses.
JujuDbSnap = "juju-db"
// WiredTiger is a storage type introduced in 3
WiredTiger StorageEngine = "wiredTiger"
)
// JujuDbSnapMongodPath is the path that the juju-db snap
// makes mongod available at
var JujuDbSnapMongodPath = "/snap/bin/juju-db.mongod"
// WithAddresses represents an entity that has a set of
// addresses. e.g. a state Machine object
type WithAddresses interface {
Addresses() network.SpaceAddresses
}
// IsMaster returns a boolean that represents whether the given
// machine's peer address is the primary mongo host for the replicaset
var IsMaster = isMaster
func isMaster(session *mgo.Session, obj WithAddresses) (bool, error) {
addrs := obj.Addresses()
masterHostPort, err := replicaset.MasterHostPort(session)
// If the replica set has not been configured, then we
// can have only one master and the caller must
// be that master.
if err == replicaset.ErrMasterNotConfigured {
return true, nil
}
if err != nil {
return false, err
}
masterAddr, _, err := net.SplitHostPort(masterHostPort)
if err != nil {
return false, err
}
for _, addr := range addrs {
if addr.Value == masterAddr {
return true, nil
}
}
return false, nil
}
// SelectPeerAddress returns the address to use as the mongo replica set peer
// address by selecting it from the given addresses.
// If no addresses are available an empty string is returned.
func SelectPeerAddress(addrs network.ProviderAddresses) string {
// The second bool result is ignored intentionally (we return an empty
// string if no suitable address is available.)
addr, _ := addrs.OneMatchingScope(network.ScopeMatchCloudLocal)
return addr.Value
}
// GenerateSharedSecret generates a pseudo-random shared secret (keyfile)
// for use with Mongo replica sets.
func GenerateSharedSecret() (string, error) {
// "A key’s length must be between 6 and 1024 characters and may
// only contain characters in the base64 set."
// -- http://docs.mongodb.org/manual/tutorial/generate-key-file/
buf := make([]byte, base64.StdEncoding.DecodedLen(1024))
if _, err := rand.Read(buf); err != nil {
return "", fmt.Errorf("cannot read random secret: %v", err)
}
return base64.StdEncoding.EncodeToString(buf), nil
}
/*
Values set as per bug:
https://bugs.launchpad.net/juju/+bug/1656430
net.ipv4.tcp_max_syn_backlog = 4096
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 1000
net.ipv4.tcp_fin_timeout = 30
Values set as per mongod recommendation (see syslog on default mongod run)
/sys/kernel/mm/transparent_hugepage/enabled 'always' > 'never'
/sys/kernel/mm/transparent_hugepage/defrag 'always' > 'never'
*/
// TODO(bootstrap): tweaks this to mongo OCI image.
var mongoKernelTweaks = map[string]string{
"/sys/kernel/mm/transparent_hugepage/enabled": "never",
"/sys/kernel/mm/transparent_hugepage/defrag": "never",
"/proc/sys/net/ipv4/tcp_max_syn_backlog": "4096",
"/proc/sys/net/core/somaxconn": "16384",
"/proc/sys/net/core/netdev_max_backlog": "1000",
"/proc/sys/net/ipv4/tcp_fin_timeout": "30",
}
// NewMemoryProfile returns a Memory Profile from the passed value.
func NewMemoryProfile(m string) (MemoryProfile, error) {
mp := MemoryProfile(m)
if err := mp.Validate(); err != nil {
return MemoryProfile(""), err
}
return mp, nil
}
// MemoryProfile represents a type of meory configuration for Mongo.
type MemoryProfile string
// String returns a string representation of this profile value.
func (m MemoryProfile) String() string {
return string(m)
}
func (m MemoryProfile) Validate() error {
if m != MemoryProfileLow && m != MemoryProfileDefault {
return errors.NotValidf("memory profile %q", m)
}
return nil
}
const (
// MemoryProfileLow will use as little memory as possible in mongo.
MemoryProfileLow MemoryProfile = "low"
// MemoryProfileDefault will use mongo config ootb.
MemoryProfileDefault MemoryProfile = "default"
)
// EnsureServerParams is a parameter struct for EnsureServer.
type EnsureServerParams struct {
// APIPort is the port to connect to the api server.
APIPort int
// StatePort is the port to connect to the mongo server.
StatePort int
// Cert is the certificate.
Cert string
// PrivateKey is the certificate's private key.
PrivateKey string
// CAPrivateKey is the CA certificate's private key.
CAPrivateKey string
// SharedSecret is a secret shared between mongo servers.
SharedSecret string
// SystemIdentity is the identity of the system.
SystemIdentity string
// DataDir is the machine agent data directory.
DataDir string
// ConfigDir is where mongo config goes.
ConfigDir string
// Namespace is the machine agent's namespace, which is used to
// generate a unique service name for Mongo.
Namespace string
// OplogSize is the size of the Mongo oplog.
// If this is zero, then EnsureServer will
// calculate a default size according to the
// algorithm defined in Mongo.
OplogSize int
// SetNUMAControlPolicy preference - whether the user
// wants to set the numa control policy when starting mongo.
SetNUMAControlPolicy bool
// MemoryProfile determines which value is going to be used by
// the cache and future memory tweaks.
MemoryProfile MemoryProfile
// The channel for installing the mongo snap in focal and later.
JujuDBSnapChannel string
}
// EnsureServerInstalled ensures that the MongoDB server is installed,
// configured, and ready to run.
func EnsureServerInstalled(ctx context.Context, args EnsureServerParams) error {
return ensureServer(ctx, args, mongoKernelTweaks)
}
func ensureServer(ctx context.Context, args EnsureServerParams, mongoKernelTweaks map[string]string) (err error) {
tweakSysctlForMongo(mongoKernelTweaks)
mongoDep := dependency.Mongo(args.JujuDBSnapChannel)
if args.DataDir == "" {
args.DataDir = dataPathForJujuDbSnap
}
if args.ConfigDir == "" {
args.ConfigDir = systemd.EtcSystemdDir
}
logger.Infof(
"Ensuring mongo server is running; data directory %s; port %d",
args.DataDir, args.StatePort,
)
if err := setupDataDirectory(args); err != nil {
return errors.Annotatef(err, "cannot set up data directory")
}
// TODO(wallyworld) - set up Numactl if requested in args.SetNUMAControlPolicy
svc, err := mongoSnapService(args.DataDir, args.ConfigDir, args.JujuDBSnapChannel)
if err != nil {
return errors.Annotatef(err, "cannot create mongo snap service")
}
hostBase, err := coreos.HostBase()
if err != nil {
return errors.Annotatef(err, "cannot get host base")
}
if err := installMongod(mongoDep, hostBase, svc); err != nil {
return errors.Annotatef(err, "cannot install mongod")
}
finder := NewMongodFinder()
mongoPath, err := finder.InstalledAt()
if err != nil {
return errors.Annotatef(err, "unable to find mongod install path")
}
logVersion(mongoPath)
oplogSizeMB := args.OplogSize
if oplogSizeMB == 0 {
oplogSizeMB, err = defaultOplogSize(dbDir(args.DataDir))
if err != nil {
return errors.Annotatef(err, "unable to calculate default oplog size")
}
}
mongoArgs := generateConfig(oplogSizeMB, args)
// Update snap configuration.
// TODO(tsm): refactor out to service.Configure
err = mongoArgs.writeConfig(configPath(args.DataDir))
if err != nil {
return errors.Annotatef(err, "unable to write config")
}
if err := snap.SetSnapConfig(ServiceName, "configpath", configPath(args.DataDir)); err != nil {
return errors.Annotatef(err, "unable to set snap config")
}
// Update the systemd service configuration.
if err := svc.ConfigOverride(); err != nil {
return errors.Annotatef(err, "unable to update systemd service configuration")
}
// Ensure the mongo service is running, after we've installed and
// configured it.
// We do this in two retry loops. The outer loop, will try and start
// the service repeatedly over the span of 5 minutes. The inner loop will
// try and ensure that the service is running over the span of 10 seconds.
// If the service is running, then it will return nil, causing the outer
// loop to complete. If the service is not running, and the inner retry loop
// has been exhausted, then the outer loop will attempt to start the service
// again after a delay.
// If the mongo service is not installed, then nothing we do here, will
// cause the service to start. So we will just return the error.
return retry.Call(retry.CallArgs{
Func: func() error {
if err := svc.Start(); err != nil {
logger.Debugf("cannot start mongo service: %v", err)
}
return ensureMongoServiceRunning(ctx, svc)
},
IsFatalError: func(err error) bool {
// If the service is not installed, then we should attempt
// to install it again, by bouncing.
return errors.Cause(err) == ErrMongoServiceNotInstalled
},
NotifyFunc: func(err error, attempt int) {
logger.Debugf("attempt %d to start mongo service: %v", attempt, err)
},
Stop: ctx.Done(),
Attempts: -1,
Delay: 10 * time.Second,
MaxDelay: 1 * time.Minute,
MaxDuration: time.Minute * 5,
BackoffFunc: retry.DoubleDelay,
Clock: clock.WallClock,
})
}
const (
// ErrMongoServiceNotInstalled is returned when the mongo service is not
// installed.
ErrMongoServiceNotInstalled = errors.ConstError("mongo service not installed")
// ErrMongoServiceNotRunning is returned when the mongo service is not
// running.
ErrMongoServiceNotRunning = errors.ConstError("mongo service not running")
)
func ensureMongoServiceRunning(ctx context.Context, svc MongoSnapService) error {
return retry.Call(retry.CallArgs{
Func: func() error {
running, err := svc.Running()
if err != nil {
// If the service is not installed, then we should attempt
// to install it.
return errors.Annotatef(ErrMongoServiceNotInstalled, err.Error())
}
if running {
return nil
}
return ErrMongoServiceNotRunning
},
Stop: ctx.Done(),
Attempts: 10,
Delay: 1 * time.Second,
Clock: clock.WallClock,
})
}
func setupDataDirectory(args EnsureServerParams) error {
dbDir := dbDir(args.DataDir)
if err := os.MkdirAll(dbDir, 0700); err != nil {
return errors.Annotate(err, "cannot create mongo database directory")
}
// TODO(fix): rather than copy, we should ln -s coz it could be changed later!!!
if err := UpdateSSLKey(args.DataDir, args.Cert, args.PrivateKey); err != nil {
return errors.Trace(err)
}
err := utils.AtomicWriteFile(sharedSecretPath(args.DataDir), []byte(args.SharedSecret), 0600)
if err != nil {
return errors.Annotatef(err, "cannot write mongod shared secret to %v", sharedSecretPath(args.DataDir))
}
if err := os.MkdirAll(logPath(dbDir), 0755); err != nil {
return errors.Annotate(err, "cannot create mongodb logging directory")
}
return nil
}
func truncateAndWriteIfExists(procFile, value string) error {
if _, err := os.Stat(procFile); os.IsNotExist(err) {
logger.Debugf("%q does not exist, will not set %q", procFile, value)
return errors.Errorf("%q does not exist, will not set %q", procFile, value)
}
f, err := os.OpenFile(procFile, os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return errors.Trace(err)
}
defer f.Close()
_, err = f.WriteString(value)
return errors.Trace(err)
}
func tweakSysctlForMongo(editables map[string]string) {
for editableFile, value := range editables {
if err := truncateAndWriteIfExists(editableFile, value); err != nil {
logger.Errorf("could not set the value of %q to %q because of: %v\n", editableFile, value, err)
}
}
}
// UpdateSSLKey writes a new SSL key used by mongo to validate connections from Juju controller(s)
func UpdateSSLKey(dataDir, cert, privateKey string) error {
err := utils.AtomicWriteFile(sslKeyPath(dataDir), []byte(GenerateSSLKey(cert, privateKey)), 0600)
return errors.Annotate(err, "cannot write SSL key")
}
// GenerateSSLKey combines cert and private key to generate the ssl key - server.pem.
func GenerateSSLKey(cert, privateKey string) string {
return cert + "\n" + privateKey
}
func logVersion(mongoPath string) {
cmd := exec.Command(mongoPath, "--version")
output, err := cmd.CombinedOutput()
if err != nil {
logger.Infof("failed to read the output from %s --version: %v", mongoPath, err)
return
}
logger.Debugf("using mongod: %s --version:\n%s", mongoPath, output)
}
func mongoSnapService(dataDir, configDir, snapChannel string) (MongoSnapService, error) {
snapName := JujuDbSnap
jujuDbLocalSnapPattern := regexp.MustCompile(`juju-db_[0-9]+\.snap`)
// If we're installing a local snap, then provide an absolute path
// as a snap <name>. snap install <name> will then do the Right Thing (TM).
files, err := os.ReadDir(path.Join(dataDir, "snap"))
if err == nil {
for _, fullFileName := range files {
_, fileName := path.Split(fullFileName.Name())
if jujuDbLocalSnapPattern.MatchString(fileName) {
snapName = fullFileName.Name()
}
}
}
backgroundServices := []snap.BackgroundService{
{
Name: "daemon",
EnableAtStartup: true,
},
}
conf := common.Conf{
Desc: ServiceName + " snap",
Limit: mongoULimits,
}
svc, err := newSnapService(
snapName, ServiceName, conf, snap.Command, configDir, snapChannel, "", backgroundServices, []snap.Installable{})
return svc, errors.Trace(err)
}
// Override for testing.
var installMongo = packaging.InstallDependency
func installMongod(mongoDep packaging.Dependency, hostBase base.Base, snapSvc MongoSnapService) error {
// Do either a local snap install or a real install from the store.
if snapSvc.Name() == ServiceName {
// Store snap.
return installMongo(mongoDep, hostBase)
} else {
// Local snap.
return snapSvc.Install()
}
}
// dbDir returns the dir where mongo storage is.
func dbDir(dataDir string) string {
return filepath.Join(dataDir, "db")
}
// MongoSnapService represents a mongo snap.
type MongoSnapService interface {
Exists() (bool, error)
Installed() (bool, error)
Running() (bool, error)
ConfigOverride() error
Name() string
Start() error
Restart() error
Install() error
}
var newSnapService = func(mainSnap, serviceName string, conf common.Conf, snapPath, configDir, channel string, confinementPolicy snap.ConfinementPolicy, backgroundServices []snap.BackgroundService, prerequisites []snap.Installable) (MongoSnapService, error) {
return snap.NewService(mainSnap, serviceName, conf, snapPath, configDir, channel, confinementPolicy, backgroundServices, prerequisites)
}
// CurrentReplicasetConfig is overridden in tests.
var CurrentReplicasetConfig = func(session *mgo.Session) (*replicaset.Config, error) {
return replicaset.CurrentConfig(session)
}