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

fix(api): mongo upgraders batch updates to ensure no timeouts #9675

Open
wants to merge 1 commit into
base: 4.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
@Component
public class MissingEnvironmentUpgrader extends MongoUpgrader {

private static final int UPGRADER_BATCH_SIZE = 1000;
public static final int MISSING_ENVIRONMENT_UPGRADER_ORDER = ThemeTypeUpgrader.THEME_TYPE_UPGRADER_ORDER + 1;

@Override
public String version() {
return "v1";
return "v2";
}

@Override
Expand All @@ -64,10 +65,15 @@ private void updateEnvironmentFromApi(final Set<Boolean> upgradeStatus) {
);

if (!bulkActions.isEmpty()) {
// This upgrade is only done on data created before 3.17.0 as ApiKey#api as been deprecated
upgradeStatus.add(this.getCollection("keys").bulkWrite(bulkActions).wasAcknowledged());
upgradeStatus.add(this.getCollection("plans").bulkWrite(bulkActions).wasAcknowledged());
upgradeStatus.add(this.getCollection("subscriptions").bulkWrite(bulkActions).wasAcknowledged());
for (int i = 0; i < bulkActions.size(); i += UPGRADER_BATCH_SIZE) {
// Get the end index for the sublist, ensuring it doesn't go out of bounds
int end = Math.min(i + UPGRADER_BATCH_SIZE, bulkActions.size());
var batchActions = bulkActions.subList(i, end);
// This upgrade is only done on data created before 3.17.0 as ApiKey#api as been deprecated
upgradeStatus.add(this.getCollection("keys").bulkWrite(batchActions).wasAcknowledged());
upgradeStatus.add(this.getCollection("plans").bulkWrite(batchActions).wasAcknowledged());
upgradeStatus.add(this.getCollection("subscriptions").bulkWrite(batchActions).wasAcknowledged());
}
}
}

Expand All @@ -87,7 +93,12 @@ private void updateEnvironmentFromSubscription(final Set<Boolean> upgradeStatus)
);

if (!bulkActions.isEmpty()) {
upgradeStatus.add(this.getCollection("keys").bulkWrite(bulkActions).wasAcknowledged());
for (int i = 0; i < bulkActions.size(); i += UPGRADER_BATCH_SIZE) {
// Get the end index for the sublist, ensuring it doesn't go out of bounds
int end = Math.min(i + UPGRADER_BATCH_SIZE, bulkActions.size());
var batchActions = bulkActions.subList(i, end);
upgradeStatus.add(this.getCollection("keys").bulkWrite(batchActions).wasAcknowledged());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.gravitee.repository.mongodb.management.upgrade.upgrader.common.MongoUpgrader;
import io.gravitee.repository.mongodb.management.upgrade.upgrader.dashboards.DashboardTypeUpgrader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.bson.Document;
import org.springframework.stereotype.Component;

Expand All @@ -32,15 +34,17 @@
@Component
public class PlanDefinitionVersionUpgrader extends MongoUpgrader {

private static final int UPGRADER_BATCH_SIZE = 1000;
public static final int PLAN_DEFINITION_VERSION_UPGRADER_ORDER = DashboardTypeUpgrader.DASHBOARD_TYPE_UPGRADER_ORDER + 1;

@Override
public String version() {
return "v1";
return "v2";
}

@Override
public boolean upgrade() {
Set<Boolean> upgradeStatus = new HashSet<>();
var query = new Document("definitionVersion", DefinitionVersion.V4.name());
var projection = Projections.fields(Projections.include("_id", "definitionVersion"));

Expand All @@ -58,7 +62,13 @@ public boolean upgrade() {
});

if (!bulkActions.isEmpty()) {
return this.getCollection("plans").bulkWrite(bulkActions).wasAcknowledged();
for (int i = 0; i < bulkActions.size(); i += UPGRADER_BATCH_SIZE) {
// Get the end index for the sublist, ensuring it doesn't go out of bounds
int end = Math.min(i + UPGRADER_BATCH_SIZE, bulkActions.size());
var batchActions = bulkActions.subList(i, end);
upgradeStatus.add(this.getCollection("plans").bulkWrite(batchActions).wasAcknowledged());
}
return upgradeStatus.stream().allMatch(Boolean.TRUE::equals);
}
return true;
}
Expand Down