Skip to content

Commit

Permalink
Move index handlers into handler package (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
aprudhomme authored Oct 4, 2024
1 parent f4da306 commit 2e2b40d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.yelp.nrtsearch.server.luceneserver.handler.GlobalStateHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.IndicesHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.LiveSettingsHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.LiveSettingsV2Handler;
import com.yelp.nrtsearch.server.luceneserver.handler.MetricsHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.NewNRTPointHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.ReadyHandler;
Expand All @@ -66,6 +67,7 @@
import com.yelp.nrtsearch.server.luceneserver.handler.SearchV2Handler;
import com.yelp.nrtsearch.server.luceneserver.handler.SendRawFileHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.SettingsHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.SettingsV2Handler;
import com.yelp.nrtsearch.server.luceneserver.handler.StartIndexHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.StartIndexV2Handler;
import com.yelp.nrtsearch.server.luceneserver.handler.StatsHandler;
Expand All @@ -74,8 +76,6 @@
import com.yelp.nrtsearch.server.luceneserver.handler.UpdateFieldsHandler;
import com.yelp.nrtsearch.server.luceneserver.handler.WriteNRTPointHandler;
import com.yelp.nrtsearch.server.luceneserver.highlights.HighlighterService;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.LiveSettingsV2Handler;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.SettingsV2Handler;
import com.yelp.nrtsearch.server.luceneserver.logging.HitsLoggerCreator;
import com.yelp.nrtsearch.server.luceneserver.rescore.RescorerCreator;
import com.yelp.nrtsearch.server.luceneserver.script.ScriptService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yelp.nrtsearch.server.luceneserver.index.handlers;
package com.yelp.nrtsearch.server.luceneserver.handler;

import com.google.protobuf.util.JsonFormat;
import com.yelp.nrtsearch.server.grpc.IndexLiveSettings;
import com.yelp.nrtsearch.server.grpc.LiveSettingsV2Request;
import com.yelp.nrtsearch.server.grpc.LiveSettingsV2Response;
import com.yelp.nrtsearch.server.luceneserver.handler.Handler;
import com.yelp.nrtsearch.server.luceneserver.index.IndexStateManager;
import com.yelp.nrtsearch.server.luceneserver.state.GlobalState;
import io.grpc.Status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.yelp.nrtsearch.server.grpc.FieldDefRequest;
import com.yelp.nrtsearch.server.grpc.FieldDefResponse;
import com.yelp.nrtsearch.server.luceneserver.index.IndexStateManager;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler;
import com.yelp.nrtsearch.server.luceneserver.state.GlobalState;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
Expand All @@ -40,7 +39,8 @@ public void handle(
try {
IndexStateManager indexStateManager =
getGlobalState().getIndexStateManager(fieldDefRequest.getIndexName());
FieldDefResponse reply = FieldUpdateHandler.handle(indexStateManager, fieldDefRequest);
String updatedFields = indexStateManager.updateFields(fieldDefRequest.getFieldList());
FieldDefResponse reply = FieldDefResponse.newBuilder().setResponse(updatedFields).build();
logger.info("RegisterFieldsHandler registered fields " + reply);
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yelp.nrtsearch.server.luceneserver.index.handlers;
package com.yelp.nrtsearch.server.luceneserver.handler;

import com.google.protobuf.util.JsonFormat;
import com.yelp.nrtsearch.server.grpc.IndexSettings;
import com.yelp.nrtsearch.server.grpc.SettingsV2Request;
import com.yelp.nrtsearch.server.grpc.SettingsV2Response;
import com.yelp.nrtsearch.server.luceneserver.handler.Handler;
import com.yelp.nrtsearch.server.luceneserver.index.IndexStateManager;
import com.yelp.nrtsearch.server.luceneserver.state.GlobalState;
import io.grpc.Status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.yelp.nrtsearch.server.grpc.FieldDefRequest;
import com.yelp.nrtsearch.server.grpc.FieldDefResponse;
import com.yelp.nrtsearch.server.luceneserver.index.IndexStateManager;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler;
import com.yelp.nrtsearch.server.luceneserver.state.GlobalState;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
Expand All @@ -40,7 +39,8 @@ public void handle(
try {
IndexStateManager indexStateManager =
getGlobalState().getIndexStateManager(fieldDefRequest.getIndexName());
FieldDefResponse reply = FieldUpdateHandler.handle(indexStateManager, fieldDefRequest);
String updatedFields = indexStateManager.updateFields(fieldDefRequest.getFieldList());
FieldDefResponse reply = FieldDefResponse.newBuilder().setResponse(updatedFields).build();
logger.info("UpdateFieldsHandler registered fields " + reply);
responseObserver.onNext(reply);
responseObserver.onCompleted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import com.yelp.nrtsearch.server.grpc.IndexStateInfo;
import com.yelp.nrtsearch.server.grpc.Mode;
import com.yelp.nrtsearch.server.grpc.ReplicationServerClient;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler.UpdatedFieldInfo;
import com.yelp.nrtsearch.server.luceneserver.index.FieldUpdateUtils.UpdatedFieldInfo;
import com.yelp.nrtsearch.server.luceneserver.nrt.NrtDataManager;
import com.yelp.nrtsearch.server.luceneserver.state.BackendGlobalState;
import com.yelp.nrtsearch.server.luceneserver.state.GlobalState;
Expand Down Expand Up @@ -87,7 +86,7 @@ public synchronized void load() throws IOException {
// name. Let's fix it here so that it updates on the next commit.
stateInfo = fixIndexName(stateInfo, indexName);
UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
new FieldAndFacetState(), Collections.emptyMap(), stateInfo.getFieldsMap().values());
currentState =
createIndexState(stateInfo, updatedFieldInfo.fieldAndFacetState, liveSettingsOverrides);
Expand Down Expand Up @@ -201,7 +200,7 @@ public synchronized String updateFields(List<Field> fields) throws IOException {
throw new IllegalStateException("No state for index: " + indexName);
}
UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
currentState.getFieldAndFacetState(),
currentState.getCurrentStateInfo().getFieldsMap(),
fields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yelp.nrtsearch.server.luceneserver.index.handlers;
package com.yelp.nrtsearch.server.luceneserver.index;

import com.yelp.nrtsearch.server.grpc.Field;
import com.yelp.nrtsearch.server.grpc.FieldDefRequest;
import com.yelp.nrtsearch.server.grpc.FieldDefResponse;
import com.yelp.nrtsearch.server.grpc.FieldType;
import com.yelp.nrtsearch.server.luceneserver.doc.DocLookup;
import com.yelp.nrtsearch.server.luceneserver.field.FieldDef;
import com.yelp.nrtsearch.server.luceneserver.field.FieldDefCreator;
import com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef;
import com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef;
import com.yelp.nrtsearch.server.luceneserver.index.FieldAndFacetState;
import com.yelp.nrtsearch.server.luceneserver.index.IndexState;
import com.yelp.nrtsearch.server.luceneserver.index.IndexStateManager;
import com.yelp.nrtsearch.server.luceneserver.script.ScoreScript;
import com.yelp.nrtsearch.server.luceneserver.script.ScriptService;
import com.yelp.nrtsearch.server.utils.ScriptParamsUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -40,24 +34,10 @@
import org.slf4j.LoggerFactory;

/** Static helper class to handle a request to add/update fields. */
public class FieldUpdateHandler {
private static final Logger logger = LoggerFactory.getLogger(FieldUpdateHandler.class);
public class FieldUpdateUtils {
private static final Logger logger = LoggerFactory.getLogger(FieldUpdateUtils.class);

private FieldUpdateHandler() {}

/**
* Handle a FieldDefRequest.
*
* @param indexStateManager state manager for index
* @param request request message
* @return response message
* @throws IOException on error committing state
*/
public static FieldDefResponse handle(
IndexStateManager indexStateManager, FieldDefRequest request) throws IOException {
String updatedFields = indexStateManager.updateFields(request.getFieldList());
return FieldDefResponse.newBuilder().setResponse(updatedFields).build();
}
private FieldUpdateUtils() {}

/**
* Container class to hold the {@link Field} definitions used in state, and the corresponding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yelp.nrtsearch.server.luceneserver.index.handlers;
package com.yelp.nrtsearch.server.luceneserver.index;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -43,9 +43,7 @@
import com.yelp.nrtsearch.server.luceneserver.field.ObjectFieldDef;
import com.yelp.nrtsearch.server.luceneserver.field.TextFieldDef;
import com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef;
import com.yelp.nrtsearch.server.luceneserver.index.FieldAndFacetState;
import com.yelp.nrtsearch.server.luceneserver.index.IndexStateManager;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler.UpdatedFieldInfo;
import com.yelp.nrtsearch.server.luceneserver.index.FieldUpdateUtils.UpdatedFieldInfo;
import com.yelp.nrtsearch.server.luceneserver.script.ScriptService;
import com.yelp.nrtsearch.server.luceneserver.script.js.JsScriptEngine;
import com.yelp.nrtsearch.server.luceneserver.similarity.SimilarityCreator;
Expand All @@ -62,7 +60,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

public class FieldUpdateHandlerTest {
public class FieldUpdateUtilsTest {

private static final List<Field> simpleUpdates = new ArrayList<>();

Expand Down Expand Up @@ -101,7 +99,7 @@ private UpdatedFieldInfo initializeAndCheckSimple() {
Map<String, Field> initialFields = Collections.emptyMap();

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(initialState, initialFields, simpleUpdates);
FieldUpdateUtils.updateFields(initialState, initialFields, simpleUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(2, fields.size());
assertEquals(simpleUpdates.get(0), fields.get("field1"));
Expand Down Expand Up @@ -147,7 +145,7 @@ public void testRegisterAdditionalFields() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(4, fields.size());
Expand Down Expand Up @@ -191,7 +189,7 @@ public void testRegisterIdField() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(4, fields.size());
Expand Down Expand Up @@ -237,11 +235,11 @@ public void testRegisterSecondIdField() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);

try {
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
updatedFieldInfo.fieldAndFacetState,
updatedFieldInfo.fields,
Collections.singleton(
Expand Down Expand Up @@ -279,7 +277,7 @@ public void testRegisterAnalyzedFields() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(4, fields.size());
Expand Down Expand Up @@ -317,7 +315,7 @@ public void testRegisterVirtualField() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(3, fields.size());
Expand Down Expand Up @@ -360,7 +358,7 @@ public void testRegisterVirtualFieldRefRequest() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(4, fields.size());
Expand Down Expand Up @@ -424,7 +422,7 @@ public void testRegisterFieldWithChildren() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(3, fields.size());
Expand Down Expand Up @@ -494,7 +492,7 @@ public void testRegisterFieldWithNestedChildren() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(3, fields.size());
Expand Down Expand Up @@ -549,7 +547,7 @@ public void testRegisterFieldsWithFacets() {
.build());

UpdatedFieldInfo updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
Map<String, Field> fields = updatedFieldInfo.fields;
assertEquals(4, fields.size());
Expand Down Expand Up @@ -587,7 +585,7 @@ public void testRegisterFieldsWithFacets() {
.build());

updatedFieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
updatedFieldInfo.fieldAndFacetState, updatedFieldInfo.fields, fieldUpdates2);
fields = updatedFieldInfo.fields;
assertEquals(5, fields.size());
Expand Down Expand Up @@ -635,7 +633,7 @@ public void testRegisterFieldNameConflict() {
.build());

try {
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
initialInfo.fieldAndFacetState, initialInfo.fields, fieldUpdates);
fail();
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -669,7 +667,8 @@ public void testUpdateRequestHandle() throws IOException {
FieldDefRequest request =
FieldDefRequest.newBuilder().setIndexName("test_index").addAllField(fieldUpdates).build();

FieldDefResponse response = FieldUpdateHandler.handle(mockManager, request);
String updatedFields = mockManager.updateFields(request.getFieldList());
FieldDefResponse response = FieldDefResponse.newBuilder().setResponse(updatedFields).build();
assertEquals("result_string", response.getResponse());

verify(mockManager, times(1)).updateFields(fieldUpdates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
import com.yelp.nrtsearch.server.luceneserver.field.FieldDefCreator;
import com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef;
import com.yelp.nrtsearch.server.luceneserver.field.IntFieldDef;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler;
import com.yelp.nrtsearch.server.luceneserver.index.handlers.FieldUpdateHandler.UpdatedFieldInfo;
import com.yelp.nrtsearch.server.luceneserver.index.FieldUpdateUtils.UpdatedFieldInfo;
import com.yelp.nrtsearch.server.luceneserver.nrt.NrtDataManager;
import com.yelp.nrtsearch.server.luceneserver.similarity.SimilarityCreator;
import com.yelp.nrtsearch.server.luceneserver.state.BackendGlobalState;
Expand Down Expand Up @@ -382,7 +381,7 @@ public void testIndexSort_set() throws IOException {
.setSearch(true)
.build();
UpdatedFieldInfo fieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
new FieldAndFacetState(), Collections.emptyMap(), Collections.singleton(sortField));
ImmutableIndexState indexState =
getIndexState(
Expand Down Expand Up @@ -1022,7 +1021,7 @@ public void testGetField() throws IOException {
.setSearch(true)
.build();
UpdatedFieldInfo fieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
new FieldAndFacetState(), Collections.emptyMap(), Collections.singleton(field));
IndexStateInfo indexStateInfo =
getEmptyState().toBuilder().putAllFields(fieldInfo.fields).build();
Expand Down Expand Up @@ -1060,7 +1059,7 @@ public void testGetAllFields() throws IOException {
.setSearch(true)
.build();
UpdatedFieldInfo fieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
new FieldAndFacetState(), Collections.emptyMap(), Collections.singleton(field));
IndexStateInfo indexStateInfo =
getEmptyState().toBuilder().putAllFields(fieldInfo.fields).build();
Expand All @@ -1078,7 +1077,7 @@ public void testGetAllFieldsJSON() throws IOException {
.setSearch(true)
.build();
UpdatedFieldInfo fieldInfo =
FieldUpdateHandler.updateFields(
FieldUpdateUtils.updateFields(
new FieldAndFacetState(), Collections.emptyMap(), Collections.singleton(field));
IndexStateInfo indexStateInfo =
getEmptyState().toBuilder().putAllFields(fieldInfo.fields).build();
Expand Down

0 comments on commit 2e2b40d

Please sign in to comment.