/**
  * This method is called when there is a topology change in the cluster.
  *
  * <p>This method is intended for internal use only.
  */
 public void reconfigure(Bucket bucket) {
   reconfiguring = true;
   if (bucket.isNotUpdating()) {
     getLogger()
         .info(
             "Bucket configuration is disconnected from cluster "
                 + "configuration updates, attempting to reconnect.");
     CouchbaseConnectionFactory cbcf = (CouchbaseConnectionFactory) connFactory;
     cbcf.requestConfigReconnect(cbcf.getBucketName(), this);
   }
   try {
     vconn.reconfigure(bucket);
     if (mconn instanceof CouchbaseConnection) {
       CouchbaseConnection cbConn = (CouchbaseConnection) mconn;
       cbConn.reconfigure(bucket);
     } else {
       CouchbaseMemcachedConnection cbMConn = (CouchbaseMemcachedConnection) mconn;
       cbMConn.reconfigure(bucket);
     }
   } catch (IllegalArgumentException ex) {
     getLogger()
         .warn("Failed to reconfigure client, staying with " + "previous configuration.", ex);
   } finally {
     reconfiguring = false;
   }
 }
  /**
   * Get a CouchbaseClient based on the REST response from a Couchbase server where the username is
   * different than the bucket name.
   *
   * <p>Note that when specifying a ConnectionFactory you must specify a BinaryConnectionFactory.
   * Also the ConnectionFactory's protocol and locator values are always overwritten. The protocol
   * will always be binary and the locator will be chosen based on the bucket type you are
   * connecting to.
   *
   * <p>To connect to the "default" special bucket for a given cluster, use an empty string as the
   * password.
   *
   * <p>If a password has not been assigned to the bucket, it is typically an empty string.
   *
   * <p>The subscribe variable is determines whether or not we will subscribe to the configuration
   * changes feed. This constructor should be used when calling super from subclasses of
   * CouchbaseClient since the subclass might want to start the changes feed later.
   *
   * @param cf the ConnectionFactory to use to create connections
   * @throws IOException if connections could not be made
   * @throws ConfigurationException if the configuration provided by the server has issues or is not
   *     compatible
   */
  public CouchbaseClient(CouchbaseConnectionFactory cf) throws IOException {
    super(cf, AddrUtil.getAddresses(cf.getVBucketConfig().getServers()));
    List<InetSocketAddress> addrs =
        AddrUtil.getAddressesFromURL(cf.getVBucketConfig().getCouchServers());

    getLogger().info(MODE_ERROR);
    vconn = cf.createViewConnection(addrs);
    cf.getConfigurationProvider().subscribe(cf.getBucketName(), this);
  }
 @Override
 public boolean shutdown(long timeout, TimeUnit unit) {
   boolean shutdownResult = false;
   try {
     shutdownResult = super.shutdown(timeout, unit);
     CouchbaseConnectionFactory cf = (CouchbaseConnectionFactory) connFactory;
     cf.getConfigurationProvider().shutdown();
     vconn.shutdown();
   } catch (IOException ex) {
     Logger.getLogger(CouchbaseClient.class.getName())
         .log(Level.SEVERE, "Unexpected IOException in shutdown", ex);
     throw new RuntimeException(null, ex);
   }
   return shutdownResult;
 }
  @Override
  public List<InputSplit> getSplits(JobContext ctx) throws IOException {
    final Configuration conf = ctx.getConfiguration();
    final URI ClusterURI;
    try {
      ClusterURI = new URI(conf.get(CouchbaseConfig.CB_INPUT_CLUSTER));
    } catch (URISyntaxException e) {
      throw new IOException(e);
    }
    final List<URI> ClientURIList = new ArrayList<URI>();
    ClientURIList.add(ClusterURI.resolve("/pools"));
    final String bucket = conf.get(CouchbaseConfig.CB_INPUT_BUCKET, "default");
    final String password = conf.get(CouchbaseConfig.CB_INPUT_PASSWORD, "");

    final CouchbaseConnectionFactory fact =
        new CouchbaseConnectionFactory(ClientURIList, bucket, password);

    final com.couchbase.client.vbucket.config.Config vbconfig = fact.getVBucketConfig();

    final List<VBucket> allVBuckets = vbconfig.getVbuckets();
    @SuppressWarnings("unchecked")
    final ArrayList<Integer>[] vblists = new ArrayList[vbconfig.getServersCount()];
    int vbid = 0;
    for (VBucket v : allVBuckets) {
      if (vblists[v.getMaster()] == null) {
        vblists[v.getMaster()] = new ArrayList<Integer>();
      }
      vblists[v.getMaster()].add(vbid);
      vbid++;
    }
    final ArrayList<InputSplit> splits = new ArrayList<InputSplit>();
    for (ArrayList<Integer> vblist : vblists) {
      splits.add(new CouchbaseSplit(vblist));
    }
    return splits;
  }
  /**
   * Create ViewNode connections and queue them up for connect.
   *
   * <p>This method also defines the connection params for each connection, including the default
   * settings like timeouts and the user agent string.
   *
   * @param addrs addresses of all the nodes it should connect to.
   * @return Returns a list of the ViewNodes.
   * @throws IOException
   */
  private List<ViewNode> createConnections(List<InetSocketAddress> addrs) throws IOException {

    List<ViewNode> nodeList = new LinkedList<ViewNode>();

    for (InetSocketAddress a : addrs) {
      HttpParams params = new SyncBasicHttpParams();
      params
          .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
          .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000)
          .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
          .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
          .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
          .setParameter(CoreProtocolPNames.USER_AGENT, "Couchbase Java Client 1.0.2");

      HttpProcessor httpproc =
          new ImmutableHttpProcessor(
              new HttpRequestInterceptor[] {
                new RequestContent(), new RequestTargetHost(),
                new RequestConnControl(), new RequestUserAgent(),
                new RequestExpectContinue(),
              });

      AsyncNHttpClientHandler protocolHandler =
          new AsyncNHttpClientHandler(
              httpproc,
              new MyHttpRequestExecutionHandler(this),
              new DefaultConnectionReuseStrategy(),
              new DirectByteBufferAllocator(),
              params);
      protocolHandler.setEventListener(new EventLogger());

      AsyncConnectionManager connMgr =
          new AsyncConnectionManager(
              new HttpHost(a.getHostName(), a.getPort()),
              NUM_CONNS,
              protocolHandler,
              params,
              new RequeueOpCallback(this));
      getLogger().info("Added %s to connect queue", a.getHostName());

      ViewNode node = connFactory.createViewNode(a, connMgr);
      node.init();
      nodeList.add(node);
    }

    return nodeList;
  }
 /**
  * Check if the {@link ViewNode} has active VBuckets.
  *
  * @param node the node to check
  * @return true or false if it has active VBuckets.
  */
 private boolean hasActiveVBuckets(ViewNode node) {
   DefaultConfig config = (DefaultConfig) connFactory.getVBucketConfig();
   return config.nodeHasActiveVBuckets(node.getSocketAddress());
 }