// We've already exceeded the maximum number of server redirects
// so consider this as an error condition for the client.
// Invoke the operationFailed callback and just return.
logger.debug("Exceeded the number of server redirects ({}) so error out.",
curNumServerRedirects);
PubSubException exception = new ServiceDownException(
new TooManyServerRedirectsException("Already reached max number of redirects: "
+ curNumServerRedirects));
pubSubData.getCallback().operationFailed(pubSubData.context, exception);
return;
}
// We will redirect and try to connect to the correct server
// stored in the StatusMsg of the response. First store the
// server that we sent the PubSub request to for the topic.
ByteString triedServer = ByteString.copyFromUtf8(HedwigSocketAddress.sockAddrStr(
NetUtils.getHostFromChannel(channel)));
if (pubSubData.triedServers == null) {
pubSubData.triedServers = new LinkedList<ByteString>();
}
pubSubData.shouldClaim = true;
pubSubData.triedServers.add(triedServer);
// Now get the redirected server host (expected format is
// Hostname:Port:SSLPort) from the server's response message. If one is
// not given for some reason, then redirect to the default server
// host/VIP to repost the request.
String statusMsg = response.getStatusMsg();
InetSocketAddress redirectedHost;
boolean redirectToDefaultServer;
if (statusMsg != null && statusMsg.length() > 0) {
if (cfg.isSSLEnabled()) {
redirectedHost = new HedwigSocketAddress(statusMsg).getSSLSocketAddress();
} else {
redirectedHost = new HedwigSocketAddress(statusMsg).getSocketAddress();
}
redirectToDefaultServer = false;
} else {
redirectedHost = cfg.getDefaultServerHost();
redirectToDefaultServer = true;
}
// Make sure the redirected server is not one we've already attempted
// already before in this PubSub request.
if (pubSubData.triedServers.contains(ByteString.copyFromUtf8(HedwigSocketAddress.sockAddrStr(redirectedHost)))) {
logger.error("We've already sent this PubSubRequest before to redirectedHost: {}, pubSubData: {}",
va(redirectedHost, pubSubData));
PubSubException exception = new ServiceDownException(
new ServerRedirectLoopException("Already made the request before to redirected host: "
+ redirectedHost));
pubSubData.getCallback().operationFailed(pubSubData.context, exception);
return;
}