夯住(Hang)是指程序仍在运行,卡在某个方法调用上,没有返回也没有异常抛出;卡住时间从几秒到几小时不等。 Java程序发生Hang时,应该首先使用 jstack 把java进程的堆栈信息保存下来 ,供后继分析使用。
调用OSS Java SDK的程序Hang,有以下可能的情况及原因。
说明:
-
jstack -l <pid> > js.txt
可以把pid的堆栈信息保存到文件js.txt中。pid可以通过jps
命令查找到。 - 抓包工具,Windows系统下推荐使用
Wireshark
。捕获筛选器可以设置成host <bucket-name>.<endpoint>
,例如:
host my-bucket.oss-cn-hangzhou.aliyuncs.com`
Linux使用tcpdump
命令抓包,命令格式如下:
sudo tcpdump tcp -i <eth0> -t -s 0 -c 200000 and host <bucket>.<endpoint> -w <file.cap>
例如:
sudo tcpdump tcp -i eth0 -t -s 0 -c 200000 and host my-bucket.oss-cn-hangzhou.aliyuncs.com -w out.cap`
然后用Wireshark打开file.cap分析抓包数据。
- getObject获取的InputStream需要close,示例如下:
OSSObject ossObject = ossClient.getObject(bucketName, key);
ossObject.getObjectContent().close();
- OSS Java SDK 2.2.3前的版本getSimplifiedObjectMeta调用有bug,连接没有关闭,会造成泄漏。2.2.3版本修复。
- hang在
PoolingHttpClientConnectionManager.leaseConnection
的堆栈如下:
"main" prio=6 tid=0x000000000291e000 nid=0xc40 waiting on condition [0x0000000002dae000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007d85697f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
at org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:138)
at org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:306)
at org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64)
at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:192)
at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:185)
at org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:107)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:276)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:113)
at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:123)
at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:68)
at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:94)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:146)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:113)
at com.aliyun.oss.internal.OSSObjectOperation.getObject(OSSObjectOperation.java:229)
at com.aliyun.oss.OSSClient.getObject(OSSClient.java:629)
at com.aliyun.oss.OSSClient.getObject(OSSClient.java:617)
at samples.HelloOSS.main(HelloOSS.java:49)
- hang在
java.net.SocketOutputStream.socketWrite
的堆栈如下:
"main" prio=10 tid=0x0000000040112800 nid=0x2ad8 runnable [0x00000000418e1000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at org.apache.http.impl.conn.LoggingOutputStream.write(LoggingOutputStream.java:77)
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:126)
at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:138)
at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:169)
at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:115)
at com.aliyun.oss.common.comm.RepeatableInputStreamEntity$NoAutoClosedInputStreamEntity.writeTo(RepeatableInputStreamEntity.java:127)
at com.aliyun.oss.common.comm.RepeatableInputStreamEntity.writeTo(RepeatableInputStreamEntity.java:71)
at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:158)
at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:162)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at com.aliyun.oss.common.comm.DefaultServiceClient.sendRequestCore(DefaultServiceClient.java:113)
at com.aliyun.oss.common.comm.ServiceClient.sendRequestImpl(ServiceClient.java:121)
at com.aliyun.oss.common.comm.ServiceClient.sendRequest(ServiceClient.java:67)
at com.aliyun.oss.internal.OSSOperation.send(OSSOperation.java:92)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:140)
at com.aliyun.oss.internal.OSSOperation.doOperation(OSSOperation.java:111)
at com.aliyun.oss.internal.OSSObjectOperation.writeObjectInternal(OSSObjectOperation.java:665)
at com.aliyun.oss.internal.OSSObjectOperation.putObject(OSSObjectOperation.java:132)
at com.aliyun.oss.OSSClient.putObject(OSSClient.java:528)
at com.aliyun.oss.OSSClient.putObject(OSSClient.java:516)
at com.aliyun.oss.OSSClient.putObject(OSSClient.java:522)
at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:25)