Source:
<code>private void waitMachines( Map<String, CompletableFuture<Void>> machinesFutures, List<CompletableFuture<?>> toCancelFutures, CompletableFuture<Void> failure) throws InfrastructureException { try { LOG.debug( "Waiting to start machines of workspace '{}'", getContext().getIdentity().getWorkspaceId()); CompletableFuture<?>[] tracedMachineFutures = machinesFutures .entrySet() .stream() .map( e -> <START> e.getValue() <END> .thenRun( () -> { Span span = machineStartupTraces.remove(e.getKey()); if (span != null) { span.finish(); } })) .toArray(CompletableFuture[]::new); final CompletableFuture<Void> allDone = CompletableFuture.allOf(tracedMachineFutures); CompletableFuture.anyOf(allDone, failure) .get(startSynchronizer.getStartTimeoutMillis(), TimeUnit.MILLISECONDS); if (failure.isCompletedExceptionally()) { cancelAll(toCancelFutures); finishAllStartupTraces(); failure.get(); } LOG.debug("Machines of workspace '{}' started", getContext().getIdentity().getWorkspaceId()); } catch (TimeoutException ex) { InfrastructureException ie = new InfrastructureException( "Waiting for Kubernetes environment '" + getContext().getIdentity().getEnvName() + "' of the workspace'" + getContext().getIdentity().getWorkspaceId() + "' reached timeout"); failure.completeExceptionally(ie); cancelAll(toCancelFutures); finishAllStartupTraces(); throw ie; } catch (InterruptedException ex) { RuntimeStartInterruptedException runtimeInterruptedEx = new RuntimeStartInterruptedException(getContext().getIdentity()); failure.completeExceptionally(runtimeInterruptedEx); cancelAll(toCancelFutures); finishAllStartupTraces(); throw runtimeInterruptedEx; } catch (ExecutionException ex) { failure.completeExceptionally(ex.getCause()); cancelAll(toCancelFutures); finishAllStartupTraces(); wrapAndRethrow(ex.getCause()); } }</code><technical_language>code more clean with: private CompletableFuture<Void> toFinishMachineSpanFuture(Map.Entry<String, CompletableFuture<Void>> machineNameToFuture) { ... } machinesFutures .entrySet() .stream() .map(this::toFinishMachineSpanFuture) .toArray(CompletableFuture[]::new);</technical_language>

Target:
private void waitMachines( Map<String, CompletableFuture<Void>> machinesFutures, List<CompletableFuture<?>> toCancelFutures, CompletableFuture<Void> failure) throws InfrastructureException { try { LOG.debug( "Waiting to start machines of workspace '{}'", getContext().getIdentity().getWorkspaceId()); final CompletableFuture<Void> allDone = CompletableFuture.allOf( machinesFutures.values().toArray(new CompletableFuture[machinesFutures.size()])); CompletableFuture.anyOf(allDone, failure) .get(startSynchronizer.getStartTimeoutMillis(), TimeUnit.MILLISECONDS); if (failure.isCompletedExceptionally()) { cancelAll(toCancelFutures); failure.get(); } LOG.debug("Machines of workspace '{}' started", getContext().getIdentity().getWorkspaceId()); } catch (TimeoutException ex) { InfrastructureException ie = new InfrastructureException( "Waiting for Kubernetes environment '" + getContext().getIdentity().getEnvName() + "' of the workspace'" + getContext().getIdentity().getWorkspaceId() + "' reached timeout"); failure.completeExceptionally(ie); cancelAll(toCancelFutures); finishAllStartupTracesAsFailures(ie); throw ie; } catch (InterruptedException ex) { RuntimeStartInterruptedException runtimeInterruptedEx = new RuntimeStartInterruptedException(getContext().getIdentity()); failure.completeExceptionally(runtimeInterruptedEx); cancelAll(toCancelFutures); finishAllStartupTracesAsFailures(ex); throw runtimeInterruptedEx; } catch (ExecutionException ex) { failure.completeExceptionally(ex.getCause()); cancelAll(toCancelFutures); wrapAndRethrow(ex.getCause()); } }