Skip to content

Conversation

@oskarhane
Copy link
Member

@oskarhane oskarhane commented Jun 11, 2018

This introduces a web worker pool (generic so it can be used for other expensive computations as well).
Currently the pool size for cypher queries is hard coded in https://github.com/neo4j/neo4j-browser/pull/782/files#diff-046ce42896ad79b95a44f41c7faa1f94R39

This could possibly be a user config in the future, if we see the need to for it.
If no worker is free, the jobs are queued and executed in order.

This can be tested by setting the max pool size to 1 and execute a long running query (this takes about 10 secs), like

MATCH (n:Movie)<--()-->(m:Movie)<--()-->(o:Movie)<--()-->(p:Movie)<--()-->(q:Movie)
WITH n,p,count(*) as c
RETURN count(*)

on the movie data set and directly afterwards execute a few more. These will then wait with a spinner.

In the blob directory you'll only see one ww blob.
I've set the hard coded pool size to 10 for no particular reason. I've never hit that limit if I'm not hammering the keyboard with queries.

Try it

Open browser, open devtools -> Application.
Expand Other under Frames.
See all the uuid named blobs. That's ww (to verify, right click and show in network tab).

  1. Reload neo4j-browser
  2. Click a few meta items in sidebar (or run any queries). Wait for about 30 secs.
  3. Compare results with and without the changes in this pr.

Before

oskarhane-mbpt 2018-06-11 at 13 17 27

After

oskarhane-mbpt 2018-06-11 at 13 15 25

changelog: Fix issue where neo4j-browser kept allocating more and more disk space

Make them long running
@reznord
Copy link
Contributor

reznord commented Jun 11, 2018

This is a huge improvement and saves a lot of disk space

@oskarhane oskarhane changed the title Long running web workers Pool and re-use web workers Jun 12, 2018
Since it’s general purpose and not bolt specific
@oskarhane
Copy link
Member Author

oskarhane commented Jun 13, 2018

For future reference: I've rewritten the original (PoC) implementation to a full blown web worker pool and focus the api on the work that are to be done rather than the web workers themselves.

const createWorker = () => /* ... your create worker function */
const maxWorkers = 10
const pool = new WorkPool(createWorker, maxWorkers)

const payload = {type: 'TEST', data: {x: 1}}

const work = pool.doWork({id: 1, payload, onmessage: (msg) => {
  // Handle response
  work.finish() // You need to manually finish the work to release the worker
}})

// additional tricks
const work = pool.getWorkById(1) // Get access to work
work.finish() // cancels if it's in queue
work.onFinish(() => { console.log('finishing now') }) // Set a function to be called once worker is done
work.execute({type: 'SECOND_MESSAGE', data: {x: 2}}) // Post additional message to the worker that's doing the `work`

let connectionProperties = null
let boltWorkerRegister = {}
let cancellationRegister = {}
let boltWorkPool = new WorkPool(() => new BoltWorkerModule(), 10)
Copy link
Contributor

@pe4cey pe4cey Jun 13, 2018

Choose a reason for hiding this comment

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

FYI if we choose to surface the worker pool as :config in the future we will probably need to only allow worker pools > 0 as a pool of 0 is always in a BUSY state

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, 0 would be infinite imo.
New ones won't be recreated as long as there are existing free ones anyway.

@pe4cey pe4cey merged commit 949a039 into neo4j:master Jun 13, 2018
@oskarhane oskarhane deleted the long-running-ww branch June 13, 2018 12:07
myzero1 pushed a commit to myzero1/neo4j-browser that referenced this pull request May 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants