-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from palantir/feature/use-host-networked-ports
Feature/use host networked ports
- Loading branch information
Showing
26 changed files
with
354 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/palantir/docker/compose/connection/waiting/ClusterHealthCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2016 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.palantir.docker.compose.connection.waiting; | ||
|
||
import com.palantir.docker.compose.connection.Cluster; | ||
import com.palantir.docker.compose.connection.Container; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
@FunctionalInterface | ||
public interface ClusterHealthCheck { | ||
static ClusterHealthCheck serviceHealthCheck(List<String> containerNames, HealthCheck<List<Container>> delegate) { | ||
return transformingHealthCheck(cluster -> cluster.containers(containerNames), delegate); | ||
} | ||
|
||
static ClusterHealthCheck serviceHealthCheck(String containerName, HealthCheck<Container> containerCheck) { | ||
return transformingHealthCheck(cluster -> cluster.container(containerName), containerCheck); | ||
} | ||
|
||
static <T> ClusterHealthCheck transformingHealthCheck(Function<Cluster, T> transform, HealthCheck<T> healthCheck) { | ||
return cluster -> { | ||
T target = transform.apply(cluster); | ||
return healthCheck.isHealthy(target); | ||
}; | ||
} | ||
|
||
SuccessOrFailure isClusterHealthy(Cluster cluster); | ||
} |
66 changes: 62 additions & 4 deletions
66
src/main/java/com/palantir/docker/compose/connection/waiting/ClusterWait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,72 @@ | ||
/* | ||
* Copyright 2016 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.docker.compose.connection.waiting; | ||
|
||
import com.jayway.awaitility.Awaitility; | ||
import com.jayway.awaitility.core.ConditionTimeoutException; | ||
import com.palantir.docker.compose.connection.Cluster; | ||
import java.util.Optional; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.joda.time.Duration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ClusterWait { | ||
private static final Logger log = LoggerFactory.getLogger(ClusterWait.class); | ||
private final ClusterHealthCheck clusterHealthCheck; | ||
private final Duration timeout; | ||
|
||
public ClusterWait(ClusterHealthCheck clusterHealthCheck, Duration timeout) { | ||
this.clusterHealthCheck = clusterHealthCheck; | ||
this.timeout = timeout; | ||
} | ||
|
||
public void waitUntilReady(Cluster cluster) { | ||
final AtomicReference<Optional<SuccessOrFailure>> lastSuccessOrFailure = new AtomicReference<>( | ||
Optional.empty()); | ||
|
||
log.debug("Waiting for cluster to be healthy"); | ||
try { | ||
Awaitility.await() | ||
.pollInterval(50, TimeUnit.MILLISECONDS) | ||
.atMost(timeout.getMillis(), TimeUnit.MILLISECONDS) | ||
.until(weHaveSuccess(cluster, lastSuccessOrFailure)); | ||
} catch (ConditionTimeoutException e) { | ||
throw new IllegalStateException(serviceDidNotStartupExceptionMessage(lastSuccessOrFailure)); | ||
} | ||
} | ||
|
||
private Callable<Boolean> weHaveSuccess(Cluster cluster, | ||
AtomicReference<Optional<SuccessOrFailure>> lastSuccessOrFailure) { | ||
return () -> { | ||
SuccessOrFailure successOrFailure = clusterHealthCheck.isClusterHealthy(cluster); | ||
lastSuccessOrFailure.set(Optional.of(successOrFailure)); | ||
return successOrFailure.succeeded(); | ||
}; | ||
} | ||
|
||
@FunctionalInterface | ||
public interface ClusterWait { | ||
private String serviceDidNotStartupExceptionMessage( | ||
AtomicReference<Optional<SuccessOrFailure>> lastSuccessOrFailure) { | ||
String healthcheckFailureMessage = lastSuccessOrFailure.get() | ||
.flatMap(SuccessOrFailure::toOptionalFailureMessage) | ||
.orElse("The healthcheck did not finish before the timeout"); | ||
|
||
void waitUntilReady(Cluster cluster); | ||
return "The cluster failed to pass a startup check: " + healthcheckFailureMessage; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.