Skip to content

Commit

Permalink
Add a bunch of debug statements useful when debugging sync issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zozs committed Aug 9, 2021
1 parent 285ae89 commit 64d44b2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ async function main () {
})
}

process.on('unhandledRejection', error => {
console.error('==> Got unhandled rejection:', error.message)
})

main().catch(e => console.error(`top-level exception: ${e} json: ${JSON.stringify(e)}`))
7 changes: 7 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Debug from 'debug'
import Koa from 'koa'
import Router from '@koa/router'
import { addDebugRoutes } from './debug-routes.js'
import DistributedStorage from './distributed-storage.js'

const debug = Debug('whalesong:api')

Expand Down Expand Up @@ -111,8 +112,11 @@ export async function setupApp (storage, host, port, externalUrl) {

router.head('/v2/:org/:name/manifests/:tag', async (ctx) => {
const { org, name, tag } = ctx.params
debug(`Client requested (HEAD) (org,name,tag): (${org}, ${name}, ${tag})`)
const pubKey = await lookupOrg(ctx, org)
debug(`Found pubkey ${pubKey} from org ${org}`)
const { digest, size, contentType } = await storage.hasManifest(pubKey, name, tag)
debug(`hasManifest returned digest and size ${digest} size ${size}`)
if (digest != null) {
ctx.body = null
ctx.set('Docker-Content-Digest', digest)
Expand All @@ -127,8 +131,11 @@ export async function setupApp (storage, host, port, externalUrl) {

router.get('/v2/:org/:name/manifests/:tag', async (ctx) => {
const { org, name, tag } = ctx.params
debug(`Client requested (GET) (org,name,tag): (${org}, ${name}, ${tag})`)
const pubKey = await lookupOrg(ctx, org)
debug(`Found pubkey ${pubKey} from org ${org}`)
const { digest, stream, contentType } = await storage.getManifest(pubKey, name, tag)
debug(`getManifest returned digest ${digest} stream ${stream}`)
if (digest != null) {
console.debug(`Retrieving manifest with digest ${digest}`)
ctx.body = stream
Expand Down
4 changes: 2 additions & 2 deletions lib/distributed-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ async function setupHyperbeeClient (feed, pubKey, shouldReplicate) {
const corePath = path.join(await baseDir(), 'hypercores', feed)

// TODO: to sparse, or not to sparse, that is the question.
const core = hypercore(corePath, pubKey)
// const core = hypercore(corePath, pubKey, { sparse: true })
// const core = hypercore(corePath, pubKey)
const core = hypercore(corePath, pubKey, { sparse: true })

const bee = new Hyperbee(core, {
keyEncoding: 'utf-8',
Expand Down
9 changes: 9 additions & 0 deletions lib/distributed-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class DistributedStorage {
blobStore (org) {
// returns the blob sub-database for the hyperbee for a given organization if it exists, otherwise nullish
const bee = this.hyperbees.get(org)
debug(`returning blob store for org ${org}, got bee ${bee}}`)
return bee?.sub('blob')
}

manifestStore (org, name) {
// returns the manifest sub-database for the hyperbee for a given organization if it exists, otherwise nullish
debug(`returning manifest store for org ${org}`)
const bee = this.hyperbees.get(org)
return bee?.sub('manifest').sub(name)
}
Expand All @@ -46,16 +48,19 @@ class DistributedStorage {
const blobs = this.blobStore(org)
// TODO: should we make an initial sync here too just like for manifests if org. does not exist?
if (blobs) {
debug(`getting blob ${dockerDigest}`)
const blobObjectNode = await blobs.get(dockerDigest)
const blobObject = blobObjectNode?.value

debug(`blob lookup resulted in ${JSON.stringify(blobObject)}`)
if (!blobObject) {
return { stream: null, size: null, contentType: null }
}

const { contentType, size } = blobObject
const ipfsCid = blobObject.locations[0].ref // currently, support only ipfs cid and a single location for every blob

debug(`blob lookup yielded ipfs cid ${ipfsCid}`)
if (!ipfsCid) {
return { stream: null, size: null, contentType: null }
}
Expand Down Expand Up @@ -102,7 +107,9 @@ class DistributedStorage {

async hasBlob (org, name, digest) {
const blobs = this.blobStore(org)
debug(`trying to check if blob ${digest} exists`)
const blobObjectNode = await blobs?.get(digest)
debug(`blob object node is ${JSON.stringify(blobObjectNode)}`)
if (blobObjectNode) {
const { contentType, size } = blobObjectNode?.value
return { contentType, size }
Expand Down Expand Up @@ -177,9 +184,11 @@ class DistributedStorage {
debug(`got manifest for ${org}/${name}:${tagOrDigest} returned digest ${manifestDigest}`)
}

debug(`trying to get manifestDigest ${manifestDigest}`)
if (manifestDigest) {
// We now know the digest to use. Grab it!
const { stream, size, contentType } = await this.getBlob(org, name, manifestDigest)
debug(`got blob with size ${size}`)
if (stream != null) {
return { digest: manifestDigest, stream, size, contentType }
}
Expand Down

0 comments on commit 64d44b2

Please sign in to comment.