@Test public void server_registration_should_remove_the_backend_from_the_backends_where_it_was_previously_registered_and_preserve_its_original_user_provided_context() { EntryPointKey key = new EntryPointKeyDefaultImpl("some_key"); Map<String, String> serverContext = new HashMap<>(); serverContext.put("key1", "value1"); serverContext.put("key2", "value2"); Map<String, String> serverUserContext = new HashMap<>(); serverUserContext.put("key1", "value3"); serverUserContext.put("key2", "value4"); EntryPointBackendServer server = new EntryPointBackendServer("ijklm", "10.98.71.1", "9090", serverContext, serverUserContext); Map<String, String> backendContext = new HashMap<>(); backendContext.put("key1", "value1"); backendContext.put("key2", "value2"); EntryPointBackend backend = new EntryPointBackend("BACKEND", newHashSet(server), backendContext); EntryPoint currentConfig = EntryPoint .onHaproxy("haproxy", 0) .withUser("hapuser") .withVersion("hapversion") .definesFrontends(ImmutableSet.of()) .definesBackends(ImmutableSet.of(backend)) .withGlobalContext(ImmutableMap.of()) .build(); IncomingEntryPointBackendServer newServerInEvent = new IncomingEntryPointBackendServer("ijklm", "10.98.71.2", "9092", new HashMap<>()); RegisterServerEvent event = new RegisterServerEvent(CorrelationId.newCorrelationId(), key, "NEWBACKEND", ImmutableSet.of(newServerInEvent)); EntryPointBackendServer expectedServer = new EntryPointBackendServer("ijklm", "10.98.71.2", "9092", new HashMap<>(), serverUserContext); EntryPointBackend expectedBackend1 = new EntryPointBackend("BACKEND", newHashSet(), backendContext); EntryPointBackend expectedBackend2 = new EntryPointBackend("NEWBACKEND", newHashSet(expectedServer), new HashMap<>()); EntryPoint expectedConfig = EntryPoint .onHaproxy("haproxy", 0) .withUser("hapuser") .withVersion("hapversion") .definesFrontends(ImmutableSet.of()) .definesBackends(ImmutableSet.of(expectedBackend1, expectedBackend2)) .withGlobalContext(ImmutableMap.of()) .build(); when(stateManager.lock(key)).thenReturn(true); when(stateManager.getCommittingConfiguration(key)).thenReturn(Optional.empty()); when(stateManager.getPendingConfiguration(key)).thenReturn(Optional.empty()); when(stateManager.getCurrentConfiguration(key)).thenReturn(Optional.of(currentConfig)); when(stateManager.prepare(key, expectedConfig)).thenReturn(Optional.of(expectedConfig)); handler.handle(event); verify(stateManager).prepare(eq(key), eq(expectedConfig)); }
public void handle(RegisterServerEvent event) { LOGGER.info("handles {}", event); EntryPointKey key = event.getKey(); try { if (this.stateManager.lock(key)) { Optional<EntryPoint> existingConfiguration = Optional.ofNullable( stateManager.getPendingConfiguration(key) .orElseGet(() -> stateManager.getCommittingConfiguration(key) .orElseGet(() -> stateManager.getCurrentConfiguration(key) .orElseGet(() -> { LOGGER.warn("can't find an entrypoint for key {} from register server event {}", key, event); return null; }))) ); existingConfiguration.map(c -> c.registerServers(event.getBackend(), event.getServers())).ifPresent(c -> { Optional<EntryPoint> preparedConfiguration = stateManager.prepare(key, c); if (preparedConfiguration.isPresent()) { LOGGER.info("new servers registered for EntryPoint {}", event.getKey().getID()); if (LOGGER.isDebugEnabled()) { for (IncomingEntryPointBackendServer server : event.getServers()) { LOGGER.debug("- registered server {}", server); } } } else { LOGGER.warn("can't prepare configuration on key {}", key); } }); } } catch (Exception e) { LOGGER.error("Following error occurred while registering server {} for entry point {}", event, key, e); } finally { this.stateManager.release(key); } }
EntryPointEventHandler { public void handle(RegisterServerEvent event) { LOGGER.info("handles {}", event); EntryPointKey key = event.getKey(); try { if (this.stateManager.lock(key)) { Optional<EntryPoint> existingConfiguration = Optional.ofNullable( stateManager.getPendingConfiguration(key) .orElseGet(() -> stateManager.getCommittingConfiguration(key) .orElseGet(() -> stateManager.getCurrentConfiguration(key) .orElseGet(() -> { LOGGER.warn("can't find an entrypoint for key {} from register server event {}", key, event); return null; }))) ); existingConfiguration.map(c -> c.registerServers(event.getBackend(), event.getServers())).ifPresent(c -> { Optional<EntryPoint> preparedConfiguration = stateManager.prepare(key, c); if (preparedConfiguration.isPresent()) { LOGGER.info("new servers registered for EntryPoint {}", event.getKey().getID()); if (LOGGER.isDebugEnabled()) { for (IncomingEntryPointBackendServer server : event.getServers()) { LOGGER.debug("- registered server {}", server); } } } else { LOGGER.warn("can't prepare configuration on key {}", key); } }); } } catch (Exception e) { LOGGER.error("Following error occurred while registering server {} for entry point {}", event, key, e); } finally { this.stateManager.release(key); } } }
EntryPointEventHandler { public void handle(RegisterServerEvent event) { LOGGER.info("handles {}", event); EntryPointKey key = event.getKey(); try { if (this.stateManager.lock(key)) { Optional<EntryPoint> existingConfiguration = Optional.ofNullable( stateManager.getPendingConfiguration(key) .orElseGet(() -> stateManager.getCommittingConfiguration(key) .orElseGet(() -> stateManager.getCurrentConfiguration(key) .orElseGet(() -> { LOGGER.warn("can't find an entrypoint for key {} from register server event {}", key, event); return null; }))) ); existingConfiguration.map(c -> c.registerServers(event.getBackend(), event.getServers())).ifPresent(c -> { Optional<EntryPoint> preparedConfiguration = stateManager.prepare(key, c); if (preparedConfiguration.isPresent()) { LOGGER.info("new servers registered for EntryPoint {}", event.getKey().getID()); if (LOGGER.isDebugEnabled()) { for (IncomingEntryPointBackendServer server : event.getServers()) { LOGGER.debug("- registered server {}", server); } } } else { LOGGER.warn("can't prepare configuration on key {}", key); } }); } } catch (Exception e) { LOGGER.error("Following error occurred while registering server {} for entry point {}", event, key, e); } finally { this.stateManager.release(key); } } EntryPointEventHandler(EntryPointStateManager stateManager, PortRepository portRepository,
                                  HaproxyRepository haproxyRepository, TemplateLocator templateLocator,
                                  TemplateGenerator templateGenerator,
                                  Subscriber<CommitRequestedEvent> commitRequestedSubscriber); }
EntryPointEventHandler { public void handle(RegisterServerEvent event) { LOGGER.info("handles {}", event); EntryPointKey key = event.getKey(); try { if (this.stateManager.lock(key)) { Optional<EntryPoint> existingConfiguration = Optional.ofNullable( stateManager.getPendingConfiguration(key) .orElseGet(() -> stateManager.getCommittingConfiguration(key) .orElseGet(() -> stateManager.getCurrentConfiguration(key) .orElseGet(() -> { LOGGER.warn("can't find an entrypoint for key {} from register server event {}", key, event); return null; }))) ); existingConfiguration.map(c -> c.registerServers(event.getBackend(), event.getServers())).ifPresent(c -> { Optional<EntryPoint> preparedConfiguration = stateManager.prepare(key, c); if (preparedConfiguration.isPresent()) { LOGGER.info("new servers registered for EntryPoint {}", event.getKey().getID()); if (LOGGER.isDebugEnabled()) { for (IncomingEntryPointBackendServer server : event.getServers()) { LOGGER.debug("- registered server {}", server); } } } else { LOGGER.warn("can't prepare configuration on key {}", key); } }); } } catch (Exception e) { LOGGER.error("Following error occurred while registering server {} for entry point {}", event, key, e); } finally { this.stateManager.release(key); } } EntryPointEventHandler(EntryPointStateManager stateManager, PortRepository portRepository,
                                  HaproxyRepository haproxyRepository, TemplateLocator templateLocator,
                                  TemplateGenerator templateGenerator,
                                  Subscriber<CommitRequestedEvent> commitRequestedSubscriber); void handle(RegisterServerEvent event); void handle(TryCommitCurrentConfigurationEvent event); void handle(TryCommitPendingConfigurationEvent event); void handle(CommitCompletedEvent event); void handle(CommitFailedEvent event); }
EntryPointEventHandler { public void handle(RegisterServerEvent event) { LOGGER.info("handles {}", event); EntryPointKey key = event.getKey(); try { if (this.stateManager.lock(key)) { Optional<EntryPoint> existingConfiguration = Optional.ofNullable( stateManager.getPendingConfiguration(key) .orElseGet(() -> stateManager.getCommittingConfiguration(key) .orElseGet(() -> stateManager.getCurrentConfiguration(key) .orElseGet(() -> { LOGGER.warn("can't find an entrypoint for key {} from register server event {}", key, event); return null; }))) ); existingConfiguration.map(c -> c.registerServers(event.getBackend(), event.getServers())).ifPresent(c -> { Optional<EntryPoint> preparedConfiguration = stateManager.prepare(key, c); if (preparedConfiguration.isPresent()) { LOGGER.info("new servers registered for EntryPoint {}", event.getKey().getID()); if (LOGGER.isDebugEnabled()) { for (IncomingEntryPointBackendServer server : event.getServers()) { LOGGER.debug("- registered server {}", server); } } } else { LOGGER.warn("can't prepare configuration on key {}", key); } }); } } catch (Exception e) { LOGGER.error("Following error occurred while registering server {} for entry point {}", event, key, e); } finally { this.stateManager.release(key); } } EntryPointEventHandler(EntryPointStateManager stateManager, PortRepository portRepository,
                                  HaproxyRepository haproxyRepository, TemplateLocator templateLocator,
                                  TemplateGenerator templateGenerator,
                                  Subscriber<CommitRequestedEvent> commitRequestedSubscriber); void handle(RegisterServerEvent event); void handle(TryCommitCurrentConfigurationEvent event); void handle(TryCommitPendingConfigurationEvent event); void handle(CommitCompletedEvent event); void handle(CommitFailedEvent event);  }