Skip to content

Commit 649d77b

Browse files
committed
Assorted bug fixes:
* Removed keys from log files (replaced with discovery keys). * Periodically log mem/cpu usage * Set preview: false in repl
1 parent 76f8bab commit 649d77b

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const mkdirp = require('mkdirp')
55
const sub = require('subleveldown')
66
const grpc = require('@grpc/grpc-js')
77
const bjson = require('buffer-json-encoding')
8+
const processTop = require('process-top')
89
const Corestore = require('corestore')
910
const HypercoreCache = require('hypercore-cache')
1011
const SwarmNetworker = require('corestore-swarm-networking')
@@ -102,6 +103,7 @@ class HyperdriveDaemon extends EventEmitter {
102103

103104
// Set in start.
104105
this.server = null
106+
this._topTimer = null
105107
this._dbs = null
106108
this._isMain = !!opts.main
107109
this._cleanup = null
@@ -144,7 +146,6 @@ class HyperdriveDaemon extends EventEmitter {
144146

145147
const seed = this.corestore._deriveSecret(NAMESPACE, 'replication-keypair')
146148
const swarmId = this.corestore._deriveSecret(NAMESPACE, 'swarm-id')
147-
log.info({ swarmId: swarmId.toString('hex'), seed: seed.toString('hex') }, 'creating replication keypair and swarm ID')
148149
this._networkOpts.keyPair = HypercoreProtocol.keyPair(seed)
149150
this._networkOpts.id = swarmId
150151

@@ -309,6 +310,10 @@ class HyperdriveDaemon extends EventEmitter {
309310
this._isClosed = true
310311

311312
try {
313+
if (this._topTimer) {
314+
clearInterval(this._topTimer)
315+
this._topTimer = null
316+
}
312317
if (this.server) this.server.forceShutdown()
313318
log.info('waiting for fuse to unmount')
314319
if (this.fuse && this.fuse.fuseConfigured) await this.fuse.unmount()
@@ -340,6 +345,10 @@ class HyperdriveDaemon extends EventEmitter {
340345

341346
async start () {
342347
await this.ready()
348+
this._topTimer = setInterval(() => {
349+
log.info(processTop().toJSON(), 'process stats')
350+
}, 1000 * 60)
351+
343352
this.server = new grpc.Server()
344353

345354
this.server.addService(rpc.fuse.services.FuseService, {

lib/debug/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = class DebugManager extends EventEmitter {
3434
const r = repl.start({
3535
input: pumpify(call, inputDecoder),
3636
output: pumpify.obj(outputEncoder, call),
37-
preview: true,
37+
preview: false,
3838
terminal: true,
3939
completer: line => {
4040
const keys = Object.keys(r.context)

lib/drives/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class DriveManager extends EventEmitter {
134134
if (!drive) return null
135135

136136
const driveKey = drive.key.toString('hex')
137+
const driveDKey = drive.discoveryKey.toString('hex')
137138
const driveSessions = this._sessionsByKey.get(driveKey)
138139
this._sessions.delete(id)
139140
const idx = driveSessions.indexOf(id)
@@ -142,12 +143,12 @@ class DriveManager extends EventEmitter {
142143
// If there are still active sessions, don't close the drive.
143144
if (driveSessions.length) return null
144145

145-
log.debug({ id, key: driveKey }, 'closing drive because all associated sessions have closed')
146+
log.debug({ id, discoveryKey: driveDKey }, 'closing drive because all associated sessions have closed')
146147
this._sessionsByKey.delete(driveKey)
147148

148149
// If a drive is closed in memory-only mode, its storage will be deleted, so don't actually close.
149150
if (this.memoryOnly) {
150-
log.debug({ id, key: driveKey }, 'aborting drive close because we\'re in memory-only mode')
151+
log.debug({ id, discoveryKey: driveDKey }, 'aborting drive close because we\'re in memory-only mode')
151152
return null
152153
}
153154

@@ -171,7 +172,7 @@ class DriveManager extends EventEmitter {
171172
return new Promise((resolve, reject) => {
172173
drive.close(err => {
173174
if (err) return reject(err)
174-
log.debug({ id, key: driveKey }, 'closed drive and cleaned up any remaining watchers')
175+
log.debug({ id, discoveryKey: driveDKey }, 'closed drive and cleaned up any remaining watchers')
175176
return resolve()
176177
})
177178
})
@@ -320,7 +321,7 @@ class DriveManager extends EventEmitter {
320321
const mountInfo = { version: trie.version }
321322
const mountKey = feed.key.toString('hex')
322323

323-
log.info({ key }, 'registering mountpoint in drive index')
324+
log.info({ discoveryKey: feed.discoveryKey.toString('hex') }, 'registering mountpoint in drive index')
324325
const parentConfig = (await this.getNetworkConfiguration(drive)) || initialConfig || {}
325326
const existingMountConfig = (await this.getNetworkConfiguration(feed)) || {}
326327
const mountConfig = {
@@ -407,11 +408,11 @@ class DriveManager extends EventEmitter {
407408
}
408409

409410
function configurationError (err) {
410-
log.error({ err, encodedKey }, 'network configuration error')
411+
log.error({ err, discoveryKey: encodedKey }, 'network configuration error')
411412
}
412413

413414
function configurationSuccess () {
414-
log.debug({ encodedKey }, 'network configuration succeeded')
415+
log.debug({ discoveryKey: encodedKey }, 'network configuration succeeded')
415416
}
416417
}
417418

@@ -977,7 +978,6 @@ class DriveManager extends EventEmitter {
977978
driveWatchers.splice(driveWatchers.indexOf(watcher), 1)
978979
if (!driveWatchers.length) self._watchers.delete(keyString)
979980
}
980-
if (err) log.error({ id, path, err }, 'watch stream errored')
981981
call.end()
982982
}
983983
},

lib/fuse/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class FuseManager extends EventEmitter {
6363
log.debug('attempting to refresh the root drive if it exists.')
6464
const rootDriveMeta = await this._getRootDriveInfo()
6565
const { opts = {}, mnt = constants.mountpoint } = rootDriveMeta || {}
66-
log.debug({ opts, mnt }, 'refreshing root mount on restart')
66+
log.debug('refreshing root mount on restart')
6767
await this.mount(mnt, opts)
6868
return true
6969
}
@@ -72,7 +72,7 @@ class FuseManager extends EventEmitter {
7272
log.debug('getting root drive metadata')
7373
try {
7474
const rootDriveMeta = await this.db.get('root-drive')
75-
log.debug({ rootDriveMeta }, 'got root drive metadata')
75+
log.debug('got root drive metadata')
7676
return rootDriveMeta
7777
} catch (err) {
7878
if (!err.notFound) throw err
@@ -259,7 +259,7 @@ class FuseManager extends EventEmitter {
259259

260260
try {
261261
const key = datEncoding.decode(match.groups.key)
262-
log.debug({ key: match.groups.key, filename }, 'opening stats file for drive')
262+
log.debug({ filename }, 'opening stats file for drive')
263263
const drive = await this.driveManager.get(key)
264264
var stats = null
265265

@@ -306,7 +306,7 @@ class FuseManager extends EventEmitter {
306306
if (!match.groups.key || !match.groups.filename) return cb(fuse.ENOENT)
307307
const filename = match.groups.filename
308308
if (filename !== 'networking.json' && filename !== 'storage.json') return cb(fuse.ENOENT)
309-
log.debug({ key: match.groups.key, filename }, 'closing stats file')
309+
log.debug({ filename }, 'closing stats file')
310310
return this._virtualFiles.close(...[...args, cb])
311311
}
312312

@@ -472,7 +472,7 @@ class FuseManager extends EventEmitter {
472472
mnt = mnt || constants.mountpoint
473473

474474
await ensureFuse()
475-
log.debug({ mnt, mountOpts }, 'mounting a drive')
475+
log.debug({ mnt }, 'mounting a drive')
476476

477477
// TODO: Stop using the hash field to pass this flag once hashes are supported.
478478
if (mnt === constants.mountpoint && (!mountOpts.hash || (mountOpts.hash.toString() !== 'force'))) {
@@ -490,7 +490,7 @@ class FuseManager extends EventEmitter {
490490
return mountSubdrive(relativePath, drive)
491491

492492
async function mountSubdrive (relativePath, drive) {
493-
log.debug({ key: drive.key.toString('hex') }, 'mounting a sub-hyperdrive')
493+
log.debug({ discoveryKey: drive.discoveryKey.toString('hex') }, 'mounting a sub-hyperdrive')
494494
mountOpts.uid = process.getuid()
495495
mountOpts.gid = process.getgid()
496496
return new Promise((resolve, reject) => {
@@ -502,7 +502,7 @@ class FuseManager extends EventEmitter {
502502
}
503503

504504
async function mountRoot (drive) {
505-
log.debug({ key: drive.key.toString('hex') }, 'mounting the root drive')
505+
log.debug({ discoveryKey: drive.discoveryKey.toString('hex') }, 'mounting the root drive')
506506

507507
const fuse = new HyperdriveFuse(drive, constants.mountpoint, {
508508
force: true,
@@ -514,7 +514,7 @@ class FuseManager extends EventEmitter {
514514
const wrappedHandlers = self._wrapHandlers(handlers)
515515
await fuse.mount(wrappedHandlers)
516516

517-
log.debug({ mnt, wrappedHandlers }, 'mounted the root drive')
517+
log.debug({ mnt }, 'mounted the root drive')
518518
mountOpts.key = drive.key
519519

520520
await self.db.put('root-drive', { mnt, opts: { ...mountOpts, key: datEncoding.encode(drive.key) } })
@@ -531,7 +531,7 @@ class FuseManager extends EventEmitter {
531531
if (err) log.error({ error: err }, 'mountpoint error')
532532
self.driveManager.createSession(null, feed.key)
533533
.then(() => {
534-
log.info({ key: feed.key.toString('hex') }, 'created session for mountpoint')
534+
log.info({ discoveryKey: feed.discoveryKey.toString('hex') }, 'created session for mountpoint')
535535
})
536536
.catch(err => {
537537
log.error({ error: err }, 'could not create session for mountpoint')
@@ -586,12 +586,12 @@ class FuseManager extends EventEmitter {
586586
// The corestore name is not very important here, since the initial drive will be discarded after mount.
587587
const drive = await this._createDrive(null, { ...this.opts, name: crypto.randomBytes(64).toString('hex') })
588588

589-
log.debug({ path, key: drive.key.toString('hex') }, 'mounting a drive at a path')
589+
log.debug({ path, discoveryKey: drive.discoveryKey.toString('hex') }, 'mounting a drive at a path')
590590
return new Promise((resolve, reject) => {
591591
const innerPath = path.slice(this._rootMnt.length)
592592
this._rootDrive.mount(innerPath, opts, err => {
593593
if (err) return reject(err)
594-
log.debug({ path, key: drive.key.toString('hex') }, 'drive mounted')
594+
log.debug({ path, discoveryKey: drive.discoveryKey.toString('hex') }, 'drive mounted')
595595
return resolve()
596596
})
597597
})

manager.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ async function start (opts = {}) {
6767
else if (Array.isArray(opts.bootstrap) && opts.bootstrap.length) args.push('--bootstrap', opts.bootstrap.join(','))
6868

6969
var interpreter = opts.interpreter || process.execPath
70-
var interpreterArgs = `--max-old-space-size=${opts.heapSize}`
71-
console.log('interpreterArgs:', interpreterArgs)
70+
var interpreterArgs = [`--max-old-space-size=${opts.heapSize}`]
7271
if (!IS_WINDOWS) {
7372
const execArg = [interpreter, interpreterArgs, script].concat(args).map(escapeStringArg).join(' ')
7473
args = ['-c', execArg]
@@ -98,7 +97,6 @@ async function start (opts = {}) {
9897
await fs.rename(constants.unstructuredLog, constants.unstructuredLog.replace('.log', '.old.log'))
9998
}
10099
} catch (err) {
101-
console.log('err:', err)
102100
// If the log file couldn't be rotated, it's OK.
103101
}
104102

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"peersockets": "^0.3.0",
6262
"pino": "^5.12.6",
6363
"pm2": "^4.2.1",
64+
"process-top": "^1.1.0",
6465
"pump": "^3.0.0",
6566
"pumpify": "^2.0.1",
6667
"random-access-memory": "^3.1.1",

0 commit comments

Comments
 (0)