You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by sudheeshkatkam <gi...@git.apache.org> on 2016/08/10 22:57:09 UTC

[GitHub] drill pull request #480: DRILL-4606: Add DrillClient.Builder class

Github user sudheeshkatkam commented on a diff in the pull request:

    https://github.com/apache/drill/pull/480#discussion_r74346449
  
    --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/client/DrillClient.java ---
    @@ -73,81 +78,173 @@
     import com.fasterxml.jackson.core.JsonProcessingException;
     import com.fasterxml.jackson.databind.ObjectMapper;
     import com.fasterxml.jackson.databind.node.ArrayNode;
    -import com.google.common.base.Strings;
    -import com.google.common.util.concurrent.AbstractCheckedFuture;
    -import com.google.common.util.concurrent.SettableFuture;
     
     /**
      * Thin wrapper around a UserClient that handles connect/close and transforms
      * String into ByteBuf.
    + *
    + * To create non-default objects, use {@link DrillClient.Builder the builder class}.
    + * E.g.
    + * <code>
    + *   DrillClient client = DrillClient.newBuilder()
    + *       .setConfig(...)
    + *       .setIsDirectConnection(true)
    + *       .build();
    + * </code>
    + *
    + * Except for {@link #runQuery} and {@link #cancelQuery}, this class is generally not thread safe.
      */
     public class DrillClient implements Closeable, ConnectionThrottle {
       private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillClient.class);
     
       private static final ObjectMapper objectMapper = new ObjectMapper();
       private final DrillConfig config;
    -  private UserClient client;
    -  private UserProperties props = null;
    -  private volatile ClusterCoordinator clusterCoordinator;
    -  private volatile boolean connected = false;
       private final BufferAllocator allocator;
    -  private int reconnectTimes;
    -  private int reconnectDelay;
    -  private boolean supportComplexTypes;
    -  private final boolean ownsZkConnection;
    +  private final EventLoopGroup eventLoopGroup;
    +  private final ExecutorService executor;
    +  private final boolean isDirectConnection;
    +  private final int reconnectTimes;
    +  private final int reconnectDelay;
    +
    +  // TODO: clusterCoordinator should be initialized in the constructor.
    +  // Currently, initialization is tightly coupled with #connect.
    +  private ClusterCoordinator clusterCoordinator;
    +
    +  // checks if this client owns these resources (used when closing)
       private final boolean ownsAllocator;
    -  private final boolean isDirectConnection; // true if the connection bypasses zookeeper and connects directly to a drillbit
    -  private EventLoopGroup eventLoopGroup;
    -  private ExecutorService executor;
    +  private final boolean ownsZkConnection;
    +  private final boolean ownsEventLoopGroup;
    +  private final boolean ownsExecutor;
     
    -  public DrillClient() throws OutOfMemoryException {
    -    this(DrillConfig.create(), false);
    +  // once #setSupportComplexTypes() is removed, make this final
    +  private boolean supportComplexTypes;
    +
    +  private UserClient client;
    +  private UserProperties props;
    +  private boolean connected;
    +
    +  public DrillClient() {
    +    this(newBuilder());
       }
     
    -  public DrillClient(boolean isDirect) throws OutOfMemoryException {
    -    this(DrillConfig.create(), isDirect);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(boolean isDirect) {
    +    this(newBuilder()
    +        .setDirectConnection(isDirect));
       }
     
    -  public DrillClient(String fileName) throws OutOfMemoryException {
    -    this(DrillConfig.create(fileName), false);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(String fileName) {
    +    this(newBuilder()
    +        .setConfigFromFile(fileName));
       }
     
    -  public DrillClient(DrillConfig config) throws OutOfMemoryException {
    -    this(config, null, false);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(DrillConfig config) {
    +    this(newBuilder()
    +        .setConfig(config));
       }
     
    -  public DrillClient(DrillConfig config, boolean isDirect)
    -      throws OutOfMemoryException {
    -    this(config, null, isDirect);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(DrillConfig config, boolean isDirect) {
    +    this(newBuilder()
    +        .setConfig(config)
    +        .setDirectConnection(isDirect));
       }
     
    -  public DrillClient(DrillConfig config, ClusterCoordinator coordinator)
    -    throws OutOfMemoryException {
    -    this(config, coordinator, null, false);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(DrillConfig config, ClusterCoordinator coordinator) {
    +    this(newBuilder()
    +        .setConfig(config)
    +        .setClusterCoordinator(coordinator));
       }
     
    -  public DrillClient(DrillConfig config, ClusterCoordinator coordinator, boolean isDirect)
    -    throws OutOfMemoryException {
    -    this(config, coordinator, null, isDirect);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(DrillConfig config, ClusterCoordinator coordinator, boolean isDirect) {
    +    this(newBuilder()
    +        .setConfig(config)
    +        .setClusterCoordinator(coordinator)
    +        .setDirectConnection(isDirect));
       }
     
    -  public DrillClient(DrillConfig config, ClusterCoordinator coordinator, BufferAllocator allocator)
    -      throws OutOfMemoryException {
    -    this(config, coordinator, allocator, false);
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(DrillConfig config, ClusterCoordinator coordinator, BufferAllocator allocator) {
    +    this(newBuilder()
    +        .setConfig(config)
    +        .setClusterCoordinator(coordinator)
    +        .setAllocator(allocator));
    +  }
    +
    +  /**
    +   * @deprecated Create a DrillClient using {@link DrillClient.Builder}.
    +   */
    +  @Deprecated
    +  public DrillClient(DrillConfig config, ClusterCoordinator coordinator, BufferAllocator allocator,
    +                     boolean isDirect) {
    +    this(newBuilder()
    +        .setConfig(config)
    +        .setClusterCoordinator(coordinator)
    +        .setAllocator(allocator)
    +        .setDirectConnection(isDirect));
       }
     
    -  public DrillClient(DrillConfig config, ClusterCoordinator coordinator, BufferAllocator allocator, boolean isDirect) {
    -    // if isDirect is true, the client will connect directly to the drillbit instead of
    -    // going thru the zookeeper
    -    this.isDirectConnection = isDirect;
    -    this.ownsZkConnection = coordinator == null && !isDirect;
    -    this.ownsAllocator = allocator == null;
    -    this.allocator = ownsAllocator ? RootAllocatorFactory.newRoot(config) : allocator;
    -    this.config = config;
    -    this.clusterCoordinator = coordinator;
    +  // used by DrillClient.Builder
    +  private DrillClient(final Builder builder) {
    +    this.config = builder.config != null ? builder.config : DrillConfig.create();
    +
    +    this.ownsAllocator = builder.allocator == null;
    +    this.allocator = !ownsAllocator ? builder.allocator : RootAllocatorFactory.newRoot(config);
    +
    +    this.isDirectConnection = builder.isDirectConnection;
    +    this.ownsZkConnection = builder.clusterCoordinator == null && !isDirectConnection;
    +    this.clusterCoordinator = builder.clusterCoordinator; // could be null
    +
    +    this.ownsEventLoopGroup = builder.eventLoopGroup == null;
    +    this.eventLoopGroup = !ownsEventLoopGroup ? builder.eventLoopGroup :
    +        TransportCheck.createEventLoopGroup(config.getInt(ExecConstants.CLIENT_RPC_THREADS), "Client-");
    +
    +    this.ownsExecutor = builder.executor == null;
    --- End diff --
    
    These constructors are light weight, no real work is done.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---