From 02a005c9dbca48237e14c35caa11649bb360dd44 Mon Sep 17 00:00:00 2001 From: Dilshat Aliev Date: Thu, 19 Jul 2018 14:32:56 +0600 Subject: [PATCH] #2547 Send peer metrics on container destruction --- .../core/hubmanager/api/HubManager.java | 2 + .../core/hubmanager/impl/HubManagerImpl.java | 61 +++++++++++++++++-- .../impl/adapter/HubAdapterImpl.java | 22 +++++-- .../impl/util/ReschedulableTimer.java | 45 ++++++++++++++ 4 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/util/ReschedulableTimer.java diff --git a/management/server/core/hub-manager/hub-manager-api/src/main/java/io/subutai/core/hubmanager/api/HubManager.java b/management/server/core/hub-manager/hub-manager-api/src/main/java/io/subutai/core/hubmanager/api/HubManager.java index 74fcaf1580a..8009abaf06c 100644 --- a/management/server/core/hub-manager/hub-manager-api/src/main/java/io/subutai/core/hubmanager/api/HubManager.java +++ b/management/server/core/hub-manager/hub-manager-api/src/main/java/io/subutai/core/hubmanager/api/HubManager.java @@ -49,4 +49,6 @@ public interface HubManager void sendPeersMertics() throws HubManagerException; void sendContainerMertics() throws HubManagerException; + + void schedulePeerMetrics(); } diff --git a/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/HubManagerImpl.java b/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/HubManagerImpl.java index d8bebbcf60a..6210a4c54aa 100644 --- a/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/HubManagerImpl.java +++ b/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/HubManagerImpl.java @@ -22,6 +22,7 @@ import com.google.common.collect.Sets; import io.subutai.common.dao.DaoManager; +import io.subutai.common.host.ContainerHostInfo; import io.subutai.common.host.ResourceHostInfo; import io.subutai.common.metric.QuotaAlertValue; import io.subutai.common.peer.LocalPeer; @@ -61,6 +62,7 @@ import io.subutai.core.hubmanager.impl.tunnel.TunnelEventProcessor; import io.subutai.core.hubmanager.impl.tunnel.TunnelProcessor; import io.subutai.core.hubmanager.impl.util.EnvironmentUserHelper; +import io.subutai.core.hubmanager.impl.util.ReschedulableTimer; import io.subutai.core.identity.api.IdentityManager; import io.subutai.core.identity.api.model.User; import io.subutai.core.metric.api.Monitor; @@ -131,6 +133,8 @@ public class HubManagerImpl extends HostListener implements HubManager private ContainerMetricsProcessor containerMetricsProcessor; + private ReschedulableTimer peerMetricsTimer; + public HubManagerImpl( DaoManager daoManager ) { @@ -156,6 +160,22 @@ public void init() initHubRequesters(); initHeartbeatProcessors(); + + peerMetricsTimer = new ReschedulableTimer( new Runnable() + { + @Override + public void run() + { + try + { + sendPeersMertics(); + } + catch ( HubManagerException e ) + { + log.error( "Error sending peer metrics: {}", e.getMessage() ); + } + } + } ); } catch ( Exception e ) { @@ -265,23 +285,32 @@ private void initHeartbeatProcessors() @Override public void sendHeartbeat() throws HubManagerException { - p2pLogsSender.process(); - heartbeatProcessor.sendHeartbeat( true ); - containerEventProcessor.process(); + if ( isRegisteredWithHub() ) + { + p2pLogsSender.process(); + heartbeatProcessor.sendHeartbeat( true ); + containerEventProcessor.process(); + } } @Override public void sendPeersMertics() throws HubManagerException { - peerMetricsProcessor.request(); + if ( isRegisteredWithHub() ) + { + peerMetricsProcessor.request(); + } } @Override public void sendContainerMertics() throws HubManagerException { - containerMetricsProcessor.request(); + if ( isRegisteredWithHub() ) + { + containerMetricsProcessor.request(); + } } @@ -690,4 +719,26 @@ public BrokerSettingsDto getBrokers() return response.getEntity(); } + + + @Override + public void onContainerCreated( final ContainerHostInfo containerInfo ) + { + schedulePeerMetrics(); + } + + + /** + * This method schedules sending of peer metrics to Bazaar. If a pending round is still there it gets rescheduled. + * This is done to not overwhelm Bazaar with frequent requests that can happen for example when environment is + * destroyed and its containers get destroyed one by one very quickly. + */ + @Override + public void schedulePeerMetrics() + { + if ( isRegisteredWithHub() ) + { + peerMetricsTimer.schedule( 15L ); + } + } } \ No newline at end of file diff --git a/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/adapter/HubAdapterImpl.java b/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/adapter/HubAdapterImpl.java index e2a860ae737..c296fa46514 100644 --- a/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/adapter/HubAdapterImpl.java +++ b/management/server/core/hub-manager/hub-manager-impl/src/main/java/io/subutai/core/hubmanager/impl/adapter/HubAdapterImpl.java @@ -88,7 +88,13 @@ public HubAdapterImpl( DaoManager daoManager, PeerManager peerManager, IdentityM private RestClient getRestClient() { - return ServiceLocator.lookup( HubManager.class ).getRestClient(); + return getHubManager().getRestClient(); + } + + + private HubManager getHubManager() + { + return ServiceLocator.lookup( HubManager.class ); } @@ -406,14 +412,14 @@ private void onContainerHostnameChange( String envId, String contId, String host @Override public void onEnvironmentCreated( final Environment environment ) { - //not used + getHubManager().schedulePeerMetrics(); } @Override public void onEnvironmentGrown( final Environment environment, final Set newContainers ) { - //not used + getHubManager().schedulePeerMetrics(); } @@ -421,13 +427,15 @@ public void onEnvironmentGrown( final Environment environment, final Set= 0 ); + + if ( timerTask != null ) + { + timerTask.cancel(); + } + + timerTask = new TimerTask() + { + @Override + public void run() + { + task.run(); + } + }; + + this.schedule( timerTask, TimeUnit.SECONDS.toMillis( delayInSec ) ); + } +}