Skip to content

Commit

Permalink
Develop NoFMemberPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannifs committed Mar 27, 2015
1 parent 8a9c73e commit 2264a8d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/main/java/org/fogbowcloud/manager/core/NoFMemberPicker.java
Original file line number Diff line number Diff line change
@@ -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<FederationMember> onlineMembers = facade.getMembers();
Map<String, ResourceUsage> membersUsage = accoutingPlugin.getMembersUsage();
LinkedList<ReputableFederationMember> reputableMembers = new LinkedList<ReputableFederationMember>();

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<ReputableFederationMember> {
@Override
public int compare(ReputableFederationMember firstReputableMember,
ReputableFederationMember secondReputableMember) {

return new Double(firstReputableMember.getReputation()).compareTo(new Double(
secondReputableMember.getReputation()));
}
}
}

Original file line number Diff line number Diff line change
@@ -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<Request> openRequests);

public Request takeFrom(List<Request> requestWithInstance);

}
Original file line number Diff line number Diff line change
@@ -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<FederationMember>());
Mockito.when(accoutingPlugin.getMembersUsage()).thenReturn(
new HashMap<String, ResourceUsage>());

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<FederationMember> membersToReturn = new ArrayList<FederationMember>();
membersToReturn.add(localMember);

Mockito.when(facade.getMembers()).thenReturn(membersToReturn);
Mockito.when(accoutingPlugin.getMembersUsage()).thenReturn(
new HashMap<String, ResourceUsage>());

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<FederationMember> membersToReturn = new ArrayList<FederationMember>();
membersToReturn.add(localMember);
membersToReturn.add(remoteMember);
Mockito.when(facade.getMembers()).thenReturn(membersToReturn);

// mocking accounting
HashMap<String, ResourceUsage> membersUsageToReturn = new HashMap<String, ResourceUsage>();
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));
}
}

0 comments on commit 2264a8d

Please sign in to comment.