Search in sources :

Example 71 with ServiceException

use of com.google.protobuf.ServiceException in project hbase by apache.

the class TokenUtil method obtainToken.

/**
   * Obtain and return an authentication token for the current user.
   * @param conn The HBase cluster connection
   * @return the authentication token instance
   */
public static Token<AuthenticationTokenIdentifier> obtainToken(Connection conn) throws IOException {
    Table meta = null;
    try {
        meta = conn.getTable(TableName.META_TABLE_NAME);
        CoprocessorRpcChannel rpcChannel = meta.coprocessorService(HConstants.EMPTY_START_ROW);
        AuthenticationProtos.AuthenticationService.BlockingInterface service = AuthenticationProtos.AuthenticationService.newBlockingStub(rpcChannel);
        AuthenticationProtos.GetAuthenticationTokenResponse response = service.getAuthenticationToken(null, AuthenticationProtos.GetAuthenticationTokenRequest.getDefaultInstance());
        return toToken(response.getToken());
    } catch (ServiceException se) {
        ProtobufUtil.handleRemoteException(se);
    } finally {
        if (meta != null) {
            meta.close();
        }
    }
    // dummy return for ServiceException block
    return null;
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ServiceException(com.google.protobuf.ServiceException) CoprocessorRpcChannel(org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel) AuthenticationProtos(org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos)

Example 72 with ServiceException

use of com.google.protobuf.ServiceException in project hadoop by apache.

the class TestRPCUtil method testRPCRuntimeExceptionUnwrapping.

@Test
public void testRPCRuntimeExceptionUnwrapping() {
    String message = "RPCRuntimeExceptionUnwrapping";
    RuntimeException re = new NullPointerException(message);
    ServiceException se = new ServiceException(re);
    Throwable t = null;
    try {
        RPCUtil.unwrapAndThrowException(se);
    } catch (Throwable thrown) {
        t = thrown;
    }
    Assert.assertTrue(NullPointerException.class.isInstance(t));
    Assert.assertTrue(t.getMessage().contains(message));
}
Also used : ServiceException(com.google.protobuf.ServiceException) Test(org.junit.Test)

Example 73 with ServiceException

use of com.google.protobuf.ServiceException in project hadoop by apache.

the class TestRPCUtil method testRPCServiceExceptionUnwrapping.

@Test
public void testRPCServiceExceptionUnwrapping() {
    String message = "ServiceExceptionMessage";
    ServiceException se = new ServiceException(message);
    Throwable t = null;
    try {
        RPCUtil.unwrapAndThrowException(se);
    } catch (Throwable thrown) {
        t = thrown;
    }
    Assert.assertTrue(IOException.class.isInstance(t));
    Assert.assertTrue(t.getMessage().contains(message));
}
Also used : ServiceException(com.google.protobuf.ServiceException) IOException(java.io.IOException) Test(org.junit.Test)

Example 74 with ServiceException

use of com.google.protobuf.ServiceException in project hadoop by apache.

the class TestRPCUtil method verifyRemoteExceptionUnwrapping.

private void verifyRemoteExceptionUnwrapping(Class<? extends Throwable> expectedLocalException, String realExceptionClassName) {
    String message = realExceptionClassName + "Message";
    RemoteException re = new RemoteException(realExceptionClassName, message);
    ServiceException se = new ServiceException(re);
    Throwable t = null;
    try {
        RPCUtil.unwrapAndThrowException(se);
    } catch (Throwable thrown) {
        t = thrown;
    }
    Assert.assertTrue("Expected exception [" + expectedLocalException + "] but found " + t, expectedLocalException.isInstance(t));
    Assert.assertTrue("Expected message [" + message + "] but found " + t.getMessage(), t.getMessage().contains(message));
}
Also used : ServiceException(com.google.protobuf.ServiceException) RemoteException(org.apache.hadoop.ipc.RemoteException)

Example 75 with ServiceException

use of com.google.protobuf.ServiceException in project MSEC by Tencent.

the class ServiceImpl method getMP3List.

public Msec.GetMP3ListResponse getMP3List(RpcController controller, Msec.GetMP3ListRequest request) throws ServiceException {
    //Add your code here
    AccessMonitor.add("getMP3List_entry");
    String type = request.getType();
    AccessLog.doLog(AccessLog.LOG_LEVEL_DEBUG, "Req:" + request);
    Msec.GetMP3ListResponse.Builder builder = Msec.GetMP3ListResponse.newBuilder();
    if (!(type.equals("special") || type.equals("standard"))) {
        builder.setStatus(100);
        builder.setMsg("invaid type field in request");
        return builder.build();
    }
    String jsonStr = String.format("{\"handleClass\":\"com.bison.GetMP3List\",  \"requestBody\": {\"type\":\"%s\"} }", type);
    String lenStr = String.format("%-10d", jsonStr.getBytes().length);
    AccessLB lb = new AccessLB();
    Route r = new Route();
    Socket socket = new Socket();
    DBUtil util = null;
    try {
        lb.getroutebyname("Database.mysql", r);
        util = new DBUtil(r.getIp() + ":" + r.getPort());
        if (util.getConnection() == null) {
            builder.setStatus(100);
            builder.setMsg("db connect failed!");
            AccessMonitor.add("connect_mysql_fail");
            return builder.build();
        }
        AccessMonitor.add("connect_mysql_succ");
        //get route information
        lb.getroutebyname("Jsoup.jsoup", r);
        if (r.getComm_type() != Route.COMM_TYPE.COMM_TYPE_TCP && r.getComm_type() != Route.COMM_TYPE.COMM_TYPE_ALL) {
            builder.setStatus(100);
            builder.setMsg("tcp is not supported by jsoup");
            return builder.build();
        }
        // connect server
        //20 seconds
        socket.setSoTimeout(20000);
        socket.connect(new InetSocketAddress(r.getIp(), r.getPort()), 2000);
        // send request bytes
        socket.getOutputStream().write(lenStr.getBytes(Charset.forName("utf8")));
        socket.getOutputStream().write(jsonStr.getBytes(Charset.forName("utf8")));
        // recv response bytes
        byte[] buf = new byte[102400];
        int max = 10;
        int total = 0;
        while (total < max) {
            int len = socket.getInputStream().read(buf, total, max - total);
            if (len <= 0) {
                socket.close();
                throw new Exception("recv json length failed");
            }
            total += len;
        }
        max = new Integer(new String(buf, 0, 10, Charset.forName("utf8")).trim()).intValue();
        total = 0;
        while (total < max) {
            int len = socket.getInputStream().read(buf, total, max - total);
            if (len <= 0) {
                socket.close();
                throw new Exception("recv json bytes failed");
            }
            total += len;
        }
        // parse the response json
        JSONObject jsonObject = new JSONObject(new String(buf, 0, total, Charset.forName("utf8")));
        int status = jsonObject.getInt("status");
        if (status != 0) {
            throw new Exception("json string status:" + status);
        }
        JSONArray mp3s = jsonObject.getJSONArray("mp3s");
        for (int i = 0; i < mp3s.length(); i++) {
            JSONObject mp3 = mp3s.getJSONObject(i);
            String title = mp3.getString("title");
            String url = mp3.getString("url");
            Msec.OneMP3.Builder bb = Msec.OneMP3.newBuilder();
            bb.setTitle(title);
            bb.setUrl(url);
            builder.addMp3S(bb.build());
            String sql = "insert into mp3_list(title, url) values(?,?)";
            List<Object> params = new ArrayList<Object>();
            params.add(title);
            params.add(url);
            try {
                int addNum = util.updateByPreparedStatement(sql, params);
                if (addNum < 0) {
                    builder.setMsg("db add record failed.");
                    builder.setStatus(100);
                    return builder.build();
                }
                AccessMonitor.add("access_mysql_succ");
            } catch (SQLException e) {
                builder.setMsg("db add record failed:" + e.toString());
                builder.setStatus(100);
                e.printStackTrace();
                return builder.build();
            }
        }
        AccessMonitor.add("access_jsoup_succ");
        AccessLog.doLog(AccessLog.LOG_LEVEL_INFO, "Resp OK:" + mp3s.length());
        AccessLog.doLog(AccessLog.LOG_LEVEL_ERROR, "Resp OK");
    } catch (Exception e) {
        e.printStackTrace();
        builder.setStatus(100);
        builder.setMsg(e.getMessage());
        AccessMonitor.add("getMP3List_fail");
        return builder.build();
    } finally {
        try {
            socket.close();
        } catch (Exception e) {
        }
        if (util != null && util.getConnection() != null) {
            util.releaseConn();
        }
    }
    builder.setStatus(0);
    builder.setMsg("success");
    return builder.build();
}
Also used : SQLException(java.sql.SQLException) InetSocketAddress(java.net.InetSocketAddress) JSONArray(org.json.JSONArray) ServiceException(com.google.protobuf.ServiceException) SQLException(java.sql.SQLException) AccessLB(api.lb.msec.org.AccessLB) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Route(api.lb.msec.org.Route) Socket(java.net.Socket)

Aggregations

ServiceException (com.google.protobuf.ServiceException)139 IOException (java.io.IOException)66 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)12 Configuration (org.apache.hadoop.conf.Configuration)11 FsPermission (org.apache.hadoop.fs.permission.FsPermission)5 Table (org.apache.hadoop.hbase.client.Table)5 CoprocessorRpcChannel (org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel)5 InetSocketAddress (java.net.InetSocketAddress)4 DatanodeCommand (org.apache.hadoop.hdfs.server.protocol.DatanodeCommand)4 ByteString (com.google.protobuf.ByteString)3 InterruptedIOException (java.io.InterruptedIOException)3 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 Callable (java.util.concurrent.Callable)3 ExecutionException (java.util.concurrent.ExecutionException)3 ExecutorService (java.util.concurrent.ExecutorService)3 EncryptionZone (org.apache.hadoop.hdfs.protocol.EncryptionZone)3 LocatedBlock (org.apache.hadoop.hdfs.protocol.LocatedBlock)3 Server (org.apache.hadoop.ipc.Server)3