Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
AnalyzerBeans Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.eobjects.analyzer.cluster;
 
 import java.util.List;
 import java.util.Map;
 
Helper class to perform the reduction phase of org.eobjects.analyzer.result.AnalyzerResults collected in a DistributedAnalysisResultFuture.
 
 
     private static final Logger logger = LoggerFactory.getLogger(DistributedAnalysisResultReducer.class);
 
     private final AnalysisJob _masterJob;
     private final LifeCycleHelper _lifeCycleHelper;
     private final RowProcessingPublisher _publisher;
     private final AnalysisListener _analysisListener;
     private final AtomicBoolean _hasRun;
 
     public DistributedAnalysisResultReducer(AnalysisJob masterJobLifeCycleHelper lifeCycleHelper,
             RowProcessingPublisher publisherAnalysisListener analysisListener) {
          = masterJob;
          = lifeCycleHelper;
          = publisher;
          = analysisListener;
          = new AtomicBoolean(false);
     }
 
     public void reduce(final List<AnalysisResultFutureresultsfinal Map<ComponentJobAnalyzerResultresultMap,
             final List<AnalysisResultReductionExceptionreductionErrors) {
         final int size = results.size();
         try {
             .debug("Starting reduce phase of {} results"size);
             reduceResults(resultsresultMapreductionErrors);
             .debug("Finished reduce phase of {} results"size);
         } finally {
             closeNonDistributableComponents();
         }
     }
 
     private void closeNonDistributableComponents() {
         .closeConsumers();
     }

    
Reduces all the analyzer results of an analysis

Parameters:
results
resultMap
reductionErrors
 
     private void reduceResults(final List<AnalysisResultFutureresults,
             final Map<ComponentJobAnalyzerResultresultMap,
             final List<AnalysisResultReductionExceptionreductionErrors) {
 
         if (.get()) {
             // already reduced
             return;
         }
         
         .set(true);
        for (AnalysisResultFuture result : results) {
            if (result.isErrornous()) {
                .error("Encountered errorneous slave result. Result reduction will stop. Result={}"result);
                final List<Throwableerrors = result.getErrors();
                if (!errors.isEmpty()) {
                    final Throwable firstError = errors.get(0);
                    .error(
                            "Encountered error before reducing results (showing stack trace of invoking the reducer): "
                                    + firstError.getMessage(), new Throwable());
                    .errorUknown(firstError);
                }
                // error occurred!
                return;
            }
        }
        final Collection<AnalyzerJobanalyzerJobs = .getAnalyzerJobs();
        for (AnalyzerJob masterAnalyzerJob : analyzerJobs) {
            final Collection<AnalyzerResultslaveResults = new ArrayList<AnalyzerResult>();
            .info("Reducing {} slave results for component: {}"results.size(), masterAnalyzerJob);
            for (AnalysisResultFuture result : results) {
                final Map<ComponentJobAnalyzerResultslaveResultMap = result.getResultMap();
                final List<AnalyzerJobslaveAnalyzerJobs = CollectionUtils2.filterOnClass(slaveResultMap.keySet(),
                        AnalyzerJob.class);
                final AnalyzerJobHelper analyzerJobHelper = new AnalyzerJobHelper(slaveAnalyzerJobs);
                final AnalyzerJob slaveAnalyzerJob = analyzerJobHelper.getAnalyzerJob(masterAnalyzerJob);
                if (slaveAnalyzerJob == null) {
                    throw new IllegalStateException("Could not resolve slave component matching [" + masterAnalyzerJob
                            + "] in slave result: " + result);
                }
                final AnalyzerResult analyzerResult = result.getResult(slaveAnalyzerJob);
                slaveResults.add(analyzerResult);
            }
            reduce(masterAnalyzerJobslaveResultsresultMapreductionErrors);
        }
    }

    
Reduces result for a single analyzer

Parameters:
analyzerJob
slaveResults
resultMap
reductionErrors
    @SuppressWarnings("unchecked")
    private void reduce(AnalyzerJob analyzerJobCollection<AnalyzerResultslaveResults,
            Map<ComponentJobAnalyzerResultresultMapList<AnalysisResultReductionExceptionreductionErrors) {
        if (slaveResults.size() == 1) {
            // special case where these was only 1 slave job
            final AnalyzerResult firstResult = slaveResults.iterator().next();
            resultMap.put(analyzerJobfirstResult);
            .analyzerSuccess(analyzerJobfirstResult);
            return;
        }
        final Class<? extends AnalyzerResultReducer<?>> reducerClass = analyzerJob.getDescriptor()
                .getResultReducerClass();
        final ComponentDescriptor<? extends AnalyzerResultReducer<?>> reducerDescriptor = Descriptors
                .ofComponent(reducerClass);
        AnalyzerResultReducer<AnalyzerResultreducer = null;
        boolean success = false;
        try {
            reducer = (AnalyzerResultReducer<AnalyzerResult>) reducerDescriptor.newInstance();
            .assignProvidedProperties(reducerDescriptorreducer);
            .initialize(reducerDescriptorreducer);
            final AnalyzerResult reducedResult = reducer.reduce(slaveResults);
            resultMap.put(analyzerJobreducedResult);
            
            success = true;
            .analyzerSuccess(analyzerJobreducedResult);
        } catch (Exception e) {
            AnalysisResultReductionException reductionError = new AnalysisResultReductionException(analyzerJob,
                    slaveResultse);
            reductionErrors.add(reductionError);
            .errorInComponent(analyzerJobnulle);
        } finally {
            if (reducer != null) {
                .close(reducerDescriptorreducersuccess);
            }
        }
    }
New to GrepCode? Check out our FAQ X