Search in sources :

Example 1 with IAppRuntimeContextProvider

use of org.apache.asterix.common.transactions.IAppRuntimeContextProvider in project asterixdb by apache.

the class RecoveryManager method startRecoveryRedoPhase.

private synchronized void startRecoveryRedoPhase(Set<Integer> partitions, ILogReader logReader, long lowWaterMarkLSN, Set<Integer> winnerJobSet) throws IOException, ACIDException {
    int redoCount = 0;
    int jobId = -1;
    long resourceId;
    long maxDiskLastLsn;
    long lsn = -1;
    ILSMIndex index = null;
    LocalResource localResource = null;
    DatasetLocalResource localResourceMetadata = null;
    boolean foundWinner = false;
    JobEntityCommits jobEntityWinners = null;
    IAppRuntimeContextProvider appRuntimeContext = txnSubsystem.getAsterixAppRuntimeContextProvider();
    IDatasetLifecycleManager datasetLifecycleManager = appRuntimeContext.getDatasetLifecycleManager();
    Map<Long, LocalResource> resourcesMap = localResourceRepository.loadAndGetAllResources();
    Map<Long, Long> resourceId2MaxLSNMap = new HashMap<>();
    TxnId tempKeyTxnId = new TxnId(-1, -1, -1, null, -1, false);
    ILogRecord logRecord = null;
    try {
        logReader.initializeScan(lowWaterMarkLSN);
        logRecord = logReader.next();
        while (logRecord != null) {
            if (IS_DEBUG_MODE) {
                LOGGER.info(logRecord.getLogRecordForDisplay());
            }
            lsn = logRecord.getLSN();
            jobId = logRecord.getJobId();
            foundWinner = false;
            switch(logRecord.getLogType()) {
                case LogType.UPDATE:
                    if (partitions.contains(logRecord.getResourcePartition())) {
                        if (winnerJobSet.contains(jobId)) {
                            foundWinner = true;
                        } else if (jobId2WinnerEntitiesMap.containsKey(jobId)) {
                            jobEntityWinners = jobId2WinnerEntitiesMap.get(jobId);
                            tempKeyTxnId.setTxnId(jobId, logRecord.getDatasetId(), logRecord.getPKHashValue(), logRecord.getPKValue(), logRecord.getPKValueSize());
                            if (jobEntityWinners.containsEntityCommitForTxnId(lsn, tempKeyTxnId)) {
                                foundWinner = true;
                            }
                        }
                        if (foundWinner) {
                            resourceId = logRecord.getResourceId();
                            localResource = resourcesMap.get(resourceId);
                            /*******************************************************************
                                 * [Notice]
                                 * -> Issue
                                 * Delete index may cause a problem during redo.
                                 * The index operation to be redone couldn't be redone because the corresponding index
                                 * may not exist in NC due to the possible index drop DDL operation.
                                 * -> Approach
                                 * Avoid the problem during redo.
                                 * More specifically, the problem will be detected when the localResource of
                                 * the corresponding index is retrieved, which will end up with 'null'.
                                 * If null is returned, then just go and process the next
                                 * log record.
                                 *******************************************************************/
                            if (localResource == null) {
                                LOGGER.log(Level.WARNING, "resource was not found for resource id " + resourceId);
                                logRecord = logReader.next();
                                continue;
                            }
                            /*******************************************************************/
                            //get index instance from IndexLifeCycleManager
                            //if index is not registered into IndexLifeCycleManager,
                            //create the index using LocalMetadata stored in LocalResourceRepository
                            //get partition path in this node
                            localResourceMetadata = (DatasetLocalResource) localResource.getResource();
                            index = (ILSMIndex) datasetLifecycleManager.get(localResource.getPath());
                            if (index == null) {
                                //#. create index instance and register to indexLifeCycleManager
                                index = (ILSMIndex) localResourceMetadata.createInstance(serviceCtx);
                                datasetLifecycleManager.register(localResource.getPath(), index);
                                datasetLifecycleManager.open(localResource.getPath());
                                //#. get maxDiskLastLSN
                                ILSMIndex lsmIndex = index;
                                try {
                                    maxDiskLastLsn = ((AbstractLSMIOOperationCallback) lsmIndex.getIOOperationCallback()).getComponentLSN(lsmIndex.getImmutableComponents());
                                } catch (HyracksDataException e) {
                                    datasetLifecycleManager.close(localResource.getPath());
                                    throw e;
                                }
                                //#. set resourceId and maxDiskLastLSN to the map
                                resourceId2MaxLSNMap.put(resourceId, maxDiskLastLsn);
                            } else {
                                maxDiskLastLsn = resourceId2MaxLSNMap.get(resourceId);
                            }
                            if (lsn > maxDiskLastLsn) {
                                redo(logRecord, datasetLifecycleManager);
                                redoCount++;
                            }
                        }
                    }
                    break;
                case LogType.JOB_COMMIT:
                case LogType.ENTITY_COMMIT:
                case LogType.ABORT:
                case LogType.FLUSH:
                case LogType.WAIT:
                case LogType.MARKER:
                    //do nothing
                    break;
                default:
                    throw new ACIDException("Unsupported LogType: " + logRecord.getLogType());
            }
            logRecord = logReader.next();
        }
        LOGGER.info("Logs REDO phase completed. Redo logs count: " + redoCount);
    } finally {
        //close all indexes
        Set<Long> resourceIdList = resourceId2MaxLSNMap.keySet();
        for (long r : resourceIdList) {
            datasetLifecycleManager.close(resourcesMap.get(r).getPath());
        }
    }
}
Also used : HashMap(java.util.HashMap) ILSMIndex(org.apache.hyracks.storage.am.lsm.common.api.ILSMIndex) Checkpoint(org.apache.asterix.common.transactions.Checkpoint) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) DatasetLocalResource(org.apache.asterix.common.dataflow.DatasetLocalResource) LocalResource(org.apache.hyracks.storage.common.LocalResource) ACIDException(org.apache.asterix.common.exceptions.ACIDException) DatasetLocalResource(org.apache.asterix.common.dataflow.DatasetLocalResource) IDatasetLifecycleManager(org.apache.asterix.common.api.IDatasetLifecycleManager) TxnId(org.apache.asterix.transaction.management.service.recovery.TxnId) IAppRuntimeContextProvider(org.apache.asterix.common.transactions.IAppRuntimeContextProvider) ILogRecord(org.apache.asterix.common.transactions.ILogRecord)

Example 2 with IAppRuntimeContextProvider

use of org.apache.asterix.common.transactions.IAppRuntimeContextProvider in project asterixdb by apache.

the class NCAppRuntimeContext method initialize.

@Override
public void initialize(boolean initialRun) throws IOException, ACIDException {
    ioManager = getServiceContext().getIoManager();
    threadExecutor = new ThreadExecutor(getServiceContext().getThreadFactory());
    fileMapManager = new FileMapManager(ioManager);
    ICacheMemoryAllocator allocator = new HeapBufferAllocator();
    IPageCleanerPolicy pcp = new DelayPageCleanerPolicy(600000);
    IPageReplacementStrategy prs = new ClockPageReplacementStrategy(allocator, storageProperties.getBufferCachePageSize(), storageProperties.getBufferCacheNumPages());
    AsynchronousScheduler.INSTANCE.init(getServiceContext().getThreadFactory());
    lsmIOScheduler = AsynchronousScheduler.INSTANCE;
    metadataMergePolicyFactory = new PrefixMergePolicyFactory();
    ILocalResourceRepositoryFactory persistentLocalResourceRepositoryFactory = new PersistentLocalResourceRepositoryFactory(ioManager, getServiceContext().getNodeId(), metadataProperties);
    localResourceRepository = (PersistentLocalResourceRepository) persistentLocalResourceRepositoryFactory.createRepository();
    IAppRuntimeContextProvider asterixAppRuntimeContextProvider = new AppRuntimeContextProviderForRecovery(this);
    txnSubsystem = new TransactionSubsystem(getServiceContext(), getServiceContext().getNodeId(), asterixAppRuntimeContextProvider, txnProperties);
    IRecoveryManager recoveryMgr = txnSubsystem.getRecoveryManager();
    SystemState systemState = recoveryMgr.getSystemState();
    if (initialRun || systemState == SystemState.PERMANENT_DATA_LOSS) {
        //delete any storage data before the resource factory is initialized
        localResourceRepository.deleteStorageData(true);
    }
    datasetLifecycleManager = new DatasetLifecycleManager(storageProperties, localResourceRepository, MetadataIndexImmutableProperties.FIRST_AVAILABLE_USER_DATASET_ID, txnSubsystem.getLogManager(), ioManager.getIODevices().size());
    isShuttingdown = false;
    activeManager = new ActiveManager(threadExecutor, getServiceContext().getNodeId(), activeProperties.getMemoryComponentGlobalBudget(), compilerProperties.getFrameSize());
    if (replicationProperties.isParticipant(getServiceContext().getNodeId())) {
        String nodeId = getServiceContext().getNodeId();
        replicaResourcesManager = new ReplicaResourcesManager(localResourceRepository, metadataProperties);
        replicationManager = new ReplicationManager(nodeId, replicationProperties, replicaResourcesManager, txnSubsystem.getLogManager(), asterixAppRuntimeContextProvider);
        //pass replication manager to replication required object
        //LogManager to replicate logs
        txnSubsystem.getLogManager().setReplicationManager(replicationManager);
        //PersistentLocalResourceRepository to replicate metadata files and delete backups on drop index
        localResourceRepository.setReplicationManager(replicationManager);
        /*
             * add the partitions that will be replicated in this node as inactive partitions
             */
        //get nodes which replicate to this node
        Set<String> remotePrimaryReplicas = replicationProperties.getRemotePrimaryReplicasIds(nodeId);
        for (String clientId : remotePrimaryReplicas) {
            //get the partitions of each client
            ClusterPartition[] clientPartitions = metadataProperties.getNodePartitions().get(clientId);
            for (ClusterPartition partition : clientPartitions) {
                localResourceRepository.addInactivePartition(partition.getPartitionId());
            }
        }
        //initialize replication channel
        replicationChannel = new ReplicationChannel(nodeId, replicationProperties, txnSubsystem.getLogManager(), replicaResourcesManager, replicationManager, getServiceContext(), asterixAppRuntimeContextProvider);
        remoteRecoveryManager = new RemoteRecoveryManager(replicationManager, this, replicationProperties);
        bufferCache = new BufferCache(ioManager, prs, pcp, fileMapManager, storageProperties.getBufferCacheMaxOpenFiles(), getServiceContext().getThreadFactory(), replicationManager);
    } else {
        bufferCache = new BufferCache(ioManager, prs, pcp, fileMapManager, storageProperties.getBufferCacheMaxOpenFiles(), getServiceContext().getThreadFactory());
    }
    /*
         * The order of registration is important. The buffer cache must registered before recovery and transaction
         * managers. Notes: registered components are stopped in reversed order
         */
    ILifeCycleComponentManager lccm = getServiceContext().getLifeCycleComponentManager();
    lccm.register((ILifeCycleComponent) bufferCache);
    /*
         * LogManager must be stopped after RecoveryManager, DatasetLifeCycleManager, and ReplicationManager
         * to process any logs that might be generated during stopping these components
         */
    lccm.register((ILifeCycleComponent) txnSubsystem.getLogManager());
    /*
         * ReplicationManager must be stopped after indexLifecycleManager and recovery manager
         * so that any logs/files generated during closing datasets or checkpoints are sent to remote replicas
         */
    if (replicationManager != null) {
        lccm.register(replicationManager);
    }
    lccm.register((ILifeCycleComponent) txnSubsystem.getRecoveryManager());
    /*
         * Stopping indexLifecycleManager will flush and close all datasets.
         */
    lccm.register((ILifeCycleComponent) datasetLifecycleManager);
    lccm.register((ILifeCycleComponent) txnSubsystem.getTransactionManager());
    lccm.register((ILifeCycleComponent) txnSubsystem.getLockManager());
    lccm.register(txnSubsystem.getCheckpointManager());
}
Also used : SystemState(org.apache.asterix.common.transactions.IRecoveryManager.SystemState) IDatasetLifecycleManager(org.apache.asterix.common.api.IDatasetLifecycleManager) DatasetLifecycleManager(org.apache.asterix.common.context.DatasetLifecycleManager) ReplicaResourcesManager(org.apache.asterix.replication.storage.ReplicaResourcesManager) IReplicaResourcesManager(org.apache.asterix.common.replication.IReplicaResourcesManager) ILocalResourceRepositoryFactory(org.apache.hyracks.storage.common.file.ILocalResourceRepositoryFactory) AppRuntimeContextProviderForRecovery(org.apache.asterix.api.common.AppRuntimeContextProviderForRecovery) ThreadExecutor(org.apache.asterix.common.api.ThreadExecutor) IAppRuntimeContextProvider(org.apache.asterix.common.transactions.IAppRuntimeContextProvider) IRemoteRecoveryManager(org.apache.asterix.common.replication.IRemoteRecoveryManager) RemoteRecoveryManager(org.apache.asterix.replication.recovery.RemoteRecoveryManager) IReplicationManager(org.apache.asterix.common.replication.IReplicationManager) ReplicationManager(org.apache.asterix.replication.management.ReplicationManager) ITransactionSubsystem(org.apache.asterix.common.transactions.ITransactionSubsystem) ILifeCycleComponentManager(org.apache.hyracks.api.lifecycle.ILifeCycleComponentManager) PersistentLocalResourceRepositoryFactory(org.apache.asterix.transaction.management.resource.PersistentLocalResourceRepositoryFactory) IPageCleanerPolicy(org.apache.hyracks.storage.common.buffercache.IPageCleanerPolicy) IRecoveryManager(org.apache.asterix.common.transactions.IRecoveryManager) ReplicationChannel(org.apache.asterix.replication.management.ReplicationChannel) IReplicationChannel(org.apache.asterix.common.replication.IReplicationChannel) HeapBufferAllocator(org.apache.hyracks.storage.common.buffercache.HeapBufferAllocator) FileMapManager(org.apache.asterix.common.context.FileMapManager) IFileMapManager(org.apache.hyracks.storage.common.file.IFileMapManager) PrefixMergePolicyFactory(org.apache.hyracks.storage.am.lsm.common.impls.PrefixMergePolicyFactory) DelayPageCleanerPolicy(org.apache.hyracks.storage.common.buffercache.DelayPageCleanerPolicy) ICacheMemoryAllocator(org.apache.hyracks.storage.common.buffercache.ICacheMemoryAllocator) ActiveManager(org.apache.asterix.active.ActiveManager) IBufferCache(org.apache.hyracks.storage.common.buffercache.IBufferCache) BufferCache(org.apache.hyracks.storage.common.buffercache.BufferCache) IPageReplacementStrategy(org.apache.hyracks.storage.common.buffercache.IPageReplacementStrategy) ClockPageReplacementStrategy(org.apache.hyracks.storage.common.buffercache.ClockPageReplacementStrategy) ClusterPartition(org.apache.asterix.common.cluster.ClusterPartition)

Aggregations

IDatasetLifecycleManager (org.apache.asterix.common.api.IDatasetLifecycleManager)2 IAppRuntimeContextProvider (org.apache.asterix.common.transactions.IAppRuntimeContextProvider)2 HashMap (java.util.HashMap)1 ActiveManager (org.apache.asterix.active.ActiveManager)1 AppRuntimeContextProviderForRecovery (org.apache.asterix.api.common.AppRuntimeContextProviderForRecovery)1 ThreadExecutor (org.apache.asterix.common.api.ThreadExecutor)1 ClusterPartition (org.apache.asterix.common.cluster.ClusterPartition)1 DatasetLifecycleManager (org.apache.asterix.common.context.DatasetLifecycleManager)1 FileMapManager (org.apache.asterix.common.context.FileMapManager)1 DatasetLocalResource (org.apache.asterix.common.dataflow.DatasetLocalResource)1 ACIDException (org.apache.asterix.common.exceptions.ACIDException)1 IRemoteRecoveryManager (org.apache.asterix.common.replication.IRemoteRecoveryManager)1 IReplicaResourcesManager (org.apache.asterix.common.replication.IReplicaResourcesManager)1 IReplicationChannel (org.apache.asterix.common.replication.IReplicationChannel)1 IReplicationManager (org.apache.asterix.common.replication.IReplicationManager)1 Checkpoint (org.apache.asterix.common.transactions.Checkpoint)1 ILogRecord (org.apache.asterix.common.transactions.ILogRecord)1 IRecoveryManager (org.apache.asterix.common.transactions.IRecoveryManager)1 SystemState (org.apache.asterix.common.transactions.IRecoveryManager.SystemState)1 ITransactionSubsystem (org.apache.asterix.common.transactions.ITransactionSubsystem)1