/** * Registers the time when the last keep-alive was received for a given platform ident. * * @param platformIdent * ID of the platform ident. */ public void handleKeepAliveSignal(long platformIdent) { AgentStatusData agentStatusData = agentStatusDataMap.get(platformIdent); if (null != agentStatusData) { agentStatusData.setLastKeepAliveTimestamp(System.currentTimeMillis()); // Updates the agent status if no keep-alive messages were received before if (agentStatusData.getAgentConnection() == AgentConnection.NO_KEEP_ALIVE) { agentStatusData.setAgentConnection(AgentConnection.CONNECTED); if (log.isInfoEnabled()) { log.info("Platform " + platformIdent + " sending keep-alive signals again."); } } } else { registerConnected(platformIdent); agentStatusDataMap.get(platformIdent).setInstrumentationStatus(InstrumentationStatus.NO_CLASS_CACHE_AVAILABLE); if (log.isInfoEnabled()) { log.info("Platform " + platformIdent + " has been reconnected, but no class cache is available for this agent. Please reconnect the agent, to reload the class cache."); } } }
/** * Creates an {@link UpdatedInstrumentationMessage} which contains all stored * {@link InstrumentationDefinition}. The created message is put in the message provider for the * agent to fetch. * * @param platformId * the id of the platform which {@link InstrumentationDefinition}s should be provided * for fetching */ public synchronized void flush(long platformId) { if (log.isDebugEnabled()) { log.debug("Flushing new instrumentations for agent {}.", platformId); } Map<String, InstrumentationDefinition> pendingDefinitions = definitionBuffer.put(platformId, new HashMap<String, InstrumentationDefinition>()); if (MapUtils.isNotEmpty(pendingDefinitions)) { UpdatedInstrumentationMessage message = new UpdatedInstrumentationMessage(); message.getMessageContent().addAll(pendingDefinitions.values()); messageProvider.provideMessage(platformId, message); } AgentStatusData agentStatusData = agentStatusDataProvider.getAgentStatusDataMap().get(platformId); if (agentStatusData != null) { agentStatusData.setInstrumentationStatus(InstrumentationStatus.UP_TO_DATE); } }
/** * Registers that the agent was connected. * * @param platformIdent * ID of the platform ident. */ public void registerConnected(long platformIdent) { AgentStatusData agentStatusData = agentStatusDataMap.get(platformIdent); if (null == agentStatusData) { agentStatusData = new AgentStatusData(AgentConnection.CONNECTED); AgentStatusData existing = agentStatusDataMap.putIfAbsent(platformIdent, agentStatusData); if (null != existing) { agentStatusData = existing; } } long currentTimeMillis = System.currentTimeMillis(); agentStatusData.setLastKeepAliveTimestamp(currentTimeMillis); agentStatusData.setConnectionTimestamp(currentTimeMillis); agentStatusData.setAgentConnection(AgentConnection.CONNECTED); agentStatusData.setPendingSinceTime(currentTimeMillis); // set instrumentation status up-to-date agentStatusData.setInstrumentationStatus(InstrumentationStatus.UP_TO_DATE); }
/** * Handles an event of type {@link ClassInstrumentationChangedEvent}. * * @param event * the event instance */ private void handleClassInstrumentationChangedEvent(ClassInstrumentationChangedEvent event) { if (log.isDebugEnabled()) { log.debug("Putting instrumentation definitions for agent {} into the definition buffer.", event.getAgentId()); } Map<String, InstrumentationDefinition> pendingDefinitions = definitionBuffer.get(event.getAgentId()); if (pendingDefinitions == null) { pendingDefinitions = new HashMap<>(); definitionBuffer.put(event.getAgentId(), pendingDefinitions); } for (InstrumentationDefinition definition : event.getInstrumentationDefinitions()) { pendingDefinitions.put(definition.getClassName(), definition); } AgentStatusData agentStatusData = agentStatusDataProvider.getAgentStatusDataMap().get(event.getAgentId()); if (agentStatusData != null) { if (agentStatusData.getInstrumentationStatus() != InstrumentationStatus.PENDING) { agentStatusData.setInstrumentationStatus(InstrumentationStatus.PENDING); agentStatusData.setPendingSinceTime(System.currentTimeMillis()); } } }
@Test @SuppressWarnings("rawtypes") public void successful() throws Exception { when(definition.getClassName()).thenReturn("class.one"); ClassInstrumentationChangedEvent event = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definition)); messageGate.onApplicationEvent(event); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(ImmutableMap.of(10L, statusData)); when(statusData.getInstrumentationStatus()).thenReturn(InstrumentationStatus.PENDING); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(1)); messageGate.flush(10L); ArgumentCaptor<IAgentMessage> messageCaptor = ArgumentCaptor.forClass(IAgentMessage.class); verify(messageProvider).provideMessage(eq(10L), messageCaptor.capture()); verify(statusData).setInstrumentationStatus(InstrumentationStatus.UP_TO_DATE); verifyNoMoreInteractions(statusData, messageProvider); assertThat(((UpdatedInstrumentationMessage) messageCaptor.getValue()).getMessageContent(), contains(definition)); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(0)); }
@Test public void changeEventAddInstrumentationDefinitions() throws Exception { when(definitionOne.getClassName()).thenReturn("class.one"); when(definitionTwo.getClassName()).thenReturn("class.two"); when(definitionThree.getClassName()).thenReturn("class.three"); ClassInstrumentationChangedEvent eventOne = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionOne, definitionTwo)); ClassInstrumentationChangedEvent eventTwo = new ClassInstrumentationChangedEvent(this, 10L, Arrays.asList(definitionThree)); when(agentStatusDataProvider.getAgentStatusDataMap()).thenReturn(ImmutableMap.of(10L, statusData)); when(statusData.getInstrumentationStatus()).thenReturn(InstrumentationStatus.UP_TO_DATE, InstrumentationStatus.PENDING); long currentTime = System.currentTimeMillis(); messageGate.onApplicationEvent(eventOne); messageGate.onApplicationEvent(eventTwo); verify(definitionOne).getClassName(); verify(definitionTwo).getClassName(); verify(definitionThree).getClassName(); verify(agentStatusDataProvider, times(2)).getAgentStatusDataMap(); verify(statusData, times(2)).getInstrumentationStatus(); verify(statusData).setInstrumentationStatus(InstrumentationStatus.PENDING); ArgumentCaptor<Long> timeCaptor = ArgumentCaptor.forClass(Long.class); verify(statusData).setPendingSinceTime(timeCaptor.capture()); verifyNoMoreInteractions(definitionOne, definitionTwo, definitionThree, agentStatusDataProvider, statusData); verifyZeroInteractions(messageProvider); assertThat(timeCaptor.getValue(), greaterThanOrEqualTo(currentTime)); assertThat(getDefinitionBuffer().entrySet(), hasSize(1)); assertThat(getDefinitionBuffer().get(10L).entrySet(), hasSize(3)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.one", definitionOne)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.two", definitionTwo)); assertThat(getDefinitionBuffer().get(10L), hasEntry("class.three", definitionThree)); }