Skip to content

Commit

Permalink
increase db connection pool, destroy outdated db session faster (afte…
Browse files Browse the repository at this point in the history
…r 1/2h instead of 3h)
  • Loading branch information
rbudde committed Sep 4, 2020
1 parent 95490fd commit df86bc0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
7 changes: 6 additions & 1 deletion Docker/_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ The shell script `$SCRIPT_DIR/run.sh` has commands, that are used for operating
```bash
bash <SCRIPT_DIR>/run.sh -q auto-restart <server-name> <server-url>
```
The command used for initiating 'auto-restart' for the prod server and looking up the pid (if not found in the log-file) are:
```bash
/bin/bash /data/openroberta-lab/scripts/run.sh -q auto-restart master http://me-roblab-prod:8080
ps -AfL | fgrep auto-restart
```

* `alive`: usually called from cron. It takes a server URL and checks whether the server is alive. It sends by default mail to admins.
Use `crontab -e` to add the following line to the crontab. call <SCRIPT_DIR>/run.sh -help to learn about other parameters of the call:
Expand All @@ -296,7 +301,7 @@ bash <SCRIPT_DIR>/run.sh -q auto-restart <server-name> <server-url>
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: openrobertalab service
# Description: Start, stop and restart the database server and the openroberta server declared in server/servers.txt
# Description: Start, stop and restart the database server and the openroberta server
### END INIT INFO

# Author: rbudde
Expand Down
2 changes: 1 addition & 1 deletion Docker/openroberta/scripts/hotfix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo '
# For deployments, two parameters are mandatory (both WITHOUT SNAPSHOT) #
# 1. the version number of the hotfix to be deployed at master (likely the version from develop without -SNAPSHOT) #
# 2. the next version to be set in develop (the script suffixes this with "-SNAPSHOT" internally) #
# E.g. ./hotfix.sh 2.4.1 2.5.0 #
# E.g. ./hotfix.sh 4.0.2 4.0.3 #
# #
# ASSUMPTIONS: #
# - maven project, may be hierarchical (parent + modules). mvn is used and on the PATH! #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException>
@Override
public Response toResponse(RuntimeException e) {
try {
LOG.error("server error - exception: " + e.getMessage().substring(0, 60), e);
String errorMessage = e.getMessage();
errorMessage = (errorMessage == null) ? "-no error message in exception-" : errorMessage.substring(0, 60);
LOG.error("server error - exception: " + errorMessage, e);
final BaseResponse response = BaseResponse.make();
String keyAsString = Key.SERVER_ERROR.toString();
response.setRc("error").setMessage(keyAsString).setCause(keyAsString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public class ServerStarter {
private static final String ADMIN_DIR_KEY = "server.admin.dir=";
private static final String LOG_LEVEL_KEY = "server.log.level=";

private static final long INTERVAL_TO_CHECK_EXPIRATIONS_SEC = TimeUnit.HOURS.toSeconds(3);
private static final long INTERVAL_HTTPSESSION_EXPIRE_SEC = TimeUnit.HOURS.toSeconds(3);
private static final long INTERVAL_DB_SESSION_EXPIRE_SEC = TimeUnit.MINUTES.toSeconds(30);

private final ServerProperties serverProperties; // for the startup
private Injector injector;
Expand Down Expand Up @@ -345,7 +346,7 @@ private IIpToCountry configureIpToCountryDb() {
}

/**
* configure a thread, that runs every 3 hours a service, that will remove expired HttpSessionState objects
* configure a thread, that runs periodically a service, that will remove expired HttpSessionState objects
*/
private void configureHttpSessionStateCleanup() {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Expand All @@ -355,11 +356,11 @@ public void run() {
HttpSessionState.removeExpired();
}
};
scheduler.scheduleAtFixedRate(cleanupHttpSessionState, INTERVAL_TO_CHECK_EXPIRATIONS_SEC + 1, INTERVAL_TO_CHECK_EXPIRATIONS_SEC, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(cleanupHttpSessionState, INTERVAL_HTTPSESSION_EXPIRE_SEC + 1, INTERVAL_HTTPSESSION_EXPIRE_SEC, TimeUnit.SECONDS);
}

/**
* configure a thread, that runs every 3 hours a service, that will remove expired database sessions (sessions older than 2 hours)
* configure a thread, that runs periodically a service, that will remove expired database sessions
*/
private void configureDbSessionCleanup() {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Expand All @@ -369,7 +370,7 @@ public void run() {
DbSession.cleanupSessions();
}
};
scheduler.scheduleAtFixedRate(cleanupDbSession, INTERVAL_TO_CHECK_EXPIRATIONS_SEC + 2, INTERVAL_TO_CHECK_EXPIRATIONS_SEC, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(cleanupDbSession, INTERVAL_DB_SESSION_EXPIRE_SEC + 2, INTERVAL_DB_SESSION_EXPIRE_SEC, TimeUnit.SECONDS);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
*/
public class DbSession {
private static final Logger LOG = LoggerFactory.getLogger(DbSession.class);
/**
* if a db session is older than this value, it will be logged as a potential resource misuse
*/
private static final long DURATION_TIMEOUT_MSEC_FOR_LOGGING = TimeUnit.SECONDS.toMillis(5);
private static final long DURATION_TIMEOUT_MSEC_FOR_CLEANUP = TimeUnit.HOURS.toMillis(2);
private static final int NUMBER_OF_SESSIONS_TO_SHOW = 50;
/**
* if a db session is older than this value, it will be closed and removed to avoid a resource leak
*/
private static final long DURATION_TIMEOUT_MSEC_FOR_CLEANUP = TimeUnit.MINUTES.toMillis(5);

private Session session;

Expand Down Expand Up @@ -68,13 +73,17 @@ public class DbSession {
* rollback the current transaction
*/
public void rollback() {
LOG.info("rollback");
addTransaction("rollback"); // for analyzing db session usage.
if ( this.session != null ) {
LOG.info("rollback");
Transaction transaction = this.session.getTransaction();
transaction.rollback();
this.session.close();
this.session = null;
} else {
LOG.info("rollback attempt - session was removed already");
}

Transaction transaction = this.session.getTransaction();
transaction.rollback();
this.session.close();
this.session = null;
}

/**
Expand Down Expand Up @@ -112,12 +121,13 @@ public void close() {
if ( new Date().getTime() - creationTime > DURATION_TIMEOUT_MSEC_FOR_LOGGING ) {
LOG.error("db session " + sessionId + " too old: " + sessionAge + "msec\n" + getFullInfo());
}
currentOpenSessionCounter.decrementAndGet();
if ( this.numberOfActions == 0 ) {
unusedSessionCounter.getAndIncrement();
}
if ( sessionMap.remove(this.sessionId) == null ) {
LOG.error("FATAL: could not remove db session " + this.sessionId);
} else {
currentOpenSessionCounter.decrementAndGet();
}
}

Expand Down Expand Up @@ -224,7 +234,7 @@ private void addSaveOrDelete(String cmd, Object object) {
}

/**
* remove (cleanup) all those db sessions, that are outdated. This is a VERY dangerous operation!
* rollback and remove all those db sessions, that are outdated (older than {@link #DURATION_TIMEOUT_MSEC_FOR_CLEANUP}). This is a VERY dangerous operation!
*/
public static void cleanupSessions() {
final long now = new Date().getTime();
Expand All @@ -248,7 +258,7 @@ public static void cleanupSessions() {
}
}
if ( !somethingExpired ) {
LOG.info("no expired database session");
LOG.info("no sessions expired");
}
}

Expand Down
2 changes: 1 addition & 1 deletion OpenRobertaServer/src/main/resources/hibernate-cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<property name="connection.autocommit">false</property>

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.max_size">200</property>
<property name="hibernate.c3p0.timeout">700</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.max_statements">100</property>
Expand Down

0 comments on commit df86bc0

Please sign in to comment.