Search in sources :

Example 1 with IDataWriter

use of org.jumpmind.symmetric.io.data.IDataWriter in project symmetric-ds by JumpMind.

the class MongoDataLoaderFactory method getDataWriter.

@Override
public IDataWriter getDataWriter(String sourceNodeId, ISymmetricDialect symmetricDialect, TransformWriter transformWriter, List<IDatabaseWriterFilter> filters, List<IDatabaseWriterErrorHandler> errorHandlers, List<? extends Conflict> conflictSettings, List<ResolvedData> resolvedData) {
    try {
        if (objectMapper == null) {
            objectMapper = (IDBObjectMapper) Class.forName("org.jumpmind.symmetric.io.SimpleDBObjectMapper").newInstance();
        }
        Method method = objectMapper.getClass().getMethod("setDefaultDatabaseName", String.class);
        if (method != null) {
            method.invoke(objectMapper, parameterService.getString("mongodb.default.databasename", "default"));
        }
    } catch (Exception e) {
        log.debug("Failed to call setDefaultDatabaseName on mapper", e);
    }
    try {
        Class<?> clientManagerClass = Class.forName("org.jumpmind.symmetric.io.SimpleMongoClientManager");
        Constructor<?> clientManagerConstrutor = clientManagerClass.getConstructor(new Class<?>[] { IParameterService.class, String.class });
        Class<?> dbWriterClass = Class.forName("org.jumpmind.symmetric.io.MongoDatabaseWriter");
        Constructor<?> dbWriterConstructor = dbWriterClass.getConstructor(new Class<?>[] { IDBObjectMapper.class, IMongoClientManager.class, IDatabaseWriterConflictResolver.class, DatabaseWriterSettings.class });
        Object clientManager = clientManagerConstrutor.newInstance(parameterService, typeName);
        return (IDataWriter) dbWriterConstructor.newInstance(objectMapper, clientManager, new DefaultTransformWriterConflictResolver(transformWriter), buildDatabaseWriterSettings(filters, errorHandlers, conflictSettings, resolvedData));
    } catch (Exception e) {
        log.warn("Failed to create the mongo database writer.  Check to see if all of the required jars have been added");
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : DefaultTransformWriterConflictResolver(org.jumpmind.symmetric.io.data.writer.DefaultTransformWriterConflictResolver) Method(java.lang.reflect.Method) IDataWriter(org.jumpmind.symmetric.io.data.IDataWriter)

Example 2 with IDataWriter

use of org.jumpmind.symmetric.io.data.IDataWriter in project symmetric-ds by JumpMind.

the class DataExtractorService method extractOutgoingBatch.

protected OutgoingBatch extractOutgoingBatch(ProcessInfo processInfo, Node targetNode, IDataWriter dataWriter, OutgoingBatch currentBatch, boolean useStagingDataWriter, boolean updateBatchStatistics, ExtractMode mode) {
    if (currentBatch.getStatus() != Status.OK || ExtractMode.EXTRACT_ONLY == mode || ExtractMode.FOR_SYM_CLIENT == mode) {
        Node sourceNode = nodeService.findIdentity();
        IDataWriter writer = wrapWithTransformWriter(sourceNode, targetNode, processInfo, dataWriter, useStagingDataWriter);
        long ts = System.currentTimeMillis();
        long extractTimeInMs = 0l;
        long byteCount = 0l;
        long transformTimeInMs = 0l;
        if (currentBatch.getStatus() == Status.IG) {
            cleanupIgnoredBatch(sourceNode, targetNode, currentBatch, writer);
        } else if (!isPreviouslyExtracted(currentBatch, true)) {
            String semaphoreKey = useStagingDataWriter ? Long.toString(currentBatch.getBatchId()) : currentBatch.getNodeBatchId();
            Semaphore lock = null;
            try {
                synchronized (locks) {
                    lock = locks.get(semaphoreKey);
                    if (lock == null) {
                        lock = new Semaphore(1);
                        locks.put(semaphoreKey, lock);
                    }
                    try {
                        lock.acquire();
                    } catch (InterruptedException e) {
                        throw new org.jumpmind.exception.InterruptedException(e);
                    }
                }
                if (!isPreviouslyExtracted(currentBatch, true)) {
                    currentBatch.setExtractCount(currentBatch.getExtractCount() + 1);
                    if (updateBatchStatistics) {
                        changeBatchStatus(Status.QY, currentBatch, mode);
                    }
                    currentBatch.resetStats();
                    DataContext ctx = new DataContext();
                    ctx.put(Constants.DATA_CONTEXT_TARGET_NODE_ID, targetNode.getNodeId());
                    ctx.put(Constants.DATA_CONTEXT_TARGET_NODE_EXTERNAL_ID, targetNode.getExternalId());
                    ctx.put(Constants.DATA_CONTEXT_TARGET_NODE_GROUP_ID, targetNode.getNodeGroupId());
                    ctx.put(Constants.DATA_CONTEXT_TARGET_NODE, targetNode);
                    ctx.put(Constants.DATA_CONTEXT_SOURCE_NODE, sourceNode);
                    ctx.put(Constants.DATA_CONTEXT_SOURCE_NODE_ID, sourceNode.getNodeId());
                    ctx.put(Constants.DATA_CONTEXT_SOURCE_NODE_EXTERNAL_ID, sourceNode.getExternalId());
                    ctx.put(Constants.DATA_CONTEXT_SOURCE_NODE_GROUP_ID, sourceNode.getNodeGroupId());
                    IDataReader dataReader = buildExtractDataReader(sourceNode, targetNode, currentBatch, processInfo);
                    new DataProcessor(dataReader, writer, "extract").process(ctx);
                    extractTimeInMs = System.currentTimeMillis() - ts;
                    Statistics stats = getExtractStats(writer);
                    if (stats != null) {
                        transformTimeInMs = stats.get(DataWriterStatisticConstants.TRANSFORMMILLIS);
                        extractTimeInMs = extractTimeInMs - transformTimeInMs;
                        byteCount = stats.get(DataWriterStatisticConstants.BYTECOUNT);
                        statisticManager.incrementDataBytesExtracted(currentBatch.getChannelId(), byteCount);
                        statisticManager.incrementDataExtracted(currentBatch.getChannelId(), stats.get(DataWriterStatisticConstants.STATEMENTCOUNT));
                    }
                }
            } catch (RuntimeException ex) {
                IStagedResource resource = getStagedResource(currentBatch);
                if (resource != null) {
                    resource.close();
                    resource.delete();
                }
                throw ex;
            } finally {
                IStagedResource resource = getStagedResource(currentBatch);
                if (resource != null) {
                    resource.setState(State.DONE);
                }
                lock.release();
                synchronized (locks) {
                    locks.remove(semaphoreKey);
                }
            }
        }
        if (updateBatchStatistics) {
            long dataEventCount = currentBatch.getDataEventCount();
            long insertEventCount = currentBatch.getInsertEventCount();
            currentBatch = requeryIfEnoughTimeHasPassed(ts, currentBatch);
            // preserve in the case of a reload event
            if (dataEventCount > currentBatch.getDataEventCount()) {
                currentBatch.setDataEventCount(dataEventCount);
            }
            // preserve in the case of a reload event
            if (insertEventCount > currentBatch.getInsertEventCount()) {
                currentBatch.setInsertEventCount(insertEventCount);
            }
            // "re-queried"
            if (extractTimeInMs > 0) {
                currentBatch.setExtractMillis(extractTimeInMs);
            }
            if (byteCount > 0) {
                currentBatch.setByteCount(byteCount);
            }
        }
    }
    processInfo.incrementCurrentBatchCount();
    return currentBatch;
}
Also used : IDataReader(org.jumpmind.symmetric.io.data.IDataReader) Node(org.jumpmind.symmetric.model.Node) Semaphore(java.util.concurrent.Semaphore) DataProcessor(org.jumpmind.symmetric.io.data.DataProcessor) Statistics(org.jumpmind.util.Statistics) DataReaderStatistics(org.jumpmind.symmetric.io.data.reader.DataReaderStatistics) DataContext(org.jumpmind.symmetric.io.data.DataContext) IStagedResource(org.jumpmind.symmetric.io.stage.IStagedResource) IDataWriter(org.jumpmind.symmetric.io.data.IDataWriter)

Example 3 with IDataWriter

use of org.jumpmind.symmetric.io.data.IDataWriter in project symmetric-ds by JumpMind.

the class DataExtractorService method extract.

public List<OutgoingBatch> extract(ProcessInfo processInfo, Node targetNode, String queue, IOutgoingTransport transport) {
    /*
         * make sure that data is routed before extracting if the route job is
         * not configured to start automatically
         */
    if (!parameterService.is(ParameterConstants.START_ROUTE_JOB) && parameterService.is(ParameterConstants.ROUTE_ON_EXTRACT)) {
        routerService.routeData(true);
    }
    OutgoingBatches batches = null;
    if (queue != null) {
        NodeGroupLinkAction defaultAction = configurationService.getNodeGroupLinkFor(nodeService.findIdentity().getNodeGroupId(), targetNode.getNodeGroupId(), false).getDataEventAction();
        ProcessInfoKey.ProcessType processType = processInfo.getKey().getProcessType();
        NodeGroupLinkAction action = null;
        if (processType.equals(ProcessInfoKey.ProcessType.PUSH_JOB)) {
            action = NodeGroupLinkAction.P;
        } else if (processType.equals(ProcessInfoKey.ProcessType.PULL_HANDLER)) {
            action = NodeGroupLinkAction.W;
        }
        batches = outgoingBatchService.getOutgoingBatches(targetNode.getNodeId(), queue, action, defaultAction, false);
    } else {
        batches = outgoingBatchService.getOutgoingBatches(targetNode.getNodeId(), false);
    }
    if (batches.containsBatches()) {
        ChannelMap channelMap = transport.getSuspendIgnoreChannelLists(configurationService, queue, targetNode);
        List<OutgoingBatch> activeBatches = filterBatchesForExtraction(batches, channelMap);
        if (activeBatches.size() > 0) {
            BufferedWriter writer = transport.openWriter();
            IDataWriter dataWriter = new ProtocolDataWriter(nodeService.findIdentityNodeId(), writer, targetNode.requires13Compatiblity());
            return extract(processInfo, targetNode, activeBatches, dataWriter, writer, ExtractMode.FOR_SYM_CLIENT);
        }
    }
    return Collections.emptyList();
}
Also used : ChannelMap(org.jumpmind.symmetric.model.ChannelMap) NodeGroupLinkAction(org.jumpmind.symmetric.model.NodeGroupLinkAction) ProtocolDataWriter(org.jumpmind.symmetric.io.data.writer.ProtocolDataWriter) ProcessType(org.jumpmind.symmetric.model.ProcessInfoKey.ProcessType) ProcessInfoKey(org.jumpmind.symmetric.model.ProcessInfoKey) OutgoingBatches(org.jumpmind.symmetric.model.OutgoingBatches) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) BufferedWriter(java.io.BufferedWriter) BatchBufferedWriter(org.jumpmind.symmetric.transport.BatchBufferedWriter) IDataWriter(org.jumpmind.symmetric.io.data.IDataWriter)

Example 4 with IDataWriter

use of org.jumpmind.symmetric.io.data.IDataWriter in project symmetric-ds by JumpMind.

the class DataLoaderService method buildDataWriter.

protected IDataWriter buildDataWriter(ProcessInfo processInfo, String sourceNodeId, String channelId, long batchId, boolean isRetry) {
    TransformTable[] transforms = null;
    NodeGroupLink link = null;
    List<ResolvedData> resolvedDatas = new ArrayList<ResolvedData>();
    List<IDatabaseWriterFilter> filters = extensionService.getExtensionPointList(IDatabaseWriterFilter.class);
    List<IDatabaseWriterFilter> dynamicFilters = filters;
    List<IDatabaseWriterErrorHandler> errorHandlers = extensionService.getExtensionPointList(IDatabaseWriterErrorHandler.class);
    List<IDatabaseWriterErrorHandler> dynamicErrorHandlers = errorHandlers;
    if (sourceNodeId != null) {
        Node sourceNode = nodeService.findNode(sourceNodeId, true);
        if (sourceNode != null) {
            link = new NodeGroupLink(sourceNode.getNodeGroupId(), parameterService.getNodeGroupId());
        }
        Map<LoadFilterType, Map<String, List<LoadFilter>>> loadFilters = loadFilterService.findLoadFiltersFor(link, true);
        List<DynamicDatabaseWriterFilter> databaseWriterFilters = DynamicDatabaseWriterFilter.getDatabaseWriterFilters(engine, loadFilters);
        if (loadFilters != null && loadFilters.size() > 0) {
            dynamicFilters = new ArrayList<IDatabaseWriterFilter>(filters.size() + 1);
            dynamicFilters.addAll(filters);
            dynamicFilters.addAll(databaseWriterFilters);
            dynamicErrorHandlers = new ArrayList<IDatabaseWriterErrorHandler>(errorHandlers.size() + 1);
            dynamicErrorHandlers.addAll(errorHandlers);
            dynamicErrorHandlers.addAll(databaseWriterFilters);
        }
        List<TransformTableNodeGroupLink> transformsList = transformService.findTransformsFor(link, TransformPoint.LOAD);
        transforms = transformsList != null ? transformsList.toArray(new TransformTable[transformsList.size()]) : null;
        if (isRetry) {
            List<IncomingError> incomingErrors = getIncomingErrors(batchId, sourceNodeId);
            for (IncomingError incomingError : incomingErrors) {
                if (incomingError.isResolveIgnore() || StringUtils.isNotBlank(incomingError.getResolveData())) {
                    resolvedDatas.add(new ResolvedData(incomingError.getFailedRowNumber(), incomingError.getResolveData(), incomingError.isResolveIgnore()));
                }
            }
        }
    }
    TransformWriter transformWriter = new TransformWriter(platform, TransformPoint.LOAD, null, transformService.getColumnTransforms(), transforms);
    IDataWriter targetWriter = getFactory(channelId).getDataWriter(sourceNodeId, symmetricDialect, transformWriter, dynamicFilters, dynamicErrorHandlers, getConflictSettingsNodeGroupLinks(link, false), resolvedDatas);
    transformWriter.setNestedWriter(new ProcessInfoDataWriter(targetWriter, processInfo));
    return transformWriter;
}
Also used : ResolvedData(org.jumpmind.symmetric.io.data.writer.ResolvedData) Node(org.jumpmind.symmetric.model.Node) ArrayList(java.util.ArrayList) TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) DynamicDatabaseWriterFilter(org.jumpmind.symmetric.load.DynamicDatabaseWriterFilter) IncomingError(org.jumpmind.symmetric.model.IncomingError) IDatabaseWriterErrorHandler(org.jumpmind.symmetric.io.data.writer.IDatabaseWriterErrorHandler) TransformTableNodeGroupLink(org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) IDatabaseWriterFilter(org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter) ProcessInfoDataWriter(org.jumpmind.symmetric.model.ProcessInfoDataWriter) LoadFilterType(org.jumpmind.symmetric.model.LoadFilter.LoadFilterType) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) TransformWriter(org.jumpmind.symmetric.io.data.writer.TransformWriter) ChannelMap(org.jumpmind.symmetric.model.ChannelMap) Map(java.util.Map) HashMap(java.util.HashMap) TransformTableNodeGroupLink(org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink) IDataWriter(org.jumpmind.symmetric.io.data.IDataWriter)

Example 5 with IDataWriter

use of org.jumpmind.symmetric.io.data.IDataWriter in project symmetric-ds by JumpMind.

the class DataExtractorService method extractOnlyOutgoingBatch.

/**
     * This method will extract an outgoing batch, but will not update the outgoing batch status
     */
public boolean extractOnlyOutgoingBatch(String nodeId, long batchId, Writer writer) {
    boolean extracted = false;
    Node targetNode = null;
    if (Constants.UNROUTED_NODE_ID.equals(nodeId)) {
        targetNode = new Node(nodeId, parameterService.getNodeGroupId());
    } else {
        targetNode = nodeService.findNode(nodeId);
    }
    if (targetNode != null) {
        OutgoingBatch batch = outgoingBatchService.findOutgoingBatch(batchId, nodeId);
        if (batch != null) {
            IDataWriter dataWriter = new ProtocolDataWriter(nodeService.findIdentityNodeId(), writer, targetNode.requires13Compatiblity());
            List<OutgoingBatch> batches = new ArrayList<OutgoingBatch>(1);
            batches.add(batch);
            batches = extract(new ProcessInfo(), targetNode, batches, dataWriter, null, ExtractMode.EXTRACT_ONLY);
            extracted = batches.size() > 0;
        }
    }
    return extracted;
}
Also used : ProtocolDataWriter(org.jumpmind.symmetric.io.data.writer.ProtocolDataWriter) Node(org.jumpmind.symmetric.model.Node) ArrayList(java.util.ArrayList) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) IDataWriter(org.jumpmind.symmetric.io.data.IDataWriter)

Aggregations

IDataWriter (org.jumpmind.symmetric.io.data.IDataWriter)5 Node (org.jumpmind.symmetric.model.Node)3 ArrayList (java.util.ArrayList)2 ProtocolDataWriter (org.jumpmind.symmetric.io.data.writer.ProtocolDataWriter)2 ChannelMap (org.jumpmind.symmetric.model.ChannelMap)2 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)2 BufferedWriter (java.io.BufferedWriter)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Semaphore (java.util.concurrent.Semaphore)1 DataContext (org.jumpmind.symmetric.io.data.DataContext)1 DataProcessor (org.jumpmind.symmetric.io.data.DataProcessor)1 IDataReader (org.jumpmind.symmetric.io.data.IDataReader)1 DataReaderStatistics (org.jumpmind.symmetric.io.data.reader.DataReaderStatistics)1 TransformTable (org.jumpmind.symmetric.io.data.transform.TransformTable)1 DefaultTransformWriterConflictResolver (org.jumpmind.symmetric.io.data.writer.DefaultTransformWriterConflictResolver)1 IDatabaseWriterErrorHandler (org.jumpmind.symmetric.io.data.writer.IDatabaseWriterErrorHandler)1 IDatabaseWriterFilter (org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter)1 ResolvedData (org.jumpmind.symmetric.io.data.writer.ResolvedData)1