Search in sources :

Example 41 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class LogWriterUtils method createLogWriter.

/**
   * Creates a new LogWriter and adds it to the config properties. The config can then be used to
   * connect to DistributedSystem, thus providing early access to the LogWriter before connecting.
   * This call does not connect to the DistributedSystem. It simply creates and returns the
   * LogWriter that will eventually be used by the DistributedSystem that connects using config.
   * 
   * @param properties the DistributedSystem config properties to add LogWriter to
   * @return early access to the DistributedSystem LogWriter
   * @deprecated Please use a <code>Logger</code> from {@link LogService#getLogger()} instead.
   */
public static LogWriter createLogWriter(final Properties properties) {
    Properties nonDefault = properties;
    if (nonDefault == null) {
        nonDefault = new Properties();
    }
    DistributedTestUtils.addHydraProperties(nonDefault);
    DistributionConfig dc = new DistributionConfigImpl(nonDefault);
    LogWriter logger = LogWriterFactory.createLogWriterLogger(false, /* isLoner */
    false, /* isSecurityLog */
    dc, false);
    // if config was non-null, then these will be added to it...
    nonDefault.put(DistributionConfig.LOG_WRITER_NAME, logger);
    return logger;
}
Also used : DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) ManagerLogWriter(org.apache.geode.internal.logging.ManagerLogWriter) LogWriter(org.apache.geode.LogWriter) InternalLogWriter(org.apache.geode.internal.logging.InternalLogWriter) DistributionConfigImpl(org.apache.geode.distributed.internal.DistributionConfigImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Example 42 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class PartitionedRegionCacheCloseDUnitTest method testCacheClose.

////////// test methods ////////////////
@Test
public void testCacheClose() throws Exception, Throwable {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    // Create PRs On 2 VMs
    CacheSerializableRunnable createPRs = new CacheSerializableRunnable("createPrRegions") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            for (int i = 0; i < MAX_REGIONS; i++) {
                cache.createRegion(rName + i, createRegionAttributesForPR(1, 20));
            }
        }
    };
    // Create PRs on only 2 VMs
    vm0.invoke(createPRs);
    vm1.invoke(createPRs);
    // Do put operations on these 3 PRs asynchronosly.
    AsyncInvocation async0, async1;
    async0 = vm0.invokeAsync(new CacheSerializableRunnable("doPutOperations") {

        public void run2() {
            Cache cache = getCache();
            for (int j = 0; j < MAX_REGIONS; j++) {
                PartitionedRegion pr = (PartitionedRegion) cache.getRegion(Region.SEPARATOR + rName + j);
                assertNotNull(pr);
                int numBuckets = pr.getTotalNumberOfBuckets();
                Integer key;
                for (int k = 0; k < numBuckets; k++) {
                    key = new Integer(k);
                    pr.put(key, rName + k);
                }
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("VM0 Done put successfully for PR = " + rName + j);
            }
        }
    });
    async1 = vm1.invokeAsync(new CacheSerializableRunnable("doPutOperations") {

        public void run2() {
            Cache cache = getCache();
            for (int j = 0; j < MAX_REGIONS; j++) {
                PartitionedRegion pr = (PartitionedRegion) cache.getRegion(Region.SEPARATOR + rName + (j));
                assertNotNull(pr);
                int numBuckets = pr.getTotalNumberOfBuckets();
                int max = numBuckets * 2;
                Integer key;
                for (int k = numBuckets; k < max; k++) {
                    key = new Integer(k);
                    pr.put(key, rName + k);
                }
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("VM1 Done put successfully for PR = " + rName + j);
            }
        }
    });
    ThreadUtils.join(async0, 30 * 1000);
    ThreadUtils.join(async1, 30 * 1000);
    if (async0.exceptionOccurred()) {
        Assert.fail("Exception during async0", async0.getException());
    }
    // Here we would close cache on one of the vms.
    CacheSerializableRunnable closeCache = new CacheSerializableRunnable("closeCache") {

        public void run2() {
            Cache cache = getCache();
            cache.close();
        }
    };
    // Here we would close cache on one of the vms.
    CacheSerializableRunnable validateCacheCloseCleanUp = new CacheSerializableRunnable("validateCacheCloseCleanUp") {

        public void run2() {
            InternalCache cache = (InternalCache) getCache();
            LogWriter logger = cache.getLogger();
            final Region root = PartitionedRegionHelper.getPRRoot(cache);
            for (int j = 0; j < MAX_REGIONS; j++) {
                final String regionName = "#" + rName + j;
                Wait.waitForCriterion(new WaitCriterion() {

                    private Set<Node> nodes;

                    @Override
                    public boolean done() {
                        // Verify PRConfig for each Region.
                        PartitionRegionConfig prConf = (PartitionRegionConfig) root.get(regionName);
                        nodes = prConf.getNodes();
                        return nodes.size() == 1;
                    }

                    @Override
                    public String description() {
                        return "Expected 1 node, but found " + nodes;
                    }
                }, 30000, 100, true);
            // Verify Bucket2Node Regions.
            // Region b2n = root
            // .getSubregion(PartitionedRegionHelper.BUCKET_2_NODE_TABLE_PREFIX
            // + prConf.getPRId());
            // assertNotNull(b2n);
            // logger.info("Size of b2n for region = " + regionName + " = "
            // + b2n.size());
            //
            // for (java.util.Iterator itr = b2n.values().iterator(); itr.hasNext();) {
            // VersionedArrayList nodeList = (VersionedArrayList)itr.next();
            // logger.info("Size of nodeList for b2n entries for region = "
            // + regionName + " = " + nodeList.size());
            // assertIndexDetailsEquals("Node list: " + nodeList, 1, nodeList.size());
            // }
            }
        }
    };
    // Close all the PRs on vm2
    vm0.invoke(closeCache);
    vm1.invoke(validateCacheCloseCleanUp);
}
Also used : Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) LogWriter(org.apache.geode.LogWriter) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 43 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class TestLogWriterFactory method createLogWriter.

public static LogWriter createLogWriter(final boolean appendToFile, final boolean isLoner, final boolean isSecurityLog, final DistributionConfig config, final boolean logConfig, final FileOutputStream[] FOSHolder) {
    assertFalse(isSecurityLog);
    LogWriter logger = null;
    File logFile = config.getLogFile();
    assertNotNull(logFile);
    PrintStream out;
    String firstMsg = null;
    boolean firstMsgWarning = false;
    LogWriter mlw = null;
    if (logFile == null || logFile.equals(new File(""))) {
        out = System.out;
    } else {
        if (logFile.exists()) {
            boolean useChildLogging = config.getLogFile() != null && !config.getLogFile().equals(new File("")) && config.getLogFileSizeLimit() != 0;
            boolean statArchivesRolling = config.getStatisticArchiveFile() != null && !config.getStatisticArchiveFile().equals(new File("")) && config.getArchiveFileSizeLimit() != 0 && config.getStatisticSamplingEnabled();
            if (!appendToFile || useChildLogging || statArchivesRolling) {
                // check useChildLogging for
                // bug 50659
                File oldMain = ManagerLogWriter.getLogNameForOldMainLog(logFile, isSecurityLog || useChildLogging || statArchivesRolling);
                boolean succeeded = LogFileUtils.renameAggressively(logFile, oldMain);
                if (succeeded) {
                    firstMsg = LocalizedStrings.InternalDistributedSystem_RENAMED_OLD_LOG_FILE_TO_0.toLocalizedString(oldMain);
                } else {
                    firstMsgWarning = true;
                    firstMsg = LocalizedStrings.InternalDistributedSystem_COULD_NOT_RENAME_0_TO_1.toLocalizedString(new Object[] { logFile, oldMain });
                }
            }
        }
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(logFile, true);
        } catch (FileNotFoundException ex) {
            String s = LocalizedStrings.InternalDistributedSystem_COULD_NOT_OPEN_LOG_FILE_0.toLocalizedString(logFile);
            throw new GemFireIOException(s, ex);
        }
        out = new PrintStream(fos);
        if (FOSHolder != null) {
            FOSHolder[0] = fos;
        }
        if (isSecurityLog) {
            mlw = new SecurityManagerLogWriter(config.getSecurityLogLevel(), out, config.getName());
        } else {
            mlw = new ManagerLogWriter(config.getLogLevel(), out, config.getName());
        }
        ((ManagerLogWriter) mlw).setConfig(config);
    }
    if (mlw.infoEnabled()) {
        if (!isLoner || /* do this on a loner to fix bug 35602 */
        !Boolean.getBoolean(InternalLocator.INHIBIT_DM_BANNER)) {
            mlw.info(Banner.getString(null));
        }
    }
    logger = mlw;
    if (firstMsg != null) {
        if (firstMsgWarning) {
            logger.warning(firstMsg);
        } else {
            logger.info(firstMsg);
        }
    }
    if (logConfig && logger.configEnabled()) {
        logger.convertToLogWriterI18n().config(LocalizedStrings.InternalDistributedSystem_STARTUP_CONFIGURATIONN_0, config.toLoggerString());
    }
    // fix #46493 by moving redirectOutput invocation here
    if (ProcessLauncherContext.isRedirectingOutput()) {
        try {
            OSProcess.redirectOutput(config.getLogFile());
        } catch (IOException e) {
            logger.error(e);
        // throw new GemFireIOException("Unable to redirect output to " + config.getLogFile(), e);
        }
    }
    return logger;
}
Also used : PrintStream(java.io.PrintStream) LogWriter(org.apache.geode.LogWriter) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) GemFireIOException(org.apache.geode.GemFireIOException) GemFireIOException(org.apache.geode.GemFireIOException) IOException(java.io.IOException) File(java.io.File)

Example 44 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class LogWriterDisabledPerformanceTest method createPerformanceLogger.

@Override
protected PerformanceLogger createPerformanceLogger() {
    final LogWriter logWriter = createLogWriter();
    final PerformanceLogger perfLogger = new PerformanceLogger() {

        @Override
        public void log(final String message) {
            logWriter.fine(message);
        }

        @Override
        public boolean isEnabled() {
            return logWriter.fineEnabled();
        }
    };
    return perfLogger;
}
Also used : LogWriter(org.apache.geode.LogWriter)

Example 45 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class LogWriterPerformanceTest method createLogWriter.

protected LogWriter createLogWriter() {
    final Properties props = createGemFireProperties();
    // create configuration with log-file and log-level
    // this.configDirectory = new File(getUniqueName());
    this.configDirectory.mkdir();
    assertTrue(this.configDirectory.isDirectory() && this.configDirectory.canWrite());
    // this.gemfireProperties = new File(this.configDirectory, "gemfire.properties");
    // writeProperties(props, this.gemfireProperties);
    final DistributionConfig config = new DistributionConfigImpl(props, false, false);
    // create a LogWriter that writes to log-file
    final boolean appendToFile = false;
    final boolean isLoner = true;
    final boolean isSecurityLog = false;
    final boolean logConfig = true;
    final FileOutputStream[] fosHolder = null;
    final LogWriter logWriter = TestLogWriterFactory.createLogWriter(appendToFile, isLoner, isSecurityLog, config, logConfig, fosHolder);
    return logWriter;
}
Also used : DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) LogWriter(org.apache.geode.LogWriter) DistributionConfigImpl(org.apache.geode.distributed.internal.DistributionConfigImpl) FileOutputStream(java.io.FileOutputStream) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Aggregations

LogWriter (org.apache.geode.LogWriter)87 Test (org.junit.Test)34 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)18 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)17 Host (org.apache.geode.test.dunit.Host)17 Region (org.apache.geode.cache.Region)15 DistributedSystem (org.apache.geode.distributed.DistributedSystem)15 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)15 VM (org.apache.geode.test.dunit.VM)13 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)12 Iterator (java.util.Iterator)11 Set (java.util.Set)11 Cache (org.apache.geode.cache.Cache)11 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)11 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)10 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 Properties (java.util.Properties)8 InternalLogWriter (org.apache.geode.internal.logging.InternalLogWriter)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7