Search in sources :

Example 6 with NodeMetaDataParameters

use of org.alfresco.solr.client.NodeMetaDataParameters in project SearchServices by Alfresco.

the class SolrInformationServer method indexNode.

@Override
public void indexNode(Node node, boolean overwrite) throws IOException, AuthenticationException, JSONException {
    SolrQueryRequest request = null;
    UpdateRequestProcessor processor = null;
    try {
        request = getLocalSolrQueryRequest();
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        long start = System.nanoTime();
        if ((node.getStatus() == SolrApiNodeStatus.DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
            // fix up any secondary paths
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(node.getId());
            nmdp.setToNodeId(node.getId());
            List<NodeMetaData> nodeMetaDatas;
            if ((node.getStatus() == SolrApiNodeStatus.DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED)) {
                // Fake the empty node metadata for this parent deleted node
                NodeMetaData nodeMetaData = createDeletedNodeMetaData(node);
                nodeMetaDatas = Collections.singletonList(nodeMetaData);
            } else {
                nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
            }
            NodeMetaData nodeMetaData = null;
            if (!nodeMetaDatas.isEmpty()) {
                nodeMetaData = nodeMetaDatas.get(0);
                if (!(nodeMetaData.getTxnId() > node.getTxnId())) {
                    if (node.getStatus() == SolrApiNodeStatus.DELETED) {
                        try {
                            // Lock the node to ensure that no other trackers work with this node until this code completes.
                            if (!spinLock(nodeMetaData.getId(), 120000)) {
                                // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                                throw new Exception("Unable to acquire lock on nodeId:" + nodeMetaData.getId());
                            }
                            solrContentStore.removeDocFromContentStore(nodeMetaData);
                        } finally {
                            unlock(nodeMetaData.getId());
                        }
                    }
                }
            // else, the node has moved on to a later transaction, and it will be indexed later
            }
            if (log.isDebugEnabled()) {
                log.debug(".. deleting");
            }
            deleteNode(processor, request, node);
        }
        if ((node.getStatus() == SolrApiNodeStatus.UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED)) {
            log.info(".. updating");
            long nodeId = node.getId();
            try {
                if (!spinLock(nodeId, 120000)) {
                    // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                    throw new Exception("Unable to acquire lock on nodeId:" + nodeId);
                }
                NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
                nmdp.setFromNodeId(node.getId());
                nmdp.setToNodeId(node.getId());
                List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
                AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
                addDocCmd.overwrite = overwrite;
                if (!nodeMetaDatas.isEmpty()) {
                    NodeMetaData nodeMetaData = nodeMetaDatas.get(0);
                    if (!(nodeMetaData.getTxnId() > node.getTxnId())) {
                    /*
                        if (mayHaveChildren(nodeMetaData))
                        {
                            cascadeUpdate(nodeMetaData, overwrite, request, processor);
                        }
            				 */
                    }
                    if (node.getTxnId() == Long.MAX_VALUE) {
                        // This is a re-index. We need to clear the txnId from the pr
                        this.cleanContentCache.remove(nodeMetaData.getTxnId());
                    }
                    if ((node.getStatus() == SolrApiNodeStatus.UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
                        // check index control
                        Map<QName, PropertyValue> properties = nodeMetaData.getProperties();
                        StringPropertyValue pValue = (StringPropertyValue) properties.get(ContentModel.PROP_IS_INDEXED);
                        if (pValue != null) {
                            Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                            if (!isIndexed.booleanValue()) {
                                if (log.isDebugEnabled()) {
                                    log.debug(".. clearing unindexed");
                                }
                                deleteNode(processor, request, node);
                                SolrInputDocument doc = createNewDoc(nodeMetaData, DOC_TYPE_UNINDEXED_NODE);
                                solrContentStore.storeDocOnSolrContentStore(nodeMetaData, doc);
                                addDocCmd.solrDoc = doc;
                                processor.processAdd(addDocCmd);
                                long end = System.nanoTime();
                                this.trackerStats.addNodeTime(end - start);
                                return;
                            }
                        }
                        // Make sure any unindexed or error doc is removed.
                        if (log.isDebugEnabled()) {
                            log.debug(".. deleting node " + node.getId());
                        }
                        deleteNode(processor, request, node);
                        SolrInputDocument doc = createNewDoc(nodeMetaData, DOC_TYPE_NODE);
                        addToNewDocAndCache(nodeMetaData, doc);
                        addDocCmd.solrDoc = doc;
                        processor.processAdd(addDocCmd);
                    }
                }
            // Ends checking for a nodeMetaData
            } finally {
                unlock(nodeId);
            }
        }
        // Ends checking for updated or unknown node status
        long end = System.nanoTime();
        this.trackerStats.addNodeTime(end - start);
    } catch (Exception e) {
        log.warn("Node index failed and skipped for " + node.getId() + " in Tx " + node.getTxnId(), e);
        if (processor == null) {
            if (request == null) {
                request = getLocalSolrQueryRequest();
            }
            processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        }
        if (log.isDebugEnabled()) {
            log.debug(".. deleting on exception");
        }
        deleteNode(processor, request, node);
        AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
        addDocCmd.overwrite = overwrite;
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(FIELD_SOLR4_ID, PREFIX_ERROR + node.getId());
        doc.addField(FIELD_VERSION, "0");
        doc.addField(FIELD_DBID, node.getId());
        doc.addField(FIELD_INTXID, node.getTxnId());
        doc.addField(FIELD_EXCEPTION_MESSAGE, e.getMessage());
        doc.addField(FIELD_DOC_TYPE, DOC_TYPE_ERROR_NODE);
        StringWriter stringWriter = new StringWriter(4096);
        PrintWriter printWriter = new PrintWriter(stringWriter, true);
        try {
            e.printStackTrace(printWriter);
            String stack = stringWriter.toString();
            doc.addField(FIELD_EXCEPTION_STACK, stack.length() < 32766 ? stack : stack.substring(0, 32765));
        } finally {
            printWriter.close();
        }
        addDocCmd.solrDoc = doc;
        processor.processAdd(addDocCmd);
    } finally {
        if (processor != null) {
            processor.finish();
        }
        if (request != null) {
            request.close();
        }
    }
}
Also used : StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) NodeMetaData(org.alfresco.solr.client.NodeMetaData) QName(org.alfresco.service.namespace.QName) StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) MLTextPropertyValue(org.alfresco.solr.client.MLTextPropertyValue) ContentPropertyValue(org.alfresco.solr.client.ContentPropertyValue) MultiPropertyValue(org.alfresco.solr.client.MultiPropertyValue) PropertyValue(org.alfresco.solr.client.PropertyValue) JSONException(org.json.JSONException) AuthenticationException(org.alfresco.httpclient.AuthenticationException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) StringWriter(java.io.StringWriter) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) PrintWriter(java.io.PrintWriter)

Aggregations

NodeMetaData (org.alfresco.solr.client.NodeMetaData)6 NodeMetaDataParameters (org.alfresco.solr.client.NodeMetaDataParameters)6 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 IOException (java.io.IOException)4 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)4 IntArrayList (com.carrotsearch.hppc.IntArrayList)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 TenantAclIdDbId (org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId)3 StringPropertyValue (org.alfresco.solr.client.StringPropertyValue)3 SolrDocument (org.apache.solr.common.SolrDocument)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 AuthenticationException (org.alfresco.httpclient.AuthenticationException)2 QName (org.alfresco.service.namespace.QName)2 IOpenBitSet (org.alfresco.solr.adapters.IOpenBitSet)2 AclChangeSet (org.alfresco.solr.client.AclChangeSet)2 ContentPropertyValue (org.alfresco.solr.client.ContentPropertyValue)2 MLTextPropertyValue (org.alfresco.solr.client.MLTextPropertyValue)2