/
ExampleMultiServiceResource.java
274 lines (243 loc) · 11.1 KB
/
ExampleMultiServiceResource.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.mesosphere.sdk.helloworld.scheduler;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AbstractFileFilter;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import com.mesosphere.sdk.framework.FrameworkConfig;
import com.mesosphere.sdk.http.ResponseUtils;
import com.mesosphere.sdk.offer.LoggingUtils;
import com.mesosphere.sdk.scheduler.AbstractScheduler;
import com.mesosphere.sdk.scheduler.DefaultScheduler;
import com.mesosphere.sdk.scheduler.SchedulerBuilder;
import com.mesosphere.sdk.scheduler.SchedulerConfig;
import com.mesosphere.sdk.scheduler.multi.MultiServiceEventClient;
import com.mesosphere.sdk.scheduler.multi.MultiServiceManager;
import com.mesosphere.sdk.scheduler.multi.ServiceFactory;
import com.mesosphere.sdk.scheduler.multi.ServiceStore;
import com.mesosphere.sdk.scheduler.uninstall.UninstallScheduler;
import com.mesosphere.sdk.specification.DefaultServiceSpec;
import com.mesosphere.sdk.specification.ServiceSpec;
import com.mesosphere.sdk.specification.yaml.RawServiceSpec;
import com.mesosphere.sdk.storage.Persister;
import com.mesosphere.sdk.storage.PersisterException;
/**
* Example implementation of a resource which dynamically adds and removes services from a dynamic multi-scheduler.
* This implementation allows users to add/remove example scenario yaml files, referenced by their filename.
*/
@Path("/v1/multi")
public class ExampleMultiServiceResource {
private static final Logger LOGGER = LoggingUtils.getLogger(ExampleMultiServiceResource.class);
private static final String YAML_DIR = "hello-world-scheduler/";
private static final String YAML_EXT = ".yml";
private static final Charset CHARSET = StandardCharsets.UTF_8;
/**
* Data type for storing the service name and yaml name which serializes to JSON.
*/
private static class ContextData {
private final String serviceName;
private final String yamlName;
private final Map<String, String> envOverride;
private ContextData(String serviceName, String yamlName, Map<String, String> envOverride) {
this.serviceName = serviceName;
this.yamlName = yamlName;
this.envOverride = new TreeMap<>(envOverride);
}
private static ContextData deserialize(byte[] context) {
JSONObject obj = new JSONObject(new String(context, CHARSET));
Map<String, String> params = new TreeMap<>();
JSONArray jsonParams = obj.getJSONArray("params");
for (int i = 0; i < jsonParams.length(); ++i) {
JSONObject jsonParam = jsonParams.getJSONObject(i);
params.put(jsonParam.getString("key"), jsonParam.getString("value"));
}
return new ContextData(obj.getString("name"), obj.getString("yaml"), params);
}
private byte[] serialize() {
JSONObject obj = new JSONObject();
obj.put("name", serviceName);
obj.put("yaml", yamlName);
JSONArray params = new JSONArray();
for (Map.Entry<String, String> entry : envOverride.entrySet()) {
JSONObject jsonEntry = new JSONObject();
jsonEntry.put("key", entry.getKey());
jsonEntry.put("value", entry.getValue());
params.put(jsonEntry);
}
obj.put("params", params);
return obj.toString().getBytes(CHARSET);
}
}
private final MultiServiceManager multiServiceManager;
private final ServiceStore serviceStore;
ExampleMultiServiceResource(
SchedulerConfig schedulerConfig,
FrameworkConfig frameworkConfig,
Persister persister,
Collection<Scenario.Type> scenarios,
MultiServiceManager multiServiceManager) {
this.multiServiceManager = multiServiceManager;
ServiceFactory serviceFactory = new ServiceFactory() {
@Override
public AbstractScheduler buildService(byte[] context) throws Exception {
// Generate a ServiceSpec from the provided yaml file name, which is in the context
ContextData contextData = ContextData.deserialize(context);
File yamlFile = getYamlFile(contextData.yamlName);
// Render service specs, with any provided parameters overriding the scheduler env:
Map<String, String> serviceParameters = new HashMap<>();
serviceParameters.putAll(System.getenv());
serviceParameters.putAll(contextData.envOverride);
RawServiceSpec rawServiceSpec =
RawServiceSpec.newBuilder(yamlFile).setEnv(serviceParameters).build();
ServiceSpec serviceSpec = DefaultServiceSpec.newGenerator(
rawServiceSpec, schedulerConfig, serviceParameters, yamlFile.getParentFile())
// Override any framework-level params in the servicespec (role, principal, ...) with ours:
.setMultiServiceFrameworkConfig(frameworkConfig)
.build();
SchedulerBuilder builder = DefaultScheduler.newBuilder(serviceSpec, schedulerConfig, persister)
.setPlansFrom(rawServiceSpec)
.enableMultiService(frameworkConfig.getFrameworkName());
return Scenario.customize(builder, scenarios).build();
}
};
this.serviceStore = new ServiceStore(persister, serviceFactory);
}
/**
* Returns a list of all available YAML examples, suitable for launching an example service against.
*/
@Path("yaml")
@GET
public Response listYamls() {
JSONArray yamls = new JSONArray();
// Sort names alphabetically:
Collection<File> files = new TreeSet<>(FileUtils.listFiles(
new File(YAML_DIR),
new AbstractFileFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(YAML_EXT);
}
},
null /* do not iterate subdirs */));
for (File f : files) {
String name = f.getName();
// Remove .yml extension in response:
yamls.put(name.substring(0, name.length() - YAML_EXT.length()));
}
return ResponseUtils.jsonOkResponse(yamls);
}
/**
* Returns a list of added active services.
*/
@GET
public Response listServices() {
JSONArray services = new JSONArray();
for (String serviceName : multiServiceManager.getServiceNames()) {
JSONObject service = new JSONObject();
service.put("service", serviceName);
Optional<AbstractScheduler> scheduler = multiServiceManager.getService(serviceName);
// Technically, the scheduler could disappear if it's uninstalled while we iterate over service names
if (!scheduler.isPresent()) {
continue;
}
// YAML file path
try {
Optional<byte[]> context = serviceStore.get(serviceName);
if (context.isPresent()) {
service.put("yaml", ContextData.deserialize(context.get()).yamlName);
}
} catch (PersisterException e) {
LOGGER.error(String.format("Failed to get yaml filename for service %s", serviceName), e);
}
// Detect uninstall-in-progress by class type
service.put("uninstall", scheduler.get() instanceof UninstallScheduler);
services.put(service);
}
return ResponseUtils.jsonOkResponse(services);
}
/**
* Triggers uninstall of a specified service. Once it has finished uninstalling, it will automatically be removed
* from the set of active services.
*/
@Path("{serviceName}")
@DELETE
public Response uninstall(@PathParam("serviceName") String serviceName) {
multiServiceManager.uninstallService(serviceName);
return ResponseUtils.plainOkResponse("Triggered removal of service: " + serviceName);
}
/**
* Accepts a new service to be launched immediately, using the provided example yaml name.
*
* <p>See {@link #listYamls()} for a list of available yaml files.
*/
@Path("{serviceName}")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response add(
@PathParam("serviceName") String serviceName,
@QueryParam("yaml") String yamlName,
Map<String, String> envOverride) {
// Create an AbstractScheduler using the specified file, bailing if it doesn't work.
AbstractScheduler service;
try {
service = serviceStore.put(new ContextData(serviceName, yamlName, envOverride).serialize());
} catch (Exception e) {
LOGGER.error("Failed to generate or persist service", e);
return ResponseUtils.plainResponse(
String.format("Failed to generate or persist service: %s", e.getMessage()),
Response.Status.BAD_REQUEST);
}
multiServiceManager.putService(service);
try {
JSONObject obj = new JSONObject();
obj.put("name", service.getServiceSpec().getName());
obj.put("yaml", yamlName);
return ResponseUtils.jsonOkResponse(obj);
} catch (Exception e) {
// This should never happen.
LOGGER.error("JSON error when encoding response for adding or updating service", e);
return Response.serverError().build();
}
}
/**
* Recovers any previously added service instances and re-adds them to the internal MultiServiceManager.
*
* <p>Recovery should always be invoked once during startup to rebuild any previously-added services. If no services
* were active or if this is the initial launch of the scheduler, then this is effectively a no-op.
*/
public void recover() throws PersisterException {
for (AbstractScheduler service : serviceStore.recover()) {
multiServiceManager.putService(service);
}
}
/**
* Returns an uninstall callback suitable for passing to the MultiServiceEventClient.
*/
public MultiServiceEventClient.UninstallCallback getUninstallCallback() {
return serviceStore.getUninstallCallback();
}
/**
* Returns the specified example YAML file from the scheduler filesystem.
*/
public static File getYamlFile(String yamlName) {
return new File(YAML_DIR, yamlName + YAML_EXT);
}
}