Skip to content

MLE-22812 Refactor: Created ForestPlanner #758

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

Merged
merged 1 commit into from
Jul 8, 2025
Merged
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
@@ -0,0 +1,52 @@
/*
* Copyright © 2025 MarkLogic Corporation. All Rights Reserved.
*/
package com.marklogic.appdeployer.command.forests;

import com.marklogic.appdeployer.AppConfig;
import com.marklogic.appdeployer.command.CommandContext;
import com.marklogic.appdeployer.command.databases.DatabasePlan;
import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand;
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand;
import com.marklogic.mgmt.ManageClient;
import com.marklogic.mgmt.api.forest.Forest;

import java.util.ArrayList;
import java.util.List;

/**
* Intended to be the central interface for "Give me a database name and a set of inputs, and I'll give you back a list
* of forests with replicas that should be created for that database". We'll sort out the naming of this and the
* related classes later.
*/
public class ForestPlanner {

private final ManageClient manageClient;

public ForestPlanner(ManageClient manageClient) {
this.manageClient = manageClient;
}

public List<Forest> previewForestPlan(String database, AppConfig appConfig) {
// We unfortunately still need a CommandContext here, even though this is just for previewing forests. The
// classes that this depends on would need to be modified first to only require an AppConfig and/or ManageClient.
final CommandContext context = new CommandContext(appConfig, this.manageClient, null);

List<DatabasePlan> plans = new DeployOtherDatabasesCommand().buildDatabasePlans(context);
DeployDatabaseCommand dbCommand = null;
for (DatabasePlan plan : plans) {
if (plan.getDatabaseName().equals(database)) {
dbCommand = plan.getDeployDatabaseCommand();
break;
}
}
if (dbCommand == null) {
throw new IllegalArgumentException("Did not find any database plan with a database name of: " + database);
}

DeployForestsCommand deployForestsCommand = dbCommand.buildDeployForestsCommand(database, context);
return deployForestsCommand != null ?
deployForestsCommand.buildForests(context, true) :
new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2025 MarkLogic Corporation. All Rights Reserved.
*/
package com.marklogic.appdeployer.command.forests;

import com.marklogic.appdeployer.AbstractAppDeployerTest;
import com.marklogic.mgmt.api.forest.Forest;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ForestPlannerTest extends AbstractAppDeployerTest {

private static final String DB_NAME = "sample-app-content";

@Test
void test() {
List<Forest> forests = new ForestPlanner(manageClient).previewForestPlan(DB_NAME, appConfig);
assertEquals(3, forests.size());
assertEquals("sample-app-content-1", forests.get(0).getForestName());
assertEquals("sample-app-content-2", forests.get(1).getForestName());
assertEquals("sample-app-content-3", forests.get(2).getForestName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
*/
package com.marklogic.gradle.task.forests

import com.marklogic.appdeployer.command.databases.DatabasePlan
import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand
import com.marklogic.appdeployer.command.forests.DeployForestsCommand
import com.marklogic.appdeployer.impl.SimpleAppDeployer

import com.marklogic.appdeployer.command.forests.ForestPlanner
import com.marklogic.gradle.task.MarkLogicTask
import com.marklogic.mgmt.api.forest.Forest
import org.gradle.api.GradleException
import org.gradle.api.tasks.TaskAction

/**
Expand All @@ -41,27 +37,7 @@ class PrintForestPlanTask extends MarkLogicTask {
}

String database = project.property("database")

SimpleAppDeployer appDeployer = getAppDeployer()
DeployOtherDatabasesCommand command = appDeployer.getCommandOfType(DeployOtherDatabasesCommand.class)
List<DatabasePlan> plans = command.buildDatabasePlans(getCommandContext())

DeployDatabaseCommand deployDatabaseCommand
for (DatabasePlan plan : plans) {
if (database.equals(plan.getDatabaseName())) {
deployDatabaseCommand = plan.getDeployDatabaseCommand()
break
}
}

if (deployDatabaseCommand == null) {
throw new GradleException("Did not find any database plan with a database name of: " + database)
}

DeployForestsCommand deployForestsCommand = deployDatabaseCommand.buildDeployForestsCommand(database, getCommandContext())
List<Forest> forests = deployForestsCommand != null ?
deployForestsCommand.buildForests(getCommandContext(), true) :
new ArrayList<>()
List<Forest> forests = new ForestPlanner(getManageClient()).previewForestPlan(database, getAppConfig())

if (forests.isEmpty()) {
println "\nNo primary forests will be created the next time the database '" + database + "' is deployed. This is " +
Expand Down