Skip to content
Permalink
Browse files

Properly lock solrInstances for reboot and restoration of embedded Solr

Putting a synchronization lock directly on the solrInstances property
was ineffective as it is assigned a new (unlocked) instance in these
operations.
  • Loading branch information...
luccioman committed Jul 8, 2018
1 parent 9630f81 commit f4676015611e1e9c602587f8e096336fef3305c5
Showing with 47 additions and 11 deletions.
  1. +47 −11 source/net/yacy/search/index/Fulltext.java
@@ -40,6 +40,7 @@
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import java.util.zip.Deflater;
import java.util.zip.GZIPOutputStream;
@@ -96,6 +97,10 @@
private final File archivePath;
private Export exportthread; // will have a export thread assigned if exporter is running
private InstanceMirror solrInstances;

/** Synchronization lock for solrInstances property */
private ReentrantLock solrInstancesLock;

private final CollectionConfiguration collectionConfiguration;
private final WebgraphConfiguration webgraphConfiguration;
private boolean writeWebgraph;
@@ -106,6 +111,7 @@ protected Fulltext(final File segmentPath, final File archivePath,
this.archivePath = archivePath;
this.exportthread = null; // will have a export thread assigned if exporter is running
this.solrInstances = new InstanceMirror();
this.solrInstancesLock = new ReentrantLock();
this.collectionConfiguration = collectionConfiguration;
this.webgraphConfiguration = webgraphConfiguration;
this.writeWebgraph = false;
@@ -198,22 +204,35 @@ public RemoteSolrConnector getDefaultRemoteSolrConnector() {
}

public EmbeddedInstance getEmbeddedInstance() {
synchronized (this.solrInstances) {
if (this.solrInstances.isConnectedEmbedded()) return this.solrInstances.getEmbedded();
this.solrInstancesLock.lock();
try {
if (this.solrInstances.isConnectedEmbedded()) {
return this.solrInstances.getEmbedded();
}
return null;
} finally {
this.solrInstancesLock.unlock();
}
}

public SolrConnector getDefaultConnector() {
synchronized (this.solrInstances) {
this.solrInstancesLock.lock();
try {
return this.solrInstances.getDefaultMirrorConnector();
} finally {
this.solrInstancesLock.unlock();
}
}

public SolrConnector getWebgraphConnector() {
if (!this.writeWebgraph) return null;
synchronized (this.solrInstances) {
if (!this.writeWebgraph) {
return null;
}
this.solrInstancesLock.lock();
try {
return this.solrInstances.getGenericMirrorConnector(WebgraphSchema.CORE_NAME);
} finally {
this.solrInstancesLock.unlock();
}
}

@@ -232,8 +251,11 @@ public void clearCaches() {
}

public void clearLocalSolr() throws IOException {
if (this.exportthread != null) this.exportthread.interrupt();
synchronized (this.solrInstances) {
if (this.exportthread != null) {
this.exportthread.interrupt();
}
this.solrInstancesLock.lock();
try {
EmbeddedInstance instance = this.solrInstances.getEmbedded();
if (instance != null) {
for (String name: instance.getCoreNames()) {
@@ -242,18 +264,23 @@ public void clearLocalSolr() throws IOException {
this.commit(false);
}
this.solrInstances.clearCaches();
} finally {
this.solrInstancesLock.unlock();
}
}

public void clearRemoteSolr() throws IOException {
synchronized (this.solrInstances) {
this.solrInstancesLock.lock();
try {
ShardInstance instance = this.solrInstances.getRemote();
if (instance != null) {
for (String name: instance.getCoreNames()) {
this.solrInstances.getRemoteConnector(name).clear();
}
}
this.solrInstances.clearCaches();
} finally {
this.solrInstancesLock.unlock();
}
}

@@ -593,7 +620,8 @@ public File dumpEmbeddedSolr() throws SolrException {
}
final File storagePath = esc.getContainerPath();
final File zipOut = new File(this.archivePath, storagePath.getName() + "_" + GenericFormatter.SHORT_DAY_FORMATTER.format() + ".zip");
synchronized (this.solrInstances) {
this.solrInstancesLock.lock();
try {
this.disconnectLocalSolr();
try {
ZIPWriter.zip(storagePath, zipOut);
@@ -606,6 +634,8 @@ public File dumpEmbeddedSolr() throws SolrException {
ConcurrentLog.logException(e);
}
}
} finally {
this.solrInstancesLock.unlock();
}
return zipOut;
}
@@ -621,7 +651,8 @@ public void restoreEmbeddedSolr(final File solrDumpZipFile) {
throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "No embedded Solr available.");
}
final File storagePath = esc.getContainerPath();
synchronized (this.solrInstances) {
this.solrInstancesLock.lock();
try {
// this.disconnectLocalSolr(); // moved to (InstanceMirror) sorlInstances.close()
this.solrInstances.close();
try {
@@ -636,6 +667,8 @@ public void restoreEmbeddedSolr(final File solrDumpZipFile) {
ConcurrentLog.logException(e);
}
}
} finally {
this.solrInstancesLock.unlock();
}
}

@@ -654,7 +687,8 @@ public void optimize(final int size) {
* Please check before that the local embedded Solr is enabled and no external remote Solr is attached.
*/
public void rebootEmbeddedLocalSolr() {
synchronized (this.solrInstances) {
this.solrInstancesLock.lock();
try {
this.disconnectLocalSolr();
// this.solrInstances.close(); // moved to (InstanceMirror) sorlInstances.close()
this.solrInstances = new InstanceMirror();
@@ -663,6 +697,8 @@ public void rebootEmbeddedLocalSolr() {
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
} finally {
this.solrInstancesLock.unlock();
}
}

0 comments on commit f467601

Please sign in to comment.
You can’t perform that action at this time.