This recipe demonstrates how to create a simple word count sample using a master-worker pattern using HTTP invocation.
Where applicable:
Replace [PROJECT-ID] with your Cloud Platform project ID
- Client calls the "master" function via HTTP
- Master function pulls file from Google Cloud Storage
- Master function segments the file and fans out requests to multiple workers
- Worker functions process each segment and report the result back to the master
- Master function reduces all results from workers into a single result
- Master function returns aggregate result to the client
-
Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project
-
Clone this repository
cd ~/ git clone https://github.com/jasonpolites/gcf-recipes.git cd gcf-recipes/worker_http
-
Create a Cloud Storage Bucket to stage our deployment
gsutil mb gs://[PROJECT-ID]-gcf-recipes-bucket
-
Upload the sample file to the bucket
gsutil cp sample.txt gs://[PROJECT-ID]-gcf-recipes-bucket
-
Deploy the "worker" function with an HTTP trigger
gcloud alpha functions deploy mapr-worker --bucket [PROJECT-ID]-gcf-recipes-bucket --trigger-http --entry-point worker
-
Deploy the "master" function with an HTTP trigger
gcloud alpha functions deploy mapr-master --bucket [PROJECT-ID]-gcf-recipes-bucket --trigger-http --entry-point master
-
Call the "master" function using the sample file, and the URL of the "worker" function as arguments
gcloud alpha functions call mapr-master --data '{"bucket": "[PROJECT-ID]-gcf-recipes-bucket", "file": "sample.txt", "workerFunctionUrl": "https://[REGION].[PROJECT-ID].cloudfunctions.net/mapr-worker"}'
-
Alternatively because we deployed the functions with HTTP Triggers, you can just cURL the master function
curl -X POST -H "Content-Type:application/json" https://[REGION].[PROJECT-ID].cloudfunctions.net/mapr-master --data '{"bucket": "[PROJECT-ID]-gcf-recipes-bucket", "file": "sample.txt", "workerFunctionUrl": "https://[REGION].[PROJECT-ID].cloudfunctions.net/mapr-worker"}'
You can determine the region and URL of your function by using the describe command
gcloud alpha functions describe mapr-worker
You should see something like this in your console
The file sample.txt has 114 words
This recipe comes with a suite of unit tests. To run the tests locally, just use npm test
npm install
npm test
The tests will also produce code coverage reports, written to the /coverage
directory. After running the tests, you can view coverage with
open coverage/lcov-report/index.html