Search in sources :

Example 1 with ActorDistributedGroupMember

use of actor4j.core.actors.ActorDistributedGroupMember 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;
		}
		*/
}
Also used : ActorCell(actor4j.core.ActorCell) ActorGroupMember(actor4j.core.actors.ActorGroupMember) Actor(actor4j.core.actors.Actor) ActorDistributedGroupMember(actor4j.core.actors.ActorDistributedGroupMember) UUID(java.util.UUID) LinkedList(java.util.LinkedList)

Example 2 with ActorDistributedGroupMember

use of actor4j.core.actors.ActorDistributedGroupMember 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();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Actor(actor4j.core.actors.Actor) ActorGroupMember(actor4j.core.actors.ActorGroupMember) ActorDistributedGroupMember(actor4j.core.actors.ActorDistributedGroupMember) UUID(java.util.UUID) ActorCell(actor4j.core.ActorCell) List(java.util.List) Lock(java.util.concurrent.locks.Lock) ActorThread(actor4j.core.ActorThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ActorGroupMember(actor4j.core.actors.ActorGroupMember) Actor(actor4j.core.actors.Actor) ActorDistributedGroupMember(actor4j.core.actors.ActorDistributedGroupMember)

Aggregations

ActorCell (actor4j.core.ActorCell)2 Actor (actor4j.core.actors.Actor)2 ActorDistributedGroupMember (actor4j.core.actors.ActorDistributedGroupMember)2 ActorGroupMember (actor4j.core.actors.ActorGroupMember)2 UUID (java.util.UUID)2 ActorThread (actor4j.core.ActorThread)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1