use of org.apache.thrift.transport.TTransportException in project jstorm by alibaba.
the class ReturnResults method execute.
@Override
public void execute(Tuple input) {
String result = (String) input.getValue(0);
String returnInfo = (String) input.getValue(1);
// LOG.info("Receive one message, resultInfo:{}, result:{}", returnInfo, result);
if (returnInfo != null) {
Map retMap = (Map) JSONValue.parse(returnInfo);
final String host = (String) retMap.get("host");
final int port = Utils.getInt(retMap.get("port"));
String hostPort = host + ":" + port;
String id = (String) retMap.get("id");
DistributedRPCInvocations.Iface client;
if (local) {
client = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(host);
} else {
List server = new ArrayList() {
{
add(host);
add(port);
}
};
if (!_clients.containsKey(server)) {
try {
_clients.put(server, new DRPCInvocationsClient(_conf, host, port));
} catch (TTransportException ex) {
throw new RuntimeException(ex);
}
}
client = _clients.get(server);
}
try {
client.result(id, result);
_collector.ack(input);
} catch (AuthorizationException aze) {
LOG.error("Not authorized to return results to DRPC server " + hostPort, aze);
_collector.fail(input);
if (client instanceof DRPCInvocationsClient) {
try {
LOG.info("reconnecting... ");
// Blocking call
((DRPCInvocationsClient) client).reconnectClient();
} catch (TException e2) {
throw new RuntimeException(e2);
}
}
} catch (TException e) {
LOG.error("Failed to return results to DRPC server " + hostPort, e);
_collector.fail(input);
if (client instanceof DRPCInvocationsClient) {
try {
LOG.info("reconnecting... ");
// Blocking call
((DRPCInvocationsClient) client).reconnectClient();
} catch (TException e2) {
throw new RuntimeException(e2);
}
}
}
}
}
use of org.apache.thrift.transport.TTransportException in project jstorm by alibaba.
the class KerberosSaslTransportPlugin method connect.
@Override
public TTransport connect(TTransport transport, String serverHost, String asUser) throws TTransportException, IOException {
// create an authentication callback handler
ClientCallbackHandler client_callback_handler = new ClientCallbackHandler(login_conf);
// login our user
Login login = null;
try {
// specify a configuration object to be used
Configuration.setConfiguration(login_conf);
// now login
login = new Login(AuthUtils.LOGIN_CONTEXT_CLIENT, client_callback_handler);
} catch (LoginException ex) {
LOG.error("Server failed to login in principal:" + ex, ex);
throw new RuntimeException(ex);
}
final Subject subject = login.getSubject();
if (subject.getPrivateCredentials(KerberosTicket.class).isEmpty()) {
// error
throw new RuntimeException("Fail to verify user principal with section \"" + AuthUtils.LOGIN_CONTEXT_CLIENT + "\" in login configuration file " + login_conf);
}
final String principal = StringUtils.isBlank(asUser) ? getPrincipal(subject) : asUser;
String serviceName = AuthUtils.get(login_conf, AuthUtils.LOGIN_CONTEXT_CLIENT, "serviceName");
if (serviceName == null) {
serviceName = AuthUtils.SERVICE;
}
Map<String, String> props = new TreeMap<String, String>();
props.put(Sasl.QOP, "auth");
props.put(Sasl.SERVER_AUTH, "false");
LOG.debug("SASL GSSAPI client transport is being established");
final TTransport sasalTransport = new TSaslClientTransport(KERBEROS, principal, serviceName, serverHost, props, null, transport);
// open Sasl transport with the login credential
try {
Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
public Void run() {
try {
LOG.debug("do as:" + principal);
sasalTransport.open();
} catch (Exception e) {
LOG.error("Client failed to open SaslClientTransport to interact with a server during session initiation: " + e, e);
}
return null;
}
});
} catch (PrivilegedActionException e) {
throw new RuntimeException(e);
}
return sasalTransport;
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testAsyncRetrySelection.
@Test
public void testAsyncRetrySelection() throws Exception {
// verify subclasses pass the retry filter
class HopelesslyLost extends NotFoundException {
}
Capture<AsyncMethodCallback<Integer>> callbackCapture1 = new Capture<AsyncMethodCallback<Integer>>();
expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture1));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
Capture<AsyncMethodCallback<Integer>> callbackCapture2 = new Capture<AsyncMethodCallback<Integer>>();
expectAsyncServiceRetry(true).calculateMass(eq("jake"), capture(callbackCapture2));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
// Verifies that our callback was called.
TTransportException nonRetryableException = new TTransportException();
callback.onError(nonRetryableException);
Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
control.replay();
TestServiceAsync testService = thrift.builder().withRetries(2).retryOn(NotFoundException.class).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create();
testService.calculateMass("jake", callback);
callbackCapture1.getValue().onError(new HopelesslyLost());
callbackCapture2.getValue().onError(nonRetryableException);
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 1);
assertReconnectsTotal(thrift, 1);
assertTimeoutsTotal(thrift, 0);
control.verify();
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testDoCallThriftException.
@Test
public void testDoCallThriftException() throws Exception {
Capture<TTransport> transportCapture = new Capture<TTransport>();
TestService testService = expectThriftError(transportCapture);
TTransportException tException = new TTransportException();
expect(testService.calculateMass("jake")).andThrow(tException);
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
Thrift<TestService> thrift = createThrift(expectUnusedExecutorService());
control.replay();
try {
thrift.builder().blocking().create().calculateMass("jake");
fail("Expected thrift exception to bubble unmodified");
} catch (TException e) {
assertSame(tException, e);
}
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 1);
assertReconnectsTotal(thrift, 1);
assertTimeoutsTotal(thrift, 0);
assertTrue(transportCapture.hasCaptured());
assertFalse("Expected the transport to be forcibly closed when a thrift error is encountered", transportCapture.getValue().isOpen());
control.verify();
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testAsyncRetriesRecover.
@Test
public void testAsyncRetriesRecover() throws Exception {
// Capture the callback that Thift has wrapped around our callback.
Capture<AsyncMethodCallback<Integer>> callbackCapture = new Capture<AsyncMethodCallback<Integer>>();
// 1st call
expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture));
expectLastCall().andThrow(new TTransportException());
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
// 1st retry recovers
expectAsyncServiceRetry(false).calculateMass(eq("jake"), capture(callbackCapture));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.SUCCESS), anyLong());
// Verifies that our callback was called.
callback.onComplete(42);
Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
control.replay();
thrift.builder().withRetries(1).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake", callback);
// Mimicks the async response from the server.
callbackCapture.getValue().onComplete(42);
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 0);
assertReconnectsTotal(thrift, 0);
assertTimeoutsTotal(thrift, 0);
control.verify();
}
Aggregations