possible fix for concurrency problem

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6360 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 15 years ago
parent a6a3090c3d
commit 2275f885a8

@ -338,22 +338,23 @@ public class SplitTable implements ObjectIndex {
this.responseCounter = 0; this.responseCounter = 0;
this.finishCounter = finishCounter; this.finishCounter = finishCounter;
this.readyCheck = new Semaphore(0); this.readyCheck = new Semaphore(0);
this.discovery = null;
} }
public byte[] getKey() { public byte[] getKey() {
return this.key; return this.key;
} }
public void commitDiscovery() { public synchronized void commitNoDiscovery() {
this.responseCounter++; this.responseCounter++;
if (this.responseCounter >= this.finishCounter) this.readyCheck.release(); if (this.responseCounter >= this.finishCounter) this.readyCheck.release();
} }
public void commitDiscovery(ObjectIndex discovery) { public synchronized void commitDiscovery(ObjectIndex discovery) {
this.responseCounter++; this.responseCounter++;
this.discovery = discovery; this.discovery = discovery;
this.readyCheck.release(); this.readyCheck.release();
} }
public ObjectIndex discover() { public ObjectIndex discover(long timeout) {
try { try {
this.readyCheck.acquire(); this.readyCheck.tryAcquire(1, timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
return this.discovery; return this.discovery;
} }
@ -392,37 +393,47 @@ public class SplitTable implements ObjectIndex {
} }
public void run() { public void run() {
DiscoverOrder order; DiscoverOrder order;
try { while (true) {
while ((order = orderQueue.take()) != poisonDiscoverOrder) { try {
// check if in the given objectIndex is the key as given in the order 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())) { if (order.objectIndex.has(order.challenge.getKey())) {
order.challenge.commitDiscovery(order.objectIndex); order.challenge.commitDiscovery(order.objectIndex);
} else { } 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) { private ObjectIndex keeperOf(final byte[] key) {
int tableCount = this.tables.size(); Challenge challenge = null;
Challenge challenge = new Challenge(key, tableCount); synchronized (tables) {
int tableCount = this.tables.size();
// submit discover orders to the processing units challenge = new Challenge(key, tableCount);
final Iterator<ObjectIndex> i = tables.values().iterator();
while (i.hasNext()) { // submit discover orders to the processing units
try { final Iterator<ObjectIndex> i = tables.values().iterator();
this.orderQueue.put(new DiscoverOrder(challenge, i.next())); while (i.hasNext()) {
} catch (InterruptedException e) { try {
e.printStackTrace(); this.orderQueue.put(new DiscoverOrder(challenge, i.next()));
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} }
// wait for a result // 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())); //System.out.println("result of discovery: file = " + ((result == null) ? "null" : result.filename()));
return result; return result;
} }

Loading…
Cancel
Save