|
16 | 16 |
|
17 | 17 | package com.hivemq.protocols.fsm; |
18 | 18 |
|
| 19 | +import com.codahale.metrics.MetricRegistry; |
| 20 | +import com.google.common.collect.Sets; |
| 21 | +import com.hivemq.adapter.sdk.api.events.EventService; |
| 22 | +import com.hivemq.adapter.sdk.api.events.model.Event; |
| 23 | +import com.hivemq.configuration.entity.adapter.ProtocolAdapterEntity; |
| 24 | +import com.hivemq.configuration.reader.ProtocolAdapterExtractor; |
| 25 | +import com.hivemq.edge.HiveMQEdgeRemoteService; |
| 26 | +import com.hivemq.edge.VersionProvider; |
| 27 | +import com.hivemq.edge.modules.adapters.data.TagManager; |
| 28 | +import com.hivemq.edge.modules.adapters.impl.ModuleServicesImpl; |
| 29 | +import com.hivemq.edge.modules.api.adapters.ProtocolAdapterPollingService; |
| 30 | +import com.hivemq.protocols.InternalProtocolAdapterWritingService; |
| 31 | +import com.hivemq.protocols.ProtocolAdapterConfig; |
| 32 | +import com.hivemq.protocols.ProtocolAdapterConfigConverter; |
| 33 | +import com.hivemq.protocols.ProtocolAdapterFactoryManager; |
| 34 | +import com.hivemq.protocols.ProtocolAdapterMetrics; |
| 35 | +import com.hivemq.protocols.northbound.NorthboundConsumerFactory; |
| 36 | +import org.jetbrains.annotations.NotNull; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.Set; |
| 45 | +import java.util.concurrent.ConcurrentHashMap; |
| 46 | +import java.util.concurrent.ExecutionException; |
| 47 | +import java.util.concurrent.ExecutorService; |
| 48 | +import java.util.concurrent.Executors; |
| 49 | +import java.util.function.Function; |
| 50 | +import java.util.stream.Collectors; |
| 51 | + |
| 52 | + |
19 | 53 | public class ProtocolAdapterOperator { |
| 54 | + private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolAdapterOperator.class); |
| 55 | + |
| 56 | + private final @NotNull Map<String, ProtocolAdapterInstance> protocolAdapterMap; |
| 57 | + private final @NotNull MetricRegistry metricRegistry; |
| 58 | + private final @NotNull ModuleServicesImpl moduleServices; |
| 59 | + private final @NotNull HiveMQEdgeRemoteService remoteService; |
| 60 | + private final @NotNull EventService eventService; |
| 61 | + private final @NotNull ProtocolAdapterConfigConverter configConverter; |
| 62 | + private final @NotNull VersionProvider versionProvider; |
| 63 | + private final @NotNull ProtocolAdapterPollingService protocolAdapterPollingService; |
| 64 | + private final @NotNull ProtocolAdapterMetrics protocolAdapterMetrics; |
| 65 | + private final @NotNull InternalProtocolAdapterWritingService protocolAdapterWritingService; |
| 66 | + private final @NotNull ProtocolAdapterFactoryManager protocolAdapterFactoryManager; |
| 67 | + private final @NotNull NorthboundConsumerFactory northboundConsumerFactory; |
| 68 | + private final @NotNull TagManager tagManager; |
| 69 | + private final @NotNull ProtocolAdapterExtractor protocolAdapterConfig; |
| 70 | + private final @NotNull ExecutorService executorService; |
| 71 | + private volatile @NotNull ProtocolAdapterOperatorState state; |
| 72 | + |
| 73 | + public ProtocolAdapterOperator( |
| 74 | + final @NotNull MetricRegistry metricRegistry, |
| 75 | + final @NotNull ModuleServicesImpl moduleServices, |
| 76 | + final @NotNull HiveMQEdgeRemoteService remoteService, |
| 77 | + final @NotNull EventService eventService, |
| 78 | + final @NotNull ProtocolAdapterConfigConverter configConverter, |
| 79 | + final @NotNull VersionProvider versionProvider, |
| 80 | + final @NotNull ProtocolAdapterPollingService protocolAdapterPollingService, |
| 81 | + final @NotNull ProtocolAdapterMetrics protocolAdapterMetrics, |
| 82 | + final @NotNull InternalProtocolAdapterWritingService protocolAdapterWritingService, |
| 83 | + final @NotNull ProtocolAdapterFactoryManager protocolAdapterFactoryManager, |
| 84 | + final @NotNull NorthboundConsumerFactory northboundConsumerFactory, |
| 85 | + final @NotNull TagManager tagManager, |
| 86 | + final @NotNull ProtocolAdapterExtractor protocolAdapterConfig) { |
| 87 | + this.protocolAdapterMap = new ConcurrentHashMap<>(); |
| 88 | + this.metricRegistry = metricRegistry; |
| 89 | + this.moduleServices = moduleServices; |
| 90 | + this.remoteService = remoteService; |
| 91 | + this.eventService = eventService; |
| 92 | + this.configConverter = configConverter; |
| 93 | + this.versionProvider = versionProvider; |
| 94 | + this.protocolAdapterPollingService = protocolAdapterPollingService; |
| 95 | + this.protocolAdapterMetrics = protocolAdapterMetrics; |
| 96 | + this.protocolAdapterWritingService = protocolAdapterWritingService; |
| 97 | + this.protocolAdapterFactoryManager = protocolAdapterFactoryManager; |
| 98 | + this.northboundConsumerFactory = northboundConsumerFactory; |
| 99 | + this.tagManager = tagManager; |
| 100 | + this.protocolAdapterConfig = protocolAdapterConfig; |
| 101 | + this.executorService = Executors.newSingleThreadExecutor(); |
| 102 | + this.state = ProtocolAdapterOperatorState.Idle; |
| 103 | + Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown)); |
| 104 | + protocolAdapterWritingService.addWritingChangedCallback(() -> protocolAdapterFactoryManager.writingEnabledChanged( |
| 105 | + protocolAdapterWritingService.writingEnabled())); |
| 106 | + } |
| 107 | + |
| 108 | + public @NotNull ProtocolAdapterOperatorState getState() { |
| 109 | + return state; |
| 110 | + } |
| 111 | + |
| 112 | + public void start() { |
| 113 | + if (LOGGER.isDebugEnabled()) { |
| 114 | + LOGGER.debug("Starting adapters"); |
| 115 | + } |
| 116 | + protocolAdapterConfig.registerConsumer(this::refresh); |
| 117 | + } |
| 118 | + |
| 119 | + public void refresh(final @NotNull List<ProtocolAdapterEntity> configs) { |
| 120 | + executorService.submit(() -> { |
| 121 | + state = ProtocolAdapterOperatorState.Running; |
| 122 | + LOGGER.info("Refreshing adapters"); |
| 123 | + |
| 124 | + final Map<String, ProtocolAdapterConfig> protocolAdapterConfigs = configs.stream() |
| 125 | + .map(configConverter::fromEntity) |
| 126 | + .collect(Collectors.toMap(ProtocolAdapterConfig::getAdapterId, Function.identity())); |
| 127 | + |
| 128 | + final Set<String> oldProtocolAdapterIdSet = new HashSet<>(protocolAdapterMap.keySet()); |
| 129 | + final Set<String> newProtocolAdapterIdSet = new HashSet<>(protocolAdapterConfigs.keySet()); |
| 130 | + |
| 131 | + final Set<String> toBeDeletedProtocolAdapterIdSet = |
| 132 | + new HashSet<>(Sets.difference(oldProtocolAdapterIdSet, newProtocolAdapterIdSet)); |
| 133 | + final Set<String> toBeCreatedProtocolAdapterIdSet = |
| 134 | + new HashSet<>(Sets.difference(newProtocolAdapterIdSet, oldProtocolAdapterIdSet)); |
| 135 | + final Set<String> toBeUpdatedProtocolAdapterIdSet = |
| 136 | + new HashSet<>(Sets.intersection(newProtocolAdapterIdSet, oldProtocolAdapterIdSet)); |
| 137 | + |
| 138 | + final List<String> failedAdapters = new ArrayList<>(); |
| 139 | + |
| 140 | + toBeDeletedProtocolAdapterIdSet.forEach(adapterId -> { |
| 141 | +// try { |
| 142 | +// if (LOGGER.isDebugEnabled()) { |
| 143 | +// LOGGER.debug("Deleting adapter '{}'", adapterId); |
| 144 | +// } |
| 145 | +// stopAsync(adapterId, true).whenComplete((ignored, t) -> deleteAdapterInternal(adapterId)).get(); |
| 146 | +// } catch (final InterruptedException e) { |
| 147 | +// Thread.currentThread().interrupt(); |
| 148 | +// failedAdapters.add(adapterId); |
| 149 | +// LOGGER.error("Interrupted while deleting adapter {}", adapterId, e); |
| 150 | +// } catch (final ExecutionException e) { |
| 151 | +// failedAdapters.add(adapterId); |
| 152 | +// LOGGER.error("Failed deleting adapter {}", adapterId, e); |
| 153 | +// } |
| 154 | + }); |
| 155 | + |
| 156 | + toBeCreatedProtocolAdapterIdSet.forEach(name -> { |
| 157 | +// try { |
| 158 | +// if (LOGGER.isDebugEnabled()) { |
| 159 | +// LOGGER.debug("Creating adapter '{}'", name); |
| 160 | +// } |
| 161 | +// startAsync(createAdapterInternal(protocolAdapterConfigs.get(name), |
| 162 | +// versionProvider.getVersion())).get(); |
| 163 | +// } catch (final InterruptedException e) { |
| 164 | +// Thread.currentThread().interrupt(); |
| 165 | +// failedAdapters.add(name); |
| 166 | +// LOGGER.error("Interrupted while adding adapter {}", name, e); |
| 167 | +// } catch (final ExecutionException e) { |
| 168 | +// failedAdapters.add(name); |
| 169 | +// LOGGER.error("Failed adding adapter {}", name, e); |
| 170 | +// } |
| 171 | + }); |
| 172 | + |
| 173 | + toBeUpdatedProtocolAdapterIdSet.forEach(name -> { |
| 174 | +// try { |
| 175 | +// final var wrapper = protocolAdapters.get(name); |
| 176 | +// if (wrapper == null) { |
| 177 | +// LOGGER.error( |
| 178 | +// "Existing adapters were modified while a refresh was ongoing, adapter with name '{}' was deleted and could not be updated", |
| 179 | +// name); |
| 180 | +// } |
| 181 | +// if (wrapper != null && !protocolAdapterConfigs.get(name).equals(wrapper.getConfig())) { |
| 182 | +// if (LOGGER.isDebugEnabled()) { |
| 183 | +// LOGGER.debug("Updating adapter '{}'", name); |
| 184 | +// } |
| 185 | +// stopAsync(name, true).thenApply(v -> { |
| 186 | +// deleteAdapterInternal(name); |
| 187 | +// return null; |
| 188 | +// }) |
| 189 | +// .thenCompose(ignored -> startAsync(createAdapterInternal(protocolAdapterConfigs.get(name), |
| 190 | +// versionProvider.getVersion()))) |
| 191 | +// .get(); |
| 192 | +// } else { |
| 193 | +// if (LOGGER.isDebugEnabled()) { |
| 194 | +// LOGGER.debug("Not-updating adapter '{}' since the config is unchanged", name); |
| 195 | +// } |
| 196 | +// } |
| 197 | +// } catch (final InterruptedException e) { |
| 198 | +// Thread.currentThread().interrupt(); |
| 199 | +// failedAdapters.add(name); |
| 200 | +// LOGGER.error("Interrupted while updating adapter {}", name, e); |
| 201 | +// } catch (final ExecutionException e) { |
| 202 | +// failedAdapters.add(name); |
| 203 | +// LOGGER.error("Failed updating adapter {}", name, e); |
| 204 | +// } |
| 205 | + }); |
| 206 | + |
| 207 | + if (failedAdapters.isEmpty()) { |
| 208 | + eventService.configurationEvent() |
| 209 | + .withSeverity(Event.SEVERITY.INFO) |
| 210 | + .withMessage("Configuration has been successfully updated") |
| 211 | + .fire(); |
| 212 | + } else { |
| 213 | + eventService.configurationEvent() |
| 214 | + .withSeverity(Event.SEVERITY.CRITICAL) |
| 215 | + .withMessage("Reloading of configuration failed") |
| 216 | + .fire(); |
| 217 | + } |
| 218 | + state = ProtocolAdapterOperatorState.Idle; |
| 219 | + }); |
| 220 | + } |
20 | 221 | } |
0 commit comments