LUCENE-5612: Reuse the socket's input/output

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1588590 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2014-04-18 23:13:32 +00:00
parent 2af485f151
commit 7f3e945a2a
2 changed files with 45 additions and 41 deletions

View File

@ -19,6 +19,7 @@ package org.apache.lucene.store;
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
@ -89,46 +90,46 @@ public class LockStressTest {
final InetSocketAddress addr = new InetSocketAddress(verifierHost, verifierPort);
System.out.println("Connecting to server " + addr +
" and registering as client " + myID + "...");
Socket socket = new Socket();
socket.setReuseAddress(true);
socket.connect(addr, 500);
OutputStream os = socket.getOutputStream();
os.write(myID);
os.flush();
lockFactory.setLockPrefix("test");
final LockFactory verifyLF = new VerifyingLockFactory(lockFactory, socket);
final Lock l = verifyLF.makeLock("test.lock");
final Random rnd = new Random();
// wait for starting gun
if (socket.getInputStream().read() != 43) {
throw new IOException("Protocol violation");
}
for (int i = 0; i < count; i++) {
boolean obtained = false;
try {
obtained = l.obtain(rnd.nextInt(100) + 10);
} catch (LockObtainFailedException e) {
try (Socket socket = new Socket()) {
socket.setReuseAddress(true);
socket.connect(addr, 500);
final OutputStream out = socket.getOutputStream();
final InputStream in = socket.getInputStream();
out.write(myID);
out.flush();
lockFactory.setLockPrefix("test");
final LockFactory verifyLF = new VerifyingLockFactory(lockFactory, in, out);
final Lock l = verifyLF.makeLock("test.lock");
final Random rnd = new Random();
// wait for starting gun
if (in.read() != 43) {
throw new IOException("Protocol violation");
}
if (obtained) {
for (int i = 0; i < count; i++) {
boolean obtained = false;
try {
obtained = l.obtain(rnd.nextInt(100) + 10);
} catch (LockObtainFailedException e) {
}
if (obtained) {
Thread.sleep(sleepTimeMS);
l.close();
}
if (i % 500 == 0) {
System.out.println((i * 100. / count) + "% done.");
}
Thread.sleep(sleepTimeMS);
l.close();
}
if (i % 500 == 0) {
System.out.println((i * 100. / count) + "% done.");
}
Thread.sleep(sleepTimeMS);
}
}
IOUtils.closeWhileHandlingException(socket);
System.out.println("Finished " + count + " tries.");
}
}

View File

@ -19,6 +19,7 @@ package org.apache.lucene.store;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
@ -38,7 +39,8 @@ import java.io.OutputStream;
public class VerifyingLockFactory extends LockFactory {
final LockFactory lf;
final Socket socket;
final InputStream in;
final OutputStream out;
private class CheckedLock extends Lock {
private final Lock lock;
@ -48,10 +50,9 @@ public class VerifyingLockFactory extends LockFactory {
}
private void verify(byte message) throws IOException {
final OutputStream out = socket.getOutputStream();
out.write(message);
out.flush();
final int ret = socket.getInputStream().read();
final int ret = in.read();
if (ret < 0) {
throw new IllegalStateException("Lock server died because of locking error.");
}
@ -84,11 +85,13 @@ public class VerifyingLockFactory extends LockFactory {
/**
* @param lf the LockFactory that we are testing
* @param socket the socket connected to {@link LockVerifyServer}
* @param in the socket's input to {@link LockVerifyServer}
* @param out the socket's output to {@link LockVerifyServer}
*/
public VerifyingLockFactory(LockFactory lf, Socket socket) {
public VerifyingLockFactory(LockFactory lf, InputStream in, OutputStream out) throws IOException {
this.lf = lf;
this.socket = socket;
this.in = in;
this.out = out;
}
@Override