-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClusterState.java
147 lines (131 loc) · 5.43 KB
/
ClusterState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.application.manager.business;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.ActionProvider;
import org.appng.api.DataContainer;
import org.appng.api.DataProvider;
import org.appng.api.FieldProcessor;
import org.appng.api.Options;
import org.appng.api.Request;
import org.appng.api.Scope;
import org.appng.api.messaging.Messaging;
import org.appng.api.model.Application;
import org.appng.api.model.Site;
import org.appng.api.model.Site.SiteState;
import org.appng.application.manager.MessageConstants;
import org.appng.core.controller.messaging.NodeEvent;
import org.appng.core.controller.messaging.NodeEvent.MemoryUsage;
import org.appng.core.controller.messaging.NodeEvent.NodeState;
import org.appng.core.controller.messaging.RequestNodeState;
import org.appng.core.controller.messaging.SiteStateEvent;
import org.springframework.stereotype.Service;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class ClusterState implements DataProvider, ActionProvider<Void> {
public DataContainer getData(Site site, Application application, org.appng.api.Environment environment,
Options options, Request request, FieldProcessor fieldProcessor) {
DataContainer dataContainer = new DataContainer(fieldProcessor);
String mode = options.getOptionValue("mode", "value");
String nodeId = options.getOptionValue("mode", "nodeId");
Map<String, NodeState> nodeStates = environment.getAttribute(Scope.PLATFORM, NodeEvent.NODE_STATE);
LocalNodeState localNode = getLocalNode(site, environment);
LocalNodeState currentNode = localNode;
Set<LocalNodeState> items = new HashSet<>();
items.add(localNode);
boolean clusterAvailable = null != nodeStates;
if (!clusterAvailable) {
if (Messaging.isEnabled(environment)) {
fieldProcessor.addErrorMessage(request.getMessage(MessageConstants.CLUSTER_NOT_AVAILABLE));
} else {
fieldProcessor.addNoticeMessage(request.getMessage(MessageConstants.CLUSTER_DISABLED));
}
} else if (StringUtils.isNotBlank(nodeId)) {
NodeState nodeState = nodeStates.get(nodeId);
if (null != nodeState) {
currentNode = new LocalNodeState(nodeState);
}
}
if (StringUtils.isBlank(mode)) {
if (clusterAvailable) {
nodeStates.values().forEach(ns -> items.add(new LocalNodeState(ns)));
}
dataContainer.setPage(items, fieldProcessor.getPageable());
} else if (null != currentNode) {
Map<?, ?> entries = null;
if ("props".equals(mode)) {
entries = currentNode.getProps();
} else if ("env".equals(mode)) {
entries = currentNode.getEnv();
} else if ("siteState".equals(mode)) {
entries = currentNode.getSiteStates();
}
List<Entry<String, ?>> sortedEntries = null == entries ? new ArrayList<>()
: Environment.getSortedEntries(entries);
dataContainer.setPage(sortedEntries, fieldProcessor.getPageable());
}
return dataContainer;
}
private LocalNodeState getLocalNode(Site site, org.appng.api.Environment environment) {
NodeEvent nodeEvent = new NodeEvent(environment, site.getName());
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heap = nodeEvent.new MemoryUsage(memoryMXBean.getHeapMemoryUsage());
MemoryUsage nonHeap = nodeEvent.new MemoryUsage(memoryMXBean.getNonHeapMemoryUsage());
Map<String, SiteState> siteStates = environment.getAttribute(Scope.PLATFORM, SiteStateEvent.SITE_STATE);
LocalNodeState localNode = new LocalNodeState(Messaging.getNodeId(environment), new Date(), heap, nonHeap,
System.getProperties(), System.getenv(), siteStates);
if (log.isDebugEnabled()) {
log.debug("local node is {}", localNode);
}
localNode.setCurrent(true);
return localNode;
}
public void perform(Site site, Application application, org.appng.api.Environment environment, Options options,
Request request, Void formBean, FieldProcessor fieldProcessor) {
site.sendEvent(new RequestNodeState(site.getName()));
}
@Data
@RequiredArgsConstructor
@EqualsAndHashCode(of = { "nodeId" })
public class LocalNodeState {
private final String nodeId;
private final Date date;
private final MemoryUsage heap;
private final MemoryUsage nonHeap;
private final Properties props;
private final Map<String, String> env;
private final Map<String, SiteState> siteStates;
private boolean current = false;
public LocalNodeState(NodeState nodeState) {
this(nodeState.getNodeId(), nodeState.getDate(), nodeState.getHeap(), nodeState.getNonHeap(),
nodeState.getProps(), nodeState.getEnv(), nodeState.getSiteStates());
}
}
}