All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.singularity.client.SingularityClientProvider Maven / Gradle / Ivy

The newest version!
package com.hubspot.singularity.client;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.hubspot.horizon.HttpClient;
import com.hubspot.horizon.HttpResponse;
import com.hubspot.singularity.SingularityClientCredentials;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;

@Singleton
public class SingularityClientProvider implements Provider {
  private static final String DEFAULT_CONTEXT_PATH = "singularity/api";

  private final HttpClient httpClient;

  private String contextPath = DEFAULT_CONTEXT_PATH;
  private List hosts = Collections.emptyList();
  private Optional credentials = Optional.empty();
  private boolean ssl = false;

  private int retryAttempts = 3;
  private Predicate retryStrategy = HttpResponse::isServerError;

  @Inject
  public SingularityClientProvider(
    @Named(SingularityClientModule.HTTP_CLIENT_NAME) HttpClient httpClient
  ) {
    this.httpClient = httpClient;
  }

  @Inject(optional = true) // optional because we have a default
  public SingularityClientProvider setContextPath(
    @Named(SingularityClientModule.CONTEXT_PATH) String contextPath
  ) {
    this.contextPath = contextPath;
    return this;
  }

  @Inject(optional = true) // optional in case we use fixed hosts
  public SingularityClientProvider setHosts(
    @Named(SingularityClientModule.HOSTS_PROPERTY_NAME) String commaSeparatedHosts
  ) {
    return setHosts(commaSeparatedHosts.split(","));
  }

  @Inject(optional = true) // optional in case we use Curator
  public SingularityClientProvider setCurator(
    @Named(SingularityClientModule.CURATOR_NAME) CuratorFramework curator
  ) {
    return setHosts(getClusterMembers(curator));
  }

  @Inject(optional = true)
  public SingularityClientProvider setHosts(
    @Named(SingularityClientModule.HOSTS_PROPERTY_NAME) List hosts
  ) {
    this.hosts = ImmutableList.copyOf(hosts);
    return this;
  }

  @Inject(optional = true)
  public SingularityClientProvider setCredentials(
    @Named(
      SingularityClientModule.CREDENTIALS_PROPERTY_NAME
    ) SingularityClientCredentials credentials
  ) {
    this.credentials = Optional.of(credentials);
    return this;
  }

  @Inject(optional = true)
  public SingularityClientProvider setRetryAttempts(
    @Named(SingularityClientModule.RETRY_ATTEMPTS) int retryAttempts
  ) {
    this.retryAttempts = retryAttempts;
    return this;
  }

  @Inject(optional = true)
  public SingularityClientProvider setRetryStrategy(
    @Named(SingularityClientModule.RETRY_STRATEGY) Predicate retryStrategy
  ) {
    this.retryStrategy = retryStrategy;
    return this;
  }

  public SingularityClientProvider setHosts(String... hosts) {
    this.hosts = Arrays.asList(hosts);
    return this;
  }

  @Inject(optional = true)
  public SingularityClientProvider setSsl(boolean ssl) {
    this.ssl = ssl;
    return this;
  }

  @Override
  public SingularityClient get() {
    Preconditions.checkState(contextPath != null, "contextPath null");
    Preconditions.checkState(!hosts.isEmpty(), "no hosts provided");
    return new SingularityClient(contextPath, httpClient, hosts, credentials, ssl);
  }

  public SingularityClient get(Optional credentials) {
    Preconditions.checkState(contextPath != null, "contextPath null");
    Preconditions.checkState(!hosts.isEmpty(), "no hosts provided");
    Preconditions.checkNotNull(credentials);
    return new SingularityClient(contextPath, httpClient, hosts, credentials, ssl);
  }

  static String getClusterMembers(CuratorFramework curator) {
    try {
      final List leaders = curator
        .getChildren()
        .forPath(SingularityClusterManager.LEADER_PATH);
      final List hosts = Lists.newArrayListWithCapacity(leaders.size());

      for (String leader : leaders) {
        byte[] data = curator
          .getData()
          .forPath(ZKPaths.makePath(SingularityClusterManager.LEADER_PATH, leader));

        hosts.add(new String(data, UTF_8));
      }

      return Joiner.on(",").join(hosts);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy