Search in sources :

Example 1 with AclChangeSets

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

the class AclTracker method checkRepoAndIndexConsistency.

/**
 * Checks the first and last TX time
 * @param state the state of this tracker
 * @throws AuthenticationException
 * @throws IOException
 * @throws JSONException
 */
private void checkRepoAndIndexConsistency(TrackerState state) throws AuthenticationException, IOException, JSONException {
    AclChangeSets firstChangeSets = null;
    if (state.getLastGoodChangeSetCommitTimeInIndex() == 0) {
        state.setCheckedLastAclTransactionTime(true);
        state.setCheckedFirstAclTransactionTime(true);
        log.info("No acl transactions found - no verification required");
        firstChangeSets = client.getAclChangeSets(null, 0L, null, 2000L, 1);
        if (!firstChangeSets.getAclChangeSets().isEmpty()) {
            AclChangeSet firstChangeSet = firstChangeSets.getAclChangeSets().get(0);
            long firstChangeSetCommitTime = firstChangeSet.getCommitTimeMs();
            state.setLastGoodChangeSetCommitTimeInIndex(firstChangeSetCommitTime);
            setLastChangeSetIdAndCommitTimeInTrackerState(firstChangeSets, state);
        }
    }
    if (!state.isCheckedFirstAclTransactionTime()) {
        firstChangeSets = client.getAclChangeSets(null, 0l, null, 2000L, 1);
        if (!firstChangeSets.getAclChangeSets().isEmpty()) {
            AclChangeSet firstAclChangeSet = firstChangeSets.getAclChangeSets().get(0);
            long firstAclTxId = firstAclChangeSet.getId();
            long firstAclTxCommitTime = firstAclChangeSet.getCommitTimeMs();
            int setSize = this.infoSrv.getAclTxDocsSize("" + firstAclTxId, "" + firstAclTxCommitTime);
            if (setSize == 0) {
                log.error("First acl transaction was not found with the correct timestamp.");
                log.error("SOLR has successfully connected to your repository  however the SOLR indexes and repository database do not match.");
                log.error("If this is a new or rebuilt database your SOLR indexes also need to be re-built to match the database.");
                log.error("You can also check your SOLR connection details in solrcore.properties.");
                throw new AlfrescoRuntimeException("Initial acl transaction not found with correct timestamp");
            } else if (setSize == 1) {
                state.setCheckedFirstTransactionTime(true);
                log.info("Verified first acl transaction and timestamp in index");
            } else {
                log.warn("Duplicate initial acl transaction found with correct timestamp");
            }
        }
    }
    // Checks that the last aclTxId in solr is <= last aclTxId in repo
    if (!state.isCheckedLastAclTransactionTime()) {
        if (firstChangeSets == null) {
            firstChangeSets = client.getAclChangeSets(null, 0l, null, 2000L, 1);
        }
        setLastChangeSetIdAndCommitTimeInTrackerState(firstChangeSets, state);
        Long maxChangeSetCommitTimeInRepo = firstChangeSets.getMaxChangeSetCommitTime();
        Long maxChangeSetIdInRepo = firstChangeSets.getMaxChangeSetId();
        if (maxChangeSetCommitTimeInRepo != null && maxChangeSetIdInRepo != null) {
            AclChangeSet maxAclTxInIndex = this.infoSrv.getMaxAclChangeSetIdAndCommitTimeInIndex();
            if (maxAclTxInIndex.getCommitTimeMs() > maxChangeSetCommitTimeInRepo) {
                log.error("Last acl transaction was found in index with timestamp later than that of repository.");
                log.error("Max Acl Tx In Index: " + maxAclTxInIndex.getId() + ", In Repo: " + maxChangeSetIdInRepo);
                log.error("Max Acl Tx Commit Time In Index: " + maxAclTxInIndex.getCommitTimeMs() + ", In Repo: " + maxChangeSetCommitTimeInRepo);
                log.error("SOLR has successfully connected to your repository  however the SOLR indexes and repository database do not match.");
                log.error("If this is a new or rebuilt database your SOLR indexes also need to be re-built to match the database.");
                log.error("You can also check your SOLR connection details in solrcore.properties.");
                throw new AlfrescoRuntimeException("Last acl transaction found in index with incorrect timestamp");
            } else {
                state.setCheckedLastAclTransactionTime(true);
                log.info("Verified last acl transaction timestamp in index less than or equal to that of repository.");
            }
        }
    }
}
Also used : AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AclChangeSets(org.alfresco.solr.client.AclChangeSets) AclChangeSet(org.alfresco.solr.client.AclChangeSet)

Example 2 with AclChangeSets

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

the class AclTracker method trackAclChangeSets.

/**
 * @throws AuthenticationException
 * @throws IOException
 * @throws JSONException
 */
protected void trackAclChangeSets() throws AuthenticationException, IOException, JSONException {
    long startElapsed = System.nanoTime();
    boolean upToDate = false;
    AclChangeSets aclChangeSets;
    BoundedDeque<AclChangeSet> changeSetsFound = new BoundedDeque<AclChangeSet>(100);
    HashSet<AclChangeSet> changeSetsIndexed = new LinkedHashSet<AclChangeSet>();
    long totalAclCount = 0;
    int aclCount = 0;
    do {
        try {
            getWriteLock().acquire();
            /*
                * We acquire the tracker state again here and set it globally. This is because the
                * tracker state could have been invalidated due to a rollback by the CommitTracker.
                * In this case the state will revert to the last transaction state record in the index.
                */
            this.state = getTrackerState();
            Long fromCommitTime = getChangeSetFromCommitTime(changeSetsFound, state.getLastGoodChangeSetCommitTimeInIndex());
            aclChangeSets = getSomeAclChangeSets(changeSetsFound, fromCommitTime, TIME_STEP_1_HR_IN_MS, 2000, state.getTimeToStopIndexing());
            setLastChangeSetIdAndCommitTimeInTrackerState(aclChangeSets, state);
            log.info("Scanning Acl change sets ...");
            if (aclChangeSets.getAclChangeSets().size() > 0) {
                log.info(".... from " + aclChangeSets.getAclChangeSets().get(0));
                log.info(".... to " + aclChangeSets.getAclChangeSets().get(aclChangeSets.getAclChangeSets().size() - 1));
            } else {
                log.info(".... none found after lastTxCommitTime " + fromCommitTime);
            }
            ArrayList<AclChangeSet> changeSetBatch = new ArrayList<AclChangeSet>();
            for (AclChangeSet changeSet : aclChangeSets.getAclChangeSets()) {
                boolean isInIndex = (changeSet.getCommitTimeMs() <= state.getLastIndexedChangeSetCommitTime() && infoSrv.aclChangeSetInIndex(changeSet.getId(), true));
                if (isInIndex) {
                    changeSetsFound.add(changeSet);
                } else {
                    // correctly next time
                    if (changeSet.getCommitTimeMs() > state.getTimeToStopIndexing()) {
                        upToDate = true;
                        break;
                    }
                    changeSetBatch.add(changeSet);
                    if (getAclCount(changeSetBatch) > changeSetAclsBatchSize) {
                        aclCount += indexBatchOfChangeSets(changeSetBatch);
                        totalAclCount += aclCount;
                        for (AclChangeSet scheduled : changeSetBatch) {
                            changeSetsFound.add(scheduled);
                            changeSetsIndexed.add(scheduled);
                        }
                        changeSetBatch.clear();
                    }
                }
                if (aclCount > batchCount) {
                    if (super.infoSrv.getRegisteredSearcherCount() < getMaxLiveSearchers()) {
                        indexAclChangeSetAfterAsynchronous(changeSetsIndexed, state);
                        long endElapsed = System.nanoTime();
                        trackerStats.addElapsedAclTime(aclCount, endElapsed - startElapsed);
                        startElapsed = endElapsed;
                        aclCount = 0;
                    }
                }
                checkShutdown();
            }
            if (!changeSetBatch.isEmpty()) {
                if (getAclCount(changeSetBatch) > 0) {
                    aclCount += indexBatchOfChangeSets(changeSetBatch);
                    totalAclCount += aclCount;
                }
                for (AclChangeSet scheduled : changeSetBatch) {
                    changeSetsFound.add(scheduled);
                    changeSetsIndexed.add(scheduled);
                }
                changeSetBatch.clear();
            }
            if (changeSetsIndexed.size() > 0) {
                indexAclChangeSetAfterAsynchronous(changeSetsIndexed, state);
                long endElapsed = System.nanoTime();
                trackerStats.addElapsedAclTime(aclCount, endElapsed - startElapsed);
                startElapsed = endElapsed;
                aclCount = 0;
            }
        } catch (InterruptedException e) {
            throw new IOException(e);
        } finally {
            getWriteLock().release();
        }
    } while ((aclChangeSets.getAclChangeSets().size() > 0) && (upToDate == false));
    log.info("total number of acls updated: " + totalAclCount);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BoundedDeque(org.alfresco.solr.BoundedDeque) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AclChangeSets(org.alfresco.solr.client.AclChangeSets) AclChangeSet(org.alfresco.solr.client.AclChangeSet)

Example 3 with AclChangeSets

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

the class AclTracker method getAclsForDbAclTransaction.

/**
 * @param acltxid Long
 * @return List<Long>
 */
public List<Long> getAclsForDbAclTransaction(Long acltxid) {
    try {
        ArrayList<Long> answer = new ArrayList<Long>();
        AclChangeSets changeSet = client.getAclChangeSets(null, acltxid, null, acltxid + 1, 1);
        List<Acl> acls = client.getAcls(changeSet.getAclChangeSets(), null, Integer.MAX_VALUE);
        for (Acl acl : acls) {
            answer.add(acl.getId());
        }
        return answer;
    } catch (IOException e) {
        throw new AlfrescoRuntimeException("Failed to get acls", e);
    } catch (JSONException e) {
        throw new AlfrescoRuntimeException("Failed to get acls", e);
    } catch (AuthenticationException e) {
        throw new AlfrescoRuntimeException("Failed to get acls", e);
    }
}
Also used : AuthenticationException(org.alfresco.httpclient.AuthenticationException) ArrayList(java.util.ArrayList) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONException(org.json.JSONException) AclChangeSets(org.alfresco.solr.client.AclChangeSets) Acl(org.alfresco.solr.client.Acl) IOException(java.io.IOException)

Example 4 with AclChangeSets

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

the class AclTrackerTest method mockChangeSets.

// TODO: Commented out, approach not working
// Rethink or finish later.
/*@Test
    public void checkTrackingWhenAclChangeSetsToReindex() throws IllegalAccessException, IOException, AuthenticationException, JSONException
    {
        @SuppressWarnings("unchecked")
        ConcurrentLinkedQueue<Long> aclChangeSetsToReindex = (ConcurrentLinkedQueue<Long>)
        FieldUtils.readField(tracker, "aclChangeSetsToReindex", true);
        aclChangeSetsToReindex.add(301L);
        aclChangeSetsToReindex.add(302L);
        aclChangeSetsToReindex.add(303L);
        
        // tracker will loop through list of ACLs to reindex, and get changesets for each changeset ID.
        when(client.getAclChangeSets(null, 201L, null, 202L, 1)).thenReturn(mockChangeSets(201L));
        when(client.getAclChangeSets(null, 202L, null, 203L, 1)).thenReturn(mockChangeSets(202L));
        when(client.getAclChangeSets(null, 203L, null, 204L, 1)).thenReturn(mockChangeSets(203L));
    
        when(client.getAcls(Collections.singletonList(new AclChangeSet(201L, 0L, 1)), null, Integer.MAX_VALUE)).thenReturn(Collections.singletonList(new Acl(201L, 0L)));
        when(client.getAcls(Collections.singletonList(new AclChangeSet(202L, 0L, 1)), null, Integer.MAX_VALUE)).thenReturn(Collections.singletonList(new Acl(202L, 0L)));
        when(client.getAcls(Collections.singletonList(new AclChangeSet(203L, 0L, 1)), null, Integer.MAX_VALUE)).thenReturn(Collections.singletonList(new Acl(203L, 0L)));
    
        // TODO: ...more...
        
        // Invoke the behaviour we're testing
        testTrackChangesRan();
        
        verify(informationServer).deleteByAclChangeSetId(301L);
        verify(informationServer).deleteByAclChangeSetId(302L);
        verify(informationServer).deleteByAclChangeSetId(303L);
        
        // TODO: verify checkShutdown
        verify(informationServer).commit();
    }*/
private AclChangeSets mockChangeSets(long id) {
    List<AclChangeSet> changeSets = Collections.singletonList(new AclChangeSet(id, 0L, 1));
    AclChangeSets acs = mock(AclChangeSets.class);
    when(acs.getAclChangeSets()).thenReturn(changeSets);
    return acs;
}
Also used : AclChangeSets(org.alfresco.solr.client.AclChangeSets) AclChangeSet(org.alfresco.solr.client.AclChangeSet)

Example 5 with AclChangeSets

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

the class AclTracker method indexAclChangeSets.

protected void indexAclChangeSets() throws AuthenticationException, IOException, JSONException {
    boolean requiresCommit = false;
    while (aclChangeSetsToIndex.peek() != null) {
        Long aclChangeSetId = aclChangeSetsToIndex.poll();
        if (aclChangeSetId != null) {
            AclChangeSets aclChangeSets = client.getAclChangeSets(null, aclChangeSetId, null, aclChangeSetId + 1, 1);
            if ((aclChangeSets.getAclChangeSets().size() > 0) && aclChangeSetId.equals(aclChangeSets.getAclChangeSets().get(0).getId())) {
                AclChangeSet changeSet = aclChangeSets.getAclChangeSets().get(0);
                List<Acl> acls = client.getAcls(Collections.singletonList(changeSet), null, Integer.MAX_VALUE);
                for (Acl acl : acls) {
                    List<AclReaders> readers = client.getAclReaders(Collections.singletonList(acl));
                    indexAcl(readers, false);
                }
                this.infoSrv.indexAclTransaction(changeSet, false);
                requiresCommit = true;
            }
        }
        checkShutdown();
    }
    if (requiresCommit) {
        checkShutdown();
    // this.infoSrv.commit();
    }
}
Also used : AclChangeSets(org.alfresco.solr.client.AclChangeSets) Acl(org.alfresco.solr.client.Acl) AclChangeSet(org.alfresco.solr.client.AclChangeSet) AclReaders(org.alfresco.solr.client.AclReaders)

Aggregations

AclChangeSets (org.alfresco.solr.client.AclChangeSets)8 AclChangeSet (org.alfresco.solr.client.AclChangeSet)6 Acl (org.alfresco.solr.client.Acl)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 BoundedDeque (org.alfresco.solr.BoundedDeque)2 AclReaders (org.alfresco.solr.client.AclReaders)2 LinkedHashSet (java.util.LinkedHashSet)1 AuthenticationException (org.alfresco.httpclient.AuthenticationException)1 IOpenBitSet (org.alfresco.solr.adapters.IOpenBitSet)1 JSONException (org.json.JSONException)1