-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
268 lines (217 loc) · 7.61 KB
/
index.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
var gcloud = require('gcloud');
// Override logging to easily omit logs during tests
var logger = require('./logger');
var self = {
/**
* Counts the number of words in the line.
*/
'worker': function(context, data) {
// We expect the data argument to contain a 'line' property.
var batch = data['batch'];
// Batch should be an array.
var count = 0;
for (var i = 0; i < batch.length; i++) {
var line = batch[i];
// Just split to count words.
count += line.split(/\s+/).length;
}
// Create a pubsub client to publish the result
var pubsub = gcloud.pubsub({
// We're using the API from the same project as the Cloud Function.
projectId: process.env.GCP_PROJECT,
});
var outTopic = pubsub.topic(data['out-topic']);
logger.log('Worker ' + data['worker'] + ' reporting total count of ' +
count + ' in batch of size [' + batch.length + ']');
outTopic.publish({
data: {
count: count,
worker: data['worker']
}
}, function(err) {
if (err) {
logger.error(err);
context.failure(err);
return;
}
context.success(count + '');
});
},
/**
* Reads the source file and fans out to the workers.
*/
'master': function(context, data) {
// Create a gcs client
var gcs = gcloud.storage({
// We're using the API from the same project as the Cloud Function.
projectId: process.env.GCP_PROJECT,
});
// Create a pubsub client to publish the work and read the results of the workers.
var pubsub = gcloud.pubsub({
// We're using the API from the same project as the Cloud Function.
projectId: process.env.GCP_PROJECT,
});
// Get the bucket containing our source file
var bucket = gcs.bucket(data['bucket']);
// The topic we are going to publish to
var inTopic = pubsub.topic(data['in-topic']);
var file = data['file'];
var gcsFile = bucket.file(file);
var batchSize = data['batch-size'] || 3;
self._onProcessFile(gcsFile, inTopic, data['out-topic'], batchSize,
function(err, count) {
if (err) {
logger.error(err);
context.failure(err);
return;
}
// The topic we are going to subscribe to
var outTopic = pubsub.topic(data['out-topic']);
// Subscribe to the out topic and wait for results
self._receiveResults(outTopic, count, function(err, result) {
if (err) {
logger.error(err);
context.failure(err);
return;
}
context.success('The file ' + file + ' has ' + result +
' words');
});
});
},
/**
* Processes the source file line-by-line.
* @param gcsFile The name (path to) of the GCS File to be processed.
* @param topic The pub/sub topic on to which messages will be published
* @param strOutTopic The name of the topic to which the worker will ultimately publish.
* @param batchSize The number of line per batch (default 3)
* @param callback errback function to receive results in the form ()
*/
'_onProcessFile': function(gcsFile, topic, strOutTopic, batchSize, callback) {
const readLine = require('readline');
// Load the master file using the stream API
var inStream = gcsFile.createReadStream()
.on('error', function(err) {
logger.error(err);
callback(err);
return;
});
// use the readLine module to read the stream line-by line
var lineReader = readLine.createInterface({
input: inStream
});
// Create an array to hold our request promises
var promises = [];
// We are going to batch the lines, we could use any number here
var batch = [];
// Group lines in to the file into batches and publish to the topic
lineReader.on('line', function(line) {
if (batch.length === batchSize) {
// Send the batch.
promises.push(self._publishBatch(topic, batch, strOutTopic,
'worker' + promises.length)); // Give each worker an ID so we can handle duplicates
batch = [];
}
batch.push(line.trim());
});
// Invoke the callback once the file has been completely read
lineReader.on('close', function() {
// We might have trailing lines in an incomplete batch.
if (batch.length > 0) {
promises.push(self._publishBatch(topic, batch, strOutTopic,
'worker' + promises.length));
}
// Wait for all promises to return
Promise.all(promises).then(
function(result) {
logger.log('All batches [' + result.length +
'] have been published');
// The result will be an array of return values from the workers.
callback(null, result.length);
},
function(err) {
logger.error(err);
callback(err);
});
});
},
/**
* Publishes a batch of file lines to the given topic.
* @param topic The pub/sub topic to which messages will be published.
* @param batch An array of Strings representing lines in the file.
* @param strOutTopic The name of the topic to which the worker will ultimately publish.
* @param workerId A (locally) unique ID for the worker so we can handle at-least-once duplicates from pubsub.
* @return A Promise that resolves when the publish has completed.
*/
'_publishBatch': function(topic, batch, strOutTopic, workerId) {
return new Promise(function(resolve, reject) {
logger.log('Sending batch of ' + batch.length +
' lines to worker worker' + workerId);
topic.publish({
data: {
'batch': batch,
'out-topic': strOutTopic,
'worker': workerId
}
}, function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
},
/**
* Recieves results on the given topic.
* @param topic The pub/sub topic in which messages will be received.
* @param count The number of messages we expect in this batch.
* @param callback errback function to receive results.
*/
'_receiveResults': function(topic, count, callback) {
// Subscribe to the out topic and wait for results
var options = {
autoAck: true,
reuseExisting: true
};
// The subscription name can be anything, we're going to re-use it.
topic.subscribe('mapr-pubsub-subscription', options,
function(err, subscription) {
if (err) {
callback(err);
return;
}
var words = 0;
// Track returned workers to avoid duplicates
var returned = {};
var onError = function(err) {
logger.error(err);
callback(err);
return;
};
var onMessage = function(message) {
var worker = message['data']['worker'];
logger.log('Got count of ' + message['data']['count'] +
' from worker ' + worker);
if (returned[worker] !== true) {
count--;
returned[worker] = true;
words += parseInt(message['data']['count']);
if (count === 0) {
// Remove listeners to stop pulling for messages.
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
callback(null, words);
}
} else {
logger.log('Received duplicate result from worker ' +
worker);
}
};
// Register listeners to start pulling for messages.
subscription.on('error', onError);
subscription.on('message', onMessage);
});
},
};
module.exports = self;