Skip to content

Commit 8a82608

Browse files
committed
use separate process for each persist server; also include pid in logger output
1 parent d4e03b1 commit 8a82608

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

src/packages/backend/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function initTransports() {
128128
// Similar as in debug source code, except I stuck a timestamp
129129
// at the beginning, which I like... except also aware of
130130
// non-printf formatting.
131-
const line = `${new Date().toISOString()}: ${myFormat(...args)}\n`;
131+
const line = `${new Date().toISOString()} (${process.pid}):${myFormat(...args)}\n`;
132132

133133
if (transports.console) {
134134
// the console transport:

src/packages/server/conat/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { init as initChangefeedServer } from "@cocalc/database/conat/changefeed-
44
import { init as initLLM } from "./llm";
55
import { loadConatConfiguration } from "./configuration";
66
import { createTimeService } from "@cocalc/conat/service/time";
7-
import { initPersistServer } from "@cocalc/backend/conat/persist";
8-
import { conatPersistCount, conatApiCount } from "@cocalc/backend/data";
7+
export { initConatPersist } from "./persist";
8+
import { conatApiCount } from "@cocalc/backend/data";
99

1010
export { loadConatConfiguration };
1111

@@ -19,16 +19,6 @@ export async function initConatChangefeedServer() {
1919
initChangefeedServer();
2020
}
2121

22-
export async function initConatPersist() {
23-
logger.debug("initPersistServer: sqlite3 stream persistence", {
24-
conatPersistCount,
25-
});
26-
await loadConatConfiguration();
27-
for (let i = 0; i < conatPersistCount; i++) {
28-
initPersistServer();
29-
}
30-
}
31-
3222
export async function initConatApi() {
3323
logger.debug("initConatApi: the central api services", { conatApiCount });
3424
await loadConatConfiguration();

src/packages/server/conat/persist/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { loadConatConfiguration } from "./configuration";
1+
import { loadConatConfiguration } from "../configuration";
22
import { initPersistServer } from "@cocalc/backend/conat/persist";
33
import { conatPersistCount } from "@cocalc/backend/data";
4+
import { createForkedPersistServer } from "./start-server";
45

56
import getLogger from "@cocalc/backend/logger";
67

@@ -28,8 +29,8 @@ async function createPersistCluster() {
2829
conatPersistCount,
2930
"nodes",
3031
);
31-
await loadConatConfiguration();
3232
for (let i = 0; i < conatPersistCount; i++) {
33-
initPersistServer();
33+
logger.debug("initPersistServer: starting node ", i);
34+
createForkedPersistServer();
3435
}
3536
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { loadConatConfiguration } from "../configuration";
2+
import { initPersistServer } from "@cocalc/backend/conat/persist";
3+
import { getLogger } from "@cocalc/backend/logger";
4+
import { addErrorListeners } from "@cocalc/server/metrics/error-listener";
5+
6+
const logger = getLogger("server:conat:persist:start-persist-node");
7+
8+
async function main() {
9+
logger.debug("starting forked persist node in process", process.pid);
10+
console.log("starting forked persist node in process", process.pid);
11+
addErrorListeners();
12+
await loadConatConfiguration();
13+
await initPersistServer();
14+
}
15+
16+
main();

src/packages/server/conat/socketio/start-cluster-node.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
The code here is run when conatSocketioCount > 1 (i.e., env var CONAT_SOCKETIO_COUNT).
3+
This does NOT use the socketio cluster adapter or the nodejs cluster module.
4+
Every worker process this spawns runs independently after it starts and there is
5+
no single node coordinating communications like with the socketio cluster adapter,
6+
and traffic across the cluster is minimal. This will thus *scale* much better,
7+
though this is also just using normal TCP networking for communication instead of
8+
IPC (like socketios cluster adapter). Also, the traffic between nodes is precisely
9+
what is needed for Conat, so it's really solving a differnet problem than socketio's
10+
cluster adapter, and under the hood what this actually does is much more sophisticated,
11+
with each node maintaining and serving sqlite backed streams of data about their state.
12+
13+
This code exists mainly for testing and potentially also for scaling cocalc to
14+
more traffic when running on a single machine without Kubernetes.
15+
16+
One cpu support several hundred simultaneous active connections -- if you want to
17+
have 500 active users all using projects (that also means additional connections) --
18+
you will definitely need more than one node.
19+
*/
20+
121
import "@cocalc/backend/conat/persist";
222
import { init, type Options } from "@cocalc/conat/core/server";
323
import { getUser, isAllowed } from "./auth";
@@ -6,7 +26,7 @@ import { loadConatConfiguration } from "../configuration";
626
import { getLogger } from "@cocalc/backend/logger";
727

828
async function main() {
9-
console.log("main");
29+
console.log("conat server: starting a cluster node");
1030

1131
addErrorListeners();
1232
const configDone = loadConatConfiguration();

0 commit comments

Comments
 (0)