-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.js
230 lines (203 loc) · 7.64 KB
/
init.js
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
/*
////////////////////////////////////////////////////////////////////
:: Spatial Join for GeoJson features stored in MongoDB collection ::
Joins attributes from outer layer features to the inner layer
features contained within each respective outer layer feature.
Author: Jaskirat R.
Email: [email protected]
Example:
node init.js --db 'mongodb://localhost:27017/db' \
--innerLayer buildings --outerLayer lots \
--outputLayer buildings_spatialJoin \
--outerLayerAttributes 'attribute-a' 'attribute-b' 'attribute-c'
////////////////////////////////////////////////////////////////////
*/
const MongoClient = require('mongodb').MongoClient
const cluster = require('cluster')
const numCPUs = require('os').cpus().length
const async = require('async')
// Accept outerLayerAttributes as array. Provide the attributes seperated by spaces.
const argv = require('yargs').array('outerLayerAttributes').argv
const mongoUrl = argv.db // 'mongodb://localhost:27017/nyc'
const innerLayerCollection = argv.innerLayer // 'buildings'
const outerLayerCollection = argv.outerLayer // 'lots'
const outerLayerAttributes = argv.outerLayerAttributes// 'attribute-a' 'attribute-b' 'attribute-c'
const outputLayerCollection = argv.outputLayer // 'buildings_spatialJoin'
var workerBatchSize = 1000 // Size of features to be sent at once by master to workers for processing
var childProcessPid = [] // Keep track of Child Processes PIDs
var featureCursor
var db = null
var dbCollections = []
if (innerLayerCollection && outerLayerCollection && outputLayerCollection) {
if (innerLayerCollection === outerLayerCollection || outerLayerCollection === outputLayerCollection || innerLayerCollection === outputLayerCollection) {
console.error(`Error: Input different layer collection names`)
process.exit()
}
// Begin the join process
init()
} else {
console.error(`Invalid Arguments`)
console.log(`Example: node init.js --db 'mongodb://localhost:27017/db' --innerLayer buildings --outerLayer lots --outputLayer buildings_spatialJoin --outerLayerAttributes attribute-a attribute-b attribute-c`)
}
function init() {
if (cluster.isMaster) {
console.log(`MongoDB URL: ${mongoUrl}`)
console.log(`Inner Layer: ${innerLayerCollection}`)
console.log(`Outer Layer: ${outerLayerCollection}`)
console.log(`Output Layer: ${outputLayerCollection}`)
console.log(`Attributes to Join: ${outerLayerAttributes}`)
MongoClient.connect(mongoUrl)
.then(_db => {
console.log('Connected to DB')
console.time('spatialJoin')
db = _db // Make it available globally for Master
return db
})
.then((db) => {
return db.collection(outerLayerCollection).count()
})
.then((count) => {
// Adjust the worker batch size
adjustWorkerBatchSize(count)
// Also fetch all the collection names in the database
return db.listCollections().toArray()
})
.then(() => {
featureCursor = db.collection(outerLayerCollection).find()
})
.catch(err => {
console.error('Could not connect to DB ', err)
})
// Start workers
for (let i = 0; i < numCPUs; i++) {
var master = cluster.fork()
childProcessPid.push(master.process.pid)
}
// Message listener for Master from each worker
for (var id in cluster.workers) {
cluster.workers[id].on('message', (msg) => { messageHandler(msg) })
}
// Master queue to iterate over a batch of features
var qMaster = async.queue((worker, callback) => {
// Send Next Batch of Features
sendFeatureBatchToWorker(worker, () => {
callback()
})
}, 1)
qMaster.drain = function() {
// Do nothing
console.log('Master queue drained. Waiting for request from workers.')
}
// Handle incoming messages from workers
var messageHandler = function(msg) {
if (msg.action === 'request') {
// Determine which worker is requesting next batch of features
for (const id in cluster.workers) {
if (cluster.workers[id].process.pid === msg.pid) {
qMaster.push(cluster.workers[id], (err) => { if (err) console.error(err) })
}
}
}
}
}
if (cluster.isWorker) {
var dbWorker = null
MongoClient.connect(mongoUrl)
.then(_db => {
dbWorker = _db // Make it available globally within Worker
return null
})
.then(() => {
// Ask for first batch of features
console.log(`Worker process ${process.pid} started`)
process.send({ action: 'request', pid: process.pid })
})
.catch(err => { console.error('Could not connect to DB ', err) })
// Spatial join worker queue of concurrency 10
var qWorker = async.queue((outerLayerFeature, callback) => {
// -------------------------------------
// Find inner layer features and perform the join
// -------------------------------------
dbWorker.collection(innerLayerCollection).find({
'geometry': {
'$geoWithin': {
'$geometry': {
type: outerLayerFeature.geometry.type,
coordinates: outerLayerFeature.geometry.coordinates
}
}
}
}).toArray((err, innerLayerFeatures) => {
if (err) { console.error(`Error finding inner features: ${err}`) }
async.eachSeries(innerLayerFeatures, (innerLayerFeature, callback) => {
// Output the join to outputLayerCollection
for (var i = 0; i < outerLayerAttributes.length; i++) {
innerLayerFeature.properties[outerLayerAttributes[i]] = outerLayerFeature.properties[outerLayerAttributes[i]]
}
delete innerLayerFeature._id
dbWorker.collection(outputLayerCollection).insert(innerLayerFeature, (err, res) => {
if (err) callback(err)
else callback() // Successful insert
})
}, () => {
callback() // Inserted all found features
})
})
// -------------------------------------
}, 10)
// Request master for more features
qWorker.drain = function() {
// Send a message back to master
console.log(process.pid, 'batch processed')
process.send({ action: 'request', pid: process.pid })
}
// Receive messages from the master process.
process.on('message', (msg) => {
if (msg.data) {
// Rx some data from master and add it to the queue
qWorker.push(msg.data, (err) => { if (err) console.error(`Worker Queue Error: ${err}`) })
}
})
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`)
})
}
// Send a batch of features to worker thread for processing
function sendFeatureBatchToWorker(worker, callback) {
var batchCount = 0
nextFeature(worker)
function nextFeature(worker) {
// console.log('worker ', worker.process.pid)
featureCursor.nextObject((err, feature) => {
if (!err && (feature !== null)) {
worker.send({ data: feature })
if (batchCount <= workerBatchSize) {
batchCount++
process.nextTick(() => {
nextFeature(worker)
})
} else {
callback() // End of batch
}
} else if (err) {
console.error(`Master Queue Error: ${err}`)
process.exit()
} else {
// TODO: Check if other workers are done. Only then quit the application
console.timeEnd('spatialJoin')
console.log('Master queue exhausted')
// process.exit()
}
})
}
}
function adjustWorkerBatchSize(count) {
if (count < 10) {
workerBatchSize = 1
} else if (count < 100) {
workerBatchSize = 10
} else {
workerBatchSize = 1000
}
}