alfresco-community-repo/source/java/org/alfresco/repo/node/index/AVMFullIndexRecoveryComponent.java
Kevin Roast 16861e9117 Merged V3.0 to HEAD
12123: Merged V2.2 to V3.0
      11466: Fixed sql-query DELETE syntax
      11614: Flush after putChild, fix for ETWOTWO-777
      11641: Merged V2.1 to V2.2
         11632: Improvements for AVM index FULL and AUTO rebuild. 
      11646: Upgrade scripts tweaks:
      11650: Added unit test to confirm fix of ETWOTWO-740
      11674: Added missing EHCache definitions for QName, Namespace and Locale caches
      11825: Fixed Eclipse classpath addition of path for Oracle JDBC driver
   12125: ETHREEOH-899: Image transformations do not follow Options
   12127: Merged V2.2 to V3.0
      11675: Node DAO optimizations
      11680: Full Fix for ETWOTWO-777 + more protection for nested write transactions beneath read transactions.
      11729: AVM creates and deletes no longer update the directory mod time - ETWOTWO-801
      11738: Fix for ETWOTWO - fixed check for TX propagation mode
      11748: Fixed ETWOTWO-578: RepositoryWebService fetchMore() does not fetch last node
      11749: Incorporate feedback from ACT-5440: MySQL-specific tweaks to the upgrade scripts
      11750: Moved t_qnames_dyn section to after t_qnames
      11752: Fixed ETWOTWO-734: ImporterComponent uses Lucene queries
      11785: Build Fix:Remove auto created person TX commit fro DB
      11853: Fix for ETWOTWO-687 - missed a case when generating lists of actions for modified files list
      11940: Stress test main method for ETWOTWO-744
      11950: Fixed ETWOTWO-909 and ETWOTWO-911
      11987: Dirty checking for attribute related  DAOs
      12008: Fixed test for transaction-requiring AttributeService
   12128: Merged V2.2 to V3.0
      11530: Merged V2.1 to V2.2
         11499: Defensive clear of the security context to avoid any ticket sharing for a given user - ETWOTWO-326

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@12501 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2008-12-18 17:23:44 +00:00

325 lines
10 KiB
Java

package org.alfresco.repo.node.index;
import java.util.LinkedHashMap;
import java.util.List;
import org.alfresco.repo.node.index.FullIndexRecoveryComponent.RecoveryMode;
import org.alfresco.repo.search.AVMSnapShotTriggeredIndexingMethodInterceptor;
import org.alfresco.repo.search.IndexMode;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avm.AVMStoreDescriptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Check and recover the indexes for AVM stores
*
* @author andyh
*/
public class AVMFullIndexRecoveryComponent extends AbstractReindexComponent
{
private static Log logger = LogFactory.getLog(AVMFullIndexRecoveryComponent.class);
private RecoveryMode recoveryMode;
private boolean lockServer;
private AVMService avmService;
private AVMSnapShotTriggeredIndexingMethodInterceptor avmSnapShotTriggeredIndexingMethodInterceptor;
/**
* Set the type of recovery to perform. Default is {@link RecoveryMode#VALIDATE to validate} the indexes only.
*
* @param recoveryMode
* one of the {@link RecoveryMode } values
*/
public void setRecoveryMode(String recoveryMode)
{
this.recoveryMode = RecoveryMode.valueOf(recoveryMode);
}
/**
* Set this on to put the server into READ-ONLY mode for the duration of the index recovery. The default is
* <tt>true</tt>, i.e. the server will be locked against further updates.
*
* @param lockServer
* true to force the server to be read-only
*/
public void setLockServer(boolean lockServer)
{
this.lockServer = lockServer;
}
public void setAvmService(AVMService avmService)
{
this.avmService = avmService;
}
public void setAvmSnapShotTriggeredIndexingMethodInterceptor(AVMSnapShotTriggeredIndexingMethodInterceptor avmSnapShotTriggeredIndexingMethodInterceptor)
{
this.avmSnapShotTriggeredIndexingMethodInterceptor = avmSnapShotTriggeredIndexingMethodInterceptor;
}
@Override
protected void reindexImpl()
{
processStores();
}
private void processStores()
{
List<AVMStoreDescriptor> stores = avmService.getStores();
LinkedHashMap<String, RecoveryMode> actions = new LinkedHashMap<String, RecoveryMode>();
if (stores.size() == 0)
{
return;
}
switch (recoveryMode)
{
case AUTO:
case VALIDATE:
int count = 0;
int tracker = -1;
if (logger.isDebugEnabled())
{
logger.debug("Checking indexes for AVM Stores: " + recoveryMode);
}
for (AVMStoreDescriptor store : stores)
{
if (isShuttingDown())
{
return;
}
actions.put(store.getName(), checkStore(store.getName()));
count++;
if (count * 10l / stores.size() > tracker)
{
tracker = (int) (count * 10l / stores.size());
if (logger.isDebugEnabled())
{
logger.debug(" Store check " + (tracker * 10) + "% complete");
}
}
}
if (logger.isDebugEnabled())
{
logger.debug("Finished checking indexes for AVM Stores");
}
break;
case FULL:
case NONE:
for (AVMStoreDescriptor store : stores)
{
if (isShuttingDown())
{
return;
}
actions.put(store.getName(), checkStore(store.getName()));
}
break;
default:
}
int full = 0;
int auto = 0;
int invalid = 0;
for (String store : actions.keySet())
{
RecoveryMode mode = actions.get(store);
switch (mode)
{
case AUTO:
auto++;
break;
case FULL:
full++;
break;
case VALIDATE:
invalid++;
break;
case NONE:
default:
}
}
if (recoveryMode != RecoveryMode.NONE)
{
if (logger.isDebugEnabled())
{
logger.debug("Invalid indexes: " + invalid);
logger.debug("Indexes for full rebuild: " + full);
logger.debug("Indexes for auto update: " + auto);
}
}
int count = 0;
int tracker = -1;
int total = full + auto;
if (total > 0)
{
logger.info("Rebuilding indexes for " + total + " AVM Stores");
for (String store : actions.keySet())
{
RecoveryMode mode = actions.get(store);
if (isShuttingDown())
{
return;
}
if ((mode == RecoveryMode.FULL) || (mode == RecoveryMode.AUTO))
{
processStore(store, mode);
count++;
}
if (count * 10l / total > tracker)
{
tracker = (int) (count * 10l / total);
logger.info(" Reindex " + (tracker * 10) + "% complete");
}
}
logger.info("Finished rebuilding indexes for AVM Stores");
}
}
private RecoveryMode checkStore(String store)
{
if (logger.isDebugEnabled())
{
logger.debug("Checking AVM store for index recovery: " + recoveryMode + " on store " + store);
}
// do we just ignore
if (recoveryMode == RecoveryMode.NONE)
{
return RecoveryMode.NONE;
}
// Nothing to do for unindexed stores
if (avmSnapShotTriggeredIndexingMethodInterceptor.getIndexMode(store) == IndexMode.UNINDEXED)
{
if (!avmSnapShotTriggeredIndexingMethodInterceptor.hasIndexBeenCreated(store))
{
logger.warn(" Index for avm store " + store + " is out of date");
return recoveryMode;
}
else
{
return RecoveryMode.NONE;
}
}
if (recoveryMode == RecoveryMode.FULL) // no validate required
{
return RecoveryMode.FULL;
}
else
// validate first
{
if (!avmSnapShotTriggeredIndexingMethodInterceptor.hasIndexBeenCreated(store))
{
logger.warn(" Index for avm store " + store + " is out of date");
return recoveryMode;
}
int lastActualSnapshotId = avmService.getLatestSnapshotID(store);
if (lastActualSnapshotId <= 0)
{
return RecoveryMode.NONE;
}
int lastIndexedSnapshotId = avmSnapShotTriggeredIndexingMethodInterceptor.getLastIndexedSnapshot(store);
if (lastActualSnapshotId != lastIndexedSnapshotId)
{
logger.warn(" Index for avm store " + store + " is out of date");
return recoveryMode;
}
else
{
return RecoveryMode.NONE;
}
}
}
private void processStore(String store, RecoveryMode mode)
{
// put the server into read-only mode for the duration
boolean allowWrite = !transactionService.isReadOnly();
try
{
if (lockServer)
{
// set the server into read-only mode
transactionService.setAllowWrite(false);
}
recoverStore(store, mode);
}
finally
{
// restore read-only state
transactionService.setAllowWrite(allowWrite);
}
}
private void recoverStore(final String store, final RecoveryMode mode)
{
if (mode == RecoveryMode.AUTO)
{
logger.info(" Auto recovering index for " + store);
}
else if (mode == RecoveryMode.FULL)
{
logger.info(" Rebuilding index for " + store);
}
if (!avmSnapShotTriggeredIndexingMethodInterceptor.hasIndexBeenCreated(store))
{
avmSnapShotTriggeredIndexingMethodInterceptor.createIndex(store);
}
if (avmSnapShotTriggeredIndexingMethodInterceptor.getIndexMode(store) != IndexMode.UNINDEXED)
{
final int latest = avmService.getLatestSnapshotID(store);
if (latest <= 0)
{
return;
}
final int latestIndexed = avmSnapShotTriggeredIndexingMethodInterceptor.getLastIndexedSnapshot(store);
RetryingTransactionCallback<Object> reindexWork = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Exception
{
if (mode == RecoveryMode.AUTO)
{
logger.info(" Rebuilding index for snapshots " + latestIndexed +" to "+latest);
avmSnapShotTriggeredIndexingMethodInterceptor.indexSnapshot(store, latestIndexed, latest);
}
else
{
logger.info(" Rebuilding index for snapshots " + 0 +" to "+latest);
avmSnapShotTriggeredIndexingMethodInterceptor.indexSnapshot(store, 0, latest);
}
return null;
}
};
transactionService.getRetryingTransactionHelper().doInTransaction(reindexWork, true, true);
}
if (logger.isDebugEnabled())
{
logger.debug(" Index updated for " + store);
}
}
}