Skip to content

Commit b202bc0

Browse files
authored
Merge pull request #9 from little-pan/v0.3.29-res-limit
1) fix process idle check bug; 2) add process idle test case
2 parents d4cabf2 + 75bed84 commit b202bc0

6 files changed

Lines changed: 121 additions & 44 deletions

File tree

src/main/java/org/sqlite/server/SQLiteServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public abstract class SQLiteServer implements AutoCloseable {
8282
public static final int OPEN_TIMEOUT_DEFAULT = 30000;
8383
public static final long MAX_ALLOWED_PACKET_DEFAULT = 16L << 20;
8484
public static final int SLEEP_TIMEOUT_DEFAULT = 300000;
85-
public static final int SLEEP_IN_TX_TIMEOUT_DEFAULT = 30000;
85+
public static final int SLEEP_IN_TX_TIMEOUT_DEFAULT = 60000;
8686
// SQLite settings
8787
public static final int BUSY_TIMEOUT_DEFAULT = 50000;
8888
public static final JournalMode JOURNAL_MODE_DEFAULT = JournalMode.WAL;

src/main/java/org/sqlite/server/SQLiteWorker.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,18 @@ public void run() {
168168
long lastIdleCheck = 0L, idleCheckIntv = -1L;
169169

170170
for (; !isStopped() || processors.size() > 0;) {
171-
final long curr = currentTimeMillis();
172-
long timeout = minSelectTimeout(curr);
171+
final long curr = currentTimeMillis(), timeout;
173172
int n;
174173

175174
// Idle check
176175
if (curr - lastIdleCheck >= idleCheckIntv) {
177176
lastIdleCheck = curr;
178177
processIdle(curr);
179178
idleCheckIntv = idleCheckInterval();
180-
if (timeout < 0L) {
181-
timeout = idleCheckIntv;
182-
}
183179
}
184180

185181
// Do select
182+
timeout = minSelectTimeout(curr, idleCheckIntv);
186183
if (timeout < 0L) {
187184
n = this.selector.select();
188185
} else if (timeout == 0L) {
@@ -337,16 +334,14 @@ protected void processIdle(final long curr) {
337334
state.unlock();
338335
}
339336

340-
if (timeout > 0L) {
341-
if (curr - start > timeout) {
342-
try {
343-
p.sendErrorResponse(message, "53400");
344-
} catch (IOException e) {
345-
// ignore
346-
} finally {
347-
IoUtils.close(p.getConnection());
348-
p.stop();
349-
}
337+
if (timeout > 0L && curr - start > timeout) {
338+
try {
339+
p.sendErrorResponse(message, "53400");
340+
} catch (IOException e) {
341+
// ignore
342+
} finally {
343+
IoUtils.close(p.getConnection());
344+
p.stop();
350345
}
351346
}
352347

@@ -462,7 +457,7 @@ protected long idleCheckInterval() {
462457
return (timeout == 0L? -1L: timeout);
463458
}
464459

465-
protected long minSelectTimeout(final long curr) {
460+
protected long minSelectTimeout(final long curr, final long idleCheckIntv) {
466461
SlotAllocator<SQLiteProcessor> busyProcs = this.busyProcs;
467462
long timeout = -1L;
468463

@@ -501,6 +496,10 @@ protected long minSelectTimeout(final long curr) {
501496
}
502497
}
503498

499+
if (timeout < 0L) {
500+
timeout = idleCheckIntv;
501+
}
502+
504503
return timeout;
505504
}
506505

src/test/java/org/sqlite/TestAll.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121

2222
import org.junit.Test;
23+
import org.sqlite.server.ProcessIdleTest;
2324
import org.sqlite.server.SQLiteServerTest;
2425
import org.sqlite.server.jdbc.ConnectionTest;
2526
import org.sqlite.server.jdbc.PreparedStatementTest;
@@ -78,6 +79,7 @@ protected TestAll addAll() {
7879
add(new DateTimeUtilsTest()).
7980
add(new HibernateTest()).
8081
add(new PreparedStatementTest()).
82+
add(new ProcessIdleTest()).
8183
add(new SpinLockTest()).
8284
add(new StatementTest()).
8385
add(new SQLReaderTest()).

src/test/java/org/sqlite/TestDbBase.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,53 +43,53 @@ public abstract class TestDbBase extends TestBase {
4343
protected static String password = "123456";
4444

4545
protected static final String [] environments = {
46-
"SQLite WAL pg extended query environment", "SQLite DELETE pg extended query environment",
47-
"SQLite WAL pg simple query environment", "SQLite DELETE pg simple query environment",
46+
"SQLite WAL pg extended query environment", //"SQLite DELETE pg extended query environment",
47+
"SQLite WAL pg simple query environment", //"SQLite DELETE pg simple query environment",
4848
};
4949

5050
protected static final String [] urls = {
5151
"jdbc:postgresql://localhost:"+getPortDefault()+"/"+getDbDefault()+
5252
"?preferQueryMode=extended&socketFactory=org.sqlite.server.jdbc.pg.PgSocketFactory"
5353
,//"&loggerLevel=TRACE&loggerFile=./logs/pgjdbc.log",
54-
"jdbc:postgresql://localhost:"+getPortDefault()+"/"+getDbDefault()+
55-
"?preferQueryMode=extended&socketFactory=org.sqlite.server.jdbc.pg.PgSocketFactory"
56-
,//"&loggerLevel=TRACE&loggerFile=./logs/pgjdbc.log",
57-
"jdbc:postgresql://localhost:"+getPortDefault()+"/"+getDbDefault()+
58-
"?preferQueryMode=simple&socketFactory=org.sqlite.server.jdbc.pg.PgSocketFactory"
59-
,//"&loggerLevel=TRACE&loggerFile=./logs/pgjdbc.log",
54+
//"jdbc:postgresql://localhost:"+getPortDefault()+"/"+getDbDefault()+
55+
// "?preferQueryMode=extended&socketFactory=org.sqlite.server.jdbc.pg.PgSocketFactory"
56+
// ,//"&loggerLevel=TRACE&loggerFile=./logs/pgjdbc.log",
6057
"jdbc:postgresql://localhost:"+getPortDefault()+"/"+getDbDefault()+
6158
"?preferQueryMode=simple&socketFactory=org.sqlite.server.jdbc.pg.PgSocketFactory"
6259
,//"&loggerLevel=TRACE&loggerFile=./logs/pgjdbc.log",
60+
//"jdbc:postgresql://localhost:"+getPortDefault()+"/"+getDbDefault()+
61+
// "?preferQueryMode=simple&socketFactory=org.sqlite.server.jdbc.pg.PgSocketFactory"
62+
// ,//"&loggerLevel=TRACE&loggerFile=./logs/pgjdbc.log",
6363
};
6464

6565
protected static final String [][] initArgsList = new String[][] {
6666
{"-D", dataDir, "-p", password, "--journal-mode", "wal"},
67-
{"-D", dataDir, "-p", password, "--journal-mode", "delete",
68-
"-S", "normal"
69-
},
67+
//{"-D", dataDir, "-p", password, "--journal-mode", "delete",
68+
// "-S", "normal"
69+
//},
7070
{"-D", dataDir, "-p", password, "--journal-mode", "wal"},
71-
{"-D", dataDir, "-p", password, "--journal-mode", "delete",
72-
"-S", "normal"
73-
},
71+
//{"-D", dataDir, "-p", password, "--journal-mode", "delete",
72+
// "-S", "normal"
73+
//},
7474
};
7575

7676
protected static final String [][] bootArgsList = new String[][] {
7777
{"-D", dataDir, //"--trace-error", "-T",
7878
"--worker-count", "4", "--max-conns", "50",
79-
"--journal-mode", "wal", "--max-allowed-packet", "0",
80-
},
81-
{"-D", dataDir, //"--trace-error", "-T",
82-
"--worker-count", "4", "--max-conns", "50",
83-
"--journal-mode", "delete", "-S", "normal",
79+
"--journal-mode", "wal", "--max-allowed-packet", "0",
8480
},
81+
//{"-D", dataDir, //"--trace-error", "-T",
82+
// "--worker-count", "4", "--max-conns", "50",
83+
// "--journal-mode", "delete", "-S", "normal",
84+
//},
8585
{"-D", dataDir, //"--trace-error", "-T",
8686
"--worker-count", "4", "--max-conns", "50",
87-
"--journal-mode", "wal", "--max-allowed-packet", "0x1000000",
88-
},
89-
{"-D", dataDir, //"--trace-error", "-T",
90-
"--worker-count", "4", "--max-conns", "50",
91-
"--journal-mode", "delete", "-S", "normal", "--max-allowed-packet", "0x10000",
87+
"--journal-mode", "wal", "--max-allowed-packet", "0x1000000",
9288
},
89+
//{"-D", dataDir, //"--trace-error", "-T",
90+
// "--worker-count", "4", "--max-conns", "50",
91+
// "--journal-mode", "delete", "-S", "normal", "--max-allowed-packet", "0x10000",
92+
//},
9393
};
9494

9595
protected DbTestEnv currentEnv;
@@ -335,6 +335,11 @@ public void close() {
335335

336336
super.close();
337337
}
338+
339+
public long getSleepInTxTimeout() {
340+
return this.server.getSleepInTxTimeout();
341+
}
342+
338343
}
339344

340345
protected static class DbTestEnvIterator implements Iterator<TestEnv> {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2019 little-pan. A SQLite server based on the C/S architecture.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.sqlite.server;
17+
18+
import java.sql.Connection;
19+
import java.sql.SQLException;
20+
import java.sql.Statement;
21+
22+
import org.sqlite.TestDbBase;
23+
24+
/** SQLite server process idle test.
25+
*
26+
* @author little-pan
27+
* @since 2019-12-15
28+
*
29+
*/
30+
public class ProcessIdleTest extends TestDbBase {
31+
32+
public static void main(String[] args) throws SQLException {
33+
new ProcessIdleTest().test();
34+
}
35+
36+
@Override
37+
protected void doTest() throws SQLException {
38+
idleInTxTimeoutTest("select 1");
39+
}
40+
41+
protected void idleInTxTimeoutTest(String sql) throws SQLException {
42+
try (Connection conn = getConnection()) {
43+
conn.setAutoCommit(false);
44+
Statement s = conn.createStatement();
45+
s.execute(sql);
46+
47+
info("ProcessIdleTest: idle...");
48+
sleep(this.currentEnv.getSleepInTxTimeout() + 1000L);
49+
info("ProcessIdleTest: wakeup");
50+
51+
try {
52+
conn.rollback();
53+
fail("Not closed after idle timeout");
54+
} catch (SQLException e) {
55+
String sqlState = e.getSQLState();
56+
if (!sqlState.startsWith("53")/*On LINUX?*/
57+
&& !sqlState.startsWith("08")/*On windows?*/) {
58+
throw e;
59+
}
60+
}
61+
62+
s.close();
63+
}
64+
}
65+
66+
}

src/test/java/org/sqlite/server/jdbc/TransactionTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public void run() {
141141
boolean failed = true;
142142
try {
143143
// Case-1. "read only" transaction mode in BEGIN
144-
try (Connection conn = getConnection()) {
144+
try (Connection conn = getConnection(true)) {
145+
conn.setAutoCommit(true);
145146
Statement stmt = conn.createStatement();
146147

147148
stmt.executeUpdate("begin read only");
@@ -151,7 +152,8 @@ public void run() {
151152
}
152153

153154
// Case-2. "read only" transaction mode in "SET TRANSACTION"
154-
try (Connection conn = getConnection()) {
155+
try (Connection conn = getConnection(true)) {
156+
conn.setAutoCommit(true);
155157
Statement stmt = conn.createStatement();
156158

157159
stmt.executeUpdate("begin");
@@ -191,7 +193,8 @@ public void run() {
191193
}
192194

193195
// Case-2. "read only" transaction mode in "SET SESSION characteristics as TRANSACTION"
194-
try (Connection conn = getConnection()) {
196+
try (Connection conn = getConnection(true)) {
197+
conn.setAutoCommit(true);
195198
Statement stmt = conn.createStatement();
196199
conn.setReadOnly(true);
197200

@@ -209,6 +212,8 @@ public void run() {
209212
connectionTest(conn, "select 1", "1");
210213
doReadOnlyTxTest(stmt, false);
211214
stmt.execute("rollback");
215+
216+
conn.setReadOnly(false);
212217
}
213218
failed = false;
214219
} catch (SQLException e) {

0 commit comments

Comments
 (0)