Search in sources :

Example 1 with ApiEvent

use of com.cloudera.api.swagger.model.ApiEvent in project knox by apache.

the class PollingConfigurationAnalyzer method getRelevantEvents.

/**
 * Get relevant events for the specified ClouderaManager cluster.
 *
 * @param address     The address of the ClouderaManager instance.
 * @param clusterName The name of the cluster.
 *
 * @return A List of StartEvent objects for service start events since the last time they were queried.
 */
private List<StartEvent> getRelevantEvents(final String address, final String clusterName) {
    List<StartEvent> relevantEvents = new ArrayList<>();
    // Get the last event query timestamp
    Instant lastTimestamp = getEventQueryTimestamp(address, clusterName);
    // If this is the first query, then define the last timestamp
    if (lastTimestamp == null) {
        lastTimestamp = Instant.now().minus(eventQueryDefaultTimestampOffset, ChronoUnit.MILLIS);
    }
    // Go back in time an '2 x interval' more to mitigate the chance of losing a relevant audit event
    lastTimestamp = lastTimestamp.minus(interval * 2, ChronoUnit.SECONDS);
    log.queryingConfigActivationEventsFromCluster(clusterName, address, lastTimestamp.toString());
    // Record the new event query timestamp for this address/cluster
    setEventQueryTimestamp(address, clusterName, Instant.now());
    // Query the event log from CM for service/cluster start events
    final List<ApiEvent> events = queryEvents(getApiClient(configCache.getDiscoveryConfig(address, clusterName)), clusterName, lastTimestamp.toString());
    if (events.isEmpty()) {
        log.noActivationEventFound();
    } else {
        for (ApiEvent event : events) {
            if (isRelevantEvent(event)) {
                relevantEvents.add(new StartEvent(event));
            }
        }
    }
    return relevantEvents;
}
Also used : ApiEvent(com.cloudera.api.swagger.model.ApiEvent) Instant(java.time.Instant) ArrayList(java.util.ArrayList)

Example 2 with ApiEvent

use of com.cloudera.api.swagger.model.ApiEvent in project knox by apache.

the class PollingConfigurationAnalyzerTest method doTestStartEvent.

private void doTestStartEvent(final ApiEventCategory category) {
    final String clusterName = "My Cluster";
    final String serviceType = NameNodeServiceModelGenerator.SERVICE_TYPE;
    final String service = NameNodeServiceModelGenerator.SERVICE;
    List<ApiEventAttribute> apiEventAttrs = new ArrayList<>();
    apiEventAttrs.add(createEventAttribute("CLUSTER", clusterName));
    apiEventAttrs.add(createEventAttribute("SERVICE_TYPE", serviceType));
    apiEventAttrs.add(createEventAttribute("SERVICE", service));
    ApiEvent apiEvent = createApiEvent(category, apiEventAttrs, null);
    PollingConfigurationAnalyzer.StartEvent restartEvent = new PollingConfigurationAnalyzer.StartEvent(apiEvent);
    assertNotNull(restartEvent);
    assertEquals(clusterName, restartEvent.getClusterName());
    assertEquals(serviceType, restartEvent.getServiceType());
    assertEquals(service, restartEvent.getService());
    assertNotNull(restartEvent.getTimestamp());
}
Also used : ApiEventAttribute(com.cloudera.api.swagger.model.ApiEventAttribute) ApiEvent(com.cloudera.api.swagger.model.ApiEvent) ArrayList(java.util.ArrayList)

Example 3 with ApiEvent

use of com.cloudera.api.swagger.model.ApiEvent in project knox by apache.

the class PollingConfigurationAnalyzerTest method testRollingClusterRestartEvent.

/**
 * Test the rolling restart of an entire cluster, for which it should be assumed that configuration has changed.
 */
@Test
public void testRollingClusterRestartEvent() {
    final String address = "http://host1:1234";
    final String clusterName = "Cluster 6";
    // Simulate a successful rolling cluster restart event
    ApiEvent rollingRestartEvent = createApiEvent(clusterName, PollingConfigurationAnalyzer.CM_SERVICE_TYPE, PollingConfigurationAnalyzer.CM_SERVICE, PollingConfigurationAnalyzer.ROLLING_RESTART_COMMAND, PollingConfigurationAnalyzer.SUCCEEDED_STATUS, "EV_CLUSTER_ROLLING_RESTARTED");
    ChangeListener listener = doTestEvent(rollingRestartEvent, address, clusterName, Collections.emptyMap(), Collections.emptyMap());
    assertTrue("Expected a change notification", listener.wasNotified(address, clusterName));
}
Also used : ApiEvent(com.cloudera.api.swagger.model.ApiEvent) ConfigurationChangeListener(org.apache.knox.gateway.topology.discovery.ClusterConfigurationMonitor.ConfigurationChangeListener) Test(org.junit.Test)

Example 4 with ApiEvent

use of com.cloudera.api.swagger.model.ApiEvent in project knox by apache.

the class PollingConfigurationAnalyzerTest method createApiEvent.

private ApiEvent createApiEvent(final ApiEventCategory category, final List<ApiEventAttribute> attrs, String id) {
    ApiEvent event = EasyMock.createNiceMock(ApiEvent.class);
    if (id == null) {
        EasyMock.expect(event.getId()).andReturn(RandomStringUtils.random(8, true, true)).anyTimes();
    } else {
        EasyMock.expect(event.getId()).andReturn(id).anyTimes();
    }
    EasyMock.expect(event.getTimeOccurred()).andReturn(Instant.now().toString()).anyTimes();
    EasyMock.expect(event.getCategory()).andReturn(category).anyTimes();
    EasyMock.expect(event.getAttributes()).andReturn(attrs).anyTimes();
    EasyMock.replay(event);
    return event;
}
Also used : ApiEvent(com.cloudera.api.swagger.model.ApiEvent)

Example 5 with ApiEvent

use of com.cloudera.api.swagger.model.ApiEvent in project knox by apache.

the class PollingConfigurationAnalyzerTest method testRollingServiceRestartWithoutConfigChange.

/**
 * Test the rolling restart of an existing service when no relevant configuration has changed.
 */
@Test
public void testRollingServiceRestartWithoutConfigChange() {
    final String clusterName = "Cluster 1";
    // Simulate a successful rolling service restart event
    ApiEvent rollingRestartEvent = createApiEvent(clusterName, NameNodeServiceModelGenerator.SERVICE_TYPE, NameNodeServiceModelGenerator.SERVICE, PollingConfigurationAnalyzer.ROLLING_RESTART_COMMAND, PollingConfigurationAnalyzer.SUCCEEDED_STATUS, "EV_SERVICE_ROLLING_RESTARTED");
    doTestEventWithoutConfigChange(rollingRestartEvent, clusterName);
}
Also used : ApiEvent(com.cloudera.api.swagger.model.ApiEvent) Test(org.junit.Test)

Aggregations

ApiEvent (com.cloudera.api.swagger.model.ApiEvent)15 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)4 ConfigurationChangeListener (org.apache.knox.gateway.topology.discovery.ClusterConfigurationMonitor.ConfigurationChangeListener)4 ApiEventAttribute (com.cloudera.api.swagger.model.ApiEventAttribute)2 EventsResourceApi (com.cloudera.api.swagger.EventsResourceApi)1 ApiException (com.cloudera.api.swagger.client.ApiException)1 ApiEventQueryResult (com.cloudera.api.swagger.model.ApiEventQueryResult)1 Instant (java.time.Instant)1