|
55 | 55 | import java.util.concurrent.ConcurrentHashMap; |
56 | 56 | import java.util.concurrent.ExecutionException; |
57 | 57 | import java.util.concurrent.ExecutorService; |
| 58 | +import java.util.concurrent.Executors; |
58 | 59 | import java.util.concurrent.Future; |
59 | 60 | import java.util.concurrent.atomic.AtomicBoolean; |
60 | 61 | import java.util.function.Function; |
@@ -83,7 +84,7 @@ public class ProtocolAdapterManager { |
83 | 84 | private final @NotNull NorthboundConsumerFactory northboundConsumerFactory; |
84 | 85 | private final @NotNull TagManager tagManager; |
85 | 86 | private final @NotNull ProtocolAdapterExtractor protocolAdapterConfig; |
86 | | - private final @NotNull AtomicBoolean refreshing = new AtomicBoolean(false); |
| 87 | + private final @NotNull ExecutorService executorService; |
87 | 88 |
|
88 | 89 | @Inject |
89 | 90 | public ProtocolAdapterManager( |
@@ -113,6 +114,8 @@ public ProtocolAdapterManager( |
113 | 114 | this.northboundConsumerFactory = northboundConsumerFactory; |
114 | 115 | this.tagManager = tagManager; |
115 | 116 | this.protocolAdapterConfig = protocolAdapterConfig; |
| 117 | + this.executorService = Executors.newSingleThreadExecutor(); |
| 118 | + Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown)); |
116 | 119 | protocolAdapterWritingService.addWritingChangedCallback(() -> protocolAdapterFactoryManager.writingEnabledChanged( |
117 | 120 | protocolAdapterWritingService.writingEnabled())); |
118 | 121 | } |
@@ -216,90 +219,86 @@ public void start() { |
216 | 219 | } |
217 | 220 |
|
218 | 221 | public void refresh(final @NotNull List<ProtocolAdapterEntity> configs) { |
219 | | - if(refreshing.compareAndSet(false, true)) { |
220 | | - try { |
221 | | - log.info("Refreshing adapters"); |
| 222 | + executorService.submit(() -> { |
| 223 | + log.info("Refreshing adapters"); |
222 | 224 |
|
223 | | - final Map<String, ProtocolAdapterConfig> protocolAdapterConfigs = configs |
224 | | - .stream() |
225 | | - .map(configConverter::fromEntity) |
226 | | - .collect(Collectors.toMap(ProtocolAdapterConfig::getAdapterId, Function.identity())); |
| 225 | + final Map<String, ProtocolAdapterConfig> protocolAdapterConfigs = configs |
| 226 | + .stream() |
| 227 | + .map(configConverter::fromEntity) |
| 228 | + .collect(Collectors.toMap(ProtocolAdapterConfig::getAdapterId, Function.identity())); |
227 | 229 |
|
228 | | - final List<String> loadListOfAdapterNames = new ArrayList<>(protocolAdapterConfigs.keySet()); |
| 230 | + final List<String> loadListOfAdapterNames = new ArrayList<>(protocolAdapterConfigs.keySet()); |
229 | 231 |
|
230 | | - final List<String> adaptersToBeDeleted = new ArrayList<>(protocolAdapters.keySet()); |
231 | | - adaptersToBeDeleted.removeAll(loadListOfAdapterNames); |
| 232 | + final List<String> adaptersToBeDeleted = new ArrayList<>(protocolAdapters.keySet()); |
| 233 | + adaptersToBeDeleted.removeAll(loadListOfAdapterNames); |
232 | 234 |
|
233 | | - final List<String> adaptersToBeCreated = new ArrayList<>(loadListOfAdapterNames); |
234 | | - adaptersToBeCreated.removeAll(protocolAdapters.keySet()); |
| 235 | + final List<String> adaptersToBeCreated = new ArrayList<>(loadListOfAdapterNames); |
| 236 | + adaptersToBeCreated.removeAll(protocolAdapters.keySet()); |
235 | 237 |
|
236 | | - final List<String> adaptersToBeUpdated = new ArrayList<>(protocolAdapters.keySet()); |
237 | | - adaptersToBeUpdated.removeAll(adaptersToBeCreated); |
238 | | - adaptersToBeUpdated.removeAll(adaptersToBeDeleted); |
| 238 | + final List<String> adaptersToBeUpdated = new ArrayList<>(protocolAdapters.keySet()); |
| 239 | + adaptersToBeUpdated.removeAll(adaptersToBeCreated); |
| 240 | + adaptersToBeUpdated.removeAll(adaptersToBeDeleted); |
239 | 241 |
|
240 | | - final List<String> failedAdapters = new ArrayList<>(); |
| 242 | + final List<String> failedAdapters = new ArrayList<>(); |
241 | 243 |
|
242 | | - adaptersToBeDeleted.forEach(name -> { |
243 | | - try { |
244 | | - log.debug("Deleting adapter '{}'", name); |
245 | | - stop(name, true).whenComplete((ignored, t) -> deleteAdapterInternal(name)).get(); |
246 | | - } catch (final InterruptedException | ExecutionException e) { |
247 | | - failedAdapters.add(name); |
248 | | - log.error("Failed deleting adapter {}", name, e); |
249 | | - } |
250 | | - }); |
| 244 | + adaptersToBeDeleted.forEach(name -> { |
| 245 | + try { |
| 246 | + log.debug("Deleting adapter '{}'", name); |
| 247 | + stop(name, true).whenComplete((ignored, t) -> { |
| 248 | + deleteAdapterInternal(name); |
| 249 | + }).get(); |
| 250 | + } catch (final InterruptedException | ExecutionException e) { |
| 251 | + failedAdapters.add(name); |
| 252 | + log.error("Failed deleting adapter {}", name, e); |
| 253 | + } |
| 254 | + }); |
251 | 255 |
|
252 | | - adaptersToBeCreated.forEach(name -> { |
253 | | - try { |
254 | | - log.debug("Creating adapter '{}'", name); |
255 | | - start(createAdapterInternal(protocolAdapterConfigs.get(name), versionProvider.getVersion())).get(); |
256 | | - } catch (final InterruptedException | ExecutionException e) { |
257 | | - failedAdapters.add(name); |
258 | | - log.error("Failed adding adapter {}", name, e); |
259 | | - } |
260 | | - }); |
| 256 | + adaptersToBeCreated.forEach(name -> { |
| 257 | + try { |
| 258 | + log.debug("Creating adapter '{}'", name); |
| 259 | + start(createAdapterInternal(protocolAdapterConfigs.get(name), versionProvider.getVersion())).get(); |
| 260 | + } catch (final InterruptedException | ExecutionException e) { |
| 261 | + failedAdapters.add(name); |
| 262 | + log.error("Failed adding adapter {}", name, e); |
| 263 | + } |
| 264 | + }); |
261 | 265 |
|
262 | | - adaptersToBeUpdated.forEach(name -> { |
263 | | - try { |
264 | | - if(!protocolAdapterConfigs.get(name).equals(knownConfigs.get(name))) { |
265 | | - log.debug("Updating adapter '{}'", name); |
266 | | - stop(name, true) |
267 | | - .thenApply(v -> { |
268 | | - deleteAdapterInternal(name); |
269 | | - return null; |
270 | | - }) |
271 | | - .thenCompose(ignored -> |
272 | | - start( |
273 | | - createAdapterInternal(protocolAdapterConfigs.get(name), |
274 | | - versionProvider.getVersion()))) |
275 | | - .get(); |
276 | | - } else { |
277 | | - log.debug("Not-updating adapter '{}' since the config is unchanged", name); |
278 | | - } |
279 | | - } catch (final InterruptedException | ExecutionException e) { |
280 | | - failedAdapters.add(name); |
281 | | - log.error("Failed updating adapter {}", name, e); |
| 266 | + adaptersToBeUpdated.forEach(name -> { |
| 267 | + try { |
| 268 | + if(!protocolAdapterConfigs.get(name).equals(knownConfigs.get(name))) { |
| 269 | + log.debug("Updating adapter '{}'", name); |
| 270 | + stop(name, true) |
| 271 | + .thenApply(v -> { |
| 272 | + deleteAdapterInternal(name); |
| 273 | + return null; |
| 274 | + }) |
| 275 | + .thenCompose(ignored -> |
| 276 | + start( |
| 277 | + createAdapterInternal(protocolAdapterConfigs.get(name), |
| 278 | + versionProvider.getVersion()))) |
| 279 | + .get(); |
| 280 | + } else { |
| 281 | + log.debug("Not-updating adapter '{}' since the config is unchanged", name); |
282 | 282 | } |
| 283 | + } catch (final InterruptedException | ExecutionException e) { |
| 284 | + failedAdapters.add(name); |
| 285 | + log.error("Failed updating adapter {}", name, e); |
| 286 | + } |
283 | 287 |
|
284 | | - }); |
| 288 | + }); |
285 | 289 |
|
286 | | - if (failedAdapters.isEmpty()) { |
287 | | - eventService.configurationEvent() |
288 | | - .withSeverity(Event.SEVERITY.INFO) |
289 | | - .withMessage("Configuration has been successfully updated") |
290 | | - .fire(); |
291 | | - } else { |
292 | | - eventService.configurationEvent() |
293 | | - .withSeverity(Event.SEVERITY.CRITICAL) |
294 | | - .withMessage("Reloading of configuration failed") |
295 | | - .fire(); |
296 | | - } |
297 | | - } finally { |
298 | | - refreshing.set(false); |
| 290 | + if (failedAdapters.isEmpty()) { |
| 291 | + eventService.configurationEvent() |
| 292 | + .withSeverity(Event.SEVERITY.INFO) |
| 293 | + .withMessage("Configuration has been successfully updated") |
| 294 | + .fire(); |
| 295 | + } else { |
| 296 | + eventService.configurationEvent() |
| 297 | + .withSeverity(Event.SEVERITY.CRITICAL) |
| 298 | + .withMessage("Reloading of configuration failed") |
| 299 | + .fire(); |
299 | 300 | } |
300 | | - } else { |
301 | | - log.warn("Refresh already in progress, skipping this refresh call"); |
302 | | - } |
| 301 | + }); |
303 | 302 | } |
304 | 303 |
|
305 | 304 | //legacy handling, hardcoded here, to not add legacy stuff into the adapter-sdk |
|
0 commit comments