Skip to content

Commit

Permalink
optimize temporary and permanent limits db writings and db reading (#82)
Browse files Browse the repository at this point in the history
We do this slow data migration asynchronously in parallel to the server normal operations over the coming weeks. We force the migration when writing new data. In a few weeks, we will force the migration at startup of the server of all the remaining data that has not been migrated yet.

Signed-off-by: Etienne Homer <[email protected]>
Co-authored-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt and EtienneLt authored Jan 15, 2025
1 parent f50f58e commit f8bd618
Show file tree
Hide file tree
Showing 15 changed files with 1,229 additions and 110 deletions.
1 change: 0 additions & 1 deletion network-store-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,36 @@
*/
public final class QueryCatalog {

static final String MINIMAL_VALUE_REQUIREMENT_ERROR = "Function should not be called without at least one value.";
public static final String MINIMAL_VALUE_REQUIREMENT_ERROR = "Function should not be called without at least one value.";

static final String VARIANT_ID_COLUMN = "variantId";
static final String UUID_COLUMN = "uuid";
static final String NETWORK_UUID_COLUMN = "networkUuid";
static final String VARIANT_NUM_COLUMN = "variantNum";
public static final String NETWORK_UUID_COLUMN = "networkUuid";
public static final String VARIANT_NUM_COLUMN = "variantNum";
static final String ID_COLUMN = "id";
static final String VOLTAGE_LEVEL_ID_COLUMN = "voltageLevelId";
static final String VOLTAGE_LEVEL_ID_1_COLUMN = "voltageLevelId1";
static final String VOLTAGE_LEVEL_ID_2_COLUMN = "voltageLevelId2";
static final String VOLTAGE_LEVEL_ID_3_COLUMN = "voltageLevelId3";
static final String NAME_COLUMN = "name";
static final String EQUIPMENT_TYPE_COLUMN = "equipmentType";
public static final String NAME_COLUMN = "name";
public static final String EQUIPMENT_TYPE_COLUMN = "equipmentType";
static final String REGULATING_EQUIPMENT_TYPE_COLUMN = "regulatingEquipmentType";
static final String REGULATED_EQUIPMENT_TYPE_COLUMN = "regulatedEquipmentType";
static final String EQUIPMENT_ID_COLUMN = "equipmentId";
public static final String EQUIPMENT_ID_COLUMN = "equipmentId";
static final String REGULATING_EQUIPMENT_ID = "regulatingEquipmentId";
static final String INDEX_COLUMN = "index";
static final String TAPCHANGER_TYPE_COLUMN = "tapChangerType";
static final String ALPHA_COLUMN = "alpha";
static final String OPERATIONAL_LIMITS_GROUP_ID_COLUMN = "operationalLimitsGroupId";
static final String SELECTED_OPERATIONAL_LIMITS_GROUP_ID_COLUMN = "selectedOperationalLimitsGroupId";
public static final String V211_TEMPORARY_LIMIT_TABLE = "v211temporarylimit";
public static final String V211_PERMANENT_LIMIT_TABLE = "v211permanentlimit";
static final String TEMPORARY_LIMITS_TABLE = "temporarylimits";
static final String TEMPORARY_LIMITS_COLUMN = "temporarylimits";
static final String PERMANENT_LIMITS_TABLE = "permanentlimits";
static final String PERMANENT_LIMITS_COLUMN = "permanentlimits";
static final String TAP_CHANGER_STEP_TABLE = "tapChangerStep";
static final String REGULATING_POINT_TABLE = "regulatingPoint";
static final String REGULATION_MODE = "regulationMode";
static final String SIDE_COLUMN = "side";
static final String LIMIT_TYPE_COLUMN = "limitType";
public static final String SIDE_COLUMN = "side";
static final String REGULATING = "regulating";

private QueryCatalog() {
Expand Down Expand Up @@ -285,11 +288,9 @@ public static String buildCloneNetworksQuery(Collection<String> columns) {

// Temporary Limits
public static String buildCloneTemporaryLimitsQuery() {
return "insert into temporarylimit(" + EQUIPMENT_ID_COLUMN + ", " + EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " + VARIANT_NUM_COLUMN + ", " + OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " + SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " + NAME_COLUMN +
", value_, acceptableDuration, fictitious) " + "select " + EQUIPMENT_ID_COLUMN + ", " +
EQUIPMENT_TYPE_COLUMN + ", ?, ?, " + OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " + SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " + NAME_COLUMN +
", value_, acceptableDuration, fictitious from temporarylimit where " + NETWORK_UUID_COLUMN +
return "insert into " + TEMPORARY_LIMITS_TABLE + "(" + EQUIPMENT_ID_COLUMN + ", " + EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " + VARIANT_NUM_COLUMN + ", " + TEMPORARY_LIMITS_COLUMN + ") " + "select " + EQUIPMENT_ID_COLUMN + ", " +
EQUIPMENT_TYPE_COLUMN + ", ?, ?, " + TEMPORARY_LIMITS_COLUMN + " from " + TEMPORARY_LIMITS_TABLE + " where " + NETWORK_UUID_COLUMN +
" = ? and " + VARIANT_NUM_COLUMN + " = ?";
}

Expand All @@ -298,11 +299,8 @@ public static String buildTemporaryLimitQuery(String columnNameForWhereClause) {
EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " +
VARIANT_NUM_COLUMN + ", " +
OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " +
SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " +
NAME_COLUMN + ", " +
"value_, acceptableDuration, fictitious " +
"from temporarylimit where " +
TEMPORARY_LIMITS_COLUMN +
" from " + TEMPORARY_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
columnNameForWhereClause + " = ?";
Expand All @@ -316,54 +314,49 @@ public static String buildTemporaryLimitWithInClauseQuery(String columnNameForIn
EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " +
VARIANT_NUM_COLUMN + ", " +
OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " +
SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " +
NAME_COLUMN + ", " +
"value_, acceptableDuration, fictitious " +
"from temporarylimit where " +
TEMPORARY_LIMITS_COLUMN +
" from " + TEMPORARY_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
columnNameForInClause + " in (" +
"?, ".repeat(numberOfValues - 1) + "?)";
}

public static String buildInsertTemporaryLimitsQuery() {
return "insert into temporarylimit(" +
return "insert into " + TEMPORARY_LIMITS_TABLE + "(" +
EQUIPMENT_ID_COLUMN + ", " + EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " +
VARIANT_NUM_COLUMN + ", " +
OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " + SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " +
NAME_COLUMN + ", value_, acceptableDuration, fictitious)" +
" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
VARIANT_NUM_COLUMN + ", " + TEMPORARY_LIMITS_COLUMN + ") " +
" values (?, ?, ?, ?, ?)";
}

public static String buildDeleteTemporaryLimitsVariantEquipmentINQuery(int numberOfValues) {
if (numberOfValues < 1) {
throw new IllegalArgumentException(MINIMAL_VALUE_REQUIREMENT_ERROR);
}
return "delete from temporarylimit where " +
return "delete from " + TEMPORARY_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
EQUIPMENT_ID_COLUMN + " in (" +
"?, ".repeat(numberOfValues - 1) + "?)";
}

public static String buildDeleteTemporaryLimitsVariantQuery() {
return "delete from temporarylimit where " +
return "delete from " + TEMPORARY_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ?";
}

public static String buildDeleteTemporaryLimitsQuery() {
return "delete from temporarylimit where " +
return "delete from " + TEMPORARY_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ?";
}

// permanent Limits
// Permanent Limits
public static String buildClonePermanentLimitsQuery() {
return "insert into permanentlimit(" + EQUIPMENT_ID_COLUMN + ", " + EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " + VARIANT_NUM_COLUMN + ", " + OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " + SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", value_) " + "select " + EQUIPMENT_ID_COLUMN + ", " +
EQUIPMENT_TYPE_COLUMN + ", ?, ?, " + OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " + SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", value_ from permanentlimit where " + NETWORK_UUID_COLUMN +
return "insert into " + PERMANENT_LIMITS_TABLE + "(" + EQUIPMENT_ID_COLUMN + ", " + EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " + VARIANT_NUM_COLUMN + ", " + PERMANENT_LIMITS_COLUMN + ") " + "select " + EQUIPMENT_ID_COLUMN + ", " +
EQUIPMENT_TYPE_COLUMN + ", ?, ?, " + PERMANENT_LIMITS_COLUMN + " from " + PERMANENT_LIMITS_TABLE + " where " + NETWORK_UUID_COLUMN +
" = ? and " + VARIANT_NUM_COLUMN + " = ?";
}

Expand All @@ -372,10 +365,8 @@ public static String buildPermanentLimitQuery(String columnNameForWhereClause) {
EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " +
VARIANT_NUM_COLUMN + ", " +
OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " +
SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " +
"value_ " +
"from permanentlimit where " +
PERMANENT_LIMITS_COLUMN +
" from " + PERMANENT_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
columnNameForWhereClause + " = ?";
Expand All @@ -389,44 +380,41 @@ public static String buildPermanentLimitWithInClauseQuery(String columnNameForIn
EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " +
VARIANT_NUM_COLUMN + ", " +
OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " +
SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", " +
"value_ " +
"from permanentlimit where " +
PERMANENT_LIMITS_COLUMN +
" from " + PERMANENT_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
columnNameForInClause + " in (" +
"?, ".repeat(numberOfValues - 1) + "?)";
}

public static String buildInsertPermanentLimitsQuery() {
return "insert into permanentlimit(" +
return "insert into " + PERMANENT_LIMITS_TABLE + " (" +
EQUIPMENT_ID_COLUMN + ", " + EQUIPMENT_TYPE_COLUMN + ", " +
NETWORK_UUID_COLUMN + ", " +
VARIANT_NUM_COLUMN + ", " +
OPERATIONAL_LIMITS_GROUP_ID_COLUMN + ", " + SIDE_COLUMN + ", " + LIMIT_TYPE_COLUMN + ", value_)" +
" values (?, ?, ?, ?, ?, ?, ?, ?)";
VARIANT_NUM_COLUMN + ", " + PERMANENT_LIMITS_COLUMN + ") " +
" values (?, ?, ?, ?, ?)";
}

public static String buildDeletePermanentLimitsVariantEquipmentINQuery(int numberOfValues) {
if (numberOfValues < 1) {
throw new IllegalArgumentException(MINIMAL_VALUE_REQUIREMENT_ERROR);
}
return "delete from permanentlimit where " +
return "delete from " + PERMANENT_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ? and " +
EQUIPMENT_ID_COLUMN + " in (" +
"?, ".repeat(numberOfValues - 1) + "?)";
}

public static String buildDeletePermanentLimitsVariantQuery() {
return "delete from permanentlimit where " +
return "delete from " + PERMANENT_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ? and " +
VARIANT_NUM_COLUMN + " = ?";
}

public static String buildDeletePermanentLimitsQuery() {
return "delete from permanentlimit where " +
return "delete from " + PERMANENT_LIMITS_TABLE + " where " +
NETWORK_UUID_COLUMN + " = ?";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Schema(description = "limits attributes")
public class LimitsInfos {

@Schema(description = "List of permeant limits")
@Schema(description = "List of permanent limits")
private List<PermanentLimitAttributes> permanentLimits = new ArrayList<>();

@Schema(description = "List of temporary limits")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.network.store.server.json;

import com.powsybl.iidm.network.LimitType;
import com.powsybl.network.store.server.dto.PermanentLimitAttributes;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

/**
* @author Etienne Lesot <etienne.lesot at rte-france.com>
*/
@ToString
@Builder
@AllArgsConstructor
@Getter
public class PermanentLimitSqlData {

private String operationalLimitsGroupId;
private double value;
private Integer side;
private LimitType limitType;

public PermanentLimitSqlData() {
// empty constructor for Jackson
}

public static PermanentLimitSqlData of(PermanentLimitAttributes permanentLimitAttributes) {
return PermanentLimitSqlData.builder()
.operationalLimitsGroupId(permanentLimitAttributes.getOperationalLimitsGroupId())
.value(permanentLimitAttributes.getValue())
.side(permanentLimitAttributes.getSide())
.limitType(permanentLimitAttributes.getLimitType())
.build();
}

public PermanentLimitAttributes toPermanentLimitAttributes() {
return PermanentLimitAttributes.builder()
.operationalLimitsGroupId(operationalLimitsGroupId)
.value(value)
.side(side)
.limitType(limitType)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.network.store.server.json;

import com.powsybl.iidm.network.LimitType;
import com.powsybl.network.store.model.TemporaryLimitAttributes;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

/**
* @author Etienne Lesot <etienne.lesot at rte-france.com>
*/
@ToString
@Builder
@AllArgsConstructor
@Getter
public class TemporaryLimitSqlData {
private String operationalLimitsGroupId;
private Integer side;
private LimitType limitType;
private String name;
private double value;
private Integer acceptableDuration;
private boolean fictitious;

public TemporaryLimitSqlData() {
// empty constructor for Jackson
}

public static TemporaryLimitSqlData of(TemporaryLimitAttributes temporaryLimit) {
return TemporaryLimitSqlData.builder()
.operationalLimitsGroupId(temporaryLimit.getOperationalLimitsGroupId())
.side(temporaryLimit.getSide())
.limitType(temporaryLimit.getLimitType())
.name(temporaryLimit.getName())
.value(temporaryLimit.getValue())
.acceptableDuration(temporaryLimit.getAcceptableDuration())
.fictitious(temporaryLimit.isFictitious())
.build();
}

public TemporaryLimitAttributes toTemporaryLimitAttributes() {
return TemporaryLimitAttributes.builder()
.operationalLimitsGroupId(operationalLimitsGroupId)
.side(side)
.limitType(limitType)
.name(name)
.value(value)
.acceptableDuration(acceptableDuration)
.fictitious(fictitious)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.network.store.server.migration;

import com.powsybl.network.store.model.*;
import com.powsybl.network.store.server.NetworkStoreRepository;
import com.powsybl.network.store.server.migration.v211limits.V211LimitsMigration;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@RestController
@RequestMapping(value = "/" + NetworkStoreApi.VERSION + "/migration")
@Tag(name = "Network store migration")
public class MigrationController {

@Autowired
private NetworkStoreRepository repository;

@PutMapping(value = "/v211limits/{networkId}/{variantNum}")
@Operation(summary = "Migrate limits of a network")
@ApiResponses(@ApiResponse(responseCode = "200", description = "Successfully migrated limits from V2.11.0 to new model"))
public ResponseEntity<Void> migrateV211Limits(@Parameter(description = "Network ID", required = true) @PathVariable("networkId") UUID networkId,
@Parameter(description = "Variant num", required = true) @PathVariable("variantNum") int variantNum) {
V211LimitsMigration.migrateV211Limits(repository, networkId, variantNum);
return ResponseEntity.ok().build();
}
}
Loading

0 comments on commit f8bd618

Please sign in to comment.