-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchedulerAwarePolicy.java
187 lines (154 loc) · 6.9 KB
/
SchedulerAwarePolicy.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* 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: [email protected]
*
* 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.io.File;
import org.apache.log4j.Logger;
import org.objectweb.proactive.core.util.wrapper.BooleanWrapper;
import org.objectweb.proactive.extensions.annotation.ActiveObject;
import org.ow2.proactive.authentication.crypto.Credentials;
import org.ow2.proactive.resourcemanager.authentication.Client;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
import org.ow2.proactive.resourcemanager.nodesource.common.Configurable;
import org.ow2.proactive.resourcemanager.nodesource.policy.NodeSourcePolicy;
import org.ow2.proactive.scheduler.common.NotificationData;
import org.ow2.proactive.scheduler.common.Scheduler;
import org.ow2.proactive.scheduler.common.SchedulerAuthenticationInterface;
import org.ow2.proactive.scheduler.common.SchedulerConnection;
import org.ow2.proactive.scheduler.common.SchedulerEvent;
import org.ow2.proactive.scheduler.common.SchedulerEventListener;
import org.ow2.proactive.scheduler.common.SchedulerState;
import org.ow2.proactive.scheduler.common.exception.ConnectionException;
import org.ow2.proactive.scheduler.common.job.JobInfo;
import org.ow2.proactive.scheduler.common.job.JobState;
import org.ow2.proactive.scheduler.common.job.UserIdentification;
import org.ow2.proactive.scheduler.common.task.TaskInfo;
@ActiveObject
public abstract class SchedulerAwarePolicy extends NodeSourcePolicy implements SchedulerEventListener {
protected static Logger logger = Logger.getLogger(SchedulerAwarePolicy.class);
@Configurable(description = "url used to contact the scheduler, e.g. pnp://, pamr://")
protected String schedulerUrl = "";
@Configurable(credential = true, description = "credentials used when contacting the scheduler")
protected File schedulerCredentialsPath;
@Configurable(description = "timeout in ms for the resource manger to recover broken node source")
protected long nodeSourceRecoveryTimeout = 10000;
@Configurable(description = "number of trials for the resource manager to recover a broken node source")
protected int nodeSourceRecoveryTrialsNumber = 10;
protected SchedulerState state;
private byte[] credentials;
protected Scheduler scheduler;
/**
* Configure a policy with given parameters.
* @param policyParameters parameters defined by user
*/
@Override
public BooleanWrapper configure(Object... policyParameters) {
super.configure(policyParameters);
if (policyParameters[2] == null) {
throw new IllegalArgumentException("Scheduler url must be specified");
}
schedulerUrl = policyParameters[2].toString();
if (policyParameters[3] == null) {
throw new IllegalArgumentException("Credentials must be specified");
}
credentials = (byte[]) policyParameters[3];
if (policyParameters[4] == null) {
nodeSourceRecoveryTimeout = PAResourceManagerProperties.RM_SCHEDULER_AWARE_POLICY_NODESOURCE_RECOVERY_TIMEOUT.getValueAsLong();
}
if (policyParameters[5] == null) {
nodeSourceRecoveryTrialsNumber = PAResourceManagerProperties.RM_SCHEDULER_AWARE_POLICY_NODESOURCE_RECOVERY_TRIAL_NUMBER.getValueAsInt();
}
return new BooleanWrapper(true);
}
@Override
public BooleanWrapper activate() {
try {
waitAndConnectToScheduler();
} catch (Exception e) {
logger.debug("Number of trials exceeded and could not contact scheduler", e);
}
try {
state = scheduler.addEventListener(getSchedulerListener(), false, true, getEventsList());
} catch (Exception e) {
logger.error("", e);
throw new RuntimeException(e.getMessage(), e);
}
return new BooleanWrapper(true);
}
private void waitAndConnectToScheduler() throws Exception {
SchedulerAuthenticationInterface authentication;
boolean firstException = true;
int trialsNumber = 0;
while (scheduler == null && trialsNumber <= nodeSourceRecoveryTrialsNumber) {
trialsNumber++;
try {
authentication = SchedulerConnection.join(schedulerUrl);
Credentials creds = Credentials.getCredentialsBase64(credentials);
scheduler = authentication.login(creds);
Thread.sleep(nodeSourceRecoveryTimeout);
} catch (Throwable t) {
if (firstException) {
logger.warn("Could not contact scheduler at url " + schedulerUrl +
" this is normal if the scheduler has just been restarted", t);
firstException = false;
} else {
logger.debug("Could not contact scheduler", t);
}
}
if (trialsNumber > nodeSourceRecoveryTrialsNumber)
throw new ConnectionException("Number of trials exceeded and could not contact scheduler");
}
}
@Override
public void shutdown(Client initiator) {
try {
scheduler.removeEventListener();
scheduler.disconnect();
} catch (Exception e) {
logger.error("", e);
}
super.shutdown(initiator);
}
@Override
public void jobStateUpdatedEvent(NotificationData<JobInfo> notification) {
}
@Override
public void jobUpdatedFullDataEvent(JobState job) {
}
@Override
public void jobSubmittedEvent(JobState job) {
}
@Override
public void schedulerStateUpdatedEvent(SchedulerEvent eventType) {
}
@Override
public void taskStateUpdatedEvent(NotificationData<TaskInfo> notification) {
}
@Override
public void usersUpdatedEvent(NotificationData<UserIdentification> notification) {
}
protected abstract SchedulerEvent[] getEventsList();
protected abstract SchedulerEventListener getSchedulerListener();
}