@Test public void server_registration_should_do_nothing_if_there_is_no_configuration_at_all() { EntryPointKey key = new EntryPointKeyDefaultImpl("some_key"); IncomingEntryPointBackendServer server = new IncomingEntryPointBackendServer("ijklm", "10.98.71.1", "9090", new HashMap<>()); RegisterServerEvent event = new RegisterServerEvent(CorrelationId.newCorrelationId(), key, "BACKEND", ImmutableSet.of(server)); 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.empty()); handler.handle(event); verify(stateManager, never()).prepare(any(), any()); }
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);  }