Skip to content

Commit 3e95e07

Browse files
committed
Trying to fix Sonar complaints
1 parent 0a0ec83 commit 3e95e07

4 files changed

Lines changed: 127 additions & 77 deletions

File tree

domain-models-runtime/src/main/java/org/folio/rest/persist/cache/CachedConnectionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private Future<PgConnection> setRoleAndSchemaWithRecycle(String schemaName,
177177
});
178178
}
179179

180-
private String getSetRoleAndSchemaSql(String schemaName, String tenantId) {
180+
private static String getSetRoleAndSchemaSql(String schemaName, String tenantId) {
181181
return PostgresClient.DEFAULT_SCHEMA.equals(tenantId)
182182
? "SET ROLE NONE; SET SCHEMA ''"
183183
: ("SET ROLE '" + schemaName + "'; SET SCHEMA '" + schemaName + "'");

domain-models-runtime/src/main/java/org/folio/rest/persist/cache/ConnectionCache.java

Lines changed: 28 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.stream.Collectors;
1010
import org.apache.logging.log4j.LogManager;
1111
import 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 = "\nCONNECTION 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 = "\nCONNECTION 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
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.folio.rest.tools.utils;
2+
3+
public class ConnectionCacheMetrics {
4+
private int hits;
5+
private int misses;
6+
private int active;
7+
private int poolSize;
8+
private int recycled;
9+
private int recycleErrors;
10+
private int newConnections;
11+
private int newConnectionErrors;
12+
13+
public int getHits() {
14+
return hits;
15+
}
16+
17+
public int getMisses() {
18+
return misses;
19+
}
20+
21+
public int getActive() {
22+
return active;
23+
}
24+
25+
public int getPoolSize() {
26+
return poolSize;
27+
}
28+
29+
public void setPoolSize(int poolSize) {
30+
this.poolSize = poolSize;
31+
}
32+
33+
public int getRecycled() {
34+
return recycled;
35+
}
36+
37+
public int getRecycleErrors() {
38+
return recycleErrors;
39+
}
40+
41+
public int getNewConnections() {
42+
return newConnections;
43+
}
44+
45+
public int getNewConnectionErrors() {
46+
return newConnectionErrors;
47+
}
48+
49+
public void incrementActive() {
50+
active++;
51+
}
52+
53+
public void decrementActive() {
54+
active--;
55+
}
56+
57+
public void clear() {
58+
hits = 0;
59+
misses = 0;
60+
active = 0;
61+
recycled = 0;
62+
recycleErrors = 0;
63+
newConnections = 0;
64+
newConnectionErrors = 0;
65+
}
66+
67+
public void incrementHits() {
68+
hits = (hits == Integer.MAX_VALUE) ? 0 : (hits + 1);
69+
}
70+
71+
public void incrementMisses() {
72+
misses = (misses == Integer.MAX_VALUE) ? 0 : (misses + 1);
73+
}
74+
75+
public void incrementNewConnections() {
76+
newConnections = (newConnections == Integer.MAX_VALUE) ? 0 : (newConnections + 1);
77+
}
78+
79+
public void incrementNewConnectionErrors() {
80+
newConnectionErrors = (newConnectionErrors == Integer.MAX_VALUE) ? 0 : (newConnectionErrors + 1);
81+
}
82+
83+
public void incrementRecycled() {
84+
recycled = (recycled == Integer.MAX_VALUE) ? 0 : (recycled + 1);
85+
}
86+
87+
public void incrementRecycleErrors() {
88+
recycleErrors = (recycleErrors == Integer.MAX_VALUE) ? 0 : (recycleErrors + 1);
89+
}
90+
91+
public String toString(String msg, int cacheSize) {
92+
return msg + String.format(
93+
":: %s hits, %s misses, %s recycled, %s recycleErrors, %s newConnections, "
94+
+ "%s newConnectionErrors, %s size, %s active, %s pool",
95+
hits, misses, recycled, recycleErrors, newConnections, newConnectionErrors,
96+
cacheSize, active, poolSize);
97+
}
98+
}

domain-models-runtime/src/test/java/org/folio/rest/persist/cache/CachedConnectionManagerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.vertx.core.Vertx;
55
import io.vertx.ext.unit.TestContext;
66
import io.vertx.ext.unit.junit.VertxUnitRunner;
7-
import io.vertx.pgclient.PgConnection;
87
import io.vertx.pgclient.PgPool;
98
import java.util.concurrent.atomic.AtomicBoolean;
109
import org.folio.rest.persist.PgConnectionMock;

0 commit comments

Comments
 (0)