iobroker.sql 4.8.0 → 4.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/build/lib/connection-options.js +24 -0
- package/build/lib/connection-options.js.map +1 -1
- package/build/lib/errors.js +20 -0
- package/build/lib/errors.js.map +1 -1
- package/build/lib/mssql-client.js +5 -4
- package/build/lib/mssql-client.js.map +1 -1
- package/build/lib/optional-drivers.js.map +1 -1
- package/build/lib/sql-string.js +13 -0
- package/build/lib/sql-string.js.map +1 -1
- package/build/lib/sqlite3-client.js +9 -1
- package/build/lib/sqlite3-client.js.map +1 -1
- package/build/main.js +100 -7
- package/build/main.js.map +1 -1
- package/io-package.json +14 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ You can leave port 0 if the default port is desired.
|
|
|
18
18
|
|
|
19
19
|
## Connection Settings
|
|
20
20
|
- **DB Type**: Type of the SQL DB: MySQL, PostgreSQL, MS-SQL or SQLite3
|
|
21
|
-
- **Host**: IP address or host name with SQL Server
|
|
21
|
+
- **Host**: IP address or host name with SQL Server. MS SQL also takes the port here (`192.168.1.5:1433`); an IPv6 address with port goes in brackets (`[2001:db8::1]:1433`)
|
|
22
22
|
- **Unix socket** (MySQL only): Path of a local unix socket, e.g. `/var/run/mysqld/mysqld.sock`. When set, the adapter connects through that socket and ignores Host and Port, which are hidden. Useful when the server runs on the same machine — it is faster than TCP — and it is the only way to reach a database on the Docker host from a container on a macvlan network.
|
|
23
23
|
- **Port**: Port of SQL Server (leave blank if not sure)
|
|
24
24
|
- **Database name**: Database name. Default iobroker
|
|
@@ -683,6 +683,11 @@ sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
|
|
683
683
|
-->
|
|
684
684
|
|
|
685
685
|
## Changelog
|
|
686
|
+
### 4.8.1 (2026-11-28)
|
|
687
|
+
* (@Schimi1983) While the database is not reachable, the values are kept for the next write without trying a new connection for every value, and without an error line per value; writes waiting for a connection no longer stay in memory until the database is back. The regular flush of the buffers and the stop use at most 10 connections at once instead of up to 100 (#587)
|
|
688
|
+
* (@Schimi1983) Fewer lost values in special setups: PostgreSQL restarts (`57P01`, `57P03`, ...) and aborted connections count as connection errors, so the values are kept for the next write; SQLite waits up to 5 s instead of 1 s for a file that another program has locked; the PostgreSQL database name keeps its upper case letters and may contain `-`; an MS SQL server given as IPv6 address works, with a port in brackets: `[2001:db8::1]:1433` (#587)
|
|
689
|
+
* (@GermanBluefox) PostgreSQL: a connection whose session the server ended (e.g. terminated by an administrator, server restart) is no longer put back into the pool, so the next statement no longer fails once with "Connection terminated unexpectedly"
|
|
690
|
+
|
|
686
691
|
### 4.8.0 (2026-11-28)
|
|
687
692
|
* (@DutchmanNL) PostgreSQL: an aggregated `getHistory` of one datapoint reads the rows in batches of 2000 (pg-cursor) and aggregates them batch by batch, instead of holding the whole range in memory first. The answer stays the same; without pg-cursor, with pg-native, and for `integralTotal` the values are read as before (#516)
|
|
688
693
|
* (@Schimi1983) MS SQL: new tables store names and string values as Unicode (`nvarchar`, `N'...'`), so Cyrillic letters or emojis no longer become `?`, and the names compare case-sensitively. The database name is put in brackets, so a name like `io-broker` works. Existing tables are not changed; the README explains how to convert them (#588)
|
|
@@ -699,10 +704,6 @@ sendTo('sql.0', 'getEnabledDPs', {}, function (result) {
|
|
|
699
704
|
* (@Schimi1983) "Clean up" no longer deletes the history of datapoints that are logged under an alias name, and stops with an error instead of deleting everything when the object database cannot be read (#587)
|
|
700
705
|
* (@Schimi1983) MySQL/MariaDB: new tables use `utf8mb4`, so values, IDs and sources with emojis are stored instead of failing, and the names compare case-sensitively (`utf8mb4_bin`), also in a database created by a hoster. Existing tables are not changed; the README explains how to convert them (#588)
|
|
701
706
|
|
|
702
|
-
### 4.5.0 (2026-11-10)
|
|
703
|
-
* (@Schimi1983) The database password is stored encrypted (`encryptedNative`). A password saved with an older version is encrypted once on the first start, the instance then restarts by itself. Note: after going back to 4.4.1 or older, enter the password in the instance settings again (#587)
|
|
704
|
-
* (@DutchmanNL) PostgreSQL: new opt-in setting "Relax PostgreSQL commit durability" runs the adapter's connections with `synchronous_commit=off`, so commits no longer wait for the disk flush. This strongly reduces disk I/O and wear. A crash of the PostgreSQL server can lose the values of the last few hundred milliseconds; the database cannot be corrupted (#516)
|
|
705
|
-
|
|
706
707
|
[Older changelogs can be found there](CHANGELOG_OLD.md)
|
|
707
708
|
|
|
708
709
|
## License
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildMySQLOptions = buildMySQLOptions;
|
|
4
|
+
exports.splitServer = splitServer;
|
|
4
5
|
/**
|
|
5
6
|
* Build the mysql2 connection options for a configuration.
|
|
6
7
|
*
|
|
@@ -35,4 +36,27 @@ function buildMySQLOptions(config) {
|
|
|
35
36
|
ssl,
|
|
36
37
|
};
|
|
37
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Split the host of the MS SQL settings into server and port.
|
|
41
|
+
*
|
|
42
|
+
* The host may carry the port (`db.example:1433`). It used to be cut at the first colon, so an IPv6
|
|
43
|
+
* address like `2001:db8::1` became the server `2001` with the port `NaN` (#587). An IPv6 address
|
|
44
|
+
* with a port is written in brackets: `[2001:db8::1]:1433`.
|
|
45
|
+
*
|
|
46
|
+
* @param server the host as configured
|
|
47
|
+
* @returns the server without port, and the port if one was given
|
|
48
|
+
*/
|
|
49
|
+
function splitServer(server) {
|
|
50
|
+
// [2001:db8::1]:1433 or [2001:db8::1]
|
|
51
|
+
const bracketed = /^\[([^\]]+)\](?::(\d+))?$/.exec(server);
|
|
52
|
+
if (bracketed) {
|
|
53
|
+
return bracketed[2] ? { server: bracketed[1], port: parseInt(bracketed[2], 10) } : { server: bracketed[1] };
|
|
54
|
+
}
|
|
55
|
+
// host:port - with more than one colon it is an IPv6 address without port
|
|
56
|
+
const parts = server.split(':');
|
|
57
|
+
if (parts.length === 2 && /^\d+$/.test(parts[1])) {
|
|
58
|
+
return { server: parts[0], port: parseInt(parts[1], 10) };
|
|
59
|
+
}
|
|
60
|
+
return { server };
|
|
61
|
+
}
|
|
38
62
|
//# sourceMappingURL=connection-options.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection-options.js","sourceRoot":"","sources":["../../src/lib/connection-options.ts"],"names":[],"mappings":";;AAqBA,8CAmBC;
|
|
1
|
+
{"version":3,"file":"connection-options.js","sourceRoot":"","sources":["../../src/lib/connection-options.ts"],"names":[],"mappings":";;AAqBA,8CAmBC;AAYD,kCAYC;AA3DD;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,iBAAiB,CAAC,MAA6B;IAC3D,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,CAAC,CAAC,MAAM,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IAE7F,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;QACpB,OAAO;YACH,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,EAAE;YACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,EAAE;YAC/B,GAAG;SACN,CAAC;IACN,CAAC;IAED,OAAO;QACH,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,gCAAgC;QACnD,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,EAAE;QACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,EAAE;QAC/B,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,SAAS;QAC9B,GAAG;KACN,CAAC;AACN,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,WAAW,CAAC,MAAc;IACtC,sCAAsC;IACtC,MAAM,SAAS,GAAG,2BAA2B,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC3D,IAAI,SAAS,EAAE,CAAC;QACZ,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAChH,CAAC;IACD,0EAA0E;IAC1E,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAChC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;IAC9D,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,CAAC;AACtB,CAAC"}
|
package/build/lib/errors.js
CHANGED
|
@@ -146,11 +146,23 @@ function counterTypeMismatch(id, storageType) {
|
|
|
146
146
|
const CONNECTION_ERROR_CODES = [
|
|
147
147
|
'PROTOCOL_CONNECTION_LOST', // mysql2: server closed the socket (e.g. after wait_timeout)
|
|
148
148
|
'ECONNRESET',
|
|
149
|
+
'ECONNABORTED',
|
|
149
150
|
'EPIPE',
|
|
150
151
|
'ETIMEDOUT',
|
|
151
152
|
'ECONNCLOSED', // mssql
|
|
152
153
|
'ENOTOPEN', // mssql
|
|
153
154
|
'ESOCKET', // mssql/tedious
|
|
155
|
+
// PostgreSQL (SQLSTATE): the server has ended the session, e.g. when it shuts down or restarts, or
|
|
156
|
+
// refuses connections while it starts. pg passes these to the statement that was running. Unlike
|
|
157
|
+
// the severity text (see isConnectionError()), the codes do not depend on lc_messages.
|
|
158
|
+
'57P01', // admin_shutdown, also pg_terminate_backend()
|
|
159
|
+
'57P02', // crash_shutdown
|
|
160
|
+
'57P03', // cannot_connect_now
|
|
161
|
+
'57P04', // database_dropped
|
|
162
|
+
'57P05', // idle_session_timeout
|
|
163
|
+
'08000', // connection_exception
|
|
164
|
+
'08003', // connection_does_not_exist
|
|
165
|
+
'08006', // connection_failure
|
|
154
166
|
// the server cannot be reached at all (while connecting)
|
|
155
167
|
'ECONNREFUSED',
|
|
156
168
|
'EHOSTUNREACH',
|
|
@@ -191,6 +203,14 @@ function isConnectionError(err) {
|
|
|
191
203
|
if (error.fatal === true) {
|
|
192
204
|
return true;
|
|
193
205
|
}
|
|
206
|
+
// pg: the server ends the session after every FATAL or PANIC error, e.g. 57P01 when the backend is
|
|
207
|
+
// terminated. pg notices that only when the socket closes a moment later, so the client went back
|
|
208
|
+
// into the pool, and the next statement on it failed with "Connection terminated unexpectedly".
|
|
209
|
+
// pg takes the severity from the localized field, so a server with e.g. Russian messages does not
|
|
210
|
+
// send "FATAL"; the codes in CONNECTION_ERROR_CODES cover the usual cases there.
|
|
211
|
+
if (error.severity === 'FATAL' || error.severity === 'PANIC') {
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
194
214
|
if (typeof error.code === 'string' && CONNECTION_ERROR_CODES.includes(error.code)) {
|
|
195
215
|
return true;
|
|
196
216
|
}
|
package/build/lib/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":";;AA4BA,kCA0EC;AA4CD,kDAKC;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":";;AA4BA,kCA0EC;AA4CD,kDAKC;AAwDD,8CA0BC;AAzOD,gGAAgG;AAChG,kEAAkE;AAClE,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB;;;;GAIG;AACH,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,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AAO,KAAK,QAAQ;QACjC,yBAAyB,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,OAAiB,CAAC,CAAC,CACzE,CAAC;AACN,CAAC"}
|
|
@@ -7,6 +7,7 @@ exports.MSSQLClientPool = exports.MSSQLClient = void 0;
|
|
|
7
7
|
const connection_factory_1 = require("./connection-factory");
|
|
8
8
|
const sql_client_1 = __importDefault(require("./sql-client"));
|
|
9
9
|
const sql_client_pool_1 = require("./sql-client-pool");
|
|
10
|
+
const connection_options_1 = require("./connection-options");
|
|
10
11
|
class MSSQLConnectionFactory extends connection_factory_1.ConnectionFactory {
|
|
11
12
|
ConnectionPool;
|
|
12
13
|
Request;
|
|
@@ -22,10 +23,10 @@ class MSSQLConnectionFactory extends connection_factory_1.ConnectionFactory {
|
|
|
22
23
|
e => callback(new Error(`Node.js DB driver "mssql" could not be loaded: ${e}`)));
|
|
23
24
|
return;
|
|
24
25
|
}
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
options.
|
|
26
|
+
const { server, port } = (0, connection_options_1.splitServer)(options.server);
|
|
27
|
+
options.server = server;
|
|
28
|
+
if (port) {
|
|
29
|
+
options.port = port;
|
|
29
30
|
}
|
|
30
31
|
options.pool ||= {};
|
|
31
32
|
options.pool.min = 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mssql-client.js","sourceRoot":"","sources":["../../src/lib/mssql-client.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAyD;AACzD,8DAAqC;AACrC,uDAAmE;
|
|
1
|
+
{"version":3,"file":"mssql-client.js","sourceRoot":"","sources":["../../src/lib/mssql-client.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAyD;AACzD,8DAAqC;AACrC,uDAAmE;AACnE,6DAAmD;AAMnD,MAAM,sBAAuB,SAAQ,sCAAiB;IAC1C,cAAc,CAAoC;IAClD,OAAO,CAA6B;IAE5C,cAAc,C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9E,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,CACH,UAA0B,EAC1B,GAAW,EACX,QAAoE;QAEpE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAC7C,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,GAAkB,EAAE,MAAmB,EAAE,EAAE;YAC3D,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;QACrC,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AAED,MAAa,WAAY,SAAQ,oBAAS;IACtC,YAAY,OAAqB;QAC7B,KAAK,CAAC,OAAO,EAAE,IAAI,sBAAsB,EAAE,CAAC,CAAC;IACjD,CAAC;CACJ;AAJD,kCAIC;AAED,MAAa,eAAgB,SAAQ,+BAAa;IAC9C,YAAY,WAAuB,EAAE,UAAwB;QACzD,KAAK,CAAC,WAAW,EAAE,UAAU,EAAE,IAAI,sBAAsB,EAAE,CAAC,CAAC;IACjE,CAAC;CACJ;AAJD,0CAIC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optional-drivers.js","sourceRoot":"","sources":["../../src/lib/optional-drivers.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;
|
|
1
|
+
{"version":3,"file":"optional-drivers.js","sourceRoot":"","sources":["../../src/lib/optional-drivers.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAoDH,oCAEC;AAXD;;;;;;;;GAQG;AACH,SAAgB,YAAY,CAAI,IAAY;IACxC,OAAO,MAAM,CAAC,IAAI,CAAe,CAAC;AACtC,CAAC"}
|
package/build/lib/sql-string.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.quoteSqlString = quoteSqlString;
|
|
4
4
|
exports.quoteNString = quoteNString;
|
|
5
5
|
exports.quoteMsIdentifier = quoteMsIdentifier;
|
|
6
|
+
exports.quotePgIdentifier = quotePgIdentifier;
|
|
6
7
|
exports.quotePgString = quotePgString;
|
|
7
8
|
exports.numberOrNull = numberOrNull;
|
|
8
9
|
exports.boolOrNull = boolOrNull;
|
|
@@ -52,6 +53,18 @@ function quoteNString(value) {
|
|
|
52
53
|
function quoteMsIdentifier(name) {
|
|
53
54
|
return `[${String(name).replace(/]/g, ']]')}]`;
|
|
54
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Return `name` as a PostgreSQL identifier in double quotes, e.g. a database name.
|
|
58
|
+
*
|
|
59
|
+
* Unquoted, PostgreSQL folds a name to lower case and refuses `-`: `CREATE DATABASE IoBroker` created
|
|
60
|
+
* `iobroker`, the adapter then connected to `IoBroker`, failed, and created it again every 30 s.
|
|
61
|
+
* A `"` in the name is doubled.
|
|
62
|
+
*
|
|
63
|
+
* @param name the identifier
|
|
64
|
+
*/
|
|
65
|
+
function quotePgIdentifier(name) {
|
|
66
|
+
return `"${String(name).replace(/"/g, '""')}"`;
|
|
67
|
+
}
|
|
55
68
|
/**
|
|
56
69
|
* Return `value` as a PostgreSQL escape string literal (`E'...'`), including the prefix and quotes.
|
|
57
70
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-string.js","sourceRoot":"","sources":["../../src/lib/sql-string.ts"],"names":[],"mappings":";;AAqBA,wCAGC;AAUD,oCAEC;AAUD,8CAEC;AAaD,sCAGC;AAUD,oCAEC;AASD,gCASC;
|
|
1
|
+
{"version":3,"file":"sql-string.js","sourceRoot":"","sources":["../../src/lib/sql-string.ts"],"names":[],"mappings":";;AAqBA,wCAGC;AAUD,oCAEC;AAUD,8CAEC;AAWD,8CAEC;AAaD,sCAGC;AAUD,oCAEC;AASD,gCAS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C,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;AACxF,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,KAAc,EAAE,OAAgB;IACvD,MAAM,CAAC,GAAG,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,eAAM,EAAC,KAAK,CAAC,CAAC;IACvE,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QACb,OAAO,MAAM,CAAC;IAClB,CAAC;IACD,IAAI,OAAO,EAAE,CAAC;QACV,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;AAChC,CAAC"}
|
|
@@ -3,11 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SQLite3ClientPool = exports.SQLite3Client = exports.SQLite3ConnectionFactory = void 0;
|
|
6
|
+
exports.SQLite3ClientPool = exports.SQLite3Client = exports.SQLite3ConnectionFactory = exports.SQLITE_BUSY_TIMEOUT = void 0;
|
|
7
7
|
const connection_factory_1 = require("./connection-factory");
|
|
8
8
|
const sql_client_1 = __importDefault(require("./sql-client"));
|
|
9
9
|
const sql_client_pool_1 = require("./sql-client-pool");
|
|
10
10
|
const optional_drivers_1 = require("./optional-drivers");
|
|
11
|
+
/**
|
|
12
|
+
* Milliseconds a statement waits while another process holds a lock on the database file, e.g. a
|
|
13
|
+
* viewer or a backup writing it. sqlite3 waits 1 s by default; after that the insert failed with
|
|
14
|
+
* SQLITE_BUSY, which is no connection error, so its rows were lost (#587).
|
|
15
|
+
*/
|
|
16
|
+
exports.SQLITE_BUSY_TIMEOUT = 5000;
|
|
11
17
|
class SQLite3ConnectionFactory extends connection_factory_1.ConnectionFactory {
|
|
12
18
|
Database;
|
|
13
19
|
openConnection(options, callback) {
|
|
@@ -27,6 +33,7 @@ class SQLite3ConnectionFactory extends connection_factory_1.ConnectionFactory {
|
|
|
27
33
|
callback(err);
|
|
28
34
|
}
|
|
29
35
|
else {
|
|
36
|
+
db.configure('busyTimeout', exports.SQLITE_BUSY_TIMEOUT);
|
|
30
37
|
callback(null, db);
|
|
31
38
|
}
|
|
32
39
|
});
|
|
@@ -37,6 +44,7 @@ class SQLite3ConnectionFactory extends connection_factory_1.ConnectionFactory {
|
|
|
37
44
|
callback(err);
|
|
38
45
|
}
|
|
39
46
|
else {
|
|
47
|
+
db.configure('busyTimeout', exports.SQLITE_BUSY_TIMEOUT);
|
|
40
48
|
callback(null, db);
|
|
41
49
|
}
|
|
42
50
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite3-client.js","sourceRoot":"","sources":["../../src/lib/sqlite3-client.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAyD;AACzD,8DAAqC;AACrC,uDAAmE;AAEnE,yDAK4B;
|
|
1
|
+
{"version":3,"file":"sqlite3-client.js","sourceRoot":"","sources":["../../src/lib/sqlite3-client.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAyD;AACzD,8DAAqC;AACrC,uDAAmE;AAEnE,yDAK4B;AAI5B;;;;GAIG;AACU,QAAA,mBAAmB,GAAG,IAAI,CAAC;AAIxC,MAAa,wBAAyB,SAAQ,sCAAiB;IACnD,QAAQ,CAAyC;IAEzD,cAAc,CAAC,OAAuB,EAAE,QAAmE;QACvG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACjB,KAAK,IAAA,+BAAY,EAAgB,SAAS,CAAC,CAAC,IAAI,CAC5C,OAAO,CAAC,EAAE;gBAC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;YACJ,QAAQ,EAAE,EAAE,CAAC;QACjB,CAAC;IACL,CAAC;IAED,OAAO,CAAI,EAAmB,EAAE,GAAW,EAAE,QAAwD;QACjG,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAC;IAC9B,CAAC;CACJ;AAjDD,4DAiDC;AAED,MAAa,aAAc,SAAQ,oBAAS;IACxC,YAAY,aAA6B;QACrC,KAAK,CAAC,aAAa,EAAE,IAAI,wBAAwB,EAAE,CAAC,CAAC;IACzD,CAAC;CACJ;AAJD,sCAIC;AAED,MAAa,iBAAkB,SAAQ,+BAAa;IAChD,YAAY,WAAuB,EAAE,aAA6B;QAC9D,KAAK,CAAC,WAAW,EAAE,aAAa,EAAE,IAAI,wBAAwB,EAAE,CAAC,CAAC;IACtE,CAAC;CACJ;AAJD,8CAIC"}
|
package/build/main.js
CHANGED
|
@@ -50,6 +50,7 @@ const values_1 = require("./lib/values");
|
|
|
50
50
|
const password_1 = require("./lib/password");
|
|
51
51
|
const names_1 = require("./lib/names");
|
|
52
52
|
const statement_1 = require("./lib/statement");
|
|
53
|
+
const sql_string_1 = require("./lib/sql-string");
|
|
53
54
|
const statistics_1 = require("./lib/statistics");
|
|
54
55
|
const postgresql_client_1 = require("./lib/postgresql-client");
|
|
55
56
|
const sqlite3_client_1 = require("./lib/sqlite3-client");
|
|
@@ -200,6 +201,22 @@ const STAT_TABLES = ['ts_number', 'ts_string', 'ts_bool', 'ts_counter'];
|
|
|
200
201
|
const MAX_TASKS = 100;
|
|
201
202
|
/** Milliseconds a pooled connection may be idle before it is replaced (`max_age` of the pool) */
|
|
202
203
|
const POOL_MAX_IDLE_AGE = 60_000;
|
|
204
|
+
/**
|
|
205
|
+
* Connections the inserts use at most at the same time (or `maxConnections`, if lower). The flush of
|
|
206
|
+
* all buffers (every 10 minutes and on stop) starts one insert per datapoint; with the default of 100
|
|
207
|
+
* connections it opened up to 100 connections at once, and the pool closed all but two right after.
|
|
208
|
+
* Hosted databases often allow fewer connections per user ("Too many connections"). The other inserts
|
|
209
|
+
* wait and get a returned connection directly (#587).
|
|
210
|
+
*/
|
|
211
|
+
const INSERT_CONNECTIONS = 10;
|
|
212
|
+
/**
|
|
213
|
+
* Milliseconds after a failed connect in which no new connect is tried: the writes in between keep
|
|
214
|
+
* their rows for the next write at once, instead of every value starting a connect and waiting for
|
|
215
|
+
* its timeout while the database is down (#587).
|
|
216
|
+
*/
|
|
217
|
+
const UNREACHABLE_RETRY_DELAY = 5_000;
|
|
218
|
+
/** Code of the error a borrow fails with while the database is known to be unreachable */
|
|
219
|
+
const DB_UNREACHABLE = 'IOB_DB_UNREACHABLE';
|
|
203
220
|
/** Milliseconds between two warnings about values kept while the database is not reachable */
|
|
204
221
|
const KEPT_LOG_INTERVAL = 60_000;
|
|
205
222
|
/**
|
|
@@ -397,14 +414,38 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
397
414
|
this.log.debug((0, statement_1.statementToString)(statement));
|
|
398
415
|
}
|
|
399
416
|
}
|
|
400
|
-
|
|
417
|
+
/**
|
|
418
|
+
* Borrow a connection, at most `limit` at the same time; beyond that the callback waits for a
|
|
419
|
+
* returned connection.
|
|
420
|
+
*
|
|
421
|
+
* After a failed connect the database counts as unreachable: for {@link UNREACHABLE_RETRY_DELAY}
|
|
422
|
+
* every borrow fails at once with the error code {@link DB_UNREACHABLE}, then one borrow at a time
|
|
423
|
+
* checks whether the server is back.
|
|
424
|
+
*
|
|
425
|
+
* @param callback gets the connection, or the error
|
|
426
|
+
* @param limit connections at the same time, `maxConnections` by default
|
|
427
|
+
*/
|
|
428
|
+
borrowClientFromPool(callback, limit = this.config.maxConnections) {
|
|
401
429
|
if (!this.clientPool) {
|
|
402
430
|
this.setConnected(false);
|
|
403
431
|
return callback(new Error('No database connection'));
|
|
404
432
|
}
|
|
405
|
-
|
|
433
|
+
let probe = false;
|
|
434
|
+
if (this.#unreachable) {
|
|
435
|
+
if (this.#probing || Date.now() - this.#unreachable.since < UNREACHABLE_RETRY_DELAY) {
|
|
436
|
+
return callback(this.#unreachableError());
|
|
437
|
+
}
|
|
438
|
+
// this borrow checks whether the server is back, the others fail at once until it is done
|
|
439
|
+
this.#probing = probe = true;
|
|
440
|
+
}
|
|
441
|
+
if (this.activeConnections >= limit) {
|
|
406
442
|
if (this.logConnectionUsage) {
|
|
407
|
-
this.log.debug(`Borrow connection not possible: ${this.activeConnections} >=
|
|
443
|
+
this.log.debug(`Borrow connection not possible: ${this.activeConnections} >= ${limit} - Store for Later`);
|
|
444
|
+
}
|
|
445
|
+
if (probe) {
|
|
446
|
+
// the connections in use may be connects that are still waiting for their timeout
|
|
447
|
+
this.#probing = false;
|
|
448
|
+
return callback(this.#unreachableError());
|
|
408
449
|
}
|
|
409
450
|
this.poolBorrowGuard.push(callback);
|
|
410
451
|
return;
|
|
@@ -414,6 +455,9 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
414
455
|
this.log.debug(`Borrow connection from pool: ${this.activeConnections} now`);
|
|
415
456
|
}
|
|
416
457
|
this.clientPool.borrow((err, client) => {
|
|
458
|
+
if (probe) {
|
|
459
|
+
this.#probing = false;
|
|
460
|
+
}
|
|
417
461
|
if (!err && client) {
|
|
418
462
|
// Log connection-level driver errors (ECONNRESET, PROTOCOL_CONNECTION_LOST). The
|
|
419
463
|
// crash safety itself does not depend on this listener: SQLClient handles the
|
|
@@ -432,6 +476,10 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
432
476
|
}
|
|
433
477
|
});
|
|
434
478
|
}
|
|
479
|
+
if (this.#unreachable) {
|
|
480
|
+
this.#unreachable = null;
|
|
481
|
+
this.resetConnectionError();
|
|
482
|
+
}
|
|
435
483
|
// A real, working connection was handed out - this, not the mere existence of a pool
|
|
436
484
|
// object, is what info.connection reports (#374).
|
|
437
485
|
this.consecutiveBorrowFailures = 0;
|
|
@@ -444,10 +492,41 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
444
492
|
if (++this.consecutiveBorrowFailures >= 5) {
|
|
445
493
|
this.setConnected(false);
|
|
446
494
|
}
|
|
495
|
+
const error = err || new Error('No client');
|
|
496
|
+
this.#unreachable = { since: Date.now(), error };
|
|
497
|
+
// The callbacks waiting for a connection were only served by a returned working
|
|
498
|
+
// connection, so after a failed connect they waited until the server was back and the
|
|
499
|
+
// next write returned one: their rows stayed in memory outside the limits of
|
|
500
|
+
// #keepForRetry, and the stop waited for them until the stopTimeout. They would fail
|
|
501
|
+
// the same way now.
|
|
502
|
+
this.#failWaiters();
|
|
447
503
|
}
|
|
448
504
|
callback(err, client);
|
|
449
505
|
});
|
|
450
506
|
}
|
|
507
|
+
/** Set after a failed connect, cleared by the next connection that works */
|
|
508
|
+
#unreachable = null;
|
|
509
|
+
/** Whether a borrow is checking if the unreachable server is back */
|
|
510
|
+
#probing = false;
|
|
511
|
+
/** The error of a borrow while the database is known to be unreachable */
|
|
512
|
+
#unreachableError() {
|
|
513
|
+
const cause = this.#unreachable?.error;
|
|
514
|
+
return Object.assign(new Error(`Database not reachable${cause ? `: ${(0, errors_1.formatError)(cause)}` : ''}`, { cause }), {
|
|
515
|
+
code: DB_UNREACHABLE,
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
/** Fail every callback that waits for a connection; there will be none to hand them */
|
|
519
|
+
#failWaiters() {
|
|
520
|
+
if (!this.poolBorrowGuard.length) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const waiters = this.poolBorrowGuard;
|
|
524
|
+
this.poolBorrowGuard = [];
|
|
525
|
+
const error = this.clientPool ? this.#unreachableError() : new Error('No database connection');
|
|
526
|
+
for (const waiter of waiters) {
|
|
527
|
+
waiter(error);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
451
530
|
returnClientToPool(client) {
|
|
452
531
|
if (client) {
|
|
453
532
|
this.activeConnections--;
|
|
@@ -701,7 +780,9 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
701
780
|
let mySQLOptions;
|
|
702
781
|
let postgreSQLOptions;
|
|
703
782
|
const poolOptions = {
|
|
704
|
-
|
|
783
|
+
// the connections of a flush are reused by the next inserts instead of being closed and opened
|
|
784
|
+
// again; idle ones are still closed after `max_age`
|
|
785
|
+
max_idle: this.config.dbtype === 'sqlite' ? 1 : Math.min(this.config.maxConnections, INSERT_CONNECTIONS) || 2,
|
|
705
786
|
};
|
|
706
787
|
if (this.config.dbtype !== 'sqlite') {
|
|
707
788
|
// A connection that was idle for a while is opened anew instead of being reused: servers
|
|
@@ -785,7 +866,9 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
785
866
|
}, 30000);
|
|
786
867
|
return;
|
|
787
868
|
}
|
|
788
|
-
|
|
869
|
+
// quoted: unquoted, PostgreSQL folds the name to lower case and refuses `-`
|
|
870
|
+
const create = `CREATE DATABASE ${(0, sql_string_1.quotePgIdentifier)(this.config.dbname)};`;
|
|
871
|
+
_client.execute(create, (err) => {
|
|
789
872
|
_client.disconnect();
|
|
790
873
|
const typedError = err;
|
|
791
874
|
if (typedError && typedError.code !== '42P04') {
|
|
@@ -838,6 +921,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
838
921
|
this.activeConnections = 0;
|
|
839
922
|
if (err) {
|
|
840
923
|
this.clientPool = null;
|
|
924
|
+
this.#failWaiters();
|
|
841
925
|
this.setConnected(false);
|
|
842
926
|
// JSON.stringify(Error) is "{}", and toString() of an AggregateError is just
|
|
843
927
|
// "AggregateError" - formatError() digs the real reason out of both
|
|
@@ -862,6 +946,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
862
946
|
this.log.debug(ex.stack);
|
|
863
947
|
}
|
|
864
948
|
this.clientPool = null;
|
|
949
|
+
this.#failWaiters();
|
|
865
950
|
this.activeConnections = 0;
|
|
866
951
|
this.setConnected(false);
|
|
867
952
|
this.reconnectTimeout && clearTimeout(this.reconnectTimeout);
|
|
@@ -1098,6 +1183,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1098
1183
|
this.clientPool?.close();
|
|
1099
1184
|
this.activeConnections = 0;
|
|
1100
1185
|
this.clientPool = null;
|
|
1186
|
+
this.#failWaiters();
|
|
1101
1187
|
this.setConnected(false);
|
|
1102
1188
|
this.logConnectionError(err || 'No database connection');
|
|
1103
1189
|
return cb?.(err || new Error('No database connection'));
|
|
@@ -1196,6 +1282,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1196
1282
|
this.clientPool.close();
|
|
1197
1283
|
this.activeConnections = 0;
|
|
1198
1284
|
this.clientPool = null;
|
|
1285
|
+
this.#failWaiters();
|
|
1199
1286
|
this.setConnected(false);
|
|
1200
1287
|
}
|
|
1201
1288
|
if (typeof this.finished === 'object') {
|
|
@@ -1363,6 +1450,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1363
1450
|
this.clientPool.close();
|
|
1364
1451
|
this.activeConnections = 0;
|
|
1365
1452
|
this.clientPool = null;
|
|
1453
|
+
this.#failWaiters();
|
|
1366
1454
|
this.setConnected(false);
|
|
1367
1455
|
}
|
|
1368
1456
|
// so that a second finish() (stopInstance message, then unload) does not wait forever
|
|
@@ -1997,11 +2085,16 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
1997
2085
|
*/
|
|
1998
2086
|
_insertValuesIntoDB(list, index, id, cb) {
|
|
1999
2087
|
const groups = (0, write_buffer_1.splitBatch)(list);
|
|
2088
|
+
const limit = Math.min(this.config.maxConnections, INSERT_CONNECTIONS);
|
|
2000
2089
|
this.borrowClientFromPool((err, client) => {
|
|
2001
2090
|
if (err || !client) {
|
|
2002
2091
|
this.returnClientToPool(client);
|
|
2003
2092
|
const error = err || new Error('No client');
|
|
2004
|
-
|
|
2093
|
+
// While the database is down every write fails: the failed connect is logged (once,
|
|
2094
|
+
// repeated hourly), the rows kept for the next write are reported by #keepForRetry.
|
|
2095
|
+
if (error.code !== DB_UNREACHABLE) {
|
|
2096
|
+
this.logConnectionError(error);
|
|
2097
|
+
}
|
|
2005
2098
|
cb?.(error, list);
|
|
2006
2099
|
return;
|
|
2007
2100
|
}
|
|
@@ -2032,7 +2125,7 @@ class SqlAdapter extends adapter_core_1.Adapter {
|
|
|
2032
2125
|
run(0);
|
|
2033
2126
|
};
|
|
2034
2127
|
next(0);
|
|
2035
|
-
});
|
|
2128
|
+
}, limit);
|
|
2036
2129
|
}
|
|
2037
2130
|
_executeQuery(query, id, cb) {
|
|
2038
2131
|
this.#debugStatement(query);
|