Made synchronous creation of index + put lock at collection level (cannot // index creation on the same collection, throws an error in MongoDB)
MODIFY
src/locking/LockingManager.ts
src/locking/LockingManager.ts
@@ -42,6 +42,7 @@ export default class LockingManager {
message: `Acquired successfully the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`,
detailedMessages: { lock }
});
+ // Console.log(`Acquire the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`);
return true;
} catch (error) {
Logging.logError({
@@ -76,6 +77,7 @@ export default class LockingManager {
message: `Released successfully the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`,
detailedMessages: { lock }
});
+ // Console.log(`Released the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`);
return true;
}

MODIFY
src/server/rest/service/TransactionService.ts
src/server/rest/service/TransactionService.ts
@@ -364,7 +364,7 @@ export default class TransactionService {
// Get the consumption
let consumptions: Consumption[];
if (filteredRequest.LoadAllConsumptions) {
- consumptions = await ConsumptionStorage.getAllTransactionConsumptions(req.user.tenantID, { transactionId: transaction.id });
+ consumptions = await ConsumptionStorage.getTransactionConsumptions(req.user.tenantID, { transactionId: transaction.id });
} else {
consumptions = await ConsumptionStorage.getOptimizedTransactionConsumptions(req.user.tenantID, { transactionId: transaction.id });
}

MODIFY
src/storage/mongodb/ConsumptionStorage.ts
src/storage/mongodb/ConsumptionStorage.ts
@@ -148,9 +148,9 @@ export default class ConsumptionStorage {
return consumptionsMDB;
}
- static async getAllTransactionConsumptions(tenantID: string, params: { transactionId: number }): Promise<Consumption[]> {
+ static async getTransactionConsumptions(tenantID: string, params: { transactionId: number }): Promise<Consumption[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getAllTransactionConsumptions');
+ const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getTransactionConsumptions');
// Check
await Utils.checkTenant(tenantID);
// Create Aggregation
@@ -172,7 +172,7 @@ export default class ConsumptionStorage {
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Debug
- Logging.traceEnd('ConsumptionStorage', 'getAllTransactionConsumptions', uniqueTimerID, { transactionId: params.transactionId });
+ Logging.traceEnd('ConsumptionStorage', 'getTransactionConsumptions', uniqueTimerID, { transactionId: params.transactionId });
return consumptionsMDB;
}

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -128,12 +128,10 @@ export default class MongoDBStorage {
{ fields: { connectorId: 1, userId: 1 }, options: { unique: true } }
]);
await this.handleIndexesInCollection(collections, tenantID, 'consumptions', [
- { fields: { siteID: 1 } },
- { fields: { transactionId: 1, endedAt: 1 } },
- { fields: { siteAreaID: 1 } },
{ fields: { transactionId: 1 } },
- { fields: { chargeBoxID: 1, connectorId: 1 } },
- { fields: { userID: 1 } }
+ { fields: { siteID: 1, startedAt: 1 } },
+ { fields: { siteAreaID: 1, startedAt: 1 } },
+ { fields: { transactionId: 1 } }
]);
Logging.logDebug({
tenantID: tenantID,
@@ -169,27 +167,6 @@ export default class MongoDBStorage {
}
}
- // pragma public async migrateTenantDatabase(tenantID: string): Promise<void> {
- // // Safety check
- // if (!this.db) {
- // throw new BackendError({
- // source: Constants.CENTRAL_SERVER,
- // module: MODULE_NAME,
- // method: 'migrateTenantDatabase',
- // message: 'Not supposed to call migrateTenantDatabase before database start',
- // action: ServerAction.MONGO_DB
- // });
- // }
- // // Migrate not prefixed collections
- // const collections = await this.db.listCollections().toArray();
-
- // for (const collection of collections) {
- // if (!DatabaseUtils.getFixedCollections().includes(collection.name) && !collection.name.includes('.')) {
- // await this.db.collection(collection.name).rename(DatabaseUtils.getCollectionName(tenantID, collection.name));
- // }
- // }
- // }
-
public getGridFSBucket(name: string): GridFSBucket {
return new GridFSBucket(this.db, { bucketName: name });
}
@@ -316,6 +293,10 @@ export default class MongoDBStorage {
action: ServerAction.MONGO_DB
});
}
+ // Indexes collection Lock
+ const createCollection = LockingManager.createExclusiveLock(tenantID, LockEntity.DATABASE_INDEX, `collection-${tenantID}.${name}`);
+ if (await LockingManager.acquire(createCollection)) {
+ try {
// Check Logs
const tenantCollectionName = DatabaseUtils.getCollectionName(tenantID, name);
const foundCollection = allCollections.find((collection) => collection.name === tenantCollectionName);
@@ -334,17 +315,10 @@ export default class MongoDBStorage {
// Check if it exists
const foundIndex = databaseIndexes.find((existingIndex) => (JSON.stringify(existingIndex.key) === JSON.stringify(index.fields)));
if (!foundIndex) {
- // Indexes creation Lock
- const createIndexesLock = LockingManager.createExclusiveLock(tenantID, LockEntity.DATABASE_INDEX, `index-${JSON.stringify(index.fields)}`);
- if (await LockingManager.acquire(createIndexesLock)) {
- try {
// Create Indexes
- this.db.collection(tenantCollectionName).createIndex(index.fields, index.options);
- } finally {
- // Release the indexes creation Lock
- await LockingManager.release(createIndexesLock);
- }
- }
+ console.log(`Create index ${JSON.stringify(index)} on collection ${tenantID}.${name}`);
+ // eslint-disable-next-line @typescript-eslint/await-thenable
+ await this.db.collection(tenantCollectionName).createIndex(index.fields, index.options);
}
}
// Check each index that should be dropped
@@ -356,18 +330,15 @@ export default class MongoDBStorage {
// Exists?
const foundIndex = indexes.find((index) => (JSON.stringify(index.fields) === JSON.stringify(databaseIndex.key)));
if (!foundIndex) {
- // Indexes drop Lock
- const dropIndexesLock = LockingManager.createExclusiveLock(tenantID, LockEntity.DATABASE_INDEX, `index-${JSON.stringify(databaseIndex.key)}`);
- if (await LockingManager.acquire(dropIndexesLock)) {
- try {
// Drop indexes
+ console.log(`Drop index ${JSON.stringify(databaseIndex.key)} on collection ${tenantID}.${name}`);
await this.db.collection(tenantCollectionName).dropIndex(databaseIndex.key);
- } finally {
- // Release the indexes drop Lock
- await LockingManager.release(dropIndexesLock);
}
}
}
+ } finally {
+ // Release the creation Lock
+ await LockingManager.release(createCollection);
}
}
return false;

Made synchronous creation of index + put lock at collection level (cannot // index creation on the same collection, throws an error in MongoDB)