1.25稳定性bug修改同步到1.26

This commit is contained in:
jif2.zhang 2025-08-11 11:26:01 +08:00
parent d40594bce1
commit 08ddb35bc3
8 changed files with 1225 additions and 100 deletions

View File

@ -1,5 +1,6 @@
package com.sunyard.sge.database;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sunyard.SydApiException;
@ -29,7 +30,9 @@ public class SydApiBaseFunction implements SydApi {
private boolean isShortLinkMode = false;
// c 加速句柄短链接
private PointerByReference phHandle = null;
private Pointer[] phHandle = new Pointer[1];
private Pointer pointer = null;
private ILogFactory iLogFactory = new LogbackFactory();
private ILogger log = iLogFactory.getLogger(SydApiBaseFunction.class);
@ -64,7 +67,7 @@ public class SydApiBaseFunction implements SydApi {
@Override
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
// long s = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, allDealTimeOut);
}
@ -72,8 +75,9 @@ public class SydApiBaseFunction implements SydApi {
if (pcIpList.length != iPortList.length) {
throw new IllegalArgumentException("ip与端口数量不一致");
}
SydApi4j[] apis = new SydApi4j[pcIpList.length];
int iDealTimeOut = allDealTimeOut / pcIpList.length;
SydApi4j[] apis = new SydApi4j[pcIpList.length];
boolean connected = false;
for (int i = 0; i < pcIpList.length; i++) {
@ -97,7 +101,7 @@ public class SydApiBaseFunction implements SydApi {
// c 加速链接
if ( Env.isLinux() ) {
PointerByReference phHandle = new PointerByReference();
Pointer[] phHandle = new Pointer[1];
int connectResult = SydLibrary.INSTANCE.SYD_Connect_Ex(
pcIpList,
iPortList,
@ -108,17 +112,21 @@ public class SydApiBaseFunction implements SydApi {
phHandle);
if ( 0 != connectResult ){
// 释放已建立的链接
this.SYD_Disconnect();
throw new SydApiException("连接错误", -1);
}
this.phHandle = phHandle;
this.pointer = phHandle[0];
}
return this;
}
public void SYD_Disconnect() {
if ( null != phHandle && Env.isLinux()) {
SydLibrary.INSTANCE.SYD_DisConnect_Ex(phHandle.getValue());
phHandle = null;
SydLibrary.INSTANCE.SYD_DisConnect_Ex(this.pointer);
this.phHandle = null;
this.pointer = null;
}
if (null == hsms) {
@ -941,7 +949,7 @@ public class SydApiBaseFunction implements SydApi {
IntByReference encDataLen = new IntByReference(encData.length);
int i = SydLibrary.INSTANCE.SYD_SM4_Encrypt_Mac_ShortData(
phHandle.getValue(),
this.pointer,
iEncKeyIndex,
iMacKeyIndex,
pcData,
@ -987,7 +995,7 @@ public class SydApiBaseFunction implements SydApi {
log.debug("SYD_SM4_EncryptAndMac_ShortDatak1={} k2={}", iEncKeyIndex, iMacKeyIndex);
}
if ( null != phHandle ) {
if ( null != pointer ) {
return SYD_SM4_EncryptAndMac_ShortData_c(iEncKeyIndex, iMacKeyIndex, pcData);
@ -1009,7 +1017,7 @@ public class SydApiBaseFunction implements SydApi {
int i = SydLibrary.INSTANCE.SYD_SM4_Decrypt_Mac_ShortData(
phHandle.getValue(),
pointer,
iEncKeyIndex,
iMacKeyIndex,
pcData,
@ -1048,7 +1056,7 @@ public class SydApiBaseFunction implements SydApi {
log.debug("SYD_SM4_CheckMacAndDecrypt_ShortDatak1={} k2={}", iEncKeyIndex, iMacKeyIndex);
}
if( null != phHandle ) {
if( null != pointer ) {
return SYD_SM4_CheckMacAndDecrypt_ShortData_c(iEncKeyIndex, iMacKeyIndex, pcData, mac);
} else {
boolean ret = SYD_SM4Mac_ShortData(iMacKeyIndex, pcData, 1040, mac);
@ -1217,4 +1225,9 @@ public class SydApiBaseFunction implements SydApi {
}
return ia;
}
@Override
protected void finalize() throws Throwable {
this.SYD_Disconnect_Ex();
}
}

View File

@ -51,7 +51,7 @@ public class SydApiBuilder {
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
this.pool = new SydApiPool(config, linkInfo);
longLinkWorks = new CommWorker();
warmUp( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
// warmUp( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
}
public CommWorker getLongLinkWorks() {

View File

@ -166,11 +166,12 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public DataAndMac SYD_SM4_EncryptAndMac_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
if (Env.isLinux()) {
return SYD_SM4_EncryptAndMac_ShortData_c(iEncKeyIndex, iMacKeyIndex, pcData);
} else {
return SYD_SM4_EncryptAndMac_ShortData_java(iEncKeyIndex, iMacKeyIndex, pcData);
}
// if (Env.isLinux()) {
// return SYD_SM4_EncryptAndMac_ShortData_c(iEncKeyIndex, iMacKeyIndex, pcData);
// } else {
// return SYD_SM4_EncryptAndMac_ShortData_java(iEncKeyIndex, iMacKeyIndex, pcData);
// }
return SYD_SM4_EncryptAndMac_ShortData_java(iEncKeyIndex, iMacKeyIndex, pcData);
}
DataAndMac SYD_SM4_EncryptAndMac_ShortData_c(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
@ -221,11 +222,12 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] macs) {
if (Env.isLinux()) {
return SYD_SM4_CheckMacAndDecrypt_ShortData_c(iEncKeyIndex, iMacKeyIndex, pcData, macs);
} else {
return SYD_SM4_CheckMacAndDecrypt_ShortData_java(iEncKeyIndex, iMacKeyIndex, pcData, macs);
}
// if (Env.isLinux()) {
// return SYD_SM4_CheckMacAndDecrypt_ShortData_c(iEncKeyIndex, iMacKeyIndex, pcData, macs);
// } else {
// return SYD_SM4_CheckMacAndDecrypt_ShortData_java(iEncKeyIndex, iMacKeyIndex, pcData, macs);
// }
return SYD_SM4_CheckMacAndDecrypt_ShortData_java(iEncKeyIndex, iMacKeyIndex, pcData, macs);
}
@ -290,11 +292,12 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public List<DataAndMac> SYD_SM4_EncryptAndMac_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<byte[]> pcData) {
if (Env.isLinux()) {
return SYD_SM4_EncryptAndMac_BatchData_c(iEncKeyIndex, iMacKeyIndex, pcData);
} else {
return SYD_SM4_EncryptAndMac_BatchData_java(iEncKeyIndex, iMacKeyIndex, pcData);
}
// if (Env.isLinux()) {
// return SYD_SM4_EncryptAndMac_BatchData_c(iEncKeyIndex, iMacKeyIndex, pcData);
// } else {
// return SYD_SM4_EncryptAndMac_BatchData_java(iEncKeyIndex, iMacKeyIndex, pcData);
// }
return SYD_SM4_EncryptAndMac_BatchData_java(iEncKeyIndex, iMacKeyIndex, pcData);
}
@ -405,11 +408,12 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public List<DataAndMacCheck> SYD_SM4_CheckMacAndDecrypt_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<DataAndMac> pcData) {
if (Env.isLinux()) {
return SYD_SM4_CheckMacAndDecrypt_BatchData_c(iEncKeyIndex, iMacKeyIndex, pcData);
} else {
return SYD_SM4_CheckMacAndDecrypt_BatchData_java(iEncKeyIndex, iMacKeyIndex, pcData);
}
// if (Env.isLinux()) {
// return SYD_SM4_CheckMacAndDecrypt_BatchData_c(iEncKeyIndex, iMacKeyIndex, pcData);
// } else {
// return SYD_SM4_CheckMacAndDecrypt_BatchData_java(iEncKeyIndex, iMacKeyIndex, pcData);
// }
return SYD_SM4_CheckMacAndDecrypt_BatchData_java(iEncKeyIndex, iMacKeyIndex, pcData);
}

View File

@ -8,7 +8,7 @@ import com.sun.jna.ptr.PointerByReference;
*/
public class ConnectHandlerManager {
private static PointerByReference phHandle = new PointerByReference();
private static Pointer[] phHandle = new Pointer[1];
private static Boolean inited = false;
/**
@ -45,6 +45,6 @@ public class ConnectHandlerManager {
}
public static Pointer getHandler(){
return phHandle.getValue();
return phHandle[0];
}
}

View File

@ -21,7 +21,7 @@ public interface SydLibrary extends Library {
int iConnectTimeOut,
int iDealTimeOut,
int iThreadCount,
PointerByReference phHandle);
Pointer[] phHandle);
// 1.3 断开与设备的连接
int SYD_DisConnect_Ex(Pointer phHandle);

View File

@ -36,10 +36,12 @@ public class FuncTest {
public SydApi connect() {
if (isShortLink) {
System.out.println("short");
SydApi api = new SydApi4Database();
api.SYD_Short_Connect_Ex(config.getIps(), config.getPorts(), connectTimeout, dealTimeout);
return api;
} else {
System.out.println("long");
SydApiBuilder builder = new SydApiBuilder(
config.getIps(),
config.getPorts(),
@ -2185,6 +2187,7 @@ public class FuncTest {
}
} catch (SydApiException e){
printException("输入正确入参,校验失败", e);
e.printStackTrace();
}
printEnd();

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,210 @@
package test;
import com.sun.jna.Pointer;
import com.sunyard.sge.database.DataAndMac;
import com.sunyard.sge.database.SydApi;
import com.sunyard.sge.database.SydApi4Database;
import com.sunyard.sge.plus.SydLibrary;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TestOpenClose {
private static Config config ;
public static void sm4ShortMacEnde1(int numberOfThreads, int totalRequest, int plainLen) throws InterruptedException {
byte[] data = new byte[plainLen];
int keyEnc = 0;
int keyMac = 0;
Set<SydApi> syncSet = Collections.synchronizedSet(new HashSet<>());
ThreadLocal<SydApi> Api = new ThreadLocal<SydApi>() {
@Override
protected SydApi initialValue() {
SydApi api = new SydApi4Database();
try {
api.SYD_Short_Connect_Ex(config.getIps(), config.getPorts(), 2000, 2000);
System.out.println("create link " + Thread.currentThread().getName());
syncSet.add( api );
System.out.println("建立链接 " + syncSet.size());
}catch (Exception e) {
System.out.println("error link " + Thread.currentThread().getName());
// e.printStackTrace();
throw e;
}
return api;
}
};
try {
System.out.println("准备密文数据");
SydApi apiTest = Api.get();
final DataAndMac ret = apiTest.SYD_SM4_EncryptAndMac_ShortData(keyEnc, keyMac, data);
apiTest.SYD_Disconnect_Ex();
System.out.println("密文数据准备完成");
ExecutorService poolEnc = Executors.newFixedThreadPool(numberOfThreads);
long start = System.currentTimeMillis();
for (int i = 0; i < totalRequest; i++) {
poolEnc.submit(() -> {
SydApi api = Api.get();
if ( null == api ) {
System.out.println("api is null");
}
api.SYD_SM4_EncryptAndMac_ShortData(keyEnc, keyMac, data);
// System.out.println("encrypt " + Thread.currentThread().getName());
// System.out.println("Thread=" + Thread.currentThread().getName());
// SydApi4Database adb = (SydApi4Database) api;
// System.out.println("handler=" + adb.getHandler());
});
}
poolEnc.shutdown();
poolEnc.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
System.out.println("释放链接 " + syncSet.size());
syncSet.forEach(SydApi::SYD_Disconnect_Ex);
syncSet.clear();
// System.out.println("sleep");
// Thread.sleep(10 * 1000);
long end = System.currentTimeMillis();
long duration = end - start;
double tps = totalRequest * 1000.0 / duration;
double throughput = totalRequest * 1000.0 / duration * plainLen / 1024 / 1024;
System.out.println("并发数: " + numberOfThreads);
System.out.println("加密共花费 " + String.format("%.2f", duration / 1000.0) + "");
System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
double avgLatency = numberOfThreads * duration * 1.0 / totalRequest;
ExecutorService poolDec = Executors.newFixedThreadPool(numberOfThreads);
start = System.currentTimeMillis();
for (int i = 0; i < totalRequest; i++) {
poolDec.submit(() -> {
SydApi api = Api.get();
api.SYD_SM4_CheckMacAndDecrypt_ShortData(keyEnc, keyMac, ret.getData(), ret.getMac());
// System.out.println("decrypt " + Thread.currentThread().getName());
});
}
poolDec.shutdown();
poolDec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
System.out.println("释放链接 " + syncSet.size());
syncSet.forEach(SydApi::SYD_Disconnect_Ex);
syncSet.clear();
// System.out.println("sleep");
// Thread.sleep(10 * 1000);
end = System.currentTimeMillis();
duration = end - start;
tps = totalRequest * 1000.0 / duration;
throughput = totalRequest * 1000.0 / duration * plainLen / 1024 / 1024;
System.out.println("并发数: " + numberOfThreads);
System.out.println("解密共花费 " + String.format("%.2f", duration / 1000.0) + "");
System.out.println("TPS: " + String.format("%.2f", tps));
System.out.println("吞吐量: " + String.format("%.2f", throughput) + " MB/S");
avgLatency = numberOfThreads * duration * 1.0 / totalRequest;
} catch (Exception e) {
System.out.println("密文数据准备异常");
e.printStackTrace();
throw e;
}
}
public static void case1(){
System.out.println("case1 start");
// PointerByReference phHandle = new PointerByReference();
Pointer[] phHandle = new Pointer[1];
int connectResult = SydLibrary.INSTANCE.SYD_Connect_Ex(
config.getIps(),
config.getPorts(),
config.getIps().length,
1000,
2000,
0,
phHandle);
if ( 0 != connectResult ){
System.out.println("link error" + connectResult);
} else {
System.out.println("SYD_Disconnect this.phHandle=" + Pointer.nativeValue(phHandle[0]));
System.out.println("link");
}
System.out.println("SYD_Disconnect this.phHandle=" + Pointer.nativeValue(phHandle[0]));
int disResult = SydLibrary.INSTANCE.SYD_DisConnect_Ex(phHandle[0]);
if ( 0 != disResult ){
System.out.println("disResult error" + disResult);
} else {
System.out.println("disResult");
}
System.out.println("case1 end");
}
public static void case2(){
System.out.println("case2 start");
SydApi4Database api = new SydApi4Database();
api.SYD_Short_Connect_Ex(config.getIps(), config.getPorts(), 2000, 2000);
api.SYD_Disconnect_Ex();
System.out.println("case2 end");
}
public static void case3(String[] args) throws InterruptedException {
int threads = 40;
try {
threads = Integer.valueOf(args[0]);
}catch ( Exception e ) {
}
System.out.println("threads=" + threads);
System.out.println("case3 start");
while (true) {
sm4ShortMacEnde1(threads, 100000, 1024);
}
// System.out.println("case3 end");
}
public static void main(String[] args) throws InterruptedException {
// 载入配置文件
config = Config.loadConfig();
System.out.println("config=" + Arrays.toString(config.getIps()));
// case1();
// case2();
case3(args);
}
}