/** * Reconfigures the connected ViewNodes. * * <p>When a reconfiguration event happens, new ViewNodes may need to be added or old ones need to * be removed from the current configuration. This method takes care that those operations are * performed in the correct order and are executed in a thread-safe manner. * * @param bucket the bucket which has been rebalanced. */ public void reconfigure(Bucket bucket) { reconfiguring = true; try { // get a new collection of addresses from the received config HashSet<SocketAddress> newServerAddresses = new HashSet<SocketAddress>(); List<InetSocketAddress> newServers = AddrUtil.getAddressesFromURL(bucket.getConfig().getCouchServers()); for (InetSocketAddress server : newServers) { // add parsed address to our collections newServerAddresses.add(server); } // split current nodes to "odd nodes" and "stay nodes" ArrayList<ViewNode> shutdownNodes = new ArrayList<ViewNode>(); ArrayList<ViewNode> stayNodes = new ArrayList<ViewNode>(); ArrayList<InetSocketAddress> stayServers = new ArrayList<InetSocketAddress>(); wlock.lock(); try { for (ViewNode current : couchNodes) { if (newServerAddresses.contains(current.getSocketAddress())) { stayNodes.add(current); stayServers.add((InetSocketAddress) current.getSocketAddress()); } else { shutdownNodes.add(current); } } // prepare a collection of addresses for new nodes newServers.removeAll(stayServers); // create a collection of new nodes List<ViewNode> newNodes = createConnections(newServers); // merge stay nodes with new nodes List<ViewNode> mergedNodes = new ArrayList<ViewNode>(); mergedNodes.addAll(stayNodes); mergedNodes.addAll(newNodes); couchNodes = mergedNodes; } finally { wlock.unlock(); } // shutdown for the oddNodes for (ViewNode qa : shutdownNodes) { try { qa.shutdown(); } catch (IOException e) { getLogger().error("Error shutting down connection to " + qa.getSocketAddress()); } } } catch (IOException e) { getLogger().error("Connection reconfiguration failed", e); } 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); }