Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Mongo-DB calls (in-memory and/or Redis) #926

Draft
wants to merge 29 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4e6f9f1
Cache Mongo-DB calls.
jason-fox Oct 26, 2020
7872370
Linting - move functions.
jason-fox Oct 26, 2020
c2a7ea0
Merge branch 'master' into feature/cache
jason-fox Nov 2, 2020
9471f42
Make Caching configurable.
jason-fox Nov 2, 2020
7a8edc8
Add documentation
jason-fox Nov 2, 2020
d0fe4f8
Amend documentation.
jason-fox Nov 2, 2020
5d08c0e
Tidying up code and tests.
jason-fox Nov 2, 2020
6b6dba7
Merge branch 'master' into feature/cache
jason-fox Nov 16, 2020
3c1190c
Merge commit 'master' into feature/cache
jason-fox Nov 18, 2020
60d7dca
Complete linting using ESLint.
jason-fox Nov 18, 2020
56c8023
Merge branch 'master' into feature/cache
jason-fox Nov 27, 2020
90d1318
Merge branch 'master' into feature/cache
jason-fox Dec 4, 2020
acad317
Fixing merge
jason-fox Dec 4, 2020
4b7a08a
Adding Redis Cache
jason-fox Dec 7, 2020
b0daf0a
Adding Start-up test for Redis and Memcache
jason-fox Dec 7, 2020
5eaef79
Updating Docs
jason-fox Dec 7, 2020
94bc29e
Merge branch 'master' into feature/cache
jason-fox Dec 10, 2020
76dcd15
Remove debug
jason-fox Dec 10, 2020
7d88f72
Merge branch 'master' into feature/cache
jason-fox Dec 15, 2020
bb0c277
Merge branch 'master' into feature/cache
jason-fox Jan 21, 2021
55e017b
Merge branch 'master' into feature/cache
jason-fox Feb 11, 2021
a0b1dd8
Merge branch 'master' into feature/cache
jason-fox Feb 12, 2021
880f164
Merge branch 'master' into feature/cache
jason-fox Mar 2, 2021
cfdd819
Merge branch 'master' into feature/cache
jason-fox Mar 16, 2021
d39a90b
Merge branch 'master' into feature/cache
jason-fox Apr 13, 2021
e98c02e
Set cache rather than don't cache
jason-fox Apr 13, 2021
7753fec
Delete file.
jason-fox Jun 24, 2021
f3d6622
Merge branch 'master' into feature/cache
jason-fox Jun 24, 2021
12f9bcb
Merge branch 'master' into feature/cache
jason-fox Sep 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 86 additions & 62 deletions doc/installationguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,28 @@ used for the same purpose. For instance:
used to store all the device information, so it will be persistent from one execution to the other. Mongodb
databases must be configured in the `mongob` section (as described bellow). E.g.:

```javascript
{
type: "mongodb";
}
```

- **deviceRegistry.cache**: Whether to use a memory cache in front of Mongo-DB when using the `mongodb`
**deviceRegistry** option to reduce I/O. This memory cache will hold and serve a set of recently requested groups
and devices (up to a given maximum time-to-live) and return the cached response so long as the value is still within
`TTL`. When enabled the default values are to hold up to 1000 devices and 100 groups in memory and retain values for
up to 10 seconds.

```javascript
{
type: 'mongodb';
cache: {
enabled: true,
deviceSize: 1000,
deviceTTL: 10,
groupSize: 100,
groupTTL: 10
}
}
```

Expand All @@ -152,6 +171,7 @@ used for the same purpose. For instance:
retryTime: 5
}
```

```javascript
{
host: 'mongodb-0,mongodb-1,mongodb-2',
Expand Down Expand Up @@ -232,17 +252,16 @@ used for the same purpose. For instance:
the IoTAgent runs in a single thread. For more details about multi-core functionality, please refer to the
[Cluster](https://nodejs.org/api/cluster.html) module in Node.js and
[this section](howto.md#iot-agent-in-multi-thread-mode) of the library documentation.
- **defaultExpressionLanguage**: the default expression language used to
compute expressions, possible values are: `legacy` or `jexl`. When not set or
wrongly set, `legacy` is used as default value.

- **explicitAttrs**: if this flag is activated, only provisioned attributes will be processed to Context Broker.
This flag is overwritten by `explicitAttrs` flag in group or device provision.
- **relaxTemplateValidation**: if this flag is activated, `objectId` attributes for incoming devices are not validated,
and may exceptionally include characters (such as semi-colons) which are
[forbidden](https://fiware-orion.readthedocs.io/en/master/user/forbidden_characters/index.html) according to the NGSI
specification. When provisioning devices, it is necessary that the developer provides valid `objectId`-`name` mappings
whenever relaxed mode is used, to prevent the consumption of forbidden characters.
- **defaultExpressionLanguage**: the default expression language used to compute expressions, possible values are:
`legacy` or `jexl`. When not set or wrongly set, `legacy` is used as default value.

- **explicitAttrs**: if this flag is activated, only provisioned attributes will be processed to Context Broker. This
flag is overwritten by `explicitAttrs` flag in group or device provision.
- **relaxTemplateValidation**: if this flag is activated, `objectId` attributes for incoming devices are not
validated, and may exceptionally include characters (such as semi-colons) which are
[forbidden](https://fiware-orion.readthedocs.io/en/master/user/forbidden_characters/index.html) according to the
NGSI specification. When provisioning devices, it is necessary that the developer provides valid `objectId`-`name`
mappings whenever relaxed mode is used, to prevent the consumption of forbidden characters.

### Configuration using environment variables

Expand All @@ -252,54 +271,59 @@ with container-based technologies, like Docker, Heroku, etc...
The following table shows the accepted environment variables, as well as the configuration parameter the variable
overrides.

| Environment variable | Configuration attribute |
| :------------------------ | :------------------------------ |
| IOTA_CB_URL | `contextBroker.url` |
| IOTA_CB_HOST | `contextBroker.host` |
| IOTA_CB_PORT | `contextBroker.port` |
| IOTA_CB_NGSI_VERSION | `contextBroker.ngsiVersion` |
| IOTA_NORTH_HOST | `server.host` |
| IOTA_NORTH_PORT | `server.port` |
| IOTA_PROVIDER_URL | `providerUrl` |
| IOTA_AUTH_ENABLED | `authentication.enabled` |
| IOTA_AUTH_TYPE | `authentication.type` |
| IOTA_AUTH_HEADER | `authentication.header` |
| IOTA_AUTH_URL | `authentication.url` |
| IOTA_AUTH_HOST | `authentication.host` |
| IOTA_AUTH_PORT | `authentication.port` |
| IOTA_AUTH_USER | `authentication.user` |
| IOTA_AUTH_PASSWORD | `authentication.password` |
| IOTA_AUTH_CLIENT_ID | `authentication.clientId` |
| IOTA_AUTH_CLIENT_SECRET | `authentication.clientSecret` |
| IOTA_AUTH_TOKEN_PATH | `authentication.tokenPath` |
| IOTA_AUTH_PERMANENT_TOKEN | `authentication.permanentToken` |
| IOTA_REGISTRY_TYPE | `deviceRegistry.type` |
| IOTA_LOG_LEVEL | `logLevel` |
| IOTA_TIMESTAMP | `timestamp` |
| IOTA_IOTAM_URL | `iotManager.url` |
| IOTA_IOTAM_HOST | `iotManager.host` |
| IOTA_IOTAM_PORT | `iotManager.port` |
| IOTA_IOTAM_PATH | `iotManager.path` |
| IOTA_IOTAM_AGENTPATH | `iotManager.agentPath` |
| IOTA_IOTAM_PROTOCOL | `iotManager.protocol` |
| IOTA_IOTAM_DESCRIPTION | `iotManager.description` |
| IOTA_MONGO_HOST | `mongodb.host` |
| IOTA_MONGO_PORT | `mongodb.port` |
| IOTA_MONGO_DB | `mongodb.db` |
| IOTA_MONGO_REPLICASET | `mongodb.replicaSet` |
| IOTA_MONGO_USER | `mongodb.user` |
| IOTA_MONGO_PASSWORD | `mongodb.password` |
| IOTA_MONGO_AUTH_SOURCE | `mongodb.authSource` |
| IOTA_MONGO_RETRIES | `mongodb.retries` |
| IOTA_MONGO_RETRY_TIME | `mongodb.retryTime` |
| IOTA_MONGO_SSL | `mongodb.ssl ` |
| IOTA_MONGO_EXTRAARGS | `mongodb.extraArgs` |
| IOTA_SINGLE_MODE | `singleConfigurationMode` |
| IOTA_APPEND_MODE | `appendMode` |
| IOTA_POLLING_EXPIRATION | `pollingExpiration` |
| IOTA_POLLING_DAEMON_FREQ | `pollingDaemonFrequency` |
| IOTA_AUTOCAST | `autocast` |
| IOTA_MULTI_CORE | `multiCore` |
| IOTA_DEFAULT_EXPRESSION_LANGUAGE | defaultExpressionLanguage |
| IOTA_EXPLICIT_ATTRS | `explicitAttrs` |
| IOTA_RELAX_TEMPLATE_VALIDATION | `relaxTemplateValidation` |
| Environment variable | Configuration attribute |
| :------------------------------- | :-------------------------------- |
| IOTA_CB_URL | `contextBroker.url` |
| IOTA_CB_HOST | `contextBroker.host` |
| IOTA_CB_PORT | `contextBroker.port` |
| IOTA_CB_NGSI_VERSION | `contextBroker.ngsiVersion` |
| IOTA_NORTH_HOST | `server.host` |
| IOTA_NORTH_PORT | `server.port` |
| IOTA_PROVIDER_URL | `providerUrl` |
| IOTA_AUTH_ENABLED | `authentication.enabled` |
| IOTA_AUTH_TYPE | `authentication.type` |
| IOTA_AUTH_HEADER | `authentication.header` |
| IOTA_AUTH_URL | `authentication.url` |
| IOTA_AUTH_HOST | `authentication.host` |
| IOTA_AUTH_PORT | `authentication.port` |
| IOTA_AUTH_USER | `authentication.user` |
| IOTA_AUTH_PASSWORD | `authentication.password` |
| IOTA_AUTH_CLIENT_ID | `authentication.clientId` |
| IOTA_AUTH_CLIENT_SECRET | `authentication.clientSecret` |
| IOTA_AUTH_TOKEN_PATH | `authentication.tokenPath` |
| IOTA_AUTH_PERMANENT_TOKEN | `authentication.permanentToken` |
| IOTA_REGISTRY_TYPE | `deviceRegistry.type` |
| IOTA_REGISTRY_CACHE_ENABLED | `deviceRegistry.cache.enabled` |
| IOTA_REGISTRY_CACHE_DEVICE_SIZE | `deviceRegistry.cache.deviceSize` |
| IOTA_REGISTRY_CACHE_DEVICE_TTL | `deviceRegistry.cache.deviceTTL` |
| IOTA_REGISTRY_CACHE_GROUP_SIZE | `deviceRegistry.cache.groupSize` |
| IOTA_REGISTRY_CACHE_GROUP_TTL | `deviceRegistry.cache.groupTTL` |
| IOTA_LOG_LEVEL | `logLevel` |
| IOTA_TIMESTAMP | `timestamp` |
| IOTA_IOTAM_URL | `iotManager.url` |
| IOTA_IOTAM_HOST | `iotManager.host` |
| IOTA_IOTAM_PORT | `iotManager.port` |
| IOTA_IOTAM_PATH | `iotManager.path` |
| IOTA_IOTAM_AGENTPATH | `iotManager.agentPath` |
| IOTA_IOTAM_PROTOCOL | `iotManager.protocol` |
| IOTA_IOTAM_DESCRIPTION | `iotManager.description` |
| IOTA_MONGO_HOST | `mongodb.host` |
| IOTA_MONGO_PORT | `mongodb.port` |
| IOTA_MONGO_DB | `mongodb.db` |
| IOTA_MONGO_REPLICASET | `mongodb.replicaSet` |
| IOTA_MONGO_USER | `mongodb.user` |
| IOTA_MONGO_PASSWORD | `mongodb.password` |
| IOTA_MONGO_AUTH_SOURCE | `mongodb.authSource` |
| IOTA_MONGO_RETRIES | `mongodb.retries` |
| IOTA_MONGO_RETRY_TIME | `mongodb.retryTime` |
| IOTA_MONGO_SSL | `mongodb.ssl` |
| IOTA_MONGO_EXTRAARGS | `mongodb.extraArgs` |
| IOTA_SINGLE_MODE | `singleConfigurationMode` |
| IOTA_APPEND_MODE | `appendMode` |
| IOTA_POLLING_EXPIRATION | `pollingExpiration` |
| IOTA_POLLING_DAEMON_FREQ | `pollingDaemonFrequency` |
| IOTA_AUTOCAST | `autocast` |
| IOTA_MULTI_CORE | `multiCore` |
| IOTA_DEFAULT_EXPRESSION_LANGUAGE | `defaultExpressionLanguage` |
| IOTA_EXPLICIT_ATTRS | `explicitAttrs` |
| IOTA_RELAX_TEMPLATE_VALIDATION | `relaxTemplateValidation` |
27 changes: 27 additions & 0 deletions lib/commonConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ function processEnvironmentVariables() {
'IOTA_AUTH_TOKEN_PATH',
'IOTA_AUTH_PERMANENT_TOKEN',
'IOTA_REGISTRY_TYPE',
'IOTA_REGISTRY_CACHE_ENABLED',
'IOTA_REGISTRY_CACHE_DEVICE_SIZE',
'IOTA_REGISTRY_CACHE_DEVICE_TTL',
'IOTA_REGISTRY_CACHE_GROUP_SIZE',
'IOTA_REGISTRY_CACHE_GROUP_TTL',
'IOTA_LOG_LEVEL',
'IOTA_TIMESTAMP',
'IOTA_IOTAM_HOST',
Expand Down Expand Up @@ -317,6 +322,28 @@ function processEnvironmentVariables() {
if (process.env.IOTA_REGISTRY_TYPE) {
config.deviceRegistry.type = process.env.IOTA_REGISTRY_TYPE;
}
// Caching policy
config.deviceRegistry.cache = config.deviceRegistry.cache || {};
if (process.env.IOTA_REGISTRY_CACHE_ENABLED) {
config.deviceRegistry.cache.enabled =
process.env.IOTA_REGISTRY_CACHE_ENABLED === 'true';
config.deviceRegistry.cache.deviceSize = config.deviceRegistry.cache.deviceSize || 1000;
config.deviceRegistry.cache.deviceTTL = config.deviceRegistry.cache.deviceTTL || 10;
config.deviceRegistry.cache.groupSize = config.deviceRegistry.cache.groupSize || 100;
config.deviceRegistry.cache.groupTTL = config.deviceRegistry.cache.groupTTL || 10;
}
if (process.env.IOTA_REGISTRY_CACHE_DEVICE_SIZE) {
config.deviceRegistry.cache.deviceSize = process.env.IOTA_REGISTRY_CACHE_DEVICE_SIZE;
}
if (process.env.IOTA_REGISTRY_CACHE_DEVICE_TTL) {
config.deviceRegistry.cache.deviceTTL = process.env.IOTA_REGISTRY_CACHE_DEVICE_TTL;
}
if (process.env.IOTA_REGISTRY_CACHE_GROUP_SIZE) {
config.deviceRegistry.cache.groupSize = process.env.IOTA_REGISTRY_CACHE_GROUP_SIZE;
}
if (process.env.IOTA_REGISTRY_CACHE_GROUP_TTL) {
config.deviceRegistry.cache.groupTTL = process.env.IOTA_REGISTRY_CACHE_GROUP_TTL;
}

// Log Level configuration
if (process.env.IOTA_LOG_LEVEL) {
Expand Down
4 changes: 4 additions & 0 deletions lib/fiware-iotagent-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ function doActivate(newConfig, callback) {
registry = require('./services/devices/deviceRegistryMongoDB');
groupRegistry = require('./services/groups/groupRegistryMongoDB');
commandRegistry = require('./services/commands/commandRegistryMongoDB');

registry.setMemoryCache(newConfig.deviceRegistry.cache);
groupRegistry.setMemoryCache(newConfig.deviceRegistry.cache);

} else {
logger.info(context, 'Falling back to Transient Memory registry for NGSI Library');

Expand Down
Loading