Skip to content

Commit bf37314

Browse files
committed
Renames - minimal functional/logical code changes
1 parent 49bfd31 commit bf37314

File tree

3 files changed

+70
-64
lines changed

3 files changed

+70
-64
lines changed

ebean-datasource/src/main/java/io/ebean/datasource/pool/FreeConnectionBuffer.java renamed to ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionBuffer.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,62 @@
33
import java.util.*;
44

55
/**
6-
* A buffer designed especially to hold free pooled connections.
6+
* A buffer designed especially to hold pooled connections (free and busy ones)
77
* <p>
88
* All thread safety controlled externally (by PooledConnectionQueue).
99
* </p>
1010
*/
11-
final class FreeConnectionBuffer {
11+
final class ConnectionBuffer {
1212

1313

1414
private final Node free = Node.init();
1515

16-
int size = 0;
16+
int freeSize = 0;
1717

1818
/**
1919
* Return the number of entries in the buffer.
2020
*/
21-
int size() {
22-
return size;
21+
int freeSize() {
22+
return freeSize;
2323
}
2424

2525
/**
2626
* Return true if the buffer is empty.
2727
*/
28-
boolean isEmpty() {
29-
return size == 0;
28+
boolean hasFreeConnections() {
29+
return freeSize > 0;
3030
}
3131

3232
/**
3333
* Add connection to the free list.
3434
*/
35-
void add(PooledConnection pc) {
35+
void addFree(PooledConnection pc) {
3636
new Node(pc).addAfter(free);
37-
size++;
37+
freeSize++;
3838
}
3939

4040
/**
41-
* Remove a connection from the free list.
41+
* Remove a connection from the free list. Returns <code>null</code> if there is not any.
4242
*/
43-
PooledConnection remove() {
43+
PooledConnection popFree () {
4444
Node node = free.next;
45+
if (node.isBoundaryNode()) {
46+
return null;
47+
}
4548
node.remove();
46-
size--;
49+
freeSize--;
4750
return node.pc;
4851
}
4952

5053
/**
51-
* Close all connections in this buffer.
54+
* Close all connections in the free list.
5255
*/
53-
void closeAll(boolean logErrors) {
56+
void closeAllFree(boolean logErrors) {
5457
List<PooledConnection> tempList = new ArrayList<>();
55-
while (size > 0) {
56-
tempList.add(remove());
58+
PooledConnection c = popFree();
59+
while (c != null) {
60+
tempList.add(c);
61+
c = popFree();
5762
}
5863

5964
if (Log.isLoggable(System.Logger.Level.TRACE)) {
@@ -79,7 +84,7 @@ int trim(int minSize, long usedSince, long createdSince) {
7984
node = node.next;
8085
if (current.pc.shouldTrim(usedSince, createdSince)) {
8186
current.remove();
82-
size--;
87+
freeSize--;
8388
current.pc.closeConnectionFully(true);
8489
trimCount++;
8590
}

ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnectionQueue.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class PooledConnectionQueue {
2020
/**
2121
* A 'circular' buffer designed specifically for free connections.
2222
*/
23-
private final FreeConnectionBuffer freeList;
23+
private final ConnectionBuffer buffer;
2424
/**
2525
* A 'slots' buffer designed specifically for busy connections.
2626
* Fast add remove based on slot id.
@@ -75,13 +75,13 @@ final class PooledConnectionQueue {
7575
this.maxAgeMillis = pool.maxAgeMillis();
7676
this.validateStaleMillis = pool.validateStaleMillis();
7777
this.busyList = new BusyConnectionBuffer(maxSize, 20);
78-
this.freeList = new FreeConnectionBuffer();
78+
this.buffer = new ConnectionBuffer();
7979
this.lock = new ReentrantLock(false);
8080
this.notEmpty = lock.newCondition();
8181
}
8282

8383
private PoolStatus createStatus() {
84-
return new Status(minSize, maxSize, freeList.size(), busyList.size(), waitingThreads, highWaterMark,
84+
return new Status(minSize, maxSize, buffer.freeSize(), busyList.size(), waitingThreads, highWaterMark,
8585
waitCount, hitCount, totalAcquireNanos, maxAcquireNanos);
8686
}
8787

@@ -126,7 +126,7 @@ void setMaxSize(int maxSize) {
126126
}
127127

128128
private int totalConnections() {
129-
return freeList.size() + busyList.size();
129+
return buffer.freeSize() + busyList.size();
130130
}
131131

132132
void ensureMinimumConnections() throws SQLException {
@@ -135,7 +135,7 @@ void ensureMinimumConnections() throws SQLException {
135135
int add = minSize - totalConnections();
136136
if (add > 0) {
137137
for (int i = 0; i < add; i++) {
138-
freeList.add(pool.createConnectionForQueue(connectionId++));
138+
buffer.addFree(pool.createConnectionForQueue(connectionId++));
139139
}
140140
notEmpty.signal();
141141
}
@@ -156,7 +156,7 @@ void returnPooledConnection(PooledConnection c, boolean forceClose) {
156156
if (forceClose || c.shouldTrimOnReturn(lastResetTime, maxAgeMillis)) {
157157
c.closeConnectionFully(false);
158158
} else {
159-
freeList.add(c);
159+
buffer.addFree(c);
160160
notEmpty.signal();
161161
}
162162
} finally {
@@ -165,10 +165,11 @@ void returnPooledConnection(PooledConnection c, boolean forceClose) {
165165
}
166166

167167
private PooledConnection extractFromFreeList() {
168-
if (freeList.isEmpty()) {
168+
169+
PooledConnection c = buffer.popFree();
170+
if (c == null) {
169171
return null;
170172
}
171-
final PooledConnection c = freeList.remove();
172173
if (validateStaleMillis > 0 && staleEviction(c)) {
173174
c.closeConnectionFully(false);
174175
return null;
@@ -291,7 +292,7 @@ private PooledConnection _obtainConnectionWaitLoop() throws SQLException, Interr
291292

292293
try {
293294
nanos = notEmpty.awaitNanos(nanos);
294-
if (!freeList.isEmpty()) {
295+
if (buffer.hasFreeConnections()) {
295296
// successfully waited
296297
return extractFromFreeList();
297298
}
@@ -373,20 +374,20 @@ void trim(long maxInactiveMillis, long maxAgeMillis) {
373374
private boolean trimInactiveConnections(long maxInactiveMillis, long maxAgeMillis) {
374375
final long createdSince = (maxAgeMillis == 0) ? 0 : System.currentTimeMillis() - maxAgeMillis;
375376
final int trimmedCount;
376-
if (freeList.size() > minSize) {
377+
if (buffer.freeSize() > minSize) {
377378
// trim on maxInactive and maxAge
378379
long usedSince = System.currentTimeMillis() - maxInactiveMillis;
379-
trimmedCount = freeList.trim(minSize, usedSince, createdSince);
380+
trimmedCount = buffer.trim(minSize, usedSince, createdSince);
380381
} else if (createdSince > 0) {
381382
// trim only on maxAge
382-
trimmedCount = freeList.trim(0, createdSince, createdSince);
383+
trimmedCount = buffer.trim(0, createdSince, createdSince);
383384
} else {
384385
trimmedCount = 0;
385386
}
386387
if (trimmedCount > 0 && Log.isLoggable(DEBUG)) {
387388
Log.debug("DataSource [{0}] trimmed [{1}] inactive connections. New size[{2}]", name, trimmedCount, totalConnections());
388389
}
389-
return trimmedCount > 0 && freeList.size() < minSize;
390+
return trimmedCount > 0 && buffer.freeSize() < minSize;
390391
}
391392

392393
/**
@@ -395,7 +396,7 @@ private boolean trimInactiveConnections(long maxInactiveMillis, long maxAgeMilli
395396
private void closeFreeConnections(boolean logErrors) {
396397
lock.lock();
397398
try {
398-
freeList.closeAll(logErrors);
399+
buffer.closeAllFree(logErrors);
399400
} finally {
400401
lock.unlock();
401402
}

ebean-datasource/src/test/java/io/ebean/datasource/pool/FreeConnectionBufferTest.java renamed to ebean-datasource/src/test/java/io/ebean/datasource/pool/ConnectionBufferTest.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,70 @@
99
import static org.assertj.core.api.Assertions.assertThat;
1010
import static org.junit.jupiter.api.Assertions.*;
1111

12-
class FreeConnectionBufferTest {
12+
class ConnectionBufferTest {
1313

1414
@Test
1515
void test() {
1616

17-
FreeConnectionBuffer b = new FreeConnectionBuffer();
17+
ConnectionBuffer b = new ConnectionBuffer();
1818

1919
PooledConnection p0 = new PooledConnection("0");
2020
PooledConnection p1 = new PooledConnection("1");
2121
PooledConnection p2 = new PooledConnection("2");
2222
// PooledConnection p3 = new PooledConnection("3");
2323

24-
assertEquals(0, b.size());
25-
assertTrue(b.isEmpty());
24+
assertEquals(0, b.freeSize());
25+
assertFalse(b.hasFreeConnections());
2626

27-
b.add(p0);
27+
b.addFree(p0);
2828

29-
assertEquals(1, b.size());
30-
assertFalse(b.isEmpty());
29+
assertEquals(1, b.freeSize());
30+
assertTrue(b.hasFreeConnections());
3131

32-
PooledConnection r0 = b.remove();
32+
PooledConnection r0 = b.popFree();
3333
assertThat(p0).isSameAs(r0);
3434

35-
assertEquals(0, b.size());
36-
assertTrue(b.isEmpty());
35+
assertEquals(0, b.freeSize());
36+
assertFalse(b.hasFreeConnections());
3737

38-
b.add(p0);
39-
b.add(p1);
40-
b.add(p2);
38+
b.addFree(p0);
39+
b.addFree(p1);
40+
b.addFree(p2);
4141

42-
assertEquals(3, b.size());
42+
assertEquals(3, b.freeSize());
4343

44-
PooledConnection r1 = b.remove();
44+
PooledConnection r1 = b.popFree();
4545
assertSame(p2, r1);
46-
PooledConnection r2 = b.remove();
46+
PooledConnection r2 = b.popFree();
4747
assertSame(p1, r2);
4848

49-
assertEquals(1, b.size());
50-
b.add(p2);
51-
assertEquals(2, b.size());
52-
PooledConnection r3 = b.remove();
49+
assertEquals(1, b.freeSize());
50+
b.addFree(p2);
51+
assertEquals(2, b.freeSize());
52+
PooledConnection r3 = b.popFree();
5353
assertSame(p2, r3);
54-
assertEquals(1, b.size());
55-
PooledConnection r4 = b.remove();
54+
assertEquals(1, b.freeSize());
55+
PooledConnection r4 = b.popFree();
5656
assertSame(p0, r4);
57-
assertEquals(0, b.size());
57+
assertEquals(0, b.freeSize());
5858

59-
b.add(p2);
60-
b.add(p1);
61-
b.add(p0);
59+
b.addFree(p2);
60+
b.addFree(p1);
61+
b.addFree(p0);
6262

63-
assertEquals(3, b.size());
63+
assertEquals(3, b.freeSize());
6464

65-
PooledConnection r5 = b.remove();
65+
PooledConnection r5 = b.popFree();
6666
assertSame(p0, r5);
67-
assertEquals(2, b.size());
67+
assertEquals(2, b.freeSize());
6868

69-
PooledConnection r6 = b.remove();
69+
PooledConnection r6 = b.popFree();
7070
assertSame(p1, r6);
71-
assertEquals(1, b.size());
71+
assertEquals(1, b.freeSize());
7272

73-
PooledConnection r7 = b.remove();
73+
PooledConnection r7 = b.popFree();
7474
assertSame(p2, r7);
75-
assertEquals(0, b.size());
75+
assertEquals(0, b.freeSize());
7676

7777
}
7878

0 commit comments

Comments
 (0)