Merge pull request #80 from SAP/worker-configuration
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 3 Jul 2022 11:19:02 +0000 (13:19 +0200)
committerGitHub <noreply@github.com>
Sun, 3 Jul 2022 11:19:02 +0000 (13:19 +0200)
README.md
src/assets/config-template.json
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStationUtils.ts
src/types/ConfigurationData.ts
src/utils/Configuration.ts

index 76d9d4738d25ff6fba9048df3c04ad4a96772a6e..448d9a8215f25647b82bf12e739bb3dc1b79f3ff 100644 (file)
--- a/README.md
+++ b/README.md
@@ -87,28 +87,22 @@ But the modifications to test have to be done to the files in the build target d
 
 **src/assets/config.json**:
 
-| Key                        | Value(s)                                         | Default Value                                                               | Value type                                                                                 | Description                                                                                                                                             |
-| -------------------------- | ------------------------------------------------ | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| supervisionUrls            |                                                  | []                                                                          | string \| string[]                                                                         | string or array of global connection URIs to OCPP-J servers                                                                                             |
-| supervisionUrlDistribution | round-robin/random/sequential                    | round-robin                                                                 | boolean                                                                                    | supervision urls distribution policy to simulated charging stations                                                                                     |
-| workerProcess              | workerSet/staticPool/dynamicPool                 | workerSet                                                                   | string                                                                                     | worker threads process type                                                                                                                             |
-| workerStartDelay           |                                                  | 500                                                                         | integer                                                                                    | milliseconds to wait at worker threads startup (only for workerSet threads process type)                                                                |
-| elementStartDelay          |                                                  | 0                                                                           | integer                                                                                    | milliseconds to wait at charging station startup                                                                                                        |
-| workerPoolMinSize          |                                                  | 4                                                                           | integer                                                                                    | worker threads pool minimum number of threads                                                                                                           |
-| workerPoolMaxSize          |                                                  | 16                                                                          | integer                                                                                    | worker threads pool maximum number of threads                                                                                                           |
-| workerPoolStrategy         | ROUND_ROBIN/LESS_RECENTLY_USED/...               | [poolifier](https://github.com/poolifier/poolifier) default: ROUND_ROBBIN   | string                                                                                     | worker threads pool [poolifier](https://github.com/poolifier/poolifier) worker choice strategy                                                          |
-| chargingStationsPerWorker  |                                                  | 1                                                                           | integer                                                                                    | number of charging stations per worker threads for the `workerSet` process type                                                                         |
-| logStatisticsInterval      |                                                  | 60                                                                          | integer                                                                                    | seconds between charging stations statistics output in the logs                                                                                         |
-| logConsole                 | true/false                                       | false                                                                       | boolean                                                                                    | output logs on the console                                                                                                                              |
-| logFormat                  |                                                  | simple                                                                      | string                                                                                     | winston log format                                                                                                                                      |
-| logRotate                  | true/false                                       | true                                                                        | boolean                                                                                    | enable daily log files rotation                                                                                                                         |
-| logMaxFiles                |                                                  | 7                                                                           | integer                                                                                    | maximum number of log files to keep                                                                                                                     |
-| logLevel                   | emerg/alert/crit/error/warning/notice/info/debug | info                                                                        | string                                                                                     | winston logging level                                                                                                                                   |
-| logFile                    |                                                  | combined.log                                                                | string                                                                                     | log file relative path                                                                                                                                  |
-| logErrorFile               |                                                  | error.log                                                                   | string                                                                                     | error log file relative path                                                                                                                            |
-| uiServer                   |                                                  | { "enabled": true, "options": { "host: "localhost", "port": 8080 } }        | { enabled: boolean; options: ServerOptions; }                                              | UI WebSocket server configuration section                                                                                                               |
-| performanceStorage         |                                                  | { "enabled": false, "type": "jsonfile", "file:///performanceRecords.json" } | { enabled: boolean; type: string; URI: string; } where type can be 'jsonfile' or 'mongodb' | performance storage configuration section                                                                                                               |
-| stationTemplateUrls        |                                                  | {}[]                                                                        | { file: string; numberOfStations: number; }[]                                              | array of charging station configuration templates URIs configuration section (charging station configuration template file name and number of stations) |
+| Key                        | Value(s)                                         | Default Value                                                                                                                                                                                                  | Value type                                                                                                                                                                                                                                        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
+| -------------------------- | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| supervisionUrls            |                                                  | []                                                                                                                                                                                                             | string \| string[]                                                                                                                                                                                                                                | string or array of global connection URIs to OCPP-J servers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
+| supervisionUrlDistribution | round-robin/random/sequential                    | round-robin                                                                                                                                                                                                    | boolean                                                                                                                                                                                                                                           | supervision urls distribution policy to simulated charging stations                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
+| logStatisticsInterval      |                                                  | 60                                                                                                                                                                                                             | integer                                                                                                                                                                                                                                           | seconds between charging stations statistics output in the logs                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
+| logConsole                 | true/false                                       | false                                                                                                                                                                                                          | boolean                                                                                                                                                                                                                                           | output logs on the console                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
+| logFormat                  |                                                  | simple                                                                                                                                                                                                         | string                                                                                                                                                                                                                                            | winston log format                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
+| logRotate                  | true/false                                       | true                                                                                                                                                                                                           | boolean                                                                                                                                                                                                                                           | enable daily log files rotation                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
+| logMaxFiles                |                                                  | 7                                                                                                                                                                                                              | integer                                                                                                                                                                                                                                           | maximum number of log files to keep                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
+| logLevel                   | emerg/alert/crit/error/warning/notice/info/debug | info                                                                                                                                                                                                           | string                                                                                                                                                                                                                                            | winston logging level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
+| logFile                    |                                                  | combined.log                                                                                                                                                                                                   | string                                                                                                                                                                                                                                            | log file relative path                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
+| logErrorFile               |                                                  | error.log                                                                                                                                                                                                      | string                                                                                                                                                                                                                                            | error log file relative path                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
+| worker                     |                                                  | {<br />"processType": "workerSet",<br />"startDelay": 500,<br />"elementStartDelay": 0,<br />"elementsPerWorker": 1,<br />"poolMinSize": 4,<br />"poolMaxSize": 16,<br />"poolStrategy": "ROUND_ROBBIN"<br />} | {<br />"processType": WorkerProcessType;<br />"startDelay": number;<br />"elementStartDelay": number;<br />"elementsPerWorker": number;<br />"poolMinSize": number;<br />"poolMaxSize": number;<br />"poolStrategy": WorkerChoiceStrategy;<br />} | Worker configuration section:<br />- processType: worker threads process type (workerSet/staticPool/dynamicPool)<br />- startDelay: milliseconds to wait at worker threads startup (only for workerSet threads process type)<br />- elementStartDelay: milliseconds to wait at charging station startup<br />- elementsPerWorker: number of charging stations per worker threads for the `workerSet` process type<br />- poolMinSize: worker threads pool minimum number of threads</br >- poolMaxSize: worker threads pool maximum number of threads<br />- poolStrategy: worker threads pool [poolifier](https://github.com/poolifier/poolifier) worker choice strategy |
+| uiServer                   |                                                  | {<br />"enabled": true,<br />"options": {<br />"host: "localhost",<br />"port": 8080<br />}<br />}                                                                                                             | {<br />enabled: boolean;<br />options: ServerOptions;<br />}                                                                                                                                                                                      | UI WebSocket server configuration section                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
+| performanceStorage         |                                                  | {<br />"enabled": false,<br />"type": "jsonfile",<br />"file:///performanceRecords.json"<br />}                                                                                                                | {<br />enabled: boolean;<br />type: string;<br />URI: string;<br />}<br />where type can be 'jsonfile' or 'mongodb'                                                                                                                               | performance storage configuration section                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
+| stationTemplateUrls        |                                                  | {}[]                                                                                                                                                                                                           | {<br />file: string;<br />numberOfStations: number;<br />}[]                                                                                                                                                                                      | array of charging station configuration templates URIs configuration section (charging station configuration template file name and number of stations)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
 
 #### Worker process model:
 
index f5595f6172a89e0292569805518e0b4ec8f3efa6..51def2914a4144ecd8e3da819aceecd7c2be0d0e 100644 (file)
@@ -1,14 +1,16 @@
 {
   "supervisionUrls": ["ws://localhost:8010/OCPP16/5be7fb271014d90008992f06"],
   "supervisionUrlDistribution": "round-robin",
+  "worker": {
+    "processType": "workerSet",
+    "elementsPerWorker": 1,
+    "poolMinSize": 4,
+    "poolMaxSize": 16
+  },
   "performanceStorage": {
     "enabled": true,
     "type": "jsonfile"
   },
-  "chargingStationsPerWorker": 1,
-  "workerProcess": "workerSet",
-  "workerPoolMinSize": 4,
-  "workerPoolMaxSize": 16,
   "stationTemplateUrls": [
     {
       "file": "siemens.station-template.json",
index af96523f1f13c3853e487a375b536a8c02a7f3b8..ed9f1bef259640df365739f65ab5a4a8913c24d2 100644 (file)
@@ -109,13 +109,13 @@ export default class Bootstrap {
                 this.version
               } started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
                 ChargingStationUtils.workerDynamicPoolInUse()
-                  ? `${Configuration.getWorkerPoolMinSize().toString()}/`
+                  ? `${Configuration.getWorker().poolMinSize.toString()}/`
                   : ''
               }${this.workerImplementation.size}${
                 ChargingStationUtils.workerPoolInUse()
-                  ? `/${Configuration.getWorkerPoolMaxSize().toString()}`
+                  ? `/${Configuration.getWorker().poolMaxSize.toString()}`
                   : ''
-              } worker(s) concurrently running in '${Configuration.getWorkerProcess()}' mode${
+              } worker(s) concurrently running in '${Configuration.getWorker().processType}' mode${
                 this.workerImplementation.maxElementsPerWorker
                   ? ` (${this.workerImplementation.maxElementsPerWorker} charging station(s) per worker)`
                   : ''
@@ -154,15 +154,15 @@ export default class Bootstrap {
     !this.workerImplementation &&
       (this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(
         this.workerScript,
-        Configuration.getWorkerProcess(),
+        Configuration.getWorker().processType,
         {
-          workerStartDelay: Configuration.getWorkerStartDelay(),
-          elementStartDelay: Configuration.getElementStartDelay(),
-          poolMaxSize: Configuration.getWorkerPoolMaxSize(),
-          poolMinSize: Configuration.getWorkerPoolMinSize(),
-          elementsPerWorker: Configuration.getChargingStationsPerWorker(),
+          workerStartDelay: Configuration.getWorker().startDelay,
+          elementStartDelay: Configuration.getWorker().elementStartDelay,
+          poolMaxSize: Configuration.getWorker().poolMaxSize,
+          poolMinSize: Configuration.getWorker().poolMinSize,
+          elementsPerWorker: Configuration.getWorker().elementsPerWorker,
           poolOptions: {
-            workerChoiceStrategy: Configuration.getWorkerPoolStrategy(),
+            workerChoiceStrategy: Configuration.getWorker().poolStrategy,
           },
           messageHandler: async (msg: ChargingStationWorkerMessage) => {
             if (msg.id === ChargingStationWorkerMessageEvents.STARTED) {
index 964c606b0d2a51234c5cfea1af844bb7ec929bbc..7208fb80fe383fa916836306d63c33d9405f634a 100644 (file)
@@ -157,12 +157,12 @@ export class ChargingStationUtils {
 
   public static workerPoolInUse(): boolean {
     return [WorkerProcessType.DYNAMIC_POOL, WorkerProcessType.STATIC_POOL].includes(
-      Configuration.getWorkerProcess()
+      Configuration.getWorker().processType
     );
   }
 
   public static workerDynamicPoolInUse(): boolean {
-    return Configuration.getWorkerProcess() === WorkerProcessType.DYNAMIC_POOL;
+    return Configuration.getWorker().processType === WorkerProcessType.DYNAMIC_POOL;
   }
 
   /**
index 9aed2f1fab3fae597ed6b45684aa481241439f31..708f411c04f4d5bf34c79987617c83ddda083f96 100644 (file)
@@ -28,19 +28,37 @@ export interface StorageConfiguration {
   uri?: string;
 }
 
+export interface WorkerConfiguration {
+  processType?: WorkerProcessType;
+  startDelay?: number;
+  elementsPerWorker?: number;
+  elementStartDelay?: number;
+  poolMinSize?: number;
+  poolMaxSize?: number;
+  poolStrategy?: WorkerChoiceStrategy;
+}
+
 export default interface ConfigurationData {
   supervisionUrls?: string | string[];
   supervisionUrlDistribution?: SupervisionUrlDistribution;
   stationTemplateUrls: StationTemplateUrl[];
   uiServer?: UIServerConfiguration;
   performanceStorage?: StorageConfiguration;
+  worker?: WorkerConfiguration;
   autoReconnectMaxRetries?: number;
+  // deprecated
   workerProcess?: WorkerProcessType;
+  // deprecated
   workerStartDelay?: number;
+  // deprecated
   elementStartDelay?: number;
+  // deprecated
   workerPoolMinSize?: number;
+  // deprecated
   workerPoolMaxSize?: number;
+  // deprecated
   workerPoolStrategy?: WorkerChoiceStrategy;
+  // deprecated
   chargingStationsPerWorker?: number;
   logStatisticsInterval?: number;
   logFormat?: string;
index 716e53095b8f7d76a4572751b7d04b9fc428d017..3fec05a882ba2803339307c83a9edbcf625ea9f2 100644 (file)
@@ -4,6 +4,7 @@ import ConfigurationData, {
   StorageConfiguration,
   SupervisionUrlDistribution,
   UIServerConfiguration,
+  WorkerConfiguration,
 } from '../types/ConfigurationData';
 
 import Constants from './Constants';
@@ -11,7 +12,6 @@ import { EmptyObject } from '../types/EmptyObject';
 import { FileType } from '../types/FileType';
 import { HandleErrorParams } from '../types/Error';
 import { StorageType } from '../types/Storage';
-import type { WorkerChoiceStrategy } from 'poolifier';
 import WorkerConstants from '../worker/WorkerConstants';
 import { WorkerProcessType } from '../types/Worker';
 import chalk from 'chalk';
@@ -161,57 +161,89 @@ export default class Configuration {
     return Configuration.getConfig().stationTemplateUrls;
   }
 
-  static getWorkerProcess(): WorkerProcessType {
+  static getWorker(): WorkerConfiguration {
     Configuration.warnDeprecatedConfigurationKey(
       'useWorkerPool',
       null,
-      "Use 'workerProcess' to define the type of worker process model to use instead"
+      "Use 'worker' section to define the type of worker process model instead"
+    );
+    Configuration.warnDeprecatedConfigurationKey(
+      'workerProcess',
+      null,
+      "Use 'worker' section to define the type of worker process model instead"
+    );
+    Configuration.warnDeprecatedConfigurationKey(
+      'workerStartDelay',
+      null,
+      "Use 'worker' section to define the worker start delay instead"
+    );
+    Configuration.warnDeprecatedConfigurationKey(
+      'chargingStationsPerWorker',
+      null,
+      "Use 'worker' section to define the number of element(s) per worker instead"
+    );
+    Configuration.warnDeprecatedConfigurationKey(
+      'elementStartDelay',
+      null,
+      "Use 'worker' section to define the worker's element start delay instead"
+    );
+    Configuration.warnDeprecatedConfigurationKey(
+      'workerPoolMinSize',
+      null,
+      "Use 'worker' section to define the worker pool minimum size instead"
     );
-    return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess')
-      ? Configuration.getConfig().workerProcess
-      : WorkerProcessType.WORKER_SET;
-  }
-
-  static getWorkerStartDelay(): number {
-    return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay')
-      ? Configuration.getConfig().workerStartDelay
-      : WorkerConstants.DEFAULT_WORKER_START_DELAY;
-  }
-
-  static getElementStartDelay(): number {
-    return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'elementStartDelay')
-      ? Configuration.getConfig().elementStartDelay
-      : WorkerConstants.DEFAULT_ELEMENT_START_DELAY;
-  }
-
-  static getWorkerPoolMinSize(): number {
-    return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMinSize')
-      ? Configuration.getConfig().workerPoolMinSize
-      : WorkerConstants.DEFAULT_POOL_MIN_SIZE;
-  }
-
-  static getWorkerPoolMaxSize(): number {
     Configuration.warnDeprecatedConfigurationKey(
       'workerPoolSize;',
       null,
-      "Use 'workerPoolMaxSize' instead"
+      "Use 'worker' section to define the worker pool maximum size instead"
     );
-    return Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerPoolMaxSize')
-      ? Configuration.getConfig().workerPoolMaxSize
-      : WorkerConstants.DEFAULT_POOL_MAX_SIZE;
-  }
-
-  static getWorkerPoolStrategy(): WorkerChoiceStrategy {
-    return Configuration.getConfig().workerPoolStrategy;
-  }
-
-  static getChargingStationsPerWorker(): number {
-    return Configuration.objectHasOwnProperty(
-      Configuration.getConfig(),
-      'chargingStationsPerWorker'
-    )
-      ? Configuration.getConfig().chargingStationsPerWorker
-      : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER;
+    Configuration.warnDeprecatedConfigurationKey(
+      'workerPoolMaxSize;',
+      null,
+      "Use 'worker' section to define the worker pool maximum size instead"
+    );
+    Configuration.warnDeprecatedConfigurationKey(
+      'workerPoolStrategy;',
+      null,
+      "Use 'worker' section to define the worker pool strategy instead"
+    );
+    const workerConfiguration: WorkerConfiguration = {
+      processType: Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerProcess')
+        ? Configuration.getConfig().workerProcess
+        : WorkerProcessType.WORKER_SET,
+      startDelay: Configuration.objectHasOwnProperty(Configuration.getConfig(), 'workerStartDelay')
+        ? Configuration.getConfig().workerStartDelay
+        : WorkerConstants.DEFAULT_WORKER_START_DELAY,
+      elementsPerWorker: Configuration.objectHasOwnProperty(
+        Configuration.getConfig(),
+        'chargingStationsPerWorker'
+      )
+        ? Configuration.getConfig().chargingStationsPerWorker
+        : WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
+      elementStartDelay: Configuration.objectHasOwnProperty(
+        Configuration.getConfig(),
+        'elementStartDelay'
+      )
+        ? Configuration.getConfig().elementStartDelay
+        : WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
+      poolMinSize: Configuration.objectHasOwnProperty(
+        Configuration.getConfig(),
+        'workerPoolMinSize'
+      )
+        ? Configuration.getConfig().workerPoolMinSize
+        : WorkerConstants.DEFAULT_POOL_MIN_SIZE,
+      poolMaxSize: Configuration.objectHasOwnProperty(
+        Configuration.getConfig(),
+        'workerPoolMaxSize'
+      )
+        ? Configuration.getConfig().workerPoolMaxSize
+        : WorkerConstants.DEFAULT_POOL_MAX_SIZE,
+      poolStrategy: Configuration.getConfig().workerPoolStrategy,
+    };
+    if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'worker')) {
+      return { ...workerConfiguration, ...Configuration.getConfig().worker };
+    }
+    return workerConfiguration;
   }
 
   static getLogConsole(): boolean {