Search in sources :

Example 6 with MainWithChildrenRollingFileHandler

use of org.apache.geode.internal.io.MainWithChildrenRollingFileHandler in project geode by apache.

the class StatisticsMonitorTest method setUp.

@Before
public void setUp() throws Exception {
    final long startTime = System.currentTimeMillis();
    this.manager = new TestStatisticsManager(1, getClass().getSimpleName(), startTime);
    final StatArchiveHandlerConfig mockStatArchiveHandlerConfig = mock(StatArchiveHandlerConfig.class, getClass().getSimpleName() + "$" + StatArchiveHandlerConfig.class.getSimpleName());
    when(mockStatArchiveHandlerConfig.getArchiveFileName()).thenReturn(new File(""));
    when(mockStatArchiveHandlerConfig.getArchiveFileSizeLimit()).thenReturn(0L);
    when(mockStatArchiveHandlerConfig.getArchiveDiskSpaceLimit()).thenReturn(0L);
    when(mockStatArchiveHandlerConfig.getSystemId()).thenReturn(0L);
    when(mockStatArchiveHandlerConfig.getSystemStartTime()).thenReturn(0L);
    when(mockStatArchiveHandlerConfig.getSystemDirectoryPath()).thenReturn("");
    when(mockStatArchiveHandlerConfig.getProductDescription()).thenReturn(getClass().getSimpleName());
    StatisticsSampler sampler = new TestStatisticsSampler(manager);
    this.sampleCollector = new SampleCollector(sampler);
    this.sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(), new MainWithChildrenRollingFileHandler());
}
Also used : MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) File(java.io.File) Before(org.junit.Before)

Example 7 with MainWithChildrenRollingFileHandler

use of org.apache.geode.internal.io.MainWithChildrenRollingFileHandler in project geode by apache.

the class ValueMonitorIntegrationTest method testValueMonitorListener.

@Test
public void testValueMonitorListener() throws Exception {
    long startTime = System.currentTimeMillis();
    TestStatisticsManager manager = new TestStatisticsManager(1, "ValueMonitorIntegrationTest", startTime);
    StatisticsSampler sampler = new TestStatisticsSampler(manager);
    StatArchiveHandlerConfig mockStatArchiveHandlerConfig = this.mockContext.mock(StatArchiveHandlerConfig.class, testName.getMethodName() + "$StatArchiveHandlerConfig");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatArchiveHandlerConfig).getArchiveFileName();
            will(returnValue(new File("")));
            allowing(mockStatArchiveHandlerConfig).getArchiveFileSizeLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getArchiveDiskSpaceLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getSystemId();
            will(returnValue(1));
            allowing(mockStatArchiveHandlerConfig).getSystemStartTime();
            will(returnValue(startTime));
            allowing(mockStatArchiveHandlerConfig).getSystemDirectoryPath();
            will(returnValue(""));
            allowing(mockStatArchiveHandlerConfig).getProductDescription();
            will(returnValue("testFoo"));
        }
    });
    SampleCollector sampleCollector = new SampleCollector(sampler);
    sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(), new MainWithChildrenRollingFileHandler());
    StatisticDescriptor[] statsST1 = new StatisticDescriptor[] { manager.createDoubleCounter("double_counter_1", "double_counter_1_desc", "double_counter_1_units"), manager.createIntCounter("int_counter_2", "int_counter_2_desc", "int_counter_2_units"), manager.createLongCounter("long_counter_3", "long_counter_3_desc", "long_counter_3_units") };
    StatisticsType ST1 = manager.createType("ST1_name", "ST1_desc", statsST1);
    Statistics st1_1 = manager.createAtomicStatistics(ST1, "st1_1_text", 1);
    Statistics st1_2 = manager.createAtomicStatistics(ST1, "st1_2_text", 2);
    st1_1.incDouble("double_counter_1", 1000.0001);
    st1_1.incInt("int_counter_2", 2);
    st1_1.incLong("long_counter_3", 3333333333L);
    st1_2.incDouble("double_counter_1", 2000.0002);
    st1_2.incInt("int_counter_2", 3);
    st1_2.incLong("long_counter_3", 4444444444L);
    List<StatisticsNotification> notifications = new ArrayList<>();
    StatisticsListener listener = (final StatisticsNotification notification) -> {
        notifications.add(notification);
    };
    ValueMonitor monitor = new ValueMonitor().addStatistics(st1_1);
    monitor.addListener(listener);
    assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
    long timeStamp = NanoTimer.getTime();
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
    StatisticsNotification notification = notifications.remove(0);
    assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
    // validate 1 notification occurs with all 3 stats of st1_1
    st1_1.incDouble("double_counter_1", 1.1);
    st1_1.incInt("int_counter_2", 2);
    st1_1.incLong("long_counter_3", 3);
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
    notification = notifications.remove(0);
    assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
    int statCount = 0;
    Map<String, Number> expectedValues = new HashMap<>();
    expectedValues.put("double_counter_1", 1001.1001);
    expectedValues.put("int_counter_2", 4);
    expectedValues.put("long_counter_3", 3333333336L);
    for (StatisticId statId : notification) {
        Number value = expectedValues.remove(statId.getStatisticDescriptor().getName());
        assertNotNull(value);
        assertEquals(value, notification.getValue(statId));
        statCount++;
    }
    assertEquals(3, statCount);
    // validate no notification occurs when no stats are updated
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
    // validate no notification occurs when only other stats are updated
    st1_2.incDouble("double_counter_1", 3.3);
    st1_2.incInt("int_counter_2", 1);
    st1_2.incLong("long_counter_3", 2);
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertTrue("Unexpected notifications: " + notifications, notifications.isEmpty());
    // validate notification only contains stats added to monitor
    st1_1.incInt("int_counter_2", 100);
    st1_2.incInt("int_counter_2", 200);
    assertEquals(2, sampleCollector.currentHandlersForTesting().size());
    timeStamp += NanoTimer.millisToNanos(1000);
    sampleCollector.sample(timeStamp);
    awaitAtLeastTimeoutOrUntilNotifications(notifications, 2 * 1000);
    assertEquals("Unexpected notifications: " + notifications, 1, notifications.size());
    notification = notifications.remove(0);
    assertEquals(StatisticsNotification.Type.VALUE_CHANGED, notification.getType());
    statCount = 0;
    expectedValues = new HashMap<>();
    expectedValues.put("int_counter_2", 104);
    for (StatisticId statId : notification) {
        Number value = expectedValues.remove(statId.getStatisticDescriptor().getName());
        assertNotNull(value);
        assertEquals(value, notification.getValue(statId));
        statCount++;
    }
    assertEquals(1, statCount);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Expectations(org.jmock.Expectations) StatisticsType(org.apache.geode.StatisticsType) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) Statistics(org.apache.geode.Statistics) StatisticDescriptor(org.apache.geode.StatisticDescriptor) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with MainWithChildrenRollingFileHandler

use of org.apache.geode.internal.io.MainWithChildrenRollingFileHandler in project geode by apache.

the class ValueMonitorIntegrationTest method testAddRemoveListener.

@Test
public void testAddRemoveListener() throws Exception {
    long startTime = System.currentTimeMillis();
    List<Statistics> statsList = new ArrayList<Statistics>();
    StatisticsManager mockStatisticsManager = this.mockContext.mock(StatisticsManager.class, testName.getMethodName() + "$StatisticsManager");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatisticsManager).getName();
            will(returnValue("mockStatisticsManager"));
            allowing(mockStatisticsManager).getId();
            will(returnValue(1));
            allowing(mockStatisticsManager).getStartTime();
            will(returnValue(startTime));
            allowing(mockStatisticsManager).getStatListModCount();
            will(returnValue(0));
            allowing(mockStatisticsManager).getStatsList();
            will(returnValue(statsList));
        }
    });
    StatisticsSampler mockStatisticsSampler = this.mockContext.mock(StatisticsSampler.class, testName.getMethodName() + "$StatisticsSampler");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatisticsSampler).getStatisticsModCount();
            will(returnValue(0));
            allowing(mockStatisticsSampler).getStatistics();
            will(returnValue(new Statistics[] {}));
        }
    });
    StatArchiveHandlerConfig mockStatArchiveHandlerConfig = this.mockContext.mock(StatArchiveHandlerConfig.class, testName.getMethodName() + "$StatArchiveHandlerConfig");
    this.mockContext.checking(new Expectations() {

        {
            allowing(mockStatArchiveHandlerConfig).getArchiveFileName();
            will(returnValue(new File("")));
            allowing(mockStatArchiveHandlerConfig).getArchiveFileSizeLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getArchiveDiskSpaceLimit();
            will(returnValue(0));
            allowing(mockStatArchiveHandlerConfig).getSystemId();
            will(returnValue(1));
            allowing(mockStatArchiveHandlerConfig).getSystemStartTime();
            will(returnValue(startTime));
            allowing(mockStatArchiveHandlerConfig).getSystemDirectoryPath();
            will(returnValue(""));
            allowing(mockStatArchiveHandlerConfig).getProductDescription();
            will(returnValue("testAddRemoveListener"));
        }
    });
    // need a real SampleCollector for this test or the monitor can't get the handler
    SampleCollector sampleCollector = new SampleCollector(mockStatisticsSampler);
    sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(), new MainWithChildrenRollingFileHandler());
    List<StatisticsNotification> notifications = new ArrayList<>();
    StatisticsListener listener = (final StatisticsNotification notification) -> {
        notifications.add(notification);
    };
    ValueMonitor monitor = new ValueMonitor();
    long timeStamp = System.currentTimeMillis();
    Type type = Type.VALUE_CHANGED;
    Number value = 43;
    StatisticsNotification notification = createStatisticsNotification(timeStamp, type, value);
    monitor.notifyListeners(notification);
    assertTrue(notifications.isEmpty());
    monitor.addListener(listener);
    monitor.notifyListeners(notification);
    assertEquals(1, notifications.size());
    notification = notifications.remove(0);
    assertNotNull(notification);
    assertEquals(timeStamp, notification.getTimeStamp());
    assertEquals(type, notification.getType());
    StatisticId statId = createStatisticId(null, null);
    assertEquals(value, notification.getValue(statId));
    monitor.removeListener(listener);
    monitor.notifyListeners(notification);
    assertTrue(notifications.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) ArrayList(java.util.ArrayList) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) Statistics(org.apache.geode.Statistics) StatisticsType(org.apache.geode.StatisticsType) Type(org.apache.geode.internal.statistics.StatisticsNotification.Type) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with MainWithChildrenRollingFileHandler

use of org.apache.geode.internal.io.MainWithChildrenRollingFileHandler in project geode by apache.

the class ManagerLogWriter method getLogNameForOldMainLog.

public static File getLogNameForOldMainLog(File log, boolean useOldFile) {
    /*
     * this is just searching for the existing logfile name we need to search for meta log file name
     * 
     */
    RollingFileHandler rollingFileHandler = new MainWithChildrenRollingFileHandler();
    File dir = rollingFileHandler.getParentFile(log.getAbsoluteFile());
    int previousMainId = rollingFileHandler.calcNextMainId(dir, true);
    if (useOldFile) {
        if (previousMainId > 0) {
            previousMainId--;
        }
    }
    if (previousMainId == 0) {
        previousMainId = 1;
    }
    File result = null;
    int childId = rollingFileHandler.calcNextChildId(log, previousMainId > 0 ? previousMainId : 0);
    StringBuffer buf = new StringBuffer(log.getPath());
    int insertIdx = buf.lastIndexOf(".");
    if (insertIdx == -1) {
        buf.append(rollingFileHandler.formatId(previousMainId)).append(rollingFileHandler.formatId(childId));
    } else {
        buf.insert(insertIdx, rollingFileHandler.formatId(childId));
        buf.insert(insertIdx, rollingFileHandler.formatId(previousMainId));
    }
    result = new File(buf.toString());
    return result;
}
Also used : RollingFileHandler(org.apache.geode.internal.io.RollingFileHandler) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) MainWithChildrenRollingFileHandler(org.apache.geode.internal.io.MainWithChildrenRollingFileHandler) File(java.io.File)

Aggregations

MainWithChildrenRollingFileHandler (org.apache.geode.internal.io.MainWithChildrenRollingFileHandler)9 File (java.io.File)8 Before (org.junit.Before)5 Statistics (org.apache.geode.Statistics)4 StatisticsType (org.apache.geode.StatisticsType)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 StatisticDescriptor (org.apache.geode.StatisticDescriptor)2 Expectations (org.jmock.Expectations)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 CancelException (org.apache.geode.CancelException)1 NanoTimer (org.apache.geode.internal.NanoTimer)1 RollingFileHandler (org.apache.geode.internal.io.RollingFileHandler)1