Skip to content

Commit d4cabf2

Browse files
authored
Merge pull request #8 from little-pan/v0.3.29-res-limit
Add "--open-timeout" to SQLiteServer command line
2 parents 4779608 + 5e28b66 commit d4cabf2

18 files changed

Lines changed: 329 additions & 121 deletions

bin/build.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ call mvn -f "%SQLITED_HOME%\pom.xml" compile test-compile
7676
call mvn -f "%SQLITED_HOME%\pom.xml" dependency:copy-dependencies -DoutputDirectory="%SQLITED_HOME%\test-lib"
7777

7878
set CLASSPATH=%SQLITED_HOME%\target\classes;%SQLITED_HOME%\target\test-classes;%SQLITED_HOME%\test-lib\*
79-
call "%SQLITED_HOME%\bin\initdb.bat" -D "%SQLITED_HOME%\temp" -p 123456 -d test
79+
set JAVA_OPTS=-Xmx64m
80+
call "%SQLITED_HOME%\bin\sqlited.bat" initdb -D "%SQLITED_HOME%\temp" -p 123456 -d test
8081
if %ERRORLEVEL% NEQ 0 exit /b 1
8182
echo Test initdb ok
8283
call java -Xmx256m -DSQLITED_HOME="%SQLITED_HOME%" org.sqlite.TestAll

bin/build.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ if [ "$TEST_ARG" = "test" ] ; then
6060
if [ ! -d "$SQLITED_HOME/logs" ] ; then mkdir "$SQLITED_HOME/logs" ; fi
6161
if [ ! -d "$SQLITED_HOME/target" ] ; then mkdir "$SQLITED_HOME/target" ; fi
6262

63-
mvn -f "$SQLITED_HOME"/pom.xml package -Dmaven.test.skip=true
64-
mvn -f "$SQLITED_HOME"/pom.xml dependency:copy-dependencies -DincludeScope=compile -DoutputDirectory="$SQLITED_HOME"/lib
65-
cp "$SQLITED_HOME"/target/*.jar "$SQLITED_HOME"/lib
66-
"$SQLITED_HOME"/bin/initdb.sh -D "$SQLITED_HOME"/temp -p 123456 -d test
63+
if [ "$CLEAN_ARG" = "clean" -o ! -f "$SQLITED_HOME"/target/classes/org/sqlite/server/SQLiteServer.class ] ; then
64+
mvn -f "$SQLITED_HOME"/pom.xml compile test-compile
65+
mvn -f "$SQLITED_HOME"/pom.xml dependency:copy-dependencies -DoutputDirectory="$SQLITED_HOME"/test-lib
66+
fi
67+
export CLASSPATH="$SQLITED_HOME"/target/classes:"$SQLITED_HOME"/target/test-classes
68+
for jar in "$SQLITED_HOME"/test-lib/*.jar ; do
69+
export CLASSPATH=$CLASSPATH:$jar
70+
done
71+
72+
"$SQLITED_HOME"/bin/sqlited.sh initdb -D "$SQLITED_HOME"/temp -p 123456 -d test
6773
if [ $? != 0 ] ; then
6874
echo "Test initdb failed!"
6975
exit 1
7076
fi
7177
echo "Test initdb ok"
7278

73-
mvn -f "$SQLITED_HOME"/pom.xml compile test-compile
74-
mvn -f "$SQLITED_HOME"/pom.xml dependency:copy-dependencies -DoutputDirectory="$SQLITED_HOME"/test-lib
75-
CLASSPATH="$SQLITED_HOME"/target/classes:"$SQLITED_HOME"/target/test-classes
76-
for jar in "$SQLITED_HOME"/test-lib/*.jar ; do
77-
CLASSPATH=$CLASSPATH:$jar
78-
done
79-
java -Xmx256m -classpath "$CLASSPATH" -DSQLITED_HOME="$SQLITED_HOME" org.sqlite.TestAll
79+
java -Xmx256m -DSQLITED_HOME="$SQLITED_HOME" org.sqlite.TestAll
8080
if [ $? != 0 ] ; then
8181
echo "Test all test cases failed!"
8282
exit 1

bin/initdb.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ goto end
3636

3737
:okHome
3838
set "EXECUTABLE=%SQLITED_HOME%\bin\sqlited.bat"
39+
set CLASSPATH=
3940
call "%EXECUTABLE%" initdb %*
4041

4142
:end

bin/initdb.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export JAVA_OPTS=-Xmx64m
2424
BIN_DIR=`dirname "$PRG"`
2525
export SQLITED_HOME=`dirname "$BIN_DIR"`
2626
export SQLITED_HOME=`readlink -f "$SQLITED_HOME"`
27+
unset CLASSPATH
2728

2829
exec "$SQLITED_HOME"/bin/sqlited.sh initdb "$@"

bin/sqlited.bat

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ echo This environment variable is needed to run this program
3333
goto end
3434

3535
:okHome
36-
set CLASSPATH=.;%SQLITED_HOME%\lib\*;%SQLITED_HOME%\conf
36+
if "%CLASSPATH%" == "" (
37+
set CLASSPATH=.;%SQLITED_HOME%\lib\*;%SQLITED_HOME%\conf
38+
)
3739
java %JAVA_OPTS% -DSQLITED_HOME="%SQLITED_HOME%" org.sqlite.server.SQLiteServer %*
3840

3941
:end

bin/sqlited.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ BIN_DIR=`dirname "$PRG"`
2323
export SQLITED_HOME=`dirname "$BIN_DIR"`
2424
export SQLITED_HOME=`readlink -f "$SQLITED_HOME"`
2525

26-
CLASSPATH="$SQLITED_HOME"/conf
27-
for jar in "$SQLITED_HOME"/lib/*.jar ; do
28-
CLASSPATH=$CLASSPATH:$jar
29-
done
26+
if [ "$CLASSPATH" = "" ] ; then
27+
export CLASSPATH="$SQLITED_HOME"/conf
28+
for jar in "$SQLITED_HOME"/lib/*.jar ; do
29+
export CLASSPATH=$CLASSPATH:$jar
30+
done
31+
fi
3032

31-
java $JAVA_OPTS -classpath "$CLASSPATH" -DSQLITED_HOME="$SQLITED_HOME" org.sqlite.server.SQLiteServer "$@"
33+
java $JAVA_OPTS -DSQLITED_HOME="$SQLITED_HOME" org.sqlite.server.SQLiteServer "$@"

bin/startup.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ goto end
3636

3737
:okHome
3838
set "EXECUTABLE=%SQLITED_HOME%\bin\sqlited.bat"
39+
set CLASSPATH=
3940
call "%EXECUTABLE%" boot %*
4041

4142
:end

bin/startup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export JAVA_OPTS=-Xmx256m
2424
BIN_DIR=`dirname "$PRG"`
2525
export SQLITED_HOME=`dirname "$BIN_DIR"`
2626
export SQLITED_HOME=`readlink -f "$SQLITED_HOME"`
27+
unset CLASSPATH
2728

2829
exec "$SQLITED_HOME"/bin/sqlited.sh boot "$@"

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ public void setSlot(int slot) throws IllegalStateException {
305305
this.slot = slot;
306306
}
307307

308+
protected SQLiteProcessorState getState() {
309+
return this.state;
310+
}
311+
308312
// SQLContext methods
309313
@Override
310314
public void checkReadOnly(SQLStatement sqlStmt) throws SQLException {
@@ -567,7 +571,7 @@ protected boolean isRunning() throws SQLException {
567571
protected void read() {
568572
try {
569573
if (isRunning()) {
570-
this.state.setState("read data from network");
574+
this.state.startRead();
571575
process();
572576
} else {
573577
this.worker.close(this);
@@ -636,7 +640,6 @@ protected int resetReadBuffer() {
636640

637641
protected void write() {
638642
try {
639-
this.state.setState("flush data into network");
640643
flush();
641644
} catch (Exception e) {
642645
this.server.traceError(log, "flush error", e);
@@ -655,6 +658,7 @@ protected void flush() throws IOException, SQLException {
655658
for (;;) {
656659
ByteBuffer buf = nextWriteBuffer();
657660
if (buf != null) {
661+
this.state.startWrite();
658662
for (; buf.hasRemaining();) {
659663
int n = ch.write(buf);
660664
if (n == 0) {
@@ -666,6 +670,7 @@ protected void flush() throws IOException, SQLException {
666670
}
667671
if (buf.hasRemaining()) {
668672
this.writeQueue.offerFirst(buf);
673+
this.state.startSleep();
669674
return;
670675
}
671676
continue;
@@ -685,7 +690,7 @@ protected void flush() throws IOException, SQLException {
685690
if (rb != null && rb.position() > 0) {
686691
read();
687692
}
688-
this.state.setState("");
693+
this.state.startSleep();
689694
} else {
690695
this.worker.close(this);
691696
}
@@ -871,7 +876,7 @@ public void deleteDbFile(DropDatabaseStatement stmt) throws SQLException {
871876
// Do delete
872877
if (!dbFile.delete()) {
873878
String message = String.format("Can't delete database file of '%s'", dbFile);
874-
trace(log, "{}: {}", message, dbFile);
879+
trace(log, "{}: {}", this, message);
875880
throw convertError(SQLiteErrorCode.SQLITE_IOERR, message);
876881
}
877882
for (String ext: new String[] {"-wal", "-shm", "-journal"}) {
@@ -942,6 +947,9 @@ protected boolean shutdownOutput() {
942947
return false;
943948
}
944949

950+
protected abstract void sendErrorResponse(String message, String sqlState)
951+
throws IOException;
952+
945953
public void stop() {
946954
if (isStopped()) {
947955
return;

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

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@
2828
*/
2929
public class SQLiteProcessorState implements AutoCloseable {
3030

31+
public static final int INIT = 0x0000, AUTH = 0x0001, OPEN = 0x0002,
32+
QUERY = 0x0004, SLEEP = 0x0008, SLEEP_IN_TX = 0x0010,
33+
STOPPED= 0x0020, CLOSED = 0x0040, READ = 0x0080, WRITE= 0x0100;
34+
3135
private final SpinLock lock = new SpinLock();
3236

3337
protected final SQLiteProcessor processor;
3438
protected String command;
3539
protected long startTime;
36-
protected String state = "";
40+
protected int state = INIT;
41+
protected String stateText = "";
3742
protected String sql;
3843

3944
public SQLiteProcessorState(SQLiteProcessor processor) {
4045
this.processor = processor;
4146
this.startTime = System.currentTimeMillis();
42-
this.state = "init";
47+
this.stateText = "init";
4348
}
4449

4550
public SQLiteProcessor getProcessor() {
@@ -99,14 +104,23 @@ public void setStartTime(long startTime) {
99104
}
100105
}
101106

102-
public String getState() {
103-
return state;
107+
public int getState() {
108+
this.lock.lock();
109+
try {
110+
return this.state;
111+
} finally {
112+
this.lock.unlock();
113+
}
104114
}
105115

106-
public void setState(String state) {
116+
public String getStateText() {
117+
return this.stateText;
118+
}
119+
120+
public void setStateText(String stateText) {
107121
this.lock.lock();
108122
try {
109-
this.state = state;
123+
this.stateText = stateText;
110124
} finally {
111125
this.lock.unlock();
112126
}
@@ -130,12 +144,60 @@ public String getInfo() {
130144
return getInfo(false);
131145
}
132146

147+
public void lock() {
148+
this.lock.lock();
149+
}
150+
151+
public void unlock() {
152+
this.lock.unlock();
153+
}
154+
155+
public void startAuth() {
156+
this.lock.lock();
157+
try {
158+
this.command = "Auth";
159+
this.startTime = System.currentTimeMillis();
160+
this.state = AUTH;
161+
this.stateText = "start authentication";
162+
this.sql = null;
163+
} finally {
164+
this.lock.unlock();
165+
}
166+
}
167+
133168
public void startOpen() {
134169
this.lock.lock();
135170
try {
136171
this.command = "Open";
137172
this.startTime = System.currentTimeMillis();
138-
this.state = "open and init database";
173+
this.state = OPEN;
174+
this.stateText = "open and init database";
175+
this.sql = null;
176+
} finally {
177+
this.lock.unlock();
178+
}
179+
}
180+
181+
public void startRead() {
182+
this.lock.lock();
183+
try {
184+
this.command = "Read";
185+
this.startTime = System.currentTimeMillis();
186+
this.state = READ;
187+
this.stateText = "read data from network";
188+
this.sql = null;
189+
} finally {
190+
this.lock.unlock();
191+
}
192+
}
193+
194+
public void startWrite() {
195+
this.lock.lock();
196+
try {
197+
this.command = "Write";
198+
this.startTime = System.currentTimeMillis();
199+
this.state = WRITE;
200+
this.stateText = "flush data into network";
139201
this.sql = null;
140202
} finally {
141203
this.lock.unlock();
@@ -151,19 +213,33 @@ public void startQuery(SQLStatement sqlStatement, String state) {
151213
try {
152214
this.command = "Query";
153215
this.startTime = System.currentTimeMillis();
154-
this.state = state;
216+
this.state = QUERY;
217+
this.stateText = state;
155218
this.sql = sqlStatement.toString();
156219
} finally {
157220
this.lock.unlock();
158221
}
159222
}
160223

161224
public void startSleep() {
225+
boolean inTx = false;
226+
if (this.processor.isOpen()) {
227+
if (this.processor.getConnection() != null) {
228+
inTx = !this.processor.isAutoCommit();
229+
}
230+
}
231+
162232
this.lock.lock();
163233
try {
164234
this.command = "Sleep";
165235
this.startTime = System.currentTimeMillis();
166-
this.state = "";
236+
if (inTx) {
237+
this.state = SLEEP_IN_TX;
238+
this.stateText = "idle in transaction";
239+
} else {
240+
this.state = SLEEP;
241+
this.stateText = "idle";
242+
}
167243
this.sql = null;
168244
} finally {
169245
this.lock.unlock();
@@ -177,6 +253,7 @@ public SQLiteProcessorState copy() {
177253
copy.command = this.command;
178254
copy.startTime = this.startTime;
179255
copy.state = this.state;
256+
copy.stateText = this.stateText;
180257
copy.sql = this.sql;
181258
} finally {
182259
this.lock.unlock();
@@ -190,7 +267,8 @@ public void stop() {
190267
try {
191268
this.command = null;
192269
this.startTime = System.currentTimeMillis();
193-
this.state = "stopped";
270+
this.state = STOPPED;
271+
this.stateText = "stopped";
194272
this.sql = null;
195273
} finally {
196274
this.lock.unlock();
@@ -207,7 +285,8 @@ public void close() {
207285
try {
208286
this.command = null;
209287
this.startTime = System.currentTimeMillis();
210-
this.state = "closed";
288+
this.state = CLOSED;
289+
this.stateText = "closed";
211290
this.sql = null;
212291
} finally {
213292
this.lock.unlock();

0 commit comments

Comments
 (0)