Search in sources :

Example 21 with ActorSelection

use of akka.actor.ActorSelection in project transporter by wang4ever.

the class ActorManager method actorCreate.

/**
 * 创建一个actor
 *
 * @param actorName
 *            actor名称
 * @return
 */
public ActorBean actorCreate(String actorName) {
    // 1.0 Check info.
    if (this._system == null)
        throw new AkkaException("Create actor failed('" + actorName + "'), actor system is unnitialized.");
    if (StringUtils.equalsIgnoreCase(actorName, ActorPath.DEFAULT_ACTOR_NAME) && this.defaultActorBean != null)
        throw new AkkaException("Create actor failed, because the name '" + actorName + "' is the guardian.");
    // 1.1 Create accpetActor.
    try {
        this._system.actorOf(Props.create(AccpetActor.class, actorName), actorName);
    } catch (InvalidActorNameException e) {
        if (logger.isInfoEnabled())
            logger.info("Create an existing actor '{}'", actorName);
    } catch (Throwable t) {
        logger.error("Creating actor '" + actorName + "' failed.", t);
        throw new AkkaException(t.getMessage(), t);
    }
    // 1.2 Selection actor info.
    String path = new ActorPath(conf.getActorSystemName(), conf.getHostname(), conf.getRemote().getPort(), actorName).asString();
    ActorSelection actorSel = this._system.actorSelection(path);
    ActorBean ab = new ActorBean(actorSel.anchor(), path);
    // Default actorBean.
    if (StringUtils.equalsIgnoreCase(actorName, ActorPath.DEFAULT_ACTOR_NAME))
        this.defaultActorBean = ab;
    // 1.3 Save actor info.
    this.repository.putActorBean(actorName, ab);
    return ab;
}
Also used : InvalidActorNameException(akka.actor.InvalidActorNameException) ActorSelection(akka.actor.ActorSelection) ActorPath(io.transport.cluster.config.ActorPath) AccpetActor(io.transport.cluster.actor.AccpetActor) AkkaException(akka.AkkaException)

Example 22 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class TestActorFactory method verifyActorReady.

@SuppressWarnings("checkstyle:IllegalCatch")
private void verifyActorReady(ActorRef actorRef) {
    // Sometimes we see messages go to dead letters soon after creation - it seems the actor isn't quite
    // in a state yet to receive messages or isn't actually created yet. This seems to happen with
    // actorSelection so, to alleviate it, we use an actorSelection and send an Identify message with
    // retries to ensure it's ready.
    Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS);
    Throwable lastError = null;
    Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
        try {
            ActorSelection actorSelection = system.actorSelection(actorRef.path().toString());
            Future<Object> future = Patterns.ask(actorSelection, new Identify(""), timeout);
            ActorIdentity reply = (ActorIdentity) Await.result(future, timeout.duration());
            Assert.assertNotNull("Identify returned null", reply.getRef());
            return;
        } catch (Exception | AssertionError e) {
            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
            lastError = e;
        }
    }
    throw new RuntimeException(lastError);
}
Also used : ActorSelection(akka.actor.ActorSelection) Timeout(akka.util.Timeout) Stopwatch(com.google.common.base.Stopwatch) Identify(akka.actor.Identify) InvalidActorNameException(akka.actor.InvalidActorNameException) ActorIdentity(akka.actor.ActorIdentity)

Example 23 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class Follower method isLeaderAvailabilityKnown.

private boolean isLeaderAvailabilityKnown() {
    if (leaderId == null) {
        return false;
    }
    Optional<Cluster> cluster = context.getCluster();
    if (!cluster.isPresent()) {
        return false;
    }
    ActorSelection leaderActor = context.getPeerActorSelection(leaderId);
    if (leaderActor == null) {
        return false;
    }
    Address leaderAddress = leaderActor.anchorPath().address();
    CurrentClusterState state = cluster.get().state();
    Set<Member> unreachable = state.getUnreachable();
    log.debug("{}: Checking for leader {} in the cluster unreachable set {}", logName(), leaderAddress, unreachable);
    for (Member m : unreachable) {
        if (leaderAddress.equals(m.address())) {
            log.info("{}: Leader {} is unreachable", logName(), leaderAddress);
            return false;
        }
    }
    for (Member m : state.getMembers()) {
        if (leaderAddress.equals(m.address())) {
            if (m.status() == MemberStatus.up() || m.status() == MemberStatus.weaklyUp()) {
                log.debug("{}: Leader {} cluster status is {} - leader is available", logName(), leaderAddress, m.status());
                return true;
            } else {
                log.debug("{}: Leader {} cluster status is {} - leader is unavailable", logName(), leaderAddress, m.status());
                return false;
            }
        }
    }
    log.debug("{}: Leader {} not found in the cluster member set", logName(), leaderAddress);
    return false;
}
Also used : CurrentClusterState(akka.cluster.ClusterEvent.CurrentClusterState) ActorSelection(akka.actor.ActorSelection) Address(akka.actor.Address) Cluster(akka.cluster.Cluster) Member(akka.cluster.Member)

Example 24 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class Shard method handleCanCommitTransaction.

private void handleCanCommitTransaction(final CanCommitTransaction canCommit) {
    LOG.debug("{}: Can committing transaction {}", persistenceId(), canCommit.getTransactionId());
    if (isLeader()) {
        commitCoordinator.handleCanCommit(canCommit.getTransactionId(), getSender(), this);
    } else {
        ActorSelection leader = getLeader();
        if (leader == null) {
            messageRetrySupport.addMessageToRetry(canCommit, getSender(), "Could not canCommit transaction " + canCommit.getTransactionId());
        } else {
            LOG.debug("{}: Forwarding CanCommitTransaction to leader {}", persistenceId(), leader);
            leader.forward(canCommit, getContext());
        }
    }
}
Also used : ActorSelection(akka.actor.ActorSelection)

Example 25 with ActorSelection

use of akka.actor.ActorSelection in project controller by opendaylight.

the class Shard method onMakeLeaderLocal.

private void onMakeLeaderLocal() {
    LOG.debug("{}: onMakeLeaderLocal received", persistenceId());
    if (isLeader()) {
        getSender().tell(new Status.Success(null), getSelf());
        return;
    }
    final ActorSelection leader = getLeader();
    if (leader == null) {
        // Leader is not present. The cluster is most likely trying to
        // elect a leader and we should let that run its normal course
        // TODO we can wait for the election to complete and retry the
        // request. We can also let the caller retry by sending a flag
        // in the response indicating the request is "reTryable".
        getSender().tell(new Failure(new LeadershipTransferFailedException("We cannot initiate leadership transfer to local node. " + "Currently there is no leader for " + persistenceId())), getSelf());
        return;
    }
    leader.tell(new RequestLeadership(getId(), getSender()), getSelf());
}
Also used : FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) Status(akka.actor.Status) LeadershipTransferFailedException(org.opendaylight.controller.cluster.raft.LeadershipTransferFailedException) ActorSelection(akka.actor.ActorSelection) RequestLeadership(org.opendaylight.controller.cluster.raft.messages.RequestLeadership) Failure(akka.actor.Status.Failure)

Aggregations

ActorSelection (akka.actor.ActorSelection)43 ActorRef (akka.actor.ActorRef)9 Test (org.junit.Test)6 PrimaryShardInfo (org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo)5 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)4 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 InvalidActorNameException (akka.actor.InvalidActorNameException)3 Status (akka.actor.Status)3 TestActorRef (akka.testkit.TestActorRef)3 TestKit (akka.testkit.javadsl.TestKit)3 Timeout (akka.util.Timeout)3 ArrayList (java.util.ArrayList)3 ClientActorContext (org.opendaylight.controller.cluster.access.client.ClientActorContext)3 AkkaException (akka.AkkaException)2 ActorIdentity (akka.actor.ActorIdentity)2 Identify (akka.actor.Identify)2 Failure (akka.actor.Status.Failure)2 Success (akka.actor.Status.Success)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)2