Skip to content
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 @@ -28,12 +28,17 @@
import org.apache.bigtop.manager.server.command.job.AbstractJob;
import org.apache.bigtop.manager.server.command.job.JobContext;
import org.apache.bigtop.manager.server.holder.SpringContextHolder;
import org.apache.bigtop.manager.server.model.dto.ServiceDTO;
import org.apache.bigtop.manager.server.model.dto.command.ServiceCommandDTO;
import org.apache.bigtop.manager.server.utils.StackUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public abstract class AbstractServiceJob extends AbstractJob {

Expand Down Expand Up @@ -79,6 +84,115 @@ protected Map<String, List<String>> getComponentHostsMap() {
return componentHostsMap;
}

/**
* Filter component-hosts map for a single service by component membership.
*/
protected Map<String, List<String>> filterComponentHostsByService(
Map<String, List<String>> all, String serviceName) {
Map<String, List<String>> result = new HashMap<>();
ServiceDTO serviceDTO = StackUtils.getServiceDTO(serviceName);
// Collect component names for this service
List<String> componentNames = serviceDTO.getComponents().stream()
.map(c -> c.getName().toLowerCase())
.toList();
for (Map.Entry<String, List<String>> entry : all.entrySet()) {
String comp = entry.getKey();
if (componentNames.contains(comp.toLowerCase())) {
result.put(comp, entry.getValue());
}
}
return result;
}

/**
* Order services per required-services in metainfo.xml.
* For Start/Add: required services first; For Stop: reverse (dependents first).
*/
protected List<String> getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command command) {
List<String> services = getServiceNames();
// Build graph: edge from required -> service
Map<String, List<String>> graph = new HashMap<>();
Map<String, Integer> indegree = new HashMap<>();
for (String s : services) {
graph.putIfAbsent(s, new ArrayList<>());
indegree.putIfAbsent(s, 0);
}
// Use transitive required services
Map<String, List<String>> allReqMemo = new HashMap<>();
for (String s : services) {
List<String> allReq = getAllRequiredServices(s, allReqMemo);
for (String r : allReq) {
if (!services.contains(r)) {
// skip requirements not part of current command set
continue;
}
graph.computeIfAbsent(r, k -> new ArrayList<>()).add(s);
indegree.put(s, indegree.getOrDefault(s, 0) + 1);
indegree.putIfAbsent(r, indegree.getOrDefault(r, 0));
}
}
// Kahn topological sort
Queue<String> q = new LinkedList<>();
for (Map.Entry<String, Integer> e : indegree.entrySet()) {
if (e.getValue() == 0) q.add(e.getKey());
}
List<String> ordered = new ArrayList<>();
while (!q.isEmpty()) {
String u = q.poll();
ordered.add(u);
for (String v : graph.getOrDefault(u, List.of())) {
indegree.put(v, indegree.get(v) - 1);
if (indegree.get(v) == 0) q.add(v);
}
}
// If cycle or missing, fall back preserving original order for remaining
if (ordered.size() < services.size()) {
HashSet<String> seen = new HashSet<>(ordered);
for (String s : services) {
if (!seen.contains(s)) ordered.add(s);
}
}
// For STOP, reverse to stop dependents first
if (command == org.apache.bigtop.manager.common.enums.Command.STOP) {
List<String> reversed = new ArrayList<>();
for (int i = ordered.size() - 1; i >= 0; i--) {
reversed.add(ordered.get(i));
}
return reversed;
}
return ordered;
}

/**
* Recursively collect transitive required services for a given service from stack metainfo.
* Uses memoization and guards against cycles.
*/
private List<String> getAllRequiredServices(String serviceName, Map<String, List<String>> memo) {
if (memo.containsKey(serviceName)) {
return memo.get(serviceName);
}
ServiceDTO dto = StackUtils.getServiceDTO(serviceName);
List<String> direct = dto.getRequiredServices();
if (direct == null) direct = List.of();
HashSet<String> visited = new HashSet<>();
List<String> result = new ArrayList<>();
// DFS
for (String dep : direct) {
if (visited.add(dep)) {
result.add(dep);
// Recurse
List<String> sub = getAllRequiredServices(dep, memo);
for (String s : sub) {
if (visited.add(s)) {
result.add(s);
}
}
}
}
memo.put(serviceName, result);
return result;
}

private List<String> getServiceNames() {
return jobContext.getCommandDTO().getServiceCommands().stream()
.map(ServiceCommandDTO::getServiceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,25 @@ protected void createStages() {
CommandDTO commandDTO = jobContext.getCommandDTO();
Map<String, List<String>> componentHostsMap = getComponentHostsMap();

// Install components
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.ADD, commandDTO));
// Order services by required-services for ADD
List<String> orderedServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.ADD);
for (String serviceName : orderedServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);

// Configure components
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.CONFIGURE, commandDTO));
// Install components
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.ADD, commandDTO));

// Init/Start/Prepare components
// Since the order of these stages might be mixed up, we need to sort and add them together.
// For example, the order usually is init -> start -> prepare, but Hive Metastore init requires MySQL Server to
// be prepared.
List<Command> commands = List.of(Command.INIT, Command.START, Command.PREPARE);
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, commands, commandDTO));
// Configure components
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.CONFIGURE, commandDTO));

// Check all master components after started
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.CHECK, commandDTO));
// Init/Start/Prepare components (combined ordering handled in helper)
List<Command> commands = List.of(Command.INIT, Command.START, Command.PREPARE);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, commands, commandDTO));

// Check all master components after started
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.CHECK, commandDTO));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,27 @@ protected void createStages() {
CommandDTO commandDTO = jobContext.getCommandDTO();
Map<String, List<String>> componentHostsMap = getComponentHostsMap();

stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.CONFIGURE, commandDTO));
// Order services for CONFIGURE and restart
List<String> orderedServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.CONFIGURE);
for (String serviceName : orderedServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.CONFIGURE, commandDTO));
}

// Restart services
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.STOP, commandDTO));
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.START, commandDTO));
// Restart services stop then start respecting ordering
List<String> stopServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.STOP);
for (String serviceName : stopServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.STOP, commandDTO));
}
List<String> startServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.START);
for (String serviceName : startServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.START, commandDTO));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ protected void createStages() {
CommandDTO commandDTO = jobContext.getCommandDTO();
Map<String, List<String>> componentHostsMap = getComponentHostsMap();

// Restart services
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.STOP, commandDTO));
stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, Command.START, commandDTO));
// STOP in reverse order
List<String> stopServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.STOP);
for (String serviceName : stopServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.STOP, commandDTO));
}
// START in forward order
List<String> startServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.START);
for (String serviceName : startServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, Command.START, commandDTO));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ protected void createStages() {
CommandDTO commandDTO = jobContext.getCommandDTO();
Map<String, List<String>> componentHostsMap = getComponentHostsMap();

stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, commandDTO));
// Order services by required-services for START
List<String> orderedServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.START);
for (String serviceName : orderedServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, commandDTO));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ protected void createStages() {
CommandDTO commandDTO = jobContext.getCommandDTO();
Map<String, List<String>> componentHostsMap = getComponentHostsMap();

stages.addAll(ComponentStageHelper.createComponentStages(componentHostsMap, commandDTO));
// Order services by required-services for STOP (reverse dependencies)
List<String> orderedServices =
getOrderedServiceNamesForCommand(org.apache.bigtop.manager.common.enums.Command.STOP);
for (String serviceName : orderedServices) {
Map<String, List<String>> perServiceHosts = filterComponentHostsByService(componentHostsMap, serviceName);
stages.addAll(ComponentStageHelper.createComponentStages(perServiceHosts, commandDTO));
}
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
"SECONDARYNAMENODE-RESTART": [
"NAMENODE-RESTART"
],
"DATANODE-STOP": [
"HBASE_MASTER-STOP"
],
"NAMENODE-STOP": [
"DATANODE-STOP",
"SECONDARYNAMENODE-STOP",
"HBASE_MASTER-STOP",
"HIVE_METASTORE-STOP"
"SECONDARYNAMENODE-STOP"
],
"NAMENODE-START": [
"ZKFC-START",
"JOURNALNODE-START",
"ZOOKEEPER_SERVER-START"
],
"ZKFC-START": [
"ZOOKEEPER_SERVER-START"
"JOURNALNODE-START"
],
"ZKFC-STOP": [
"NAMENODE-STOP"
Expand All @@ -29,7 +20,6 @@
"NAMENODE-STOP"
],
"RESOURCEMANAGER-START": [
"ZOOKEEPER_SERVER-START",
"NAMENODE-START",
"DATANODE-START"
],
Expand All @@ -44,10 +34,6 @@
"NODEMANAGER-RESTART": [
"NAMENODE-RESTART"
],
"NODEMANAGER-STOP": [
"HIVE_METASTORE-STOP",
"HIVESERVER2-STOP"
],
"HISTORY_SERVER-START": ["NAMENODE-START", "DATANODE-START"],
"HISTORY_SERVER-RESTART": ["NAMENODE-RESTART"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@
],
"HBASE_MASTER-STOP": [
"HBASE_REGIONSERVER-STOP"
],
"HBASE_MASTER-START": [
"NAMENODE-START",
"DATANODE-START",
"ZOOKEEPER_SERVER-START"
]
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
{
"HIVE_METASTORE-INIT": [
"MYSQL_SERVER-PREPARE"
],
"HIVE_METASTORE-START": [
"NAMENODE-START",
"NODEMANAGER-START"
],
"HIVE_METASTORE-RESTART": [
"MYSQL_SERVER-RESTART",
"NAMENODE-RESTART",
"NODEMANAGER-RESTART"
],
"HIVE_METASTORE-STOP": [
"SPARK_HISTORYSERVER-STOP",
"SPARK_THRIFTSERVER-STOP"
],
"HIVESERVER2-START": [
"NODEMANAGER-START",
"ZOOKEEPER_SERVER-START",
"HIVE_METASTORE-START"
],
"HIVESERVER2-RESTART": [
"NODEMANAGER-RESTART",
"ZOOKEEPER_SERVER-RESTART",
"HIVE_METASTORE-RESTART"
]
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void testCreateStages() {
when(ComponentStageHelper.createComponentStages(any(), eq(Command.CHECK), any()))
.thenReturn(stageList4);

// Ensure ordered services is non-empty to trigger stage aggregation in createStages
when(serviceAddJob.getOrderedServiceNamesForCommand(any())).thenReturn(List.of("dummy-service"));

// Use real method for createStages
doCallRealMethod().when(serviceAddJob).createStages();
serviceAddJob.createStages();

Expand Down
Loading
Loading