use of actor4j.core.actors.ActorGroupMember in project actor4j-core by relvaner.
the class ActorBalancingOnCreation method balance.
public void balance(Map<UUID, Long> cellsMap, List<ActorThread> actorThreads, Map<UUID, Long> groupsMap, Map<UUID, Integer> groupsDistributedMap, Map<UUID, ActorCell> cells) {
List<UUID> buffer = new LinkedList<>();
for (UUID id : cells.keySet()) buffer.add(id);
int i = 0, j = 0;
for (ActorCell cell : cells.values()) {
Actor actor = cell.getActor();
if (actor instanceof ActorGroupMember) {
Long id = groupsMap.get(((ActorGroupMember) actor).getGroupId());
if (id == null) {
id = actorThreads.get(i).getId();
groupsMap.put(((ActorGroupMember) actor).getGroupId(), id);
i++;
if (i == actorThreads.size())
i = 0;
}
if (buffer.remove(cell.getId()))
cellsMap.put(cell.getId(), id);
} else if (actor instanceof ActorDistributedGroupMember) {
Integer threadIndex = groupsDistributedMap.get(((ActorDistributedGroupMember) actor).getGroupId());
Long id = null;
if (threadIndex == null) {
id = actorThreads.get(j).getId();
groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), j);
} else {
threadIndex++;
if (threadIndex == actorThreads.size())
threadIndex = 0;
id = actorThreads.get(threadIndex).getId();
groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), threadIndex);
}
if (buffer.remove(cell.getId()))
cellsMap.put(cell.getId(), id);
j++;
if (j == actorThreads.size())
j = 0;
}
}
i = 0;
for (UUID id : buffer) {
cellsMap.put(id, actorThreads.get(i).getId());
i++;
if (i == actorThreads.size())
i = 0;
}
/*
int i=0;
for (UUID id : system.cells.keySet()) {
cellsMap.put(id, actorThreads.get(i).getId());
i++;
if (i==actorThreads.size())
i = 0;
}
*/
}
use of actor4j.core.actors.ActorGroupMember in project actor4j-core by relvaner.
the class ActorBalancingOnRuntime method registerCell.
public void registerCell(Map<UUID, Long> cellsMap, List<Long> threadsList, Map<Long, ActorThread> threadsMap, Map<UUID, Long> groupsMap, Map<UUID, Integer> groupsDistributedMap, ActorCell cell) {
try {
lock.lock();
Actor actor = cell.getActor();
if (actor instanceof ActorGroupMember) {
Long threadId = groupsMap.get(((ActorGroupMember) actor).getGroupId());
if (threadId == null) {
threadId = pollThreadId(threadsMap);
groupsMap.put(((ActorGroupMember) actor).getGroupId(), threadId);
}
cellsMap.put(cell.getId(), threadId);
} else if (actor instanceof ActorDistributedGroupMember) {
Integer threadIndex = groupsDistributedMap.get(((ActorDistributedGroupMember) actor).getGroupId());
Long id = null;
if (threadIndex == null) {
id = threadsList.get(pollThreadIndex.get());
groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), pollThreadIndex.get());
} else {
threadIndex++;
if (threadIndex == threadsMap.size())
threadIndex = 0;
groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), threadIndex);
id = threadsList.get(threadIndex);
}
cellsMap.put(cell.getId(), id);
pollThreadIndex.updateAndGet((index) -> index == threadsList.size() - 1 ? 0 : index + 1);
} else
cellsMap.put(cell.getId(), pollThreadId(threadsMap));
} finally {
lock.unlock();
}
}
Aggregations