kotman12 commented on code in PR #2382: URL: https://github.com/apache/solr/pull/2382#discussion_r1597272168
########## solr/modules/monitor/src/java/org/apache/solr/monitor/update/MonitorUpdateRequestProcessor.java: ########## @@ -0,0 +1,257 @@ +/* + * + * * 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.monitor.update; + +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.InvertableType; +import org.apache.lucene.document.StoredValue; +import org.apache.lucene.index.DocValuesType; +import org.apache.lucene.index.IndexableField; +import org.apache.lucene.index.IndexableFieldType; +import org.apache.lucene.monitor.MonitorFields; +import org.apache.lucene.monitor.MonitorQuery; +import org.apache.lucene.monitor.Presearcher; +import org.apache.lucene.monitor.QCEVisitor; +import org.apache.lucene.util.BytesRef; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.util.JavaBinCodec; +import org.apache.solr.core.SolrCore; +import org.apache.solr.monitor.MonitorConstants; +import org.apache.solr.monitor.MonitorSchemaFields; +import org.apache.solr.monitor.SimpleQueryParser; +import org.apache.solr.schema.IndexSchema; +import org.apache.solr.schema.SchemaField; +import org.apache.solr.update.AddUpdateCommand; +import org.apache.solr.update.processor.UpdateRequestProcessor; + +public class MonitorUpdateRequestProcessor extends UpdateRequestProcessor { + + private final SolrCore core; + private final IndexSchema indexSchema; + private final Presearcher presearcher; + private final MonitorSchemaFields monitorSchemaFields; + + public MonitorUpdateRequestProcessor( + UpdateRequestProcessor next, SolrCore core, Presearcher presearcher) { + super(next); + this.core = core; + this.indexSchema = core.getLatestSchema(); + this.presearcher = presearcher; + this.monitorSchemaFields = new MonitorSchemaFields(indexSchema); + } + + @Override + public void processAdd(AddUpdateCommand cmd) throws IOException { + var solrInputDocument = cmd.getSolrInputDocument(); + var queryId = + (String) solrInputDocument.getFieldValue(indexSchema.getUniqueKeyField().getName()); + var queryFieldValue = solrInputDocument.getFieldValue(MonitorFields.MONITOR_QUERY); + if (queryFieldValue != null) { + var payload = + Optional.ofNullable(solrInputDocument.getFieldValue(MonitorFields.PAYLOAD)) + .map(Object::toString) + .orElse(null); + List<SolrInputDocument> children = + Optional.of(queryFieldValue) + .filter(String.class::isInstance) + .map(String.class::cast) + .map( + queryStr -> + new MonitorQuery( + queryId, SimpleQueryParser.parse(queryStr, core), queryStr, Map.of())) + .stream() + .flatMap(monitorQuery -> decompose(monitorQuery, payload)) + .map(this::toSolrInputDoc) + .collect(Collectors.toList()); + if (children.isEmpty()) { + throw new SolrException( + SolrException.ErrorCode.INVALID_STATE, "Query could not be decomposed"); + } + SolrInputDocument firstChild = children.get(0); + if (solrInputDocument.hasChildDocuments()) { + solrInputDocument.getChildDocuments().clear(); + } + solrInputDocument.addChildDocuments(children.stream().skip(1).collect(Collectors.toList())); + if (solrInputDocument.hasChildDocuments()) { + solrInputDocument + .getChildDocuments() + .forEach( + child -> + solrInputDocument.forEach( + field -> { + if (!MonitorFields.RESERVED_MONITOR_FIELDS.contains(field.getName())) { + child.addField(field.getName(), field.getValue()); + } + })); + solrInputDocument + .getChildDocuments() + .forEach( + child -> + child.setField( + indexSchema.getUniqueKeyField().getName(), + child.getFieldValue(MonitorFields.CACHE_ID))); + } + copyFirstChildToParent(solrInputDocument, firstChild); + } + super.processAdd(cmd); + } + + private void copyFirstChildToParent(SolrInputDocument parent, SolrInputDocument firstChild) { + parent.setField( + indexSchema.getUniqueKeyField().getName(), + firstChild.getFieldValue(MonitorFields.CACHE_ID)); + for (var firstBornInputField : firstChild) { + parent.setField(firstBornInputField.getName(), firstBornInputField.getValue()); + } + } + + private Stream<Document> decompose(MonitorQuery monitorQuery, String payload) { + return QCEVisitor.decompose(monitorQuery, MonitorConstants.QUERY_DECOMPOSER).stream() + .map( + qce -> { + Document doc = + presearcher.indexQuery(qce.getMatchQuery(), monitorQuery.getMetadata()); + doc.add(monitorSchemaFields.getQueryId().createField(qce.getQueryId())); + doc.add(monitorSchemaFields.getCacheId().createField(qce.getCacheId())); + doc.add( + monitorSchemaFields.getMonitorQuery().createField(monitorQuery.getQueryString())); + if (payload != null) { + doc.add(monitorSchemaFields.getPayload().createField(payload)); + } + return doc; + }); + } + + private SolrInputDocument toSolrInputDoc(Document subDocument) { Review Comment: Ah yes, this is probably the hairiest bit of the integration. I mentioned it briefly in the description but I should have brought more attention to it. I'll admit that I may be relying on some implementation details. Basically, lucene monitor gives you back some fields as token streams. You can "materialize" those back into a solr document field and if you make that field an `IndexableField`, you can pass that `TokenStream` directly to the lucene indexing chain because solr assumes an `IndexableField` needs no adaptation [here](https://github.com/apache/solr/blob/b8364e2309d0fa5120b87b945b70663b4f56ee14/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java#L59-L66). Perhaps this is an implementation detail? Either way, it seemed logical and I thought it would be quite a pain to serialize that token stream returned by lucene-monitor into a more conventional solr document field just to deserialize it again later on. The motivation behind `DerivedSkipTLogField` is that all lucene-monitor derived fields are just that, derived. So there is no need to write them to the tlog as long as we always perform the derivation. This assumption creates a dependency on the order of the `MonitorUpdateProcessor` which needs to be called out; it needs to be done _after_ the `DistributedUpdateProcessor`. But because derivation is cheap and the alternatives were ugly, I figured this was the best way. I've manually tested shutting down a solr instance with uncommitted changes and then bringing it back up with updates applied successfully. This makes me think this approach will work although automated tests should probably be added if this approach were to be accepted. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org