From 4078fc71f6253c2983a125370e495a40473397c1 Mon Sep 17 00:00:00 2001 From: Dilshat Aliev Date: Mon, 10 Sep 2018 15:15:58 +0600 Subject: [PATCH] #2642 added check if peer can provide requested amount of resources for local environments --- .../impl/EnvironmentManagerImpl.java | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/EnvironmentManagerImpl.java b/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/EnvironmentManagerImpl.java index 75d7b12fa5f..aa69b7f1dcf 100644 --- a/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/EnvironmentManagerImpl.java +++ b/management/server/core/environment-manager/environment-manager-impl/src/main/java/io/subutai/core/environment/impl/EnvironmentManagerImpl.java @@ -45,6 +45,8 @@ import io.subutai.common.environment.EnvironmentNotFoundException; import io.subutai.common.environment.EnvironmentPeer; import io.subutai.common.environment.EnvironmentStatus; +import io.subutai.common.environment.Node; +import io.subutai.common.environment.Nodes; import io.subutai.common.environment.Topology; import io.subutai.common.exception.ActionFailedException; import io.subutai.common.host.ContainerHostInfo; @@ -440,6 +442,24 @@ Environment createEnvironment( final Topology topology, final boolean async, Tra throw new EnvironmentCreationException( String.format( "Peer %s is offline", peer.getName() ) ); } + + + try + { + if ( !peer.canAccommodate( new Nodes( topology.getPeerNodes( peer.getId() ) ) ) ) + { + operationTracker.addLogFailed( + String.format( "Peer %s can not accommodate the requested containers", peer.getName() ) ); + + throw new EnvironmentCreationException( + String.format( "Peer %s can not accommodate the requested containers", peer.getName() ) ); + } + } + catch ( PeerException e ) + { + operationTracker.addLogFailed( e.getMessage() ); + throw new EnvironmentCreationException( e.getMessage() ); + } } @@ -602,6 +622,35 @@ public EnvironmentCreationRef modifyEnvironment( final String environmentId, fin throw new EnvironmentModificationException( String.format( "Peer %s is offline", peer.getName() ) ); } + + Set newNodes = topology == null ? Sets.newHashSet() : topology.getPeerNodes( peer.getId() ); + Map changedQuotas = + getPeerChangedContainers( peer.getId(), changedContainers, environment ); + + //check if peer can accommodate the requested nodes + if ( ( hasContainerCreation && !newNodes.isEmpty() ) || ( hasQuotaModification && !changedQuotas + .isEmpty() ) ) + { + try + { + if ( !peer.canAccommodate( new Nodes( newNodes, changedQuotas ) ) ) + { + operationTracker.addLogFailed( + String.format( "Peer %s can not accommodate the requested containers", + peer.getName() ) ); + + throw new EnvironmentModificationException( + String.format( "Peer %s can not accommodate the requested containers", + peer.getName() ) ); + } + } + catch ( PeerException e ) + { + operationTracker.addLogFailed( e.getMessage() ); + + throw new EnvironmentModificationException( e.getMessage() ); + } + } } if ( environment.getStatus() == EnvironmentStatus.UNDER_MODIFICATION @@ -668,8 +717,7 @@ public void run() private Map getPeerChangedContainers( final String peerId, - final Map - allChangedContainers, + final Map allChangedContainers, final LocalEnvironment environment ) throws EnvironmentModificationException { @@ -1552,8 +1600,7 @@ PGPSecretKeyRing createEnvironmentKeyPair( EnvironmentId envId ) throws Environm protected P2PSecretKeyModificationWorkflow getP2PSecretKeyModificationWorkflow( final LocalEnvironment environment, final String p2pSecretKey, final long p2pSecretKeyTtlSec, - final TrackerOperation - operationTracker ) + final TrackerOperation operationTracker ) { return new P2PSecretKeyModificationWorkflow( environment, p2pSecretKey, p2pSecretKeyTtlSec, operationTracker, this ); @@ -1596,8 +1643,7 @@ protected EnvironmentModifyWorkflow getEnvironmentModifyingWorkflow( final Local final Topology topology, final TrackerOperation operationTracker, final List removedContainers, - final Map - changedContainers ) + final Map changedContainers ) { return new EnvironmentModifyWorkflow( Common.DEFAULT_DOMAIN_NAME, identityManager, peerManager, securityManager, @@ -1606,8 +1652,7 @@ protected EnvironmentModifyWorkflow getEnvironmentModifyingWorkflow( final Local protected EnvironmentDestructionWorkflow getEnvironmentDestructionWorkflow( final LocalEnvironment environment, - final TrackerOperation - operationTracker ) + final TrackerOperation operationTracker ) { return new EnvironmentDestructionWorkflow( this, environment, operationTracker ); }