Apache Solr open-source search software
LUCENE-5612: Reuse the socket's input/output
Uwe Schindler7f3e945a2a6d
diff --git a/lucene/core/src/java/org/apache/lucene/store/LockStressTest.java b/lucene/core/src/java/org/apache/lucene/store/LockStressTest.javaindex 7dc378da481..bc64acc0e72 100644--- a/lucene/core/src/java/org/apache/lucene/store/LockStressTest.java+++ b/lucene/core/src/java/org/apache/lucene/store/LockStressTest.java@@ -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();- if (obtained) {- Thread.sleep(sleepTimeMS);- l.close();- }+ 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();- if (i % 500 == 0) {- System.out.println((i * 100. / count) + "% done.");+ // wait for starting gun+ if (in.read() != 43) {+ throw new IOException("Protocol violation");}- Thread.sleep(sleepTimeMS);+ 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);+ }}- IOUtils.closeWhileHandlingException(socket);-System.out.println("Finished " + count + " tries.");}}diff --git a/lucene/core/src/java/org/apache/lucene/store/VerifyingLockFactory.java b/lucene/core/src/java/org/apache/lucene/store/VerifyingLockFactory.javaindex 639d8f2408f..9922ab8946d 100644--- a/lucene/core/src/java/org/apache/lucene/store/VerifyingLockFactory.java+++ b/lucene/core/src/java/org/apache/lucene/store/VerifyingLockFactory.java@@ -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