This was based on my experience building a new event provider for Apache OpenWhisk, which led me to prototype an easier way to add event sources to platform whilst cutting down on the boilerplate code required.
This blog post is overview of what I talked about on the call, explaining the background for the project and what was built. Based on positive feedback from the community, I have now open-sourced both components of the experiment and will be merging it back upstream into Apache OpenWhisk in future.
pluggable event providers - why?
At the end of last year, I was asked to prototype an S3-compatible Object Store event source for Apache OpenWhisk. Reviewing the existing event providers helped me understand how they work and what was needed to build a new event source.
This led me to an interesting question…
Why do we have relatively few community contributions for event sources?
Most of the existing event sources in the project were contributed by IBM. There hasn’t been a new event source from an external community member. This is in stark contrast to additional platform runtimes. Support for PHP, Ruby, DotNet, Go and many more languages all came from community contributions.
Digging into the source code for the existing feed providers, I came to the following conclusions….
- Trigger feed providers are not simple to implement.
- Documentation how existing providers work is lacking.
Feed providers can feel a bit like magic to users. You call the
wsk CLI with a
feed parameter and that’s it, the platform handles everything else. But what actually happens to bind triggers to external event sources?
Let’s start by explaining how trigger feeds are implemented in Apache OpenWhisk, before moving onto my idea to make contributing new feed providers easier.
how trigger feeds work
Users normally interact with trigger feeds using the
wsk CLI. Whilst creating a trigger, the
feed parameter can be included to connect that trigger to an external event source. Feed provider options as provided as further CLI parameters.
1 2 3 4 5 6
But what are those trigger feed identifiers used with the
It turns out they are just normal actions which have been shared in a public package!
The CLI creates the trigger (using the platform API) and then invokes the referenced feed action. Invocation parameters include the following values used to manage the trigger feed lifecycle.
lifecycleEvent- Feed operation (
triggerName- Trigger identifier.
authKey- API key provided to invoke trigger.
Custom feed parameters from the user are also included in the event parameters.
This is the entire interaction of the platform with the feed provider.
Providers are responsible for the full management lifecycle of trigger feed event sources. They have to maintain the list of registered triggers and auth keys, manage connections to user-provided event sources, fire triggers upon external events, handle retries and back-offs in cases of rate-limiting and much more.
Feed providers used with a trigger are stored as custom annotations. This allows the CLI to call the same feed action to stop the event binding when the trigger is deleted.
Reading the source code for the existing feed providers, nearly all of the code is responsible for handling the lifecycle of trigger management events, rather than integrating with the external event source.
Despite this, all of the existing providers are in separate repositories and don’t share code explicitly, although the same source files have been replicated in different repos.
The CouchDB feed provider is a good example of how feed providers can be implemented.
couchdb feed provider
This action just proxies the incoming requests to a separate web action. The web action implements the logic to handle the trigger lifecycle event. The web action uses a CouchDB database used to store registered triggers. Based upon the lifecycle event details, the web action updates the database document for that trigger.
The feed provider also runs a seperate Docker container, which handles listening to CouchDB change feeds from user-provided credentials. It uses the changes feed from the trigger management database, modified from the web action, to listen for triggers being added, removed, disabled or re-enabled.
building a new event provider?
Having understood how feed providers work (and how the existing providers were designed), I started to think about the new event source for an S3-compatible object store.
Realising ~90% of the code between providers was the same, I wondered if there was a different approach to creating new event providers, rather than cloning an existing provider and changing the small amount of code used to interact with the event sources.
What about building a generic event provider which a pluggable event source?
This generic event provider would handle all the trigger management logic, which isn’t specific to individual event sources. The event source plugin would manage connecting to external event sources and then firing triggers as event occurred. Event source plugins would implement a standard interface and be registered dynamically during startup.
Using this approach would make it much easier to contribute and maintain new event sources.
Users would be able to create new event sources with a few lines of custom integration code, rather than replicating all the generic trigger lifecycle management code.
Maintaining a single repo for the generic event provider is easier than having the same code copied and pasted in multiple independent repositories.
I started hacking away at the existing CouchDB event provider to replace the event source integration with a generic plugin interface. Having completed this, I then wrote a new S3-compatible event source using the plugin model. After a couple of weeks I had something working….
generic event provider
The generic event provider is based on the exiting CouchDB feed provider source code. The project contains the stateful container code and feed package actions (public & web). It uses the same platform services (CouchDB and Redis) as the existing provider to maintain trigger details.
The event provider plugin is integrated through the
EVENT_PROVIDER environment variable. The name should refer to a Node.js module from NPM with the following interface.
1 2 3 4 5 6 7 8 9 10 11 12
When a new trigger is added to the trigger feeds’ database, the details will be passed to the
add method. Trigger parameters will be used to set up listening to the external event source. When external events occur, the
trigger_manager can be use to automatically fire triggers.
When users delete triggers with feeds, the trigger will be removed from the database. This will lead to the
remove method being called. Plugins should stop listening to messages for this event source.
firing trigger events
As event arrive from the external source, the plugin can use the
trigger_manager instance, passed in through the constructor, to fire triggers with the identifier.
trigger_manager parameter exposes two async functions:
fireTrigger(id, params)- fire trigger given by id passed into
addmethod with event parameters.
disableTrigger(id, status_code, message)- disable trigger feed due to external event source issues.
Both functions handle the retry logic and error handling for those operations. These should be used by the event provider plugin to fire triggers when events arrive from external sources and then disable triggers due to external event source issues.
validating event source parameters
This static function on the plugin constructor is used to validate incoming trigger feed parameters for correctness, e.g. checking authentication credentials for an event source. It is passed the trigger parameters from the user.
S3 event feed provider
The feed provider polls buckets on an interval using the
ListObjects API call. Results are cached in Redis to allow comparison between intervals. Comparing the differences in bucket file name and etags, allows file change events to be detected.
Users can call the feed provider with a bucket name, endpoint, API key and polling interval.
File events are fired as the bucket files change with the following trigger events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Pssst - if you are using IBM Cloud Functions - I actually have this deployed and running so you can try it out. Use the
/firstname.lastname@example.org_dev/s3-trigger-feed/changes feed action name. This package is only available in the London region.
Feedback on the call was overwhelming positive on my experiment. Based upon this, I’ve now open-sourced both the generic event provider and s3 event source plugin to allow the community to evaluate the project further.
I’d like to build a few more example event providers to validate the approach further before moving towards contributing this code back upstream.
If you want to try this generic event provider out with your own install of OpenWhisk, please see the documentation in the README for how to get started.
If you want to build new event sources, please see the instructions in the generic feed provider repository and take a look at the S3 plugin for an example to follow.