Search in sources :

Example 1 with AlfrescoCoreAdminHandler

use of org.alfresco.solr.AlfrescoCoreAdminHandler in project SearchServices by Alfresco.

the class ConsistencyComponent method process.

@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    AlfrescoCoreAdminHandler adminHandler = (AlfrescoCoreAdminHandler) req.getCore().getCoreContainer().getMultiCoreHandler();
    boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);
    MetadataTracker metaTrkr = adminHandler.getTrackerRegistry().getTrackerForCore(req.getCore().getName(), MetadataTracker.class);
    if (metaTrkr != null && !isShard) {
        TrackerState metadataTrkrState = metaTrkr.getTrackerState();
        long lastIndexedTx = metadataTrkrState.getLastIndexedTxId();
        long lastIndexTxCommitTime = metadataTrkrState.getLastIndexedTxCommitTime();
        long lastTxIdOnServer = metadataTrkrState.getLastTxIdOnServer();
        long transactionsToDo = lastTxIdOnServer - lastIndexedTx;
        if (transactionsToDo < 0) {
            transactionsToDo = 0;
        }
        rb.rsp.add("lastIndexedTx", lastIndexedTx);
        rb.rsp.add("lastIndexedTxTime", lastIndexTxCommitTime);
        rb.rsp.add("txRemaining", transactionsToDo);
    }
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler) TrackerState(org.alfresco.solr.TrackerState) MetadataTracker(org.alfresco.solr.tracker.MetadataTracker)

Example 2 with AlfrescoCoreAdminHandler

use of org.alfresco.solr.AlfrescoCoreAdminHandler in project SearchServices by Alfresco.

the class CachedDocTransformer method transform.

/* (non-Javadoc)
     * @see org.apache.solr.response.transform.DocTransformer#transform(org.apache.solr.common.SolrDocument, int)
     */
@Override
public void transform(SolrDocument doc, int docid, float score) throws IOException {
    SolrInputDocument cachedDoc = null;
    try {
        String id = getFieldValueString(doc, FIELD_SOLR4_ID);
        TenantAclIdDbId tenantAndDbId = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
        CoreContainer coreContainer = context.getSearcher().getCore().getCoreContainer();
        AlfrescoCoreAdminHandler coreAdminHandler = (AlfrescoCoreAdminHandler) coreContainer.getMultiCoreHandler();
        SolrInformationServer srv = (SolrInformationServer) coreAdminHandler.getInformationServers().get(context.getSearcher().getCore().getName());
        SolrContentStore solrContentStore = srv.getSolrContentStore();
        cachedDoc = solrContentStore.retrieveDocFromSolrContentStore(tenantAndDbId.tenant, tenantAndDbId.dbId);
    } catch (StringIndexOutOfBoundsException e) {
    // ignore invalid forms ....
    }
    if (cachedDoc != null) {
        Collection<String> fieldNames = cachedDoc.getFieldNames();
        for (String fieldName : fieldNames) {
            SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
            if (schemaField != null) {
                doc.removeFields(fieldName);
                if (schemaField.multiValued()) {
                    int index = fieldName.lastIndexOf("@{");
                    if (index == -1) {
                        doc.addField(fieldName, cachedDoc.getFieldValues(fieldName));
                    } else {
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        Collection<Object> values = cachedDoc.getFieldValues(fieldName);
                        ArrayList<Object> newValues = new ArrayList<Object>(values.size());
                        for (Object value : values) {
                            if (value instanceof String) {
                                String stringValue = (String) value;
                                int start = stringValue.lastIndexOf('\u0000');
                                if (start == -1) {
                                    newValues.add(stringValue);
                                } else {
                                    newValues.add(stringValue.substring(start + 1));
                                }
                            } else {
                                newValues.add(value);
                            }
                        }
                        doc.removeFields(alfrescoFieldName);
                        doc.addField(alfrescoFieldName, newValues);
                    }
                } else {
                    int index = fieldName.lastIndexOf("@{");
                    if (index == -1) {
                        doc.addField(fieldName, cachedDoc.getFieldValue(fieldName));
                    } else {
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        alfrescoFieldName = alfrescoFieldName.contains(":") ? alfrescoFieldName.replace(":", "_") : alfrescoFieldName;
                        Object value = cachedDoc.getFieldValue(fieldName);
                        if (value instanceof String) {
                            String stringValue = (String) value;
                            int start = stringValue.lastIndexOf('\u0000');
                            if (start == -1) {
                                doc.removeFields(alfrescoFieldName);
                                doc.addField(alfrescoFieldName, stringValue);
                            } else {
                                doc.removeFields(alfrescoFieldName);
                                doc.addField(alfrescoFieldName, stringValue.substring(start + 1));
                            }
                        } else {
                            doc.removeFields(alfrescoFieldName);
                            doc.addField(alfrescoFieldName, value);
                        }
                    }
                }
            }
        }
    }
}
Also used : TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) SolrContentStore(org.alfresco.solr.content.SolrContentStore) ArrayList(java.util.ArrayList) SolrInformationServer(org.alfresco.solr.SolrInformationServer) SchemaField(org.apache.solr.schema.SchemaField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) CoreContainer(org.apache.solr.core.CoreContainer) AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler)

Example 3 with AlfrescoCoreAdminHandler

use of org.alfresco.solr.AlfrescoCoreAdminHandler in project SearchServices by Alfresco.

the class AlfrescoSolrHighlighter method getDocument.

private Document getDocument(Document doc, SolrQueryRequest req) throws IOException {
    try {
        String id = getFieldValueString(doc, FIELD_SOLR4_ID);
        TenantAclIdDbId tenantAndDbId = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
        CoreContainer coreContainer = req.getSearcher().getCore().getCoreContainer();
        AlfrescoCoreAdminHandler coreAdminHandler = (AlfrescoCoreAdminHandler) coreContainer.getMultiCoreHandler();
        SolrInformationServer srv = (SolrInformationServer) coreAdminHandler.getInformationServers().get(req.getSearcher().getCore().getName());
        SolrContentStore solrContentStore = srv.getSolrContentStore();
        SolrInputDocument sid = solrContentStore.retrieveDocFromSolrContentStore(tenantAndDbId.tenant, tenantAndDbId.dbId);
        if (sid == null) {
            sid = new SolrInputDocument();
            sid.addField(FIELD_SOLR4_ID, id);
            sid.addField("_version_", 0);
            return DocumentBuilder.toDocument(sid, req.getSchema());
        } else {
            sid.removeField(FIELD_SOLR4_ID);
            sid.addField(FIELD_SOLR4_ID, id);
            return DocumentBuilder.toDocument(sid, req.getSchema());
        }
    } catch (StringIndexOutOfBoundsException e) {
        throw new IOException(e);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) CoreContainer(org.apache.solr.core.CoreContainer) AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler) SolrContentStore(org.alfresco.solr.content.SolrContentStore) IOException(java.io.IOException) SolrInformationServer(org.alfresco.solr.SolrInformationServer)

Example 4 with AlfrescoCoreAdminHandler

use of org.alfresco.solr.AlfrescoCoreAdminHandler in project SearchServices by Alfresco.

the class AlfrescoSolrTrackerRollbackTest method testTrackers.

@Test
public void testTrackers() throws Exception {
    AlfrescoCoreAdminHandler alfrescoCoreAdminHandler = (AlfrescoCoreAdminHandler) h.getCore().getCoreContainer().getMultiCoreHandler();
    /*
        * Create and index an AclChangeSet.
        */
    AclChangeSet aclChangeSet = getAclChangeSet(1, 1);
    Acl acl = getAcl(aclChangeSet);
    Acl acl2 = getAcl(aclChangeSet);
    AclReaders aclReaders = getAclReaders(aclChangeSet, acl, list("joel"), list("phil"), null);
    AclReaders aclReaders2 = getAclReaders(aclChangeSet, acl2, list("jim"), list("phil"), null);
    indexAclChangeSet(aclChangeSet, list(acl, acl2), list(aclReaders, aclReaders2));
    // Check for the ACL state stamp.
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!ACLTX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_ACLTXID, aclChangeSet.getId(), aclChangeSet.getId() + 1, true, false), BooleanClause.Occur.MUST));
    BooleanQuery waitForQuery = builder.build();
    waitForDocCount(waitForQuery, 1, MAX_WAIT_TIME);
    TrackerRegistry trackerRegistry = alfrescoCoreAdminHandler.getTrackerRegistry();
    Collection<Tracker> trackers = trackerRegistry.getTrackersForCore(h.getCore().getName());
    MetadataTracker metadataTracker = null;
    CommitTracker commitTracker = null;
    for (Tracker tracker : trackers) {
        if (tracker instanceof MetadataTracker) {
            metadataTracker = (MetadataTracker) tracker;
        } else if (tracker instanceof CommitTracker) {
            commitTracker = (CommitTracker) tracker;
        }
    }
    /*
        * Create and index a Transaction
        */
    // First create a transaction.
    Transaction txn = getTransaction(0, 3, 1);
    // Next create two nodes to update for the transaction
    Node folderNode = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node fileNode = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node errorNode = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    // Next create the NodeMetaData for each node. TODO: Add more metadata
    NodeMetaData folderMetaData = getNodeMetaData(folderNode, txn, acl, "mike", null, false);
    NodeMetaData fileMetaData = getNodeMetaData(fileNode, txn, acl, "mike", ancestors(folderMetaData.getNodeRef()), false);
    // The errorNodeMetaData will cause an exception.
    NodeMetaData errorMetaData = getNodeMetaData(errorNode, txn, acl, "lisa", ancestors(folderMetaData.getNodeRef()), true);
    // Index the transaction, nodes, and nodeMetaDatas.
    // Note that the content is automatically created by the test framework.
    indexTransaction(txn, list(errorNode, folderNode, fileNode), list(errorMetaData, folderMetaData, fileMetaData));
    // Check for the TXN state stamp.
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!TX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_TXID, txn.getId(), txn.getId() + 1, true, false), BooleanClause.Occur.MUST));
    waitForQuery = builder.build();
    waitForDocCount(waitForQuery, 1, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 2, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", Long.toString(fileNode.getId()))), 1, MAX_WAIT_TIME);
    // Stop the commit tracker
    // This will allow the metadata tracker to index the next transaction and leave it uncommitted in the index.
    commitTracker.getRunLock().acquire();
    Transaction rollbackTxn = getTransaction(0, 1, 2);
    Node rollbackNode = getNode(rollbackTxn, acl, Node.SolrApiNodeStatus.UPDATED);
    NodeMetaData rollbackMetaData = getNodeMetaData(rollbackNode, rollbackTxn, acl, "mike", null, false);
    indexTransaction(rollbackTxn, list(rollbackNode), list(rollbackMetaData));
    long cycles = metadataTracker.getTrackerState().getTrackerCycles();
    // Wait three tracker cycles
    while (metadataTracker.getTrackerState().getTrackerCycles() < cycles + 3) {
        Thread.sleep(1000);
    }
    // Take the rollback transaction out of the queue so it doesn't get re-indexed following the rollback.
    // This will prove the rollback transaction was rolled back
    SOLRAPIQueueClient.transactionQueue.remove(rollbackTxn);
    metadataTracker.setRollback(true);
    commitTracker.getRunLock().release();
    while (commitTracker.getRollbackCount() == 0) {
        Thread.sleep(1000);
    }
    // The rollback occurred
    // Let's add another node and acl
    AclChangeSet afterRollbackAclChangeSet = getAclChangeSet(1, 10);
    Acl afterRollbackAcl = getAcl(aclChangeSet);
    AclReaders afterRollbackAclReaders = getAclReaders(afterRollbackAclChangeSet, afterRollbackAcl, list("joel"), list("phil"), null);
    indexAclChangeSet(afterRollbackAclChangeSet, list(afterRollbackAcl), list(afterRollbackAclReaders));
    Transaction afterRollbackTxn = getTransaction(0, 1, 3);
    Node afterRollbackNode = getNode(afterRollbackTxn, acl, Node.SolrApiNodeStatus.UPDATED);
    // Next create the NodeMetaData for each node. TODO: Add more metadata
    NodeMetaData afterRollbackMetaData = getNodeMetaData(afterRollbackNode, afterRollbackTxn, acl, "mike", null, false);
    // Index the transaction, nodes, and nodeMetaDatas.
    // Note that the content is automatically created by the test framework.
    indexTransaction(afterRollbackTxn, list(afterRollbackNode), list(afterRollbackMetaData));
    // Wait for the node to appear
    // Assert the rolled back transaction is not in the index.
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 3, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", Long.toString(afterRollbackNode.getId()))), 1, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", Long.toString(rollbackNode.getId()))), 0, MAX_WAIT_TIME);
    // Check for the ACL state stamp.
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!ACLTX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_ACLTXID, afterRollbackAclChangeSet.getId(), afterRollbackAclChangeSet.getId() + 1, true, false), BooleanClause.Occur.MUST));
    waitForQuery = builder.build();
    waitForDocCount(waitForQuery, 1, MAX_WAIT_TIME);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) NodeMetaData(org.alfresco.solr.client.NodeMetaData) AlfrescoSolrUtils.getNodeMetaData(org.alfresco.solr.AlfrescoSolrUtils.getNodeMetaData) Node(org.alfresco.solr.client.Node) AlfrescoSolrUtils.getNode(org.alfresco.solr.AlfrescoSolrUtils.getNode) Acl(org.alfresco.solr.client.Acl) AlfrescoSolrUtils.getAcl(org.alfresco.solr.AlfrescoSolrUtils.getAcl) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler) Transaction(org.alfresco.solr.client.Transaction) AlfrescoSolrUtils.getTransaction(org.alfresco.solr.AlfrescoSolrUtils.getTransaction) AlfrescoSolrUtils.getAclChangeSet(org.alfresco.solr.AlfrescoSolrUtils.getAclChangeSet) AlfrescoSolrUtils.indexAclChangeSet(org.alfresco.solr.AlfrescoSolrUtils.indexAclChangeSet) AclChangeSet(org.alfresco.solr.client.AclChangeSet) AlfrescoSolrUtils.getAclReaders(org.alfresco.solr.AlfrescoSolrUtils.getAclReaders) AclReaders(org.alfresco.solr.client.AclReaders) Test(org.junit.Test)

Example 5 with AlfrescoCoreAdminHandler

use of org.alfresco.solr.AlfrescoCoreAdminHandler in project SearchServices by Alfresco.

the class MetadataTrackerTest method setUp.

@Before
public void setUp() throws Exception {
    doReturn("workspace://SpacesStore").when(props).getProperty("alfresco.stores");
    when(srv.getTrackerStats()).thenReturn(trackerStats);
    this.metadataTracker = spy(new MetadataTracker(props, repositoryClient, coreName, srv));
    ModelTracker modelTracker = mock(ModelTracker.class);
    when(modelTracker.hasModels()).thenReturn(true);
    AlfrescoCoreAdminHandler adminHandler = mock(AlfrescoCoreAdminHandler.class);
    TrackerRegistry registry = new TrackerRegistry();
    registry.setModelTracker(modelTracker);
    when(adminHandler.getTrackerRegistry()).thenReturn(registry);
    when(srv.getAdminHandler()).thenReturn(adminHandler);
}
Also used : AlfrescoCoreAdminHandler(org.alfresco.solr.AlfrescoCoreAdminHandler) Before(org.junit.Before)

Aggregations

AlfrescoCoreAdminHandler (org.alfresco.solr.AlfrescoCoreAdminHandler)9 CoreContainer (org.apache.solr.core.CoreContainer)6 SolrInformationServer (org.alfresco.solr.SolrInformationServer)5 TenantAclIdDbId (org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId)3 SolrContentStore (org.alfresco.solr.content.SolrContentStore)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)1 AlfrescoSolrUtils.getAcl (org.alfresco.solr.AlfrescoSolrUtils.getAcl)1 AlfrescoSolrUtils.getAclChangeSet (org.alfresco.solr.AlfrescoSolrUtils.getAclChangeSet)1 AlfrescoSolrUtils.getAclReaders (org.alfresco.solr.AlfrescoSolrUtils.getAclReaders)1 AlfrescoSolrUtils.getNode (org.alfresco.solr.AlfrescoSolrUtils.getNode)1 AlfrescoSolrUtils.getNodeMetaData (org.alfresco.solr.AlfrescoSolrUtils.getNodeMetaData)1 AlfrescoSolrUtils.getTransaction (org.alfresco.solr.AlfrescoSolrUtils.getTransaction)1 AlfrescoSolrUtils.indexAclChangeSet (org.alfresco.solr.AlfrescoSolrUtils.indexAclChangeSet)1 TrackerState (org.alfresco.solr.TrackerState)1 Acl (org.alfresco.solr.client.Acl)1 AclChangeSet (org.alfresco.solr.client.AclChangeSet)1 AclReaders (org.alfresco.solr.client.AclReaders)1 Node (org.alfresco.solr.client.Node)1