-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SOLR-17504: CoreContainer calls UpdateHandler.commit. #2786
Changes from 4 commits
66b6c32
604fba6
8cafc88
bf1ba0f
c7ba76c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,21 @@ | |
|
||
/** A commit index command encapsulated in an object. */ | ||
public class CommitUpdateCommand extends UpdateCommand { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome docs! |
||
// Behavior about opening a searcher after a hard commit. | ||
// - Open a standard searcher and wait for it. (default) | ||
// openSearcher == true && waitSearcher == true && closeSearcher == false | ||
// - Open a standard searcher, but do not wait for it. | ||
// openSearcher == true && waitSearcher == false && closeSearcher == false | ||
// - Open a real-time searcher quicker without auto-warming. | ||
// openSearcher == false && closeSearcher == false | ||
// - Do not open any searcher and prevent further updates (e.g. when the core is closing). | ||
// closeSearcher == true | ||
|
||
public boolean optimize; | ||
public boolean openSearcher = true; // open a new searcher as part of a hard commit | ||
public boolean waitSearcher = true; | ||
private boolean closeSearcher = false; | ||
public boolean expungeDeletes = false; | ||
public boolean softCommit = false; | ||
public boolean prepareCommit = false; | ||
|
@@ -47,6 +59,26 @@ public CommitUpdateCommand(SolrQueryRequest req, boolean optimize) { | |
this.optimize = optimize; | ||
} | ||
|
||
/** | ||
* Creates a {@link CommitUpdateCommand} to commit before closing the core and also prevent any | ||
* new update by set the core in read-only mode. Does not open a new searcher. | ||
*/ | ||
public static CommitUpdateCommand closeOnCommit(SolrQueryRequest req, boolean optimize) { | ||
CommitUpdateCommand cmd = new CommitUpdateCommand(req, optimize); | ||
cmd.openSearcher = false; | ||
cmd.waitSearcher = false; | ||
cmd.closeSearcher = true; | ||
return cmd; | ||
} | ||
|
||
/** | ||
* Indicates whether this command is a commit before the core is closed. Any new updates must be | ||
* prevented after the commit. | ||
*/ | ||
public boolean isClosingOnCommit() { | ||
return closeSearcher; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "commit"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -738,6 +738,9 @@ public void commit(CommitUpdateCommand cmd) throws IOException { | |
|
||
RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core); | ||
try { | ||
if (cmd.isClosingOnCommit()) { | ||
core.readOnly = true; | ||
} | ||
IndexWriter writer = iw.get(); | ||
if (cmd.optimize) { | ||
writer.forceMerge(cmd.maxOptimizeSegments); | ||
|
@@ -786,7 +789,7 @@ public void commit(CommitUpdateCommand cmd) throws IOException { | |
if (ulog != null) ulog.preSoftCommit(cmd); | ||
if (cmd.openSearcher) { | ||
core.getSearcher(true, false, waitSearcher); | ||
} else { | ||
} else if (!cmd.isClosingOnCommit()) { | ||
// force open a new realtime searcher so realtime-get and versioning code can see the | ||
// latest | ||
RefCounted<SolrIndexSearcher> searchHolder = core.openNewSearcher(true, true); | ||
|
@@ -971,8 +974,10 @@ public void closeWriter(IndexWriter writer) throws IOException { | |
|
||
// todo: refactor this shared code (or figure out why a real CommitUpdateCommand can't | ||
// be used) | ||
Comment on lines
975
to
976
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know you didn't write that comment but it's unclear to me what is meant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, also unclear to me. |
||
SolrIndexWriter.setCommitData(writer, cmd.getVersion(), null); | ||
writer.commit(); | ||
if (shouldCommit(cmd, writer)) { | ||
SolrIndexWriter.setCommitData(writer, cmd.getVersion(), cmd.commitData); | ||
writer.commit(); | ||
} | ||
|
||
synchronized (solrCoreState.getUpdateLock()) { | ||
ulog.postCommit(cmd); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is beautiful |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.update; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.apache.lucene.index.IndexWriter; | ||
import org.apache.solr.SolrTestCaseJ4; | ||
import org.apache.solr.common.util.TimeSource; | ||
import org.apache.solr.core.SolrCore; | ||
import org.apache.solr.util.LogLevel; | ||
import org.apache.solr.util.TimeOut; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
/** Tests {@link DirectUpdateHandler2} with update log enabled. */ | ||
@LogLevel("org.apache.solr.update=INFO") | ||
public class DirectUpdateHandlerWithUpdateLogTest extends SolrTestCaseJ4 { | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws Exception { | ||
System.setProperty("solr.updateHandler", SpyingUpdateHandler.class.getName()); | ||
initCore("solrconfig.xml", "schema11.xml"); | ||
} | ||
|
||
@Test | ||
public void testShouldCommitHook() throws Exception { | ||
// Given a core. | ||
SolrCore core = h.getCore(); | ||
assertNotNull(core); | ||
SpyingUpdateHandler updater = (SpyingUpdateHandler) core.getUpdateHandler(); | ||
updater.shouldCommitCallCount.set(0); | ||
|
||
// When we add a doc and commit. | ||
assertU(adoc("id", "1")); | ||
assertU(commit()); | ||
// Then the shouldCommit hook is called. | ||
assertEquals(1, updater.shouldCommitCallCount.get()); | ||
|
||
// When we add a doc and soft commit. | ||
assertU(adoc("id", "2")); | ||
assertU(commit("softCommit", "true")); | ||
// Then the shouldCommit hook is not called. | ||
assertEquals(1, updater.shouldCommitCallCount.get()); | ||
// And when we commit. | ||
assertU(commit()); | ||
// Then the shouldCommit hook is called. | ||
assertEquals(2, updater.shouldCommitCallCount.get()); | ||
|
||
// When we commit with no updates (empty commit). | ||
assertU(commit()); | ||
// Then the shouldCommit hook is called (may commit only user metadata). | ||
assertEquals(3, updater.shouldCommitCallCount.get()); | ||
|
||
// When we add a doc, do not commit, and close the IndexWriter. | ||
assertU(adoc("id", "3")); | ||
h.close(); | ||
// Then the shouldCommit hook is called. | ||
new TimeOut(10, TimeUnit.SECONDS, TimeSource.NANO_TIME) | ||
.waitFor( | ||
"Timeout waiting for should commit hook", | ||
() -> updater.shouldCommitCallCount.get() == 4); | ||
} | ||
|
||
public static class SpyingUpdateHandler extends DirectUpdateHandler2 { | ||
|
||
final AtomicInteger shouldCommitCallCount = new AtomicInteger(); | ||
|
||
public SpyingUpdateHandler(SolrCore core) { | ||
super(core); | ||
} | ||
|
||
public SpyingUpdateHandler(SolrCore core, UpdateHandler updateHandler) { | ||
super(core, updateHandler); | ||
} | ||
|
||
@Override | ||
protected boolean shouldCommit(CommitUpdateCommand cmd, IndexWriter writer) throws IOException { | ||
shouldCommitCallCount.incrementAndGet(); | ||
return super.shouldCommit(cmd, writer); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose doing this before was to protect changes happening after the commit sneaking it. Why did you move it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, good point. I moved it because I didn't realize your point and UpdateHanlder.commit() checks the core is not readonly, otherwise stops with an exception.
Actually, with your nice idea of an enum in CommitUpdateCommand, the intent would be clear and commit() would not reject if the core is closed after the commit. And we could restore the core.readonly = true at the right location here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as an aside, I don't like that CoreContainer is doing SolrCore internal manipulations... like this should be a method on SolrCore like core.commit()