Skip to content

Commit 71de3ea

Browse files
committed
Add affinity support
1 parent c7f0922 commit 71de3ea

9 files changed

Lines changed: 365 additions & 45 deletions

File tree

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourceBuilder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Properties;
88
import java.util.function.BooleanSupplier;
99
import java.util.function.Consumer;
10+
import java.util.function.Supplier;
1011

1112
/**
1213
* Builder for DataSourcePool.
@@ -639,6 +640,26 @@ default DataSourceBuilder customProperties(Map<String, String> customProperties)
639640
*/
640641
DataSourceBuilder addProperty(String key, int value);
641642

643+
/**
644+
* sets the affinity-size (internal hashmap of distinct affinity keys). Should be a prime number. Default: 257
645+
*/
646+
DataSourceBuilder affinitySize(int affinitySize);
647+
648+
/**
649+
* Returns the affinity size.
650+
*/
651+
int getAffinitySize();
652+
653+
/**
654+
* Sets the affinity provider. e.g. Thread::currentThread.
655+
*/
656+
DataSourceBuilder affinityProvider(Supplier<Object> affinityProvider);
657+
658+
/**
659+
* Returns the affinity provider.
660+
*/
661+
Supplier<Object> getAffinityProvider();
662+
642663
/**
643664
* Set the database owner username (used to create connection for use with InitDatabase).
644665
*/

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourceConfig.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.Properties;
1111
import java.util.function.Consumer;
12+
import java.util.function.Supplier;
1213

1314
/**
1415
* Configuration information for a DataSource.
@@ -84,6 +85,8 @@ public class DataSourceConfig implements DataSourceBuilder.Settings {
8485
private boolean shutdownOnJvmExit;
8586
private boolean validateOnHeartbeat = !System.getenv().containsKey("LAMBDA_TASK_ROOT");
8687
private boolean enforceCleanClose;
88+
private int affinitySize = 257;
89+
private Supplier<Object> affinityProvider;
8790

8891
@Override
8992
public Settings settings() {
@@ -146,6 +149,8 @@ public DataSourceConfig copy() {
146149
copy.alert = alert;
147150
copy.listener = listener;
148151
copy.enforceCleanClose = enforceCleanClose;
152+
copy.affinitySize = affinitySize;
153+
copy.affinityProvider = affinityProvider;
149154
return copy;
150155
}
151156

@@ -654,6 +659,28 @@ public DataSourceConfig addProperty(String key, int value) {
654659
return addProperty(key, Integer.toString(value));
655660
}
656661

662+
@Override
663+
public DataSourceBuilder affinitySize(int affinitySize) {
664+
this.affinitySize = affinitySize;
665+
return this;
666+
}
667+
668+
@Override
669+
public int getAffinitySize() {
670+
return affinitySize;
671+
}
672+
673+
@Override
674+
public DataSourceBuilder affinityProvider(Supplier<Object> affinityProvider) {
675+
this.affinityProvider = affinityProvider;
676+
return this;
677+
}
678+
679+
@Override
680+
public Supplier<Object> getAffinityProvider() {
681+
return affinityProvider;
682+
}
683+
657684
@Override
658685
public String getOwnerUsername() {
659686
return ownerUsername;
@@ -801,6 +828,7 @@ private void loadSettings(ConfigPropertiesHelper properties) {
801828
shutdownOnJvmExit = properties.getBoolean("shutdownOnJvmExit", shutdownOnJvmExit);
802829
validateOnHeartbeat = properties.getBoolean("validateOnHeartbeat", validateOnHeartbeat);
803830
enforceCleanClose = properties.getBoolean("enforceCleanClose", enforceCleanClose);
831+
affinitySize = properties.getInt("affinityCacheSize", affinitySize);
804832

805833

806834
String isoLevel = properties.get("isolationLevel", _isolationLevel(isolationLevel));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.ebean.datasource;
2+
3+
import java.sql.Connection;
4+
5+
/**
6+
* Interface for connection objects returned from the ebean-datasource connection pool
7+
*
8+
* @author Roland Praml, Foconis Analytics GmbH
9+
*/
10+
public interface DataSourceConnection extends Connection {
11+
12+
/**
13+
* Returns the affinity-ID, this connection was assigned to.
14+
*/
15+
Object affinityId();
16+
17+
}

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourcePool.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ static DataSourceBuilder builder() {
8181
*/
8282
void offline();
8383

84+
/**
85+
* Returns a connection for given affinity ID. It is guaranteed, that connection.affinityId in listener etc.
86+
* is the same object.
87+
*/
88+
DataSourceConnection getConnection(Object affinityId) throws SQLException;
89+
8490
/**
8591
* Shutdown the pool.
8692
* <p>

0 commit comments

Comments
 (0)