-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Logging hits feature interface * Added unit test for hits logger plugin * Added hits logger creator test * Use HitsLoggerProvider to initialize HitsLogger with params * Addressed PR comments * Added/improved some comments * Fixed spotless violations * Replace * imports * Return empty map instead of null in HitsLoggerPlugin
- Loading branch information
1 parent
ef18628
commit 67f2893
Showing
12 changed files
with
623 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/yelp/nrtsearch/server/luceneserver/logging/HitsLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed 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 com.yelp.nrtsearch.server.luceneserver.logging; | ||
|
||
import com.yelp.nrtsearch.server.grpc.SearchResponse; | ||
import com.yelp.nrtsearch.server.luceneserver.search.SearchContext; | ||
import java.util.List; | ||
|
||
/** | ||
* This is the hits logger interface to provide the new logging hits feature with different | ||
* implementations. HitLoggers are supposed to be initiated once at the startup time and registered | ||
* in the {@link HitsLoggerCreator}. | ||
*/ | ||
public interface HitsLogger { | ||
/** | ||
* This will be invoked once as the last fetch task of the search request. | ||
* | ||
* @param context the {@link SearchContext} to keep the contexts for this search request | ||
* @param hits query hits | ||
*/ | ||
void log(SearchContext context, List<SearchResponse.Hit.Builder> hits); | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/com/yelp/nrtsearch/server/luceneserver/logging/HitsLoggerCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed 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 com.yelp.nrtsearch.server.luceneserver.logging; | ||
|
||
import com.yelp.nrtsearch.server.config.LuceneServerConfiguration; | ||
import com.yelp.nrtsearch.server.grpc.LoggingHits; | ||
import com.yelp.nrtsearch.server.plugins.HitsLoggerPlugin; | ||
import com.yelp.nrtsearch.server.plugins.Plugin; | ||
import com.yelp.nrtsearch.server.utils.StructValueTransformer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** Factory class that handles registration and creation of {@link HitsLogger}s. */ | ||
public class HitsLoggerCreator { | ||
private static HitsLoggerCreator instance; | ||
private final Map<String, HitsLoggerProvider<?>> hitsLoggerMap = new HashMap<>(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param configuration server configuration | ||
*/ | ||
public HitsLoggerCreator(LuceneServerConfiguration configuration) {} | ||
|
||
private void register(Map<String, HitsLoggerProvider<? extends HitsLogger>> hitsLoggers) { | ||
hitsLoggers.forEach(this::register); | ||
} | ||
|
||
private void register(String name, HitsLoggerProvider<?> hitsLoggerProvider) { | ||
if (hitsLoggerMap.containsKey(name)) { | ||
throw new IllegalArgumentException("HitsLogger " + name + " already exists"); | ||
} | ||
hitsLoggerMap.put(name, hitsLoggerProvider); | ||
} | ||
|
||
/** | ||
* Initialize singleton instance of {@link HitsLoggerCreator}. Registers the hits logger provided | ||
* by {@link HitsLoggerPlugin}s. | ||
* | ||
* @param configuration service configuration | ||
* @param plugins list of loaded plugins | ||
*/ | ||
public static void initialize(LuceneServerConfiguration configuration, Iterable<Plugin> plugins) { | ||
instance = new HitsLoggerCreator(configuration); | ||
for (Plugin plugin : plugins) { | ||
if (plugin instanceof HitsLoggerPlugin loggerPlugin) { | ||
instance.register(loggerPlugin.getHitsLoggers()); | ||
} | ||
} | ||
} | ||
|
||
/** Get singleton instance. */ | ||
public static HitsLoggerCreator getInstance() { | ||
return instance; | ||
} | ||
|
||
/** | ||
* Create a {@link HitsLogger} instance given the {@link LoggingHits} message from the {@link | ||
* com.yelp.nrtsearch.server.grpc.SearchRequest} | ||
* | ||
* @param grpcLoggingHits definition message | ||
* @return the corresponding hits logger | ||
*/ | ||
public HitsLogger createHitsLogger(LoggingHits grpcLoggingHits) { | ||
HitsLoggerProvider<?> provider = hitsLoggerMap.get(grpcLoggingHits.getName()); | ||
if (provider == null) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Unknown hits logger name [%s] is specified; The available hits loggers are %s", | ||
grpcLoggingHits.getName(), hitsLoggerMap.keySet())); | ||
} | ||
return provider.get(StructValueTransformer.transformStruct(grpcLoggingHits.getParams())); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/yelp/nrtsearch/server/luceneserver/logging/HitsLoggerFetchTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed 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 com.yelp.nrtsearch.server.luceneserver.logging; | ||
|
||
import com.yelp.nrtsearch.server.grpc.LoggingHits; | ||
import com.yelp.nrtsearch.server.grpc.SearchResponse; | ||
import com.yelp.nrtsearch.server.luceneserver.search.FetchTasks.FetchTask; | ||
import com.yelp.nrtsearch.server.luceneserver.search.SearchContext; | ||
import com.yelp.nrtsearch.server.plugins.HitsLoggerPlugin; | ||
import java.util.List; | ||
|
||
/** | ||
* Implementation of {@link FetchTask} which holds the required context to be able to log hits for a | ||
* search request. | ||
*/ | ||
public class HitsLoggerFetchTask implements FetchTask { | ||
private final HitsLogger hitsLogger; | ||
|
||
public HitsLoggerFetchTask(LoggingHits loggingHits) { | ||
this.hitsLogger = HitsLoggerCreator.getInstance().createHitsLogger(loggingHits); | ||
} | ||
|
||
/** | ||
* Calls {@link HitsLogger} that logs hits. The logic for logging is implemented via {@link | ||
* HitsLoggerPlugin} | ||
* | ||
* @param searchContext search context | ||
* @param hits list of hits for query response | ||
*/ | ||
@Override | ||
public void processAllHits(SearchContext searchContext, List<SearchResponse.Hit.Builder> hits) { | ||
hitsLogger.log(searchContext, hits); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/yelp/nrtsearch/server/luceneserver/logging/HitsLoggerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 Yelp Inc. | ||
* | ||
* Licensed 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 com.yelp.nrtsearch.server.luceneserver.logging; | ||
|
||
import com.yelp.nrtsearch.server.grpc.LoggingHits; | ||
import java.util.Map; | ||
|
||
/** | ||
* Interface for getting a {@link HitsLogger} implementation initialized with the given parameters | ||
* map. | ||
* | ||
* @param <T> HitsLogger type | ||
*/ | ||
public interface HitsLoggerProvider<T extends HitsLogger> { | ||
/** | ||
* Get task instance with the given parameters. | ||
* | ||
* @param params java native representation of {@link LoggingHits#getParams()} | ||
* @return {@link com.yelp.nrtsearch.server.luceneserver.logging.HitsLogger} instance | ||
*/ | ||
T get(Map<String, Object> params); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.