-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
InfinispanDevServiceProcessor.java
340 lines (292 loc) · 15.8 KB
/
InfinispanDevServiceProcessor.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package io.quarkus.infinispan.client.deployment.devservices;
import static io.quarkus.runtime.LaunchMode.DEVELOPMENT;
import static org.infinispan.server.test.core.InfinispanContainer.DEFAULT_USERNAME;
import java.io.Closeable;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.infinispan.client.hotrod.configuration.ClientIntelligence;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;
import org.infinispan.commons.util.Version;
import org.infinispan.server.test.core.InfinispanContainer;
import org.jboss.logging.Logger;
import org.testcontainers.containers.BindMode;
import io.quarkus.deployment.Feature;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
import io.quarkus.deployment.builditem.DevServicesResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService;
import io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem;
import io.quarkus.deployment.builditem.DockerStatusBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.console.ConsoleInstalledBuildItem;
import io.quarkus.deployment.console.StartupLogCompressor;
import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig;
import io.quarkus.deployment.logging.LoggingSetupBuildItem;
import io.quarkus.devservices.common.ConfigureUtil;
import io.quarkus.devservices.common.ContainerLocator;
import io.quarkus.infinispan.client.runtime.InfinispanClientBuildTimeConfig;
import io.quarkus.infinispan.client.runtime.InfinispanClientUtil;
import io.quarkus.infinispan.client.runtime.InfinispanClientsBuildTimeConfig;
import io.quarkus.infinispan.client.runtime.InfinispanDevServicesConfig;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigUtils;
@BuildSteps(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
public class InfinispanDevServiceProcessor {
private static final Logger log = Logger.getLogger(InfinispanDevServiceProcessor.class);
/**
* Label to add to shared Dev Service for Infinispan running in containers.
* This allows other applications to discover the running service and use it instead of starting a new instance.
*/
private static final String DEV_SERVICE_LABEL = "quarkus-dev-service-infinispan";
public static final int DEFAULT_INFINISPAN_PORT = ConfigurationProperties.DEFAULT_HOTROD_PORT;
private static final ContainerLocator infinispanContainerLocator = new ContainerLocator(DEV_SERVICE_LABEL,
DEFAULT_INFINISPAN_PORT);
private static final String DEFAULT_PASSWORD = "password";
private static final String QUARKUS = "quarkus.";
private static final String DOT = ".";
private static final String UNDERSCORE = "_";
private static volatile Map<String, RunningDevService> devServices;
private static volatile Map<String, InfinispanClientBuildTimeConfig.DevServiceConfiguration> capturedDevServicesConfiguration;
private static volatile boolean first = true;
private static volatile Map<String, String> properties = new HashMap<>();
@BuildStep
public List<DevServicesResultBuildItem> startInfinispanContainers(LaunchModeBuildItem launchMode,
DockerStatusBuildItem dockerStatusBuildItem,
List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem,
InfinispanClientsBuildTimeConfig config,
Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem,
CuratedApplicationShutdownBuildItem closeBuildItem,
LoggingSetupBuildItem loggingSetupBuildItem,
GlobalDevServicesConfig globalDevServicesConfig) {
// figure out if we need to shut down and restart existing Infinispan containers
// if not and the Infinispan containers have already started we just return
if (devServices != null) {
boolean restartRequired = false;
for (String devServiceName : devServices.keySet()) {
InfinispanClientBuildTimeConfig.DevServiceConfiguration devServiceConfig = capturedDevServicesConfiguration.get(
devServiceName);
restartRequired = restartRequired
|| !config.getInfinispanClientBuildTimeConfig(devServiceName).devService.equals(
devServiceConfig);
}
if (!restartRequired) {
return devServices.values().stream().map(RunningDevService::toBuildItem).collect(Collectors.toList());
}
for (Closeable closeable : devServices.values()) {
try {
closeable.close();
} catch (Throwable e) {
log.error("Failed to stop infinispan container", e);
}
}
devServices = null;
capturedDevServicesConfiguration = null;
}
capturedDevServicesConfiguration = new HashMap<>();
Map<String, RunningDevService> newDevServices = new HashMap<>();
capturedDevServicesConfiguration.put(InfinispanClientUtil.DEFAULT_INFINISPAN_CLIENT_NAME,
config.defaultInfinispanClient.devService);
for (Map.Entry<String, InfinispanClientBuildTimeConfig> entry : config.namedInfinispanClients.entrySet()) {
capturedDevServicesConfiguration.put(entry.getKey(), entry.getValue().devService);
}
StartupLogCompressor compressor = new StartupLogCompressor(
(launchMode.isTest() ? "(test) " : "") + "Infinispan Dev Services Starting:", consoleInstalledBuildItem,
loggingSetupBuildItem);
runInfinispanDevService(InfinispanClientUtil.DEFAULT_INFINISPAN_CLIENT_NAME, launchMode,
compressor, dockerStatusBuildItem, devServicesSharedNetworkBuildItem, config.defaultInfinispanClient,
globalDevServicesConfig, newDevServices,
properties);
config.namedInfinispanClients.entrySet().forEach(dServ -> {
runInfinispanDevService(dServ.getKey(), launchMode,
compressor, dockerStatusBuildItem, devServicesSharedNetworkBuildItem, dServ.getValue(),
globalDevServicesConfig,
newDevServices, properties);
});
devServices = newDevServices;
if (first) {
first = false;
Runnable closeTask = () -> {
if (devServices != null) {
for (Closeable closeable : devServices.values()) {
try {
closeable.close();
} catch (Throwable t) {
log.error("Failed to stop infinispan", t);
}
}
}
first = true;
devServices = null;
capturedDevServicesConfiguration = null;
};
closeBuildItem.addCloseTask(closeTask, true);
}
return devServices.values().stream().map(RunningDevService::toBuildItem).collect(Collectors.toList());
}
private void runInfinispanDevService(String clientName,
LaunchModeBuildItem launchMode,
StartupLogCompressor compressor,
DockerStatusBuildItem dockerStatusBuildItem,
List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem,
InfinispanClientBuildTimeConfig config,
GlobalDevServicesConfig globalDevServicesConfig,
Map<String, RunningDevService> newDevServices,
Map<String, String> properties) {
try {
InfinispanDevServicesConfig namedDevServiceConfig = config.devService.devservices;
RunningDevService devService = startContainer(clientName, dockerStatusBuildItem, namedDevServiceConfig,
launchMode.getLaunchMode(),
!devServicesSharedNetworkBuildItem.isEmpty(), globalDevServicesConfig.timeout, properties);
if (devService == null) {
compressor.closeAndDumpCaptured();
return;
}
newDevServices.put(clientName, devService);
log.infof("The infinispan server is ready to accept connections on %s",
devService.getConfig().get(getConfigPrefix(clientName) + "hosts"));
compressor.close();
} catch (Throwable t) {
compressor.closeAndDumpCaptured();
throw new RuntimeException(t);
}
}
private RunningDevService startContainer(String clientName, DockerStatusBuildItem dockerStatusBuildItem,
InfinispanDevServicesConfig devServicesConfig, LaunchMode launchMode,
boolean useSharedNetwork, Optional<Duration> timeout, Map<String, String> properties) {
if (!devServicesConfig.enabled) {
// explicitly disabled
log.debug("Not starting Dev Services for Infinispan as it has been disabled in the config");
return null;
}
String configPrefix = getConfigPrefix(clientName);
boolean needToStart = !ConfigUtils.isPropertyPresent(configPrefix + "hosts")
&& !ConfigUtils.isPropertyPresent(configPrefix + "server-list");
if (!needToStart) {
log.debug("Not starting Dev Services for Infinispan as 'hosts', 'uri' or 'server-list' have been provided");
return null;
}
if (!dockerStatusBuildItem.isDockerAvailable()) {
log.warn(
"Please configure 'quarkus.infinispan-client.hosts' or 'quarkus.infinispan-client.uri' or get a working docker instance");
return null;
}
log.infof("Starting Dev Service for connection %s", clientName);
log.infof("Apply Dev Services config %s", devServicesConfig);
Supplier<RunningDevService> infinispanServerSupplier = () -> {
QuarkusInfinispanContainer infinispanContainer = new QuarkusInfinispanContainer(clientName, devServicesConfig,
launchMode,
useSharedNetwork);
timeout.ifPresent(infinispanContainer::withStartupTimeout);
infinispanContainer.withEnv(devServicesConfig.containerEnv);
infinispanContainer.start();
return getRunningDevService(clientName, infinispanContainer.getContainerId(), infinispanContainer::close,
infinispanContainer.getHost() + ":" + infinispanContainer.getPort(),
infinispanContainer.getUser(), infinispanContainer.getPassword(), properties);
};
return infinispanContainerLocator.locateContainer(devServicesConfig.serviceName, devServicesConfig.shared, launchMode)
.map(containerAddress -> getRunningDevService(clientName, containerAddress.getId(), null,
containerAddress.getUrl(), DEFAULT_USERNAME, DEFAULT_PASSWORD, properties)) // TODO can this be always right ?
.orElseGet(infinispanServerSupplier);
}
private RunningDevService getRunningDevService(String clientName, String containerId, Closeable closeable, String hosts,
String username, String password, Map<String, String> config) {
config.put(getConfigPrefix(clientName) + "hosts", hosts);
config.put(getConfigPrefix(clientName) + "client-intelligence", ClientIntelligence.BASIC.name());
config.put(getConfigPrefix(clientName) + "username", username);
config.put(getConfigPrefix(clientName) + "password", password);
return new RunningDevService(runningServiceName(clientName), containerId, closeable, config);
}
private String runningServiceName(String clientName) {
if (InfinispanClientUtil.isDefault(clientName)) {
return Feature.INFINISPAN_CLIENT.getName();
}
return Feature.INFINISPAN_CLIENT.getName() + UNDERSCORE + clientName;
}
private String getConfigPrefix(String name) {
if (name.equals(InfinispanClientUtil.DEFAULT_INFINISPAN_CLIENT_NAME)) {
return QUARKUS + InfinispanClientUtil.INFINISPAN_CLIENT_CONFIG_ROOT_NAME + DOT;
}
return QUARKUS + InfinispanClientUtil.INFINISPAN_CLIENT_CONFIG_ROOT_NAME + DOT + name + DOT;
}
private static class QuarkusInfinispanContainer extends InfinispanContainer {
private final OptionalInt fixedExposedPort;
private final boolean useSharedNetwork;
private String hostName = null;
public QuarkusInfinispanContainer(String clientName, InfinispanDevServicesConfig config,
LaunchMode launchMode, boolean useSharedNetwork) {
super(config.imageName.orElse(IMAGE_BASENAME + ":" + Version.getVersion()));
this.fixedExposedPort = config.port;
this.useSharedNetwork = useSharedNetwork;
if (launchMode == DEVELOPMENT) {
String label = config.serviceName;
if (InfinispanClientUtil.DEFAULT_INFINISPAN_DEV_SERVICE_NAME.equals(label)
&& !InfinispanClientUtil.isDefault(clientName)) {
// Adds the client name suffix to create a different service name in named connections
label = label + UNDERSCORE + clientName;
}
withLabel(DEV_SERVICE_LABEL, label);
}
withUser(DEFAULT_USERNAME);
withPassword(InfinispanDevServiceProcessor.DEFAULT_PASSWORD);
String command = "-c infinispan.xml";
if (config.site.isPresent()) {
command = "-c infinispan-xsite.xml -Dinfinispan.site.name=" + config.site.get();
}
command = command + config.configFiles.map(files -> files.stream().map(file -> {
String userConfigFile = "/user-config/" + file;
withClasspathResourceMapping(file, userConfigFile, BindMode.READ_ONLY);
return " -c " + userConfigFile;
}).collect(Collectors.joining())).orElse("");
if (config.mcastPort.isPresent()) {
command = command + " -Djgroups.mcast_port=" + config.mcastPort.getAsInt();
}
if (config.tracing.isPresent()) {
command = command + " -Dinfinispan.tracing.enabled=" + config.tracing.get();
command = command + " -Dotel.exporter.otlp.endpoint=" + config.exporterOtlpEndpoint.get();
command = command + " -Dotel.service.name=infinispan-server-service -Dotel.metrics.exporter=none";
}
config.artifacts.ifPresent(a -> withArtifacts(a.toArray(new String[0])));
withCommand(command);
}
@Override
protected void configure() {
super.configure();
if (useSharedNetwork) {
hostName = ConfigureUtil.configureSharedNetwork(this, "infinispan");
return;
}
if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), DEFAULT_INFINISPAN_PORT);
} else {
addExposedPort(DEFAULT_INFINISPAN_PORT);
}
}
public int getPort() {
if (useSharedNetwork) {
return DEFAULT_INFINISPAN_PORT;
}
if (fixedExposedPort.isPresent()) {
return fixedExposedPort.getAsInt();
}
return super.getFirstMappedPort();
}
public String getUser() {
return DEFAULT_USERNAME;
}
public String getPassword() {
return InfinispanDevServiceProcessor.DEFAULT_PASSWORD;
}
@Override
public String getHost() {
return useSharedNetwork ? hostName : super.getHost();
}
}
}