Support heart beat timeout, tidy up free buffer

This commit is contained in:
Rob Bygrave
2014-01-16 00:28:00 +13:00
parent 4900a460d1
commit 5f4789f9ba
13 changed files with 464 additions and 378 deletions
@@ -7,7 +7,6 @@ import java.sql.ResultSet;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebeaninternal.server.core.DefaultBackgroundExecutor;
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.Status;
@@ -22,43 +21,45 @@ public class TestDataSourceMax extends BaseTestCase {
if (skipThisTest) {
return;
}
Ebean.getServer(null);
String name = "h2";
String name = "mysql";
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.loadSettings(name);
dsConfig.setMinConnections(2);
dsConfig.setMaxConnections(8);
dsConfig.setMaxConnections(25);
dsConfig.setWaitTimeoutMillis(30000);
dsConfig.setCaptureStackTrace(true);
DataSourcePool pool = new DataSourcePool(null, name, dsConfig);
// Assert.assertEquals(3, pool.getMaxSize());
DefaultBackgroundExecutor bg = new DefaultBackgroundExecutor(10, 2, 180, 30, "testDs");
//pool.checkDataSource();
// if (true) {
// pool.shutdown(false);
// return;
// }
DefaultBackgroundExecutor bg = new DefaultBackgroundExecutor(1, 2, 180, 30, "testDs");
try {
for (int i = 0; i < 12; i++) {
// Thread.sleep(10*i);
bg.execute(new ConnRunner(pool, 4000));
bg.execute(new ConnRunner(pool, 4000, i));
}
System.out.println("main thread sleep ... " + pool.getStatus(false));
Thread.sleep(10000);
pool.getStatistics(true);
Thread.sleep(30000);
Status status = pool.getStatus(false);
System.out.println(status);
pool.shutdown(false);
// this dumpOrder was for 3 vectors used in PooledConnectionQueue
// that logged the order of wait, notify and obtain events
// I have remove that code.
// String s = pool.dumpOrder();
// System.err.println(s);
} catch (Exception e) {
e.printStackTrace();
@@ -70,24 +71,38 @@ public class TestDataSourceMax extends BaseTestCase {
final DataSourcePool pool;
final long sleepMillis;
final int position;
ConnRunner(DataSourcePool pool, long sleepMillis) {
ConnRunner(DataSourcePool pool, long sleepMillis, int position) {
this.pool = pool;
this.sleepMillis = sleepMillis;
this.position = position;
}
private void waitSomeTime(long count) {
try {
System.out.println(position+" sleep " + sleepMillis+" count:"+count);
Thread.sleep(sleepMillis);
System.out.println(position+" sleep done");
} catch (InterruptedException e){
throw new RuntimeException(e);
}
}
public void run() {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
long count = -1;
try {
connection = pool.getConnection();
pstmt = connection.prepareStatement("select count(*) from o_customer");
rset = pstmt.executeQuery();
System.out.println("sleep " + sleepMillis);
Thread.sleep(sleepMillis);
System.out.println("sleep done");
while (rset.next()) {
// do nothing actually
count = rset.getLong(1);
}
} catch (Exception ex) {
ex.printStackTrace();
@@ -113,6 +128,7 @@ public class TestDataSourceMax extends BaseTestCase {
}
}
}
waitSomeTime(count);
}
}
}
@@ -0,0 +1,70 @@
package com.avaje.ebeaninternal.server.lib.sql;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebeaninternal.server.core.DefaultBackgroundExecutor;
import com.avaje.tests.model.basic.Customer;
public class TestDataSourceMaxWithEntity extends BaseTestCase {
@Test
public void test() {
boolean skipThisTest = true;
if (skipThisTest) {
return;
}
EbeanServer server = Ebean.getServer(null);
DefaultBackgroundExecutor bg = new DefaultBackgroundExecutor(1, 2, 180, 30, "testDs");
try {
for (int i = 0; i < 12; i++) {
// Thread.sleep(10*i);
bg.execute(new ConnRunner(server, 4000, i));
}
System.out.println("main thread sleep ... ");
Thread.sleep(30000);
server.shutdown(true, false);
} catch (Exception e) {
e.printStackTrace();
}
}
private static class ConnRunner implements Runnable {
final EbeanServer server;
final long sleepMillis;
final int position;
ConnRunner(EbeanServer server, long sleepMillis, int position) {
this.server = server;
this.sleepMillis = sleepMillis;
this.position = position;
}
public void run() {
server.find(Customer.class).findRowCount();
try {
System.out.println(position+" sleep " + sleepMillis);
Thread.sleep(sleepMillis);
System.out.println(position+" sleep done");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@@ -10,14 +10,13 @@ public class TestFreeBuffer extends BaseTestCase {
@Test
public void test() {
FreeConnectionBuffer b = new FreeConnectionBuffer(3);
FreeConnectionBuffer b = new FreeConnectionBuffer();
PooledConnection p0 = new PooledConnection("0");
PooledConnection p1 = new PooledConnection("1");
PooledConnection p2 = new PooledConnection("2");
// PooledConnection p3 = new PooledConnection("3");
Assert.assertEquals(3, b.getCapacity());
Assert.assertEquals(0, b.size());
Assert.assertEquals(true, b.isEmpty());
@@ -7,44 +7,21 @@ import org.mockito.Mockito;
import com.avaje.ebean.BaseTestCase;
public class TestFreeBufferTrim extends BaseTestCase {
@Test
public void test() {
FreeConnectionBuffer b = new FreeConnectionBuffer(3);
Assert.assertEquals(0, b.size());
PooledConnection p0 = Mockito.mock(PooledConnection.class);
PooledConnection p1 = Mockito.mock(PooledConnection.class);
PooledConnection p2 = Mockito.mock(PooledConnection.class);
b.add(p0);
b.add(p1);
b.add(p2);
Assert.assertEquals(3, b.size());
// add 1 second as this is going to fast for System.currentTimeMillis()
long now = System.currentTimeMillis()+1000;
int trimCount = b.trim(now);
Assert.assertEquals(0, b.size());
Assert.assertEquals(3, trimCount);
}
@Test
public void testWithTime() {
FreeConnectionBuffer b = new FreeConnectionBuffer(3);
FreeConnectionBuffer b = new FreeConnectionBuffer();
Assert.assertEquals(0, b.size());
PooledConnection p0 = Mockito.mock(PooledConnection.class);
Mockito.when(p0.getLastUsedTime()).thenReturn(1000l);
Mockito.when(p0.shouldTrim(1500, 0)).thenReturn(true);
PooledConnection p1 = Mockito.mock(PooledConnection.class);
Mockito.when(p1.getLastUsedTime()).thenReturn(2000l);
Mockito.when(p1.shouldTrim(1500, 0)).thenReturn(true);
PooledConnection p2 = Mockito.mock(PooledConnection.class);
Mockito.when(p2.getLastUsedTime()).thenReturn(1100l);
Mockito.when(p2.shouldTrim(1500, 0)).thenReturn(false);
b.add(p0);
b.add(p1);
@@ -52,7 +29,7 @@ public class TestFreeBufferTrim extends BaseTestCase {
Assert.assertEquals(3, b.size());
int trimCount = b.trim(1500);
int trimCount = b.trim(1500, 0);
Assert.assertEquals(1, b.size());
Assert.assertEquals(2, trimCount);