-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
20 changed files
with
1,452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/teragrep/rlp_01/client/IManagedRelpConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
src/main/java/com/teragrep/rlp_01/client/IRelpConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
125
src/main/java/com/teragrep/rlp_01/client/ManagedRelpConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/teragrep/rlp_01/client/ManagedRelpConnectionStub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/teragrep/rlp_01/client/RebindableRelpConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.