Search in sources :

Example 6 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class HttpOutgoingTransport method requestReservation.

/**
     * Before streaming data to the remote node, make sure it is ok to. We have
     * found that we can be more efficient on a push by relying on HTTP
     * keep-alive.
     *
     * @throws IOException
     * @throws {@link ConnectionRejectedException}
     * @throws {@link AuthenticationException}
     */
private HttpURLConnection requestReservation(String queue) {
    try {
        connection = HttpTransportManager.openConnection(url, basicAuthUsername, basicAuthPassword);
        connection.setUseCaches(false);
        connection.setConnectTimeout(httpTimeout);
        connection.setReadTimeout(httpTimeout);
        connection.setRequestMethod("HEAD");
        connection.setRequestProperty(WebConstants.THREAD_CHANNEL, queue);
        analyzeResponseCode(connection.getResponseCode());
    } catch (IOException ex) {
        throw new IoException(ex);
    }
    return connection;
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 7 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class SnapshotUtil method createSnapshot.

public static File createSnapshot(ISymmetricEngine engine) {
    String dirName = engine.getEngineName().replaceAll(" ", "-") + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    IParameterService parameterService = engine.getParameterService();
    File tmpDir = new File(parameterService.getTempDirectory(), dirName);
    tmpDir.mkdirs();
    File logDir = null;
    String parameterizedLogDir = parameterService.getString("server.log.dir");
    if (isNotBlank(parameterizedLogDir)) {
        logDir = new File(parameterizedLogDir);
    }
    if (logDir != null && logDir.exists()) {
        log.info("Using server.log.dir setting as the location of the log files");
    } else {
        logDir = new File("logs");
        if (!logDir.exists()) {
            Map<File, Layout> matches = findSymmetricLogFile();
            if (matches != null && matches.size() == 1) {
                logDir = matches.keySet().iterator().next().getParentFile();
            }
        }
        if (!logDir.exists()) {
            logDir = new File("../logs");
        }
        if (!logDir.exists()) {
            logDir = new File("target");
        }
        if (logDir.exists()) {
            File[] files = logDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.getName().toLowerCase().endsWith(".log")) {
                        try {
                            FileUtils.copyFileToDirectory(file, tmpDir);
                        } catch (IOException e) {
                            log.warn("Failed to copy " + file.getName() + " to the snapshot directory", e);
                        }
                    }
                }
            }
        }
    }
    FileWriter fwriter = null;
    try {
        fwriter = new FileWriter(new File(tmpDir, "config-export.csv"));
        engine.getDataExtractorService().extractConfigurationStandalone(engine.getNodeService().findIdentity(), fwriter, TableConstants.SYM_NODE, TableConstants.SYM_NODE_SECURITY, TableConstants.SYM_NODE_IDENTITY, TableConstants.SYM_NODE_HOST, TableConstants.SYM_NODE_CHANNEL_CTL, TableConstants.SYM_CONSOLE_USER, TableConstants.SYM_MONITOR_EVENT, TableConstants.SYM_CONSOLE_EVENT);
    } catch (Exception e) {
        log.warn("Failed to export symmetric configuration", e);
    } finally {
        IOUtils.closeQuietly(fwriter);
    }
    File serviceConfFile = new File("conf/sym_service.conf");
    try {
        if (serviceConfFile.exists()) {
            FileUtils.copyFileToDirectory(serviceConfFile, tmpDir);
        }
    } catch (Exception e) {
        log.warn("Failed to copy " + serviceConfFile.getName() + " to the snapshot directory", e);
    }
    TreeSet<Table> tables = new TreeSet<Table>();
    FileOutputStream fos = null;
    try {
        ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
        List<TriggerHistory> triggerHistories = triggerRouterService.getActiveTriggerHistories();
        for (TriggerHistory triggerHistory : triggerHistories) {
            Table table = engine.getDatabasePlatform().getTableFromCache(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), false);
            if (table != null && !table.getName().toUpperCase().startsWith(engine.getSymmetricDialect().getTablePrefix().toUpperCase())) {
                tables.add(table);
            }
        }
        List<Trigger> triggers = triggerRouterService.getTriggers();
        for (Trigger trigger : triggers) {
            Table table = engine.getDatabasePlatform().getTableFromCache(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName(), false);
            if (table != null) {
                tables.add(table);
            }
        }
        fos = new FileOutputStream(new File(tmpDir, "table-definitions.xml"));
        DbExport export = new DbExport(engine.getDatabasePlatform());
        export.setFormat(Format.XML);
        export.setNoData(true);
        export.exportTables(fos, tables.toArray(new Table[tables.size()]));
    } catch (Exception e) {
        log.warn("Failed to export table definitions", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    String tablePrefix = engine.getTablePrefix();
    DbExport export = new DbExport(engine.getDatabasePlatform());
    export.setFormat(Format.CSV);
    export.setNoCreateInfo(true);
    extract(export, new File(tmpDir, "sym_identity.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_IDENTITY));
    extract(export, new File(tmpDir, "sym_node.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE));
    extract(export, new File(tmpDir, "sym_node_security.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_SECURITY));
    extract(export, new File(tmpDir, "sym_node_host.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_HOST));
    extract(export, new File(tmpDir, "sym_trigger_hist.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_TRIGGER_HIST));
    try {
        if (!parameterService.is(ParameterConstants.CLUSTER_LOCKING_ENABLED)) {
            engine.getNodeCommunicationService().persistToTableForSnapshot();
            engine.getClusterService().persistToTableForSnapshot();
        }
    } catch (Exception e) {
        log.warn("Unable to add SYM_NODE_COMMUNICATION to the snapshot.", e);
    }
    extract(export, new File(tmpDir, "sym_lock.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_LOCK));
    extract(export, new File(tmpDir, "sym_node_communication.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_COMMUNICATION));
    extract(export, 10000, "order by create_time desc", new File(tmpDir, "sym_outgoing_batch.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_OUTGOING_BATCH));
    extract(export, 10000, "where status != 'OK' order by create_time", new File(tmpDir, "sym_outgoing_batch_not_ok.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_OUTGOING_BATCH));
    extract(export, 10000, "order by create_time desc", new File(tmpDir, "sym_incoming_batch.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_INCOMING_BATCH));
    extract(export, 10000, "where status != 'OK' order by create_time", new File(tmpDir, "sym_incoming_batch_not_ok.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_INCOMING_BATCH));
    extract(export, 5000, "order by start_id, end_id desc", new File(tmpDir, "sym_data_gap.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_DATA_GAP));
    extract(export, new File(tmpDir, "sym_table_reload_request.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST));
    extract(export, 5000, "order by relative_dir, file_name", new File(tmpDir, "sym_file_snapshot.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_FILE_SNAPSHOT));
    extract(export, new File(tmpDir, "sym_console_event.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_CONSOLE_EVENT));
    extract(export, new File(tmpDir, "sym_monitor_event.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_MONITOR_EVENT));
    extract(export, new File(tmpDir, "sym_extract_request.csv"), TableConstants.getTableName(tablePrefix, TableConstants.SYM_EXTRACT_REQUEST));
    if (engine.getSymmetricDialect() instanceof FirebirdSymmetricDialect) {
        final String[] monTables = { "mon$database", "mon$attachments", "mon$transactions", "mon$statements", "mon$io_stats", "mon$record_stats", "mon$memory_usage", "mon$call_stack", "mon$context_variables" };
        for (String table : monTables) {
            extract(export, new File(tmpDir, "firebird-" + table + ".csv"), table);
        }
    }
    fwriter = null;
    try {
        fwriter = new FileWriter(new File(tmpDir, "threads.txt"));
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        long[] threadIds = threadBean.getAllThreadIds();
        for (long l : threadIds) {
            ThreadInfo info = threadBean.getThreadInfo(l, 100);
            if (info != null) {
                String threadName = info.getThreadName();
                fwriter.append(StringUtils.rightPad(threadName, THREAD_INDENT_SPACE));
                fwriter.append(AppUtils.formatStackTrace(info.getStackTrace(), THREAD_INDENT_SPACE, false));
                fwriter.append("\n");
            }
        }
    } catch (Exception e) {
        log.warn("Failed to export thread information", e);
    } finally {
        IOUtils.closeQuietly(fwriter);
    }
    fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "parameters.properties"));
        Properties effectiveParameters = engine.getParameterService().getAllParameters();
        SortedProperties parameters = new SortedProperties();
        parameters.putAll(effectiveParameters);
        parameters.remove("db.password");
        parameters.store(fos, "parameters.properties");
    } catch (IOException e) {
        log.warn("Failed to export parameter information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "parameters-changed.properties"));
        Properties defaultParameters = new Properties();
        InputStream in = SnapshotUtil.class.getResourceAsStream("/symmetric-default.properties");
        defaultParameters.load(in);
        IOUtils.closeQuietly(in);
        in = SnapshotUtil.class.getResourceAsStream("/symmetric-console-default.properties");
        if (in != null) {
            defaultParameters.load(in);
            IOUtils.closeQuietly(in);
        }
        Properties effectiveParameters = engine.getParameterService().getAllParameters();
        Properties changedParameters = new SortedProperties();
        Map<String, ParameterMetaData> parameters = ParameterConstants.getParameterMetaData();
        for (String key : parameters.keySet()) {
            String defaultValue = defaultParameters.getProperty((String) key);
            String currentValue = effectiveParameters.getProperty((String) key);
            if (defaultValue == null && currentValue != null || (defaultValue != null && !defaultValue.equals(currentValue))) {
                changedParameters.put(key, currentValue == null ? "" : currentValue);
            }
        }
        changedParameters.remove("db.password");
        changedParameters.store(fos, "parameters-changed.properties");
    } catch (Exception e) {
        log.warn("Failed to export parameters-changed information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    writeRuntimeStats(engine, tmpDir);
    writeJobsStats(engine, tmpDir);
    if ("true".equals(System.getProperty(SystemConstants.SYSPROP_STANDALONE_WEB))) {
        writeDirectoryListing(engine, tmpDir);
    }
    fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "system.properties"));
        SortedProperties props = new SortedProperties();
        props.putAll(System.getProperties());
        props.store(fos, "system.properties");
    } catch (Exception e) {
        log.warn("Failed to export thread information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    try {
        File jarFile = new File(getSnapshotDirectory(engine), tmpDir.getName() + ".zip");
        JarBuilder builder = new JarBuilder(tmpDir, jarFile, new File[] { tmpDir }, Version.version());
        builder.build();
        FileUtils.deleteDirectory(tmpDir);
        return jarFile;
    } catch (Exception e) {
        throw new IoException("Failed to package snapshot files into archive", e);
    }
}
Also used : FileWriter(java.io.FileWriter) JarBuilder(org.jumpmind.util.JarBuilder) IParameterService(org.jumpmind.symmetric.service.IParameterService) Properties(java.util.Properties) Trigger(org.jumpmind.symmetric.model.Trigger) ThreadInfo(java.lang.management.ThreadInfo) TreeSet(java.util.TreeSet) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) ThreadMXBean(java.lang.management.ThreadMXBean) Table(org.jumpmind.db.model.Table) FirebirdSymmetricDialect(org.jumpmind.symmetric.db.firebird.FirebirdSymmetricDialect) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.util.Date) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) DbExport(org.jumpmind.symmetric.io.data.DbExport) Layout(org.apache.log4j.Layout) FileOutputStream(java.io.FileOutputStream) IoException(org.jumpmind.exception.IoException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) ParameterMetaData(org.jumpmind.properties.DefaultParameterParser.ParameterMetaData)

Example 8 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class TypedPropertiesFactory method reload.

public TypedProperties reload() {
    PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
    factoryBean.setIgnoreResourceNotFound(true);
    factoryBean.setLocalOverride(true);
    factoryBean.setSingleton(false);
    factoryBean.setProperties(properties);
    factoryBean.setLocations(buildLocations(propertiesFile));
    try {
        TypedProperties properties = new TypedProperties(factoryBean.getObject());
        SymmetricUtils.replaceSystemAndEnvironmentVariables(properties);
        return properties;
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) TypedProperties(org.jumpmind.properties.TypedProperties)

Example 9 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class FileSyncService method loadFilesFromPush.

public void loadFilesFromPush(String nodeId, InputStream in, OutputStream out) {
    INodeService nodeService = engine.getNodeService();
    Node local = nodeService.findIdentity();
    Node sourceNode = nodeService.findNode(nodeId, true);
    if (local != null && sourceNode != null) {
        ProcessInfo processInfo = engine.getStatisticManager().newProcessInfo(new ProcessInfoKey(nodeId, local.getNodeId(), ProcessInfoKey.ProcessType.FILE_SYNC_PUSH_HANDLER));
        try {
            List<IncomingBatch> list = processZip(in, nodeId, processInfo);
            NodeSecurity security = nodeService.findNodeSecurity(local.getNodeId(), true);
            processInfo.setStatus(ProcessInfo.Status.ACKING);
            engine.getTransportManager().writeAcknowledgement(out, sourceNode, list, local, security != null ? security.getNodePassword() : null);
            processInfo.setStatus(ProcessInfo.Status.OK);
        } catch (Throwable e) {
            processInfo.setStatus(ProcessInfo.Status.ERROR);
            if (e instanceof IOException) {
                throw new IoException((IOException) e);
            } else if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new RuntimeException(e);
            }
        }
    } else {
        throw new SymmetricException("Could not load data because the node is not registered");
    }
}
Also used : NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) INodeService(org.jumpmind.symmetric.service.INodeService) Node(org.jumpmind.symmetric.model.Node) SymmetricException(org.jumpmind.symmetric.SymmetricException) ProcessInfoKey(org.jumpmind.symmetric.model.ProcessInfoKey) IoException(org.jumpmind.exception.IoException) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) IOException(java.io.IOException) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch)

Example 10 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class FileSyncService method processZip.

protected List<IncomingBatch> processZip(InputStream is, String sourceNodeId, ProcessInfo processInfo) throws IOException {
    File unzipDir = new File(parameterService.getTempDirectory(), String.format("filesync_incoming/%s/%s", engine.getNodeService().findIdentityNodeId(), sourceNodeId));
    FileUtils.deleteDirectory(unzipDir);
    unzipDir.mkdirs();
    try {
        AppUtils.unzip(is, unzipDir);
    } catch (IoException ex) {
        if (ex.toString().contains("EOFException")) {
        // This happens on Android, when there is an empty zip.
        //log.debug("Caught exception while unzipping.", ex);
        } else {
            throw ex;
        }
    }
    Set<Long> batchIds = new TreeSet<Long>();
    String[] files = unzipDir.list(DirectoryFileFilter.INSTANCE);
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            try {
                batchIds.add(Long.parseLong(files[i]));
            } catch (NumberFormatException e) {
                log.error("Unexpected directory name.  Expected a number representing a batch id.  Instead the directory was named '{}'", files[i]);
            }
        }
    }
    List<IncomingBatch> batchesProcessed = new ArrayList<IncomingBatch>();
    IIncomingBatchService incomingBatchService = engine.getIncomingBatchService();
    processInfo.setStatus(ProcessInfo.Status.LOADING);
    for (Long batchId : batchIds) {
        processInfo.setCurrentBatchId(batchId);
        processInfo.incrementBatchCount();
        File batchDir = new File(unzipDir, Long.toString(batchId));
        IncomingBatch incomingBatch = new IncomingBatch();
        File batchInfo = new File(batchDir, "batch-info.txt");
        if (batchInfo.exists()) {
            List<String> info = FileUtils.readLines(batchInfo);
            if (info != null && info.size() > 0) {
                incomingBatch.setChannelId(info.get(0).trim());
            } else {
                incomingBatch.setChannelId(Constants.CHANNEL_FILESYNC);
            }
        } else {
            incomingBatch.setChannelId(Constants.CHANNEL_FILESYNC);
        }
        incomingBatch.setBatchId(batchId);
        incomingBatch.setStatus(IncomingBatch.Status.LD);
        incomingBatch.setNodeId(sourceNodeId);
        incomingBatch.setByteCount(FileUtils.sizeOfDirectory(batchDir));
        batchesProcessed.add(incomingBatch);
        if (incomingBatchService.acquireIncomingBatch(incomingBatch)) {
            File syncScript = new File(batchDir, "sync.bsh");
            if (syncScript.exists()) {
                String script = FileUtils.readFileToString(syncScript);
                Interpreter interpreter = new Interpreter();
                boolean isLocked = false;
                try {
                    setInterpreterVariables(engine, sourceNodeId, batchDir, interpreter);
                    long waitMillis = getParameterService().getLong(ParameterConstants.FILE_SYNC_LOCK_WAIT_MS);
                    log.debug("The {} node is attempting to get shared lock for to update incoming status", sourceNodeId);
                    isLocked = engine.getClusterService().lock(ClusterConstants.FILE_SYNC_SHARED, ClusterConstants.TYPE_SHARED, waitMillis);
                    if (isLocked) {
                        log.debug("The {} node got a shared file sync lock", sourceNodeId);
                        @SuppressWarnings("unchecked") Map<String, String> filesToEventType = (Map<String, String>) interpreter.eval(script);
                        if (engine.getParameterService().is(ParameterConstants.FILE_SYNC_PREVENT_PING_BACK)) {
                            updateFileIncoming(sourceNodeId, filesToEventType);
                        }
                        incomingBatch.setStatementCount(filesToEventType != null ? filesToEventType.size() : 0);
                    } else {
                        throw new RuntimeException("Could not obtain file sync shared lock within " + waitMillis + " millis");
                    }
                    incomingBatch.setStatus(IncomingBatch.Status.OK);
                    if (incomingBatchService.isRecordOkBatchesEnabled()) {
                        incomingBatchService.updateIncomingBatch(incomingBatch);
                    } else if (incomingBatch.isRetry()) {
                        incomingBatchService.deleteIncomingBatch(incomingBatch);
                    }
                } catch (Throwable ex) {
                    if (ex instanceof TargetError) {
                        Throwable target = ((TargetError) ex).getTarget();
                        if (target != null) {
                            ex = target;
                        }
                    } else if (ex instanceof EvalError) {
                        log.error("Failed to evalulate the script:\n{}", script);
                    }
                    if (ex instanceof FileConflictException) {
                        log.error(ex.getMessage() + ".  Failed to process file sync batch " + batchId);
                    } else {
                        log.error("Failed to process file sync batch " + batchId, ex);
                    }
                    incomingBatch.setErrorFlag(true);
                    incomingBatch.setStatus(IncomingBatch.Status.ER);
                    incomingBatch.setSqlMessage(ex.getMessage());
                    if (incomingBatchService.isRecordOkBatchesEnabled() || incomingBatch.isRetry()) {
                        incomingBatchService.updateIncomingBatch(incomingBatch);
                    } else {
                        incomingBatchService.insertIncomingBatch(incomingBatch);
                    }
                    processInfo.setStatus(ProcessInfo.Status.ERROR);
                    break;
                } finally {
                    log.debug("The {} node is done processing file sync files", sourceNodeId);
                    if (isLocked) {
                        engine.getClusterService().unlock(ClusterConstants.FILE_SYNC_SHARED, ClusterConstants.TYPE_SHARED);
                    }
                }
            } else {
                log.error("Could not find the sync.bsh script for batch {}", batchId);
            }
        }
    }
    return batchesProcessed;
}
Also used : ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) IIncomingBatchService(org.jumpmind.symmetric.service.IIncomingBatchService) Interpreter(bsh.Interpreter) EvalError(bsh.EvalError) TargetError(bsh.TargetError) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) IoException(org.jumpmind.exception.IoException) FileConflictException(org.jumpmind.symmetric.file.FileConflictException) File(java.io.File) Map(java.util.Map)

Aggregations

IoException (org.jumpmind.exception.IoException)48 IOException (java.io.IOException)41 File (java.io.File)8 Table (org.jumpmind.db.model.Table)8 BufferedReader (java.io.BufferedReader)6 Column (org.jumpmind.db.model.Column)6 BufferedWriter (java.io.BufferedWriter)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SymmetricException (org.jumpmind.symmetric.SymmetricException)3 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)3 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 BufferedInputStream (java.io.BufferedInputStream)2