From 2264a8d3960dc92da51a353a4f7c561f95936203 Mon Sep 17 00:00:00 2001 From: giovannifs Date: Fri, 27 Mar 2015 18:00:32 -0300 Subject: [PATCH] Develop NoFMemberPicker --- .../manager/core/NoFMemberPicker.java | 81 ++++++++++++++++++ .../prioritization/PrioritizationPlugin.java | 13 +++ .../manager/core/TestNoFMemberPicker.java | 85 +++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 src/main/java/org/fogbowcloud/manager/core/NoFMemberPicker.java create mode 100644 src/main/java/org/fogbowcloud/manager/core/plugins/prioritization/PrioritizationPlugin.java create mode 100644 src/test/java/org/fogbowcloud/manager/core/TestNoFMemberPicker.java diff --git a/src/main/java/org/fogbowcloud/manager/core/NoFMemberPicker.java b/src/main/java/org/fogbowcloud/manager/core/NoFMemberPicker.java new file mode 100644 index 00000000..b13e797a --- /dev/null +++ b/src/main/java/org/fogbowcloud/manager/core/NoFMemberPicker.java @@ -0,0 +1,81 @@ +package org.fogbowcloud.manager.core; + +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.fogbowcloud.manager.core.model.FederationMember; +import org.fogbowcloud.manager.core.plugins.AccountingPlugin; +import org.fogbowcloud.manager.core.plugins.accounting.ResourceUsage; + +public class NoFMemberPicker implements FederationMemberPicker { + + private AccountingPlugin accoutingPlugin; + private String localMemberId; + + public NoFMemberPicker(Properties properties, AccountingPlugin accoutingPlugin) { + this.accoutingPlugin = accoutingPlugin; + this.localMemberId = properties.getProperty(ConfigurationConstants.XMPP_JID_KEY); + } + + @Override + public FederationMember pick(ManagerController facade) { + List onlineMembers = facade.getMembers(); + Map membersUsage = accoutingPlugin.getMembersUsage(); + LinkedList reputableMembers = new LinkedList(); + + for (FederationMember currentMember : onlineMembers) { + String memberId = currentMember.getResourcesInfo().getId(); + if (localMemberId.equals(memberId)) { + continue; + } + + double reputation = 0d; + if (membersUsage.containsKey(memberId)) { + reputation = membersUsage.get(memberId).getConsumed() + - membersUsage.get(memberId).getDonated() + + Math.sqrt(membersUsage.get(memberId).getDonated()); + } + reputableMembers.add(new ReputableFederationMember(currentMember, reputation)); + } + + if (reputableMembers.isEmpty()) { + return null; + } + Collections.sort(reputableMembers, new ReputableFederationMemberComparator()); + return reputableMembers.getLast().getMember(); + } + + class ReputableFederationMember { + + private FederationMember member; + private double reputation; + + public ReputableFederationMember(FederationMember member, double reputation) { + this.member = member; + this.reputation = reputation; + } + + public FederationMember getMember() { + return member; + } + + public double getReputation() { + return reputation; + } + } + + class ReputableFederationMemberComparator implements Comparator { + @Override + public int compare(ReputableFederationMember firstReputableMember, + ReputableFederationMember secondReputableMember) { + + return new Double(firstReputableMember.getReputation()).compareTo(new Double( + secondReputableMember.getReputation())); + } + } +} + diff --git a/src/main/java/org/fogbowcloud/manager/core/plugins/prioritization/PrioritizationPlugin.java b/src/main/java/org/fogbowcloud/manager/core/plugins/prioritization/PrioritizationPlugin.java new file mode 100644 index 00000000..edd1cdaf --- /dev/null +++ b/src/main/java/org/fogbowcloud/manager/core/plugins/prioritization/PrioritizationPlugin.java @@ -0,0 +1,13 @@ +package org.fogbowcloud.manager.core.plugins.prioritization; + +import java.util.List; + +import org.fogbowcloud.manager.occi.request.Request; + +public interface PrioritizationPlugin { + + public Request chooseForAllocation(List openRequests); + + public Request takeFrom(List requestWithInstance); + +} diff --git a/src/test/java/org/fogbowcloud/manager/core/TestNoFMemberPicker.java b/src/test/java/org/fogbowcloud/manager/core/TestNoFMemberPicker.java new file mode 100644 index 00000000..25837b03 --- /dev/null +++ b/src/test/java/org/fogbowcloud/manager/core/TestNoFMemberPicker.java @@ -0,0 +1,85 @@ +package org.fogbowcloud.manager.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Properties; + +import org.fogbowcloud.manager.core.model.FederationMember; +import org.fogbowcloud.manager.core.model.ResourcesInfo; +import org.fogbowcloud.manager.core.plugins.AccountingPlugin; +import org.fogbowcloud.manager.core.plugins.accounting.ResourceUsage; +import org.fogbowcloud.manager.core.util.DefaultDataTestHelper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestNoFMemberPicker { + + private AccountingPlugin accoutingPlugin; + private Properties properties; + private ManagerController facade; + + @Before + public void setUp(){ + properties = new Properties(); + properties.put(ConfigurationConstants.XMPP_JID_KEY, + DefaultDataTestHelper.LOCAL_MANAGER_COMPONENT_URL); + + accoutingPlugin = Mockito.mock(AccountingPlugin.class); + + facade = Mockito.mock(ManagerController.class); + } + + @Test + public void testEmptyMembers() { + // mocking + Mockito.when(facade.getMembers()).thenReturn(new ArrayList()); + Mockito.when(accoutingPlugin.getMembersUsage()).thenReturn( + new HashMap()); + + NoFMemberPicker nofPicker = new NoFMemberPicker(properties, accoutingPlugin); + Assert.assertNull(nofPicker.pick(facade)); + } + + @Test + public void testOnlyLocalMember() { + // mocking + FederationMember localMember = new FederationMember(new ResourcesInfo( + DefaultDataTestHelper.LOCAL_MANAGER_COMPONENT_URL, "", "", "", "", null)); + ArrayList membersToReturn = new ArrayList(); + membersToReturn.add(localMember); + + Mockito.when(facade.getMembers()).thenReturn(membersToReturn); + Mockito.when(accoutingPlugin.getMembersUsage()).thenReturn( + new HashMap()); + + NoFMemberPicker nofPicker = new NoFMemberPicker(properties, accoutingPlugin); + Assert.assertNull(nofPicker.pick(facade)); + } + + @Test + public void testOneRemoteMember() { + // mocking facade + FederationMember localMember = new FederationMember(new ResourcesInfo( + DefaultDataTestHelper.LOCAL_MANAGER_COMPONENT_URL, "", "", "", "", null)); + FederationMember remoteMember = new FederationMember(new ResourcesInfo( + DefaultDataTestHelper.REMOTE_MANAGER_COMPONENT_URL, "", "", "", "", null)); + ArrayList membersToReturn = new ArrayList(); + membersToReturn.add(localMember); + membersToReturn.add(remoteMember); + Mockito.when(facade.getMembers()).thenReturn(membersToReturn); + + // mocking accounting + HashMap membersUsageToReturn = new HashMap(); + ResourceUsage resUsage = new ResourceUsage(DefaultDataTestHelper.REMOTE_MANAGER_COMPONENT_URL); + resUsage.addConsumption(4); + resUsage.addDonation(16); + membersUsageToReturn.put(DefaultDataTestHelper.REMOTE_MANAGER_COMPONENT_URL, resUsage); + Mockito.when(accoutingPlugin.getMembersUsage()).thenReturn( + membersUsageToReturn); + + NoFMemberPicker nofPicker = new NoFMemberPicker(properties, accoutingPlugin); + Assert.assertEquals(remoteMember, nofPicker.pick(facade)); + } +}