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;
}
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);
}
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;
}
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());
}
}
}
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());
}
Aggregations