-
Notifications
You must be signed in to change notification settings - Fork 68
/
ActivityTypeLoader.java
180 lines (157 loc) · 7.11 KB
/
ActivityTypeLoader.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
/*
* Copyright (c) 2022 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.engine.core.lifecycle;
import io.nosqlbench.engine.api.activityapi.core.ActivityType;
import io.nosqlbench.engine.api.activityimpl.ActivityDef;
import io.nosqlbench.engine.api.activityimpl.uniform.DriverAdapter;
import io.nosqlbench.engine.api.activityimpl.uniform.StandardActivityType;
import io.nosqlbench.nb.annotations.Maturity;
import io.nosqlbench.nb.api.NBEnvironment;
import io.nosqlbench.nb.api.content.Content;
import io.nosqlbench.nb.api.content.NBIO;
import io.nosqlbench.nb.api.errors.BasicError;
import io.nosqlbench.nb.api.spi.SimpleServiceLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
public class ActivityTypeLoader {
private static final Logger logger = LogManager.getLogger(ActivityTypeLoader.class);
private final SimpleServiceLoader<ActivityType> ACTIVITYTYPE_SPI_FINDER = new SimpleServiceLoader<ActivityType>(ActivityType.class, Maturity.Any);
private final SimpleServiceLoader<DriverAdapter> DRIVERADAPTER_SPI_FINDER = new SimpleServiceLoader<>(DriverAdapter.class, Maturity.Any);
private final Set<URL> jarUrls = new HashSet<>();
public ActivityTypeLoader setMaturity(Maturity maturity) {
ACTIVITYTYPE_SPI_FINDER.setMaturity(maturity);
return this;
}
public ActivityTypeLoader() {
List<String> libpaths = NBEnvironment.INSTANCE.interpolateEach(":", "$" + NBEnvironment.NBLIBS);
Set<URL> urlsToAdd = new HashSet<>();
for (String libpaths_entry : libpaths) {
Path libpath = Path.of(libpaths_entry);
if (Files.isDirectory(libpath)) {
urlsToAdd = addLibDir(urlsToAdd, libpath);
} else if (Files.isRegularFile(libpath) && libpath.toString().toLowerCase().endsWith(".zip")) {
urlsToAdd = addZipDir(urlsToAdd, libpath);
} else if (Files.isRegularFile(libpath) && libpath.toString().toLowerCase().endsWith(".jar")) {
urlsToAdd = addJarFile(urlsToAdd, libpath);
}
}
extendClassLoader(urlsToAdd);
}
private synchronized void extendClassLoader(String... paths) {
Set<URL> urls = new HashSet<>();
for (String path : paths) {
URL url = null;
try {
url = new URL(path);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
urls.add(url);
}
extendClassLoader(urls);
}
private synchronized void extendClassLoader(Set<URL> urls) {
Set<URL> newUrls = new HashSet<>();
if (!jarUrls.containsAll(urls)) {
for (URL url : urls) {
if (!jarUrls.contains(url)) {
newUrls.add(url);
jarUrls.add(url);
}
}
URL[] newUrlAry = newUrls.toArray(new URL[]{});
URLClassLoader ucl = URLClassLoader.newInstance(newUrlAry, Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(ucl);
logger.debug("Extended class loader layering with " + newUrls);
} else {
logger.debug("All URLs specified were already in a class loader.");
}
}
private Set<URL> addJarFile(Set<URL> urls, Path libpath) {
try {
urls.add(libpath.toUri().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return urls;
}
private Set<URL> addZipDir(Set<URL> urlsToAdd, Path libpath) {
return urlsToAdd;
}
private Set<URL> addLibDir(Set<URL> urlsToAdd, Path libpath) {
Set<URL> urls = NBIO.local()
.prefix(libpath.toString())
.extension(".jar")
.list().stream().map(Content::getURL)
.collect(Collectors.toSet());
urlsToAdd.addAll(urls);
return urlsToAdd;
}
public Optional<ActivityType> load(ActivityDef activityDef) {
final String driverName = activityDef.getParams()
.getOptionalString("driver", "type")
.orElseThrow(() -> new BasicError("The parameter 'driver=' is required."));
activityDef.getParams()
.getOptionalString("jar")
.map(jar -> {
Set<URL> urls = NBIO.local().search(jar)
.list()
.stream().map(Content::getURL)
.collect(Collectors.toSet());
return urls;
})
.ifPresent(this::extendClassLoader);
return this.getDriverAdapter(driverName,activityDef)
.or(() -> ACTIVITYTYPE_SPI_FINDER.getOptionally(driverName));
}
private Optional<ActivityType> getDriverAdapter(String activityTypeName, ActivityDef activityDef) {
Optional<DriverAdapter> oda = DRIVERADAPTER_SPI_FINDER.getOptionally(activityTypeName);
if (oda.isPresent()) {
DriverAdapter<?, ?> driverAdapter = oda.get();
// activityDef.getParams().remove("driver");
// if (driverAdapter instanceof NBConfigurable) {
// NBConfigModel cfgModel = ((NBConfigurable) driverAdapter).getConfigModel();
// Optional<String> op_yaml_loc = activityDef.getParams().getOptionalString("yaml", "workload");
// if (op_yaml_loc.isPresent()) {
// Map<String,Object> disposable = new LinkedHashMap<>(activityDef.getParams());
// StmtsDocList workload = StatementsLoader.loadPath(logger, op_yaml_loc.get(), disposable, "activities");
// cfgModel=cfgModel.add(workload.getConfigModel());
// }
// NBConfiguration cfg = cfgModel.apply(activityDef.getParams());
// ((NBConfigurable) driverAdapter).applyConfig(cfg);
// }
ActivityType activityType = new StandardActivityType<>(driverAdapter, activityDef);
return Optional.of(activityType);
} else {
return Optional.empty();
}
}
public Set<String> getAllSelectors() {
Map<String, Maturity> allSelectors = ACTIVITYTYPE_SPI_FINDER.getAllSelectors();
Map<String, Maturity> addAdapters = DRIVERADAPTER_SPI_FINDER.getAllSelectors();
Set<String> all = new LinkedHashSet<>();
all.addAll(allSelectors.keySet());
all.addAll(addAdapters.keySet());
return all;
}
}