foundationdb icon indicating copy to clipboard operation
foundationdb copied to clipboard

Node.js Bindings

Open alecgrieser opened this issue 8 years ago • 28 comments

We have historically maintained bindings in Node that wrapped our C bindings. However, it was initially developed for an older version of Node, and as time went on, it became incompatible with modern versions of the language. At this point, it is not particularly useful for Node users due to those incompatibilities. It also could probably benefit from some of the new Node features that have been developed in the intervening releases. As a result, it would seem like we should remove the Node bindings until such time as a suitable replacement is available. If there is demand for such a binding, the old Node bindings will still exist in history, so our previous work could plausibly be used as a jumping off point for someone who wanted to create their own.

alecgrieser avatar Apr 06 '18 17:04 alecgrieser

Will and I might need these; we can look into the cost of resurrecting them. Can you guys provide the test infrastructure if we can provide working code?

davidscherer avatar Apr 19 '18 18:04 davidscherer

Couple of years ago I have fixed several bugs in Node.js bindings (bug with future resolution on JS side which lead to JS runloop halt, SIGABRT due to missing HandleScope, bug with cleaning up of weak handles in FdbOptions resulting in memory leak) and made it compile and work under Node.js v4. We used the fixed bindings in production for some time without any issues. Didn't try to compile them for newer Node versions, though. Can share the fixed code if it helps.

We would really love to see Node.js bindings resurrected!

skozin avatar Apr 19 '18 22:04 skozin

Please share the fixed code if you can. I want to use FDB from nodejs. I'd be happy to spend some time resurrecting the bindings.

Is there documentation on the wire format around anywhere? A popular option for remote client networking libraries these days is to start with pure javascript implementations, and then replace out methods with native code when they're available as an optimisation. (Eg ws does this).

josephg avatar Apr 19 '18 23:04 josephg

@josephg As to scoping the work for a pure javascript client, you might want to check out this forum post: https://forums.foundationdb.org/t/how-difficult-would-it-be-to-implement-the-wire-protocol-in-other-languages/69

The tl;dr is that our client is pretty complicated and reimplementing it would be a lot of work that would be a significant surface area for bugs.

alecgrieser avatar Apr 20 '18 00:04 alecgrieser

@josephg ok, I'll try publish the repo today.

skozin avatar Apr 20 '18 07:04 skozin

Here is the repo: https://github.com/skozin/node-fdb. It builds upon the work done by @justmoon in https://github.com/justmoon/node-fdb to make it build under Node v0.12. The changes are:

  1. Make it build under Node v4.0: https://github.com/skozin/node-fdb/pull/1/commits/fd2c5d2ecf912a580ede5c99cac3e129b017c4bc.
  2. Fix microtask queue halt due to usage of process.nextTick to resolve a Future: https://github.com/skozin/node-fdb/pull/1/commits/ad0d35c7526cca49f653659cc2635122299556aa.
  3. Fix SIGABRT due to missing HandleScope: https://github.com/skozin/node-fdb/pull/1/commits/bc8a370d877dace7e023d2a4147936a152fae7e5.
  4. Fix memory leak/crash due to weak callback not following contract defined by v8: https://github.com/skozin/node-fdb/pull/1/commits/52e4ba9d77cc630cbd4876cfe4707d8afdf58bc2.

You can see all changes in this PR: skozin/node-fdb#1.

skozin avatar Apr 20 '18 11:04 skozin

Thanks. I've cloned to josephg/node-fdb. I'll bring it up to date with modern nodejs code over the next week or so.

~~Can someone confirm that this client code is published under the same license as foundationdb itself?~~ (Edit: It looks like it was always published under a BSD license. Perfect 😁 )

josephg avatar Apr 20 '18 11:04 josephg

An update on this: I'm making a lot of changes to the library to bring it in line with how the modern js / node ecosystem works. I am breaking backwards compatibility in the process. Specifically:

  • (Controversial) I'm removing the code that lets the user specify the API version. I'd rather rely on npm's strict handling of semver, which is enforced all the way through to library consumers. Each version of the library will correspond to a specific fdb API version. Npm library consumers are already used to pinning specific package versions for API compatibility. This simplifies the code dramatically, although it will also introduce API inconsistency with the other fdb frontends. I think thats the correct tradeoff
  • Remove the future class. Everything that previously returned a future will now return a javascript Promise object
  • CreateDatabase and CreateCluster currently block. I'm moving them behind JS promises.
  • Needing to depend on a system-wide install of the foundationdb client library is really rubbish for deployment. I'm not sure how hard this'll be, but I want to embed the relevant parts of the fdb C++ code and the relevant parts of boost into the module directly somehow. npm install foundationdb should set up everything on every platform.
  • I'm also considering porting the code to typescript
  • Its missing tests right now - we should hook it up to the testing code that the other first class bindings use

josephg avatar Apr 21 '18 00:04 josephg

  • Needing to depend on a system-wide install of the foundationdb client library is really rubbish for deployment. I'm not sure how hard this'll be, but I want to embed the relevant parts of the fdb C++ code and the relevant parts of boost into the module directly somehow. npm install foundationdb should set up everything on every platform.

I will point out two things. The first is that all that is really needed is the client library itself, which can usually be included by making sure that it is in a known path (say, "myapp/lib") and setting, say, the DYLD_LIBRARY_PATH or LD_LIBRARY_PATH environment variable to that path so that the dynamic linker picks it up. So it's not necessary for it to exist in the default system location, which I agree would be annoying and probably untenable for deploying in certain environments.

The second is that the benefit of separating the client library from the bindings is that you can upgrade the native client without having to upgrade either the bindings or the application. This means that, properly configured, you can pick up bug fixes, performance improvements, wire protocol changes, etc., all "for free" without having to change code. It can also make working with the multi-version client somewhat easier (and make it so that the user can avoid using it in certain situations).

There are similar arguments to be made about a user-configurable API version (that it lets the user update their bindings without having to update their code base). I don't know enough about node development to really comment on the norms of that community, so maybe it is a reasonable trade-off.

alecgrieser avatar Apr 21 '18 00:04 alecgrieser

I won't comment now on all of the above, but I did have a couple thoughts that may help provide some context as you are designing the new bindings:

createDatabase and createCluster actually used to be asynchronous in Node and other bindings. We intentionally made them synchronous calls because they are effectively synchronous under the hood and it simplifies the usage of them to not have to deal with them being asynchronous. While you can make them asynchronous, it's not really necessary.

Trying to embed the FoundationDB library into the module may limit some of the flexibility of the bindings, particularly if you are also simultaneously eliminating API versions. In particular, it may complicate the upgrade process somewhat.

It's required that a client have a compatible version of libfdb_c available to communicate with the cluster, so if you tied a module with a particular version of libfdb_c, you would be required to upgrade your module in order to support a cluster upgrade. However, with fixed API versions, that will likely also involve an API version change, which will force an update to client code at the same time.

There's also the multi-version client capability (https://apple.github.io/foundationdb/api-general.html?#multi-version-client) which allows a single client application to load multiple versions of libfdb_c to support upgrades without downtime. In order to use it, you'll need your modules to have access to libfdb_c libs at both the current version of the cluster and the target version.

A benefit of having both the API versioning and the external libfdb_c is that different parts of the upgrade can happen independently. You can upgrade the cluster without touching the bindings currently being used or changing your client code at all, and the multi-version client makes this possible without experiencing downtime. Then you can later upgrade the bindings, thereby getting bug fixes and performance improvements, etc, without having to update your client code. Eventually, you can also update your client code and switch to the current API version. Ideally, the new approach for the Node bindings can find someway to maintain this flexibility.

ajbeamon avatar Apr 21 '18 01:04 ajbeamon

all that is really needed is the client library itself, which can usually be included by making sure that it is in a known path (say, "myapp/lib") and setting, say, the DYLD_LIBRARY_PATH or LD_LIBRARY_PATH environment variable to that path so that the dynamic linker picks it up. So it's not necessary for it to exist in the default system location, which I agree would be annoying and probably untenable for deploying in certain environments.

This is still pretty annoying. The standard nodejs deployment process is to do a git checkout (or some equivalent), then npm install and then run it. Needing any kind of separately installed fdb library is super frustrating. I could work around that by bundling a compiled copy of the fdb client library into the node module. That would work ok but we'd need a built copy of that for every arch / platform pair that nodejs supports. There's a long tail of platforms which nodejs runs on, and maintaining compiled binaries for all platforms sounds like no fun. (Freebsd? Illumos? Raspberry pi? ARM? Weird linuxes like NixOS? Windows x64 & 32?). Fdb doesn't support many platforms yet, but I hope that'll slowly change over the next few months. I know Bryan Cantrill, Joyent's CEO is excited so I'm sure we'll see a Illumos / SmartOS port soon. And I suspect a FreeBSD build isn't far off either.

This is why having a pure javascript version of the client is so nice. You don't need to worry about any of this stuff. And when we do care about performance, native bindings can be used as an optimization.

The really fancy way to do this would be to build the client library as a webassembly module and ship that to all platforms. That'll be fast & work everywhere; but it'll require code changes in the client as it stands right now. Threading support in wasm is still in progress. It'll probably land in nodejs in the next few months.

It's required that a client have a compatible version of libfdb_c available to communicate with the cluster, so if you tied a module with a particular version of libfdb_c, you would be required to upgrade your module in order to support a cluster upgrade. However, with fixed API versions, that will likely also involve an API version change, which will force an update to client code at the same time.

Oh what? Thats really annoying. How tight is compatibility? Do you generally need a new copy of libfdb_c with each and every update to the server, forcing client and server deployment to be paired? Thats a pretty annoying deployment burden across the ecosystem. If cluster upgrades have strict frontend version requirements then it'll be really hard to do zero-downtime deployments.

In nodejs we might be able to partially solve it by using even more npm modules. We could maintain two npm packages:

  • One package is libfdb_c. That has its version tied to the version of fdb you're running in your cluster. Eg 510.0.0, 520.0.0, etc. (Or 5.1.0 if those fdb version numbers are semver-compatible).
  • The second package is the frontend client library. This consumes your chosen version of libfdb_c through npm peerDependancies.

That way when you deploy a protocol-incompatible version of the server you can update your frontend systems without being forced to bump the version of your client code. But its still going to be annoying if you have to change any frontend code when you deploy a new version of the cluster binaries.

createDatabase and createCluster actually used to be asynchronous ... While you can make them asynchronous, it's not really necessary.

Yeah I see that in the code. In practice making them async doesn't add a lot. But with promises it doesn't cost a lot to have them asynchronous, and its cleaner. 🤷‍♀️ I'm not super opinionated on that call either way to be honest.

josephg avatar Apr 21 '18 02:04 josephg

I feel as though this thread would be a good candidate for being discussed on the community forums, given that it's largely a discussion vs a task. (More info on the difference between how we're using GitHub Issues vs the forums within the contribution guide.)

We've been moving similar threads for Swift and Rust bindings to the forums. When it seems natural to do so, perhaps we can migrate this discussion there as well?

davelester avatar Apr 21 '18 04:04 davelester

This is why having a pure javascript version of the client is so nice. You don't need to worry about any of this stuff. And when we do care about performance, native bindings can be used as an optimization.

There was a short discussion about this on the recent Hacker News post which may help provide some relevant details about why we took the approach we did.

Do you generally need a new copy of libfdb_c with each and every update to the server, forcing client and server deployment to be paired?

Major and minor version upgrades are always protocol incompatible, so any upgrades between them will have this requirement.

If cluster upgrades have strict frontend version requirements then it'll be really hard to do zero-downtime deployments.

Our multi-version client is the current tool used to support zero-downtime upgrades. It basically allows you to preload a different version of the client library in advance of the cluster being upgraded, so you don't have to coordinate an upgrade of the clients and cluster all simultaneously.

Yeah I see that in the code. In practice making them async doesn't add a lot. But with promises it doesn't cost a lot to have them asynchronous, and its cleaner. 🤷‍♀️ I'm not super opinionated on that call either way to be honest.

I don't personally have strong opinions about this either, but for what it's worth I believe all the other high-level bindings are synchronous here. We also found it a bit cleaner for the API user to have this function be synchronous (not only is the code using it more succinct, but it being asynchronous may cause a user to unnecessarily try to deal with cases where it may not return immediately, etc).

ajbeamon avatar Apr 21 '18 05:04 ajbeamon

Also, see here for another discussion about implementing the client in another language:

https://forums.foundationdb.org/t/how-difficult-would-it-be-to-implement-the-wire-protocol-in-other-languages/69/5

ajbeamon avatar Apr 21 '18 05:04 ajbeamon

Oh gosh, ok. So the intended deployment strategy is to first deploy an additional copy of the frontend library, then roll an update on the cluster. The client will reconnect using every copy of the wire format that it has access to. But the javascript library / user code will specifically request an older version of the protocol running over that wire format and everything should work. And then later the user can remove the additional copy of fdb_c from all the clients.

Whew, alright then.

This isn't an easy situation to pave over for the user because afaik npm doesn't let you resolve multiple copies of the same library. We might be able to make something work using an npm scope for versions of the client library; so, publish fdb_c_500 and fdb_c_510 and fdb_c_520 and so on. The user would explicitly install all the versions of fdb_c they'll need for deployment. Then at service startup they pass a list ['500', '510'] of copies of fdb_c that the client code will use if it needs to to connect. The problem then is that the node client library will need to pick one of those versions to link against; and I'm not sure which version we should pick. Maybe it should just be evergreen, but always let you specify an alternate library list. So you end up pinning two client versions: one specifies the node library API version (specified in node_modules). And the other specifies the version(s) you are (or want to be) running on your cluster. And for that you both have to npm install --save fdb_c_500 and configure the client so it knows to always add that library if it needs it.

I'm going to focus on making the other stuff work first. This is kind of a mess.

josephg avatar Apr 21 '18 06:04 josephg

I published an initial version of the library. For now it just has kv support. I'll re-add the layer support over the next week or two. We also need to figure out a plan for testing - where did the tests go? How was this tested last time it was developed?

josephg avatar Apr 22 '18 14:04 josephg

I wrote up a topic on the forum that goes into a little more detail on what bindings need to do: Creating New Bindings. The most relevant part to your question is the part about integrating into the bindingtester.

The tests for the old node bindings lived within the "tests" subdirectory. Here was the main stack-based tester implementation: tester.js. They were invoked by the bindingtester, our randomized tester. The forum post goes into a little more detail on how bindings are integrated into the bindingtester, if that helps. They were removed from the tester with commit ff956c3fd.

alecgrieser avatar Apr 22 '18 20:04 alecgrieser

Hi @josephg, I'd love to try the new Node.js module foundationdb and contribute in any way I can. I found the fork of node-fdb you're working with - should I file any issues or pull requests there?

eliot-akira avatar Apr 22 '18 23:04 eliot-akira

@eliot-akira Great! Yes please!

josephg avatar Apr 23 '18 00:04 josephg

@josephg Looks like issues are disabled in the settings for the repo?

eliot-akira avatar Apr 23 '18 00:04 eliot-akira