mirror of
https://gitee.com/BDWare/agent-backend
synced 2025-04-27 14:42:16 +00:00
Compare commits
49 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7da128f73f | ||
|
f8465b121a | ||
|
f61e2c970c | ||
|
e9f7a9140b | ||
|
540a4232d0 | ||
|
22532666dd | ||
|
73feba9b0e | ||
|
1bf3ccc059 | ||
|
07e5070316 | ||
|
e925d0e9e7 | ||
|
94f1b6dc7d | ||
|
3f4d154d2b | ||
|
0d4f0243a2 | ||
|
af298b6ae7 | ||
|
e76865fb09 | ||
|
8fa064ee8a | ||
|
aef5f5fe38 | ||
|
964d9a9fbc | ||
|
6d90352b6f | ||
|
b229ed8c67 | ||
|
7c91416350 | ||
|
e93c07ed84 | ||
|
926078e081 | ||
|
b5a9b2d085 | ||
|
7791c78c58 | ||
|
90cdabfa92 | ||
|
87d32ab220 | ||
|
ed507b14b4 | ||
|
78366fcb55 | ||
|
ea71e8a358 | ||
|
2758eb42e4 | ||
|
ccdc33424e | ||
|
18521aaf82 | ||
|
928e89988e | ||
|
de30170d65 | ||
|
a553a13a31 | ||
|
3000ba5874 | ||
|
da1f524a06 | ||
|
5a4fdb8a13 | ||
|
0a24986907 | ||
|
aacd939408 | ||
|
b1bf67c802 | ||
|
8618e61d98 | ||
|
f3f146b30a | ||
|
e2df294d65 | ||
|
fc7512f50f | ||
|
ce77ebe0b9 | ||
|
bc8f539c9c | ||
|
9f30c938da |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/ypkcache
|
||||
/BDWareProjectDir
|
||||
/defaultLog/
|
||||
/ContractDB
|
||||
|
33
build.gradle
33
build.gradle
@ -4,6 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
mainClassName = 'org.bdware.server.CMHttpServer'
|
||||
apply from: '../spotless.gradle'
|
||||
|
||||
|
||||
application {
|
||||
@ -34,18 +35,31 @@ dependencies {
|
||||
implementation project(":cm")
|
||||
implementation project(":mockjava")
|
||||
implementation project(":front-base")
|
||||
implementation project(":consistency-sdk")
|
||||
implementation 'io.prometheus:simpleclient_httpserver:0.12.0'
|
||||
implementation 'org.knowhowlab.osgi:sigar:1.6.5_01'
|
||||
implementation 'org.hyperic.sigar:sigar:1.6.4'
|
||||
implementation 'io.grpc:grpc-all:1.43.1'
|
||||
implementation 'org.apache.velocity:velocity-engine-core:2.3'
|
||||
implementation 'com.nimbusds:nimbus-jose-jwt:9.10'
|
||||
implementation 'org.bdware.doip:doip-sdk:1.5.6'
|
||||
implementation 'org.bdware.doip:doip-audit-tool:1.5.4'
|
||||
implementation 'org.bdware.doip:bdosclient:0.1.0'
|
||||
implementation fileTree(dir: 'lib', include: '*.jar')
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
implementation 'io.netty:netty-tcnative-boringssl-static:2.0.59.Final'
|
||||
implementation 'io.netty:netty-tcnative-boringssl-static:2.0.59.Final-linux-aarch_64-fedora'
|
||||
// implementation 'io.netty:netty-tcnative-boringssl-static:2.0.59.Final'
|
||||
// implementation 'io.netty:netty-tcnative-boringssl-static:2.0.59.Final'
|
||||
// implementation 'io.netty:netty-tcnative-boringssl-static:2.0.59.Final'
|
||||
// implementation 'io.netty:netty-tcnative-boringssl-static:2.0.59.Final'
|
||||
}
|
||||
|
||||
compileJava {
|
||||
options.compilerArgs << "-Xlint:unchecked"
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
jar {
|
||||
String libs = ''
|
||||
configurations.runtimeClasspath.each {
|
||||
@ -77,6 +91,9 @@ task copyScript(type: Copy) {
|
||||
include 'cmstop.sh'
|
||||
include 'cmconfig.json.template'
|
||||
include 'killContracts.sh'
|
||||
include 'genKeyAndCMConfig.sh'
|
||||
include 'cmvar.json'
|
||||
include 'log4j2.properties'
|
||||
}
|
||||
into "./build/output"
|
||||
println("copyScript done !")
|
||||
@ -107,7 +124,12 @@ task copyDynamicLibrary(type: Copy) {
|
||||
into './build/output/dynamicLibrary'
|
||||
}
|
||||
|
||||
task copyLibs(type: Copy, dependsOn: ["copyScript", 'copySsl']) {
|
||||
task copyCustomPlugin(type: Copy, dependsOn: [":custom-plugin:jar"]) {
|
||||
from '../custom-plugin/build/libs/custom-plugin.jar'
|
||||
into "./build/output/libs/"
|
||||
}
|
||||
|
||||
task copyLibs(type: Copy, dependsOn: ["copyScript", 'copySsl', 'copyCustomPlugin']) {
|
||||
from configurations.runtimeClasspath
|
||||
into "./build/output/libs/"
|
||||
}
|
||||
@ -150,11 +172,6 @@ task copyDockerfile(type: Copy) {
|
||||
into "./build/"
|
||||
}
|
||||
|
||||
//task copyCP(type: Copy, dependsOn: ":cp:buildBundle") {
|
||||
// from "../cp/build/output"
|
||||
// into "./build/output"
|
||||
//}
|
||||
|
||||
task copyKeys(type: Copy) {
|
||||
from "./keys"
|
||||
into "./build/output/keys"
|
||||
|
@ -1,11 +1,80 @@
|
||||
# 配置项
|
||||
## wsPluginActions: WS端,包括contractManagerFrameHandler和CMHttpHandler
|
||||
## clientToAgentPlugins: client -> cluster 包括MasterClientFrameHandler
|
||||
## clientToClusterPlugins:[], client -> NodeCenterClientHandler
|
||||
## tcpPlugins:tcp 包括TcpserverFrameHandler
|
||||
|
||||
# 配置说明
|
||||
## 使用字符串分割,如需添加多项,在agent-backend/script/config.json.template里修改如:
|
||||
## 示例格式
|
||||
|
||||
```json
|
||||
{
|
||||
"cmi": "",
|
||||
"debug": "",
|
||||
"disableDoRepo": false,
|
||||
"disableLocalLhs": false,
|
||||
"doipCertPath": "",
|
||||
"doipLhsAddress": "",
|
||||
"doipPort": -1,
|
||||
"doipUserHandle": "",
|
||||
"enableEventPersistence": false,
|
||||
"enableSsl": "./ssl/chained.pem:./ssl/domain.pem",
|
||||
"ip": "127.0.0.1",
|
||||
"isLAN": true,
|
||||
"overwrite": false,
|
||||
"servicePort": 21030,
|
||||
"textFileSuffixes": ".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv",
|
||||
"withBdledgerClient": "./runnable/bdledger_mac",
|
||||
"withBdledgerServer": false,
|
||||
"consistencyPlugins": "./libs/custom-plugin.jar",
|
||||
"startContract": [],
|
||||
"datachainConf": "021.node.internetapi.cn:21121"
|
||||
}
|
||||
```
|
||||
|
||||
## consistencyPlugins 配置
|
||||
|
||||
下述四项插件使用字符串分割,如需添加多项,以逗号分隔,在cmconfig.json里修改如:
|
||||
|
||||
``````
|
||||
"consistencyPlugins": "xxx/xxx.jar,xxx/xxx.jar"
|
||||
``````
|
||||
|
||||
## 通讯插件配置
|
||||
|
||||
下述四项插件使用字符串分割,如需添加多项,在cmconfig.json里修改如:
|
||||
|
||||
``````
|
||||
"wsPluginActions": "org.bdware.metering.MeteringAction"
|
||||
``````
|
||||
|
||||
1. wsPluginActions: WS端,包括contractManagerFrameHandler和CMHttpHandler
|
||||
2. clientToAgentPlugins: client -> cluster 包括MasterClientFrameHandler
|
||||
3. clientToClusterPlugins:[], client -> NodeCenterClientHandler
|
||||
4. tcpPlugins:tcp 包括TcpserverFrameHandler
|
||||
|
||||
## startContract配置说明
|
||||
|
||||
其中startConfig.json为json数组结构,格式如下:
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"path": "./BDWareProjectDir/publicCompiled/xxx.ypk",
|
||||
"owner": "",
|
||||
"killBeforeStart": "",
|
||||
"createParam": {}
|
||||
},
|
||||
{
|
||||
|
||||
}
|
||||
]
|
||||
```
|
||||
path为必填配置项。表示启动的ypk的路径。
|
||||
owner为可选配置,不填时,使用NodeManger的key作为Owner。
|
||||
killBeforeStart为可选配置,填写kill的合约名称。
|
||||
createParam为可选配置。表示合约的启动参数。
|
||||
|
||||
## datachainConf 配置说明
|
||||
|
||||
1.针对Window Docker/Mac Docker
|
||||
可使用`"datachainConf":"host.docker.internal:2401"`进行配置。
|
||||
2.针对Linux Docker,可通过查看`docker 0`的网卡IP进行设置。
|
||||
如:`"datachainConf":"172.16.10.1:2401"`。
|
||||
3.如果是在同一dockercompose.yml下启动,可以使用:
|
||||
`"datachainConf":"bdledger:2401"`进行配置。
|
BIN
dynamicLibrary/libnetty_tcnative_linux_aarch_64.so
Normal file
BIN
dynamicLibrary/libnetty_tcnative_linux_aarch_64.so
Normal file
Binary file not shown.
BIN
dynamicLibrary/libnetty_tcnative_linux_x86_64.so
Normal file
BIN
dynamicLibrary/libnetty_tcnative_linux_x86_64.so
Normal file
Binary file not shown.
BIN
dynamicLibrary/libsigar-aarch64-linux.so
Normal file
BIN
dynamicLibrary/libsigar-aarch64-linux.so
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
lib/bdledger-java-sdk-dev-201104.2ce17584.jar
Normal file
BIN
lib/bdledger-java-sdk-dev-201104.2ce17584.jar
Normal file
Binary file not shown.
@ -1,19 +1,22 @@
|
||||
{
|
||||
"ip": "127.0.0.1",
|
||||
"servicePort": 21030,
|
||||
"isLAN" : true,
|
||||
"disableDoRepo": false,
|
||||
"doipPort": 21032,
|
||||
"doipCertPath": "",
|
||||
"doipUserHandle": "",
|
||||
"doipLhsAddress": "",
|
||||
"withBdledgerServer": false,
|
||||
"withBdledgerClient": "",
|
||||
"enableEventPersistence": false,
|
||||
"enableSsl": "./ssl/chained.pem:./ssl/domain.pem",
|
||||
"textFileSuffixes": ".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv",
|
||||
"wsPluginActions": "org.bdware.metering.MeteringAction",
|
||||
"clientToAgentPlugins": "",
|
||||
"clientToClusterPlugins": "",
|
||||
"tcpPlugins": ""
|
||||
"cmi":"",
|
||||
"debug":"",
|
||||
"disableDoRepo":false,
|
||||
"disableLocalLhs":false,
|
||||
"doipCertPath":"",
|
||||
"doipLhsAddress":"",
|
||||
"doipPort":21032,
|
||||
"doipUserHandle":"",
|
||||
"enableEventPersistence":false,
|
||||
"enableSsl":"./ssl/chained.pem:./ssl/domain.pem",
|
||||
"ip":"127.0.0.1",
|
||||
"isLAN":true,
|
||||
"overwrite":false,
|
||||
"servicePort":21030,
|
||||
"textFileSuffixes":".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv",
|
||||
"withBdledgerClient":"./runnable/bdledger_mac",
|
||||
"withBdledgerServer":false,
|
||||
"consistencyPlugins": "./libs/custom-plugin.jar",
|
||||
"datachainConf": "_LEDGERHOSTCONF"
|
||||
|
||||
}
|
@ -18,7 +18,7 @@ fi
|
||||
|
||||
# ps -ef | grep java | grep bdserver | grep CM | awk '{print $2}' | xargs kill -9
|
||||
|
||||
java -Dfile.encoding=UTF-8 -Djava.library.path="./dynamicLibrary" -cp "./libs/*:bdagent.jar" org.bdware.server.CMHttpServer 1>./log/cm.log 2>./log/cm.err &
|
||||
java -Dfile.encoding=UTF-8 -Djava.library.path="./dynamicLibrary" -Dlog4j.configurationFile=./log4j2.properties -cp "./libs/*:bdagent.jar" org.bdware.server.CMHttpServer 1>/dev/null 2>/dev/null &
|
||||
#-Xmx3550m -Xms3550m -Xmn2g
|
||||
#-XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=5
|
||||
# 以下可启用https/wss,其中,./ssl/xxx.pfx为ssl证书的路径,:后面的123456是密码。
|
||||
|
3
script/cmvar.json
Normal file
3
script/cmvar.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"_LEDGERHOSTCONF": "localhost:2401"
|
||||
}
|
5
script/genKeyAndCMConfig.sh
Executable file
5
script/genKeyAndCMConfig.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
echo "generate manger.key, manager.keypair!"
|
||||
java -cp cp/libs:cp/yjs.jar org.bdware.sc.SM2Helper generateKeyToFile
|
||||
echo "generate CMConfig!"
|
||||
java -cp cp/libs/*:cp/yjs.jar org.bdware.sc.SM2Helper generateCMConfig
|
40
script/log4j2.properties
Normal file
40
script/log4j2.properties
Normal file
@ -0,0 +1,40 @@
|
||||
filter.threshold.type=ThresholdFilter
|
||||
filter.threshold.level=debug
|
||||
appender.console.type=Console
|
||||
appender.console.name=STDOUT
|
||||
appender.console.layout.type=PatternLayout
|
||||
appender.console.layout.pattern=%highlight{[%-5p] %d{HH:mm:ss.SSS} %m (%F:%L)[%M]%n}{FATAL=Bright Red,ERROR=Red,WARN=Yellow,INFO=Green,DEBUG=Blue,TRACE=White}
|
||||
|
||||
appender.logrolling.type=RollingFile
|
||||
appender.logrolling.name=infoLogFile
|
||||
appender.logrolling.append=true
|
||||
appender.logrolling.fileName=./log/cm.log
|
||||
appender.logrolling.filePattern=./log/cm-%i.log
|
||||
appender.logrolling.layout.type=PatternLayout
|
||||
appender.logrolling.layout.pattern=[%-5p] %d{HH:mm:ss.SSS} %m (%F:%L)[%M]%n
|
||||
appender.logrolling.policies.type = Policies
|
||||
appender.logrolling.policies.size.type = SizeBasedTriggeringPolicy
|
||||
appender.logrolling.policies.size.size = 100MB
|
||||
appender.logrolling.strategy.type = DefaultRolloverStrategy
|
||||
appender.logrolling.strategy.max = 5
|
||||
|
||||
appender.errrolling.type=RollingFile
|
||||
appender.errrolling.name=errorLogFile
|
||||
appender.errrolling.append=true
|
||||
appender.errrolling.fileName=./log/cm.err
|
||||
appender.errrolling.filePattern=./log/cm-%i.err
|
||||
appender.errrolling.layout.type=PatternLayout
|
||||
appender.errrolling.layout.pattern=[%-5p] %d{HH:mm:ss.SSS} %m (%F:%L)[%M]%n
|
||||
appender.errrolling.policies.type = Policies
|
||||
appender.errrolling.policies.size.type = SizeBasedTriggeringPolicy
|
||||
appender.errrolling.policies.size.size = 100MB
|
||||
appender.errrolling.strategy.type = DefaultRolloverStrategy
|
||||
appender.errrolling.strategy.max = 5
|
||||
|
||||
rootLogger.level=info
|
||||
rootLogger.appenderRef.stdout.ref=STDOUT
|
||||
rootLogger.appenderRef.errorFile.ref=errorLogFile
|
||||
rootLogger.appenderRef.errorFile.level=error
|
||||
rootLogger.appenderRef.infoFile.ref=infoLogFile
|
||||
rootLogger.appenderRef.infoFile.level=info
|
||||
rootLogger.appenderRef.log.ref=log
|
@ -1,3 +1,5 @@
|
||||
#/bin/bash
|
||||
|
||||
scp -P 222 ./build/bdserver.zip dev@47.95.110.68:/data/public/releases/bdcontract/$1/
|
||||
|
||||
#In_235813
|
@ -38,12 +38,12 @@ public class HeartBeatUtil {
|
||||
|
||||
public synchronized void schedule(TimerTask checkAliveTask, int delay, int period) {
|
||||
try {
|
||||
if (!recordedFuture.containsKey(checkAliveTask)) {
|
||||
ScheduledFuture<?> future =
|
||||
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(
|
||||
checkAliveTask, delay, period, TimeUnit.MILLISECONDS);
|
||||
recordedFuture.put(checkAliveTask, future);
|
||||
if (recordedFuture.containsKey(checkAliveTask)) {
|
||||
cancel(checkAliveTask);
|
||||
}
|
||||
ScheduledFuture<?> future = ContractManager.scheduledThreadPool
|
||||
.scheduleWithFixedDelay(checkAliveTask, delay, period, TimeUnit.MILLISECONDS);
|
||||
recordedFuture.put(checkAliveTask, future);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -24,10 +24,8 @@ public class ContractExecuteInfo implements Serializable {
|
||||
public String invokeID; // TODO
|
||||
public ContractExecType type;
|
||||
public transient PriorityQueue<ContractRequest> queue; // contract request
|
||||
public transient Map<Integer, String> uniReqIDMap =
|
||||
new ConcurrentHashMap<>(); // 用于请求
|
||||
public transient Map<Integer, ResultCallback> resultMap =
|
||||
new ConcurrentHashMap<>(); // 用于请求
|
||||
public transient Map<Integer, String> uniReqIDMap = new ConcurrentHashMap<>(); // 用于请求
|
||||
public transient Map<Integer, ResultCallback> resultMap = new ConcurrentHashMap<>(); // 用于请求
|
||||
public transient PriorityQueue<TransRecord> trans_queue; // transRecord
|
||||
private String contractID;
|
||||
private boolean isPrivate = false;
|
||||
@ -44,10 +42,14 @@ public class ContractExecuteInfo implements Serializable {
|
||||
}
|
||||
|
||||
public void init() {
|
||||
if (queue == null) queue = new PriorityQueue<ContractRequest>();
|
||||
if (trans_queue == null) trans_queue = new PriorityQueue<TransRecord>();
|
||||
if (uniReqIDMap == null) uniReqIDMap = new ConcurrentHashMap<Integer, String>();
|
||||
if (resultMap == null) resultMap = new ConcurrentHashMap<Integer, ResultCallback>();
|
||||
if (queue == null)
|
||||
queue = new PriorityQueue<ContractRequest>();
|
||||
if (trans_queue == null)
|
||||
trans_queue = new PriorityQueue<TransRecord>();
|
||||
if (uniReqIDMap == null)
|
||||
uniReqIDMap = new ConcurrentHashMap<Integer, String>();
|
||||
if (resultMap == null)
|
||||
resultMap = new ConcurrentHashMap<Integer, ResultCallback>();
|
||||
}
|
||||
|
||||
public int getLastExeSeq() {
|
||||
@ -56,15 +58,15 @@ public class ContractExecuteInfo implements Serializable {
|
||||
|
||||
public void setLastExeSeq(int lastExeSeq) {
|
||||
this.lastExeSeq.set(lastExeSeq);
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.LastExeSeq.toString(), contractID)) { // 如果现在是Stable模式就同步刷到磁盘
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.LastExeSeq.toString(), contractID, lastExeSeq + "");
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) { // 如果现在是Stable模式就同步刷到磁盘
|
||||
KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID,
|
||||
lastExeSeq + "");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSequent(int a) {
|
||||
if (a - lastExeSeq.get() == 1) return true;
|
||||
if (a - lastExeSeq.get() == 1)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -85,7 +87,8 @@ public class ContractExecuteInfo implements Serializable {
|
||||
}
|
||||
|
||||
public String getPubKeyPath() {
|
||||
if (!isPrivate) return null;
|
||||
if (!isPrivate)
|
||||
return null;
|
||||
return pubKeyPath;
|
||||
}
|
||||
|
||||
@ -136,7 +139,8 @@ public class ContractExecuteInfo implements Serializable {
|
||||
System.out.println("memory=" + (memory == null ? "null" : memory));
|
||||
}
|
||||
|
||||
public boolean isDirectExecute(ContractRequest request, String uniReqID, ResultCallback result) {
|
||||
public boolean isDirectExecute(ContractRequest request, String uniReqID,
|
||||
ResultCallback result) {
|
||||
synchronized (this) {
|
||||
if (request.seq < lastExeSeq.get()) {
|
||||
if (request.getRequestID().endsWith("mul")) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.bdware.server;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import io.netty.channel.*;
|
||||
@ -15,18 +17,26 @@ import io.netty.handler.ssl.OptionalSslHandler;
|
||||
import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
import io.netty.handler.stream.ChunkedWriteHandler;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import io.prometheus.client.exporter.HTTPServer;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.core.config.Configurator;
|
||||
import org.bdware.sc.*;
|
||||
import org.bdware.sc.ContractClient;
|
||||
import org.bdware.sc.ContractManager;
|
||||
import org.bdware.sc.ContractPort;
|
||||
import org.bdware.sc.RecoverMechTimeRecorder;
|
||||
import org.bdware.sc.bean.Contract;
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
import org.bdware.sc.db.CMTables;
|
||||
import org.bdware.sc.db.KeyValueDBUtil;
|
||||
import org.bdware.sc.db.MultiIndexTimeRocksDBUtil;
|
||||
import org.bdware.sc.util.ExceptionUtil;
|
||||
import org.bdware.sdk.consistency.ConsistencyPluginManager;
|
||||
import org.bdware.server.action.FileActions;
|
||||
import org.bdware.server.action.UserManagerAction;
|
||||
import org.bdware.server.doip.ContractRepositoryMain;
|
||||
import org.bdware.server.http.CMHttpHandler;
|
||||
import org.bdware.server.nodecenter.client.NodeCenterClientHandler;
|
||||
@ -39,19 +49,22 @@ import java.lang.reflect.Field;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CMHttpServer {
|
||||
private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class);
|
||||
private static final String CONFIG_PATH = "cmconfig.json";
|
||||
public static EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
public static MultiIndexTimeRocksDBUtil nodeLogDB =
|
||||
new MultiIndexTimeRocksDBUtil(
|
||||
"./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
|
||||
public static MultiIndexTimeRocksDBUtil nodeLogDB = new MultiIndexTimeRocksDBUtil(
|
||||
"./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
|
||||
public static URLClassLoader pluginLoader;
|
||||
private static SslContext sslContext = null;
|
||||
private static CMDConf cmdConf;
|
||||
final String PATH = "/SCIDE/SCExecutor";
|
||||
private final int port;
|
||||
|
||||
@ -60,58 +73,54 @@ public class CMHttpServer {
|
||||
}
|
||||
|
||||
private static void configServer(CMDConf cmdConf) throws IOException {
|
||||
if (cmdConf.disableDoRepo) {
|
||||
DoConfig.callContractUsingDOI = false;
|
||||
GlobalConf.DOAConf doaConf = new GlobalConf.DOAConf();
|
||||
if (cmdConf.doipPort != -1) {
|
||||
doaConf.doipAddress = "tcp://" + cmdConf.ip + ":" + cmdConf.doipPort;
|
||||
} else {
|
||||
doaConf.doipAddress = "tcp://" + cmdConf.ip + ":" + (cmdConf.servicePort + 2);
|
||||
}
|
||||
GlobalConf.instance.isLAN = cmdConf.isLAN;
|
||||
if (!cmdConf.doipCertPath.isEmpty()) {
|
||||
String[] conf = cmdConf.doipCertPath.split(":");
|
||||
// DOAConf.certPath = conf[0];
|
||||
// DOAConf.certPassword = conf[1];
|
||||
if (!StringUtil.isNullOrEmpty(cmdConf.repoDoid)) {
|
||||
doaConf.repoDoid = cmdConf.repoDoid;
|
||||
}
|
||||
if (!StringUtil.isNullOrEmpty(cmdConf.repoName)) {
|
||||
doaConf.repoName = cmdConf.repoName;
|
||||
}
|
||||
if (!StringUtil.isNullOrEmpty(cmdConf.lhsAddress)) {
|
||||
doaConf.lhsAddress = cmdConf.lhsAddress;
|
||||
}
|
||||
GlobalConf.initDOAConfig(doaConf);
|
||||
|
||||
if (!cmdConf.doipUserHandle.isEmpty()) {
|
||||
DOAConf.repoDoid = cmdConf.doipUserHandle;
|
||||
}
|
||||
if (!cmdConf.doipLhsAddress.isEmpty()) {
|
||||
DOAConf.lhsAddress = cmdConf.doipLhsAddress;
|
||||
}
|
||||
if (cmdConf.withBdledgerServer) {
|
||||
ContractManager.threadPool.execute(
|
||||
() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4));
|
||||
ContractManager.threadPool
|
||||
.execute(() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4));
|
||||
}
|
||||
// 可自动运行bdledger可执行文件,也可在shell脚步中运行和停止
|
||||
if (!cmdConf.withBdledgerClient.isEmpty()) {
|
||||
ContractManager.scheduledThreadPool.schedule(
|
||||
() -> {
|
||||
File ledgerClient = new File(cmdConf.withBdledgerClient);
|
||||
LOGGER.debug("canRead=" + ledgerClient.canRead() +
|
||||
" path=" + ledgerClient.getAbsolutePath());
|
||||
try {
|
||||
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("start bdledger client failed: " + e.getMessage());
|
||||
}
|
||||
},
|
||||
1, TimeUnit.SECONDS);
|
||||
ContractManager.scheduledThreadPool.schedule(() -> {
|
||||
File ledgerClient = new File(cmdConf.withBdledgerClient);
|
||||
LOGGER.debug("canRead=" + ledgerClient.canRead() + " path="
|
||||
+ ledgerClient.getAbsolutePath());
|
||||
try {
|
||||
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
LOGGER.warn("start bdledger client failed: " + e.getMessage());
|
||||
}
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
if (cmdConf.enableEventPersistence) {
|
||||
ContractManager.eventPersistenceEnabled = true;
|
||||
}
|
||||
if (!cmdConf.enableSsl.isEmpty()) {
|
||||
if (!StringUtil.isNullOrEmpty(cmdConf.enableSsl)) {
|
||||
try {
|
||||
String[] filePaths = cmdConf.enableSsl.split(":");
|
||||
File chainedFile = new File(filePaths[0]), keyFile = new File(filePaths[1]);
|
||||
if (chainedFile.exists() && keyFile.exists()) {
|
||||
sslContext =
|
||||
SslContextBuilder.forServer(chainedFile, keyFile)
|
||||
.ciphers(
|
||||
null,
|
||||
(ciphers, defaultCiphers, supportedCiphers) ->
|
||||
defaultCiphers.stream()
|
||||
.filter(x -> null != x && !x.contains("RC4"))
|
||||
.toArray(String[]::new))
|
||||
.build();
|
||||
sslContext = SslContextBuilder.forServer(chainedFile, keyFile)
|
||||
.ciphers(null,
|
||||
(ciphers, defaultCiphers, supportedCiphers) -> defaultCiphers
|
||||
.stream().filter(x -> null != x && !x.contains("RC4"))
|
||||
.toArray(String[]::new))
|
||||
.build();
|
||||
LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -131,10 +140,10 @@ public class CMHttpServer {
|
||||
// plugins
|
||||
CMHttpHandler.wsPluginActions = parseStrAsList(cmdConf.wsPluginActions);
|
||||
TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins);
|
||||
NodeCenterClientHandler.clientToClusterPlugins = parseStrAsList(cmdConf.clientToClusterPlugins);
|
||||
NodeCenterClientHandler.clientToClusterPlugins =
|
||||
parseStrAsList(cmdConf.clientToClusterPlugins);
|
||||
org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins);
|
||||
|
||||
if (!cmdConf.debug.isEmpty()) {
|
||||
if (!StringUtil.isNullOrEmpty(cmdConf.debug)) {
|
||||
try {
|
||||
String[] classes = cmdConf.debug.split(",");
|
||||
for (String clz : classes) {
|
||||
@ -149,14 +158,101 @@ public class CMHttpServer {
|
||||
LOGGER.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
if (cmdConf.datachainConf != null) {
|
||||
GlobalConf.resetDataChain(cmdConf.datachainConf);
|
||||
}
|
||||
if (cmdConf.overwrite) {
|
||||
cmdConf.write(CONFIG_PATH);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void startByPath(JsonObject jo) {
|
||||
String path = jo.get("path").getAsString();
|
||||
path = findNewestYPK(path);
|
||||
File f = new File(path);
|
||||
if (!f.getName().endsWith(".ypk") || !f.exists())
|
||||
return;
|
||||
Contract c = new Contract();
|
||||
c.setScript(f.getAbsolutePath());
|
||||
c.setType(ContractExecType.Sole);
|
||||
if (jo.has("owner"))
|
||||
c.setOwner(jo.get("owner").getAsString());
|
||||
else
|
||||
c.setOwner(UserManagerAction.getNodeManager());
|
||||
if (jo.has("createParam"))
|
||||
c.setCreateParam(jo.get("createParam"));
|
||||
ContractManager.instance.startContract(c);
|
||||
}
|
||||
|
||||
private static String findNewestYPK(String path) {
|
||||
File toParse = new File(path);
|
||||
if (!toParse.exists()) {
|
||||
File dir = toParse.getParentFile();
|
||||
String fileName = toParse.getName();
|
||||
Pattern pattern = Pattern.compile(fileName);
|
||||
if (dir.isDirectory() && dir.exists()) {
|
||||
File[] available = dir.listFiles((f) -> {
|
||||
String name = f.getName();
|
||||
return pattern.matcher(name).matches();
|
||||
});
|
||||
List<File> result = Arrays.asList(available);
|
||||
return findNewestFile(result).getAbsolutePath();
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static File findNewestFile(List<File> files) {
|
||||
File newestFile = null;
|
||||
String newestVersion = null;
|
||||
for (File file : files) {
|
||||
String fileName = file.getName();
|
||||
String version = extractVersion(fileName);
|
||||
|
||||
if (newestVersion == null || compareVersions(version, newestVersion) > 0) {
|
||||
newestFile = file;
|
||||
newestVersion = version;
|
||||
}
|
||||
}
|
||||
return newestFile;
|
||||
}
|
||||
|
||||
private static String extractVersion(String fileName) {
|
||||
// Assuming the version is between the last '-' and the last '.'
|
||||
int lastDashIndex = fileName.lastIndexOf('-');
|
||||
int lastDotIndex = fileName.lastIndexOf('.');
|
||||
if (lastDashIndex == -1 || lastDotIndex == -1 || lastDashIndex >= lastDotIndex) {
|
||||
throw new IllegalArgumentException("Invalid file name format: " + fileName);
|
||||
}
|
||||
return fileName.substring(lastDashIndex + 1, lastDotIndex);
|
||||
}
|
||||
|
||||
public static int compareVersions(String v1, String v2) {
|
||||
String[] parts1 = v1.split("\\.");
|
||||
String[] parts2 = v2.split("\\.");
|
||||
|
||||
int length = Math.max(parts1.length, parts2.length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int num1 = i < parts1.length ? Integer.parseInt(parts1[i]) : 0;
|
||||
int num2 = i < parts2.length ? Integer.parseInt(parts2[i]) : 0;
|
||||
|
||||
if (num1 < num2) {
|
||||
return -1;
|
||||
}
|
||||
if (num1 > num2) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0; // Both versions are equal
|
||||
}
|
||||
|
||||
|
||||
private static String[] parseStrAsList(String str) {
|
||||
if (str == null) {
|
||||
return new String[]{};
|
||||
return new String[] {};
|
||||
}
|
||||
return str.split(",");
|
||||
}
|
||||
@ -164,7 +260,7 @@ public class CMHttpServer {
|
||||
private static void addDirToPath(String s) {
|
||||
try {
|
||||
LOGGER.info("add to path: " + s);
|
||||
// Field field = ClassLoader.class.getDeclaredField("sys_paths");
|
||||
// Field field = ClassLoader.class.getDeclaredField("sys_paths");
|
||||
Field field = ClassLoader.class.getDeclaredField("usr_paths");
|
||||
field.setAccessible(true);
|
||||
String[] path = (String[]) field.get(null);
|
||||
@ -189,7 +285,7 @@ public class CMHttpServer {
|
||||
if (!confFile.exists() && confTemplate.exists()) {
|
||||
FileUtils.copyFile(confTemplate, confFile);
|
||||
}
|
||||
CMDConf cmdConf = CMDConf.parseFile(CONFIG_PATH);
|
||||
cmdConf = CMDConf.parseFile(CONFIG_PATH);
|
||||
|
||||
// addDirToPath(new File("./dynamicLibrary").getAbsolutePath());
|
||||
|
||||
@ -208,55 +304,50 @@ public class CMHttpServer {
|
||||
|
||||
cmdConf.parseArgs(args);
|
||||
configServer(cmdConf);
|
||||
|
||||
// check default key pair
|
||||
File keyFile = new File("manager.key");
|
||||
if (keyFile.exists()) {
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(keyFile));
|
||||
String pubKey = br.readLine();
|
||||
String nowManager =
|
||||
KeyValueDBUtil.instance.getValue(
|
||||
CMTables.ConfigDB.toString(), "__NodeManager__");
|
||||
String nowManager = KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(),
|
||||
"__NodeManager__");
|
||||
// manager.key is used when node manager isn' set
|
||||
if (null == nowManager || nowManager.isEmpty()) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ConfigDB.toString(), "__NodeManager__", pubKey);
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.NodeRole.toString(), pubKey,
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(),
|
||||
"__NodeManager__", pubKey);
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
|
||||
"NodeManager,ContractProvider,ContractUser,ContractInstanceManager");
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.NodeTime.toString(),
|
||||
pubKey,
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
|
||||
Long.toString(new Date().getTime()));
|
||||
LOGGER.info("set node manager from manager.key");
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
start(cmdConf.ip, cmdConf.servicePort, cmdConf);
|
||||
GlobalConf.initIpPort(cmdConf.ip + ":" + cmdConf.servicePort);
|
||||
// GlobalConf.initMasterAddress(cmdConf.ip + ":" + (cmdConf.servicePort + 1));
|
||||
start(cmdConf.servicePort);
|
||||
}
|
||||
|
||||
public static void start(String ip, int port, CMDConf cmdConf) throws IOException {
|
||||
public static void start(int port) throws IOException {
|
||||
LOGGER.info("start server at:" + port);
|
||||
GlobalConf.initMasterAddress(ip + ":" + (port + 1));
|
||||
GlobalConf.initIpPort(ip + ":" + port);
|
||||
LOGGER.debug("dir:" + new File("./").getAbsolutePath());
|
||||
new CMHttpServer(port).start();
|
||||
ContractRepositoryMain.start();
|
||||
// never reach here!!
|
||||
}
|
||||
|
||||
/**
|
||||
* port: http & websocket port port+1: tcp port port+2: doip port port+3: prometheus
|
||||
*/
|
||||
private void start() {
|
||||
ConsistencyPluginManager.setContext(new SDKContext());
|
||||
// EpollEventLoopGroup
|
||||
// EpollServerSocketChannel
|
||||
// ContractManager.reconnectPort = (port - 18000) * 30 + 1630;
|
||||
// if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630;
|
||||
File[] pluginJar = new File("./pluginLib/")
|
||||
.listFiles(pathname -> pathname.getName().endsWith(".jar"));
|
||||
// if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630;
|
||||
File[] pluginJar =
|
||||
new File("./pluginLib/").listFiles(pathname -> pathname.getName().endsWith(".jar"));
|
||||
URL[] urls;
|
||||
if (pluginJar != null && pluginJar.length > 0) {
|
||||
urls = new URL[pluginJar.length];
|
||||
@ -269,7 +360,7 @@ public class CMHttpServer {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
urls = new URL[]{};
|
||||
urls = new URL[] {};
|
||||
}
|
||||
pluginLoader = new URLClassLoader(urls, CMHttpServer.class.getClassLoader());
|
||||
if (port >= 18000 && port < 18100) {
|
||||
@ -277,39 +368,37 @@ public class CMHttpServer {
|
||||
} else {
|
||||
ContractManager.cPort = new ContractPort(1615);
|
||||
}
|
||||
ContractRepositoryMain.start();
|
||||
final CMHttpHandler serverHandler = new CMHttpHandler();
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
||||
NettyConnectServerHandler trafficSharp =
|
||||
new NettyConnectServerHandler(new AtomicInteger(0));
|
||||
// NettyConnectServerHandler trafficSharp =
|
||||
// new NettyConnectServerHandler(new AtomicInteger(0));
|
||||
try {
|
||||
ServerBootstrap b1 = new ServerBootstrap();
|
||||
b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
|
||||
b1.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.localAddress(port)
|
||||
.childHandler(
|
||||
new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel arg0) {
|
||||
if (sslContext != null) {
|
||||
arg0.pipeline().addLast(new OptionalSslHandler(sslContext));
|
||||
}
|
||||
arg0.pipeline()
|
||||
.addLast(trafficSharp)
|
||||
.addLast(new HttpServerCodec())
|
||||
.addLast(new HttpObjectAggregator(10 * 1024 * 1024))
|
||||
.addLast(
|
||||
new WebSocketServerProtocolHandler(
|
||||
PATH, null, true))
|
||||
.addLast(new ChunkedWriteHandler())
|
||||
.addLast(serverHandler)
|
||||
.addLast(new ContractManagerFrameHandler());
|
||||
}
|
||||
});
|
||||
b1.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||
.localAddress(port).childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel arg0) {
|
||||
if (sslContext != null) {
|
||||
arg0.pipeline().addLast(new OptionalSslHandler(sslContext));
|
||||
}
|
||||
arg0.pipeline()// .addLast(trafficSharp)
|
||||
.addLast(new HttpServerCodec())
|
||||
.addLast(new HttpObjectAggregator(10 * 1024 * 1024))
|
||||
.addLast(new WebSocketServerProtocolHandler(PATH, null, true))
|
||||
.addLast(new ChunkedWriteHandler()).addLast(serverHandler)
|
||||
.addLast(new ContractManagerFrameHandler());
|
||||
}
|
||||
});
|
||||
final Channel ch = b1.bind(port).sync().channel();
|
||||
LOGGER.debug("[CMHttpServer] listen master port at:" + port);
|
||||
new HTTPServer(port + 3);
|
||||
NetworkManager.instance.initTCP(port + 1, workerGroup);
|
||||
|
||||
loadStartContractConfiguration();
|
||||
|
||||
|
||||
ch.closeFuture().sync();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -320,10 +409,37 @@ public class CMHttpServer {
|
||||
}
|
||||
}
|
||||
|
||||
private void loadStartContractConfiguration() {
|
||||
if (cmdConf.startContract != null && !cmdConf.startContract.isEmpty()) {
|
||||
ContractManager.scheduledThreadPool.schedule(() -> {
|
||||
for (JsonElement je : cmdConf.startContract) {
|
||||
try {
|
||||
JsonObject jo = je.getAsJsonObject();
|
||||
if (!jo.has("path"))
|
||||
continue;
|
||||
if (!jo.has("owner"))
|
||||
jo.addProperty("owner", UserManagerAction.getNodeManager());
|
||||
if (jo.has("killBeforeStart")) {
|
||||
ContractManager.instance
|
||||
.stopContract(jo.get("killBeforeStart").getAsString());
|
||||
}
|
||||
if (jo.get("path").getAsString().startsWith("@")) {
|
||||
jo.addProperty("bcoId", jo.get("path").getAsString().substring(1));
|
||||
ContractRepositoryMain.currentHandler.startUsingJsonObject(jo);
|
||||
} else {
|
||||
startByPath(jo);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@Sharable
|
||||
public static class NettyConnectServerHandler extends ChannelInboundHandlerAdapter {
|
||||
public NettyConnectServerHandler(AtomicInteger connectNum) {
|
||||
}
|
||||
public NettyConnectServerHandler(AtomicInteger connectNum) {}
|
||||
|
||||
@Override
|
||||
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
|
||||
|
@ -21,7 +21,7 @@ public class CongestionControl {
|
||||
public static int maxMasterServerLoad = 0;
|
||||
|
||||
// configurations
|
||||
public static int maxHttpConnCount = 100;
|
||||
public static int maxHttpConnCount = 1000;
|
||||
|
||||
// TODO remove sync call of executeOnOthreNode
|
||||
public static void httpCControl(ChannelHandlerContext ctx) {
|
||||
@ -47,7 +47,6 @@ public class CongestionControl {
|
||||
jo.addProperty("slaveConn", slaveCounter.get());
|
||||
jo.addProperty("slaveQueue", masterProxyLoad.get());
|
||||
jo.addProperty("masterQueue", masterServerLoad.get());
|
||||
|
||||
jo.addProperty("maxConnCount", maxHttpConnCount);
|
||||
jo.addProperty("httpMaxQueue", currHttpMax);
|
||||
jo.addProperty("slaveMaxQueue", maxMasterProxyLoad);
|
||||
|
@ -1,6 +0,0 @@
|
||||
package org.bdware.server;
|
||||
|
||||
public class DOAConf {
|
||||
public static String lhsAddress;
|
||||
public static String repoDoid;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package org.bdware.server;
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
@ -66,12 +67,13 @@ public class GRPCPool implements ChainOpener {
|
||||
RocksDB hash2Data;
|
||||
private String dataNodeIP;
|
||||
private int dataNodePort;
|
||||
static final String DBDir = "ContractManagerDB/GRPCPool";
|
||||
|
||||
// public static OnHashCallback callback; //for CM write on chain
|
||||
// public static OnHashCallback callback; //for CM write on chain
|
||||
GRPCPool() {
|
||||
logIndex = new LenVarTimeSerialIndex("./log/hashToData.index", 20);
|
||||
requestID2Hash = openDB("./log/requestIDToHash", "req2Hash");
|
||||
hash2Data = openDB("./log/hashToData", "hash2Data");
|
||||
logIndex = new LenVarTimeSerialIndex("./" + DBDir + "/hashToData.index", 20);
|
||||
requestID2Hash = openDB("./" + DBDir + "/requestIDToHash", "req2Hash");
|
||||
hash2Data = openDB("./" + DBDir + "/hashToData", "hash2Data");
|
||||
contractName2LogIndex = new HashMap<>();
|
||||
instances = new ArrayList<>();
|
||||
for (int i = 0; i < WORKER_COUNT; ++i) {
|
||||
@ -111,15 +113,12 @@ public class GRPCPool implements ChainOpener {
|
||||
params.put("DOI", "86.5000.470/" + sucFix);
|
||||
params.put("Description", "DataWare Contract Info");
|
||||
params.put("Interface", "");
|
||||
params.put(
|
||||
"Address",
|
||||
params.put("Address",
|
||||
"http://39.106.6.6:8080/SCIDE/CMManager?action=queryDataByHash&hash=" + hash);
|
||||
byte[] signature;
|
||||
try {
|
||||
signature =
|
||||
SM2Util.sign(
|
||||
GlobalConf.instance.keyPair.getPrivateKeyParameter(),
|
||||
origin.getBytes());
|
||||
signature = SM2Util.sign(GlobalConf.instance.keyPair.getPrivateKeyParameter(),
|
||||
origin.getBytes());
|
||||
} catch (Exception e) {
|
||||
signature = "no signature".getBytes();
|
||||
}
|
||||
@ -129,14 +128,8 @@ public class GRPCPool implements ChainOpener {
|
||||
HttpPostForm.postForm(url, params);
|
||||
}
|
||||
|
||||
private static Line wrapperLine(
|
||||
OnHashCallback cb,
|
||||
String from,
|
||||
String to,
|
||||
String data,
|
||||
String requestID,
|
||||
String contractName,
|
||||
String namedLedger) {
|
||||
private static Line wrapperLine(OnHashCallback cb, String from, String to, String data,
|
||||
String requestID, String contractName, String namedLedger) {
|
||||
Line l = new Line();
|
||||
l.cb = cb;
|
||||
l.from = from;
|
||||
@ -149,24 +142,15 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
|
||||
public static void updateContractInfo(String contractName, long times, long traffic) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ContractInfo.toString(), contractName + "-Times", times + "");
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ContractInfo.toString(), contractName + "-Traffic", traffic + "");
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ContractInfo.toString(), contractName + "-Times",
|
||||
times + "");
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ContractInfo.toString(),
|
||||
contractName + "-Traffic", traffic + "");
|
||||
}
|
||||
|
||||
public static void addLocalContractLog(
|
||||
String action,
|
||||
String contractID,
|
||||
String contractName,
|
||||
String pubKey,
|
||||
String function,
|
||||
long startTime,
|
||||
long costTime,
|
||||
long totalGas,
|
||||
long executionGas,
|
||||
long extraGas,
|
||||
Map<String, String> logType) {
|
||||
public static void addLocalContractLog(String action, String contractID, String contractName,
|
||||
String pubKey, String function, long startTime, long costTime, long totalGas,
|
||||
long executionGas, long extraGas, Map<String, String> logType) {
|
||||
// if (disable) return;
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("action", action);
|
||||
@ -190,8 +174,7 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() {
|
||||
}
|
||||
protected void finalize() {}
|
||||
|
||||
public void reloadConf(String confFile) {
|
||||
loadConf(confFile);
|
||||
@ -221,25 +204,14 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToChain(
|
||||
OnHashCallback callback,
|
||||
String from,
|
||||
String to,
|
||||
String data,
|
||||
String requestID,
|
||||
String namedLedger) {
|
||||
public void writeToChain(OnHashCallback callback, String from, String to, String data,
|
||||
String requestID, String namedLedger) {
|
||||
writeToChainWithContract(callback, from, to, data, requestID, null, namedLedger);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToChainWithContract(
|
||||
OnHashCallback cb,
|
||||
String from,
|
||||
String to,
|
||||
String data,
|
||||
String requestID,
|
||||
String contractName,
|
||||
String namedLedger) {
|
||||
public void writeToChainWithContract(OnHashCallback cb, String from, String to, String data,
|
||||
String requestID, String contractName, String namedLedger) {
|
||||
Line l = wrapperLine(cb, from, to, data, requestID, contractName, namedLedger);
|
||||
WorkerThread t = getNextWorkerThread();
|
||||
if (t != null) {
|
||||
@ -247,9 +219,30 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject getLedgerParams() {
|
||||
JsonObject jo = new JsonObject();
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
String conf = GlobalConf.instance.datachainConf;
|
||||
conf = conf.replaceAll(",", "\n");
|
||||
Scanner sc = new Scanner(new ByteArrayInputStream(conf.getBytes()));
|
||||
while (sc.hasNextLine()) {
|
||||
String str = sc.nextLine();
|
||||
String[] ipAndPort = str.split(":");
|
||||
String ip = ipAndPort[0];
|
||||
int port = Integer.parseInt(ipAndPort[1]);
|
||||
JsonObject client = new JsonObject();
|
||||
client.addProperty("ip", ip);
|
||||
client.addProperty("port", port);
|
||||
jsonArray.add(client);
|
||||
}
|
||||
jo.add("nodes", jsonArray);
|
||||
return jo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reRegister(String doid) {
|
||||
//TODO just success
|
||||
// TODO just success
|
||||
// throw new IllegalStateException("TODO");
|
||||
}
|
||||
|
||||
@ -258,13 +251,8 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeContractResultToLocalAndLedger(
|
||||
String result,
|
||||
ContractClient client,
|
||||
ContractRequest contractRequest,
|
||||
OnHashCallback cb,
|
||||
long start,
|
||||
long time) {
|
||||
public void writeContractResultToLocalAndLedger(String result, ContractClient client,
|
||||
ContractRequest contractRequest, OnHashCallback cb, long start, long time) {
|
||||
ContractResultLine l = new ContractResultLine();
|
||||
l.client = client;
|
||||
l.contractRequest = contractRequest;
|
||||
@ -321,21 +309,14 @@ public class GRPCPool implements ChainOpener {
|
||||
ret.put("action", "onQueryHashByReqID");
|
||||
if (args.has("requestID")) {
|
||||
try {
|
||||
ret.put(
|
||||
"data",
|
||||
new String(
|
||||
requestID2Hash.get(
|
||||
args.get("requestID").getAsString().getBytes())));
|
||||
ret.put("data", new String(
|
||||
requestID2Hash.get(args.get("requestID").getAsString().getBytes())));
|
||||
} catch (Exception e) {
|
||||
ret.put(
|
||||
"data",
|
||||
"Match failed! "
|
||||
+ "Can't find hash by this requestID "
|
||||
+ args.get("requestID").getAsString());
|
||||
ret.put("data", "Match failed! " + "Can't find hash by this requestID "
|
||||
+ args.get("requestID").getAsString());
|
||||
}
|
||||
} else {
|
||||
ret.put(
|
||||
"data",
|
||||
ret.put("data",
|
||||
"Can't find hash by this requestID " + args.get("requestID").getAsString());
|
||||
}
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
@ -362,13 +343,16 @@ public class GRPCPool implements ChainOpener {
|
||||
index = createIndexIfNotExist(args.get("contractName").getAsString());
|
||||
|
||||
}
|
||||
//根据事务类型查找
|
||||
// 根据事务类型查找
|
||||
else if (args.has("type")) {
|
||||
index = createIndexIfNotExist(args.get("type").getAsString());
|
||||
} else index = logIndex;
|
||||
} else
|
||||
index = logIndex;
|
||||
List<byte[]> result;
|
||||
if (offset < 0) result = index.requestLast(count);
|
||||
else result = index.request(offset, count);
|
||||
if (offset < 0)
|
||||
result = index.requestLast(count);
|
||||
else
|
||||
result = index.request(offset, count);
|
||||
for (byte[] data : result) {
|
||||
long date = HashUtil.bytes2Long(data);
|
||||
String hash = HashUtil.byteArray2Str(data, 8);
|
||||
@ -397,12 +381,10 @@ public class GRPCPool implements ChainOpener {
|
||||
ret.put("action", "onQueryDataByHash");
|
||||
if (args.has("hash")) {
|
||||
try {
|
||||
JsonObject jo =
|
||||
JsonParser.parseString(
|
||||
new String(
|
||||
hash2Data.get(
|
||||
args.get("hash").getAsString().getBytes())))
|
||||
.getAsJsonObject();
|
||||
JsonObject jo = JsonParser
|
||||
.parseString(new String(
|
||||
hash2Data.get(args.get("hash").getAsString().getBytes())))
|
||||
.getAsJsonObject();
|
||||
jo.addProperty("action", "onQueryDataByHash");
|
||||
resultCallback.onResult(JsonUtil.toJson(jo));
|
||||
return;
|
||||
@ -435,19 +417,16 @@ public class GRPCPool implements ChainOpener {
|
||||
ret.put("action", "onQueryDataByHash");
|
||||
if (args.has("hash")) {
|
||||
try {
|
||||
JsonObject jo =
|
||||
JsonParser.parseString(
|
||||
new String(
|
||||
hash2Data.get(
|
||||
args.get("hash").getAsString().getBytes())))
|
||||
.getAsJsonObject();
|
||||
JsonObject jo = JsonParser
|
||||
.parseString(new String(
|
||||
hash2Data.get(args.get("hash").getAsString().getBytes())))
|
||||
.getAsJsonObject();
|
||||
jo.addProperty("action", "onQueryDataByHash");
|
||||
resultCallback.onResult(JsonUtil.toJson(jo));
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ret.put(
|
||||
"data",
|
||||
ret.put("data",
|
||||
"!!! " + "Can't find data by hash " + args.get("hash").getAsString());
|
||||
}
|
||||
} else {
|
||||
@ -463,7 +442,8 @@ public class GRPCPool implements ChainOpener {
|
||||
LenVarTimeSerialIndex index = null;
|
||||
if (args.has("contractName"))
|
||||
index = createIndexIfNotExist(args.get("contractName").getAsString());
|
||||
if (index == null) index = logIndex;
|
||||
if (index == null)
|
||||
index = logIndex;
|
||||
ret.put("count", index.size() + "");
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
}
|
||||
@ -490,11 +470,8 @@ public class GRPCPool implements ChainOpener {
|
||||
obj.put("hash", hash);
|
||||
obj.put("date", date);
|
||||
try {
|
||||
obj.put(
|
||||
"data",
|
||||
JsonParser.parseString(
|
||||
new String(
|
||||
hash2Data.get(args.get("hash").getAsString().getBytes()))));
|
||||
obj.put("data", JsonParser.parseString(
|
||||
new String(hash2Data.get(args.get("hash").getAsString().getBytes()))));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -525,11 +502,8 @@ public class GRPCPool implements ChainOpener {
|
||||
obj.put("hash", hash);
|
||||
obj.put("date", date);
|
||||
try {
|
||||
obj.put(
|
||||
"data",
|
||||
shortStr(
|
||||
new String(
|
||||
hash2Data.get(args.get("hash").getAsString().getBytes()))));
|
||||
obj.put("data", shortStr(
|
||||
new String(hash2Data.get(args.get("hash").getAsString().getBytes()))));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -539,8 +513,10 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
|
||||
private String shortStr(String str) {
|
||||
if (str != null && str.length() > 50) return str.substring(0, 50);
|
||||
else return str;
|
||||
if (str != null && str.length() > 50)
|
||||
return str.substring(0, 50);
|
||||
else
|
||||
return str;
|
||||
}
|
||||
|
||||
@Action(async = true, userPermission = 1L << 24)
|
||||
@ -553,7 +529,7 @@ public class GRPCPool implements ChainOpener {
|
||||
RocksIterator iter = hash2Data.newIterator();
|
||||
iter.seekToFirst();
|
||||
List<String> hashes = new ArrayList<>();
|
||||
for (; iter.isValid(); ) {
|
||||
for (; iter.isValid();) {
|
||||
iter.value();
|
||||
hashes.add(new String(iter.value()));
|
||||
iter.next();
|
||||
@ -562,7 +538,8 @@ public class GRPCPool implements ChainOpener {
|
||||
List<Long> times = new ArrayList<>();
|
||||
for (String str : hashes) {
|
||||
Long time = getDataTime(str);
|
||||
if (time < 0) continue;
|
||||
if (time < 0)
|
||||
continue;
|
||||
times.add(time);
|
||||
memoryIndex.put(time, str);
|
||||
}
|
||||
@ -580,8 +557,7 @@ public class GRPCPool implements ChainOpener {
|
||||
try {
|
||||
String obj = new String(hash2Data.get(key.getBytes()));
|
||||
Map<String, String> parsedObj =
|
||||
JsonUtil.fromJson(obj, new TypeToken<Map<String, String>>() {
|
||||
}.getType());
|
||||
JsonUtil.fromJson(obj, new TypeToken<Map<String, String>>() {}.getType());
|
||||
String data = parsedObj.get("data");
|
||||
int len = "1575452533275".length();
|
||||
|
||||
@ -598,9 +574,8 @@ public class GRPCPool implements ChainOpener {
|
||||
private synchronized LenVarTimeSerialIndex createIndexIfNotExist(String contractName) {
|
||||
LenVarTimeSerialIndex index = contractName2LogIndex.get(contractName);
|
||||
if (index == null) {
|
||||
index =
|
||||
new LenVarTimeSerialIndex(
|
||||
"./log/hashByContract/" + contractName + ".index", 20);
|
||||
index = new LenVarTimeSerialIndex(
|
||||
"./" + DBDir + "/hashByContract/" + contractName + ".index", 20);
|
||||
contractName2LogIndex.put(contractName, index);
|
||||
}
|
||||
return index;
|
||||
@ -625,8 +600,7 @@ public class GRPCPool implements ChainOpener {
|
||||
static SecureRandom random = new SecureRandom((System.currentTimeMillis() + "").getBytes());
|
||||
boolean exit = false;
|
||||
|
||||
WorkerThread() {
|
||||
}
|
||||
WorkerThread() {}
|
||||
|
||||
public static byte[] getRandomByte(int len) {
|
||||
Random random = new Random();
|
||||
@ -640,7 +614,7 @@ public class GRPCPool implements ChainOpener {
|
||||
|
||||
public void run() {
|
||||
while (true) {
|
||||
// System.out.println("[GRPCPool] for circleing");
|
||||
// System.out.println("[GRPCPool] for circleing");
|
||||
Line l = uploadContent.poll();
|
||||
if (l != null) {
|
||||
if (l instanceof ContractResultLine) {
|
||||
@ -689,65 +663,50 @@ public class GRPCPool implements ChainOpener {
|
||||
|
||||
ListenableFuture<LedgerProto.SendTransactionResponse> ret2;
|
||||
if (l.ledgerName != null && !l.ledgerName.equals("")) {
|
||||
ret2 =
|
||||
client.sendTransaction(
|
||||
l.ledgerName,
|
||||
CommonProto.TransactionType.MESSAGE,
|
||||
l.from,
|
||||
random.nextLong(),
|
||||
l.to,
|
||||
l.data.getBytes(StandardCharsets.UTF_8));
|
||||
ret2 = client.sendTransaction(l.ledgerName, CommonProto.TransactionType.MESSAGE,
|
||||
l.from, random.nextLong(), l.to,
|
||||
l.data.getBytes(StandardCharsets.UTF_8));
|
||||
} else {
|
||||
ret2 =
|
||||
client.sendTransaction(
|
||||
LEDGER_NAME,
|
||||
CommonProto.TransactionType.MESSAGE,
|
||||
l.from, random.nextLong(),
|
||||
l.to,
|
||||
l.data.getBytes(StandardCharsets.UTF_8));
|
||||
ret2 = client.sendTransaction(LEDGER_NAME, CommonProto.TransactionType.MESSAGE,
|
||||
l.from, random.nextLong(), l.to,
|
||||
l.data.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
if (ret2 != null)
|
||||
ret2.addListener(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
ByteString hash = ret2.get().getHash();
|
||||
String hashStr;
|
||||
if (hash == null) {
|
||||
hash = ByteString.copyFrom(getRandomByte(20));
|
||||
hashStr = HashUtil.byteArray2Str(hash.toByteArray());
|
||||
LOGGER.info("generate fake hash: " + hashStr);
|
||||
} else {
|
||||
hashStr = HashUtil.byteArray2Str(hash.toByteArray());
|
||||
}
|
||||
if (!l.data.startsWith("MockTransaction_")) {
|
||||
if (null != l.cb) {
|
||||
l.cb.publishHash(l.requestID, hashStr);
|
||||
}
|
||||
instance.logIndex.index(HashUtil.str16ToBytes(hashStr));
|
||||
if (null != l.contractName) {
|
||||
LenVarTimeSerialIndex index =
|
||||
instance.createIndexIfNotExist(
|
||||
l.contractName);
|
||||
index.index(HashUtil.str16ToBytes(hashStr));
|
||||
}
|
||||
instance.hash2Data.put(
|
||||
hashStr.getBytes(), JsonUtil.toJson(l).getBytes());
|
||||
instance.requestID2Hash.put(
|
||||
l.requestID.getBytes(), hashStr.getBytes());
|
||||
LOGGER.debug(
|
||||
"put requestID: "
|
||||
+ l.requestID
|
||||
+ " hash: "
|
||||
+ hashStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn(e.getMessage());
|
||||
}
|
||||
ret2.addListener(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
ByteString hash = ret2.get().getHash();
|
||||
String hashStr;
|
||||
if (hash == null) {
|
||||
hash = ByteString.copyFrom(getRandomByte(20));
|
||||
hashStr = HashUtil.byteArray2Str(hash.toByteArray());
|
||||
LOGGER.info("generate fake hash: " + hashStr);
|
||||
} else {
|
||||
hashStr = HashUtil.byteArray2Str(hash.toByteArray());
|
||||
}
|
||||
},
|
||||
executorService);
|
||||
if (!l.data.startsWith("MockTransaction_")) {
|
||||
if (null != l.cb) {
|
||||
l.cb.publishHash(l.requestID, hashStr);
|
||||
}
|
||||
instance.logIndex.index(HashUtil.str16ToBytes(hashStr));
|
||||
if (null != l.contractName) {
|
||||
LenVarTimeSerialIndex index =
|
||||
instance.createIndexIfNotExist(l.contractName);
|
||||
index.index(HashUtil.str16ToBytes(hashStr));
|
||||
}
|
||||
instance.hash2Data.put(hashStr.getBytes(),
|
||||
JsonUtil.toJson(l).getBytes());
|
||||
instance.requestID2Hash.put(l.requestID.getBytes(),
|
||||
hashStr.getBytes());
|
||||
LOGGER.debug(
|
||||
"put requestID: " + l.requestID + " hash: " + hashStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn(e.getMessage());
|
||||
}
|
||||
}
|
||||
}, executorService);
|
||||
|
||||
// ByteString hash = ByteString.copyFrom(new byte[] { 1, 2, 2, 2 });
|
||||
} catch (Exception e) {
|
||||
@ -757,8 +716,8 @@ public class GRPCPool implements ChainOpener {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> analysisLocalContractLog(
|
||||
String data2, ContractRequest c, String detail) {
|
||||
public Map<String, String> analysisLocalContractLog(String data2, ContractRequest c,
|
||||
String detail) {
|
||||
// 本地日志
|
||||
JsonObject jo = JsonParser.parseString(data2).getAsJsonObject();
|
||||
Map<String, String> logType = new HashMap<>();
|
||||
@ -793,7 +752,7 @@ public class GRPCPool implements ChainOpener {
|
||||
logType.put("branch", "branch log failure");
|
||||
}
|
||||
} else if (info.equals("Result")) {
|
||||
s = jo.get("result").getAsString();
|
||||
s = jo.get("result").toString();
|
||||
s = JsonUtil.toJson(s);
|
||||
if (s.length() > 2) {
|
||||
s = s.substring(1, s.length() - 1);
|
||||
@ -822,7 +781,7 @@ public class GRPCPool implements ChainOpener {
|
||||
ContractClient client = l.client;
|
||||
cr = JsonUtil.fromJson(l.data, ContractResult.class);
|
||||
// updateContractInfo(
|
||||
// client.getContractName(), client.getTimes(), client.getTraffic());
|
||||
// client.getContractName(), client.getTimes(), client.getTraffic());
|
||||
String detail = client.getLogType(l.contractRequest.getAction());
|
||||
Map<String, String> logType =
|
||||
analysisLocalContractLog(l.data, l.contractRequest, detail);
|
||||
@ -848,32 +807,14 @@ public class GRPCPool implements ChainOpener {
|
||||
|
||||
// LOGGER.debug("type: " + JsonUtil.toJson(logType) + " | detail: " +
|
||||
// detail);
|
||||
addLocalContractLog(
|
||||
"executeContract",
|
||||
contractID,
|
||||
contractName,
|
||||
l.contractRequest.getPublicKey(),
|
||||
l.contractRequest.getAction(),
|
||||
l.start,
|
||||
l.costTime,
|
||||
cr.totalGas,
|
||||
cr.executionGas,
|
||||
cr.extraGas,
|
||||
logType);
|
||||
addLocalContractLog("executeContract", contractID, contractName,
|
||||
l.contractRequest.getPublicKey(), l.contractRequest.getAction(), l.start,
|
||||
l.costTime, cr.totalGas, cr.executionGas, cr.extraGas, logType);
|
||||
// 929 1277 1448 1417 1507
|
||||
if (l.contractRequest.fromContract != null) {
|
||||
addLocalContractLog(
|
||||
"executeContract",
|
||||
contractID,
|
||||
contractName,
|
||||
l.contractRequest.fromContract,
|
||||
l.contractRequest.getAction(),
|
||||
l.start,
|
||||
l.costTime,
|
||||
cr.totalGas,
|
||||
cr.executionGas,
|
||||
cr.extraGas,
|
||||
logType);
|
||||
addLocalContractLog("executeContract", contractID, contractName,
|
||||
l.contractRequest.fromContract, l.contractRequest.getAction(), l.start,
|
||||
l.costTime, cr.totalGas, cr.executionGas, cr.extraGas, logType);
|
||||
}
|
||||
// 1027 1563 1461 1519 1501
|
||||
if (toBDContract || toNamedLedger) {
|
||||
@ -888,29 +829,17 @@ public class GRPCPool implements ChainOpener {
|
||||
// 1025 1422 1497 1447 1445
|
||||
|
||||
if (toBDContract) {
|
||||
Line l1 =
|
||||
wrapperLine(
|
||||
l.cb,
|
||||
l.contractRequest.getPublicKey(),
|
||||
"executeContract",
|
||||
JsonUtil.toJson(logType),
|
||||
l.contractRequest.getRequestID(),
|
||||
contractName,
|
||||
"");
|
||||
Line l1 = wrapperLine(l.cb, l.contractRequest.getPublicKey(), "executeContract",
|
||||
JsonUtil.toJson(logType), l.contractRequest.getRequestID(),
|
||||
contractName, "");
|
||||
writeChain(l1);
|
||||
}
|
||||
// 1424 1421 1443 1160 1611
|
||||
// 1424 1421 1443 1160 1611
|
||||
if (toNamedLedger) {
|
||||
for (String name : namedLedger) {
|
||||
Line l2 =
|
||||
wrapperLine(
|
||||
l.cb,
|
||||
l.contractRequest.getPublicKey(),
|
||||
"executeContract",
|
||||
JsonUtil.toJson(logType),
|
||||
l.contractRequest.getRequestID(),
|
||||
contractName,
|
||||
name);
|
||||
Line l2 = wrapperLine(l.cb, l.contractRequest.getPublicKey(),
|
||||
"executeContract", JsonUtil.toJson(logType),
|
||||
l.contractRequest.getRequestID(), contractName, name);
|
||||
writeChain(l2);
|
||||
}
|
||||
}
|
||||
|
@ -17,13 +17,23 @@ import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class GlobalConf {
|
||||
private static final Logger LOGGER = LogManager.getLogger(GlobalConf.class);
|
||||
|
||||
public static GlobalConf instance = init();
|
||||
|
||||
|
||||
public static class DOAConf {
|
||||
|
||||
public String doipAddress;
|
||||
public String lhsAddress = "";
|
||||
public String repoDoid = "";
|
||||
public String repoName = "";
|
||||
}
|
||||
|
||||
public DOAConf doaConf;
|
||||
public String projectDir; // 从configDB读取;默认./ProjectDir/
|
||||
// 对于public-->SCIDEProjectDir/public/
|
||||
// 对于private-->SCIDEProjectDir/private/4567856786789/
|
||||
@ -40,23 +50,24 @@ public class GlobalConf {
|
||||
public String publicCompiledDir;
|
||||
public String privateDir;
|
||||
public String privateCompiledDir;
|
||||
public String bcoDir;
|
||||
|
||||
public String memoryDir;
|
||||
public String masterAddress;
|
||||
// public String masterAddress;
|
||||
public String ipPort;
|
||||
public boolean isLAN = true;
|
||||
private String nodeCenterUrl; // 从ConfigDB读。
|
||||
private String nodeCenterWSUrl;
|
||||
|
||||
|
||||
private static GlobalConf init() {
|
||||
java.util.logging.Logger.getLogger(org.bdware.bdledger.api.grpc.Client.class.getName())
|
||||
.setLevel(Level.OFF);
|
||||
Configurator.setLevel(
|
||||
"io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
|
||||
Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
|
||||
org.apache.logging.log4j.Level.OFF);
|
||||
Configurator.setLevel(
|
||||
"io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
|
||||
Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
|
||||
org.apache.logging.log4j.Level.OFF);
|
||||
Configurator.setLevel(
|
||||
"io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
|
||||
Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
|
||||
org.apache.logging.log4j.Level.OFF);
|
||||
|
||||
KeyValueDBUtil.setupCM();
|
||||
@ -66,29 +77,19 @@ public class GlobalConf {
|
||||
|
||||
try {
|
||||
String dbName = CMTables.ConfigDB.toString();
|
||||
Class<?> clz = com.sleepycat.je.utilint.Tracer.class;
|
||||
URL uri = clz.getClassLoader().getResource("com/sleepycat/je/utilint/Tracer.class");
|
||||
LOGGER.debug(null == uri ? "uri is null!" : uri.getPath());
|
||||
clz = com.sleepycat.je.log.FileHeader.class;
|
||||
uri = clz.getClassLoader().getResource("com/sleepycat/je/log/FileHeader.class");
|
||||
LOGGER.debug(null == uri ? "uri is null!" : uri.getPath());
|
||||
|
||||
if (!KeyValueDBUtil.instance.getKeys(dbName).contains("hasInited")) {
|
||||
KeyValueDBUtil.instance.setValue(dbName, "hasInited", "true");
|
||||
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
dbName, "projectDir", new File("./BDWareProjectDir/").getAbsolutePath());
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
dbName,
|
||||
"ADSPDir",
|
||||
KeyValueDBUtil.instance.setValue(dbName, "projectDir",
|
||||
new File("./BDWareProjectDir/").getAbsolutePath());
|
||||
KeyValueDBUtil.instance.setValue(dbName, "ADSPDir",
|
||||
new File("./BDWareProjectDir/ADSPDir/").getAbsolutePath());
|
||||
File f = new File("./yjs.jar");
|
||||
if (f.exists()) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
dbName, "yjsPath", new File("./yjs.jar").getAbsolutePath());
|
||||
KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
|
||||
new File("./yjs.jar").getAbsolutePath());
|
||||
} else {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
dbName, "yjsPath", new File("./cp/yjs.jar").getAbsolutePath());
|
||||
KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
|
||||
new File("./cp/yjs.jar").getAbsolutePath());
|
||||
}
|
||||
conf.keyPairStr = SM2Util.generateSM2KeyPair().toJson();
|
||||
|
||||
@ -104,43 +105,27 @@ public class GlobalConf {
|
||||
|
||||
// long time = System.currentTimeMillis()+15811200000;
|
||||
String licence =
|
||||
ByteHexUtil.encode(
|
||||
SM2Util.encrypt(
|
||||
conf.keyPair.getPublicKey(),
|
||||
(HardwareInfo.getCPUID()
|
||||
+ "=="
|
||||
+ (System.currentTimeMillis()
|
||||
+ 15811200000L))
|
||||
.getBytes()));
|
||||
ByteHexUtil
|
||||
.encode(SM2Util.encrypt(conf.keyPair.getPublicKey(),
|
||||
(HardwareInfo.getCPUID() + "=="
|
||||
+ (System.currentTimeMillis() + 15811200000L))
|
||||
.getBytes()));
|
||||
LOGGER.debug("licence:" + licence);
|
||||
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"deviceName %s license %s %s==%d",
|
||||
deviceName,
|
||||
licence,
|
||||
HardwareInfo.getCPUID(),
|
||||
(System.currentTimeMillis() + 15811200000L)));
|
||||
LOGGER.debug(String.format("deviceName %s license %s %s==%d", deviceName, licence,
|
||||
HardwareInfo.getCPUID(), (System.currentTimeMillis() + 15811200000L)));
|
||||
KeyValueDBUtil.instance.setValue(dbName, "licence", licence);
|
||||
KeyValueDBUtil.instance.setValue(dbName, "nodeName", deviceName.substring(0, 10));
|
||||
KeyValueDBUtil.instance.setValue(dbName, "masterAddress", "null");
|
||||
KeyValueDBUtil.instance.setValue(dbName, "ipPort", "null");
|
||||
JsonObject doipConfig = new JsonObject();
|
||||
doipConfig.addProperty("lhsAddress", "tcp://127.0.0.1:2641");
|
||||
doipConfig.addProperty("repoDoid", "bdware.trusted.test/Repo." + conf.keyPair.getPublicKeyStr().substring(0, 6));
|
||||
KeyValueDBUtil.instance.setValue(dbName, "doipConfig", doipConfig.toString());
|
||||
KeyValueDBUtil.instance.setValue(dbName, "nodeCenter", "ws://127.0.0.1:18002");
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
dbName,
|
||||
"datachainConf",
|
||||
"39.104.70.160:18091\n"
|
||||
+ "47.98.247.70:18091\n"
|
||||
+ "47.98.248.208:18091\n"
|
||||
+ "39.104.77.165:18091\n"
|
||||
+ "47.98.249.131:18091");
|
||||
KeyValueDBUtil.instance.setValue(dbName, "nodeCenter", "ws://127.0.0.1:18005");
|
||||
KeyValueDBUtil.instance.setValue(dbName, "datachainConf",
|
||||
"39.104.70.160:18091\n" + "47.98.247.70:18091\n" + "47.98.248.208:18091\n"
|
||||
+ "39.104.77.165:18091\n" + "47.98.249.131:18091");
|
||||
}
|
||||
|
||||
conf.projectDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir");
|
||||
conf.bcoDir = conf.projectDir + "/bco";
|
||||
conf.publicDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/public";
|
||||
conf.ADSPDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/ADSPDir";
|
||||
conf.publicCompiledDir =
|
||||
@ -155,15 +140,18 @@ public class GlobalConf {
|
||||
conf.name = KeyValueDBUtil.instance.getValue(dbName, "nodeName");
|
||||
conf.ipPort = KeyValueDBUtil.instance.getValue(dbName, "ipPort");
|
||||
conf.isLAN = "true".equals(KeyValueDBUtil.instance.getValue(dbName, "isLAN"));
|
||||
conf.masterAddress = KeyValueDBUtil.instance.getValue(dbName, "masterAddress");
|
||||
// conf.masterAddress = KeyValueDBUtil.instance.getValue(dbName, "masterAddress");
|
||||
conf.nodeCenterUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenter");
|
||||
conf.nodeCenterWSUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenterWS");
|
||||
conf.peerID = KeyValueDBUtil.instance.getValue(dbName, "peerID");
|
||||
conf.keyPairStr = KeyValueDBUtil.instance.getValue(dbName, "keyPairStr");
|
||||
// logger.debug("keyPairStr" + conf.keyPairStr);
|
||||
conf.keyPair = SM2KeyPair.fromJson(conf.keyPairStr);
|
||||
loadDOAConfig(KeyValueDBUtil.instance.getValue(dbName, "doipConfig"));
|
||||
conf.doaConf = new DOAConf();
|
||||
loadDOAConfig(conf.doaConf, KeyValueDBUtil.instance.getValue(dbName, "doipConfig"));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
}
|
||||
// verifyLicence(conf);
|
||||
@ -204,9 +192,8 @@ public class GlobalConf {
|
||||
LOGGER.debug("[GlobalConf] ProjectDir Path:" + new File(conf.projectDir).getAbsolutePath());
|
||||
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.publicDir).getAbsolutePath());
|
||||
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.ADSPDir).getAbsolutePath());
|
||||
LOGGER.debug(
|
||||
"[GlobalConf] publicDirCompiled Path:"
|
||||
+ new File(conf.publicCompiledDir).getAbsolutePath());
|
||||
LOGGER.debug("[GlobalConf] publicDirCompiled Path:"
|
||||
+ new File(conf.publicCompiledDir).getAbsolutePath());
|
||||
LOGGER.debug(
|
||||
"[GlobalConf] PersonalDir Path:" + new File(conf.privateDir).getAbsolutePath());
|
||||
LOGGER.debug("[GlobalConf] memorytDir Path:" + new File(conf.memoryDir).getAbsolutePath());
|
||||
@ -221,23 +208,19 @@ public class GlobalConf {
|
||||
return conf;
|
||||
}
|
||||
|
||||
private static void loadDOAConfig(String val) {
|
||||
private static void loadDOAConfig(DOAConf doaConf, String val) {
|
||||
try {
|
||||
JsonObject doipConfig = JsonUtil.parseStringAsJsonObject(val);
|
||||
DOAConf.lhsAddress = doipConfig.get("lhsAddress").getAsString();
|
||||
DOAConf.repoDoid = doipConfig.get("repoDoid").getAsString();
|
||||
//TODO 这个怎么获取?要通过啥来获取自己的repoDoid呢?
|
||||
//所以现在的lhsAddress暂时没起作用。
|
||||
if (val != null && !"null".equals(val)) {
|
||||
JsonObject doipConfig = JsonUtil.parseStringAsJsonObject(val);
|
||||
doaConf.lhsAddress = doipConfig.get("lhsAddress").getAsString();
|
||||
doaConf.repoDoid = doipConfig.get("repoDoid").getAsString();
|
||||
doaConf.doipAddress = doipConfig.get("doipAddress").getAsString();
|
||||
doaConf.repoName = doipConfig.get("repoName").getAsString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDOAConfig() {
|
||||
JsonObject doipConfig = new JsonObject();
|
||||
doipConfig.addProperty("lhsAddress", DOAConf.lhsAddress);
|
||||
doipConfig.addProperty("repoDoid", DOAConf.repoDoid);
|
||||
return doipConfig.toString();
|
||||
}
|
||||
|
||||
private static void deleteJelck(File file) {
|
||||
if (file.exists()) {
|
||||
@ -247,16 +230,17 @@ public class GlobalConf {
|
||||
|
||||
private static void verifyLicence(GlobalConf conf) {
|
||||
try {
|
||||
// String pubkey =
|
||||
// "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi" +
|
||||
// "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" +
|
||||
// "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" +
|
||||
// "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" +
|
||||
// "ZjFhZjFkYzBjODUxZjRlNDg2ZWIyOTM5NDI3MDc4MjA5NDg5ODliODVhZDNlOGJlNWJiYWEzZDUyMWU2" +
|
||||
// "MjdmZjE3NGY4Y2ZlZDk3NTY4OWNlNDEzOGYyMTgyOWIwMDVmMzE0YjM3MmNlZmFkZjBkNmUyOTY4ZGUz" +
|
||||
// "ZmFlNGUxNTFkMWFmNWE4Mjc4ZjQ2MDI5ODBjY2JkMDM0ZDE0YWRjZDk1ZjI1MjY3NmRlODRjYzdkNzU5" +
|
||||
// "NGYyYTAxMTliYWJmYjgyMGRjMWNjZWZjNThjNWUwYWRjMDQyM2MzYzA1ODNhZTU1MWZlN2Y5YTYwYjkx" +
|
||||
// "Zjg2YWViNDNlMzU0NzlhYWI5YmFjOTAwN2IsMTAwMDEsMA==";
|
||||
// String pubkey =
|
||||
// "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi"
|
||||
// +
|
||||
// "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" +
|
||||
// "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" +
|
||||
// "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" +
|
||||
// "ZjFhZjFkYzBjODUxZjRlNDg2ZWIyOTM5NDI3MDc4MjA5NDg5ODliODVhZDNlOGJlNWJiYWEzZDUyMWU2" +
|
||||
// "MjdmZjE3NGY4Y2ZlZDk3NTY4OWNlNDEzOGYyMTgyOWIwMDVmMzE0YjM3MmNlZmFkZjBkNmUyOTY4ZGUz" +
|
||||
// "ZmFlNGUxNTFkMWFmNWE4Mjc4ZjQ2MDI5ODBjY2JkMDM0ZDE0YWRjZDk1ZjI1MjY3NmRlODRjYzdkNzU5" +
|
||||
// "NGYyYTAxMTliYWJmYjgyMGRjMWNjZWZjNThjNWUwYWRjMDQyM2MzYzA1ODNhZTU1MWZlN2Y5YTYwYjkx" +
|
||||
// "Zjg2YWViNDNlMzU0NzlhYWI5YmFjOTAwN2IsMTAwMDEsMA==";
|
||||
LOGGER.debug("beforedecode:" + conf.licence);
|
||||
|
||||
if (conf.licence != null && conf.licence.length() > 8) {
|
||||
@ -264,11 +248,8 @@ public class GlobalConf {
|
||||
try {
|
||||
// byte[] arr = key.encode(new BASE64Decoder().decodeBuffer(conf.licence));
|
||||
// String actualKey = new String(arr);
|
||||
String arr =
|
||||
new String(
|
||||
SM2Util.decrypt(
|
||||
conf.keyPair.getPrivateKeyParameter(),
|
||||
ByteHexUtil.decode(conf.licence)));
|
||||
String arr = new String(SM2Util.decrypt(conf.keyPair.getPrivateKeyParameter(),
|
||||
ByteHexUtil.decode(conf.licence)));
|
||||
|
||||
LOGGER.debug("[GlobalConf] actualKey:" + arr);
|
||||
|
||||
@ -322,25 +303,30 @@ public class GlobalConf {
|
||||
}
|
||||
}
|
||||
|
||||
public static void initMasterAddress(String val) {
|
||||
if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1")) {
|
||||
resetMasterAddress(val);
|
||||
}
|
||||
// public static void initMasterAddress(String val) {
|
||||
// if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1"))
|
||||
// {
|
||||
// resetMasterAddress(val);
|
||||
// }
|
||||
// }
|
||||
|
||||
public static void initDOAConfig(DOAConf doaConf) {
|
||||
if (instance.doaConf == null || instance.doaConf.doipAddress == null)
|
||||
resetDOAConfig(JsonUtil.toJson(doaConf));
|
||||
}
|
||||
|
||||
public static boolean resetMasterAddress(String val) {
|
||||
try {
|
||||
instance.masterAddress = val;
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "masterAddress", val);
|
||||
NetworkManager.instance.reInitNodeCenter();
|
||||
ContractRepositoryMain.start();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// public static boolean resetMasterAddress(String val) {
|
||||
// try {
|
||||
// instance.masterAddress = val;
|
||||
// KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "masterAddress", val);
|
||||
// NetworkManager.instance.reInitNodeCenter();
|
||||
// return true;
|
||||
// } catch (Exception e) {
|
||||
// LOGGER.error(e.getMessage());
|
||||
// LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
public static boolean resetNodeCenter(String val) {
|
||||
try {
|
||||
@ -355,6 +341,19 @@ public class GlobalConf {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean resetNodeCenterWS(String val) {
|
||||
try {
|
||||
instance.nodeCenterWSUrl = val;
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "nodeCenterWS", val);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean resetDeviceName(String val) {
|
||||
try {
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "nodeName", val);
|
||||
@ -410,7 +409,7 @@ public class GlobalConf {
|
||||
}
|
||||
|
||||
public static void initIpPort(String val) {
|
||||
if ("null".equals(instance.ipPort) || instance.masterAddress.startsWith("127.0.0.1")) {
|
||||
if ("null".equals(instance.ipPort) || instance.ipPort.startsWith("127.0.0.1")) {
|
||||
resetIpPort(val);
|
||||
}
|
||||
}
|
||||
@ -421,11 +420,11 @@ public class GlobalConf {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean resetDOIPConfig(String val) {
|
||||
public static boolean resetDOAConfig(String val) {
|
||||
try {
|
||||
loadDOAConfig(val);
|
||||
//TODO get prefix if needed!
|
||||
loadDOAConfig(instance.doaConf, val);
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "doipConfig", val);
|
||||
ContractRepositoryMain.start();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
@ -460,6 +459,10 @@ public class GlobalConf {
|
||||
return instance.keyPair.getPublicKeyStr();
|
||||
}
|
||||
|
||||
public static String getNodeCenterWSUrl() {
|
||||
return instance.nodeCenterWSUrl;
|
||||
}
|
||||
|
||||
public boolean resetLicenceInternal(String licence2) {
|
||||
licence2 = licence2.replaceAll(" ", "+");
|
||||
LOGGER.info("resetLicence:" + licence2);
|
||||
|
137
src/main/java/org/bdware/server/SDKContext.java
Normal file
137
src/main/java/org/bdware/server/SDKContext.java
Normal file
@ -0,0 +1,137 @@
|
||||
package org.bdware.server;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bdware.sc.ContractManager;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.units.RecoverFlag;
|
||||
import org.bdware.sc.units.RequestCache;
|
||||
import org.bdware.sdk.consistency.api.context.*;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.SyncResult;
|
||||
import org.bdware.server.action.p2p.MasterClientTCPAction;
|
||||
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import org.bdware.server.trustedmodel.ResultCollector;
|
||||
import org.bdware.units.NetworkManager;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class SDKContext implements ISDKContext {
|
||||
IMasterServerTCPAction masterServerTCPActionProxy = new MasterServerTCPActionProxy();
|
||||
|
||||
IMasterClientTCPAction masterClientTCPAction = new MasterClientTCPActionProxy();
|
||||
|
||||
INetworkManager networkManagerProxy = new NetworkManagerProxy();
|
||||
|
||||
ICMActions cmActionsProxy = new CMActionsProxy();
|
||||
|
||||
IMasterServerRecoverMechAction masterServerRecoverMechActionProxy =
|
||||
new MasterServerRecoverMechActionProxy();
|
||||
|
||||
IGlobalConf globalConfProxy = new GlobalProxy();
|
||||
|
||||
@Override
|
||||
public IMasterClientTCPAction getMasterClientTCPAction() {
|
||||
return masterClientTCPAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMasterServerTCPAction getMasterServerTCPAction() {
|
||||
return masterServerTCPActionProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public INetworkManager getNetworkManager() {
|
||||
return networkManagerProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICMActions getCMActions() {
|
||||
return cmActionsProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMasterServerRecoverMechAction getMasterServerRecoverMechAction() {
|
||||
return masterServerRecoverMechActionProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGlobalConf getGlobalConf() {
|
||||
return globalConfProxy;
|
||||
}
|
||||
|
||||
public static class MasterServerTCPActionProxy implements IMasterServerTCPAction {
|
||||
@Override
|
||||
public SyncResult getSync() {
|
||||
return MasterServerTCPAction.sync;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, RequestCache> getReqCache() {
|
||||
return MasterServerTCPAction.requestCache;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MasterClientTCPActionProxy implements IMasterClientTCPAction {
|
||||
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
|
||||
private final MasterClientTCPAction _masterClientTCPAction = new MasterClientTCPAction();
|
||||
|
||||
@Override
|
||||
public void asyncExecuteContractLocally(JsonObject jo, ResultCallback rc) {
|
||||
executorService.execute(() -> _masterClientTCPAction.executeContractLocally(jo, rc));
|
||||
}
|
||||
}
|
||||
|
||||
public static class NetworkManagerProxy implements INetworkManager {
|
||||
@Override
|
||||
public void sendToAgent(String pubkey, String content) {
|
||||
NetworkManager.instance.sendToAgent(pubkey, content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAgentConnection(String pubkey) {
|
||||
return NetworkManager.instance.hasAgentConnection(pubkey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultCallback createResultCallback(String requestID, ResultCallback rc, int count) {
|
||||
return new ResultCollector(requestID, rc, count);
|
||||
}
|
||||
}
|
||||
|
||||
public static class CMActionsProxy implements ICMActions {
|
||||
@Override
|
||||
public ContractManager getManager() {
|
||||
return CMActions.manager;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MasterServerRecoverMechActionProxy
|
||||
implements IMasterServerRecoverMechAction {
|
||||
@Override
|
||||
public Map<String, Map<String, RecoverFlag>> getRecoverStatusMap() {
|
||||
return MasterServerRecoverMechAction.recoverStatus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restartContractFromCommonMode(String nodeID, String contractID) {
|
||||
MasterServerRecoverMechAction.restartContractFromCommonMode(nodeID, contractID);
|
||||
}
|
||||
}
|
||||
|
||||
public static class GlobalProxy implements IGlobalConf {
|
||||
@Override
|
||||
public String getNodeID() {
|
||||
return GlobalConf.getNodeID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SM2KeyPair getKeyPair() {
|
||||
return GlobalConf.instance.keyPair;
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -17,7 +17,7 @@ import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class CMLogAction {
|
||||
// static SocketGet get;
|
||||
// static SocketGet get;
|
||||
|
||||
@Action(async = true, userPermission = 0)
|
||||
public void setLogStage(JsonObject json, ResultCallback result) {
|
||||
@ -49,7 +49,8 @@ public class CMLogAction {
|
||||
if (json.has("contractName")) {
|
||||
contractName = json.get("contractName").getAsString();
|
||||
}
|
||||
queryByOffset(ContractManager.logsDB, "onQueryContractLogByOffset", contractName, json, result);
|
||||
queryByOffset(ContractManager.logsDB, "onQueryContractLogByOffset", contractName, json,
|
||||
result);
|
||||
}
|
||||
|
||||
@Action(async = true, userPermission = 1L << 22)
|
||||
@ -220,12 +221,8 @@ public class CMLogAction {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("action", "onQueryAEState");
|
||||
if (json.has("caller")) {
|
||||
ret.add(
|
||||
"result",
|
||||
JsonParser.parseString(
|
||||
JsonUtil.toJson(
|
||||
ActionExecutor.getStatistic(
|
||||
json.get("caller").getAsString()))));
|
||||
ret.add("result", JsonParser.parseString(JsonUtil
|
||||
.toJson(ActionExecutor.getStatistic(json.get("caller").getAsString()))));
|
||||
} else {
|
||||
ret.add("result", JsonParser.parseString(JsonUtil.toJson(ActionExecutor.getAllData())));
|
||||
}
|
||||
@ -244,17 +241,15 @@ public class CMLogAction {
|
||||
public void checkIsContract(JsonObject json, ResultCallback rc) {
|
||||
String requestID = new Random().nextLong() + "_" + System.currentTimeMillis();
|
||||
json.addProperty("requestID", requestID);
|
||||
JsonObject ret = CMActions.manager.nodeCenterConn.checkIsContract(requestID, json.toString());
|
||||
JsonObject ret =
|
||||
CMActions.manager.nodeCenterConn.checkIsContract(requestID, json.toString());
|
||||
json.remove("list");
|
||||
json.add("result", ret);
|
||||
json.addProperty("action", "onCheckIsContract");
|
||||
rc.onResult(json.toString());
|
||||
}
|
||||
|
||||
private void queryLogSize(
|
||||
MultiIndexTimeDBUtilIntf db,
|
||||
String action,
|
||||
String label,
|
||||
private void queryLogSize(MultiIndexTimeDBUtilIntf db, String action, String label,
|
||||
ResultCallback result) {
|
||||
try {
|
||||
JsonObject ret = new JsonObject();
|
||||
@ -274,20 +269,14 @@ public class CMLogAction {
|
||||
}
|
||||
|
||||
|
||||
private void queryByDate(
|
||||
MultiIndexTimeDBUtilIntf db,
|
||||
String action,
|
||||
String label,
|
||||
JsonObject json,
|
||||
ResultCallback result) {
|
||||
private void queryByDate(MultiIndexTimeDBUtilIntf db, String action, String label,
|
||||
JsonObject json, ResultCallback result) {
|
||||
// TODO FixMe
|
||||
// Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1
|
||||
// column 240 path $.function
|
||||
if (!json.has("start")) {
|
||||
result.onResult(
|
||||
"{\"action\":\""
|
||||
+ action
|
||||
+ "\",\"status\":\"failed\",\"data\":\"missing start\"}");
|
||||
result.onResult("{\"action\":\"" + action
|
||||
+ "\",\"status\":\"failed\",\"data\":\"missing start\"}");
|
||||
return;
|
||||
}
|
||||
long date = json.get("start").getAsLong();
|
||||
@ -307,17 +296,11 @@ public class CMLogAction {
|
||||
result.onResult(JsonUtil.toJson(ret));
|
||||
}
|
||||
|
||||
private void queryByOffset(
|
||||
MultiIndexTimeDBUtilIntf db,
|
||||
String action,
|
||||
String label,
|
||||
JsonObject json,
|
||||
ResultCallback result) {
|
||||
private void queryByOffset(MultiIndexTimeDBUtilIntf db, String action, String label,
|
||||
JsonObject json, ResultCallback result) {
|
||||
if (!json.has("count")) {
|
||||
result.onResult(
|
||||
"{\"action\":\""
|
||||
+ action
|
||||
+ "\",\"status\":\"failed\",\"data\":\"missing count\"}");
|
||||
result.onResult("{\"action\":\"" + action
|
||||
+ "\",\"status\":\"failed\",\"data\":\"missing count\"}");
|
||||
return;
|
||||
}
|
||||
int count = json.get("count").getAsInt();
|
||||
|
@ -9,8 +9,8 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
//UNUSED
|
||||
//TO Merge
|
||||
// UNUSED
|
||||
// TO Merge
|
||||
public class CheckPointCallback implements OnHashCallback {
|
||||
public static Map<String, String> lastHash = new ConcurrentHashMap<>(); // contractID,hash
|
||||
|
||||
@ -23,11 +23,11 @@ public class CheckPointCallback implements OnHashCallback {
|
||||
reqStr.put("contractID", contractID);
|
||||
reqStr.put("hash", hashStr);
|
||||
String sendStr = JsonUtil.toJson(reqStr);
|
||||
MultiContractMeta info = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
MultiContractMeta info =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
for (String node : info.getMembers()) {
|
||||
NetworkManager.instance.sendToAgent(node, sendStr);
|
||||
System.out.println(
|
||||
"发送请求给 " + node.substring(0, 5) + " 更新ledger中最新检查点的hash!");
|
||||
System.out.println("发送请求给 " + node.substring(0, 5) + " 更新ledger中最新检查点的hash!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import org.bdware.sc.util.JsonUtil;
|
||||
public class EventActions {
|
||||
@Action(async = true, userPermission = 0)
|
||||
public void deliverEvent(JsonObject args, final ResultCallback rcb) {
|
||||
CMActions.manager.deliverEvent(
|
||||
JsonUtil.fromJson(args.get("data").getAsString(), REvent.class));
|
||||
CMActions.manager
|
||||
.deliverEvent(JsonUtil.fromJson(args.get("data").getAsString(), REvent.class));
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,13 @@ package org.bdware.server.action;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bdware.sc.ContractClient;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.event.REvent;
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import static org.bdware.sc.event.REvent.REventSemantics.valueOf;
|
||||
import static org.bdware.sc.event.REvent.REventType.PUBLISH;
|
||||
|
||||
public class EventWSActions {
|
||||
@Action(async = true, userPermission = 0)
|
||||
@ -25,7 +31,7 @@ public class EventWSActions {
|
||||
ContractClient client = CMActions.manager.getClient(argCID);
|
||||
if (null == client) {
|
||||
ret.addProperty("data", "invalid contract ID or Name!");
|
||||
rcb.onResult(ret.toString());
|
||||
rcb.onResult(ret);
|
||||
return;
|
||||
}
|
||||
String contractID = client.getContractID();
|
||||
@ -36,4 +42,38 @@ public class EventWSActions {
|
||||
ret.addProperty("data", topic);
|
||||
rcb.onResult(ret);
|
||||
}
|
||||
|
||||
@Action(async = true, userPermission = 0)
|
||||
public void pubEvent(JsonObject args, final ResultCallback rcb) {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("action", "onPubEvent");
|
||||
if (!args.has("topic") || !args.has("content")) {
|
||||
ret.addProperty("data", "topic or content are missed!");
|
||||
rcb.onResult(ret);
|
||||
return;
|
||||
}
|
||||
String requestID = args.has("requestID") ? args.get("requestID").getAsString()
|
||||
: HashUtil.sha3(String.valueOf(System.currentTimeMillis()), "_",
|
||||
String.valueOf(Math.random()));
|
||||
REvent event = new REvent(args.get("topic").getAsString(), PUBLISH,
|
||||
args.get("content").getAsString(), requestID);
|
||||
if (args.has("semantics")) {
|
||||
try {
|
||||
event.setSemantics(valueOf(args.get("semantics").getAsString()));
|
||||
} catch (Exception ignored) {
|
||||
ret.addProperty("data", "unsupported semantics!");
|
||||
rcb.onResult(ret);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (args.has("pubkey") && args.has("sign")) {
|
||||
event.setPublicKey(args.get("pubkey").getAsString());
|
||||
event.setSignature(args.get("sign").getAsString());
|
||||
} else {
|
||||
SM2KeyPair keyPair = SM2Util.generateSM2KeyPair();
|
||||
event.doSignature(keyPair);
|
||||
ret.addProperty("keys", keyPair.toJson());
|
||||
}
|
||||
rcb.onResult(ret);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -18,25 +18,17 @@ public class HttpFileAction {
|
||||
LOGGER.debug("[HttpFileAction] donwloadContract : ");
|
||||
try {
|
||||
|
||||
/* already verified before into downloadContract!
|
||||
String arg = "action=" + json.get("action").getAsString();
|
||||
arg += "&projectName=" + json.get("projectName").getAsString();
|
||||
arg += "&isPrivate=" + json.get("isPrivate").getAsString();
|
||||
arg += "&pubKey=" + json.get("pubKey").getAsString();
|
||||
arg += "×tamp=" + json.get("timestamp").getAsString();
|
||||
ECPoint pubKey = SM2KeyPair.publicKeyStr2ECPoint(json.get("pubKey").getAsString());
|
||||
boolean verify =
|
||||
new SM2()
|
||||
.verify(
|
||||
arg,
|
||||
SM2.Signature.loadFromString(json.get("sign").getAsString()),
|
||||
"",
|
||||
pubKey);
|
||||
if (!verify) {
|
||||
result.onResult("/dev/null/2");
|
||||
return;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
* already verified before into downloadContract! String arg = "action=" +
|
||||
* json.get("action").getAsString(); arg += "&projectName=" +
|
||||
* json.get("projectName").getAsString(); arg += "&isPrivate=" +
|
||||
* json.get("isPrivate").getAsString(); arg += "&pubKey=" +
|
||||
* json.get("pubKey").getAsString(); arg += "×tamp=" +
|
||||
* json.get("timestamp").getAsString(); ECPoint pubKey =
|
||||
* SM2KeyPair.publicKeyStr2ECPoint(json.get("pubKey").getAsString()); boolean verify =
|
||||
* new SM2() .verify( arg, SM2.Signature.loadFromString(json.get("sign").getAsString()),
|
||||
* "", pubKey); if (!verify) { result.onResult("/dev/null/2"); return; }
|
||||
*/
|
||||
|
||||
String projectName = json.get("projectName").getAsString();
|
||||
if (projectName.contains("..")) {
|
||||
|
@ -24,41 +24,41 @@ import java.util.Map;
|
||||
public class LedgerActions {
|
||||
public static LedgerActions instance = new LedgerActions();
|
||||
|
||||
// @Action
|
||||
// private void sendMockTransaction(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
// @Action
|
||||
// private void sendMockTransaction(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
//
|
||||
// @Action
|
||||
// private void queryTransByHash(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
// @Action
|
||||
// private void queryTransByHash(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
|
||||
// @Action
|
||||
// private void queryHash(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
// @Action
|
||||
// private void queryHash(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
//
|
||||
// @Action
|
||||
// private void queryRecent(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
// @Action
|
||||
// private void queryRecent(JsonObject args, ResultCallback resultCallback) {
|
||||
// Result r = new Result();
|
||||
// r.data = "TODO";
|
||||
// r.status = true;
|
||||
// r.msg = null;
|
||||
// resultCallback.onResult(gson.toJson(r));
|
||||
// }
|
||||
|
||||
@Action
|
||||
public void queryLedgers(JsonObject jo, ResultCallback resultCallback) {
|
||||
@ -69,7 +69,8 @@ public class LedgerActions {
|
||||
LedgerProto.GetLedgersResponse ledgers = wt.getLedgersSync();
|
||||
List<String> array = new ArrayList<>();
|
||||
ret.put("list", array);
|
||||
for (int i = 0; i < ledgers.getLedgersCount(); i++) array.add(ledgers.getLedgers(i));
|
||||
for (int i = 0; i < ledgers.getLedgersCount(); i++)
|
||||
array.add(ledgers.getLedgers(i));
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
@ -99,67 +100,61 @@ public class LedgerActions {
|
||||
public void sendTransaction(JsonObject jo, ResultCallback resultCallback) {
|
||||
Client wt = GRPCPool.grpcClients.get(0);
|
||||
String ledgerName = "bdcontract";
|
||||
if (jo.has("ledger")) ledgerName = jo.get("ledger").getAsString();
|
||||
if (jo.has("ledger"))
|
||||
ledgerName = jo.get("ledger").getAsString();
|
||||
String from = jo.get("from").getAsString();
|
||||
String to = jo.get("to").getAsString();
|
||||
String data = jo.get("data").getAsString();
|
||||
ListenableFuture<LedgerProto.SendTransactionResponse> future =
|
||||
wt.sendTransaction(
|
||||
ledgerName,
|
||||
CommonProto.TransactionType.MESSAGE,
|
||||
from,
|
||||
random.nextLong(),
|
||||
to,
|
||||
data.getBytes());
|
||||
future.addListener(
|
||||
() -> {
|
||||
try {
|
||||
ByteString hash = future.get().getHash();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", true, HashUtil.byteArray2Str(hash.toByteArray()));
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
String dataStr = bo.toString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", false, dataStr);
|
||||
e.printStackTrace();
|
||||
}
|
||||
},
|
||||
ContractManager.threadPool);
|
||||
wt.sendTransaction(ledgerName, CommonProto.TransactionType.MESSAGE, from,
|
||||
random.nextLong(), to, data.getBytes());
|
||||
future.addListener(() -> {
|
||||
try {
|
||||
ByteString hash = future.get().getHash();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", true,
|
||||
HashUtil.byteArray2Str(hash.toByteArray()));
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
String dataStr = bo.toString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", false, dataStr);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, ContractManager.threadPool);
|
||||
}
|
||||
|
||||
@Action
|
||||
public void queryTransactionByHash(JsonObject jo, ResultCallback resultCallback) {
|
||||
String hash = jo.get("hash").getAsString();
|
||||
String ledgerName = "bdcontract";
|
||||
if (jo.has("ledger")) ledgerName = jo.get("ledger").getAsString();
|
||||
if (jo.has("ledger"))
|
||||
ledgerName = jo.get("ledger").getAsString();
|
||||
Client wt = GRPCPool.grpcClients.get(0);
|
||||
if (wt == null) {
|
||||
resultCallback.onResult(
|
||||
"{\"status\":\"Exception\",\"data\":\"no available worker thread\"}");
|
||||
resultCallback
|
||||
.onResult("{\"status\":\"Exception\",\"data\":\"no available worker thread\"}");
|
||||
return;
|
||||
}
|
||||
ListenableFuture<QueryProto.GetTransactionByHashResponse> future =
|
||||
wt.getTransactionByHash(ledgerName, hash);
|
||||
future.addListener(
|
||||
() -> {
|
||||
try {
|
||||
CommonProto.Transaction trans = future.get().getTransaction();
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("from", HashUtil.byteArray2Str(trans.getFrom().toByteArray()));
|
||||
ret.put("to", HashUtil.byteArray2Str(trans.getTo().toByteArray()));
|
||||
ret.put("data", trans.getData().toStringUtf8());
|
||||
ret.put("action", "onQueryTransactionByHash");
|
||||
ret.put("type", trans.getType().name());
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "onQueryTransactionByHash");
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
ret.put("data", bo.toString());
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
e.printStackTrace();
|
||||
}
|
||||
},
|
||||
ContractManager.threadPool);
|
||||
future.addListener(() -> {
|
||||
try {
|
||||
CommonProto.Transaction trans = future.get().getTransaction();
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("from", HashUtil.byteArray2Str(trans.getFrom().toByteArray()));
|
||||
ret.put("to", HashUtil.byteArray2Str(trans.getTo().toByteArray()));
|
||||
ret.put("data", trans.getData().toStringUtf8());
|
||||
ret.put("action", "onQueryTransactionByHash");
|
||||
ret.put("type", trans.getType().name());
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "onQueryTransactionByHash");
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
ret.put("data", bo.toString());
|
||||
resultCallback.onResult(JsonUtil.toJson(ret));
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, ContractManager.threadPool);
|
||||
}
|
||||
}
|
||||
|
@ -42,11 +42,8 @@ public class ManagerActions {
|
||||
public void getEncodedUUID(JsonObject args, ResultCallback resultCallback) {
|
||||
String data = null;
|
||||
try {
|
||||
data =
|
||||
ByteUtils.toHexString(
|
||||
SM2Util.encrypt(
|
||||
GlobalConf.instance.keyPair.getPublicKey(),
|
||||
HardwareInfo.getCPUID().getBytes()));
|
||||
data = ByteUtils.toHexString(SM2Util.encrypt(GlobalConf.instance.keyPair.getPublicKey(),
|
||||
HardwareInfo.getCPUID().getBytes()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -65,8 +62,7 @@ public class ManagerActions {
|
||||
try {
|
||||
String key = args.get("key").getAsString();
|
||||
String val = args.get("val").getAsString();
|
||||
boolean
|
||||
status = true;
|
||||
boolean status = true;
|
||||
Object data;
|
||||
switch (key) {
|
||||
case "licence":
|
||||
@ -82,17 +78,19 @@ public class ManagerActions {
|
||||
data = status = GlobalConf.resetDataChain(val);
|
||||
break;
|
||||
case "doipConfig":
|
||||
data = status = GlobalConf.resetDOIPConfig(val);
|
||||
data = status = GlobalConf.resetDOAConfig(val);
|
||||
break;
|
||||
case "nodeCenter":
|
||||
data = status = GlobalConf.resetNodeCenter(val);
|
||||
break;
|
||||
case "nodeCenterWS":
|
||||
data = status = GlobalConf.resetNodeCenterWS(val);
|
||||
break;
|
||||
case "nodeName":
|
||||
data = status = GlobalConf.resetNodeName(val);
|
||||
break;
|
||||
case "masterAddress":
|
||||
data = status = GlobalConf.resetMasterAddress(val);
|
||||
break;
|
||||
// data = status = GlobalConf.resetMasterAddress(val);
|
||||
default:
|
||||
status = false;
|
||||
data = "unsupported key:" + key;
|
||||
@ -101,8 +99,10 @@ public class ManagerActions {
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onUpdateConfig", false,
|
||||
"exception:" + e.getMessage());
|
||||
|
||||
}
|
||||
resultCallback.onResult("Exception");
|
||||
}
|
||||
|
||||
@Action(userPermission = 1L << 11)
|
||||
@ -117,19 +117,19 @@ public class ManagerActions {
|
||||
data.put("licence", GlobalConf.instance.licence);
|
||||
data.put("expireTime", new Date(GlobalConf.instance.expireTime) + "");
|
||||
data.put("nodeName", GlobalConf.instance.name);
|
||||
data.put("doipConfig", GlobalConf.getDOAConfig());
|
||||
data.put("doipConfig", GlobalConf.instance.doaConf);
|
||||
data.put("nodePubKey", GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onLoadConfig", true, data);
|
||||
}
|
||||
|
||||
@Action(userPermission = 1L << 11)
|
||||
public void listNodeInfos(JsonObject args, ResultCallback resultCallback) {
|
||||
ControllerManager.getNodeCenterController().getNodeInfos(
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onListNodeInfos", true, str);
|
||||
}
|
||||
});
|
||||
ControllerManager.getNodeCenterController().getNodeInfos(new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onListNodeInfos", true, str);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Action(userPermission = 1L << 26)
|
||||
@ -147,13 +147,14 @@ public class ManagerActions {
|
||||
data.put("licence", GlobalConf.instance.licence);
|
||||
data.put("nodeName", GlobalConf.instance.name);
|
||||
data.put("ipPort", GlobalConf.instance.ipPort);
|
||||
data.put("doipConfig", GlobalConf.getDOAConfig());
|
||||
data.put("isLAN", String.valueOf(GlobalConf.isLAN()));
|
||||
data.put("doipConfig", JsonUtil.toJson(GlobalConf.instance.doaConf));
|
||||
data.put("peerID", GlobalConf.instance.peerID);
|
||||
data.put("bdledger", GlobalConf.instance.datachainConf.replace("\n", " "));
|
||||
data.put("clusterConnected", String.valueOf(NetworkManager.instance.isConnectedToNodeCenter()));
|
||||
data.put("clusterConnected",
|
||||
String.valueOf(NetworkManager.instance.isConnectedToNodeCenter()));
|
||||
data.put("nodePubKey", GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
data.put("masterAddress", GlobalConf.instance.masterAddress);
|
||||
// data.put("masterAddress", GlobalConf.instance.masterAddress);
|
||||
data.put("nodeCenterWS", GlobalConf.getNodeCenterWSUrl());
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onLoadNodeConfig", true, data);
|
||||
}
|
||||
|
||||
@ -161,45 +162,61 @@ public class ManagerActions {
|
||||
public void changeNodeCenter(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
LOGGER.debug(JsonUtil.toJson(args));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeNodeCenter", true, GlobalConf.resetNodeCenter(val));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeNodeCenter", true,
|
||||
GlobalConf.resetNodeCenter(val));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void changeNodeCenterWS(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
LOGGER.debug(JsonUtil.toJson(args));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeNodeCenter", true,
|
||||
GlobalConf.resetNodeCenterWS(val));
|
||||
}
|
||||
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void changeBDledger(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
String lines = val.replace(" ", "\n");
|
||||
LOGGER.debug("changeBDledger " + lines);
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeBDledger", true, GlobalConf.resetDataChain(lines));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeBDledger", true,
|
||||
GlobalConf.resetDataChain(lines));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void changeNodeName(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeNodeName", true, GlobalConf.resetNodeName(val));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeNodeName", true,
|
||||
GlobalConf.resetNodeName(val));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void changeIpPort(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeIpPort", true, GlobalConf.resetIpPort(val));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeIpPort", true,
|
||||
GlobalConf.resetIpPort(val));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void changeDOIPConfig(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeDOIPConfig", true, GlobalConf.resetDOIPConfig(val));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeDOIPConfig", true,
|
||||
GlobalConf.resetDOAConfig(val));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void changeYJSPath(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeYJSPath", true, GlobalConf.resetYjsPath(val));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onChangeYJSPath", true,
|
||||
GlobalConf.resetYjsPath(val));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void uploadLicence(JsonObject args, ResultCallback resultCallback) {
|
||||
String val = args.get("data").getAsString();
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onUploadLicence", true, GlobalConf.resetLicence(val));
|
||||
ReplyUtil.replyWithStatus(resultCallback, "onUploadLicence", true,
|
||||
GlobalConf.resetLicence(val));
|
||||
}
|
||||
|
||||
@Action(userPermission = 1 << 11)
|
||||
@ -211,7 +228,8 @@ public class ManagerActions {
|
||||
public void applyNodeRoleDeprecated(JsonObject json, ResultCallback resultCallback) {
|
||||
try {
|
||||
String pubKey = null;
|
||||
if (json.has("verifiedPubKey")) pubKey = json.get("verifiedPubKey").getAsString();
|
||||
if (json.has("verifiedPubKey"))
|
||||
pubKey = json.get("verifiedPubKey").getAsString();
|
||||
if (pubKey == null) {
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Illegal user\"}");
|
||||
@ -232,17 +250,15 @@ public class ManagerActions {
|
||||
}
|
||||
|
||||
if (str == null || str.length() == 0) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ApplyRole.toString(), pubKey, role.name());
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey,
|
||||
role.name());
|
||||
|
||||
} else {
|
||||
if (!str.contains(role.name()))
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ApplyRole.toString(), pubKey, str + "," + role.name());
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey,
|
||||
str + "," + role.name());
|
||||
}
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ApplyTime.toString(),
|
||||
pubKey,
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ApplyTime.toString(), pubKey,
|
||||
Long.toString(new Date().getTime()));
|
||||
ReplyUtil.simpleReply(resultCallback, "onApplyNodeRole", "success");
|
||||
|
||||
@ -253,8 +269,7 @@ public class ManagerActions {
|
||||
ret.addProperty("action", "onApplyNodeRole");
|
||||
ret.addProperty("data", "success");
|
||||
ret.addProperty("role", ro);
|
||||
resultCallback.onResult(
|
||||
ret);
|
||||
resultCallback.onResult(ret);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.bdware.server.action;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@ -11,40 +13,40 @@ import org.bdware.sc.db.CMTables;
|
||||
import org.bdware.sc.db.KeyValueDBUtil;
|
||||
import org.bdware.sc.units.RecoverFlag;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.GRPCPool;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import org.bdware.server.trustedmodel.MultiPointContractInfo;
|
||||
import org.bdware.server.trustedmodel.MultiPointCooperateContractInfo;
|
||||
import org.bdware.server.trustedmodel.ResultCollector;
|
||||
import org.bdware.server.ws.ContractManagerFrameHandler;
|
||||
import org.bdware.units.NetworkManager;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// WebSocket接口
|
||||
public class MasterWSAction {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MasterWSAction.class);
|
||||
private final UserManagerAction managerAction;
|
||||
private final ContractManagerFrameHandler handler;
|
||||
|
||||
public MasterWSAction(UserManagerAction userManagerAction) {
|
||||
this.managerAction = userManagerAction;
|
||||
public MasterWSAction(ContractManagerFrameHandler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public static boolean hostMaster(String contractID) {
|
||||
return CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).isMaster();
|
||||
}
|
||||
|
||||
private boolean waitForConnection(Set<String> nodeNames) {
|
||||
private boolean waitForConnection(List<String> nodeNames) {
|
||||
LOGGER.info("waitForAllNodes:" + JsonUtil.toJson(nodeNames));
|
||||
for (int i = 0; i < 5; i++) {
|
||||
for (int i = 0; i < 500; i++) {
|
||||
boolean all = true;
|
||||
for (String str : nodeNames) {
|
||||
if (!NetworkManager.instance.hasAgentConnection(str)) {
|
||||
@ -68,6 +70,16 @@ public class MasterWSAction {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void warmConnection(JsonObject args, ResultCallback rc) {
|
||||
long start = System.currentTimeMillis();
|
||||
JsonArray pubkeys = args.get("pubKeys").getAsJsonArray();
|
||||
for (JsonElement pri : pubkeys)
|
||||
NetworkManager.instance.connectToAgent(pri.getAsString(), null);
|
||||
long end = System.currentTimeMillis();
|
||||
rc.onResult("{\"duration\":" + (end - start) + ", \"action\":\"onWarmConnection\"}");
|
||||
}
|
||||
|
||||
@Action(async = true, userPermission = 1L << 26)
|
||||
// zyx modified
|
||||
public void startContractMultiPoint(JsonObject args, final ResultCallback rc) {
|
||||
@ -92,36 +104,36 @@ public class MasterWSAction {
|
||||
} else if (args.has("projectName")) {
|
||||
contract.setScript("/" + args.get("projectName").getAsString());
|
||||
}
|
||||
if (args.has("createParam"))
|
||||
contract.setCreateParam(args.get("createParam"));
|
||||
// contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json");
|
||||
|
||||
// String toVerify =
|
||||
// "MultiPoint|"
|
||||
// + args.get("type").getAsString()
|
||||
// + "|"
|
||||
// + contract.getScriptStr()
|
||||
// + "|"
|
||||
// + managerAction.getPubKey();
|
||||
// boolean verify =
|
||||
// SM2Util.plainStrVerify(
|
||||
// managerAction.getPubKey(), toVerify,
|
||||
// "MultiPoint|"
|
||||
// + args.get("type").getAsString()
|
||||
// + "|"
|
||||
// + contract.getScriptStr()
|
||||
// + "|"
|
||||
// + managerAction.getPubKey();
|
||||
// boolean verify =
|
||||
// SM2Util.plainStrVerify(
|
||||
// managerAction.getPubKey(), toVerify,
|
||||
// args.get("sign").getAsString());
|
||||
//
|
||||
// if (!verify) {
|
||||
// Map<String, String> ret = new HashMap<>();
|
||||
// ret.put("action", "onStartContractTrustfullyResult");
|
||||
// ret.put("data", "verify signature failed");
|
||||
// ret.put("status", "Error");
|
||||
// rc.onResult(JsonUtil.toJson(ret));
|
||||
// return;
|
||||
// }
|
||||
// if (!verify) {
|
||||
// Map<String, String> ret = new HashMap<>();
|
||||
// ret.put("action", "onStartContractTrustfullyResult");
|
||||
// ret.put("data", "verify signature failed");
|
||||
// ret.put("status", "Error");
|
||||
// rc.onResult(JsonUtil.toJson(ret));
|
||||
// return;
|
||||
// }
|
||||
|
||||
SM2KeyPair sm2Key;
|
||||
String parPath = GlobalConf.instance.publicCompiledDir;
|
||||
if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) {
|
||||
parPath =
|
||||
GlobalConf.instance.privateCompiledDir
|
||||
+ "/"
|
||||
+ args.get("verifiedPubKey").getAsString();
|
||||
parPath = GlobalConf.instance.privateCompiledDir + "/"
|
||||
+ args.get("verifiedPubKey").getAsString();
|
||||
}
|
||||
String scriptOrPath = contract.getScriptStr();
|
||||
|
||||
@ -142,55 +154,42 @@ public class MasterWSAction {
|
||||
contract.setPublicKey(sm2Key.getPublicKeyStr());
|
||||
|
||||
// TODO
|
||||
// contract.setNodeCenterRepoDOI(DOIPMainServer4NC.repoIdentifier);
|
||||
// contract.setNodeCenterRepoDOI(DOIPMainServer4NC.repoIdentifier);
|
||||
// 多点合约部署时,由NC预先注册一个DOI以备使用
|
||||
/*
|
||||
if (DoConfig.openContractDORegister) {
|
||||
try {
|
||||
HandleService hs = new HandleService(HandleServiceUtils.hrRegister);
|
||||
String tmpDOI = hs.registerContract(DOIPMainServer4NC.repoIdentifier);
|
||||
if (tmpDOI == "" || tmpDOI == null)
|
||||
contract.setDOI("RegisterFailed");
|
||||
else
|
||||
contract.setDOI(tmpDOI);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
contract.setDOI("RegisterFailed");
|
||||
}
|
||||
}
|
||||
*/
|
||||
Set<String> nodeNames; // nodes' peerID
|
||||
// if (contract.getType().equals(ContractExecType.SelfAdaptiveSharding)) {
|
||||
// if (ContractManager.instance.nodeCenterConn.listNodes().length < 3) {
|
||||
// rc.onResult(
|
||||
// "{\"status\":\"Error\",\"result\":\"No enough nodes!\","
|
||||
// + "\"action\":\"onStartTrustfulContract\"}");
|
||||
// return;
|
||||
// }
|
||||
// int unitSize = args.get("selectUnitNum").getAsInt();
|
||||
// nodeNames =
|
||||
// Arrays.asList(ContractManager.instance.nodeCenterConn.getClusterByKey(
|
||||
// contractID,
|
||||
// Math.max(unitSize, 3)));
|
||||
// } else {
|
||||
* if (DoConfig.openContractDORegister) { try { HandleService hs = new
|
||||
* HandleService(HandleServiceUtils.hrRegister); String tmpDOI =
|
||||
* hs.registerContract(DOIPMainServer4NC.repoIdentifier); if (tmpDOI == "" || tmpDOI ==
|
||||
* null) contract.setDOI("RegisterFailed"); else contract.setDOI(tmpDOI); } catch (Exception
|
||||
* e) { e.printStackTrace(); contract.setDOI("RegisterFailed"); } }
|
||||
*/
|
||||
List<String> nodeNames; // nodes' peerID
|
||||
// if (contract.getType().equals(ContractExecType.SelfAdaptiveSharding)) {
|
||||
// if (ContractManager.instance.nodeCenterConn.listNodes().length < 3) {
|
||||
// rc.onResult(
|
||||
// "{\"status\":\"Error\",\"result\":\"No enough nodes!\","
|
||||
// + "\"action\":\"onStartTrustfulContract\"}");
|
||||
// return;
|
||||
// }
|
||||
// int unitSize = args.get("selectUnitNum").getAsInt();
|
||||
// nodeNames =
|
||||
// Arrays.asList(ContractManager.instance.nodeCenterConn.getClusterByKey(
|
||||
// contractID,
|
||||
// Math.max(unitSize, 3)));
|
||||
// } else {
|
||||
// all nodes' peerID in the unit
|
||||
String[] nodeNamesStr =
|
||||
args.get("peersID").getAsString().split(",");
|
||||
String[] nodeNamesStr = args.get("peersID").getAsString().split(",");
|
||||
// record information of these nodes
|
||||
nodeNames =
|
||||
Arrays.stream(nodeNamesStr)
|
||||
.filter(x -> null != x && !x.isEmpty())
|
||||
.collect(Collectors.toSet());
|
||||
// }
|
||||
nodeNames = Arrays.stream(nodeNamesStr).filter(x -> null != x && !x.isEmpty())
|
||||
.collect(Collectors.toList());
|
||||
// }
|
||||
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
String masterNode = keyPair.getPublicKeyStr();
|
||||
nodeNames.add(masterNode);
|
||||
assert nodeNames.contains(masterNode);
|
||||
// nodeNames.add(masterNode);
|
||||
int nodeSize = nodeNames.size();
|
||||
|
||||
// 方式一向NodeCenter发,要求Slave节点主动连接到Master节点.
|
||||
|
||||
Map<String, Object> requestConnect = new HashMap<>();
|
||||
requestConnect.put("action", "requestConnectToMaster");
|
||||
LOGGER.debug(multiPointContractInfo.masterNode);
|
||||
@ -199,47 +198,34 @@ public class MasterWSAction {
|
||||
if (contract.getType() == ContractExecType.Sharding) {
|
||||
requestConnect.put("connectAll", true);
|
||||
}
|
||||
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发
|
||||
waitForConnection(nodeNames);
|
||||
// NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发
|
||||
// waitForConnection(nodeNames);
|
||||
LOGGER.debug(JsonUtil.toPrettyJson(requestConnect));
|
||||
ContractManager.threadPool.execute(
|
||||
() -> {
|
||||
// 多点合约更新repository信息
|
||||
if (contract.getDOI() != null && !contract.getDOI().equals("null")) {
|
||||
LOGGER.info("multipoint contract doi register");
|
||||
GRPCPool.instance.reRegister(contract.getDOI());
|
||||
} else {
|
||||
LOGGER.info(
|
||||
"multipoint contract don't update register "
|
||||
+ (null == contract.getDOI() ? "null" : contract.getDOI()));
|
||||
}
|
||||
});
|
||||
final long curr = System.currentTimeMillis();
|
||||
String requestID = curr + "_" + (int) (Math.random() * 10000);
|
||||
ResultCollector collector =
|
||||
new ResultCollector(
|
||||
requestID,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "onStartContractTrustfullyResult");
|
||||
if (args.has("requestID")) {
|
||||
ret.put("responseID", args.get("requestID").getAsString());
|
||||
}
|
||||
ret.put("data", str);
|
||||
ret.put("executionTime", (System.currentTimeMillis() - curr) + "");
|
||||
rc.onResult(ret);
|
||||
}
|
||||
},
|
||||
nodeSize);
|
||||
ResultCollector collector = new ResultCollector(requestID, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "onStartContractTrustfullyResult");
|
||||
if (args.has("requestID")) {
|
||||
ret.put("responseID", args.get("requestID").getAsString());
|
||||
}
|
||||
ret.put("data", str);
|
||||
ret.put("executionTime", (System.currentTimeMillis() - curr) + "");
|
||||
rc.onResult(ret);
|
||||
}
|
||||
}, nodeSize);
|
||||
MasterServerTCPAction.sync.sleepWithTimeout(requestID, collector, 20);
|
||||
Map<String, Object> request = new HashMap<>();
|
||||
request.put("master", keyPair.getPublicKeyStr());
|
||||
if (args.has("isPrivate")) {
|
||||
request.put("isPrivate", args.get("isPrivate").getAsString());
|
||||
}
|
||||
request.put("pubKey", managerAction.getPubKey());
|
||||
if (args.has("isDebug")) {
|
||||
contract.setDebug(args.get("isDebug").getAsBoolean());
|
||||
}
|
||||
request.put("pubKey", getPubKey(args));
|
||||
request.put("action", "startContractTrustfully");
|
||||
request.put("requestID", requestID);
|
||||
request.put("members", nodeNames); // 执行这个合约的所有节点的pubKey
|
||||
@ -255,7 +241,7 @@ public class MasterWSAction {
|
||||
contract.setNumOfCopies(nodeSize);
|
||||
break;
|
||||
default:
|
||||
contract.setNumOfCopies(1); // TODO copies是1?
|
||||
contract.setNumOfCopies(1); // TODO copies是1?
|
||||
break;
|
||||
}
|
||||
LOGGER.debug("contract " + contract.getID() + " has been added into contractID2Members");
|
||||
@ -278,16 +264,17 @@ public class MasterWSAction {
|
||||
if (!MasterServerRecoverMechAction.recoverStatus.containsKey(nodeID)) {
|
||||
MasterServerRecoverMechAction.recoverStatus.put(nodeID, new ConcurrentHashMap<>());
|
||||
}
|
||||
MasterServerRecoverMechAction.recoverStatus
|
||||
.get(nodeID)
|
||||
.put(contractID, RecoverFlag.Fine);
|
||||
MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID,
|
||||
RecoverFlag.Fine);
|
||||
}
|
||||
rc.onResult(
|
||||
"{\"status\":\"Success\",\"result\":\""
|
||||
+ contract.getID()
|
||||
+ "\","
|
||||
+ "\"action\":\"onStartTrustfulContract\"}");
|
||||
rc.onResult("{\"status\":\"Success\",\"result\":\"" + contract.getID() + "\","
|
||||
+ "\"action\":\"onStartTrustfulContract\"}");
|
||||
LOGGER.info("startContractMultiPoint succeed!");
|
||||
}
|
||||
|
||||
public String getPubKey(JsonObject jo) {
|
||||
if (handler != null)
|
||||
return handler.getPubKey();
|
||||
return jo.get("pubKey").getAsString();
|
||||
}
|
||||
}
|
||||
|
@ -18,13 +18,14 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
public class MultiRequestInfo {
|
||||
public static final Object lock = new Object();
|
||||
private static final Logger LOGGER = LogManager.getLogger(MultiRequestInfo.class);
|
||||
public static Map<String, MultiRequestInfo> reqInfos = new ConcurrentHashMap<String, MultiRequestInfo>(); //key为requestID
|
||||
public static Map<String, MultiRequestInfo> reqInfos =
|
||||
new ConcurrentHashMap<String, MultiRequestInfo>(); // key为requestID
|
||||
public final String requestID;
|
||||
public final int seq;
|
||||
private final int total;
|
||||
private final AtomicInteger count = new AtomicInteger(0);
|
||||
private final Map<String, Boolean> flag = new HashMap<>(); //uniID是否执行
|
||||
public Map<String, ResultCallback> callbackMap = new ConcurrentHashMap<>(); //key是uniqueID,这个map中存的是第2个及之后的请求的callback第1个请求的callback在正常的请求队列中
|
||||
private final Map<String, Boolean> flag = new HashMap<>(); // uniID是否执行
|
||||
public Map<String, ResultCallback> callbackMap = new ConcurrentHashMap<>(); // key是uniqueID,这个map中存的是第2个及之后的请求的callback第1个请求的callback在正常的请求队列中
|
||||
|
||||
public MultiRequestInfo(String id, int t) {
|
||||
requestID = id;
|
||||
@ -32,41 +33,47 @@ public class MultiRequestInfo {
|
||||
total = Integer.parseInt(id.split("_")[1]);
|
||||
}
|
||||
|
||||
//交给线程池中线程完成,不能是同一个线程跑
|
||||
// 交给线程池中线程完成,不能是同一个线程跑
|
||||
public static void exeMultiReq(ContractRequest request, String uniID) {
|
||||
ContractManagerFrameHandler.executorService.execute(
|
||||
() -> {
|
||||
exeMultiReqInternal(request, uniID);
|
||||
}
|
||||
);
|
||||
ContractManagerFrameHandler.executorService.execute(() -> {
|
||||
exeMultiReqInternal(request, uniID);
|
||||
});
|
||||
}
|
||||
|
||||
//多点合约请求第一个放入队列中,其余的不放入队列完成,此方法对于不放入队列中的多点合约请求
|
||||
// 多点合约请求第一个放入队列中,其余的不放入队列完成,此方法对于不放入队列中的多点合约请求
|
||||
private static void exeMultiReqInternal(ContractRequest request, String uniID) {
|
||||
//logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId() + " ---1");
|
||||
// logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" +
|
||||
// Thread.currentThread().getId() + " ---1");
|
||||
|
||||
//TODO 2个线程都卡在这???
|
||||
// TODO 2个线程都卡在这???
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
//logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId() + " ---2");
|
||||
// logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" +
|
||||
// Thread.currentThread().getId() + " ---2");
|
||||
|
||||
MultiContractMeta cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(request.getContractID());
|
||||
//logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId() + " ---3 " + (reqInfos.containsKey(request.getRequestID())));
|
||||
MultiContractMeta cei = CMActions.manager.multiContractRecorder
|
||||
.getMultiContractMeta(request.getContractID());
|
||||
// logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" +
|
||||
// Thread.currentThread().getId() + " ---3 " +
|
||||
// (reqInfos.containsKey(request.getRequestID())));
|
||||
|
||||
|
||||
MultiRequestInfo mri = reqInfos.get(request.getRequestID());
|
||||
//logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId() + " ---4");
|
||||
// logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" +
|
||||
// Thread.currentThread().getId() + " ---4");
|
||||
|
||||
synchronized (mri.flag) {
|
||||
//logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId() + " ---5");
|
||||
// logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" +
|
||||
// Thread.currentThread().getId() + " ---5");
|
||||
|
||||
if (cei.curExeSeq > -1 && cei.curExeSeq < mri.seq) {
|
||||
// logger.info("还未执行到 " + mri.seq + " 当前序号是 " + cei.curExeSeq);
|
||||
// logger.info("还未执行到 " + mri.seq + " 当前序号是 " + cei.curExeSeq);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mri.flag.get(uniID)) {
|
||||
//logger.info("uniID=" + uniID + " 已执行过 " + " Thread-id=" + Thread.currentThread().getId());
|
||||
// logger.info("uniID=" + uniID + " 已执行过 " + " Thread-id=" +
|
||||
// Thread.currentThread().getId());
|
||||
return;
|
||||
} else {
|
||||
mri.flag.put(uniID, true);
|
||||
@ -75,28 +82,30 @@ public class MultiRequestInfo {
|
||||
|
||||
if (-1 == cei.curExeSeq)
|
||||
cei.curExeSeq = mri.seq;
|
||||
//logger.info("调试位置 3 cei.curExeSeq=" + cei.curExeSeq + " MultiRequestInfo 执行请求 " + request.seq + " uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId());
|
||||
String data2 = CMActions.manager.executeLocally(request, null);
|
||||
// logger.info("本地执行的结果为\n" + data2);
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "receiveTrustfullyResult");
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
int seq = request.seq;
|
||||
// logger.info("调试位置 3 cei.curExeSeq=" + cei.curExeSeq + " MultiRequestInfo 执行请求 " +
|
||||
// request.seq + " uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId());
|
||||
CMActions.manager.executeLocallyAsync(request, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "receiveTrustfullyResult");
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
int seq = request.seq;
|
||||
|
||||
ret.put("responseID", uniID);
|
||||
ret.put("executeTime", (System.currentTimeMillis() - start) + "");
|
||||
ret.put("data", data2);
|
||||
mri.countIncrease();
|
||||
|
||||
//logger.info("返回 uniID=" + uniID + " 的结果");
|
||||
|
||||
mri.callbackMap.get(uniID).onResult(JsonUtil.toJson(ret));
|
||||
ret.put("responseID", uniID);
|
||||
ret.put("executeTime", (System.currentTimeMillis() - start) + "");
|
||||
ret.put("data", str);
|
||||
mri.countIncrease();
|
||||
// logger.info("返回 uniID=" + uniID + " 的结果");
|
||||
mri.callbackMap.get(uniID).onResult(JsonUtil.toJson(ret));
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
|
||||
public synchronized void countIncrease() {
|
||||
int tmp = count.incrementAndGet();
|
||||
//logger.info("【MultiRequestInfo】 countIncrease tmp=" + tmp + " total=" + total);
|
||||
// logger.info("【MultiRequestInfo】 countIncrease tmp=" + tmp + " total=" + total);
|
||||
if (tmp == total) {
|
||||
reqInfos.remove(requestID);
|
||||
}
|
||||
|
@ -76,7 +76,8 @@ public class ProcessAction {
|
||||
sc.nextLine();
|
||||
while (sc.hasNextLine()) {
|
||||
String line = sc.nextLine();
|
||||
if (!line.contains("yjs.jar") || !line.contains("java")) continue;
|
||||
if (!line.contains("yjs.jar") || !line.contains("java"))
|
||||
continue;
|
||||
LOGGER.debug("==ListLeakCP=:" + line);
|
||||
Scanner sc2 = new Scanner(new ByteArrayInputStream(line.getBytes()));
|
||||
sc2.next();
|
||||
|
@ -23,8 +23,7 @@ public class ReadmeGenerator {
|
||||
|
||||
private String generateReadme() {
|
||||
VelocityEngine engine = new VelocityEngine();
|
||||
engine.setProperty(
|
||||
"file.resource.loader.path",
|
||||
engine.setProperty("file.resource.loader.path",
|
||||
new File(templatePath, "ReadmeTemplate").getAbsolutePath());
|
||||
createREADMEIfNotExists(new File(templatePath, "ReadmeTemplate"));
|
||||
VelocityContext ctx = new VelocityContext();
|
||||
@ -32,18 +31,17 @@ public class ReadmeGenerator {
|
||||
readmeFuncs = new ArrayList<>();
|
||||
}
|
||||
for (ReadmeFunction rm : readmeFuncs) {
|
||||
/*if (rm.funcDefaultArg == null) {
|
||||
rm.funcDefaultArg = "未提供默认参数";
|
||||
}
|
||||
if (rm.funcDefaultResult == null) {
|
||||
rm.funcDefaultResult = "暂无返回结果示例";
|
||||
}*/
|
||||
/*
|
||||
* if (rm.funcDefaultArg == null) { rm.funcDefaultArg = "未提供默认参数"; } if
|
||||
* (rm.funcDefaultResult == null) { rm.funcDefaultResult = "暂无返回结果示例"; }
|
||||
*/
|
||||
if (rm.funcDescription == null) {
|
||||
rm.funcDescription = "暂无描述";
|
||||
}
|
||||
/*if (rm.funcMockTemplate == null) {
|
||||
rm.funcMockTemplate = "{\"result\":\"emptyMock\"}";
|
||||
}*/
|
||||
/*
|
||||
* if (rm.funcMockTemplate == null) { rm.funcMockTemplate =
|
||||
* "{\"result\":\"emptyMock\"}"; }
|
||||
*/
|
||||
if (rm.funcArgTemplate == null) {
|
||||
rm.funcArgTemplate = "暂无参数模板";
|
||||
}
|
||||
@ -76,63 +74,24 @@ public class ReadmeGenerator {
|
||||
readmeTemplate.mkdirs();
|
||||
File content = new File(readmeTemplate, "README.md.vm");
|
||||
try {
|
||||
FileUtils.writeStringToFile(
|
||||
content,
|
||||
/*"# 简介\n"
|
||||
+ "\n"
|
||||
+ "${contractDesp}\n"
|
||||
+ "\n"
|
||||
+ "#set($hash='#')\n"
|
||||
+ "# 获取数据相关方法说明\n"
|
||||
+ "#foreach($func in ${readmeFuncs})\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash ${func.funcName}\n"
|
||||
+ "\n"
|
||||
+ ">$func.funcDescription\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash$hash 参数示例\n"
|
||||
+ "```json\n"
|
||||
+ "$func.funcDefaultArg\n"
|
||||
+ "```\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash$hash 数据模板\n"
|
||||
+ "```json\n"
|
||||
+ "$func.funcMockTemplate\n"
|
||||
+ "```\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash$hash 返回结果\n"
|
||||
+ "\n"
|
||||
+ "```json\n"
|
||||
+ "$func.funcDefaultResult\n"
|
||||
+ "```\n"
|
||||
+ "\n"
|
||||
+ "#end\n"
|
||||
+ "\n",*/
|
||||
"# 简介\n"
|
||||
+ "\n"
|
||||
+ "${contractDesp}\n"
|
||||
+ "\n"
|
||||
+ "#set($hash='#')\n"
|
||||
+ "# 获取数据相关方法说明\n"
|
||||
+ "#foreach($func in ${readmeFuncs})\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash${func.funcName}\n"
|
||||
+ "\n"
|
||||
+ ">$func.funcDescription\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash$hash 参数模板\n"
|
||||
+ "```json\n"
|
||||
+ "$func.funcDefaultArg\n"
|
||||
+ "```\n"
|
||||
+ "\n"
|
||||
+ "$hash$hash$hash 调用结果\n"
|
||||
+ "\n"
|
||||
+ "```json\n"
|
||||
+ "$func.funcRecordsString\n"
|
||||
+ "```\n"
|
||||
+ "\n"
|
||||
+ "#end\n"
|
||||
+ "\n",
|
||||
FileUtils.writeStringToFile(content,
|
||||
/*
|
||||
* "# 简介\n" + "\n" + "${contractDesp}\n" + "\n" + "#set($hash='#')\n" +
|
||||
* "# 获取数据相关方法说明\n" + "#foreach($func in ${readmeFuncs})\n" + "\n" +
|
||||
* "$hash$hash ${func.funcName}\n" + "\n" + ">$func.funcDescription\n" +
|
||||
* "\n" + "$hash$hash$hash 参数示例\n" + "```json\n" + "$func.funcDefaultArg\n"
|
||||
* + "```\n" + "\n" + "$hash$hash$hash 数据模板\n" + "```json\n" +
|
||||
* "$func.funcMockTemplate\n" + "```\n" + "\n" + "$hash$hash$hash 返回结果\n" +
|
||||
* "\n" + "```json\n" + "$func.funcDefaultResult\n" + "```\n" + "\n" +
|
||||
* "#end\n" + "\n",
|
||||
*/
|
||||
"# 简介\n" + "\n" + "${contractDesp}\n" + "\n" + "#set($hash='#')\n"
|
||||
+ "# 获取数据相关方法说明\n" + "#foreach($func in ${readmeFuncs})\n" + "\n"
|
||||
+ "$hash$hash${func.funcName}\n" + "\n" + ">$func.funcDescription\n"
|
||||
+ "\n" + "$hash$hash$hash 参数模板\n" + "```json\n"
|
||||
+ "$func.funcDefaultArg\n" + "```\n" + "\n"
|
||||
+ "$hash$hash$hash 调用结果\n" + "\n" + "```json\n"
|
||||
+ "$func.funcRecordsString\n" + "```\n" + "\n" + "#end\n" + "\n",
|
||||
StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -149,7 +108,8 @@ public class ReadmeGenerator {
|
||||
if (!name.exists()) {
|
||||
FileUtils.writeStringToFile(name, generateReadme(), StandardCharsets.UTF_8);
|
||||
return "生成文档 " + name.getName();
|
||||
} else return "请删除已有README.md";
|
||||
} else
|
||||
return "请删除已有README.md";
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "生成文档异常";
|
||||
@ -170,19 +130,19 @@ public class ReadmeGenerator {
|
||||
public String funcDescription;
|
||||
public String funcDefaultArg;
|
||||
public String funcDefaultResult;
|
||||
//public String funcMockTemplate;
|
||||
// public String funcMockTemplate;
|
||||
public String funcRecordsString;
|
||||
public String funcArgTemplate;
|
||||
public String funcArgSchema;
|
||||
public String funcResultSchema;
|
||||
public String funcResultTemplate;
|
||||
|
||||
public ReadmeFunction(
|
||||
String name, String description, String argSchema, String argTemplate, String resultSchema, String recordsString) {
|
||||
public ReadmeFunction(String name, String description, String argSchema, String argTemplate,
|
||||
String resultSchema, String recordsString) {
|
||||
funcName = name;
|
||||
funcDescription = description;
|
||||
funcArgTemplate = argTemplate;
|
||||
//funcResultTemplate = resTemplate;
|
||||
// funcResultTemplate = resTemplate;
|
||||
funcRecordsString = recordsString;
|
||||
funcArgSchema = argSchema;
|
||||
funcResultSchema = resultSchema;
|
||||
@ -205,9 +165,9 @@ public class ReadmeGenerator {
|
||||
return funcDefaultResult.replace("\\\"", "\"");
|
||||
}
|
||||
|
||||
/*public String getFuncMockTemplate() {
|
||||
return funcMockTemplate;
|
||||
}*/
|
||||
/*
|
||||
* public String getFuncMockTemplate() { return funcMockTemplate; }
|
||||
*/
|
||||
|
||||
public String getFuncArgTemplate() {
|
||||
return funcArgTemplate;
|
||||
|
@ -1,11 +1,12 @@
|
||||
package org.bdware.server.action;
|
||||
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
|
||||
public class RequestToMaster {
|
||||
long requestTime;
|
||||
ContractRequest cr;
|
||||
|
||||
public RequestToMaster(ContractRequest c){
|
||||
public RequestToMaster(ContractRequest c) {
|
||||
requestTime = System.currentTimeMillis();
|
||||
cr = c;
|
||||
}
|
||||
|
@ -3,10 +3,7 @@ package org.bdware.server.action;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractClient;
|
||||
import org.bdware.sc.ContractManager;
|
||||
import org.bdware.sc.ContractMeta;
|
||||
import org.bdware.sc.ContractStatusEnum;
|
||||
import org.bdware.sc.*;
|
||||
import org.bdware.sc.bean.Contract;
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
@ -40,7 +37,7 @@ public class TemporyTestAction {
|
||||
@Action
|
||||
public void startContractForTest(JsonObject args, ResultCallback resultCallback) {
|
||||
FileActions.startIfManifestDetected(
|
||||
new File(args.get("path").getAsString()).getAbsoluteFile());
|
||||
new File(args.get("path").getAsString()).getAbsoluteFile(), true);
|
||||
resultCallback.onResult("done");
|
||||
}
|
||||
|
||||
@ -87,25 +84,22 @@ public class TemporyTestAction {
|
||||
|
||||
@Action(async = true)
|
||||
public void batchCompile(JsonObject args, ResultCallback resultCallback) {
|
||||
ContractManagerFrameHandler.executorService.execute(
|
||||
() -> {
|
||||
File privateDir = new File(GlobalConf.instance.privateDir);
|
||||
File[] list =
|
||||
privateDir.listFiles(
|
||||
new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
});
|
||||
totalCompile = list.length;
|
||||
compileProgress = 0;
|
||||
if (list != null)
|
||||
for (File userDir : list) {
|
||||
compileProgress++;
|
||||
compileAllUserProject(userDir);
|
||||
}
|
||||
});
|
||||
ContractManagerFrameHandler.executorService.execute(() -> {
|
||||
File privateDir = new File(GlobalConf.instance.privateDir);
|
||||
File[] list = privateDir.listFiles(new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
});
|
||||
totalCompile = list.length;
|
||||
compileProgress = 0;
|
||||
if (list != null)
|
||||
for (File userDir : list) {
|
||||
compileProgress++;
|
||||
compileAllUserProject(userDir);
|
||||
}
|
||||
});
|
||||
peekComplieProgress(args, resultCallback);
|
||||
}
|
||||
|
||||
@ -172,6 +166,10 @@ public class TemporyTestAction {
|
||||
Contract c = new Contract();
|
||||
c.setScript(args.get("script").getAsString());
|
||||
c.setOwner(args.get("owner").getAsString());
|
||||
if (args.has("doipStartPort")) {
|
||||
c.setDoipPort(args.get("doipStartPort").getAsInt());
|
||||
} else
|
||||
c.setDoipPort(1716);
|
||||
ContractManager.instance.allocateKeyIfNotExists(c);
|
||||
JsonObject jo = JsonUtil.parseObjectAsJsonObject(c);
|
||||
jo.addProperty("action", "onAllocateKeyPair");
|
||||
@ -179,10 +177,17 @@ public class TemporyTestAction {
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void reconnectAll(JsonObject args, ResultCallback resultCallback) {
|
||||
ContractManager.instance.reconnectContractProcess();
|
||||
String data = ContractManager.instance.listContractsWithOwner(
|
||||
args.get("owner").getAsString(), null, 0);
|
||||
public void reconnectPort(JsonObject args, ResultCallback resultCallback) {
|
||||
ContractPort.PortVisitor reconnectVisitor = CMActions.manager.statusRecorder.getVisitor();
|
||||
String host = null;
|
||||
if (args.has("host"))
|
||||
host = args.get("host").getAsString();
|
||||
if (host != null)
|
||||
reconnectVisitor.visit(host, args.get("port").getAsInt());
|
||||
else
|
||||
reconnectVisitor.visit(args.get("port").getAsInt());
|
||||
String data = ContractManager.instance
|
||||
.listContractsWithOwner(args.get("owner").getAsString(), null, 0);
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("data", data);
|
||||
ret.addProperty("action", "onReconnectAll");
|
||||
|
@ -21,18 +21,15 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
// 节点管理员用户管理页面
|
||||
public class UserManagerAction {
|
||||
public static final String NodeManager = "__NodeManager__";
|
||||
static final Counter counter =
|
||||
Counter.build()
|
||||
.name("contract_login_counter")
|
||||
.help("contract login counter")
|
||||
.register();
|
||||
static final Counter counter = Counter.build().name("contract_login_counter")
|
||||
.help("contract login counter").register();
|
||||
private static final Logger LOGGER = LogManager.getLogger(UserManagerAction.class);
|
||||
public static ConcurrentLinkedQueue<String> contractInstanceMangerPool =
|
||||
new ConcurrentLinkedQueue<>();
|
||||
private final Random random = new Random();
|
||||
ContractManagerFrameHandler handler;
|
||||
private String sessionID;
|
||||
private String pubKey;
|
||||
private String publicKey;
|
||||
|
||||
public static String getNodeManager() {
|
||||
return KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), NodeManager);
|
||||
@ -42,6 +39,36 @@ public class UserManagerAction {
|
||||
handler = contractManagerFrameHandler;
|
||||
}
|
||||
|
||||
@Action(userPermission = 0)
|
||||
public void resetNodeManager(JsonObject json, ResultCallback resultCallback) {
|
||||
getNodeRole(json, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
if (str.contains("NodeManager")) {
|
||||
if (json.has("newPubKey")) {
|
||||
String newPubKey = json.get("newPubKey").getAsString();
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), NodeManager,
|
||||
newPubKey);
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), newPubKey,
|
||||
"NodeManager");
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onResetNodeManager\",\"data\":\"success\",\"pubKey\":\""
|
||||
+ newPubKey + "\"}");
|
||||
} else {
|
||||
// just keep the same
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onResetNodeManager\",\"data\":\"success\",\"pubKey\":\""
|
||||
+ getPubKey(json) + "\"}");
|
||||
}
|
||||
} else {
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onResetNodeManager\",\"data\":\"failed, no permission\"}");
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Action(userPermission = 0)
|
||||
public void getSessionID(JsonObject json, ResultCallback resultCallback) {
|
||||
sessionID = random.nextLong() + "_session_node";
|
||||
@ -51,13 +78,13 @@ public class UserManagerAction {
|
||||
@Action(userPermission = 0)
|
||||
public void login(JsonObject json, ResultCallback resultCallback) {
|
||||
if (sessionID == null) {
|
||||
resultCallback.onResult("{\"action\":\"onLogin\",\"status\":\"failed, no session\"," +
|
||||
"\"data\":\"" + Role.Anonymous.name() + "\"}");
|
||||
resultCallback.onResult("{\"action\":\"onLogin\",\"status\":\"failed, no session\","
|
||||
+ "\"data\":\"" + Role.Anonymous.name() + "\"}");
|
||||
}
|
||||
LOGGER.debug("[NodeManagerAction] session:" + (sessionID));
|
||||
String pubKey = json.get("pubKey").getAsString();
|
||||
if (SM2Util.plainStrVerify(pubKey, sessionID, json.get("signature").getAsString())) {
|
||||
this.pubKey = pubKey;
|
||||
this.publicKey = pubKey;
|
||||
counter.inc();
|
||||
// 是否有该用户的私有目录,没有就创建
|
||||
// BUG修复:需要仅在验证通过后可创建。
|
||||
@ -67,16 +94,22 @@ public class UserManagerAction {
|
||||
}
|
||||
getNodeRole(json, resultCallback);
|
||||
} else {
|
||||
handler.setPermission(0);
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onLogin\",\"status\":\"verify sign failed\"," +
|
||||
"\"data\":\"" + Role.Anonymous.name() + "\"}");
|
||||
|
||||
setHandlerPermission(0);
|
||||
resultCallback.onResult("{\"action\":\"onLogin\",\"status\":\"verify sign failed\","
|
||||
+ "\"data\":\"" + Role.Anonymous.name() + "\"}");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void setHandlerPermission(long i) {
|
||||
if (handler != null)
|
||||
handler.setPermission(i);
|
||||
}
|
||||
|
||||
public void getNodeRole(JsonObject json, ResultCallback resultCallback) {
|
||||
if (pubKey == null) {
|
||||
String pubKey = getPubKey(json);
|
||||
if (getPubKey(json) == null) {
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}");
|
||||
return;
|
||||
@ -89,30 +122,30 @@ public class UserManagerAction {
|
||||
ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey);
|
||||
if (isNodeManager) {
|
||||
if (ret != null && ret.length() > 0) {
|
||||
handler.setPermission(0x86000f41L | Role.compoundValue(ret.split(",")));
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}");
|
||||
setHandlerPermission(0x86000f41L | Role.compoundValue(ret.split(",")));
|
||||
resultCallback
|
||||
.onResult("{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}");
|
||||
} else {
|
||||
handler.setPermission(0x86000f41L);
|
||||
resultCallback.onResult(
|
||||
"{\"action\":\"onLogin\",\"data\":\"NodeManager\"}");
|
||||
setHandlerPermission(0x86000f41L);
|
||||
resultCallback
|
||||
.onResult("{\"action\":\"onLogin\",\"data\":\"NodeManager\"}");
|
||||
}
|
||||
} else {
|
||||
if (ret == null || ret.equals("")) {
|
||||
ret = Role.Anonymous.name();
|
||||
handler.setPermission(0);
|
||||
setHandlerPermission(0);
|
||||
} else {
|
||||
handler.setPermission(Role.compoundValue(ret.split(",")));
|
||||
setHandlerPermission(Role.compoundValue(ret.split(",")));
|
||||
}
|
||||
resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}");
|
||||
}
|
||||
} else {
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), NodeManager, pubKey);
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.NodeRole.toString(), pubKey, "NodeManager");
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime()));
|
||||
handler.setPermission(0x86000d41L);
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
|
||||
"NodeManager");
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
|
||||
Long.toString(new Date().getTime()));
|
||||
setHandlerPermission(0x86000d41L);
|
||||
resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"NodeManager\"}");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -124,7 +157,7 @@ public class UserManagerAction {
|
||||
|
||||
@Action(userPermission = 0)
|
||||
public void applyNodeRole(JsonObject json, ResultCallback resultCallback) {
|
||||
if (pubKey == null) {
|
||||
if (getPubKey(json) == null) {
|
||||
resultCallback.onResult("{\"action\":\"onApplyNodeRole\",\"data\":\"missing pubKey\"}");
|
||||
return;
|
||||
}
|
||||
@ -133,7 +166,7 @@ public class UserManagerAction {
|
||||
LOGGER.debug("[role]" + ro);
|
||||
Role role = Role.parse(json.get("role").getAsString());
|
||||
if (role != Role.Anonymous) {
|
||||
applyNodeRoleAtDB(role, resultCallback);
|
||||
applyNodeRoleAtDB(role, getPubKey(json), resultCallback);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -141,7 +174,7 @@ public class UserManagerAction {
|
||||
"{\"action\":\"onApplyNodeRole\",\"data\":\"success\",\"role\":\"" + ro + "\"}");
|
||||
}
|
||||
|
||||
private void applyNodeRoleAtDB(Role role, ResultCallback resultCallback) {
|
||||
private void applyNodeRoleAtDB(Role role, String pubKey, ResultCallback resultCallback) {
|
||||
String str = KeyValueDBUtil.instance.getValue(CMTables.ApplyRole.toString(), pubKey);
|
||||
String already = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey);
|
||||
if (already != null && already.contains(role.toString())) {
|
||||
@ -154,11 +187,11 @@ public class UserManagerAction {
|
||||
|
||||
} else {
|
||||
if (!str.contains(role.name()))
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ApplyRole.toString(), pubKey, str + "," + role.name());
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey,
|
||||
str + "," + role.name());
|
||||
}
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime()));
|
||||
KeyValueDBUtil.instance.setValue(CMTables.ApplyTime.toString(), pubKey,
|
||||
Long.toString(new Date().getTime()));
|
||||
resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"success\"}");
|
||||
}
|
||||
|
||||
@ -238,8 +271,8 @@ public class UserManagerAction {
|
||||
|
||||
if (roString.length() > 0) {
|
||||
roString = new StringBuilder(roString.subSequence(0, roString.length() - 1).toString());
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.NodeRole.toString(), pubKey, roString.toString());
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
|
||||
roString.toString());
|
||||
} else {
|
||||
KeyValueDBUtil.instance.delete(CMTables.NodeRole.toString(), pubKey);
|
||||
KeyValueDBUtil.instance.delete(CMTables.NodeTime.toString(), pubKey);
|
||||
@ -250,6 +283,9 @@ public class UserManagerAction {
|
||||
@Action(userPermission = 1 << 10)
|
||||
public void authNodeRole(JsonObject json, ResultCallback resultCallback) {
|
||||
String pubKey = json.get("pubKey").getAsString();
|
||||
if (json.has("authorizedPubKey")) {
|
||||
pubKey = json.get("authorizedPubKey").getAsString();
|
||||
}
|
||||
boolean isAccept = json.get("isAccept").getAsBoolean();
|
||||
if (pubKey == null || pubKey.length() == 0) {
|
||||
resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"missing pubKey\"}");
|
||||
@ -277,8 +313,8 @@ public class UserManagerAction {
|
||||
already = roles;
|
||||
}
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey, already);
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime()));
|
||||
KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
|
||||
Long.toString(new Date().getTime()));
|
||||
KeyValueDBUtil.instance.delete(CMTables.ApplyRole.toString(), pubKey);
|
||||
KeyValueDBUtil.instance.delete(CMTables.ApplyTime.toString(), pubKey);
|
||||
resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"success\"}");
|
||||
@ -286,15 +322,26 @@ public class UserManagerAction {
|
||||
|
||||
@Action(userPermission = 0)
|
||||
public void queryRole(JsonObject args, final ResultCallback resultCallback) {
|
||||
if (pubKey == null) {
|
||||
String pubkey = getPubKey(args);
|
||||
if (pubkey == null) {
|
||||
resultCallback.onResult("{\"action\":\"onQueryRole\",\"data\":\"anonymous\"}");
|
||||
return;
|
||||
}
|
||||
args.addProperty("pubKey", pubKey);
|
||||
args.addProperty("pubKey", pubkey);
|
||||
args.addProperty("requestID", random.nextLong() + "_ID");
|
||||
}
|
||||
|
||||
public String getPubKey() {
|
||||
return pubKey;
|
||||
return getPubKey(null);
|
||||
}
|
||||
|
||||
private String getPubKey(JsonObject json) {
|
||||
try {
|
||||
if (this.publicKey != null)
|
||||
return this.publicKey;
|
||||
return json.get("pubKey").getAsString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ import java.util.Map;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class AliveCheckClientAction {
|
||||
public static final int sendDelay = 2000;
|
||||
public static final int checkDelay = 5000;
|
||||
public static final int sendDelay = 20000;
|
||||
public static final int checkDelay = 50000;
|
||||
private static final Logger LOGGER = LogManager.getLogger(AliveCheckClientAction.class);
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss");
|
||||
private final String masterPubkey;
|
||||
@ -57,76 +57,65 @@ public class AliveCheckClientAction {
|
||||
|
||||
public void checkMasterAlive(ResultCallback rc) {
|
||||
if (sendPingTask == null)
|
||||
sendPingTask =
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"f %s",
|
||||
DATE_FORMAT
|
||||
.format(System.currentTimeMillis())));
|
||||
Map<String, String> ping = new HashMap<>();
|
||||
ping.put("action", "masterPing");
|
||||
rc.onResult(JsonUtil.toJson(ping));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
sendPingTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
LOGGER.debug(String.format("f %s",
|
||||
DATE_FORMAT.format(System.currentTimeMillis())));
|
||||
Map<String, String> ping = new HashMap<>();
|
||||
ping.put("action", "masterPing");
|
||||
rc.onResult(JsonUtil.toJson(ping));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
if (null == checkAliveTask)
|
||||
checkAliveTask =
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
run1();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
HeartBeatUtil.getInstance().cancel(this);
|
||||
checkAliveTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
run1();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
HeartBeatUtil.getInstance().cancel(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void run1() {
|
||||
long cur = System.currentTimeMillis();
|
||||
if (cur - lastMasterPongTime >= (2 * sendDelay)) {
|
||||
LOGGER.info("master maybe crash down, because lastMasterPongTime="
|
||||
+ DATE_FORMAT.format(lastMasterPongTime) + "!");
|
||||
|
||||
// 向NC发通知重新选举master,如果NC没有收到所有节点的重选请求,就认为是这个节点和master连接断开
|
||||
// 这个节点需要重连master
|
||||
Map<String, String> request = new HashMap<>();
|
||||
request.put("action", "electMaster");
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
request.put("nodeID", keyPair.getPublicKeyStr());
|
||||
for (MultiContractMeta meta : CMActions.manager.multiContractRecorder
|
||||
.getStatus().values()) {
|
||||
if (meta.getMasterNode().equals(masterPubkey)) {
|
||||
LOGGER.info("master of contract " + meta.getContractID()
|
||||
+ " maybe crash down! master=" + masterPubkey);
|
||||
request.put("contractID", meta.getContractID());
|
||||
int lastSeq = meta.getLastExeSeq();
|
||||
request.put("lastExe", lastSeq + "");
|
||||
request.put("master", masterPubkey);
|
||||
request.put("members", meta.joinMembers(","));
|
||||
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(request));
|
||||
MasterElectTimeRecorder.findMasterCrash =
|
||||
System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
public void run1() {
|
||||
long cur = System.currentTimeMillis();
|
||||
if (cur - lastMasterPongTime >= (2 * sendDelay)) {
|
||||
LOGGER.info(
|
||||
"master maybe crash down, because lastMasterPongTime="
|
||||
+ DATE_FORMAT.format(lastMasterPongTime)
|
||||
+ "!");
|
||||
|
||||
// 向NC发通知重新选举master,如果NC没有收到所有节点的重选请求,就认为是这个节点和master连接断开
|
||||
// 这个节点需要重连master
|
||||
Map<String, String> request = new HashMap<>();
|
||||
request.put("action", "electMaster");
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
request.put("nodeID", keyPair.getPublicKeyStr());
|
||||
for (MultiContractMeta meta : CMActions.manager.multiContractRecorder.getStatus().values()) {
|
||||
if (meta.getMasterNode().equals(masterPubkey)) {
|
||||
LOGGER.info(
|
||||
"master of contract "
|
||||
+ meta.getContractID()
|
||||
+ " maybe crash down! master="
|
||||
+ masterPubkey);
|
||||
request.put("contractID", meta.getContractID());
|
||||
int lastSeq = meta.getLastExeSeq();
|
||||
request.put("lastExe", lastSeq + "");
|
||||
request.put("master", masterPubkey);
|
||||
request.put("members", meta.joinMembers(","));
|
||||
NetworkManager.instance.sendToNodeCenter(
|
||||
JsonUtil.toJson(request));
|
||||
MasterElectTimeRecorder.findMasterCrash =
|
||||
System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
NetworkManager.instance.closeAgent(masterPubkey);
|
||||
} // if
|
||||
}
|
||||
};
|
||||
NetworkManager.instance.closeAgent(masterPubkey);
|
||||
} // if
|
||||
}
|
||||
};
|
||||
lastMasterPongTime = System.currentTimeMillis();
|
||||
LOGGER.info(
|
||||
"lastMasterPongTime=" + DATE_FORMAT.format(lastMasterPongTime));
|
||||
LOGGER.info("lastMasterPongTime=" + DATE_FORMAT.format(lastMasterPongTime));
|
||||
HeartBeatUtil.getInstance().schedule(sendPingTask, sendDelay / 2, checkDelay);
|
||||
HeartBeatUtil.getInstance().schedule(checkAliveTask, sendDelay, checkDelay);
|
||||
}
|
||||
@ -138,7 +127,8 @@ public class AliveCheckClientAction {
|
||||
|
||||
public void waitForSetNodeID() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (waitForSetNode) return;
|
||||
if (waitForSetNode)
|
||||
return;
|
||||
try {
|
||||
Thread.sleep(30);
|
||||
} catch (InterruptedException e) {
|
||||
@ -150,9 +140,9 @@ public class AliveCheckClientAction {
|
||||
public void init() {
|
||||
LOGGER.info("init nodeID:");
|
||||
assert masterPubkey != null;
|
||||
NetworkManager.instance.sendToAgent(masterPubkey, "{\"action\":\"setNodeInfo\",\"pubKey\":\""
|
||||
+ GlobalConf.instance.keyPair.getPublicKeyStr()
|
||||
+ "\"}");
|
||||
NetworkManager.instance.sendToAgent(masterPubkey,
|
||||
"{\"action\":\"setNodeInfo\",\"pubKey\":\""
|
||||
+ GlobalConf.instance.keyPair.getPublicKeyStr() + "\"}");
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import org.bdware.server.action.Action;
|
||||
import org.bdware.server.tcp.TCPServerFrameHandler;
|
||||
import org.bdware.units.NetworkManager;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class AliveCheckServerAction {
|
||||
@ -23,18 +22,12 @@ public class AliveCheckServerAction {
|
||||
public String pubKey;
|
||||
|
||||
public AliveCheckServerAction(TCPServerFrameHandler handler) {
|
||||
lastSlavePingTime =
|
||||
System.currentTimeMillis()
|
||||
+ 5000L
|
||||
+ AliveCheckClientAction.sendDelay
|
||||
+ AliveCheckClientAction.checkDelay;
|
||||
lastSlavePingTime = System.currentTimeMillis() + 5000L + AliveCheckClientAction.sendDelay
|
||||
+ AliveCheckClientAction.checkDelay;
|
||||
|
||||
checkAliveTask = new HeartBeatTask(AliveCheckClientAction.sendDelay);
|
||||
HeartBeatUtil.getInstance()
|
||||
.schedule(
|
||||
checkAliveTask,
|
||||
AliveCheckClientAction.sendDelay,
|
||||
AliveCheckClientAction.checkDelay);
|
||||
HeartBeatUtil.getInstance().schedule(checkAliveTask, AliveCheckClientAction.sendDelay,
|
||||
AliveCheckClientAction.checkDelay);
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@ -57,10 +50,10 @@ public class AliveCheckServerAction {
|
||||
try {
|
||||
long cur = System.currentTimeMillis();
|
||||
if (cur - lastSlavePingTime >= (2L * delay)) {
|
||||
LOGGER.info(
|
||||
new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(lastSlavePingTime)
|
||||
+ " "
|
||||
+ delay);
|
||||
// LOGGER.info(
|
||||
// new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(lastSlavePingTime)
|
||||
// + " "
|
||||
// + delay);
|
||||
Set<String> contracts = new HashSet<>();
|
||||
String nodeID = pubKey;
|
||||
if (nodeID == null) {
|
||||
@ -68,21 +61,17 @@ public class AliveCheckServerAction {
|
||||
HeartBeatUtil.getInstance().cancel(this);
|
||||
return;
|
||||
}
|
||||
LOGGER.info(
|
||||
"Master心跳机制发现节点 "
|
||||
+ nodeID.substring(0, 5)
|
||||
+ " 下线! "
|
||||
+ this.toString());
|
||||
LOGGER.info("Master心跳机制发现节点 " + nodeID.substring(0, 5) + " 下线! "
|
||||
+ this.toString());
|
||||
HeartBeatUtil.getInstance().cancel(this);
|
||||
for (String contractID :
|
||||
MasterServerRecoverMechAction.recoverStatus.get(nodeID).keySet()) {
|
||||
// RecoverFlag flag =
|
||||
for (String contractID : MasterServerRecoverMechAction.recoverStatus.get(nodeID)
|
||||
.keySet()) {
|
||||
// RecoverFlag flag =
|
||||
//
|
||||
// MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID);
|
||||
// if (flag == RecoverFlag.Fine)
|
||||
MasterServerRecoverMechAction.recoverStatus
|
||||
.get(nodeID)
|
||||
.put(contractID, RecoverFlag.ToRecover);
|
||||
// if (flag == RecoverFlag.Fine)
|
||||
MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID,
|
||||
RecoverFlag.ToRecover);
|
||||
contracts.add(contractID);
|
||||
MasterServerTCPAction.notifyNodeOffline(contractID, nodeID);
|
||||
}
|
||||
|
@ -14,8 +14,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CommunicationAction implements OnHashCallback {
|
||||
public CommunicationAction() {
|
||||
}
|
||||
public CommunicationAction() {}
|
||||
|
||||
@Action(async = true)
|
||||
public void updatePeerID(JsonObject jsonObject, final ResultCallback callback) {
|
||||
@ -26,13 +25,14 @@ public class CommunicationAction implements OnHashCallback {
|
||||
@Action(async = true)
|
||||
public void updateContractID(JsonObject jsonObject, final ResultCallback callback) {
|
||||
ExecutionManager.instance.setNextContractID(jsonObject.get("content").getAsString());
|
||||
// System.out.println("ContractID updated " + ExecutionManager.instance.nextContractID);
|
||||
// System.out.println("ContractID updated " + ExecutionManager.instance.nextContractID);
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void onGetP2PNodes(JsonObject args, final ResultCallback rc) {
|
||||
String requestID = args.get(BaseFunctionManager.REQUEST_ID).getAsString();
|
||||
ResultCallback resultCallback = CommunicationManager.instance.requestCallbacks.remove(requestID);
|
||||
ResultCallback resultCallback =
|
||||
CommunicationManager.instance.requestCallbacks.remove(requestID);
|
||||
String allPeers;
|
||||
allPeers = args.get("content").getAsString();
|
||||
System.out.println("String coding " + allPeers);
|
||||
@ -46,6 +46,6 @@ public class CommunicationAction implements OnHashCallback {
|
||||
r.put("action", "onHashResult");
|
||||
r.put("responseID", reqID);
|
||||
r.put("data", hashStr);
|
||||
CommunicationManager.instance.send(JsonUtil.toJson(r), new String[]{});
|
||||
CommunicationManager.instance.send(JsonUtil.toJson(r), new String[] {});
|
||||
}
|
||||
}
|
||||
|
@ -19,8 +19,6 @@ import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.Action;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.RequestToMaster;
|
||||
import org.bdware.server.executor.consistency.PBFTExecutor;
|
||||
import org.bdware.server.executor.consistency.RequestAllExecutor;
|
||||
import org.bdware.server.tcp.TCPClientFrameHandler;
|
||||
import org.bdware.server.trustedmodel.AgentManager;
|
||||
import org.bdware.server.trustedmodel.ContractUnitStatus;
|
||||
@ -36,26 +34,24 @@ import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class MasterClientRecoverMechAction {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MasterClientRecoverMechAction.class);
|
||||
public static Set<String> recoverSet =
|
||||
new ConcurrentSkipListSet<>(); // contracts which don't finish recoverRestart
|
||||
public static Map<String, Queue<RequestToMaster>>
|
||||
requestsToMaster; // when master is re-electing,client node cache ites received requests
|
||||
public static Set<String> recoverSet = new ConcurrentSkipListSet<>(); // contracts which don't
|
||||
// finish recoverRestart
|
||||
public static Map<String, Queue<RequestToMaster>> requestsToMaster; // when master is
|
||||
// re-electing,client node
|
||||
// cache ites received
|
||||
// requests
|
||||
private final Map<String, OutputStream> stateFileMap = new HashMap<>();
|
||||
private final Map<String, OutputStream> transFileMap = new HashMap<>();
|
||||
|
||||
public MasterClientRecoverMechAction() {
|
||||
}
|
||||
public MasterClientRecoverMechAction() {}
|
||||
|
||||
// 告知master自己需要恢复,携带之前的运行模式,如果是StableMode需要携带lastExeSeq信息
|
||||
public static void askForRecover(String contractID, String masterID, String nodeID) {
|
||||
RecoverMechTimeRecorder.startRecover = System.currentTimeMillis();
|
||||
|
||||
// recoverSet.add(contractID);
|
||||
LOGGER.info(
|
||||
"[MasterClientRecoverMechAction] askForRecover contractID="
|
||||
+ contractID
|
||||
+ " masterID="
|
||||
+ masterID.substring(0, 5));
|
||||
// recoverSet.add(contractID);
|
||||
LOGGER.info("[MasterClientRecoverMechAction] askForRecover contractID=" + contractID
|
||||
+ " masterID=" + masterID.substring(0, 5));
|
||||
TCPClientFrameHandler master = AgentManager.getHandler(masterID);
|
||||
|
||||
Map<String, String> ret = new HashMap<String, String>();
|
||||
@ -63,27 +59,22 @@ public class MasterClientRecoverMechAction {
|
||||
ret.put("contractID", contractID);
|
||||
ret.put("nodeID", nodeID);
|
||||
|
||||
/* //没有这个合约进程
|
||||
if(!MasterClientTCPAction.requests.containsKey(contractID)){
|
||||
if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){
|
||||
logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq");
|
||||
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(),contractID);
|
||||
}
|
||||
}else{
|
||||
ContractClient client = CMActions.manager.getClient(contractID);
|
||||
ContractExecuteInfo cei = MasterClientTCPAction.requests.get(contractID);
|
||||
if (client == null || !client.getContractType().equals(cei.type)){
|
||||
if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){
|
||||
logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq");
|
||||
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(),contractID);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
/*
|
||||
* //没有这个合约进程 if(!MasterClientTCPAction.requests.containsKey(contractID)){
|
||||
* if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){
|
||||
* logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq");
|
||||
* KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(),contractID); } }else{
|
||||
* ContractClient client = CMActions.manager.getClient(contractID); ContractExecuteInfo cei
|
||||
* = MasterClientTCPAction.requests.get(contractID); if (client == null ||
|
||||
* !client.getContractType().equals(cei.type)){
|
||||
* if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){
|
||||
* logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq");
|
||||
* KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(),contractID); } } }
|
||||
*/
|
||||
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) {
|
||||
ret.put("mode", ContractUnitStatus.StableMode.toString());
|
||||
ret.put(
|
||||
"lastExeSeq",
|
||||
ret.put("lastExeSeq",
|
||||
KeyValueDBUtil.instance.getValue(CMTables.LastExeSeq.toString(), contractID));
|
||||
} else {
|
||||
ret.put("mode", ContractUnitStatus.CommonMode.toString());
|
||||
@ -100,20 +91,17 @@ public class MasterClientRecoverMechAction {
|
||||
public static void stableModeCheckpoint(String contractID) {
|
||||
LOGGER.info("设置合约" + contractID + " stable模式下的 checkpoint");
|
||||
final MultiContractMeta cei;
|
||||
synchronized (
|
||||
cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.LastExeSeq.toString(), contractID, cei.getLastExeSeq() + "");
|
||||
synchronized (cei =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)) {
|
||||
KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID,
|
||||
cei.getLastExeSeq() + "");
|
||||
|
||||
CMActions.manager.clearSyncFiles(contractID);
|
||||
|
||||
// TODO 将ContractExecuteInfo持久化
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
||||
String fileName =
|
||||
"ContractExecuteInfo_"
|
||||
+ format.format(new Date())
|
||||
+ "_"
|
||||
+ new Random().nextInt();
|
||||
String fileName = "ContractExecuteInfo_" + format.format(new Date()) + "_"
|
||||
+ new Random().nextInt();
|
||||
String parPath = GlobalConf.instance.ADSPDir + "/" + contractID + "/cr";
|
||||
File file = new File(parPath + "/" + fileName);
|
||||
try {
|
||||
@ -137,7 +125,8 @@ public class MasterClientRecoverMechAction {
|
||||
public void saveStateFile(String path, ContractRecord record) {
|
||||
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
ObjectOutputStream writer;
|
||||
try {
|
||||
FileOutputStream fileout = new FileOutputStream(file);
|
||||
@ -172,7 +161,8 @@ public class MasterClientRecoverMechAction {
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else fout = (FileOutputStream) stateFileMap.get(fileName);
|
||||
} else
|
||||
fout = (FileOutputStream) stateFileMap.get(fileName);
|
||||
|
||||
String data = args.get("data").getAsString();
|
||||
try {
|
||||
@ -207,27 +197,27 @@ public class MasterClientRecoverMechAction {
|
||||
recoverFromCommonMode(path, false, rc);
|
||||
}
|
||||
|
||||
// //无状态合约同步lastExeSeq之后发送恢复完成
|
||||
// public void statelessContract(String contractID){
|
||||
// logger.info("无状态合约请求unitLastExeSeq");
|
||||
// Map<String, String> ret = new HashMap<String, String>();
|
||||
// ret.put("action", "statelessUnitLastExe");
|
||||
// ret.put("contractID", contractID);
|
||||
// ret.put("nodeID",GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
// handler.sendMsg(JsonUtil.toJson(ret));
|
||||
// }
|
||||
// //无状态合约同步lastExeSeq之后发送恢复完成
|
||||
// public void statelessContract(String contractID){
|
||||
// logger.info("无状态合约请求unitLastExeSeq");
|
||||
// Map<String, String> ret = new HashMap<String, String>();
|
||||
// ret.put("action", "statelessUnitLastExe");
|
||||
// ret.put("contractID", contractID);
|
||||
// ret.put("nodeID",GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
// handler.sendMsg(JsonUtil.toJson(ret));
|
||||
// }
|
||||
//
|
||||
// @Action(async = true)
|
||||
// public void statelessReceiveLastExe(JsonObject args, final ResultCallback rc) {
|
||||
// String contractID = args.get("contractID").getAsString();
|
||||
// int unitLast = args.get("unitLastExe").getAsInt();
|
||||
// @Action(async = true)
|
||||
// public void statelessReceiveLastExe(JsonObject args, final ResultCallback rc) {
|
||||
// String contractID = args.get("contractID").getAsString();
|
||||
// int unitLast = args.get("unitLastExe").getAsInt();
|
||||
//
|
||||
// ContractExecuteInfo cei = MasterClientTCPAction.requests.get(contractID);
|
||||
// synchronized(cei){
|
||||
// cei.setLastExeSeq(unitLast);
|
||||
// }
|
||||
// sendRecoverFinish(contractID,"stateless");
|
||||
// }
|
||||
// ContractExecuteInfo cei = MasterClientTCPAction.requests.get(contractID);
|
||||
// synchronized(cei){
|
||||
// cei.setLastExeSeq(unitLast);
|
||||
// }
|
||||
// sendRecoverFinish(contractID,"stateless");
|
||||
// }
|
||||
|
||||
// 普通节点从CommonMode恢复
|
||||
public void recoverFromCommonMode(String path, boolean isMaster, ResultCallback rc) {
|
||||
@ -254,9 +244,8 @@ public class MasterClientRecoverMechAction {
|
||||
}
|
||||
// cei.printContent();
|
||||
if (cei.queue == null) {
|
||||
cei.init(
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(
|
||||
cei.getContractID()));
|
||||
cei.init(CMActions.manager.multiContractRecorder
|
||||
.getMultiContractMeta(cei.getContractID()));
|
||||
}
|
||||
recoverSet.add(cei.getContractID());
|
||||
|
||||
@ -266,12 +255,12 @@ public class MasterClientRecoverMechAction {
|
||||
return;
|
||||
}
|
||||
|
||||
// //无状态合约
|
||||
// String stateful = CMActions.manager.getContractStateful(cei.getContractID());
|
||||
// if(stateful.equals("false")){
|
||||
// statelessContract(cei.getContractID());
|
||||
// return;
|
||||
// }
|
||||
// //无状态合约
|
||||
// String stateful = CMActions.manager.getContractStateful(cei.getContractID());
|
||||
// if(stateful.equals("false")){
|
||||
// statelessContract(cei.getContractID());
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (isMaster) {
|
||||
CMActions.manager.setContractIsMaster(cei.getContractID(), "true");
|
||||
@ -300,12 +289,9 @@ public class MasterClientRecoverMechAction {
|
||||
try {
|
||||
LOGGER.info(" 开始等待 checkAndRestart锁 : ");
|
||||
time1 = System.currentTimeMillis();
|
||||
LOGGER.info(
|
||||
"time1="
|
||||
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
||||
.format(new Date(time1))
|
||||
+ " "
|
||||
+ time1);
|
||||
LOGGER.info("time1="
|
||||
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1))
|
||||
+ " " + time1);
|
||||
ContractManager.checkAndStartLock.wait(10000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
@ -314,10 +300,8 @@ public class MasterClientRecoverMechAction {
|
||||
|
||||
long time2 = System.currentTimeMillis();
|
||||
LOGGER.info(
|
||||
"time2="
|
||||
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time2))
|
||||
+ " "
|
||||
+ time2);
|
||||
"time2=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time2))
|
||||
+ " " + time2);
|
||||
LOGGER.info("等待 checkAndRestart锁 结束 ");
|
||||
if (time2 - time1 >= 10000) {
|
||||
LOGGER.info("[ADSP]CheckAndStart 锁 等待超时");
|
||||
@ -335,42 +319,27 @@ public class MasterClientRecoverMechAction {
|
||||
if (client == null) {
|
||||
System.out.println("恢复 checkAndRestart client == null\n");
|
||||
} else {
|
||||
System.out.println(
|
||||
"恢复 checkAndRestart client == null: false"
|
||||
+ " client.getContractType="
|
||||
+ client.getContractType()
|
||||
+ "\n");
|
||||
System.out.println("恢复 checkAndRestart client == null: false"
|
||||
+ " client.getContractType=" + client.getContractType() + "\n");
|
||||
}
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(cei.getContractID());
|
||||
meta.setContractExecutor(
|
||||
MasterClientTCPAction.createContractExecutor(meta.contract, contractID, cei.getMasterNode(), cei.getMembers()));
|
||||
switch (meta.contract.getType()) {
|
||||
case RequestAllResponseFirst:
|
||||
case RequestAllResponseHalf:
|
||||
case RequestAllResponseAll:
|
||||
((RequestAllExecutor) meta.contractExecutor).setSeq(cei.getLastExeSeq() + 1);
|
||||
break;
|
||||
case PBFT:
|
||||
((PBFTExecutor) meta.contractExecutor).setSeq(cei.getLastExeSeq() + 1);
|
||||
break;
|
||||
case Sharding:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
meta.setContractExecutor(MasterClientTCPAction.createContractExecutor(meta.contract,
|
||||
contractID, cei.getMasterNode(), cei.getMembers()));
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
args.put("ceiLastExeSeq", cei.getLastExeSeq());
|
||||
meta.contractExecutor.onRecover(args);
|
||||
// 认为contractID不会重
|
||||
if (client != null
|
||||
&& client.isProcessAlive()
|
||||
if (client != null && client.isProcessAlive()
|
||||
&& client.getContractType().equals(cei.type)) {
|
||||
LOGGER.info("恢复节点已有这个合约进程!");
|
||||
|
||||
if (!KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.UnitContracts.toString(), cei.getContractID())) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.UnitContracts.toString(), cei.getContractID(), "exist");
|
||||
if (!KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(),
|
||||
cei.getContractID())) {
|
||||
KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(),
|
||||
cei.getContractID(), "exist");
|
||||
}
|
||||
CMActions.manager.multiContractRecorder.updateValue(cei);
|
||||
//MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction);
|
||||
// MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction);
|
||||
CMActions.manager.setContractIsMaster(contractID, "false");
|
||||
} else {
|
||||
LOGGER.info("[MasterClientRecoverMechAction] 恢复节点没有这个合约进程 : ");
|
||||
@ -386,22 +355,18 @@ public class MasterClientRecoverMechAction {
|
||||
contract.setPublicKey(cei.publicKey);
|
||||
int copies = cei.getMembers().length;
|
||||
contract.setNumOfCopies(copies);
|
||||
if (!KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.UnitContracts.toString(), cei.getContractID())) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.UnitContracts.toString(), cei.getContractID(), "exist");
|
||||
if (!KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(),
|
||||
cei.getContractID())) {
|
||||
KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(),
|
||||
cei.getContractID(), "exist");
|
||||
}
|
||||
CMActions.manager.multiContractRecorder.updateValue(cei);
|
||||
//MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction);
|
||||
// MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction);
|
||||
parPath = GlobalConf.instance.publicCompiledDir + "/" + cei.getYpkName();
|
||||
|
||||
if (cei.isPrivate()) {
|
||||
parPath =
|
||||
GlobalConf.instance.privateCompiledDir
|
||||
+ "/"
|
||||
+ cei.getPubKeyPath()
|
||||
+ "/"
|
||||
+ cei.getYpkName();
|
||||
parPath = GlobalConf.instance.privateCompiledDir + "/" + cei.getPubKeyPath()
|
||||
+ "/" + cei.getYpkName();
|
||||
}
|
||||
}
|
||||
|
||||
@ -467,7 +432,8 @@ public class MasterClientRecoverMechAction {
|
||||
String path = contractID + "_temp_stateFile_" + new Random().nextLong();
|
||||
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
ObjectOutputStream writer;
|
||||
try {
|
||||
FileOutputStream fileout = new FileOutputStream(file);
|
||||
@ -504,7 +470,7 @@ public class MasterClientRecoverMechAction {
|
||||
LOGGER.info("times=" + times);
|
||||
int index = 1; // 数据包序号
|
||||
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0; ) {
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0;) {
|
||||
// logger.info("len = " + len);
|
||||
String data = ByteUtil.encodeBASE64(buff, len);
|
||||
req.put("data", data);
|
||||
@ -524,9 +490,9 @@ public class MasterClientRecoverMechAction {
|
||||
}
|
||||
fin.close();
|
||||
|
||||
// req.put("isDone", "true");
|
||||
// req.remove("data");
|
||||
// handler.sendMsg(JsonUtil.toJson(req));
|
||||
// req.put("isDone", "true");
|
||||
// req.remove("data");
|
||||
// handler.sendMsg(JsonUtil.toJson(req));
|
||||
|
||||
// delete state file
|
||||
if (file.isFile() && file.exists()) {
|
||||
@ -547,17 +513,12 @@ public class MasterClientRecoverMechAction {
|
||||
int unitLastExeSeq = json.get("unitLastExeSeq").getAsInt();
|
||||
recoverSet.add(contractID);
|
||||
// 查看自己和master的lastExeSeq差距
|
||||
int lastExeSeq =
|
||||
Integer.parseInt(
|
||||
KeyValueDBUtil.instance.getValue(
|
||||
CMTables.LastExeSeq.toString(), contractID));
|
||||
int lastExeSeq = Integer.parseInt(
|
||||
KeyValueDBUtil.instance.getValue(CMTables.LastExeSeq.toString(), contractID));
|
||||
|
||||
LOGGER.info("unitLastExeSeq=" + unitLastExeSeq + " lastExeSeq=" + lastExeSeq);
|
||||
LOGGER.info(
|
||||
"从StableMode恢复,采用先从本地load的方式 unitLastExeSeq="
|
||||
+ unitLastExeSeq
|
||||
+ " lastExeDeq="
|
||||
+ lastExeSeq);
|
||||
LOGGER.info("从StableMode恢复,采用先从本地load的方式 unitLastExeSeq=" + unitLastExeSeq
|
||||
+ " lastExeDeq=" + lastExeSeq);
|
||||
|
||||
// Step 1 查看最新的sync file
|
||||
String parPath = GlobalConf.instance.ADSPDir + "/" + contractID + "/sync";
|
||||
@ -613,50 +574,32 @@ public class MasterClientRecoverMechAction {
|
||||
}
|
||||
// record.printContent();
|
||||
if (cei.queue == null) {
|
||||
cei.init(
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(
|
||||
cei.getContractID()));
|
||||
cei.init(CMActions.manager.multiContractRecorder
|
||||
.getMultiContractMeta(cei.getContractID()));
|
||||
}
|
||||
CMActions.manager.multiContractRecorder.updateValue(cei);
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
switch (meta.contract.getType()) {
|
||||
case RequestAllResponseFirst:
|
||||
case RequestAllResponseHalf:
|
||||
case RequestAllResponseAll:
|
||||
((RequestAllExecutor) meta.contractExecutor).setSeq(cei.getLastExeSeq() + 1);
|
||||
break;
|
||||
case PBFT:
|
||||
((PBFTExecutor) meta.contractExecutor).setSeq(cei.getLastExeSeq() + 1);
|
||||
break;
|
||||
case Sharding:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
args.put("ceiLastExeSeq", cei.getLastExeSeq());
|
||||
meta.contractExecutor.onRecover(args);
|
||||
boolean flag = checkAndRestart(cei); // if need,restart the contract process
|
||||
|
||||
if (!flag) {
|
||||
return;
|
||||
}
|
||||
// 无状态合约
|
||||
// String stateful = CMActions.manager.getContractStateful(cei.getContractID());
|
||||
// if(stateful.equals("false")){
|
||||
// statelessContract(cei.getContractID());
|
||||
// return;
|
||||
// }
|
||||
// String stateful = CMActions.manager.getContractStateful(cei.getContractID());
|
||||
// if(stateful.equals("false")){
|
||||
// statelessContract(cei.getContractID());
|
||||
// return;
|
||||
// }
|
||||
|
||||
// step3 loadMemory
|
||||
// step3 loadMemory
|
||||
String memoryFileName = content[1].split(";")[1];
|
||||
ContractClient client = ContractManager.instance.getClient(cei.getContractID());
|
||||
|
||||
String temp1 =
|
||||
CMActions.manager.loadMemory(
|
||||
client,
|
||||
GlobalConf.instance.ADSPDir
|
||||
+ "/"
|
||||
+ contractID
|
||||
+ "/memory/"
|
||||
+ memoryFileName);
|
||||
String temp1 = CMActions.manager.loadMemory(client,
|
||||
GlobalConf.instance.ADSPDir + "/" + contractID + "/memory/" + memoryFileName);
|
||||
LOGGER.info("loadMemory结果" + temp1);
|
||||
// logger.info("测试位置 333 step3结束后状态为\n" + CMActions.manager.dumpContract(contractID, ""));
|
||||
|
||||
@ -664,10 +607,8 @@ public class MasterClientRecoverMechAction {
|
||||
|
||||
// step4 redo local trans
|
||||
String transFileName = content[2].split(";")[1];
|
||||
String temp =
|
||||
CMActions.manager.redo(
|
||||
contractID,
|
||||
GlobalConf.instance.ADSPDir + "/" + contractID + "/trans/" + transFileName);
|
||||
String temp = CMActions.manager.redo(contractID,
|
||||
GlobalConf.instance.ADSPDir + "/" + contractID + "/trans/" + transFileName);
|
||||
LOGGER.info("redo from local结果" + temp);
|
||||
synchronized (cei) {
|
||||
cei.setLastExeSeq(lastExeSeq); // 通过数据库中值设置lastExeSeq,和本地记录的ContractRecord是同步的
|
||||
@ -732,13 +673,12 @@ public class MasterClientRecoverMechAction {
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("action", "onQueryUnitStatus");
|
||||
LOGGER.info(
|
||||
"恢复节点查看自己的状态 "
|
||||
+ KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.LastExeSeq.toString(), contractID));
|
||||
LOGGER.info("恢复节点查看自己的状态 "
|
||||
+ KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID));
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID))
|
||||
jo.addProperty("mode", ContractUnitStatus.StableMode.toString());
|
||||
else jo.addProperty("mode", ContractUnitStatus.CommonMode.toString());
|
||||
else
|
||||
jo.addProperty("mode", ContractUnitStatus.CommonMode.toString());
|
||||
// jo.addProperty("mode",
|
||||
// KeyValueDBUtil.instance.getValue(CMTables.UnitStatus.toString(),contractID));
|
||||
jo.addProperty("nodeID", keyPair.getPublicKeyStr());
|
||||
@ -753,7 +693,8 @@ public class MasterClientRecoverMechAction {
|
||||
ContractUnitStatus cur_mode;
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID))
|
||||
cur_mode = ContractUnitStatus.StableMode;
|
||||
else cur_mode = ContractUnitStatus.CommonMode;
|
||||
else
|
||||
cur_mode = ContractUnitStatus.CommonMode;
|
||||
LOGGER.info("合约" + contractID + "当前模式为" + cur_mode + " 期望设置为" + mode);
|
||||
if (cur_mode == ContractUnitStatus.CommonMode
|
||||
&& mode.equals(ContractUnitStatus.StableMode.toString())) {
|
||||
@ -767,10 +708,10 @@ public class MasterClientRecoverMechAction {
|
||||
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID);
|
||||
}
|
||||
}
|
||||
// else if(cur_mode == ContractUnitStatus.StableMode &&
|
||||
// else if(cur_mode == ContractUnitStatus.StableMode &&
|
||||
// mode.equals(ContractUnitStatus.StableMode.toString())){
|
||||
// stableModeCheckpoint(contractID);
|
||||
// }
|
||||
// stableModeCheckpoint(contractID);
|
||||
// }
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
@ -803,7 +744,8 @@ public class MasterClientRecoverMechAction {
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else fout = (FileOutputStream) transFileMap.get(fileName);
|
||||
} else
|
||||
fout = (FileOutputStream) transFileMap.get(fileName);
|
||||
|
||||
String data = args.get("data").getAsString();
|
||||
try {
|
||||
|
@ -15,18 +15,12 @@ import org.bdware.sc.db.KeyValueDBUtil;
|
||||
import org.bdware.sc.units.MultiContractMeta;
|
||||
import org.bdware.sc.units.PubKeyNode;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.sdk.consistency.ConsistencyPluginManager;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.*;
|
||||
import org.bdware.server.executor.consistency.PBFTExecutor;
|
||||
import org.bdware.server.executor.consistency.RequestAllExecutor;
|
||||
import org.bdware.server.executor.unconsistency.MultiPointCooperationExecutor;
|
||||
import org.bdware.server.executor.unconsistency.RequestOnceExecutor;
|
||||
import org.bdware.server.executor.unconsistency.ResponseOnceExecutor;
|
||||
import org.bdware.server.tcp.PubkeyResultCallback;
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
import org.bdware.server.trustedmodel.KillUnitContractInfo;
|
||||
import org.bdware.server.trustedmodel.SelfAdaptiveShardingExecutor;
|
||||
import org.bdware.server.trustedmodel.SingleNodeExecutor;
|
||||
import org.bdware.units.NetworkManager;
|
||||
import org.bdware.units.function.ExecutionManager;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
@ -55,28 +49,24 @@ public class MasterClientTCPAction {
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
request.put("nodeID", keyPair.getPublicKeyStr());
|
||||
|
||||
// if (!contractID2MasterInfo.containsKey(contractID)) {
|
||||
// contractID = cc.getContractID();
|
||||
// }
|
||||
//
|
||||
// MasterClientTCPAction mcta = contractID2MasterInfo.get(contractID);
|
||||
// if (mcta != null) {
|
||||
// mcta.closeMaster();
|
||||
// }
|
||||
// if (!contractID2MasterInfo.containsKey(contractID)) {
|
||||
// contractID = cc.getContractID();
|
||||
// }
|
||||
//
|
||||
// MasterClientTCPAction mcta = contractID2MasterInfo.get(contractID);
|
||||
// if (mcta != null) {
|
||||
// mcta.closeMaster();
|
||||
// }
|
||||
|
||||
request.put("contractID", contractID);
|
||||
int lastSeq =
|
||||
CMActions.manager
|
||||
.multiContractRecorder
|
||||
.getMultiContractMeta(contractID)
|
||||
.getLastExeSeq();
|
||||
int lastSeq = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getLastExeSeq();
|
||||
request.put("lastExe", lastSeq + "");
|
||||
request.put("master", "null"); // 不管之前master是哪个,NC处是null,所有节点重选
|
||||
if (null != uniNumber) request.put("uniNumber", uniNumber);
|
||||
if (null != uniNumber)
|
||||
request.put("uniNumber", uniNumber);
|
||||
|
||||
String members = CMActions.manager
|
||||
.multiContractRecorder
|
||||
.getMultiContractMeta(contractID)
|
||||
String members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.joinMembers(",");
|
||||
|
||||
request.put("members", members);
|
||||
@ -84,47 +74,15 @@ public class MasterClientTCPAction {
|
||||
LOGGER.info("认为合约 " + contractID + " 的master崩溃 当前master为null 向NC发送重选信息");
|
||||
}
|
||||
|
||||
public static ContractExecutor createContractExecutor(Contract contract, String contractID, String masterPubkey, String[] members) {
|
||||
ContractExecutor executor = null;
|
||||
int nodeSize = contract.getNumOfCopies();
|
||||
switch (contract.getType()) {
|
||||
case Sole:
|
||||
LOGGER.info("Sole contract is not supported in multi-point mode");
|
||||
return SingleNodeExecutor.instance;
|
||||
case RequestOnce:
|
||||
executor = new RequestOnceExecutor(contractID);
|
||||
break;
|
||||
case ResponseOnce:
|
||||
executor = new ResponseOnceExecutor(contractID);
|
||||
break;
|
||||
case RequestAllResponseFirst:
|
||||
executor =
|
||||
new RequestAllExecutor(
|
||||
ContractExecType.RequestAllResponseFirst, 1, contractID);
|
||||
break;
|
||||
case RequestAllResponseHalf:
|
||||
executor =
|
||||
new RequestAllExecutor(
|
||||
ContractExecType.RequestAllResponseHalf,
|
||||
nodeSize / 2 + 1,
|
||||
contractID);
|
||||
break;
|
||||
case RequestAllResponseAll:
|
||||
executor =
|
||||
new RequestAllExecutor(
|
||||
ContractExecType.RequestAllResponseAll, nodeSize, contractID);
|
||||
break;
|
||||
case Sharding:
|
||||
executor = new MultiPointCooperationExecutor(ContractExecType.Sharding, nodeSize, contractID);
|
||||
break;
|
||||
case SelfAdaptiveSharding:
|
||||
executor = new SelfAdaptiveShardingExecutor(contractID);
|
||||
break;
|
||||
case PBFT:
|
||||
executor = new PBFTExecutor(nodeSize, contractID, masterPubkey, members);
|
||||
break;
|
||||
}
|
||||
return executor;
|
||||
public static ContractExecutor createContractExecutor(Contract contract, String contractID,
|
||||
String masterPubkey, String[] members) {
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
args.put("contractID", contractID);
|
||||
args.put("nodeSize", contract.getNumOfCopies());
|
||||
args.put("masterPubkey", masterPubkey);
|
||||
args.put("members", members);
|
||||
return ConsistencyPluginManager.getInstance()
|
||||
.createContractExecutor(contract.getType().name(), args);
|
||||
}
|
||||
|
||||
public static void dealRequests(String contractID) {
|
||||
@ -182,13 +140,13 @@ public class MasterClientTCPAction {
|
||||
LOGGER.info("返回 uniID=" + cei.uniReqIDMap.get(seq) + " 的结果");
|
||||
cei.resultMap.get(seq).onResult(JsonUtil.toJson(ret));
|
||||
cei.setLastExeSeq(seq);
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.LastExeSeq.toString(), contractID)) {
|
||||
KeyValueDBUtil.instance.setValue(
|
||||
CMTables.LastExeSeq.toString(), contractID, seq + "");
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
|
||||
contractID)) {
|
||||
KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID,
|
||||
seq + "");
|
||||
}
|
||||
|
||||
// ledger检查点 public static final int LEDGER_PERIOD = 100; //账本检查点
|
||||
// ledger检查点 public static final int LEDGER_PERIOD = 100; //账本检查点
|
||||
if (seq % 100 == 0) {
|
||||
LOGGER.info("遇到ledger检查点 seq=" + cei.getLastExeSeq());
|
||||
boolean isMaster = CMActions.manager.getContractIsMaster(contractID);
|
||||
@ -196,9 +154,8 @@ public class MasterClientTCPAction {
|
||||
String lastHash;
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.CheckPointLastHash.toString(), contractID)) {
|
||||
lastHash =
|
||||
KeyValueDBUtil.instance.getValue(
|
||||
CMTables.CheckPointLastHash.toString(), contractID);
|
||||
lastHash = KeyValueDBUtil.instance.getValue(
|
||||
CMTables.CheckPointLastHash.toString(), contractID);
|
||||
} else {
|
||||
lastHash = "firstCheckPoint";
|
||||
}
|
||||
@ -234,10 +191,8 @@ public class MasterClientTCPAction {
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
ret.put("responseID", responseID);
|
||||
ContractResult cr =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive(ComponedContractResult.EXPIRED_REQ));
|
||||
ContractResult cr = new ContractResult(ContractResult.Status.Error,
|
||||
new JsonPrimitive(ComponedContractResult.EXPIRED_REQ));
|
||||
ret.put("data", JsonUtil.toJson(cr));
|
||||
cb.onResult(JsonUtil.toJson(ret));
|
||||
}
|
||||
@ -268,8 +223,7 @@ public class MasterClientTCPAction {
|
||||
public void onKillContractProcess(JsonObject jo, ResultCallback result) {
|
||||
LOGGER.info("[MasterClientTCPAction] : onKillContractProcess");
|
||||
if (killUnitContractMap.containsKey(jo.get("requestID").getAsString())) {
|
||||
KillUnitContractInfo info =
|
||||
killUnitContractMap.get(jo.get("requestID").getAsString());
|
||||
KillUnitContractInfo info = killUnitContractMap.get(jo.get("requestID").getAsString());
|
||||
Map<String, Object> r = new HashMap<>();
|
||||
r.put("action", "onKillContractProcess");
|
||||
r.put("data", jo.get("data"));
|
||||
@ -291,12 +245,12 @@ public class MasterClientTCPAction {
|
||||
ContractClient cc = CMActions.manager.getClient(id);
|
||||
|
||||
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.LastExeSeq.toString(), cc.getContractID())) {
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
|
||||
cc.getContractID())) {
|
||||
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), cc.getContractID());
|
||||
}
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.UnitContracts.toString(), cc.getContractID())) {
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(),
|
||||
cc.getContractID())) {
|
||||
KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), cc.getContractID());
|
||||
}
|
||||
File file = new File(GlobalConf.instance.ADSPDir + "/" + cc.getContractID());
|
||||
@ -336,11 +290,11 @@ public class MasterClientTCPAction {
|
||||
// 需要计算出自己的ShardingID?路由规则(id/requester/arg-->shardingId),
|
||||
// 也在MultiPointCooperationExecutor中实现
|
||||
}
|
||||
//TODO master连接
|
||||
// TODO master连接
|
||||
// contractID2MasterInfo.put(contractID, this); // 记录contractID 和 master之间的对应关系
|
||||
MultiContractMeta cei =
|
||||
MultiContractMeta multiContractMeta =
|
||||
CMActions.manager.multiContractRecorder.createIfNotExist(contractID);
|
||||
cei.setLastExeSeq(-1);
|
||||
multiContractMeta.setLastExeSeq(-1);
|
||||
if (!contract.getScriptStr().startsWith("/")) {
|
||||
contract.setScript(dumpToDisk(contract, jo));
|
||||
}
|
||||
@ -348,33 +302,31 @@ public class MasterClientTCPAction {
|
||||
String[] pp = contract.getScriptStr().split("/");
|
||||
|
||||
String parPath = GlobalConf.instance.publicCompiledDir;
|
||||
synchronized (cei) { // 合约执行信息
|
||||
cei.setYpkName(pp[pp.length - 1]);
|
||||
cei.setMembers(jo.get("members").getAsJsonArray());
|
||||
cei.type = contract.getType();
|
||||
cei.key = contract.getKey();
|
||||
cei.publicKey = contract.getPublicKey();
|
||||
// TODO 第一次来的meta没有cid!!!
|
||||
synchronized (multiContractMeta) { // 合约执行信息
|
||||
multiContractMeta.setYpkName(pp[pp.length - 1]);
|
||||
multiContractMeta.setMembers(jo.get("members").getAsJsonArray());
|
||||
multiContractMeta.type = contract.getType();
|
||||
multiContractMeta.key = contract.getKey();
|
||||
multiContractMeta.publicKey = contract.getPublicKey();
|
||||
if (jo.has("isPrivate") && jo.get("isPrivate").getAsBoolean()) {
|
||||
parPath =
|
||||
GlobalConf.instance.privateCompiledDir
|
||||
+ "/"
|
||||
+ jo.get("pubKey").getAsString();
|
||||
parPath = GlobalConf.instance.privateCompiledDir + "/"
|
||||
+ jo.get("pubKey").getAsString();
|
||||
|
||||
cei.setIsPrivate(true);
|
||||
cei.setPubKeyPath(jo.get("pubKey").getAsString());
|
||||
cei.setYpkName(pp[pp.length - 1]);
|
||||
multiContractMeta.setIsPrivate(true);
|
||||
multiContractMeta.setPubKeyPath(jo.get("pubKey").getAsString());
|
||||
multiContractMeta.setYpkName(pp[pp.length - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
File temp = new File(parPath, pp[pp.length - 1]);
|
||||
if (!temp.exists()) {
|
||||
result.onResult(
|
||||
String.format(
|
||||
"{\"action\":\"onStartContractTrustfully\",\"result\":\"missing contract files\"," +
|
||||
"\"requestID\":\"%s\",\"pubKey\":\"%s\"}",
|
||||
jo.get("requestID").getAsString(),
|
||||
GlobalConf.instance.keyPair.getPublicKeyStr()));
|
||||
result.onResult(String.format(
|
||||
"{\"action\":\"onStartContractTrustfully\",\"result\":\"missing contract files\","
|
||||
+ "\"requestID\":\"%s\",\"pubKey\":\"%s\"}",
|
||||
jo.get("requestID").getAsString(),
|
||||
GlobalConf.instance.keyPair.getPublicKeyStr()));
|
||||
return;
|
||||
}
|
||||
contract.setScript(temp.getAbsolutePath());
|
||||
@ -382,31 +334,30 @@ public class MasterClientTCPAction {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 这个地方判定,从参数中的master数据 和 globalConf中的数据 进行对比,如果一致的话,说明该节点为master
|
||||
cei.setMaster(jo.get("master").getAsString());
|
||||
multiContractMeta.setMaster(jo.get("master").getAsString());
|
||||
if (contract.getType().needSeq())
|
||||
cei.setIsMaster(GlobalConf.getNodeID().equals(jo.get("master").getAsString()));
|
||||
multiContractMeta
|
||||
.setIsMaster(GlobalConf.getNodeID().equals(jo.get("master").getAsString()));
|
||||
else {
|
||||
cei.setIsMaster(true);
|
||||
multiContractMeta.setIsMaster(true);
|
||||
}
|
||||
|
||||
LOGGER.debug("startup arguments: " + JsonUtil.toJson(contract));
|
||||
CMActions.manager.multiContractRecorder.updateValue(multiContractMeta);
|
||||
LOGGER.info("startup arguments: " + JsonUtil.toJson(contract));
|
||||
String ret = CMActions.manager.startContract(contract); // 调用CMActions 里的启动合约的方法,启动结果
|
||||
|
||||
LOGGER.debug("startup result: " + ret);
|
||||
CMActions.manager.multiContractRecorder.updateValue(cei);
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.createIfNotExist(contractID);
|
||||
|
||||
meta.setContractExecutor(createContractExecutor(contract, contractID, jo.get("master").getAsString(), cei.getMembers())); // 分配不同的Executor
|
||||
// TODO 合约终止后从数据库中移除,但是为了测试可以人为制造合约终止但不从数据库中移除(异常停止)
|
||||
LOGGER.info("startup result: " + ret);
|
||||
// IMPORTANT!!!!!!!!!!
|
||||
// meta should get after manger.startContract, because startContract reInitilized it!
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
// IMPORTANT!!!!!!!!!!
|
||||
meta.setContractExecutor(createContractExecutor(contract, contractID,
|
||||
jo.get("master").getAsString(), multiContractMeta.getMembers())); // 分配不同的Executor
|
||||
// TODO 合约终止后从数据库中移除,但是为了测试可以人为制造合约终止但不从数据库中移除(异常停止)
|
||||
KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(), contractID, "exist");
|
||||
|
||||
if (CMActions.manager.getClient(contract.getID()) != null) {
|
||||
result.onResult(
|
||||
"{\"action\":\"onStartContractTrustfully\",\"result\":\"success\",\"requestID\":\""
|
||||
+ jo.get("requestID").getAsString()
|
||||
+ "\",\"pubKey\":\""
|
||||
+ GlobalConf.instance.keyPair.getPublicKeyStr()
|
||||
+ "\"}");
|
||||
+ jo.get("requestID").getAsString() + "\",\"pubKey\":\""
|
||||
+ GlobalConf.instance.keyPair.getPublicKeyStr() + "\"}");
|
||||
ExecutionManager.instance.updateLocalContractToNodeCenter();
|
||||
} else {
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
@ -417,11 +368,11 @@ public class MasterClientTCPAction {
|
||||
resultMap.put("pubKey", GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
result.onResult(JsonUtil.toJson(resultMap));
|
||||
}
|
||||
// if (contract.getType() == ContractExecType.Sharding) {
|
||||
// for (String str : cei.getMembers()) {
|
||||
// NetworkManager.instance.getNCClientHandler().controller.connectToMaster(str, null);
|
||||
// }
|
||||
// }
|
||||
// if (contract.getType() == ContractExecType.Sharding) {
|
||||
// for (String str : cei.getMembers()) {
|
||||
// NetworkManager.instance.getNCClientHandler().controller.connectToMaster(str, null);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
private String dumpToDisk(Contract contract, JsonObject jo) {
|
||||
@ -429,9 +380,8 @@ public class MasterClientTCPAction {
|
||||
String.format("Script_%s", Math.abs(contract.getScriptStr().hashCode()));
|
||||
try {
|
||||
jo.addProperty("isPrivate", true);
|
||||
File scriptDir =
|
||||
new File(
|
||||
GlobalConf.instance.privateCompiledDir, jo.get("pubKey").getAsString());
|
||||
File scriptDir = new File(GlobalConf.instance.privateCompiledDir,
|
||||
jo.get("pubKey").getAsString());
|
||||
if (!scriptDir.exists()) {
|
||||
scriptDir.mkdirs();
|
||||
}
|
||||
@ -454,14 +404,14 @@ public class MasterClientTCPAction {
|
||||
return "/" + scriptName + ".ypk";
|
||||
}
|
||||
|
||||
//TODO 这个奇怪的action可以放到这个SelfAdaptiveSharding的相关类里
|
||||
// TODO 这个奇怪的action可以放到这个SelfAdaptiveSharding的相关类里
|
||||
@Action(async = true)
|
||||
public void deliverBlock(JsonObject jo, ResultCallback result) {
|
||||
if (jo.has("contractID") && jo.has("data")) {
|
||||
String contractID = jo.get("contractID").getAsString();
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
if (null != meta && meta.contractExecutor instanceof SelfAdaptiveShardingExecutor) {
|
||||
((SelfAdaptiveShardingExecutor) meta.contractExecutor).execute(jo.get("data").getAsString());
|
||||
if (null != meta && meta.contractExecutor != null) {
|
||||
meta.contractExecutor.onDeliverBlock(jo.get("data").getAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -470,15 +420,10 @@ public class MasterClientTCPAction {
|
||||
public void executeContractLocally(JsonObject jo, ResultCallback result) {
|
||||
final ContractRequest request =
|
||||
JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class);
|
||||
LOGGER.info("[MasterCientTCPAction] executeContractLocally " + JsonUtil.toJson(jo));
|
||||
// LOGGER.info("[MasterCientTCPAction] executeContractLocally " + JsonUtil.toJson(jo));
|
||||
|
||||
LOGGER.info(
|
||||
"[MasterClient] executeLocally,uniReq:"
|
||||
+ jo.get("uniReqID").getAsString()
|
||||
+ " ->reqID:"
|
||||
+ request.getRequestID()
|
||||
+ " "
|
||||
+ jo.get("data").toString());
|
||||
// LOGGER.info("[MasterClient] executeLocally,uniReq:" + jo.get("uniReqID").getAsString() +
|
||||
// " ->reqID:" + request.getRequestID() + " " + jo.get("data").toString());
|
||||
|
||||
// requestOne and responseOne do not need sequencing
|
||||
if (!request.needSeq) {
|
||||
@ -505,8 +450,8 @@ public class MasterClientTCPAction {
|
||||
boolean isFirst = false;
|
||||
synchronized (MultiRequestInfo.lock) { // 加锁,防止同时放入多个同requestID的请求到cei的队列中
|
||||
if (MultiRequestInfo.reqInfos.containsKey(request.getRequestID())) {
|
||||
// logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" +
|
||||
// cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + "
|
||||
// logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" +
|
||||
// cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + "
|
||||
// 非第一个请求");
|
||||
|
||||
MultiRequestInfo mri =
|
||||
@ -521,8 +466,8 @@ public class MasterClientTCPAction {
|
||||
mri.putFlag(jo.get("uniReqID").getAsString(), false);
|
||||
}
|
||||
} else {
|
||||
// logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" +
|
||||
// cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + "
|
||||
// logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" +
|
||||
// cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + "
|
||||
// 是第一个请求");
|
||||
MultiRequestInfo mri =
|
||||
new MultiRequestInfo(request.getRequestID(), request.seq);
|
||||
@ -540,13 +485,8 @@ public class MasterClientTCPAction {
|
||||
}
|
||||
} else {
|
||||
synchronized (cei) {
|
||||
LOGGER.info(
|
||||
"合约"
|
||||
+ contractID
|
||||
+ " 请求序号为 "
|
||||
+ request.seq
|
||||
+ " 上一次执行请求序号为 "
|
||||
+ cei.getLastExeSeq());
|
||||
LOGGER.info("合约" + contractID + " 请求序号为 " + request.seq + " 上一次执行请求序号为 "
|
||||
+ cei.getLastExeSeq());
|
||||
// cei.printContent();
|
||||
|
||||
if (request.seq <= cei.getLastExeSeq()) {
|
||||
@ -579,15 +519,17 @@ public class MasterClientTCPAction {
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void receiveContractExecutionServer(JsonObject jsonObject, ResultCallback resultCallback) {
|
||||
MasterServerTCPAction.sync.wakeUp(
|
||||
jsonObject.get("responseID").getAsString(), jsonObject.get("data").getAsString());
|
||||
public void receiveContractExecutionServer(JsonObject jsonObject,
|
||||
ResultCallback resultCallback) {
|
||||
MasterServerTCPAction.sync.wakeUp(jsonObject.get("responseID").getAsString(),
|
||||
jsonObject.get("data").getAsString());
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void contractSyncMessage(JsonObject jsonObject, ResultCallback resultCallback) {
|
||||
String contractID = jsonObject.get("contractID").getAsString();
|
||||
MultiContractMeta mcm = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
MultiContractMeta mcm =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
byte[] data = ByteUtil.decodeBASE64(jsonObject.get("data").getAsString());
|
||||
PubKeyNode node = new PubKeyNode();
|
||||
@ -601,8 +543,7 @@ public class MasterClientTCPAction {
|
||||
@Action(async = true)
|
||||
public void reRouteContract(JsonObject jo, ResultCallback result) {
|
||||
LOGGER.info("Receive Reroute Info:" + jo.toString());
|
||||
MasterServerTCPAction.sync.instrumentWakeUp(
|
||||
jo.get("responseID").getAsString(),
|
||||
MasterServerTCPAction.sync.instrumentWakeUp(jo.get("responseID").getAsString(),
|
||||
(resultCallback, result1) -> {
|
||||
resultCallback.cancelTimeOut();
|
||||
LOGGER.info("try To reRoute");
|
||||
@ -613,67 +554,58 @@ public class MasterClientTCPAction {
|
||||
(SyncResult.ContractResultCallback) resultCallback;
|
||||
if (cb.getReRouteCount() > 0) {
|
||||
ContractResult contractResult =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new ContractResult(ContractResult.Status.Error,
|
||||
new JsonPrimitive(
|
||||
"Contract "
|
||||
+ cr.get("contractID").getAsString()
|
||||
"Contract " + cr.get("contractID").getAsString()
|
||||
+ "can't be located in router"));
|
||||
resultCallback.onResult(JsonUtil.toJson(contractResult));
|
||||
return;
|
||||
} else cb.incReRouteCount();
|
||||
} else
|
||||
cb.incReRouteCount();
|
||||
LOGGER.info("inc reRoute:" + cb.getReRouteCount());
|
||||
}
|
||||
String pubkey =
|
||||
CMActions.manager.nodeCenterConn.reRouteContract(
|
||||
cr.get("contractID").getAsString());
|
||||
LOGGER.info(
|
||||
"ReRoute Result:"
|
||||
+ cr.get("contractID").getAsString()
|
||||
+ " pubKey:"
|
||||
+ pubkey);
|
||||
String pubkey = CMActions.manager.nodeCenterConn
|
||||
.reRouteContract(cr.get("contractID").getAsString());
|
||||
LOGGER.info("ReRoute Result:" + cr.get("contractID").getAsString() + " pubKey:"
|
||||
+ pubkey);
|
||||
|
||||
if (pubkey == null) {
|
||||
ContractResult contractResult =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive(
|
||||
"Contract "
|
||||
+ cr.get("contractID").getAsString()
|
||||
+ "can't be located in router using reroute"));
|
||||
new ContractResult(ContractResult.Status.Error,
|
||||
new JsonPrimitive("Contract "
|
||||
+ cr.get("contractID").getAsString()
|
||||
+ "can't be located in router using reroute"));
|
||||
resultCallback.onResult(JsonUtil.toJson(contractResult));
|
||||
return;
|
||||
}
|
||||
LOGGER.debug("Receive Reroute Result:" + pubkey);
|
||||
ContractRequest contractRequest =
|
||||
JsonUtil.fromJson(cr, ContractRequest.class);
|
||||
CMActions.manager.masterStub.executeByOtherNodeAsync(
|
||||
pubkey, contractRequest, resultCallback);
|
||||
},
|
||||
jo);
|
||||
ContractRequest contractRequest = JsonUtil.fromJson(cr, ContractRequest.class);
|
||||
CMActions.manager.masterStub.executeByOtherNodeAsync(pubkey, contractRequest,
|
||||
resultCallback);
|
||||
}, jo);
|
||||
}
|
||||
|
||||
// public String requestContractExecution(ContractRequest c) {
|
||||
// try {
|
||||
// LOGGER.info("[MasterClientTCPAction] requestContractExecution " + JsonUtil.toJson(c));
|
||||
// Map<String, Object> req = new HashMap<>();
|
||||
// req.put("action", "requestContractExecution");
|
||||
// req.put("requestID", c.getRequestID());
|
||||
// req.put("data", c);
|
||||
// handler.sendMsg(JsonUtil.toJson(req));
|
||||
// // 这里可能出错。是不是在这里校验CollectResult?
|
||||
// ContractResult str = MasterServerTCPAction.sync.syncSleep(c.getRequestID());
|
||||
// LOGGER.info("[RequestContractGet]" + JsonUtil.toJson(str));
|
||||
// return JsonUtil.toJson(str);
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// ContractResult cr =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Exception, new JsonPrimitive(bo.toString()));
|
||||
// return JsonUtil.toJson(cr);
|
||||
// }
|
||||
// }
|
||||
// public String requestContractExecution(ContractRequest c) {
|
||||
// try {
|
||||
// LOGGER.info("[MasterClientTCPAction] requestContractExecution " + JsonUtil.toJson(c));
|
||||
// Map<String, Object> req = new HashMap<>();
|
||||
// req.put("action", "requestContractExecution");
|
||||
// req.put("requestID", c.getRequestID());
|
||||
// req.put("data", c);
|
||||
// handler.sendMsg(JsonUtil.toJson(req));
|
||||
// // 这里可能出错。是不是在这里校验CollectResult?
|
||||
// ContractResult str = MasterServerTCPAction.sync.syncSleep(c.getRequestID());
|
||||
// LOGGER.info("[RequestContractGet]" + JsonUtil.toJson(str));
|
||||
// return JsonUtil.toJson(str);
|
||||
// } catch (Exception e) {
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// ContractResult cr =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Exception, new JsonPrimitive(bo.toString()));
|
||||
// return JsonUtil.toJson(cr);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
@ -25,15 +25,14 @@ public class MasterClientTransferAction {
|
||||
|
||||
private final Map<String, String> id2Memory = new HashMap<>();
|
||||
|
||||
private MasterClientTransferAction() {
|
||||
}
|
||||
private MasterClientTransferAction() {}
|
||||
|
||||
public void transferInstance(String agentPubkey, String contractID) {
|
||||
LOGGER.info("transferInstance contractID=" + contractID);
|
||||
//step2 save state
|
||||
// step2 save state
|
||||
String mem = CMActions.manager.dumpContract(contractID, "");
|
||||
id2Memory.put(contractID, mem);
|
||||
//step3 send ypk or script and start other,kill local
|
||||
// step3 send ypk or script and start other,kill local
|
||||
sendAndStart(agentPubkey, contractID);
|
||||
}
|
||||
|
||||
@ -48,7 +47,7 @@ public class MasterClientTransferAction {
|
||||
String script = cc.contractMeta.contract.getScriptStr();
|
||||
ContractStartInfo csi = cc.contractMeta.contract.startInfo;
|
||||
|
||||
//kill
|
||||
// kill
|
||||
LOGGER.info("sendAndRestart stopContract");
|
||||
CMActions.manager.stopContract(contractID);
|
||||
|
||||
@ -66,7 +65,8 @@ public class MasterClientTransferAction {
|
||||
File file;
|
||||
String path;
|
||||
if (csi.isPrivate) {
|
||||
file = new File(GlobalConf.instance.privateCompiledDir + "/" + csi.pubKeyPath + "/" + csi.ypkName);
|
||||
file = new File(GlobalConf.instance.privateCompiledDir + "/" + csi.pubKeyPath + "/"
|
||||
+ csi.ypkName);
|
||||
path = csi.pubKeyPath + "/" + csi.ypkName;
|
||||
} else {
|
||||
file = new File(GlobalConf.instance.publicCompiledDir + "/" + csi.ypkName);
|
||||
@ -90,7 +90,7 @@ public class MasterClientTransferAction {
|
||||
long count = 0;
|
||||
long total = file.length();
|
||||
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0; ) {
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0;) {
|
||||
String data = ByteUtil.encodeBASE64(buff, len);
|
||||
req.put("data", data);
|
||||
count += len;
|
||||
@ -116,7 +116,7 @@ public class MasterClientTransferAction {
|
||||
|
||||
@Action(async = true)
|
||||
public void onSendAndStart(JsonObject jo, ResultCallback result) {
|
||||
//step4 send memory
|
||||
// step4 send memory
|
||||
String contractID = jo.get("contractID").getAsString();
|
||||
LOGGER.info("onSendAndStart contractID=" + contractID);
|
||||
String mem = id2Memory.get(contractID);
|
||||
@ -130,11 +130,13 @@ public class MasterClientTransferAction {
|
||||
public boolean sendMemory(String contractID, String memory, ResultCallback cb) {
|
||||
LOGGER.info("sendMemory contractID=" + contractID);
|
||||
|
||||
String path = contractID + "_temp_stateFile_" + new Random().nextLong() + "_" + System.currentTimeMillis();
|
||||
String path = contractID + "_temp_stateFile_" + new Random().nextLong() + "_"
|
||||
+ System.currentTimeMillis();
|
||||
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
|
||||
LOGGER.info(file.getAbsolutePath());
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
ObjectOutputStream writer;
|
||||
try {
|
||||
FileOutputStream fileout = new FileOutputStream(file);
|
||||
@ -160,7 +162,7 @@ public class MasterClientTransferAction {
|
||||
byte[] buff = new byte[30 * 1024];
|
||||
long count = 0;
|
||||
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0; ) {
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0;) {
|
||||
String data = ByteUtil.encodeBASE64(buff, len);
|
||||
req.put("data", data);
|
||||
count += len;
|
||||
@ -175,7 +177,7 @@ public class MasterClientTransferAction {
|
||||
req.put("contractID", contractID);
|
||||
req.remove("data");
|
||||
cb.onResult(JsonUtil.toJson(req));
|
||||
//delete state file
|
||||
// delete state file
|
||||
if (file.isFile() && file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
@ -190,16 +192,16 @@ public class MasterClientTransferAction {
|
||||
|
||||
@Action(async = true)
|
||||
public void onSendMemory(JsonObject jo, ResultCallback result) {
|
||||
//step5 close connect
|
||||
// step5 close connect
|
||||
LOGGER.info("onSendMemory done");
|
||||
// if (handler != null) {
|
||||
// LOGGER.info("handler close");
|
||||
// NetworkManager.instance.closeAgent(master);
|
||||
//
|
||||
// }
|
||||
// if (NetworkManager.CONNECTORS.containsKey(master)) {
|
||||
// NetworkManager.CONNECTORS.remove(master);
|
||||
// }
|
||||
// if (handler != null) {
|
||||
// LOGGER.info("handler close");
|
||||
// NetworkManager.instance.closeAgent(master);
|
||||
//
|
||||
// }
|
||||
// if (NetworkManager.CONNECTORS.containsKey(master)) {
|
||||
// NetworkManager.CONNECTORS.remove(master);
|
||||
// }
|
||||
LOGGER.info("transfer contract instance finished.");
|
||||
}
|
||||
}
|
||||
|
@ -16,8 +16,6 @@ import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.Action;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.executor.consistency.PBFTExecutor;
|
||||
import org.bdware.server.executor.consistency.RequestAllExecutor;
|
||||
import org.bdware.server.trustedmodel.ContractUnitStatus;
|
||||
import org.bdware.units.NetworkManager;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
@ -32,8 +30,7 @@ public class MasterServerRecoverMechAction {
|
||||
public static Map<String, Map<String, RecoverFlag>> recoverStatus = new ConcurrentHashMap<>();
|
||||
private final Map<String, OutputStream> stateFileMap = new HashMap<>();
|
||||
|
||||
public MasterServerRecoverMechAction() {
|
||||
}
|
||||
public MasterServerRecoverMechAction() {}
|
||||
|
||||
// 从disk-durable恢复
|
||||
public static void restartContractFromStableMode(String nodeID, String contractID) {
|
||||
@ -48,8 +45,8 @@ public class MasterServerRecoverMechAction {
|
||||
Map<String, String> request = new HashMap<String, String>();
|
||||
request.put("action", "recoverFromStableMode");
|
||||
request.put("contractID", contractID);
|
||||
int temp =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getCurSeqAtMaster();
|
||||
int temp = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getCurSeqAtMaster();
|
||||
request.put("unitLastExeSeq", temp + "");
|
||||
NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(request));
|
||||
}
|
||||
@ -83,15 +80,15 @@ public class MasterServerRecoverMechAction {
|
||||
RecoverMechTimeRecorder.writeCEIStart.put(nodeID, System.currentTimeMillis());
|
||||
|
||||
MultiContractMeta cei =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(
|
||||
contractID); // master的server直接调client端,因为master自己的client端就在自己本地
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); // master的server直接调client端,因为master自己的client端就在自己本地
|
||||
cei.memory = CMActions.manager.dumpContract(contractID, "");
|
||||
// cei.printContent();
|
||||
|
||||
String path = contractID + "_temp_stateFile_" + new Random().nextLong();
|
||||
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
ObjectOutputStream writer;
|
||||
try {
|
||||
FileOutputStream fileout = new FileOutputStream(file);
|
||||
@ -127,7 +124,7 @@ public class MasterServerRecoverMechAction {
|
||||
times++;
|
||||
LOGGER.info("times=" + times);
|
||||
int index = 1; // 数据包序号
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0; ) {
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0;) {
|
||||
// logger.info("len = " + len);
|
||||
String data = ByteUtil.encodeBASE64(buff, len);
|
||||
req.put("data", data);
|
||||
@ -145,10 +142,10 @@ public class MasterServerRecoverMechAction {
|
||||
// Thread.sleep(300);
|
||||
}
|
||||
fin.close();
|
||||
// req.put("isDone", "true");
|
||||
// req.remove("data");
|
||||
// req.put("isDone", "true");
|
||||
// req.remove("data");
|
||||
//
|
||||
// node.connection.sendMsg(JsonUtil.toJson(req));
|
||||
// node.connection.sendMsg(JsonUtil.toJson(req));
|
||||
|
||||
// master delete state file
|
||||
if (file.isFile() && file.exists()) {
|
||||
@ -165,8 +162,8 @@ public class MasterServerRecoverMechAction {
|
||||
public static void masterRestartFromOther(String contractID) {
|
||||
LOGGER.info("Master从别的节点load当前状态");
|
||||
|
||||
String masterID =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMasterNode();
|
||||
String masterID = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getMasterNode();
|
||||
if (recoverStatus.get(masterID).get(contractID) != RecoverFlag.ToRecover) {
|
||||
return;
|
||||
}
|
||||
@ -174,15 +171,15 @@ public class MasterServerRecoverMechAction {
|
||||
|
||||
// 找一个普通节点来dump
|
||||
String relyNode = null;
|
||||
for (String nodeID :
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers()) {
|
||||
for (String nodeID : CMActions.manager.multiContractRecorder
|
||||
.getMultiContractMeta(contractID).getMembers()) {
|
||||
if (nodeID.equals(masterID)) {
|
||||
continue;
|
||||
}
|
||||
if (recoverStatus.get(nodeID).get(contractID) != RecoverFlag.Fine) {
|
||||
continue;
|
||||
}
|
||||
//TODO????似乎人重连接?
|
||||
// TODO????似乎人重连接?
|
||||
if (NetworkManager.instance.hasAgentConnection(nodeID)) {
|
||||
relyNode = nodeID;
|
||||
break;
|
||||
@ -204,12 +201,16 @@ public class MasterServerRecoverMechAction {
|
||||
|
||||
public static void unitModeCheck(String contractID) {
|
||||
String stateful = CMActions.manager.getContractStateful(contractID);
|
||||
// just disable unitModeCheck
|
||||
if (stateful != null)
|
||||
return;
|
||||
if (stateful.equals("false")) {
|
||||
LOGGER.info("无状态集群合约无需运行模式检查及切换!");
|
||||
return;
|
||||
}
|
||||
|
||||
MultiContractMeta mpci = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
MultiContractMeta mpci =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
synchronized (mpci) {
|
||||
int total = 0, fineNum = 0;
|
||||
for (String nodeId : mpci.getMembers()) {
|
||||
@ -223,27 +224,17 @@ public class MasterServerRecoverMechAction {
|
||||
total++;
|
||||
}
|
||||
|
||||
LOGGER.info(
|
||||
"合约"
|
||||
+ contractID
|
||||
+ "的集群,正常运行的节点有"
|
||||
+ fineNum
|
||||
+ "个,总共节点有"
|
||||
+ total
|
||||
+ "个. Math.ceil(total / 2)="
|
||||
+ Math.ceil((double) total / 2)
|
||||
+ " online > Math.ceil(total / 2)"
|
||||
+ (fineNum > Math.ceil(total / 2))
|
||||
+ " mpci.unitStatus="
|
||||
+ mpci.unitStatus);
|
||||
LOGGER.info("合约" + contractID + "的集群,正常运行的节点有" + fineNum + "个,总共节点有" + total
|
||||
+ "个. Math.ceil(total / 2)=" + Math.ceil((double) total / 2)
|
||||
+ " online > Math.ceil(total / 2)" + (fineNum > Math.ceil(total / 2))
|
||||
+ " mpci.unitStatus=" + mpci.unitStatus);
|
||||
ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode;
|
||||
|
||||
if (fineNum <= Math.ceil((double) total / 2)) {
|
||||
unitStatus = ContractUnitStatus.StableMode;
|
||||
}
|
||||
|
||||
LOGGER.info(
|
||||
"合约" + contractID + "的集群更改模式为" + unitStatus);
|
||||
LOGGER.info("合约" + contractID + "的集群更改模式为" + unitStatus);
|
||||
|
||||
Map<String, String> req = new HashMap<String, String>();
|
||||
req.put("action", "changeUnitStatus");
|
||||
@ -251,12 +242,8 @@ public class MasterServerRecoverMechAction {
|
||||
req.put("mode", unitStatus.toString());
|
||||
for (String nodeId : mpci.getMembers()) {
|
||||
NetworkManager.instance.sendToAgentIfConnected(nodeId, JsonUtil.toJson(req));
|
||||
LOGGER.info(
|
||||
"发消息给节点 "
|
||||
+ nodeId.substring(0, 5)
|
||||
+ " 设置合约"
|
||||
+ contractID
|
||||
+ "的集群模式为StableMode");
|
||||
LOGGER.info("发消息给节点 " + nodeId.substring(0, 5) + " 设置合约" + contractID
|
||||
+ "的集群模式为StableMode");
|
||||
|
||||
|
||||
}
|
||||
@ -265,14 +252,15 @@ public class MasterServerRecoverMechAction {
|
||||
}
|
||||
|
||||
// 新选出的Master恢复
|
||||
public static void newMasterRecover(
|
||||
String contractID, String allMembers, List<String> onlineMembers) {
|
||||
public static void newMasterRecover(String contractID, String allMembers,
|
||||
List<String> onlineMembers) {
|
||||
LOGGER.info("[MasterServerRecoverMechAction] 新master开始恢复 : ");
|
||||
|
||||
MasterElectTimeRecorder.masterStartRecover = System.currentTimeMillis();
|
||||
|
||||
// step1 恢复MultiContractInfo
|
||||
MultiContractMeta cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
// step1 恢复MultiContractInfo
|
||||
MultiContractMeta cei =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
synchronized (cei) {
|
||||
cei.setSeqAtMaster(cei.getLastExeSeq());
|
||||
}
|
||||
@ -294,29 +282,18 @@ public class MasterServerRecoverMechAction {
|
||||
}
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
|
||||
meta.setContractExecutor(
|
||||
MasterClientTCPAction.createContractExecutor(meta.contract, contractID, cei.getMasterNode(), cei.getMembers()));
|
||||
switch (meta.contract.getType()) {
|
||||
case RequestAllResponseFirst:
|
||||
case RequestAllResponseHalf:
|
||||
case RequestAllResponseAll:
|
||||
((RequestAllExecutor) meta.contractExecutor).setSeq(cei.getLastExeSeq() + 1);
|
||||
break;
|
||||
case PBFT:
|
||||
((PBFTExecutor) meta.contractExecutor).setSeq(cei.getLastExeSeq() + 1);
|
||||
break;
|
||||
case Sharding:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
meta.setContractExecutor(MasterClientTCPAction.createContractExecutor(meta.contract,
|
||||
contractID, cei.getMasterNode(), cei.getMembers()));
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
args.put("ceiLastExeSeq", cei.getLastExeSeq());
|
||||
meta.contractExecutor.onRecover(args);
|
||||
CMActions.manager.multiContractRecorder.updateValue(cei);
|
||||
LOGGER.info("[master recover] contractID2Members put 合约 " + contractID);
|
||||
LOGGER.info("新master恢复 MultiPointContractInfo 完成!");
|
||||
LOGGER.info(
|
||||
"[MasterServerRecoverMechAction] step1 新master恢复 MultiPointContractInfo 完成! \n");
|
||||
|
||||
// step2 恢复recoverStatues这个map
|
||||
// step2 恢复recoverStatues这个map
|
||||
for (String nodeID : nodeNames) {
|
||||
if (onlineMembers.contains(nodeID)) { // 设置所有在线的节点Fine状态
|
||||
if (!recoverStatus.containsKey(nodeID)) {
|
||||
@ -330,12 +307,8 @@ public class MasterServerRecoverMechAction {
|
||||
recoverStatus.put(nodeID, new ConcurrentHashMap<>());
|
||||
}
|
||||
recoverStatus.get(nodeID).put(contractID, RecoverFlag.ToRecover);
|
||||
LOGGER.info(
|
||||
"新master设置 合约 "
|
||||
+ contractID
|
||||
+ " 节点 "
|
||||
+ nodeID.substring(0, 5)
|
||||
+ " 为ToRecover");
|
||||
LOGGER.info("新master设置 合约 " + contractID + " 节点 " + nodeID.substring(0, 5)
|
||||
+ " 为ToRecover");
|
||||
}
|
||||
}
|
||||
LOGGER.info("新master恢复 recoverStatus map 完成!");
|
||||
@ -343,7 +316,7 @@ public class MasterServerRecoverMechAction {
|
||||
|
||||
MasterElectTimeRecorder.masterRecoverFinish = System.currentTimeMillis();
|
||||
|
||||
// step3 告知NC更新路由信息
|
||||
// step3 告知NC更新路由信息
|
||||
Map<String, String> re = new HashMap<>();
|
||||
re.put("action", "changeMaster");
|
||||
re.put("contractID", contractID);
|
||||
@ -351,35 +324,35 @@ public class MasterServerRecoverMechAction {
|
||||
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(re));
|
||||
LOGGER.info("[MasterServerRecoverMechAction] step3 告知NC更新路由信息! \n");
|
||||
|
||||
// step4 查看集群当前状态
|
||||
// step4 查看集群当前状态
|
||||
unitModeCheck(contractID);
|
||||
LOGGER.info("新master 查看当前集群状态 完成!");
|
||||
LOGGER.info("[MasterServerRecoverMechAction] step4 新master查看当前集群状态 完成!");
|
||||
|
||||
// step5 向其他可能和旧的master断连,但和NC连接的节点发请求让连新的master
|
||||
// String off = "";
|
||||
// for (String str : offlines) {
|
||||
// if (off.equals("")) {
|
||||
// off = str;
|
||||
// } else {
|
||||
// off += ",";
|
||||
// off += str;
|
||||
// }
|
||||
// }
|
||||
// step5 向其他可能和旧的master断连,但和NC连接的节点发请求让连新的master
|
||||
// String off = "";
|
||||
// for (String str : offlines) {
|
||||
// if (off.equals("")) {
|
||||
// off = str;
|
||||
// } else {
|
||||
// off += ",";
|
||||
// off += str;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Map<String, String> request = new HashMap<>();
|
||||
// request.put("action", "updateFormerMaster");
|
||||
// request.put("contractID", contractID);
|
||||
// request.put("offlines", off);
|
||||
// request.put("master", mpci.masterNode);
|
||||
// CMActions.client.sendMsg(JsonUtil.toJson(request));
|
||||
// Map<String, String> request = new HashMap<>();
|
||||
// request.put("action", "updateFormerMaster");
|
||||
// request.put("contractID", contractID);
|
||||
// request.put("offlines", off);
|
||||
// request.put("master", mpci.masterNode);
|
||||
// CMActions.client.sendMsg(JsonUtil.toJson(request));
|
||||
|
||||
LOGGER.info("新的master恢复完毕!");
|
||||
LOGGER.info("[MasterServerRecoverMechAction] 新master恢复完成!");
|
||||
}
|
||||
|
||||
public String judegeRecoverMethod(
|
||||
String contractID, String nodeMode, String unitMode, int nodeLastExe, int unitLastExe) {
|
||||
public String judegeRecoverMethod(String contractID, String nodeMode, String unitMode,
|
||||
int nodeLastExe, int unitLastExe) {
|
||||
if (nodeMode.equals(ContractUnitStatus.CommonMode.toString())
|
||||
&& nodeLastExe == unitLastExe) {
|
||||
return "";
|
||||
@ -416,13 +389,14 @@ public class MasterServerRecoverMechAction {
|
||||
String unitMode;
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID))
|
||||
unitMode = ContractUnitStatus.StableMode.toString();
|
||||
else unitMode = ContractUnitStatus.CommonMode.toString();
|
||||
else
|
||||
unitMode = ContractUnitStatus.CommonMode.toString();
|
||||
int nodeLastExe = Integer.MIN_VALUE;
|
||||
if (jo.has("lastExeSeq")) {
|
||||
nodeLastExe = jo.get("lastExeSeq").getAsInt();
|
||||
}
|
||||
int unitLastExe =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getCurSeqAtMaster();
|
||||
int unitLastExe = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getCurSeqAtMaster();
|
||||
|
||||
RecoverMechTimeRecorder.startJudgeRecoverMethod.put(nodeID, System.currentTimeMillis());
|
||||
|
||||
@ -431,15 +405,8 @@ public class MasterServerRecoverMechAction {
|
||||
|
||||
RecoverMechTimeRecorder.judgeRecoverMethodFinish.put(nodeID, System.currentTimeMillis());
|
||||
|
||||
LOGGER.info(
|
||||
"[MasterServerRecoverMechAciton] askForRecover mode="
|
||||
+ nodeMode
|
||||
+ " contractID="
|
||||
+ contractID
|
||||
+ " nodeID="
|
||||
+ nodeID
|
||||
+ " method="
|
||||
+ method);
|
||||
LOGGER.info("[MasterServerRecoverMechAciton] askForRecover mode=" + nodeMode
|
||||
+ " contractID=" + contractID + " nodeID=" + nodeID + " method=" + method);
|
||||
if (method.equals("common")) {
|
||||
LOGGER.info("restartContractFromCommonMode");
|
||||
restartContractFromCommonMode(nodeID, contractID);
|
||||
@ -458,8 +425,8 @@ public class MasterServerRecoverMechAction {
|
||||
|
||||
@Action(async = true)
|
||||
public void restartContractFromCommonMode(JsonObject jo, ResultCallback cb) {
|
||||
restartContractFromCommonMode(
|
||||
jo.get("nodeID").getAsString(), jo.get("contractID").getAsString());
|
||||
restartContractFromCommonMode(jo.get("nodeID").getAsString(),
|
||||
jo.get("contractID").getAsString());
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
@ -468,7 +435,8 @@ public class MasterServerRecoverMechAction {
|
||||
LOGGER.info("master查询缓存的 trans records 从 " + startSeq + " 开始");
|
||||
String nodeId = jo.get("nodeID").getAsString();
|
||||
String contractID = jo.get("contractID").getAsString();
|
||||
MultiContractMeta mpci = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
MultiContractMeta mpci =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
synchronized (mpci) {
|
||||
if (mpci.unitStatus == ContractUnitStatus.CommonMode) {
|
||||
restartContractFromCommonMode(nodeId, contractID);
|
||||
@ -488,7 +456,8 @@ public class MasterServerRecoverMechAction {
|
||||
String path = contractID + "_temp_TransFile_" + new Random().nextLong();
|
||||
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
|
||||
try {
|
||||
FileWriter fw = new FileWriter(file, true);
|
||||
@ -527,7 +496,7 @@ public class MasterServerRecoverMechAction {
|
||||
int index = 1; // 数据包序号
|
||||
long count = 0;
|
||||
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0; ) {
|
||||
for (int len = 0; (len = (fin.read(buff))) > 0;) {
|
||||
// logger.info("len = " + len);
|
||||
String data = ByteUtil.encodeBASE64(buff, len);
|
||||
req.put("data", data);
|
||||
@ -548,12 +517,12 @@ public class MasterServerRecoverMechAction {
|
||||
}
|
||||
fin.close();
|
||||
|
||||
// req.put("isDone", "true");
|
||||
// req.put("contractID", contractID);
|
||||
// req.put("last", last);
|
||||
// req.remove("data");
|
||||
// req.put("isDone", "true");
|
||||
// req.put("contractID", contractID);
|
||||
// req.put("last", last);
|
||||
// req.remove("data");
|
||||
//
|
||||
// node.connection.sendMsg(JsonUtil.toJson(req));
|
||||
// node.connection.sendMsg(JsonUtil.toJson(req));
|
||||
|
||||
// master delete state file
|
||||
if (file.isFile() && file.exists()) {
|
||||
@ -610,7 +579,8 @@ public class MasterServerRecoverMechAction {
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else fout = (FileOutputStream) stateFileMap.get(fileName);
|
||||
} else
|
||||
fout = (FileOutputStream) stateFileMap.get(fileName);
|
||||
|
||||
String data = args.get("data").getAsString();
|
||||
try {
|
||||
@ -631,25 +601,26 @@ public class MasterServerRecoverMechAction {
|
||||
req.put("isMaster", "true");
|
||||
req.put("filePath", dir.getAbsolutePath());
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
NetworkManager.instance.sendToAgent(keyPair.getPublicKeyStr(), JsonUtil.toJson(req));
|
||||
NetworkManager.instance.sendToAgent(keyPair.getPublicKeyStr(),
|
||||
JsonUtil.toJson(req));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// @Action(async = true)
|
||||
// public void statelessUnitLastExe(JsonObject jo, ResultCallback cb) {
|
||||
// String contractID = jo.get("contractID").getAsString();
|
||||
// String unitLastExe =
|
||||
// @Action(async = true)
|
||||
// public void statelessUnitLastExe(JsonObject jo, ResultCallback cb) {
|
||||
// String contractID = jo.get("contractID").getAsString();
|
||||
// String unitLastExe =
|
||||
// MasterServerTCPAction.contractID2Members.get(contractID).getCurSeq() + "";
|
||||
// String nodeID = jo.get("nodeID").getAsString();
|
||||
// String nodeID = jo.get("nodeID").getAsString();
|
||||
//
|
||||
// Map<String, String> request = new HashMap<String, String>();
|
||||
// request.put("action", "statelessReceiveLastExe");
|
||||
// request.put("contractID", contractID);
|
||||
// request.put("unitLastExe", unitLastExe);
|
||||
// SlaveNode node = MasterServerTCPAction.id2Slaves.get(nodeID);
|
||||
// node.connection.sendMsg(JsonUtil.toJson(request));
|
||||
// }
|
||||
// Map<String, String> request = new HashMap<String, String>();
|
||||
// request.put("action", "statelessReceiveLastExe");
|
||||
// request.put("contractID", contractID);
|
||||
// request.put("unitLastExe", unitLastExe);
|
||||
// SlaveNode node = MasterServerTCPAction.id2Slaves.get(nodeID);
|
||||
// node.connection.sendMsg(JsonUtil.toJson(request));
|
||||
// }
|
||||
}
|
||||
|
@ -14,17 +14,15 @@ import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.units.MultiContractMeta;
|
||||
import org.bdware.sc.units.RequestCache;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.sdk.consistency.api.NotifiableResultMerger;
|
||||
import org.bdware.server.CongestionControl;
|
||||
import org.bdware.server.action.Action;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.SyncResult;
|
||||
import org.bdware.server.executor.consistency.RequestAllExecutor;
|
||||
import org.bdware.server.trustedmodel.KillUnitContractResultCollector;
|
||||
import org.bdware.server.trustedmodel.ResultCollector;
|
||||
import org.bdware.units.NetworkManager;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -41,26 +39,21 @@ public class MasterServerTCPAction {
|
||||
|
||||
static {
|
||||
final Object flag = new Object();
|
||||
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(
|
||||
() -> {
|
||||
boolean cleared = clearCache();
|
||||
if (cleared) {
|
||||
try {
|
||||
synchronized (flag) {
|
||||
flag.wait(14000L);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(() -> {
|
||||
boolean cleared = clearCache();
|
||||
if (cleared) {
|
||||
try {
|
||||
synchronized (flag) {
|
||||
flag.wait(14000L);
|
||||
}
|
||||
},
|
||||
0,
|
||||
1,
|
||||
TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public MasterServerTCPAction() {
|
||||
}
|
||||
public MasterServerTCPAction() {}
|
||||
|
||||
public static void notifyNodeOffline(String contractID, String nodeID) {
|
||||
synchronized (sync) {
|
||||
@ -68,24 +61,22 @@ public class MasterServerTCPAction {
|
||||
ResultCallback cb = sync.waitObj.get(requestID);
|
||||
if (cb instanceof ResultCollector) {
|
||||
ResultCollector rc = (ResultCollector) cb;
|
||||
if (rc.getCommitter() instanceof RequestAllExecutor.ResultMerger) {
|
||||
RequestAllExecutor.ResultMerger merger =
|
||||
(RequestAllExecutor.ResultMerger) rc.getCommitter();
|
||||
if (rc.getCommitter() instanceof NotifiableResultMerger) {
|
||||
NotifiableResultMerger merger = (NotifiableResultMerger) rc.getCommitter();
|
||||
if (merger.getContractID().equals(contractID)) {
|
||||
LOGGER.info("node " + nodeID + " offline! in the cluster of contract " + contractID);
|
||||
LOGGER.debug("nodeID=" + nodeID + " contractID=" + contractID +
|
||||
": " + merger.getInfo());
|
||||
ContractResult cr =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Exception,
|
||||
new JsonPrimitive("node offline"));
|
||||
LOGGER.info("node " + nodeID + " offline! in the cluster of contract "
|
||||
+ contractID);
|
||||
LOGGER.debug("nodeID=" + nodeID + " contractID=" + contractID + ": "
|
||||
+ merger.getInfo());
|
||||
ContractResult cr = new ContractResult(ContractResult.Status.Exception,
|
||||
new JsonPrimitive("node offline"));
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("data", JsonUtil.toJson(cr));
|
||||
jo.addProperty("responseID", requestID);
|
||||
jo.addProperty("action", "receiveTrustfullyResult");
|
||||
jo.addProperty("nodeID", nodeID);
|
||||
LOGGER.debug("[cb] nodeID=" + nodeID + " contractID=" + contractID +
|
||||
": " + jo);
|
||||
LOGGER.debug("[cb] nodeID=" + nodeID + " contractID=" + contractID
|
||||
+ ": " + jo);
|
||||
sync.wakeUp(requestID, jo.toString());
|
||||
}
|
||||
}
|
||||
@ -97,7 +88,8 @@ public class MasterServerTCPAction {
|
||||
static RequestCache getCache(String contractID) {
|
||||
if (contractID != null) {
|
||||
RequestCache cache = requestCache.get(contractID);
|
||||
if (cache != null) return cache;
|
||||
if (cache != null)
|
||||
return cache;
|
||||
else {
|
||||
LOGGER.info("[MasterServerTCPAction] create requestcache:" + contractID);
|
||||
RequestCache reqc = new RequestCache();
|
||||
@ -109,17 +101,18 @@ public class MasterServerTCPAction {
|
||||
}
|
||||
|
||||
static boolean clearCache() {
|
||||
if (requestCache.isEmpty()) return true;
|
||||
if (requestCache.isEmpty())
|
||||
return true;
|
||||
|
||||
// final long time = System.currentTimeMillis() - 120000L; // 120s
|
||||
// requestCache
|
||||
// .entrySet()
|
||||
// .removeIf(
|
||||
// entry -> {
|
||||
// RequestCache cache = entry.getValue();
|
||||
// if (cache == null) return true;
|
||||
// return cache.getTime() < time;
|
||||
// });
|
||||
// final long time = System.currentTimeMillis() - 120000L; // 120s
|
||||
// requestCache
|
||||
// .entrySet()
|
||||
// .removeIf(
|
||||
// entry -> {
|
||||
// RequestCache cache = entry.getValue();
|
||||
// if (cache == null) return true;
|
||||
// return cache.getTime() < time;
|
||||
// });
|
||||
|
||||
for (RequestCache cache : requestCache.values()) {
|
||||
cache.decSize();
|
||||
@ -131,15 +124,8 @@ public class MasterServerTCPAction {
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
MultiContractMeta ret =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
System.out.println(
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
||||
.format(new Date(System.currentTimeMillis()))
|
||||
+ " [MasterServerTCPAction] getCMInfo: "
|
||||
+ meta.getName()
|
||||
+ " "
|
||||
+ meta.getID()
|
||||
+ " "
|
||||
+ meta.getStatus());
|
||||
LOGGER.debug(String.format("getCMInfo: %s %s %s", meta.getName(), meta.getID(),
|
||||
meta.getStatus()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -178,9 +164,8 @@ public class MasterServerTCPAction {
|
||||
jo.addProperty("action", "killContractProcessAtSlave");
|
||||
MultiContractMeta contractMeta =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(cl.getContractID());
|
||||
KillUnitContractResultCollector collector =
|
||||
new KillUnitContractResultCollector(
|
||||
jo.get("requestID").getAsString(), cb, contractMeta.getMembers().length);
|
||||
KillUnitContractResultCollector collector = new KillUnitContractResultCollector(
|
||||
jo.get("requestID").getAsString(), cb, contractMeta.getMembers().length);
|
||||
sync.sleep(jo.get("requestID").getAsString(), collector);
|
||||
for (String member : contractMeta.getMembers()) {
|
||||
NetworkManager.instance.sendToAgent(member, jo.toString());
|
||||
@ -190,27 +175,27 @@ public class MasterServerTCPAction {
|
||||
@Action(async = true)
|
||||
public void receiveTrustfullyResult(JsonObject jo, ResultCallback cb) {
|
||||
String responseID = jo.get("responseID").getAsString();
|
||||
LOGGER.info("========== ExecuteContractLocally wakeUp:" + responseID);
|
||||
// LOGGER.info("========== ExecuteContractLocally wakeUp:" + responseID);
|
||||
MasterServerTCPAction.sync.wakeUp(responseID, jo.toString());
|
||||
}
|
||||
|
||||
// TODO to Remove
|
||||
// @Action(async = true)
|
||||
// // 假设该结点有运行这个合约调用的合约
|
||||
// public void executeContractLocallyServer(JsonObject jo, ResultCallback cb) {
|
||||
// final ContractRequest request =
|
||||
// JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class);
|
||||
// long start = System.currentTimeMillis();
|
||||
// String data2 = CMActions.manager.executeLocally(request, null);
|
||||
// Map<String, String> ret = new HashMap<>();
|
||||
// ret.put("action", "receiveTrustfullyResultServer");
|
||||
// SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
// ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
// ret.put("responseID", jo.get("uniReqID").getAsString());
|
||||
// ret.put("executeTime", (System.currentTimeMillis() - start) + "");
|
||||
// ret.put("data", data2);
|
||||
// cb.onResult(JsonUtil.toJson(ret));
|
||||
// }
|
||||
// TODO to Remove
|
||||
// @Action(async = true)
|
||||
// // 假设该结点有运行这个合约调用的合约
|
||||
// public void executeContractLocallyServer(JsonObject jo, ResultCallback cb) {
|
||||
// final ContractRequest request =
|
||||
// JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class);
|
||||
// long start = System.currentTimeMillis();
|
||||
// String data2 = CMActions.manager.executeLocally(request, null);
|
||||
// Map<String, String> ret = new HashMap<>();
|
||||
// ret.put("action", "receiveTrustfullyResultServer");
|
||||
// SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
// ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
// ret.put("responseID", jo.get("uniReqID").getAsString());
|
||||
// ret.put("executeTime", (System.currentTimeMillis() - start) + "");
|
||||
// ret.put("data", data2);
|
||||
// cb.onResult(JsonUtil.toJson(ret));
|
||||
// }
|
||||
|
||||
|
||||
@Action(async = true)
|
||||
@ -230,15 +215,15 @@ public class MasterServerTCPAction {
|
||||
// 调用方向为MasterClient->MasterServer,
|
||||
// 如果为多节点,则为MasterServer executeLocally-> MasterClient
|
||||
// executeLocally,否则为MasterServer->Local
|
||||
// LOGGER.info("[requestContractExecution]:" + jo.toString());
|
||||
// loggger.bug
|
||||
// LOGGER.info("[requestContractExecution]:" + jo.toString());
|
||||
// loggger.bug
|
||||
JsonObject cr = jo.get("data").getAsJsonObject();
|
||||
String requestID = cr.get("requestID").getAsString();
|
||||
String contractID = cr.get("contractID").getAsString();
|
||||
ContractMeta contractMeta =
|
||||
CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
if (contractMeta == null || contractMeta.getStatus() == KILLED) {
|
||||
LOGGER.debug("send ReRoute response:" + cr.toString());
|
||||
LOGGER.debug("send ReRoute response: " + cr);
|
||||
JsonObject result = new JsonObject();
|
||||
result.addProperty("action", "reRouteContract");
|
||||
result.addProperty("responseID", cr.get("requestID").getAsString());
|
||||
@ -248,20 +233,20 @@ public class MasterServerTCPAction {
|
||||
return;
|
||||
}
|
||||
MultiContractMeta info = MasterServerTCPAction.getMPCInfo(contractMeta.getID());
|
||||
// LOGGER.info("[MasterServerTCPAction] requestContractExecution " + new
|
||||
// LOGGER.info("[MasterServerTCPAction] requestContractExecution " + new
|
||||
// Gson().toJson(jo));
|
||||
// LOGGER.info("MultiPointContractInfo info=" + (info == null ? "null" : "not
|
||||
// LOGGER.info("MultiPointContractInfo info=" + (info == null ? "null" : "not
|
||||
// null") +
|
||||
// "\n");
|
||||
|
||||
if (info != null && contractMeta.contract.getType() != ContractExecType.Sharding) {
|
||||
if (info != null && ContractExecType.Sharding != contractMeta.contract.getType()
|
||||
&& !ContractExecType.SelfAdaptiveSharding
|
||||
.equals(contractMeta.contract.getType())) {
|
||||
// 这个是个多节点的合约
|
||||
// Just forward it to the correct Node
|
||||
// Master节点直接发3个,聚合后返回结果。
|
||||
contractMeta.contractExecutor.execute(
|
||||
requestID,
|
||||
JsonUtil.fromJson(cr, ContractRequest.class),
|
||||
new ResultCallback() {
|
||||
contractMeta.contractExecutor.execute(requestID,
|
||||
JsonUtil.fromJson(cr, ContractRequest.class), new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
@ -283,19 +268,20 @@ public class MasterServerTCPAction {
|
||||
// 这个是个单节点的合约
|
||||
// executeContract(CacheTest应该要有多个进来。
|
||||
if (null != client && !client.getContractType().needSeq()) {
|
||||
contractMeta.contractExecutor.execute(requestID, contractRequest, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("action", "receiveContractExecutionServer");
|
||||
result.put("responseID", cr.get("requestID").getAsString());
|
||||
result.put("data", str);
|
||||
cb.onResult(JsonUtil.toJson(result));
|
||||
CongestionControl.masterServerLoad.decrementAndGet();
|
||||
}
|
||||
}, null);
|
||||
contractMeta.contractExecutor.execute(requestID, contractRequest,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("action", "receiveContractExecutionServer");
|
||||
result.put("responseID", cr.get("requestID").getAsString());
|
||||
result.put("data", str);
|
||||
cb.onResult(JsonUtil.toJson(result));
|
||||
CongestionControl.masterServerLoad.decrementAndGet();
|
||||
}
|
||||
}, null);
|
||||
} else {
|
||||
LOGGER.debug("send ReRoute response:" + cr.toString());
|
||||
LOGGER.debug("send ReRoute response:" + cr);
|
||||
JsonObject result = new JsonObject();
|
||||
result.addProperty("action", "reRouteContract");
|
||||
result.addProperty("responseID", cr.get("requestID").getAsString());
|
||||
|
@ -22,8 +22,7 @@ public class MasterServerTransferAction {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MasterServerTransferAction.class);
|
||||
private Map<String, OutputStream> fileMap = new HashMap<>();
|
||||
|
||||
public MasterServerTransferAction() {
|
||||
}
|
||||
public MasterServerTransferAction() {}
|
||||
|
||||
@Action(async = true)
|
||||
public void transferByYPK(JsonObject args, ResultCallback cb) {
|
||||
@ -54,7 +53,8 @@ public class MasterServerTransferAction {
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else fout = (FileOutputStream) fileMap.get(fileKey);
|
||||
} else
|
||||
fout = (FileOutputStream) fileMap.get(fileKey);
|
||||
if (isDone) {
|
||||
if (fout != null)
|
||||
try {
|
||||
@ -88,11 +88,10 @@ public class MasterServerTransferAction {
|
||||
c.setID(contractID);
|
||||
|
||||
c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
c.doSignature(GlobalConf.instance.keyPair);
|
||||
|
||||
try {
|
||||
File temp = new File(path);
|
||||
if (temp.isFile() && temp.exists()) c.setScript(temp.getAbsolutePath());
|
||||
if (temp.isFile() && temp.exists())
|
||||
c.setScript(temp.getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -119,7 +118,6 @@ public class MasterServerTransferAction {
|
||||
c.setID(contractID);
|
||||
|
||||
c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
c.doSignature(GlobalConf.instance.keyPair);
|
||||
c.setScript(script);
|
||||
|
||||
return CMActions.manager.startContract(c);
|
||||
@ -145,7 +143,8 @@ public class MasterServerTransferAction {
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else fout = (FileOutputStream) fileMap.get(fileName);
|
||||
} else
|
||||
fout = (FileOutputStream) fileMap.get(fileName);
|
||||
if (isDone) {
|
||||
if (fout != null)
|
||||
try {
|
||||
|
@ -4,20 +4,17 @@ import com.google.gson.JsonObject;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.action.Action;
|
||||
import org.bdware.units.NetworkManager;
|
||||
import org.bdware.units.beans.MultiPointContractInfo;
|
||||
import org.bdware.units.function.ContractUnitManager;
|
||||
import org.bdware.units.function.ExecutionManager;
|
||||
import org.bdware.units.function.SequencingManager;
|
||||
|
||||
/**
|
||||
* 负责集群信息的方法
|
||||
* 如获取集群信息,master信息等
|
||||
* 负责集群信息的方法 如获取集群信息,master信息等
|
||||
*/
|
||||
public class UnitsInfoAction {
|
||||
|
||||
public UnitsInfoAction() {
|
||||
}
|
||||
public UnitsInfoAction() {}
|
||||
|
||||
@Action(async = true)
|
||||
public void getContractUnit(String contractId, final ResultCallback callback) {
|
||||
@ -29,7 +26,8 @@ public class UnitsInfoAction {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", "getContractUnit");
|
||||
jsonObject.addProperty("contractId", contractId);
|
||||
ContractUnitManager.instance.send(jsonObject.toString(), new String[]{"NetworkManager.instance.getTcpNodeCenter()"}, callback);
|
||||
ContractUnitManager.instance.send(jsonObject.toString(),
|
||||
new String[] {"NetworkManager.instance.getTcpNodeCenter()"}, callback);
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +41,8 @@ public class UnitsInfoAction {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action", "getContractUnitMaster");
|
||||
jsonObject.addProperty("content", contractId);
|
||||
ContractUnitManager.instance.send(jsonObject.toString(), new String[]{"NetworkManager.instance.getTcpNodeCenter()"}, callback);
|
||||
ContractUnitManager.instance.send(jsonObject.toString(),
|
||||
new String[] {"NetworkManager.instance.getTcpNodeCenter()"}, callback);
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,7 +57,8 @@ public class UnitsInfoAction {
|
||||
@Action(async = true)
|
||||
public void updateContractStartInfo(JsonObject args, final ResultCallback callback) {
|
||||
System.out.println("[updateContractStartInfo] " + args);
|
||||
MultiPointContractInfo contractInfo = JsonUtil.fromJson(args.get("content").getAsString(), MultiPointContractInfo.class);
|
||||
MultiPointContractInfo contractInfo =
|
||||
JsonUtil.fromJson(args.get("content").getAsString(), MultiPointContractInfo.class);
|
||||
String contractID = args.get("contractID").getAsString();
|
||||
ContractUnitManager.instance.addContractInfo(contractID, contractInfo);
|
||||
SequencingManager.instance.updateConsensusCenter(contractID, contractInfo);
|
||||
|
@ -35,8 +35,7 @@ import java.io.File;
|
||||
public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
private static final Logger LOGGER = LogManager.getLogger(_UNUSED_ExecutionAction.class);
|
||||
|
||||
public _UNUSED_ExecutionAction() {
|
||||
}
|
||||
public _UNUSED_ExecutionAction() {}
|
||||
|
||||
// TODO TOMerge
|
||||
@Action(async = true)
|
||||
@ -59,7 +58,6 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
// contractID not hash
|
||||
c.setID(contractID);
|
||||
c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
|
||||
c.doSignature(GlobalConf.instance.keyPair);
|
||||
// c.setConsensusType(sequencing);
|
||||
// c.setNetworkType(network);
|
||||
if (content != null && content.startsWith("/")) {
|
||||
@ -72,23 +70,18 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
} else {
|
||||
unitContractMessage.msgType =
|
||||
UnitContractMessageType.ContractUnitResponse.getValue();
|
||||
// unitContractMessage.cuType =
|
||||
// unitContractMessage.cuType =
|
||||
// UnitContractRequestType.START.getValue();
|
||||
unitContractMessage.action = "startContractResponse";
|
||||
unitContractMessage.content =
|
||||
JsonUtil.toJson(
|
||||
new ContractResult(
|
||||
ContractResult.Status.Exception,
|
||||
new JsonPrimitive("Contract not found")));
|
||||
reply =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.addReceiver(sender)
|
||||
.setContent(
|
||||
ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
|
||||
.build();
|
||||
// byte[] bytes = reply.toByteArray();
|
||||
JsonUtil.toJson(new ContractResult(ContractResult.Status.Exception,
|
||||
new JsonPrimitive("Contract not found")));
|
||||
reply = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID).addReceiver(sender)
|
||||
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
|
||||
.build();
|
||||
// byte[] bytes = reply.toByteArray();
|
||||
callback.onResult(JsonUtil.toJson(reply));
|
||||
return;
|
||||
}
|
||||
@ -97,24 +90,21 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
c.setScript(content);
|
||||
}
|
||||
|
||||
content = CMActions.manager.startContractAndRedirect(c, System.out);
|
||||
content = CMActions.manager.startContract(c);
|
||||
LOGGER.info("[START] result: " + content);
|
||||
result = JsonUtil.fromJson(content, ContractResult.class);
|
||||
unitContractMessage.msgType = UnitContractMessageType.ContractUnitResponse.getValue();
|
||||
// unitContractMessage.statusType = result.status ==
|
||||
// unitContractMessage.statusType = result.status ==
|
||||
// ContractResult.Status.Success?0:1;
|
||||
unitContractMessage.action = "startContractResponse";
|
||||
// Collector investigates success or fail
|
||||
unitContractMessage.content = JsonUtil.toJson(result);
|
||||
// TODO save privKey in contractClient
|
||||
unitContractMessage.pubkey = "";
|
||||
reply =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.addReceiver(sender)
|
||||
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
|
||||
.build();
|
||||
reply = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID).addReceiver(sender)
|
||||
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage))).build();
|
||||
byte[] bytes = reply.toByteArray();
|
||||
callback.onResult(ByteUtil.encodeBASE64(bytes));
|
||||
if (result.status == ContractResult.Status.Success) {
|
||||
@ -131,10 +121,8 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
System.out.println("[BaseFunctionManager] handleResponse null");
|
||||
return;
|
||||
}
|
||||
if (responseCenter.updateReply(
|
||||
arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(),
|
||||
arg.get("pubkey").getAsString())) {
|
||||
if (responseCenter.updateReply(arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(), arg.get("pubkey").getAsString())) {
|
||||
// save contractInfo
|
||||
String contractID = arg.get("contractID").getAsString();
|
||||
int sequencing = arg.get("sequencing").getAsInt();
|
||||
@ -159,62 +147,54 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
@Action(async = true)
|
||||
public void executeContractAtSlave(JsonObject args, final ResultCallback callback) {
|
||||
String sender = args.get("sender").getAsString();
|
||||
UnitContractMessage unitContractMessage = JsonUtil.fromJson(args, UnitContractMessage.class);
|
||||
UnitContractMessage unitContractMessage =
|
||||
JsonUtil.fromJson(args, UnitContractMessage.class);
|
||||
String requestID = unitContractMessage.requestID;
|
||||
int sequencing = unitContractMessage.sequencing;
|
||||
int network = unitContractMessage.network;
|
||||
// int rcheck = unitContractMessage.rcheck;
|
||||
// int rcheck = unitContractMessage.rcheck;
|
||||
String contractID = unitContractMessage.contractID;
|
||||
String operation = unitContractMessage.operation;
|
||||
String arg = unitContractMessage.arg;
|
||||
// String content = unitContractMessage.content;
|
||||
// String content = unitContractMessage.content;
|
||||
LOGGER.info("[executeContractAtSlave] [EXECUTE] " + args);
|
||||
final ContractRequest c = new ContractRequest();
|
||||
c.setContractID(contractID);
|
||||
c.setRequestID(requestID);
|
||||
// c.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
|
||||
// c.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
|
||||
// c.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
|
||||
// c.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
|
||||
c.setAction(operation);
|
||||
c.setArg(arg);
|
||||
// if (c.withEvaluatesAnalysis) {
|
||||
// c.setValue(jo.get("hasValue").getAsLong());
|
||||
// }
|
||||
// if (jo.has("gasLimit")) {
|
||||
// c.setGasLimit(jo.get("gasLimit").getAsLong());
|
||||
// }
|
||||
// if (args.has("requester")) {
|
||||
// c.setPublicKey(args.get("requester").getAsString());
|
||||
// c.setSignature(args.get("signature").getAsString());
|
||||
// }
|
||||
// c.setPublicKey(args.get("pubkey").getAsString());
|
||||
// c.setSignature(args.get("signature").getAsString());
|
||||
// if (c.withEvaluatesAnalysis) {
|
||||
// c.setValue(jo.get("hasValue").getAsLong());
|
||||
// }
|
||||
// if (jo.has("gasLimit")) {
|
||||
// c.setGasLimit(jo.get("gasLimit").getAsLong());
|
||||
// }
|
||||
// if (args.has("requester")) {
|
||||
// c.setPublicKey(args.get("requester").getAsString());
|
||||
// c.setSignature(args.get("signature").getAsString());
|
||||
// }
|
||||
// c.setPublicKey(args.get("pubkey").getAsString());
|
||||
// c.setSignature(args.get("signature").getAsString());
|
||||
String reqID;
|
||||
long start = System.currentTimeMillis();
|
||||
CMActions.manager.executeContractInternal(
|
||||
c,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
// TODO qianming
|
||||
unitContractMessage.signature = str;
|
||||
unitContractMessage.action = "executeContractResponse";
|
||||
unitContractMessage.content = str;
|
||||
UnitMessage unitMessage =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(
|
||||
BDLedgerContract.UnitMessageType
|
||||
.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.addReceiver(sender)
|
||||
.setContent(
|
||||
ByteString.copyFromUtf8(
|
||||
JsonUtil.toJson(unitContractMessage)))
|
||||
.build();
|
||||
byte[] bytes = unitMessage.toByteArray();
|
||||
callback.onResult(ByteUtil.encodeBASE64(bytes));
|
||||
}
|
||||
},
|
||||
null);
|
||||
CMActions.manager.executeContractInternal(c, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
// TODO qianming
|
||||
unitContractMessage.signature = str;
|
||||
unitContractMessage.action = "executeContractResponse";
|
||||
unitContractMessage.content = str;
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID).addReceiver(sender)
|
||||
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
|
||||
.build();
|
||||
byte[] bytes = unitMessage.toByteArray();
|
||||
callback.onResult(ByteUtil.encodeBASE64(bytes));
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
@ -226,10 +206,8 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
System.out.println("[BaseFunctionManager] responseCenter null");
|
||||
return;
|
||||
}
|
||||
if (responseCenter.updateReply(
|
||||
arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(),
|
||||
arg.get("signature").getAsString())) {
|
||||
if (responseCenter.updateReply(arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(), arg.get("signature").getAsString())) {
|
||||
// call back
|
||||
responseCenter.formReply();
|
||||
ExecutionManager.instance.responseCenters.remove(requestID);
|
||||
@ -239,38 +217,30 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
@Action(async = true)
|
||||
public void stopContractAtSlave(JsonObject args, final ResultCallback callback) {
|
||||
String sender = args.get("sender").getAsString();
|
||||
UnitContractMessage unitContractMessage = JsonUtil.fromJson(args, UnitContractMessage.class);
|
||||
UnitContractMessage unitContractMessage =
|
||||
JsonUtil.fromJson(args, UnitContractMessage.class);
|
||||
String requestID = unitContractMessage.requestID;
|
||||
int sequencing = unitContractMessage.sequencing;
|
||||
int network = unitContractMessage.network;
|
||||
// int rcheck = unitContractMessage.rcheck;
|
||||
// int rcheck = unitContractMessage.rcheck;
|
||||
String contractID = unitContractMessage.contractID;
|
||||
LOGGER.info("[stopContractAtSlave] [EXECUTE] " + args);
|
||||
final ContractRequest c = new ContractRequest();
|
||||
c.setContractID(contractID);
|
||||
c.setRequestID(requestID);
|
||||
String ret =
|
||||
CMActions.manager.stopContractWithOwner(
|
||||
GlobalConf.instance.keyPair.getPublicKeyStr(), contractID);
|
||||
GRPCPool.instance.writeToChain(
|
||||
this,
|
||||
contractID,
|
||||
"stopContract",
|
||||
ret,
|
||||
args.has("requestID") ? args.get("requestID").getAsString() : null,
|
||||
"");
|
||||
String ret = CMActions.manager
|
||||
.stopContractWithOwner(GlobalConf.instance.keyPair.getPublicKeyStr(), contractID);
|
||||
GRPCPool.instance.writeToChain(this, contractID, "stopContract", ret,
|
||||
args.has("requestID") ? args.get("requestID").getAsString() : null, "");
|
||||
ContractResult contractResult =
|
||||
new ContractResult(ContractResult.Status.Success, new JsonPrimitive("success"));
|
||||
unitContractMessage.signature = ret;
|
||||
unitContractMessage.action = "stopContractResponse";
|
||||
unitContractMessage.content = JsonUtil.toJson(contractResult);
|
||||
UnitMessage unitMessage =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.addReceiver(sender)
|
||||
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
|
||||
.build();
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setSender(GlobalConf.instance.peerID).addReceiver(sender)
|
||||
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage))).build();
|
||||
byte[] bytes = unitMessage.toByteArray();
|
||||
callback.onResult(ByteUtil.encodeBASE64(bytes));
|
||||
}
|
||||
@ -285,10 +255,8 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
System.out.println("[BaseFunctionManager] responseCenter null");
|
||||
return;
|
||||
}
|
||||
if (responseCenter.updateReply(
|
||||
arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(),
|
||||
arg.get("signature").getAsString())) {
|
||||
if (responseCenter.updateReply(arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(), arg.get("signature").getAsString())) {
|
||||
// call back
|
||||
boolean allSuccess = responseCenter.isAllSuccess();
|
||||
responseCenter.formReply();
|
||||
@ -300,6 +268,5 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publishHash(String reqID, String hashStr) {
|
||||
}
|
||||
public void publishHash(String reqID, String hashStr) {}
|
||||
}
|
||||
|
144
src/main/java/org/bdware/server/doip/BCOManager.java
Normal file
144
src/main/java/org/bdware/server/doip/BCOManager.java
Normal file
@ -0,0 +1,144 @@
|
||||
package org.bdware.server.doip;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.bdosclient.CodeRepoClient;
|
||||
import org.bdware.bdosclient.bean.UploadingStatus;
|
||||
import org.bdware.bdosclient.bean.YPKInfo;
|
||||
import org.bdware.doip.audit.EndpointConfig;
|
||||
import org.bdware.doip.audit.client.AuditIrpClient;
|
||||
import org.bdware.doip.audit.writer.AuditType;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.endpoint.client.DoipMessageCallback;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class BCOManager {
|
||||
private final String codeRepoDoid;
|
||||
private final String routerURI;
|
||||
private final SM2KeyPair keyPair;
|
||||
CodeRepoClient client;
|
||||
File bcodir;
|
||||
static Logger LOGGER = LogManager.getLogger(BCOManager.class);
|
||||
|
||||
public BCOManager(String doid, File bcoDir, String routerURI, SM2KeyPair keyPair) {
|
||||
this.bcodir = bcoDir;
|
||||
this.codeRepoDoid = CodeRepoClient.getDefaultCodeRepo(doid);
|
||||
this.routerURI = routerURI;
|
||||
this.keyPair = keyPair;
|
||||
}
|
||||
|
||||
public String download(String bcoId) {
|
||||
if (client == null) {
|
||||
EndpointConfig config = new EndpointConfig();
|
||||
config.routerURI = GlobalConf.instance.doaConf.lhsAddress;
|
||||
config.auditType = AuditType.None;
|
||||
AuditIrpClient irpClient = new AuditIrpClient(codeRepoDoid, config);
|
||||
client = new CodeRepoClient(codeRepoDoid, irpClient, keyPair);
|
||||
}
|
||||
YPKInfo ypkInfo = new YPKInfo();
|
||||
client.retrieveBCO(bcoId, new DoipMessageCallback() {
|
||||
@Override
|
||||
public void onResult(DoipMessage doipMessage) {
|
||||
String body = doipMessage.body.getDataAsJsonString();
|
||||
LOGGER.info("Retrieve " + bcoId + " Result:" + body);
|
||||
YPKInfo parsed = new Gson().fromJson(body, YPKInfo.class);
|
||||
if (parsed != null && parsed.md5 != null
|
||||
&& parsed.status == UploadingStatus.Finished) {
|
||||
ypkInfo.copyFrom(parsed);
|
||||
}
|
||||
synchronized (ypkInfo) {
|
||||
ypkInfo.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (ypkInfo) {
|
||||
try {
|
||||
if (ypkInfo.status == null)
|
||||
ypkInfo.wait(4000L);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
LOGGER.info("Download:" + new Gson().toJson(ypkInfo));
|
||||
if (ypkInfo.status == UploadingStatus.Finished) {
|
||||
File target = getYpkFile(ypkInfo);
|
||||
if (target.exists() && (client.calFileMd5(target.getAbsolutePath()) + ".ypk")
|
||||
.equals(target.getName()))
|
||||
return target.getAbsolutePath();
|
||||
DownloadProgressChecker checker = new DownloadProgressChecker();
|
||||
client.downloadYPK(bcoId, checker, target);
|
||||
LOGGER.info("start Download:" + new Gson().toJson(ypkInfo));
|
||||
checker.waitForResult(30000);
|
||||
LOGGER.info("checker:" + checker.isSuccess);
|
||||
if (checker.isSuccess)
|
||||
return target.getAbsolutePath();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static class DownloadProgressChecker implements CodeRepoClient.ProgressCallback {
|
||||
boolean finished = false;
|
||||
boolean isSuccess = false;
|
||||
|
||||
@Override
|
||||
public void onStart(String s) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgress(String s, int i, int i1) {
|
||||
LOGGER.info(s + " " + i + " /" + i1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish(String s) {
|
||||
finished = true;
|
||||
isSuccess = true;
|
||||
synchronized (this) {
|
||||
this.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(String s, DoipMessage doipMessage) {
|
||||
finished = true;
|
||||
isSuccess = false;
|
||||
synchronized (this) {
|
||||
this.notify();
|
||||
}
|
||||
LOGGER.info("[Download error]" + s + " doipMessage:"
|
||||
+ doipMessage.body.getDataAsJsonString());
|
||||
|
||||
}
|
||||
|
||||
public void waitForResult(long timeout) {
|
||||
synchronized (this) {
|
||||
if (!this.finished) {
|
||||
try {
|
||||
this.wait(timeout);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private File getYpkFile(YPKInfo ypkInfo) {
|
||||
File f = new File(bcodir, ypkInfo.pkgName);
|
||||
if (ypkInfo.version != null)
|
||||
f = new File(f, ypkInfo.version);
|
||||
f = new File(f, ypkInfo.md5 + ".ypk");
|
||||
return f;
|
||||
}
|
||||
|
||||
private static AuditIrpClient initIrpClient() {
|
||||
EndpointConfig config = new EndpointConfig();
|
||||
config.routerURI = GlobalConf.instance.doaConf.lhsAddress;
|
||||
config.auditType = AuditType.None;
|
||||
AuditIrpClient irpClient = new AuditIrpClient(GlobalConf.instance.doaConf.repoDoid, config);
|
||||
return irpClient;
|
||||
}
|
||||
}
|
@ -1,34 +1,55 @@
|
||||
package org.bdware.server.doip;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.*;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import org.bdware.doip.core.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.core.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.core.model.digitalObject.DigitalObject;
|
||||
import org.bdware.doip.core.model.digitalObject.DoType;
|
||||
import org.bdware.doip.core.model.digitalObject.Element;
|
||||
import org.bdware.doip.core.model.operations.BasicOperations;
|
||||
import org.bdware.doip.endpoint.doipServer.DoipServiceInfo;
|
||||
import org.bdware.doip.endpoint.doipServer.Op;
|
||||
import org.bdware.doip.endpoint.doipServer.RepositoryHandlerBase;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.doip.audit.server.DefaultRepoHandlerInjector;
|
||||
import org.bdware.doip.codec.digitalObject.DigitalObject;
|
||||
import org.bdware.doip.codec.digitalObject.DoType;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
|
||||
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.codec.operations.BasicOperations;
|
||||
import org.bdware.doip.endpoint.server.DoipServiceInfo;
|
||||
import org.bdware.doip.endpoint.server.Op;
|
||||
import org.bdware.doip.endpoint.server.RepositoryHandlerBase;
|
||||
import org.bdware.irp.stateinfo.StateInfoBase;
|
||||
import org.bdware.sc.ContractClient;
|
||||
import org.bdware.sc.ContractMeta;
|
||||
import org.bdware.sc.ContractStatusEnum;
|
||||
import org.bdware.sc.bean.Contract;
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.bean.FunctionDesp;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.db.CMTables;
|
||||
import org.bdware.sc.db.KeyValueDBUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bdware.server.doip.ContractRepositoryMain.currentIrpClient;
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
public class ContractRepositoryHandler extends RepositoryHandlerBase
|
||||
implements DefaultRepoHandlerInjector {
|
||||
BCOManager bcoManager;
|
||||
static Logger LOGGER = LogManager.getLogger(ContractRepositoryHandler.class);
|
||||
|
||||
public ContractRepositoryHandler(DoipServiceInfo info) {
|
||||
super(info);
|
||||
GlobalConf.DOAConf doaConf = GlobalConf.instance.doaConf;
|
||||
String bcoDir = GlobalConf.instance.bcoDir;
|
||||
bcoManager = new BCOManager(doaConf.repoDoid, new File(bcoDir), doaConf.lhsAddress,
|
||||
GlobalConf.instance.keyPair);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -38,6 +59,11 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
|
||||
@Override
|
||||
public DoipMessage handleListOps(DoipMessage doipMessage) {
|
||||
if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) {
|
||||
return replyStringWithStatus(doipMessage,
|
||||
"unsupported doid:" + doipMessage.header.parameters.id,
|
||||
DoipResponseCode.DoNotFound);
|
||||
}
|
||||
JsonArray ops = new JsonArray();
|
||||
ops.add(BasicOperations.Hello.getName());
|
||||
ops.add("call");
|
||||
@ -46,7 +72,126 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
|
||||
@Override
|
||||
public DoipMessage handleCreate(DoipMessage doipMessage) {
|
||||
return replyStringWithStatus(doipMessage, "not supported yet", DoipResponseCode.Declined);
|
||||
if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) {
|
||||
return replyStringWithStatus(doipMessage,
|
||||
"unsupported doid:" + doipMessage.header.parameters.id,
|
||||
DoipResponseCode.DoNotFound);
|
||||
}
|
||||
// TODD 验证签名。完事后进入
|
||||
if (doipMessage.credential != null && doipMessage.credential.getSigner() != null) {
|
||||
String permissions = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(),
|
||||
doipMessage.credential.getSigner());
|
||||
if (!permissions.contains("ContractInstanceManager")) {
|
||||
return replyStringWithStatus(doipMessage, "permission denied, role:" + permissions,
|
||||
DoipResponseCode.Declined);
|
||||
}
|
||||
}
|
||||
DigitalObject digitalObject = null;
|
||||
try {
|
||||
digitalObject = doipMessage.body.getDataAsDigitalObject();
|
||||
digitalObject.attributes.remove("verifiedPubKey");
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
return replyStringWithStatus(doipMessage, "exception:" + new String(bo.toByteArray()),
|
||||
DoipResponseCode.Invalid);
|
||||
}
|
||||
digitalObject.attributes.addProperty("owner", doipMessage.credential.getSigner());
|
||||
try {
|
||||
String result = startUsingJsonObject(digitalObject.attributes);
|
||||
// Please note startContractByYPK is invoked in sync mode method.
|
||||
return replyStringWithStatus(doipMessage, result, DoipResponseCode.Success);
|
||||
} catch (Exception e) {
|
||||
return replyStringWithStatus(doipMessage, e.getMessage(),
|
||||
DoipResponseCode.MoreThanOneErrors);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String startUsingJsonObject(JsonObject attributes) {
|
||||
LOGGER.info("create BDO, bCcoId:" + attributes.get("bcoId").getAsString());
|
||||
String path = null;
|
||||
if (attributes.has("bcoId"))
|
||||
path = bcoManager.download(attributes.get("bcoId").getAsString());
|
||||
if (path == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"exception, failed to locate bcoId:" + attributes.get("bcoId").getAsString());
|
||||
}
|
||||
attributes.addProperty("path", path);
|
||||
Contract c = new Contract();
|
||||
if (attributes.has("contractExecType"))
|
||||
c.setType(ContractExecType.valueOf(attributes.get("contractExecType").getAsString()));
|
||||
else
|
||||
c.setType(ContractExecType.Sole);
|
||||
if (attributes.has("shardingId"))
|
||||
c.setShardingId(Integer.valueOf(attributes.get("shardingId").getAsString()));
|
||||
else
|
||||
c.setShardingId(-1);
|
||||
c.setScript(path);
|
||||
c.setOwner(attributes.get("owner").getAsString());
|
||||
if (attributes.has("createParam")) {
|
||||
c.setCreateParam(attributes.get("createParam"));
|
||||
|
||||
}
|
||||
SM2KeyPair sm2Key = null;
|
||||
if (attributes.has("sm2KeyPair"))
|
||||
sm2Key = SM2KeyPair.fromJson(attributes.get("sm2KeyPair").getAsJsonObject().toString());
|
||||
if (sm2Key == null)
|
||||
CMActions.manager.allocateKeyIfNotExists(c);
|
||||
else {
|
||||
c.setKey(sm2Key.getPrivateKeyStr());
|
||||
c.setPublicKey(sm2Key.getPublicKeyStr());
|
||||
String contractID = String.valueOf(sm2Key.getPublicKeyStr().hashCode());
|
||||
c.setID(contractID); // contractID是根据hash算出来的
|
||||
}
|
||||
String result = CMActions.manager.startContract(c);
|
||||
registerBDOID(c.getID(), c.getPublicKey(), attributes.get("bcoId").getAsString(), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void registerBDOID(String contractID, String contractPubKey, String bcoId,
|
||||
String startResult) {
|
||||
try {
|
||||
|
||||
JsonObject startResultJO = JsonParser.parseString(startResult).getAsJsonObject();
|
||||
if (!startResultJO.get("status").getAsString().equals("Success"))
|
||||
return;
|
||||
int doipPort = 0;
|
||||
if (startResultJO.has("doipStartPort"))
|
||||
doipPort = startResultJO.get("doipStartPort").getAsInt();
|
||||
if (startResultJO.has("doipListenPort"))
|
||||
doipPort = startResultJO.get("doipListenPort").getAsInt();
|
||||
StateInfoBase base = new StateInfoBase();
|
||||
String repoId = GlobalConf.instance.doaConf.repoDoid;
|
||||
String uri = GlobalConf.instance.doaConf.doipAddress;
|
||||
int lastPos = uri.lastIndexOf(":");
|
||||
base.identifier = repoId + "/" + contractID;
|
||||
base.handleValues = new JsonObject();
|
||||
base.handleValues.addProperty("bdwType", "BDO");
|
||||
base.handleValues.addProperty("bcoId", bcoId);
|
||||
base.handleValues.addProperty("repoId", repoId);
|
||||
base.handleValues.addProperty("date", System.currentTimeMillis());
|
||||
if (doipPort > 0) {
|
||||
base.handleValues.addProperty("address", uri.substring(0, lastPos + 1) + doipPort);
|
||||
base.handleValues.addProperty("version", "2.1");
|
||||
base.handleValues.addProperty("protocol", "DOIP");
|
||||
}
|
||||
base.handleValues.addProperty("pubKey", contractPubKey);
|
||||
StateInfoBase oldBase = currentIrpClient.resolve(base.identifier, false);
|
||||
String registerResult = null;
|
||||
LOGGER.info("base:" + oldBase == null ? "NULL" : JsonUtil.toPrettyJson(oldBase));
|
||||
if (oldBase == null)
|
||||
registerResult = currentIrpClient.register(base);
|
||||
else
|
||||
registerResult = currentIrpClient.reRegister(base);
|
||||
// LOGGER.info("TORegister:" + new
|
||||
// GsonBuilder().setPrettyPrinting().create().toJson(base));
|
||||
LOGGER.info(base.identifier + " register result:" + registerResult);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// irp
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -56,36 +201,65 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
|
||||
@Override
|
||||
public DoipMessage handleDelete(DoipMessage doipMessage) {
|
||||
return replyStringWithStatus(doipMessage, "not supported yet", DoipResponseCode.Declined);
|
||||
String id = doipMessage.header.parameters.attributes.get("elementId").getAsString();
|
||||
id = id.replaceAll(".*/", "");
|
||||
LOGGER.info("handleDelete:" + id);
|
||||
// TODO judge whethere the do exists
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
|
||||
if (meta == null) {
|
||||
return replyStringWithStatus(doipMessage, "can't locate do",
|
||||
DoipResponseCode.DoNotFound);
|
||||
}
|
||||
if (meta.getStatus() == ContractStatusEnum.KILLED) {
|
||||
return replyStringWithStatus(doipMessage, "already deleted!",
|
||||
DoipResponseCode.Declined);
|
||||
}
|
||||
DoipMessage[] content = new DoipMessage[1];
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("action", "killContractProcess");
|
||||
String result = CMActions.manager.stopContract(meta.getID());
|
||||
return replyStringWithStatus(doipMessage, result, DoipResponseCode.Success);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DoipMessage handleRetrieve(DoipMessage doipMessage) {
|
||||
if (doipMessage.header.parameters.id.equals(GlobalConf.instance.doaConf.repoDoid)) {
|
||||
if (doipMessage.header.parameters.attributes == null
|
||||
|| !doipMessage.header.parameters.attributes.has("element"))
|
||||
return retrieveList(doipMessage);
|
||||
else
|
||||
return retrieveBDO(doipMessage,
|
||||
doipMessage.header.parameters.attributes.get("element").getAsString());
|
||||
} else
|
||||
return replyStringWithStatus(doipMessage, "no such do", DoipResponseCode.Declined);
|
||||
|
||||
}
|
||||
|
||||
private DoipMessage retrieveBDO(DoipMessage doipMessage, String idOrName) {
|
||||
ContractClient client = null;
|
||||
try {
|
||||
client = CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id);
|
||||
client = CMActions.manager.getContractClientByDoi(idOrName);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (client == null) {
|
||||
String doid = doipMessage.header.parameters.id;
|
||||
if (client == null) {
|
||||
String doid = idOrName;
|
||||
int off = doid.lastIndexOf("/");
|
||||
if (off > 0)
|
||||
doid = doid.substring(off + 1);
|
||||
client = CMActions.manager.getContractClientByDoi(doid);
|
||||
}
|
||||
if (client == null) {
|
||||
return replyStringWithStatus(
|
||||
doipMessage, "Contract instance not exist!", DoipResponseCode.DoNotFound);
|
||||
return replyStringWithStatus(doipMessage, "BDO instance not exist!",
|
||||
DoipResponseCode.DoNotFound);
|
||||
}
|
||||
byte[] data = doipMessage.body.getEncodedData();
|
||||
|
||||
if (data == null || data.length == 0) {
|
||||
DigitalObject contractDO = contract2DigitalObject(client);
|
||||
return replyDO(doipMessage, contractDO);
|
||||
JsonObject contractDO = contract2DigitalObject(client);
|
||||
return replyString(doipMessage, contractDO.toString());
|
||||
} else {
|
||||
JsonObject jo = JsonUtil.parseStringAsJsonObject(new String(data));
|
||||
List<JsonElement> result = new ArrayList<>();
|
||||
jo.addProperty("contractID", client.contractMeta.getID());
|
||||
jo.addProperty("bdoSuffix", client.contractMeta.getID());
|
||||
CMActions.executeContractInternal(jo, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject str) {
|
||||
@ -112,7 +286,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
result.add(new JsonPrimitive("Timeout!"));
|
||||
retStatus = DoipResponseCode.MoreThanOneErrors;
|
||||
}
|
||||
DigitalObject digitalObject = new DigitalObject(doipMessage.header.parameters.id, DoType.DO);
|
||||
DigitalObject digitalObject =
|
||||
new DigitalObject(doipMessage.header.parameters.id, DoType.DO);
|
||||
digitalObject.addAttribute("status", retStatus.getName());
|
||||
digitalObject.addAttribute("result", result.get(0));
|
||||
|
||||
@ -120,40 +295,57 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
}
|
||||
}
|
||||
|
||||
private DigitalObject contract2DigitalObject(ContractClient contractClient) {
|
||||
DigitalObject contractDO = new DigitalObject(contractClient.getContractDOI(), DoType.DO);
|
||||
contractDO.addAttribute("contractName", contractClient.getContractName());
|
||||
// contractDO.addAttribute("script",contract.contract.getScriptStr());
|
||||
contractDO.addAttribute("owner", contractClient.contractMeta.contract.getOwner());
|
||||
private DoipMessage retrieveList(DoipMessage message) {
|
||||
JsonArray listDO = new JsonArray();
|
||||
for (String key : CMActions.manager.statusRecorder.getStatus().keySet()) {
|
||||
ContractClient contractClient = CMActions.manager.getContractClientByDoi(key);
|
||||
if (contractClient != null) {
|
||||
JsonObject ele = new JsonObject();
|
||||
ele.addProperty("suffix", contractClient.getContractID());
|
||||
ele.addProperty("bdoName", contractClient.contractMeta.getName());
|
||||
ele.addProperty("status", contractClient.contractMeta.getStatus().toString());
|
||||
listDO.add(ele);
|
||||
}
|
||||
}
|
||||
return replyString(message, listDO.toString());
|
||||
}
|
||||
|
||||
private JsonObject contract2DigitalObject(ContractClient contractClient) {
|
||||
JsonObject contractDO = new JsonObject();
|
||||
contractDO.addProperty("suffix", contractClient.getContractID());
|
||||
contractDO.addProperty("bdoName", contractClient.getContractName());
|
||||
contractDO.addProperty("owner", contractClient.contractMeta.contract.getOwner());
|
||||
JsonArray operations = new JsonArray();
|
||||
contractDO.add("operations", operations);
|
||||
for (FunctionDesp f : contractClient.getExportedFunctions()) {
|
||||
Element fe = new Element(f.functionName, "function");
|
||||
fe.setAttribute("annotation", JsonUtil.toJson(f.annotations));
|
||||
contractDO.addElements(fe);
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("opeationName", f.functionName);
|
||||
jo.add("annotation", JsonUtil.parseObject(f.annotations));
|
||||
operations.add(jo);
|
||||
}
|
||||
return contractDO;
|
||||
}
|
||||
|
||||
@Op(op = BasicOperations.Extension, name = "call")
|
||||
public DoipMessage handleCall(DoipMessage doipMessage) {
|
||||
ContractClient cc = CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id);
|
||||
ContractClient cc =
|
||||
CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id);
|
||||
if (cc == null)
|
||||
return replyStringWithStatus(
|
||||
doipMessage, "Contract instance not exist!", DoipResponseCode.DoNotFound);
|
||||
return replyStringWithStatus(doipMessage, "Contract instance not exist!",
|
||||
DoipResponseCode.DoNotFound);
|
||||
// todo transform doipMessage to args
|
||||
JsonObject args = doipMessage.header.parameters.attributes;
|
||||
ContractRequest cr = new ContractRequest();
|
||||
|
||||
cr.setContractID(cc.getContractID());
|
||||
cr.setContractDOI(doipMessage.header.parameters.id);
|
||||
|
||||
if (args.has("withDynamicAnalysis"))
|
||||
cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
|
||||
if (args.has("withEvaluatesAnalysis"))
|
||||
cr.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
|
||||
|
||||
if (args.get("elementID") == null)
|
||||
return replyStringWithStatus(
|
||||
doipMessage, "missing elementID", DoipResponseCode.Invalid);
|
||||
return replyStringWithStatus(doipMessage, "missing elementID",
|
||||
DoipResponseCode.Invalid);
|
||||
cr.setAction(args.get("elementID").getAsString());
|
||||
|
||||
if (doipMessage.body.getEncodedData() != null)
|
||||
@ -163,7 +355,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
cr.setRequesterDOI(doipMessage.credential.getSigner());
|
||||
|
||||
String reqID;
|
||||
if (args.has("requestID")) reqID = args.get("requestID").getAsString();
|
||||
if (args.has("requestID"))
|
||||
reqID = args.get("requestID").getAsString();
|
||||
else {
|
||||
reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 1000);
|
||||
args.addProperty("requestID", reqID);
|
||||
@ -172,11 +365,43 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
|
||||
|
||||
String resp = CMActions.manager.execute(cr, null);
|
||||
|
||||
// StringContainer sc = new StringContainer();
|
||||
// SyncContainer sync = new SyncContainer(sc);
|
||||
// StringContainer sc = new StringContainer();
|
||||
// SyncContainer sync = new SyncContainer(sc);
|
||||
|
||||
return replyString(doipMessage, resp);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void injectCreate(DoipMessage request, DoipMessageFactory.DoipMessageBuilder builder) {
|
||||
builder.setDoipMessage(handleCreate(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectHello(DoipMessage request, DoipMessageFactory.DoipMessageBuilder builder) {
|
||||
builder.setDoipMessage(handleHello(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectListOps(DoipMessage doipMessage,
|
||||
DoipMessageFactory.DoipMessageBuilder builder) {
|
||||
builder.setDoipMessage(handleListOps(doipMessage));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectUpdate(DoipMessage request, DoipMessageFactory.DoipMessageBuilder builder) {
|
||||
// use default behavior
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectRetrieve(DoipMessage doipMessage,
|
||||
DoipMessageFactory.DoipMessageBuilder builder) {
|
||||
builder.setDoipMessage(handleRetrieve(doipMessage));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectDelete(DoipMessage doipMessage,
|
||||
DoipMessageFactory.DoipMessageBuilder builder) {
|
||||
builder.setDoipMessage(handleDelete(doipMessage));
|
||||
}
|
||||
}
|
||||
|
@ -1,50 +1,118 @@
|
||||
package org.bdware.server.doip;
|
||||
|
||||
import com.nimbusds.jose.jwk.JWK;
|
||||
import com.google.gson.Gson;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.doip.core.crypto.GlobalCertifications;
|
||||
import org.bdware.doip.core.doipMessage.DoipMessageFormat;
|
||||
import org.bdware.doip.core.utils.GlobalConfigurations;
|
||||
import org.bdware.doip.endpoint.doipServer.DoipListenerInfo;
|
||||
import org.bdware.doip.endpoint.doipServer.DoipServerImpl;
|
||||
import org.bdware.doip.endpoint.doipServer.DoipServiceInfo;
|
||||
import org.bdware.server.DOAConf;
|
||||
import org.bdware.doip.audit.EndpointConfig;
|
||||
import org.bdware.doip.audit.client.AuditIrpClient;
|
||||
import org.bdware.doip.audit.server.AuditDoipServer;
|
||||
import org.bdware.doip.audit.writer.AuditType;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
|
||||
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.encrypt.SM2Signer;
|
||||
import org.bdware.doip.endpoint.server.DoipListenerConfig;
|
||||
import org.bdware.doip.endpoint.server.DoipRequestHandler;
|
||||
import org.bdware.doip.endpoint.server.DoipServiceInfo;
|
||||
import org.bdware.sc.ContractMeta;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ContractRepositoryMain {
|
||||
private static final Logger LOGGER = LogManager.getLogger(ContractRepositoryMain.class);
|
||||
static DoipServerImpl currentServer;
|
||||
static AuditDoipServer currentServer;
|
||||
static LocalDoipFrowarder forwarder;
|
||||
static final String repoType = "BDRepo";
|
||||
public static AuditIrpClient currentIrpClient;
|
||||
public static ContractRepositoryHandler currentHandler;
|
||||
|
||||
public static void start() {
|
||||
try {
|
||||
String[] ipAndPort = GlobalConf.instance.masterAddress.split(":");
|
||||
String url = String.format("tcp://%s:%d", ipAndPort[0], Integer.valueOf(ipAndPort[1]) + 1);
|
||||
String doid = DOAConf.repoDoid;
|
||||
LOGGER.info("DOIPServer start:" + url);
|
||||
if (url == null || doid == null) {
|
||||
LOGGER.warn("missing args, failed to start! url:" + url + " doid:" + doid);
|
||||
LOGGER.info("DOIPServer, start" + new Gson().toJson(GlobalConf.instance.doaConf));
|
||||
String url = GlobalConf.instance.doaConf.doipAddress;
|
||||
forwarder = new LocalDoipFrowarder();
|
||||
if (url == null || GlobalConf.instance.doaConf.repoDoid.isEmpty()) {
|
||||
LOGGER.warn("missing args, failed to start! url:" + url + " doid:"
|
||||
+ GlobalConf.instance.doaConf.repoDoid);
|
||||
return;
|
||||
}
|
||||
if (currentServer != null) {
|
||||
currentServer.stop();
|
||||
}
|
||||
List<DoipListenerConfig> infos = new ArrayList<>();
|
||||
infos.add(new DoipListenerConfig(url, "2.1"));
|
||||
DoipServiceInfo info = new DoipServiceInfo(GlobalConf.instance.doaConf.repoDoid,
|
||||
GlobalConf.instance.keyPair.getPublicKeyStr(), repoType, infos);
|
||||
EndpointConfig config = new EndpointConfig();
|
||||
config.publicKey = GlobalConf.instance.keyPair.getPublicKeyStr();
|
||||
config.privateKey = GlobalConf.instance.keyPair.getPrivateKeyStr();
|
||||
config.auditType = AuditType.None;
|
||||
config.routerURI = GlobalConf.instance.doaConf.lhsAddress;
|
||||
config.repoName = GlobalConf.instance.doaConf.repoName;
|
||||
currentIrpClient = new AuditIrpClient(config);
|
||||
currentServer = new AuditDoipServer(config, info);
|
||||
currentHandler = new ContractRepositoryHandler(info);
|
||||
currentServer.setRepositoryHandler(currentHandler);
|
||||
DoipRequestHandler requestCallback = currentServer.getRequestCallback();
|
||||
SM2Signer signer = new SM2Signer(SM2KeyPair.fromJson(new Gson().toJson(config)));
|
||||
DelegateDoipHandler delegateHandler = new DelegateDoipHandler(requestCallback) {
|
||||
@Override
|
||||
protected DoipMessage delegateMessage(ChannelHandlerContext context,
|
||||
DoipMessage message) {
|
||||
DoipMessageFactory.DoipMessageBuilder builder =
|
||||
new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createResponse(DoipResponseCode.DelegateRequired, message);
|
||||
String id = message.header.parameters.id;
|
||||
id = id.replaceAll(".*/", "");
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
|
||||
if (enableDelegate(meta)) {
|
||||
LOGGER.info("delegate:" + message.requestID + " --> doipPort:"
|
||||
+ meta.contract.getDoipPort());
|
||||
// if port is near cmhttp server port
|
||||
builder.addAttributes("port", meta.contract.getDoipPort());
|
||||
DoipMessage ret;
|
||||
ret = builder.create();
|
||||
if (message.credential != null && message.credential.getSigner() != null) {
|
||||
signer.signMessage(ret);
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
LOGGER.info("forward:" + message.requestID + " --> doipPort:"
|
||||
+ meta.contract.getDoipPort());
|
||||
return forwarder.forward(meta, message);
|
||||
}
|
||||
|
||||
List<DoipListenerInfo> infos = new ArrayList<>();
|
||||
infos.add(new DoipListenerInfo(url, "2.1", DoipMessageFormat.PACKET));
|
||||
GlobalCertifications.jwKeyPair = JWK.parse("{\"kty\":\"EC\",\"d\":\"wMrGJjk877bKQ_AAvdb8DM1XpUFo6POSLYDXid6AfBw\",\"use\":\"sig\",\"crv\":\"P-256\",\"kid\":\"AIBD\\/dou.ADMIN\",\"x\":\"N0TydMwdnvkyHrXVh1zfc9_7DV6tRqfCGElhywzXJYo\",\"y\":\"rX5pGGML9efP2r8hoMSwxaR9gwVHmqaldsG-yfE_eUI\"}");
|
||||
GlobalConfigurations.userID = GlobalCertifications.jwKeyPair.getKeyID();
|
||||
DoipServiceInfo info = new DoipServiceInfo(doid, GlobalConfigurations.userID, "ContractRepo", infos);
|
||||
currentServer = new DoipServerImpl(info);
|
||||
currentServer.setRepositoryHandler(new ContractRepositoryHandler(info));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(ChannelHandlerContext context, DoipMessage message) {
|
||||
String id = message.header.parameters.id;
|
||||
if (id != null) {
|
||||
id = id.replaceAll(".*/", "");
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
|
||||
if (meta != null)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
currentServer.setRequestCallback(delegateHandler);
|
||||
currentServer.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.warn("load doip config exception!");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean enableDelegate(ContractMeta meta) {
|
||||
int port = meta.contract.getDoipPort();
|
||||
|
||||
int delta = Integer.valueOf(GlobalConf.instance.ipPort.split(":")[1]) - port;
|
||||
return Math.abs(delta) < 200;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package org.bdware.server.doip;
|
||||
|
||||
public class ContractRepositoryServer {
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.bdware.server.doip;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.endpoint.server.DoipRequestHandler;
|
||||
|
||||
public abstract class DelegateDoipHandler implements DoipRequestHandler {
|
||||
private final DoipRequestHandler original;
|
||||
|
||||
public DelegateDoipHandler(DoipRequestHandler original) {
|
||||
this.original = original;
|
||||
}
|
||||
|
||||
public DoipMessage onRequest(ChannelHandlerContext context, DoipMessage doipMessage) {
|
||||
if (accept(context, doipMessage)) {
|
||||
return delegateMessage(context, doipMessage);
|
||||
} else
|
||||
return original.onRequest(context, doipMessage);
|
||||
}
|
||||
|
||||
protected abstract DoipMessage delegateMessage(ChannelHandlerContext context,
|
||||
DoipMessage message);
|
||||
|
||||
public abstract boolean accept(ChannelHandlerContext context, DoipMessage message);
|
||||
}
|
61
src/main/java/org/bdware/server/doip/LocalDoipFrowarder.java
Normal file
61
src/main/java/org/bdware/server/doip/LocalDoipFrowarder.java
Normal file
@ -0,0 +1,61 @@
|
||||
package org.bdware.server.doip;
|
||||
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessage;
|
||||
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
|
||||
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
|
||||
import org.bdware.doip.endpoint.client.ClientConfig;
|
||||
import org.bdware.doip.endpoint.client.DoipClientImpl;
|
||||
import org.bdware.doip.endpoint.client.DoipMessageCallback;
|
||||
import org.bdware.sc.ContractMeta;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LocalDoipFrowarder {
|
||||
Map<Integer, DoipClientImpl> connections;
|
||||
|
||||
public LocalDoipFrowarder() {
|
||||
connections = new HashMap<>();
|
||||
}
|
||||
|
||||
public DoipMessage forward(ContractMeta meta, DoipMessage message) {
|
||||
DoipClientImpl connection;
|
||||
try {
|
||||
if (connections.containsKey(meta.contract.getDoipPort())) {
|
||||
connection = connections.get(meta.contract.getDoipPort());
|
||||
} else {
|
||||
connection = new DoipClientImpl();
|
||||
connection.connect(
|
||||
ClientConfig.fromUrl("tcp://127.0.0.1:" + meta.contract.getDoipPort()));
|
||||
connections.put(meta.contract.getDoipPort(), connection);
|
||||
}
|
||||
if (!connection.isConnected())
|
||||
connection.reconnect();
|
||||
DoipMessage[] result = new DoipMessage[1];
|
||||
connection.sendRawMessage(message, new DoipMessageCallback() {
|
||||
@Override
|
||||
public void onResult(DoipMessage msg) {
|
||||
synchronized (result) {
|
||||
result[0] = msg;
|
||||
result.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (result) {
|
||||
if (result[0] == null)
|
||||
result.wait(30000L);
|
||||
}
|
||||
return result[0];
|
||||
} catch (Exception e) {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
DoipMessageFactory.DoipMessageBuilder builder =
|
||||
new DoipMessageFactory.DoipMessageBuilder();
|
||||
builder.createResponse(DoipResponseCode.UnKnownError, message);
|
||||
builder.setBody(bo.toByteArray());
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
}
|
12
src/main/java/org/bdware/server/executor/Context.java
Normal file
12
src/main/java/org/bdware/server/executor/Context.java
Normal file
@ -0,0 +1,12 @@
|
||||
package org.bdware.server.executor;
|
||||
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
|
||||
public interface Context {
|
||||
|
||||
public String[] getMembers(String contractID);
|
||||
|
||||
public void sendToAgent(String pubkey, String content);
|
||||
|
||||
public void sleepWithTimeout(final String requestID, ResultCallback cb, int timeOut);
|
||||
}
|
@ -1,375 +1,378 @@
|
||||
package org.bdware.server.executor.consistency;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ComponedContractResult;
|
||||
import org.bdware.sc.ContractResult;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.conn.Node;
|
||||
import org.bdware.sc.conn.OnHashCallback;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.consistency.Committer;
|
||||
import org.bdware.sc.consistency.pbft.PBFTAlgorithm;
|
||||
import org.bdware.sc.consistency.pbft.PBFTMember;
|
||||
import org.bdware.sc.consistency.pbft.PBFTMessage;
|
||||
import org.bdware.sc.consistency.pbft.PBFTType;
|
||||
import org.bdware.sc.units.*;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.GlobalConf;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import org.bdware.server.trustedmodel.ContractCluster;
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
import org.bdware.server.trustedmodel.MultiReqSeq;
|
||||
import org.bdware.server.trustedmodel.ResultCollector;
|
||||
import org.bdware.units.NetworkManager;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
//TODO 追赶差下的调用
|
||||
public class PBFTExecutor implements ContractExecutor {
|
||||
private static final Logger LOGGER = LogManager.getLogger(PBFTExecutor.class);
|
||||
final Object lock = new Object();
|
||||
private final List<PubKeyNode> members;
|
||||
int resultCount;
|
||||
|
||||
AtomicInteger request_index = new AtomicInteger(0);
|
||||
// key为requestID,value为其seq
|
||||
Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
|
||||
Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
|
||||
// MultiPointContractInfo info;
|
||||
String contractID;
|
||||
PBFTAlgorithm pbft;
|
||||
ContractCluster contractCluster;
|
||||
boolean isMaster;
|
||||
|
||||
public PBFTExecutor(
|
||||
int c, String con_id, final String masterPubkey, String[] members) {
|
||||
resultCount = c;
|
||||
contractID = con_id;
|
||||
this.members = new ArrayList<>();
|
||||
isMaster = GlobalConf.getNodeID().equals(masterPubkey);
|
||||
pbft = new PBFTAlgorithm(isMaster);
|
||||
int count = 0;
|
||||
for (String mem : members) {
|
||||
PubKeyNode pubkeyNode = new PubKeyNode();
|
||||
pubkeyNode.pubkey = mem;
|
||||
PBFTMember pbftMember = new PBFTMember();
|
||||
pbftMember.isMaster = mem.equals(masterPubkey);
|
||||
pbft.addMember(pubkeyNode, pbftMember);
|
||||
this.members.add(pubkeyNode);
|
||||
if (GlobalConf.getNodeID().equals(mem)) {
|
||||
pbft.setSendID(count);
|
||||
}
|
||||
count++;
|
||||
}
|
||||
contractCluster = new ContractCluster(contractID, this.members);
|
||||
pbft.setConnection(contractCluster);
|
||||
final MultiContractMeta cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
pbft.setCommitter(new Committer() {
|
||||
@Override
|
||||
public void onCommit(ContractRequest data) {
|
||||
ResultCallback ret = null;
|
||||
final long startTime = System.currentTimeMillis();
|
||||
ret = new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("action", "receiveTrustfullyResult");
|
||||
SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
ret.put("responseID", data.getRequestID());
|
||||
ret.put("executeTime", (System.currentTimeMillis() - startTime) + "");
|
||||
ret.put("data", str);
|
||||
cei.setLastExeSeq(data.seq);
|
||||
NetworkManager.instance.sendToAgent(masterPubkey, JsonUtil.toJson(ret));
|
||||
}
|
||||
};
|
||||
CMActions.manager.executeLocallyAsync(data, ret, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onSyncMessage(Node node, byte[] data) {
|
||||
|
||||
pbft.onMessage(node, data);
|
||||
}
|
||||
|
||||
public void setSeq(int seq) {
|
||||
request_index = new AtomicInteger(seq);
|
||||
pbft.setAtomSeq(request_index.get());
|
||||
}
|
||||
|
||||
public ResultCallback createResultCallback(
|
||||
final String requestID,
|
||||
final ResultCallback originalCb,
|
||||
final int count,
|
||||
final int request_seq,
|
||||
final String contractID) {
|
||||
ComponedContractResult componedContractResult = new ComponedContractResult(count);
|
||||
// TODO 加对应的超时?
|
||||
return new ResultCollector(
|
||||
requestID, new ResultMerger(originalCb, count, request_seq, contractID), count);
|
||||
}
|
||||
|
||||
public void sendRequest(String id, ContractRequest req, ResultCallback collector) {
|
||||
// Map<String, Object> reqStr = new HashMap<>();
|
||||
// reqStr.put("uniReqID", id);
|
||||
// reqStr.put("data", req);
|
||||
// reqStr.put("action", "executeContractLocally");
|
||||
ContractRequest cr2 = ContractRequest.parse(req.toByte());
|
||||
cr2.setRequestID(id);
|
||||
PBFTMessage request = new PBFTMessage();
|
||||
request.setOrder(req.seq);
|
||||
request.setType(PBFTType.Request);
|
||||
request.setContent(cr2.toByte());
|
||||
for (PubKeyNode node : members) {
|
||||
if (!NetworkManager.instance.hasAgentConnection(node.pubkey)) {
|
||||
LOGGER.warn("cmNode " + node.pubkey.substring(0, 5) + " is null");
|
||||
collector.onResult(
|
||||
"{\"status\":\"Error\",\"result\":\"node offline\","
|
||||
+ "\"nodeID\":\""
|
||||
+ node
|
||||
+ "\","
|
||||
+ "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
// } else if (MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
|
||||
// != RecoverFlag.Fine) {
|
||||
// collector.onResult(
|
||||
// "{\"status\":\"Error\",\"result\":\"node recovering\","
|
||||
// + "\"nodeID\":\""
|
||||
// + node
|
||||
// + "\","
|
||||
// + "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
// contractCluster.sendMessage(node, request.getBytes());
|
||||
} else {
|
||||
contractCluster.sendMessage(node, request.getBytes());
|
||||
}
|
||||
}
|
||||
// master负责缓存请求
|
||||
if (!MasterServerTCPAction.requestCache.containsKey(contractID)) {
|
||||
MasterServerTCPAction.requestCache.put(contractID, new RequestCache());
|
||||
}
|
||||
// TODO 多调多统一个seq的有多个请求,这个需要改
|
||||
String[] nodes =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID());
|
||||
LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean checkCurNodeNumValid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) {
|
||||
LOGGER.debug(JsonUtil.toJson(req));
|
||||
MultiContractMeta meta = CMActions.manager.multiContractRecorder.getMultiContractMeta(req.getContractID());
|
||||
if (meta == null || !meta.isMaster()) {
|
||||
CMActions.manager.executeContractOnOtherNodes(req, rc);
|
||||
return;
|
||||
}
|
||||
req.setContractID(CMActions.manager.getContractIDByName(req.getContractID()));
|
||||
|
||||
// 三个相同requestID进来的时候,会有冲突。
|
||||
// 仅在此处有冲突么?
|
||||
// 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。
|
||||
|
||||
// 如果是多点合约的请求,A1、A2、A3的序号应该一致,不能分配一个新的seq,根据requestID判断是否不需要重新分配一个序号
|
||||
//TODO seqMap memory leak
|
||||
//TODO
|
||||
//TODO
|
||||
if (null != requestID && requestID.endsWith("_mul")) {
|
||||
synchronized (lock) {
|
||||
if (seqMap.containsKey(requestID)) {
|
||||
req.seq = seqMap.get(requestID).seq;
|
||||
} else {
|
||||
req.seq = request_index.getAndIncrement();
|
||||
seqMap.put(requestID, new MultiReqSeq(req.seq));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
req.seq = request_index.getAndIncrement();
|
||||
}
|
||||
req.needSeq = true;
|
||||
String id =
|
||||
System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq;
|
||||
LOGGER.info("execute receive requestID=" + requestID + " msgID=" + id);
|
||||
|
||||
if (checkCurNodeNumValid()) {
|
||||
LOGGER.debug("checkCurNodeNumValid=true");
|
||||
ResultCallback collector =
|
||||
createResultCallback(id, rc, resultCount, req.seq, req.getContractID());
|
||||
MasterServerTCPAction.sync.sleep(id, collector);
|
||||
LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq);
|
||||
sendRequest(id, req, collector);
|
||||
} else {
|
||||
LOGGER.debug("invalidNodeNumOnResult");
|
||||
request_index.getAndDecrement();
|
||||
ContractResult finalResult =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive("node number unavailable, request refused."));
|
||||
rc.onResult(JsonUtil.toJson(finalResult));
|
||||
}
|
||||
|
||||
// }
|
||||
|
||||
/* // 三个相同requestID进来的时候,会有冲突。
|
||||
// 仅在此处有冲突么?
|
||||
// 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。
|
||||
req.seq = request_index.getAndIncrement();
|
||||
req.needSeq = true;
|
||||
ResultCallback collector = createResultCallback(id, rc, resultCount, req.getContractID());
|
||||
MasterServerTCPAction.sync.sleep(id, collector);
|
||||
sendRequest(id, req, collector);*/
|
||||
}
|
||||
|
||||
// 清理缓存的多点合约请求序号
|
||||
public void clearCache() {
|
||||
final long time = System.currentTimeMillis() - 30000L;
|
||||
seqMap.entrySet()
|
||||
.removeIf(
|
||||
entry -> {
|
||||
MultiReqSeq cache = entry.getValue();
|
||||
if (null == cache) {
|
||||
return true;
|
||||
}
|
||||
return cache.startTime < time;
|
||||
});
|
||||
}
|
||||
|
||||
public static class ResultMerger extends ResultCallback {
|
||||
ComponedContractResult componedContractResult;
|
||||
AtomicInteger order;
|
||||
String contractID;
|
||||
int count;
|
||||
int request_seq;
|
||||
ResultCallback originalCallback;
|
||||
Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
|
||||
|
||||
ResultMerger(
|
||||
final ResultCallback originalCb,
|
||||
final int count,
|
||||
final int request_seq,
|
||||
final String contractID) {
|
||||
originalCallback = originalCb;
|
||||
this.count = count;
|
||||
this.request_seq = request_seq;
|
||||
this.contractID = contractID;
|
||||
componedContractResult = new ComponedContractResult(count);
|
||||
order = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
public String getContractID() {
|
||||
return contractID;
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return "contractID="
|
||||
+ contractID
|
||||
+ " 收到第 "
|
||||
+ order
|
||||
+ " 个节点回复 : "
|
||||
+ " order="
|
||||
+ order
|
||||
+ " count="
|
||||
+ count
|
||||
+ " ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
// TODO 必须在这里聚合。
|
||||
// str的data是个ContractResult
|
||||
// 在这儿也是返回个ContractResult
|
||||
try {
|
||||
LOGGER.debug("a result of contract" + contractID + ": " + str);
|
||||
JsonObject obj = JsonParser.parseString(str).getAsJsonObject();
|
||||
if (obj.has("nodeID")) {
|
||||
String id = obj.get("nodeID").getAsString();
|
||||
if (nodeIDs.contains(id)) {
|
||||
LOGGER.debug(
|
||||
"ignored result because the result of node "
|
||||
+ id.substring(0, 5)
|
||||
+ " has been received");
|
||||
return;
|
||||
}
|
||||
nodeIDs.add(id);
|
||||
}
|
||||
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"contractID=%s received=%s order=%d count=%d",
|
||||
contractID, str, order.get(), count));
|
||||
componedContractResult.add(obj);
|
||||
// 收集到所有结果
|
||||
if (order.incrementAndGet() == count) {
|
||||
ContractResult finalResult = componedContractResult.figureFinalResult();
|
||||
finalResult.needSeq = true;
|
||||
finalResult.seq = request_seq;
|
||||
|
||||
// if (null == finalResult) {
|
||||
// finalResult =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Exception,
|
||||
// new JsonPrimitive(
|
||||
// "no nore than half of the
|
||||
// consistent result"));
|
||||
// originalCallback.onResult(new
|
||||
// Gson().toJson(finalResult));
|
||||
// } else {
|
||||
originalCallback.onResult(JsonUtil.toJson(finalResult));
|
||||
// }
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"%d results are the same: %s",
|
||||
finalResult.size, finalResult.result));
|
||||
|
||||
// 集群中事务序号+1
|
||||
CMActions.manager.multiContractRecorder
|
||||
.getMultiContractMeta(contractID)
|
||||
.nextSeqAtMaster();
|
||||
|
||||
// recover,其中无状态合约CP出错无需恢复
|
||||
Set<String> nodesID = componedContractResult.getProblemNodes();
|
||||
if (null == nodesID || nodesID.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (String nodeID : nodesID) {
|
||||
LOGGER.warn("node fails! " + nodeID);
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
== RecoverFlag.Fine) {
|
||||
MasterServerRecoverMechAction.recoverStatus
|
||||
.get(nodeID)
|
||||
.put(contractID, RecoverFlag.ToRecover);
|
||||
}
|
||||
}
|
||||
for (String nodeID : nodesID) {
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
== RecoverFlag.ToRecover) {
|
||||
LOGGER.warn("node in recover " + nodeID);
|
||||
|
||||
// 因为该节点结果有误,所以即时是stableMode也认为trans记录不可信
|
||||
// 直接通过load别的节点来恢复
|
||||
MasterServerRecoverMechAction.restartContractFromCommonMode(
|
||||
nodeID, contractID);
|
||||
}
|
||||
}
|
||||
}
|
||||
// clearCache();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.warn("result exception!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// package org.bdware.server.executor.consistency;
|
||||
//
|
||||
// import com.google.gson.JsonObject;
|
||||
// import com.google.gson.JsonParser;
|
||||
// import com.google.gson.JsonPrimitive;
|
||||
// import org.apache.logging.log4j.LogManager;
|
||||
// import org.apache.logging.log4j.Logger;
|
||||
// import org.bdware.sc.ComponedContractResult;
|
||||
// import org.bdware.sc.ContractResult;
|
||||
// import org.bdware.sc.bean.ContractRequest;
|
||||
// import org.bdware.sc.conn.Node;
|
||||
// import org.bdware.sc.conn.OnHashCallback;
|
||||
// import org.bdware.sc.conn.ResultCallback;
|
||||
// import org.bdware.sc.consistency.Committer;
|
||||
// import org.bdware.sc.consistency.pbft.PBFTAlgorithm;
|
||||
// import org.bdware.sc.consistency.pbft.PBFTMember;
|
||||
// import org.bdware.sc.consistency.pbft.PBFTMessage;
|
||||
// import org.bdware.sc.consistency.pbft.PBFTType;
|
||||
// import org.bdware.sc.units.*;
|
||||
// import org.bdware.sc.util.JsonUtil;
|
||||
// import org.bdware.server.GlobalConf;
|
||||
// import org.bdware.server.action.CMActions;
|
||||
// import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
// import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
// import org.bdware.server.trustedmodel.ContractCluster;
|
||||
// import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
// import org.bdware.server.trustedmodel.MultiReqSeq;
|
||||
// import org.bdware.server.trustedmodel.ResultCollector;
|
||||
// import org.bdware.units.NetworkManager;
|
||||
// import org.zz.gmhelper.SM2KeyPair;
|
||||
//
|
||||
// import java.util.*;
|
||||
// import java.util.concurrent.ConcurrentHashMap;
|
||||
// import java.util.concurrent.atomic.AtomicInteger;
|
||||
//
|
||||
//// TODO 追赶差下的调用
|
||||
// public class PBFTExecutor implements ContractExecutor {
|
||||
// private static final Logger LOGGER = LogManager.getLogger(PBFTExecutor.class);
|
||||
// final Object lock = new Object();
|
||||
// private final List<PubKeyNode> members;
|
||||
// int resultCount;
|
||||
//
|
||||
// AtomicInteger request_index = new AtomicInteger(0);
|
||||
// // key为requestID,value为其seq
|
||||
// Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
|
||||
// Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
|
||||
// // MultiPointContractInfo info;
|
||||
// String contractID;
|
||||
// PBFTAlgorithm pbft;
|
||||
// ContractCluster contractCluster;
|
||||
// boolean isMaster;
|
||||
//
|
||||
// public PBFTExecutor(
|
||||
// int c, String con_id, final String masterPubkey, String[] members) {
|
||||
// resultCount = c;
|
||||
// contractID = con_id;
|
||||
// this.members = new ArrayList<>();
|
||||
// isMaster = GlobalConf.getNodeID().equals(masterPubkey);
|
||||
// pbft = new PBFTAlgorithm(isMaster);
|
||||
// int count = 0;
|
||||
// for (String mem : members) {
|
||||
// PubKeyNode pubkeyNode = new PubKeyNode();
|
||||
// pubkeyNode.pubkey = mem;
|
||||
// PBFTMember pbftMember = new PBFTMember();
|
||||
// pbftMember.isMaster = mem.equals(masterPubkey);
|
||||
// pbft.addMember(pubkeyNode, pbftMember);
|
||||
// this.members.add(pubkeyNode);
|
||||
// if (GlobalConf.getNodeID().equals(mem)) {
|
||||
// pbft.setSendID(count);
|
||||
// }
|
||||
// count++;
|
||||
// }
|
||||
// contractCluster = new ContractCluster(contractID, this.members);
|
||||
// pbft.setConnection(contractCluster);
|
||||
// final MultiContractMeta cei =
|
||||
// CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
// pbft.setCommitter(new Committer() {
|
||||
// @Override
|
||||
// public void onCommit(ContractRequest data) {
|
||||
// ResultCallback ret = null;
|
||||
// final long startTime = System.currentTimeMillis();
|
||||
// ret = new ResultCallback() {
|
||||
// @Override
|
||||
// public void onResult(String str) {
|
||||
// Map<String, String> ret = new HashMap<>();
|
||||
// ret.put("action", "receiveTrustfullyResult");
|
||||
// SM2KeyPair keyPair = GlobalConf.instance.keyPair;
|
||||
// ret.put("nodeID", keyPair.getPublicKeyStr());
|
||||
// ret.put("responseID", data.getRequestID());
|
||||
// ret.put("executeTime", (System.currentTimeMillis() - startTime) + "");
|
||||
// ret.put("data", str);
|
||||
// cei.setLastExeSeq(data.seq);
|
||||
// NetworkManager.instance.sendToAgent(masterPubkey, JsonUtil.toJson(ret));
|
||||
// }
|
||||
// };
|
||||
// CMActions.manager.executeLocallyAsync(data, ret, null);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// public void onSyncMessage(Node node, byte[] data) {
|
||||
//
|
||||
// pbft.onMessage(node, data);
|
||||
// }
|
||||
//
|
||||
// public void setSeq(int seq) {
|
||||
// request_index = new AtomicInteger(seq);
|
||||
// pbft.setAtomSeq(request_index.get());
|
||||
// }
|
||||
//
|
||||
// public ResultCallback createResultCallback(
|
||||
// final String requestID,
|
||||
// final ResultCallback originalCb,
|
||||
// final int count,
|
||||
// final int request_seq,
|
||||
// final String contractID) {
|
||||
// ComponedContractResult componedContractResult = new ComponedContractResult(count);
|
||||
// // TODO 加对应的超时?
|
||||
// return new ResultCollector(
|
||||
// requestID, new ResultMerger(originalCb, count, request_seq, contractID), count);
|
||||
// }
|
||||
//
|
||||
// public void sendRequest(String id, ContractRequest req, ResultCallback collector) {
|
||||
//// Map<String, Object> reqStr = new HashMap<>();
|
||||
//// reqStr.put("uniReqID", id);
|
||||
//// reqStr.put("data", req);
|
||||
//// reqStr.put("action", "executeContractLocally");
|
||||
// ContractRequest cr2 = ContractRequest.parse(req.toByte());
|
||||
// cr2.setRequestID(id);
|
||||
// PBFTMessage request = new PBFTMessage();
|
||||
// request.setOrder(req.seq);
|
||||
// request.setType(PBFTType.Request);
|
||||
// request.setContent(cr2.toByte());
|
||||
// for (PubKeyNode node : members) {
|
||||
// if (!NetworkManager.instance.hasAgentConnection(node.pubkey)) {
|
||||
// LOGGER.warn("cmNode " + node.pubkey.substring(0, 5) + " is null");
|
||||
// collector.onResult(
|
||||
// "{\"status\":\"Error\",\"result\":\"node offline\","
|
||||
// + "\"nodeID\":\""
|
||||
// + node
|
||||
// + "\","
|
||||
// + "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
//// } else if (MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
|
||||
//// != RecoverFlag.Fine) {
|
||||
//// collector.onResult(
|
||||
//// "{\"status\":\"Error\",\"result\":\"node recovering\","
|
||||
//// + "\"nodeID\":\""
|
||||
//// + node
|
||||
//// + "\","
|
||||
//// + "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
//// contractCluster.sendMessage(node, request.getBytes());
|
||||
// } else {
|
||||
// contractCluster.sendMessage(node, request.getBytes());
|
||||
// }
|
||||
// }
|
||||
// // master负责缓存请求
|
||||
// if (!MasterServerTCPAction.requestCache.containsKey(contractID)) {
|
||||
// MasterServerTCPAction.requestCache.put(contractID, new RequestCache());
|
||||
// }
|
||||
// // TODO 多调多统一个seq的有多个请求,这个需要改
|
||||
// String[] nodes =
|
||||
// CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
// LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID());
|
||||
// LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes));
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public boolean checkCurNodeNumValid() {
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
|
||||
// {
|
||||
// LOGGER.debug(JsonUtil.toJson(req));
|
||||
// MultiContractMeta meta =
|
||||
// CMActions.manager.multiContractRecorder.getMultiContractMeta(req.getContractID());
|
||||
// if (meta == null || !meta.isMaster()) {
|
||||
// CMActions.manager.executeContractOnOtherNodes(req, rc);
|
||||
// return;
|
||||
// }
|
||||
// req.setContractID(CMActions.manager.getContractIDByName(req.getContractID()));
|
||||
//
|
||||
// // 三个相同requestID进来的时候,会有冲突。
|
||||
// // 仅在此处有冲突么?
|
||||
// // 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。
|
||||
//
|
||||
// // 如果是多点合约的请求,A1、A2、A3的序号应该一致,不能分配一个新的seq,根据requestID判断是否不需要重新分配一个序号
|
||||
// //TODO seqMap memory leak
|
||||
// //TODO
|
||||
// //TODO
|
||||
// if (null != requestID && requestID.endsWith("_mul")) {
|
||||
// synchronized (lock) {
|
||||
// if (seqMap.containsKey(requestID)) {
|
||||
// req.seq = seqMap.get(requestID).seq;
|
||||
// } else {
|
||||
// req.seq = request_index.getAndIncrement();
|
||||
// seqMap.put(requestID, new MultiReqSeq(req.seq));
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// req.seq = request_index.getAndIncrement();
|
||||
// }
|
||||
// req.needSeq = true;
|
||||
// String id =
|
||||
// System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq;
|
||||
// LOGGER.info("execute receive requestID=" + requestID + " msgID=" + id);
|
||||
//
|
||||
// if (checkCurNodeNumValid()) {
|
||||
// LOGGER.debug("checkCurNodeNumValid=true");
|
||||
// ResultCallback collector =
|
||||
// createResultCallback(id, rc, resultCount, req.seq, req.getContractID());
|
||||
// MasterServerTCPAction.sync.sleep(id, collector);
|
||||
// LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq);
|
||||
// sendRequest(id, req, collector);
|
||||
// } else {
|
||||
// LOGGER.debug("invalidNodeNumOnResult");
|
||||
// request_index.getAndDecrement();
|
||||
// ContractResult finalResult =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Error,
|
||||
// new JsonPrimitive("node number unavailable, request refused."));
|
||||
// rc.onResult(JsonUtil.toJson(finalResult));
|
||||
// }
|
||||
//
|
||||
// // }
|
||||
//
|
||||
// /* // 三个相同requestID进来的时候,会有冲突。
|
||||
// // 仅在此处有冲突么?
|
||||
// // 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。
|
||||
// req.seq = request_index.getAndIncrement();
|
||||
// req.needSeq = true;
|
||||
// ResultCallback collector = createResultCallback(id, rc, resultCount, req.getContractID());
|
||||
// MasterServerTCPAction.sync.sleep(id, collector);
|
||||
// sendRequest(id, req, collector);*/
|
||||
// }
|
||||
//
|
||||
// // 清理缓存的多点合约请求序号
|
||||
// public void clearCache() {
|
||||
// final long time = System.currentTimeMillis() - 30000L;
|
||||
// seqMap.entrySet()
|
||||
// .removeIf(
|
||||
// entry -> {
|
||||
// MultiReqSeq cache = entry.getValue();
|
||||
// if (null == cache) {
|
||||
// return true;
|
||||
// }
|
||||
// return cache.startTime < time;
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// public static class ResultMerger extends ResultCallback {
|
||||
// ComponedContractResult componedContractResult;
|
||||
// AtomicInteger order;
|
||||
// String contractID;
|
||||
// int count;
|
||||
// int request_seq;
|
||||
// ResultCallback originalCallback;
|
||||
// Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
|
||||
//
|
||||
// ResultMerger(
|
||||
// final ResultCallback originalCb,
|
||||
// final int count,
|
||||
// final int request_seq,
|
||||
// final String contractID) {
|
||||
// originalCallback = originalCb;
|
||||
// this.count = count;
|
||||
// this.request_seq = request_seq;
|
||||
// this.contractID = contractID;
|
||||
// componedContractResult = new ComponedContractResult(count);
|
||||
// order = new AtomicInteger(0);
|
||||
// }
|
||||
//
|
||||
// public String getContractID() {
|
||||
// return contractID;
|
||||
// }
|
||||
//
|
||||
// public String getInfo() {
|
||||
// return "contractID="
|
||||
// + contractID
|
||||
// + " 收到第 "
|
||||
// + order
|
||||
// + " 个节点回复 : "
|
||||
// + " order="
|
||||
// + order
|
||||
// + " count="
|
||||
// + count
|
||||
// + " ";
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onResult(String str) {
|
||||
// // TODO 必须在这里聚合。
|
||||
// // str的data是个ContractResult
|
||||
// // 在这儿也是返回个ContractResult
|
||||
// try {
|
||||
// LOGGER.debug("a result of contract" + contractID + ": " + str);
|
||||
// JsonObject obj = JsonParser.parseString(str).getAsJsonObject();
|
||||
// if (obj.has("nodeID")) {
|
||||
// String id = obj.get("nodeID").getAsString();
|
||||
// if (nodeIDs.contains(id)) {
|
||||
// LOGGER.debug(
|
||||
// "ignored result because the result of node "
|
||||
// + id.substring(0, 5)
|
||||
// + " has been received");
|
||||
// return;
|
||||
// }
|
||||
// nodeIDs.add(id);
|
||||
// }
|
||||
//
|
||||
// LOGGER.debug(
|
||||
// String.format(
|
||||
// "contractID=%s received=%s order=%d count=%d",
|
||||
// contractID, str, order.get(), count));
|
||||
// componedContractResult.add(obj);
|
||||
// // 收集到所有结果
|
||||
// if (order.incrementAndGet() == count) {
|
||||
// ContractResult finalResult = componedContractResult.figureFinalResult();
|
||||
// finalResult.needSeq = true;
|
||||
// finalResult.seq = request_seq;
|
||||
//
|
||||
// // if (null == finalResult) {
|
||||
// // finalResult =
|
||||
// // new ContractResult(
|
||||
// // ContractResult.Status.Exception,
|
||||
// // new JsonPrimitive(
|
||||
// // "no nore than half of the
|
||||
// // consistent result"));
|
||||
// // originalCallback.onResult(new
|
||||
// // Gson().toJson(finalResult));
|
||||
// // } else {
|
||||
// originalCallback.onResult(JsonUtil.toJson(finalResult));
|
||||
// // }
|
||||
// LOGGER.debug(
|
||||
// String.format(
|
||||
// "%d results are the same: %s",
|
||||
// finalResult.size, finalResult.result));
|
||||
//
|
||||
// // 集群中事务序号+1
|
||||
// CMActions.manager.multiContractRecorder
|
||||
// .getMultiContractMeta(contractID)
|
||||
// .nextSeqAtMaster();
|
||||
//
|
||||
// // recover,其中无状态合约CP出错无需恢复
|
||||
// Set<String> nodesID = componedContractResult.getProblemNodes();
|
||||
// if (null == nodesID || nodesID.isEmpty()) {
|
||||
// return;
|
||||
// }
|
||||
// for (String nodeID : nodesID) {
|
||||
// LOGGER.warn("node fails! " + nodeID);
|
||||
// if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
// == RecoverFlag.Fine) {
|
||||
// MasterServerRecoverMechAction.recoverStatus
|
||||
// .get(nodeID)
|
||||
// .put(contractID, RecoverFlag.ToRecover);
|
||||
// }
|
||||
// }
|
||||
// for (String nodeID : nodesID) {
|
||||
// if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
// == RecoverFlag.ToRecover) {
|
||||
// LOGGER.warn("node in recover " + nodeID);
|
||||
//
|
||||
// // 因为该节点结果有误,所以即时是stableMode也认为trans记录不可信
|
||||
// // 直接通过load别的节点来恢复
|
||||
// MasterServerRecoverMechAction.restartContractFromCommonMode(
|
||||
// nodeID, contractID);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // clearCache();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// LOGGER.warn("result exception!");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
@ -40,11 +40,10 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
// key为requestID,value为其seq
|
||||
Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
|
||||
Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
|
||||
// MultiPointContractInfo info;
|
||||
// MultiPointContractInfo info;
|
||||
String contractID;
|
||||
|
||||
public RequestAllExecutor(
|
||||
ContractExecType t, int c, String con_id) {
|
||||
public RequestAllExecutor(ContractExecType t, int c, String con_id) {
|
||||
type = t;
|
||||
resultCount = c;
|
||||
contractID = con_id;
|
||||
@ -54,16 +53,13 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
request_index = new AtomicInteger(seq);
|
||||
}
|
||||
|
||||
public ResultCallback createResultCallback(
|
||||
final String requestID,
|
||||
final ResultCallback originalCb,
|
||||
final int count,
|
||||
final int request_seq,
|
||||
public ResultCallback createResultCallback(final String requestID,
|
||||
final ResultCallback originalCb, final int count, final int request_seq,
|
||||
final String contractID) {
|
||||
ComponedContractResult componedContractResult = new ComponedContractResult(count);
|
||||
// TODO 加对应的超时?
|
||||
return new ResultCollector(
|
||||
requestID, new ResultMerger(originalCb, count, request_seq, contractID), count);
|
||||
return new ResultCollector(requestID,
|
||||
new ResultMerger(originalCb, count, request_seq, contractID), count);
|
||||
}
|
||||
|
||||
public void sendRequest(String id, ContractRequest req, ResultCallback collector) {
|
||||
@ -82,8 +78,8 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
|
||||
LOGGER.debug(JsonUtil.toJson(req));
|
||||
|
||||
String[] nodes =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
String[] nodes = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getMembers();
|
||||
LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID());
|
||||
LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes));
|
||||
for (String node : nodes) {
|
||||
@ -91,19 +87,13 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
if (!NetworkManager.instance.hasAgentConnection(node)) {
|
||||
LOGGER.warn("cmNode " + node.substring(0, 5) + " is null");
|
||||
collector.onResult(
|
||||
"{\"status\":\"Error\",\"result\":\"node offline\","
|
||||
+ "\"nodeID\":\""
|
||||
+ node
|
||||
+ "\","
|
||||
+ "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
} else if (MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
|
||||
!= RecoverFlag.Fine) {
|
||||
"{\"status\":\"Error\",\"result\":\"node offline\"," + "\"nodeID\":\""
|
||||
+ node + "\"," + "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
} else if (MasterServerRecoverMechAction.recoverStatus.get(node)
|
||||
.get(contractID) != RecoverFlag.Fine) {
|
||||
collector.onResult(
|
||||
"{\"status\":\"Error\",\"result\":\"node recovering\","
|
||||
+ "\"nodeID\":\""
|
||||
+ node
|
||||
+ "\","
|
||||
+ "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
"{\"status\":\"Error\",\"result\":\"node recovering\"," + "\"nodeID\":\""
|
||||
+ node + "\"," + "\"action\":\"onExecuteContractTrustfully\"}");
|
||||
NetworkManager.instance.sendToAgent(node, sendStr);
|
||||
} else {
|
||||
LOGGER.info("send request to cmNode " + node.substring(0, 5));
|
||||
@ -113,16 +103,17 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
}
|
||||
|
||||
public boolean checkCurNodeNumValid() {
|
||||
String[] nodes =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
String[] nodes = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getMembers();
|
||||
int validNode = 0;
|
||||
Map<String, String> mapResult = new HashMap<>();
|
||||
for (String node : nodes) {
|
||||
mapResult.put(node.substring(0, 5), String.format("%s %s", NetworkManager.instance.hasAgentConnection(node) + "",
|
||||
MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)));
|
||||
mapResult.put(node.substring(0, 5),
|
||||
String.format("%s %s", NetworkManager.instance.hasAgentConnection(node) + "",
|
||||
MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)));
|
||||
if (NetworkManager.instance.hasAgentConnection(node)
|
||||
&& MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
|
||||
== RecoverFlag.Fine) {
|
||||
&& MasterServerRecoverMechAction.recoverStatus.get(node)
|
||||
.get(contractID) == RecoverFlag.Fine) {
|
||||
validNode++;
|
||||
}
|
||||
}
|
||||
@ -136,9 +127,11 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) {
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc,
|
||||
OnHashCallback hcb) {
|
||||
LOGGER.debug(JsonUtil.toJson(req));
|
||||
MultiContractMeta meta = CMActions.manager.multiContractRecorder.getMultiContractMeta(req.getContractID());
|
||||
MultiContractMeta meta =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(req.getContractID());
|
||||
if (meta == null || !meta.isMaster()) {
|
||||
CMActions.manager.executeContractOnOtherNodes(req, rc);
|
||||
return;
|
||||
@ -150,9 +143,9 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
// 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。
|
||||
|
||||
// 如果是多点合约的请求,A1、A2、A3的序号应该一致,不能分配一个新的seq,根据requestID判断是否不需要重新分配一个序号
|
||||
//TODO seqMap memory leak
|
||||
//TODO
|
||||
//TODO
|
||||
// TODO seqMap memory leak
|
||||
// TODO
|
||||
// TODO
|
||||
if (null != requestID && requestID.endsWith("_mul")) {
|
||||
synchronized (lock) {
|
||||
if (seqMap.containsKey(requestID)) {
|
||||
@ -180,37 +173,32 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
} else {
|
||||
LOGGER.debug("invalidNodeNumOnResult");
|
||||
request_index.getAndDecrement();
|
||||
ContractResult finalResult =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive("node number unavailable, request refused."));
|
||||
ContractResult finalResult = new ContractResult(ContractResult.Status.Error,
|
||||
new JsonPrimitive("node number unavailable, request refused."));
|
||||
rc.onResult(JsonUtil.toJson(finalResult));
|
||||
}
|
||||
|
||||
// }
|
||||
|
||||
/* // 三个相同requestID进来的时候,会有冲突。
|
||||
// 仅在此处有冲突么?
|
||||
// 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。
|
||||
req.seq = request_index.getAndIncrement();
|
||||
req.needSeq = true;
|
||||
ResultCallback collector = createResultCallback(id, rc, resultCount, req.getContractID());
|
||||
MasterServerTCPAction.sync.sleep(id, collector);
|
||||
sendRequest(id, req, collector);*/
|
||||
/*
|
||||
* // 三个相同requestID进来的时候,会有冲突。 // 仅在此处有冲突么? //
|
||||
* 这里是从MasterServer->MasterClient,请求的是"executeContractLocally"。 req.seq =
|
||||
* request_index.getAndIncrement(); req.needSeq = true; ResultCallback collector =
|
||||
* createResultCallback(id, rc, resultCount, req.getContractID());
|
||||
* MasterServerTCPAction.sync.sleep(id, collector); sendRequest(id, req, collector);
|
||||
*/
|
||||
}
|
||||
|
||||
// 清理缓存的多点合约请求序号
|
||||
public void clearCache() {
|
||||
final long time = System.currentTimeMillis() - 30000L;
|
||||
seqMap.entrySet()
|
||||
.removeIf(
|
||||
entry -> {
|
||||
MultiReqSeq cache = entry.getValue();
|
||||
if (null == cache) {
|
||||
return true;
|
||||
}
|
||||
return cache.startTime < time;
|
||||
});
|
||||
seqMap.entrySet().removeIf(entry -> {
|
||||
MultiReqSeq cache = entry.getValue();
|
||||
if (null == cache) {
|
||||
return true;
|
||||
}
|
||||
return cache.startTime < time;
|
||||
});
|
||||
}
|
||||
|
||||
public static class ResultMerger extends ResultCallback {
|
||||
@ -222,10 +210,7 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
ResultCallback originalCallback;
|
||||
Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
|
||||
|
||||
ResultMerger(
|
||||
final ResultCallback originalCb,
|
||||
final int count,
|
||||
final int request_seq,
|
||||
ResultMerger(final ResultCallback originalCb, final int count, final int request_seq,
|
||||
final String contractID) {
|
||||
originalCallback = originalCb;
|
||||
this.count = count;
|
||||
@ -240,16 +225,8 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return "contractID="
|
||||
+ contractID
|
||||
+ " 收到第 "
|
||||
+ order
|
||||
+ " 个节点回复 : "
|
||||
+ " order="
|
||||
+ order
|
||||
+ " count="
|
||||
+ count
|
||||
+ " ";
|
||||
return "contractID=" + contractID + " 收到第 " + order + " 个节点回复 : " + " order=" + order
|
||||
+ " count=" + count + " ";
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -263,19 +240,15 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
if (obj.has("nodeID")) {
|
||||
String id = obj.get("nodeID").getAsString();
|
||||
if (nodeIDs.contains(id)) {
|
||||
LOGGER.debug(
|
||||
"ignored result because the result of node "
|
||||
+ id.substring(0, 5)
|
||||
+ " has been received");
|
||||
LOGGER.debug("ignored result because the result of node "
|
||||
+ id.substring(0, 5) + " has been received");
|
||||
return;
|
||||
}
|
||||
nodeIDs.add(id);
|
||||
}
|
||||
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"contractID=%s received=%s order=%d count=%d",
|
||||
contractID, str, order.get(), count));
|
||||
LOGGER.debug(String.format("contractID=%s received=%s order=%d count=%d",
|
||||
contractID, str, order.get(), count));
|
||||
componedContractResult.add(obj);
|
||||
// 收集到所有结果
|
||||
if (order.incrementAndGet() == count) {
|
||||
@ -283,26 +256,23 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
finalResult.needSeq = true;
|
||||
finalResult.seq = request_seq;
|
||||
|
||||
// if (null == finalResult) {
|
||||
// finalResult =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Exception,
|
||||
// new JsonPrimitive(
|
||||
// "no nore than half of the
|
||||
// if (null == finalResult) {
|
||||
// finalResult =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Exception,
|
||||
// new JsonPrimitive(
|
||||
// "no nore than half of the
|
||||
// consistent result"));
|
||||
// originalCallback.onResult(new
|
||||
// originalCallback.onResult(new
|
||||
// Gson().toJson(finalResult));
|
||||
// } else {
|
||||
// } else {
|
||||
originalCallback.onResult(JsonUtil.toJson(finalResult));
|
||||
// }
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"%d results are the same: %s",
|
||||
finalResult.size, finalResult.result));
|
||||
// }
|
||||
LOGGER.debug(String.format("%d results are the same: %s", finalResult.size,
|
||||
finalResult.result));
|
||||
|
||||
// 集群中事务序号+1
|
||||
CMActions.manager.multiContractRecorder
|
||||
.getMultiContractMeta(contractID)
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.nextSeqAtMaster();
|
||||
|
||||
// recover,其中无状态合约CP出错无需恢复
|
||||
@ -312,26 +282,25 @@ public class RequestAllExecutor implements ContractExecutor {
|
||||
}
|
||||
for (String nodeID : nodesID) {
|
||||
LOGGER.warn("node fails! " + nodeID);
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
== RecoverFlag.Fine) {
|
||||
MasterServerRecoverMechAction.recoverStatus
|
||||
.get(nodeID)
|
||||
.put(contractID, RecoverFlag.ToRecover);
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID)
|
||||
.get(contractID) == RecoverFlag.Fine) {
|
||||
MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID,
|
||||
RecoverFlag.ToRecover);
|
||||
}
|
||||
}
|
||||
for (String nodeID : nodesID) {
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
== RecoverFlag.ToRecover) {
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID)
|
||||
.get(contractID) == RecoverFlag.ToRecover) {
|
||||
LOGGER.warn("node in recover " + nodeID);
|
||||
|
||||
// 因为该节点结果有误,所以即时是stableMode也认为trans记录不可信
|
||||
// 直接通过load别的节点来恢复
|
||||
MasterServerRecoverMechAction.restartContractFromCommonMode(
|
||||
nodeID, contractID);
|
||||
MasterServerRecoverMechAction.restartContractFromCommonMode(nodeID,
|
||||
contractID);
|
||||
}
|
||||
}
|
||||
}
|
||||
// clearCache();
|
||||
// clearCache();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.warn("result exception!");
|
||||
|
@ -1,400 +1,403 @@
|
||||
package org.bdware.server.executor.unconsistency;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ComponedContractResult;
|
||||
import org.bdware.sc.ContractMeta;
|
||||
import org.bdware.sc.ContractResult;
|
||||
import org.bdware.sc.bean.*;
|
||||
import org.bdware.sc.conn.OnHashCallback;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.units.MultiContractMeta;
|
||||
import org.bdware.sc.units.RecoverFlag;
|
||||
import org.bdware.sc.units.RequestCache;
|
||||
import org.bdware.sc.units.ResultCache;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
import org.bdware.server.trustedmodel.MultiReqSeq;
|
||||
import org.bdware.server.trustedmodel.ResultCollector;
|
||||
import org.bdware.units.NetworkManager;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
// 改为MultiPointCooperationExecutor
|
||||
public class MultiPointCooperationExecutor implements ContractExecutor {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MultiPointCooperationExecutor.class);
|
||||
final Object lock = new Object();
|
||||
int resultCount;
|
||||
AtomicInteger request_index = new AtomicInteger(0);
|
||||
ContractExecType type;
|
||||
// key为requestID,value为其seq
|
||||
Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
|
||||
Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
|
||||
// MultiPointContractInfo info;
|
||||
MultiContractMeta multiMeta;
|
||||
String contractID;
|
||||
|
||||
public MultiPointCooperationExecutor(ContractExecType t, int c, String con_id) {
|
||||
LOGGER.info("-- sharding executor---");
|
||||
type = t;
|
||||
resultCount = c;
|
||||
contractID = con_id;
|
||||
multiMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
}
|
||||
|
||||
public void setSeq(int seq) {
|
||||
request_index = new AtomicInteger(seq);
|
||||
}
|
||||
|
||||
public ResultCallback createResultCallback(
|
||||
final String requestID,
|
||||
final ResultCallback originalCb,
|
||||
final int count,
|
||||
final int request_seq,
|
||||
final String contractID, JoinInfo joinInfo) {
|
||||
// TODO 加对应的超时?
|
||||
return new ResultCollector(
|
||||
requestID,
|
||||
new MultiPointCooperationExecutor.ResultMerger(originalCb, count, request_seq, contractID, joinInfo),
|
||||
count); // 把count改成了1,设置成获得1个响应就行
|
||||
}
|
||||
|
||||
public void sendRequest(String id, ContractRequest req, String[] nodes) {
|
||||
Map<String, Object> reqStr = new HashMap<>();
|
||||
reqStr.put("uniReqID", id);
|
||||
reqStr.put("data", req);
|
||||
req.needSeq = false;
|
||||
reqStr.put("action", "executeContractLocally");
|
||||
String sendStr = JsonUtil.toJson(reqStr);
|
||||
// master负责缓存请求
|
||||
if (!MasterServerTCPAction.requestCache.containsKey(contractID)) {
|
||||
MasterServerTCPAction.requestCache.put(contractID, new RequestCache());
|
||||
}
|
||||
// TODO 多调多统一个seq的有多个请求,这个需要改
|
||||
MasterServerTCPAction.requestCache.get(contractID).put(req.seq, sendStr);
|
||||
LOGGER.debug(JsonUtil.toJson(req));
|
||||
LOGGER.info("node size = " + nodes.length);
|
||||
LOGGER.debug("nodes:" + JsonUtil.toJson(nodes));
|
||||
for (String node : nodes) {
|
||||
LOGGER.info(
|
||||
"[sendRequests] get cmNode "
|
||||
+ node.substring(0, 5)
|
||||
+ " not null "
|
||||
+ "RequestAllExecutor 发送请求给 "
|
||||
+ node.substring(0, 5));
|
||||
NetworkManager.instance.sendToAgent(node, sendStr);
|
||||
}
|
||||
}
|
||||
|
||||
private String[] getAccordingToRouteInfo(RouteInfo routeInfo, ContractRequest req, String[] members) {
|
||||
try {
|
||||
int val;
|
||||
switch (routeInfo.useDefault) {
|
||||
case byRequester:
|
||||
val =
|
||||
new BigInteger(req.getRequester(), 16)
|
||||
.mod(new BigInteger("" + members.length))
|
||||
.intValue();
|
||||
while (val < 0) {
|
||||
val = val + members.length;
|
||||
}
|
||||
return new String[]{members[val]};
|
||||
case byArgHash:
|
||||
val = req.getArg().hashCode();
|
||||
val = val % members.length;
|
||||
while (val < 0) {
|
||||
val += members.length;
|
||||
}
|
||||
return new String[]{members[val]};
|
||||
case byTarget:
|
||||
JsonObject jo = req.getArg().getAsJsonObject();
|
||||
val =
|
||||
new BigInteger(jo.get("target").getAsString(), 16)
|
||||
.mod(new BigInteger("" + members.length))
|
||||
.intValue();
|
||||
while (val < 0) {
|
||||
val = val + members.length;
|
||||
}
|
||||
return new String[]{members[val]};
|
||||
default:
|
||||
return members;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return members;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkCurNodeNumValid() {
|
||||
LOGGER.info("checkCurNodeNumValid");
|
||||
String[] nodes = multiMeta.getMembers();
|
||||
// List<String> nodes = info.members;
|
||||
int validNode = 0;
|
||||
for (String node : nodes) {
|
||||
if (NetworkManager.instance.hasAgentConnection(node)
|
||||
&& MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
|
||||
== RecoverFlag.Fine) {
|
||||
validNode++;
|
||||
}
|
||||
}
|
||||
int c = resultCount;
|
||||
if (type == ContractExecType.Sharding) c = (int) Math.ceil((double) c / 2);
|
||||
LOGGER.info("c=" + c + " validNode=" + validNode);
|
||||
return validNode >= c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) {
|
||||
LOGGER.info("[MultiPointCooperationExecutor] execute " + JsonUtil.toJson(req));
|
||||
// 获得action 函数名
|
||||
LOGGER.info("action is : " + req.getAction());
|
||||
req.setContractID(CMActions.manager.getContractIDByName(req.getContractID()));
|
||||
if (requestID != null && requestID.endsWith("_mul")) {
|
||||
synchronized (lock) {
|
||||
if (seqMap.containsKey(requestID)) {
|
||||
req.seq = seqMap.get(requestID).seq;
|
||||
} else {
|
||||
req.seq = request_index.getAndIncrement();
|
||||
seqMap.put(requestID, new MultiReqSeq(req.seq));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
req.seq = request_index.getAndIncrement();
|
||||
}
|
||||
req.needSeq = true;
|
||||
String id =
|
||||
System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq;
|
||||
LOGGER.info("execute receive requestID= " + requestID + " msgID=" + id);
|
||||
if (checkCurNodeNumValid()) { // 校验成功 current node num 合法
|
||||
LOGGER.info("checkCurNodeNumValid true");
|
||||
ContractMeta meta =
|
||||
CMActions.manager.statusRecorder.getContractMeta(req.getContractID());
|
||||
FunctionDesp fun = meta.getExportedFunction(req.getAction());
|
||||
ResultCallback collector;
|
||||
// TODO @fanbo 下面的count 1要改,应该是根据route的规则来。
|
||||
//Count 根据join规则来。
|
||||
//nodes 根据route规则来。
|
||||
JoinInfo joinInfo = fun.joinInfo;
|
||||
RouteInfo routeInfo = fun.routeInfo;
|
||||
int count = getJoinCount(joinInfo, contractID);
|
||||
LOGGER.info("requestID=" + requestID + " join Count: " + count);
|
||||
|
||||
String[] members = multiMeta.getMembers();
|
||||
String[] nodes = getAccordingToRouteInfo(routeInfo, req, members);
|
||||
if (nodes.length < count) {
|
||||
count = nodes.length;
|
||||
}
|
||||
collector =
|
||||
createResultCallback(id, rc, count, req.seq, req.getContractID(), joinInfo); // 初始化结果收集器
|
||||
MasterServerTCPAction.sync.sleep(id, collector);
|
||||
LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq);
|
||||
sendRequest(id, req, nodes); // 发送请求
|
||||
} else {
|
||||
LOGGER.info("invalidNodeNumOnResult");
|
||||
request_index.getAndDecrement();
|
||||
ContractResult finalResult =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive("node number unavailbale,request refused."));
|
||||
rc.onResult(JsonUtil.toJson(finalResult));
|
||||
}
|
||||
}
|
||||
|
||||
private int getJoinCount(JoinInfo joinInfo, String contractID) {
|
||||
if (joinInfo == null) return resultCount;
|
||||
if (joinInfo.joinCount.isJsonPrimitive() && joinInfo.joinCount.getAsJsonPrimitive().isNumber()) {
|
||||
return joinInfo.joinCount.getAsJsonPrimitive().getAsInt();
|
||||
}
|
||||
try {
|
||||
ContractRequest cr = new ContractRequest();
|
||||
cr.setContractID(contractID);
|
||||
cr.setAction(joinInfo.joinCount.getAsString());
|
||||
//TODO Arg需要好好设计一下。
|
||||
//TODO 又好用又简单的那种设计
|
||||
//TODO
|
||||
cr.setArg("");
|
||||
String result = CMActions.manager.executeLocally(cr, null);
|
||||
return JsonUtil.parseString(result).getAsJsonObject().get("result").getAsInt();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 清理缓存的多点合约请求序号
|
||||
public void clearCache() {
|
||||
final long time = System.currentTimeMillis() - 30000L;
|
||||
seqMap.entrySet()
|
||||
.removeIf(
|
||||
entry -> {
|
||||
MultiReqSeq cache = entry.getValue();
|
||||
if (null == cache) {
|
||||
return true;
|
||||
}
|
||||
return cache.startTime < time;
|
||||
});
|
||||
}
|
||||
|
||||
public static class ResultMerger extends ResultCallback {
|
||||
ComponedContractResult componedContractResult;
|
||||
AtomicInteger order;
|
||||
String contractID;
|
||||
int count; // 记录有多少个节点
|
||||
int request_seq;
|
||||
ResultCallback originalCallback;
|
||||
Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
|
||||
JoinInfo joinInfo;
|
||||
|
||||
ResultMerger(
|
||||
final ResultCallback originalCb,
|
||||
final int count,
|
||||
final int request_seq,
|
||||
final String contractID,
|
||||
final JoinInfo joinInfo) {
|
||||
originalCallback = originalCb;
|
||||
this.count = count;
|
||||
this.request_seq = request_seq;
|
||||
this.contractID = contractID;
|
||||
componedContractResult = new ComponedContractResult(count);
|
||||
order = new AtomicInteger(0);
|
||||
this.joinInfo = joinInfo;
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return "contractID="
|
||||
+ contractID
|
||||
+ " 收到第 "
|
||||
+ order
|
||||
+ " 个节点回复 : "
|
||||
+ " order="
|
||||
+ order
|
||||
+ " count="
|
||||
+ count
|
||||
+ " ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
// TODO 必须在这里聚合。
|
||||
// str的data是个ContractResult
|
||||
// 在这儿也是返回个ContractResult
|
||||
try {
|
||||
LOGGER.info(str);
|
||||
JsonObject obj = JsonParser.parseString(str).getAsJsonObject();
|
||||
String id = obj.get("nodeID").getAsString();
|
||||
if (nodeIDs.contains(id)) {
|
||||
LOGGER.info("已经收到节点 " + id.substring(0, 5) + " 的结果,该结果被忽略");
|
||||
return;
|
||||
}
|
||||
nodeIDs.add(id);
|
||||
LOGGER.info(
|
||||
"contractID="
|
||||
+ contractID
|
||||
+ " 收到第 "
|
||||
+ order
|
||||
+ " 个节点回复 : "
|
||||
+ str
|
||||
+ " order="
|
||||
+ order
|
||||
+ " count="
|
||||
+ count);
|
||||
componedContractResult.add(obj);
|
||||
// 收集到所有结果
|
||||
if (order.incrementAndGet() == count) {
|
||||
ContractResult finalResult = componedContractResult.mergeFinalResult();
|
||||
|
||||
finalResult.needSeq = true;
|
||||
finalResult.seq = request_seq;
|
||||
|
||||
// if (null == finalResult) {
|
||||
// finalResult =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Exception,
|
||||
// new JsonPrimitive(
|
||||
// "no nore than half of the
|
||||
// consistent result"));
|
||||
// originalCallback.onResult(new
|
||||
// Gson().toJson(finalResult));
|
||||
// } else {
|
||||
if (joinInfo != null) {
|
||||
handleJoinInfo(finalResult, joinInfo);
|
||||
}
|
||||
originalCallback.onResult(JsonUtil.toJson(finalResult));
|
||||
// }
|
||||
LOGGER.info(
|
||||
"本次执行最终结果为 " + finalResult.size + "个节点合并的,结果为 " + finalResult.result);
|
||||
|
||||
// 集群中事务序号+1
|
||||
// MasterServerTCPAction.contractID2Members.get(contractID).nextSeq();
|
||||
CMActions.manager
|
||||
.multiContractRecorder
|
||||
.getMultiContractMeta(contractID)
|
||||
.nextSeqAtMaster();
|
||||
// recover,其中无状态合约CP出错无需恢复
|
||||
Set<String> nodesID = componedContractResult.getProblemNodes();
|
||||
if (null == nodesID || nodesID.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (String nodeID : nodesID) {
|
||||
LOGGER.info("结果出现问题的节点有:" + nodeID);
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
== RecoverFlag.Fine) {
|
||||
MasterServerRecoverMechAction.recoverStatus
|
||||
.get(nodeID)
|
||||
.put(contractID, RecoverFlag.ToRecover);
|
||||
}
|
||||
}
|
||||
for (String nodeID : nodesID) {
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
== RecoverFlag.ToRecover) {
|
||||
LOGGER.info("问题节点开始恢复:" + nodeID);
|
||||
|
||||
// 因为该节点结果有误,所以即时是stableMode也认为trans记录不可信
|
||||
// 直接通过load别的节点来恢复
|
||||
MasterServerRecoverMechAction.restartContractFromCommonMode(
|
||||
nodeID, contractID);
|
||||
}
|
||||
}
|
||||
}
|
||||
// clearCache();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.info("本次执行最终结果为有异常");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleJoinInfo(ContractResult finalResult, JoinInfo joinInfo) {
|
||||
JsonObject jo = finalResult.result.getAsJsonObject();
|
||||
if (joinInfo != null && joinInfo.joinRule != null) {
|
||||
//TODO 不应该是double 类型
|
||||
switch (joinInfo.joinRule) {
|
||||
case "add":
|
||||
double val = 0;
|
||||
for (String key : jo.keySet()) {
|
||||
val += jo.get(key).getAsDouble();
|
||||
}
|
||||
finalResult.result = new JsonPrimitive(val);
|
||||
break;
|
||||
case "multiply":
|
||||
val = 1;
|
||||
for (String key : jo.keySet()) {
|
||||
val *= jo.get(key).getAsDouble();
|
||||
}
|
||||
finalResult.result = new JsonPrimitive(val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// package org.bdware.server.executor.unconsistency;
|
||||
//
|
||||
// import com.google.gson.JsonObject;
|
||||
// import com.google.gson.JsonParser;
|
||||
// import com.google.gson.JsonPrimitive;
|
||||
// import org.apache.logging.log4j.LogManager;
|
||||
// import org.apache.logging.log4j.Logger;
|
||||
// import org.bdware.sc.ComponedContractResult;
|
||||
// import org.bdware.sc.ContractMeta;
|
||||
// import org.bdware.sc.ContractResult;
|
||||
// import org.bdware.sc.bean.*;
|
||||
// import org.bdware.sc.conn.OnHashCallback;
|
||||
// import org.bdware.sc.conn.ResultCallback;
|
||||
// import org.bdware.sc.units.MultiContractMeta;
|
||||
// import org.bdware.sc.units.RecoverFlag;
|
||||
// import org.bdware.sc.units.RequestCache;
|
||||
// import org.bdware.sc.units.ResultCache;
|
||||
// import org.bdware.sc.util.JsonUtil;
|
||||
// import org.bdware.server.action.CMActions;
|
||||
// import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
// import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
// import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
// import org.bdware.server.trustedmodel.MultiReqSeq;
|
||||
// import org.bdware.server.trustedmodel.ResultCollector;
|
||||
// import org.bdware.units.NetworkManager;
|
||||
//
|
||||
// import java.math.BigInteger;
|
||||
// import java.util.HashMap;
|
||||
// import java.util.HashSet;
|
||||
// import java.util.Map;
|
||||
// import java.util.Set;
|
||||
// import java.util.concurrent.ConcurrentHashMap;
|
||||
// import java.util.concurrent.atomic.AtomicInteger;
|
||||
//
|
||||
//// 改为MultiPointCooperationExecutor
|
||||
// public class MultiPointCooperationExecutor implements ContractExecutor {
|
||||
// private static final Logger LOGGER = LogManager.getLogger(MultiPointCooperationExecutor.class);
|
||||
// final Object lock = new Object();
|
||||
// int resultCount;
|
||||
// AtomicInteger request_index = new AtomicInteger(0);
|
||||
// ContractExecType type;
|
||||
// // key为requestID,value为其seq
|
||||
// Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
|
||||
// Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
|
||||
// // MultiPointContractInfo info;
|
||||
// MultiContractMeta multiMeta;
|
||||
// String contractID;
|
||||
//
|
||||
// public MultiPointCooperationExecutor(ContractExecType t, int c, String con_id) {
|
||||
// LOGGER.info("-- sharding executor---");
|
||||
// type = t;
|
||||
// resultCount = c;
|
||||
// contractID = con_id;
|
||||
// multiMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
// }
|
||||
//
|
||||
// public void setSeq(int seq) {
|
||||
// request_index = new AtomicInteger(seq);
|
||||
// }
|
||||
//
|
||||
// public ResultCallback createResultCallback(
|
||||
// final String requestID,
|
||||
// final ResultCallback originalCb,
|
||||
// final int count,
|
||||
// final int request_seq,
|
||||
// final String contractID, JoinInfo joinInfo) {
|
||||
// // TODO 加对应的超时?
|
||||
// return new ResultCollector(
|
||||
// requestID,
|
||||
// new MultiPointCooperationExecutor.ResultMerger(originalCb, count, request_seq, contractID,
|
||||
// joinInfo),
|
||||
// count); // 把count改成了1,设置成获得1个响应就行
|
||||
// }
|
||||
//
|
||||
// public void sendRequest(String id, ContractRequest req, String[] nodes) {
|
||||
// Map<String, Object> reqStr = new HashMap<>();
|
||||
// reqStr.put("uniReqID", id);
|
||||
// reqStr.put("data", req);
|
||||
// req.needSeq = false;
|
||||
// reqStr.put("action", "executeContractLocally");
|
||||
// String sendStr = JsonUtil.toJson(reqStr);
|
||||
// // master负责缓存请求
|
||||
// if (!MasterServerTCPAction.requestCache.containsKey(contractID)) {
|
||||
// MasterServerTCPAction.requestCache.put(contractID, new RequestCache());
|
||||
// }
|
||||
// // TODO 多调多统一个seq的有多个请求,这个需要改
|
||||
// MasterServerTCPAction.requestCache.get(contractID).put(req.seq, sendStr);
|
||||
// LOGGER.debug(JsonUtil.toJson(req));
|
||||
// LOGGER.info("node size = " + nodes.length);
|
||||
// LOGGER.debug("nodes:" + JsonUtil.toJson(nodes));
|
||||
// for (String node : nodes) {
|
||||
// LOGGER.info(
|
||||
// "[sendRequests] get cmNode "
|
||||
// + node.substring(0, 5)
|
||||
// + " not null "
|
||||
// + "RequestAllExecutor 发送请求给 "
|
||||
// + node.substring(0, 5));
|
||||
// NetworkManager.instance.sendToAgent(node, sendStr);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private String[] getAccordingToRouteInfo(RouteInfo routeInfo, ContractRequest req, String[]
|
||||
// members) {
|
||||
// try {
|
||||
// int val;
|
||||
// switch (routeInfo.useDefault) {
|
||||
// case byRequester:
|
||||
// val =
|
||||
// new BigInteger(req.getRequester(), 16)
|
||||
// .mod(new BigInteger("" + members.length))
|
||||
// .intValue();
|
||||
// while (val < 0) {
|
||||
// val = val + members.length;
|
||||
// }
|
||||
// return new String[]{members[val]};
|
||||
// case byArgHash:
|
||||
// val = req.getArg().hashCode();
|
||||
// val = val % members.length;
|
||||
// while (val < 0) {
|
||||
// val += members.length;
|
||||
// }
|
||||
// return new String[]{members[val]};
|
||||
// case byTarget:
|
||||
// JsonObject jo = req.getArg().getAsJsonObject();
|
||||
// val =
|
||||
// new BigInteger(jo.get("target").getAsString(), 16)
|
||||
// .mod(new BigInteger("" + members.length))
|
||||
// .intValue();
|
||||
// while (val < 0) {
|
||||
// val = val + members.length;
|
||||
// }
|
||||
// return new String[]{members[val]};
|
||||
// default:
|
||||
// return members;
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// return members;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public boolean checkCurNodeNumValid() {
|
||||
// LOGGER.info("checkCurNodeNumValid");
|
||||
// String[] nodes = multiMeta.getMembers();
|
||||
// // List<String> nodes = info.members;
|
||||
// int validNode = 0;
|
||||
// for (String node : nodes) {
|
||||
// if (NetworkManager.instance.hasAgentConnection(node)
|
||||
// && MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
|
||||
// == RecoverFlag.Fine) {
|
||||
// validNode++;
|
||||
// }
|
||||
// }
|
||||
// int c = resultCount;
|
||||
// if (type == ContractExecType.Sharding) c = (int) Math.ceil((double) c / 2);
|
||||
// LOGGER.info("c=" + c + " validNode=" + validNode);
|
||||
// return validNode >= c;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
|
||||
// {
|
||||
// LOGGER.info("[MultiPointCooperationExecutor] execute " + JsonUtil.toJson(req));
|
||||
// // 获得action 函数名
|
||||
// LOGGER.info("action is : " + req.getAction());
|
||||
// req.setContractID(CMActions.manager.getContractIDByName(req.getContractID()));
|
||||
// if (requestID != null && requestID.endsWith("_mul")) {
|
||||
// synchronized (lock) {
|
||||
// if (seqMap.containsKey(requestID)) {
|
||||
// req.seq = seqMap.get(requestID).seq;
|
||||
// } else {
|
||||
// req.seq = request_index.getAndIncrement();
|
||||
// seqMap.put(requestID, new MultiReqSeq(req.seq));
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// req.seq = request_index.getAndIncrement();
|
||||
// }
|
||||
// req.needSeq = true;
|
||||
// String id =
|
||||
// System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq;
|
||||
// LOGGER.info("execute receive requestID= " + requestID + " msgID=" + id);
|
||||
// if (checkCurNodeNumValid()) { // 校验成功 current node num 合法
|
||||
// LOGGER.info("checkCurNodeNumValid true");
|
||||
// ContractMeta meta =
|
||||
// CMActions.manager.statusRecorder.getContractMeta(req.getContractID());
|
||||
// FunctionDesp fun = meta.getExportedFunction(req.getAction());
|
||||
// ResultCallback collector;
|
||||
// // TODO @fanbo 下面的count 1要改,应该是根据route的规则来。
|
||||
// //Count 根据join规则来。
|
||||
// //nodes 根据route规则来。
|
||||
// JoinInfo joinInfo = fun.joinInfo;
|
||||
// RouteInfo routeInfo = fun.routeInfo;
|
||||
// int count = getJoinCount(joinInfo, contractID);
|
||||
// LOGGER.info("requestID=" + requestID + " join Count: " + count);
|
||||
//
|
||||
// String[] members = multiMeta.getMembers();
|
||||
// String[] nodes = getAccordingToRouteInfo(routeInfo, req, members);
|
||||
// if (nodes.length < count) {
|
||||
// count = nodes.length;
|
||||
// }
|
||||
// collector =
|
||||
// createResultCallback(id, rc, count, req.seq, req.getContractID(), joinInfo); // 初始化结果收集器
|
||||
// MasterServerTCPAction.sync.sleep(id, collector);
|
||||
// LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq);
|
||||
// sendRequest(id, req, nodes); // 发送请求
|
||||
// } else {
|
||||
// LOGGER.info("invalidNodeNumOnResult");
|
||||
// request_index.getAndDecrement();
|
||||
// ContractResult finalResult =
|
||||
// new ContractResult(
|
||||
// ContractResult.Status.Error,
|
||||
// new JsonPrimitive("node number unavailbale,request refused."));
|
||||
// rc.onResult(JsonUtil.toJson(finalResult));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private int getJoinCount(JoinInfo joinInfo, String contractID) {
|
||||
// if (joinInfo == null) return resultCount;
|
||||
// if (joinInfo.joinCount.isJsonPrimitive() && joinInfo.joinCount.getAsJsonPrimitive().isNumber()) {
|
||||
// return joinInfo.joinCount.getAsJsonPrimitive().getAsInt();
|
||||
// }
|
||||
// try {
|
||||
// ContractRequest cr = new ContractRequest();
|
||||
// cr.setContractID(contractID);
|
||||
// cr.setAction(joinInfo.joinCount.getAsString());
|
||||
// //TODO Arg需要好好设计一下。
|
||||
// //TODO 又好用又简单的那种设计
|
||||
// //TODO
|
||||
// cr.setArg("");
|
||||
// String result = CMActions.manager.executeLocally(cr, null);
|
||||
// return JsonUtil.parseString(result).getAsJsonObject().get("result").getAsInt();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// return 1;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 清理缓存的多点合约请求序号
|
||||
// public void clearCache() {
|
||||
// final long time = System.currentTimeMillis() - 30000L;
|
||||
// seqMap.entrySet()
|
||||
// .removeIf(
|
||||
// entry -> {
|
||||
// MultiReqSeq cache = entry.getValue();
|
||||
// if (null == cache) {
|
||||
// return true;
|
||||
// }
|
||||
// return cache.startTime < time;
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// public static class ResultMerger extends ResultCallback {
|
||||
// ComponedContractResult componedContractResult;
|
||||
// AtomicInteger order;
|
||||
// String contractID;
|
||||
// int count; // 记录有多少个节点
|
||||
// int request_seq;
|
||||
// ResultCallback originalCallback;
|
||||
// Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
|
||||
// JoinInfo joinInfo;
|
||||
//
|
||||
// ResultMerger(
|
||||
// final ResultCallback originalCb,
|
||||
// final int count,
|
||||
// final int request_seq,
|
||||
// final String contractID,
|
||||
// final JoinInfo joinInfo) {
|
||||
// originalCallback = originalCb;
|
||||
// this.count = count;
|
||||
// this.request_seq = request_seq;
|
||||
// this.contractID = contractID;
|
||||
// componedContractResult = new ComponedContractResult(count);
|
||||
// order = new AtomicInteger(0);
|
||||
// this.joinInfo = joinInfo;
|
||||
// }
|
||||
//
|
||||
// public String getInfo() {
|
||||
// return "contractID="
|
||||
// + contractID
|
||||
// + " 收到第 "
|
||||
// + order
|
||||
// + " 个节点回复 : "
|
||||
// + " order="
|
||||
// + order
|
||||
// + " count="
|
||||
// + count
|
||||
// + " ";
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onResult(String str) {
|
||||
// // TODO 必须在这里聚合。
|
||||
// // str的data是个ContractResult
|
||||
// // 在这儿也是返回个ContractResult
|
||||
// try {
|
||||
// LOGGER.info(str);
|
||||
// JsonObject obj = JsonParser.parseString(str).getAsJsonObject();
|
||||
// String id = obj.get("nodeID").getAsString();
|
||||
// if (nodeIDs.contains(id)) {
|
||||
// LOGGER.info("已经收到节点 " + id.substring(0, 5) + " 的结果,该结果被忽略");
|
||||
// return;
|
||||
// }
|
||||
// nodeIDs.add(id);
|
||||
// LOGGER.info(
|
||||
// "contractID="
|
||||
// + contractID
|
||||
// + " 收到第 "
|
||||
// + order
|
||||
// + " 个节点回复 : "
|
||||
// + str
|
||||
// + " order="
|
||||
// + order
|
||||
// + " count="
|
||||
// + count);
|
||||
// componedContractResult.add(obj);
|
||||
// // 收集到所有结果
|
||||
// if (order.incrementAndGet() == count) {
|
||||
// ContractResult finalResult = componedContractResult.mergeFinalResult();
|
||||
//
|
||||
// finalResult.needSeq = true;
|
||||
// finalResult.seq = request_seq;
|
||||
//
|
||||
// // if (null == finalResult) {
|
||||
// // finalResult =
|
||||
// // new ContractResult(
|
||||
// // ContractResult.Status.Exception,
|
||||
// // new JsonPrimitive(
|
||||
// // "no nore than half of the
|
||||
// // consistent result"));
|
||||
// // originalCallback.onResult(new
|
||||
// // Gson().toJson(finalResult));
|
||||
// // } else {
|
||||
// if (joinInfo != null) {
|
||||
// handleJoinInfo(finalResult, joinInfo);
|
||||
// }
|
||||
// originalCallback.onResult(JsonUtil.toJson(finalResult));
|
||||
// // }
|
||||
// LOGGER.info(
|
||||
// "本次执行最终结果为 " + finalResult.size + "个节点合并的,结果为 " + finalResult.result);
|
||||
//
|
||||
// // 集群中事务序号+1
|
||||
// // MasterServerTCPAction.contractID2Members.get(contractID).nextSeq();
|
||||
// CMActions.manager
|
||||
// .multiContractRecorder
|
||||
// .getMultiContractMeta(contractID)
|
||||
// .nextSeqAtMaster();
|
||||
// // recover,其中无状态合约CP出错无需恢复
|
||||
// Set<String> nodesID = componedContractResult.getProblemNodes();
|
||||
// if (null == nodesID || nodesID.isEmpty()) {
|
||||
// return;
|
||||
// }
|
||||
// for (String nodeID : nodesID) {
|
||||
// LOGGER.info("结果出现问题的节点有:" + nodeID);
|
||||
// if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
// == RecoverFlag.Fine) {
|
||||
// MasterServerRecoverMechAction.recoverStatus
|
||||
// .get(nodeID)
|
||||
// .put(contractID, RecoverFlag.ToRecover);
|
||||
// }
|
||||
// }
|
||||
// for (String nodeID : nodesID) {
|
||||
// if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
|
||||
// == RecoverFlag.ToRecover) {
|
||||
// LOGGER.info("问题节点开始恢复:" + nodeID);
|
||||
//
|
||||
// // 因为该节点结果有误,所以即时是stableMode也认为trans记录不可信
|
||||
// // 直接通过load别的节点来恢复
|
||||
// MasterServerRecoverMechAction.restartContractFromCommonMode(
|
||||
// nodeID, contractID);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // clearCache();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// LOGGER.info("本次执行最终结果为有异常");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void handleJoinInfo(ContractResult finalResult, JoinInfo joinInfo) {
|
||||
// JsonObject jo = finalResult.result.getAsJsonObject();
|
||||
// if (joinInfo != null && joinInfo.joinRule != null) {
|
||||
// //TODO 不应该是double 类型
|
||||
// switch (joinInfo.joinRule) {
|
||||
// case "add":
|
||||
// double val = 0;
|
||||
// for (String key : jo.keySet()) {
|
||||
// val += jo.get(key).getAsDouble();
|
||||
// }
|
||||
// finalResult.result = new JsonPrimitive(val);
|
||||
// break;
|
||||
// case "multiply":
|
||||
// val = 1;
|
||||
// for (String key : jo.keySet()) {
|
||||
// val *= jo.get(key).getAsDouble();
|
||||
// }
|
||||
// finalResult.result = new JsonPrimitive(val);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
@ -1,68 +1,70 @@
|
||||
package org.bdware.server.executor.unconsistency;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.conn.OnHashCallback;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.ControllerManager;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
import org.bdware.server.trustedmodel.AgentManager;
|
||||
import org.bdware.units.NetworkManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class RequestOnceExecutor implements ContractExecutor {
|
||||
private static final Logger LOGGER = LogManager.getLogger(RequestOnceExecutor.class);
|
||||
String contractID;
|
||||
AtomicInteger order = new AtomicInteger(0);
|
||||
|
||||
public RequestOnceExecutor(String contractID) {
|
||||
this.contractID = contractID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) {
|
||||
ResultCallback cb =
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
LOGGER.debug(str);
|
||||
JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
|
||||
JsonObject result =
|
||||
JsonParser.parseString(jo.get("data").getAsString())
|
||||
.getAsJsonObject();
|
||||
for (String key : result.keySet()) jo.add(key, result.get(key));
|
||||
jo.remove("action");
|
||||
jo.addProperty("action", "onExecuteResult");
|
||||
LOGGER.debug(jo.toString());
|
||||
rc.onResult(jo.toString());
|
||||
}
|
||||
};
|
||||
MasterServerTCPAction.sync.sleep(requestID, cb);
|
||||
String[] members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
for (int i = 0; i < members.length; i++) {
|
||||
LOGGER.info("[members]:" + members.length);
|
||||
int size = members.length;
|
||||
String nodeID = members[order.incrementAndGet() % size];
|
||||
//ADD Connect
|
||||
Map<String, Object> obj = new HashMap<>();
|
||||
obj.put("action", "executeContractLocally");
|
||||
obj.put("requestID",requestID);
|
||||
obj.put("data", req);
|
||||
obj.put("uniReqID", requestID);
|
||||
NetworkManager.instance.sendToAgent(nodeID,JsonUtil.toJson(obj));
|
||||
return;
|
||||
}
|
||||
rc.onResult(
|
||||
"{\"status\":\"Error\",\"result\":\"all nodes "
|
||||
+ " offline\",\"action\":\"onExecuteContract\"}");
|
||||
}
|
||||
}
|
||||
// package org.bdware.server.executor.unconsistency;
|
||||
//
|
||||
// import com.google.gson.JsonObject;
|
||||
// import com.google.gson.JsonParser;
|
||||
// import org.apache.logging.log4j.LogManager;
|
||||
// import org.apache.logging.log4j.Logger;
|
||||
// import org.bdware.sc.bean.ContractRequest;
|
||||
// import org.bdware.sc.conn.OnHashCallback;
|
||||
// import org.bdware.sc.conn.ResultCallback;
|
||||
// import org.bdware.sc.util.JsonUtil;
|
||||
// import org.bdware.server.ControllerManager;
|
||||
// import org.bdware.server.action.CMActions;
|
||||
// import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
// import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
// import org.bdware.server.trustedmodel.AgentManager;
|
||||
// import org.bdware.units.NetworkManager;
|
||||
//
|
||||
// import java.util.HashMap;
|
||||
// import java.util.Map;
|
||||
// import java.util.concurrent.atomic.AtomicInteger;
|
||||
//
|
||||
// public class RequestOnceExecutor implements ContractExecutor {
|
||||
// private static final Logger LOGGER = LogManager.getLogger(RequestOnceExecutor.class);
|
||||
// String contractID;
|
||||
// AtomicInteger order = new AtomicInteger(0);
|
||||
//
|
||||
// public RequestOnceExecutor(String contractID) {
|
||||
// this.contractID = contractID;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
|
||||
// {
|
||||
// ResultCallback cb =
|
||||
// new ResultCallback() {
|
||||
// @Override
|
||||
// public void onResult(String str) {
|
||||
// LOGGER.debug(str);
|
||||
// JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
|
||||
// JsonObject result =
|
||||
// JsonParser.parseString(jo.get("data").getAsString())
|
||||
// .getAsJsonObject();
|
||||
// for (String key : result.keySet()) jo.add(key, result.get(key));
|
||||
// jo.remove("action");
|
||||
// jo.addProperty("action", "onExecuteResult");
|
||||
// LOGGER.debug(jo.toString());
|
||||
// rc.onResult(jo.toString());
|
||||
// }
|
||||
// };
|
||||
// MasterServerTCPAction.sync.sleep(requestID, cb);
|
||||
// String[] members =
|
||||
// CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
// for (int i = 0; i < members.length; i++) {
|
||||
// LOGGER.info("[members]:" + members.length);
|
||||
// int size = members.length;
|
||||
// String nodeID = members[order.incrementAndGet() % size];
|
||||
// //ADD Connect
|
||||
// Map<String, Object> obj = new HashMap<>();
|
||||
// obj.put("action", "executeContractLocally");
|
||||
// obj.put("requestID",requestID);
|
||||
// obj.put("data", req);
|
||||
// obj.put("uniReqID", requestID);
|
||||
// NetworkManager.instance.sendToAgent(nodeID,JsonUtil.toJson(obj));
|
||||
// return;
|
||||
// }
|
||||
// rc.onResult(
|
||||
// "{\"status\":\"Error\",\"result\":\"all nodes "
|
||||
// + " offline\",\"action\":\"onExecuteContract\"}");
|
||||
// }
|
||||
// }
|
||||
|
@ -1,83 +1,86 @@
|
||||
package org.bdware.server.executor.unconsistency;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractResult;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.conn.OnHashCallback;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
import org.bdware.units.NetworkManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ResponseOnceExecutor implements ContractExecutor {
|
||||
private static final Logger LOGGER = LogManager.getLogger(ResponseOnceExecutor.class);
|
||||
private final String contractID;
|
||||
AtomicInteger order = new AtomicInteger(0);
|
||||
|
||||
public ResponseOnceExecutor(String contractID) {
|
||||
this.contractID = contractID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) {
|
||||
executeInternal(requestID, rc, req, 2);
|
||||
}
|
||||
|
||||
private void executeInternal(
|
||||
String requestID, ResultCallback rc, ContractRequest req, int count) {
|
||||
// String contractID = req.getContractID();
|
||||
// TODO 标注失效节点,是否选择重新迁移??
|
||||
ResultCallback cb =
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
LOGGER.debug(str);
|
||||
JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
|
||||
jo.remove("action");
|
||||
jo.addProperty("action", "onExecuteResult");
|
||||
LOGGER.debug(jo.toString());
|
||||
if (jo.has("data")) {
|
||||
String data = jo.get("data").getAsString();
|
||||
ContractResult cr = JsonUtil.fromJson(data, ContractResult.class);
|
||||
if (cr.status != ContractResult.Status.Success && count > 0) {
|
||||
executeInternal(requestID, rc, req, count - 1);
|
||||
} else rc.onResult(jo.toString());
|
||||
} else {
|
||||
JsonObject jo2 = new JsonObject();
|
||||
jo2.addProperty("action", "onExecuteResult");
|
||||
jo.remove("action");
|
||||
jo2.addProperty("data", jo.toString());
|
||||
rc.onResult(jo2.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5);
|
||||
if (!sendOnce(requestID, req))
|
||||
rc.onResult(
|
||||
"{\"status\":\"Error\",\"data\":\"{\\\"status\\\":\\\"Error\\\",\\\"result\\\":\\\"all nodes offline\\\"}\",\"action\":\"onExecuteContract\"}");
|
||||
}
|
||||
|
||||
private boolean sendOnce(String requestID, ContractRequest req) {
|
||||
String[] members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
for (int i = 0; i < members.length; i++) {
|
||||
int size = members.length;
|
||||
String nodeID = members[order.incrementAndGet() % size];
|
||||
Map<String, Object> obj = new HashMap<>();
|
||||
obj.put("action", "executeContractLocally");
|
||||
obj.put("data", req);
|
||||
obj.put("uniReqID", requestID);
|
||||
NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(obj));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// package org.bdware.server.executor.unconsistency;
|
||||
//
|
||||
// import com.google.gson.JsonObject;
|
||||
// import com.google.gson.JsonParser;
|
||||
// import org.apache.logging.log4j.LogManager;
|
||||
// import org.apache.logging.log4j.Logger;
|
||||
// import org.bdware.sc.ContractResult;
|
||||
// import org.bdware.sc.bean.ContractRequest;
|
||||
// import org.bdware.sc.conn.OnHashCallback;
|
||||
// import org.bdware.sc.conn.ResultCallback;
|
||||
// import org.bdware.sc.util.JsonUtil;
|
||||
// import org.bdware.server.action.CMActions;
|
||||
// import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
// import org.bdware.server.trustedmodel.ContractExecutor;
|
||||
// import org.bdware.units.NetworkManager;
|
||||
//
|
||||
// import java.util.HashMap;
|
||||
// import java.util.Map;
|
||||
// import java.util.concurrent.atomic.AtomicInteger;
|
||||
//
|
||||
// public class ResponseOnceExecutor implements ContractExecutor {
|
||||
// private static final Logger LOGGER = LogManager.getLogger(ResponseOnceExecutor.class);
|
||||
// private final String contractID;
|
||||
// AtomicInteger order = new AtomicInteger(0);
|
||||
//
|
||||
// public ResponseOnceExecutor(String contractID) {
|
||||
// this.contractID = contractID;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
|
||||
// {
|
||||
// executeInternal(requestID, rc, req, 2);
|
||||
// }
|
||||
//
|
||||
// private void executeInternal(
|
||||
// String requestID, ResultCallback rc, ContractRequest req, int count) {
|
||||
// // String contractID = req.getContractID();
|
||||
// // TODO 标注失效节点,是否选择重新迁移??
|
||||
// ResultCallback cb =
|
||||
// new ResultCallback() {
|
||||
// @Override
|
||||
// public void onResult(String str) {
|
||||
// LOGGER.debug(str);
|
||||
// JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
|
||||
// jo.remove("action");
|
||||
// jo.addProperty("action", "onExecuteResult");
|
||||
// LOGGER.debug(jo.toString());
|
||||
// if (jo.has("data")) {
|
||||
// String data = jo.get("data").getAsString();
|
||||
// ContractResult cr = JsonUtil.fromJson(data, ContractResult.class);
|
||||
// if (cr.status != ContractResult.Status.Success && count > 0) {
|
||||
// executeInternal(requestID, rc, req, count - 1);
|
||||
// } else rc.onResult(jo.toString());
|
||||
// } else {
|
||||
// JsonObject jo2 = new JsonObject();
|
||||
// jo2.addProperty("action", "onExecuteResult");
|
||||
// jo.remove("action");
|
||||
// jo2.addProperty("data", jo.toString());
|
||||
// rc.onResult(jo2.toString());
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5);
|
||||
// if (!sendOnce(requestID, req))
|
||||
// rc.onResult(
|
||||
// "{\"status\":\"Error\",\"data\":\"{\\\"status\\\":\\\"Error\\\",\\\"result\\\":\\\"all nodes
|
||||
// offline\\\"}\",\"action\":\"onExecuteContract\"}");
|
||||
// }
|
||||
//
|
||||
// private boolean sendOnce(String requestID, ContractRequest req) {
|
||||
// String[] members =
|
||||
// CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
// for (int i = 0; i < members.length; i++) {
|
||||
// int size = members.length;
|
||||
// String nodeID = members[order.incrementAndGet() % size];
|
||||
// Map<String, Object> obj = new HashMap<>();
|
||||
// obj.put("action", "executeContractLocally");
|
||||
// obj.put("data", req);
|
||||
// obj.put("uniReqID", requestID);
|
||||
// NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(obj));
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
@ -33,30 +33,30 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hashCallback) {
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rc,
|
||||
OnHashCallback hashCallback) {
|
||||
executeInternal(requestID, rc, req, 2);
|
||||
}
|
||||
|
||||
private void executeInternal(
|
||||
String requestID, ResultCallback rc, ContractRequest req, int count) {
|
||||
// String contractID = req.getContractID();
|
||||
private void executeInternal(String requestID, ResultCallback rc, ContractRequest req,
|
||||
int count) {
|
||||
// String contractID = req.getContractID();
|
||||
// TODO 标注失效节点,是否选择重新迁移??
|
||||
ResultCallback cb =
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
LOGGER.debug(str);
|
||||
JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
|
||||
JsonObject result =
|
||||
JsonParser.parseString(jo.get("data").getAsString())
|
||||
.getAsJsonObject();
|
||||
for (String key : result.keySet()) jo.add(key, result.get(key));
|
||||
jo.remove("action");
|
||||
jo.addProperty("action", "onExecuteResult");
|
||||
LOGGER.debug(jo.toString());
|
||||
rc.onResult(jo.toString());
|
||||
}
|
||||
};
|
||||
ResultCallback cb = new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
LOGGER.debug(str);
|
||||
JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
|
||||
JsonObject result =
|
||||
JsonParser.parseString(jo.get("data").getAsString()).getAsJsonObject();
|
||||
for (String key : result.keySet())
|
||||
jo.add(key, result.get(key));
|
||||
jo.remove("action");
|
||||
jo.addProperty("action", "onExecuteResult");
|
||||
LOGGER.debug(jo.toString());
|
||||
rc.onResult(jo.toString());
|
||||
}
|
||||
};
|
||||
MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5);
|
||||
if (!send(requestID, req))
|
||||
rc.onResult(
|
||||
@ -69,7 +69,8 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
|
||||
req.getArg();
|
||||
req.getRequester();
|
||||
req.getAction();
|
||||
MultiContractMeta mMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(meta.getID());
|
||||
MultiContractMeta mMeta =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(meta.getID());
|
||||
|
||||
FunctionDesp desp = meta.getExportedFunction(req.getAction());
|
||||
RouteInfo info = desp.getRoute();
|
||||
@ -77,10 +78,8 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
|
||||
if (info != null && info.useDefault != null) {
|
||||
switch (info.useDefault) {
|
||||
case byRequester:
|
||||
hash =
|
||||
new BigInteger(req.getRequester(), 16)
|
||||
.mod(BigInteger.valueOf(mMeta.getMembers().length))
|
||||
.intValue();
|
||||
hash = new BigInteger(req.getRequester(), 16)
|
||||
.mod(BigInteger.valueOf(mMeta.getMembers().length)).intValue();
|
||||
break;
|
||||
case byArgHash:
|
||||
hash = req.getArg().hashCode();
|
||||
@ -88,13 +87,15 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
String[] members =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
|
||||
String[] members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
|
||||
.getMembers();
|
||||
String nodeID;
|
||||
for (int i = 0; i < members.length; i++) {
|
||||
int size = members.length;
|
||||
if (hash != -1) nodeID = members[hash % size];
|
||||
else nodeID = members[order.incrementAndGet() % size];
|
||||
if (hash != -1)
|
||||
nodeID = members[hash % size];
|
||||
else
|
||||
nodeID = members[order.incrementAndGet() % size];
|
||||
Map<String, Object> obj = new HashMap<>();
|
||||
obj.put("action", "executeContractLocally");
|
||||
obj.put("data", req);
|
||||
|
@ -30,7 +30,6 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
|
||||
@ -45,11 +44,8 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
URIHandler uriHandlers;
|
||||
|
||||
public CMHttpHandler() {
|
||||
FileFilter fileFilter =
|
||||
pathname ->
|
||||
!pathname.getName().equals("CenterPortal.html")
|
||||
&& !pathname.getAbsolutePath()
|
||||
.contains(".."); // TODO 路径判断,过滤下载任意文件
|
||||
FileFilter fileFilter = pathname -> !pathname.getName().equals("CenterPortal.html")
|
||||
&& !pathname.getAbsolutePath().contains(".."); // TODO 路径判断,过滤下载任意文件
|
||||
HttpFileHandleAdapter ideFileService = new HttpFileHandleAdapter(
|
||||
new File("./WebContent/ide/").getAbsolutePath(), fileFilter) {
|
||||
@URIPath({"/"})
|
||||
@ -58,68 +54,59 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
channelRead0(ctx, request);
|
||||
}
|
||||
};
|
||||
HttpFileHandleAdapter otherFileService = new HttpFileHandleAdapter(
|
||||
new File("./WebContent/").getAbsolutePath(), fileFilter) {
|
||||
@URIPath({"/client/", "/doc/"})
|
||||
private void handleFile(ChannelHandlerContext ctx, FullHttpRequest request)
|
||||
throws Exception {
|
||||
channelRead0(ctx, request);
|
||||
}
|
||||
};
|
||||
actionExecutor =
|
||||
new ActionExecutor<ResultCallback, JsonObject>(
|
||||
ContractManagerFrameHandler.executorService,
|
||||
new CMActions(),
|
||||
ManagerActions.instance,
|
||||
GRPCPool.instance,
|
||||
new HttpFileAction(),
|
||||
new BDIndexerAction(),
|
||||
LedgerActions.instance,
|
||||
new CMLogAction(),
|
||||
new TemporyTestAction()) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, JsonObject arg, long per) {
|
||||
boolean flag = a.httpAccess();
|
||||
long val = a.userPermission(); // 使用者必须达到的permission
|
||||
if (val == 0) {
|
||||
return true;
|
||||
}
|
||||
long permission = arg.get("permission").getAsLong();
|
||||
boolean flag2 = false; // 启动http权限后应改为false
|
||||
String status = "refuse";
|
||||
String action = arg.get("action").getAsString();
|
||||
|
||||
if (val == 0) {
|
||||
flag2 = true;
|
||||
} else if ((permission & val) == val) {
|
||||
System.out.print((permission & val));
|
||||
flag2 = true;
|
||||
}
|
||||
|
||||
if (flag && flag2) {
|
||||
status = "accept";
|
||||
}
|
||||
|
||||
String pubkey = "anonymity";
|
||||
if (arg.has("pubKey")) {
|
||||
pubkey = arg.get("pubKey").getAsString();
|
||||
}
|
||||
TimeDBUtil.instance.put(
|
||||
CMTables.LocalNodeLogDB.toString(),
|
||||
String.format(
|
||||
"{\"action\":\"%s\",\"pubKey\":\"%s\",\"status\":\"%s\",\"date\":%d}",
|
||||
action,
|
||||
pubkey,
|
||||
status,
|
||||
System.currentTimeMillis()));
|
||||
return flag && flag2;
|
||||
HttpFileHandleAdapter otherFileService =
|
||||
new HttpFileHandleAdapter(new File("./WebContent/").getAbsolutePath(), fileFilter) {
|
||||
@URIPath({"/client/", "/doc/", "/blog/"})
|
||||
private void handleFile(ChannelHandlerContext ctx, FullHttpRequest request)
|
||||
throws Exception {
|
||||
channelRead0(ctx, request);
|
||||
}
|
||||
};
|
||||
UserManagerAction userManagerAction = new UserManagerAction(null);
|
||||
MasterWSAction masterWSAction = new MasterWSAction(null);
|
||||
actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(
|
||||
ContractManagerFrameHandler.executorService, new CMActions(),
|
||||
ManagerActions.instance, new FileActions(null), userManagerAction, masterWSAction,
|
||||
GRPCPool.instance, new HttpFileAction(), new BDIndexerAction(),
|
||||
LedgerActions.instance, new CMLogAction(), new TemporyTestAction()) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, JsonObject arg, long per) {
|
||||
boolean flag = a.httpAccess();
|
||||
long val = a.userPermission(); // 使用者必须达到的permission
|
||||
if (val == 0) {
|
||||
return true;
|
||||
}
|
||||
long permission = arg.get("permission").getAsLong();
|
||||
boolean flag2 = false; // 启动http权限后应改为false
|
||||
String status = "refuse";
|
||||
String action = arg.get("action").getAsString();
|
||||
|
||||
for (String str : wsPluginActions) {
|
||||
Object obj = createInstanceByClzName(str);
|
||||
actionExecutor.appendHandler(obj);
|
||||
}
|
||||
if (val == 0) {
|
||||
flag2 = true;
|
||||
} else if ((permission & val) == val) {
|
||||
System.out.print((permission & val));
|
||||
flag2 = true;
|
||||
}
|
||||
|
||||
if (flag && flag2) {
|
||||
status = "accept";
|
||||
}
|
||||
|
||||
String pubkey = "anonymity";
|
||||
if (arg.has("pubKey")) {
|
||||
pubkey = arg.get("pubKey").getAsString();
|
||||
}
|
||||
TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(), String.format(
|
||||
"{\"action\":\"%s\",\"pubKey\":\"%s\",\"status\":\"%s\",\"date\":%d}",
|
||||
action, pubkey, status, System.currentTimeMillis()));
|
||||
return flag && flag2;
|
||||
}
|
||||
};
|
||||
if (wsPluginActions != null)
|
||||
for (String str : wsPluginActions) {
|
||||
Object obj = createInstanceByClzName(str);
|
||||
actionExecutor.appendHandler(obj);
|
||||
}
|
||||
|
||||
uriHandlers = new URIHandler();
|
||||
DOIPOverHttpHandler doipOverHttpHandler = new DOIPOverHttpHandler();
|
||||
@ -163,89 +150,57 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
|
||||
@URIPath(method = HttpMethod.OPTIONS)
|
||||
public void crossOrigin(ChannelHandlerContext ctx, FullHttpRequest request) {
|
||||
DefaultFullHttpResponse fullResponse =
|
||||
new DefaultFullHttpResponse(
|
||||
request.protocolVersion(),
|
||||
OK,
|
||||
Unpooled.wrappedBuffer("success".getBytes()));
|
||||
DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
|
||||
request.protocolVersion(), OK, Unpooled.wrappedBuffer("success".getBytes()));
|
||||
fullResponse.headers().remove("Access-Control-Allow-Origin");
|
||||
fullResponse.headers().remove("Access-Control-Allow-Headers");
|
||||
fullResponse.headers().add("Access-Control-Allow-Origin", "*");
|
||||
fullResponse
|
||||
.headers()
|
||||
.add("Access-Control-Allow-Headers",
|
||||
"Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " +
|
||||
"X-Requested-With, Accept, Accept-Language, Cache-Control, Connection");
|
||||
fullResponse.headers().add("Access-Control-Allow-Methods", "*");
|
||||
fullResponse.headers().add("Access-Control-Allow-Headers",
|
||||
"Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, "
|
||||
+ "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection");
|
||||
ChannelFuture f = ctx.write(fullResponse);
|
||||
f.addListener(ChannelFutureListener.CLOSE);
|
||||
LOGGER.info("[OOOOOOOOption] received!");
|
||||
}
|
||||
|
||||
@URIPath({"/SCIDE/CMManager", "/SCIDE/SCManager"})
|
||||
@URIPath({"/SCIDE/CMManager", "/SCIDE/SCManager", "/BDO"})
|
||||
public void handleHttpGet(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
|
||||
QueryStringDecoder decoderQuery = new QueryStringDecoder(msg.uri());
|
||||
Map<String, List<String>> parameters = decoderQuery.parameters();
|
||||
JsonObject transformedParam = new JsonObject();
|
||||
for (String key : parameters.keySet()) {
|
||||
List<String> val = parameters.get(key);
|
||||
if (null != val) {
|
||||
transformedParam.addProperty(key, val.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
// 匿名用户权限为0
|
||||
transformedParam.addProperty("permission", 0);
|
||||
transformedParam.remove("verifiedPubKey");
|
||||
|
||||
// 验签 有pubKey就必须有sign
|
||||
String uri = URLDecoder.decode(msg.uri(), "UTF-8").split("\\?")[1];
|
||||
if (transformedParam.has("pubKey")) {
|
||||
int index = uri.lastIndexOf('&');
|
||||
String str = uri.substring(0, index);
|
||||
LOGGER.info("before verifying: " + str);
|
||||
boolean verify = false;
|
||||
try {
|
||||
verify =
|
||||
SM2Util.plainStrVerify(
|
||||
transformedParam.get("pubKey").getAsString(),
|
||||
str,
|
||||
transformedParam.get("sign").getAsString());
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
}
|
||||
LOGGER.info("verify signature: " + verify);
|
||||
if (verify) {
|
||||
// 查permission
|
||||
String pubkey = transformedParam.get("pubKey").getAsString();
|
||||
String ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubkey);
|
||||
long permission;
|
||||
if (ret != null && ret.length() > 0) {
|
||||
permission = 0x86000d41L | Role.compoundValue(ret.split(","));
|
||||
} else {
|
||||
assert ret != null;
|
||||
permission = Role.compoundValue(ret.split(","));
|
||||
}
|
||||
transformedParam.addProperty("permission", permission);
|
||||
LOGGER.info("user permission: " + permission);
|
||||
transformedParam.addProperty(
|
||||
"verifiedPubKey", transformedParam.get("pubKey").getAsString());
|
||||
}
|
||||
}
|
||||
JsonObject transformedParam =
|
||||
ArgParser.parseGetAndVerify(msg, new ArgParser.VerifiedCallback() {
|
||||
@Override
|
||||
public void onResult(boolean verified, JsonObject transformedParam) {
|
||||
LOGGER.info("verify signature: " + verified + " "
|
||||
+ transformedParam.toString());
|
||||
if (verified) {
|
||||
// 查permission
|
||||
String pubkey = transformedParam.get("pubKey").getAsString();
|
||||
String ret = KeyValueDBUtil.instance
|
||||
.getValue(CMTables.NodeRole.toString(), pubkey);
|
||||
long permission;
|
||||
if (ret != null && ret.length() > 0) {
|
||||
permission = 0x86000d41L | Role.compoundValue(ret.split(","));
|
||||
} else {
|
||||
assert ret != null;
|
||||
permission = Role.compoundValue(ret.split(","));
|
||||
}
|
||||
transformedParam.addProperty("permission", permission);
|
||||
LOGGER.info("user permission: " + permission);
|
||||
transformedParam.addProperty("verifiedPubKey",
|
||||
transformedParam.get("pubKey").getAsString());
|
||||
}
|
||||
}
|
||||
});
|
||||
handleReq(transformedParam, ctx, msg);
|
||||
}
|
||||
|
||||
@URIPath(
|
||||
method = HttpMethod.POST,
|
||||
value = {"/SCIDE/CMManager", "/SCIDE/SCManager"})
|
||||
@URIPath(method = HttpMethod.POST, value = {"/SCIDE/CMManager", "/SCIDE/SCManager", "/BDO"})
|
||||
public void handleHttpPost(ChannelHandlerContext ctx, FullHttpRequest msg)
|
||||
throws UnsupportedEncodingException {
|
||||
// http请求中规定签名必须是最后一个且公钥名必须为pubKey,否则验签失败
|
||||
// 倒数第二个参数是pubKey,倒数第一个参数是sign
|
||||
// 有pubKey就必须签名
|
||||
String uri = URLDecoder.decode(msg.uri(), "UTF-8");
|
||||
LOGGER.info("handle " + uri);
|
||||
|
||||
ByteBuf content = msg.content();
|
||||
byte[] reqContent = new byte[content.readableBytes()];
|
||||
content.readBytes(reqContent);
|
||||
@ -267,11 +222,8 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
String str = "action:" + action;
|
||||
boolean verify = false;
|
||||
try {
|
||||
verify =
|
||||
SM2Util.plainStrVerify(
|
||||
map.get("pubKey").getAsString(),
|
||||
str,
|
||||
map.get("sign").getAsString());
|
||||
verify = SM2Util.plainStrVerify(map.get("pubKey").getAsString(), str,
|
||||
map.get("sign").getAsString());
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(e));
|
||||
@ -304,10 +256,13 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
if (!map.has("action")) {
|
||||
LOGGER.error("no action in map!");
|
||||
ret = UNSUPPORTED_ACTION.getBytes();
|
||||
DefaultFullHttpResponse response =
|
||||
new DefaultFullHttpResponse(
|
||||
HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(ret));
|
||||
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
|
||||
OK, Unpooled.wrappedBuffer(ret));
|
||||
response.headers().add("Access-Control-Allow-Origin", "*");
|
||||
response.headers().add("Access-Control-Allow-Methods", "*");
|
||||
ChannelFuture f = ctx.write(response);
|
||||
response.headers().add("Access-Control-Allow-Origin", "*");
|
||||
response.headers().add("Access-Control-Allow-Methods", "*");
|
||||
f.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
} else {
|
||||
@ -317,34 +272,46 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
HttpResultCallback cb;
|
||||
if (action.equals("downloadContract")) {
|
||||
cb = new FileDownloaderCallback(ctx, req);
|
||||
} else if (action.equals("distributeContract")) {
|
||||
cb = new HttpServerSentEventResultCallback(ctx, null);
|
||||
cb.addHeader("Access-Control-Allow-Origin", "*");
|
||||
cb.addHeader("Access-Control-Allow-Methods", "*");
|
||||
cb.addHeader("Content-Type", "text/event-stream");
|
||||
cb.addHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
|
||||
cb.addHeader("Transfer-Encoding", "chunked");
|
||||
((HttpServerSentEventResultCallback) cb).writeInitialHead();
|
||||
} else if (map.has("callback")) {
|
||||
cb = new HttpResultCallback(ctx, map.get("callback").getAsString());
|
||||
cb.addHeader("Access-Control-Allow-Origin", "*");
|
||||
cb.addHeader("Access-Control-Allow-Methods", "*");
|
||||
cb.addHeader("Content-Type", "application/json");
|
||||
} else {
|
||||
cb = new HttpResultCallback(ctx, null);
|
||||
cb.addHeader("Content-Type", "application/json");
|
||||
cb.addHeader("Access-Control-Allow-Origin", "*");
|
||||
cb.addHeader("Access-Control-Allow-Methods", "*");
|
||||
}
|
||||
if (map.get("action").getAsString().equals("downloadUUID")) {
|
||||
cb.addHeader("Access-Control-Allow-Origin", "*");
|
||||
cb.addHeader("Access-Control-Allow-Methods", "*");
|
||||
cb.addHeader("content-disposition", "attachment;filename=encodeduuid.key");
|
||||
}
|
||||
actionExecutor.handle(action, map, cb);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
DefaultFullHttpResponse response =
|
||||
new DefaultFullHttpResponse(
|
||||
HttpVersion.HTTP_1_1,
|
||||
OK,
|
||||
Unpooled.wrappedBuffer(e.getMessage().getBytes()));
|
||||
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK,
|
||||
Unpooled.wrappedBuffer(e.getMessage().getBytes()));
|
||||
response.headers().add("Access-Control-Allow-Origin", "*");
|
||||
response.headers().add("Access-Control-Allow-Methods", "*");
|
||||
ChannelFuture f = ctx.write(response);
|
||||
f.addListener(ChannelFutureListener.CLOSE);
|
||||
} catch (Exception e) {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("msg", ExceptionUtil.exceptionToString(e));
|
||||
DefaultFullHttpResponse response =
|
||||
new DefaultFullHttpResponse(
|
||||
HttpVersion.HTTP_1_1,
|
||||
OK,
|
||||
Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
|
||||
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK,
|
||||
Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
|
||||
response.headers().add("Access-Control-Allow-Origin", "*");
|
||||
response.headers().add("Access-Control-Allow-Methods", "*");
|
||||
ChannelFuture f = ctx.write(response);
|
||||
f.addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
@ -352,9 +319,11 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
LOGGER.warn("catch exception in " + parseCtx2String(ctx) + ": " + cause.getClass().getSimpleName());
|
||||
LOGGER.warn("catch exception in " + parseCtx2String(ctx) + ": "
|
||||
+ cause.getClass().getSimpleName());
|
||||
LOGGER.debug(ExceptionUtil.exceptionToString(cause));
|
||||
// ctx.close();
|
||||
cause.printStackTrace();
|
||||
// ctx.close();
|
||||
}
|
||||
|
||||
private String parseCtx2String(ChannelHandlerContext ctx) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.bdware.server.http;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
@ -13,6 +14,7 @@ import io.netty.util.concurrent.GenericFutureListener;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractMeta;
|
||||
import org.bdware.sc.YPKResourceManager;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.action.CMActions;
|
||||
@ -20,11 +22,7 @@ import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.*;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
||||
@ -32,6 +30,39 @@ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
||||
public class DOIPOverHttpHandler {
|
||||
private static final Logger LOGGER = LogManager.getLogger(DOIPOverHttpHandler.class);
|
||||
Map<String, ZipFile> zipFilePool = new HashMap<>();
|
||||
Map<String, Long> lastVisit = new HashMap<>();
|
||||
Map<String, Long> lastModified = new HashMap<>();
|
||||
|
||||
public synchronized void pruneZipPool() {
|
||||
Set<String> toPrune = new HashSet<>();
|
||||
long current = System.currentTimeMillis();
|
||||
for (String str : zipFilePool.keySet()) {
|
||||
Long last = lastVisit.get(str);
|
||||
if (last != null && current - last > 30L * 24L * 3600L * 1000L) {
|
||||
toPrune.add(str);
|
||||
}
|
||||
}
|
||||
for (String str : toPrune) {
|
||||
try {
|
||||
ZipFile zf = zipFilePool.get(str);
|
||||
zipFilePool.remove(str);
|
||||
lastVisit.remove(str);
|
||||
lastModified.remove(str);
|
||||
zf.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@URIPath({"/HTTPStat/"})
|
||||
public void getHTTPStat(ChannelHandlerContext ctx, FullHttpRequest request) {
|
||||
FullHttpResponse response =
|
||||
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(
|
||||
new Gson().toJson(DOIPOverHttpStatics.httpRecords).getBytes()));
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
||||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
@URIPath({"/contracts/", "/DOIP/"})
|
||||
public void mockDOIP(ChannelHandlerContext ctx, FullHttpRequest request) {
|
||||
@ -41,32 +72,27 @@ public class DOIPOverHttpHandler {
|
||||
String ops = arg.get("operation").getAsString();
|
||||
Operations defaultOp = Operations.parse(ops);
|
||||
if (defaultOp == null) {
|
||||
CMActions.executeContractInternal(
|
||||
arg,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
FullHttpResponse response =
|
||||
new DefaultFullHttpResponse(
|
||||
HTTP_1_1,
|
||||
HttpResponseStatus.OK,
|
||||
Unpooled.wrappedBuffer(str.getBytes()));
|
||||
response.headers()
|
||||
.set(
|
||||
HttpHeaderNames.CONTENT_TYPE,
|
||||
"text/plain; charset=UTF-8");
|
||||
ctx.writeAndFlush(response)
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
},
|
||||
null);
|
||||
CMActions.executeContractInternal(arg, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
|
||||
HttpResponseStatus.OK, Unpooled.wrappedBuffer(str.getBytes()));
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE,
|
||||
"text/plain; charset=UTF-8");
|
||||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
}, null);
|
||||
} else {
|
||||
switch (defaultOp) {
|
||||
case ListOps:
|
||||
sendListContractProcess(arg, ctx);
|
||||
return;
|
||||
case Retrieve:
|
||||
sendAssets(arg, ctx);
|
||||
if (arg.has("__public") && arg.get("__public").getAsBoolean()) {
|
||||
sendPublicFiles(arg, ctx);
|
||||
} else {
|
||||
sendAssets(arg, ctx);
|
||||
}
|
||||
return;
|
||||
case RetrieveYPK:
|
||||
sendYpk(arg, ctx, request);
|
||||
@ -80,11 +106,8 @@ public class DOIPOverHttpHandler {
|
||||
e.printStackTrace();
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
e.printStackTrace(new PrintStream(bo));
|
||||
FullHttpResponse response =
|
||||
new DefaultFullHttpResponse(
|
||||
HTTP_1_1,
|
||||
HttpResponseStatus.OK,
|
||||
Unpooled.wrappedBuffer(bo.toByteArray()));
|
||||
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK,
|
||||
Unpooled.wrappedBuffer(bo.toByteArray()));
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
||||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
@ -93,22 +116,18 @@ public class DOIPOverHttpHandler {
|
||||
private void sendListContractProcess(JsonObject arg, ChannelHandlerContext ctx) {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("action", "onListOps");
|
||||
ret.addProperty(
|
||||
"data", CMActions.manager.listTheContracts(arg.get("contractID").getAsString()));
|
||||
FullHttpResponse response =
|
||||
new DefaultFullHttpResponse(
|
||||
HTTP_1_1,
|
||||
HttpResponseStatus.OK,
|
||||
Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
|
||||
ret.addProperty("data",
|
||||
CMActions.manager.listTheContracts(arg.get("contractID").getAsString()));
|
||||
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK,
|
||||
Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
|
||||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
private void sendYpk(JsonObject arg, ChannelHandlerContext ctx, FullHttpRequest request)
|
||||
throws IOException {
|
||||
ContractMeta meta =
|
||||
CMActions.manager.statusRecorder.getContractMeta(
|
||||
arg.get("contractID").getAsString());
|
||||
ContractMeta meta = CMActions.manager.statusRecorder
|
||||
.getContractMeta(arg.get("contractID").getAsString());
|
||||
if (meta == null) {
|
||||
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
|
||||
return;
|
||||
@ -142,44 +161,115 @@ public class DOIPOverHttpHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO @杨环宇 把zipFilePool用起来,做一个延迟关闭的东西,避免频繁打开文件的操作。
|
||||
private void sendAssets(JsonObject arg, ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
ContractMeta meta =
|
||||
CMActions.manager.statusRecorder.getContractMeta(
|
||||
arg.get("contractID").getAsString());
|
||||
private void sendPublicFiles(JsonObject arg, ChannelHandlerContext ctx) throws Exception {
|
||||
ContractMeta meta = CMActions.manager.statusRecorder
|
||||
.getContractMeta(arg.get("contractID").getAsString());
|
||||
if (meta == null) {
|
||||
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
String path = arg.get("argument").getAsString();
|
||||
ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr()));
|
||||
ZipEntry zipEntry = zipFile.getEntry(path);
|
||||
|
||||
if (zipEntry == null) {
|
||||
File dir = new File("./ContractDB", meta.getName());
|
||||
File toSend = new File(dir, path);
|
||||
FileInputStream fin;
|
||||
try {
|
||||
if (!toSend.exists() || toSend.isDirectory()) {
|
||||
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
|
||||
return;
|
||||
} else
|
||||
fin = new FileInputStream(toSend);
|
||||
} catch (Exception e) {
|
||||
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
InputStream in = zipFile.getInputStream(zipEntry);
|
||||
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
|
||||
HttpFileHandleAdapter.appendContentType(path, response.headers());
|
||||
ctx.write(response);
|
||||
ChannelFuture future = ctx.writeAndFlush(new ChunkedStream(in));
|
||||
future.addListener(
|
||||
new GenericFutureListener<Future<? super Void>>() {
|
||||
@Override
|
||||
public void operationComplete(Future<? super Void> arg0) throws Exception {
|
||||
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||
zipFile.close();
|
||||
}
|
||||
});
|
||||
ChannelFuture future = ctx.writeAndFlush(new ChunkedStream(fin));
|
||||
future.addListener(new GenericFutureListener<Future<? super Void>>() {
|
||||
@Override
|
||||
public void operationComplete(Future<? super Void> arg0) throws Exception {
|
||||
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||
fin.close();
|
||||
}
|
||||
});
|
||||
// 写入文件尾部
|
||||
future.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
|
||||
private void injectExtraArgs(JsonObject transformedParam, String fulluri) {
|
||||
|
||||
private void sendAssets(JsonObject arg, ChannelHandlerContext ctx) throws Exception {
|
||||
ContractMeta meta = CMActions.manager.statusRecorder
|
||||
.getContractMeta(arg.get("contractID").getAsString());
|
||||
if (meta == null) {
|
||||
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
String path = arg.get("argument").getAsString();
|
||||
String dir = YPKResourceManager.getUnzipDir(meta.contract);
|
||||
File toSend = new File(dir, path);
|
||||
if (!toSend.exists()) {
|
||||
if (YPKResourceManager.existYPK(meta.contract)) {
|
||||
YPKResourceManager.unzipYPK(meta.contract);
|
||||
} else {
|
||||
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
}
|
||||
InputStream in = YPKResourceManager.getCachedStream(meta.contract, path);
|
||||
DOIPOverHttpStatics.Records httpRecords = new DOIPOverHttpStatics.Records();
|
||||
httpRecords.start = System.currentTimeMillis();
|
||||
httpRecords.url = path;
|
||||
DOIPOverHttpStatics.httpRecords.add(httpRecords);
|
||||
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
|
||||
HttpFileHandleAdapter.appendContentType(path, response.headers());
|
||||
// HttpHeaders headers = response.headers();
|
||||
// headers.set(HttpHeaderNames.CACHE_CONTROL, "public, max-age=2592000");
|
||||
ctx.write(response);
|
||||
ChannelFuture future = ctx.writeAndFlush(new ChunkedStream(in));
|
||||
future.addListener(new GenericFutureListener<Future<? super Void>>() {
|
||||
@Override
|
||||
public void operationComplete(Future<? super Void> arg0) throws Exception {
|
||||
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
|
||||
in.close();
|
||||
httpRecords.end = System.currentTimeMillis();
|
||||
httpRecords.dur = httpRecords.end - httpRecords.start;
|
||||
ctx.close();
|
||||
}
|
||||
});
|
||||
// 写入文件尾部
|
||||
future.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
|
||||
private synchronized ZipFile getZipFileFromPool(String scriptStr) throws Exception {
|
||||
try {
|
||||
ZipFile cachedFile = zipFilePool.get(scriptStr);
|
||||
if (cachedFile == null) {
|
||||
File f = new File(scriptStr);
|
||||
cachedFile = new ZipFile(f);
|
||||
zipFilePool.put(scriptStr, cachedFile);
|
||||
lastModified.put(scriptStr, f.lastModified());
|
||||
} else {
|
||||
File f = new File(scriptStr);
|
||||
if (lastModified.get(scriptStr) != f.lastModified()) {
|
||||
cachedFile.close();
|
||||
cachedFile = new ZipFile(f);
|
||||
zipFilePool.put(scriptStr, cachedFile);
|
||||
lastModified.put(scriptStr, f.lastModified());
|
||||
}
|
||||
}
|
||||
|
||||
lastVisit.put(scriptStr, System.currentTimeMillis());
|
||||
return cachedFile;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new ZipFile(new File(scriptStr));
|
||||
}
|
||||
|
||||
private void injectExtraArgs(JsonObject transformedParam, String fulluri) {
|
||||
String[] data = fulluri.split("/");
|
||||
String contractIDOrName = data[2];
|
||||
String uris[] = URLDecoder.decode(fulluri).split("\\?");
|
||||
@ -187,34 +277,40 @@ public class DOIPOverHttpHandler {
|
||||
if (uri != null && transformedParam.has("pubKey")) {
|
||||
int index = fulluri.lastIndexOf('&');
|
||||
String str = fulluri;
|
||||
if (index > 0) str = fulluri.substring(0, index);
|
||||
if (index > 0)
|
||||
str = fulluri.substring(0, index);
|
||||
LOGGER.info("[mockDOIP] before verify:" + str);
|
||||
boolean verify = false;
|
||||
try {
|
||||
verify =
|
||||
SM2Util.plainStrVerify(
|
||||
transformedParam.get("pubKey").getAsString(),
|
||||
str,
|
||||
transformedParam.get("sign").getAsString());
|
||||
verify = SM2Util.plainStrVerify(transformedParam.get("pubKey").getAsString(), str,
|
||||
transformedParam.get("sign").getAsString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (verify) {
|
||||
transformedParam.addProperty(
|
||||
"verifiedPubKey", transformedParam.get("pubKey").getAsString());
|
||||
} else transformedParam.remove("verifiedPubKey");
|
||||
transformedParam.addProperty("verifiedPubKey",
|
||||
transformedParam.get("pubKey").getAsString());
|
||||
} else
|
||||
transformedParam.remove("verifiedPubKey");
|
||||
}
|
||||
transformedParam.addProperty("contractID", data[2].replaceAll("\\?.*$", ""));
|
||||
if (data.length > 3) {
|
||||
String assetPath = data[3];
|
||||
if (assetPath.equals("assets")) {
|
||||
transformedParam.remove("__public");
|
||||
if (assetPath.equals("assets") && !fulluri.contains("..")) {
|
||||
transformedParam.addProperty("operation", Operations.Retrieve.val);
|
||||
int index = data[0].length() + data[1].length() + data[2].length() + 2;
|
||||
String path = fulluri.substring(index);
|
||||
path = path.replaceAll("\\?.*$", "");
|
||||
if (path.equals("/assets/"))
|
||||
path+="index.html";
|
||||
path += "index.html";
|
||||
transformedParam.addProperty("argument", path);
|
||||
} else if (assetPath.equals("public") && !fulluri.contains("..")) {
|
||||
transformedParam.addProperty("operation", Operations.Retrieve.val);
|
||||
int index = data[0].length() + data[1].length() + data[2].length() + 2;
|
||||
String path = fulluri.substring(index);
|
||||
transformedParam.addProperty("argument", path);
|
||||
transformedParam.addProperty("__public", true);
|
||||
}
|
||||
if (assetPath.startsWith("ypk?")) {
|
||||
transformedParam.addProperty("operation", Operations.RetrieveYPK.val);
|
||||
@ -230,22 +326,19 @@ public class DOIPOverHttpHandler {
|
||||
JsonObject transformedParam = new JsonObject();
|
||||
for (String key : parameters.keySet()) {
|
||||
List<String> val = parameters.get(key);
|
||||
if (val != null) transformedParam.addProperty(key, val.get(0));
|
||||
if (val != null)
|
||||
transformedParam.addProperty(key, val.get(0));
|
||||
}
|
||||
return transformedParam;
|
||||
}
|
||||
|
||||
enum Operations {
|
||||
Hello("0.DOIP/Op.Hello"),
|
||||
ListOps("0.DOIP/Op.ListOperations"),
|
||||
Retrieve("0.DOIP/Op.Retrieve"),
|
||||
RetrieveYPK("86.BDWare/Op.RetrieveYPK"),
|
||||
Create("0.DOIP/Op.Create"),
|
||||
Update("0.DOIP/Op.Update"),
|
||||
Delete("0.DOIP/Op.Delete"),
|
||||
Search("0.DOIP/Op.Search"),
|
||||
Extension("0.DOIP/Op.Extension"),
|
||||
Unknown("0.DOIP/Op.Unknown");
|
||||
Hello("0.DOIP/Op.Hello"), ListOps("0.DOIP/Op.ListOperations"), Retrieve(
|
||||
"0.DOIP/Op.Retrieve"), RetrieveYPK("86.BDWare/Op.RetrieveYPK"), Create(
|
||||
"0.DOIP/Op.Create"), Update("0.DOIP/Op.Update"), Delete(
|
||||
"0.DOIP/Op.Delete"), Search("0.DOIP/Op.Search"), Extension(
|
||||
"0.DOIP/Op.Extension"), Unknown("0.DOIP/Op.Unknown");
|
||||
|
||||
private final String val;
|
||||
|
||||
Operations(String s) {
|
||||
@ -254,7 +347,8 @@ public class DOIPOverHttpHandler {
|
||||
|
||||
public static Operations parse(String str) {
|
||||
for (Operations ops : EnumSet.allOf(Operations.class)) {
|
||||
if (ops.val.equals(str)) return ops;
|
||||
if (ops.val.equals(str))
|
||||
return ops;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
package org.bdware.server.http;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DOIPOverHttpStatics {
|
||||
public static List<Records> httpRecords = new ArrayList<Records>();
|
||||
|
||||
static class Records {
|
||||
long start;
|
||||
long end;
|
||||
long dur;
|
||||
String url;
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ import org.bdware.sc.conn.ByteUtil;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.db.CMTables;
|
||||
import org.bdware.sc.db.KeyValueDBUtil;
|
||||
import org.bdware.sc.event.REvent;
|
||||
import org.bdware.sc.units.MultiContractMeta;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.GlobalConf;
|
||||
@ -40,17 +41,17 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientController.class);
|
||||
public static SyncResult sync = new SyncResult();
|
||||
private static boolean startCheck = false;
|
||||
private final Map<String, FileOutputStream> fileMap;
|
||||
private final NetNeighbors neighbors;
|
||||
public Map<String, ResultCallback> distributeReqMap = new ConcurrentHashMap<>();
|
||||
// public NodeCenterClientController cmClientController;
|
||||
// public NodeCenterClientController cmClientController;
|
||||
String nodeID;
|
||||
NodeCenterClientHandler handler;
|
||||
// 合约contractID,master的公钥
|
||||
Map<String, String> contractID2PubKey = new ConcurrentHashMap<>();
|
||||
Deque<JsonObject> receiveQueue = new ArrayDeque<>();
|
||||
ReceiveFileThread receiveFileThread = new ReceiveFileThread();
|
||||
|
||||
public NodeCenterClientController(String nodeID) {
|
||||
this.fileMap = new HashMap<>();
|
||||
this.nodeID = nodeID;
|
||||
this.neighbors = new NetNeighbors();
|
||||
}
|
||||
@ -122,11 +123,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
setNodeID.put("id", keyPair.getPublicKeyStr());
|
||||
String signature = "no signature";
|
||||
try {
|
||||
byte[] sig =
|
||||
SM2Util.sign(
|
||||
keyPair.getPrivateKeyParameter(),
|
||||
(keyPair.getPublicKeyStr() + json.get("session").getAsString())
|
||||
.getBytes());
|
||||
byte[] sig = SM2Util.sign(keyPair.getPrivateKeyParameter(),
|
||||
(keyPair.getPublicKeyStr() + json.get("session").getAsString()).getBytes());
|
||||
signature = ByteUtils.toHexString(sig);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -135,7 +133,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
setNodeID.put("nodeName", GlobalConf.instance.name);
|
||||
setNodeID.put("peerID", GlobalConf.instance.peerID);
|
||||
setNodeID.put("ipPort", GlobalConf.instance.ipPort);
|
||||
setNodeID.put("masterAddress", GlobalConf.instance.masterAddress);
|
||||
setNodeID.put("masterAddress", GlobalConf.instance.ipPort);
|
||||
// setNodeID.put("masterAddress", GlobalConf.instance.masterAddress);
|
||||
|
||||
sendMsg(JsonUtil.toJson(setNodeID));
|
||||
}
|
||||
@ -147,8 +146,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
|
||||
@Action
|
||||
public void syncPong(JsonObject json, ResultCallback resultCallback) {
|
||||
sync.wakeUp(
|
||||
json.get("requestID").getAsString(), "{\"status\":\"Success\",\"result\":\"a\"}");
|
||||
sync.wakeUp(json.get("requestID").getAsString(),
|
||||
"{\"status\":\"Success\",\"result\":\"a\"}");
|
||||
}
|
||||
|
||||
public boolean syncPing() {
|
||||
@ -171,12 +170,9 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
}
|
||||
|
||||
public void listCMInfo() {
|
||||
sendMsg(
|
||||
"{\"action\":\"listCMInfo\",\"pubKey\":\""
|
||||
+ KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey")
|
||||
+ "\",\"requestID\":\""
|
||||
+ System.currentTimeMillis()
|
||||
+ "\"}");
|
||||
sendMsg("{\"action\":\"listCMInfo\",\"pubKey\":\""
|
||||
+ KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey")
|
||||
+ "\",\"requestID\":\"" + System.currentTimeMillis() + "\"}");
|
||||
}
|
||||
|
||||
@Action
|
||||
@ -208,8 +204,9 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
public void updateNonMasters(JsonObject jo, ResultCallback cb) {
|
||||
String[] contracts = jo.get("contracts").getAsString().split(",");
|
||||
for (String id : contracts) {
|
||||
LOGGER.info("旧的master设置合约 " + id + " 自己不再是master");
|
||||
CMActions.manager.setContractIsMaster(id, false + "");
|
||||
if (null != CMActions.manager.setContractIsMaster(id, String.valueOf(false))) {
|
||||
LOGGER.warn("master of contract " + id + " changes");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,6 +221,14 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
LOGGER.info("update " + info.size() + " contracts in node center");
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void publishEventFromCenter(JsonObject jo, ResultCallback rcb) {
|
||||
if (jo.has("event")) {
|
||||
CMActions.manager
|
||||
.deliverEvent(JsonUtil.fromJson(jo.get("event").getAsString(), REvent.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void requestReadMe(JsonObject jo, ResultCallback cb) throws IOException {
|
||||
JsonObject resp = new JsonObject();
|
||||
@ -237,7 +242,6 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
resp.addProperty("pubkey", meta.contract.getPublicKey());
|
||||
resp.addProperty("name", meta.getName());
|
||||
resp.addProperty("buildTime", meta.contract.getBuildTime());
|
||||
resp.addProperty("doi", meta.contract.getDOI());
|
||||
ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr()));
|
||||
ZipEntry zipEntry = zipFile.getEntry("/assets/README.md");
|
||||
if (null == zipEntry) {
|
||||
@ -248,7 +252,7 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
InputStream in = zipFile.getInputStream(zipEntry);
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
byte[] buff = new byte[1024];
|
||||
for (int k; (k = in.read(buff)) > 0; ) {
|
||||
for (int k; (k = in.read(buff)) > 0;) {
|
||||
bo.write(buff, 0, k);
|
||||
}
|
||||
resp.addProperty("readmeStr", bo.toString());
|
||||
@ -269,15 +273,18 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
String requestID = jo.get("requestID").getAsString();
|
||||
String requesterNodeID = jo.get("requesterNodeID").getAsString();
|
||||
String crStr = jo.get("contractRequest").getAsString();
|
||||
String cr =
|
||||
CMActions.manager.executeLocally(
|
||||
JsonUtil.fromJson(crStr, ContractRequest.class), null);
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("action", "onReceiveContractExecution");
|
||||
ret.addProperty("requestID", requestID);
|
||||
ret.addProperty("requesterNodeID", requesterNodeID);
|
||||
ret.addProperty("contractResult", cr);
|
||||
sendMsg(JsonUtil.toJson(ret));
|
||||
CMActions.manager.executeLocallyAsync(JsonUtil.fromJson(crStr, ContractRequest.class),
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
JsonObject ret = new JsonObject();
|
||||
ret.addProperty("action", "onReceiveContractExecution");
|
||||
ret.addProperty("requestID", requestID);
|
||||
ret.addProperty("requesterNodeID", requesterNodeID);
|
||||
ret.addProperty("contractResult", str);
|
||||
sendMsg(JsonUtil.toJson(ret));
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
@ -330,8 +337,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
|
||||
@Override
|
||||
public String routeContract(String contractID) {
|
||||
LOGGER.info("[CMClientController] routeContract : " + contractID);
|
||||
LOGGER.info("contractID2Pubkey.contractsKey=" + contractID2PubKey.containsKey(contractID));
|
||||
LOGGER.debug("[CMClientController] routeContract : " + contractID);
|
||||
LOGGER.debug("contractID2Pubkey.contractsKey=" + contractID2PubKey.containsKey(contractID));
|
||||
// TODO RouteContract是不是?IRP协议/DOIP协议?
|
||||
if (contractID2PubKey.containsKey(contractID)) {
|
||||
return contractID2PubKey.get(contractID);
|
||||
@ -347,15 +354,15 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 100000);
|
||||
req.put("requestID", requestID);
|
||||
sendMsg(JsonUtil.toJson(req));
|
||||
//TODO use async instead?
|
||||
// TODO use async instead?
|
||||
ContractResult cr = sync.syncSleep(requestID);
|
||||
LOGGER.debug("result: " + JsonUtil.toJson(cr));
|
||||
LOGGER.info("node " + pubKey + " succeed!");
|
||||
if (!cr.result.equals(JsonNull.INSTANCE)) {
|
||||
try {
|
||||
JsonObject jo = cr.result.getAsJsonObject();
|
||||
NetworkManager.instance.updateAgentRouter(
|
||||
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString());
|
||||
NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
|
||||
jo.get("masterAddress").getAsString());
|
||||
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
|
||||
return "success";
|
||||
} catch (Exception e) {
|
||||
@ -379,18 +386,17 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
if (!cr.result.equals(JsonNull.INSTANCE)) {
|
||||
try {
|
||||
JsonObject jo = cr.result.getAsJsonObject();
|
||||
NetworkManager.instance.updateAgentRouter(
|
||||
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString());
|
||||
NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
|
||||
jo.get("masterAddress").getAsString());
|
||||
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
|
||||
LOGGER.info(
|
||||
String.format("the master of contract %s: pubKey=%s address=%s",
|
||||
contractID,
|
||||
jo.get("pubKey").getAsString(),
|
||||
jo.get("masterAddress").getAsString()));
|
||||
LOGGER.info(String.format("the master of contract %s: pubKey=%s address=%s",
|
||||
contractID, jo.get("pubKey").getAsString(),
|
||||
jo.get("masterAddress").getAsString()));
|
||||
contractID2PubKey.put(contractID, jo.get("pubKey").getAsString());
|
||||
return jo.get("pubKey").getAsString();
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("the master of contract " + contractID + " is null! " + e.getMessage());
|
||||
LOGGER.warn(
|
||||
"the master of contract " + contractID + " is null! " + e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -409,8 +415,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
return;
|
||||
}
|
||||
startCheck = true;
|
||||
for (MultiContractMeta meta :
|
||||
CMActions.manager.multiContractRecorder.getStatus().values()) {
|
||||
for (MultiContractMeta meta : CMActions.manager.multiContractRecorder.getStatus()
|
||||
.values()) {
|
||||
String contractID = meta.getContractID();
|
||||
LOGGER.info("check master of contract " + contractID);
|
||||
ContractMeta cmeta = CMActions.manager.statusRecorder.getContractMeta(meta.getID());
|
||||
@ -424,20 +430,21 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
// 该合约可能在这个节点崩溃期间已经被终止,不存在了,这个节点不用恢复这个合约了,直接从数据库中删除
|
||||
LOGGER.info("该合约在集群已经不存在!");
|
||||
KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), contractID);
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.CheckPointLastHash.toString(), contractID)) {
|
||||
KeyValueDBUtil.instance.delete(
|
||||
CMTables.CheckPointLastHash.toString(), contractID);
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.CheckPointLastHash.toString(),
|
||||
contractID)) {
|
||||
KeyValueDBUtil.instance.delete(CMTables.CheckPointLastHash.toString(),
|
||||
contractID);
|
||||
}
|
||||
if (KeyValueDBUtil.instance.containsKey(
|
||||
CMTables.LastExeSeq.toString(), contractID)) {
|
||||
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
|
||||
contractID)) {
|
||||
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
MasterClientRecoverMechAction.recoverSet.add(contractID);
|
||||
|
||||
LOGGER.info("queryUnitContractsID master为" + master.substring(0, 5) + " -> 合约id:" + contractID);
|
||||
LOGGER.info("queryUnitContractsID master为" + master.substring(0, 5) + " -> 合约id:"
|
||||
+ contractID);
|
||||
RecoverMechTimeRecorder.queryMasterFinish = System.currentTimeMillis();
|
||||
queryUnitContractsID2(contractID, master);
|
||||
}
|
||||
@ -478,9 +485,7 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
|
||||
@Action(async = true)
|
||||
public void requestLog(JsonObject json, ResultCallback rc) {
|
||||
if (!json.has("requestID")
|
||||
|| !json.has("contractID")
|
||||
|| !json.has("offset")
|
||||
if (!json.has("requestID") || !json.has("contractID") || !json.has("offset")
|
||||
|| !json.has("count")) {
|
||||
LOGGER.debug(
|
||||
"[CMClientController] missing arguments, requestID / contractID / offset / count");
|
||||
@ -518,69 +523,20 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
queryNCRepoDOI(json, result);
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void receiveProject(JsonObject args, final ResultCallback rc) {
|
||||
LOGGER.debug("position----7");
|
||||
|
||||
String fileName = args.get("fileName").getAsString();
|
||||
boolean isAppend = args.get("isAppend").getAsBoolean();
|
||||
boolean isDone = args.get("isDone").getAsBoolean();
|
||||
boolean isPrivate = args.get("isPrivate").getAsBoolean();
|
||||
|
||||
LOGGER.debug(
|
||||
String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate));
|
||||
String path = GlobalConf.instance.publicCompiledDir;
|
||||
if (isPrivate && args.has("pubKey")) {
|
||||
path = GlobalConf.instance.privateCompiledDir + "/" + args.get("pubKey").getAsString();
|
||||
}
|
||||
|
||||
File dir = new File(path);
|
||||
if (!dir.exists()) {
|
||||
LOGGER.debug("mkdir " + dir.getAbsoluteFile() + ": " + dir.mkdirs());
|
||||
}
|
||||
FileOutputStream fout = null;
|
||||
if (!isAppend) {
|
||||
try {
|
||||
fout = new FileOutputStream(new File(dir, fileName));
|
||||
fileMap.put(fileName, fout);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
fout = fileMap.get(fileName);
|
||||
}
|
||||
if (isDone) {
|
||||
if (fout != null)
|
||||
try {
|
||||
fout.close();
|
||||
fileMap.remove(fileName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOGGER.debug("receive finish.");
|
||||
Map<String, String> req = new HashMap<>();
|
||||
req.put("action", "onReceiveProject");
|
||||
req.put("requestID", args.get("requestID").getAsString());
|
||||
req.put("nodeID", nodeID);
|
||||
req.put("progress", "100");
|
||||
rc.onResult(JsonUtil.toJson(req));
|
||||
} else {
|
||||
String data = args.get("data").getAsString();
|
||||
byte[] byteData = ByteUtil.decodeBASE64(data);
|
||||
try {
|
||||
if (null != fout && null != byteData) {
|
||||
fout.write(byteData);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@Action(async = false)
|
||||
public synchronized void receiveProject(JsonObject args, final ResultCallback rc) {
|
||||
try {
|
||||
receiveQueue.add(args);
|
||||
synchronized (receiveFileThread) {
|
||||
receiveFileThread.notify();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void queryNCRepoDOI(JsonObject json, ResultCallback result) {
|
||||
LOGGER.debug("sendProject: position ---- 3");
|
||||
String projectName = json.get("projectName").getAsString();
|
||||
LOGGER.debug("sendProject: position ---- 3\tprojectName=" + projectName);
|
||||
String parPath = GlobalConf.instance.publicCompiledDir;
|
||||
boolean isPrivate = false;
|
||||
if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) {
|
||||
@ -601,11 +557,11 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
LOGGER.debug("tempZip = " + tempZip.getAbsolutePath());
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(tempZip);
|
||||
byte[] buff = new byte[30 * 1024];
|
||||
byte[] buff = new byte[500 * 1024];
|
||||
long total = tempZip.length();
|
||||
LOGGER.debug("temp length = " + total);
|
||||
long count = 0;
|
||||
for (int len; (len = (fin.read(buff))) > 0; ) {
|
||||
for (int len; (len = (fin.read(buff))) > 0;) {
|
||||
LOGGER.debug("read len = " + len);
|
||||
req.put("data", ByteUtil.encodeBASE64(buff, len));
|
||||
count += len;
|
||||
@ -669,8 +625,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
MasterElectTimeRecorder.slaveConnectFinish = System.currentTimeMillis();
|
||||
|
||||
// 开启master恢复
|
||||
MasterServerRecoverMechAction.newMasterRecover(
|
||||
contractID, json.get("members").getAsString(), onlineMembers);
|
||||
MasterServerRecoverMechAction.newMasterRecover(contractID,
|
||||
json.get("members").getAsString(), onlineMembers);
|
||||
}
|
||||
|
||||
private boolean waitForConnection(List<String> nodeNames) {
|
||||
@ -690,7 +646,8 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else return true;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -701,18 +658,34 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void onDistribute(JsonObject json, ResultCallback rc) {
|
||||
public void onDistribute(JsonObject json, ResultCallback rc) throws IOException {
|
||||
if (json.has("over")) {
|
||||
String distributeID = json.get("distributeID").getAsString();
|
||||
String distributeID = null;
|
||||
if (json.has("distributeID"))
|
||||
distributeID = json.get("distributeID").getAsString();
|
||||
else
|
||||
distributeID = json.get("responseID").getAsString();
|
||||
ResultCallback to = distributeReqMap.get(distributeID);
|
||||
distributeReqMap.remove(distributeID);
|
||||
to.onResult(json.get("content").getAsString());
|
||||
if (to instanceof Closeable) {
|
||||
((Closeable) to).close();
|
||||
}
|
||||
} else {
|
||||
String distributeID = json.get("distributeID").getAsString();
|
||||
distributeReqMap.get(distributeID).onResult(json.get("content").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void onDistributeYPK(JsonObject json, ResultCallback rc) {
|
||||
try {
|
||||
onDistribute(json, rc);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Action(async = true)
|
||||
public void transferInstance(JsonObject jo, ResultCallback result) {
|
||||
LOGGER.info("transferInstance");
|
||||
@ -743,13 +716,11 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
public void NCStartElect(JsonObject jo, ResultCallback result) {
|
||||
String conID = jo.get("contractID").getAsString();
|
||||
String uniNumber = null;
|
||||
if (jo.has("nuiNumber")) uniNumber = jo.get("nuiNumber").getAsString();
|
||||
if (jo.has("nuiNumber"))
|
||||
uniNumber = jo.get("nuiNumber").getAsString();
|
||||
ContractClient cc = CMActions.manager.getClient(conID);
|
||||
LOGGER.info(
|
||||
"[CMClientController] NCStartElect : contractID="
|
||||
+ conID
|
||||
+ " client==null:"
|
||||
+ (null == cc));
|
||||
LOGGER.info("[CMClientController] NCStartElect : contractID=" + conID + " client==null:"
|
||||
+ (null == cc));
|
||||
|
||||
// 不是自己本地的合约
|
||||
if (null == cc) {
|
||||
@ -786,4 +757,91 @@ public class NodeCenterClientController implements NodeCenterConn {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReceiveFileThread extends Thread {
|
||||
private final Map<String, FileOutputStream> fileMap = new HashMap<>();
|
||||
|
||||
ReceiveFileThread() {
|
||||
super();
|
||||
this.start();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (;;) {
|
||||
if (receiveQueue.size() > 0) {
|
||||
try {
|
||||
JsonObject jo = receiveQueue.poll();
|
||||
receiveProject(jo);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
synchronized (ReceiveFileThread.this) {
|
||||
try {
|
||||
this.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void receiveProject(JsonObject args) {
|
||||
|
||||
String fileName = args.get("fileName").getAsString();
|
||||
boolean isAppend = args.get("isAppend").getAsBoolean();
|
||||
boolean isDone = args.get("isDone").getAsBoolean();
|
||||
boolean isPrivate = args.get("isPrivate").getAsBoolean();
|
||||
LOGGER.debug(String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone,
|
||||
isPrivate));
|
||||
String path = GlobalConf.instance.publicCompiledDir;
|
||||
if (isPrivate && args.has("pubKey")) {
|
||||
path = GlobalConf.instance.privateCompiledDir + "/"
|
||||
+ args.get("pubKey").getAsString();
|
||||
}
|
||||
File dir = new File(path);
|
||||
if (!dir.exists()) {
|
||||
LOGGER.debug("mkdir " + dir.getAbsoluteFile() + ": " + dir.mkdirs());
|
||||
}
|
||||
FileOutputStream fout = null;
|
||||
if (!isAppend) {
|
||||
try {
|
||||
fout = new FileOutputStream(new File(dir, fileName));
|
||||
fileMap.put(fileName, fout);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
fout = fileMap.get(fileName);
|
||||
}
|
||||
if (isDone) {
|
||||
if (fout != null)
|
||||
try {
|
||||
fout.close();
|
||||
fileMap.remove(fileName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOGGER.debug("receive finish.");
|
||||
Map<String, String> req = new HashMap<>();
|
||||
req.put("action", "onReceiveProject");
|
||||
req.put("requestID", args.get("requestID").getAsString());
|
||||
req.put("nodeID", nodeID);
|
||||
req.put("progress", "100");
|
||||
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(req));
|
||||
} else {
|
||||
String data = args.get("data").getAsString();
|
||||
byte[] byteData = ByteUtil.decodeBASE64(data);
|
||||
try {
|
||||
if (null != fout && null != byteData) {
|
||||
fout.write(byteData);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.bdware.server.CMHttpServer.pluginLoader;
|
||||
|
||||
@ -31,7 +30,7 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
|
||||
public boolean hasPermission;
|
||||
private final NodeCenterClientController controller;
|
||||
Channel channel;
|
||||
// UDPTrustfulExecutor udpExecutor;
|
||||
// UDPTrustfulExecutor udpExecutor;
|
||||
// RecoverMechExecutor recoverMechExecutor;
|
||||
ActionExecutor<ResultCallback, JsonObject> ae;
|
||||
private boolean isConnected;
|
||||
@ -82,8 +81,8 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
|
||||
}
|
||||
|
||||
public synchronized void sendMsg(String msg) {
|
||||
// logger.debug("[CMClientHandler] sendMsg, TID:" +
|
||||
// Thread.currentThread().getId() + msg+" isActive:"+channel.isActive()+"
|
||||
// logger.debug("[CMClientHandler] sendMsg, TID:" +
|
||||
// Thread.currentThread().getId() + msg+" isActive:"+channel.isActive()+"
|
||||
// "+channel.isOpen());
|
||||
ByteBuf buf = Unpooled.wrappedBuffer(msg.getBytes());
|
||||
channel.writeAndFlush(buf);
|
||||
@ -100,19 +99,16 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
|
||||
final JsonObject arg =
|
||||
JsonParser.parseReader(new InputStreamReader(new ByteArrayInputStream(bytes)))
|
||||
.getAsJsonObject();
|
||||
// logger.debug("[CMClient] receive:" + arg.toString());
|
||||
// logger.debug("[CMClient] receive:" + arg.toString());
|
||||
|
||||
if (arg.has("action")) {
|
||||
final String action = arg.get("action").getAsString();
|
||||
ae.handle(
|
||||
action,
|
||||
arg,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
sendMsg(str);
|
||||
}
|
||||
});
|
||||
ae.handle(action, arg, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
sendMsg(str);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} catch (java.lang.IllegalArgumentException e) {
|
||||
@ -136,7 +132,8 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
|
||||
public void close() {
|
||||
try {
|
||||
isConnected = false;
|
||||
if (channel != null) channel.close();
|
||||
if (channel != null)
|
||||
channel.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
@ -19,4 +19,4 @@ public class PubkeyResultCallback extends ResultCallback {
|
||||
public void onResult(String str) {
|
||||
rc.onResult(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,16 +40,10 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
public TCPClientFrameHandler(String masterPubkey) {
|
||||
master = masterPubkey;
|
||||
aliveCheckClientAction = new AliveCheckClientAction(masterPubkey);
|
||||
ae = new ActionExecutor<>(
|
||||
executorService,
|
||||
aliveCheckClientAction,
|
||||
new MasterClientTCPAction(),
|
||||
new MasterClientRecoverMechAction(),
|
||||
MasterClientTransferAction.instance,
|
||||
new MasterServerRecoverMechAction(),
|
||||
new MasterServerTransferAction(),
|
||||
new MasterServerTCPAction(),
|
||||
new EventActions());
|
||||
ae = new ActionExecutor<>(executorService, aliveCheckClientAction,
|
||||
new MasterClientTCPAction(), new MasterClientRecoverMechAction(),
|
||||
MasterClientTransferAction.instance, new MasterServerRecoverMechAction(),
|
||||
new MasterServerTransferAction(), new MasterServerTCPAction(), new EventActions());
|
||||
for (String str : clientToAgentPlugins) {
|
||||
Object obj = createInstanceByClzName(str);
|
||||
ae.appendHandler(obj);
|
||||
@ -66,21 +60,22 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
}
|
||||
|
||||
|
||||
// public void updateContractID2Client(String contract) {
|
||||
// // 如果该合约正在和旧的master保持连接
|
||||
// if (MasterClientTCPAction.contractID2MasterInfo.containsKey(contract)) {
|
||||
// MasterClientTCPAction former =
|
||||
// MasterClientTCPAction.contractID2MasterInfo.get(contract);
|
||||
// former.closeMaster();
|
||||
// }
|
||||
//
|
||||
// MasterClientTCPAction.contractID2MasterInfo.put(contract, actions);
|
||||
// }
|
||||
// public void updateContractID2Client(String contract) {
|
||||
// // 如果该合约正在和旧的master保持连接
|
||||
// if (MasterClientTCPAction.contractID2MasterInfo.containsKey(contract)) {
|
||||
// MasterClientTCPAction former =
|
||||
// MasterClientTCPAction.contractID2MasterInfo.get(contract);
|
||||
// former.closeMaster();
|
||||
// }
|
||||
//
|
||||
// MasterClientTCPAction.contractID2MasterInfo.put(contract, actions);
|
||||
// }
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
aliveCheckClientAction.closeMaster();
|
||||
if (channel != null) channel.close();
|
||||
if (channel != null)
|
||||
channel.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@ -108,7 +103,8 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
ByteBuf bb = (ByteBuf) frame;
|
||||
JsonObject arg;
|
||||
try {
|
||||
arg = JsonUtil.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb)));
|
||||
arg = JsonUtil
|
||||
.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Response response = new Response();
|
||||
@ -119,7 +115,6 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
}
|
||||
Response response;
|
||||
try {
|
||||
//LOGGER.info("====== TCPClientFrameHandler:" + arg.toString());
|
||||
final String action = arg.get("action").getAsString();
|
||||
PubkeyResultCallback pc = new PubkeyResultCallback(master, new ResultCallback() {
|
||||
@Override
|
||||
@ -127,10 +122,7 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
sendMsg(ret);
|
||||
}
|
||||
});
|
||||
ae.handle(
|
||||
action,
|
||||
arg, pc
|
||||
);
|
||||
ae.handle(action, arg, pc);
|
||||
} catch (IllegalArgumentException e) {
|
||||
response = new Response();
|
||||
response.action = "onException";
|
||||
@ -146,12 +138,9 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
int count = 0;
|
||||
for (String s : strs) {
|
||||
if (s.contains("sun.reflect")
|
||||
|| s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache")
|
||||
|| s.contains("java.util")
|
||||
|| s.contains("java.lang")
|
||||
|| s.contains("io.netty")) {
|
||||
if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache") || s.contains("java.util")
|
||||
|| s.contains("java.lang") || s.contains("io.netty")) {
|
||||
continue;
|
||||
}
|
||||
ret.append(s);
|
||||
|
@ -31,52 +31,44 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
public TCPServerFrameHandler() {
|
||||
checkAction = new AliveCheckServerAction(this);
|
||||
ae =
|
||||
new ActionExecutor<ResultCallback, JsonObject>(
|
||||
executorService,
|
||||
checkAction,
|
||||
new MasterClientTCPAction(),
|
||||
new MasterClientRecoverMechAction(),
|
||||
MasterClientTransferAction.instance,
|
||||
new MasterServerRecoverMechAction(),
|
||||
new MasterServerTransferAction(),
|
||||
new MasterServerTCPAction(),
|
||||
new EventActions()) {
|
||||
@Override
|
||||
public boolean checkPermission(
|
||||
Action a, final JsonObject args, long permission) {
|
||||
return true;
|
||||
// long val = a.userPermission();
|
||||
//
|
||||
// boolean flag;
|
||||
// String status = "refuse";
|
||||
// String action = args.get("action");
|
||||
//
|
||||
// if (val == 0) {
|
||||
// flag = true;
|
||||
// status = "accept";
|
||||
// } else if ((permission & val) == val) {
|
||||
// flag = true;
|
||||
// status = "accept";
|
||||
// } else {
|
||||
// flag = false;
|
||||
// }
|
||||
//
|
||||
// TimeDBUtil.instance.put(
|
||||
// NCTables.CMLog.toString(),
|
||||
// "{\"action\":\""
|
||||
// + action
|
||||
// + "\",\"pubKey\":\""
|
||||
// + pubKey
|
||||
// + "\",\"status\":\""
|
||||
// + status
|
||||
// + "\",\"date\":"
|
||||
// + System.currentTimeMillis()
|
||||
// + "}");
|
||||
//
|
||||
// return flag;
|
||||
}
|
||||
};
|
||||
ae = new ActionExecutor<ResultCallback, JsonObject>(executorService, checkAction,
|
||||
new MasterClientTCPAction(), new MasterClientRecoverMechAction(),
|
||||
MasterClientTransferAction.instance, new MasterServerRecoverMechAction(),
|
||||
new MasterServerTransferAction(), new MasterServerTCPAction(), new EventActions()) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, final JsonObject args, long permission) {
|
||||
return true;
|
||||
// long val = a.userPermission();
|
||||
//
|
||||
// boolean flag;
|
||||
// String status = "refuse";
|
||||
// String action = args.get("action");
|
||||
//
|
||||
// if (val == 0) {
|
||||
// flag = true;
|
||||
// status = "accept";
|
||||
// } else if ((permission & val) == val) {
|
||||
// flag = true;
|
||||
// status = "accept";
|
||||
// } else {
|
||||
// flag = false;
|
||||
// }
|
||||
//
|
||||
// TimeDBUtil.instance.put(
|
||||
// NCTables.CMLog.toString(),
|
||||
// "{\"action\":\""
|
||||
// + action
|
||||
// + "\",\"pubKey\":\""
|
||||
// + pubKey
|
||||
// + "\",\"status\":\""
|
||||
// + status
|
||||
// + "\",\"date\":"
|
||||
// + System.currentTimeMillis()
|
||||
// + "}");
|
||||
//
|
||||
// return flag;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -98,8 +90,9 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
ByteBuf bb = (ByteBuf) frame;
|
||||
JsonObject arg;
|
||||
try {
|
||||
arg = JsonUtil.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb)));
|
||||
// logger.info("[MasterServer] receive:" + arg.toString());
|
||||
arg = JsonUtil
|
||||
.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb)));
|
||||
// logger.info("[MasterServer] receive:" + arg.toString());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -114,16 +107,14 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
final String action = arg.get("action").getAsString();
|
||||
|
||||
PubkeyResultCallback pubkeyResultCallback = new PubkeyResultCallback(checkAction.pubKey, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String ret) {
|
||||
sendMsg(ret);
|
||||
}
|
||||
});
|
||||
ae.handle(
|
||||
action,
|
||||
arg, pubkeyResultCallback
|
||||
);
|
||||
PubkeyResultCallback pubkeyResultCallback =
|
||||
new PubkeyResultCallback(checkAction.pubKey, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String ret) {
|
||||
sendMsg(ret);
|
||||
}
|
||||
});
|
||||
ae.handle(action, arg, pubkeyResultCallback);
|
||||
|
||||
} catch (IllegalArgumentException e) {
|
||||
response = new Response();
|
||||
@ -141,12 +132,9 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
int count = 0;
|
||||
for (String s : strs) {
|
||||
if (s.contains("sun.reflect")
|
||||
|| s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache")
|
||||
|| s.contains("java.util")
|
||||
|| s.contains("java.lang")
|
||||
|| s.contains("io.netty")) {
|
||||
if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache") || s.contains("java.util")
|
||||
|| s.contains("java.lang") || s.contains("io.netty")) {
|
||||
continue;
|
||||
}
|
||||
ret.append(s);
|
||||
|
@ -33,28 +33,24 @@ public class AgentManager implements AgentPeerManagerIntf {
|
||||
|
||||
@Override
|
||||
public void executeByOtherNodeAsync(String pubKey, ContractRequest c, ResultCallback cb) {
|
||||
LOGGER.error(pubKey + " " + c.getRequestID() + " " + c.getContractID() + " cb:" + cb);
|
||||
LOGGER.debug(pubKey + " " + c.getRequestID() + " " + c.getContractID() + " cb:" + cb);
|
||||
int maxMasterProxyLoad = CongestionControl.masterProxyLoad.incrementAndGet();
|
||||
if (maxMasterProxyLoad > CongestionControl.maxMasterProxyLoad)
|
||||
CongestionControl.maxMasterProxyLoad = maxMasterProxyLoad;
|
||||
if (CongestionControl.slaveControl()) {
|
||||
ContractResult cr =
|
||||
new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive("canceled because of queue too long"));
|
||||
ContractResult cr = new ContractResult(ContractResult.Status.Error,
|
||||
new JsonPrimitive("canceled because of queue too long"));
|
||||
cb.onResult(JsonUtil.parseObjectAsJsonObject(cr));
|
||||
CongestionControl.masterProxyLoad.decrementAndGet();
|
||||
return;
|
||||
}
|
||||
MasterServerTCPAction.sync.sleep(
|
||||
c.getRequestID(),
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
cb.onResult(JsonUtil.parseStringAsJsonObject(str));
|
||||
CongestionControl.masterProxyLoad.decrementAndGet();
|
||||
}
|
||||
});
|
||||
MasterServerTCPAction.sync.sleep(c.getRequestID(), new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
cb.onResult(JsonUtil.parseStringAsJsonObject(str));
|
||||
CongestionControl.masterProxyLoad.decrementAndGet();
|
||||
}
|
||||
});
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
req.put("action", "requestContractExecutionServer");
|
||||
req.put("requestID", c.getRequestID());
|
||||
@ -93,8 +89,8 @@ public class AgentManager implements AgentPeerManagerIntf {
|
||||
|
||||
@Override
|
||||
public void transferToOtherNode(String pubKey, String contractID) {
|
||||
// TODO 问题1,合约的ypk或者script怎么获取?ypk如果是包含私有路径的可以在Contract中设置一个字段,启动的时候写入吗 目前认为这个从前端传入
|
||||
// TODO 问题2 转移过程中进度返回给哪个前端显示?需要显示吗?
|
||||
// TODO 问题1,合约的ypk或者script怎么获取?ypk如果是包含私有路径的可以在Contract中设置一个字段,启动的时候写入吗 目前认为这个从前端传入
|
||||
// TODO 问题2 转移过程中进度返回给哪个前端显示?需要显示吗?
|
||||
LOGGER.info("transferToOtherNode : pubKey=" + pubKey + " contractID=" + contractID);
|
||||
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
|
||||
MasterClientTransferAction.instance.transferInstance(pubKey, meta.getID());
|
||||
|
@ -9,7 +9,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class KillUnitContractResultCollector extends ResultCallback {
|
||||
// private static final Logger LOGGER = LogManager.getLogger(ResultCallback.class);
|
||||
// private static final Logger LOGGER = LogManager.getLogger(ResultCallback.class);
|
||||
int total;
|
||||
String requestID;
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
@ -24,9 +24,9 @@ public class KillUnitContractResultCollector extends ResultCallback {
|
||||
|
||||
@Override
|
||||
public void onResult(String str) {
|
||||
//解析str,判断该节点是否kill成功
|
||||
Map<String, String> map = JsonUtil.fromJson(str, new TypeToken<Map<String, String>>() {
|
||||
}.getType());
|
||||
// 解析str,判断该节点是否kill成功
|
||||
Map<String, String> map =
|
||||
JsonUtil.fromJson(str, new TypeToken<Map<String, String>>() {}.getType());
|
||||
String data = map.get("data");
|
||||
if (data != null && data.equals("success")) {
|
||||
//
|
||||
|
@ -5,9 +5,9 @@ import org.bdware.sc.bean.ContractExecType;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
//在master上
|
||||
// 在master上
|
||||
public class MultiPointContractInfo {
|
||||
public List<String> members; //pubKey
|
||||
public List<String> members; // pubKey
|
||||
public String masterNode;
|
||||
public ContractExecType type;
|
||||
public ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode;
|
||||
|
@ -5,10 +5,10 @@ import org.bdware.sc.bean.ContractExecType;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
//改名 MultiPointCooperateContractInfo
|
||||
// 改名 MultiPointCooperateContractInfo
|
||||
public class MultiPointCooperateContractInfo extends MultiPointContractInfo {
|
||||
public List<String> members; //pubKey
|
||||
public List<Integer> shardingId; //sharding id 初始化时被分配
|
||||
public List<String> members; // pubKey
|
||||
public List<Integer> shardingId; // sharding id 初始化时被分配
|
||||
public String masterNode;
|
||||
public ContractExecType type;
|
||||
public ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode;
|
||||
|
@ -1,11 +0,0 @@
|
||||
package org.bdware.server.trustedmodel;
|
||||
|
||||
public class MultiReqSeq {
|
||||
public final int seq;
|
||||
public final long startTime;
|
||||
|
||||
public MultiReqSeq(int s){
|
||||
seq = s;
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import org.bdware.server.action.p2p.MasterServerTCPAction;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class ResultCollector extends ResultCallback {
|
||||
// private static final Logger LOGGER = LogManager.getLogger(ResultCallback.class);
|
||||
// private static final Logger LOGGER = LogManager.getLogger(ResultCallback.class);
|
||||
int total;
|
||||
String requestID;
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
|
@ -1,305 +0,0 @@
|
||||
package org.bdware.server.trustedmodel;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.sc.ContractClient;
|
||||
import org.bdware.sc.ContractManager;
|
||||
import org.bdware.sc.ContractResult;
|
||||
import org.bdware.sc.bean.ContractRequest;
|
||||
import org.bdware.sc.bean.FunctionDesp;
|
||||
import org.bdware.sc.bean.SM2Verifiable;
|
||||
import org.bdware.sc.conn.OnHashCallback;
|
||||
import org.bdware.sc.conn.ResultCallback;
|
||||
import org.bdware.sc.units.MultiContractMeta;
|
||||
import org.bdware.sc.units.RecoverFlag;
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
import org.bdware.server.action.CMActions;
|
||||
import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
|
||||
import org.bdware.units.NetworkManager;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Kaidong Wu
|
||||
*/
|
||||
public class SelfAdaptiveShardingExecutor implements ContractExecutor {
|
||||
private static final Logger LOGGER = LogManager.getLogger(SelfAdaptiveShardingExecutor.class);
|
||||
private static final int SUBMIT_LIMIT = 1024;
|
||||
private final Queue<ContractRequest> reqQueue = new ConcurrentLinkedQueue<>();
|
||||
private final MultiContractMeta meta;
|
||||
private final Map<String, Block> toExecuted = new ConcurrentHashMap<>();
|
||||
private final Set<String> executedBlocks = ConcurrentHashMap.newKeySet();
|
||||
private final Map<String, Boolean> executedTxs = new ConcurrentHashMap<>();
|
||||
private final Object flag = new Object();
|
||||
private final ScheduledFuture<?> future;
|
||||
private boolean running = true;
|
||||
private Block b = new Block();
|
||||
|
||||
public SelfAdaptiveShardingExecutor(String contractID) {
|
||||
this.meta =
|
||||
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
|
||||
this.future = ContractManager.scheduledThreadPool.scheduleWithFixedDelay(
|
||||
this::submitBlock,
|
||||
2,
|
||||
2,
|
||||
TimeUnit.SECONDS);
|
||||
LOGGER.debug(String.format("ContractManager.threadPool=%d/%d",
|
||||
((ThreadPoolExecutor) ContractManager.threadPool).getActiveCount(),
|
||||
((ThreadPoolExecutor) ContractManager.threadPool).getPoolSize()));
|
||||
ContractManager.threadPool.execute(() -> {
|
||||
LOGGER.info(
|
||||
"[SelfAdaptiveShardingExecutor " + meta.getContractID() + "] starting service..." + running);
|
||||
while (running) {
|
||||
LOGGER.info("checking blocks to be executed, latest block=" +
|
||||
this.b.prevHash + ", to be executed size=" + toExecuted.size());
|
||||
LOGGER.debug("executed: " + JsonUtil.toJson(executedBlocks) + "\n\t" + JsonUtil.toJson(executedTxs));
|
||||
while (!toExecuted.isEmpty()) {
|
||||
String key = this.b.prevHash;
|
||||
Block block = toExecuted.get(key);
|
||||
if (null != block) {
|
||||
executeBlock(block);
|
||||
}
|
||||
toExecuted.remove(key);
|
||||
}
|
||||
synchronized (flag) {
|
||||
try {
|
||||
flag.wait();
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.warn(String.format(
|
||||
"[SelfAdaptiveShardingExecutor %s] waiting is interrupted: %s",
|
||||
meta.getContractID(),
|
||||
e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String requestID, ContractRequest req, ResultCallback rcb, OnHashCallback hcb) {
|
||||
// check client
|
||||
ContractClient client = CMActions.manager.getClient(meta.getContractID());
|
||||
if (null == client) {
|
||||
LOGGER.error("contract " + meta.getContractID() + " not found!");
|
||||
rcb.onResult(JsonUtil.toJson(new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive("contract " + meta.getContractID() + " not found!"))));
|
||||
return;
|
||||
}
|
||||
// check function
|
||||
FunctionDesp funDesp = client.contractMeta.getExportedFunction(req.getAction());
|
||||
if (null == funDesp) {
|
||||
LOGGER.warn("action " + req.getAction() + " of contract " + meta.getContractID() + " not found!");
|
||||
rcb.onResult(JsonUtil.toJson(new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive(
|
||||
String.format("action %s of contract %s not found!",
|
||||
req.getAction(),
|
||||
meta.getContractID())))));
|
||||
return;
|
||||
}
|
||||
// for view function, execute it
|
||||
if (funDesp.isView) {
|
||||
CMActions.manager.executeLocallyAsync(req, rcb, hcb);
|
||||
return;
|
||||
}
|
||||
// normal function, check if it is in blocks
|
||||
if (executedTxs.containsKey(requestID)) {
|
||||
rcb.onResult(JsonUtil.toJson(new ContractResult(
|
||||
ContractResult.Status.Error,
|
||||
new JsonPrimitive("this request has been packed!"))));
|
||||
return;
|
||||
}
|
||||
// add blocks into request cache
|
||||
LOGGER.debug("receive contract request " + requestID);
|
||||
executedTxs.put(requestID, false);
|
||||
reqQueue.add(req);
|
||||
rcb.onResult(JsonUtil.toJson(new ContractResult(
|
||||
ContractResult.Status.Executing,
|
||||
new JsonPrimitive("this request is adding into blocks"))));
|
||||
// if cache is full, submit
|
||||
if (reqQueue.size() >= SUBMIT_LIMIT) {
|
||||
ContractManager.threadPool.execute(this::submitBlock);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// stop threads
|
||||
this.future.cancel(true);
|
||||
this.running = false;
|
||||
LOGGER.info("destruct executor of contract " + meta.getContractID());
|
||||
}
|
||||
|
||||
public void execute(String blockStr) {
|
||||
Block block = JsonUtil.fromJson(blockStr, Block.class);
|
||||
// the block must have not been cached or executed, and must be valid
|
||||
if (!toExecuted.containsKey(block.prevHash) &&
|
||||
!executedBlocks.contains(block.hash) &&
|
||||
block.isValid()) {
|
||||
// add block into block cache
|
||||
LOGGER.info(String.format(
|
||||
"[SelfAdaptiveShardingExecutor %s] receive block %s -> %s," +
|
||||
" %d transactions, timestamp=%d, size=%d",
|
||||
meta.getContractID(),
|
||||
block.hash,
|
||||
block.prevHash,
|
||||
block.requests.length,
|
||||
block.timestamp,
|
||||
blockStr.length()));
|
||||
toExecuted.put(block.prevHash, block);
|
||||
// notify thread to execute blocks
|
||||
synchronized (flag) {
|
||||
flag.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void executeBlock(Block block) {
|
||||
// used for the thread to execute blocks
|
||||
LOGGER.debug("start");
|
||||
// check contract requests, requests must have not been executed
|
||||
for (ContractRequest request : block.requests) {
|
||||
if (executedTxs.containsKey(request.getRequestID()) && executedTxs.get(request.getRequestID())) {
|
||||
LOGGER.debug("find request " + request.getRequestID() + " has been executed!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// TODO check status
|
||||
// executed requests
|
||||
for (ContractRequest request : block.requests) {
|
||||
String ret = CMActions.manager.executeLocally(request, null);
|
||||
LOGGER.debug(String.format(
|
||||
"[SelfAdaptiveShardingExecutor %s] result of request %s: %s",
|
||||
meta.getContractID(),
|
||||
request.getRequestID(),
|
||||
ret));
|
||||
executedTxs.put(request.getRequestID(), true);
|
||||
}
|
||||
LOGGER.info(String.format(
|
||||
"[SelfAdaptiveShardingExecutor %s] execute %d transactions of block %s",
|
||||
meta.getContractID(),
|
||||
block.requests.length,
|
||||
block.hash));
|
||||
// TODO create check point
|
||||
this.b = new Block(block.hash, this.b.height + 1);
|
||||
executedBlocks.add(block.hash);
|
||||
}
|
||||
|
||||
private void submitBlock() {
|
||||
Block block = fillBlock();
|
||||
if (null != block) {
|
||||
LOGGER.info("deliver block " + block.hash + "...");
|
||||
LOGGER.debug(JsonUtil.toPrettyJson(block));
|
||||
String[] nodes = this.meta.getMembers();
|
||||
JsonObject req = new JsonObject();
|
||||
req.addProperty("action", "deliverBlock");
|
||||
req.addProperty("data", JsonUtil.toJson(block));
|
||||
req.addProperty("contractID", this.meta.getContractID());
|
||||
String reqStr = req.toString();
|
||||
// deliver blocks
|
||||
for (String node : nodes) {
|
||||
if (MasterServerRecoverMechAction.recoverStatus.get(node).get(this.meta.getContractID())
|
||||
== RecoverFlag.Fine) {
|
||||
NetworkManager.instance.sendToAgent(node, reqStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized Block fillBlock() {
|
||||
// pack contract requests into a block
|
||||
ContractRequest[] requests = new ContractRequest[Math.min(reqQueue.size(), SUBMIT_LIMIT)];
|
||||
if (requests.length == 0) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < requests.length; ++i) {
|
||||
requests[i] = reqQueue.poll();
|
||||
}
|
||||
this.b.fillBlock(requests);
|
||||
return this.b;
|
||||
}
|
||||
|
||||
static class Block extends SM2Verifiable {
|
||||
String prevHash = "0";
|
||||
String hash;
|
||||
int height;
|
||||
String checkPoint;
|
||||
String body;
|
||||
String nodePubKey;
|
||||
ContractRequest[] requests;
|
||||
long timestamp;
|
||||
|
||||
public Block() {
|
||||
this.height = 0;
|
||||
}
|
||||
|
||||
public Block(String prev, int height) {
|
||||
this.prevHash = prev;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public void fillBlock(ContractRequest[] requests) {
|
||||
this.requests = requests;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
this.body = merkle(requests);
|
||||
this.hash = computeHash();
|
||||
doSignature(CMActions.manager.nodeCenterConn.getNodeKeyPair());
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return computeHash().equals(hash) && body.equals(merkle(this.requests)) && verifySignature();
|
||||
}
|
||||
|
||||
private String computeHash() {
|
||||
return HashUtil.sha3(
|
||||
String.valueOf(this.height),
|
||||
this.prevHash,
|
||||
this.checkPoint,
|
||||
this.body);
|
||||
}
|
||||
|
||||
private String merkle(ContractRequest[] requests) {
|
||||
// manage requests as a merkle tree
|
||||
if (requests.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (requests.length == 1) {
|
||||
return HashUtil.sha3(requests[0].getRequestID());
|
||||
}
|
||||
Queue<String> reqQueue =
|
||||
Arrays.stream(requests).map(ContractRequest::getRequestID)
|
||||
.collect(Collectors.toCollection(ArrayDeque::new));
|
||||
do {
|
||||
int size;
|
||||
for (size = reqQueue.size(); size > 1; size -= 2) {
|
||||
reqQueue.add(HashUtil.sha3(reqQueue.poll(), reqQueue.poll()));
|
||||
}
|
||||
if (size == 1) {
|
||||
reqQueue.add(reqQueue.poll());
|
||||
}
|
||||
} while (1 != reqQueue.size());
|
||||
return reqQueue.poll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPublicKey() {
|
||||
return nodePubKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPublicKey(String pubkey) {
|
||||
this.nodePubKey = pubkey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentStr() {
|
||||
return this.hash;
|
||||
}
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
|
||||
public FileActions fileAction;
|
||||
CMActions cmAction;
|
||||
|
||||
//TemporyTestAction temAction;
|
||||
// TemporyTestAction temAction;
|
||||
|
||||
StringBuilder dataCache = new StringBuilder();
|
||||
// TODO optimize ae-> too much method?
|
||||
@ -45,60 +45,41 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
|
||||
public void initActionExecutor() {
|
||||
CongestionControl.wsCounter.getAndIncrement();
|
||||
cmAction = new CMActions(this);
|
||||
//temAction = new TemporyTestAction();
|
||||
// temAction = new TemporyTestAction();
|
||||
fileAction = new FileActions(this);
|
||||
userManagerAction = new UserManagerAction(this);
|
||||
|
||||
ae =
|
||||
new ActionExecutor<ResultCallback, JsonObject>(
|
||||
executorService,
|
||||
cmAction,
|
||||
//temAction,
|
||||
fileAction,
|
||||
ManagerActions.instance,
|
||||
userManagerAction,
|
||||
new MasterWSAction(userManagerAction), // 多节点执行
|
||||
new CMLogAction(),
|
||||
new ProcessAction(),
|
||||
GRPCPool.instance,
|
||||
new EventWSActions()) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, JsonObject arg, long permission) {
|
||||
long val = a.userPermission();
|
||||
ae = new ActionExecutor<ResultCallback, JsonObject>(executorService, cmAction,
|
||||
// temAction,
|
||||
fileAction, ManagerActions.instance, userManagerAction, new MasterWSAction(this), // 多节点执行
|
||||
new CMLogAction(), new ProcessAction(), GRPCPool.instance, new EventWSActions()) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, JsonObject arg, long permission) {
|
||||
long val = a.userPermission();
|
||||
|
||||
boolean flag;
|
||||
String status = "refuse";
|
||||
String action = arg.get("action").getAsString();
|
||||
if (val == 0) {
|
||||
flag = true;
|
||||
status = "accept";
|
||||
} else if ((permission & val) == val) {
|
||||
flag = true;
|
||||
status = "accept";
|
||||
} else {
|
||||
flag = false;
|
||||
}
|
||||
LOGGER.debug(
|
||||
String.format(
|
||||
"%s val:%d permission:%d status:%s",
|
||||
action,
|
||||
val,
|
||||
permission,
|
||||
status));
|
||||
boolean flag;
|
||||
String status = "refuse";
|
||||
String action = arg.get("action").getAsString();
|
||||
if (val == 0) {
|
||||
flag = true;
|
||||
status = "accept";
|
||||
} else if ((permission & val) == val) {
|
||||
flag = true;
|
||||
status = "accept";
|
||||
} else {
|
||||
flag = false;
|
||||
}
|
||||
LOGGER.debug(String.format("%s val:%d permission:%d status:%s", action, val,
|
||||
permission, status));
|
||||
|
||||
CMHttpServer.nodeLogDB.put(
|
||||
action,
|
||||
String.format(
|
||||
"{\"action\":\"%s\",\"pubKey\":\"%s\",\"status\":\"%s\",\"date\":%d}",
|
||||
action,
|
||||
userManagerAction.getPubKey(),
|
||||
status,
|
||||
System.currentTimeMillis()));
|
||||
// TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(),
|
||||
// sb.toString());
|
||||
return flag;
|
||||
}
|
||||
};
|
||||
CMHttpServer.nodeLogDB.put(action, String.format(
|
||||
"{\"action\":\"%s\",\"pubKey\":\"%s\",\"status\":\"%s\",\"date\":%d}",
|
||||
action, userManagerAction.getPubKey(), status, System.currentTimeMillis()));
|
||||
// TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(),
|
||||
// sb.toString());
|
||||
return flag;
|
||||
}
|
||||
};
|
||||
for (String str : CMHttpHandler.wsPluginActions) {
|
||||
Object obj = createInstanceByClzName(str);
|
||||
ae.appendHandler(obj);
|
||||
@ -156,8 +137,7 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
|
||||
dataCache.append(map.get("data").getAsString());
|
||||
response = new Response();
|
||||
response.action = "sendNextSegment";
|
||||
ctx.channel()
|
||||
.writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response)));
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response)));
|
||||
return;
|
||||
} else {
|
||||
if (dataCache.length() > 0) {
|
||||
@ -170,41 +150,39 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
|
||||
}
|
||||
String action = map.get("action").getAsString();
|
||||
map.remove("verifiedPubKey");
|
||||
if (ae == null) initActionExecutor();
|
||||
if (ae == null)
|
||||
initActionExecutor();
|
||||
|
||||
if (userManagerAction.getPubKey() != null
|
||||
&& userManagerAction.getPubKey().length() > 0) {
|
||||
map.addProperty("verifiedPubKey", userManagerAction.getPubKey());
|
||||
}
|
||||
final JsonObject jmap = map;
|
||||
ae.handle(
|
||||
action,
|
||||
map,
|
||||
new ResultCallback(ctx.channel()) {
|
||||
@Override
|
||||
public void onResult(Map jo) {
|
||||
if (jmap.has("requestID")) {
|
||||
jo.put("responseID", jmap.get("requestID").getAsString());
|
||||
}
|
||||
onResult(JsonUtil.toJson(jo));
|
||||
}
|
||||
ae.handle(action, map, new ResultCallback(ctx.channel()) {
|
||||
@Override
|
||||
public void onResult(Map jo) {
|
||||
if (jmap.has("requestID")) {
|
||||
jo.put("responseID", jmap.get("requestID").getAsString());
|
||||
}
|
||||
onResult(JsonUtil.toJson(jo));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResult(JsonObject jo) {
|
||||
if (jmap.has("requestID")) {
|
||||
jo.add("responseID", jmap.get("requestID"));
|
||||
}
|
||||
onResult(jo.toString());
|
||||
}
|
||||
@Override
|
||||
public void onResult(JsonObject jo) {
|
||||
if (jmap.has("requestID")) {
|
||||
jo.add("responseID", jmap.get("requestID"));
|
||||
}
|
||||
onResult(jo.toString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onResult(String ret) {
|
||||
if (ret != null) {
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(ret));
|
||||
}
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onResult(String ret) {
|
||||
if (ret != null) {
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(ret));
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IllegalArgumentException e) {
|
||||
response = new Response();
|
||||
response.action = "onException";
|
||||
@ -239,7 +217,8 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
|
||||
}
|
||||
ret.append(s);
|
||||
ret.append("\n");
|
||||
if (count++ > 5) break;
|
||||
if (count++ > 5)
|
||||
break;
|
||||
}
|
||||
response.data = ret.toString();
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response)));
|
||||
|
@ -16,26 +16,30 @@ import org.apache.logging.log4j.Logger;
|
||||
/**
|
||||
* An HTTP server which serves Web Socket requests at:
|
||||
*
|
||||
* <p>http://localhost:8080/websocket
|
||||
* <p>
|
||||
* http://localhost:8080/websocket
|
||||
*
|
||||
* <p>Open your browser at <a href="http://localhost:8080/">http://localhost:8080/</a>, then the
|
||||
* demo page will be loaded and a Web Socket connection will be made automatically.
|
||||
* <p>
|
||||
* Open your browser at <a href="http://localhost:8080/">http://localhost:8080/</a>, then the demo
|
||||
* page will be loaded and a Web Socket connection will be made automatically.
|
||||
*
|
||||
* <p>This server illustrates support for the different web socket specification versions and will
|
||||
* work with:
|
||||
* <p>
|
||||
* This server illustrates support for the different web socket specification versions and will work
|
||||
* with:
|
||||
*
|
||||
* <ul>
|
||||
* <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
|
||||
* <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
|
||||
* <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
|
||||
* <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
|
||||
* <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
|
||||
* <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
|
||||
* <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
|
||||
* <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
|
||||
* <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
|
||||
* <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
|
||||
* <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
|
||||
* <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
|
||||
* </ul>
|
||||
*/
|
||||
public final class WebSocketServer {
|
||||
private static final boolean SSL = System.getProperty("ssl") != null;
|
||||
private static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8443" : "8080"));
|
||||
private static final int PORT =
|
||||
Integer.parseInt(System.getProperty("port", SSL ? "8443" : "8080"));
|
||||
private static final Logger LOGGER = LogManager.getLogger(WebSocketServer.class);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
@ -52,19 +56,14 @@ public final class WebSocketServer {
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
try {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||
.handler(new LoggingHandler(LogLevel.INFO))
|
||||
.childHandler(new WebSocketServerInitializer(sslCtx));
|
||||
|
||||
Channel ch = b.bind(PORT).sync().channel();
|
||||
|
||||
LOGGER.info(
|
||||
"Open your web browser and navigate to "
|
||||
+ (SSL ? "https" : "http")
|
||||
+ "://127.0.0.1:"
|
||||
+ PORT
|
||||
+ '/');
|
||||
LOGGER.info("Open your web browser and navigate to " + (SSL ? "https" : "http")
|
||||
+ "://127.0.0.1:" + PORT + '/');
|
||||
|
||||
ch.closeFuture().sync();
|
||||
} finally {
|
||||
|
@ -13,25 +13,25 @@ import io.netty.handler.ssl.SslContext;
|
||||
*/
|
||||
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
private static final String WEBSOCKET_PATH = "/websocket";
|
||||
private static final String WEBSOCKET_PATH = "/websocket";
|
||||
|
||||
private final SslContext sslCtx;
|
||||
private final SslContext sslCtx;
|
||||
|
||||
public WebSocketServerInitializer(SslContext sslCtx) {
|
||||
this.sslCtx = sslCtx;
|
||||
}
|
||||
public WebSocketServerInitializer(SslContext sslCtx) {
|
||||
this.sslCtx = sslCtx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
if (sslCtx != null) {
|
||||
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
|
||||
}
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
pipeline.addLast(new HttpObjectAggregator(65536));
|
||||
pipeline.addLast(new WebSocketServerCompressionHandler());
|
||||
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
|
||||
pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
|
||||
pipeline.addLast(new ContractManagerFrameHandler());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
if (sslCtx != null) {
|
||||
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
|
||||
}
|
||||
pipeline.addLast(new HttpServerCodec());
|
||||
pipeline.addLast(new HttpObjectAggregator(65536));
|
||||
pipeline.addLast(new WebSocketServerCompressionHandler());
|
||||
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
|
||||
pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
|
||||
pipeline.addLast(new ContractManagerFrameHandler());
|
||||
}
|
||||
}
|
||||
|
@ -41,10 +41,11 @@ import java.util.concurrent.TimeUnit;
|
||||
* @author OliveDS (Shuang Deng)
|
||||
*/
|
||||
public class NetworkManager {
|
||||
//Manage server->client connection;
|
||||
// Manage server->client connection;
|
||||
public static final Map<String, AgentConnector> CONNECTORS = new ConcurrentHashMap<>();
|
||||
//Manage client->server connection;
|
||||
public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS = new ConcurrentHashMap<>();
|
||||
// Manage client->server connection;
|
||||
public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS =
|
||||
new ConcurrentHashMap<>();
|
||||
public static final String NODE_CENTER_CLIENT = "NODE_CENTER_CLIENT";
|
||||
public static final String P2P_GRPC_CLIENT = "P2P_GRPC_CLIENT";
|
||||
private static final Map<String, String> slaverRouter = new HashMap<>();
|
||||
@ -59,18 +60,13 @@ public class NetworkManager {
|
||||
}
|
||||
|
||||
public static void reconnectAgent(String master) {
|
||||
LOGGER.debug(
|
||||
String.format("master=%s\t%s",
|
||||
master,
|
||||
JsonUtil.toJson(slaverRouter)));
|
||||
LOGGER.debug(String.format("master=%s\t%s", master, JsonUtil.toJson(slaverRouter)));
|
||||
try {
|
||||
NetworkManager.AgentConnector conn;
|
||||
synchronized (conn = NetworkManager.CONNECTORS.get(master)) {
|
||||
if (!conn.handler.isOpen()) {
|
||||
String[] ipAndPort = slaverRouter.get(master).split(":");
|
||||
conn.bootstrap
|
||||
.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1]))
|
||||
.sync()
|
||||
conn.bootstrap.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])).sync()
|
||||
.channel();
|
||||
}
|
||||
}
|
||||
@ -94,46 +90,36 @@ public class NetworkManager {
|
||||
nodeCenterClientHandler = ControllerManager.createNodeCenterClientHandler();
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
b.group(group);
|
||||
b.channel(NioSocketChannel.class)
|
||||
.handler(
|
||||
new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler);
|
||||
}
|
||||
});
|
||||
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(
|
||||
() -> {
|
||||
try {
|
||||
// manager.clearCache();
|
||||
String URL = GlobalConf.getNodeCenterUrl();
|
||||
// LOGGER.debug("GlobalConf.getNodeCenterUrl() -> URL=" + URL);
|
||||
URI uri = null;
|
||||
try {
|
||||
uri = new URI(URL);
|
||||
} catch (URISyntaxException e) {
|
||||
LOGGER.error("creating uri failed! " + e.getMessage());
|
||||
}
|
||||
if (!nodeCenterClientHandler.isConnected()
|
||||
|| !ControllerManager.getNodeCenterController().syncPing()) {
|
||||
nodeCenterClientHandler.close();
|
||||
assert null != uri;
|
||||
b.connect(uri.getHost(), uri.getPort()).sync().channel();
|
||||
LOGGER.info(
|
||||
"connect to node center: "
|
||||
+ uri.getHost()
|
||||
+ ":"
|
||||
+ uri.getPort());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
LOGGER.warn("connecting to node center failed! " + e.getMessage());
|
||||
}
|
||||
},
|
||||
0,
|
||||
10,
|
||||
TimeUnit.SECONDS);
|
||||
b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler);
|
||||
}
|
||||
});
|
||||
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(() -> {
|
||||
try {
|
||||
// manager.clearCache();
|
||||
String URL = GlobalConf.getNodeCenterUrl();
|
||||
// LOGGER.debug("GlobalConf.getNodeCenterUrl() -> URL=" + URL);
|
||||
URI uri = null;
|
||||
try {
|
||||
uri = new URI(URL);
|
||||
} catch (URISyntaxException e) {
|
||||
LOGGER.error("creating uri failed! " + e.getMessage());
|
||||
}
|
||||
if (!nodeCenterClientHandler.isConnected()
|
||||
|| !ControllerManager.getNodeCenterController().syncPing()) {
|
||||
nodeCenterClientHandler.close();
|
||||
assert null != uri;
|
||||
b.connect(uri.getHost(), uri.getPort()).sync().channel();
|
||||
LOGGER.info("connect to node center: " + uri.getHost() + ":" + uri.getPort());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// LOGGER.warn("connecting to node center failed! " + e.getMessage());
|
||||
}
|
||||
}, 4, 30 + (int) (20 * Math.random()), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
@ -142,20 +128,16 @@ public class NetworkManager {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
||||
b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
|
||||
b.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.option(ChannelOption.SO_BACKLOG, 100)
|
||||
.localAddress(port)
|
||||
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||
.option(ChannelOption.SO_BACKLOG, 100).localAddress(port)
|
||||
.childOption(ChannelOption.SO_KEEPALIVE, true)
|
||||
.childHandler(
|
||||
new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel arg0) {
|
||||
arg0.pipeline()
|
||||
.addLast(new DelimiterCodec())
|
||||
.addLast(new TCPServerFrameHandler());
|
||||
}
|
||||
});
|
||||
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel arg0) {
|
||||
arg0.pipeline().addLast(new DelimiterCodec())
|
||||
.addLast(new TCPServerFrameHandler());
|
||||
}
|
||||
});
|
||||
|
||||
b.bind(port).sync().channel();
|
||||
} catch (InterruptedException e) {
|
||||
@ -164,7 +146,7 @@ public class NetworkManager {
|
||||
}
|
||||
|
||||
|
||||
//TODO Remove in future
|
||||
// TODO Remove in future
|
||||
public void sendToNodeCenter(String msg) {
|
||||
nodeCenterClientHandler.sendMsg(msg);
|
||||
}
|
||||
@ -174,9 +156,8 @@ public class NetworkManager {
|
||||
}
|
||||
|
||||
public void waitForNodeCenterConnected() {
|
||||
for (int i = 0;
|
||||
i < 10 && null != nodeCenterClientHandler && !nodeCenterClientHandler.isConnected();
|
||||
i++) {
|
||||
for (int i = 0; i < 10 && null != nodeCenterClientHandler
|
||||
&& !nodeCenterClientHandler.isConnected(); i++) {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (InterruptedException e) {
|
||||
@ -191,7 +172,7 @@ public class NetworkManager {
|
||||
}
|
||||
}
|
||||
|
||||
//----------AgentNetworkManagement
|
||||
// ----------AgentNetworkManagement
|
||||
|
||||
|
||||
public void updateAgentRouter(String nodeID, String address) {
|
||||
@ -217,7 +198,6 @@ public class NetworkManager {
|
||||
|
||||
public void connectToAgent(String master, String contractID) {
|
||||
LOGGER.info("master=" + master);
|
||||
// LOGGER.debug("master=" + master + "\n\trouter=" + slaverRouter.get(master));
|
||||
Bootstrap b;
|
||||
AgentConnector connector = null;
|
||||
synchronized (CONNECTORS) {
|
||||
@ -234,15 +214,13 @@ public class NetworkManager {
|
||||
connector.handler = handler;
|
||||
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
|
||||
b.group(CMHttpServer.workerGroup);
|
||||
b.channel(NioSocketChannel.class)
|
||||
.handler(
|
||||
new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(new DelimiterCodec()).addLast(handler);
|
||||
}
|
||||
});
|
||||
b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(new DelimiterCodec()).addLast(handler);
|
||||
}
|
||||
});
|
||||
}
|
||||
reconnectAgent(master);
|
||||
}
|
||||
@ -293,8 +271,8 @@ public class NetworkManager {
|
||||
return CONNECTORS.containsKey(pubKey);
|
||||
}
|
||||
|
||||
//-------UNUSED TOMerge------------
|
||||
//UNUSED
|
||||
// -------UNUSED TOMerge------------
|
||||
// UNUSED
|
||||
public TCPClientFrameHandler createTCPClient(String peer, String ipPort)
|
||||
throws InterruptedException {
|
||||
if (peer.equals(GlobalConf.instance.peerID)) {
|
||||
@ -307,30 +285,28 @@ public class NetworkManager {
|
||||
NioEventLoopGroup group = new NioEventLoopGroup();
|
||||
Bootstrap b = new Bootstrap();
|
||||
final TCPClientFrameHandler handler = new TCPClientFrameHandler(peer);
|
||||
//tcpClientMap.put(peer, handler);
|
||||
b.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
// tcpClientMap.put(peer, handler);
|
||||
b.group(group).channel(NioSocketChannel.class)
|
||||
.remoteAddress(new InetSocketAddress(host, port))
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
.handler(
|
||||
new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline()
|
||||
// .addLast(new
|
||||
// IdleStateHandler(0, 4, 0, TimeUnit.SECONDS))
|
||||
.addLast(new DelimiterCodec())
|
||||
.addLast(new IdleStateHandler(0, 5, 0))
|
||||
// .addLast(new
|
||||
// HeartBeatEncode())
|
||||
.addLast(handler);
|
||||
}
|
||||
});
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ch.pipeline()
|
||||
// .addLast(new
|
||||
// IdleStateHandler(0, 4, 0, TimeUnit.SECONDS))
|
||||
.addLast(new DelimiterCodec())
|
||||
.addLast(new IdleStateHandler(0, 5, 0))
|
||||
// .addLast(new
|
||||
// HeartBeatEncode())
|
||||
.addLast(handler);
|
||||
}
|
||||
});
|
||||
ChannelFuture f = b.connect().sync();
|
||||
if (f.isSuccess()) {
|
||||
LOGGER.info("TCP Client " + peer + " started");
|
||||
}
|
||||
// channel = (SocketChannel) future.channel();
|
||||
// channel = (SocketChannel) future.channel();
|
||||
return handler;
|
||||
}
|
||||
|
||||
@ -353,14 +329,14 @@ public class NetworkManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* UNUSED send to TCP nodes, if fail send by p2p
|
||||
* UNUSED send to TCP nodes, if fail send by p2p
|
||||
*
|
||||
* @param msg unit message
|
||||
*/
|
||||
public void send(UnitMessage msg) {
|
||||
Iterator<String> iterator = msg.getReceiverList().iterator();
|
||||
String peer;
|
||||
// boolean sendByP2P = false;
|
||||
// boolean sendByP2P = false;
|
||||
TCPClientFrameHandler tcpClientFrameHandler;
|
||||
while (iterator.hasNext()) {
|
||||
peer = iterator.next();
|
||||
@ -369,17 +345,16 @@ public class NetworkManager {
|
||||
LOGGER.info("send msg to itself " + msg);
|
||||
continue;
|
||||
}
|
||||
// tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer, null);
|
||||
// tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer,
|
||||
// null);
|
||||
if (peerID2TCPAddress.containsKey(peer)) {
|
||||
//recreateTCPClient(peer);
|
||||
// instance.tcpClientMap.put(peer, tcpClientFrameHandler);
|
||||
UnitMessage unitMessage =
|
||||
msg.toBuilder().clearReceiver().addReceiver(peer).build();
|
||||
// recreateTCPClient(peer);
|
||||
// instance.tcpClientMap.put(peer, tcpClientFrameHandler);
|
||||
UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
|
||||
LOGGER.info("send msg by p2p to " + peer);
|
||||
JavaContractServiceGrpcServer.sendMsg(unitMessage);
|
||||
} else {
|
||||
UnitMessage unitMessage =
|
||||
msg.toBuilder().clearReceiver().addReceiver(peer).build();
|
||||
UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
|
||||
LOGGER.info("send msg by p2p to " + peer);
|
||||
JavaContractServiceGrpcServer.sendMsg(unitMessage);
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.bdware.units.beans;
|
||||
|
||||
import org.bdware.sc.bean.ContractExecType;
|
||||
import org.bdware.units.enums.ConsensusType;
|
||||
import org.bdware.units.enums.NetworkType;
|
||||
import org.bdware.units.enums.ResponseType;
|
||||
@ -11,29 +10,33 @@ public class MultiPointContractInfo {
|
||||
public Set<String> members;
|
||||
public Map<String, String> member2PubKey;
|
||||
public String master;
|
||||
// public ContractType contractType;
|
||||
// public ContractType contractType;
|
||||
public String contractId;
|
||||
public ConsensusType consensusType;
|
||||
public ResponseType responseType;
|
||||
public NetworkType networkType;
|
||||
|
||||
public MultiPointContractInfo(){
|
||||
// member2PubKey = new HashMap<>(10);
|
||||
// members = new HashSet<>(10);
|
||||
public MultiPointContractInfo() {
|
||||
// member2PubKey = new HashMap<>(10);
|
||||
// members = new HashSet<>(10);
|
||||
}
|
||||
|
||||
public Set<String> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public String getContractId() {
|
||||
return contractId;
|
||||
}
|
||||
|
||||
public String getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public boolean hasPeer(String peerID) {
|
||||
return members.contains(peerID);
|
||||
}
|
||||
|
||||
public String getPubKey(String peerID) {
|
||||
return member2PubKey.get(peerID);
|
||||
|
||||
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* ExecutionManager内部消息,封装在UnitMessage中作为content
|
||||
*
|
||||
* @author oliveds
|
||||
*/
|
||||
public class UnitContractMessage implements Serializable {
|
||||
|
@ -12,4 +12,4 @@ public interface ConsensusAlgorithm {
|
||||
void setCommitter(Committer c);
|
||||
|
||||
void setConnection(TrustfulExecutorConnection c);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ public interface ConsensusCenter {
|
||||
|
||||
void handle(UnitSequencingMessage unitSequencingMessage);
|
||||
|
||||
// void updateMaster(String master);
|
||||
// void updateMaster(String master);
|
||||
|
||||
// UnitSequencingMessage formConsensusRequest(UnitContractMessage msg);
|
||||
// UnitSequencingMessage formConsensusRequest(UnitContractMessage msg);
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
MultiPointContractInfo contractInfo;
|
||||
Map<Long, PBFTInfo> infos;
|
||||
Map<Long, UnitContractMessage> committedMsg;
|
||||
// Map<String, PBFTInfo> original;
|
||||
// String master;
|
||||
// Map<String, PBFTInfo> original;
|
||||
// String master;
|
||||
boolean isMaster;
|
||||
private String contractID;
|
||||
private NetworkType networkType;
|
||||
@ -36,12 +36,12 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
networkType = NetworkType.getType(msg.network);
|
||||
infos = new HashMap<>();
|
||||
committedMsg = new HashMap<>();
|
||||
// original = new HashMap<>();
|
||||
// original = new HashMap<>();
|
||||
// receive init, then insert
|
||||
// members = ContractUnitManager.instance.getContractInfo(contractID);
|
||||
// if (members.master.equals(GlobalConf.instance.peerID)) {
|
||||
// isMaster = true;
|
||||
// }
|
||||
// members = ContractUnitManager.instance.getContractInfo(contractID);
|
||||
// if (members.master.equals(GlobalConf.instance.peerID)) {
|
||||
// isMaster = true;
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -59,32 +59,33 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
PBFTInfo temp;
|
||||
switch (ConsensusStage.fromByte(unitSequencingMessage.stageType)) {
|
||||
case Init:
|
||||
MultiPointContractInfo contractInfo =
|
||||
JsonUtil.fromJson(unitSequencingMessage.content, MultiPointContractInfo.class);
|
||||
MultiPointContractInfo contractInfo = JsonUtil
|
||||
.fromJson(unitSequencingMessage.content, MultiPointContractInfo.class);
|
||||
updateUnitInfo(contractInfo);
|
||||
break;
|
||||
case AddMember:
|
||||
// PBFTMember member = PBFTMember.parse(pbftMessage.content);
|
||||
// if (member != null)
|
||||
// members.put(sender, member);
|
||||
// case DeleteMember:
|
||||
// PBFTMember member = PBFTMember.parse(pbftMessage.content);
|
||||
// if (member != null)
|
||||
// members.put(sender, member);
|
||||
// case DeleteMember:
|
||||
break;
|
||||
case Request:
|
||||
// master form preprepare, full content
|
||||
// int hash = unitSequencingMessage.content.hashCode();
|
||||
// original.put(hash, new Pair<Node, PBFTMessage>(sender, pbftMessage));
|
||||
// int hash = unitSequencingMessage.content.hashCode();
|
||||
// original.put(hash, new Pair<Node, PBFTMessage>(sender, pbftMessage));
|
||||
if (isMaster) {
|
||||
temp = new PBFTInfo();
|
||||
temp.order = allocatedID.incrementAndGet();
|
||||
temp.request = unitSequencingMessage.content;
|
||||
infos.put(temp.order, temp);
|
||||
// original.put(unitSequencingMessage.requestID, temp);
|
||||
// original.put(unitSequencingMessage.requestID, temp);
|
||||
unitSequencingMessage.stageType = PBFTType.PrePrepare.toInt();
|
||||
unitSequencingMessage.order = temp.order;
|
||||
// unitSequencingMessage.content = unitSequencingMessage.content.hashCode() + "";
|
||||
// unitSequencingMessage.content = unitSequencingMessage.content.hashCode() +
|
||||
// "";
|
||||
broadcast(unitSequencingMessage);
|
||||
} else {
|
||||
// sendToMaster(unitSequencingMessage);
|
||||
// sendToMaster(unitSequencingMessage);
|
||||
}
|
||||
break;
|
||||
case PrePrepare:
|
||||
@ -111,7 +112,7 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
}
|
||||
if (temp.updatePrepare(unitSequencingMessage, this)) {
|
||||
UnitSequencingMessage commitMsg = temp.formCommit(unitSequencingMessage);
|
||||
// temp.updateCommit(commitMsg, this);
|
||||
// temp.updateCommit(commitMsg, this);
|
||||
broadcast(commitMsg);
|
||||
}
|
||||
break;
|
||||
@ -138,7 +139,7 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
unitSequencingMessage.stageType = ConsensusStage.PrePrepare.toInt();
|
||||
unitSequencingMessage.sender = GlobalConf.instance.peerID;
|
||||
String content = JsonUtil.toJson(unitSequencingMessage);
|
||||
SequencingManager.instance.send(content, new String[]{peer});
|
||||
SequencingManager.instance.send(content, new String[] {peer});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -152,7 +153,8 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
}
|
||||
|
||||
private void broadcast(UnitSequencingMessage msg) {
|
||||
SequencingManager.instance.send(JsonUtil.toJson(msg), contractInfo.getMembers().toArray(new String[0]));
|
||||
SequencingManager.instance.send(JsonUtil.toJson(msg),
|
||||
contractInfo.getMembers().toArray(new String[0]));
|
||||
}
|
||||
|
||||
class PBFTInfo {
|
||||
@ -175,7 +177,8 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
isPrePrepareReceived = false;
|
||||
}
|
||||
|
||||
public synchronized boolean updatePrepare(UnitSequencingMessage message, PBFTCenter center) {
|
||||
public synchronized boolean updatePrepare(UnitSequencingMessage message,
|
||||
PBFTCenter center) {
|
||||
if (isSendCommit) {
|
||||
return false;
|
||||
}
|
||||
@ -209,18 +212,15 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
}
|
||||
|
||||
public String getDisplayStr() {
|
||||
return String.format(
|
||||
"pSize=%d cSize=%d isSendCommit=%b isSendReply=%b buffSize=%d",
|
||||
null == prepare ? -1 : prepare.size(),
|
||||
null == commit ? -1 : commit.size(),
|
||||
isSendCommit,
|
||||
isSendReply,
|
||||
buff.size());
|
||||
return String.format("pSize=%d cSize=%d isSendCommit=%b isSendReply=%b buffSize=%d",
|
||||
null == prepare ? -1 : prepare.size(), null == commit ? -1 : commit.size(),
|
||||
isSendCommit, isSendReply, buff.size());
|
||||
}
|
||||
|
||||
public UnitSequencingMessage formPrepare(UnitSequencingMessage unitSequencingMessage) {
|
||||
// UnitSequencingMessage prepareMsg = new UnitSequencingMessage(ConsensusStage.Prepare.toInt());
|
||||
// prepareMsg.order = this.order;
|
||||
// UnitSequencingMessage prepareMsg = new
|
||||
// UnitSequencingMessage(ConsensusStage.Prepare.toInt());
|
||||
// prepareMsg.order = this.order;
|
||||
unitSequencingMessage.content = String.valueOf(this.request.hashCode());
|
||||
unitSequencingMessage.sender = GlobalConf.instance.peerID;
|
||||
unitSequencingMessage.stageType = ConsensusStage.Prepare.toInt();
|
||||
@ -228,17 +228,19 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
}
|
||||
|
||||
public void requestPrePrepareFromMaster() {
|
||||
UnitSequencingMessage requestMsg = new UnitSequencingMessage(ConsensusStage.ReSend.toInt());
|
||||
UnitSequencingMessage requestMsg =
|
||||
new UnitSequencingMessage(ConsensusStage.ReSend.toInt());
|
||||
requestMsg.order = this.order;
|
||||
requestMsg.sender = GlobalConf.instance.peerID;
|
||||
// return requestMsg;
|
||||
// return requestMsg;
|
||||
String content = JsonUtil.toJson(requestMsg);
|
||||
SequencingManager.instance.send(content, new String[]{contractInfo.master});
|
||||
SequencingManager.instance.send(content, new String[] {contractInfo.master});
|
||||
}
|
||||
|
||||
public UnitSequencingMessage formCommit(UnitSequencingMessage unitSequencingMessage) {
|
||||
// UnitSequencingMessage commitMsg = new UnitSequencingMessage(ConsensusStage.Commit.toInt());
|
||||
// unitSequencingMessage.order = this.order;
|
||||
// UnitSequencingMessage commitMsg = new
|
||||
// UnitSequencingMessage(ConsensusStage.Commit.toInt());
|
||||
// unitSequencingMessage.order = this.order;
|
||||
unitSequencingMessage.content = String.valueOf(this.request.hashCode());
|
||||
unitSequencingMessage.sender = GlobalConf.instance.peerID;
|
||||
unitSequencingMessage.stageType = ConsensusStage.Commit.toInt();
|
||||
@ -246,7 +248,8 @@ public class PBFTCenter implements ConsensusCenter {
|
||||
}
|
||||
|
||||
public void onCommit() {
|
||||
UnitContractMessage unitContractMessage = JsonUtil.fromJson(request, UnitContractMessage.class);
|
||||
UnitContractMessage unitContractMessage =
|
||||
JsonUtil.fromJson(request, UnitContractMessage.class);
|
||||
if (this.order == (committedID.get() + 1L)) {
|
||||
execute(unitContractMessage, this.order);
|
||||
} else {
|
||||
|
@ -10,8 +10,7 @@ public class RAFTCenter implements ConsensusCenter {
|
||||
private static ConsensusType PBFT;
|
||||
private String contractID;
|
||||
|
||||
public RAFTCenter(UnitContractMessage msg) {
|
||||
}
|
||||
public RAFTCenter(UnitContractMessage msg) {}
|
||||
|
||||
@Override
|
||||
public void updateUnitInfo(MultiPointContractInfo contractInfo) {
|
||||
|
@ -35,12 +35,12 @@ public class SingleCenter implements ConsensusCenter {
|
||||
networkType = NetworkType.getType(msg.network);
|
||||
infos = new HashMap<>();
|
||||
committedMsg = new HashMap<>();
|
||||
// original = new HashMap<>();
|
||||
// original = new HashMap<>();
|
||||
// receive init, then insert
|
||||
// members = ContractUnitManager.instance.getContractInfo(contractID);
|
||||
// if (members.master.equals(GlobalConf.instance.peerID)) {
|
||||
// isMaster = true;
|
||||
// }
|
||||
// members = ContractUnitManager.instance.getContractInfo(contractID);
|
||||
// if (members.master.equals(GlobalConf.instance.peerID)) {
|
||||
// isMaster = true;
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -58,8 +58,8 @@ public class SingleCenter implements ConsensusCenter {
|
||||
SingleInfo temp;
|
||||
switch (ConsensusStage.fromByte(unitSequencingMessage.stageType)) {
|
||||
case Init:
|
||||
MultiPointContractInfo contractInfo =
|
||||
JsonUtil.fromJson(unitSequencingMessage.content, MultiPointContractInfo.class);
|
||||
MultiPointContractInfo contractInfo = JsonUtil
|
||||
.fromJson(unitSequencingMessage.content, MultiPointContractInfo.class);
|
||||
updateUnitInfo(contractInfo);
|
||||
break;
|
||||
case Request:
|
||||
@ -72,7 +72,7 @@ public class SingleCenter implements ConsensusCenter {
|
||||
unitSequencingMessage.order = temp.order;
|
||||
broadcast(unitSequencingMessage);
|
||||
} else {
|
||||
// sendToMaster(unitSequencingMessage);
|
||||
// sendToMaster(unitSequencingMessage);
|
||||
}
|
||||
break;
|
||||
case Commit:
|
||||
@ -93,7 +93,7 @@ public class SingleCenter implements ConsensusCenter {
|
||||
unitSequencingMessage.stageType = ConsensusStage.PrePrepare.toInt();
|
||||
unitSequencingMessage.sender = GlobalConf.instance.peerID;
|
||||
String content = JsonUtil.toJson(unitSequencingMessage);
|
||||
SequencingManager.instance.send(content, new String[]{peer});
|
||||
SequencingManager.instance.send(content, new String[] {peer});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -102,8 +102,7 @@ public class SingleCenter implements ConsensusCenter {
|
||||
}
|
||||
|
||||
private void broadcast(UnitSequencingMessage msg) {
|
||||
SequencingManager.instance.send(
|
||||
JsonUtil.toJson(msg),
|
||||
SequencingManager.instance.send(JsonUtil.toJson(msg),
|
||||
contractInfo.getMembers().toArray(new String[contractInfo.members.size()]));
|
||||
}
|
||||
|
||||
@ -116,7 +115,8 @@ public class SingleCenter implements ConsensusCenter {
|
||||
isSendReply = false;
|
||||
}
|
||||
|
||||
public synchronized boolean updateCommit(UnitSequencingMessage message, SingleCenter center) {
|
||||
public synchronized boolean updateCommit(UnitSequencingMessage message,
|
||||
SingleCenter center) {
|
||||
if (isSendReply) {
|
||||
return false;
|
||||
}
|
||||
@ -126,7 +126,8 @@ public class SingleCenter implements ConsensusCenter {
|
||||
}
|
||||
|
||||
public void onCommit() {
|
||||
UnitContractMessage unitContractMessage = JsonUtil.fromJson(request, UnitContractMessage.class);
|
||||
UnitContractMessage unitContractMessage =
|
||||
JsonUtil.fromJson(request, UnitContractMessage.class);
|
||||
if (this.order == (committedID.get() + 1L)) {
|
||||
execute(unitContractMessage, this.order);
|
||||
} else {
|
||||
|
@ -1,7 +1,8 @@
|
||||
package org.bdware.units.enums;
|
||||
|
||||
public enum ConsensusStage {
|
||||
Init(0), Request(1), PrePrepare(2), Prepare(3), Commit(4), Reply(5), Unknown(6), ReSend(7), AddMember(8);
|
||||
Init(0), Request(1), PrePrepare(2), Prepare(3), Commit(4), Reply(5), Unknown(6), ReSend(
|
||||
7), AddMember(8);
|
||||
|
||||
private int type;
|
||||
|
||||
|
@ -1,12 +1,8 @@
|
||||
package org.bdware.units.enums;
|
||||
|
||||
public enum UnitContractMessageType {
|
||||
ContractUnitRequest(0),
|
||||
ContractUnitResponse (1),
|
||||
GetMyPeerID(2),
|
||||
MyPeerIDResponse(3),
|
||||
ContractStatusRequest(4),
|
||||
ContractStatusResponse (5);
|
||||
ContractUnitRequest(0), ContractUnitResponse(1), GetMyPeerID(2), MyPeerIDResponse(
|
||||
3), ContractStatusRequest(4), ContractStatusResponse(5);
|
||||
|
||||
private final int value;
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
package org.bdware.units.enums;
|
||||
|
||||
public enum UnitContractNetworkType {
|
||||
TCP(0),
|
||||
P2P (1);
|
||||
TCP(0), P2P(1);
|
||||
|
||||
private final int value;
|
||||
|
||||
|
@ -1,11 +1,7 @@
|
||||
package org.bdware.units.enums;
|
||||
|
||||
public enum UnitContractRequestType {
|
||||
START(0),
|
||||
STOP (1),
|
||||
EXECUTE(2),
|
||||
REPLY(3),
|
||||
REQUEST(4);
|
||||
START(0), STOP(1), EXECUTE(2), REPLY(3), REQUEST(4);
|
||||
|
||||
private final int value;
|
||||
|
||||
|
@ -26,7 +26,8 @@ public class BaseFunctionManager {
|
||||
|
||||
public void send(String msg, String[] peers) {
|
||||
String requestId = getRequestId();
|
||||
BDLedgerContract.UnitMessage unitMessage = this.addRequestIdAndGetMessage(requestId, msg, peers);
|
||||
BDLedgerContract.UnitMessage unitMessage =
|
||||
this.addRequestIdAndGetMessage(requestId, msg, peers);
|
||||
NetworkManager.instance.send(unitMessage);
|
||||
}
|
||||
|
||||
@ -39,7 +40,8 @@ public class BaseFunctionManager {
|
||||
|
||||
public void send(String msg, String[] peers, ResultCallback resultCallback) {
|
||||
String requestId = getRequestId();
|
||||
BDLedgerContract.UnitMessage unitMessage = this.addRequestIdAndGetMessage(requestId, msg, peers);
|
||||
BDLedgerContract.UnitMessage unitMessage =
|
||||
this.addRequestIdAndGetMessage(requestId, msg, peers);
|
||||
NetworkManager.instance.send(unitMessage);
|
||||
requestCallbacks.put(requestId, resultCallback);
|
||||
}
|
||||
@ -58,10 +60,8 @@ public class BaseFunctionManager {
|
||||
System.out.println("[BaseFunctionManager] handleResponse null");
|
||||
return;
|
||||
}
|
||||
if (responseCenter.updateReply(
|
||||
arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(),
|
||||
arg.get("pubkey").getAsString())) {
|
||||
if (responseCenter.updateReply(arg.get("content").getAsString(),
|
||||
arg.get("sender").getAsString(), arg.get("pubkey").getAsString())) {
|
||||
responseCenter.formReply();
|
||||
}
|
||||
}
|
||||
@ -74,12 +74,12 @@ public class BaseFunctionManager {
|
||||
return UNIT_MESSAGE_TYPE;
|
||||
}
|
||||
|
||||
private BDLedgerContract.UnitMessage addRequestIdAndGetMessage(String requestId, String msg, String[] peers) {
|
||||
private BDLedgerContract.UnitMessage addRequestIdAndGetMessage(String requestId, String msg,
|
||||
String[] peers) {
|
||||
JsonObject jsonObject = (JsonObject) JsonParser.parseString(msg);
|
||||
jsonObject.addProperty(REQUEST_ID, requestId);
|
||||
BDLedgerContract.UnitMessage.Builder builder = BDLedgerContract.UnitMessage.newBuilder()
|
||||
.setMsgType(getMessageType())
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.setMsgType(getMessageType()).setSender(GlobalConf.instance.peerID)
|
||||
.setContent(ByteString.copyFromUtf8(jsonObject.toString()));
|
||||
for (String peer : peers) {
|
||||
builder.addReceiver(peer);
|
||||
@ -87,16 +87,12 @@ public class BaseFunctionManager {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private BDLedgerContract.UnitMessage addRequestIdAndGetMessage(
|
||||
String requestId,
|
||||
String msg,
|
||||
String[] peers,
|
||||
BDLedgerContract.UnitMessageType unitMessageType) {
|
||||
private BDLedgerContract.UnitMessage addRequestIdAndGetMessage(String requestId, String msg,
|
||||
String[] peers, BDLedgerContract.UnitMessageType unitMessageType) {
|
||||
JsonObject jsonObject = (JsonObject) JsonParser.parseString(msg);
|
||||
jsonObject.addProperty(REQUEST_ID, requestId);
|
||||
BDLedgerContract.UnitMessage.Builder builder = BDLedgerContract.UnitMessage.newBuilder()
|
||||
.setMsgType(unitMessageType)
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.setMsgType(unitMessageType).setSender(GlobalConf.instance.peerID)
|
||||
.setContent(ByteString.copyFromUtf8(jsonObject.toString()));
|
||||
for (String peer : peers) {
|
||||
builder.addReceiver(peer);
|
||||
@ -109,4 +105,4 @@ public class BaseFunctionManager {
|
||||
String action;
|
||||
Object data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,9 +34,10 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
private static final String MANAGER_SYMBOL = "COMM";
|
||||
private static final Logger LOGGER = LogManager.getLogger(CommunicationManager.class);
|
||||
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE = BDLedgerContract.UnitMessageType.UnitCommunicateMessage;
|
||||
private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE =
|
||||
BDLedgerContract.UnitMessageType.UnitCommunicateMessage;
|
||||
protected CommunicationAction communicationAction;
|
||||
//new ThreadPoolExecutor(10,30,1L, TimeUnit.HOURS, new LinkedBlockingDeque<Runnable>(100));
|
||||
// new ThreadPoolExecutor(10,30,1L, TimeUnit.HOURS, new LinkedBlockingDeque<Runnable>(100));
|
||||
private ActionExecutor<ResultCallback, JsonObject> ae;
|
||||
private NodeCenterClientController nodeCenterClientController;
|
||||
private String[] p2pPeers;
|
||||
@ -49,11 +50,8 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
instance = new CommunicationManager();
|
||||
instance.nodeCenterClientController = ControllerManager.getNodeCenterController();
|
||||
instance.communicationAction = new CommunicationAction();
|
||||
instance.ae = new ActionExecutor<ResultCallback, JsonObject>(
|
||||
executorService,
|
||||
instance.nodeCenterClientController,
|
||||
instance.communicationAction
|
||||
) {
|
||||
instance.ae = new ActionExecutor<ResultCallback, JsonObject>(executorService,
|
||||
instance.nodeCenterClientController, instance.communicationAction) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, final JsonObject args, long permission) {
|
||||
return true;
|
||||
@ -67,11 +65,9 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
}
|
||||
|
||||
public static void send(String msg, String symbol) {
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder()
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder().setSender(GlobalConf.instance.peerID)
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msg))
|
||||
.build();
|
||||
.setContent(ByteString.copyFromUtf8(msg)).build();
|
||||
NetworkManager.instance.sendTo(unitMessage, symbol);
|
||||
}
|
||||
|
||||
@ -81,39 +77,38 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
}
|
||||
|
||||
public void handle(String str, String sender) {
|
||||
// JsonObject jsonObject = (JsonObject) JsonParser.parseStringAsJsonObject(str);
|
||||
// if(jsonObject.has("action")) {
|
||||
// String action = jsonObject.get("action").getAsString();
|
||||
// switch (action) {
|
||||
// case "myPeerId":
|
||||
// GlobalConf.setPeerID(jsonObject.get("peerId").getAsString());
|
||||
// break;
|
||||
// default:
|
||||
// logger.debug("[CommunicationManager] not handled " + jsonObject.toString());
|
||||
// }
|
||||
// }
|
||||
// JsonObject jsonObject = (JsonObject) JsonParser.parseStringAsJsonObject(str);
|
||||
// if(jsonObject.has("action")) {
|
||||
// String action = jsonObject.get("action").getAsString();
|
||||
// switch (action) {
|
||||
// case "myPeerId":
|
||||
// GlobalConf.setPeerID(jsonObject.get("peerId").getAsString());
|
||||
// break;
|
||||
// default:
|
||||
// logger.debug("[CommunicationManager] not handled " + jsonObject.toString());
|
||||
// }
|
||||
// }
|
||||
JsonObject arg = (JsonObject) JsonParser.parseString(str);
|
||||
Response response;
|
||||
try {
|
||||
final String action = arg.get("action").getAsString();
|
||||
// if (isResponse(action)) {
|
||||
// handleResponse(arg);
|
||||
// return;
|
||||
// }
|
||||
// logger.info("[CommunicationManager] handle:" + arg.toString());
|
||||
ae.handle(action, arg,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String arg) {
|
||||
System.out.println("[CommunicationManager] onResult");
|
||||
// CommunicationManager.instance.send(arg, new String[]{sender});
|
||||
}
|
||||
});
|
||||
// if (isResponse(action)) {
|
||||
// handleResponse(arg);
|
||||
// return;
|
||||
// }
|
||||
// logger.info("[CommunicationManager] handle:" + arg.toString());
|
||||
ae.handle(action, arg, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String arg) {
|
||||
System.out.println("[CommunicationManager] onResult");
|
||||
// CommunicationManager.instance.send(arg, new String[]{sender});
|
||||
}
|
||||
});
|
||||
} catch (IllegalArgumentException e) {
|
||||
response = new Response();
|
||||
response.action = "onException";
|
||||
response.data = e.getMessage();
|
||||
CommunicationManager.instance.send(JsonUtil.toJson(response), new String[]{sender});
|
||||
CommunicationManager.instance.send(JsonUtil.toJson(response), new String[] {sender});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
@ -124,12 +119,9 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
int count = 0;
|
||||
for (String s : strs) {
|
||||
if (s.contains("sun.reflect")
|
||||
|| s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache")
|
||||
|| s.contains("java.util")
|
||||
|| s.contains("java.lang")
|
||||
|| s.contains("io.netty")) {
|
||||
if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache") || s.contains("java.util")
|
||||
|| s.contains("java.lang") || s.contains("io.netty")) {
|
||||
continue;
|
||||
}
|
||||
ret.append(s);
|
||||
@ -149,11 +141,9 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty(ACTION, "getP2PNodes");
|
||||
jsonObject.addProperty(REQUEST_ID, requestID);
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder()
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder().setSender(GlobalConf.instance.peerID)
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage)
|
||||
.setContent(ByteString.copyFromUtf8(jsonObject.toString()))
|
||||
.build();
|
||||
.setContent(ByteString.copyFromUtf8(jsonObject.toString())).build();
|
||||
NetworkManager.instance.send(unitMessage, NetworkType.P2P);
|
||||
}
|
||||
|
||||
@ -186,11 +176,9 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
public void askForNextContractID() {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty(ACTION, "askForNextContractID");
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder()
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
UnitMessage unitMessage = UnitMessage.newBuilder().setSender(GlobalConf.instance.peerID)
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage)
|
||||
.setContent(ByteString.copyFromUtf8(jsonObject.toString()))
|
||||
.build();
|
||||
.setContent(ByteString.copyFromUtf8(jsonObject.toString())).build();
|
||||
NetworkManager.instance.send(unitMessage, NetworkType.P2P);
|
||||
}
|
||||
|
||||
@ -206,5 +194,4 @@ public class CommunicationManager extends BaseFunctionManager {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -26,15 +26,17 @@ public class ContractUnitManager extends BaseFunctionManager {
|
||||
private static final String MANAGER_SYMBOL = "UNIT";
|
||||
private static final Logger LOGGER = LogManager.getLogger(ExecutionManager.class);
|
||||
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
final private BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE = BDLedgerContract.UnitMessageType.UnitUnitMessage; // init after all static is inited
|
||||
final private BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE =
|
||||
BDLedgerContract.UnitMessageType.UnitUnitMessage; // init after all static is inited
|
||||
public static ContractUnitManager instance = init();
|
||||
private final Map<String, MultiPointContractInfo> contractID2Members = new ConcurrentHashMap<>();
|
||||
private final Map<String, MultiPointContractInfo> contractID2Members =
|
||||
new ConcurrentHashMap<>();
|
||||
protected ActionExecutor<ResultCallback, JsonObject> actionExecutor;
|
||||
protected UnitsInfoAction unitsInfoActions;
|
||||
private Map<String, RequestCache> requestCache = new ConcurrentHashMap<>();
|
||||
private Map<String, CMNodeBean> nodeinfos;
|
||||
public ContractUnitManager() {
|
||||
}
|
||||
|
||||
public ContractUnitManager() {}
|
||||
|
||||
public static ContractUnitManager init() {
|
||||
if (instance != null) {
|
||||
@ -42,7 +44,8 @@ public class ContractUnitManager extends BaseFunctionManager {
|
||||
}
|
||||
instance = new ContractUnitManager();
|
||||
instance.unitsInfoActions = new UnitsInfoAction();
|
||||
instance.actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(executorService, instance.unitsInfoActions) {
|
||||
instance.actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(executorService,
|
||||
instance.unitsInfoActions) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, final JsonObject args, long permission) {
|
||||
return true;
|
||||
@ -62,18 +65,17 @@ public class ContractUnitManager extends BaseFunctionManager {
|
||||
return;
|
||||
}
|
||||
LOGGER.info("[ContractUnitManager] handle:" + arg);
|
||||
actionExecutor.handle(action, arg,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String arg) {
|
||||
ContractUnitManager.instance.send(arg, new String[]{sender});
|
||||
}
|
||||
});
|
||||
actionExecutor.handle(action, arg, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String arg) {
|
||||
ContractUnitManager.instance.send(arg, new String[] {sender});
|
||||
}
|
||||
});
|
||||
} catch (IllegalArgumentException e) {
|
||||
response = new Response();
|
||||
response.action = "onException";
|
||||
response.data = e.getMessage();
|
||||
ContractUnitManager.instance.send(JsonUtil.toJson(response), new String[]{sender});
|
||||
ContractUnitManager.instance.send(JsonUtil.toJson(response), new String[] {sender});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
@ -84,12 +86,9 @@ public class ContractUnitManager extends BaseFunctionManager {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
int count = 0;
|
||||
for (String s : strs) {
|
||||
if (s.contains("sun.reflect")
|
||||
|| s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache")
|
||||
|| s.contains("java.util")
|
||||
|| s.contains("java.lang")
|
||||
|| s.contains("io.netty")) {
|
||||
if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache") || s.contains("java.util")
|
||||
|| s.contains("java.lang") || s.contains("io.netty")) {
|
||||
continue;
|
||||
}
|
||||
ret.append(s);
|
||||
|
@ -46,34 +46,31 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE =
|
||||
BDLedgerContract.UnitMessageType.UnitContractMessage;
|
||||
public Map<String, String> contractID2Sequencing = new HashMap<>();
|
||||
// protected MasterServerTCPAction masterActions;
|
||||
// protected MasterClientTCPAction clientActions;
|
||||
// protected MasterServerTCPAction masterActions;
|
||||
// protected MasterClientTCPAction clientActions;
|
||||
protected ActionExecutor<ResultCallback, JsonObject> actionExecutor;
|
||||
private Map<String, String> contractID2PubKey = new HashMap<>();
|
||||
private Map<String, RequestCache> requestCaches = new ConcurrentHashMap<>();
|
||||
private String nextContractID;
|
||||
public static ExecutionManager instance = init();
|
||||
|
||||
public ExecutionManager() {
|
||||
}
|
||||
public ExecutionManager() {}
|
||||
|
||||
public static ExecutionManager init() {
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
instance = new ExecutionManager();
|
||||
// instance.masterActions = new MasterServerTCPAction();
|
||||
// instance.clientActions = new MasterClientTCPAction();
|
||||
instance.actionExecutor =
|
||||
new ActionExecutor<ResultCallback, JsonObject>(
|
||||
executorService) { // , instance.masterActions,
|
||||
// instance.clientActions) {
|
||||
@Override
|
||||
public boolean checkPermission(
|
||||
Action a, final JsonObject args, long permission) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
// instance.masterActions = new MasterServerTCPAction();
|
||||
// instance.clientActions = new MasterClientTCPAction();
|
||||
instance.actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(executorService) { // ,
|
||||
// instance.masterActions,
|
||||
// instance.clientActions) {
|
||||
@Override
|
||||
public boolean checkPermission(Action a, final JsonObject args, long permission) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
return instance;
|
||||
}
|
||||
|
||||
@ -82,33 +79,29 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
arg.addProperty("sender", sender);
|
||||
Response response;
|
||||
try {
|
||||
// if (arg.get("msgType").getAsInt() == 1) {
|
||||
// handleResponse(arg);
|
||||
// return;
|
||||
// }
|
||||
// if (arg.get("msgType").getAsInt() == 1) {
|
||||
// handleResponse(arg);
|
||||
// return;
|
||||
// }
|
||||
final String action = arg.get("action").getAsString();
|
||||
LOGGER.info("[ExecutionManager] handle:" + arg.toString());
|
||||
// requestID2Sender.put(arg.get("requestID").getAsString(), sender);
|
||||
actionExecutor.handle(
|
||||
action,
|
||||
arg,
|
||||
new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String msg) {
|
||||
try {
|
||||
UnitMessage unitMessage =
|
||||
UnitMessage.parseFrom(ByteUtil.decodeBASE64(msg));
|
||||
NetworkManager.instance.send(unitMessage);
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
// requestID2Sender.put(arg.get("requestID").getAsString(), sender);
|
||||
actionExecutor.handle(action, arg, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(String msg) {
|
||||
try {
|
||||
UnitMessage unitMessage = UnitMessage.parseFrom(ByteUtil.decodeBASE64(msg));
|
||||
NetworkManager.instance.send(unitMessage);
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IllegalArgumentException e) {
|
||||
response = new Response();
|
||||
response.action = "onException";
|
||||
response.data = e.getMessage();
|
||||
ExecutionManager.instance.send(JsonUtil.toJson(response), new String[]{sender});
|
||||
ExecutionManager.instance.send(JsonUtil.toJson(response), new String[] {sender});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
@ -119,12 +112,9 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
int count = 0;
|
||||
for (String s : strs) {
|
||||
if (s.contains("sun.reflect")
|
||||
|| s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache")
|
||||
|| s.contains("java.util")
|
||||
|| s.contains("java.lang")
|
||||
|| s.contains("io.netty")) {
|
||||
if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|
||||
|| s.contains("org.apache") || s.contains("java.util")
|
||||
|| s.contains("java.lang") || s.contains("io.netty")) {
|
||||
continue;
|
||||
}
|
||||
ret.append(s);
|
||||
@ -143,11 +133,11 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
*/
|
||||
public void updateLocalContractToNodeCenter() {
|
||||
ControllerManager.getNodeCenterController().updateContract();
|
||||
// List<?> info = CMActions.manager.getContractDespList();
|
||||
// JsonObject jo = new JsonObject();
|
||||
// jo.addProperty("action", "updateContract");
|
||||
// jo.addProperty("contracts", JsonUtil.toJson(info));
|
||||
// send(jo.toString(), new String[]{GlobalConf.getNodeCenterAddress()});
|
||||
// List<?> info = CMActions.manager.getContractDespList();
|
||||
// JsonObject jo = new JsonObject();
|
||||
// jo.addProperty("action", "updateContract");
|
||||
// jo.addProperty("contracts", JsonUtil.toJson(info));
|
||||
// send(jo.toString(), new String[]{GlobalConf.getNodeCenterAddress()});
|
||||
}
|
||||
|
||||
public void killContractRequest(String contract, ResultCallback rc) {
|
||||
@ -164,21 +154,15 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
if (info.consensusType == ConsensusType.None) {
|
||||
peers = info.getMembers().toArray(new String[info.members.size()]);
|
||||
} else {
|
||||
peers = new String[]{info.master};
|
||||
peers = new String[] {info.master};
|
||||
}
|
||||
ResponseCenter responseCenter =
|
||||
new ResponseCenter(
|
||||
requestId,
|
||||
rc,
|
||||
peers.length,
|
||||
UnitContractRequestType.STOP,
|
||||
ResponseType.ALL.getValue());
|
||||
ResponseCenter responseCenter = new ResponseCenter(requestId, rc, peers.length,
|
||||
UnitContractRequestType.STOP, ResponseType.ALL.getValue());
|
||||
responseCenter.setInfos(contract);
|
||||
this.responseCenters.put(requestId, responseCenter);
|
||||
// respCaches.put(requestId, new RespCache(num));
|
||||
// respCaches.put(requestId, new RespCache(num));
|
||||
UnitContractMessage unitContractMessage =
|
||||
new UnitContractMessage(
|
||||
UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
new UnitContractMessage(UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
UnitContractRequestType.STOP.getValue());
|
||||
unitContractMessage.action = "stopContractAtSlave";
|
||||
unitContractMessage.requestID = requestId;
|
||||
@ -190,49 +174,38 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
unitContractMessage.collector = GlobalConf.instance.peerID;
|
||||
// Sequencing
|
||||
if (consensusType != ConsensusType.None) {
|
||||
SequencingManager.instance.sequencingRequest(
|
||||
unitContractMessage.contractID, unitContractMessage, peers, rc);
|
||||
SequencingManager.instance.sequencingRequest(unitContractMessage.contractID,
|
||||
unitContractMessage, peers, rc);
|
||||
} else {
|
||||
String msgContent = JsonUtil.toJson(unitContractMessage);
|
||||
UnitMessage unitMessage;
|
||||
unitMessage =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.addAllReceiver(Arrays.asList(peers))
|
||||
.build();
|
||||
unitMessage = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.setSender(GlobalConf.instance.peerID).addAllReceiver(Arrays.asList(peers))
|
||||
.build();
|
||||
NetworkManager.instance.send(unitMessage, networkType);
|
||||
}
|
||||
}
|
||||
|
||||
public void startContractRequest(
|
||||
boolean tcpType,
|
||||
String contract,
|
||||
int num,
|
||||
String[] peers,
|
||||
String content,
|
||||
int sequencing,
|
||||
int response,
|
||||
ResultCallback resultCallback) {
|
||||
public void startContractRequest(boolean tcpType, String contract, int num, String[] peers,
|
||||
String content, int sequencing, int response, ResultCallback resultCallback) {
|
||||
LOGGER.info("[startContractRequest] " + contract + " " + num);
|
||||
String requestId = getRequestId();
|
||||
// this.requestCallbacks.put(requestId, resultCallback);
|
||||
ResponseCenter responseCenter =
|
||||
new ResponseCenter(
|
||||
requestId, resultCallback, num, UnitContractRequestType.START, response);
|
||||
// this.requestCallbacks.put(requestId, resultCallback);
|
||||
ResponseCenter responseCenter = new ResponseCenter(requestId, resultCallback, num,
|
||||
UnitContractRequestType.START, response);
|
||||
responseCenter.setType(ResponseType.ALL);
|
||||
this.responseCenters.put(requestId, responseCenter);
|
||||
// respCaches.put(requestId, new RespCache(num));
|
||||
// respCaches.put(requestId, new RespCache(num));
|
||||
UnitContractMessage unitContractMessage =
|
||||
new UnitContractMessage(
|
||||
UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
new UnitContractMessage(UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
UnitContractRequestType.START.getValue());
|
||||
unitContractMessage.action = "startContractAtSlave";
|
||||
unitContractMessage.requestID = requestId;
|
||||
unitContractMessage.contractID = getNextContractID();
|
||||
ExecutionManager.instance.askForNextContractID();
|
||||
// logger.info("hashcode " + nextContractID.hashCode());
|
||||
// logger.info("hashcode " + nextContractID.hashCode());
|
||||
unitContractMessage.content = content;
|
||||
unitContractMessage.sequencing = sequencing;
|
||||
unitContractMessage.rcheck = response;
|
||||
@ -242,22 +215,18 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
UnitMessage unitMessage;
|
||||
if (null != peers && peers.length > 0) {
|
||||
responseCenter.updatePeers(peers);
|
||||
unitMessage =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.addAllReceiver(Arrays.asList(peers))
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.build();
|
||||
unitMessage = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.addAllReceiver(Arrays.asList(peers)).setSender(GlobalConf.instance.peerID)
|
||||
.build();
|
||||
} else {
|
||||
// abandon java needs to know peers in order to collect response
|
||||
// abandon java needs to know peers in order to collect response
|
||||
// No specified receiver, send to p2p nodes randomly
|
||||
unitMessage =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.build();
|
||||
unitMessage = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.setSender(GlobalConf.instance.peerID).build();
|
||||
}
|
||||
NetworkManager.instance.send(unitMessage, tcpType ? NetworkType.TCP : NetworkType.P2P);
|
||||
}
|
||||
@ -272,32 +241,24 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
nextContractID = content;
|
||||
}
|
||||
|
||||
public void executeContractRequest(
|
||||
String contract,
|
||||
String operation,
|
||||
String arg,
|
||||
int network,
|
||||
int sequencing,
|
||||
int responseType,
|
||||
ResultCallback rc) {
|
||||
public void executeContractRequest(String contract, String operation, String arg, int network,
|
||||
int sequencing, int responseType, ResultCallback rc) {
|
||||
String requestId = getRequestId();
|
||||
// this.requestCallbacks.put(requestId, resultCallback);
|
||||
// this.requestCallbacks.put(requestId, resultCallback);
|
||||
String[] peers;
|
||||
MultiPointContractInfo info = ContractUnitManager.instance.getContractInfo(contract);
|
||||
if (info.consensusType == ConsensusType.None) {
|
||||
peers = info.getMembers().toArray(new String[info.members.size()]);
|
||||
} else {
|
||||
peers = new String[]{info.master};
|
||||
peers = new String[] {info.master};
|
||||
}
|
||||
ResponseCenter responseCenter =
|
||||
new ResponseCenter(
|
||||
requestId, rc, peers.length, UnitContractRequestType.EXECUTE, responseType);
|
||||
ResponseCenter responseCenter = new ResponseCenter(requestId, rc, peers.length,
|
||||
UnitContractRequestType.EXECUTE, responseType);
|
||||
responseCenter.setInfos(contract);
|
||||
this.responseCenters.put(requestId, responseCenter);
|
||||
// respCaches.put(requestId, new RespCache(num));
|
||||
// respCaches.put(requestId, new RespCache(num));
|
||||
UnitContractMessage unitContractMessage =
|
||||
new UnitContractMessage(
|
||||
UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
new UnitContractMessage(UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
UnitContractRequestType.EXECUTE.getValue());
|
||||
unitContractMessage.action = "executeContractAtSlave";
|
||||
unitContractMessage.requestID = requestId;
|
||||
@ -310,61 +271,54 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
// Sequencing
|
||||
ConsensusType consensusType = ConsensusType.getType(sequencing);
|
||||
if (consensusType != ConsensusType.None) {
|
||||
SequencingManager.instance.sequencingRequest(
|
||||
unitContractMessage.contractID, unitContractMessage, peers, rc);
|
||||
SequencingManager.instance.sequencingRequest(unitContractMessage.contractID,
|
||||
unitContractMessage, peers, rc);
|
||||
} else {
|
||||
String msgContent = JsonUtil.toJson(unitContractMessage);
|
||||
UnitMessage unitMessage;
|
||||
unitMessage =
|
||||
UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.setSender(GlobalConf.instance.peerID)
|
||||
.addAllReceiver(Arrays.asList(peers))
|
||||
.build();
|
||||
unitMessage = UnitMessage.newBuilder()
|
||||
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
.setContent(ByteString.copyFromUtf8(msgContent))
|
||||
.setSender(GlobalConf.instance.peerID).addAllReceiver(Arrays.asList(peers))
|
||||
.build();
|
||||
NetworkManager.instance.send(unitMessage, NetworkType.getType(network));
|
||||
}
|
||||
}
|
||||
|
||||
public void stopContractRequest(
|
||||
String contract,
|
||||
String operation,
|
||||
String arg,
|
||||
int network,
|
||||
int sequencing,
|
||||
ResultCallback rc) {
|
||||
// String requestId = getRequestId();
|
||||
//// this.requestCallbacks.put(requestId, resultCallback);
|
||||
// MultiPointContractInfo info =
|
||||
public void stopContractRequest(String contract, String operation, String arg, int network,
|
||||
int sequencing, ResultCallback rc) {
|
||||
// String requestId = getRequestId();
|
||||
//// this.requestCallbacks.put(requestId, resultCallback);
|
||||
// MultiPointContractInfo info =
|
||||
// ContractUnitManager.instance.getContractInfo(contract);
|
||||
// String[] peers;
|
||||
// if (info.consensusType == ConsensusType.None) {
|
||||
// peers = info.getMembers().toArray(new String[info.members.size()]);
|
||||
// } else {
|
||||
// peers = new String[]{info.master};
|
||||
// }
|
||||
// this.responseCenters.put(requestId, new ResponseCenter(requestId, rc,
|
||||
// String[] peers;
|
||||
// if (info.consensusType == ConsensusType.None) {
|
||||
// peers = info.getMembers().toArray(new String[info.members.size()]);
|
||||
// } else {
|
||||
// peers = new String[]{info.master};
|
||||
// }
|
||||
// this.responseCenters.put(requestId, new ResponseCenter(requestId, rc,
|
||||
// peers.length, UnitContractRequestType.EXECUTE, sequencing));
|
||||
//// respCaches.put(requestId, new RespCache(num));
|
||||
// UnitContractMessage unitContractMessage = new UnitContractMessage(
|
||||
// UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
// UnitContractRequestType.EXECUTE.getValue());
|
||||
// unitContractMessage.action = "executeContractAtSlave";
|
||||
// unitContractMessage.requestID = requestId;
|
||||
// unitContractMessage.contractID = contract;
|
||||
// unitContractMessage.operation = operation;
|
||||
// unitContractMessage.arg = arg;
|
||||
// unitContractMessage.sequencing = sequencing;
|
||||
// unitContractMessage.network = network;
|
||||
// String msgContent = gson.toJson(unitContractMessage);
|
||||
// UnitMessage unitMessage;
|
||||
// unitMessage = UnitMessage.newBuilder()
|
||||
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
// .setContent(ByteString.copyFromUtf8(msgContent))
|
||||
// .setSender(GlobalConf.instance.peerID)
|
||||
// .addAllReceiver(Arrays.asList(peers))
|
||||
// .build();
|
||||
// NetworkManager.instance.send(unitMessage, NetworkType.getType(network));
|
||||
//// respCaches.put(requestId, new RespCache(num));
|
||||
// UnitContractMessage unitContractMessage = new UnitContractMessage(
|
||||
// UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
// UnitContractRequestType.EXECUTE.getValue());
|
||||
// unitContractMessage.action = "executeContractAtSlave";
|
||||
// unitContractMessage.requestID = requestId;
|
||||
// unitContractMessage.contractID = contract;
|
||||
// unitContractMessage.operation = operation;
|
||||
// unitContractMessage.arg = arg;
|
||||
// unitContractMessage.sequencing = sequencing;
|
||||
// unitContractMessage.network = network;
|
||||
// String msgContent = gson.toJson(unitContractMessage);
|
||||
// UnitMessage unitMessage;
|
||||
// unitMessage = UnitMessage.newBuilder()
|
||||
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
// .setContent(ByteString.copyFromUtf8(msgContent))
|
||||
// .setSender(GlobalConf.instance.peerID)
|
||||
// .addAllReceiver(Arrays.asList(peers))
|
||||
// .build();
|
||||
// NetworkManager.instance.send(unitMessage, NetworkType.getType(network));
|
||||
}
|
||||
|
||||
public void askForNextContractID() {
|
||||
@ -389,5 +343,4 @@ public class ExecutionManager extends BaseFunctionManager {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ public class RecoveryManager extends BaseFunctionManager {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.addProperty("action", "NCStartElect");
|
||||
jo.addProperty("contractID", contractID);
|
||||
//NetworkManager.instance.cmClientHandler.sendMsg(JsonUtil.toJson(jo));
|
||||
// NetworkManager.instance.cmClientHandler.sendMsg(JsonUtil.toJson(jo));
|
||||
}
|
||||
|
||||
|
||||
|
@ -23,16 +23,15 @@ import java.util.Map;
|
||||
public class SequencingManager extends BaseFunctionManager {
|
||||
private static final String MANAGER_SYMBOL = "SEQUENCING";
|
||||
private static final Logger LOGGER = LogManager.getLogger(SequencingManager.class);
|
||||
// protected SequencingActions sequencingActions;
|
||||
// protected ActionExecutor<ResultCallback, JsonObject> actionExecutor;
|
||||
// protected SequencingActions sequencingActions;
|
||||
// protected ActionExecutor<ResultCallback, JsonObject> actionExecutor;
|
||||
private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE =
|
||||
BDLedgerContract.UnitMessageType.UnitSequencingMessage;
|
||||
// protected static ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
// protected static ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
private final Map<String, ConsensusCenter> consensusCenters = new HashMap<>();
|
||||
public static SequencingManager instance = init();
|
||||
|
||||
public SequencingManager() {
|
||||
}
|
||||
public SequencingManager() {}
|
||||
|
||||
public static SequencingManager init() {
|
||||
if (null != instance) {
|
||||
@ -51,8 +50,8 @@ public class SequencingManager extends BaseFunctionManager {
|
||||
String contractID = unitSequencingMessage.contractID;
|
||||
ConsensusCenter consensusCenter = consensusCenters.get(contractID);
|
||||
if (null == consensusCenter) {
|
||||
// consensusCenter = createConsensusCenter(unitSequencingMessage);
|
||||
// this.consensusCenters.put(contractID, consensusCenter);
|
||||
// consensusCenter = createConsensusCenter(unitSequencingMessage);
|
||||
// this.consensusCenters.put(contractID, consensusCenter);
|
||||
}
|
||||
// create null, then no dingxu
|
||||
if (null == consensusCenter) {
|
||||
@ -60,125 +59,124 @@ public class SequencingManager extends BaseFunctionManager {
|
||||
return;
|
||||
}
|
||||
consensusCenter.handle(unitSequencingMessage);
|
||||
// try {
|
||||
//// if (arg.get("msgType").getAsInt() == 1) {
|
||||
//// handleResponse(arg);
|
||||
//// return;
|
||||
//// }
|
||||
// final String action = arg.get("action").getAsString();
|
||||
// logger.info("[SequencingManager] handle:" + arg.toString());
|
||||
// actionExecutor.handle(action, arg,
|
||||
// new ResultCallback() {
|
||||
// @Override
|
||||
// public void onResult(String msg) {
|
||||
// try {
|
||||
// BDLedgerContract.UnitMessage unitMessage =
|
||||
// BDLedgerContract.UnitMessage.parseFrom(Base64.decode(msg));
|
||||
// NetworkManager.instance.send(unitMessage);
|
||||
// } catch (InvalidProtocolBufferException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// } catch (IllegalArgumentException e) {
|
||||
// response = new SequencingManager.Response();
|
||||
// response.action = "onException";
|
||||
// response.data = e.getMessage();
|
||||
// ExecutionManager.instance.send(JsonUtil.toJson(response), new String[]{sender});
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// response = new SequencingManager.Response();
|
||||
// response.action = "onException";
|
||||
// String[] strs = bo.toString().split("\n");
|
||||
// StringBuilder ret = new StringBuilder();
|
||||
// int count = 0;
|
||||
// for (String s : strs) {
|
||||
// if (s.contains("sun.reflect")
|
||||
// || s.contains("java.lang.reflect")
|
||||
// || s.contains("org.apache")
|
||||
// || s.contains("java.util")
|
||||
// || s.contains("java.lang")
|
||||
// || s.contains("io.netty")) {
|
||||
// continue;
|
||||
// }
|
||||
// ret.append(s);
|
||||
// ret.append("\n");
|
||||
// if (count++ > 10) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// response.data = ret.toString();
|
||||
// logger.debug(JsonUtil.toJson(response));
|
||||
// }
|
||||
// try {
|
||||
//// if (arg.get("msgType").getAsInt() == 1) {
|
||||
//// handleResponse(arg);
|
||||
//// return;
|
||||
//// }
|
||||
// final String action = arg.get("action").getAsString();
|
||||
// logger.info("[SequencingManager] handle:" + arg.toString());
|
||||
// actionExecutor.handle(action, arg,
|
||||
// new ResultCallback() {
|
||||
// @Override
|
||||
// public void onResult(String msg) {
|
||||
// try {
|
||||
// BDLedgerContract.UnitMessage unitMessage =
|
||||
// BDLedgerContract.UnitMessage.parseFrom(Base64.decode(msg));
|
||||
// NetworkManager.instance.send(unitMessage);
|
||||
// } catch (InvalidProtocolBufferException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// } catch (IllegalArgumentException e) {
|
||||
// response = new SequencingManager.Response();
|
||||
// response.action = "onException";
|
||||
// response.data = e.getMessage();
|
||||
// ExecutionManager.instance.send(JsonUtil.toJson(response), new String[]{sender});
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
// e.printStackTrace(new PrintStream(bo));
|
||||
// response = new SequencingManager.Response();
|
||||
// response.action = "onException";
|
||||
// String[] strs = bo.toString().split("\n");
|
||||
// StringBuilder ret = new StringBuilder();
|
||||
// int count = 0;
|
||||
// for (String s : strs) {
|
||||
// if (s.contains("sun.reflect")
|
||||
// || s.contains("java.lang.reflect")
|
||||
// || s.contains("org.apache")
|
||||
// || s.contains("java.util")
|
||||
// || s.contains("java.lang")
|
||||
// || s.contains("io.netty")) {
|
||||
// continue;
|
||||
// }
|
||||
// ret.append(s);
|
||||
// ret.append("\n");
|
||||
// if (count++ > 10) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// response.data = ret.toString();
|
||||
// logger.debug(JsonUtil.toJson(response));
|
||||
// }
|
||||
}
|
||||
|
||||
public void createConsensusCenterToStore(UnitContractMessage msg) {
|
||||
//create local center
|
||||
// create local center
|
||||
String contractID = msg.contractID;
|
||||
ConsensusCenter consensusCenter = createConsensusCenter(msg);
|
||||
consensusCenters.put(contractID, consensusCenter);
|
||||
}
|
||||
|
||||
public void sequencingRequest(
|
||||
String contractID,
|
||||
UnitContractMessage msg,
|
||||
String[] peers,
|
||||
public void sequencingRequest(String contractID, UnitContractMessage msg, String[] peers,
|
||||
ResultCallback resultCallback) {
|
||||
// content is UnitContractMessage
|
||||
// ConsensusCenter consensusCenter = consensusCenters.get(contractID);
|
||||
// if (null == consensusCenter) {
|
||||
// consensusCenter = createConsensusCenter(msg);
|
||||
// this.consensusCenters.put(contractID, consensusCenter);
|
||||
// }
|
||||
// // create null, then no dingxu
|
||||
// if (consensusCenter == null) {
|
||||
// ExecutionManager.instance.send(gson.toJson(msg), peers);
|
||||
// return;
|
||||
// }
|
||||
// ConsensusCenter consensusCenter = consensusCenters.get(contractID);
|
||||
// if (null == consensusCenter) {
|
||||
// consensusCenter = createConsensusCenter(msg);
|
||||
// this.consensusCenters.put(contractID, consensusCenter);
|
||||
// }
|
||||
// // create null, then no dingxu
|
||||
// if (consensusCenter == null) {
|
||||
// ExecutionManager.instance.send(gson.toJson(msg), peers);
|
||||
// return;
|
||||
// }
|
||||
String requestID = msg.requestID;
|
||||
requestCallbacks.put(requestID, resultCallback);
|
||||
UnitSequencingMessage sequencingMessage = new UnitSequencingMessage(ConsensusStage.Request.toInt());
|
||||
UnitSequencingMessage sequencingMessage =
|
||||
new UnitSequencingMessage(ConsensusStage.Request.toInt());
|
||||
sequencingMessage.sequencing = msg.sequencing;
|
||||
sequencingMessage.network = msg.network;
|
||||
sequencingMessage.contractID = contractID;
|
||||
sequencingMessage.requestID = requestID;
|
||||
sequencingMessage.content = JsonUtil.toJson(msg);
|
||||
sequencingMessage.master = ContractUnitManager.instance.getMaster(contractID);
|
||||
send(JsonUtil.toJson(sequencingMessage), new String[]{sequencingMessage.master});
|
||||
send(JsonUtil.toJson(sequencingMessage), new String[] {sequencingMessage.master});
|
||||
|
||||
// this.responseCenters.put(requestId,
|
||||
// new ResponseCenter(requestId, resultCallback, num, UnitContractRequestType.START, sequencing));
|
||||
//// respCaches.put(requestId, new RespCache(num));
|
||||
// UnitContractMessage unitContractMessage = new UnitContractMessage(
|
||||
// UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
// UnitContractRequestType.START.getValue());
|
||||
// unitContractMessage.action = "startContractAtSlave";
|
||||
// unitContractMessage.requestID = requestId;
|
||||
//// unitContractMessage.contractID = contract;
|
||||
// unitContractMessage.content = content;
|
||||
// unitContractMessage.sequencing = sequencing;
|
||||
// unitContractMessage.num = num;
|
||||
// unitContractMessage.network = tcpType?0:1;
|
||||
// String msgContent = gson.toJson(unitContractMessage);
|
||||
// BDLedgerContract.UnitMessage unitMessage;
|
||||
// if (null != peers && peers.length > 0) {
|
||||
// unitMessage = BDLedgerContract.UnitMessage.newBuilder()
|
||||
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
// .setContent(ByteString.copyFromUtf8(msgContent))
|
||||
// .addAllReceiver(Arrays.asList(peers))
|
||||
// .setSender(GlobalConf.instance.peerID)
|
||||
// .build();
|
||||
// } else {
|
||||
// // No specified receiver, send to p2p nodes randomly
|
||||
// unitMessage = BDLedgerContract.UnitMessage.newBuilder()
|
||||
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
// .setContent(ByteString.copyFromUtf8(msgContent))
|
||||
// .setSender(GlobalConf.instance.peerID)
|
||||
// .build();
|
||||
// }
|
||||
// NetworkManager.instance.send(unitMessage, tcpType?null:NetworkManager.P2P_GRPC_CLIENT);
|
||||
// this.responseCenters.put(requestId,
|
||||
// new ResponseCenter(requestId, resultCallback, num, UnitContractRequestType.START,
|
||||
// sequencing));
|
||||
//// respCaches.put(requestId, new RespCache(num));
|
||||
// UnitContractMessage unitContractMessage = new UnitContractMessage(
|
||||
// UnitContractMessageType.ContractUnitRequest.getValue(),
|
||||
// UnitContractRequestType.START.getValue());
|
||||
// unitContractMessage.action = "startContractAtSlave";
|
||||
// unitContractMessage.requestID = requestId;
|
||||
//// unitContractMessage.contractID = contract;
|
||||
// unitContractMessage.content = content;
|
||||
// unitContractMessage.sequencing = sequencing;
|
||||
// unitContractMessage.num = num;
|
||||
// unitContractMessage.network = tcpType?0:1;
|
||||
// String msgContent = gson.toJson(unitContractMessage);
|
||||
// BDLedgerContract.UnitMessage unitMessage;
|
||||
// if (null != peers && peers.length > 0) {
|
||||
// unitMessage = BDLedgerContract.UnitMessage.newBuilder()
|
||||
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
// .setContent(ByteString.copyFromUtf8(msgContent))
|
||||
// .addAllReceiver(Arrays.asList(peers))
|
||||
// .setSender(GlobalConf.instance.peerID)
|
||||
// .build();
|
||||
// } else {
|
||||
// // No specified receiver, send to p2p nodes randomly
|
||||
// unitMessage = BDLedgerContract.UnitMessage.newBuilder()
|
||||
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
|
||||
// .setContent(ByteString.copyFromUtf8(msgContent))
|
||||
// .setSender(GlobalConf.instance.peerID)
|
||||
// .build();
|
||||
// }
|
||||
// NetworkManager.instance.send(unitMessage, tcpType?null:NetworkManager.P2P_GRPC_CLIENT);
|
||||
}
|
||||
|
||||
private ConsensusCenter createConsensusCenter(UnitContractMessage msg) {
|
||||
@ -195,19 +193,21 @@ public class SequencingManager extends BaseFunctionManager {
|
||||
}
|
||||
|
||||
public void updateContractInfo2Unit(int network, String contractID, String firstSuccessPeer) {
|
||||
MultiPointContractInfo contractInfo = ContractUnitManager.instance.getContractInfo(contractID);
|
||||
MultiPointContractInfo contractInfo =
|
||||
ContractUnitManager.instance.getContractInfo(contractID);
|
||||
contractInfo.master = firstSuccessPeer;
|
||||
UnitContractMessage unitContractMessage = new UnitContractMessage(0);
|
||||
unitContractMessage.content = JsonUtil.toJson(contractInfo);
|
||||
unitContractMessage.contractID = contractID;
|
||||
// unitContractMessage.network = network;
|
||||
// unitContractMessage.network = network;
|
||||
unitContractMessage.action = "updateContractStartInfo";
|
||||
send(JsonUtil.toJson(unitContractMessage),
|
||||
contractInfo.getMembers().toArray(new String[0]), BDLedgerContract.UnitMessageType.UnitUnitMessage);
|
||||
send(JsonUtil.toJson(unitContractMessage), contractInfo.getMembers().toArray(new String[0]),
|
||||
BDLedgerContract.UnitMessageType.UnitUnitMessage);
|
||||
}
|
||||
|
||||
public void updateConsensusCenter(String contractID, MultiPointContractInfo info) {
|
||||
LOGGER.info("[updateConsensusCenter] consensusCenters " + contractID + " " + consensusCenters.size());
|
||||
LOGGER.info("[updateConsensusCenter] consensusCenters " + contractID + " "
|
||||
+ consensusCenters.size());
|
||||
ConsensusCenter consensusCenter = consensusCenters.get(contractID);
|
||||
while (null == consensusCenter) {
|
||||
try {
|
||||
@ -232,5 +232,4 @@ public class SequencingManager extends BaseFunctionManager {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user