Skip to content

Commit 6ae69e2

Browse files
committed
[bugfix] Fixed thread naming and grouping per-database instance. Also likely fixed some subtle concurrency issues in the Admin Client and FileSyncThread
Closes #1699
1 parent ae60ae5 commit 6ae69e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+985
-475
lines changed

extensions/debuggee/src/org/exist/debuggee/ScriptRunner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.exist.xquery.CompiledXQuery;
3232
import org.exist.xquery.XQuery;
3333

34+
import static org.exist.util.ThreadUtils.nameInstanceThread;
35+
3436
/**
3537
* @author <a href="mailto:[email protected]">Dmitriy Shabanov</a>
3638
*
@@ -48,7 +50,7 @@ public ScriptRunner(SessionImpl session, CompiledXQuery compiled) {
4850
this.session = session;
4951
expression = compiled;
5052

51-
thread = new Thread(this);
53+
thread = newInstanceThread(BrokerPool.getInstance(), "scriptRunner" this);
5254
thread.setDaemon(true);
5355
thread.setName("Debug session "+compiled.getContext().hashCode());
5456
}

extensions/debuggee/src/org/exist/debugger/DebuggerImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import org.w3c.dom.NodeList;
5151
import org.w3c.dom.Text;
5252

53+
import static org.exist.util.ThreadUtils.newGlobalThread;
54+
5355
/**
5456
* @author <a href="mailto:[email protected]">Dmitriy Shabanov</a>
5557
*
@@ -117,7 +119,7 @@ public DebuggingSource init(String url) throws IOException,
117119
sources = new HashMap<String, DebuggingSource>();
118120
currentTransactionId = 1;
119121

120-
Thread session = new Thread(new HttpSession(this, url));
122+
Thread session = newGlobalThread("debuggerHttpSession" new HttpSession(this, url));
121123
session.start();
122124

123125
// 30s timeout

extensions/fluent/src/org/exist/fluent/Database.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.text.MessageFormat;
3131
import java.util.*;
3232

33+
import static org.exist.util.ThreadUtils.newInstanceThread;
34+
3335
/**
3436
* <p>The global entry point to an embedded instance of the <a href='http://exist-db.org'>eXist </a>database.
3537
* The static methods on this class control the lifecycle of the database connection. It follows that
@@ -561,7 +563,7 @@ private static class Defragmenter implements Runnable {
561563

562564
public void start() {
563565
if (thread != null) return;
564-
thread = new Thread(this, "Database defragmenter");
566+
thread = newInstanceThread(pool, "fluent.database-defragmenter", this);
565567
thread.setPriority(Thread.NORM_PRIORITY-3);
566568
thread.setDaemon(true);
567569
thread.start();

extensions/fluent/src/org/exist/fluent/WeakMultiValueHashMap.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.lang.ref.WeakReference;
44
import java.util.*;
55

6+
import static org.exist.util.ThreadUtils.newGlobalThread;
7+
68
class WeakMultiValueHashMap<K,V> {
79

810
/**
@@ -84,7 +86,7 @@ public void remove() {
8486

8587
private static final Sweeper SWEEPER = new Sweeper();
8688
static {
87-
Thread thread = new Thread(SWEEPER, "WeakMultiValueHashMap sweeper");
89+
Thread thread = newGlobalThread("fluent.weakMultiValueHashMap.sweeper", SWEEPER);
8890
thread.setPriority(Thread.NORM_PRIORITY-3);
8991
thread.setDaemon(true);
9092
thread.start();

extensions/modules/src/org/exist/xquery/modules/exi/EXIUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected static InputStream getInputStream(Item item, XQueryContext context) th
5050
Serializer serializer = context.getBroker().newSerializer();
5151

5252
NodeValue node = (NodeValue) item;
53-
return new NodeInputStream(serializer, node);
53+
return new NodeInputStream(context.getBroker().getBrokerPool(), serializer, node);
5454
default:
5555
LOG.error("Wrong item type " + Type.getTypeName(item.getType()));
5656
throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));

src/org/exist/Database.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public interface Database {
5858

5959
public String getId();
6060

61+
ThreadGroup getThreadGroup();
62+
6163
/**
6264
*
6365
* @return SecurityManager

src/org/exist/backup/Backup.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.exist.util.FileUtils;
2525
import com.evolvedbinary.j8fu.function.FunctionE;
26+
import org.exist.util.NamedThreadGroupFactory;
2627
import org.exist.util.SystemExitCodes;
2728
import org.xml.sax.SAXException;
2829
import org.xml.sax.helpers.AttributesImpl;
@@ -75,6 +76,10 @@ public class Backup
7576

7677
private static final int currVersion = 1;
7778

79+
private static final AtomicInteger backupThreadId = new AtomicInteger();
80+
private static final NamedThreadGroupFactory backupThreadGroupFactory = new NamedThreadGroupFactory("java-backup-tool");
81+
private final ThreadGroup backupThreadGroup = backupThreadGroupFactory.newThreadGroup(null);
82+
7883
private Path target;
7984
private XmldbURI rootCollection;
8085
private String user;
@@ -193,13 +198,17 @@ public void backup( boolean guiMode, JFrame parent ) throws XMLDBException, IOEx
193198
final BackupDialog dialog = new BackupDialog( parent, false );
194199
dialog.setSize( new Dimension( 350, 150 ) );
195200
dialog.setVisible( true );
196-
final BackupThread thread = new BackupThread( current, dialog, this );
197-
thread.start();
201+
final BackupRunnable backupRunnable = new BackupRunnable(current, dialog, this);
202+
final Thread backupThread = newBackupThread("backup-" + backupThreadId.getAndIncrement(), backupRunnable);
203+
backupThread.start();
204+
205+
206+
//super("exist-backupThread-" + backupThreadId.getAndIncrement());
198207

199208
if( parent == null ) {
200209

201210
// if backup runs as a single dialog, wait for it (or app will terminate)
202-
while( thread.isAlive() ) {
211+
while( backupThread.isAlive() ) {
203212

204213
synchronized( this ) {
205214

@@ -486,15 +495,24 @@ public static void writeACLPermission(SAXSerializer serializer, ACLPermission ac
486495
serializer.endElement(Namespaces.EXIST_NS, "acl", "acl");
487496
}
488497

489-
private static class BackupThread extends Thread {
498+
/**
499+
* Create a new thread for this backup instance.
500+
*
501+
* @param threadName the name of the thread
502+
* @param runnable the function to execute on the thread
503+
*
504+
* @return the thread
505+
*/
506+
private Thread newBackupThread(final String threadName, final Runnable runnable) {
507+
return new Thread(backupThreadGroup, runnable, backupThreadGroup.getName() + "." + threadName);
508+
}
509+
510+
private static class BackupRunnable implements Runnable {
490511
private final Collection collection;
491512
private final BackupDialog dialog;
492513
private final Backup backup;
493514

494-
private static final AtomicInteger backupThreadId = new AtomicInteger();
495-
496-
public BackupThread(final Collection collection, final BackupDialog dialog, final Backup backup) {
497-
super("exist-backupThread-" + backupThreadId.getAndIncrement());
515+
public BackupRunnable(final Collection collection, final BackupDialog dialog, final Backup backup) {
498516
this.collection = collection;
499517
this.dialog = dialog;
500518
this.backup = backup;

src/org/exist/backup/ExportGUI.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.exist.security.PermissionDeniedException;
4848

4949
import static java.nio.charset.StandardCharsets.UTF_8;
50+
import static org.exist.util.ThreadUtils.newInstanceThread;
5051

5152

5253
/**
@@ -337,7 +338,7 @@ private void startBtncheck(java.awt.event.ActionEvent evt) { // GEN-FIRST:event_
337338
closeLog();
338339
}
339340
};
340-
new Thread(checkRun).start();
341+
newInstanceThread(pool, "export-gui.check-run", checkRun).start();
341342
} // GEN-LAST:event_startBtncheck
342343

343344

@@ -358,7 +359,7 @@ private void exportBtnActionPerformed(java.awt.event.ActionEvent evt) { // GEN-F
358359
closeLog();
359360
}
360361
};
361-
new Thread(th).start();
362+
newInstanceThread(pool, "export-gui.export", th).start();
362363
} // GEN-LAST:event_exportBtnActionPerformed
363364

364365

src/org/exist/backup/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Optional;
3030
import java.util.concurrent.ExecutorService;
3131

32+
import org.exist.util.NamedThreadFactory;
3233
import org.exist.util.SystemExitCodes;
3334
import org.xml.sax.SAXException;
3435

@@ -361,7 +362,7 @@ public Void call() throws Exception {
361362
}
362363
};
363364

364-
final ExecutorService executor = Executors.newSingleThreadExecutor();
365+
final ExecutorService executor = Executors.newSingleThreadExecutor(new NamedThreadFactory(null, null, "backup.restore-with-gui"));
365366
final Future<Void> future = executor.submit(callable);
366367

367368
while (!future.isDone() && !future.isCancelled()) {

0 commit comments

Comments
 (0)