From 2275f885a8925783c884090d85bce873f9613cc0 Mon Sep 17 00:00:00 2001 From: orbiter Date: Tue, 29 Sep 2009 21:40:50 +0000 Subject: [PATCH] possible fix for concurrency problem git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6360 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- .../de/anomic/kelondro/table/SplitTable.java | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/source/de/anomic/kelondro/table/SplitTable.java b/source/de/anomic/kelondro/table/SplitTable.java index b92d92d74..64934354a 100644 --- a/source/de/anomic/kelondro/table/SplitTable.java +++ b/source/de/anomic/kelondro/table/SplitTable.java @@ -338,22 +338,23 @@ public class SplitTable implements ObjectIndex { this.responseCounter = 0; this.finishCounter = finishCounter; this.readyCheck = new Semaphore(0); + this.discovery = null; } public byte[] getKey() { return this.key; } - public void commitDiscovery() { + public synchronized void commitNoDiscovery() { this.responseCounter++; if (this.responseCounter >= this.finishCounter) this.readyCheck.release(); } - public void commitDiscovery(ObjectIndex discovery) { + public synchronized void commitDiscovery(ObjectIndex discovery) { this.responseCounter++; this.discovery = discovery; this.readyCheck.release(); } - public ObjectIndex discover() { + public ObjectIndex discover(long timeout) { try { - this.readyCheck.acquire(); + this.readyCheck.tryAcquire(1, timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) {} return this.discovery; } @@ -392,37 +393,47 @@ public class SplitTable implements ObjectIndex { } public void run() { DiscoverOrder order; - try { - while ((order = orderQueue.take()) != poisonDiscoverOrder) { - // check if in the given objectIndex is the key as given in the order + while (true) { + try { + order = orderQueue.take(); + } catch (InterruptedException e) { + e.printStackTrace(); + continue; + } + if (order == poisonDiscoverOrder) break; + // check if in the given objectIndex is the key as given in the order + try { if (order.objectIndex.has(order.challenge.getKey())) { order.challenge.commitDiscovery(order.objectIndex); } else { - order.challenge.commitDiscovery(); + order.challenge.commitNoDiscovery(); } + } catch (Exception e) { + e.printStackTrace(); + order.challenge.commitNoDiscovery(); } - } catch (InterruptedException e) { - e.printStackTrace(); } } } private ObjectIndex keeperOf(final byte[] key) { - int tableCount = this.tables.size(); - Challenge challenge = new Challenge(key, tableCount); - - // submit discover orders to the processing units - final Iterator i = tables.values().iterator(); - while (i.hasNext()) { - try { - this.orderQueue.put(new DiscoverOrder(challenge, i.next())); - } catch (InterruptedException e) { - e.printStackTrace(); + Challenge challenge = null; + synchronized (tables) { + int tableCount = this.tables.size(); + challenge = new Challenge(key, tableCount); + + // submit discover orders to the processing units + final Iterator i = tables.values().iterator(); + while (i.hasNext()) { + try { + this.orderQueue.put(new DiscoverOrder(challenge, i.next())); + } catch (InterruptedException e) { + e.printStackTrace(); + } } - } - + } // wait for a result - ObjectIndex result = challenge.discover(); + ObjectIndex result = challenge.discover(10000); //System.out.println("result of discovery: file = " + ((result == null) ? "null" : result.filename())); return result; }