Skip to content

Commit 331aa86

Browse files
committed
in theory this implements sticky routing in a way that works in all our deployment settings
1 parent d6d780a commit 331aa86

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/packages/conat/core/server.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ export interface Options {
142142
// if true, use https when creating an internal client.
143143
ssl?: boolean;
144144

145-
// WARNING: **superclusters are NOT fully iplemented yet.**
145+
// WARNING: **superclusters are NOT implemented yet**. Maybe they
146+
// are a very bad idea and should NOT be implemented. In practice,
147+
// when I hit a need for this it's better to just explicitly make
148+
// a process with two clients, and use an explicit protocol to
149+
// connect the clusters...
146150
//
147151
// if clusterName is set, enable clustering. Each node
148152
// in the cluster must have a different name. systemAccountPassword
@@ -456,8 +460,12 @@ export class ConatServer extends EventEmitter {
456460
this.stickyClient = this.client({
457461
systemAccountPassword: this.options.systemAccountPassword,
458462
});
459-
if (!this.cluster) {
460-
// not a cluster, so we can server as the sticky routing serivce
463+
if (!this.cluster || this.options.localClusterSize != null) {
464+
// not a cluster or a local cluster, so we view THIS server
465+
// as the canonical server and it also hosts the
466+
// sticky routing service. The one case where we don't do
467+
// this is for a non-local cluster, e.g., deploying on
468+
// Kubernetes.
461469
createStickyRouter({ client: this.stickyClient });
462470
}
463471
};
@@ -1105,7 +1113,10 @@ export class ConatServer extends EventEmitter {
11051113
if (localClusterSize < 2) {
11061114
return;
11071115
}
1108-
// spawn additional servers as separate processes to form a cluster
1116+
// Spawn additional servers as *separate processes* to form a cluster.
1117+
// This is mainly used for testing, but is also very useful on
1118+
// a single powerful machine where we want more routing than a single
1119+
// nodejs process provides. This cannot do autoscaling though.
11091120
const port = this.options.port;
11101121
if (!port) {
11111122
throw Error("bug -- port must be set");

src/packages/conat/core/sticky.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export async function createStickyRouter({
113113
}
114114
};
115115
listen();
116+
return sub;
116117
}
117118

118119
const stickyRequest = reuseInFlight(

src/packages/hub/hub.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import {
2727
initConatPersist,
2828
loadConatConfiguration,
2929
} from "@cocalc/server/conat";
30-
import { initConatServer } from "@cocalc/server/conat/socketio";
30+
import {
31+
initConatServer,
32+
initStickyRouterService,
33+
} from "@cocalc/server/conat/socketio";
3134
import { init_passport } from "@cocalc/server/hub/auth";
3235
import { initialOnPremSetup } from "@cocalc/server/initial-onprem-setup";
3336
import initHandleMentions from "@cocalc/server/mentions/handle";
@@ -282,7 +285,15 @@ async function startServer(): Promise<void> {
282285
console.log(msg);
283286
}
284287

288+
// UNIQUE SERVICE: THESE ARE THE SERVICES THAT HAVE TO RUN *EXACTLY ONCE*.
285289
if (program.all || program.mentions) {
290+
if (program.mode == "kucalc") {
291+
// init kucalc it's critical to have one sticky router service running,
292+
// and this is the hub node where we do this. Do NOT do this
293+
// if there is no clustering or a local conat cluster, since then
294+
// you end up with two sticky routers.
295+
initStickyRouterService();
296+
}
286297
// kucalc: for now we just have the hub-mentions servers
287298
// do the new project pool maintenance, since there is only
288299
// one hub-stats.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
export { init as initConatServer } from "./server";
2+
import { loadConatConfiguration } from "../configuration";
3+
import { conat } from "@cocalc/backend/conat";
4+
import { createStickyRouter } from "@cocalc/conat/core/sticky";
5+
6+
export async function initStickyRouterService() {
7+
await loadConatConfiguration();
8+
const client = conat();
9+
createStickyRouter({ client });
10+
}

0 commit comments

Comments
 (0)