Simplistic miniature etcd replacement based on TinyRaft (https://git.yourcmc.ru/vitalif/tinyraft/)
 
Go to file
Vitaliy Filippov 967dab461e Implement anticli.js load command 2024-06-12 16:31:58 +03:00
.eslintrc.js Add ESLint 2024-06-01 17:14:14 +03:00
README.md Implement anticli.js load command 2024-06-12 16:31:58 +03:00
anticli.js Implement anticli.js load command 2024-06-12 16:31:58 +03:00
anticluster.js Add license headers 2024-06-01 20:42:10 +03:00
antietcd-app.js Mark executables 2024-06-12 16:30:11 +03:00
antietcd.d.ts Add raftchange event to AntiEtcd type definitions 2024-06-01 17:14:14 +03:00
antietcd.js Fix "undefined this.antietcd.etctree" 2024-06-10 01:18:28 +03:00
antipersistence.js Add license headers 2024-06-01 20:42:10 +03:00
common.js Inherit RequestError from Error 2024-06-12 14:49:07 +03:00
etctree.js Allow get with empty prefix 2024-06-12 16:30:11 +03:00
etctree.spec.js Add license headers 2024-06-01 20:42:10 +03:00
package.json Implement anticli.js load command 2024-06-12 16:31:58 +03:00
stable-stringify.js Add Anti-Etcd - etcd mock, already sufficient to run Vitastor tests 2024-06-01 17:12:28 +03:00
vitastor_persist_filter.js Add license headers 2024-06-01 20:42:10 +03:00

README.md

AntiEtcd

Simplistic miniature etcd replacement based on TinyRaft

  • Embeddable
  • REST API only, gRPC is shit and will never be supported
  • TinyRaft-based leader election
  • Websocket-based cluster communication
  • Supports a limited subset of etcd REST APIs
  • With optional persistence

Contents

CLI Usage

npm install antietcd

node_modules/.bin/antietcd \
    [--cert ssl.crt] [--key ssl.key] [--port 12379] \
    [--data data.gz] [--persist_interval 500] \
    [--node_id node1 --cluster_key abcdef --cluster node1=http://localhost:12379,node2=http://localhost:12380,node3=http://localhost:12381]
    [other options]

Antietcd doesn't background itself, so use systemd or start-stop-daemon to run it as a background service.

CLI Client

node_modules/.bin/anticli [OPTIONS] put <key> [<value>]
node_modules/.bin/anticli [OPTIONS] get <key> [-p|--prefix] [-v|--print-value-only] [-k|--keys-only] [--no-temp]
node_modules/.bin/anticli [OPTIONS] del <key> [-p|--prefix]
node_modules/.bin/anticli [OPTIONS] load [--with-lease] < dump.json

For put, if <value> is not specified, it will be read from STDIN.

Options:

--endpoints|-e http://node1:2379,http://node2:2379,http://node3:2379
Specify HTTP endpoints to connect to
--cert <cert>
Use TLS with this certificate file (PEM format)
--key <key>
Use TLS with this key file (PEM format)
--timeout 1000
Specify request timeout in milliseconds
--json or --write-out=json
Print raw response in JSON

Options

HTTP

--port 2379
Listen port
--ip 192.168.0.10
Listen address (by default listen on all interfaces)
--cert <cert>
Use TLS with this certificate file (PEM format)
--key <key>
Use TLS with this key file (PEM format)
--ca <ca>
Use trusted root certificates from this file. Specify <ca> = <cert> if your certificate is self-signed.
--client_cert_auth 1
Require TLS client certificates signed by or by default CA to connect.
--ws_keepalive_interval 30000
Client websocket ping (keepalive) interval in milliseconds

Persistence

--data <filename>
Store persistent data in <filename>
--persist_interval <milliseconds>
Persist data on disk after this interval, not immediately after change
--persist_filter ./filter.js
Use persistence filter from ./filter.js (or a module).
Persistence filter is a function(cfg) returning function(key, value) ran for every change and returning a new value or undefined to skip persistence.
--compact_revisions 1000
Number of previous revisions to keep deletion information in memory

Clustering

--node_id <id>
ID of this cluster node
--cluster <id1>=<url1>,<id2>=<url2>,...
All other cluster nodes
--cluster_key <key>
Shared cluster key for identification
--election_timeout 5000
Raft election timeout
--heartbeat_timeout 1000
Raft leader heartbeat timeout
--wait_quorum_timeout 30000
Timeout for requests to wait for quorum to come up
--leader_priority <number>
Raft leader priority for this node (optional)
--stale_read 1
Allow to serve reads from followers. Specify 0 to disallow
--reconnect_interval 1000
Unavailable peer connection retry interval
--dump_timeout 5000
Timeout for dump command in milliseconds
--load_timeout 5000
Timeout for load command in milliseconds
--forward_timeout 1000
Timeout for forwarding requests from follower to leader in milliseconds
--replication_timeout 1000
Timeout for replicating requests from leader to follower in milliseconds
--compact_timeout 1000
Timeout for compaction requests from leader to follower in milliseconds

Embedded Usage

const AntiEtcd = require('antietcd');

// Configuration may contain all the same options like in CLI, without "--"
// Except that persist_filter should be a callback (key, value) => newValue
const srv = new AntiEtcd({ ...configuration });

// Start server
srv.start();

// Make a local API call in generic style:
let res = await srv.api('kv_txn'|'kv_range'|'kv_put'|'kv_deleterange'|'lease_grant'|'lease_revoke'|'lease_keepalive', { ...params });

// Or function-style:
res = await srv.txn(params);
res = await srv.range(params);
res = await srv.put(params);
res = await srv.deleterange(params);
res = await srv.lease_grant(params);
res = await srv.lease_revoke(params);
res = await srv.lease_keepalive(params);

// Error handling:
try
{
    res = await srv.txn(params);
}
catch (e)
{
    if (e instanceof AntiEtcd.RequestError)
    {
        // e.code is HTTP code
        // e.message is error message
    }
}

// Watch API:
const watch_id = await srv.create_watch(params, (message) => console.log(message));
await srv.cancel_watch(watch_id);

// Stop server
srv.stop();

About Persistence

Persistence is very simple: full database is dumped into JSON, gzipped and saved as file.

By default, it is written and fsynced on disk on every change, but it can be configured to dump DB on disk at fixed intervals, for example, at most every 500 ms - of course, at expense of slightly reduced crash resiliency (example: --persist_interval 500).

You can also specify a filter to exclude some data from persistence by using the option --persist_filter ./filter.js. Persistence filter code example:

function example_filter(cfg)
{
    // <cfg> contains all command-line options
    const prefix = cfg.exclude_keys;
    if (!prefix)
    {
        return null;
    }
    return (key, value) =>
    {
        if (key.substr(0, prefix.length) == prefix)
        {
            // Skip all keys with prefix from persistence
            return undefined;
        }
        if (key === '/statistics')
        {
            // Return <unneeded_key> from inside value
            const decoded = JSON.parse(value);
            return JSON.stringify({ ...decoded, unneeded_key: undefined });
        }
        return value;
    };
}

module.exports = example_filter;

Supported etcd APIs

NOTE: key, value and range_end are always encoded in base64, like in original etcd.

Range requests are only supported across "directories" separated by /.

It means that in range requests key must always end with / and range_end must always end with 0, and that such request will return a whole subtree of keys.

/v3/kv/txn

Request:

type TxnRequest = {
  compare?: (
    { key: string, target: "MOD", mod_revision: number, result?: "LESS" }
    | { key: string, target: "CREATE", create_revision: number, result?: "LESS" }
    | { key: string, target: "VERSION", version: number, result?: "LESS" }
    | { key: string, target: "LEASE", lease: string, result?: "LESS" }
    | { key: string, target: "VALUE", value: string }
  )[],
  success?: (
    { request_put: PutRequest }
    | { request_range: RangeRequest }
    | { request_delete_range: DeleteRangeRequest }
  )[],
  failure?: (
    { request_put: PutRequest }
    | { request_range: RangeRequest }
    | { request_delete_range: DeleteRangeRequest }
  )[],
  serializable?: boolean,
}

serializable allows to serve read-only requests from follower even if stale_read is not enabled.

Response:

type TxnResponse = {
  header: { revision: number },
  succeeded: boolean,
  responses: (
    { response_put: PutResponse }
    | { response_range: RangeResponse }
    | { response_delete_range: DeleteRangeResponse }
  )[],
}

/v3/kv/put

Request:

type PutRequest = {
  key: string,
  value: string,
  lease?: string,
}

Other parameters are not supported: prev_kv, ignore_value, ignore_lease.

Response:

type PutResponse = {
  header: { revision: number },
}

/v3/kv/range

Request:

type RangeRequest = {
  key: string,
  range_end?: string,
  keys_only?: boolean,
  serializable?: boolean,
}

serializable allows to serve read-only requests from follower even if stale_read is not enabled.

Other parameters are not supported: revision, limit, sort_order, sort_target, count_only, min_mod_revision, max_mod_revision, min_create_revision, max_create_revision.

Response:

type RangeResponse = {
  header: { revision: number },
  kvs: { key: string }[] | {
    key: string,
    value: string,
    lease?: string,
    mod_revision: number,
  }[],
}

/v3/kv/deleterange

Request:

type DeleteRangeRequest = {
  key: string,
  range_end?: string,
}

Other parameters are not supported: prev_kv.

Response:

type DeleteRangeResponse = {
  header: { revision: number },
  // number of deleted keys
  deleted: number,
}

/v3/lease/grant

Request:

type LeaseGrantRequest = {
  ID?: string,
  TTL: number,
}

Response:

type LeaseGrantResponse = {
  header: { revision: number },
  ID: string,
  TTL: number,
}

/v3/lease/keepalive

Request:

type LeaseKeepaliveRequest = {
  ID: string,
}

Response:

type LeaseKeepaliveResponse = {
  result: {
    header: { revision: number },
    ID: string,
    TTL: number,
  }
}

/v3/lease/revoke or /v3/kv/lease/revoke

Request:

type LeaseRevokeRequest = {
  ID: string,
}

Response:

type LeaseRevokeResponse = {
  header: { revision: number },
}

Websocket-based watch APIs

Client-to-server message format:

type ClientMessage =
  { create_request: {
    key: string,
    range_end?: string,
    start_revision?: number,
    watch_id?: string,
  } }
  | { cancel_request: {
    watch_id: string,
  } }
  | { progress_request: {} }

Server-to-client message format:

type ServerMessage = {
  result: {
    header: { revision: number },
    watch_id: string,
    created?: boolean,
    canceled?: boolean,
    compact_revision?: number,
    events?: {
      type: 'PUT'|'DELETE',
      kv: {
        key: string,
        value: string,
        lease?: string,
        mod_revision: number,
      },
    }[],
  }
} | { error: 'bad-json' } | { error: 'empty-message' }

HTTP Error Codes

  • 400 for invalid requests
  • 404 for unsupported API / URL not found
  • 405 for non-POST request method
  • 501 for unsupported API feature - non-directory range queries and so on
  • 502 for server is stopping
  • 503 for quorum-related errors - quorum not available and so on

Author and License

Author: Vitaliy Filippov, 2024

License: Mozilla Public License 2.0 or Vitastor Network Public License 1.1