-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCronLoadBasedPolicy.java
More file actions
142 lines (124 loc) · 4.97 KB
/
CronLoadBasedPolicy.java
File metadata and controls
142 lines (124 loc) · 4.97 KB
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
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.resourcemanager.nodesource.policy;
import java.util.concurrent.atomic.AtomicBoolean;
import org.objectweb.proactive.core.util.wrapper.BooleanWrapper;
import org.objectweb.proactive.extensions.annotation.ActiveObject;
import org.ow2.proactive.resourcemanager.authentication.Client;
import org.ow2.proactive.resourcemanager.nodesource.common.Configurable;
import it.sauronsoftware.cron4j.Scheduler;
/**
*
* The policy that triggers new nodes acquisition when scheduler is overloaded within a time slot defined in crontab syntax.
*
*/
@ActiveObject
public class CronLoadBasedPolicy extends SchedulerLoadingPolicy {
/**
* Initial time for nodes acquisition
*/
@Configurable(description = "Time since the nodes acquisition is allowed (crontab format)")
private String acquisionAllowed = "* * * * *";
@Configurable(description = "Time since the nodes acquisition is forbiden (crontab format)")
private String acquisionForbidden = "* * * * *";
/**
* The way of nodes removing
*/
@Configurable(description = "the mode how nodes are removed")
private boolean preemptive = false;
@Configurable(description = "If true acquisition will be immediately allowed")
private boolean allowed = false;
private AtomicBoolean isAcquisitionAllowed = new AtomicBoolean(false);
private Scheduler cronScheduler;
/**
* Configure a policy with given parameters.
* @param policyParameters parameters defined by user
*/
@Override
public BooleanWrapper configure(Object... policyParameters) {
super.configure(policyParameters);
cronScheduler = new Scheduler();
try {
int index = 9;
acquisionAllowed = policyParameters[index++].toString();
acquisionForbidden = policyParameters[index++].toString();
preemptive = Boolean.parseBoolean(policyParameters[index++].toString());
allowed = Boolean.parseBoolean(policyParameters[index++].toString());
isAcquisitionAllowed.set(allowed);
} catch (RuntimeException e) {
throw new IllegalArgumentException(e);
}
return new BooleanWrapper(true);
}
@Override
public BooleanWrapper activate() {
BooleanWrapper activationStatus = super.activate();
if (!activationStatus.getBooleanValue()) {
return activationStatus;
}
cronScheduler.schedule(acquisionAllowed, new Runnable() {
public void run() {
logger.info("Allowing nodes acquisition");
isAcquisitionAllowed.set(true);
}
});
cronScheduler.schedule(acquisionForbidden, new Runnable() {
public void run() {
logger.info("Forbidding nodes acquisition");
isAcquisitionAllowed.set(false);
}
});
cronScheduler.start();
return new BooleanWrapper(true);
}
protected void updateNumberOfNodes() {
if (nodesNumberInNodeSource.get() > 0 && !isAcquisitionAllowed.get()) {
logger.debug("Policy triggers all nodes removal");
removeAllNodes(preemptive);
}
if (isAcquisitionAllowed.get()) {
logger.debug("Node acquisition allowed");
super.updateNumberOfNodes();
} else {
logger.debug("Node acquisition forbidden");
}
}
@Override
public void shutdown(Client initiator) {
cronScheduler.stop();
super.shutdown(initiator);
}
@Override
public String toString() {
return super.toString() + ", acquisition allowed at [" + acquisionAllowed + "]" +
", acquisition forbidden at [" + acquisionForbidden + "], preemptive: " + preemptive +
", allowed initially: " + allowed;
}
@Override
public String getDescription() {
return "Triggers new nodes acquisition when scheduler is overloaded within a time slot defined in crontab syntax.";
}
}