1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.serviceregistry.api;
23
24 import org.opencastproject.util.NotFoundException;
25
26 import java.util.Collection;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
30 import javax.xml.bind.annotation.XmlAccessType;
31 import javax.xml.bind.annotation.XmlAccessorType;
32 import javax.xml.bind.annotation.XmlAttribute;
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlElementWrapper;
35 import javax.xml.bind.annotation.XmlRootElement;
36 import javax.xml.bind.annotation.XmlType;
37
38
39
40
41 @XmlType(name = "load", namespace = "http://serviceregistry.opencastproject.org")
42 @XmlRootElement(name = "load", namespace = "http://serviceregistry.opencastproject.org")
43 @XmlAccessorType(XmlAccessType.NONE)
44 public class SystemLoad {
45
46
47 public SystemLoad() {
48 nodeLoads = new LinkedHashMap<String, NodeLoad>();
49 }
50
51
52 protected Map<String, NodeLoad> nodeLoads;
53
54
55
56
57
58
59 @XmlElementWrapper(name = "nodes")
60 @XmlElement(name = "node")
61 public Collection<NodeLoad> getNodeLoads() {
62 return nodeLoads.values();
63 }
64
65
66
67
68
69
70
71 public void setNodeLoads(Collection<NodeLoad> newLoads) {
72 for (NodeLoad node : newLoads) {
73 nodeLoads.put(node.getHost(), node);
74 }
75 }
76
77
78
79
80
81
82
83
84 public void updateNodeLoad(String host, float modifier) throws NotFoundException {
85 if (!nodeLoads.containsKey(host)) {
86 throw new NotFoundException("Host " + host + " not in this load object");
87 }
88 nodeLoads.get(host).modifyLoad(modifier);
89 }
90
91
92
93
94
95
96 public void addNodeLoad(NodeLoad load) {
97 nodeLoads.put(load.getHost(), load);
98 }
99
100
101
102
103
104
105
106
107 public NodeLoad get(String host) {
108 if (host != null && this.containsHost(host))
109 return nodeLoads.get(host);
110 return null;
111 }
112
113
114
115
116
117
118
119
120 public boolean containsHost(String host) {
121 if (host != null)
122 return nodeLoads.containsKey(host);
123 return false;
124 }
125
126 @Override
127 public String toString() {
128 StringBuilder sb = new StringBuilder();
129 sb.append("Current Loads:\n");
130 for (NodeLoad n : getNodeLoads()) {
131 sb.append(String.format(" %s: %f / %f\n", n.getHost(), n.getCurrentLoad(), n.getMaxLoad()));
132 }
133 return sb.toString();
134 }
135
136
137
138 @XmlType(name = "nodetype", namespace = "http://serviceregistry.opencastproject.org")
139 @XmlRootElement(name = "nodetype", namespace = "http://serviceregistry.opencastproject.org")
140 @XmlAccessorType(XmlAccessType.NONE)
141 public static class NodeLoad implements Comparable<NodeLoad> {
142
143
144 public NodeLoad() {
145 }
146
147 public NodeLoad(String host, float currentload, float maxload) {
148 this.host = host;
149 this.currentLoad = currentload;
150 this.maxLoad = maxload;
151 }
152
153
154 @XmlAttribute
155 protected String host;
156
157
158 @XmlAttribute
159 protected float currentLoad;
160
161 @XmlAttribute
162 protected float maxLoad;
163
164
165
166
167 public String getHost() {
168 return host;
169 }
170
171
172
173
174
175 public void setHost(String host) {
176 this.host = host;
177 }
178
179
180
181
182 public float getLoadFactor() {
183 return currentLoad / maxLoad;
184 }
185
186
187
188
189
190
191 public void modifyLoad(float modifier) {
192 this.currentLoad = this.currentLoad + modifier;
193 }
194
195 public float getCurrentLoad() {
196 return currentLoad;
197 }
198
199 public void setCurrentLoad(float load) {
200 this.currentLoad = load;
201 }
202
203 public float getMaxLoad() {
204 return maxLoad;
205 }
206
207 public void setMaxLoad(float load) {
208 this.maxLoad = load;
209 }
210
211
212 @Override
213 public int compareTo(NodeLoad other) {
214 if (other.getLoadFactor() > this.getLoadFactor()) {
215 return 1;
216 } else if (this.getLoadFactor() > other.getLoadFactor()) {
217 return -1;
218 } else {
219 return 0;
220 }
221 }
222
223 public boolean exceeds(NodeLoad other) {
224 if (this.compareTo(other) > 1) {
225 return true;
226 }
227 return false;
228 }
229 }
230 }