private CompletableFuture<ContainerHandle> startContainerInternal(int containerId) {
ContainerWithHandle newContainer = new ContainerWithHandle(this.factory.createStreamSegmentContainer(containerId),
new SegmentContainerHandle(containerId));
ContainerWithHandle existingContainer = this.containers.putIfAbsent(containerId, newContainer);
if (existingContainer != null) {
newContainer.container.close();
throw new IllegalArgumentException(String.format("Container %d is already registered.", containerId));
}
log.info("Registered SegmentContainer {}.", containerId);
Services.onStop(
newContainer.container,
() -> unregisterContainer(newContainer),
ex -> handleContainerFailure(newContainer, ex),
this.executor);
return Services.startAsync(newContainer.container, this.executor)
.thenApply(v -> newContainer.handle);
}