Skip to content

Commit bada930

Browse files
authored
Merge pull request #6 from little-pan/v0.3.29-fix-kill
fix - can't kill process issue
2 parents a9bcbc4 + 85640fa commit bada930

6 files changed

Lines changed: 108 additions & 16 deletions

File tree

bin/build.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
#!/bin/sh
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
# -----------------------------------------------------------------------------
19+
# Build Script for the SQLite Server
20+
# -----------------------------------------------------------------------------
21+
222
if [ -z "$JAVA_HOME" ] ; then
323
if [[ "$OSTYPE" == "darwin"* ]]; then
424
if [ -d "/System/Library/Frameworks/JavaVM.framework/Home" ] ; then

bin/startup.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ rem Boot script for the SQLite Server
2020
rem ---------------------------------------------------------------------------
2121
setlocal
2222

23-
set JAVA_OPTS=-Xmx128m
23+
set JAVA_OPTS=-Xmx256m
2424

2525
rem Guess SQLITED_HOME if not defined
2626
set "CURRENT_DIR=%cd%"

bin/startup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Boot Script for the SQLite Server
2020
# -----------------------------------------------------------------------------
2121

22-
export JAVA_OPTS=-Xmx128m
22+
export JAVA_OPTS=-Xmx256m
2323

2424
BIN_DIR=`dirname "$PRG"`
2525
export SQLITED_HOME=`dirname "$BIN_DIR"`

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,50 @@ public boolean isCurrentUser(User other) {
903903
&& user.getHost().equals(other.getHost()));
904904
}
905905

906+
protected boolean shutdownInput() {
907+
final SocketChannel ch = this.channel;
908+
if (ch == null) {
909+
return false;
910+
}
911+
912+
if (ch.isConnected() && ch.isOpen()) {
913+
try {
914+
trace(log, "{}: shutdown input", this);
915+
ch.shutdownInput();
916+
return true;
917+
} catch (IOException e) {
918+
// ignore
919+
}
920+
}
921+
return false;
922+
}
923+
924+
protected boolean shutdownOutput() {
925+
final SocketChannel ch = this.channel;
926+
if (ch == null) {
927+
return false;
928+
}
929+
930+
if (ch.isConnected() && ch.isOpen()) {
931+
try {
932+
trace(log, "{}: shutdown output", this);
933+
ch.shutdownOutput();
934+
return true;
935+
} catch (IOException e) {
936+
// ignore
937+
}
938+
}
939+
return false;
940+
}
941+
906942
public void stop() {
943+
if (isStopped()) {
944+
return;
945+
}
907946
this.stopped = true;
908947
this.state.stop();
948+
shutdownInput();
949+
this.worker.wakeup();
909950
}
910951

911952
public boolean isStopped() {
@@ -937,6 +978,7 @@ public void close() {
937978
this.savepointStack = null;
938979

939980
// release connections
981+
shutdownOutput();
940982
IoUtils.close(this.channel);
941983
this.channel = null;
942984
IoUtils.close(this.connection);

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ public void close(SQLiteProcessor processor) {
163163
@Override
164164
public void run() {
165165
try {
166-
for (; !isStopped() || this.processors.size() > 0;) {
166+
SlotAllocator<SQLiteProcessor> processors = this.processors;
167+
for (; !isStopped() || processors.size() > 0;) {
167168
long timeout = minSelectTimeout();
168169
int n;
169170
if (timeout < 0L) {
171+
processIdle();
170172
n = this.selector.select();
171173
} else if (timeout == 0L) {
172174
n = this.selector.selectNow();
@@ -202,6 +204,7 @@ protected void processQueues(long runNanos) throws IllegalStateException {
202204
Selector selector = this.selector;
203205
long deadNano = System.nanoTime() + runNanos;
204206
// Q1: procQueue
207+
SlotAllocator<SQLiteProcessor> processors = this.processors;
205208
for (;;) {
206209
if (runNanos > 0L && System.nanoTime() > deadNano) {
207210
return;
@@ -216,7 +219,7 @@ protected void processQueues(long runNanos) throws IllegalStateException {
216219
p.setSelector(selector);
217220
p.setWorker(this);
218221
p.setName(this.name + "-" + p.getName());
219-
if (this.processors.size() >= this.maxConns) {
222+
if (processors.size() >= this.maxConns) {
220223
p.tooManyConns();
221224
p.stop();
222225
p.enableWrite();
@@ -230,7 +233,7 @@ protected void processQueues(long runNanos) throws IllegalStateException {
230233
}
231234
this.procsLock.lock();
232235
try {
233-
final int slot = this.processors.allocate(p);
236+
final int slot = processors.allocate(p);
234237
if (slot == -1) {
235238
throw new IllegalStateException("Processor allocator full");
236239
}
@@ -284,7 +287,23 @@ protected void processQueues(long runNanos) throws IllegalStateException {
284287
}
285288
}
286289

290+
protected void processIdle() {
291+
SlotAllocator<SQLiteProcessor> processors = this.processors;
292+
this.procsLock.lock();
293+
try {
294+
for (int i = 0, n = processors.maxSlot(); i < n; ++i) {
295+
SQLiteProcessor p = processors.get(i);
296+
if (p != null && p.isStopped()) {
297+
p.write();
298+
}
299+
}
300+
} finally {
301+
this.procsLock.unlock();
302+
}
303+
}
304+
287305
protected void processIO() {
306+
final Thread currThead = Thread.currentThread();
288307
Iterator<SelectionKey> keys = this.selector.selectedKeys().iterator();
289308
for (; keys.hasNext(); keys.remove()) {
290309
SelectionKey key = keys.next();
@@ -297,18 +316,18 @@ protected void processIO() {
297316
if (key.isWritable()) {
298317
try {
299318
p = (SQLiteProcessor)key.attachment();
300-
Thread.currentThread().setName(p.getName());
319+
currThead.setName(p.getName());
301320
p.write();
302321
} finally {
303-
Thread.currentThread().setName(this.name);
322+
currThead.setName(this.name);
304323
}
305324
} else if (key.isReadable()) {
306325
try {
307326
p = (SQLiteProcessor)key.attachment();
308-
Thread.currentThread().setName(p.getName());
327+
currThead.setName(p.getName());
309328
p.read();
310329
} finally {
311-
Thread.currentThread().setName(this.name);
330+
currThead.setName(this.name);
312331
}
313332
} else {
314333
key.cancel();
@@ -411,10 +430,11 @@ protected long minSelectTimeout() {
411430
}
412431

413432
SQLiteProcessor getProcessor(int pid) {
433+
SlotAllocator<SQLiteProcessor> processors = this.processors;
414434
this.procsLock.lock();
415435
try {
416-
for (int i = 0, n = this.processors.maxSlot(); i < n; ++i) {
417-
SQLiteProcessor p = this.processors.get(i);
436+
for (int i = 0, n = processors.maxSlot(); i < n; ++i) {
437+
SQLiteProcessor p = processors.get(i);
418438
if (p != null && p.getId() == pid) {
419439
return p;
420440
}
@@ -426,12 +446,13 @@ SQLiteProcessor getProcessor(int pid) {
426446
}
427447

428448
public List<SQLiteProcessorState> getProcessorStates(final SQLiteProcessor processor) {
449+
SlotAllocator<SQLiteProcessor> processors = this.processors;
429450
this.procsLock.lock();
430451
try {
431452
List<SQLiteProcessorState> states = new ArrayList<>();
432453
final User user = processor.getUser();
433-
for (int i = 0, n = this.processors.maxSlot(); i < n; ++i) {
434-
final SQLiteProcessor p = this.processors.get(i);
454+
for (int i = 0, n = processors.maxSlot(); i < n; ++i) {
455+
final SQLiteProcessor p = processors.get(i);
435456
if (p == null) {
436457
continue;
437458
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
*
3737
*/
3838
public abstract class TestDbBase extends TestBase {
39-
39+
static {
40+
if (System.getProperty("SQLITED_HOME") == null) {
41+
String defaultDir = System.getProperty("user.dir");
42+
System.setProperty("SQLITED_HOME", defaultDir);
43+
}
44+
}
4045
protected static final String dataDir = getDataDir();
4146

4247
protected static String user = "root";
@@ -65,11 +70,11 @@ public abstract class TestDbBase extends TestBase {
6570
protected static final String [][] initArgsList = new String[][] {
6671
{"-D", dataDir, "-p", password, "--journal-mode", "wal"},
6772
{"-D", dataDir, "-p", password, "--journal-mode", "delete",
68-
"-S", "off"
73+
"-S", "normal"
6974
},
7075
{"-D", dataDir, "-p", password, "--journal-mode", "wal"},
7176
{"-D", dataDir, "-p", password, "--journal-mode", "delete",
72-
"-S", "off"
77+
"-S", "normal"
7378
},
7479
};
7580

@@ -95,6 +100,10 @@ public abstract class TestDbBase extends TestBase {
95100
protected DbTestEnv currentEnv;
96101
protected int envIndex, envMax;
97102

103+
protected TestDbBase() {
104+
105+
}
106+
98107
protected void init() {
99108
this.envIndex = 0;
100109
this.envMax = initArgsList.length;

0 commit comments

Comments
 (0)