Repository navigation
Description
There are a lot of features that are useful to have in a database which can be implemented on top of existing ReQL constructs. Some of them are not currently available out of the box.
To name a few:
- Document expiration (TTL) Reintroduce TTL/expiration on documents #746
- GraphQL support Support Facebook's GraphQL #3711
- Triggers (using changefeeds) triggers - attempt 2 #3367
- Monitoring and administration tools, including our current web UI (on the server side it's just an HTTP server that executes ReQL)
- Query logging have server log client requests #544 / Slow query log #2238
Currently, implementing these features without modifying the server requires deploying and managing a separate application. You have to manage its configuration and state, you have to make sure that it runs on the right servers, you have to deal with application failures and server restarts etc. This can often be inconvenient.
This proposal is a work in progress. Its idea is to provide a "plugin" interface that allows running small extensions directly with the RethinkDB server process (credits to @mlucy who came up with the idea). Plugins can be easily enabled on a given RethinkDB server. They can provide additional ports and APIs to clients (e.g. for GraphQL queries). Their state and configuration will be stored in the RethinkDB cluster, so that it can be automatically synchronized across all servers.
We will develop some plugins ourselves. But we will also make it extremely easy for everyone to write their own plugins.
RethinkDB plugin proposal (RFC / WIP)
Initialization
Many plugins will need a separate setup phase for creating tables, initializing configuration etc.
Rather than trying to do this automatically when a plugin is first enabled (which can be tricky in a clustered environment), I think we should just have a "setup"/"initialization" phase that needs to be triggered manually.
Command line syntax
--setup-plugin <plugin name>
--load-plugin <plugin name>
Some plugins should launch by default (web UI). I suggest we just keep the current --no-http-admin command line option to disable it.
In the long term, we might want to allow starting, stopping etc. plugins without restarting the server. This should probably work through a ReQL admin table. I suggest we don't care about that for now.
State
Plugins shall only keep state in a single database.
All of their configuration shall be stored there.
The database and all required tables shall be created during the setup phase (this is important to avoid race conditions during table creation in a cluster).
We should standardize the naming scheme for the db.
Something like _plugin_<plugin_name>?
The nice thing about having all state in a database is that this allows admins to configure
replication, and to change the plugin configuration and state directly through ReQL (things like
which port a plugin should bind to etc.).
Connectivity
The RethinkDB server provides the required access credentials to the plugin.
Usually this would require passing in the port and the auth key.
I suggest we provide plugins with a Unix domain socket instead, so that we don't have to pass out an auth key (and it will also be more efficient and more secure). The domain socket would not require an auth key to connect.
The path of the domain socket can be passed into the plugin through environment variables.
Monitoring
r.db('rethinkdb).table('server_status') will have a new field 'active_plugins' with an
array of all the running plugins.
Plugins can also write to the server log (see "Process details").
Process details
If a plugin is enabled, we simply launch a node.js instance with the right environment variables.
If it terminates, we print a log message and try restarting it after a certain time (30 seconds?).
Any line the process prints to stdout is forwarded to the rethinkdb log as "LOG_INF" entries. stderr is forwarded as "LOG_ERR" entries.
I suggest we ignore sandboxing and limiting permissions for the first revision. In the longer term, we might want to set process limits and put them in a chroot among others.
An open question is packaging. We can either bundle our own node.js server with RethinkDB, or rely on the system's one. The latter option might be preferable because it simplifies keeping node.js up to date. The downside is that some plugins might not work on all systems in case there are any incompatibilities between different node.js releases.
File structure
I think a plugin should just be a folder in a specific path (e.g. /usr/lib/rethinkdb/plugins/<plugin name>). The folder has to contain at least two files:
run.js and setup.js
For the web UI, we might want to do something special so we don't add new file dependencies (i.e. we might want to keep compiling it into the RethinkDB binary).
Activity
deontologician commentedon Sep 2, 2015
Linking to #3861 the standalone webui issue
marshall007 commentedon Sep 3, 2015
This is very similar to how plugins are implemented in ElasticSearch. ElasticSearch has the concept of "Site Plugins" whereby the contents of a plugin's
_sitedirectory are served at/_plugins/<plugin_name>. Is something like this what you have in mind for the Web UI and similar plugins?If so, does this imply that connections over HTTP in the JS driver will need to be supported in some official capacity? There's been discussions around dropping that stuff entirely, but such plugins will need some way to execute arbitrary queries.
danielmewes commentedon Sep 3, 2015
I haven't looked into ElasticSearch site plugins, but they sound superficially similar.
Since the plugins will run in a node.js container, they can use the regular JavaScript driver without a need for HTTP connections.
The browser-side part of the web UI will still need to connect to the plugin somehow though, which might require keeping the current HTTP ReQL connection logic in place for a bit longer.
What we can do with the plugins is remove the HTTP endpoint in the (C++) server. Instead the web UI plugin would receive the queries from the browser via HTTP, and would convert them into regular TCP-based ReQL requests before forwarding them to the server.
deontologician commentedon Sep 3, 2015
This is more similar to ElasticSearch rivers. The main difficulty they had with rivers, and why they're deprecating them, is that it gets into the hairy business of the database becoming a process supervisor for code with varying levels of quality. If the river you installed was buggy, suddenly your database was unreliable.
Their little more in-depth discussion is at https://www.elastic.co/blog/deprecating-rivers . It's possible that we can avoid some of the issues they ran into by carefully handling process isolation
deontologician commentedon Sep 3, 2015
I should mention that rivers actually dynamically load code into the database itself, so it was much more tightly coupled than what @danielmewes is proposing here.
marshall007 commentedon Sep 3, 2015
@danielmewes @deontologician I think you should be able to write plugins that only consist of client-side HTML and JS. What you just suggested would require each plugin to create their own backend mechanism to evaluate queries from the browser. I think whatever the Web UI uses should be exposed for any plugin to use.
marshall007 commentedon Sep 3, 2015
@danielmewes to be clear, I think what you're saying about removing support for HTTP connections from the server makes sense to do. I just think that instead of having the Web UI plugin expose an endpoint for itself, the server (or another plugin) should expose an endpoint that the Web UI and any other plugins can use.
Also, changefeeds would still need to be considered. An endpoint that parses and evaluates a plain-text query still wouldn't do you much good here.
mlucy commentedon Sep 8, 2015
I'm not 100% sure plugin state should live in a table. You can get into weird situations that way. Say, for example, you add a plugin while your cluster is one node, then later you grow it to a 16 node cluster. Unless we automatically reconfigure the plugin tables when we add nodes, which we probably shouldn't because of availability loss, the plugin is now dependent on one of your 16 servers being up, and if that server goes down then the plugin stops working even if the rest of the cluster is healthy. The only way around this is to make users responsible for reconfiguring their plugin tables when they change their cluster, which is kind of a pain.
I guess it depends on what sort of data plugins are storing. If they're storing gigabytes of user data, a table is clearly the appropriate place for that, but if they're storing a few kilobytes of configuration data then maybe something else is appropriate.
Also, some plugins (like the web UI) might want purely local data storage so they keep working even if disconnected from the rest of the cluster.
Maybe this is too complicated for the first version, though, and we should only support storing plugin data in tables at first?
This seems unnecessarily complicated to me. I think that if we just pass the plugins the port to connect on and the auth key that would be easier (we wouldn't have to add anything new to the server besides the ability to launch the plugin). That would also making porting to windows easier (unless they have an equivalent of Unix domain sockets, in which case ignore this point).
Some of the intended plugins (document expiration, triggers) seem like they would run on only one machine rather than on every machine in the cluster. Should we just leave it up to users to pick which machines plugins are launched on by editing their command-line options, or are we going to be starting and stopping plugins in a centralized way?
mlucy commentedon Sep 8, 2015
We can definitely make this better with resource limits for plugins, but I do think that plugins present a general problem that people will install a buggy plugin and then assume RethinkDB is buggy. (And nobody likes being kicked back and forth between the repo for a plugin and the repo for the main program when they have a bug report.)
Still, I think it's worth the cost. We should consider having a way to endorse plugins that we think are stable so that people know what they're getting into.
danielmewes commentedon Sep 23, 2015
@marshall007 That sounds like a great idea. I think the web UI plugin would essentially have that functionality. In fact all the specific queries it's running are already encoded in the browser-side JS code, not in the backend.
We could probably make it a library of some sort, so that you can very easily create plugins where you only need to provide the static HTML + browser JS code, without writing any additional code for the backend.
@mlucy
I thought about that too. I couldn't really come up with a great solution. I think for the moment it would be ok if we left reconfiguring the plugin tables to the administrator. Note that adding and removing replicas doesn't impair availability though (with the exception of currently running queries and changefeeds being interrupted). Additionally, many plugins could probably use outdated reads to make sure that the plugin remains available as long as at least one replica is still around.
Overall I think storing all configuration and plugin state into a table makes for a nicer user experience.
I agree that it probably depends a bit on the use case. The main scenario I had in mind was the GraphQL library.
Configuration options would include:
Especially for the latter two, being able to modify those settings through normal ReQL queries would be extremely useful I think. And we don't need to worry about providing a special API for configuration changes.
My impression is that relying on tables as the only mean for storing state and configuration will be the best way to get us started. Once we have more experience about what plugins need, we can add additional storage options or add better abstractions around the tables. We can also think about how to automatically replica the state tables to additional servers then.
Fair enough. I think that might be ok, though it feels less clean to me (especially the fact that we need to expose the auth key to plugins). We could make this an optional feature, depending on how much time we'll end up having left.
I'm not sure about what Windows offers in this respect ( @AtnNn ?).
I think for the first version, it's enough if you have to explicitly enable a plugin on a given server by providing a command line option. Long-term we should probably make that more convenient and handle the different cases you mentioned.
marshall007 commentedon Sep 23, 2015
@danielmewes perfect! We have a lot of internal tools where I'd like to be able to allow users to execute (almost) arbitrary queries from the browser. With the client-side JS library and supporting endpoint, all I'd have to do is handle authentication in a proxy. This would also just be great for prototyping single-page apps before you write a backend.
I agree with this. It means you could easily write plugins that "interact" with other plugins through ReQL. This way the Web UI (or a third-party) could manage plugins and their configuration. Another nice side affect is that plugin configuration and data could be backed up and restored.
marshall007 commentedon Sep 25, 2015
Will plugins be able to (optionally) defer the handling of requests? I can think of a couple scenarios where this would be useful.
Authentication
Rather than baking authentication into each plugin, it would be a lot nicer if that functionality were split out for reuse. Furthermore, since the authentication piece is pretty critical, it's not really something you want each plugin author implementing themselves.
You could imagine "official" plugins that implement various authentication schemes like
rethinkdb-oauthandrethinkdb-http-basicwhich could be configured to intercept and authenticate all or some subset of requests before deferring response handling to the intended receiver plugin.Logging
You might want to intercept requests in order to generate a query log or track performance metrics.
danielmewes commentedon Sep 25, 2015
@marshall007 I had thought about these things too.
I'm not sure how this is going to pan out in detail yet, but I imagine that we'll build a library of useful components over time, that you can use easily in your own plugins.
Authentication and logging are definitely examples of things that should be re-usable.
This can be done almost independently of how plugins are handled on the server side, since those libraries are going to exist in the form of node.js libraries that each plugin can just import. I agree that we should try to provide as many of those out of the box as possible over time. Partially because it makes writing plugins easier, and partially because it makes different plugins more consistent.
1 remaining item
coffeemug commentedon Oct 7, 2015
@danielmewes asked me to comment on the proposal.
I think we should adopt the following high level design goals for the first implementation:
npm)Here is some more specific feedback on a couple of key issues:
npm. Essentially our commands would be porcelain overnpm. I think we should do it for two reasons. Firstly, they already do a great job with version management, and we don't want to write our own. Secondly, plugins will depend on many libraries, andnpmalready manages dependencies really well.active_pluginsfield tor.db('rethinkdb).table('server_status').Command line args for plugins
I think we should maintain our existing command line args for the webui, and I think we can make it generic. During startup each plugin will tell rethinkdb whether it's on or off by default, and how to turn it off. So the webui will say it's on by default, and you turn it off via
--no-http-admin. GraphQL will tell rethinkdb it's off by default, and you turn it on via--graphql. Each plugin will also expose a set of command line args it accepts (e.g. for port management).I'm not entirely sure how to integrate port offsets into this, but it should be possible (e.g. the plugin can tell rethinkdb that a particular command line arg it wants is a port, and port offset will be added to all of those).
Command line API
I think we should avoid the name plugins (that's so 90s) and call them extensions. I propose the following CLI API (slightly different from Daniel's proposal; I think mine is better 😁):
How's this lookin'?
danielcompton commentedon Oct 7, 2015
Just to clarify, extensions (at least in the first version) will only be able to be written in JavaScript?
danielmewes commentedon Oct 7, 2015
marshall007 commentedon Oct 7, 2015
Instead of cramming everything under the
rethinkdbcommand, the package management stuff could itself be published as annpmpackage. I think this would have several advantages:rethinkdbcommand.npm, so extension developers and users already familiar withnpms arguments wouldn't have to learn anything new.rpmorrem(ReM).This is what Atom did for their package manager (atom/apm) and it's pretty nice, IMO.
coffeemug commentedon Oct 7, 2015
@marshall007 -- that sounds like a good idea and is probably easier to do. 👍
mlucy commentedon Oct 8, 2015
I think you should be able to type
rethinkdb <SOMETHING>to install and manage extensions, personally. That's what I would expect to be able to do, and it's consistent with existing extension-like-things likerethinkdb export.I don't particularly like
rethinkdb extendas a prefix becauserethinkdb extend --listreads weirdly, and it isn't clear what you're doing. I also don't like using--Xto specifyXwhereXis an action -- flags should be used for turning options on and off, and actions should be specified as plain words. (For example, it'sgit commit -a, notgit --commit -a. The action is a word and the option is a flag. Same thing forapt-get install-- it's notapt-get --install.How about
rethinkdb extension list,rethinkdb extension install,rethinkdb extension remove, etc?deontologician commentedon Oct 8, 2015
I feel like "extension" isn't a good name. We aren't really extending the server so much as providing a built-in process monitor. While "plugin" isn't perfect, it's closer I think. I'd expect something called an extension to add new ReQL terms or artificial tables with special behavior maybe.
mlucy commentedon Oct 8, 2015
I think "extension" is a reasonable name because we're probably going to support a lot more things later, and we'd rather not have to change the name. "plugin" is probably fine too, though.
dalanmiller commentedon Oct 8, 2015
I disagree with this structure in particular:
I think that you should be able to do as @mlucy says and list all currently installed plugins with
rethinkdb plugins listwhich will display a list of plugins that are activated or deactivated. It's awkward to add-no-to the plugin flag to signify that this plugin should not be loaded.One can activate or deactivate plugins, if everything went okay no response is given in proper GNU tool format.
If you'd like to start RethinkDB for debugging purposes with a specific set of plugins, you should also be able to pass an explicit list. RethinkDB will only use these plugins in this instance:
coffeemug commentedon Oct 8, 2015
Note that this is a cultural thing. I don't necessarily disagree, but that was modelled after
npm, and that's hownpmdoes it (and also to some degreepip).I think that's a little narrow -- it's true about v1, but there are much bigger things we can (and will) do with this system in the future.
That makes sense, but the intention here was to be compatible with the existing flag to turn off the webui. I think I agree with the sentiment in general, though, and wouldn't mind breaking compatibility for this flag (since the new thing will be much more structured and will make more sense).
I think for the
graphqldemo we really want a one-liner (which I realize is a little bit neurotic, but I do think saying "get graphql in one line" is better than giving a two line/command example). Another problem is that sayingrethinkdb plugin activate/deactivateis tricky because I might have multiple instances of RethinkDB on my server, and it's not clear which server that applies to.deontologician commentedon Oct 8, 2015
What about "pluglets" ? Imagine:
"Did you get the new GraphQL pluglet?"
"Oh yeah, it's great. I query all my graphs with it. I heard there was a falcor pluglet too."
"Yeah, all my apps use pluglets"
coffeemug commentedon Oct 8, 2015
"Actually, I wrote my app as a pluglet" 🍶
dalanmiller commentedon Oct 8, 2015
All I can think of when I hear that => 💻 🐷
webmasterkai commentedon Oct 8, 2015
More name ideas if plugin doesn't cut it: module, hinge, attachment, component, mixin, peripheral, add-on, xtra, child, method, middleware.
dalanmiller commentedon Oct 9, 2015
I guess this functionality will depend on whether or not the architecture will allow adding plugins to a RethinkDB instance that is already in flight / running. Otherwise, I'd say for specific toggle switching of plugins, you'll need to pass either the driver port or the cluster port explicitly of the instance you are reconfiguring.