2323 */
2424public class SharedClusterManager {
2525 private static Logger logger = LogManager .getLogger (SharedClusterManager .class );
26-
26+
2727 // Common KV connections setting for massively parallel collection loads
2828 // Increased from default 5 to 500 to support 5,000 collections loading in parallel
29- private static final int DEFAULT_KV_CONNECTIONS = 500 ;
30-
29+ private static final int DEFAULT_KV_CONNECTIONS = 5 ;
30+
3131 // Shared ClusterEnvironment with optimized connection settings
3232 private static ClusterEnvironment sharedEnvironment ;
33-
33+
3434 // Store cluster instances per server connection string
3535 private static ConcurrentHashMap <String , ClusterWrapper > clusterMap = new ConcurrentHashMap <>();
36-
36+
3737 // Initialize the shared environment once (lazy initialization)
3838 private static void initializeSharedEnvironment () {
3939 if (sharedEnvironment == null ) {
@@ -51,7 +51,7 @@ private static void initializeSharedEnvironment() {
5151 }
5252 }
5353 }
54-
54+
5555 /**
5656 * Get or create a shared Cluster instance for the given server connection
5757 */
@@ -72,27 +72,27 @@ public static synchronized Cluster getCluster(Server server) throws Authenticati
7272
7373 return wrapper .cluster ;
7474 }
75-
75+
7676 /**
7777 * Release reference to the shared Cluster instance
7878 */
7979 public static synchronized void releaseCluster (Server server ) {
8080 String clusterKey = getClusterKey (server );
8181 ClusterWrapper wrapper = clusterMap .get (clusterKey );
82-
82+
8383 if (wrapper != null ) {
8484 int refCount = wrapper .decrementRefCount ();
85- logger .debug ("Released Cluster instance for server: " + server .ip +
85+ logger .debug ("Released Cluster instance for server: " + server .ip +
8686 " (ref count: " + refCount + ")" );
87-
87+
8888 if (refCount == 0 ) {
8989 logger .info ("No more references, disconnecting Cluster for server: " + server .ip );
9090 wrapper .cluster .disconnect ();
9191 clusterMap .remove (clusterKey );
9292 }
9393 }
9494 }
95-
95+
9696 /**
9797 * Shutdown all cluster instances and the shared environment
9898 */
@@ -110,7 +110,7 @@ public static synchronized void shutdownAll() {
110110 logger .info ("Shared Cluster Environment shutdown complete" );
111111 }
112112 }
113-
113+
114114 private static Cluster createCluster (Server server ) throws AuthenticationFailureException {
115115 ClusterOptions clusterOptions ;
116116 try {
@@ -121,52 +121,52 @@ private static Cluster createCluster(Server server) throws AuthenticationFailure
121121 clusterOptions = ClusterOptions .clusterOptions (server .rest_username , server .rest_password )
122122 .environment (createNonTLSEnvironment ());
123123 }
124-
124+
125125 Cluster cluster = Cluster .connect (server .ip , clusterOptions );
126126 logger .info ("Cluster connection successful: " + server .ip );
127127 return cluster ;
128128 } catch (AuthenticationFailureException e ) {
129- logger .error ("Authentication failed for server: " + server .ip +
129+ logger .error ("Authentication failed for server: " + server .ip +
130130 " with user: " + server .rest_username );
131131 throw e ;
132132 } catch (Exception e ) {
133133 logger .error ("Failed to connect Cluster to server: " + server .ip , e );
134134 throw new RuntimeException ("Cluster connection failed" , e );
135135 }
136136 }
137-
137+
138138 private static ClusterEnvironment createNonTLSEnvironment () {
139139 return ClusterEnvironment .builder ()
140140 .timeoutConfig (TimeoutConfig .builder ().kvTimeout (Duration .ofSeconds (10 )))
141141 .ioConfig (IoConfig .enableDnsSrv (true ))
142142 .ioConfig (IoConfig .numKvConnections (DEFAULT_KV_CONNECTIONS ))
143143 .build ();
144144 }
145-
145+
146146 private static String getClusterKey (Server server ) {
147147 return server .ip + ":" + server .memcached_port ;
148148 }
149-
149+
150150 /**
151151 * Wrapper class to track reference count for shared Cluster instances
152152 */
153153 private static class ClusterWrapper {
154154 Cluster cluster ;
155155 AtomicInteger refCount ;
156-
156+
157157 ClusterWrapper (Cluster cluster ) {
158158 this .cluster = cluster ;
159159 this .refCount = new AtomicInteger (1 );
160160 }
161-
161+
162162 void incrementRefCount () {
163163 refCount .incrementAndGet ();
164164 }
165-
165+
166166 int decrementRefCount () {
167167 return refCount .decrementAndGet ();
168168 }
169-
169+
170170 int getRefCount () {
171171 return refCount .get ();
172172 }
0 commit comments