99import java .util .stream .Collectors ;
1010import org .apache .logging .log4j .LogManager ;
1111import org .apache .logging .log4j .Logger ;
12+ import org .folio .rest .tools .utils .ConnectionCacheMetrics ;
1213
1314/**
1415 * Provides a thread-safe cache that stores {@link CachedPgConnection} objects.
@@ -19,7 +20,7 @@ public class ConnectionCache {
1920 private static final String LOGGER_LABEL = "CONNECTION MANAGER CACHE STATE" ;
2021 private static final int INFO_LOG_LIMIT = 100 ;
2122 private final List <CachedPgConnection > cache ;
22- private final Metrics metrics = new Metrics ();
23+ private final ConnectionCacheMetrics metrics = new ConnectionCacheMetrics ();
2324
2425 public ConnectionCache () {
2526 cache = Collections .synchronizedList (new ArrayList <>());
@@ -28,7 +29,7 @@ public ConnectionCache() {
2829 public void remove (CachedPgConnection connection ) {
2930 synchronized (cache ) {
3031 cache .remove (connection );
31- metrics .active -- ;
32+ metrics .decrementActive () ;
3233 LOG .debug ("Removed connection: {} {}" ,
3334 connection .getTenantId (), connection .getSessionId ());
3435 }
@@ -60,7 +61,7 @@ public void removeOldestAvailableAndClose() {
6061 .ifPresent (connection -> {
6162 connection .getWrappedConnection ().close ();
6263 cache .remove (connection );
63- metrics .active -- ;
64+ metrics .decrementActive () ;
6465 LOG .debug ("Removed and closed oldest available connection: {} {}" ,
6566 connection .getTenantId (), connection .getSessionId ());
6667 });
@@ -80,7 +81,9 @@ public Optional<CachedPgConnection> getAvailableConnection(String tenantId, Stri
8081 // First attempt to find a connection for the tenant that is available.
8182 Optional <CachedPgConnection > connectionOptional =
8283 cache .stream ().filter (connection ->
83- connection .getTenantId ().equals (tenantId ) && connection .getSchemaName ().equals (schemaName ) && connection .isAvailable ()).findFirst ();
84+ connection .getTenantId ().equals (tenantId ) &&
85+ connection .getSchemaName ().equals (schemaName ) &&
86+ connection .isAvailable ()).findFirst ();
8487
8588 // If The first attempt fails, try to find the oldest connection for another tenant that is available.
8689 if (connectionOptional .isEmpty ()) {
@@ -123,14 +126,14 @@ public int size() {
123126 * @param context Any details that help contextualize the event.
124127 */
125128 public void log (String context ) {
126- Supplier <String > msgSupplier = () -> metrics .toString (LOGGER_LABEL + ": " + context );
127- Supplier <String > msgDebugSupplier = metrics ::toStringDebug ;
129+ Supplier <String > msgSupplier = () -> metrics .toString (LOGGER_LABEL + ": " + context , cache . size () );
130+ Supplier <String > msgDebugSupplier = this ::toStringDebug ;
128131
129132 if (LOG .isDebugEnabled ()) {
130133 LOG .debug ("{} {}" , msgSupplier .get (), msgDebugSupplier .get ());
131134 }
132135
133- if (LOG .isInfoEnabled () && (metrics .hits % INFO_LOG_LIMIT == 0 || metrics .misses % INFO_LOG_LIMIT == 0 )) {
136+ if (LOG .isInfoEnabled () && (metrics .getHits () % INFO_LOG_LIMIT == 0 || metrics .getMisses () % INFO_LOG_LIMIT == 0 )) {
134137 LOG .info (msgSupplier .get ());
135138 }
136139 }
@@ -144,11 +147,11 @@ public void incrementMisses() {
144147 }
145148
146149 public void incrementActive () {
147- metrics .active ++ ;
150+ metrics .incrementActive () ;
148151 }
149152
150153 public void decrementActive () {
151- metrics .active -- ;
154+ metrics .decrementActive () ;
152155 }
153156
154157 public void incrementRecycled () {
@@ -168,72 +171,22 @@ public void incrementNewConnectionErrors() {
168171 }
169172
170173 public void setPoolSizeMetric (int size ) {
171- metrics .poolSize = size ;
172- }
173-
174- class Metrics {
175- int hits ;
176- int misses ;
177- int active ;
178- int poolSize ;
179- int recycled ;
180- int recycleErrors ;
181- int newConnections ;
182- int newConnectionErrors ;
183-
184- void clear () {
185- hits = 0 ;
186- misses = 0 ;
187- active = 0 ;
188- recycled = 0 ;
189- recycleErrors = 0 ;
190- newConnections = 0 ;
191- newConnectionErrors = 0 ;
192- }
193-
194- void incrementHits () {
195- hits = (hits == Integer .MAX_VALUE ) ? 0 : (hits + 1 );
196- }
197-
198- void incrementMisses () {
199- misses = (misses == Integer .MAX_VALUE ) ? 0 : (misses + 1 );
200- }
201-
202- void incrementNewConnections () {
203- newConnections = (newConnections == Integer .MAX_VALUE ) ? 0 : (newConnections + 1 );
204- }
205-
206- void incrementNewConnectionErrors () {
207- newConnectionErrors = (newConnectionErrors == Integer .MAX_VALUE ) ? 0 : (newConnectionErrors + 1 );
208- }
209-
210- void incrementRecycled () {
211- recycled = (recycled == Integer .MAX_VALUE ) ? 0 : (recycled + 1 );
212- }
213-
214- void incrementRecycleErrors () {
215- recycleErrors = (recycleErrors == Integer .MAX_VALUE ) ? 0 : (recycleErrors + 1 );
216- }
217-
218- String toString (String msg ) {
219- return msg + String .format (":: %s hits, %s misses, %s recycled, %s recycleErrors, %s newConnections, %s newConnectionErrors, %s size, %s active, %s pool" ,
220- hits , misses , recycled , recycleErrors , newConnections , newConnectionErrors , cache .size (), active , poolSize );
221- }
174+ metrics .setPoolSize (size );
175+ }
222176
223- String toStringDebug () {
224- var items = "\n CONNECTION MANAGER CACHE ITEMS (DEBUG):\n " ;
225- synchronized (cache ) {
226- items += cache .stream ()
227- .map (item -> String .format ("%s %s %s %s %s" ,
228- item .getSessionId (),
229- item .isAvailable (),
230- item .getIdleSince (),
231- item .getTenantId (),
232- item .getSchemaName ()
233- ))
234- .collect (Collectors .joining ("\n " ));
235- }
236- return items ;
237- }
177+ private String toStringDebug () {
178+ var items = "\n CONNECTION MANAGER CACHE ITEMS (DEBUG):\n " ;
179+ synchronized (cache ) {
180+ items += cache .stream ()
181+ .map (item -> String .format ("%s %s %s %s %s" ,
182+ item .getSessionId (),
183+ item .isAvailable (),
184+ item .getIdleSince (),
185+ item .getTenantId (),
186+ item .getSchemaName ()
187+ ))
188+ .collect (Collectors .joining ("\n " ));
189+ }
190+ return items ;
238191 }
239192}
0 commit comments