Skip to content

Commit

Permalink
Connection pool (#70)
Browse files Browse the repository at this point in the history
* add pool from lsh_01

* RenewableRelpConnection

* move managed client code to com.teragrep.rlp_01.client

* add PoolTest

* improve PoolTest

* extract Pool interface from UnboundPool

* refactor RelpConnectionFactory to support SSLEngine provision

* remove unused import from RelpConnectionFactory

* SendMessageTest from rlp_03, it rather belongs here

* test connection pooling

* include cleanup to ManagedConnectionTest

* change Period to Duration in RelpConfig

* add testPooledRenewedConnections and testPooledReboundConnections

* change rebound managed connections to disconnect cleanly by adding new method reconnect() to IManagedRelpConnection, improve tests

* change assertAll -> assertDoesNotThrow. in SendMessageTest use AfterEach for messageList cleanup

* remove Thread.sleep from trace-logging in SendMessageTest
  • Loading branch information
kortemik authored Nov 21, 2024
1 parent 65c8f55 commit c1d2a4d
Show file tree
Hide file tree
Showing 20 changed files with 1,452 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
<scope>test</scope>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>com.teragrep</groupId>
<artifactId>rlp_03</artifactId>
<version>9.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Java Reliable Event Logging Protocol Library RLP-01
Copyright (C) 2021-2024 Suomen Kanuuna Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.teragrep.rlp_01.client;

import com.teragrep.rlp_01.pool.Poolable;

import java.io.IOException;

public interface IManagedRelpConnection extends Poolable {
void reconnect();

void connect() throws IOException;
void forceReconnect();
void ensureSent(byte[] bytes);
}
58 changes: 58 additions & 0 deletions src/main/java/com/teragrep/rlp_01/client/IRelpConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Java Reliable Event Logging Protocol Library RLP-01
Copyright (C) 2021-2024 Suomen Kanuuna Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.teragrep.rlp_01.client;

import com.teragrep.rlp_01.RelpBatch;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

// TODO refactor RelpConnection into an interface and RelpConnectionImpl and remove this
public interface IRelpConnection {

int getReadTimeout();

void setReadTimeout(int readTimeout);

int getWriteTimeout();

void setWriteTimeout(int writeTimeout);

int getConnectionTimeout();

void setConnectionTimeout(int timeout);

void setKeepAlive(boolean on);

int getRxBufferSize();

void setRxBufferSize(int size);

int getTxBufferSize();

void setTxBufferSize(int size);

boolean connect(String hostname, int port) throws IOException, IllegalStateException, TimeoutException;

void tearDown();

boolean disconnect() throws IOException, IllegalStateException, TimeoutException;

void commit(RelpBatch relpBatch) throws IOException, IllegalStateException, TimeoutException;

RelpConfig relpConfig();
}
125 changes: 125 additions & 0 deletions src/main/java/com/teragrep/rlp_01/client/ManagedRelpConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Java Reliable Event Logging Protocol Library RLP-01
Copyright (C) 2021-2024 Suomen Kanuuna Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.teragrep.rlp_01.client;

import com.teragrep.rlp_01.RelpBatch;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ManagedRelpConnection implements IManagedRelpConnection {

private final IRelpConnection relpConnection;
private boolean hasConnected;


public ManagedRelpConnection(IRelpConnection relpConnection) {
this.relpConnection = relpConnection;
this.hasConnected = false;
}

@Override
public void forceReconnect() {
tearDown();
connect();
}

@Override
public void reconnect() {
close();
connect();
}

@Override
public void connect() {
boolean connected = false;
while (!connected) {
try {
this.hasConnected = true;
connected = relpConnection
.connect(relpConnection.relpConfig().relpTarget, relpConnection.relpConfig().relpPort);
}
catch (Exception e) {
System.err.println(
"Failed to connect to relp server <["+relpConnection.relpConfig().relpTarget+"]>:<["+relpConnection.relpConfig().relpPort+"]>: <"+e.getMessage()+">");

try {
Thread.sleep(relpConnection.relpConfig().relpReconnectInterval);
}
catch (InterruptedException exception) {
System.err.println("Reconnection timer interrupted, reconnecting now");
}
}
}
}

private void tearDown() {
/*
TODO remove: wouldn't need a check hasConnected but there is a bug in RLP-01 tearDown()
see https://github.com/teragrep/rlp_01/issues/63 for further info
*/
if (hasConnected) {
relpConnection.tearDown();
}
}

@Override
public void ensureSent(byte[] bytes) {
// avoid unnecessary exception for fresh connections
if (!hasConnected) {
connect();
}

final RelpBatch relpBatch = new RelpBatch();
relpBatch.insert(bytes);
boolean notSent = true;
while (notSent) {
try {
relpConnection.commit(relpBatch);
}
catch (IllegalStateException | IOException | TimeoutException e) {
System.err.println("Exception <"+e.getMessage()+"> while sending relpBatch. Will retry");
}
if (!relpBatch.verifyTransactionAll()) {
relpBatch.retryAllFailed();
this.tearDown();
this.connect();
}
else {
notSent = false;
}
}
}

@Override
public boolean isStub() {
return false;
}

@Override
public void close() {
try {
this.relpConnection.disconnect();
}
catch (IllegalStateException | IOException | TimeoutException e) {
System.err.println("Forcefully closing connection due to exception <"+e.getMessage()+">");
}
finally {
tearDown();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Java Reliable Event Logging Protocol Library RLP-01
Copyright (C) 2021-2024 Suomen Kanuuna Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.teragrep.rlp_01.client;

import java.io.IOException;

public class ManagedRelpConnectionStub implements IManagedRelpConnection {

@Override
public void reconnect() {
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
}

@Override
public void connect() throws IOException {
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
}

@Override
public void forceReconnect() {
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
}

@Override
public void ensureSent(byte[] bytes) {
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
}

@Override
public boolean isStub() {
return true;
}

@Override
public void close() {
throw new IllegalStateException("ManagedRelpConnectionStub does not support this");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Java Reliable Event Logging Protocol Library RLP-01
Copyright (C) 2021-2024 Suomen Kanuuna Oy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.teragrep.rlp_01.client;

import java.io.IOException;

public class RebindableRelpConnection implements IManagedRelpConnection {
private final IManagedRelpConnection managedRelpConnection;
private int recordsSent;
private final int rebindRequestAmount;

public RebindableRelpConnection(IManagedRelpConnection managedRelpConnection, int rebindRequestAmount) {
this.managedRelpConnection = managedRelpConnection;
this.recordsSent = 0;
this.rebindRequestAmount = rebindRequestAmount;
}

@Override
public void reconnect() {
managedRelpConnection.reconnect();
}

@Override
public void connect() throws IOException {
managedRelpConnection.connect();
}

@Override
public void forceReconnect() {
managedRelpConnection.forceReconnect();
}

@Override
public void ensureSent(byte[] bytes) {
if (recordsSent >= rebindRequestAmount) {
reconnect();
recordsSent = 0;
}
managedRelpConnection.ensureSent(bytes);
recordsSent++;
}

@Override
public boolean isStub() {
return false;
}

@Override
public void close() throws IOException {
managedRelpConnection.close();
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/teragrep/rlp_01/client/RelpConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.teragrep.rlp_01.client;

import java.time.Duration;

public class RelpConfig {
public final String relpTarget;
public final int relpPort;
public final int relpReconnectInterval;
public final int rebindRequestAmount;
public final boolean rebindEnabled;
public final Duration maxIdle;
public final boolean maxIdleEnabled;

public RelpConfig(String relpTarget, int relpPort, int relpReconnectInterval, int rebindRequestAmount, boolean rebindEnabled, Duration maxIdle, boolean maxIdleEnabled) {
this.relpTarget = relpTarget;
this.relpPort = relpPort;
this.relpReconnectInterval = relpReconnectInterval;
this.rebindRequestAmount = rebindRequestAmount;
this.rebindEnabled = rebindEnabled;
this.maxIdle = maxIdle;
this.maxIdleEnabled = maxIdleEnabled;
}
}
Loading

0 comments on commit c1d2a4d

Please sign in to comment.