Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blockstore: lock getStream to avoid race issues #85

Merged
merged 1 commit into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"ipfs-block": "^0.3.0",
"lock": "^0.1.3",
"multihashes": "^0.2.2",
"pull-defer": "^0.2.2",
"pull-stream": "^3.4.5",
"pull-through": "^1.0.18",
"pull-write": "^1.1.0",
"run-parallel": "^1.1.6",
"run-series": "^1.1.4",
Expand All @@ -66,4 +66,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>"
]
}
}
41 changes: 26 additions & 15 deletions src/stores/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const base32 = require('base32.js')
const path = require('path')
const write = require('pull-write')
const parallel = require('run-parallel')
const through = require('pull-through')
const defer = require('pull-defer/source')

const PREFIX_LENGTH = 5

Expand Down Expand Up @@ -52,19 +52,25 @@ exports.setUp = (basePath, BlobStore, locks) => {
}

const p = multihashToPath(key, extension)
const deferred = defer()

lock(p, (release) => {
const ext = extension === 'data' ? 'protobuf' : extension
pull(
store.read(p),
pull.collect(release((err, data) => {
if (err) {
return deferred.abort(err)
}

deferred.resolve(pull.values([
new Block(Buffer.concat(data), ext)
]))
}))
)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do streams -> buffer everything -> streams?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update: nvm. It is because of interface-pull-blob-store only streams interface. We should consider to have a sync interface too on the blob-store.


const ext = extension === 'data' ? 'protobuf' : extension
let data = []

return pull(
store.read(p),
through(function (values) {
data = data.concat(values)
}, function () {
this.queue(new Block(Buffer.concat(data), ext))
this.queue(null)
})
)
return deferred
},

putStream () {
Expand All @@ -75,7 +81,10 @@ exports.setUp = (basePath, BlobStore, locks) => {
const sink = write((blocks, cb) => {
parallel(blocks.map((block) => (cb) => {
writeBlock(block, (err, meta) => {
if (err) return cb(err)
if (err) {
return cb(err)
}

if (push) {
const read = push
push = null
Expand All @@ -94,7 +103,9 @@ exports.setUp = (basePath, BlobStore, locks) => {

const source = (end, cb) => {
if (end) ended = end
if (ended) return cb(ended)
if (ended) {
return cb(ended)
}

if (written.length) {
return cb(null, written.shift())
Expand Down
2 changes: 1 addition & 1 deletion test/blockstore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module.exports = (repo) => {

it('massive read', (done) => {
parallel(_.range(20 * 100).map((i) => (cb) => {
const j = i % 20
const j = i % blockCollection.length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value? This is iterating over the earlier put blockCollection doing parallel reads

pull(
repo.blockstore.getStream(blockCollection[j].key),
pull.collect((err, meta) => {
Expand Down