Skip to content

Commit 939b467

Browse files
authored
IManagedRelpConnection with void ensureSent(RelpBatch relpBatch);, major version up because interface change (#78)
1 parent e8786c2 commit 939b467

6 files changed

Lines changed: 98 additions & 6 deletions

File tree

src/main/java/com/teragrep/rlp_01/client/IManagedRelpConnection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.teragrep.rlp_01.client;
1818

19+
import com.teragrep.rlp_01.RelpBatch;
1920
import com.teragrep.rlp_01.pool.Poolable;
2021

2122
import java.io.IOException;
@@ -26,4 +27,5 @@ public interface IManagedRelpConnection extends Poolable {
2627
void connect() throws IOException;
2728
void forceReconnect();
2829
void ensureSent(byte[] bytes);
30+
void ensureSent(RelpBatch relpBatch);
2931
}

src/main/java/com/teragrep/rlp_01/client/ManagedRelpConnection.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,13 @@ private void tearDown() {
7878
}
7979

8080
@Override
81-
public void ensureSent(byte[] bytes) {
81+
public void ensureSent(RelpBatch relpBatch) {
8282
// avoid unnecessary exception for fresh connections
8383
if (!hasConnected) {
8484
connect();
8585
}
8686

87-
final RelpBatch relpBatch = new RelpBatch();
88-
relpBatch.insert(bytes);
87+
8988
boolean notSent = true;
9089
while (notSent) {
9190
try {
@@ -105,6 +104,13 @@ public void ensureSent(byte[] bytes) {
105104
}
106105
}
107106

107+
@Override
108+
public void ensureSent(byte[] bytes) {
109+
final RelpBatch relpBatch = new RelpBatch();
110+
relpBatch.insert(bytes);
111+
ensureSent(relpBatch);
112+
}
113+
108114
@Override
109115
public boolean isStub() {
110116
return false;

src/main/java/com/teragrep/rlp_01/client/ManagedRelpConnectionStub.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package com.teragrep.rlp_01.client;
1818

19+
import com.teragrep.rlp_01.RelpBatch;
20+
1921
import java.io.IOException;
2022

2123
public class ManagedRelpConnectionStub implements IManagedRelpConnection {
@@ -40,6 +42,11 @@ public void ensureSent(byte[] bytes) {
4042
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
4143
}
4244

45+
@Override
46+
public void ensureSent(RelpBatch relpBatch) {
47+
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
48+
}
49+
4350
@Override
4451
public boolean isStub() {
4552
return true;

src/main/java/com/teragrep/rlp_01/client/RebindableRelpConnection.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package com.teragrep.rlp_01.client;
1818

19+
import com.teragrep.rlp_01.RelpBatch;
20+
1921
import java.io.IOException;
2022

2123
public class RebindableRelpConnection implements IManagedRelpConnection {
@@ -46,12 +48,20 @@ public void forceReconnect() {
4648

4749
@Override
4850
public void ensureSent(byte[] bytes) {
51+
RelpBatch relpBatch = new RelpBatch();
52+
relpBatch.insert(bytes);
53+
ensureSent(relpBatch);
54+
}
55+
56+
@Override
57+
public void ensureSent(RelpBatch relpBatch) {
4958
if (recordsSent >= rebindRequestAmount) {
5059
reconnect();
5160
recordsSent = 0;
5261
}
53-
managedRelpConnection.ensureSent(bytes);
54-
recordsSent++;
62+
int batchLength = relpBatch.getWorkQueueLength();
63+
managedRelpConnection.ensureSent(relpBatch);
64+
recordsSent = recordsSent + batchLength;
5565
}
5666

5767
@Override

src/main/java/com/teragrep/rlp_01/client/RenewableRelpConnection.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package com.teragrep.rlp_01.client;
1818

19+
import com.teragrep.rlp_01.RelpBatch;
20+
1921
import java.io.IOException;
2022
import java.time.Instant;
2123
import java.time.Duration;
@@ -52,11 +54,18 @@ public void forceReconnect() {
5254

5355
@Override
5456
public void ensureSent(byte[] bytes) {
57+
RelpBatch relpBatch = new RelpBatch();
58+
relpBatch.insert(bytes);
59+
ensureSent(relpBatch);
60+
}
61+
62+
@Override
63+
public void ensureSent(RelpBatch relpBatch) {
5564
if (lastAccess.plus(maxIdle).isBefore(Instant.now())) {
5665
forceReconnect();
5766
}
5867
lastAccess = Instant.now();
59-
managedRelpConnection.ensureSent(bytes);
68+
managedRelpConnection.ensureSent(relpBatch);
6069
}
6170

6271
@Override

src/test/java/com/teragrep/rlp_01/client/ManagedConnectionTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.teragrep.net_01.eventloop.EventLoop;
2121
import com.teragrep.net_01.eventloop.EventLoopFactory;
2222
import com.teragrep.net_01.server.ServerFactory;
23+
import com.teragrep.rlp_01.RelpBatch;
2324
import com.teragrep.rlp_01.pool.Pool;
2425
import com.teragrep.rlp_01.pool.UnboundPool;
2526
import com.teragrep.rlp_03.frame.FrameDelegationClockFactory;
@@ -281,4 +282,61 @@ public void testPooledReboundConnections() {
281282
connectionOpenCount.set(0);
282283
connectionCleanCloseCount.set(0);
283284
}
285+
286+
@Test
287+
public void testRelpBatchSend() {
288+
RelpConfig relpConfig = new RelpConfig(
289+
hostname,
290+
port,
291+
500,
292+
0,
293+
false,
294+
Duration.ZERO,
295+
false
296+
);
297+
298+
RelpConnectionFactory relpConnectionFactory = new RelpConnectionFactory(relpConfig);
299+
300+
Pool<IManagedRelpConnection> relpConnectionPool = new UnboundPool<>(relpConnectionFactory, new ManagedRelpConnectionStub());
301+
302+
int testCycles = 20;
303+
CountDownLatch countDownLatch = new CountDownLatch(testCycles);
304+
305+
306+
307+
for (int i = 0; i < testCycles; i++) {
308+
final String heyRelp = "hey this is batched relp 0 " + i;
309+
final String heyThisBeRelpToo = "hey this is batched relp 1 " + i;
310+
ForkJoinPool.commonPool().submit(() -> {
311+
RelpBatch relpBatch = new RelpBatch();
312+
relpBatch.insert(heyRelp.getBytes(StandardCharsets.UTF_8));
313+
relpBatch.insert(heyThisBeRelpToo.getBytes(StandardCharsets.UTF_8));
314+
315+
IManagedRelpConnection connection = relpConnectionPool.get();
316+
317+
// will set timer to 5 millis
318+
connection.ensureSent(relpBatch);
319+
320+
relpConnectionPool.offer(connection);
321+
countDownLatch.countDown();
322+
});
323+
}
324+
325+
Assertions.assertDoesNotThrow(() -> countDownLatch.await());
326+
327+
relpConnectionPool.close();
328+
329+
Assertions.assertEquals(testCycles*2, messageList.size());
330+
331+
Pattern heyPattern = Pattern.compile("hey this is batched relp \\d \\d+");
332+
while(!messageList.isEmpty()) {
333+
byte[] payload = messageList.removeFirst();
334+
Assertions.assertTrue(heyPattern.matcher(new String(payload, StandardCharsets.UTF_8)).matches());
335+
}
336+
337+
Assertions.assertTrue(connectionOpenCount.get() > 1);
338+
Assertions.assertEquals(connectionOpenCount.get(), connectionCleanCloseCount.get());
339+
connectionOpenCount.set(0);
340+
connectionCleanCloseCount.set(0);
341+
}
284342
}

0 commit comments

Comments
 (0)