Skip to content

Commit

Permalink
Merge pull request #2551 from subutai-io/issue-2547
Browse files Browse the repository at this point in the history
#2547 Send peer metrics on container destruction
  • Loading branch information
Dilshat authored Jul 19, 2018
2 parents 0c39d0f + 02a005c commit 9c8ff79
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ public interface HubManager
void sendPeersMertics() throws HubManagerException;

void sendContainerMertics() throws HubManagerException;

void schedulePeerMetrics();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -131,6 +133,8 @@ public class HubManagerImpl extends HostListener implements HubManager

private ContainerMetricsProcessor containerMetricsProcessor;

private ReschedulableTimer peerMetricsTimer;


public HubManagerImpl( DaoManager daoManager )
{
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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();
}
}


Expand Down Expand Up @@ -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 );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}


Expand Down Expand Up @@ -406,42 +412,50 @@ 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<EnvironmentContainerHost> newContainers )
{
//not used
getHubManager().schedulePeerMetrics();
}


@Override
public void onContainerDestroyed( final Environment environment, final String containerId )
{
destroyContainer( environment.getId(), containerId );

getHubManager().schedulePeerMetrics();
}


@Override
public void onEnvironmentDestroyed( final String environmentId )
{
//not used
getHubManager().schedulePeerMetrics();
}


@Override
public void onContainerStarted( final Environment environment, final String containerId )
{
onContainerStateChange( environment.getId(), containerId, "start" );

getHubManager().schedulePeerMetrics();

}


@Override
public void onContainerStopped( final Environment environment, final String containerId )
{
onContainerStateChange( environment.getId(), containerId, "stop" );

getHubManager().schedulePeerMetrics();

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.subutai.core.hubmanager.impl.util;


import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Preconditions;


public class ReschedulableTimer extends Timer
{
private final Runnable task;
private TimerTask timerTask;


public ReschedulableTimer( final Runnable task )
{
Preconditions.checkNotNull( task );

this.task = task;
}


public synchronized void schedule( long delayInSec )
{
Preconditions.checkArgument( delayInSec >= 0 );

if ( timerTask != null )
{
timerTask.cancel();
}

timerTask = new TimerTask()
{
@Override
public void run()
{
task.run();
}
};

this.schedule( timerTask, TimeUnit.SECONDS.toMillis( delayInSec ) );
}
}

0 comments on commit 9c8ff79

Please sign in to comment.