Skip to content

Commit

Permalink
Merge 7a3340c into fbbccb0
Browse files Browse the repository at this point in the history
  • Loading branch information
Falx committed May 6, 2022
2 parents fbbccb0 + 7a3340c commit 3367803
Show file tree
Hide file tree
Showing 30 changed files with 881 additions and 83 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ to some commonly used settings:
| `--sparqlEndpoint, -s` | | URL of the SPARQL endpoint, when using a quadstore-based configuration. |
| `--showStackTrace, -t` | false | Enables detailed logging on error output. |
| `--podConfigJson` | `./pod-config.json` | Path to the file that keeps track of dynamic Pod configurations. Only relevant when using `@css:config/dynamic.json`. |
| `--seededPodConfigJson` | | Path to the file that keeps track of seeded Pod configurations. |
| `--seededPodConfigJson`| | Path to the file that keeps track of seeded Pod configurations. |
| `--mainModulePath, -m` | | Path from where Components.js will start its lookup when initializing configurations. |
| `--workers, -w` | `1` | Run in multithreaded mode using workers. Special values are `-1` (scale to `num_cores-1`), `0` (scale to `num_cores`) and 1 (singlethreaded). |

### 🧶 Custom configurations
More substantial changes to server behavior can be achieved
Expand Down
45 changes: 39 additions & 6 deletions config/app/init/base/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"css:config/app/init/initializers/logger.json",
"css:config/app/init/initializers/server.json",
"css:config/app/init/initializers/seeded-pod.json",
"css:config/app/init/initializers/version.json"
"css:config/app/init/initializers/version.json",
"css:config/app/init/initializers/workers.json"
],
"@graph": [
{
Expand All @@ -14,12 +15,44 @@
"@type": "SequenceHandler",
"handlers": [
{ "@id": "urn:solid-server:default:LoggerInitializer" },
{ "@id": "urn:solid-server:default:BaseUrlVerifier" },
{ "@id": "urn:solid-server:default:ParallelInitializer" },
{ "@id": "urn:solid-server:default:SeededPodInitializer" },
{ "@id": "urn:solid-server:default:ServerInitializer" },
{ "@id": "urn:solid-server:default:ModuleVersionVerifier" }
{ "@id": "urn:solid-server:default:PrimaryInitializer" },
{ "@id": "urn:solid-server:default:WorkerInitializer" }
]
},
{
"comment": "This wrapped sequence handler will be executed ONLY BY THE PRIMARY PROCESS when starting the server.",
"@id": "urn:solid-server:default:PrimaryInitializer",
"@type": "ProcessHandler",
"executeOnPrimary": true,
"source": {
"comment": "These initializers will all be executed sequentially when starting the server.",
"@id": "urn:solid-server:default:PrimarySequenceInitializer",
"@type":"SequenceHandler",
"handlers": [
{ "@id": "urn:solid-server:default:BaseUrlVerifier" },
{ "@id": "urn:solid-server:default:PrimaryParallelInitializer" },
{ "@id": "urn:solid-server:default:SeededPodInitializer" },
{ "@id": "urn:solid-server:default:ModuleVersionVerifier" },
{ "@id": "urn:solid-server:default:WorkerManager" }
]
},
"clusterManager": { "@id": "urn:solid-server:default:ClusterManager" }
},
{
"comment": "This wrapped sequence handler will be executed ONLY BY THE WORKER PROCESSES when starting the server.",
"@id": "urn:solid-server:default:WorkerInitializer",
"@type": "ProcessHandler",
"executeOnPrimary": false,
"source": {
"comment": "These initializers will all be executed sequentially when starting the server.",
"@id": "urn:solid-server:default:WorkerSequenceInitializer",
"@type": "SequenceHandler",
"handlers": [
{ "@id": "urn:solid-server:default:WorkerParallelInitializer" },
{ "@id": "urn:solid-server:default:ServerInitializer" }
]
},
"clusterManager": { "@id": "urn:solid-server:default:ClusterManager" }
}
]
}
10 changes: 8 additions & 2 deletions config/app/init/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
],
"@graph": [
{
"comment": "These handlers are called whenever the server is started, and can be used to ensure that all necessary resources for booting are available.",
"@id": "urn:solid-server:default:ParallelInitializer",
"comment": "These handlers are called only for the Primary process whenever the server is started, and can be used to ensure that all necessary resources for booting are available. (in singlethreaded mode, these are always called)",
"@id": "urn:solid-server:default:PrimaryParallelInitializer",
"@type": "ParallelHandler",
"handlers": [ ]
},
{
"comment": "These handlers are called only for the workers processes whenever the server is started, and can be used to ensure that all necessary resources for booting are available. (in singlethreaded mode, these are always called)",
"@id": "urn:solid-server:default:WorkerParallelInitializer",
"@type": "ParallelHandler",
"handlers": [ ]
}
Expand Down
10 changes: 8 additions & 2 deletions config/app/init/initialize-prefilled-root.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
],
"@graph": [
{
"comment": "These handlers are called whenever the server is started, and can be used to ensure that all necessary resources for booting are available.",
"@id": "urn:solid-server:default:ParallelInitializer",
"comment": "These handlers are called only for the Primary process whenever the server is started, and can be used to ensure that all necessary resources for booting are available. (in singlethreaded mode, these are always called)",
"@id": "urn:solid-server:default:PrimaryParallelInitializer",
"@type": "ParallelHandler",
"handlers": [
{ "@id": "urn:solid-server:default:RootInitializer" }
]
},
{
"comment": "These handlers are called only for the workers processes whenever the server is started, and can be used to ensure that all necessary resources for booting are available. (in singlethreaded mode, these are always called)",
"@id": "urn:solid-server:default:WorkerParallelInitializer",
"@type": "ParallelHandler",
"handlers": [ ]
}
]
}
10 changes: 8 additions & 2 deletions config/app/init/initialize-root.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
],
"@graph": [
{
"comment": "These handlers are called whenever the server is started, and can be used to ensure that all necessary resources for booting are available.",
"@id": "urn:solid-server:default:ParallelInitializer",
"comment": "These handlers are called only for the Primary process whenever the server is started, and can be used to ensure that all necessary resources for booting are available. (in singlethreaded mode, these are always called)",
"@id": "urn:solid-server:default:PrimaryParallelInitializer",
"@type": "ParallelHandler",
"handlers": [
{ "@id": "urn:solid-server:default:RootInitializer" }
]
},
{
"comment": "These handlers are called only for the workers processes whenever the server is started, and can be used to ensure that all necessary resources for booting are available. (in singlethreaded mode, these are always called)",
"@id": "urn:solid-server:default:WorkerParallelInitializer",
"@type": "ParallelHandler",
"handlers": [ ]
}
]
}
17 changes: 17 additions & 0 deletions config/app/init/initializers/workers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^5.0.0/components/context.jsonld",
"@graph": [
{
"comment": "Spawns the required amount of workers",
"@id": "urn:solid-server:default:WorkerManager",
"@type": "WorkerManager",
"clusterManager": { "@id": "urn:solid-server:default:ClusterManager" }
},
{
"comment": "Cluster manager settings",
"@id": "urn:solid-server:default:ClusterManager",
"@type": "ClusterManager",
"workers": { "@id": "urn:solid-server:default:variable:workers" }
}
]
}
10 changes: 10 additions & 0 deletions config/app/variables/cli/cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@
"type": "string",
"describe": "Path to the file that will be used to seed pods."
}
},
{
"@type": "YargsParameter",
"name": "workers",
"options": {
"alias": "w",
"requiresArg": true,
"type": "number",
"describe": "Run the server in multithreaded mode using workers. (special values: -1: num_cores-1, 0: num_cores). Defaults to 1 (singlethreaded)"
}
}
],
"options": {
Expand Down
8 changes: 8 additions & 0 deletions config/app/variables/resolver/resolver.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
"@type": "AssetPathExtractor",
"key": "seededPodConfigJson"
}
},
{
"CombinedSettingsResolver:_resolvers_key": "urn:solid-server:default:variable:workers",
"CombinedSettingsResolver:_resolvers_value": {
"@type": "KeyExtractor",
"key": "workers",
"defaultValue": 1
}
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion config/identity/access/restricted.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"comment": "IDP-related containers require initialized resources to support authorization.",
"@id": "urn:solid-server:default:ParallelInitializer",
"@id": "urn:solid-server:default:PrimaryParallelInitializer",
"@type": "ParallelHandler",
"handlers": [
{ "@id": "urn:solid-server:default:IdpContainerInitializer" },
Expand Down
4 changes: 2 additions & 2 deletions config/identity/pod/dynamic.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
},

{
"comment": "Add to the list of initializers.",
"@id": "urn:solid-server:default:ParallelInitializer",
"comment": "Add to the list of primary initializers.",
"@id": "urn:solid-server:default:PrimaryParallelInitializer",
"@type": "ParallelHandler",
"handlers": [
{
Expand Down
5 changes: 5 additions & 0 deletions config/util/variables/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"comment": "Path to the JSON file used to seed pods.",
"@id": "urn:solid-server:default:variable:seededPodConfigJson",
"@type": "Variable"
},
{
"comment": "Run the server in multithreaded mode with the set amount of workers.",
"@id": "urn:solid-server:default:variable:workers",
"@type": "Variable"
}
]
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ export * from './init/App';
export * from './init/AppRunner';
export * from './init/BaseUrlVerifier';
export * from './init/CliResolver';
export * from './init/ClusterManager';
export * from './init/ConfigPodInitializer';
export * from './init/ContainerInitializer';
export * from './init/Initializer';
export * from './init/LoggerInitializer';
export * from './init/ModuleVersionVerifier';
export * from './init/SeededPodInitializer';
export * from './init/ServerInitializer';
export * from './init/ModuleVersionVerifier';
export * from './init/WorkerManager';

// Logging
export * from './logging/LazyLoggerFactory';
Expand Down Expand Up @@ -401,6 +403,7 @@ export * from './util/handlers/ConditionalHandler';
export * from './util/handlers/HandlerUtil';
export * from './util/handlers/MethodFilterHandler';
export * from './util/handlers/ParallelHandler';
export * from './util/handlers/ProcessHandler';
export * from './util/handlers/SequenceHandler';
export * from './util/handlers/StaticHandler';
export * from './util/handlers/StaticThrowHandler';
Expand Down
30 changes: 27 additions & 3 deletions src/init/AppRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const DEFAULT_CONFIG = resolveModulePath('config/default.json');

const DEFAULT_CLI_RESOLVER = 'urn:solid-server-app-setup:default:CliResolver';
const DEFAULT_APP = 'urn:solid-server:default:App';
const DEFAULT_CLUSTER_MANAGER = 'urn:solid-server:default:ClusterManager';

const CORE_CLI_PARAMETERS = {
config: { type: 'string', alias: 'c', default: DEFAULT_CONFIG, requiresArg: true },
Expand Down Expand Up @@ -62,10 +63,22 @@ export class AppRunner {
variableBindings: VariableBindings,
): Promise<App> {
// Create a resolver to translate (non-core) CLI parameters into values for variables
const componentsManager = await this.createComponentsManager<App>(loaderProperties, configFile);
const componentsManager = await this.createComponentsManager<any>(loaderProperties, configFile);

// Create the application using the translated variable values
return componentsManager.instantiate(DEFAULT_APP, { variables: variableBindings });
const app = await componentsManager.instantiate(DEFAULT_APP, { variables: variableBindings });

// Ensure threadsafety
const cm = await componentsManager.instantiate(DEFAULT_CLUSTER_MANAGER, { variables: variableBindings });
if (!cm.isSingleThreaded()) {
try {
cm.errorOnSingleThreadedComponents(componentsManager);
} catch (error: unknown) {
this.resolveError('Cannot run a singlethreaded-only component in a multithreaded setup!', error);
}
}

return app;
}

/**
Expand Down Expand Up @@ -139,7 +152,18 @@ export class AppRunner {
const variables = await this.resolveVariables(componentsManager, argv);

// Build and start the actual server application using the generated variable values
return await this.createApp(componentsManager, variables);
const app = await this.createApp(componentsManager, variables);

// Ensure threadsafety
const cm = await componentsManager.instantiate(DEFAULT_CLUSTER_MANAGER, variables);
if (!cm.isSingleThreaded()) {
try {
cm.errorOnSingleThreadedComponents(componentsManager);
} catch (error: unknown) {
this.resolveError('Cannot run a singlethreaded-only component in a multithreaded setup!', error);
}
}
return app;
}

/**
Expand Down
Loading

0 comments on commit 3367803

Please sign in to comment.