Compare commits

...

49 Commits

Author SHA1 Message Date
CaiHQ
7da128f73f update docker files 2024-12-23 16:13:27 +08:00
CaiHQ
f8465b121a update doip 2024-07-22 21:08:16 +08:00
CaiHQ
f61e2c970c feat: support regx match 2024-05-23 15:28:42 +08:00
Kaidong Wu
e9f7a9140b
feat(agent-backend): adds wildcard character support for startContract.path in cmconfig.json 2024-05-23 13:27:30 +08:00
CaiHQ
540a4232d0 auto prune 2023-12-15 16:46:26 +08:00
CaiHQ
22532666dd update doip-sdk 2023-12-15 16:46:24 +08:00
Frank.R.Wu
73feba9b0e build: config spotless plugin and reformat code 2023-06-15 11:08:00 +08:00
Frank.R.Wu
1bf3ccc059 fix: fix version of org.bdware.doip:bdosclient:0.0.7 2023-06-12 21:50:35 +08:00
CaiHQ
07e5070316 add temporyaction 2023-06-09 18:20:26 +08:00
CaiHQ
e925d0e9e7 support doip-start-port in TempActions 2023-04-28 17:51:36 +08:00
CaiHQ
94f1b6dc7d support doip-cluster 2023-04-24 18:57:40 +08:00
CaiHQ
3f4d154d2b update doip-sdk irp-sdk 2023-04-05 16:25:52 +08:00
CaiHQ
0d4f0243a2 prune origin doi logic 2023-03-31 17:13:50 +08:00
CaiHQ
af298b6ae7 prune old doi logic 2023-03-31 17:10:40 +08:00
CaiHQ
e76865fb09 fix sigar dynamiclib missing
fix netty dynamiclib missing
fix BCOManager
2023-03-30 23:51:52 +08:00
CaiHQ
8fa064ee8a update bdrepo support 2023-03-30 14:58:43 +08:00
CaiHQ
aef5f5fe38 Merge branch 'master' of gitee.com:BDWare/agent-backend 2023-02-08 14:02:10 +08:00
CaiHQ
964d9a9fbc prune CMHttpServer 2023-02-08 14:01:55 +08:00
CaiHQ
6d90352b6f prune CMHttpServer 2023-02-08 13:59:03 +08:00
haoeliu@foxmail.com
b229ed8c67 fix the CertificateCompressionAlgo bug by replacing netty version to 2.0.50.Final in build.gradle 2023-01-25 18:09:08 +08:00
CaiHQ
7c91416350 add update node manager 2022-11-29 15:14:58 +08:00
CaiHQ
e93c07ed84 fix: assets file cache bugs 2022-10-25 17:48:07 +08:00
CaiHQ
926078e081 add support for remote debug 2022-09-02 21:31:23 +08:00
Frank.R.Wu
b5a9b2d085 merge: remove unused files
remove SelfAdaptiveShardingExecutor
2022-07-30 16:27:37 +08:00
Frank.R.Wu
7791c78c58 feat(agent-backend): add event publishing
prune CMActions; add NodeCenterClientController.publishEventFromCenter
2022-07-29 22:59:19 +08:00
Frank.R.Wu
90cdabfa92 feat(agent-backend)
deprecate SelfAdaptiveShardingExecutor
2022-07-29 22:59:19 +08:00
Frank.R.Wu
87d32ab220 feat: update SelfAdaptiveSharding
use independent thread pool to handle messages;ignore body validation
2022-07-29 22:59:19 +08:00
Frank.R.Wu
ed507b14b4 fix: fix bugs in SelfAdaptiveShardingExecutor
remove body check in SelfAdaptiveShardingExecutor.Block.isValid
2022-07-29 22:59:19 +08:00
Frank.R.Wu
78366fcb55 feat: update SelfAdaptiveSharding
set delay of period task to 1s
2022-07-29 22:59:19 +08:00
CaiHQ
ea71e8a358 add join support 2022-06-28 19:49:54 +08:00
CaiHQ
2758eb42e4 front: support startMulitipoint with args
fix: ContractClient missing arguments error
update: @Router arguments format
fix: ContractStatusRecorder null exception
2022-06-26 17:54:30 +08:00
garvey-wong
ccdc33424e feat: assign variable isDebug 2022-06-10 12:00:44 +08:00
CaiHQ
18521aaf82 debug raft 2022-05-27 17:48:25 +08:00
CaiHQ
928e89988e upgrade common 2022-05-23 17:14:09 +08:00
CaiHQ
de30170d65 doc: docker deploy
feat: support docker debug
2022-05-23 17:06:54 +08:00
CaiHQ
a553a13a31 add byte test 2022-05-05 13:42:28 +08:00
CaiHQ
3000ba5874 update docker configs 2022-05-05 13:39:17 +08:00
garvey-wong
da1f524a06 feat: sharding executor exec locally 2022-05-03 15:01:46 +08:00
CaiHQ
5a4fdb8a13 feat: support createParam
feat: docker script
2022-04-21 10:18:47 +08:00
CaiHQ
0a24986907 add nodecenterws config 2022-03-30 11:17:08 +08:00
CaiHQ
aacd939408 support ledgerparams
add startContract at cmconfig.json
add docker scripts
2022-03-22 23:52:01 +08:00
CaiHQ
b1bf67c802 fix: MultiPointCooperationExecutor 2022-02-18 11:14:10 +08:00
CaiHQ
8618e61d98 Merge branch 'feat/consistency-sdk' of https://gitee.com/BDWare/agent-backend 2022-02-17 16:43:50 +08:00
CaiHQ
f3f146b30a test action: prune killed Contract 2022-02-17 16:42:17 +08:00
汪旭鑫
e2df294d65 refactor: sdk for consensus algorithm 2022-02-15 14:44:15 +08:00
CaiHQ
fc7512f50f upgrad doipsdk 2022-01-09 00:04:04 +08:00
CaiHQ
ce77ebe0b9 prune: use stable doip-sdk 2022-01-04 10:16:20 +08:00
Frank.R.Wu
bc8f539c9c feat: add EventWSActions.pubEvent
add EventWSActions.pubEvent to allow clients to publish events, and return generated key pair if the client doesn't signature the event
2021-12-29 20:26:21 +08:00
CaiHQ
9f30c938da optimize receive file 2021-12-29 11:52:24 +08:00
125 changed files with 7222 additions and 6849 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/ypkcache
/BDWareProjectDir /BDWareProjectDir
/defaultLog/ /defaultLog/
/ContractDB /ContractDB

View File

@ -4,6 +4,7 @@ plugins {
} }
mainClassName = 'org.bdware.server.CMHttpServer' mainClassName = 'org.bdware.server.CMHttpServer'
apply from: '../spotless.gradle'
application { application {
@ -34,18 +35,31 @@ dependencies {
implementation project(":cm") implementation project(":cm")
implementation project(":mockjava") implementation project(":mockjava")
implementation project(":front-base") implementation project(":front-base")
implementation project(":consistency-sdk")
implementation 'io.prometheus:simpleclient_httpserver:0.12.0' 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 'io.grpc:grpc-all:1.43.1'
implementation 'org.apache.velocity:velocity-engine-core:2.3' implementation 'org.apache.velocity:velocity-engine-core:2.3'
implementation 'com.nimbusds:nimbus-jose-jwt:9.10' 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' 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 { compileJava {
options.compilerArgs << "-Xlint:unchecked" options.compilerArgs << "-Xlint:unchecked"
} }
repositories {
mavenCentral()
}
jar { jar {
String libs = '' String libs = ''
configurations.runtimeClasspath.each { configurations.runtimeClasspath.each {
@ -77,6 +91,9 @@ task copyScript(type: Copy) {
include 'cmstop.sh' include 'cmstop.sh'
include 'cmconfig.json.template' include 'cmconfig.json.template'
include 'killContracts.sh' include 'killContracts.sh'
include 'genKeyAndCMConfig.sh'
include 'cmvar.json'
include 'log4j2.properties'
} }
into "./build/output" into "./build/output"
println("copyScript done !") println("copyScript done !")
@ -107,7 +124,12 @@ task copyDynamicLibrary(type: Copy) {
into './build/output/dynamicLibrary' 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 from configurations.runtimeClasspath
into "./build/output/libs/" into "./build/output/libs/"
} }
@ -150,11 +172,6 @@ task copyDockerfile(type: Copy) {
into "./build/" into "./build/"
} }
//task copyCP(type: Copy, dependsOn: ":cp:buildBundle") {
// from "../cp/build/output"
// into "./build/output"
//}
task copyKeys(type: Copy) { task copyKeys(type: Copy) {
from "./keys" from "./keys"
into "./build/output/keys" into "./build/output/keys"

View File

@ -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" "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"`进行配置。

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,19 +1,22 @@
{ {
"ip": "127.0.0.1", "cmi":"",
"servicePort": 21030, "debug":"",
"isLAN" : true, "disableDoRepo":false,
"disableDoRepo": false, "disableLocalLhs":false,
"doipPort": 21032, "doipCertPath":"",
"doipCertPath": "", "doipLhsAddress":"",
"doipUserHandle": "", "doipPort":21032,
"doipLhsAddress": "", "doipUserHandle":"",
"withBdledgerServer": false, "enableEventPersistence":false,
"withBdledgerClient": "", "enableSsl":"./ssl/chained.pem:./ssl/domain.pem",
"enableEventPersistence": false, "ip":"127.0.0.1",
"enableSsl": "./ssl/chained.pem:./ssl/domain.pem", "isLAN":true,
"textFileSuffixes": ".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv", "overwrite":false,
"wsPluginActions": "org.bdware.metering.MeteringAction", "servicePort":21030,
"clientToAgentPlugins": "", "textFileSuffixes":".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv",
"clientToClusterPlugins": "", "withBdledgerClient":"./runnable/bdledger_mac",
"tcpPlugins": "" "withBdledgerServer":false,
"consistencyPlugins": "./libs/custom-plugin.jar",
"datachainConf": "_LEDGERHOSTCONF"
} }

View File

@ -18,7 +18,7 @@ fi
# ps -ef | grep java | grep bdserver | grep CM | awk '{print $2}' | xargs kill -9 # 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 #-Xmx3550m -Xms3550m -Xmn2g
#-XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=5 #-XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=5
# 以下可启用https/wss其中./ssl/xxx.pfx为ssl证书的路径:后面的123456是密码。 # 以下可启用https/wss其中./ssl/xxx.pfx为ssl证书的路径:后面的123456是密码。

3
script/cmvar.json Normal file
View File

@ -0,0 +1,3 @@
{
"_LEDGERHOSTCONF": "localhost:2401"
}

5
script/genKeyAndCMConfig.sh Executable file
View 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
View 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

View File

@ -1,3 +1,5 @@
#/bin/bash #/bin/bash
scp -P 222 ./build/bdserver.zip dev@47.95.110.68:/data/public/releases/bdcontract/$1/ scp -P 222 ./build/bdserver.zip dev@47.95.110.68:/data/public/releases/bdcontract/$1/
#In_235813

View File

@ -38,12 +38,12 @@ public class HeartBeatUtil {
public synchronized void schedule(TimerTask checkAliveTask, int delay, int period) { public synchronized void schedule(TimerTask checkAliveTask, int delay, int period) {
try { try {
if (!recordedFuture.containsKey(checkAliveTask)) { if (recordedFuture.containsKey(checkAliveTask)) {
ScheduledFuture<?> future = cancel(checkAliveTask);
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(
checkAliveTask, delay, period, TimeUnit.MILLISECONDS);
recordedFuture.put(checkAliveTask, future);
} }
ScheduledFuture<?> future = ContractManager.scheduledThreadPool
.scheduleWithFixedDelay(checkAliveTask, delay, period, TimeUnit.MILLISECONDS);
recordedFuture.put(checkAliveTask, future);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -24,10 +24,8 @@ public class ContractExecuteInfo implements Serializable {
public String invokeID; // TODO public String invokeID; // TODO
public ContractExecType type; public ContractExecType type;
public transient PriorityQueue<ContractRequest> queue; // contract request public transient PriorityQueue<ContractRequest> queue; // contract request
public transient Map<Integer, String> uniReqIDMap = public transient Map<Integer, String> uniReqIDMap = new ConcurrentHashMap<>(); // 用于请求
new ConcurrentHashMap<>(); // 用于请求 public transient Map<Integer, ResultCallback> resultMap = new ConcurrentHashMap<>(); // 用于请求
public transient Map<Integer, ResultCallback> resultMap =
new ConcurrentHashMap<>(); // 用于请求
public transient PriorityQueue<TransRecord> trans_queue; // transRecord public transient PriorityQueue<TransRecord> trans_queue; // transRecord
private String contractID; private String contractID;
private boolean isPrivate = false; private boolean isPrivate = false;
@ -44,10 +42,14 @@ public class ContractExecuteInfo implements Serializable {
} }
public void init() { public void init() {
if (queue == null) queue = new PriorityQueue<ContractRequest>(); if (queue == null)
if (trans_queue == null) trans_queue = new PriorityQueue<TransRecord>(); queue = new PriorityQueue<ContractRequest>();
if (uniReqIDMap == null) uniReqIDMap = new ConcurrentHashMap<Integer, String>(); if (trans_queue == null)
if (resultMap == null) resultMap = new ConcurrentHashMap<Integer, ResultCallback>(); trans_queue = new PriorityQueue<TransRecord>();
if (uniReqIDMap == null)
uniReqIDMap = new ConcurrentHashMap<Integer, String>();
if (resultMap == null)
resultMap = new ConcurrentHashMap<Integer, ResultCallback>();
} }
public int getLastExeSeq() { public int getLastExeSeq() {
@ -56,15 +58,15 @@ public class ContractExecuteInfo implements Serializable {
public void setLastExeSeq(int lastExeSeq) { public void setLastExeSeq(int lastExeSeq) {
this.lastExeSeq.set(lastExeSeq); this.lastExeSeq.set(lastExeSeq);
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) { // 如果现在是Stable模式就同步刷到磁盘
CMTables.LastExeSeq.toString(), contractID)) { // 如果现在是Stable模式就同步刷到磁盘 KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID,
KeyValueDBUtil.instance.setValue( lastExeSeq + "");
CMTables.LastExeSeq.toString(), contractID, lastExeSeq + "");
} }
} }
public boolean isSequent(int a) { public boolean isSequent(int a) {
if (a - lastExeSeq.get() == 1) return true; if (a - lastExeSeq.get() == 1)
return true;
return false; return false;
} }
@ -85,7 +87,8 @@ public class ContractExecuteInfo implements Serializable {
} }
public String getPubKeyPath() { public String getPubKeyPath() {
if (!isPrivate) return null; if (!isPrivate)
return null;
return pubKeyPath; return pubKeyPath;
} }
@ -136,7 +139,8 @@ public class ContractExecuteInfo implements Serializable {
System.out.println("memory=" + (memory == null ? "null" : memory)); 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) { synchronized (this) {
if (request.seq < lastExeSeq.get()) { if (request.seq < lastExeSeq.get()) {
if (request.getRequestID().endsWith("mul")) { if (request.getRequestID().endsWith("mul")) {

View File

@ -1,5 +1,7 @@
package org.bdware.server; package org.bdware.server;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator; import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*; 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.SslContext;
import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.stream.ChunkedWriteHandler; import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.internal.StringUtil;
import io.prometheus.client.exporter.HTTPServer; import io.prometheus.client.exporter.HTTPServer;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator; 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.CMTables;
import org.bdware.sc.db.KeyValueDBUtil; import org.bdware.sc.db.KeyValueDBUtil;
import org.bdware.sc.db.MultiIndexTimeRocksDBUtil; import org.bdware.sc.db.MultiIndexTimeRocksDBUtil;
import org.bdware.sc.util.ExceptionUtil; import org.bdware.sc.util.ExceptionUtil;
import org.bdware.sdk.consistency.ConsistencyPluginManager;
import org.bdware.server.action.FileActions; import org.bdware.server.action.FileActions;
import org.bdware.server.action.UserManagerAction;
import org.bdware.server.doip.ContractRepositoryMain; import org.bdware.server.doip.ContractRepositoryMain;
import org.bdware.server.http.CMHttpHandler; import org.bdware.server.http.CMHttpHandler;
import org.bdware.server.nodecenter.client.NodeCenterClientHandler; import org.bdware.server.nodecenter.client.NodeCenterClientHandler;
@ -39,19 +49,22 @@ import java.lang.reflect.Field;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
public class CMHttpServer { public class CMHttpServer {
private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class); private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class);
private static final String CONFIG_PATH = "cmconfig.json"; private static final String CONFIG_PATH = "cmconfig.json";
public static EventLoopGroup workerGroup = new NioEventLoopGroup(); public static EventLoopGroup workerGroup = new NioEventLoopGroup();
public static MultiIndexTimeRocksDBUtil nodeLogDB = public static MultiIndexTimeRocksDBUtil nodeLogDB = new MultiIndexTimeRocksDBUtil(
new MultiIndexTimeRocksDBUtil( "./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
"./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
public static URLClassLoader pluginLoader; public static URLClassLoader pluginLoader;
private static SslContext sslContext = null; private static SslContext sslContext = null;
private static CMDConf cmdConf;
final String PATH = "/SCIDE/SCExecutor"; final String PATH = "/SCIDE/SCExecutor";
private final int port; private final int port;
@ -60,58 +73,54 @@ public class CMHttpServer {
} }
private static void configServer(CMDConf cmdConf) throws IOException { private static void configServer(CMDConf cmdConf) throws IOException {
if (cmdConf.disableDoRepo) { GlobalConf.DOAConf doaConf = new GlobalConf.DOAConf();
DoConfig.callContractUsingDOI = false; 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 (!StringUtil.isNullOrEmpty(cmdConf.repoDoid)) {
if (!cmdConf.doipCertPath.isEmpty()) { doaConf.repoDoid = cmdConf.repoDoid;
String[] conf = cmdConf.doipCertPath.split(":");
// DOAConf.certPath = conf[0];
// DOAConf.certPassword = conf[1];
} }
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) { if (cmdConf.withBdledgerServer) {
ContractManager.threadPool.execute( ContractManager.threadPool
() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4)); .execute(() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4));
} }
// 可自动运行bdledger可执行文件也可在shell脚步中运行和停止 // 可自动运行bdledger可执行文件也可在shell脚步中运行和停止
if (!cmdConf.withBdledgerClient.isEmpty()) { if (!cmdConf.withBdledgerClient.isEmpty()) {
ContractManager.scheduledThreadPool.schedule( ContractManager.scheduledThreadPool.schedule(() -> {
() -> { File ledgerClient = new File(cmdConf.withBdledgerClient);
File ledgerClient = new File(cmdConf.withBdledgerClient); LOGGER.debug("canRead=" + ledgerClient.canRead() + " path="
LOGGER.debug("canRead=" + ledgerClient.canRead() + + ledgerClient.getAbsolutePath());
" path=" + ledgerClient.getAbsolutePath()); try {
try { Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath()); } catch (IOException e) {
} catch (IOException e) { LOGGER.warn("start bdledger client failed: " + e.getMessage());
LOGGER.warn("start bdledger client failed: " + e.getMessage()); }
} }, 1, TimeUnit.SECONDS);
},
1, TimeUnit.SECONDS);
} }
if (cmdConf.enableEventPersistence) { if (cmdConf.enableEventPersistence) {
ContractManager.eventPersistenceEnabled = true; ContractManager.eventPersistenceEnabled = true;
} }
if (!cmdConf.enableSsl.isEmpty()) { if (!StringUtil.isNullOrEmpty(cmdConf.enableSsl)) {
try { try {
String[] filePaths = cmdConf.enableSsl.split(":"); String[] filePaths = cmdConf.enableSsl.split(":");
File chainedFile = new File(filePaths[0]), keyFile = new File(filePaths[1]); File chainedFile = new File(filePaths[0]), keyFile = new File(filePaths[1]);
if (chainedFile.exists() && keyFile.exists()) { if (chainedFile.exists() && keyFile.exists()) {
sslContext = sslContext = SslContextBuilder.forServer(chainedFile, keyFile)
SslContextBuilder.forServer(chainedFile, keyFile) .ciphers(null,
.ciphers( (ciphers, defaultCiphers, supportedCiphers) -> defaultCiphers
null, .stream().filter(x -> null != x && !x.contains("RC4"))
(ciphers, defaultCiphers, supportedCiphers) -> .toArray(String[]::new))
defaultCiphers.stream() .build();
.filter(x -> null != x && !x.contains("RC4"))
.toArray(String[]::new))
.build();
LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable()); LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable());
} }
} catch (Exception e) { } catch (Exception e) {
@ -131,10 +140,10 @@ public class CMHttpServer {
// plugins // plugins
CMHttpHandler.wsPluginActions = parseStrAsList(cmdConf.wsPluginActions); CMHttpHandler.wsPluginActions = parseStrAsList(cmdConf.wsPluginActions);
TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins); TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins);
NodeCenterClientHandler.clientToClusterPlugins = parseStrAsList(cmdConf.clientToClusterPlugins); NodeCenterClientHandler.clientToClusterPlugins =
parseStrAsList(cmdConf.clientToClusterPlugins);
org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins); org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins);
if (!StringUtil.isNullOrEmpty(cmdConf.debug)) {
if (!cmdConf.debug.isEmpty()) {
try { try {
String[] classes = cmdConf.debug.split(","); String[] classes = cmdConf.debug.split(",");
for (String clz : classes) { for (String clz : classes) {
@ -149,14 +158,101 @@ public class CMHttpServer {
LOGGER.warn(e.getMessage()); LOGGER.warn(e.getMessage());
} }
} }
if (cmdConf.datachainConf != null) {
GlobalConf.resetDataChain(cmdConf.datachainConf);
}
if (cmdConf.overwrite) { if (cmdConf.overwrite) {
cmdConf.write(CONFIG_PATH); 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) { private static String[] parseStrAsList(String str) {
if (str == null) { if (str == null) {
return new String[]{}; return new String[] {};
} }
return str.split(","); return str.split(",");
} }
@ -164,7 +260,7 @@ public class CMHttpServer {
private static void addDirToPath(String s) { private static void addDirToPath(String s) {
try { try {
LOGGER.info("add to path: " + s); 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 field = ClassLoader.class.getDeclaredField("usr_paths");
field.setAccessible(true); field.setAccessible(true);
String[] path = (String[]) field.get(null); String[] path = (String[]) field.get(null);
@ -189,7 +285,7 @@ public class CMHttpServer {
if (!confFile.exists() && confTemplate.exists()) { if (!confFile.exists() && confTemplate.exists()) {
FileUtils.copyFile(confTemplate, confFile); FileUtils.copyFile(confTemplate, confFile);
} }
CMDConf cmdConf = CMDConf.parseFile(CONFIG_PATH); cmdConf = CMDConf.parseFile(CONFIG_PATH);
// addDirToPath(new File("./dynamicLibrary").getAbsolutePath()); // addDirToPath(new File("./dynamicLibrary").getAbsolutePath());
@ -208,55 +304,50 @@ public class CMHttpServer {
cmdConf.parseArgs(args); cmdConf.parseArgs(args);
configServer(cmdConf); configServer(cmdConf);
// check default key pair // check default key pair
File keyFile = new File("manager.key"); File keyFile = new File("manager.key");
if (keyFile.exists()) { if (keyFile.exists()) {
try { try {
BufferedReader br = new BufferedReader(new FileReader(keyFile)); BufferedReader br = new BufferedReader(new FileReader(keyFile));
String pubKey = br.readLine(); String pubKey = br.readLine();
String nowManager = String nowManager = KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(),
KeyValueDBUtil.instance.getValue( "__NodeManager__");
CMTables.ConfigDB.toString(), "__NodeManager__");
// manager.key is used when node manager isn' set // manager.key is used when node manager isn' set
if (null == nowManager || nowManager.isEmpty()) { if (null == nowManager || nowManager.isEmpty()) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(),
CMTables.ConfigDB.toString(), "__NodeManager__", pubKey); "__NodeManager__", pubKey);
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
CMTables.NodeRole.toString(), pubKey,
"NodeManager,ContractProvider,ContractUser,ContractInstanceManager"); "NodeManager,ContractProvider,ContractUser,ContractInstanceManager");
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
CMTables.NodeTime.toString(),
pubKey,
Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
LOGGER.info("set node manager from manager.key"); LOGGER.info("set node manager from manager.key");
} }
} catch (IOException ignored) { } catch (IOException ignored) {
} }
} }
GlobalConf.initIpPort(cmdConf.ip + ":" + cmdConf.servicePort);
start(cmdConf.ip, cmdConf.servicePort, cmdConf); // 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); LOGGER.info("start server at:" + port);
GlobalConf.initMasterAddress(ip + ":" + (port + 1));
GlobalConf.initIpPort(ip + ":" + port);
LOGGER.debug("dir:" + new File("./").getAbsolutePath()); LOGGER.debug("dir:" + new File("./").getAbsolutePath());
new CMHttpServer(port).start(); new CMHttpServer(port).start();
ContractRepositoryMain.start(); // never reach here!!
} }
/** /**
* port: http & websocket port port+1: tcp port port+2: doip port port+3: prometheus * port: http & websocket port port+1: tcp port port+2: doip port port+3: prometheus
*/ */
private void start() { private void start() {
ConsistencyPluginManager.setContext(new SDKContext());
// EpollEventLoopGroup // EpollEventLoopGroup
// EpollServerSocketChannel // EpollServerSocketChannel
// ContractManager.reconnectPort = (port - 18000) * 30 + 1630; // ContractManager.reconnectPort = (port - 18000) * 30 + 1630;
// if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630; // if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630;
File[] pluginJar = new File("./pluginLib/") File[] pluginJar =
.listFiles(pathname -> pathname.getName().endsWith(".jar")); new File("./pluginLib/").listFiles(pathname -> pathname.getName().endsWith(".jar"));
URL[] urls; URL[] urls;
if (pluginJar != null && pluginJar.length > 0) { if (pluginJar != null && pluginJar.length > 0) {
urls = new URL[pluginJar.length]; urls = new URL[pluginJar.length];
@ -269,7 +360,7 @@ public class CMHttpServer {
} }
} }
} else { } else {
urls = new URL[]{}; urls = new URL[] {};
} }
pluginLoader = new URLClassLoader(urls, CMHttpServer.class.getClassLoader()); pluginLoader = new URLClassLoader(urls, CMHttpServer.class.getClassLoader());
if (port >= 18000 && port < 18100) { if (port >= 18000 && port < 18100) {
@ -277,39 +368,37 @@ public class CMHttpServer {
} else { } else {
ContractManager.cPort = new ContractPort(1615); ContractManager.cPort = new ContractPort(1615);
} }
ContractRepositoryMain.start();
final CMHttpHandler serverHandler = new CMHttpHandler(); final CMHttpHandler serverHandler = new CMHttpHandler();
EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup bossGroup = new NioEventLoopGroup(1);
NettyConnectServerHandler trafficSharp = // NettyConnectServerHandler trafficSharp =
new NettyConnectServerHandler(new AtomicInteger(0)); // new NettyConnectServerHandler(new AtomicInteger(0));
try { try {
ServerBootstrap b1 = new ServerBootstrap(); ServerBootstrap b1 = new ServerBootstrap();
b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b1.group(bossGroup, workerGroup) b1.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.channel(NioServerSocketChannel.class) .localAddress(port).childHandler(new ChannelInitializer<SocketChannel>() {
.localAddress(port) @Override
.childHandler( protected void initChannel(SocketChannel arg0) {
new ChannelInitializer<SocketChannel>() { if (sslContext != null) {
@Override arg0.pipeline().addLast(new OptionalSslHandler(sslContext));
protected void initChannel(SocketChannel arg0) { }
if (sslContext != null) { arg0.pipeline()// .addLast(trafficSharp)
arg0.pipeline().addLast(new OptionalSslHandler(sslContext)); .addLast(new HttpServerCodec())
} .addLast(new HttpObjectAggregator(10 * 1024 * 1024))
arg0.pipeline() .addLast(new WebSocketServerProtocolHandler(PATH, null, true))
.addLast(trafficSharp) .addLast(new ChunkedWriteHandler()).addLast(serverHandler)
.addLast(new HttpServerCodec()) .addLast(new ContractManagerFrameHandler());
.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(); final Channel ch = b1.bind(port).sync().channel();
LOGGER.debug("[CMHttpServer] listen master port at:" + port); LOGGER.debug("[CMHttpServer] listen master port at:" + port);
new HTTPServer(port + 3); new HTTPServer(port + 3);
NetworkManager.instance.initTCP(port + 1, workerGroup); NetworkManager.instance.initTCP(port + 1, workerGroup);
loadStartContractConfiguration();
ch.closeFuture().sync(); ch.closeFuture().sync();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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 @Sharable
public static class NettyConnectServerHandler extends ChannelInboundHandlerAdapter { public static class NettyConnectServerHandler extends ChannelInboundHandlerAdapter {
public NettyConnectServerHandler(AtomicInteger connectNum) { public NettyConnectServerHandler(AtomicInteger connectNum) {}
}
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

View File

@ -21,7 +21,7 @@ public class CongestionControl {
public static int maxMasterServerLoad = 0; public static int maxMasterServerLoad = 0;
// configurations // configurations
public static int maxHttpConnCount = 100; public static int maxHttpConnCount = 1000;
// TODO remove sync call of executeOnOthreNode // TODO remove sync call of executeOnOthreNode
public static void httpCControl(ChannelHandlerContext ctx) { public static void httpCControl(ChannelHandlerContext ctx) {
@ -47,7 +47,6 @@ public class CongestionControl {
jo.addProperty("slaveConn", slaveCounter.get()); jo.addProperty("slaveConn", slaveCounter.get());
jo.addProperty("slaveQueue", masterProxyLoad.get()); jo.addProperty("slaveQueue", masterProxyLoad.get());
jo.addProperty("masterQueue", masterServerLoad.get()); jo.addProperty("masterQueue", masterServerLoad.get());
jo.addProperty("maxConnCount", maxHttpConnCount); jo.addProperty("maxConnCount", maxHttpConnCount);
jo.addProperty("httpMaxQueue", currHttpMax); jo.addProperty("httpMaxQueue", currHttpMax);
jo.addProperty("slaveMaxQueue", maxMasterProxyLoad); jo.addProperty("slaveMaxQueue", maxMasterProxyLoad);

View File

@ -1,6 +0,0 @@
package org.bdware.server;
public class DOAConf {
public static String lhsAddress;
public static String repoDoid;
}

View File

@ -1,6 +1,7 @@
package org.bdware.server; package org.bdware.server;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -66,12 +67,13 @@ public class GRPCPool implements ChainOpener {
RocksDB hash2Data; RocksDB hash2Data;
private String dataNodeIP; private String dataNodeIP;
private int dataNodePort; 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() { GRPCPool() {
logIndex = new LenVarTimeSerialIndex("./log/hashToData.index", 20); logIndex = new LenVarTimeSerialIndex("./" + DBDir + "/hashToData.index", 20);
requestID2Hash = openDB("./log/requestIDToHash", "req2Hash"); requestID2Hash = openDB("./" + DBDir + "/requestIDToHash", "req2Hash");
hash2Data = openDB("./log/hashToData", "hash2Data"); hash2Data = openDB("./" + DBDir + "/hashToData", "hash2Data");
contractName2LogIndex = new HashMap<>(); contractName2LogIndex = new HashMap<>();
instances = new ArrayList<>(); instances = new ArrayList<>();
for (int i = 0; i < WORKER_COUNT; ++i) { 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("DOI", "86.5000.470/" + sucFix);
params.put("Description", "DataWare Contract Info"); params.put("Description", "DataWare Contract Info");
params.put("Interface", ""); params.put("Interface", "");
params.put( params.put("Address",
"Address",
"http://39.106.6.6:8080/SCIDE/CMManager?action=queryDataByHash&hash=" + hash); "http://39.106.6.6:8080/SCIDE/CMManager?action=queryDataByHash&hash=" + hash);
byte[] signature; byte[] signature;
try { try {
signature = signature = SM2Util.sign(GlobalConf.instance.keyPair.getPrivateKeyParameter(),
SM2Util.sign( origin.getBytes());
GlobalConf.instance.keyPair.getPrivateKeyParameter(),
origin.getBytes());
} catch (Exception e) { } catch (Exception e) {
signature = "no signature".getBytes(); signature = "no signature".getBytes();
} }
@ -129,14 +128,8 @@ public class GRPCPool implements ChainOpener {
HttpPostForm.postForm(url, params); HttpPostForm.postForm(url, params);
} }
private static Line wrapperLine( private static Line wrapperLine(OnHashCallback cb, String from, String to, String data,
OnHashCallback cb, String requestID, String contractName, String namedLedger) {
String from,
String to,
String data,
String requestID,
String contractName,
String namedLedger) {
Line l = new Line(); Line l = new Line();
l.cb = cb; l.cb = cb;
l.from = from; l.from = from;
@ -149,24 +142,15 @@ public class GRPCPool implements ChainOpener {
} }
public static void updateContractInfo(String contractName, long times, long traffic) { public static void updateContractInfo(String contractName, long times, long traffic) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ContractInfo.toString(), contractName + "-Times",
CMTables.ContractInfo.toString(), contractName + "-Times", times + ""); times + "");
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ContractInfo.toString(),
CMTables.ContractInfo.toString(), contractName + "-Traffic", traffic + ""); contractName + "-Traffic", traffic + "");
} }
public static void addLocalContractLog( public static void addLocalContractLog(String action, String contractID, String contractName,
String action, String pubKey, String function, long startTime, long costTime, long totalGas,
String contractID, long executionGas, long extraGas, Map<String, String> logType) {
String contractName,
String pubKey,
String function,
long startTime,
long costTime,
long totalGas,
long executionGas,
long extraGas,
Map<String, String> logType) {
// if (disable) return; // if (disable) return;
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
jo.addProperty("action", action); jo.addProperty("action", action);
@ -190,8 +174,7 @@ public class GRPCPool implements ChainOpener {
} }
@Override @Override
protected void finalize() { protected void finalize() {}
}
public void reloadConf(String confFile) { public void reloadConf(String confFile) {
loadConf(confFile); loadConf(confFile);
@ -221,25 +204,14 @@ public class GRPCPool implements ChainOpener {
} }
@Override @Override
public void writeToChain( public void writeToChain(OnHashCallback callback, String from, String to, String data,
OnHashCallback callback, String requestID, String namedLedger) {
String from,
String to,
String data,
String requestID,
String namedLedger) {
writeToChainWithContract(callback, from, to, data, requestID, null, namedLedger); writeToChainWithContract(callback, from, to, data, requestID, null, namedLedger);
} }
@Override @Override
public void writeToChainWithContract( public void writeToChainWithContract(OnHashCallback cb, String from, String to, String data,
OnHashCallback cb, String requestID, String contractName, String namedLedger) {
String from,
String to,
String data,
String requestID,
String contractName,
String namedLedger) {
Line l = wrapperLine(cb, from, to, data, requestID, contractName, namedLedger); Line l = wrapperLine(cb, from, to, data, requestID, contractName, namedLedger);
WorkerThread t = getNextWorkerThread(); WorkerThread t = getNextWorkerThread();
if (t != null) { 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 @Override
public void reRegister(String doid) { public void reRegister(String doid) {
//TODO just success // TODO just success
// throw new IllegalStateException("TODO"); // throw new IllegalStateException("TODO");
} }
@ -258,13 +251,8 @@ public class GRPCPool implements ChainOpener {
} }
@Override @Override
public void writeContractResultToLocalAndLedger( public void writeContractResultToLocalAndLedger(String result, ContractClient client,
String result, ContractRequest contractRequest, OnHashCallback cb, long start, long time) {
ContractClient client,
ContractRequest contractRequest,
OnHashCallback cb,
long start,
long time) {
ContractResultLine l = new ContractResultLine(); ContractResultLine l = new ContractResultLine();
l.client = client; l.client = client;
l.contractRequest = contractRequest; l.contractRequest = contractRequest;
@ -321,21 +309,14 @@ public class GRPCPool implements ChainOpener {
ret.put("action", "onQueryHashByReqID"); ret.put("action", "onQueryHashByReqID");
if (args.has("requestID")) { if (args.has("requestID")) {
try { try {
ret.put( ret.put("data", new String(
"data", requestID2Hash.get(args.get("requestID").getAsString().getBytes())));
new String(
requestID2Hash.get(
args.get("requestID").getAsString().getBytes())));
} catch (Exception e) { } catch (Exception e) {
ret.put( ret.put("data", "Match failed! " + "Can't find hash by this requestID "
"data", + args.get("requestID").getAsString());
"Match failed! "
+ "Can't find hash by this requestID "
+ args.get("requestID").getAsString());
} }
} else { } else {
ret.put( ret.put("data",
"data",
"Can't find hash by this requestID " + args.get("requestID").getAsString()); "Can't find hash by this requestID " + args.get("requestID").getAsString());
} }
resultCallback.onResult(JsonUtil.toJson(ret)); resultCallback.onResult(JsonUtil.toJson(ret));
@ -362,13 +343,16 @@ public class GRPCPool implements ChainOpener {
index = createIndexIfNotExist(args.get("contractName").getAsString()); index = createIndexIfNotExist(args.get("contractName").getAsString());
} }
//根据事务类型查找 // 根据事务类型查找
else if (args.has("type")) { else if (args.has("type")) {
index = createIndexIfNotExist(args.get("type").getAsString()); index = createIndexIfNotExist(args.get("type").getAsString());
} else index = logIndex; } else
index = logIndex;
List<byte[]> result; List<byte[]> result;
if (offset < 0) result = index.requestLast(count); if (offset < 0)
else result = index.request(offset, count); result = index.requestLast(count);
else
result = index.request(offset, count);
for (byte[] data : result) { for (byte[] data : result) {
long date = HashUtil.bytes2Long(data); long date = HashUtil.bytes2Long(data);
String hash = HashUtil.byteArray2Str(data, 8); String hash = HashUtil.byteArray2Str(data, 8);
@ -397,12 +381,10 @@ public class GRPCPool implements ChainOpener {
ret.put("action", "onQueryDataByHash"); ret.put("action", "onQueryDataByHash");
if (args.has("hash")) { if (args.has("hash")) {
try { try {
JsonObject jo = JsonObject jo = JsonParser
JsonParser.parseString( .parseString(new String(
new String( hash2Data.get(args.get("hash").getAsString().getBytes())))
hash2Data.get( .getAsJsonObject();
args.get("hash").getAsString().getBytes())))
.getAsJsonObject();
jo.addProperty("action", "onQueryDataByHash"); jo.addProperty("action", "onQueryDataByHash");
resultCallback.onResult(JsonUtil.toJson(jo)); resultCallback.onResult(JsonUtil.toJson(jo));
return; return;
@ -435,19 +417,16 @@ public class GRPCPool implements ChainOpener {
ret.put("action", "onQueryDataByHash"); ret.put("action", "onQueryDataByHash");
if (args.has("hash")) { if (args.has("hash")) {
try { try {
JsonObject jo = JsonObject jo = JsonParser
JsonParser.parseString( .parseString(new String(
new String( hash2Data.get(args.get("hash").getAsString().getBytes())))
hash2Data.get( .getAsJsonObject();
args.get("hash").getAsString().getBytes())))
.getAsJsonObject();
jo.addProperty("action", "onQueryDataByHash"); jo.addProperty("action", "onQueryDataByHash");
resultCallback.onResult(JsonUtil.toJson(jo)); resultCallback.onResult(JsonUtil.toJson(jo));
return; return;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ret.put( ret.put("data",
"data",
"!!! " + "Can't find data by hash " + args.get("hash").getAsString()); "!!! " + "Can't find data by hash " + args.get("hash").getAsString());
} }
} else { } else {
@ -463,7 +442,8 @@ public class GRPCPool implements ChainOpener {
LenVarTimeSerialIndex index = null; LenVarTimeSerialIndex index = null;
if (args.has("contractName")) if (args.has("contractName"))
index = createIndexIfNotExist(args.get("contractName").getAsString()); index = createIndexIfNotExist(args.get("contractName").getAsString());
if (index == null) index = logIndex; if (index == null)
index = logIndex;
ret.put("count", index.size() + ""); ret.put("count", index.size() + "");
resultCallback.onResult(JsonUtil.toJson(ret)); resultCallback.onResult(JsonUtil.toJson(ret));
} }
@ -490,11 +470,8 @@ public class GRPCPool implements ChainOpener {
obj.put("hash", hash); obj.put("hash", hash);
obj.put("date", date); obj.put("date", date);
try { try {
obj.put( obj.put("data", JsonParser.parseString(
"data", new String(hash2Data.get(args.get("hash").getAsString().getBytes()))));
JsonParser.parseString(
new String(
hash2Data.get(args.get("hash").getAsString().getBytes()))));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -525,11 +502,8 @@ public class GRPCPool implements ChainOpener {
obj.put("hash", hash); obj.put("hash", hash);
obj.put("date", date); obj.put("date", date);
try { try {
obj.put( obj.put("data", shortStr(
"data", new String(hash2Data.get(args.get("hash").getAsString().getBytes()))));
shortStr(
new String(
hash2Data.get(args.get("hash").getAsString().getBytes()))));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -539,8 +513,10 @@ public class GRPCPool implements ChainOpener {
} }
private String shortStr(String str) { private String shortStr(String str) {
if (str != null && str.length() > 50) return str.substring(0, 50); if (str != null && str.length() > 50)
else return str; return str.substring(0, 50);
else
return str;
} }
@Action(async = true, userPermission = 1L << 24) @Action(async = true, userPermission = 1L << 24)
@ -553,7 +529,7 @@ public class GRPCPool implements ChainOpener {
RocksIterator iter = hash2Data.newIterator(); RocksIterator iter = hash2Data.newIterator();
iter.seekToFirst(); iter.seekToFirst();
List<String> hashes = new ArrayList<>(); List<String> hashes = new ArrayList<>();
for (; iter.isValid(); ) { for (; iter.isValid();) {
iter.value(); iter.value();
hashes.add(new String(iter.value())); hashes.add(new String(iter.value()));
iter.next(); iter.next();
@ -562,7 +538,8 @@ public class GRPCPool implements ChainOpener {
List<Long> times = new ArrayList<>(); List<Long> times = new ArrayList<>();
for (String str : hashes) { for (String str : hashes) {
Long time = getDataTime(str); Long time = getDataTime(str);
if (time < 0) continue; if (time < 0)
continue;
times.add(time); times.add(time);
memoryIndex.put(time, str); memoryIndex.put(time, str);
} }
@ -580,8 +557,7 @@ public class GRPCPool implements ChainOpener {
try { try {
String obj = new String(hash2Data.get(key.getBytes())); String obj = new String(hash2Data.get(key.getBytes()));
Map<String, String> parsedObj = Map<String, String> parsedObj =
JsonUtil.fromJson(obj, new TypeToken<Map<String, String>>() { JsonUtil.fromJson(obj, new TypeToken<Map<String, String>>() {}.getType());
}.getType());
String data = parsedObj.get("data"); String data = parsedObj.get("data");
int len = "1575452533275".length(); int len = "1575452533275".length();
@ -598,9 +574,8 @@ public class GRPCPool implements ChainOpener {
private synchronized LenVarTimeSerialIndex createIndexIfNotExist(String contractName) { private synchronized LenVarTimeSerialIndex createIndexIfNotExist(String contractName) {
LenVarTimeSerialIndex index = contractName2LogIndex.get(contractName); LenVarTimeSerialIndex index = contractName2LogIndex.get(contractName);
if (index == null) { if (index == null) {
index = index = new LenVarTimeSerialIndex(
new LenVarTimeSerialIndex( "./" + DBDir + "/hashByContract/" + contractName + ".index", 20);
"./log/hashByContract/" + contractName + ".index", 20);
contractName2LogIndex.put(contractName, index); contractName2LogIndex.put(contractName, index);
} }
return index; return index;
@ -625,8 +600,7 @@ public class GRPCPool implements ChainOpener {
static SecureRandom random = new SecureRandom((System.currentTimeMillis() + "").getBytes()); static SecureRandom random = new SecureRandom((System.currentTimeMillis() + "").getBytes());
boolean exit = false; boolean exit = false;
WorkerThread() { WorkerThread() {}
}
public static byte[] getRandomByte(int len) { public static byte[] getRandomByte(int len) {
Random random = new Random(); Random random = new Random();
@ -640,7 +614,7 @@ public class GRPCPool implements ChainOpener {
public void run() { public void run() {
while (true) { while (true) {
// System.out.println("[GRPCPool] for circleing"); // System.out.println("[GRPCPool] for circleing");
Line l = uploadContent.poll(); Line l = uploadContent.poll();
if (l != null) { if (l != null) {
if (l instanceof ContractResultLine) { if (l instanceof ContractResultLine) {
@ -689,65 +663,50 @@ public class GRPCPool implements ChainOpener {
ListenableFuture<LedgerProto.SendTransactionResponse> ret2; ListenableFuture<LedgerProto.SendTransactionResponse> ret2;
if (l.ledgerName != null && !l.ledgerName.equals("")) { if (l.ledgerName != null && !l.ledgerName.equals("")) {
ret2 = ret2 = client.sendTransaction(l.ledgerName, CommonProto.TransactionType.MESSAGE,
client.sendTransaction( l.from, random.nextLong(), l.to,
l.ledgerName, l.data.getBytes(StandardCharsets.UTF_8));
CommonProto.TransactionType.MESSAGE,
l.from,
random.nextLong(),
l.to,
l.data.getBytes(StandardCharsets.UTF_8));
} else { } else {
ret2 = ret2 = client.sendTransaction(LEDGER_NAME, CommonProto.TransactionType.MESSAGE,
client.sendTransaction( l.from, random.nextLong(), l.to,
LEDGER_NAME, l.data.getBytes(StandardCharsets.UTF_8));
CommonProto.TransactionType.MESSAGE,
l.from, random.nextLong(),
l.to,
l.data.getBytes(StandardCharsets.UTF_8));
} }
if (ret2 != null) if (ret2 != null)
ret2.addListener( ret2.addListener(new Runnable() {
new Runnable() { @Override
@Override public void run() {
public void run() { try {
try { ByteString hash = ret2.get().getHash();
ByteString hash = ret2.get().getHash(); String hashStr;
String hashStr; if (hash == null) {
if (hash == null) { hash = ByteString.copyFrom(getRandomByte(20));
hash = ByteString.copyFrom(getRandomByte(20)); hashStr = HashUtil.byteArray2Str(hash.toByteArray());
hashStr = HashUtil.byteArray2Str(hash.toByteArray()); LOGGER.info("generate fake hash: " + hashStr);
LOGGER.info("generate fake hash: " + hashStr); } else {
} else { hashStr = HashUtil.byteArray2Str(hash.toByteArray());
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());
}
} }
}, if (!l.data.startsWith("MockTransaction_")) {
executorService); 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 }); // ByteString hash = ByteString.copyFrom(new byte[] { 1, 2, 2, 2 });
} catch (Exception e) { } catch (Exception e) {
@ -757,8 +716,8 @@ public class GRPCPool implements ChainOpener {
} }
} }
public Map<String, String> analysisLocalContractLog( public Map<String, String> analysisLocalContractLog(String data2, ContractRequest c,
String data2, ContractRequest c, String detail) { String detail) {
// 本地日志 // 本地日志
JsonObject jo = JsonParser.parseString(data2).getAsJsonObject(); JsonObject jo = JsonParser.parseString(data2).getAsJsonObject();
Map<String, String> logType = new HashMap<>(); Map<String, String> logType = new HashMap<>();
@ -793,7 +752,7 @@ public class GRPCPool implements ChainOpener {
logType.put("branch", "branch log failure"); logType.put("branch", "branch log failure");
} }
} else if (info.equals("Result")) { } else if (info.equals("Result")) {
s = jo.get("result").getAsString(); s = jo.get("result").toString();
s = JsonUtil.toJson(s); s = JsonUtil.toJson(s);
if (s.length() > 2) { if (s.length() > 2) {
s = s.substring(1, s.length() - 1); s = s.substring(1, s.length() - 1);
@ -822,7 +781,7 @@ public class GRPCPool implements ChainOpener {
ContractClient client = l.client; ContractClient client = l.client;
cr = JsonUtil.fromJson(l.data, ContractResult.class); cr = JsonUtil.fromJson(l.data, ContractResult.class);
// updateContractInfo( // updateContractInfo(
// client.getContractName(), client.getTimes(), client.getTraffic()); // client.getContractName(), client.getTimes(), client.getTraffic());
String detail = client.getLogType(l.contractRequest.getAction()); String detail = client.getLogType(l.contractRequest.getAction());
Map<String, String> logType = Map<String, String> logType =
analysisLocalContractLog(l.data, l.contractRequest, detail); analysisLocalContractLog(l.data, l.contractRequest, detail);
@ -848,32 +807,14 @@ public class GRPCPool implements ChainOpener {
// LOGGER.debug("type: " + JsonUtil.toJson(logType) + " | detail: " + // LOGGER.debug("type: " + JsonUtil.toJson(logType) + " | detail: " +
// detail); // detail);
addLocalContractLog( addLocalContractLog("executeContract", contractID, contractName,
"executeContract", l.contractRequest.getPublicKey(), l.contractRequest.getAction(), l.start,
contractID, l.costTime, cr.totalGas, cr.executionGas, cr.extraGas, logType);
contractName,
l.contractRequest.getPublicKey(),
l.contractRequest.getAction(),
l.start,
l.costTime,
cr.totalGas,
cr.executionGas,
cr.extraGas,
logType);
// 929 1277 1448 1417 1507 // 929 1277 1448 1417 1507
if (l.contractRequest.fromContract != null) { if (l.contractRequest.fromContract != null) {
addLocalContractLog( addLocalContractLog("executeContract", contractID, contractName,
"executeContract", l.contractRequest.fromContract, l.contractRequest.getAction(), l.start,
contractID, l.costTime, cr.totalGas, cr.executionGas, cr.extraGas, logType);
contractName,
l.contractRequest.fromContract,
l.contractRequest.getAction(),
l.start,
l.costTime,
cr.totalGas,
cr.executionGas,
cr.extraGas,
logType);
} }
// 1027 1563 1461 1519 1501 // 1027 1563 1461 1519 1501
if (toBDContract || toNamedLedger) { if (toBDContract || toNamedLedger) {
@ -888,29 +829,17 @@ public class GRPCPool implements ChainOpener {
// 1025 1422 1497 1447 1445 // 1025 1422 1497 1447 1445
if (toBDContract) { if (toBDContract) {
Line l1 = Line l1 = wrapperLine(l.cb, l.contractRequest.getPublicKey(), "executeContract",
wrapperLine( JsonUtil.toJson(logType), l.contractRequest.getRequestID(),
l.cb, contractName, "");
l.contractRequest.getPublicKey(),
"executeContract",
JsonUtil.toJson(logType),
l.contractRequest.getRequestID(),
contractName,
"");
writeChain(l1); writeChain(l1);
} }
// 1424 1421 1443 1160 1611 // 1424 1421 1443 1160 1611
if (toNamedLedger) { if (toNamedLedger) {
for (String name : namedLedger) { for (String name : namedLedger) {
Line l2 = Line l2 = wrapperLine(l.cb, l.contractRequest.getPublicKey(),
wrapperLine( "executeContract", JsonUtil.toJson(logType),
l.cb, l.contractRequest.getRequestID(), contractName, name);
l.contractRequest.getPublicKey(),
"executeContract",
JsonUtil.toJson(logType),
l.contractRequest.getRequestID(),
contractName,
name);
writeChain(l2); writeChain(l2);
} }
} }

View File

@ -17,13 +17,23 @@ import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util; import org.zz.gmhelper.SM2Util;
import java.io.File; import java.io.File;
import java.net.URL;
import java.util.logging.Level; import java.util.logging.Level;
public class GlobalConf { public class GlobalConf {
private static final Logger LOGGER = LogManager.getLogger(GlobalConf.class); private static final Logger LOGGER = LogManager.getLogger(GlobalConf.class);
public static GlobalConf instance = init(); 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 String projectDir; // 从configDB读取默认./ProjectDir/
// 对于public-->SCIDEProjectDir/public/ // 对于public-->SCIDEProjectDir/public/
// 对于private-->SCIDEProjectDir/private/4567856786789/ // 对于private-->SCIDEProjectDir/private/4567856786789/
@ -40,23 +50,24 @@ public class GlobalConf {
public String publicCompiledDir; public String publicCompiledDir;
public String privateDir; public String privateDir;
public String privateCompiledDir; public String privateCompiledDir;
public String bcoDir;
public String memoryDir; public String memoryDir;
public String masterAddress; // public String masterAddress;
public String ipPort; public String ipPort;
public boolean isLAN = true; public boolean isLAN = true;
private String nodeCenterUrl; // 从ConfigDB读 private String nodeCenterUrl; // 从ConfigDB读
private String nodeCenterWSUrl;
private static GlobalConf init() { private static GlobalConf init() {
java.util.logging.Logger.getLogger(org.bdware.bdledger.api.grpc.Client.class.getName()) java.util.logging.Logger.getLogger(org.bdware.bdledger.api.grpc.Client.class.getName())
.setLevel(Level.OFF); .setLevel(Level.OFF);
Configurator.setLevel( Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
"io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
org.apache.logging.log4j.Level.OFF); org.apache.logging.log4j.Level.OFF);
Configurator.setLevel( Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
"io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
org.apache.logging.log4j.Level.OFF); org.apache.logging.log4j.Level.OFF);
Configurator.setLevel( Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
"io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
org.apache.logging.log4j.Level.OFF); org.apache.logging.log4j.Level.OFF);
KeyValueDBUtil.setupCM(); KeyValueDBUtil.setupCM();
@ -66,29 +77,19 @@ public class GlobalConf {
try { try {
String dbName = CMTables.ConfigDB.toString(); 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")) { if (!KeyValueDBUtil.instance.getKeys(dbName).contains("hasInited")) {
KeyValueDBUtil.instance.setValue(dbName, "hasInited", "true"); KeyValueDBUtil.instance.setValue(dbName, "hasInited", "true");
KeyValueDBUtil.instance.setValue(dbName, "projectDir",
KeyValueDBUtil.instance.setValue( new File("./BDWareProjectDir/").getAbsolutePath());
dbName, "projectDir", new File("./BDWareProjectDir/").getAbsolutePath()); KeyValueDBUtil.instance.setValue(dbName, "ADSPDir",
KeyValueDBUtil.instance.setValue(
dbName,
"ADSPDir",
new File("./BDWareProjectDir/ADSPDir/").getAbsolutePath()); new File("./BDWareProjectDir/ADSPDir/").getAbsolutePath());
File f = new File("./yjs.jar"); File f = new File("./yjs.jar");
if (f.exists()) { if (f.exists()) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
dbName, "yjsPath", new File("./yjs.jar").getAbsolutePath()); new File("./yjs.jar").getAbsolutePath());
} else { } else {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
dbName, "yjsPath", new File("./cp/yjs.jar").getAbsolutePath()); new File("./cp/yjs.jar").getAbsolutePath());
} }
conf.keyPairStr = SM2Util.generateSM2KeyPair().toJson(); conf.keyPairStr = SM2Util.generateSM2KeyPair().toJson();
@ -104,43 +105,27 @@ public class GlobalConf {
// long time = System.currentTimeMillis()+15811200000; // long time = System.currentTimeMillis()+15811200000;
String licence = String licence =
ByteHexUtil.encode( ByteHexUtil
SM2Util.encrypt( .encode(SM2Util.encrypt(conf.keyPair.getPublicKey(),
conf.keyPair.getPublicKey(), (HardwareInfo.getCPUID() + "=="
(HardwareInfo.getCPUID() + (System.currentTimeMillis() + 15811200000L))
+ "==" .getBytes()));
+ (System.currentTimeMillis()
+ 15811200000L))
.getBytes()));
LOGGER.debug("licence:" + licence); LOGGER.debug("licence:" + licence);
LOGGER.debug( LOGGER.debug(String.format("deviceName %s license %s %s==%d", deviceName, licence,
String.format( HardwareInfo.getCPUID(), (System.currentTimeMillis() + 15811200000L)));
"deviceName %s license %s %s==%d",
deviceName,
licence,
HardwareInfo.getCPUID(),
(System.currentTimeMillis() + 15811200000L)));
KeyValueDBUtil.instance.setValue(dbName, "licence", licence); KeyValueDBUtil.instance.setValue(dbName, "licence", licence);
KeyValueDBUtil.instance.setValue(dbName, "nodeName", deviceName.substring(0, 10)); KeyValueDBUtil.instance.setValue(dbName, "nodeName", deviceName.substring(0, 10));
KeyValueDBUtil.instance.setValue(dbName, "masterAddress", "null"); KeyValueDBUtil.instance.setValue(dbName, "masterAddress", "null");
KeyValueDBUtil.instance.setValue(dbName, "ipPort", "null"); KeyValueDBUtil.instance.setValue(dbName, "ipPort", "null");
JsonObject doipConfig = new JsonObject(); KeyValueDBUtil.instance.setValue(dbName, "nodeCenter", "ws://127.0.0.1:18005");
doipConfig.addProperty("lhsAddress", "tcp://127.0.0.1:2641"); KeyValueDBUtil.instance.setValue(dbName, "datachainConf",
doipConfig.addProperty("repoDoid", "bdware.trusted.test/Repo." + conf.keyPair.getPublicKeyStr().substring(0, 6)); "39.104.70.160:18091\n" + "47.98.247.70:18091\n" + "47.98.248.208:18091\n"
KeyValueDBUtil.instance.setValue(dbName, "doipConfig", doipConfig.toString()); + "39.104.77.165:18091\n" + "47.98.249.131:18091");
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");
} }
conf.projectDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir"); conf.projectDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir");
conf.bcoDir = conf.projectDir + "/bco";
conf.publicDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/public"; conf.publicDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/public";
conf.ADSPDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/ADSPDir"; conf.ADSPDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir") + "/ADSPDir";
conf.publicCompiledDir = conf.publicCompiledDir =
@ -155,15 +140,18 @@ public class GlobalConf {
conf.name = KeyValueDBUtil.instance.getValue(dbName, "nodeName"); conf.name = KeyValueDBUtil.instance.getValue(dbName, "nodeName");
conf.ipPort = KeyValueDBUtil.instance.getValue(dbName, "ipPort"); conf.ipPort = KeyValueDBUtil.instance.getValue(dbName, "ipPort");
conf.isLAN = "true".equals(KeyValueDBUtil.instance.getValue(dbName, "isLAN")); 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.nodeCenterUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenter");
conf.nodeCenterWSUrl = KeyValueDBUtil.instance.getValue(dbName, "nodeCenterWS");
conf.peerID = KeyValueDBUtil.instance.getValue(dbName, "peerID"); conf.peerID = KeyValueDBUtil.instance.getValue(dbName, "peerID");
conf.keyPairStr = KeyValueDBUtil.instance.getValue(dbName, "keyPairStr"); conf.keyPairStr = KeyValueDBUtil.instance.getValue(dbName, "keyPairStr");
// logger.debug("keyPairStr" + conf.keyPairStr); // logger.debug("keyPairStr" + conf.keyPairStr);
conf.keyPair = SM2KeyPair.fromJson(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) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
e.printStackTrace();
LOGGER.debug(ExceptionUtil.exceptionToString(e)); LOGGER.debug(ExceptionUtil.exceptionToString(e));
} }
// verifyLicence(conf); // verifyLicence(conf);
@ -204,9 +192,8 @@ public class GlobalConf {
LOGGER.debug("[GlobalConf] ProjectDir Path:" + new File(conf.projectDir).getAbsolutePath()); 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.publicDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.ADSPDir).getAbsolutePath()); LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.ADSPDir).getAbsolutePath());
LOGGER.debug( LOGGER.debug("[GlobalConf] publicDirCompiled Path:"
"[GlobalConf] publicDirCompiled Path:" + new File(conf.publicCompiledDir).getAbsolutePath());
+ new File(conf.publicCompiledDir).getAbsolutePath());
LOGGER.debug( LOGGER.debug(
"[GlobalConf] PersonalDir Path:" + new File(conf.privateDir).getAbsolutePath()); "[GlobalConf] PersonalDir Path:" + new File(conf.privateDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] memorytDir Path:" + new File(conf.memoryDir).getAbsolutePath()); LOGGER.debug("[GlobalConf] memorytDir Path:" + new File(conf.memoryDir).getAbsolutePath());
@ -221,23 +208,19 @@ public class GlobalConf {
return conf; return conf;
} }
private static void loadDOAConfig(String val) { private static void loadDOAConfig(DOAConf doaConf, String val) {
try { try {
JsonObject doipConfig = JsonUtil.parseStringAsJsonObject(val); if (val != null && !"null".equals(val)) {
DOAConf.lhsAddress = doipConfig.get("lhsAddress").getAsString(); JsonObject doipConfig = JsonUtil.parseStringAsJsonObject(val);
DOAConf.repoDoid = doipConfig.get("repoDoid").getAsString(); doaConf.lhsAddress = doipConfig.get("lhsAddress").getAsString();
//TODO 这个怎么获取要通过啥来获取自己的repoDoid呢 doaConf.repoDoid = doipConfig.get("repoDoid").getAsString();
//所以现在的lhsAddress暂时没起作用 doaConf.doipAddress = doipConfig.get("doipAddress").getAsString();
doaConf.repoName = doipConfig.get("repoName").getAsString();
}
} catch (Exception e) { } 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) { private static void deleteJelck(File file) {
if (file.exists()) { if (file.exists()) {
@ -247,16 +230,17 @@ public class GlobalConf {
private static void verifyLicence(GlobalConf conf) { private static void verifyLicence(GlobalConf conf) {
try { try {
// String pubkey = // String pubkey =
// "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi" + // "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi"
// "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" + // +
// "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" + // "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" +
// "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" + // "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" +
// "ZjFhZjFkYzBjODUxZjRlNDg2ZWIyOTM5NDI3MDc4MjA5NDg5ODliODVhZDNlOGJlNWJiYWEzZDUyMWU2" + // "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" +
// "MjdmZjE3NGY4Y2ZlZDk3NTY4OWNlNDEzOGYyMTgyOWIwMDVmMzE0YjM3MmNlZmFkZjBkNmUyOTY4ZGUz" + // "ZjFhZjFkYzBjODUxZjRlNDg2ZWIyOTM5NDI3MDc4MjA5NDg5ODliODVhZDNlOGJlNWJiYWEzZDUyMWU2" +
// "ZmFlNGUxNTFkMWFmNWE4Mjc4ZjQ2MDI5ODBjY2JkMDM0ZDE0YWRjZDk1ZjI1MjY3NmRlODRjYzdkNzU5" + // "MjdmZjE3NGY4Y2ZlZDk3NTY4OWNlNDEzOGYyMTgyOWIwMDVmMzE0YjM3MmNlZmFkZjBkNmUyOTY4ZGUz" +
// "NGYyYTAxMTliYWJmYjgyMGRjMWNjZWZjNThjNWUwYWRjMDQyM2MzYzA1ODNhZTU1MWZlN2Y5YTYwYjkx" + // "ZmFlNGUxNTFkMWFmNWE4Mjc4ZjQ2MDI5ODBjY2JkMDM0ZDE0YWRjZDk1ZjI1MjY3NmRlODRjYzdkNzU5" +
// "Zjg2YWViNDNlMzU0NzlhYWI5YmFjOTAwN2IsMTAwMDEsMA=="; // "NGYyYTAxMTliYWJmYjgyMGRjMWNjZWZjNThjNWUwYWRjMDQyM2MzYzA1ODNhZTU1MWZlN2Y5YTYwYjkx" +
// "Zjg2YWViNDNlMzU0NzlhYWI5YmFjOTAwN2IsMTAwMDEsMA==";
LOGGER.debug("beforedecode:" + conf.licence); LOGGER.debug("beforedecode:" + conf.licence);
if (conf.licence != null && conf.licence.length() > 8) { if (conf.licence != null && conf.licence.length() > 8) {
@ -264,11 +248,8 @@ public class GlobalConf {
try { try {
// byte[] arr = key.encode(new BASE64Decoder().decodeBuffer(conf.licence)); // byte[] arr = key.encode(new BASE64Decoder().decodeBuffer(conf.licence));
// String actualKey = new String(arr); // String actualKey = new String(arr);
String arr = String arr = new String(SM2Util.decrypt(conf.keyPair.getPrivateKeyParameter(),
new String( ByteHexUtil.decode(conf.licence)));
SM2Util.decrypt(
conf.keyPair.getPrivateKeyParameter(),
ByteHexUtil.decode(conf.licence)));
LOGGER.debug("[GlobalConf] actualKey:" + arr); LOGGER.debug("[GlobalConf] actualKey:" + arr);
@ -322,25 +303,30 @@ public class GlobalConf {
} }
} }
public static void initMasterAddress(String val) { // public static void initMasterAddress(String val) {
if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1")) { // if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1"))
resetMasterAddress(val); // {
} // 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) { // public static boolean resetMasterAddress(String val) {
try { // try {
instance.masterAddress = val; // instance.masterAddress = val;
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "masterAddress", val); // KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "masterAddress", val);
NetworkManager.instance.reInitNodeCenter(); // NetworkManager.instance.reInitNodeCenter();
ContractRepositoryMain.start(); // return true;
return true; // } catch (Exception e) {
} catch (Exception e) { // LOGGER.error(e.getMessage());
LOGGER.error(e.getMessage()); // LOGGER.debug(ExceptionUtil.exceptionToString(e));
LOGGER.debug(ExceptionUtil.exceptionToString(e)); // return false;
return false; // }
} // }
}
public static boolean resetNodeCenter(String val) { public static boolean resetNodeCenter(String val) {
try { 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) { public static boolean resetDeviceName(String val) {
try { try {
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "nodeName", val); KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "nodeName", val);
@ -410,7 +409,7 @@ public class GlobalConf {
} }
public static void initIpPort(String val) { 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); resetIpPort(val);
} }
} }
@ -421,11 +420,11 @@ public class GlobalConf {
return true; return true;
} }
public static boolean resetDOIPConfig(String val) { public static boolean resetDOAConfig(String val) {
try { try {
loadDOAConfig(val); loadDOAConfig(instance.doaConf, val);
//TODO get prefix if needed!
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "doipConfig", val); KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), "doipConfig", val);
ContractRepositoryMain.start();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
LOGGER.debug(ExceptionUtil.exceptionToString(e)); LOGGER.debug(ExceptionUtil.exceptionToString(e));
@ -460,6 +459,10 @@ public class GlobalConf {
return instance.keyPair.getPublicKeyStr(); return instance.keyPair.getPublicKeyStr();
} }
public static String getNodeCenterWSUrl() {
return instance.nodeCenterWSUrl;
}
public boolean resetLicenceInternal(String licence2) { public boolean resetLicenceInternal(String licence2) {
licence2 = licence2.replaceAll(" ", "+"); licence2 = licence2.replaceAll(" ", "+");
LOGGER.info("resetLicence:" + licence2); LOGGER.info("resetLicence:" + licence2);

View 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

View File

@ -17,7 +17,7 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
public class CMLogAction { public class CMLogAction {
// static SocketGet get; // static SocketGet get;
@Action(async = true, userPermission = 0) @Action(async = true, userPermission = 0)
public void setLogStage(JsonObject json, ResultCallback result) { public void setLogStage(JsonObject json, ResultCallback result) {
@ -49,7 +49,8 @@ public class CMLogAction {
if (json.has("contractName")) { if (json.has("contractName")) {
contractName = json.get("contractName").getAsString(); 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) @Action(async = true, userPermission = 1L << 22)
@ -220,12 +221,8 @@ public class CMLogAction {
JsonObject ret = new JsonObject(); JsonObject ret = new JsonObject();
ret.addProperty("action", "onQueryAEState"); ret.addProperty("action", "onQueryAEState");
if (json.has("caller")) { if (json.has("caller")) {
ret.add( ret.add("result", JsonParser.parseString(JsonUtil
"result", .toJson(ActionExecutor.getStatistic(json.get("caller").getAsString()))));
JsonParser.parseString(
JsonUtil.toJson(
ActionExecutor.getStatistic(
json.get("caller").getAsString()))));
} else { } else {
ret.add("result", JsonParser.parseString(JsonUtil.toJson(ActionExecutor.getAllData()))); ret.add("result", JsonParser.parseString(JsonUtil.toJson(ActionExecutor.getAllData())));
} }
@ -244,17 +241,15 @@ public class CMLogAction {
public void checkIsContract(JsonObject json, ResultCallback rc) { public void checkIsContract(JsonObject json, ResultCallback rc) {
String requestID = new Random().nextLong() + "_" + System.currentTimeMillis(); String requestID = new Random().nextLong() + "_" + System.currentTimeMillis();
json.addProperty("requestID", requestID); 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.remove("list");
json.add("result", ret); json.add("result", ret);
json.addProperty("action", "onCheckIsContract"); json.addProperty("action", "onCheckIsContract");
rc.onResult(json.toString()); rc.onResult(json.toString());
} }
private void queryLogSize( private void queryLogSize(MultiIndexTimeDBUtilIntf db, String action, String label,
MultiIndexTimeDBUtilIntf db,
String action,
String label,
ResultCallback result) { ResultCallback result) {
try { try {
JsonObject ret = new JsonObject(); JsonObject ret = new JsonObject();
@ -274,20 +269,14 @@ public class CMLogAction {
} }
private void queryByDate( private void queryByDate(MultiIndexTimeDBUtilIntf db, String action, String label,
MultiIndexTimeDBUtilIntf db, JsonObject json, ResultCallback result) {
String action,
String label,
JsonObject json,
ResultCallback result) {
// TODO FixMe // TODO FixMe
// Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 // Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1
// column 240 path $.function // column 240 path $.function
if (!json.has("start")) { if (!json.has("start")) {
result.onResult( result.onResult("{\"action\":\"" + action
"{\"action\":\"" + "\",\"status\":\"failed\",\"data\":\"missing start\"}");
+ action
+ "\",\"status\":\"failed\",\"data\":\"missing start\"}");
return; return;
} }
long date = json.get("start").getAsLong(); long date = json.get("start").getAsLong();
@ -307,17 +296,11 @@ public class CMLogAction {
result.onResult(JsonUtil.toJson(ret)); result.onResult(JsonUtil.toJson(ret));
} }
private void queryByOffset( private void queryByOffset(MultiIndexTimeDBUtilIntf db, String action, String label,
MultiIndexTimeDBUtilIntf db, JsonObject json, ResultCallback result) {
String action,
String label,
JsonObject json,
ResultCallback result) {
if (!json.has("count")) { if (!json.has("count")) {
result.onResult( result.onResult("{\"action\":\"" + action
"{\"action\":\"" + "\",\"status\":\"failed\",\"data\":\"missing count\"}");
+ action
+ "\",\"status\":\"failed\",\"data\":\"missing count\"}");
return; return;
} }
int count = json.get("count").getAsInt(); int count = json.get("count").getAsInt();

View File

@ -9,8 +9,8 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
//UNUSED // UNUSED
//TO Merge // TO Merge
public class CheckPointCallback implements OnHashCallback { public class CheckPointCallback implements OnHashCallback {
public static Map<String, String> lastHash = new ConcurrentHashMap<>(); // contractID,hash 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("contractID", contractID);
reqStr.put("hash", hashStr); reqStr.put("hash", hashStr);
String sendStr = JsonUtil.toJson(reqStr); String sendStr = JsonUtil.toJson(reqStr);
MultiContractMeta info = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); MultiContractMeta info =
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
for (String node : info.getMembers()) { for (String node : info.getMembers()) {
NetworkManager.instance.sendToAgent(node, sendStr); NetworkManager.instance.sendToAgent(node, sendStr);
System.out.println( System.out.println("发送请求给 " + node.substring(0, 5) + " 更新ledger中最新检查点的hash!");
"发送请求给 " + node.substring(0, 5) + " 更新ledger中最新检查点的hash!");
} }
} }
} }

View File

@ -8,7 +8,7 @@ import org.bdware.sc.util.JsonUtil;
public class EventActions { public class EventActions {
@Action(async = true, userPermission = 0) @Action(async = true, userPermission = 0)
public void deliverEvent(JsonObject args, final ResultCallback rcb) { public void deliverEvent(JsonObject args, final ResultCallback rcb) {
CMActions.manager.deliverEvent( CMActions.manager
JsonUtil.fromJson(args.get("data").getAsString(), REvent.class)); .deliverEvent(JsonUtil.fromJson(args.get("data").getAsString(), REvent.class));
} }
} }

View File

@ -3,7 +3,13 @@ package org.bdware.server.action;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.bdware.sc.ContractClient; import org.bdware.sc.ContractClient;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.event.REvent;
import org.bdware.sc.util.HashUtil; 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 { public class EventWSActions {
@Action(async = true, userPermission = 0) @Action(async = true, userPermission = 0)
@ -25,7 +31,7 @@ public class EventWSActions {
ContractClient client = CMActions.manager.getClient(argCID); ContractClient client = CMActions.manager.getClient(argCID);
if (null == client) { if (null == client) {
ret.addProperty("data", "invalid contract ID or Name!"); ret.addProperty("data", "invalid contract ID or Name!");
rcb.onResult(ret.toString()); rcb.onResult(ret);
return; return;
} }
String contractID = client.getContractID(); String contractID = client.getContractID();
@ -36,4 +42,38 @@ public class EventWSActions {
ret.addProperty("data", topic); ret.addProperty("data", topic);
rcb.onResult(ret); 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

View File

@ -18,25 +18,17 @@ public class HttpFileAction {
LOGGER.debug("[HttpFileAction] donwloadContract : "); LOGGER.debug("[HttpFileAction] donwloadContract : ");
try { try {
/* already verified before into downloadContract! /*
String arg = "action=" + json.get("action").getAsString(); * already verified before into downloadContract! String arg = "action=" +
arg += "&projectName=" + json.get("projectName").getAsString(); * json.get("action").getAsString(); arg += "&projectName=" +
arg += "&isPrivate=" + json.get("isPrivate").getAsString(); * json.get("projectName").getAsString(); arg += "&isPrivate=" +
arg += "&pubKey=" + json.get("pubKey").getAsString(); * json.get("isPrivate").getAsString(); arg += "&pubKey=" +
arg += "&timestamp=" + json.get("timestamp").getAsString(); * json.get("pubKey").getAsString(); arg += "&timestamp=" +
ECPoint pubKey = SM2KeyPair.publicKeyStr2ECPoint(json.get("pubKey").getAsString()); * json.get("timestamp").getAsString(); ECPoint pubKey =
boolean verify = * SM2KeyPair.publicKeyStr2ECPoint(json.get("pubKey").getAsString()); boolean verify =
new SM2() * new SM2() .verify( arg, SM2.Signature.loadFromString(json.get("sign").getAsString()),
.verify( * "", pubKey); if (!verify) { result.onResult("/dev/null/2"); return; }
arg, */
SM2.Signature.loadFromString(json.get("sign").getAsString()),
"",
pubKey);
if (!verify) {
result.onResult("/dev/null/2");
return;
}
*/
String projectName = json.get("projectName").getAsString(); String projectName = json.get("projectName").getAsString();
if (projectName.contains("..")) { if (projectName.contains("..")) {

View File

@ -24,41 +24,41 @@ import java.util.Map;
public class LedgerActions { public class LedgerActions {
public static LedgerActions instance = new LedgerActions(); public static LedgerActions instance = new LedgerActions();
// @Action // @Action
// private void sendMockTransaction(JsonObject args, ResultCallback resultCallback) { // private void sendMockTransaction(JsonObject args, ResultCallback resultCallback) {
// Result r = new Result(); // Result r = new Result();
// r.data = "TODO"; // r.data = "TODO";
// r.status = true; // r.status = true;
// r.msg = null; // r.msg = null;
// resultCallback.onResult(gson.toJson(r)); // resultCallback.onResult(gson.toJson(r));
// } // }
// //
// @Action // @Action
// private void queryTransByHash(JsonObject args, ResultCallback resultCallback) { // private void queryTransByHash(JsonObject args, ResultCallback resultCallback) {
// Result r = new Result(); // Result r = new Result();
// r.data = "TODO"; // r.data = "TODO";
// r.status = true; // r.status = true;
// r.msg = null; // r.msg = null;
// resultCallback.onResult(gson.toJson(r)); // resultCallback.onResult(gson.toJson(r));
// } // }
// @Action // @Action
// private void queryHash(JsonObject args, ResultCallback resultCallback) { // private void queryHash(JsonObject args, ResultCallback resultCallback) {
// Result r = new Result(); // Result r = new Result();
// r.data = "TODO"; // r.data = "TODO";
// r.status = true; // r.status = true;
// r.msg = null; // r.msg = null;
// resultCallback.onResult(gson.toJson(r)); // resultCallback.onResult(gson.toJson(r));
// } // }
// //
// @Action // @Action
// private void queryRecent(JsonObject args, ResultCallback resultCallback) { // private void queryRecent(JsonObject args, ResultCallback resultCallback) {
// Result r = new Result(); // Result r = new Result();
// r.data = "TODO"; // r.data = "TODO";
// r.status = true; // r.status = true;
// r.msg = null; // r.msg = null;
// resultCallback.onResult(gson.toJson(r)); // resultCallback.onResult(gson.toJson(r));
// } // }
@Action @Action
public void queryLedgers(JsonObject jo, ResultCallback resultCallback) { public void queryLedgers(JsonObject jo, ResultCallback resultCallback) {
@ -69,7 +69,8 @@ public class LedgerActions {
LedgerProto.GetLedgersResponse ledgers = wt.getLedgersSync(); LedgerProto.GetLedgersResponse ledgers = wt.getLedgersSync();
List<String> array = new ArrayList<>(); List<String> array = new ArrayList<>();
ret.put("list", array); 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)); resultCallback.onResult(JsonUtil.toJson(ret));
} catch (Exception e) { } catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
@ -99,67 +100,61 @@ public class LedgerActions {
public void sendTransaction(JsonObject jo, ResultCallback resultCallback) { public void sendTransaction(JsonObject jo, ResultCallback resultCallback) {
Client wt = GRPCPool.grpcClients.get(0); Client wt = GRPCPool.grpcClients.get(0);
String ledgerName = "bdcontract"; 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 from = jo.get("from").getAsString();
String to = jo.get("to").getAsString(); String to = jo.get("to").getAsString();
String data = jo.get("data").getAsString(); String data = jo.get("data").getAsString();
ListenableFuture<LedgerProto.SendTransactionResponse> future = ListenableFuture<LedgerProto.SendTransactionResponse> future =
wt.sendTransaction( wt.sendTransaction(ledgerName, CommonProto.TransactionType.MESSAGE, from,
ledgerName, random.nextLong(), to, data.getBytes());
CommonProto.TransactionType.MESSAGE, future.addListener(() -> {
from, try {
random.nextLong(), ByteString hash = future.get().getHash();
to, ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", true,
data.getBytes()); HashUtil.byteArray2Str(hash.toByteArray()));
future.addListener( } catch (Exception e) {
() -> { ByteArrayOutputStream bo = new ByteArrayOutputStream();
try { String dataStr = bo.toString();
ByteString hash = future.get().getHash(); ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", false, dataStr);
ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", true, HashUtil.byteArray2Str(hash.toByteArray())); e.printStackTrace();
} catch (Exception e) { }
ByteArrayOutputStream bo = new ByteArrayOutputStream(); }, ContractManager.threadPool);
String dataStr = bo.toString();
ReplyUtil.replyWithStatus(resultCallback, "onSendTransaction", false, dataStr);
e.printStackTrace();
}
},
ContractManager.threadPool);
} }
@Action @Action
public void queryTransactionByHash(JsonObject jo, ResultCallback resultCallback) { public void queryTransactionByHash(JsonObject jo, ResultCallback resultCallback) {
String hash = jo.get("hash").getAsString(); String hash = jo.get("hash").getAsString();
String ledgerName = "bdcontract"; 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); Client wt = GRPCPool.grpcClients.get(0);
if (wt == null) { if (wt == null) {
resultCallback.onResult( resultCallback
"{\"status\":\"Exception\",\"data\":\"no available worker thread\"}"); .onResult("{\"status\":\"Exception\",\"data\":\"no available worker thread\"}");
return; return;
} }
ListenableFuture<QueryProto.GetTransactionByHashResponse> future = ListenableFuture<QueryProto.GetTransactionByHashResponse> future =
wt.getTransactionByHash(ledgerName, hash); wt.getTransactionByHash(ledgerName, hash);
future.addListener( future.addListener(() -> {
() -> { try {
try { CommonProto.Transaction trans = future.get().getTransaction();
CommonProto.Transaction trans = future.get().getTransaction(); Map<String, String> ret = new HashMap<>();
Map<String, String> ret = new HashMap<>(); ret.put("from", HashUtil.byteArray2Str(trans.getFrom().toByteArray()));
ret.put("from", HashUtil.byteArray2Str(trans.getFrom().toByteArray())); ret.put("to", HashUtil.byteArray2Str(trans.getTo().toByteArray()));
ret.put("to", HashUtil.byteArray2Str(trans.getTo().toByteArray())); ret.put("data", trans.getData().toStringUtf8());
ret.put("data", trans.getData().toStringUtf8()); ret.put("action", "onQueryTransactionByHash");
ret.put("action", "onQueryTransactionByHash"); ret.put("type", trans.getType().name());
ret.put("type", trans.getType().name()); resultCallback.onResult(JsonUtil.toJson(ret));
resultCallback.onResult(JsonUtil.toJson(ret)); } catch (Exception e) {
} catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream();
ByteArrayOutputStream bo = new ByteArrayOutputStream(); Map<String, String> ret = new HashMap<>();
Map<String, String> ret = new HashMap<>(); ret.put("action", "onQueryTransactionByHash");
ret.put("action", "onQueryTransactionByHash"); e.printStackTrace(new PrintStream(bo));
e.printStackTrace(new PrintStream(bo)); ret.put("data", bo.toString());
ret.put("data", bo.toString()); resultCallback.onResult(JsonUtil.toJson(ret));
resultCallback.onResult(JsonUtil.toJson(ret)); e.printStackTrace();
e.printStackTrace(); }
} }, ContractManager.threadPool);
},
ContractManager.threadPool);
} }
} }

View File

@ -42,11 +42,8 @@ public class ManagerActions {
public void getEncodedUUID(JsonObject args, ResultCallback resultCallback) { public void getEncodedUUID(JsonObject args, ResultCallback resultCallback) {
String data = null; String data = null;
try { try {
data = data = ByteUtils.toHexString(SM2Util.encrypt(GlobalConf.instance.keyPair.getPublicKey(),
ByteUtils.toHexString( HardwareInfo.getCPUID().getBytes()));
SM2Util.encrypt(
GlobalConf.instance.keyPair.getPublicKey(),
HardwareInfo.getCPUID().getBytes()));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -65,8 +62,7 @@ public class ManagerActions {
try { try {
String key = args.get("key").getAsString(); String key = args.get("key").getAsString();
String val = args.get("val").getAsString(); String val = args.get("val").getAsString();
boolean boolean status = true;
status = true;
Object data; Object data;
switch (key) { switch (key) {
case "licence": case "licence":
@ -82,17 +78,19 @@ public class ManagerActions {
data = status = GlobalConf.resetDataChain(val); data = status = GlobalConf.resetDataChain(val);
break; break;
case "doipConfig": case "doipConfig":
data = status = GlobalConf.resetDOIPConfig(val); data = status = GlobalConf.resetDOAConfig(val);
break; break;
case "nodeCenter": case "nodeCenter":
data = status = GlobalConf.resetNodeCenter(val); data = status = GlobalConf.resetNodeCenter(val);
break; break;
case "nodeCenterWS":
data = status = GlobalConf.resetNodeCenterWS(val);
break;
case "nodeName": case "nodeName":
data = status = GlobalConf.resetNodeName(val); data = status = GlobalConf.resetNodeName(val);
break; break;
case "masterAddress": case "masterAddress":
data = status = GlobalConf.resetMasterAddress(val); // data = status = GlobalConf.resetMasterAddress(val);
break;
default: default:
status = false; status = false;
data = "unsupported key:" + key; data = "unsupported key:" + key;
@ -101,8 +99,10 @@ public class ManagerActions {
return; return;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ReplyUtil.replyWithStatus(resultCallback, "onUpdateConfig", false,
"exception:" + e.getMessage());
} }
resultCallback.onResult("Exception");
} }
@Action(userPermission = 1L << 11) @Action(userPermission = 1L << 11)
@ -117,19 +117,19 @@ public class ManagerActions {
data.put("licence", GlobalConf.instance.licence); data.put("licence", GlobalConf.instance.licence);
data.put("expireTime", new Date(GlobalConf.instance.expireTime) + ""); data.put("expireTime", new Date(GlobalConf.instance.expireTime) + "");
data.put("nodeName", GlobalConf.instance.name); 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); ReplyUtil.replyWithStatus(resultCallback, "onLoadConfig", true, data);
} }
@Action(userPermission = 1L << 11) @Action(userPermission = 1L << 11)
public void listNodeInfos(JsonObject args, ResultCallback resultCallback) { public void listNodeInfos(JsonObject args, ResultCallback resultCallback) {
ControllerManager.getNodeCenterController().getNodeInfos( ControllerManager.getNodeCenterController().getNodeInfos(new ResultCallback() {
new ResultCallback() { @Override
@Override public void onResult(String str) {
public void onResult(String str) { ReplyUtil.replyWithStatus(resultCallback, "onListNodeInfos", true, str);
ReplyUtil.replyWithStatus(resultCallback, "onListNodeInfos", true, str); }
} });
});
} }
@Action(userPermission = 1L << 26) @Action(userPermission = 1L << 26)
@ -147,13 +147,14 @@ public class ManagerActions {
data.put("licence", GlobalConf.instance.licence); data.put("licence", GlobalConf.instance.licence);
data.put("nodeName", GlobalConf.instance.name); data.put("nodeName", GlobalConf.instance.name);
data.put("ipPort", GlobalConf.instance.ipPort); data.put("ipPort", GlobalConf.instance.ipPort);
data.put("doipConfig", GlobalConf.getDOAConfig()); data.put("doipConfig", JsonUtil.toJson(GlobalConf.instance.doaConf));
data.put("isLAN", String.valueOf(GlobalConf.isLAN()));
data.put("peerID", GlobalConf.instance.peerID); data.put("peerID", GlobalConf.instance.peerID);
data.put("bdledger", GlobalConf.instance.datachainConf.replace("\n", " ")); 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("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); ReplyUtil.replyWithStatus(resultCallback, "onLoadNodeConfig", true, data);
} }
@ -161,45 +162,61 @@ public class ManagerActions {
public void changeNodeCenter(JsonObject args, ResultCallback resultCallback) { public void changeNodeCenter(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); String val = args.get("data").getAsString();
LOGGER.debug(JsonUtil.toJson(args)); 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) @Action(userPermission = 1 << 10)
public void changeBDledger(JsonObject args, ResultCallback resultCallback) { public void changeBDledger(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); String val = args.get("data").getAsString();
String lines = val.replace(" ", "\n"); String lines = val.replace(" ", "\n");
LOGGER.debug("changeBDledger " + lines); LOGGER.debug("changeBDledger " + lines);
ReplyUtil.replyWithStatus(resultCallback, "onChangeBDledger", true, GlobalConf.resetDataChain(lines)); ReplyUtil.replyWithStatus(resultCallback, "onChangeBDledger", true,
GlobalConf.resetDataChain(lines));
} }
@Action(userPermission = 1 << 10) @Action(userPermission = 1 << 10)
public void changeNodeName(JsonObject args, ResultCallback resultCallback) { public void changeNodeName(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); 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) @Action(userPermission = 1 << 10)
public void changeIpPort(JsonObject args, ResultCallback resultCallback) { public void changeIpPort(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); 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) @Action(userPermission = 1 << 10)
public void changeDOIPConfig(JsonObject args, ResultCallback resultCallback) { public void changeDOIPConfig(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); 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) @Action(userPermission = 1 << 10)
public void changeYJSPath(JsonObject args, ResultCallback resultCallback) { public void changeYJSPath(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); 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) @Action(userPermission = 1 << 10)
public void uploadLicence(JsonObject args, ResultCallback resultCallback) { public void uploadLicence(JsonObject args, ResultCallback resultCallback) {
String val = args.get("data").getAsString(); 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) @Action(userPermission = 1 << 11)
@ -211,7 +228,8 @@ public class ManagerActions {
public void applyNodeRoleDeprecated(JsonObject json, ResultCallback resultCallback) { public void applyNodeRoleDeprecated(JsonObject json, ResultCallback resultCallback) {
try { try {
String pubKey = null; String pubKey = null;
if (json.has("verifiedPubKey")) pubKey = json.get("verifiedPubKey").getAsString(); if (json.has("verifiedPubKey"))
pubKey = json.get("verifiedPubKey").getAsString();
if (pubKey == null) { if (pubKey == null) {
resultCallback.onResult( resultCallback.onResult(
"{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Illegal user\"}"); "{\"action\":\"onApplyNodeRole\",\"data\":\"Failed: Illegal user\"}");
@ -232,17 +250,15 @@ public class ManagerActions {
} }
if (str == null || str.length() == 0) { if (str == null || str.length() == 0) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey,
CMTables.ApplyRole.toString(), pubKey, role.name()); role.name());
} else { } else {
if (!str.contains(role.name())) if (!str.contains(role.name()))
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey,
CMTables.ApplyRole.toString(), pubKey, str + "," + role.name()); str + "," + role.name());
} }
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ApplyTime.toString(), pubKey,
CMTables.ApplyTime.toString(),
pubKey,
Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
ReplyUtil.simpleReply(resultCallback, "onApplyNodeRole", "success"); ReplyUtil.simpleReply(resultCallback, "onApplyNodeRole", "success");
@ -253,8 +269,7 @@ public class ManagerActions {
ret.addProperty("action", "onApplyNodeRole"); ret.addProperty("action", "onApplyNodeRole");
ret.addProperty("data", "success"); ret.addProperty("data", "success");
ret.addProperty("role", ro); ret.addProperty("role", ro);
resultCallback.onResult( resultCallback.onResult(ret);
ret);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,5 +1,7 @@
package org.bdware.server.action; package org.bdware.server.action;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; 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.db.KeyValueDBUtil;
import org.bdware.sc.units.RecoverFlag; import org.bdware.sc.units.RecoverFlag;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.server.GRPCPool;
import org.bdware.server.GlobalConf; import org.bdware.server.GlobalConf;
import org.bdware.server.action.p2p.MasterServerRecoverMechAction; import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
import org.bdware.server.action.p2p.MasterServerTCPAction; import org.bdware.server.action.p2p.MasterServerTCPAction;
import org.bdware.server.trustedmodel.MultiPointContractInfo; import org.bdware.server.trustedmodel.MultiPointContractInfo;
import org.bdware.server.trustedmodel.MultiPointCooperateContractInfo; import org.bdware.server.trustedmodel.MultiPointCooperateContractInfo;
import org.bdware.server.trustedmodel.ResultCollector; import org.bdware.server.trustedmodel.ResultCollector;
import org.bdware.server.ws.ContractManagerFrameHandler;
import org.bdware.units.NetworkManager; import org.bdware.units.NetworkManager;
import org.zz.gmhelper.SM2KeyPair; import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util; import org.zz.gmhelper.SM2Util;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
// WebSocket接口 // WebSocket接口
public class MasterWSAction { public class MasterWSAction {
private static final Logger LOGGER = LogManager.getLogger(MasterWSAction.class); private static final Logger LOGGER = LogManager.getLogger(MasterWSAction.class);
private final UserManagerAction managerAction; private final ContractManagerFrameHandler handler;
public MasterWSAction(UserManagerAction userManagerAction) { public MasterWSAction(ContractManagerFrameHandler handler) {
this.managerAction = userManagerAction; this.handler = handler;
} }
public static boolean hostMaster(String contractID) { public static boolean hostMaster(String contractID) {
return CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).isMaster(); 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)); LOGGER.info("waitForAllNodes:" + JsonUtil.toJson(nodeNames));
for (int i = 0; i < 5; i++) { for (int i = 0; i < 500; i++) {
boolean all = true; boolean all = true;
for (String str : nodeNames) { for (String str : nodeNames) {
if (!NetworkManager.instance.hasAgentConnection(str)) { if (!NetworkManager.instance.hasAgentConnection(str)) {
@ -68,6 +70,16 @@ public class MasterWSAction {
return false; 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) @Action(async = true, userPermission = 1L << 26)
// zyx modified // zyx modified
public void startContractMultiPoint(JsonObject args, final ResultCallback rc) { public void startContractMultiPoint(JsonObject args, final ResultCallback rc) {
@ -92,36 +104,36 @@ public class MasterWSAction {
} else if (args.has("projectName")) { } else if (args.has("projectName")) {
contract.setScript("/" + args.get("projectName").getAsString()); contract.setScript("/" + args.get("projectName").getAsString());
} }
if (args.has("createParam"))
contract.setCreateParam(args.get("createParam"));
// contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json"); // contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json");
// String toVerify = // String toVerify =
// "MultiPoint|" // "MultiPoint|"
// + args.get("type").getAsString() // + args.get("type").getAsString()
// + "|" // + "|"
// + contract.getScriptStr() // + contract.getScriptStr()
// + "|" // + "|"
// + managerAction.getPubKey(); // + managerAction.getPubKey();
// boolean verify = // boolean verify =
// SM2Util.plainStrVerify( // SM2Util.plainStrVerify(
// managerAction.getPubKey(), toVerify, // managerAction.getPubKey(), toVerify,
// args.get("sign").getAsString()); // args.get("sign").getAsString());
// //
// if (!verify) { // if (!verify) {
// Map<String, String> ret = new HashMap<>(); // Map<String, String> ret = new HashMap<>();
// ret.put("action", "onStartContractTrustfullyResult"); // ret.put("action", "onStartContractTrustfullyResult");
// ret.put("data", "verify signature failed"); // ret.put("data", "verify signature failed");
// ret.put("status", "Error"); // ret.put("status", "Error");
// rc.onResult(JsonUtil.toJson(ret)); // rc.onResult(JsonUtil.toJson(ret));
// return; // return;
// } // }
SM2KeyPair sm2Key; SM2KeyPair sm2Key;
String parPath = GlobalConf.instance.publicCompiledDir; String parPath = GlobalConf.instance.publicCompiledDir;
if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) { if (args.has("isPrivate") && args.get("isPrivate").getAsBoolean()) {
parPath = parPath = GlobalConf.instance.privateCompiledDir + "/"
GlobalConf.instance.privateCompiledDir + args.get("verifiedPubKey").getAsString();
+ "/"
+ args.get("verifiedPubKey").getAsString();
} }
String scriptOrPath = contract.getScriptStr(); String scriptOrPath = contract.getScriptStr();
@ -142,55 +154,42 @@ public class MasterWSAction {
contract.setPublicKey(sm2Key.getPublicKeyStr()); contract.setPublicKey(sm2Key.getPublicKeyStr());
// TODO // TODO
// contract.setNodeCenterRepoDOI(DOIPMainServer4NC.repoIdentifier); // contract.setNodeCenterRepoDOI(DOIPMainServer4NC.repoIdentifier);
// 多点合约部署时由NC预先注册一个DOI以备使用 // 多点合约部署时由NC预先注册一个DOI以备使用
/* /*
if (DoConfig.openContractDORegister) { * if (DoConfig.openContractDORegister) { try { HandleService hs = new
try { * HandleService(HandleServiceUtils.hrRegister); String tmpDOI =
HandleService hs = new HandleService(HandleServiceUtils.hrRegister); * hs.registerContract(DOIPMainServer4NC.repoIdentifier); if (tmpDOI == "" || tmpDOI ==
String tmpDOI = hs.registerContract(DOIPMainServer4NC.repoIdentifier); * null) contract.setDOI("RegisterFailed"); else contract.setDOI(tmpDOI); } catch (Exception
if (tmpDOI == "" || tmpDOI == null) * e) { e.printStackTrace(); contract.setDOI("RegisterFailed"); } }
contract.setDOI("RegisterFailed"); */
else List<String> nodeNames; // nodes' peerID
contract.setDOI(tmpDOI); // if (contract.getType().equals(ContractExecType.SelfAdaptiveSharding)) {
} // if (ContractManager.instance.nodeCenterConn.listNodes().length < 3) {
catch (Exception e) { // rc.onResult(
e.printStackTrace(); // "{\"status\":\"Error\",\"result\":\"No enough nodes!\","
contract.setDOI("RegisterFailed"); // + "\"action\":\"onStartTrustfulContract\"}");
} // return;
} // }
*/ // int unitSize = args.get("selectUnitNum").getAsInt();
Set<String> nodeNames; // nodes' peerID // nodeNames =
// if (contract.getType().equals(ContractExecType.SelfAdaptiveSharding)) { // Arrays.asList(ContractManager.instance.nodeCenterConn.getClusterByKey(
// if (ContractManager.instance.nodeCenterConn.listNodes().length < 3) { // contractID,
// rc.onResult( // Math.max(unitSize, 3)));
// "{\"status\":\"Error\",\"result\":\"No enough nodes!\"," // } else {
// + "\"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 // all nodes' peerID in the unit
String[] nodeNamesStr = String[] nodeNamesStr = args.get("peersID").getAsString().split(",");
args.get("peersID").getAsString().split(",");
// record information of these nodes // record information of these nodes
nodeNames = nodeNames = Arrays.stream(nodeNamesStr).filter(x -> null != x && !x.isEmpty())
Arrays.stream(nodeNamesStr) .collect(Collectors.toList());
.filter(x -> null != x && !x.isEmpty()) // }
.collect(Collectors.toSet());
// }
SM2KeyPair keyPair = GlobalConf.instance.keyPair; SM2KeyPair keyPair = GlobalConf.instance.keyPair;
String masterNode = keyPair.getPublicKeyStr(); String masterNode = keyPair.getPublicKeyStr();
nodeNames.add(masterNode); assert nodeNames.contains(masterNode);
// nodeNames.add(masterNode);
int nodeSize = nodeNames.size(); int nodeSize = nodeNames.size();
// 方式一向NodeCenter发要求Slave节点主动连接到Master节点. // 方式一向NodeCenter发要求Slave节点主动连接到Master节点.
Map<String, Object> requestConnect = new HashMap<>(); Map<String, Object> requestConnect = new HashMap<>();
requestConnect.put("action", "requestConnectToMaster"); requestConnect.put("action", "requestConnectToMaster");
LOGGER.debug(multiPointContractInfo.masterNode); LOGGER.debug(multiPointContractInfo.masterNode);
@ -199,47 +198,34 @@ public class MasterWSAction {
if (contract.getType() == ContractExecType.Sharding) { if (contract.getType() == ContractExecType.Sharding) {
requestConnect.put("connectAll", true); requestConnect.put("connectAll", true);
} }
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发 // NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(requestConnect)); // 向NC发
waitForConnection(nodeNames); // waitForConnection(nodeNames);
LOGGER.debug(JsonUtil.toPrettyJson(requestConnect)); 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(); final long curr = System.currentTimeMillis();
String requestID = curr + "_" + (int) (Math.random() * 10000); String requestID = curr + "_" + (int) (Math.random() * 10000);
ResultCollector collector = ResultCollector collector = new ResultCollector(requestID, new ResultCallback() {
new ResultCollector( @Override
requestID, public void onResult(String str) {
new ResultCallback() { Map<String, String> ret = new HashMap<>();
@Override ret.put("action", "onStartContractTrustfullyResult");
public void onResult(String str) { if (args.has("requestID")) {
Map<String, String> ret = new HashMap<>(); ret.put("responseID", args.get("requestID").getAsString());
ret.put("action", "onStartContractTrustfullyResult"); }
if (args.has("requestID")) { ret.put("data", str);
ret.put("responseID", args.get("requestID").getAsString()); ret.put("executionTime", (System.currentTimeMillis() - curr) + "");
} rc.onResult(ret);
ret.put("data", str); }
ret.put("executionTime", (System.currentTimeMillis() - curr) + ""); }, nodeSize);
rc.onResult(ret);
}
},
nodeSize);
MasterServerTCPAction.sync.sleepWithTimeout(requestID, collector, 20); MasterServerTCPAction.sync.sleepWithTimeout(requestID, collector, 20);
Map<String, Object> request = new HashMap<>(); Map<String, Object> request = new HashMap<>();
request.put("master", keyPair.getPublicKeyStr()); request.put("master", keyPair.getPublicKeyStr());
if (args.has("isPrivate")) { if (args.has("isPrivate")) {
request.put("isPrivate", args.get("isPrivate").getAsString()); 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("action", "startContractTrustfully");
request.put("requestID", requestID); request.put("requestID", requestID);
request.put("members", nodeNames); // 执行这个合约的所有节点的pubKey request.put("members", nodeNames); // 执行这个合约的所有节点的pubKey
@ -255,7 +241,7 @@ public class MasterWSAction {
contract.setNumOfCopies(nodeSize); contract.setNumOfCopies(nodeSize);
break; break;
default: default:
contract.setNumOfCopies(1); // TODO copies是1 contract.setNumOfCopies(1); // TODO copies是1
break; break;
} }
LOGGER.debug("contract " + contract.getID() + " has been added into contractID2Members"); LOGGER.debug("contract " + contract.getID() + " has been added into contractID2Members");
@ -278,16 +264,17 @@ public class MasterWSAction {
if (!MasterServerRecoverMechAction.recoverStatus.containsKey(nodeID)) { if (!MasterServerRecoverMechAction.recoverStatus.containsKey(nodeID)) {
MasterServerRecoverMechAction.recoverStatus.put(nodeID, new ConcurrentHashMap<>()); MasterServerRecoverMechAction.recoverStatus.put(nodeID, new ConcurrentHashMap<>());
} }
MasterServerRecoverMechAction.recoverStatus MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID,
.get(nodeID) RecoverFlag.Fine);
.put(contractID, RecoverFlag.Fine);
} }
rc.onResult( rc.onResult("{\"status\":\"Success\",\"result\":\"" + contract.getID() + "\","
"{\"status\":\"Success\",\"result\":\"" + "\"action\":\"onStartTrustfulContract\"}");
+ contract.getID()
+ "\","
+ "\"action\":\"onStartTrustfulContract\"}");
LOGGER.info("startContractMultiPoint succeed!"); LOGGER.info("startContractMultiPoint succeed!");
} }
public String getPubKey(JsonObject jo) {
if (handler != null)
return handler.getPubKey();
return jo.get("pubKey").getAsString();
}
} }

View File

@ -18,13 +18,14 @@ import java.util.concurrent.atomic.AtomicInteger;
public class MultiRequestInfo { public class MultiRequestInfo {
public static final Object lock = new Object(); public static final Object lock = new Object();
private static final Logger LOGGER = LogManager.getLogger(MultiRequestInfo.class); 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 String requestID;
public final int seq; public final int seq;
private final int total; private final int total;
private final AtomicInteger count = new AtomicInteger(0); private final AtomicInteger count = new AtomicInteger(0);
private final Map<String, Boolean> flag = new HashMap<>(); //uniID是否执行 private final Map<String, Boolean> flag = new HashMap<>(); // uniID是否执行
public Map<String, ResultCallback> callbackMap = new ConcurrentHashMap<>(); //key是uniqueID这个map中存的是第2个及之后的请求的callback第1个请求的callback在正常的请求队列中 public Map<String, ResultCallback> callbackMap = new ConcurrentHashMap<>(); // key是uniqueID这个map中存的是第2个及之后的请求的callback第1个请求的callback在正常的请求队列中
public MultiRequestInfo(String id, int t) { public MultiRequestInfo(String id, int t) {
requestID = id; requestID = id;
@ -32,41 +33,47 @@ public class MultiRequestInfo {
total = Integer.parseInt(id.split("_")[1]); total = Integer.parseInt(id.split("_")[1]);
} }
//交给线程池中线程完成不能是同一个线程跑 // 交给线程池中线程完成不能是同一个线程跑
public static void exeMultiReq(ContractRequest request, String uniID) { public static void exeMultiReq(ContractRequest request, String uniID) {
ContractManagerFrameHandler.executorService.execute( ContractManagerFrameHandler.executorService.execute(() -> {
() -> { exeMultiReqInternal(request, uniID);
exeMultiReqInternal(request, uniID); });
}
);
} }
//多点合约请求第一个放入队列中其余的不放入队列完成此方法对于不放入队列中的多点合约请求 // 多点合约请求第一个放入队列中其余的不放入队列完成此方法对于不放入队列中的多点合约请求
private static void exeMultiReqInternal(ContractRequest request, String 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(); 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()); MultiContractMeta cei = CMActions.manager.multiContractRecorder
//logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId() + " ---3 " + (reqInfos.containsKey(request.getRequestID()))); .getMultiContractMeta(request.getContractID());
// logger.info("【exeMultiReqInternal】 uniID=" + uniID + " Thread-id=" +
// Thread.currentThread().getId() + " ---3 " +
// (reqInfos.containsKey(request.getRequestID())));
MultiRequestInfo mri = reqInfos.get(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) { 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) { if (cei.curExeSeq > -1 && cei.curExeSeq < mri.seq) {
// logger.info("还未执行到 " + mri.seq + " 当前序号是 " + cei.curExeSeq); // logger.info("还未执行到 " + mri.seq + " 当前序号是 " + cei.curExeSeq);
return; return;
} }
if (mri.flag.get(uniID)) { if (mri.flag.get(uniID)) {
//logger.info("uniID=" + uniID + " 已执行过 " + " Thread-id=" + Thread.currentThread().getId()); // logger.info("uniID=" + uniID + " 已执行过 " + " Thread-id=" +
// Thread.currentThread().getId());
return; return;
} else { } else {
mri.flag.put(uniID, true); mri.flag.put(uniID, true);
@ -75,28 +82,30 @@ public class MultiRequestInfo {
if (-1 == cei.curExeSeq) if (-1 == cei.curExeSeq)
cei.curExeSeq = mri.seq; cei.curExeSeq = mri.seq;
//logger.info("调试位置 3 cei.curExeSeq=" + cei.curExeSeq + " MultiRequestInfo 执行请求 " + request.seq + " uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId()); // logger.info("调试位置 3 cei.curExeSeq=" + cei.curExeSeq + " MultiRequestInfo 执行请求 " +
String data2 = CMActions.manager.executeLocally(request, null); // request.seq + " uniID=" + uniID + " Thread-id=" + Thread.currentThread().getId());
// logger.info("本地执行的结果为\n" + data2); CMActions.manager.executeLocallyAsync(request, new ResultCallback() {
Map<String, String> ret = new HashMap<>(); @Override
ret.put("action", "receiveTrustfullyResult"); public void onResult(String str) {
SM2KeyPair keyPair = GlobalConf.instance.keyPair; Map<String, String> ret = new HashMap<>();
ret.put("nodeID", keyPair.getPublicKeyStr()); ret.put("action", "receiveTrustfullyResult");
int seq = request.seq; SM2KeyPair keyPair = GlobalConf.instance.keyPair;
ret.put("nodeID", keyPair.getPublicKeyStr());
int seq = request.seq;
ret.put("responseID", uniID); ret.put("responseID", uniID);
ret.put("executeTime", (System.currentTimeMillis() - start) + ""); ret.put("executeTime", (System.currentTimeMillis() - start) + "");
ret.put("data", data2); ret.put("data", str);
mri.countIncrease(); mri.countIncrease();
// logger.info("返回 uniID=" + uniID + " 的结果");
//logger.info("返回 uniID=" + uniID + " 的结果"); mri.callbackMap.get(uniID).onResult(JsonUtil.toJson(ret));
}
mri.callbackMap.get(uniID).onResult(JsonUtil.toJson(ret)); }, null);
} }
public synchronized void countIncrease() { public synchronized void countIncrease() {
int tmp = count.incrementAndGet(); int tmp = count.incrementAndGet();
//logger.info("【MultiRequestInfo】 countIncrease tmp=" + tmp + " total=" + total); // logger.info("【MultiRequestInfo】 countIncrease tmp=" + tmp + " total=" + total);
if (tmp == total) { if (tmp == total) {
reqInfos.remove(requestID); reqInfos.remove(requestID);
} }

View File

@ -76,7 +76,8 @@ public class ProcessAction {
sc.nextLine(); sc.nextLine();
while (sc.hasNextLine()) { while (sc.hasNextLine()) {
String line = sc.nextLine(); 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); LOGGER.debug("==ListLeakCP=:" + line);
Scanner sc2 = new Scanner(new ByteArrayInputStream(line.getBytes())); Scanner sc2 = new Scanner(new ByteArrayInputStream(line.getBytes()));
sc2.next(); sc2.next();

View File

@ -23,8 +23,7 @@ public class ReadmeGenerator {
private String generateReadme() { private String generateReadme() {
VelocityEngine engine = new VelocityEngine(); VelocityEngine engine = new VelocityEngine();
engine.setProperty( engine.setProperty("file.resource.loader.path",
"file.resource.loader.path",
new File(templatePath, "ReadmeTemplate").getAbsolutePath()); new File(templatePath, "ReadmeTemplate").getAbsolutePath());
createREADMEIfNotExists(new File(templatePath, "ReadmeTemplate")); createREADMEIfNotExists(new File(templatePath, "ReadmeTemplate"));
VelocityContext ctx = new VelocityContext(); VelocityContext ctx = new VelocityContext();
@ -32,18 +31,17 @@ public class ReadmeGenerator {
readmeFuncs = new ArrayList<>(); readmeFuncs = new ArrayList<>();
} }
for (ReadmeFunction rm : readmeFuncs) { for (ReadmeFunction rm : readmeFuncs) {
/*if (rm.funcDefaultArg == null) { /*
rm.funcDefaultArg = "未提供默认参数"; * if (rm.funcDefaultArg == null) { rm.funcDefaultArg = "未提供默认参数"; } if
} * (rm.funcDefaultResult == null) { rm.funcDefaultResult = "暂无返回结果示例"; }
if (rm.funcDefaultResult == null) { */
rm.funcDefaultResult = "暂无返回结果示例";
}*/
if (rm.funcDescription == null) { if (rm.funcDescription == null) {
rm.funcDescription = "暂无描述"; rm.funcDescription = "暂无描述";
} }
/*if (rm.funcMockTemplate == null) { /*
rm.funcMockTemplate = "{\"result\":\"emptyMock\"}"; * if (rm.funcMockTemplate == null) { rm.funcMockTemplate =
}*/ * "{\"result\":\"emptyMock\"}"; }
*/
if (rm.funcArgTemplate == null) { if (rm.funcArgTemplate == null) {
rm.funcArgTemplate = "暂无参数模板"; rm.funcArgTemplate = "暂无参数模板";
} }
@ -76,63 +74,24 @@ public class ReadmeGenerator {
readmeTemplate.mkdirs(); readmeTemplate.mkdirs();
File content = new File(readmeTemplate, "README.md.vm"); File content = new File(readmeTemplate, "README.md.vm");
try { try {
FileUtils.writeStringToFile( FileUtils.writeStringToFile(content,
content, /*
/*"# 简介\n" * "# 简介\n" + "\n" + "${contractDesp}\n" + "\n" + "#set($hash='#')\n" +
+ "\n" * "# 获取数据相关方法说明\n" + "#foreach($func in ${readmeFuncs})\n" + "\n" +
+ "${contractDesp}\n" * "$hash$hash ${func.funcName}\n" + "\n" + ">$func.funcDescription\n" +
+ "\n" * "\n" + "$hash$hash$hash 参数示例\n" + "```json\n" + "$func.funcDefaultArg\n"
+ "#set($hash='#')\n" * + "```\n" + "\n" + "$hash$hash$hash 数据模板\n" + "```json\n" +
+ "# 获取数据相关方法说明\n" * "$func.funcMockTemplate\n" + "```\n" + "\n" + "$hash$hash$hash 返回结果\n" +
+ "#foreach($func in ${readmeFuncs})\n" * "\n" + "```json\n" + "$func.funcDefaultResult\n" + "```\n" + "\n" +
+ "\n" * "#end\n" + "\n",
+ "$hash$hash ${func.funcName}\n" */
+ "\n" "# 简介\n" + "\n" + "${contractDesp}\n" + "\n" + "#set($hash='#')\n"
+ ">$func.funcDescription\n" + "# 获取数据相关方法说明\n" + "#foreach($func in ${readmeFuncs})\n" + "\n"
+ "\n" + "$hash$hash${func.funcName}\n" + "\n" + ">$func.funcDescription\n"
+ "$hash$hash$hash 参数示例\n" + "\n" + "$hash$hash$hash 参数模板\n" + "```json\n"
+ "```json\n" + "$func.funcDefaultArg\n" + "```\n" + "\n"
+ "$func.funcDefaultArg\n" + "$hash$hash$hash 调用结果\n" + "\n" + "```json\n"
+ "```\n" + "$func.funcRecordsString\n" + "```\n" + "\n" + "#end\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); StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -149,7 +108,8 @@ public class ReadmeGenerator {
if (!name.exists()) { if (!name.exists()) {
FileUtils.writeStringToFile(name, generateReadme(), StandardCharsets.UTF_8); FileUtils.writeStringToFile(name, generateReadme(), StandardCharsets.UTF_8);
return "生成文档 " + name.getName(); return "生成文档 " + name.getName();
} else return "请删除已有README.md"; } else
return "请删除已有README.md";
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return "生成文档异常"; return "生成文档异常";
@ -170,19 +130,19 @@ public class ReadmeGenerator {
public String funcDescription; public String funcDescription;
public String funcDefaultArg; public String funcDefaultArg;
public String funcDefaultResult; public String funcDefaultResult;
//public String funcMockTemplate; // public String funcMockTemplate;
public String funcRecordsString; public String funcRecordsString;
public String funcArgTemplate; public String funcArgTemplate;
public String funcArgSchema; public String funcArgSchema;
public String funcResultSchema; public String funcResultSchema;
public String funcResultTemplate; public String funcResultTemplate;
public ReadmeFunction( public ReadmeFunction(String name, String description, String argSchema, String argTemplate,
String name, String description, String argSchema, String argTemplate, String resultSchema, String recordsString) { String resultSchema, String recordsString) {
funcName = name; funcName = name;
funcDescription = description; funcDescription = description;
funcArgTemplate = argTemplate; funcArgTemplate = argTemplate;
//funcResultTemplate = resTemplate; // funcResultTemplate = resTemplate;
funcRecordsString = recordsString; funcRecordsString = recordsString;
funcArgSchema = argSchema; funcArgSchema = argSchema;
funcResultSchema = resultSchema; funcResultSchema = resultSchema;
@ -205,9 +165,9 @@ public class ReadmeGenerator {
return funcDefaultResult.replace("\\\"", "\""); return funcDefaultResult.replace("\\\"", "\"");
} }
/*public String getFuncMockTemplate() { /*
return funcMockTemplate; * public String getFuncMockTemplate() { return funcMockTemplate; }
}*/ */
public String getFuncArgTemplate() { public String getFuncArgTemplate() {
return funcArgTemplate; return funcArgTemplate;

View File

@ -1,11 +1,12 @@
package org.bdware.server.action; package org.bdware.server.action;
import org.bdware.sc.bean.ContractRequest; import org.bdware.sc.bean.ContractRequest;
public class RequestToMaster { public class RequestToMaster {
long requestTime; long requestTime;
ContractRequest cr; ContractRequest cr;
public RequestToMaster(ContractRequest c){ public RequestToMaster(ContractRequest c) {
requestTime = System.currentTimeMillis(); requestTime = System.currentTimeMillis();
cr = c; cr = c;
} }

View File

@ -3,10 +3,7 @@ package org.bdware.server.action;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractClient; import org.bdware.sc.*;
import org.bdware.sc.ContractManager;
import org.bdware.sc.ContractMeta;
import org.bdware.sc.ContractStatusEnum;
import org.bdware.sc.bean.Contract; import org.bdware.sc.bean.Contract;
import org.bdware.sc.bean.ContractExecType; import org.bdware.sc.bean.ContractExecType;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
@ -40,7 +37,7 @@ public class TemporyTestAction {
@Action @Action
public void startContractForTest(JsonObject args, ResultCallback resultCallback) { public void startContractForTest(JsonObject args, ResultCallback resultCallback) {
FileActions.startIfManifestDetected( FileActions.startIfManifestDetected(
new File(args.get("path").getAsString()).getAbsoluteFile()); new File(args.get("path").getAsString()).getAbsoluteFile(), true);
resultCallback.onResult("done"); resultCallback.onResult("done");
} }
@ -87,25 +84,22 @@ public class TemporyTestAction {
@Action(async = true) @Action(async = true)
public void batchCompile(JsonObject args, ResultCallback resultCallback) { public void batchCompile(JsonObject args, ResultCallback resultCallback) {
ContractManagerFrameHandler.executorService.execute( ContractManagerFrameHandler.executorService.execute(() -> {
() -> { File privateDir = new File(GlobalConf.instance.privateDir);
File privateDir = new File(GlobalConf.instance.privateDir); File[] list = privateDir.listFiles(new FileFilter() {
File[] list = @Override
privateDir.listFiles( public boolean accept(File pathname) {
new FileFilter() { return pathname.isDirectory();
@Override }
public boolean accept(File pathname) { });
return pathname.isDirectory(); totalCompile = list.length;
} compileProgress = 0;
}); if (list != null)
totalCompile = list.length; for (File userDir : list) {
compileProgress = 0; compileProgress++;
if (list != null) compileAllUserProject(userDir);
for (File userDir : list) { }
compileProgress++; });
compileAllUserProject(userDir);
}
});
peekComplieProgress(args, resultCallback); peekComplieProgress(args, resultCallback);
} }
@ -172,6 +166,10 @@ public class TemporyTestAction {
Contract c = new Contract(); Contract c = new Contract();
c.setScript(args.get("script").getAsString()); c.setScript(args.get("script").getAsString());
c.setOwner(args.get("owner").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); ContractManager.instance.allocateKeyIfNotExists(c);
JsonObject jo = JsonUtil.parseObjectAsJsonObject(c); JsonObject jo = JsonUtil.parseObjectAsJsonObject(c);
jo.addProperty("action", "onAllocateKeyPair"); jo.addProperty("action", "onAllocateKeyPair");
@ -179,10 +177,17 @@ public class TemporyTestAction {
} }
@Action(async = true) @Action(async = true)
public void reconnectAll(JsonObject args, ResultCallback resultCallback) { public void reconnectPort(JsonObject args, ResultCallback resultCallback) {
ContractManager.instance.reconnectContractProcess(); ContractPort.PortVisitor reconnectVisitor = CMActions.manager.statusRecorder.getVisitor();
String data = ContractManager.instance.listContractsWithOwner( String host = null;
args.get("owner").getAsString(), null, 0); 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(); JsonObject ret = new JsonObject();
ret.addProperty("data", data); ret.addProperty("data", data);
ret.addProperty("action", "onReconnectAll"); ret.addProperty("action", "onReconnectAll");

View File

@ -21,18 +21,15 @@ import java.util.concurrent.ConcurrentLinkedQueue;
// 节点管理员用户管理页面 // 节点管理员用户管理页面
public class UserManagerAction { public class UserManagerAction {
public static final String NodeManager = "__NodeManager__"; public static final String NodeManager = "__NodeManager__";
static final Counter counter = static final Counter counter = Counter.build().name("contract_login_counter")
Counter.build() .help("contract login counter").register();
.name("contract_login_counter")
.help("contract login counter")
.register();
private static final Logger LOGGER = LogManager.getLogger(UserManagerAction.class); private static final Logger LOGGER = LogManager.getLogger(UserManagerAction.class);
public static ConcurrentLinkedQueue<String> contractInstanceMangerPool = public static ConcurrentLinkedQueue<String> contractInstanceMangerPool =
new ConcurrentLinkedQueue<>(); new ConcurrentLinkedQueue<>();
private final Random random = new Random(); private final Random random = new Random();
ContractManagerFrameHandler handler; ContractManagerFrameHandler handler;
private String sessionID; private String sessionID;
private String pubKey; private String publicKey;
public static String getNodeManager() { public static String getNodeManager() {
return KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), NodeManager); return KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), NodeManager);
@ -42,6 +39,36 @@ public class UserManagerAction {
handler = contractManagerFrameHandler; 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) @Action(userPermission = 0)
public void getSessionID(JsonObject json, ResultCallback resultCallback) { public void getSessionID(JsonObject json, ResultCallback resultCallback) {
sessionID = random.nextLong() + "_session_node"; sessionID = random.nextLong() + "_session_node";
@ -51,13 +78,13 @@ public class UserManagerAction {
@Action(userPermission = 0) @Action(userPermission = 0)
public void login(JsonObject json, ResultCallback resultCallback) { public void login(JsonObject json, ResultCallback resultCallback) {
if (sessionID == null) { if (sessionID == null) {
resultCallback.onResult("{\"action\":\"onLogin\",\"status\":\"failed, no session\"," + resultCallback.onResult("{\"action\":\"onLogin\",\"status\":\"failed, no session\","
"\"data\":\"" + Role.Anonymous.name() + "\"}"); + "\"data\":\"" + Role.Anonymous.name() + "\"}");
} }
LOGGER.debug("[NodeManagerAction] session:" + (sessionID)); LOGGER.debug("[NodeManagerAction] session:" + (sessionID));
String pubKey = json.get("pubKey").getAsString(); String pubKey = json.get("pubKey").getAsString();
if (SM2Util.plainStrVerify(pubKey, sessionID, json.get("signature").getAsString())) { if (SM2Util.plainStrVerify(pubKey, sessionID, json.get("signature").getAsString())) {
this.pubKey = pubKey; this.publicKey = pubKey;
counter.inc(); counter.inc();
// 是否有该用户的私有目录,没有就创建 // 是否有该用户的私有目录,没有就创建
// BUG修复需要仅在验证通过后可创建 // BUG修复需要仅在验证通过后可创建
@ -67,16 +94,22 @@ public class UserManagerAction {
} }
getNodeRole(json, resultCallback); getNodeRole(json, resultCallback);
} else { } else {
handler.setPermission(0);
resultCallback.onResult( setHandlerPermission(0);
"{\"action\":\"onLogin\",\"status\":\"verify sign failed\"," + resultCallback.onResult("{\"action\":\"onLogin\",\"status\":\"verify sign failed\","
"\"data\":\"" + Role.Anonymous.name() + "\"}"); + "\"data\":\"" + Role.Anonymous.name() + "\"}");
} }
} }
private void setHandlerPermission(long i) {
if (handler != null)
handler.setPermission(i);
}
public void getNodeRole(JsonObject json, ResultCallback resultCallback) { public void getNodeRole(JsonObject json, ResultCallback resultCallback) {
if (pubKey == null) { String pubKey = getPubKey(json);
if (getPubKey(json) == null) {
resultCallback.onResult( resultCallback.onResult(
"{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}"); "{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}");
return; return;
@ -89,30 +122,30 @@ public class UserManagerAction {
ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey); ret = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey);
if (isNodeManager) { if (isNodeManager) {
if (ret != null && ret.length() > 0) { if (ret != null && ret.length() > 0) {
handler.setPermission(0x86000f41L | Role.compoundValue(ret.split(","))); setHandlerPermission(0x86000f41L | Role.compoundValue(ret.split(",")));
resultCallback.onResult( resultCallback
"{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}"); .onResult("{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}");
} else { } else {
handler.setPermission(0x86000f41L); setHandlerPermission(0x86000f41L);
resultCallback.onResult( resultCallback
"{\"action\":\"onLogin\",\"data\":\"NodeManager\"}"); .onResult("{\"action\":\"onLogin\",\"data\":\"NodeManager\"}");
} }
} else { } else {
if (ret == null || ret.equals("")) { if (ret == null || ret.equals("")) {
ret = Role.Anonymous.name(); ret = Role.Anonymous.name();
handler.setPermission(0); setHandlerPermission(0);
} else { } else {
handler.setPermission(Role.compoundValue(ret.split(","))); setHandlerPermission(Role.compoundValue(ret.split(",")));
} }
resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}"); resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"" + ret + "\"}");
} }
} else { } else {
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), NodeManager, pubKey); KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(), NodeManager, pubKey);
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
CMTables.NodeRole.toString(), pubKey, "NodeManager"); "NodeManager");
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
CMTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
handler.setPermission(0x86000d41L); setHandlerPermission(0x86000d41L);
resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"NodeManager\"}"); resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"NodeManager\"}");
} }
} catch (Exception e) { } catch (Exception e) {
@ -124,7 +157,7 @@ public class UserManagerAction {
@Action(userPermission = 0) @Action(userPermission = 0)
public void applyNodeRole(JsonObject json, ResultCallback resultCallback) { public void applyNodeRole(JsonObject json, ResultCallback resultCallback) {
if (pubKey == null) { if (getPubKey(json) == null) {
resultCallback.onResult("{\"action\":\"onApplyNodeRole\",\"data\":\"missing pubKey\"}"); resultCallback.onResult("{\"action\":\"onApplyNodeRole\",\"data\":\"missing pubKey\"}");
return; return;
} }
@ -133,7 +166,7 @@ public class UserManagerAction {
LOGGER.debug("[role]" + ro); LOGGER.debug("[role]" + ro);
Role role = Role.parse(json.get("role").getAsString()); Role role = Role.parse(json.get("role").getAsString());
if (role != Role.Anonymous) { if (role != Role.Anonymous) {
applyNodeRoleAtDB(role, resultCallback); applyNodeRoleAtDB(role, getPubKey(json), resultCallback);
return; return;
} }
} }
@ -141,7 +174,7 @@ public class UserManagerAction {
"{\"action\":\"onApplyNodeRole\",\"data\":\"success\",\"role\":\"" + ro + "\"}"); "{\"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 str = KeyValueDBUtil.instance.getValue(CMTables.ApplyRole.toString(), pubKey);
String already = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey); String already = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), pubKey);
if (already != null && already.contains(role.toString())) { if (already != null && already.contains(role.toString())) {
@ -154,11 +187,11 @@ public class UserManagerAction {
} else { } else {
if (!str.contains(role.name())) if (!str.contains(role.name()))
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ApplyRole.toString(), pubKey,
CMTables.ApplyRole.toString(), pubKey, str + "," + role.name()); str + "," + role.name());
} }
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.ApplyTime.toString(), pubKey,
CMTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"success\"}"); resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"success\"}");
} }
@ -238,8 +271,8 @@ public class UserManagerAction {
if (roString.length() > 0) { if (roString.length() > 0) {
roString = new StringBuilder(roString.subSequence(0, roString.length() - 1).toString()); roString = new StringBuilder(roString.subSequence(0, roString.length() - 1).toString());
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
CMTables.NodeRole.toString(), pubKey, roString.toString()); roString.toString());
} else { } else {
KeyValueDBUtil.instance.delete(CMTables.NodeRole.toString(), pubKey); KeyValueDBUtil.instance.delete(CMTables.NodeRole.toString(), pubKey);
KeyValueDBUtil.instance.delete(CMTables.NodeTime.toString(), pubKey); KeyValueDBUtil.instance.delete(CMTables.NodeTime.toString(), pubKey);
@ -250,6 +283,9 @@ public class UserManagerAction {
@Action(userPermission = 1 << 10) @Action(userPermission = 1 << 10)
public void authNodeRole(JsonObject json, ResultCallback resultCallback) { public void authNodeRole(JsonObject json, ResultCallback resultCallback) {
String pubKey = json.get("pubKey").getAsString(); String pubKey = json.get("pubKey").getAsString();
if (json.has("authorizedPubKey")) {
pubKey = json.get("authorizedPubKey").getAsString();
}
boolean isAccept = json.get("isAccept").getAsBoolean(); boolean isAccept = json.get("isAccept").getAsBoolean();
if (pubKey == null || pubKey.length() == 0) { if (pubKey == null || pubKey.length() == 0) {
resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"missing pubKey\"}"); resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"missing pubKey\"}");
@ -277,8 +313,8 @@ public class UserManagerAction {
already = roles; already = roles;
} }
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey, already); KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey, already);
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
CMTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
KeyValueDBUtil.instance.delete(CMTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(CMTables.ApplyRole.toString(), pubKey);
KeyValueDBUtil.instance.delete(CMTables.ApplyTime.toString(), pubKey); KeyValueDBUtil.instance.delete(CMTables.ApplyTime.toString(), pubKey);
resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"success\"}"); resultCallback.onResult("{\"action\":\"onAuthNodeRole\",\"data\":\"success\"}");
@ -286,15 +322,26 @@ public class UserManagerAction {
@Action(userPermission = 0) @Action(userPermission = 0)
public void queryRole(JsonObject args, final ResultCallback resultCallback) { public void queryRole(JsonObject args, final ResultCallback resultCallback) {
if (pubKey == null) { String pubkey = getPubKey(args);
if (pubkey == null) {
resultCallback.onResult("{\"action\":\"onQueryRole\",\"data\":\"anonymous\"}"); resultCallback.onResult("{\"action\":\"onQueryRole\",\"data\":\"anonymous\"}");
return; return;
} }
args.addProperty("pubKey", pubKey); args.addProperty("pubKey", pubkey);
args.addProperty("requestID", random.nextLong() + "_ID"); args.addProperty("requestID", random.nextLong() + "_ID");
} }
public String getPubKey() { 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;
}
} }
} }

View File

@ -20,8 +20,8 @@ import java.util.Map;
import java.util.TimerTask; import java.util.TimerTask;
public class AliveCheckClientAction { public class AliveCheckClientAction {
public static final int sendDelay = 2000; public static final int sendDelay = 20000;
public static final int checkDelay = 5000; public static final int checkDelay = 50000;
private static final Logger LOGGER = LogManager.getLogger(AliveCheckClientAction.class); private static final Logger LOGGER = LogManager.getLogger(AliveCheckClientAction.class);
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss"); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss");
private final String masterPubkey; private final String masterPubkey;
@ -57,76 +57,65 @@ public class AliveCheckClientAction {
public void checkMasterAlive(ResultCallback rc) { public void checkMasterAlive(ResultCallback rc) {
if (sendPingTask == null) if (sendPingTask == null)
sendPingTask = sendPingTask = new TimerTask() {
new TimerTask() { @Override
@Override public void run() {
public void run() { try {
try { LOGGER.debug(String.format("f %s",
LOGGER.debug( DATE_FORMAT.format(System.currentTimeMillis())));
String.format( Map<String, String> ping = new HashMap<>();
"f %s", ping.put("action", "masterPing");
DATE_FORMAT rc.onResult(JsonUtil.toJson(ping));
.format(System.currentTimeMillis()))); } catch (Throwable t) {
Map<String, String> ping = new HashMap<>(); t.printStackTrace();
ping.put("action", "masterPing"); }
rc.onResult(JsonUtil.toJson(ping)); }
} catch (Throwable t) { };
t.printStackTrace();
}
}
};
if (null == checkAliveTask) if (null == checkAliveTask)
checkAliveTask = checkAliveTask = new TimerTask() {
new TimerTask() { @Override
@Override public void run() {
public void run() { try {
try { run1();
run1(); } catch (Throwable t) {
} catch (Throwable t) { t.printStackTrace();
t.printStackTrace(); HeartBeatUtil.getInstance().cancel(this);
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();
} }
} }
NetworkManager.instance.closeAgent(masterPubkey);
public void run1() { } // if
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
}
};
lastMasterPongTime = System.currentTimeMillis(); lastMasterPongTime = System.currentTimeMillis();
LOGGER.info( LOGGER.info("lastMasterPongTime=" + DATE_FORMAT.format(lastMasterPongTime));
"lastMasterPongTime=" + DATE_FORMAT.format(lastMasterPongTime));
HeartBeatUtil.getInstance().schedule(sendPingTask, sendDelay / 2, checkDelay); HeartBeatUtil.getInstance().schedule(sendPingTask, sendDelay / 2, checkDelay);
HeartBeatUtil.getInstance().schedule(checkAliveTask, sendDelay, checkDelay); HeartBeatUtil.getInstance().schedule(checkAliveTask, sendDelay, checkDelay);
} }
@ -138,7 +127,8 @@ public class AliveCheckClientAction {
public void waitForSetNodeID() { public void waitForSetNodeID() {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
if (waitForSetNode) return; if (waitForSetNode)
return;
try { try {
Thread.sleep(30); Thread.sleep(30);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -150,9 +140,9 @@ public class AliveCheckClientAction {
public void init() { public void init() {
LOGGER.info("init nodeID:"); LOGGER.info("init nodeID:");
assert masterPubkey != null; assert masterPubkey != null;
NetworkManager.instance.sendToAgent(masterPubkey, "{\"action\":\"setNodeInfo\",\"pubKey\":\"" NetworkManager.instance.sendToAgent(masterPubkey,
+ GlobalConf.instance.keyPair.getPublicKeyStr() "{\"action\":\"setNodeInfo\",\"pubKey\":\""
+ "\"}"); + GlobalConf.instance.keyPair.getPublicKeyStr() + "\"}");
} }
} }

View File

@ -11,7 +11,6 @@ import org.bdware.server.action.Action;
import org.bdware.server.tcp.TCPServerFrameHandler; import org.bdware.server.tcp.TCPServerFrameHandler;
import org.bdware.units.NetworkManager; import org.bdware.units.NetworkManager;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
public class AliveCheckServerAction { public class AliveCheckServerAction {
@ -23,18 +22,12 @@ public class AliveCheckServerAction {
public String pubKey; public String pubKey;
public AliveCheckServerAction(TCPServerFrameHandler handler) { public AliveCheckServerAction(TCPServerFrameHandler handler) {
lastSlavePingTime = lastSlavePingTime = System.currentTimeMillis() + 5000L + AliveCheckClientAction.sendDelay
System.currentTimeMillis() + AliveCheckClientAction.checkDelay;
+ 5000L
+ AliveCheckClientAction.sendDelay
+ AliveCheckClientAction.checkDelay;
checkAliveTask = new HeartBeatTask(AliveCheckClientAction.sendDelay); checkAliveTask = new HeartBeatTask(AliveCheckClientAction.sendDelay);
HeartBeatUtil.getInstance() HeartBeatUtil.getInstance().schedule(checkAliveTask, AliveCheckClientAction.sendDelay,
.schedule( AliveCheckClientAction.checkDelay);
checkAliveTask,
AliveCheckClientAction.sendDelay,
AliveCheckClientAction.checkDelay);
this.handler = handler; this.handler = handler;
} }
@ -57,10 +50,10 @@ public class AliveCheckServerAction {
try { try {
long cur = System.currentTimeMillis(); long cur = System.currentTimeMillis();
if (cur - lastSlavePingTime >= (2L * delay)) { if (cur - lastSlavePingTime >= (2L * delay)) {
LOGGER.info( // LOGGER.info(
new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(lastSlavePingTime) // new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss").format(lastSlavePingTime)
+ " " // + " "
+ delay); // + delay);
Set<String> contracts = new HashSet<>(); Set<String> contracts = new HashSet<>();
String nodeID = pubKey; String nodeID = pubKey;
if (nodeID == null) { if (nodeID == null) {
@ -68,21 +61,17 @@ public class AliveCheckServerAction {
HeartBeatUtil.getInstance().cancel(this); HeartBeatUtil.getInstance().cancel(this);
return; return;
} }
LOGGER.info( LOGGER.info("Master心跳机制发现节点 " + nodeID.substring(0, 5) + " 下线! "
"Master心跳机制发现节点 " + this.toString());
+ nodeID.substring(0, 5)
+ " 下线! "
+ this.toString());
HeartBeatUtil.getInstance().cancel(this); HeartBeatUtil.getInstance().cancel(this);
for (String contractID : for (String contractID : MasterServerRecoverMechAction.recoverStatus.get(nodeID)
MasterServerRecoverMechAction.recoverStatus.get(nodeID).keySet()) { .keySet()) {
// RecoverFlag flag = // RecoverFlag flag =
// //
// MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID); // MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID);
// if (flag == RecoverFlag.Fine) // if (flag == RecoverFlag.Fine)
MasterServerRecoverMechAction.recoverStatus MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID,
.get(nodeID) RecoverFlag.ToRecover);
.put(contractID, RecoverFlag.ToRecover);
contracts.add(contractID); contracts.add(contractID);
MasterServerTCPAction.notifyNodeOffline(contractID, nodeID); MasterServerTCPAction.notifyNodeOffline(contractID, nodeID);
} }

View File

@ -14,8 +14,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class CommunicationAction implements OnHashCallback { public class CommunicationAction implements OnHashCallback {
public CommunicationAction() { public CommunicationAction() {}
}
@Action(async = true) @Action(async = true)
public void updatePeerID(JsonObject jsonObject, final ResultCallback callback) { public void updatePeerID(JsonObject jsonObject, final ResultCallback callback) {
@ -26,13 +25,14 @@ public class CommunicationAction implements OnHashCallback {
@Action(async = true) @Action(async = true)
public void updateContractID(JsonObject jsonObject, final ResultCallback callback) { public void updateContractID(JsonObject jsonObject, final ResultCallback callback) {
ExecutionManager.instance.setNextContractID(jsonObject.get("content").getAsString()); 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) @Action(async = true)
public void onGetP2PNodes(JsonObject args, final ResultCallback rc) { public void onGetP2PNodes(JsonObject args, final ResultCallback rc) {
String requestID = args.get(BaseFunctionManager.REQUEST_ID).getAsString(); String requestID = args.get(BaseFunctionManager.REQUEST_ID).getAsString();
ResultCallback resultCallback = CommunicationManager.instance.requestCallbacks.remove(requestID); ResultCallback resultCallback =
CommunicationManager.instance.requestCallbacks.remove(requestID);
String allPeers; String allPeers;
allPeers = args.get("content").getAsString(); allPeers = args.get("content").getAsString();
System.out.println("String coding " + allPeers); System.out.println("String coding " + allPeers);
@ -46,6 +46,6 @@ public class CommunicationAction implements OnHashCallback {
r.put("action", "onHashResult"); r.put("action", "onHashResult");
r.put("responseID", reqID); r.put("responseID", reqID);
r.put("data", hashStr); r.put("data", hashStr);
CommunicationManager.instance.send(JsonUtil.toJson(r), new String[]{}); CommunicationManager.instance.send(JsonUtil.toJson(r), new String[] {});
} }
} }

View File

@ -19,8 +19,6 @@ import org.bdware.server.GlobalConf;
import org.bdware.server.action.Action; import org.bdware.server.action.Action;
import org.bdware.server.action.CMActions; import org.bdware.server.action.CMActions;
import org.bdware.server.action.RequestToMaster; 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.tcp.TCPClientFrameHandler;
import org.bdware.server.trustedmodel.AgentManager; import org.bdware.server.trustedmodel.AgentManager;
import org.bdware.server.trustedmodel.ContractUnitStatus; import org.bdware.server.trustedmodel.ContractUnitStatus;
@ -36,26 +34,24 @@ import java.util.zip.GZIPOutputStream;
public class MasterClientRecoverMechAction { public class MasterClientRecoverMechAction {
private static final Logger LOGGER = LogManager.getLogger(MasterClientRecoverMechAction.class); private static final Logger LOGGER = LogManager.getLogger(MasterClientRecoverMechAction.class);
public static Set<String> recoverSet = public static Set<String> recoverSet = new ConcurrentSkipListSet<>(); // contracts which don't
new ConcurrentSkipListSet<>(); // contracts which don't finish recoverRestart // finish recoverRestart
public static Map<String, Queue<RequestToMaster>> public static Map<String, Queue<RequestToMaster>> requestsToMaster; // when master is
requestsToMaster; // when master is re-electing,client node cache ites received requests // re-electing,client node
// cache ites received
// requests
private final Map<String, OutputStream> stateFileMap = new HashMap<>(); private final Map<String, OutputStream> stateFileMap = new HashMap<>();
private final Map<String, OutputStream> transFileMap = new HashMap<>(); private final Map<String, OutputStream> transFileMap = new HashMap<>();
public MasterClientRecoverMechAction() { public MasterClientRecoverMechAction() {}
}
// 告知master自己需要恢复携带之前的运行模式如果是StableMode需要携带lastExeSeq信息 // 告知master自己需要恢复携带之前的运行模式如果是StableMode需要携带lastExeSeq信息
public static void askForRecover(String contractID, String masterID, String nodeID) { public static void askForRecover(String contractID, String masterID, String nodeID) {
RecoverMechTimeRecorder.startRecover = System.currentTimeMillis(); RecoverMechTimeRecorder.startRecover = System.currentTimeMillis();
// recoverSet.add(contractID); // recoverSet.add(contractID);
LOGGER.info( LOGGER.info("[MasterClientRecoverMechAction] askForRecover contractID=" + contractID
"[MasterClientRecoverMechAction] askForRecover contractID=" + " masterID=" + masterID.substring(0, 5));
+ contractID
+ " masterID="
+ masterID.substring(0, 5));
TCPClientFrameHandler master = AgentManager.getHandler(masterID); TCPClientFrameHandler master = AgentManager.getHandler(masterID);
Map<String, String> ret = new HashMap<String, String>(); Map<String, String> ret = new HashMap<String, String>();
@ -63,27 +59,22 @@ public class MasterClientRecoverMechAction {
ret.put("contractID", contractID); ret.put("contractID", contractID);
ret.put("nodeID", nodeID); ret.put("nodeID", nodeID);
/* //没有这个合约进程 /*
if(!MasterClientTCPAction.requests.containsKey(contractID)){ * //没有这个合约进程 if(!MasterClientTCPAction.requests.containsKey(contractID)){
if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){ * if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){
logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq"); * logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq");
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(),contractID); * KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(),contractID); } }else{
} * ContractClient client = CMActions.manager.getClient(contractID); ContractExecuteInfo cei
}else{ * = MasterClientTCPAction.requests.get(contractID); if (client == null ||
ContractClient client = CMActions.manager.getClient(contractID); * !client.getContractType().equals(cei.type)){
ContractExecuteInfo cei = MasterClientTCPAction.requests.get(contractID); * if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){
if (client == null || !client.getContractType().equals(cei.type)){ * logger.info("[MasterClientRecoverMechAction] delete database lastExeSeq");
if(KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),contractID)){ * KeyValueDBUtil.instance.delete(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)) { if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) {
ret.put("mode", ContractUnitStatus.StableMode.toString()); ret.put("mode", ContractUnitStatus.StableMode.toString());
ret.put( ret.put("lastExeSeq",
"lastExeSeq",
KeyValueDBUtil.instance.getValue(CMTables.LastExeSeq.toString(), contractID)); KeyValueDBUtil.instance.getValue(CMTables.LastExeSeq.toString(), contractID));
} else { } else {
ret.put("mode", ContractUnitStatus.CommonMode.toString()); ret.put("mode", ContractUnitStatus.CommonMode.toString());
@ -100,20 +91,17 @@ public class MasterClientRecoverMechAction {
public static void stableModeCheckpoint(String contractID) { public static void stableModeCheckpoint(String contractID) {
LOGGER.info("设置合约" + contractID + " stable模式下的 checkpoint"); LOGGER.info("设置合约" + contractID + " stable模式下的 checkpoint");
final MultiContractMeta cei; final MultiContractMeta cei;
synchronized ( synchronized (cei =
cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)) { CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID,
CMTables.LastExeSeq.toString(), contractID, cei.getLastExeSeq() + ""); cei.getLastExeSeq() + "");
CMActions.manager.clearSyncFiles(contractID); CMActions.manager.clearSyncFiles(contractID);
// TODO 将ContractExecuteInfo持久化 // TODO 将ContractExecuteInfo持久化
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String fileName = String fileName = "ContractExecuteInfo_" + format.format(new Date()) + "_"
"ContractExecuteInfo_" + new Random().nextInt();
+ format.format(new Date())
+ "_"
+ new Random().nextInt();
String parPath = GlobalConf.instance.ADSPDir + "/" + contractID + "/cr"; String parPath = GlobalConf.instance.ADSPDir + "/" + contractID + "/cr";
File file = new File(parPath + "/" + fileName); File file = new File(parPath + "/" + fileName);
try { try {
@ -137,7 +125,8 @@ public class MasterClientRecoverMechAction {
public void saveStateFile(String path, ContractRecord record) { public void saveStateFile(String path, ContractRecord record) {
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path); File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.exists()) parent.mkdirs(); if (!parent.exists())
parent.mkdirs();
ObjectOutputStream writer; ObjectOutputStream writer;
try { try {
FileOutputStream fileout = new FileOutputStream(file); FileOutputStream fileout = new FileOutputStream(file);
@ -172,7 +161,8 @@ public class MasterClientRecoverMechAction {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else fout = (FileOutputStream) stateFileMap.get(fileName); } else
fout = (FileOutputStream) stateFileMap.get(fileName);
String data = args.get("data").getAsString(); String data = args.get("data").getAsString();
try { try {
@ -207,27 +197,27 @@ public class MasterClientRecoverMechAction {
recoverFromCommonMode(path, false, rc); recoverFromCommonMode(path, false, rc);
} }
// //无状态合约同步lastExeSeq之后发送恢复完成 // //无状态合约同步lastExeSeq之后发送恢复完成
// public void statelessContract(String contractID){ // public void statelessContract(String contractID){
// logger.info("无状态合约请求unitLastExeSeq"); // logger.info("无状态合约请求unitLastExeSeq");
// Map<String, String> ret = new HashMap<String, String>(); // Map<String, String> ret = new HashMap<String, String>();
// ret.put("action", "statelessUnitLastExe"); // ret.put("action", "statelessUnitLastExe");
// ret.put("contractID", contractID); // ret.put("contractID", contractID);
// ret.put("nodeID",GlobalConf.instance.keyPair.getPublicKeyStr()); // ret.put("nodeID",GlobalConf.instance.keyPair.getPublicKeyStr());
// handler.sendMsg(JsonUtil.toJson(ret)); // handler.sendMsg(JsonUtil.toJson(ret));
// } // }
// //
// @Action(async = true) // @Action(async = true)
// public void statelessReceiveLastExe(JsonObject args, final ResultCallback rc) { // public void statelessReceiveLastExe(JsonObject args, final ResultCallback rc) {
// String contractID = args.get("contractID").getAsString(); // String contractID = args.get("contractID").getAsString();
// int unitLast = args.get("unitLastExe").getAsInt(); // int unitLast = args.get("unitLastExe").getAsInt();
// //
// ContractExecuteInfo cei = MasterClientTCPAction.requests.get(contractID); // ContractExecuteInfo cei = MasterClientTCPAction.requests.get(contractID);
// synchronized(cei){ // synchronized(cei){
// cei.setLastExeSeq(unitLast); // cei.setLastExeSeq(unitLast);
// } // }
// sendRecoverFinish(contractID,"stateless"); // sendRecoverFinish(contractID,"stateless");
// } // }
// 普通节点从CommonMode恢复 // 普通节点从CommonMode恢复
public void recoverFromCommonMode(String path, boolean isMaster, ResultCallback rc) { public void recoverFromCommonMode(String path, boolean isMaster, ResultCallback rc) {
@ -254,9 +244,8 @@ public class MasterClientRecoverMechAction {
} }
// cei.printContent(); // cei.printContent();
if (cei.queue == null) { if (cei.queue == null) {
cei.init( cei.init(CMActions.manager.multiContractRecorder
CMActions.manager.multiContractRecorder.getMultiContractMeta( .getMultiContractMeta(cei.getContractID()));
cei.getContractID()));
} }
recoverSet.add(cei.getContractID()); recoverSet.add(cei.getContractID());
@ -266,12 +255,12 @@ public class MasterClientRecoverMechAction {
return; return;
} }
// //无状态合约 // //无状态合约
// String stateful = CMActions.manager.getContractStateful(cei.getContractID()); // String stateful = CMActions.manager.getContractStateful(cei.getContractID());
// if(stateful.equals("false")){ // if(stateful.equals("false")){
// statelessContract(cei.getContractID()); // statelessContract(cei.getContractID());
// return; // return;
// } // }
if (isMaster) { if (isMaster) {
CMActions.manager.setContractIsMaster(cei.getContractID(), "true"); CMActions.manager.setContractIsMaster(cei.getContractID(), "true");
@ -300,12 +289,9 @@ public class MasterClientRecoverMechAction {
try { try {
LOGGER.info(" 开始等待 checkAndRestart锁 : "); LOGGER.info(" 开始等待 checkAndRestart锁 : ");
time1 = System.currentTimeMillis(); time1 = System.currentTimeMillis();
LOGGER.info( LOGGER.info("time1="
"time1=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1))
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + " " + time1);
.format(new Date(time1))
+ " "
+ time1);
ContractManager.checkAndStartLock.wait(10000); ContractManager.checkAndStartLock.wait(10000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
@ -314,10 +300,8 @@ public class MasterClientRecoverMechAction {
long time2 = System.currentTimeMillis(); long time2 = System.currentTimeMillis();
LOGGER.info( LOGGER.info(
"time2=" "time2=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time2))
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time2)) + " " + time2);
+ " "
+ time2);
LOGGER.info("等待 checkAndRestart锁 结束 "); LOGGER.info("等待 checkAndRestart锁 结束 ");
if (time2 - time1 >= 10000) { if (time2 - time1 >= 10000) {
LOGGER.info("[ADSP]CheckAndStart 锁 等待超时"); LOGGER.info("[ADSP]CheckAndStart 锁 等待超时");
@ -335,42 +319,27 @@ public class MasterClientRecoverMechAction {
if (client == null) { if (client == null) {
System.out.println("恢复 checkAndRestart client == null\n"); System.out.println("恢复 checkAndRestart client == null\n");
} else { } else {
System.out.println( System.out.println("恢复 checkAndRestart client == null: false"
"恢复 checkAndRestart client == null: false" + " client.getContractType=" + client.getContractType() + "\n");
+ " client.getContractType="
+ client.getContractType()
+ "\n");
} }
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(cei.getContractID()); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(cei.getContractID());
meta.setContractExecutor( meta.setContractExecutor(MasterClientTCPAction.createContractExecutor(meta.contract,
MasterClientTCPAction.createContractExecutor(meta.contract, contractID, cei.getMasterNode(), cei.getMembers())); contractID, cei.getMasterNode(), cei.getMembers()));
switch (meta.contract.getType()) { Map<String, Object> args = new HashMap<>();
case RequestAllResponseFirst: args.put("ceiLastExeSeq", cei.getLastExeSeq());
case RequestAllResponseHalf: meta.contractExecutor.onRecover(args);
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;
}
// 认为contractID不会重 // 认为contractID不会重
if (client != null if (client != null && client.isProcessAlive()
&& client.isProcessAlive()
&& client.getContractType().equals(cei.type)) { && client.getContractType().equals(cei.type)) {
LOGGER.info("恢复节点已有这个合约进程!"); LOGGER.info("恢复节点已有这个合约进程!");
if (!KeyValueDBUtil.instance.containsKey( if (!KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(),
CMTables.UnitContracts.toString(), cei.getContractID())) { cei.getContractID())) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(),
CMTables.UnitContracts.toString(), cei.getContractID(), "exist"); cei.getContractID(), "exist");
} }
CMActions.manager.multiContractRecorder.updateValue(cei); CMActions.manager.multiContractRecorder.updateValue(cei);
//MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction); // MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction);
CMActions.manager.setContractIsMaster(contractID, "false"); CMActions.manager.setContractIsMaster(contractID, "false");
} else { } else {
LOGGER.info("[MasterClientRecoverMechAction] 恢复节点没有这个合约进程 : "); LOGGER.info("[MasterClientRecoverMechAction] 恢复节点没有这个合约进程 : ");
@ -386,22 +355,18 @@ public class MasterClientRecoverMechAction {
contract.setPublicKey(cei.publicKey); contract.setPublicKey(cei.publicKey);
int copies = cei.getMembers().length; int copies = cei.getMembers().length;
contract.setNumOfCopies(copies); contract.setNumOfCopies(copies);
if (!KeyValueDBUtil.instance.containsKey( if (!KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(),
CMTables.UnitContracts.toString(), cei.getContractID())) { cei.getContractID())) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(),
CMTables.UnitContracts.toString(), cei.getContractID(), "exist"); cei.getContractID(), "exist");
} }
CMActions.manager.multiContractRecorder.updateValue(cei); CMActions.manager.multiContractRecorder.updateValue(cei);
//MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction); // MasterClientTCPAction.contractID2MasterInfo.put(contractID, clientAction);
parPath = GlobalConf.instance.publicCompiledDir + "/" + cei.getYpkName(); parPath = GlobalConf.instance.publicCompiledDir + "/" + cei.getYpkName();
if (cei.isPrivate()) { if (cei.isPrivate()) {
parPath = parPath = GlobalConf.instance.privateCompiledDir + "/" + cei.getPubKeyPath()
GlobalConf.instance.privateCompiledDir + "/" + cei.getYpkName();
+ "/"
+ cei.getPubKeyPath()
+ "/"
+ cei.getYpkName();
} }
} }
@ -467,7 +432,8 @@ public class MasterClientRecoverMechAction {
String path = contractID + "_temp_stateFile_" + new Random().nextLong(); String path = contractID + "_temp_stateFile_" + new Random().nextLong();
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path); File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.exists()) parent.mkdirs(); if (!parent.exists())
parent.mkdirs();
ObjectOutputStream writer; ObjectOutputStream writer;
try { try {
FileOutputStream fileout = new FileOutputStream(file); FileOutputStream fileout = new FileOutputStream(file);
@ -504,7 +470,7 @@ public class MasterClientRecoverMechAction {
LOGGER.info("times=" + times); LOGGER.info("times=" + times);
int index = 1; // 数据包序号 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); // logger.info("len = " + len);
String data = ByteUtil.encodeBASE64(buff, len); String data = ByteUtil.encodeBASE64(buff, len);
req.put("data", data); req.put("data", data);
@ -524,9 +490,9 @@ public class MasterClientRecoverMechAction {
} }
fin.close(); fin.close();
// req.put("isDone", "true"); // req.put("isDone", "true");
// req.remove("data"); // req.remove("data");
// handler.sendMsg(JsonUtil.toJson(req)); // handler.sendMsg(JsonUtil.toJson(req));
// delete state file // delete state file
if (file.isFile() && file.exists()) { if (file.isFile() && file.exists()) {
@ -547,17 +513,12 @@ public class MasterClientRecoverMechAction {
int unitLastExeSeq = json.get("unitLastExeSeq").getAsInt(); int unitLastExeSeq = json.get("unitLastExeSeq").getAsInt();
recoverSet.add(contractID); recoverSet.add(contractID);
// 查看自己和master的lastExeSeq差距 // 查看自己和master的lastExeSeq差距
int lastExeSeq = int lastExeSeq = Integer.parseInt(
Integer.parseInt( KeyValueDBUtil.instance.getValue(CMTables.LastExeSeq.toString(), contractID));
KeyValueDBUtil.instance.getValue(
CMTables.LastExeSeq.toString(), contractID));
LOGGER.info("unitLastExeSeq=" + unitLastExeSeq + " lastExeSeq=" + lastExeSeq); LOGGER.info("unitLastExeSeq=" + unitLastExeSeq + " lastExeSeq=" + lastExeSeq);
LOGGER.info( LOGGER.info("从StableMode恢复采用先从本地load的方式 unitLastExeSeq=" + unitLastExeSeq
"从StableMode恢复采用先从本地load的方式 unitLastExeSeq=" + " lastExeDeq=" + lastExeSeq);
+ unitLastExeSeq
+ " lastExeDeq="
+ lastExeSeq);
// Step 1 查看最新的sync file // Step 1 查看最新的sync file
String parPath = GlobalConf.instance.ADSPDir + "/" + contractID + "/sync"; String parPath = GlobalConf.instance.ADSPDir + "/" + contractID + "/sync";
@ -613,50 +574,32 @@ public class MasterClientRecoverMechAction {
} }
// record.printContent(); // record.printContent();
if (cei.queue == null) { if (cei.queue == null) {
cei.init( cei.init(CMActions.manager.multiContractRecorder
CMActions.manager.multiContractRecorder.getMultiContractMeta( .getMultiContractMeta(cei.getContractID()));
cei.getContractID()));
} }
CMActions.manager.multiContractRecorder.updateValue(cei); CMActions.manager.multiContractRecorder.updateValue(cei);
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
switch (meta.contract.getType()) { Map<String, Object> args = new HashMap<>();
case RequestAllResponseFirst: args.put("ceiLastExeSeq", cei.getLastExeSeq());
case RequestAllResponseHalf: meta.contractExecutor.onRecover(args);
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;
}
boolean flag = checkAndRestart(cei); // if need,restart the contract process boolean flag = checkAndRestart(cei); // if need,restart the contract process
if (!flag) { if (!flag) {
return; return;
} }
// 无状态合约 // 无状态合约
// String stateful = CMActions.manager.getContractStateful(cei.getContractID()); // String stateful = CMActions.manager.getContractStateful(cei.getContractID());
// if(stateful.equals("false")){ // if(stateful.equals("false")){
// statelessContract(cei.getContractID()); // statelessContract(cei.getContractID());
// return; // return;
// } // }
// step3 loadMemory // step3 loadMemory
String memoryFileName = content[1].split(";")[1]; String memoryFileName = content[1].split(";")[1];
ContractClient client = ContractManager.instance.getClient(cei.getContractID()); ContractClient client = ContractManager.instance.getClient(cei.getContractID());
String temp1 = String temp1 = CMActions.manager.loadMemory(client,
CMActions.manager.loadMemory( GlobalConf.instance.ADSPDir + "/" + contractID + "/memory/" + memoryFileName);
client,
GlobalConf.instance.ADSPDir
+ "/"
+ contractID
+ "/memory/"
+ memoryFileName);
LOGGER.info("loadMemory结果" + temp1); LOGGER.info("loadMemory结果" + temp1);
// logger.info("测试位置 333 step3结束后状态为\n" + CMActions.manager.dumpContract(contractID, "")); // logger.info("测试位置 333 step3结束后状态为\n" + CMActions.manager.dumpContract(contractID, ""));
@ -664,10 +607,8 @@ public class MasterClientRecoverMechAction {
// step4 redo local trans // step4 redo local trans
String transFileName = content[2].split(";")[1]; String transFileName = content[2].split(";")[1];
String temp = String temp = CMActions.manager.redo(contractID,
CMActions.manager.redo( GlobalConf.instance.ADSPDir + "/" + contractID + "/trans/" + transFileName);
contractID,
GlobalConf.instance.ADSPDir + "/" + contractID + "/trans/" + transFileName);
LOGGER.info("redo from local结果" + temp); LOGGER.info("redo from local结果" + temp);
synchronized (cei) { synchronized (cei) {
cei.setLastExeSeq(lastExeSeq); // 通过数据库中值设置lastExeSeq和本地记录的ContractRecord是同步的 cei.setLastExeSeq(lastExeSeq); // 通过数据库中值设置lastExeSeq和本地记录的ContractRecord是同步的
@ -732,13 +673,12 @@ public class MasterClientRecoverMechAction {
SM2KeyPair keyPair = GlobalConf.instance.keyPair; SM2KeyPair keyPair = GlobalConf.instance.keyPair;
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
jo.addProperty("action", "onQueryUnitStatus"); jo.addProperty("action", "onQueryUnitStatus");
LOGGER.info( LOGGER.info("恢复节点查看自己的状态 "
"恢复节点查看自己的状态 " + KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID));
+ KeyValueDBUtil.instance.containsKey(
CMTables.LastExeSeq.toString(), contractID));
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID))
jo.addProperty("mode", ContractUnitStatus.StableMode.toString()); jo.addProperty("mode", ContractUnitStatus.StableMode.toString());
else jo.addProperty("mode", ContractUnitStatus.CommonMode.toString()); else
jo.addProperty("mode", ContractUnitStatus.CommonMode.toString());
// jo.addProperty("mode", // jo.addProperty("mode",
// KeyValueDBUtil.instance.getValue(CMTables.UnitStatus.toString(),contractID)); // KeyValueDBUtil.instance.getValue(CMTables.UnitStatus.toString(),contractID));
jo.addProperty("nodeID", keyPair.getPublicKeyStr()); jo.addProperty("nodeID", keyPair.getPublicKeyStr());
@ -753,7 +693,8 @@ public class MasterClientRecoverMechAction {
ContractUnitStatus cur_mode; ContractUnitStatus cur_mode;
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID))
cur_mode = ContractUnitStatus.StableMode; cur_mode = ContractUnitStatus.StableMode;
else cur_mode = ContractUnitStatus.CommonMode; else
cur_mode = ContractUnitStatus.CommonMode;
LOGGER.info("合约" + contractID + "当前模式为" + cur_mode + " 期望设置为" + mode); LOGGER.info("合约" + contractID + "当前模式为" + cur_mode + " 期望设置为" + mode);
if (cur_mode == ContractUnitStatus.CommonMode if (cur_mode == ContractUnitStatus.CommonMode
&& mode.equals(ContractUnitStatus.StableMode.toString())) { && mode.equals(ContractUnitStatus.StableMode.toString())) {
@ -767,10 +708,10 @@ public class MasterClientRecoverMechAction {
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID); KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID);
} }
} }
// else if(cur_mode == ContractUnitStatus.StableMode && // else if(cur_mode == ContractUnitStatus.StableMode &&
// mode.equals(ContractUnitStatus.StableMode.toString())){ // mode.equals(ContractUnitStatus.StableMode.toString())){
// stableModeCheckpoint(contractID); // stableModeCheckpoint(contractID);
// } // }
} }
@Action(async = true) @Action(async = true)
@ -803,7 +744,8 @@ public class MasterClientRecoverMechAction {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else fout = (FileOutputStream) transFileMap.get(fileName); } else
fout = (FileOutputStream) transFileMap.get(fileName);
String data = args.get("data").getAsString(); String data = args.get("data").getAsString();
try { try {

View File

@ -15,18 +15,12 @@ import org.bdware.sc.db.KeyValueDBUtil;
import org.bdware.sc.units.MultiContractMeta; import org.bdware.sc.units.MultiContractMeta;
import org.bdware.sc.units.PubKeyNode; import org.bdware.sc.units.PubKeyNode;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.sdk.consistency.ConsistencyPluginManager;
import org.bdware.server.GlobalConf; import org.bdware.server.GlobalConf;
import org.bdware.server.action.*; 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.tcp.PubkeyResultCallback;
import org.bdware.server.trustedmodel.ContractExecutor; import org.bdware.server.trustedmodel.ContractExecutor;
import org.bdware.server.trustedmodel.KillUnitContractInfo; 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.NetworkManager;
import org.bdware.units.function.ExecutionManager; import org.bdware.units.function.ExecutionManager;
import org.zz.gmhelper.SM2KeyPair; import org.zz.gmhelper.SM2KeyPair;
@ -55,28 +49,24 @@ public class MasterClientTCPAction {
SM2KeyPair keyPair = GlobalConf.instance.keyPair; SM2KeyPair keyPair = GlobalConf.instance.keyPair;
request.put("nodeID", keyPair.getPublicKeyStr()); request.put("nodeID", keyPair.getPublicKeyStr());
// if (!contractID2MasterInfo.containsKey(contractID)) { // if (!contractID2MasterInfo.containsKey(contractID)) {
// contractID = cc.getContractID(); // contractID = cc.getContractID();
// } // }
// //
// MasterClientTCPAction mcta = contractID2MasterInfo.get(contractID); // MasterClientTCPAction mcta = contractID2MasterInfo.get(contractID);
// if (mcta != null) { // if (mcta != null) {
// mcta.closeMaster(); // mcta.closeMaster();
// } // }
request.put("contractID", contractID); request.put("contractID", contractID);
int lastSeq = int lastSeq = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager .getLastExeSeq();
.multiContractRecorder
.getMultiContractMeta(contractID)
.getLastExeSeq();
request.put("lastExe", lastSeq + ""); request.put("lastExe", lastSeq + "");
request.put("master", "null"); // 不管之前master是哪个NC处是null所有节点重选 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 String members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
.multiContractRecorder
.getMultiContractMeta(contractID)
.joinMembers(","); .joinMembers(",");
request.put("members", members); request.put("members", members);
@ -84,47 +74,15 @@ public class MasterClientTCPAction {
LOGGER.info("认为合约 " + contractID + " 的master崩溃 当前master为null 向NC发送重选信息"); LOGGER.info("认为合约 " + contractID + " 的master崩溃 当前master为null 向NC发送重选信息");
} }
public static ContractExecutor createContractExecutor(Contract contract, String contractID, String masterPubkey, String[] members) { public static ContractExecutor createContractExecutor(Contract contract, String contractID,
ContractExecutor executor = null; String masterPubkey, String[] members) {
int nodeSize = contract.getNumOfCopies(); Map<String, Object> args = new HashMap<>();
switch (contract.getType()) { args.put("contractID", contractID);
case Sole: args.put("nodeSize", contract.getNumOfCopies());
LOGGER.info("Sole contract is not supported in multi-point mode"); args.put("masterPubkey", masterPubkey);
return SingleNodeExecutor.instance; args.put("members", members);
case RequestOnce: return ConsistencyPluginManager.getInstance()
executor = new RequestOnceExecutor(contractID); .createContractExecutor(contract.getType().name(), args);
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 void dealRequests(String contractID) { public static void dealRequests(String contractID) {
@ -182,13 +140,13 @@ public class MasterClientTCPAction {
LOGGER.info("返回 uniID=" + cei.uniReqIDMap.get(seq) + " 的结果"); LOGGER.info("返回 uniID=" + cei.uniReqIDMap.get(seq) + " 的结果");
cei.resultMap.get(seq).onResult(JsonUtil.toJson(ret)); cei.resultMap.get(seq).onResult(JsonUtil.toJson(ret));
cei.setLastExeSeq(seq); cei.setLastExeSeq(seq);
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
CMTables.LastExeSeq.toString(), contractID)) { contractID)) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(CMTables.LastExeSeq.toString(), contractID,
CMTables.LastExeSeq.toString(), contractID, seq + ""); seq + "");
} }
// ledger检查点 public static final int LEDGER_PERIOD = 100; //账本检查点 // ledger检查点 public static final int LEDGER_PERIOD = 100; //账本检查点
if (seq % 100 == 0) { if (seq % 100 == 0) {
LOGGER.info("遇到ledger检查点 seq=" + cei.getLastExeSeq()); LOGGER.info("遇到ledger检查点 seq=" + cei.getLastExeSeq());
boolean isMaster = CMActions.manager.getContractIsMaster(contractID); boolean isMaster = CMActions.manager.getContractIsMaster(contractID);
@ -196,9 +154,8 @@ public class MasterClientTCPAction {
String lastHash; String lastHash;
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(
CMTables.CheckPointLastHash.toString(), contractID)) { CMTables.CheckPointLastHash.toString(), contractID)) {
lastHash = lastHash = KeyValueDBUtil.instance.getValue(
KeyValueDBUtil.instance.getValue( CMTables.CheckPointLastHash.toString(), contractID);
CMTables.CheckPointLastHash.toString(), contractID);
} else { } else {
lastHash = "firstCheckPoint"; lastHash = "firstCheckPoint";
} }
@ -234,10 +191,8 @@ public class MasterClientTCPAction {
SM2KeyPair keyPair = GlobalConf.instance.keyPair; SM2KeyPair keyPair = GlobalConf.instance.keyPair;
ret.put("nodeID", keyPair.getPublicKeyStr()); ret.put("nodeID", keyPair.getPublicKeyStr());
ret.put("responseID", responseID); ret.put("responseID", responseID);
ContractResult cr = ContractResult cr = new ContractResult(ContractResult.Status.Error,
new ContractResult( new JsonPrimitive(ComponedContractResult.EXPIRED_REQ));
ContractResult.Status.Error,
new JsonPrimitive(ComponedContractResult.EXPIRED_REQ));
ret.put("data", JsonUtil.toJson(cr)); ret.put("data", JsonUtil.toJson(cr));
cb.onResult(JsonUtil.toJson(ret)); cb.onResult(JsonUtil.toJson(ret));
} }
@ -268,8 +223,7 @@ public class MasterClientTCPAction {
public void onKillContractProcess(JsonObject jo, ResultCallback result) { public void onKillContractProcess(JsonObject jo, ResultCallback result) {
LOGGER.info("[MasterClientTCPAction] : onKillContractProcess"); LOGGER.info("[MasterClientTCPAction] : onKillContractProcess");
if (killUnitContractMap.containsKey(jo.get("requestID").getAsString())) { if (killUnitContractMap.containsKey(jo.get("requestID").getAsString())) {
KillUnitContractInfo info = KillUnitContractInfo info = killUnitContractMap.get(jo.get("requestID").getAsString());
killUnitContractMap.get(jo.get("requestID").getAsString());
Map<String, Object> r = new HashMap<>(); Map<String, Object> r = new HashMap<>();
r.put("action", "onKillContractProcess"); r.put("action", "onKillContractProcess");
r.put("data", jo.get("data")); r.put("data", jo.get("data"));
@ -291,12 +245,12 @@ public class MasterClientTCPAction {
ContractClient cc = CMActions.manager.getClient(id); ContractClient cc = CMActions.manager.getClient(id);
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
CMTables.LastExeSeq.toString(), cc.getContractID())) { cc.getContractID())) {
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), cc.getContractID()); KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), cc.getContractID());
} }
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.UnitContracts.toString(),
CMTables.UnitContracts.toString(), cc.getContractID())) { cc.getContractID())) {
KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), cc.getContractID()); KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), cc.getContractID());
} }
File file = new File(GlobalConf.instance.ADSPDir + "/" + cc.getContractID()); File file = new File(GlobalConf.instance.ADSPDir + "/" + cc.getContractID());
@ -336,11 +290,11 @@ public class MasterClientTCPAction {
// 需要计算出自己的ShardingID路由规则id/requester/arg-->shardingId) // 需要计算出自己的ShardingID路由规则id/requester/arg-->shardingId)
// 也在MultiPointCooperationExecutor中实现 // 也在MultiPointCooperationExecutor中实现
} }
//TODO master连接 // TODO master连接
// contractID2MasterInfo.put(contractID, this); // 记录contractID master之间的对应关系 // contractID2MasterInfo.put(contractID, this); // 记录contractID master之间的对应关系
MultiContractMeta cei = MultiContractMeta multiContractMeta =
CMActions.manager.multiContractRecorder.createIfNotExist(contractID); CMActions.manager.multiContractRecorder.createIfNotExist(contractID);
cei.setLastExeSeq(-1); multiContractMeta.setLastExeSeq(-1);
if (!contract.getScriptStr().startsWith("/")) { if (!contract.getScriptStr().startsWith("/")) {
contract.setScript(dumpToDisk(contract, jo)); contract.setScript(dumpToDisk(contract, jo));
} }
@ -348,33 +302,31 @@ public class MasterClientTCPAction {
String[] pp = contract.getScriptStr().split("/"); String[] pp = contract.getScriptStr().split("/");
String parPath = GlobalConf.instance.publicCompiledDir; String parPath = GlobalConf.instance.publicCompiledDir;
synchronized (cei) { // 合约执行信息 // TODO 第一次来的meta没有cid!!!
cei.setYpkName(pp[pp.length - 1]); synchronized (multiContractMeta) { // 合约执行信息
cei.setMembers(jo.get("members").getAsJsonArray()); multiContractMeta.setYpkName(pp[pp.length - 1]);
cei.type = contract.getType(); multiContractMeta.setMembers(jo.get("members").getAsJsonArray());
cei.key = contract.getKey(); multiContractMeta.type = contract.getType();
cei.publicKey = contract.getPublicKey(); multiContractMeta.key = contract.getKey();
multiContractMeta.publicKey = contract.getPublicKey();
if (jo.has("isPrivate") && jo.get("isPrivate").getAsBoolean()) { if (jo.has("isPrivate") && jo.get("isPrivate").getAsBoolean()) {
parPath = parPath = GlobalConf.instance.privateCompiledDir + "/"
GlobalConf.instance.privateCompiledDir + jo.get("pubKey").getAsString();
+ "/"
+ jo.get("pubKey").getAsString();
cei.setIsPrivate(true); multiContractMeta.setIsPrivate(true);
cei.setPubKeyPath(jo.get("pubKey").getAsString()); multiContractMeta.setPubKeyPath(jo.get("pubKey").getAsString());
cei.setYpkName(pp[pp.length - 1]); multiContractMeta.setYpkName(pp[pp.length - 1]);
} }
} }
try { try {
File temp = new File(parPath, pp[pp.length - 1]); File temp = new File(parPath, pp[pp.length - 1]);
if (!temp.exists()) { if (!temp.exists()) {
result.onResult( result.onResult(String.format(
String.format( "{\"action\":\"onStartContractTrustfully\",\"result\":\"missing contract files\","
"{\"action\":\"onStartContractTrustfully\",\"result\":\"missing contract files\"," + + "\"requestID\":\"%s\",\"pubKey\":\"%s\"}",
"\"requestID\":\"%s\",\"pubKey\":\"%s\"}", jo.get("requestID").getAsString(),
jo.get("requestID").getAsString(), GlobalConf.instance.keyPair.getPublicKeyStr()));
GlobalConf.instance.keyPair.getPublicKeyStr()));
return; return;
} }
contract.setScript(temp.getAbsolutePath()); contract.setScript(temp.getAbsolutePath());
@ -382,31 +334,30 @@ public class MasterClientTCPAction {
e.printStackTrace(); e.printStackTrace();
} }
// 这个地方判定从参数中的master数据 globalConf中的数据 进行对比如果一致的话说明该节点为master // 这个地方判定从参数中的master数据 globalConf中的数据 进行对比如果一致的话说明该节点为master
cei.setMaster(jo.get("master").getAsString()); multiContractMeta.setMaster(jo.get("master").getAsString());
if (contract.getType().needSeq()) if (contract.getType().needSeq())
cei.setIsMaster(GlobalConf.getNodeID().equals(jo.get("master").getAsString())); multiContractMeta
.setIsMaster(GlobalConf.getNodeID().equals(jo.get("master").getAsString()));
else { else {
cei.setIsMaster(true); multiContractMeta.setIsMaster(true);
} }
CMActions.manager.multiContractRecorder.updateValue(multiContractMeta);
LOGGER.debug("startup arguments: " + JsonUtil.toJson(contract)); LOGGER.info("startup arguments: " + JsonUtil.toJson(contract));
String ret = CMActions.manager.startContract(contract); // 调用CMActions 里的启动合约的方法,启动结果 String ret = CMActions.manager.startContract(contract); // 调用CMActions 里的启动合约的方法,启动结果
LOGGER.info("startup result: " + ret);
LOGGER.debug("startup result: " + ret); // IMPORTANT!!!!!!!!!!
CMActions.manager.multiContractRecorder.updateValue(cei); // meta should get after manger.startContract, because startContract reInitilized it!
ContractMeta meta = CMActions.manager.statusRecorder.createIfNotExist(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
// IMPORTANT!!!!!!!!!!
meta.setContractExecutor(createContractExecutor(contract, contractID, jo.get("master").getAsString(), cei.getMembers())); // 分配不同的Executor meta.setContractExecutor(createContractExecutor(contract, contractID,
// TODO 合约终止后从数据库中移除但是为了测试可以人为制造合约终止但不从数据库中移除异常停止 jo.get("master").getAsString(), multiContractMeta.getMembers())); // 分配不同的Executor
// TODO 合约终止后从数据库中移除但是为了测试可以人为制造合约终止但不从数据库中移除异常停止
KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(), contractID, "exist"); KeyValueDBUtil.instance.setValue(CMTables.UnitContracts.toString(), contractID, "exist");
if (CMActions.manager.getClient(contract.getID()) != null) { if (CMActions.manager.getClient(contract.getID()) != null) {
result.onResult( result.onResult(
"{\"action\":\"onStartContractTrustfully\",\"result\":\"success\",\"requestID\":\"" "{\"action\":\"onStartContractTrustfully\",\"result\":\"success\",\"requestID\":\""
+ jo.get("requestID").getAsString() + jo.get("requestID").getAsString() + "\",\"pubKey\":\""
+ "\",\"pubKey\":\"" + GlobalConf.instance.keyPair.getPublicKeyStr() + "\"}");
+ GlobalConf.instance.keyPair.getPublicKeyStr()
+ "\"}");
ExecutionManager.instance.updateLocalContractToNodeCenter(); ExecutionManager.instance.updateLocalContractToNodeCenter();
} else { } else {
Map<String, Object> resultMap = new HashMap<>(); Map<String, Object> resultMap = new HashMap<>();
@ -417,11 +368,11 @@ public class MasterClientTCPAction {
resultMap.put("pubKey", GlobalConf.instance.keyPair.getPublicKeyStr()); resultMap.put("pubKey", GlobalConf.instance.keyPair.getPublicKeyStr());
result.onResult(JsonUtil.toJson(resultMap)); result.onResult(JsonUtil.toJson(resultMap));
} }
// if (contract.getType() == ContractExecType.Sharding) { // if (contract.getType() == ContractExecType.Sharding) {
// for (String str : cei.getMembers()) { // for (String str : cei.getMembers()) {
// NetworkManager.instance.getNCClientHandler().controller.connectToMaster(str, null); // NetworkManager.instance.getNCClientHandler().controller.connectToMaster(str, null);
// } // }
// } // }
} }
private String dumpToDisk(Contract contract, JsonObject jo) { private String dumpToDisk(Contract contract, JsonObject jo) {
@ -429,9 +380,8 @@ public class MasterClientTCPAction {
String.format("Script_%s", Math.abs(contract.getScriptStr().hashCode())); String.format("Script_%s", Math.abs(contract.getScriptStr().hashCode()));
try { try {
jo.addProperty("isPrivate", true); jo.addProperty("isPrivate", true);
File scriptDir = File scriptDir = new File(GlobalConf.instance.privateCompiledDir,
new File( jo.get("pubKey").getAsString());
GlobalConf.instance.privateCompiledDir, jo.get("pubKey").getAsString());
if (!scriptDir.exists()) { if (!scriptDir.exists()) {
scriptDir.mkdirs(); scriptDir.mkdirs();
} }
@ -454,14 +404,14 @@ public class MasterClientTCPAction {
return "/" + scriptName + ".ypk"; return "/" + scriptName + ".ypk";
} }
//TODO 这个奇怪的action可以放到这个SelfAdaptiveSharding的相关类里 // TODO 这个奇怪的action可以放到这个SelfAdaptiveSharding的相关类里
@Action(async = true) @Action(async = true)
public void deliverBlock(JsonObject jo, ResultCallback result) { public void deliverBlock(JsonObject jo, ResultCallback result) {
if (jo.has("contractID") && jo.has("data")) { if (jo.has("contractID") && jo.has("data")) {
String contractID = jo.get("contractID").getAsString(); String contractID = jo.get("contractID").getAsString();
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
if (null != meta && meta.contractExecutor instanceof SelfAdaptiveShardingExecutor) { if (null != meta && meta.contractExecutor != null) {
((SelfAdaptiveShardingExecutor) meta.contractExecutor).execute(jo.get("data").getAsString()); meta.contractExecutor.onDeliverBlock(jo.get("data").getAsString());
} }
} }
} }
@ -470,15 +420,10 @@ public class MasterClientTCPAction {
public void executeContractLocally(JsonObject jo, ResultCallback result) { public void executeContractLocally(JsonObject jo, ResultCallback result) {
final ContractRequest request = final ContractRequest request =
JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class); JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class);
LOGGER.info("[MasterCientTCPAction] executeContractLocally " + JsonUtil.toJson(jo)); // LOGGER.info("[MasterCientTCPAction] executeContractLocally " + JsonUtil.toJson(jo));
LOGGER.info( // LOGGER.info("[MasterClient] executeLocally,uniReq:" + jo.get("uniReqID").getAsString() +
"[MasterClient] executeLocally,uniReq:" // " ->reqID:" + request.getRequestID() + " " + jo.get("data").toString());
+ jo.get("uniReqID").getAsString()
+ " ->reqID:"
+ request.getRequestID()
+ " "
+ jo.get("data").toString());
// requestOne and responseOne do not need sequencing // requestOne and responseOne do not need sequencing
if (!request.needSeq) { if (!request.needSeq) {
@ -505,8 +450,8 @@ public class MasterClientTCPAction {
boolean isFirst = false; boolean isFirst = false;
synchronized (MultiRequestInfo.lock) { // 加锁防止同时放入多个同requestID的请求到cei的队列中 synchronized (MultiRequestInfo.lock) { // 加锁防止同时放入多个同requestID的请求到cei的队列中
if (MultiRequestInfo.reqInfos.containsKey(request.getRequestID())) { if (MultiRequestInfo.reqInfos.containsKey(request.getRequestID())) {
// logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" + // logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" +
// cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + " // cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + "
// 非第一个请求"); // 非第一个请求");
MultiRequestInfo mri = MultiRequestInfo mri =
@ -521,8 +466,8 @@ public class MasterClientTCPAction {
mri.putFlag(jo.get("uniReqID").getAsString(), false); mri.putFlag(jo.get("uniReqID").getAsString(), false);
} }
} else { } else {
// logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" + // logger.info("【收到多点合约请求】request.seq=" + request.seq + " cei.curExeSeq=" +
// cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + " // cei.curExeSeq + " cei.getLastExeSeq()=" + cei.getLastExeSeq() + "
// 是第一个请求"); // 是第一个请求");
MultiRequestInfo mri = MultiRequestInfo mri =
new MultiRequestInfo(request.getRequestID(), request.seq); new MultiRequestInfo(request.getRequestID(), request.seq);
@ -540,13 +485,8 @@ public class MasterClientTCPAction {
} }
} else { } else {
synchronized (cei) { synchronized (cei) {
LOGGER.info( LOGGER.info("合约" + contractID + " 请求序号为 " + request.seq + " 上一次执行请求序号为 "
"合约" + cei.getLastExeSeq());
+ contractID
+ " 请求序号为 "
+ request.seq
+ " 上一次执行请求序号为 "
+ cei.getLastExeSeq());
// cei.printContent(); // cei.printContent();
if (request.seq <= cei.getLastExeSeq()) { if (request.seq <= cei.getLastExeSeq()) {
@ -579,15 +519,17 @@ public class MasterClientTCPAction {
} }
@Action(async = true) @Action(async = true)
public void receiveContractExecutionServer(JsonObject jsonObject, ResultCallback resultCallback) { public void receiveContractExecutionServer(JsonObject jsonObject,
MasterServerTCPAction.sync.wakeUp( ResultCallback resultCallback) {
jsonObject.get("responseID").getAsString(), jsonObject.get("data").getAsString()); MasterServerTCPAction.sync.wakeUp(jsonObject.get("responseID").getAsString(),
jsonObject.get("data").getAsString());
} }
@Action(async = true) @Action(async = true)
public void contractSyncMessage(JsonObject jsonObject, ResultCallback resultCallback) { public void contractSyncMessage(JsonObject jsonObject, ResultCallback resultCallback) {
String contractID = jsonObject.get("contractID").getAsString(); 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); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
byte[] data = ByteUtil.decodeBASE64(jsonObject.get("data").getAsString()); byte[] data = ByteUtil.decodeBASE64(jsonObject.get("data").getAsString());
PubKeyNode node = new PubKeyNode(); PubKeyNode node = new PubKeyNode();
@ -601,8 +543,7 @@ public class MasterClientTCPAction {
@Action(async = true) @Action(async = true)
public void reRouteContract(JsonObject jo, ResultCallback result) { public void reRouteContract(JsonObject jo, ResultCallback result) {
LOGGER.info("Receive Reroute Info:" + jo.toString()); LOGGER.info("Receive Reroute Info:" + jo.toString());
MasterServerTCPAction.sync.instrumentWakeUp( MasterServerTCPAction.sync.instrumentWakeUp(jo.get("responseID").getAsString(),
jo.get("responseID").getAsString(),
(resultCallback, result1) -> { (resultCallback, result1) -> {
resultCallback.cancelTimeOut(); resultCallback.cancelTimeOut();
LOGGER.info("try To reRoute"); LOGGER.info("try To reRoute");
@ -613,67 +554,58 @@ public class MasterClientTCPAction {
(SyncResult.ContractResultCallback) resultCallback; (SyncResult.ContractResultCallback) resultCallback;
if (cb.getReRouteCount() > 0) { if (cb.getReRouteCount() > 0) {
ContractResult contractResult = ContractResult contractResult =
new ContractResult( new ContractResult(ContractResult.Status.Error,
ContractResult.Status.Error,
new JsonPrimitive( new JsonPrimitive(
"Contract " "Contract " + cr.get("contractID").getAsString()
+ cr.get("contractID").getAsString()
+ "can't be located in router")); + "can't be located in router"));
resultCallback.onResult(JsonUtil.toJson(contractResult)); resultCallback.onResult(JsonUtil.toJson(contractResult));
return; return;
} else cb.incReRouteCount(); } else
cb.incReRouteCount();
LOGGER.info("inc reRoute:" + cb.getReRouteCount()); LOGGER.info("inc reRoute:" + cb.getReRouteCount());
} }
String pubkey = String pubkey = CMActions.manager.nodeCenterConn
CMActions.manager.nodeCenterConn.reRouteContract( .reRouteContract(cr.get("contractID").getAsString());
cr.get("contractID").getAsString()); LOGGER.info("ReRoute Result:" + cr.get("contractID").getAsString() + " pubKey:"
LOGGER.info( + pubkey);
"ReRoute Result:"
+ cr.get("contractID").getAsString()
+ " pubKey:"
+ pubkey);
if (pubkey == null) { if (pubkey == null) {
ContractResult contractResult = ContractResult contractResult =
new ContractResult( new ContractResult(ContractResult.Status.Error,
ContractResult.Status.Error, new JsonPrimitive("Contract "
new JsonPrimitive( + cr.get("contractID").getAsString()
"Contract " + "can't be located in router using reroute"));
+ cr.get("contractID").getAsString()
+ "can't be located in router using reroute"));
resultCallback.onResult(JsonUtil.toJson(contractResult)); resultCallback.onResult(JsonUtil.toJson(contractResult));
return; return;
} }
LOGGER.debug("Receive Reroute Result:" + pubkey); LOGGER.debug("Receive Reroute Result:" + pubkey);
ContractRequest contractRequest = ContractRequest contractRequest = JsonUtil.fromJson(cr, ContractRequest.class);
JsonUtil.fromJson(cr, ContractRequest.class); CMActions.manager.masterStub.executeByOtherNodeAsync(pubkey, contractRequest,
CMActions.manager.masterStub.executeByOtherNodeAsync( resultCallback);
pubkey, contractRequest, resultCallback); }, jo);
},
jo);
} }
// public String requestContractExecution(ContractRequest c) { // public String requestContractExecution(ContractRequest c) {
// try { // try {
// LOGGER.info("[MasterClientTCPAction] requestContractExecution " + JsonUtil.toJson(c)); // LOGGER.info("[MasterClientTCPAction] requestContractExecution " + JsonUtil.toJson(c));
// Map<String, Object> req = new HashMap<>(); // Map<String, Object> req = new HashMap<>();
// req.put("action", "requestContractExecution"); // req.put("action", "requestContractExecution");
// req.put("requestID", c.getRequestID()); // req.put("requestID", c.getRequestID());
// req.put("data", c); // req.put("data", c);
// handler.sendMsg(JsonUtil.toJson(req)); // handler.sendMsg(JsonUtil.toJson(req));
// // 这里可能出错是不是在这里校验CollectResult? // // 这里可能出错是不是在这里校验CollectResult?
// ContractResult str = MasterServerTCPAction.sync.syncSleep(c.getRequestID()); // ContractResult str = MasterServerTCPAction.sync.syncSleep(c.getRequestID());
// LOGGER.info("[RequestContractGet]" + JsonUtil.toJson(str)); // LOGGER.info("[RequestContractGet]" + JsonUtil.toJson(str));
// return JsonUtil.toJson(str); // return JsonUtil.toJson(str);
// } catch (Exception e) { // } catch (Exception e) {
// ByteArrayOutputStream bo = new ByteArrayOutputStream(); // ByteArrayOutputStream bo = new ByteArrayOutputStream();
// e.printStackTrace(new PrintStream(bo)); // e.printStackTrace(new PrintStream(bo));
// ContractResult cr = // ContractResult cr =
// new ContractResult( // new ContractResult(
// ContractResult.Status.Exception, new JsonPrimitive(bo.toString())); // ContractResult.Status.Exception, new JsonPrimitive(bo.toString()));
// return JsonUtil.toJson(cr); // return JsonUtil.toJson(cr);
// } // }
// } // }
} }

View File

@ -25,15 +25,14 @@ public class MasterClientTransferAction {
private final Map<String, String> id2Memory = new HashMap<>(); private final Map<String, String> id2Memory = new HashMap<>();
private MasterClientTransferAction() { private MasterClientTransferAction() {}
}
public void transferInstance(String agentPubkey, String contractID) { public void transferInstance(String agentPubkey, String contractID) {
LOGGER.info("transferInstance contractID=" + contractID); LOGGER.info("transferInstance contractID=" + contractID);
//step2 save state // step2 save state
String mem = CMActions.manager.dumpContract(contractID, ""); String mem = CMActions.manager.dumpContract(contractID, "");
id2Memory.put(contractID, mem); 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); sendAndStart(agentPubkey, contractID);
} }
@ -48,7 +47,7 @@ public class MasterClientTransferAction {
String script = cc.contractMeta.contract.getScriptStr(); String script = cc.contractMeta.contract.getScriptStr();
ContractStartInfo csi = cc.contractMeta.contract.startInfo; ContractStartInfo csi = cc.contractMeta.contract.startInfo;
//kill // kill
LOGGER.info("sendAndRestart stopContract"); LOGGER.info("sendAndRestart stopContract");
CMActions.manager.stopContract(contractID); CMActions.manager.stopContract(contractID);
@ -66,7 +65,8 @@ public class MasterClientTransferAction {
File file; File file;
String path; String path;
if (csi.isPrivate) { 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; path = csi.pubKeyPath + "/" + csi.ypkName;
} else { } else {
file = new File(GlobalConf.instance.publicCompiledDir + "/" + csi.ypkName); file = new File(GlobalConf.instance.publicCompiledDir + "/" + csi.ypkName);
@ -90,7 +90,7 @@ public class MasterClientTransferAction {
long count = 0; long count = 0;
long total = file.length(); 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); String data = ByteUtil.encodeBASE64(buff, len);
req.put("data", data); req.put("data", data);
count += len; count += len;
@ -116,7 +116,7 @@ public class MasterClientTransferAction {
@Action(async = true) @Action(async = true)
public void onSendAndStart(JsonObject jo, ResultCallback result) { public void onSendAndStart(JsonObject jo, ResultCallback result) {
//step4 send memory // step4 send memory
String contractID = jo.get("contractID").getAsString(); String contractID = jo.get("contractID").getAsString();
LOGGER.info("onSendAndStart contractID=" + contractID); LOGGER.info("onSendAndStart contractID=" + contractID);
String mem = id2Memory.get(contractID); String mem = id2Memory.get(contractID);
@ -130,11 +130,13 @@ public class MasterClientTransferAction {
public boolean sendMemory(String contractID, String memory, ResultCallback cb) { public boolean sendMemory(String contractID, String memory, ResultCallback cb) {
LOGGER.info("sendMemory contractID=" + contractID); 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); File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
LOGGER.info(file.getAbsolutePath()); LOGGER.info(file.getAbsolutePath());
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.exists()) parent.mkdirs(); if (!parent.exists())
parent.mkdirs();
ObjectOutputStream writer; ObjectOutputStream writer;
try { try {
FileOutputStream fileout = new FileOutputStream(file); FileOutputStream fileout = new FileOutputStream(file);
@ -160,7 +162,7 @@ public class MasterClientTransferAction {
byte[] buff = new byte[30 * 1024]; byte[] buff = new byte[30 * 1024];
long count = 0; 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); String data = ByteUtil.encodeBASE64(buff, len);
req.put("data", data); req.put("data", data);
count += len; count += len;
@ -175,7 +177,7 @@ public class MasterClientTransferAction {
req.put("contractID", contractID); req.put("contractID", contractID);
req.remove("data"); req.remove("data");
cb.onResult(JsonUtil.toJson(req)); cb.onResult(JsonUtil.toJson(req));
//delete state file // delete state file
if (file.isFile() && file.exists()) { if (file.isFile() && file.exists()) {
file.delete(); file.delete();
} }
@ -190,16 +192,16 @@ public class MasterClientTransferAction {
@Action(async = true) @Action(async = true)
public void onSendMemory(JsonObject jo, ResultCallback result) { public void onSendMemory(JsonObject jo, ResultCallback result) {
//step5 close connect // step5 close connect
LOGGER.info("onSendMemory done"); LOGGER.info("onSendMemory done");
// if (handler != null) { // if (handler != null) {
// LOGGER.info("handler close"); // LOGGER.info("handler close");
// NetworkManager.instance.closeAgent(master); // NetworkManager.instance.closeAgent(master);
// //
// } // }
// if (NetworkManager.CONNECTORS.containsKey(master)) { // if (NetworkManager.CONNECTORS.containsKey(master)) {
// NetworkManager.CONNECTORS.remove(master); // NetworkManager.CONNECTORS.remove(master);
// } // }
LOGGER.info("transfer contract instance finished."); LOGGER.info("transfer contract instance finished.");
} }
} }

View File

@ -16,8 +16,6 @@ import org.bdware.sc.util.JsonUtil;
import org.bdware.server.GlobalConf; import org.bdware.server.GlobalConf;
import org.bdware.server.action.Action; import org.bdware.server.action.Action;
import org.bdware.server.action.CMActions; 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.server.trustedmodel.ContractUnitStatus;
import org.bdware.units.NetworkManager; import org.bdware.units.NetworkManager;
import org.zz.gmhelper.SM2KeyPair; import org.zz.gmhelper.SM2KeyPair;
@ -32,8 +30,7 @@ public class MasterServerRecoverMechAction {
public static Map<String, Map<String, RecoverFlag>> recoverStatus = new ConcurrentHashMap<>(); public static Map<String, Map<String, RecoverFlag>> recoverStatus = new ConcurrentHashMap<>();
private final Map<String, OutputStream> stateFileMap = new HashMap<>(); private final Map<String, OutputStream> stateFileMap = new HashMap<>();
public MasterServerRecoverMechAction() { public MasterServerRecoverMechAction() {}
}
// 从disk-durable恢复 // 从disk-durable恢复
public static void restartContractFromStableMode(String nodeID, String contractID) { public static void restartContractFromStableMode(String nodeID, String contractID) {
@ -48,8 +45,8 @@ public class MasterServerRecoverMechAction {
Map<String, String> request = new HashMap<String, String>(); Map<String, String> request = new HashMap<String, String>();
request.put("action", "recoverFromStableMode"); request.put("action", "recoverFromStableMode");
request.put("contractID", contractID); request.put("contractID", contractID);
int temp = int temp = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getCurSeqAtMaster(); .getCurSeqAtMaster();
request.put("unitLastExeSeq", temp + ""); request.put("unitLastExeSeq", temp + "");
NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(request)); NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(request));
} }
@ -83,15 +80,15 @@ public class MasterServerRecoverMechAction {
RecoverMechTimeRecorder.writeCEIStart.put(nodeID, System.currentTimeMillis()); RecoverMechTimeRecorder.writeCEIStart.put(nodeID, System.currentTimeMillis());
MultiContractMeta cei = MultiContractMeta cei =
CMActions.manager.multiContractRecorder.getMultiContractMeta( CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); // master的server直接调client端因为master自己的client端就在自己本地
contractID); // master的server直接调client端因为master自己的client端就在自己本地
cei.memory = CMActions.manager.dumpContract(contractID, ""); cei.memory = CMActions.manager.dumpContract(contractID, "");
// cei.printContent(); // cei.printContent();
String path = contractID + "_temp_stateFile_" + new Random().nextLong(); String path = contractID + "_temp_stateFile_" + new Random().nextLong();
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path); File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.exists()) parent.mkdirs(); if (!parent.exists())
parent.mkdirs();
ObjectOutputStream writer; ObjectOutputStream writer;
try { try {
FileOutputStream fileout = new FileOutputStream(file); FileOutputStream fileout = new FileOutputStream(file);
@ -127,7 +124,7 @@ public class MasterServerRecoverMechAction {
times++; times++;
LOGGER.info("times=" + times); LOGGER.info("times=" + times);
int index = 1; // 数据包序号 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); // logger.info("len = " + len);
String data = ByteUtil.encodeBASE64(buff, len); String data = ByteUtil.encodeBASE64(buff, len);
req.put("data", data); req.put("data", data);
@ -145,10 +142,10 @@ public class MasterServerRecoverMechAction {
// Thread.sleep(300); // Thread.sleep(300);
} }
fin.close(); fin.close();
// req.put("isDone", "true"); // req.put("isDone", "true");
// req.remove("data"); // req.remove("data");
// //
// node.connection.sendMsg(JsonUtil.toJson(req)); // node.connection.sendMsg(JsonUtil.toJson(req));
// master delete state file // master delete state file
if (file.isFile() && file.exists()) { if (file.isFile() && file.exists()) {
@ -165,8 +162,8 @@ public class MasterServerRecoverMechAction {
public static void masterRestartFromOther(String contractID) { public static void masterRestartFromOther(String contractID) {
LOGGER.info("Master从别的节点load当前状态"); LOGGER.info("Master从别的节点load当前状态");
String masterID = String masterID = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMasterNode(); .getMasterNode();
if (recoverStatus.get(masterID).get(contractID) != RecoverFlag.ToRecover) { if (recoverStatus.get(masterID).get(contractID) != RecoverFlag.ToRecover) {
return; return;
} }
@ -174,15 +171,15 @@ public class MasterServerRecoverMechAction {
// 找一个普通节点来dump // 找一个普通节点来dump
String relyNode = null; String relyNode = null;
for (String nodeID : for (String nodeID : CMActions.manager.multiContractRecorder
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers()) { .getMultiContractMeta(contractID).getMembers()) {
if (nodeID.equals(masterID)) { if (nodeID.equals(masterID)) {
continue; continue;
} }
if (recoverStatus.get(nodeID).get(contractID) != RecoverFlag.Fine) { if (recoverStatus.get(nodeID).get(contractID) != RecoverFlag.Fine) {
continue; continue;
} }
//TODO????似乎人重连接 // TODO????似乎人重连接
if (NetworkManager.instance.hasAgentConnection(nodeID)) { if (NetworkManager.instance.hasAgentConnection(nodeID)) {
relyNode = nodeID; relyNode = nodeID;
break; break;
@ -204,12 +201,16 @@ public class MasterServerRecoverMechAction {
public static void unitModeCheck(String contractID) { public static void unitModeCheck(String contractID) {
String stateful = CMActions.manager.getContractStateful(contractID); String stateful = CMActions.manager.getContractStateful(contractID);
// just disable unitModeCheck
if (stateful != null)
return;
if (stateful.equals("false")) { if (stateful.equals("false")) {
LOGGER.info("无状态集群合约无需运行模式检查及切换!"); LOGGER.info("无状态集群合约无需运行模式检查及切换!");
return; return;
} }
MultiContractMeta mpci = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); MultiContractMeta mpci =
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
synchronized (mpci) { synchronized (mpci) {
int total = 0, fineNum = 0; int total = 0, fineNum = 0;
for (String nodeId : mpci.getMembers()) { for (String nodeId : mpci.getMembers()) {
@ -223,27 +224,17 @@ public class MasterServerRecoverMechAction {
total++; total++;
} }
LOGGER.info( LOGGER.info("合约" + contractID + "的集群,正常运行的节点有" + fineNum + "个,总共节点有" + total
"合约" + "个. Math.ceil(total / 2)=" + Math.ceil((double) total / 2)
+ contractID + " online > Math.ceil(total / 2)" + (fineNum > Math.ceil(total / 2))
+ "的集群,正常运行的节点有" + " mpci.unitStatus=" + mpci.unitStatus);
+ 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; ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode;
if (fineNum <= Math.ceil((double) total / 2)) { if (fineNum <= Math.ceil((double) total / 2)) {
unitStatus = ContractUnitStatus.StableMode; unitStatus = ContractUnitStatus.StableMode;
} }
LOGGER.info( LOGGER.info("合约" + contractID + "的集群更改模式为" + unitStatus);
"合约" + contractID + "的集群更改模式为" + unitStatus);
Map<String, String> req = new HashMap<String, String>(); Map<String, String> req = new HashMap<String, String>();
req.put("action", "changeUnitStatus"); req.put("action", "changeUnitStatus");
@ -251,12 +242,8 @@ public class MasterServerRecoverMechAction {
req.put("mode", unitStatus.toString()); req.put("mode", unitStatus.toString());
for (String nodeId : mpci.getMembers()) { for (String nodeId : mpci.getMembers()) {
NetworkManager.instance.sendToAgentIfConnected(nodeId, JsonUtil.toJson(req)); NetworkManager.instance.sendToAgentIfConnected(nodeId, JsonUtil.toJson(req));
LOGGER.info( LOGGER.info("发消息给节点 " + nodeId.substring(0, 5) + " 设置合约" + contractID
"发消息给节点 " + "的集群模式为StableMode");
+ nodeId.substring(0, 5)
+ " 设置合约"
+ contractID
+ "的集群模式为StableMode");
} }
@ -265,14 +252,15 @@ public class MasterServerRecoverMechAction {
} }
// 新选出的Master恢复 // 新选出的Master恢复
public static void newMasterRecover( public static void newMasterRecover(String contractID, String allMembers,
String contractID, String allMembers, List<String> onlineMembers) { List<String> onlineMembers) {
LOGGER.info("[MasterServerRecoverMechAction] 新master开始恢复 : "); LOGGER.info("[MasterServerRecoverMechAction] 新master开始恢复 : ");
MasterElectTimeRecorder.masterStartRecover = System.currentTimeMillis(); MasterElectTimeRecorder.masterStartRecover = System.currentTimeMillis();
// step1 恢复MultiContractInfo // step1 恢复MultiContractInfo
MultiContractMeta cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); MultiContractMeta cei =
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
synchronized (cei) { synchronized (cei) {
cei.setSeqAtMaster(cei.getLastExeSeq()); cei.setSeqAtMaster(cei.getLastExeSeq());
} }
@ -294,29 +282,18 @@ public class MasterServerRecoverMechAction {
} }
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
meta.setContractExecutor( meta.setContractExecutor(MasterClientTCPAction.createContractExecutor(meta.contract,
MasterClientTCPAction.createContractExecutor(meta.contract, contractID, cei.getMasterNode(), cei.getMembers())); contractID, cei.getMasterNode(), cei.getMembers()));
switch (meta.contract.getType()) { Map<String, Object> args = new HashMap<>();
case RequestAllResponseFirst: args.put("ceiLastExeSeq", cei.getLastExeSeq());
case RequestAllResponseHalf: meta.contractExecutor.onRecover(args);
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;
}
CMActions.manager.multiContractRecorder.updateValue(cei); CMActions.manager.multiContractRecorder.updateValue(cei);
LOGGER.info("[master recover] contractID2Members put 合约 " + contractID); LOGGER.info("[master recover] contractID2Members put 合约 " + contractID);
LOGGER.info("新master恢复 MultiPointContractInfo 完成!"); LOGGER.info("新master恢复 MultiPointContractInfo 完成!");
LOGGER.info( LOGGER.info(
"[MasterServerRecoverMechAction] step1 新master恢复 MultiPointContractInfo 完成! \n"); "[MasterServerRecoverMechAction] step1 新master恢复 MultiPointContractInfo 完成! \n");
// step2 恢复recoverStatues这个map // step2 恢复recoverStatues这个map
for (String nodeID : nodeNames) { for (String nodeID : nodeNames) {
if (onlineMembers.contains(nodeID)) { // 设置所有在线的节点Fine状态 if (onlineMembers.contains(nodeID)) { // 设置所有在线的节点Fine状态
if (!recoverStatus.containsKey(nodeID)) { if (!recoverStatus.containsKey(nodeID)) {
@ -330,12 +307,8 @@ public class MasterServerRecoverMechAction {
recoverStatus.put(nodeID, new ConcurrentHashMap<>()); recoverStatus.put(nodeID, new ConcurrentHashMap<>());
} }
recoverStatus.get(nodeID).put(contractID, RecoverFlag.ToRecover); recoverStatus.get(nodeID).put(contractID, RecoverFlag.ToRecover);
LOGGER.info( LOGGER.info("新master设置 合约 " + contractID + " 节点 " + nodeID.substring(0, 5)
"新master设置 合约 " + " 为ToRecover");
+ contractID
+ " 节点 "
+ nodeID.substring(0, 5)
+ " 为ToRecover");
} }
} }
LOGGER.info("新master恢复 recoverStatus map 完成!"); LOGGER.info("新master恢复 recoverStatus map 完成!");
@ -343,7 +316,7 @@ public class MasterServerRecoverMechAction {
MasterElectTimeRecorder.masterRecoverFinish = System.currentTimeMillis(); MasterElectTimeRecorder.masterRecoverFinish = System.currentTimeMillis();
// step3 告知NC更新路由信息 // step3 告知NC更新路由信息
Map<String, String> re = new HashMap<>(); Map<String, String> re = new HashMap<>();
re.put("action", "changeMaster"); re.put("action", "changeMaster");
re.put("contractID", contractID); re.put("contractID", contractID);
@ -351,35 +324,35 @@ public class MasterServerRecoverMechAction {
NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(re)); NetworkManager.instance.sendToNodeCenter(JsonUtil.toJson(re));
LOGGER.info("[MasterServerRecoverMechAction] step3 告知NC更新路由信息! \n"); LOGGER.info("[MasterServerRecoverMechAction] step3 告知NC更新路由信息! \n");
// step4 查看集群当前状态 // step4 查看集群当前状态
unitModeCheck(contractID); unitModeCheck(contractID);
LOGGER.info("新master 查看当前集群状态 完成!"); LOGGER.info("新master 查看当前集群状态 完成!");
LOGGER.info("[MasterServerRecoverMechAction] step4 新master查看当前集群状态 完成!"); LOGGER.info("[MasterServerRecoverMechAction] step4 新master查看当前集群状态 完成!");
// step5 向其他可能和旧的master断连但和NC连接的节点发请求让连新的master // step5 向其他可能和旧的master断连但和NC连接的节点发请求让连新的master
// String off = ""; // String off = "";
// for (String str : offlines) { // for (String str : offlines) {
// if (off.equals("")) { // if (off.equals("")) {
// off = str; // off = str;
// } else { // } else {
// off += ","; // off += ",";
// off += str; // off += str;
// } // }
// } // }
// //
// Map<String, String> request = new HashMap<>(); // Map<String, String> request = new HashMap<>();
// request.put("action", "updateFormerMaster"); // request.put("action", "updateFormerMaster");
// request.put("contractID", contractID); // request.put("contractID", contractID);
// request.put("offlines", off); // request.put("offlines", off);
// request.put("master", mpci.masterNode); // request.put("master", mpci.masterNode);
// CMActions.client.sendMsg(JsonUtil.toJson(request)); // CMActions.client.sendMsg(JsonUtil.toJson(request));
LOGGER.info("新的master恢复完毕!"); LOGGER.info("新的master恢复完毕!");
LOGGER.info("[MasterServerRecoverMechAction] 新master恢复完成!"); LOGGER.info("[MasterServerRecoverMechAction] 新master恢复完成!");
} }
public String judegeRecoverMethod( public String judegeRecoverMethod(String contractID, String nodeMode, String unitMode,
String contractID, String nodeMode, String unitMode, int nodeLastExe, int unitLastExe) { int nodeLastExe, int unitLastExe) {
if (nodeMode.equals(ContractUnitStatus.CommonMode.toString()) if (nodeMode.equals(ContractUnitStatus.CommonMode.toString())
&& nodeLastExe == unitLastExe) { && nodeLastExe == unitLastExe) {
return ""; return "";
@ -416,13 +389,14 @@ public class MasterServerRecoverMechAction {
String unitMode; String unitMode;
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID)) if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(), contractID))
unitMode = ContractUnitStatus.StableMode.toString(); unitMode = ContractUnitStatus.StableMode.toString();
else unitMode = ContractUnitStatus.CommonMode.toString(); else
unitMode = ContractUnitStatus.CommonMode.toString();
int nodeLastExe = Integer.MIN_VALUE; int nodeLastExe = Integer.MIN_VALUE;
if (jo.has("lastExeSeq")) { if (jo.has("lastExeSeq")) {
nodeLastExe = jo.get("lastExeSeq").getAsInt(); nodeLastExe = jo.get("lastExeSeq").getAsInt();
} }
int unitLastExe = int unitLastExe = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getCurSeqAtMaster(); .getCurSeqAtMaster();
RecoverMechTimeRecorder.startJudgeRecoverMethod.put(nodeID, System.currentTimeMillis()); RecoverMechTimeRecorder.startJudgeRecoverMethod.put(nodeID, System.currentTimeMillis());
@ -431,15 +405,8 @@ public class MasterServerRecoverMechAction {
RecoverMechTimeRecorder.judgeRecoverMethodFinish.put(nodeID, System.currentTimeMillis()); RecoverMechTimeRecorder.judgeRecoverMethodFinish.put(nodeID, System.currentTimeMillis());
LOGGER.info( LOGGER.info("[MasterServerRecoverMechAciton] askForRecover mode=" + nodeMode
"[MasterServerRecoverMechAciton] askForRecover mode=" + " contractID=" + contractID + " nodeID=" + nodeID + " method=" + method);
+ nodeMode
+ " contractID="
+ contractID
+ " nodeID="
+ nodeID
+ " method="
+ method);
if (method.equals("common")) { if (method.equals("common")) {
LOGGER.info("restartContractFromCommonMode"); LOGGER.info("restartContractFromCommonMode");
restartContractFromCommonMode(nodeID, contractID); restartContractFromCommonMode(nodeID, contractID);
@ -458,8 +425,8 @@ public class MasterServerRecoverMechAction {
@Action(async = true) @Action(async = true)
public void restartContractFromCommonMode(JsonObject jo, ResultCallback cb) { public void restartContractFromCommonMode(JsonObject jo, ResultCallback cb) {
restartContractFromCommonMode( restartContractFromCommonMode(jo.get("nodeID").getAsString(),
jo.get("nodeID").getAsString(), jo.get("contractID").getAsString()); jo.get("contractID").getAsString());
} }
@Action(async = true) @Action(async = true)
@ -468,7 +435,8 @@ public class MasterServerRecoverMechAction {
LOGGER.info("master查询缓存的 trans records 从 " + startSeq + " 开始"); LOGGER.info("master查询缓存的 trans records 从 " + startSeq + " 开始");
String nodeId = jo.get("nodeID").getAsString(); String nodeId = jo.get("nodeID").getAsString();
String contractID = jo.get("contractID").getAsString(); String contractID = jo.get("contractID").getAsString();
MultiContractMeta mpci = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); MultiContractMeta mpci =
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
synchronized (mpci) { synchronized (mpci) {
if (mpci.unitStatus == ContractUnitStatus.CommonMode) { if (mpci.unitStatus == ContractUnitStatus.CommonMode) {
restartContractFromCommonMode(nodeId, contractID); restartContractFromCommonMode(nodeId, contractID);
@ -488,7 +456,8 @@ public class MasterServerRecoverMechAction {
String path = contractID + "_temp_TransFile_" + new Random().nextLong(); String path = contractID + "_temp_TransFile_" + new Random().nextLong();
File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path); File file = new File(GlobalConf.instance.projectDir + "/stateFiles/" + path);
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.exists()) parent.mkdirs(); if (!parent.exists())
parent.mkdirs();
try { try {
FileWriter fw = new FileWriter(file, true); FileWriter fw = new FileWriter(file, true);
@ -527,7 +496,7 @@ public class MasterServerRecoverMechAction {
int index = 1; // 数据包序号 int index = 1; // 数据包序号
long count = 0; 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); // logger.info("len = " + len);
String data = ByteUtil.encodeBASE64(buff, len); String data = ByteUtil.encodeBASE64(buff, len);
req.put("data", data); req.put("data", data);
@ -548,12 +517,12 @@ public class MasterServerRecoverMechAction {
} }
fin.close(); fin.close();
// req.put("isDone", "true"); // req.put("isDone", "true");
// req.put("contractID", contractID); // req.put("contractID", contractID);
// req.put("last", last); // req.put("last", last);
// req.remove("data"); // req.remove("data");
// //
// node.connection.sendMsg(JsonUtil.toJson(req)); // node.connection.sendMsg(JsonUtil.toJson(req));
// master delete state file // master delete state file
if (file.isFile() && file.exists()) { if (file.isFile() && file.exists()) {
@ -610,7 +579,8 @@ public class MasterServerRecoverMechAction {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else fout = (FileOutputStream) stateFileMap.get(fileName); } else
fout = (FileOutputStream) stateFileMap.get(fileName);
String data = args.get("data").getAsString(); String data = args.get("data").getAsString();
try { try {
@ -631,25 +601,26 @@ public class MasterServerRecoverMechAction {
req.put("isMaster", "true"); req.put("isMaster", "true");
req.put("filePath", dir.getAbsolutePath()); req.put("filePath", dir.getAbsolutePath());
SM2KeyPair keyPair = GlobalConf.instance.keyPair; SM2KeyPair keyPair = GlobalConf.instance.keyPair;
NetworkManager.instance.sendToAgent(keyPair.getPublicKeyStr(), JsonUtil.toJson(req)); NetworkManager.instance.sendToAgent(keyPair.getPublicKeyStr(),
JsonUtil.toJson(req));
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
// @Action(async = true) // @Action(async = true)
// public void statelessUnitLastExe(JsonObject jo, ResultCallback cb) { // public void statelessUnitLastExe(JsonObject jo, ResultCallback cb) {
// String contractID = jo.get("contractID").getAsString(); // String contractID = jo.get("contractID").getAsString();
// String unitLastExe = // String unitLastExe =
// MasterServerTCPAction.contractID2Members.get(contractID).getCurSeq() + ""; // 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>(); // Map<String, String> request = new HashMap<String, String>();
// request.put("action", "statelessReceiveLastExe"); // request.put("action", "statelessReceiveLastExe");
// request.put("contractID", contractID); // request.put("contractID", contractID);
// request.put("unitLastExe", unitLastExe); // request.put("unitLastExe", unitLastExe);
// SlaveNode node = MasterServerTCPAction.id2Slaves.get(nodeID); // SlaveNode node = MasterServerTCPAction.id2Slaves.get(nodeID);
// node.connection.sendMsg(JsonUtil.toJson(request)); // node.connection.sendMsg(JsonUtil.toJson(request));
// } // }
} }

View File

@ -14,17 +14,15 @@ import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.units.MultiContractMeta; import org.bdware.sc.units.MultiContractMeta;
import org.bdware.sc.units.RequestCache; import org.bdware.sc.units.RequestCache;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.sdk.consistency.api.NotifiableResultMerger;
import org.bdware.server.CongestionControl; import org.bdware.server.CongestionControl;
import org.bdware.server.action.Action; import org.bdware.server.action.Action;
import org.bdware.server.action.CMActions; import org.bdware.server.action.CMActions;
import org.bdware.server.action.SyncResult; import org.bdware.server.action.SyncResult;
import org.bdware.server.executor.consistency.RequestAllExecutor;
import org.bdware.server.trustedmodel.KillUnitContractResultCollector; import org.bdware.server.trustedmodel.KillUnitContractResultCollector;
import org.bdware.server.trustedmodel.ResultCollector; import org.bdware.server.trustedmodel.ResultCollector;
import org.bdware.units.NetworkManager; import org.bdware.units.NetworkManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -41,26 +39,21 @@ public class MasterServerTCPAction {
static { static {
final Object flag = new Object(); final Object flag = new Object();
ContractManager.scheduledThreadPool.scheduleWithFixedDelay( ContractManager.scheduledThreadPool.scheduleWithFixedDelay(() -> {
() -> { boolean cleared = clearCache();
boolean cleared = clearCache(); if (cleared) {
if (cleared) { try {
try { synchronized (flag) {
synchronized (flag) { flag.wait(14000L);
flag.wait(14000L);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
}, } catch (InterruptedException e) {
0, e.printStackTrace();
1, }
TimeUnit.SECONDS); }
}, 0, 1, TimeUnit.SECONDS);
} }
public MasterServerTCPAction() { public MasterServerTCPAction() {}
}
public static void notifyNodeOffline(String contractID, String nodeID) { public static void notifyNodeOffline(String contractID, String nodeID) {
synchronized (sync) { synchronized (sync) {
@ -68,24 +61,22 @@ public class MasterServerTCPAction {
ResultCallback cb = sync.waitObj.get(requestID); ResultCallback cb = sync.waitObj.get(requestID);
if (cb instanceof ResultCollector) { if (cb instanceof ResultCollector) {
ResultCollector rc = (ResultCollector) cb; ResultCollector rc = (ResultCollector) cb;
if (rc.getCommitter() instanceof RequestAllExecutor.ResultMerger) { if (rc.getCommitter() instanceof NotifiableResultMerger) {
RequestAllExecutor.ResultMerger merger = NotifiableResultMerger merger = (NotifiableResultMerger) rc.getCommitter();
(RequestAllExecutor.ResultMerger) rc.getCommitter();
if (merger.getContractID().equals(contractID)) { if (merger.getContractID().equals(contractID)) {
LOGGER.info("node " + nodeID + " offline! in the cluster of contract " + contractID); LOGGER.info("node " + nodeID + " offline! in the cluster of contract "
LOGGER.debug("nodeID=" + nodeID + " contractID=" + contractID + + contractID);
": " + merger.getInfo()); LOGGER.debug("nodeID=" + nodeID + " contractID=" + contractID + ": "
ContractResult cr = + merger.getInfo());
new ContractResult( ContractResult cr = new ContractResult(ContractResult.Status.Exception,
ContractResult.Status.Exception, new JsonPrimitive("node offline"));
new JsonPrimitive("node offline"));
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
jo.addProperty("data", JsonUtil.toJson(cr)); jo.addProperty("data", JsonUtil.toJson(cr));
jo.addProperty("responseID", requestID); jo.addProperty("responseID", requestID);
jo.addProperty("action", "receiveTrustfullyResult"); jo.addProperty("action", "receiveTrustfullyResult");
jo.addProperty("nodeID", nodeID); jo.addProperty("nodeID", nodeID);
LOGGER.debug("[cb] nodeID=" + nodeID + " contractID=" + contractID + LOGGER.debug("[cb] nodeID=" + nodeID + " contractID=" + contractID
": " + jo); + ": " + jo);
sync.wakeUp(requestID, jo.toString()); sync.wakeUp(requestID, jo.toString());
} }
} }
@ -97,7 +88,8 @@ public class MasterServerTCPAction {
static RequestCache getCache(String contractID) { static RequestCache getCache(String contractID) {
if (contractID != null) { if (contractID != null) {
RequestCache cache = requestCache.get(contractID); RequestCache cache = requestCache.get(contractID);
if (cache != null) return cache; if (cache != null)
return cache;
else { else {
LOGGER.info("[MasterServerTCPAction] create requestcache:" + contractID); LOGGER.info("[MasterServerTCPAction] create requestcache:" + contractID);
RequestCache reqc = new RequestCache(); RequestCache reqc = new RequestCache();
@ -109,17 +101,18 @@ public class MasterServerTCPAction {
} }
static boolean clearCache() { static boolean clearCache() {
if (requestCache.isEmpty()) return true; if (requestCache.isEmpty())
return true;
// final long time = System.currentTimeMillis() - 120000L; // 120s // final long time = System.currentTimeMillis() - 120000L; // 120s
// requestCache // requestCache
// .entrySet() // .entrySet()
// .removeIf( // .removeIf(
// entry -> { // entry -> {
// RequestCache cache = entry.getValue(); // RequestCache cache = entry.getValue();
// if (cache == null) return true; // if (cache == null) return true;
// return cache.getTime() < time; // return cache.getTime() < time;
// }); // });
for (RequestCache cache : requestCache.values()) { for (RequestCache cache : requestCache.values()) {
cache.decSize(); cache.decSize();
@ -131,15 +124,8 @@ public class MasterServerTCPAction {
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
MultiContractMeta ret = MultiContractMeta ret =
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
System.out.println( LOGGER.debug(String.format("getCMInfo: %s %s %s", meta.getName(), meta.getID(),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") meta.getStatus()));
.format(new Date(System.currentTimeMillis()))
+ " [MasterServerTCPAction] getCMInfo: "
+ meta.getName()
+ " "
+ meta.getID()
+ " "
+ meta.getStatus());
return ret; return ret;
} }
@ -178,9 +164,8 @@ public class MasterServerTCPAction {
jo.addProperty("action", "killContractProcessAtSlave"); jo.addProperty("action", "killContractProcessAtSlave");
MultiContractMeta contractMeta = MultiContractMeta contractMeta =
CMActions.manager.multiContractRecorder.getMultiContractMeta(cl.getContractID()); CMActions.manager.multiContractRecorder.getMultiContractMeta(cl.getContractID());
KillUnitContractResultCollector collector = KillUnitContractResultCollector collector = new KillUnitContractResultCollector(
new KillUnitContractResultCollector( jo.get("requestID").getAsString(), cb, contractMeta.getMembers().length);
jo.get("requestID").getAsString(), cb, contractMeta.getMembers().length);
sync.sleep(jo.get("requestID").getAsString(), collector); sync.sleep(jo.get("requestID").getAsString(), collector);
for (String member : contractMeta.getMembers()) { for (String member : contractMeta.getMembers()) {
NetworkManager.instance.sendToAgent(member, jo.toString()); NetworkManager.instance.sendToAgent(member, jo.toString());
@ -190,27 +175,27 @@ public class MasterServerTCPAction {
@Action(async = true) @Action(async = true)
public void receiveTrustfullyResult(JsonObject jo, ResultCallback cb) { public void receiveTrustfullyResult(JsonObject jo, ResultCallback cb) {
String responseID = jo.get("responseID").getAsString(); String responseID = jo.get("responseID").getAsString();
LOGGER.info("========== ExecuteContractLocally wakeUp:" + responseID); // LOGGER.info("========== ExecuteContractLocally wakeUp:" + responseID);
MasterServerTCPAction.sync.wakeUp(responseID, jo.toString()); MasterServerTCPAction.sync.wakeUp(responseID, jo.toString());
} }
// TODO to Remove // TODO to Remove
// @Action(async = true) // @Action(async = true)
// // 假设该结点有运行这个合约调用的合约 // // 假设该结点有运行这个合约调用的合约
// public void executeContractLocallyServer(JsonObject jo, ResultCallback cb) { // public void executeContractLocallyServer(JsonObject jo, ResultCallback cb) {
// final ContractRequest request = // final ContractRequest request =
// JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class); // JsonUtil.fromJson(jo.get("data").toString(), ContractRequest.class);
// long start = System.currentTimeMillis(); // long start = System.currentTimeMillis();
// String data2 = CMActions.manager.executeLocally(request, null); // String data2 = CMActions.manager.executeLocally(request, null);
// Map<String, String> ret = new HashMap<>(); // Map<String, String> ret = new HashMap<>();
// ret.put("action", "receiveTrustfullyResultServer"); // ret.put("action", "receiveTrustfullyResultServer");
// SM2KeyPair keyPair = GlobalConf.instance.keyPair; // SM2KeyPair keyPair = GlobalConf.instance.keyPair;
// ret.put("nodeID", keyPair.getPublicKeyStr()); // ret.put("nodeID", keyPair.getPublicKeyStr());
// ret.put("responseID", jo.get("uniReqID").getAsString()); // ret.put("responseID", jo.get("uniReqID").getAsString());
// ret.put("executeTime", (System.currentTimeMillis() - start) + ""); // ret.put("executeTime", (System.currentTimeMillis() - start) + "");
// ret.put("data", data2); // ret.put("data", data2);
// cb.onResult(JsonUtil.toJson(ret)); // cb.onResult(JsonUtil.toJson(ret));
// } // }
@Action(async = true) @Action(async = true)
@ -230,15 +215,15 @@ public class MasterServerTCPAction {
// 调用方向为MasterClient->MasterServer // 调用方向为MasterClient->MasterServer
// 如果为多节点则为MasterServer executeLocally-> MasterClient // 如果为多节点则为MasterServer executeLocally-> MasterClient
// executeLocally否则为MasterServer->Local // executeLocally否则为MasterServer->Local
// LOGGER.info("[requestContractExecution]:" + jo.toString()); // LOGGER.info("[requestContractExecution]:" + jo.toString());
// loggger.bug // loggger.bug
JsonObject cr = jo.get("data").getAsJsonObject(); JsonObject cr = jo.get("data").getAsJsonObject();
String requestID = cr.get("requestID").getAsString(); String requestID = cr.get("requestID").getAsString();
String contractID = cr.get("contractID").getAsString(); String contractID = cr.get("contractID").getAsString();
ContractMeta contractMeta = ContractMeta contractMeta =
CMActions.manager.statusRecorder.getContractMeta(contractID); CMActions.manager.statusRecorder.getContractMeta(contractID);
if (contractMeta == null || contractMeta.getStatus() == KILLED) { if (contractMeta == null || contractMeta.getStatus() == KILLED) {
LOGGER.debug("send ReRoute response:" + cr.toString()); LOGGER.debug("send ReRoute response: " + cr);
JsonObject result = new JsonObject(); JsonObject result = new JsonObject();
result.addProperty("action", "reRouteContract"); result.addProperty("action", "reRouteContract");
result.addProperty("responseID", cr.get("requestID").getAsString()); result.addProperty("responseID", cr.get("requestID").getAsString());
@ -248,20 +233,20 @@ public class MasterServerTCPAction {
return; return;
} }
MultiContractMeta info = MasterServerTCPAction.getMPCInfo(contractMeta.getID()); MultiContractMeta info = MasterServerTCPAction.getMPCInfo(contractMeta.getID());
// LOGGER.info("[MasterServerTCPAction] requestContractExecution " + new // LOGGER.info("[MasterServerTCPAction] requestContractExecution " + new
// Gson().toJson(jo)); // Gson().toJson(jo));
// LOGGER.info("MultiPointContractInfo info=" + (info == null ? "null" : "not // LOGGER.info("MultiPointContractInfo info=" + (info == null ? "null" : "not
// null") + // null") +
// "\n"); // "\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 // Just forward it to the correct Node
// Master节点直接发3个聚合后返回结果 // Master节点直接发3个聚合后返回结果
contractMeta.contractExecutor.execute( contractMeta.contractExecutor.execute(requestID,
requestID, JsonUtil.fromJson(cr, ContractRequest.class), new ResultCallback() {
JsonUtil.fromJson(cr, ContractRequest.class),
new ResultCallback() {
@Override @Override
public void onResult(String str) { public void onResult(String str) {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
@ -283,19 +268,20 @@ public class MasterServerTCPAction {
// 这个是个单节点的合约 // 这个是个单节点的合约
// executeContract(CacheTest应该要有多个进来 // executeContract(CacheTest应该要有多个进来
if (null != client && !client.getContractType().needSeq()) { if (null != client && !client.getContractType().needSeq()) {
contractMeta.contractExecutor.execute(requestID, contractRequest, new ResultCallback() { contractMeta.contractExecutor.execute(requestID, contractRequest,
@Override new ResultCallback() {
public void onResult(String str) { @Override
Map<String, String> result = new HashMap<>(); public void onResult(String str) {
result.put("action", "receiveContractExecutionServer"); Map<String, String> result = new HashMap<>();
result.put("responseID", cr.get("requestID").getAsString()); result.put("action", "receiveContractExecutionServer");
result.put("data", str); result.put("responseID", cr.get("requestID").getAsString());
cb.onResult(JsonUtil.toJson(result)); result.put("data", str);
CongestionControl.masterServerLoad.decrementAndGet(); cb.onResult(JsonUtil.toJson(result));
} CongestionControl.masterServerLoad.decrementAndGet();
}, null); }
}, null);
} else { } else {
LOGGER.debug("send ReRoute response:" + cr.toString()); LOGGER.debug("send ReRoute response:" + cr);
JsonObject result = new JsonObject(); JsonObject result = new JsonObject();
result.addProperty("action", "reRouteContract"); result.addProperty("action", "reRouteContract");
result.addProperty("responseID", cr.get("requestID").getAsString()); result.addProperty("responseID", cr.get("requestID").getAsString());

View File

@ -22,8 +22,7 @@ public class MasterServerTransferAction {
private static final Logger LOGGER = LogManager.getLogger(MasterServerTransferAction.class); private static final Logger LOGGER = LogManager.getLogger(MasterServerTransferAction.class);
private Map<String, OutputStream> fileMap = new HashMap<>(); private Map<String, OutputStream> fileMap = new HashMap<>();
public MasterServerTransferAction() { public MasterServerTransferAction() {}
}
@Action(async = true) @Action(async = true)
public void transferByYPK(JsonObject args, ResultCallback cb) { public void transferByYPK(JsonObject args, ResultCallback cb) {
@ -54,7 +53,8 @@ public class MasterServerTransferAction {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else fout = (FileOutputStream) fileMap.get(fileKey); } else
fout = (FileOutputStream) fileMap.get(fileKey);
if (isDone) { if (isDone) {
if (fout != null) if (fout != null)
try { try {
@ -88,11 +88,10 @@ public class MasterServerTransferAction {
c.setID(contractID); c.setID(contractID);
c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr()); c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
c.doSignature(GlobalConf.instance.keyPair);
try { try {
File temp = new File(path); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -119,7 +118,6 @@ public class MasterServerTransferAction {
c.setID(contractID); c.setID(contractID);
c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr()); c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
c.doSignature(GlobalConf.instance.keyPair);
c.setScript(script); c.setScript(script);
return CMActions.manager.startContract(c); return CMActions.manager.startContract(c);
@ -145,7 +143,8 @@ public class MasterServerTransferAction {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else fout = (FileOutputStream) fileMap.get(fileName); } else
fout = (FileOutputStream) fileMap.get(fileName);
if (isDone) { if (isDone) {
if (fout != null) if (fout != null)
try { try {

View File

@ -4,20 +4,17 @@ import com.google.gson.JsonObject;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.server.action.Action; import org.bdware.server.action.Action;
import org.bdware.units.NetworkManager;
import org.bdware.units.beans.MultiPointContractInfo; import org.bdware.units.beans.MultiPointContractInfo;
import org.bdware.units.function.ContractUnitManager; import org.bdware.units.function.ContractUnitManager;
import org.bdware.units.function.ExecutionManager; import org.bdware.units.function.ExecutionManager;
import org.bdware.units.function.SequencingManager; import org.bdware.units.function.SequencingManager;
/** /**
* 负责集群信息的方法 * 负责集群信息的方法 如获取集群信息master信息等
* 如获取集群信息master信息等
*/ */
public class UnitsInfoAction { public class UnitsInfoAction {
public UnitsInfoAction() { public UnitsInfoAction() {}
}
@Action(async = true) @Action(async = true)
public void getContractUnit(String contractId, final ResultCallback callback) { public void getContractUnit(String contractId, final ResultCallback callback) {
@ -29,7 +26,8 @@ public class UnitsInfoAction {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action", "getContractUnit"); jsonObject.addProperty("action", "getContractUnit");
jsonObject.addProperty("contractId", contractId); 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 jsonObject = new JsonObject();
jsonObject.addProperty("action", "getContractUnitMaster"); jsonObject.addProperty("action", "getContractUnitMaster");
jsonObject.addProperty("content", contractId); 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) @Action(async = true)
public void updateContractStartInfo(JsonObject args, final ResultCallback callback) { public void updateContractStartInfo(JsonObject args, final ResultCallback callback) {
System.out.println("[updateContractStartInfo] " + args); 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(); String contractID = args.get("contractID").getAsString();
ContractUnitManager.instance.addContractInfo(contractID, contractInfo); ContractUnitManager.instance.addContractInfo(contractID, contractInfo);
SequencingManager.instance.updateConsensusCenter(contractID, contractInfo); SequencingManager.instance.updateConsensusCenter(contractID, contractInfo);

View File

@ -35,8 +35,7 @@ import java.io.File;
public class _UNUSED_ExecutionAction implements OnHashCallback { public class _UNUSED_ExecutionAction implements OnHashCallback {
private static final Logger LOGGER = LogManager.getLogger(_UNUSED_ExecutionAction.class); private static final Logger LOGGER = LogManager.getLogger(_UNUSED_ExecutionAction.class);
public _UNUSED_ExecutionAction() { public _UNUSED_ExecutionAction() {}
}
// TODO TOMerge // TODO TOMerge
@Action(async = true) @Action(async = true)
@ -59,7 +58,6 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
// contractID not hash // contractID not hash
c.setID(contractID); c.setID(contractID);
c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr()); c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr());
c.doSignature(GlobalConf.instance.keyPair);
// c.setConsensusType(sequencing); // c.setConsensusType(sequencing);
// c.setNetworkType(network); // c.setNetworkType(network);
if (content != null && content.startsWith("/")) { if (content != null && content.startsWith("/")) {
@ -72,23 +70,18 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
} else { } else {
unitContractMessage.msgType = unitContractMessage.msgType =
UnitContractMessageType.ContractUnitResponse.getValue(); UnitContractMessageType.ContractUnitResponse.getValue();
// unitContractMessage.cuType = // unitContractMessage.cuType =
// UnitContractRequestType.START.getValue(); // UnitContractRequestType.START.getValue();
unitContractMessage.action = "startContractResponse"; unitContractMessage.action = "startContractResponse";
unitContractMessage.content = unitContractMessage.content =
JsonUtil.toJson( JsonUtil.toJson(new ContractResult(ContractResult.Status.Exception,
new ContractResult( new JsonPrimitive("Contract not found")));
ContractResult.Status.Exception, reply = UnitMessage.newBuilder()
new JsonPrimitive("Contract not found"))); .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
reply = .setSender(GlobalConf.instance.peerID).addReceiver(sender)
UnitMessage.newBuilder() .setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .build();
.setSender(GlobalConf.instance.peerID) // byte[] bytes = reply.toByteArray();
.addReceiver(sender)
.setContent(
ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
.build();
// byte[] bytes = reply.toByteArray();
callback.onResult(JsonUtil.toJson(reply)); callback.onResult(JsonUtil.toJson(reply));
return; return;
} }
@ -97,24 +90,21 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
c.setScript(content); c.setScript(content);
} }
content = CMActions.manager.startContractAndRedirect(c, System.out); content = CMActions.manager.startContract(c);
LOGGER.info("[START] result: " + content); LOGGER.info("[START] result: " + content);
result = JsonUtil.fromJson(content, ContractResult.class); result = JsonUtil.fromJson(content, ContractResult.class);
unitContractMessage.msgType = UnitContractMessageType.ContractUnitResponse.getValue(); unitContractMessage.msgType = UnitContractMessageType.ContractUnitResponse.getValue();
// unitContractMessage.statusType = result.status == // unitContractMessage.statusType = result.status ==
// ContractResult.Status.Success?0:1; // ContractResult.Status.Success?0:1;
unitContractMessage.action = "startContractResponse"; unitContractMessage.action = "startContractResponse";
// Collector investigates success or fail // Collector investigates success or fail
unitContractMessage.content = JsonUtil.toJson(result); unitContractMessage.content = JsonUtil.toJson(result);
// TODO save privKey in contractClient // TODO save privKey in contractClient
unitContractMessage.pubkey = ""; unitContractMessage.pubkey = "";
reply = reply = UnitMessage.newBuilder()
UnitMessage.newBuilder() .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .setSender(GlobalConf.instance.peerID).addReceiver(sender)
.setSender(GlobalConf.instance.peerID) .setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage))).build();
.addReceiver(sender)
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
.build();
byte[] bytes = reply.toByteArray(); byte[] bytes = reply.toByteArray();
callback.onResult(ByteUtil.encodeBASE64(bytes)); callback.onResult(ByteUtil.encodeBASE64(bytes));
if (result.status == ContractResult.Status.Success) { if (result.status == ContractResult.Status.Success) {
@ -131,10 +121,8 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
System.out.println("[BaseFunctionManager] handleResponse null"); System.out.println("[BaseFunctionManager] handleResponse null");
return; return;
} }
if (responseCenter.updateReply( if (responseCenter.updateReply(arg.get("content").getAsString(),
arg.get("content").getAsString(), arg.get("sender").getAsString(), arg.get("pubkey").getAsString())) {
arg.get("sender").getAsString(),
arg.get("pubkey").getAsString())) {
// save contractInfo // save contractInfo
String contractID = arg.get("contractID").getAsString(); String contractID = arg.get("contractID").getAsString();
int sequencing = arg.get("sequencing").getAsInt(); int sequencing = arg.get("sequencing").getAsInt();
@ -159,62 +147,54 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
@Action(async = true) @Action(async = true)
public void executeContractAtSlave(JsonObject args, final ResultCallback callback) { public void executeContractAtSlave(JsonObject args, final ResultCallback callback) {
String sender = args.get("sender").getAsString(); String sender = args.get("sender").getAsString();
UnitContractMessage unitContractMessage = JsonUtil.fromJson(args, UnitContractMessage.class); UnitContractMessage unitContractMessage =
JsonUtil.fromJson(args, UnitContractMessage.class);
String requestID = unitContractMessage.requestID; String requestID = unitContractMessage.requestID;
int sequencing = unitContractMessage.sequencing; int sequencing = unitContractMessage.sequencing;
int network = unitContractMessage.network; int network = unitContractMessage.network;
// int rcheck = unitContractMessage.rcheck; // int rcheck = unitContractMessage.rcheck;
String contractID = unitContractMessage.contractID; String contractID = unitContractMessage.contractID;
String operation = unitContractMessage.operation; String operation = unitContractMessage.operation;
String arg = unitContractMessage.arg; String arg = unitContractMessage.arg;
// String content = unitContractMessage.content; // String content = unitContractMessage.content;
LOGGER.info("[executeContractAtSlave] [EXECUTE] " + args); LOGGER.info("[executeContractAtSlave] [EXECUTE] " + args);
final ContractRequest c = new ContractRequest(); final ContractRequest c = new ContractRequest();
c.setContractID(contractID); c.setContractID(contractID);
c.setRequestID(requestID); c.setRequestID(requestID);
// c.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean(); // c.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
// c.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean(); // c.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
c.setAction(operation); c.setAction(operation);
c.setArg(arg); c.setArg(arg);
// if (c.withEvaluatesAnalysis) { // if (c.withEvaluatesAnalysis) {
// c.setValue(jo.get("hasValue").getAsLong()); // c.setValue(jo.get("hasValue").getAsLong());
// } // }
// if (jo.has("gasLimit")) { // if (jo.has("gasLimit")) {
// c.setGasLimit(jo.get("gasLimit").getAsLong()); // c.setGasLimit(jo.get("gasLimit").getAsLong());
// } // }
// if (args.has("requester")) { // if (args.has("requester")) {
// c.setPublicKey(args.get("requester").getAsString()); // c.setPublicKey(args.get("requester").getAsString());
// c.setSignature(args.get("signature").getAsString()); // c.setSignature(args.get("signature").getAsString());
// } // }
// c.setPublicKey(args.get("pubkey").getAsString()); // c.setPublicKey(args.get("pubkey").getAsString());
// c.setSignature(args.get("signature").getAsString()); // c.setSignature(args.get("signature").getAsString());
String reqID; String reqID;
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
CMActions.manager.executeContractInternal( CMActions.manager.executeContractInternal(c, new ResultCallback() {
c, @Override
new ResultCallback() { public void onResult(String str) {
@Override // TODO qianming
public void onResult(String str) { unitContractMessage.signature = str;
// TODO qianming unitContractMessage.action = "executeContractResponse";
unitContractMessage.signature = str; unitContractMessage.content = str;
unitContractMessage.action = "executeContractResponse"; UnitMessage unitMessage = UnitMessage.newBuilder()
unitContractMessage.content = str; .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
UnitMessage unitMessage = .setSender(GlobalConf.instance.peerID).addReceiver(sender)
UnitMessage.newBuilder() .setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
.setMsgType( .build();
BDLedgerContract.UnitMessageType byte[] bytes = unitMessage.toByteArray();
.UnitContractMessage) callback.onResult(ByteUtil.encodeBASE64(bytes));
.setSender(GlobalConf.instance.peerID) }
.addReceiver(sender) }, null);
.setContent(
ByteString.copyFromUtf8(
JsonUtil.toJson(unitContractMessage)))
.build();
byte[] bytes = unitMessage.toByteArray();
callback.onResult(ByteUtil.encodeBASE64(bytes));
}
},
null);
} }
@Action(async = true) @Action(async = true)
@ -226,10 +206,8 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
System.out.println("[BaseFunctionManager] responseCenter null"); System.out.println("[BaseFunctionManager] responseCenter null");
return; return;
} }
if (responseCenter.updateReply( if (responseCenter.updateReply(arg.get("content").getAsString(),
arg.get("content").getAsString(), arg.get("sender").getAsString(), arg.get("signature").getAsString())) {
arg.get("sender").getAsString(),
arg.get("signature").getAsString())) {
// call back // call back
responseCenter.formReply(); responseCenter.formReply();
ExecutionManager.instance.responseCenters.remove(requestID); ExecutionManager.instance.responseCenters.remove(requestID);
@ -239,38 +217,30 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
@Action(async = true) @Action(async = true)
public void stopContractAtSlave(JsonObject args, final ResultCallback callback) { public void stopContractAtSlave(JsonObject args, final ResultCallback callback) {
String sender = args.get("sender").getAsString(); String sender = args.get("sender").getAsString();
UnitContractMessage unitContractMessage = JsonUtil.fromJson(args, UnitContractMessage.class); UnitContractMessage unitContractMessage =
JsonUtil.fromJson(args, UnitContractMessage.class);
String requestID = unitContractMessage.requestID; String requestID = unitContractMessage.requestID;
int sequencing = unitContractMessage.sequencing; int sequencing = unitContractMessage.sequencing;
int network = unitContractMessage.network; int network = unitContractMessage.network;
// int rcheck = unitContractMessage.rcheck; // int rcheck = unitContractMessage.rcheck;
String contractID = unitContractMessage.contractID; String contractID = unitContractMessage.contractID;
LOGGER.info("[stopContractAtSlave] [EXECUTE] " + args); LOGGER.info("[stopContractAtSlave] [EXECUTE] " + args);
final ContractRequest c = new ContractRequest(); final ContractRequest c = new ContractRequest();
c.setContractID(contractID); c.setContractID(contractID);
c.setRequestID(requestID); c.setRequestID(requestID);
String ret = String ret = CMActions.manager
CMActions.manager.stopContractWithOwner( .stopContractWithOwner(GlobalConf.instance.keyPair.getPublicKeyStr(), contractID);
GlobalConf.instance.keyPair.getPublicKeyStr(), contractID); GRPCPool.instance.writeToChain(this, contractID, "stopContract", ret,
GRPCPool.instance.writeToChain( args.has("requestID") ? args.get("requestID").getAsString() : null, "");
this,
contractID,
"stopContract",
ret,
args.has("requestID") ? args.get("requestID").getAsString() : null,
"");
ContractResult contractResult = ContractResult contractResult =
new ContractResult(ContractResult.Status.Success, new JsonPrimitive("success")); new ContractResult(ContractResult.Status.Success, new JsonPrimitive("success"));
unitContractMessage.signature = ret; unitContractMessage.signature = ret;
unitContractMessage.action = "stopContractResponse"; unitContractMessage.action = "stopContractResponse";
unitContractMessage.content = JsonUtil.toJson(contractResult); unitContractMessage.content = JsonUtil.toJson(contractResult);
UnitMessage unitMessage = UnitMessage unitMessage = UnitMessage.newBuilder()
UnitMessage.newBuilder() .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .setSender(GlobalConf.instance.peerID).addReceiver(sender)
.setSender(GlobalConf.instance.peerID) .setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage))).build();
.addReceiver(sender)
.setContent(ByteString.copyFromUtf8(JsonUtil.toJson(unitContractMessage)))
.build();
byte[] bytes = unitMessage.toByteArray(); byte[] bytes = unitMessage.toByteArray();
callback.onResult(ByteUtil.encodeBASE64(bytes)); callback.onResult(ByteUtil.encodeBASE64(bytes));
} }
@ -285,10 +255,8 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
System.out.println("[BaseFunctionManager] responseCenter null"); System.out.println("[BaseFunctionManager] responseCenter null");
return; return;
} }
if (responseCenter.updateReply( if (responseCenter.updateReply(arg.get("content").getAsString(),
arg.get("content").getAsString(), arg.get("sender").getAsString(), arg.get("signature").getAsString())) {
arg.get("sender").getAsString(),
arg.get("signature").getAsString())) {
// call back // call back
boolean allSuccess = responseCenter.isAllSuccess(); boolean allSuccess = responseCenter.isAllSuccess();
responseCenter.formReply(); responseCenter.formReply();
@ -300,6 +268,5 @@ public class _UNUSED_ExecutionAction implements OnHashCallback {
} }
@Override @Override
public void publishHash(String reqID, String hashStr) { public void publishHash(String reqID, String hashStr) {}
}
} }

View 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;
}
}

View File

@ -1,34 +1,55 @@
package org.bdware.server.doip; package org.bdware.server.doip;
import com.google.gson.JsonArray; import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import org.bdware.doip.core.doipMessage.DoipMessage; import org.apache.logging.log4j.LogManager;
import org.bdware.doip.core.doipMessage.DoipResponseCode; import org.apache.logging.log4j.Logger;
import org.bdware.doip.core.model.digitalObject.DigitalObject; import org.bdware.doip.audit.server.DefaultRepoHandlerInjector;
import org.bdware.doip.core.model.digitalObject.DoType; import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.core.model.digitalObject.Element; import org.bdware.doip.codec.digitalObject.DoType;
import org.bdware.doip.core.model.operations.BasicOperations; import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.endpoint.doipServer.DoipServiceInfo; import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.endpoint.doipServer.Op; import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.endpoint.doipServer.RepositoryHandlerBase; 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.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.ContractRequest;
import org.bdware.sc.bean.FunctionDesp; import org.bdware.sc.bean.FunctionDesp;
import org.bdware.sc.conn.ResultCallback; 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.sc.util.JsonUtil;
import org.bdware.server.GlobalConf;
import org.bdware.server.action.CMActions; 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.ArrayList;
import java.util.List; import java.util.List;
import static org.bdware.server.doip.ContractRepositoryMain.currentIrpClient;
@ChannelHandler.Sharable @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) { public ContractRepositoryHandler(DoipServiceInfo info) {
super(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 @Override
@ -38,6 +59,11 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
@Override @Override
public DoipMessage handleListOps(DoipMessage doipMessage) { 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(); JsonArray ops = new JsonArray();
ops.add(BasicOperations.Hello.getName()); ops.add(BasicOperations.Hello.getName());
ops.add("call"); ops.add("call");
@ -46,7 +72,126 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
@Override @Override
public DoipMessage handleCreate(DoipMessage doipMessage) { 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 @Override
@ -56,36 +201,65 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
@Override @Override
public DoipMessage handleDelete(DoipMessage doipMessage) { 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 @Override
public DoipMessage handleRetrieve(DoipMessage doipMessage) { 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; ContractClient client = null;
try { try {
client = CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id); client = CMActions.manager.getContractClientByDoi(idOrName);
} catch (Exception e) { } catch (Exception e) {
} }
if (client == null) { if (client == null) {
String doid = doipMessage.header.parameters.id; String doid = idOrName;
int off = doid.lastIndexOf("/"); int off = doid.lastIndexOf("/");
if (off > 0) if (off > 0)
doid = doid.substring(off + 1); doid = doid.substring(off + 1);
client = CMActions.manager.getContractClientByDoi(doid); client = CMActions.manager.getContractClientByDoi(doid);
} }
if (client == null) { if (client == null) {
return replyStringWithStatus( return replyStringWithStatus(doipMessage, "BDO instance not exist!",
doipMessage, "Contract instance not exist!", DoipResponseCode.DoNotFound); DoipResponseCode.DoNotFound);
} }
byte[] data = doipMessage.body.getEncodedData(); byte[] data = doipMessage.body.getEncodedData();
if (data == null || data.length == 0) { if (data == null || data.length == 0) {
DigitalObject contractDO = contract2DigitalObject(client); JsonObject contractDO = contract2DigitalObject(client);
return replyDO(doipMessage, contractDO); return replyString(doipMessage, contractDO.toString());
} else { } else {
JsonObject jo = JsonUtil.parseStringAsJsonObject(new String(data)); JsonObject jo = JsonUtil.parseStringAsJsonObject(new String(data));
List<JsonElement> result = new ArrayList<>(); List<JsonElement> result = new ArrayList<>();
jo.addProperty("contractID", client.contractMeta.getID()); jo.addProperty("bdoSuffix", client.contractMeta.getID());
CMActions.executeContractInternal(jo, new ResultCallback() { CMActions.executeContractInternal(jo, new ResultCallback() {
@Override @Override
public void onResult(JsonObject str) { public void onResult(JsonObject str) {
@ -112,7 +286,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
result.add(new JsonPrimitive("Timeout!")); result.add(new JsonPrimitive("Timeout!"));
retStatus = DoipResponseCode.MoreThanOneErrors; 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("status", retStatus.getName());
digitalObject.addAttribute("result", result.get(0)); digitalObject.addAttribute("result", result.get(0));
@ -120,40 +295,57 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
} }
} }
private DigitalObject contract2DigitalObject(ContractClient contractClient) { private DoipMessage retrieveList(DoipMessage message) {
DigitalObject contractDO = new DigitalObject(contractClient.getContractDOI(), DoType.DO); JsonArray listDO = new JsonArray();
contractDO.addAttribute("contractName", contractClient.getContractName()); for (String key : CMActions.manager.statusRecorder.getStatus().keySet()) {
// contractDO.addAttribute("script",contract.contract.getScriptStr()); ContractClient contractClient = CMActions.manager.getContractClientByDoi(key);
contractDO.addAttribute("owner", contractClient.contractMeta.contract.getOwner()); 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()) { for (FunctionDesp f : contractClient.getExportedFunctions()) {
Element fe = new Element(f.functionName, "function"); JsonObject jo = new JsonObject();
fe.setAttribute("annotation", JsonUtil.toJson(f.annotations)); jo.addProperty("opeationName", f.functionName);
contractDO.addElements(fe); jo.add("annotation", JsonUtil.parseObject(f.annotations));
operations.add(jo);
} }
return contractDO; return contractDO;
} }
@Op(op = BasicOperations.Extension, name = "call") @Op(op = BasicOperations.Extension, name = "call")
public DoipMessage handleCall(DoipMessage doipMessage) { 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) if (cc == null)
return replyStringWithStatus( return replyStringWithStatus(doipMessage, "Contract instance not exist!",
doipMessage, "Contract instance not exist!", DoipResponseCode.DoNotFound); DoipResponseCode.DoNotFound);
// todo transform doipMessage to args // todo transform doipMessage to args
JsonObject args = doipMessage.header.parameters.attributes; JsonObject args = doipMessage.header.parameters.attributes;
ContractRequest cr = new ContractRequest(); ContractRequest cr = new ContractRequest();
cr.setContractID(cc.getContractID()); cr.setContractID(cc.getContractID());
cr.setContractDOI(doipMessage.header.parameters.id);
if (args.has("withDynamicAnalysis")) if (args.has("withDynamicAnalysis"))
cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean(); cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
if (args.has("withEvaluatesAnalysis")) if (args.has("withEvaluatesAnalysis"))
cr.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean(); cr.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
if (args.get("elementID") == null) if (args.get("elementID") == null)
return replyStringWithStatus( return replyStringWithStatus(doipMessage, "missing elementID",
doipMessage, "missing elementID", DoipResponseCode.Invalid); DoipResponseCode.Invalid);
cr.setAction(args.get("elementID").getAsString()); cr.setAction(args.get("elementID").getAsString());
if (doipMessage.body.getEncodedData() != null) if (doipMessage.body.getEncodedData() != null)
@ -163,7 +355,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
cr.setRequesterDOI(doipMessage.credential.getSigner()); cr.setRequesterDOI(doipMessage.credential.getSigner());
String reqID; String reqID;
if (args.has("requestID")) reqID = args.get("requestID").getAsString(); if (args.has("requestID"))
reqID = args.get("requestID").getAsString();
else { else {
reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 1000); reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 1000);
args.addProperty("requestID", reqID); args.addProperty("requestID", reqID);
@ -172,11 +365,43 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase {
String resp = CMActions.manager.execute(cr, null); String resp = CMActions.manager.execute(cr, null);
// StringContainer sc = new StringContainer(); // StringContainer sc = new StringContainer();
// SyncContainer sync = new SyncContainer(sc); // SyncContainer sync = new SyncContainer(sc);
return replyString(doipMessage, resp); 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));
}
} }

View File

@ -1,50 +1,118 @@
package org.bdware.server.doip; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bdware.doip.core.crypto.GlobalCertifications; import org.bdware.doip.audit.EndpointConfig;
import org.bdware.doip.core.doipMessage.DoipMessageFormat; import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.core.utils.GlobalConfigurations; import org.bdware.doip.audit.server.AuditDoipServer;
import org.bdware.doip.endpoint.doipServer.DoipListenerInfo; import org.bdware.doip.audit.writer.AuditType;
import org.bdware.doip.endpoint.doipServer.DoipServerImpl; import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.endpoint.doipServer.DoipServiceInfo; import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.server.DOAConf; 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.GlobalConf;
import org.bdware.server.action.CMActions;
import org.zz.gmhelper.SM2KeyPair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ContractRepositoryMain { public class ContractRepositoryMain {
private static final Logger LOGGER = LogManager.getLogger(ContractRepositoryMain.class); 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() { public static void start() {
try { try {
String[] ipAndPort = GlobalConf.instance.masterAddress.split(":"); LOGGER.info("DOIPServer, start" + new Gson().toJson(GlobalConf.instance.doaConf));
String url = String.format("tcp://%s:%d", ipAndPort[0], Integer.valueOf(ipAndPort[1]) + 1); String url = GlobalConf.instance.doaConf.doipAddress;
String doid = DOAConf.repoDoid; forwarder = new LocalDoipFrowarder();
LOGGER.info("DOIPServer start:" + url); if (url == null || GlobalConf.instance.doaConf.repoDoid.isEmpty()) {
if (url == null || doid == null) { LOGGER.warn("missing args, failed to start! url:" + url + " doid:"
LOGGER.warn("missing args, failed to start! url:" + url + " doid:" + doid); + GlobalConf.instance.doaConf.repoDoid);
return; return;
} }
if (currentServer != null) { if (currentServer != null) {
currentServer.stop(); 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\"}"); @Override
GlobalConfigurations.userID = GlobalCertifications.jwKeyPair.getKeyID(); public boolean accept(ChannelHandlerContext context, DoipMessage message) {
DoipServiceInfo info = new DoipServiceInfo(doid, GlobalConfigurations.userID, "ContractRepo", infos); String id = message.header.parameters.id;
currentServer = new DoipServerImpl(info); if (id != null) {
currentServer.setRepositoryHandler(new ContractRepositoryHandler(info)); id = id.replaceAll(".*/", "");
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
if (meta != null)
return true;
}
return false;
}
};
currentServer.setRequestCallback(delegateHandler);
currentServer.start(); currentServer.start();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
LOGGER.warn("load doip config exception!"); 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;
} }
} }

View File

@ -1,5 +0,0 @@
package org.bdware.server.doip;
public class ContractRepositoryServer {
}

View File

@ -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);
}

View 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();
}
}
}

View 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);
}

View File

@ -1,375 +1,378 @@
package org.bdware.server.executor.consistency; // package org.bdware.server.executor.consistency;
//
import com.google.gson.JsonObject; // import com.google.gson.JsonObject;
import com.google.gson.JsonParser; // import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; // import com.google.gson.JsonPrimitive;
import org.apache.logging.log4j.LogManager; // import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; // import org.apache.logging.log4j.Logger;
import org.bdware.sc.ComponedContractResult; // import org.bdware.sc.ComponedContractResult;
import org.bdware.sc.ContractResult; // import org.bdware.sc.ContractResult;
import org.bdware.sc.bean.ContractRequest; // import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.conn.Node; // import org.bdware.sc.conn.Node;
import org.bdware.sc.conn.OnHashCallback; // import org.bdware.sc.conn.OnHashCallback;
import org.bdware.sc.conn.ResultCallback; // import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.consistency.Committer; // import org.bdware.sc.consistency.Committer;
import org.bdware.sc.consistency.pbft.PBFTAlgorithm; // import org.bdware.sc.consistency.pbft.PBFTAlgorithm;
import org.bdware.sc.consistency.pbft.PBFTMember; // import org.bdware.sc.consistency.pbft.PBFTMember;
import org.bdware.sc.consistency.pbft.PBFTMessage; // import org.bdware.sc.consistency.pbft.PBFTMessage;
import org.bdware.sc.consistency.pbft.PBFTType; // import org.bdware.sc.consistency.pbft.PBFTType;
import org.bdware.sc.units.*; // import org.bdware.sc.units.*;
import org.bdware.sc.util.JsonUtil; // import org.bdware.sc.util.JsonUtil;
import org.bdware.server.GlobalConf; // import org.bdware.server.GlobalConf;
import org.bdware.server.action.CMActions; // import org.bdware.server.action.CMActions;
import org.bdware.server.action.p2p.MasterServerRecoverMechAction; // import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
import org.bdware.server.action.p2p.MasterServerTCPAction; // import org.bdware.server.action.p2p.MasterServerTCPAction;
import org.bdware.server.trustedmodel.ContractCluster; // import org.bdware.server.trustedmodel.ContractCluster;
import org.bdware.server.trustedmodel.ContractExecutor; // import org.bdware.server.trustedmodel.ContractExecutor;
import org.bdware.server.trustedmodel.MultiReqSeq; // import org.bdware.server.trustedmodel.MultiReqSeq;
import org.bdware.server.trustedmodel.ResultCollector; // import org.bdware.server.trustedmodel.ResultCollector;
import org.bdware.units.NetworkManager; // import org.bdware.units.NetworkManager;
import org.zz.gmhelper.SM2KeyPair; // import org.zz.gmhelper.SM2KeyPair;
//
import java.util.*; // import java.util.*;
import java.util.concurrent.ConcurrentHashMap; // import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; // import java.util.concurrent.atomic.AtomicInteger;
//
//TODO 追赶差下的调用 //// TODO 追赶差下的调用
public class PBFTExecutor implements ContractExecutor { // public class PBFTExecutor implements ContractExecutor {
private static final Logger LOGGER = LogManager.getLogger(PBFTExecutor.class); // private static final Logger LOGGER = LogManager.getLogger(PBFTExecutor.class);
final Object lock = new Object(); // final Object lock = new Object();
private final List<PubKeyNode> members; // private final List<PubKeyNode> members;
int resultCount; // int resultCount;
//
AtomicInteger request_index = new AtomicInteger(0); // AtomicInteger request_index = new AtomicInteger(0);
// key为requestIDvalue为其seq // // key为requestIDvalue为其seq
Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>(); // Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
Map<String, ResultCache> resultCache = new ConcurrentHashMap<>(); // Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
// MultiPointContractInfo info; // // MultiPointContractInfo info;
String contractID; // String contractID;
PBFTAlgorithm pbft; // PBFTAlgorithm pbft;
ContractCluster contractCluster; // ContractCluster contractCluster;
boolean isMaster; // boolean isMaster;
//
public PBFTExecutor( // public PBFTExecutor(
int c, String con_id, final String masterPubkey, String[] members) { // int c, String con_id, final String masterPubkey, String[] members) {
resultCount = c; // resultCount = c;
contractID = con_id; // contractID = con_id;
this.members = new ArrayList<>(); // this.members = new ArrayList<>();
isMaster = GlobalConf.getNodeID().equals(masterPubkey); // isMaster = GlobalConf.getNodeID().equals(masterPubkey);
pbft = new PBFTAlgorithm(isMaster); // pbft = new PBFTAlgorithm(isMaster);
int count = 0; // int count = 0;
for (String mem : members) { // for (String mem : members) {
PubKeyNode pubkeyNode = new PubKeyNode(); // PubKeyNode pubkeyNode = new PubKeyNode();
pubkeyNode.pubkey = mem; // pubkeyNode.pubkey = mem;
PBFTMember pbftMember = new PBFTMember(); // PBFTMember pbftMember = new PBFTMember();
pbftMember.isMaster = mem.equals(masterPubkey); // pbftMember.isMaster = mem.equals(masterPubkey);
pbft.addMember(pubkeyNode, pbftMember); // pbft.addMember(pubkeyNode, pbftMember);
this.members.add(pubkeyNode); // this.members.add(pubkeyNode);
if (GlobalConf.getNodeID().equals(mem)) { // if (GlobalConf.getNodeID().equals(mem)) {
pbft.setSendID(count); // pbft.setSendID(count);
} // }
count++; // count++;
} // }
contractCluster = new ContractCluster(contractID, this.members); // contractCluster = new ContractCluster(contractID, this.members);
pbft.setConnection(contractCluster); // pbft.setConnection(contractCluster);
final MultiContractMeta cei = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); // final MultiContractMeta cei =
pbft.setCommitter(new Committer() { // CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
@Override // pbft.setCommitter(new Committer() {
public void onCommit(ContractRequest data) { // @Override
ResultCallback ret = null; // public void onCommit(ContractRequest data) {
final long startTime = System.currentTimeMillis(); // ResultCallback ret = null;
ret = new ResultCallback() { // final long startTime = System.currentTimeMillis();
@Override // ret = new ResultCallback() {
public void onResult(String str) { // @Override
Map<String, String> ret = new HashMap<>(); // public void onResult(String str) {
ret.put("action", "receiveTrustfullyResult"); // Map<String, String> ret = new HashMap<>();
SM2KeyPair keyPair = GlobalConf.instance.keyPair; // ret.put("action", "receiveTrustfullyResult");
ret.put("nodeID", keyPair.getPublicKeyStr()); // SM2KeyPair keyPair = GlobalConf.instance.keyPair;
ret.put("responseID", data.getRequestID()); // ret.put("nodeID", keyPair.getPublicKeyStr());
ret.put("executeTime", (System.currentTimeMillis() - startTime) + ""); // ret.put("responseID", data.getRequestID());
ret.put("data", str); // ret.put("executeTime", (System.currentTimeMillis() - startTime) + "");
cei.setLastExeSeq(data.seq); // ret.put("data", str);
NetworkManager.instance.sendToAgent(masterPubkey, JsonUtil.toJson(ret)); // cei.setLastExeSeq(data.seq);
} // NetworkManager.instance.sendToAgent(masterPubkey, JsonUtil.toJson(ret));
}; // }
CMActions.manager.executeLocallyAsync(data, ret, null); // };
} // CMActions.manager.executeLocallyAsync(data, ret, null);
}); // }
} // });
// }
public void onSyncMessage(Node node, byte[] data) { //
// public void onSyncMessage(Node node, byte[] data) {
pbft.onMessage(node, data); //
} // pbft.onMessage(node, data);
// }
public void setSeq(int seq) { //
request_index = new AtomicInteger(seq); // public void setSeq(int seq) {
pbft.setAtomSeq(request_index.get()); // request_index = new AtomicInteger(seq);
} // pbft.setAtomSeq(request_index.get());
// }
public ResultCallback createResultCallback( //
final String requestID, // public ResultCallback createResultCallback(
final ResultCallback originalCb, // final String requestID,
final int count, // final ResultCallback originalCb,
final int request_seq, // final int count,
final String contractID) { // final int request_seq,
ComponedContractResult componedContractResult = new ComponedContractResult(count); // final String contractID) {
// TODO 加对应的超时 // ComponedContractResult componedContractResult = new ComponedContractResult(count);
return new ResultCollector( // // TODO 加对应的超时
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) { //
// Map<String, Object> reqStr = new HashMap<>(); // public void sendRequest(String id, ContractRequest req, ResultCallback collector) {
// reqStr.put("uniReqID", id); //// Map<String, Object> reqStr = new HashMap<>();
// reqStr.put("data", req); //// reqStr.put("uniReqID", id);
// reqStr.put("action", "executeContractLocally"); //// reqStr.put("data", req);
ContractRequest cr2 = ContractRequest.parse(req.toByte()); //// reqStr.put("action", "executeContractLocally");
cr2.setRequestID(id); // ContractRequest cr2 = ContractRequest.parse(req.toByte());
PBFTMessage request = new PBFTMessage(); // cr2.setRequestID(id);
request.setOrder(req.seq); // PBFTMessage request = new PBFTMessage();
request.setType(PBFTType.Request); // request.setOrder(req.seq);
request.setContent(cr2.toByte()); // request.setType(PBFTType.Request);
for (PubKeyNode node : members) { // request.setContent(cr2.toByte());
if (!NetworkManager.instance.hasAgentConnection(node.pubkey)) { // for (PubKeyNode node : members) {
LOGGER.warn("cmNode " + node.pubkey.substring(0, 5) + " is null"); // if (!NetworkManager.instance.hasAgentConnection(node.pubkey)) {
collector.onResult( // LOGGER.warn("cmNode " + node.pubkey.substring(0, 5) + " is null");
"{\"status\":\"Error\",\"result\":\"node offline\"," // collector.onResult(
+ "\"nodeID\":\"" // "{\"status\":\"Error\",\"result\":\"node offline\","
+ node // + "\"nodeID\":\""
+ "\"," // + node
+ "\"action\":\"onExecuteContractTrustfully\"}"); // + "\","
// } else if (MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID) // + "\"action\":\"onExecuteContractTrustfully\"}");
// != RecoverFlag.Fine) { //// } else if (MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
// collector.onResult( //// != RecoverFlag.Fine) {
// "{\"status\":\"Error\",\"result\":\"node recovering\"," //// collector.onResult(
// + "\"nodeID\":\"" //// "{\"status\":\"Error\",\"result\":\"node recovering\","
// + node //// + "\"nodeID\":\""
// + "\"," //// + node
// + "\"action\":\"onExecuteContractTrustfully\"}"); //// + "\","
// contractCluster.sendMessage(node, request.getBytes()); //// + "\"action\":\"onExecuteContractTrustfully\"}");
} else { //// contractCluster.sendMessage(node, request.getBytes());
contractCluster.sendMessage(node, request.getBytes()); // } else {
} // contractCluster.sendMessage(node, request.getBytes());
} // }
// master负责缓存请求 // }
if (!MasterServerTCPAction.requestCache.containsKey(contractID)) { // // master负责缓存请求
MasterServerTCPAction.requestCache.put(contractID, new RequestCache()); // if (!MasterServerTCPAction.requestCache.containsKey(contractID)) {
} // MasterServerTCPAction.requestCache.put(contractID, new RequestCache());
// TODO 多调多统一个seq的有多个请求这个需要改 // }
String[] nodes = // // TODO 多调多统一个seq的有多个请求这个需要改
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers(); // String[] nodes =
LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID()); // CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes)); // LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID());
// LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes));
} //
// }
//
public boolean checkCurNodeNumValid() { //
return true; // public boolean checkCurNodeNumValid() {
} // return true;
// }
@Override //
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) { // @Override
LOGGER.debug(JsonUtil.toJson(req)); // public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
MultiContractMeta meta = CMActions.manager.multiContractRecorder.getMultiContractMeta(req.getContractID()); // {
if (meta == null || !meta.isMaster()) { // LOGGER.debug(JsonUtil.toJson(req));
CMActions.manager.executeContractOnOtherNodes(req, rc); // MultiContractMeta meta =
return; // CMActions.manager.multiContractRecorder.getMultiContractMeta(req.getContractID());
} // if (meta == null || !meta.isMaster()) {
req.setContractID(CMActions.manager.getContractIDByName(req.getContractID())); // CMActions.manager.executeContractOnOtherNodes(req, rc);
// return;
// 三个相同requestID进来的时候会有冲突 // }
// 仅在此处有冲突么 // req.setContractID(CMActions.manager.getContractIDByName(req.getContractID()));
// 这里是从MasterServer->MasterClient请求的是"executeContractLocally" //
// // 三个相同requestID进来的时候会有冲突
// 如果是多点合约的请求A1A2A3的序号应该一致不能分配一个新的seq根据requestID判断是否不需要重新分配一个序号 // // 仅在此处有冲突么
//TODO seqMap memory leak // // 这里是从MasterServer->MasterClient请求的是"executeContractLocally"
//TODO //
//TODO // // 如果是多点合约的请求A1A2A3的序号应该一致不能分配一个新的seq根据requestID判断是否不需要重新分配一个序号
if (null != requestID && requestID.endsWith("_mul")) { // //TODO seqMap memory leak
synchronized (lock) { // //TODO
if (seqMap.containsKey(requestID)) { // //TODO
req.seq = seqMap.get(requestID).seq; // if (null != requestID && requestID.endsWith("_mul")) {
} else { // synchronized (lock) {
req.seq = request_index.getAndIncrement(); // if (seqMap.containsKey(requestID)) {
seqMap.put(requestID, new MultiReqSeq(req.seq)); // req.seq = seqMap.get(requestID).seq;
} // } else {
} // req.seq = request_index.getAndIncrement();
} else { // seqMap.put(requestID, new MultiReqSeq(req.seq));
req.seq = request_index.getAndIncrement(); // }
} // }
req.needSeq = true; // } else {
String id = // req.seq = request_index.getAndIncrement();
System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq; // }
LOGGER.info("execute receive requestID=" + requestID + " msgID=" + id); // req.needSeq = true;
// String id =
if (checkCurNodeNumValid()) { // System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq;
LOGGER.debug("checkCurNodeNumValid=true"); // LOGGER.info("execute receive requestID=" + requestID + " msgID=" + id);
ResultCallback collector = //
createResultCallback(id, rc, resultCount, req.seq, req.getContractID()); // if (checkCurNodeNumValid()) {
MasterServerTCPAction.sync.sleep(id, collector); // LOGGER.debug("checkCurNodeNumValid=true");
LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq); // ResultCallback collector =
sendRequest(id, req, collector); // createResultCallback(id, rc, resultCount, req.seq, req.getContractID());
} else { // MasterServerTCPAction.sync.sleep(id, collector);
LOGGER.debug("invalidNodeNumOnResult"); // LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq);
request_index.getAndDecrement(); // sendRequest(id, req, collector);
ContractResult finalResult = // } else {
new ContractResult( // LOGGER.debug("invalidNodeNumOnResult");
ContractResult.Status.Error, // request_index.getAndDecrement();
new JsonPrimitive("node number unavailable, request refused.")); // ContractResult finalResult =
rc.onResult(JsonUtil.toJson(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(); // /* // 三个相同requestID进来的时候会有冲突
req.needSeq = true; // // 仅在此处有冲突么
ResultCallback collector = createResultCallback(id, rc, resultCount, req.getContractID()); // // 这里是从MasterServer->MasterClient请求的是"executeContractLocally"
MasterServerTCPAction.sync.sleep(id, collector); // req.seq = request_index.getAndIncrement();
sendRequest(id, req, collector);*/ // 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( // public void clearCache() {
entry -> { // final long time = System.currentTimeMillis() - 30000L;
MultiReqSeq cache = entry.getValue(); // seqMap.entrySet()
if (null == cache) { // .removeIf(
return true; // entry -> {
} // MultiReqSeq cache = entry.getValue();
return cache.startTime < time; // if (null == cache) {
}); // return true;
} // }
// return cache.startTime < time;
public static class ResultMerger extends ResultCallback { // });
ComponedContractResult componedContractResult; // }
AtomicInteger order; //
String contractID; // public static class ResultMerger extends ResultCallback {
int count; // ComponedContractResult componedContractResult;
int request_seq; // AtomicInteger order;
ResultCallback originalCallback; // String contractID;
Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点 // int count;
// int request_seq;
ResultMerger( // ResultCallback originalCallback;
final ResultCallback originalCb, // Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
final int count, //
final int request_seq, // ResultMerger(
final String contractID) { // final ResultCallback originalCb,
originalCallback = originalCb; // final int count,
this.count = count; // final int request_seq,
this.request_seq = request_seq; // final String contractID) {
this.contractID = contractID; // originalCallback = originalCb;
componedContractResult = new ComponedContractResult(count); // this.count = count;
order = new AtomicInteger(0); // this.request_seq = request_seq;
} // this.contractID = contractID;
// componedContractResult = new ComponedContractResult(count);
public String getContractID() { // order = new AtomicInteger(0);
return contractID; // }
} //
// public String getContractID() {
public String getInfo() { // return contractID;
return "contractID=" // }
+ contractID //
+ " 收到第 " // public String getInfo() {
+ order // return "contractID="
+ " 个节点回复 : " // + contractID
+ " order=" // + " 收到第 "
+ order // + order
+ " count=" // + " 个节点回复 : "
+ count // + " order="
+ " "; // + order
} // + " count="
// + count
@Override // + " ";
public void onResult(String str) { // }
// TODO 必须在这里聚合 //
// str的data是个ContractResult // @Override
// 在这儿也是返回个ContractResult // public void onResult(String str) {
try { // // TODO 必须在这里聚合
LOGGER.debug("a result of contract" + contractID + ": " + str); // // str的data是个ContractResult
JsonObject obj = JsonParser.parseString(str).getAsJsonObject(); // // 在这儿也是返回个ContractResult
if (obj.has("nodeID")) { // try {
String id = obj.get("nodeID").getAsString(); // LOGGER.debug("a result of contract" + contractID + ": " + str);
if (nodeIDs.contains(id)) { // JsonObject obj = JsonParser.parseString(str).getAsJsonObject();
LOGGER.debug( // if (obj.has("nodeID")) {
"ignored result because the result of node " // String id = obj.get("nodeID").getAsString();
+ id.substring(0, 5) // if (nodeIDs.contains(id)) {
+ " has been received"); // LOGGER.debug(
return; // "ignored result because the result of node "
} // + id.substring(0, 5)
nodeIDs.add(id); // + " has been received");
} // return;
// }
LOGGER.debug( // nodeIDs.add(id);
String.format( // }
"contractID=%s received=%s order=%d count=%d", //
contractID, str, order.get(), count)); // LOGGER.debug(
componedContractResult.add(obj); // String.format(
// 收集到所有结果 // "contractID=%s received=%s order=%d count=%d",
if (order.incrementAndGet() == count) { // contractID, str, order.get(), count));
ContractResult finalResult = componedContractResult.figureFinalResult(); // componedContractResult.add(obj);
finalResult.needSeq = true; // // 收集到所有结果
finalResult.seq = request_seq; // if (order.incrementAndGet() == count) {
// ContractResult finalResult = componedContractResult.figureFinalResult();
// if (null == finalResult) { // finalResult.needSeq = true;
// finalResult = // finalResult.seq = request_seq;
// new ContractResult( //
// ContractResult.Status.Exception, // // if (null == finalResult) {
// new JsonPrimitive( // // finalResult =
// "no nore than half of the // // new ContractResult(
// consistent result")); // // ContractResult.Status.Exception,
// originalCallback.onResult(new // // new JsonPrimitive(
// Gson().toJson(finalResult)); // // "no nore than half of the
// } else { // // consistent result"));
originalCallback.onResult(JsonUtil.toJson(finalResult)); // // originalCallback.onResult(new
// } // // Gson().toJson(finalResult));
LOGGER.debug( // // } else {
String.format( // originalCallback.onResult(JsonUtil.toJson(finalResult));
"%d results are the same: %s", // // }
finalResult.size, finalResult.result)); // LOGGER.debug(
// String.format(
// 集群中事务序号+1 // "%d results are the same: %s",
CMActions.manager.multiContractRecorder // finalResult.size, finalResult.result));
.getMultiContractMeta(contractID) //
.nextSeqAtMaster(); // // 集群中事务序号+1
// CMActions.manager.multiContractRecorder
// recover其中无状态合约CP出错无需恢复 // .getMultiContractMeta(contractID)
Set<String> nodesID = componedContractResult.getProblemNodes(); // .nextSeqAtMaster();
if (null == nodesID || nodesID.isEmpty()) { //
return; // // recover其中无状态合约CP出错无需恢复
} // Set<String> nodesID = componedContractResult.getProblemNodes();
for (String nodeID : nodesID) { // if (null == nodesID || nodesID.isEmpty()) {
LOGGER.warn("node fails! " + nodeID); // return;
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID) // }
== RecoverFlag.Fine) { // for (String nodeID : nodesID) {
MasterServerRecoverMechAction.recoverStatus // LOGGER.warn("node fails! " + nodeID);
.get(nodeID) // if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
.put(contractID, RecoverFlag.ToRecover); // == RecoverFlag.Fine) {
} // MasterServerRecoverMechAction.recoverStatus
} // .get(nodeID)
for (String nodeID : nodesID) { // .put(contractID, RecoverFlag.ToRecover);
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID) // }
== RecoverFlag.ToRecover) { // }
LOGGER.warn("node in recover " + nodeID); // for (String nodeID : nodesID) {
// if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
// 因为该节点结果有误所以即时是stableMode也认为trans记录不可信 // == RecoverFlag.ToRecover) {
// 直接通过load别的节点来恢复 // LOGGER.warn("node in recover " + nodeID);
MasterServerRecoverMechAction.restartContractFromCommonMode( //
nodeID, contractID); // // 因为该节点结果有误所以即时是stableMode也认为trans记录不可信
} // // 直接通过load别的节点来恢复
} // MasterServerRecoverMechAction.restartContractFromCommonMode(
} // nodeID, contractID);
// clearCache(); // }
} catch (Exception e) { // }
e.printStackTrace(); // }
LOGGER.warn("result exception!"); // // clearCache();
} // } catch (Exception e) {
} // e.printStackTrace();
} // LOGGER.warn("result exception!");
} // }
// }
// }
// }

View File

@ -40,11 +40,10 @@ public class RequestAllExecutor implements ContractExecutor {
// key为requestIDvalue为其seq // key为requestIDvalue为其seq
Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>(); Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
Map<String, ResultCache> resultCache = new ConcurrentHashMap<>(); Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
// MultiPointContractInfo info; // MultiPointContractInfo info;
String contractID; String contractID;
public RequestAllExecutor( public RequestAllExecutor(ContractExecType t, int c, String con_id) {
ContractExecType t, int c, String con_id) {
type = t; type = t;
resultCount = c; resultCount = c;
contractID = con_id; contractID = con_id;
@ -54,16 +53,13 @@ public class RequestAllExecutor implements ContractExecutor {
request_index = new AtomicInteger(seq); request_index = new AtomicInteger(seq);
} }
public ResultCallback createResultCallback( public ResultCallback createResultCallback(final String requestID,
final String requestID, final ResultCallback originalCb, final int count, final int request_seq,
final ResultCallback originalCb,
final int count,
final int request_seq,
final String contractID) { final String contractID) {
ComponedContractResult componedContractResult = new ComponedContractResult(count); ComponedContractResult componedContractResult = new ComponedContractResult(count);
// TODO 加对应的超时 // TODO 加对应的超时
return new ResultCollector( return new ResultCollector(requestID,
requestID, new ResultMerger(originalCb, count, request_seq, contractID), count); new ResultMerger(originalCb, count, request_seq, contractID), count);
} }
public void sendRequest(String id, ContractRequest req, ResultCallback collector) { public void sendRequest(String id, ContractRequest req, ResultCallback collector) {
@ -82,8 +78,8 @@ public class RequestAllExecutor implements ContractExecutor {
LOGGER.debug(JsonUtil.toJson(req)); LOGGER.debug(JsonUtil.toJson(req));
String[] nodes = String[] nodes = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers(); .getMembers();
LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID()); LOGGER.info("cluster size=" + nodes.length + " contract " + req.getContractID());
LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes)); LOGGER.debug("contract " + req.getContractID() + " cluster: " + JsonUtil.toJson(nodes));
for (String node : nodes) { for (String node : nodes) {
@ -91,19 +87,13 @@ public class RequestAllExecutor implements ContractExecutor {
if (!NetworkManager.instance.hasAgentConnection(node)) { if (!NetworkManager.instance.hasAgentConnection(node)) {
LOGGER.warn("cmNode " + node.substring(0, 5) + " is null"); LOGGER.warn("cmNode " + node.substring(0, 5) + " is null");
collector.onResult( collector.onResult(
"{\"status\":\"Error\",\"result\":\"node offline\"," "{\"status\":\"Error\",\"result\":\"node offline\"," + "\"nodeID\":\""
+ "\"nodeID\":\"" + node + "\"," + "\"action\":\"onExecuteContractTrustfully\"}");
+ node } else if (MasterServerRecoverMechAction.recoverStatus.get(node)
+ "\"," .get(contractID) != RecoverFlag.Fine) {
+ "\"action\":\"onExecuteContractTrustfully\"}");
} else if (MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)
!= RecoverFlag.Fine) {
collector.onResult( collector.onResult(
"{\"status\":\"Error\",\"result\":\"node recovering\"," "{\"status\":\"Error\",\"result\":\"node recovering\"," + "\"nodeID\":\""
+ "\"nodeID\":\"" + node + "\"," + "\"action\":\"onExecuteContractTrustfully\"}");
+ node
+ "\","
+ "\"action\":\"onExecuteContractTrustfully\"}");
NetworkManager.instance.sendToAgent(node, sendStr); NetworkManager.instance.sendToAgent(node, sendStr);
} else { } else {
LOGGER.info("send request to cmNode " + node.substring(0, 5)); LOGGER.info("send request to cmNode " + node.substring(0, 5));
@ -113,16 +103,17 @@ public class RequestAllExecutor implements ContractExecutor {
} }
public boolean checkCurNodeNumValid() { public boolean checkCurNodeNumValid() {
String[] nodes = String[] nodes = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers(); .getMembers();
int validNode = 0; int validNode = 0;
Map<String, String> mapResult = new HashMap<>(); Map<String, String> mapResult = new HashMap<>();
for (String node : nodes) { for (String node : nodes) {
mapResult.put(node.substring(0, 5), String.format("%s %s", NetworkManager.instance.hasAgentConnection(node) + "", mapResult.put(node.substring(0, 5),
MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID))); String.format("%s %s", NetworkManager.instance.hasAgentConnection(node) + "",
MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID)));
if (NetworkManager.instance.hasAgentConnection(node) if (NetworkManager.instance.hasAgentConnection(node)
&& MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID) && MasterServerRecoverMechAction.recoverStatus.get(node)
== RecoverFlag.Fine) { .get(contractID) == RecoverFlag.Fine) {
validNode++; validNode++;
} }
} }
@ -136,9 +127,11 @@ public class RequestAllExecutor implements ContractExecutor {
} }
@Override @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)); 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()) { if (meta == null || !meta.isMaster()) {
CMActions.manager.executeContractOnOtherNodes(req, rc); CMActions.manager.executeContractOnOtherNodes(req, rc);
return; return;
@ -150,9 +143,9 @@ public class RequestAllExecutor implements ContractExecutor {
// 这里是从MasterServer->MasterClient请求的是"executeContractLocally" // 这里是从MasterServer->MasterClient请求的是"executeContractLocally"
// 如果是多点合约的请求A1A2A3的序号应该一致不能分配一个新的seq根据requestID判断是否不需要重新分配一个序号 // 如果是多点合约的请求A1A2A3的序号应该一致不能分配一个新的seq根据requestID判断是否不需要重新分配一个序号
//TODO seqMap memory leak // TODO seqMap memory leak
//TODO // TODO
//TODO // TODO
if (null != requestID && requestID.endsWith("_mul")) { if (null != requestID && requestID.endsWith("_mul")) {
synchronized (lock) { synchronized (lock) {
if (seqMap.containsKey(requestID)) { if (seqMap.containsKey(requestID)) {
@ -180,37 +173,32 @@ public class RequestAllExecutor implements ContractExecutor {
} else { } else {
LOGGER.debug("invalidNodeNumOnResult"); LOGGER.debug("invalidNodeNumOnResult");
request_index.getAndDecrement(); request_index.getAndDecrement();
ContractResult finalResult = ContractResult finalResult = new ContractResult(ContractResult.Status.Error,
new ContractResult( new JsonPrimitive("node number unavailable, request refused."));
ContractResult.Status.Error,
new JsonPrimitive("node number unavailable, request refused."));
rc.onResult(JsonUtil.toJson(finalResult)); rc.onResult(JsonUtil.toJson(finalResult));
} }
// } // }
/* // 三个相同requestID进来的时候会有冲突 /*
// 仅在此处有冲突么 * // 三个相同requestID进来的时候会有冲突 // 仅在此处有冲突么 //
// 这里是从MasterServer->MasterClient请求的是"executeContractLocally" * 这里是从MasterServer->MasterClient请求的是"executeContractLocally" req.seq =
req.seq = request_index.getAndIncrement(); * request_index.getAndIncrement(); req.needSeq = true; ResultCallback collector =
req.needSeq = true; * createResultCallback(id, rc, resultCount, req.getContractID());
ResultCallback collector = createResultCallback(id, rc, resultCount, req.getContractID()); * MasterServerTCPAction.sync.sleep(id, collector); sendRequest(id, req, collector);
MasterServerTCPAction.sync.sleep(id, collector); */
sendRequest(id, req, collector);*/
} }
// 清理缓存的多点合约请求序号 // 清理缓存的多点合约请求序号
public void clearCache() { public void clearCache() {
final long time = System.currentTimeMillis() - 30000L; final long time = System.currentTimeMillis() - 30000L;
seqMap.entrySet() seqMap.entrySet().removeIf(entry -> {
.removeIf( MultiReqSeq cache = entry.getValue();
entry -> { if (null == cache) {
MultiReqSeq cache = entry.getValue(); return true;
if (null == cache) { }
return true; return cache.startTime < time;
} });
return cache.startTime < time;
});
} }
public static class ResultMerger extends ResultCallback { public static class ResultMerger extends ResultCallback {
@ -222,10 +210,7 @@ public class RequestAllExecutor implements ContractExecutor {
ResultCallback originalCallback; ResultCallback originalCallback;
Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点 Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
ResultMerger( ResultMerger(final ResultCallback originalCb, final int count, final int request_seq,
final ResultCallback originalCb,
final int count,
final int request_seq,
final String contractID) { final String contractID) {
originalCallback = originalCb; originalCallback = originalCb;
this.count = count; this.count = count;
@ -240,16 +225,8 @@ public class RequestAllExecutor implements ContractExecutor {
} }
public String getInfo() { public String getInfo() {
return "contractID=" return "contractID=" + contractID + " 收到第 " + order + " 个节点回复 : " + " order=" + order
+ contractID + " count=" + count + " ";
+ " 收到第 "
+ order
+ " 个节点回复 : "
+ " order="
+ order
+ " count="
+ count
+ " ";
} }
@Override @Override
@ -263,19 +240,15 @@ public class RequestAllExecutor implements ContractExecutor {
if (obj.has("nodeID")) { if (obj.has("nodeID")) {
String id = obj.get("nodeID").getAsString(); String id = obj.get("nodeID").getAsString();
if (nodeIDs.contains(id)) { if (nodeIDs.contains(id)) {
LOGGER.debug( LOGGER.debug("ignored result because the result of node "
"ignored result because the result of node " + id.substring(0, 5) + " has been received");
+ id.substring(0, 5)
+ " has been received");
return; return;
} }
nodeIDs.add(id); nodeIDs.add(id);
} }
LOGGER.debug( LOGGER.debug(String.format("contractID=%s received=%s order=%d count=%d",
String.format( contractID, str, order.get(), count));
"contractID=%s received=%s order=%d count=%d",
contractID, str, order.get(), count));
componedContractResult.add(obj); componedContractResult.add(obj);
// 收集到所有结果 // 收集到所有结果
if (order.incrementAndGet() == count) { if (order.incrementAndGet() == count) {
@ -283,26 +256,23 @@ public class RequestAllExecutor implements ContractExecutor {
finalResult.needSeq = true; finalResult.needSeq = true;
finalResult.seq = request_seq; finalResult.seq = request_seq;
// if (null == finalResult) { // if (null == finalResult) {
// finalResult = // finalResult =
// new ContractResult( // new ContractResult(
// ContractResult.Status.Exception, // ContractResult.Status.Exception,
// new JsonPrimitive( // new JsonPrimitive(
// "no nore than half of the // "no nore than half of the
// consistent result")); // consistent result"));
// originalCallback.onResult(new // originalCallback.onResult(new
// Gson().toJson(finalResult)); // Gson().toJson(finalResult));
// } else { // } else {
originalCallback.onResult(JsonUtil.toJson(finalResult)); originalCallback.onResult(JsonUtil.toJson(finalResult));
// } // }
LOGGER.debug( LOGGER.debug(String.format("%d results are the same: %s", finalResult.size,
String.format( finalResult.result));
"%d results are the same: %s",
finalResult.size, finalResult.result));
// 集群中事务序号+1 // 集群中事务序号+1
CMActions.manager.multiContractRecorder CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
.getMultiContractMeta(contractID)
.nextSeqAtMaster(); .nextSeqAtMaster();
// recover其中无状态合约CP出错无需恢复 // recover其中无状态合约CP出错无需恢复
@ -312,26 +282,25 @@ public class RequestAllExecutor implements ContractExecutor {
} }
for (String nodeID : nodesID) { for (String nodeID : nodesID) {
LOGGER.warn("node fails! " + nodeID); LOGGER.warn("node fails! " + nodeID);
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID) if (MasterServerRecoverMechAction.recoverStatus.get(nodeID)
== RecoverFlag.Fine) { .get(contractID) == RecoverFlag.Fine) {
MasterServerRecoverMechAction.recoverStatus MasterServerRecoverMechAction.recoverStatus.get(nodeID).put(contractID,
.get(nodeID) RecoverFlag.ToRecover);
.put(contractID, RecoverFlag.ToRecover);
} }
} }
for (String nodeID : nodesID) { for (String nodeID : nodesID) {
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID) if (MasterServerRecoverMechAction.recoverStatus.get(nodeID)
== RecoverFlag.ToRecover) { .get(contractID) == RecoverFlag.ToRecover) {
LOGGER.warn("node in recover " + nodeID); LOGGER.warn("node in recover " + nodeID);
// 因为该节点结果有误所以即时是stableMode也认为trans记录不可信 // 因为该节点结果有误所以即时是stableMode也认为trans记录不可信
// 直接通过load别的节点来恢复 // 直接通过load别的节点来恢复
MasterServerRecoverMechAction.restartContractFromCommonMode( MasterServerRecoverMechAction.restartContractFromCommonMode(nodeID,
nodeID, contractID); contractID);
} }
} }
} }
// clearCache(); // clearCache();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
LOGGER.warn("result exception!"); LOGGER.warn("result exception!");

View File

@ -1,400 +1,403 @@
package org.bdware.server.executor.unconsistency; // package org.bdware.server.executor.unconsistency;
//
import com.google.gson.JsonObject; // import com.google.gson.JsonObject;
import com.google.gson.JsonParser; // import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; // import com.google.gson.JsonPrimitive;
import org.apache.logging.log4j.LogManager; // import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; // import org.apache.logging.log4j.Logger;
import org.bdware.sc.ComponedContractResult; // import org.bdware.sc.ComponedContractResult;
import org.bdware.sc.ContractMeta; // import org.bdware.sc.ContractMeta;
import org.bdware.sc.ContractResult; // import org.bdware.sc.ContractResult;
import org.bdware.sc.bean.*; // import org.bdware.sc.bean.*;
import org.bdware.sc.conn.OnHashCallback; // import org.bdware.sc.conn.OnHashCallback;
import org.bdware.sc.conn.ResultCallback; // import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.units.MultiContractMeta; // import org.bdware.sc.units.MultiContractMeta;
import org.bdware.sc.units.RecoverFlag; // import org.bdware.sc.units.RecoverFlag;
import org.bdware.sc.units.RequestCache; // import org.bdware.sc.units.RequestCache;
import org.bdware.sc.units.ResultCache; // import org.bdware.sc.units.ResultCache;
import org.bdware.sc.util.JsonUtil; // import org.bdware.sc.util.JsonUtil;
import org.bdware.server.action.CMActions; // import org.bdware.server.action.CMActions;
import org.bdware.server.action.p2p.MasterServerRecoverMechAction; // import org.bdware.server.action.p2p.MasterServerRecoverMechAction;
import org.bdware.server.action.p2p.MasterServerTCPAction; // import org.bdware.server.action.p2p.MasterServerTCPAction;
import org.bdware.server.trustedmodel.ContractExecutor; // import org.bdware.server.trustedmodel.ContractExecutor;
import org.bdware.server.trustedmodel.MultiReqSeq; // import org.bdware.server.trustedmodel.MultiReqSeq;
import org.bdware.server.trustedmodel.ResultCollector; // import org.bdware.server.trustedmodel.ResultCollector;
import org.bdware.units.NetworkManager; // import org.bdware.units.NetworkManager;
//
import java.math.BigInteger; // import java.math.BigInteger;
import java.util.HashMap; // import java.util.HashMap;
import java.util.HashSet; // import java.util.HashSet;
import java.util.Map; // import java.util.Map;
import java.util.Set; // import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; // import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; // import java.util.concurrent.atomic.AtomicInteger;
//
// 改为MultiPointCooperationExecutor //// 改为MultiPointCooperationExecutor
public class MultiPointCooperationExecutor implements ContractExecutor { // public class MultiPointCooperationExecutor implements ContractExecutor {
private static final Logger LOGGER = LogManager.getLogger(MultiPointCooperationExecutor.class); // private static final Logger LOGGER = LogManager.getLogger(MultiPointCooperationExecutor.class);
final Object lock = new Object(); // final Object lock = new Object();
int resultCount; // int resultCount;
AtomicInteger request_index = new AtomicInteger(0); // AtomicInteger request_index = new AtomicInteger(0);
ContractExecType type; // ContractExecType type;
// key为requestIDvalue为其seq // // key为requestIDvalue为其seq
Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>(); // Map<String, MultiReqSeq> seqMap = new ConcurrentHashMap<>();
Map<String, ResultCache> resultCache = new ConcurrentHashMap<>(); // Map<String, ResultCache> resultCache = new ConcurrentHashMap<>();
// MultiPointContractInfo info; // // MultiPointContractInfo info;
MultiContractMeta multiMeta; // MultiContractMeta multiMeta;
String contractID; // String contractID;
//
public MultiPointCooperationExecutor(ContractExecType t, int c, String con_id) { // public MultiPointCooperationExecutor(ContractExecType t, int c, String con_id) {
LOGGER.info("-- sharding executor---"); // LOGGER.info("-- sharding executor---");
type = t; // type = t;
resultCount = c; // resultCount = c;
contractID = con_id; // contractID = con_id;
multiMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID); // multiMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID);
} // }
//
public void setSeq(int seq) { // public void setSeq(int seq) {
request_index = new AtomicInteger(seq); // request_index = new AtomicInteger(seq);
} // }
//
public ResultCallback createResultCallback( // public ResultCallback createResultCallback(
final String requestID, // final String requestID,
final ResultCallback originalCb, // final ResultCallback originalCb,
final int count, // final int count,
final int request_seq, // final int request_seq,
final String contractID, JoinInfo joinInfo) { // final String contractID, JoinInfo joinInfo) {
// TODO 加对应的超时 // // TODO 加对应的超时
return new ResultCollector( // return new ResultCollector(
requestID, // requestID,
new MultiPointCooperationExecutor.ResultMerger(originalCb, count, request_seq, contractID, joinInfo), // new MultiPointCooperationExecutor.ResultMerger(originalCb, count, request_seq, contractID,
count); // 把count改成了1设置成获得1个响应就行 // joinInfo),
} // count); // 把count改成了1设置成获得1个响应就行
// }
public void sendRequest(String id, ContractRequest req, String[] nodes) { //
Map<String, Object> reqStr = new HashMap<>(); // public void sendRequest(String id, ContractRequest req, String[] nodes) {
reqStr.put("uniReqID", id); // Map<String, Object> reqStr = new HashMap<>();
reqStr.put("data", req); // reqStr.put("uniReqID", id);
req.needSeq = false; // reqStr.put("data", req);
reqStr.put("action", "executeContractLocally"); // req.needSeq = false;
String sendStr = JsonUtil.toJson(reqStr); // reqStr.put("action", "executeContractLocally");
// master负责缓存请求 // String sendStr = JsonUtil.toJson(reqStr);
if (!MasterServerTCPAction.requestCache.containsKey(contractID)) { // // master负责缓存请求
MasterServerTCPAction.requestCache.put(contractID, new RequestCache()); // if (!MasterServerTCPAction.requestCache.containsKey(contractID)) {
} // MasterServerTCPAction.requestCache.put(contractID, new RequestCache());
// TODO 多调多统一个seq的有多个请求这个需要改 // }
MasterServerTCPAction.requestCache.get(contractID).put(req.seq, sendStr); // // TODO 多调多统一个seq的有多个请求这个需要改
LOGGER.debug(JsonUtil.toJson(req)); // MasterServerTCPAction.requestCache.get(contractID).put(req.seq, sendStr);
LOGGER.info("node size = " + nodes.length); // LOGGER.debug(JsonUtil.toJson(req));
LOGGER.debug("nodes:" + JsonUtil.toJson(nodes)); // LOGGER.info("node size = " + nodes.length);
for (String node : nodes) { // LOGGER.debug("nodes:" + JsonUtil.toJson(nodes));
LOGGER.info( // for (String node : nodes) {
"[sendRequests] get cmNode " // LOGGER.info(
+ node.substring(0, 5) // "[sendRequests] get cmNode "
+ " not null " // + node.substring(0, 5)
+ "RequestAllExecutor 发送请求给 " // + " not null "
+ node.substring(0, 5)); // + "RequestAllExecutor 发送请求给 "
NetworkManager.instance.sendToAgent(node, sendStr); // + node.substring(0, 5));
} // NetworkManager.instance.sendToAgent(node, sendStr);
} // }
// }
private String[] getAccordingToRouteInfo(RouteInfo routeInfo, ContractRequest req, String[] members) { //
try { // private String[] getAccordingToRouteInfo(RouteInfo routeInfo, ContractRequest req, String[]
int val; // members) {
switch (routeInfo.useDefault) { // try {
case byRequester: // int val;
val = // switch (routeInfo.useDefault) {
new BigInteger(req.getRequester(), 16) // case byRequester:
.mod(new BigInteger("" + members.length)) // val =
.intValue(); // new BigInteger(req.getRequester(), 16)
while (val < 0) { // .mod(new BigInteger("" + members.length))
val = val + members.length; // .intValue();
} // while (val < 0) {
return new String[]{members[val]}; // val = val + members.length;
case byArgHash: // }
val = req.getArg().hashCode(); // return new String[]{members[val]};
val = val % members.length; // case byArgHash:
while (val < 0) { // val = req.getArg().hashCode();
val += members.length; // val = val % members.length;
} // while (val < 0) {
return new String[]{members[val]}; // val += members.length;
case byTarget: // }
JsonObject jo = req.getArg().getAsJsonObject(); // return new String[]{members[val]};
val = // case byTarget:
new BigInteger(jo.get("target").getAsString(), 16) // JsonObject jo = req.getArg().getAsJsonObject();
.mod(new BigInteger("" + members.length)) // val =
.intValue(); // new BigInteger(jo.get("target").getAsString(), 16)
while (val < 0) { // .mod(new BigInteger("" + members.length))
val = val + members.length; // .intValue();
} // while (val < 0) {
return new String[]{members[val]}; // val = val + members.length;
default: // }
return members; // return new String[]{members[val]};
} // default:
} catch (Exception e) { // return members;
return members; // }
} // } catch (Exception e) {
} // return members;
// }
public boolean checkCurNodeNumValid() { // }
LOGGER.info("checkCurNodeNumValid"); //
String[] nodes = multiMeta.getMembers(); // public boolean checkCurNodeNumValid() {
// List<String> nodes = info.members; // LOGGER.info("checkCurNodeNumValid");
int validNode = 0; // String[] nodes = multiMeta.getMembers();
for (String node : nodes) { // // List<String> nodes = info.members;
if (NetworkManager.instance.hasAgentConnection(node) // int validNode = 0;
&& MasterServerRecoverMechAction.recoverStatus.get(node).get(contractID) // for (String node : nodes) {
== RecoverFlag.Fine) { // if (NetworkManager.instance.hasAgentConnection(node)
validNode++; // && 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); // int c = resultCount;
return validNode >= c; // 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)); // @Override
// 获得action 函数名 // public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
LOGGER.info("action is : " + req.getAction()); // {
req.setContractID(CMActions.manager.getContractIDByName(req.getContractID())); // LOGGER.info("[MultiPointCooperationExecutor] execute " + JsonUtil.toJson(req));
if (requestID != null && requestID.endsWith("_mul")) { // // 获得action 函数名
synchronized (lock) { // LOGGER.info("action is : " + req.getAction());
if (seqMap.containsKey(requestID)) { // req.setContractID(CMActions.manager.getContractIDByName(req.getContractID()));
req.seq = seqMap.get(requestID).seq; // if (requestID != null && requestID.endsWith("_mul")) {
} else { // synchronized (lock) {
req.seq = request_index.getAndIncrement(); // if (seqMap.containsKey(requestID)) {
seqMap.put(requestID, new MultiReqSeq(req.seq)); // req.seq = seqMap.get(requestID).seq;
} // } else {
} // req.seq = request_index.getAndIncrement();
} else { // seqMap.put(requestID, new MultiReqSeq(req.seq));
req.seq = request_index.getAndIncrement(); // }
} // }
req.needSeq = true; // } else {
String id = // req.seq = request_index.getAndIncrement();
System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq; // }
LOGGER.info("execute receive requestID= " + requestID + " msgID=" + id); // req.needSeq = true;
if (checkCurNodeNumValid()) { // 校验成功 current node num 合法 // String id =
LOGGER.info("checkCurNodeNumValid true"); // System.currentTimeMillis() + "_" + (int) (Math.random() * 1000000) + "_" + req.seq;
ContractMeta meta = // LOGGER.info("execute receive requestID= " + requestID + " msgID=" + id);
CMActions.manager.statusRecorder.getContractMeta(req.getContractID()); // if (checkCurNodeNumValid()) { // 校验成功 current node num 合法
FunctionDesp fun = meta.getExportedFunction(req.getAction()); // LOGGER.info("checkCurNodeNumValid true");
ResultCallback collector; // ContractMeta meta =
// TODO @fanbo 下面的count 1要改应该是根据route的规则来 // CMActions.manager.statusRecorder.getContractMeta(req.getContractID());
//Count 根据join规则来 // FunctionDesp fun = meta.getExportedFunction(req.getAction());
//nodes 根据route规则来 // ResultCallback collector;
JoinInfo joinInfo = fun.joinInfo; // // TODO @fanbo 下面的count 1要改应该是根据route的规则来
RouteInfo routeInfo = fun.routeInfo; // //Count 根据join规则来
int count = getJoinCount(joinInfo, contractID); // //nodes 根据route规则来
LOGGER.info("requestID=" + requestID + " join Count: " + count); // JoinInfo joinInfo = fun.joinInfo;
// RouteInfo routeInfo = fun.routeInfo;
String[] members = multiMeta.getMembers(); // int count = getJoinCount(joinInfo, contractID);
String[] nodes = getAccordingToRouteInfo(routeInfo, req, members); // LOGGER.info("requestID=" + requestID + " join Count: " + count);
if (nodes.length < count) { //
count = nodes.length; // String[] members = multiMeta.getMembers();
} // String[] nodes = getAccordingToRouteInfo(routeInfo, req, members);
collector = // if (nodes.length < count) {
createResultCallback(id, rc, count, req.seq, req.getContractID(), joinInfo); // 初始化结果收集器 // count = nodes.length;
MasterServerTCPAction.sync.sleep(id, collector); // }
LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq); // collector =
sendRequest(id, req, nodes); // 发送请求 // createResultCallback(id, rc, count, req.seq, req.getContractID(), joinInfo); // 初始化结果收集器
} else { // MasterServerTCPAction.sync.sleep(id, collector);
LOGGER.info("invalidNodeNumOnResult"); // LOGGER.info("requestID=" + requestID + " master broadcasts request " + req.seq);
request_index.getAndDecrement(); // sendRequest(id, req, nodes); // 发送请求
ContractResult finalResult = // } else {
new ContractResult( // LOGGER.info("invalidNodeNumOnResult");
ContractResult.Status.Error, // request_index.getAndDecrement();
new JsonPrimitive("node number unavailbale,request refused.")); // ContractResult finalResult =
rc.onResult(JsonUtil.toJson(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(); // private int getJoinCount(JoinInfo joinInfo, String contractID) {
} // if (joinInfo == null) return resultCount;
try { // if (joinInfo.joinCount.isJsonPrimitive() && joinInfo.joinCount.getAsJsonPrimitive().isNumber()) {
ContractRequest cr = new ContractRequest(); // return joinInfo.joinCount.getAsJsonPrimitive().getAsInt();
cr.setContractID(contractID); // }
cr.setAction(joinInfo.joinCount.getAsString()); // try {
//TODO Arg需要好好设计一下 // ContractRequest cr = new ContractRequest();
//TODO 又好用又简单的那种设计 // cr.setContractID(contractID);
//TODO // cr.setAction(joinInfo.joinCount.getAsString());
cr.setArg(""); // //TODO Arg需要好好设计一下
String result = CMActions.manager.executeLocally(cr, null); // //TODO 又好用又简单的那种设计
return JsonUtil.parseString(result).getAsJsonObject().get("result").getAsInt(); // //TODO
} catch (Exception e) { // cr.setArg("");
e.printStackTrace(); // String result = CMActions.manager.executeLocally(cr, null);
return 1; // 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( // public void clearCache() {
entry -> { // final long time = System.currentTimeMillis() - 30000L;
MultiReqSeq cache = entry.getValue(); // seqMap.entrySet()
if (null == cache) { // .removeIf(
return true; // entry -> {
} // MultiReqSeq cache = entry.getValue();
return cache.startTime < time; // if (null == cache) {
}); // return true;
} // }
// return cache.startTime < time;
public static class ResultMerger extends ResultCallback { // });
ComponedContractResult componedContractResult; // }
AtomicInteger order; //
String contractID; // public static class ResultMerger extends ResultCallback {
int count; // 记录有多少个节点 // ComponedContractResult componedContractResult;
int request_seq; // AtomicInteger order;
ResultCallback originalCallback; // String contractID;
Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点 // int count; // 记录有多少个节点
JoinInfo joinInfo; // int request_seq;
// ResultCallback originalCallback;
ResultMerger( // Set<String> nodeIDs = new HashSet<>(); // 已收到返回结果的节点
final ResultCallback originalCb, // JoinInfo joinInfo;
final int count, //
final int request_seq, // ResultMerger(
final String contractID, // final ResultCallback originalCb,
final JoinInfo joinInfo) { // final int count,
originalCallback = originalCb; // final int request_seq,
this.count = count; // final String contractID,
this.request_seq = request_seq; // final JoinInfo joinInfo) {
this.contractID = contractID; // originalCallback = originalCb;
componedContractResult = new ComponedContractResult(count); // this.count = count;
order = new AtomicInteger(0); // this.request_seq = request_seq;
this.joinInfo = joinInfo; // this.contractID = contractID;
} // componedContractResult = new ComponedContractResult(count);
// order = new AtomicInteger(0);
public String getInfo() { // this.joinInfo = joinInfo;
return "contractID=" // }
+ contractID //
+ " 收到第 " // public String getInfo() {
+ order // return "contractID="
+ " 个节点回复 : " // + contractID
+ " order=" // + " 收到第 "
+ order // + order
+ " count=" // + " 个节点回复 : "
+ count // + " order="
+ " "; // + order
} // + " count="
// + count
@Override // + " ";
public void onResult(String str) { // }
// TODO 必须在这里聚合 //
// str的data是个ContractResult // @Override
// 在这儿也是返回个ContractResult // public void onResult(String str) {
try { // // TODO 必须在这里聚合
LOGGER.info(str); // // str的data是个ContractResult
JsonObject obj = JsonParser.parseString(str).getAsJsonObject(); // // 在这儿也是返回个ContractResult
String id = obj.get("nodeID").getAsString(); // try {
if (nodeIDs.contains(id)) { // LOGGER.info(str);
LOGGER.info("已经收到节点 " + id.substring(0, 5) + " 的结果,该结果被忽略"); // JsonObject obj = JsonParser.parseString(str).getAsJsonObject();
return; // String id = obj.get("nodeID").getAsString();
} // if (nodeIDs.contains(id)) {
nodeIDs.add(id); // LOGGER.info("已经收到节点 " + id.substring(0, 5) + " 的结果,该结果被忽略");
LOGGER.info( // return;
"contractID=" // }
+ contractID // nodeIDs.add(id);
+ " 收到第 " // LOGGER.info(
+ order // "contractID="
+ " 个节点回复 : " // + contractID
+ str // + " 收到第 "
+ " order=" // + order
+ order // + " 个节点回复 : "
+ " count=" // + str
+ count); // + " order="
componedContractResult.add(obj); // + order
// 收集到所有结果 // + " count="
if (order.incrementAndGet() == count) { // + count);
ContractResult finalResult = componedContractResult.mergeFinalResult(); // componedContractResult.add(obj);
// // 收集到所有结果
finalResult.needSeq = true; // if (order.incrementAndGet() == count) {
finalResult.seq = request_seq; // ContractResult finalResult = componedContractResult.mergeFinalResult();
//
// if (null == finalResult) { // finalResult.needSeq = true;
// finalResult = // finalResult.seq = request_seq;
// new ContractResult( //
// ContractResult.Status.Exception, // // if (null == finalResult) {
// new JsonPrimitive( // // finalResult =
// "no nore than half of the // // new ContractResult(
// consistent result")); // // ContractResult.Status.Exception,
// originalCallback.onResult(new // // new JsonPrimitive(
// Gson().toJson(finalResult)); // // "no nore than half of the
// } else { // // consistent result"));
if (joinInfo != null) { // // originalCallback.onResult(new
handleJoinInfo(finalResult, joinInfo); // // Gson().toJson(finalResult));
} // // } else {
originalCallback.onResult(JsonUtil.toJson(finalResult)); // if (joinInfo != null) {
// } // handleJoinInfo(finalResult, joinInfo);
LOGGER.info( // }
"本次执行最终结果为 " + finalResult.size + "个节点合并的,结果为 " + finalResult.result); // originalCallback.onResult(JsonUtil.toJson(finalResult));
// // }
// 集群中事务序号+1 // LOGGER.info(
// MasterServerTCPAction.contractID2Members.get(contractID).nextSeq(); // "本次执行最终结果为 " + finalResult.size + "个节点合并的,结果为 " + finalResult.result);
CMActions.manager //
.multiContractRecorder // // 集群中事务序号+1
.getMultiContractMeta(contractID) // // MasterServerTCPAction.contractID2Members.get(contractID).nextSeq();
.nextSeqAtMaster(); // CMActions.manager
// recover其中无状态合约CP出错无需恢复 // .multiContractRecorder
Set<String> nodesID = componedContractResult.getProblemNodes(); // .getMultiContractMeta(contractID)
if (null == nodesID || nodesID.isEmpty()) { // .nextSeqAtMaster();
return; // // recover其中无状态合约CP出错无需恢复
} // Set<String> nodesID = componedContractResult.getProblemNodes();
for (String nodeID : nodesID) { // if (null == nodesID || nodesID.isEmpty()) {
LOGGER.info("结果出现问题的节点有:" + nodeID); // return;
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID) // }
== RecoverFlag.Fine) { // for (String nodeID : nodesID) {
MasterServerRecoverMechAction.recoverStatus // LOGGER.info("结果出现问题的节点有:" + nodeID);
.get(nodeID) // if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
.put(contractID, RecoverFlag.ToRecover); // == RecoverFlag.Fine) {
} // MasterServerRecoverMechAction.recoverStatus
} // .get(nodeID)
for (String nodeID : nodesID) { // .put(contractID, RecoverFlag.ToRecover);
if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID) // }
== RecoverFlag.ToRecover) { // }
LOGGER.info("问题节点开始恢复:" + nodeID); // for (String nodeID : nodesID) {
// if (MasterServerRecoverMechAction.recoverStatus.get(nodeID).get(contractID)
// 因为该节点结果有误所以即时是stableMode也认为trans记录不可信 // == RecoverFlag.ToRecover) {
// 直接通过load别的节点来恢复 // LOGGER.info("问题节点开始恢复:" + nodeID);
MasterServerRecoverMechAction.restartContractFromCommonMode( //
nodeID, contractID); // // 因为该节点结果有误所以即时是stableMode也认为trans记录不可信
} // // 直接通过load别的节点来恢复
} // MasterServerRecoverMechAction.restartContractFromCommonMode(
} // nodeID, contractID);
// clearCache(); // }
} catch (Exception e) { // }
e.printStackTrace(); // }
LOGGER.info("本次执行最终结果为有异常"); // // 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 类型 // private void handleJoinInfo(ContractResult finalResult, JoinInfo joinInfo) {
switch (joinInfo.joinRule) { // JsonObject jo = finalResult.result.getAsJsonObject();
case "add": // if (joinInfo != null && joinInfo.joinRule != null) {
double val = 0; // //TODO 不应该是double 类型
for (String key : jo.keySet()) { // switch (joinInfo.joinRule) {
val += jo.get(key).getAsDouble(); // case "add":
} // double val = 0;
finalResult.result = new JsonPrimitive(val); // for (String key : jo.keySet()) {
break; // val += jo.get(key).getAsDouble();
case "multiply": // }
val = 1; // finalResult.result = new JsonPrimitive(val);
for (String key : jo.keySet()) { // break;
val *= jo.get(key).getAsDouble(); // case "multiply":
} // val = 1;
finalResult.result = new JsonPrimitive(val); // for (String key : jo.keySet()) {
break; // val *= jo.get(key).getAsDouble();
} // }
} // finalResult.result = new JsonPrimitive(val);
} // break;
} // }
} // }
// }
// }
// }

View File

@ -1,68 +1,70 @@
package org.bdware.server.executor.unconsistency; // package org.bdware.server.executor.unconsistency;
//
import com.google.gson.JsonObject; // import com.google.gson.JsonObject;
import com.google.gson.JsonParser; // import com.google.gson.JsonParser;
import org.apache.logging.log4j.LogManager; // import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; // import org.apache.logging.log4j.Logger;
import org.bdware.sc.bean.ContractRequest; // import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.conn.OnHashCallback; // import org.bdware.sc.conn.OnHashCallback;
import org.bdware.sc.conn.ResultCallback; // import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.util.JsonUtil; // import org.bdware.sc.util.JsonUtil;
import org.bdware.server.ControllerManager; // import org.bdware.server.ControllerManager;
import org.bdware.server.action.CMActions; // import org.bdware.server.action.CMActions;
import org.bdware.server.action.p2p.MasterServerTCPAction; // import org.bdware.server.action.p2p.MasterServerTCPAction;
import org.bdware.server.trustedmodel.ContractExecutor; // import org.bdware.server.trustedmodel.ContractExecutor;
import org.bdware.server.trustedmodel.AgentManager; // import org.bdware.server.trustedmodel.AgentManager;
import org.bdware.units.NetworkManager; // import org.bdware.units.NetworkManager;
//
import java.util.HashMap; // import java.util.HashMap;
import java.util.Map; // import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; // import java.util.concurrent.atomic.AtomicInteger;
//
public class RequestOnceExecutor implements ContractExecutor { // public class RequestOnceExecutor implements ContractExecutor {
private static final Logger LOGGER = LogManager.getLogger(RequestOnceExecutor.class); // private static final Logger LOGGER = LogManager.getLogger(RequestOnceExecutor.class);
String contractID; // String contractID;
AtomicInteger order = new AtomicInteger(0); // AtomicInteger order = new AtomicInteger(0);
//
public RequestOnceExecutor(String contractID) { // public RequestOnceExecutor(String contractID) {
this.contractID = contractID; // this.contractID = contractID;
} // }
//
@Override // @Override
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) { // public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
ResultCallback cb = // {
new ResultCallback() { // ResultCallback cb =
@Override // new ResultCallback() {
public void onResult(String str) { // @Override
LOGGER.debug(str); // public void onResult(String str) {
JsonObject jo = JsonParser.parseString(str).getAsJsonObject(); // LOGGER.debug(str);
JsonObject result = // JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
JsonParser.parseString(jo.get("data").getAsString()) // JsonObject result =
.getAsJsonObject(); // JsonParser.parseString(jo.get("data").getAsString())
for (String key : result.keySet()) jo.add(key, result.get(key)); // .getAsJsonObject();
jo.remove("action"); // for (String key : result.keySet()) jo.add(key, result.get(key));
jo.addProperty("action", "onExecuteResult"); // jo.remove("action");
LOGGER.debug(jo.toString()); // jo.addProperty("action", "onExecuteResult");
rc.onResult(jo.toString()); // LOGGER.debug(jo.toString());
} // rc.onResult(jo.toString());
}; // }
MasterServerTCPAction.sync.sleep(requestID, cb); // };
String[] members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers(); // MasterServerTCPAction.sync.sleep(requestID, cb);
for (int i = 0; i < members.length; i++) { // String[] members =
LOGGER.info("[members]:" + members.length); // CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
int size = members.length; // for (int i = 0; i < members.length; i++) {
String nodeID = members[order.incrementAndGet() % size]; // LOGGER.info("[members]:" + members.length);
//ADD Connect // int size = members.length;
Map<String, Object> obj = new HashMap<>(); // String nodeID = members[order.incrementAndGet() % size];
obj.put("action", "executeContractLocally"); // //ADD Connect
obj.put("requestID",requestID); // Map<String, Object> obj = new HashMap<>();
obj.put("data", req); // obj.put("action", "executeContractLocally");
obj.put("uniReqID", requestID); // obj.put("requestID",requestID);
NetworkManager.instance.sendToAgent(nodeID,JsonUtil.toJson(obj)); // obj.put("data", req);
return; // obj.put("uniReqID", requestID);
} // NetworkManager.instance.sendToAgent(nodeID,JsonUtil.toJson(obj));
rc.onResult( // return;
"{\"status\":\"Error\",\"result\":\"all nodes " // }
+ " offline\",\"action\":\"onExecuteContract\"}"); // rc.onResult(
} // "{\"status\":\"Error\",\"result\":\"all nodes "
} // + " offline\",\"action\":\"onExecuteContract\"}");
// }
// }

View File

@ -1,83 +1,86 @@
package org.bdware.server.executor.unconsistency; // package org.bdware.server.executor.unconsistency;
//
import com.google.gson.JsonObject; // import com.google.gson.JsonObject;
import com.google.gson.JsonParser; // import com.google.gson.JsonParser;
import org.apache.logging.log4j.LogManager; // import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; // import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractResult; // import org.bdware.sc.ContractResult;
import org.bdware.sc.bean.ContractRequest; // import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.conn.OnHashCallback; // import org.bdware.sc.conn.OnHashCallback;
import org.bdware.sc.conn.ResultCallback; // import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.util.JsonUtil; // import org.bdware.sc.util.JsonUtil;
import org.bdware.server.action.CMActions; // import org.bdware.server.action.CMActions;
import org.bdware.server.action.p2p.MasterServerTCPAction; // import org.bdware.server.action.p2p.MasterServerTCPAction;
import org.bdware.server.trustedmodel.ContractExecutor; // import org.bdware.server.trustedmodel.ContractExecutor;
import org.bdware.units.NetworkManager; // import org.bdware.units.NetworkManager;
//
import java.util.HashMap; // import java.util.HashMap;
import java.util.Map; // import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; // import java.util.concurrent.atomic.AtomicInteger;
//
public class ResponseOnceExecutor implements ContractExecutor { // public class ResponseOnceExecutor implements ContractExecutor {
private static final Logger LOGGER = LogManager.getLogger(ResponseOnceExecutor.class); // private static final Logger LOGGER = LogManager.getLogger(ResponseOnceExecutor.class);
private final String contractID; // private final String contractID;
AtomicInteger order = new AtomicInteger(0); // AtomicInteger order = new AtomicInteger(0);
//
public ResponseOnceExecutor(String contractID) { // public ResponseOnceExecutor(String contractID) {
this.contractID = contractID; // this.contractID = contractID;
} // }
//
@Override // @Override
public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb) { // public void execute(String requestID, ContractRequest req, ResultCallback rc, OnHashCallback hcb)
executeInternal(requestID, rc, req, 2); // {
} // executeInternal(requestID, rc, req, 2);
// }
private void executeInternal( //
String requestID, ResultCallback rc, ContractRequest req, int count) { // private void executeInternal(
// String contractID = req.getContractID(); // String requestID, ResultCallback rc, ContractRequest req, int count) {
// TODO 标注失效节点是否选择重新迁移 // // String contractID = req.getContractID();
ResultCallback cb = // // TODO 标注失效节点是否选择重新迁移
new ResultCallback() { // ResultCallback cb =
@Override // new ResultCallback() {
public void onResult(String str) { // @Override
LOGGER.debug(str); // public void onResult(String str) {
JsonObject jo = JsonParser.parseString(str).getAsJsonObject(); // LOGGER.debug(str);
jo.remove("action"); // JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
jo.addProperty("action", "onExecuteResult"); // jo.remove("action");
LOGGER.debug(jo.toString()); // jo.addProperty("action", "onExecuteResult");
if (jo.has("data")) { // LOGGER.debug(jo.toString());
String data = jo.get("data").getAsString(); // if (jo.has("data")) {
ContractResult cr = JsonUtil.fromJson(data, ContractResult.class); // String data = jo.get("data").getAsString();
if (cr.status != ContractResult.Status.Success && count > 0) { // ContractResult cr = JsonUtil.fromJson(data, ContractResult.class);
executeInternal(requestID, rc, req, count - 1); // if (cr.status != ContractResult.Status.Success && count > 0) {
} else rc.onResult(jo.toString()); // executeInternal(requestID, rc, req, count - 1);
} else { // } else rc.onResult(jo.toString());
JsonObject jo2 = new JsonObject(); // } else {
jo2.addProperty("action", "onExecuteResult"); // JsonObject jo2 = new JsonObject();
jo.remove("action"); // jo2.addProperty("action", "onExecuteResult");
jo2.addProperty("data", jo.toString()); // jo.remove("action");
rc.onResult(jo2.toString()); // jo2.addProperty("data", jo.toString());
} // rc.onResult(jo2.toString());
} // }
}; // }
MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5); // };
if (!sendOnce(requestID, req)) // MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5);
rc.onResult( // if (!sendOnce(requestID, req))
"{\"status\":\"Error\",\"data\":\"{\\\"status\\\":\\\"Error\\\",\\\"result\\\":\\\"all nodes offline\\\"}\",\"action\":\"onExecuteContract\"}"); // 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++) { // private boolean sendOnce(String requestID, ContractRequest req) {
int size = members.length; // String[] members =
String nodeID = members[order.incrementAndGet() % size]; // CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers();
Map<String, Object> obj = new HashMap<>(); // for (int i = 0; i < members.length; i++) {
obj.put("action", "executeContractLocally"); // int size = members.length;
obj.put("data", req); // String nodeID = members[order.incrementAndGet() % size];
obj.put("uniReqID", requestID); // Map<String, Object> obj = new HashMap<>();
NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(obj)); // obj.put("action", "executeContractLocally");
return true; // obj.put("data", req);
} // obj.put("uniReqID", requestID);
return false; // NetworkManager.instance.sendToAgent(nodeID, JsonUtil.toJson(obj));
} // return true;
} // }
// return false;
// }
// }

View File

@ -33,30 +33,30 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
} }
@Override @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); executeInternal(requestID, rc, req, 2);
} }
private void executeInternal( private void executeInternal(String requestID, ResultCallback rc, ContractRequest req,
String requestID, ResultCallback rc, ContractRequest req, int count) { int count) {
// String contractID = req.getContractID(); // String contractID = req.getContractID();
// TODO 标注失效节点是否选择重新迁移 // TODO 标注失效节点是否选择重新迁移
ResultCallback cb = ResultCallback cb = new ResultCallback() {
new ResultCallback() { @Override
@Override public void onResult(String str) {
public void onResult(String str) { LOGGER.debug(str);
LOGGER.debug(str); JsonObject jo = JsonParser.parseString(str).getAsJsonObject();
JsonObject jo = JsonParser.parseString(str).getAsJsonObject(); JsonObject result =
JsonObject result = JsonParser.parseString(jo.get("data").getAsString()).getAsJsonObject();
JsonParser.parseString(jo.get("data").getAsString()) for (String key : result.keySet())
.getAsJsonObject(); jo.add(key, result.get(key));
for (String key : result.keySet()) jo.add(key, result.get(key)); jo.remove("action");
jo.remove("action"); jo.addProperty("action", "onExecuteResult");
jo.addProperty("action", "onExecuteResult"); LOGGER.debug(jo.toString());
LOGGER.debug(jo.toString()); rc.onResult(jo.toString());
rc.onResult(jo.toString()); }
} };
};
MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5); MasterServerTCPAction.sync.sleepWithTimeout(requestID, cb, 5);
if (!send(requestID, req)) if (!send(requestID, req))
rc.onResult( rc.onResult(
@ -69,7 +69,8 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
req.getArg(); req.getArg();
req.getRequester(); req.getRequester();
req.getAction(); req.getAction();
MultiContractMeta mMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(meta.getID()); MultiContractMeta mMeta =
CMActions.manager.multiContractRecorder.getMultiContractMeta(meta.getID());
FunctionDesp desp = meta.getExportedFunction(req.getAction()); FunctionDesp desp = meta.getExportedFunction(req.getAction());
RouteInfo info = desp.getRoute(); RouteInfo info = desp.getRoute();
@ -77,10 +78,8 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
if (info != null && info.useDefault != null) { if (info != null && info.useDefault != null) {
switch (info.useDefault) { switch (info.useDefault) {
case byRequester: case byRequester:
hash = hash = new BigInteger(req.getRequester(), 16)
new BigInteger(req.getRequester(), 16) .mod(BigInteger.valueOf(mMeta.getMembers().length)).intValue();
.mod(BigInteger.valueOf(mMeta.getMembers().length))
.intValue();
break; break;
case byArgHash: case byArgHash:
hash = req.getArg().hashCode(); hash = req.getArg().hashCode();
@ -88,13 +87,15 @@ public class _UNUSED_RouteEnabledExecutor implements ContractExecutor {
} }
} }
String[] members = String[] members = CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID)
CMActions.manager.multiContractRecorder.getMultiContractMeta(contractID).getMembers(); .getMembers();
String nodeID; String nodeID;
for (int i = 0; i < members.length; i++) { for (int i = 0; i < members.length; i++) {
int size = members.length; int size = members.length;
if (hash != -1) nodeID = members[hash % size]; if (hash != -1)
else nodeID = members[order.incrementAndGet() % size]; nodeID = members[hash % size];
else
nodeID = members[order.incrementAndGet() % size];
Map<String, Object> obj = new HashMap<>(); Map<String, Object> obj = new HashMap<>();
obj.put("action", "executeContractLocally"); obj.put("action", "executeContractLocally");
obj.put("data", req); obj.put("data", req);

View File

@ -30,7 +30,6 @@ import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import static io.netty.handler.codec.http.HttpResponseStatus.OK; import static io.netty.handler.codec.http.HttpResponseStatus.OK;
@ -45,11 +44,8 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
URIHandler uriHandlers; URIHandler uriHandlers;
public CMHttpHandler() { public CMHttpHandler() {
FileFilter fileFilter = FileFilter fileFilter = pathname -> !pathname.getName().equals("CenterPortal.html")
pathname -> && !pathname.getAbsolutePath().contains(".."); // TODO 路径判断过滤下载任意文件
!pathname.getName().equals("CenterPortal.html")
&& !pathname.getAbsolutePath()
.contains(".."); // TODO 路径判断过滤下载任意文件
HttpFileHandleAdapter ideFileService = new HttpFileHandleAdapter( HttpFileHandleAdapter ideFileService = new HttpFileHandleAdapter(
new File("./WebContent/ide/").getAbsolutePath(), fileFilter) { new File("./WebContent/ide/").getAbsolutePath(), fileFilter) {
@URIPath({"/"}) @URIPath({"/"})
@ -58,68 +54,59 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
channelRead0(ctx, request); channelRead0(ctx, request);
} }
}; };
HttpFileHandleAdapter otherFileService = new HttpFileHandleAdapter( HttpFileHandleAdapter otherFileService =
new File("./WebContent/").getAbsolutePath(), fileFilter) { new HttpFileHandleAdapter(new File("./WebContent/").getAbsolutePath(), fileFilter) {
@URIPath({"/client/", "/doc/"}) @URIPath({"/client/", "/doc/", "/blog/"})
private void handleFile(ChannelHandlerContext ctx, FullHttpRequest request) private void handleFile(ChannelHandlerContext ctx, FullHttpRequest request)
throws Exception { throws Exception {
channelRead0(ctx, request); 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;
} }
}; };
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) { if (val == 0) {
Object obj = createInstanceByClzName(str); flag2 = true;
actionExecutor.appendHandler(obj); } 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(); uriHandlers = new URIHandler();
DOIPOverHttpHandler doipOverHttpHandler = new DOIPOverHttpHandler(); DOIPOverHttpHandler doipOverHttpHandler = new DOIPOverHttpHandler();
@ -163,89 +150,57 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
@URIPath(method = HttpMethod.OPTIONS) @URIPath(method = HttpMethod.OPTIONS)
public void crossOrigin(ChannelHandlerContext ctx, FullHttpRequest request) { public void crossOrigin(ChannelHandlerContext ctx, FullHttpRequest request) {
DefaultFullHttpResponse fullResponse = DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
new DefaultFullHttpResponse( request.protocolVersion(), OK, Unpooled.wrappedBuffer("success".getBytes()));
request.protocolVersion(),
OK,
Unpooled.wrappedBuffer("success".getBytes()));
fullResponse.headers().remove("Access-Control-Allow-Origin"); fullResponse.headers().remove("Access-Control-Allow-Origin");
fullResponse.headers().remove("Access-Control-Allow-Headers"); fullResponse.headers().remove("Access-Control-Allow-Headers");
fullResponse.headers().add("Access-Control-Allow-Origin", "*"); fullResponse.headers().add("Access-Control-Allow-Origin", "*");
fullResponse fullResponse.headers().add("Access-Control-Allow-Methods", "*");
.headers() fullResponse.headers().add("Access-Control-Allow-Headers",
.add("Access-Control-Allow-Headers", "Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, "
"Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " + + "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection");
"X-Requested-With, Accept, Accept-Language, Cache-Control, Connection");
ChannelFuture f = ctx.write(fullResponse); ChannelFuture f = ctx.write(fullResponse);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
LOGGER.info("[OOOOOOOOption] received!"); LOGGER.info("[OOOOOOOOption] received!");
} }
@URIPath({"/SCIDE/CMManager", "/SCIDE/SCManager"}) @URIPath({"/SCIDE/CMManager", "/SCIDE/SCManager", "/BDO"})
public void handleHttpGet(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { public void handleHttpGet(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
QueryStringDecoder decoderQuery = new QueryStringDecoder(msg.uri()); JsonObject transformedParam =
Map<String, List<String>> parameters = decoderQuery.parameters(); ArgParser.parseGetAndVerify(msg, new ArgParser.VerifiedCallback() {
JsonObject transformedParam = new JsonObject(); @Override
for (String key : parameters.keySet()) { public void onResult(boolean verified, JsonObject transformedParam) {
List<String> val = parameters.get(key); LOGGER.info("verify signature: " + verified + " "
if (null != val) { + transformedParam.toString());
transformedParam.addProperty(key, val.get(0)); if (verified) {
} // 查permission
} String pubkey = transformedParam.get("pubKey").getAsString();
String ret = KeyValueDBUtil.instance
// 匿名用户权限为0 .getValue(CMTables.NodeRole.toString(), pubkey);
transformedParam.addProperty("permission", 0); long permission;
transformedParam.remove("verifiedPubKey"); if (ret != null && ret.length() > 0) {
permission = 0x86000d41L | Role.compoundValue(ret.split(","));
// 验签 有pubKey就必须有sign } else {
String uri = URLDecoder.decode(msg.uri(), "UTF-8").split("\\?")[1]; assert ret != null;
if (transformedParam.has("pubKey")) { permission = Role.compoundValue(ret.split(","));
int index = uri.lastIndexOf('&'); }
String str = uri.substring(0, index); transformedParam.addProperty("permission", permission);
LOGGER.info("before verifying: " + str); LOGGER.info("user permission: " + permission);
boolean verify = false; transformedParam.addProperty("verifiedPubKey",
try { transformedParam.get("pubKey").getAsString());
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());
}
}
handleReq(transformedParam, ctx, msg); handleReq(transformedParam, ctx, msg);
} }
@URIPath( @URIPath(method = HttpMethod.POST, value = {"/SCIDE/CMManager", "/SCIDE/SCManager", "/BDO"})
method = HttpMethod.POST,
value = {"/SCIDE/CMManager", "/SCIDE/SCManager"})
public void handleHttpPost(ChannelHandlerContext ctx, FullHttpRequest msg) public void handleHttpPost(ChannelHandlerContext ctx, FullHttpRequest msg)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
// http请求中规定签名必须是最后一个且公钥名必须为pubKey否则验签失败 // http请求中规定签名必须是最后一个且公钥名必须为pubKey否则验签失败
// 倒数第二个参数是pubKey倒数第一个参数是sign // 倒数第二个参数是pubKey倒数第一个参数是sign
// 有pubKey就必须签名 // 有pubKey就必须签名
String uri = URLDecoder.decode(msg.uri(), "UTF-8"); String uri = URLDecoder.decode(msg.uri(), "UTF-8");
LOGGER.info("handle " + uri);
ByteBuf content = msg.content(); ByteBuf content = msg.content();
byte[] reqContent = new byte[content.readableBytes()]; byte[] reqContent = new byte[content.readableBytes()];
content.readBytes(reqContent); content.readBytes(reqContent);
@ -267,11 +222,8 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
String str = "action:" + action; String str = "action:" + action;
boolean verify = false; boolean verify = false;
try { try {
verify = verify = SM2Util.plainStrVerify(map.get("pubKey").getAsString(), str,
SM2Util.plainStrVerify( map.get("sign").getAsString());
map.get("pubKey").getAsString(),
str,
map.get("sign").getAsString());
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
LOGGER.debug(ExceptionUtil.exceptionToString(e)); LOGGER.debug(ExceptionUtil.exceptionToString(e));
@ -304,10 +256,13 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
if (!map.has("action")) { if (!map.has("action")) {
LOGGER.error("no action in map!"); LOGGER.error("no action in map!");
ret = UNSUPPORTED_ACTION.getBytes(); ret = UNSUPPORTED_ACTION.getBytes();
DefaultFullHttpResponse response = DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
new DefaultFullHttpResponse( OK, Unpooled.wrappedBuffer(ret));
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); ChannelFuture f = ctx.write(response);
response.headers().add("Access-Control-Allow-Origin", "*");
response.headers().add("Access-Control-Allow-Methods", "*");
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
return; return;
} else { } else {
@ -317,34 +272,46 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
HttpResultCallback cb; HttpResultCallback cb;
if (action.equals("downloadContract")) { if (action.equals("downloadContract")) {
cb = new FileDownloaderCallback(ctx, req); 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")) { } else if (map.has("callback")) {
cb = new HttpResultCallback(ctx, map.get("callback").getAsString()); 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"); cb.addHeader("Content-Type", "application/json");
} else { } else {
cb = new HttpResultCallback(ctx, null); cb = new HttpResultCallback(ctx, null);
cb.addHeader("Content-Type", "application/json"); 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")) { 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"); cb.addHeader("content-disposition", "attachment;filename=encodeduuid.key");
} }
actionExecutor.handle(action, map, cb); actionExecutor.handle(action, map, cb);
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
DefaultFullHttpResponse response = DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK,
new DefaultFullHttpResponse( Unpooled.wrappedBuffer(e.getMessage().getBytes()));
HttpVersion.HTTP_1_1, response.headers().add("Access-Control-Allow-Origin", "*");
OK, response.headers().add("Access-Control-Allow-Methods", "*");
Unpooled.wrappedBuffer(e.getMessage().getBytes()));
ChannelFuture f = ctx.write(response); ChannelFuture f = ctx.write(response);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} catch (Exception e) { } catch (Exception e) {
Map<String, String> ret = new HashMap<>(); Map<String, String> ret = new HashMap<>();
ret.put("msg", ExceptionUtil.exceptionToString(e)); ret.put("msg", ExceptionUtil.exceptionToString(e));
DefaultFullHttpResponse response = DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK,
new DefaultFullHttpResponse( Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
HttpVersion.HTTP_1_1, response.headers().add("Access-Control-Allow-Origin", "*");
OK, response.headers().add("Access-Control-Allow-Methods", "*");
Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
ChannelFuture f = ctx.write(response); ChannelFuture f = ctx.write(response);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }
@ -352,9 +319,11 @@ public class CMHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 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)); LOGGER.debug(ExceptionUtil.exceptionToString(cause));
// ctx.close(); cause.printStackTrace();
// ctx.close();
} }
private String parseCtx2String(ChannelHandlerContext ctx) { private String parseCtx2String(ChannelHandlerContext ctx) {

View File

@ -1,5 +1,6 @@
package org.bdware.server.http; package org.bdware.server.http;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractMeta; import org.bdware.sc.ContractMeta;
import org.bdware.sc.YPKResourceManager;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.server.action.CMActions; import org.bdware.server.action.CMActions;
@ -20,11 +22,7 @@ import org.zz.gmhelper.SM2Util;
import java.io.*; import java.io.*;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.EnumSet; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; 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 { public class DOIPOverHttpHandler {
private static final Logger LOGGER = LogManager.getLogger(DOIPOverHttpHandler.class); private static final Logger LOGGER = LogManager.getLogger(DOIPOverHttpHandler.class);
Map<String, ZipFile> zipFilePool = new HashMap<>(); 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/"}) @URIPath({"/contracts/", "/DOIP/"})
public void mockDOIP(ChannelHandlerContext ctx, FullHttpRequest request) { public void mockDOIP(ChannelHandlerContext ctx, FullHttpRequest request) {
@ -41,32 +72,27 @@ public class DOIPOverHttpHandler {
String ops = arg.get("operation").getAsString(); String ops = arg.get("operation").getAsString();
Operations defaultOp = Operations.parse(ops); Operations defaultOp = Operations.parse(ops);
if (defaultOp == null) { if (defaultOp == null) {
CMActions.executeContractInternal( CMActions.executeContractInternal(arg, new ResultCallback() {
arg, @Override
new ResultCallback() { public void onResult(String str) {
@Override FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
public void onResult(String str) { HttpResponseStatus.OK, Unpooled.wrappedBuffer(str.getBytes()));
FullHttpResponse response = response.headers().set(HttpHeaderNames.CONTENT_TYPE,
new DefaultFullHttpResponse( "text/plain; charset=UTF-8");
HTTP_1_1, ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
HttpResponseStatus.OK, }
Unpooled.wrappedBuffer(str.getBytes())); }, null);
response.headers()
.set(
HttpHeaderNames.CONTENT_TYPE,
"text/plain; charset=UTF-8");
ctx.writeAndFlush(response)
.addListener(ChannelFutureListener.CLOSE);
}
},
null);
} else { } else {
switch (defaultOp) { switch (defaultOp) {
case ListOps: case ListOps:
sendListContractProcess(arg, ctx); sendListContractProcess(arg, ctx);
return; return;
case Retrieve: case Retrieve:
sendAssets(arg, ctx); if (arg.has("__public") && arg.get("__public").getAsBoolean()) {
sendPublicFiles(arg, ctx);
} else {
sendAssets(arg, ctx);
}
return; return;
case RetrieveYPK: case RetrieveYPK:
sendYpk(arg, ctx, request); sendYpk(arg, ctx, request);
@ -80,11 +106,8 @@ public class DOIPOverHttpHandler {
e.printStackTrace(); e.printStackTrace();
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo)); e.printStackTrace(new PrintStream(bo));
FullHttpResponse response = FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK,
new DefaultFullHttpResponse( Unpooled.wrappedBuffer(bo.toByteArray()));
HTTP_1_1,
HttpResponseStatus.OK,
Unpooled.wrappedBuffer(bo.toByteArray()));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} }
@ -93,22 +116,18 @@ public class DOIPOverHttpHandler {
private void sendListContractProcess(JsonObject arg, ChannelHandlerContext ctx) { private void sendListContractProcess(JsonObject arg, ChannelHandlerContext ctx) {
JsonObject ret = new JsonObject(); JsonObject ret = new JsonObject();
ret.addProperty("action", "onListOps"); ret.addProperty("action", "onListOps");
ret.addProperty( ret.addProperty("data",
"data", CMActions.manager.listTheContracts(arg.get("contractID").getAsString())); CMActions.manager.listTheContracts(arg.get("contractID").getAsString()));
FullHttpResponse response = FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK,
new DefaultFullHttpResponse( Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
HTTP_1_1,
HttpResponseStatus.OK,
Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes()));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} }
private void sendYpk(JsonObject arg, ChannelHandlerContext ctx, FullHttpRequest request) private void sendYpk(JsonObject arg, ChannelHandlerContext ctx, FullHttpRequest request)
throws IOException { throws IOException {
ContractMeta meta = ContractMeta meta = CMActions.manager.statusRecorder
CMActions.manager.statusRecorder.getContractMeta( .getContractMeta(arg.get("contractID").getAsString());
arg.get("contractID").getAsString());
if (meta == null) { if (meta == null) {
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND); HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
return; return;
@ -142,44 +161,115 @@ public class DOIPOverHttpHandler {
return; return;
} }
// TODO @杨环宇 把zipFilePool用起来做一个延迟关闭的东西避免频繁打开文件的操作 private void sendPublicFiles(JsonObject arg, ChannelHandlerContext ctx) throws Exception {
private void sendAssets(JsonObject arg, ChannelHandlerContext ctx) throws Exception { ContractMeta meta = CMActions.manager.statusRecorder
.getContractMeta(arg.get("contractID").getAsString());
ContractMeta meta =
CMActions.manager.statusRecorder.getContractMeta(
arg.get("contractID").getAsString());
if (meta == null) { if (meta == null) {
HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND); HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
return; return;
} }
String path = arg.get("argument").getAsString(); String path = arg.get("argument").getAsString();
ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr())); File dir = new File("./ContractDB", meta.getName());
ZipEntry zipEntry = zipFile.getEntry(path); File toSend = new File(dir, path);
FileInputStream fin;
if (zipEntry == null) { 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); HttpFileHandleAdapter.sendError(ctx, HttpResponseStatus.NOT_FOUND);
return; return;
} }
InputStream in = zipFile.getInputStream(zipEntry);
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
HttpFileHandleAdapter.appendContentType(path, response.headers()); HttpFileHandleAdapter.appendContentType(path, response.headers());
ctx.write(response); ctx.write(response);
ChannelFuture future = ctx.writeAndFlush(new ChunkedStream(in)); ChannelFuture future = ctx.writeAndFlush(new ChunkedStream(fin));
future.addListener( future.addListener(new GenericFutureListener<Future<? super Void>>() {
new GenericFutureListener<Future<? super Void>>() { @Override
@Override public void operationComplete(Future<? super Void> arg0) throws Exception {
public void operationComplete(Future<? super Void> arg0) throws Exception { ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); fin.close();
zipFile.close(); }
} });
});
// 写入文件尾部 // 写入文件尾部
future.addListener(ChannelFutureListener.CLOSE); future.addListener(ChannelFutureListener.CLOSE);
return; 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[] data = fulluri.split("/");
String contractIDOrName = data[2]; String contractIDOrName = data[2];
String uris[] = URLDecoder.decode(fulluri).split("\\?"); String uris[] = URLDecoder.decode(fulluri).split("\\?");
@ -187,34 +277,40 @@ public class DOIPOverHttpHandler {
if (uri != null && transformedParam.has("pubKey")) { if (uri != null && transformedParam.has("pubKey")) {
int index = fulluri.lastIndexOf('&'); int index = fulluri.lastIndexOf('&');
String str = fulluri; 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); LOGGER.info("[mockDOIP] before verify:" + str);
boolean verify = false; boolean verify = false;
try { try {
verify = verify = SM2Util.plainStrVerify(transformedParam.get("pubKey").getAsString(), str,
SM2Util.plainStrVerify( transformedParam.get("sign").getAsString());
transformedParam.get("pubKey").getAsString(),
str,
transformedParam.get("sign").getAsString());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
if (verify) { if (verify) {
transformedParam.addProperty( transformedParam.addProperty("verifiedPubKey",
"verifiedPubKey", transformedParam.get("pubKey").getAsString()); transformedParam.get("pubKey").getAsString());
} else transformedParam.remove("verifiedPubKey"); } else
transformedParam.remove("verifiedPubKey");
} }
transformedParam.addProperty("contractID", data[2].replaceAll("\\?.*$", "")); transformedParam.addProperty("contractID", data[2].replaceAll("\\?.*$", ""));
if (data.length > 3) { if (data.length > 3) {
String assetPath = data[3]; String assetPath = data[3];
if (assetPath.equals("assets")) { transformedParam.remove("__public");
if (assetPath.equals("assets") && !fulluri.contains("..")) {
transformedParam.addProperty("operation", Operations.Retrieve.val); transformedParam.addProperty("operation", Operations.Retrieve.val);
int index = data[0].length() + data[1].length() + data[2].length() + 2; int index = data[0].length() + data[1].length() + data[2].length() + 2;
String path = fulluri.substring(index); String path = fulluri.substring(index);
path = path.replaceAll("\\?.*$", ""); path = path.replaceAll("\\?.*$", "");
if (path.equals("/assets/")) if (path.equals("/assets/"))
path+="index.html"; path += "index.html";
transformedParam.addProperty("argument", path); 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?")) { if (assetPath.startsWith("ypk?")) {
transformedParam.addProperty("operation", Operations.RetrieveYPK.val); transformedParam.addProperty("operation", Operations.RetrieveYPK.val);
@ -230,22 +326,19 @@ public class DOIPOverHttpHandler {
JsonObject transformedParam = new JsonObject(); JsonObject transformedParam = new JsonObject();
for (String key : parameters.keySet()) { for (String key : parameters.keySet()) {
List<String> val = parameters.get(key); 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; return transformedParam;
} }
enum Operations { enum Operations {
Hello("0.DOIP/Op.Hello"), Hello("0.DOIP/Op.Hello"), ListOps("0.DOIP/Op.ListOperations"), Retrieve(
ListOps("0.DOIP/Op.ListOperations"), "0.DOIP/Op.Retrieve"), RetrieveYPK("86.BDWare/Op.RetrieveYPK"), Create(
Retrieve("0.DOIP/Op.Retrieve"), "0.DOIP/Op.Create"), Update("0.DOIP/Op.Update"), Delete(
RetrieveYPK("86.BDWare/Op.RetrieveYPK"), "0.DOIP/Op.Delete"), Search("0.DOIP/Op.Search"), Extension(
Create("0.DOIP/Op.Create"), "0.DOIP/Op.Extension"), Unknown("0.DOIP/Op.Unknown");
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; private final String val;
Operations(String s) { Operations(String s) {
@ -254,7 +347,8 @@ public class DOIPOverHttpHandler {
public static Operations parse(String str) { public static Operations parse(String str) {
for (Operations ops : EnumSet.allOf(Operations.class)) { for (Operations ops : EnumSet.allOf(Operations.class)) {
if (ops.val.equals(str)) return ops; if (ops.val.equals(str))
return ops;
} }
return null; return null;
} }

View File

@ -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;
}
}

View File

@ -12,6 +12,7 @@ import org.bdware.sc.conn.ByteUtil;
import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.conn.ResultCallback;
import org.bdware.sc.db.CMTables; import org.bdware.sc.db.CMTables;
import org.bdware.sc.db.KeyValueDBUtil; import org.bdware.sc.db.KeyValueDBUtil;
import org.bdware.sc.event.REvent;
import org.bdware.sc.units.MultiContractMeta; import org.bdware.sc.units.MultiContractMeta;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.server.GlobalConf; import org.bdware.server.GlobalConf;
@ -40,17 +41,17 @@ public class NodeCenterClientController implements NodeCenterConn {
private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientController.class); private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientController.class);
public static SyncResult sync = new SyncResult(); public static SyncResult sync = new SyncResult();
private static boolean startCheck = false; private static boolean startCheck = false;
private final Map<String, FileOutputStream> fileMap;
private final NetNeighbors neighbors; private final NetNeighbors neighbors;
public Map<String, ResultCallback> distributeReqMap = new ConcurrentHashMap<>(); public Map<String, ResultCallback> distributeReqMap = new ConcurrentHashMap<>();
// public NodeCenterClientController cmClientController; // public NodeCenterClientController cmClientController;
String nodeID; String nodeID;
NodeCenterClientHandler handler; NodeCenterClientHandler handler;
// 合约contractIDmaster的公钥 // 合约contractIDmaster的公钥
Map<String, String> contractID2PubKey = new ConcurrentHashMap<>(); Map<String, String> contractID2PubKey = new ConcurrentHashMap<>();
Deque<JsonObject> receiveQueue = new ArrayDeque<>();
ReceiveFileThread receiveFileThread = new ReceiveFileThread();
public NodeCenterClientController(String nodeID) { public NodeCenterClientController(String nodeID) {
this.fileMap = new HashMap<>();
this.nodeID = nodeID; this.nodeID = nodeID;
this.neighbors = new NetNeighbors(); this.neighbors = new NetNeighbors();
} }
@ -122,11 +123,8 @@ public class NodeCenterClientController implements NodeCenterConn {
setNodeID.put("id", keyPair.getPublicKeyStr()); setNodeID.put("id", keyPair.getPublicKeyStr());
String signature = "no signature"; String signature = "no signature";
try { try {
byte[] sig = byte[] sig = SM2Util.sign(keyPair.getPrivateKeyParameter(),
SM2Util.sign( (keyPair.getPublicKeyStr() + json.get("session").getAsString()).getBytes());
keyPair.getPrivateKeyParameter(),
(keyPair.getPublicKeyStr() + json.get("session").getAsString())
.getBytes());
signature = ByteUtils.toHexString(sig); signature = ByteUtils.toHexString(sig);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -135,7 +133,8 @@ public class NodeCenterClientController implements NodeCenterConn {
setNodeID.put("nodeName", GlobalConf.instance.name); setNodeID.put("nodeName", GlobalConf.instance.name);
setNodeID.put("peerID", GlobalConf.instance.peerID); setNodeID.put("peerID", GlobalConf.instance.peerID);
setNodeID.put("ipPort", GlobalConf.instance.ipPort); 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)); sendMsg(JsonUtil.toJson(setNodeID));
} }
@ -147,8 +146,8 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action @Action
public void syncPong(JsonObject json, ResultCallback resultCallback) { public void syncPong(JsonObject json, ResultCallback resultCallback) {
sync.wakeUp( sync.wakeUp(json.get("requestID").getAsString(),
json.get("requestID").getAsString(), "{\"status\":\"Success\",\"result\":\"a\"}"); "{\"status\":\"Success\",\"result\":\"a\"}");
} }
public boolean syncPing() { public boolean syncPing() {
@ -171,12 +170,9 @@ public class NodeCenterClientController implements NodeCenterConn {
} }
public void listCMInfo() { public void listCMInfo() {
sendMsg( sendMsg("{\"action\":\"listCMInfo\",\"pubKey\":\""
"{\"action\":\"listCMInfo\",\"pubKey\":\"" + KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey")
+ KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey") + "\",\"requestID\":\"" + System.currentTimeMillis() + "\"}");
+ "\",\"requestID\":\""
+ System.currentTimeMillis()
+ "\"}");
} }
@Action @Action
@ -208,8 +204,9 @@ public class NodeCenterClientController implements NodeCenterConn {
public void updateNonMasters(JsonObject jo, ResultCallback cb) { public void updateNonMasters(JsonObject jo, ResultCallback cb) {
String[] contracts = jo.get("contracts").getAsString().split(","); String[] contracts = jo.get("contracts").getAsString().split(",");
for (String id : contracts) { for (String id : contracts) {
LOGGER.info("旧的master设置合约 " + id + " 自己不再是master"); if (null != CMActions.manager.setContractIsMaster(id, String.valueOf(false))) {
CMActions.manager.setContractIsMaster(id, 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"); 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) @Action(async = true)
public void requestReadMe(JsonObject jo, ResultCallback cb) throws IOException { public void requestReadMe(JsonObject jo, ResultCallback cb) throws IOException {
JsonObject resp = new JsonObject(); JsonObject resp = new JsonObject();
@ -237,7 +242,6 @@ public class NodeCenterClientController implements NodeCenterConn {
resp.addProperty("pubkey", meta.contract.getPublicKey()); resp.addProperty("pubkey", meta.contract.getPublicKey());
resp.addProperty("name", meta.getName()); resp.addProperty("name", meta.getName());
resp.addProperty("buildTime", meta.contract.getBuildTime()); resp.addProperty("buildTime", meta.contract.getBuildTime());
resp.addProperty("doi", meta.contract.getDOI());
ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr())); ZipFile zipFile = new ZipFile(new File(meta.contract.getScriptStr()));
ZipEntry zipEntry = zipFile.getEntry("/assets/README.md"); ZipEntry zipEntry = zipFile.getEntry("/assets/README.md");
if (null == zipEntry) { if (null == zipEntry) {
@ -248,7 +252,7 @@ public class NodeCenterClientController implements NodeCenterConn {
InputStream in = zipFile.getInputStream(zipEntry); InputStream in = zipFile.getInputStream(zipEntry);
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buff = new byte[1024]; 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); bo.write(buff, 0, k);
} }
resp.addProperty("readmeStr", bo.toString()); resp.addProperty("readmeStr", bo.toString());
@ -269,15 +273,18 @@ public class NodeCenterClientController implements NodeCenterConn {
String requestID = jo.get("requestID").getAsString(); String requestID = jo.get("requestID").getAsString();
String requesterNodeID = jo.get("requesterNodeID").getAsString(); String requesterNodeID = jo.get("requesterNodeID").getAsString();
String crStr = jo.get("contractRequest").getAsString(); String crStr = jo.get("contractRequest").getAsString();
String cr = CMActions.manager.executeLocallyAsync(JsonUtil.fromJson(crStr, ContractRequest.class),
CMActions.manager.executeLocally( new ResultCallback() {
JsonUtil.fromJson(crStr, ContractRequest.class), null); @Override
JsonObject ret = new JsonObject(); public void onResult(String str) {
ret.addProperty("action", "onReceiveContractExecution"); JsonObject ret = new JsonObject();
ret.addProperty("requestID", requestID); ret.addProperty("action", "onReceiveContractExecution");
ret.addProperty("requesterNodeID", requesterNodeID); ret.addProperty("requestID", requestID);
ret.addProperty("contractResult", cr); ret.addProperty("requesterNodeID", requesterNodeID);
sendMsg(JsonUtil.toJson(ret)); ret.addProperty("contractResult", str);
sendMsg(JsonUtil.toJson(ret));
}
}, null);
} }
@Action(async = true) @Action(async = true)
@ -330,8 +337,8 @@ public class NodeCenterClientController implements NodeCenterConn {
@Override @Override
public String routeContract(String contractID) { public String routeContract(String contractID) {
LOGGER.info("[CMClientController] routeContract : " + contractID); LOGGER.debug("[CMClientController] routeContract : " + contractID);
LOGGER.info("contractID2Pubkey.contractsKey=" + contractID2PubKey.containsKey(contractID)); LOGGER.debug("contractID2Pubkey.contractsKey=" + contractID2PubKey.containsKey(contractID));
// TODO RouteContract是不是IRP协议/DOIP协议 // TODO RouteContract是不是IRP协议/DOIP协议
if (contractID2PubKey.containsKey(contractID)) { if (contractID2PubKey.containsKey(contractID)) {
return contractID2PubKey.get(contractID); return contractID2PubKey.get(contractID);
@ -347,15 +354,15 @@ public class NodeCenterClientController implements NodeCenterConn {
String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 100000); String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 100000);
req.put("requestID", requestID); req.put("requestID", requestID);
sendMsg(JsonUtil.toJson(req)); sendMsg(JsonUtil.toJson(req));
//TODO use async instead? // TODO use async instead?
ContractResult cr = sync.syncSleep(requestID); ContractResult cr = sync.syncSleep(requestID);
LOGGER.debug("result: " + JsonUtil.toJson(cr)); LOGGER.debug("result: " + JsonUtil.toJson(cr));
LOGGER.info("node " + pubKey + " succeed!"); LOGGER.info("node " + pubKey + " succeed!");
if (!cr.result.equals(JsonNull.INSTANCE)) { if (!cr.result.equals(JsonNull.INSTANCE)) {
try { try {
JsonObject jo = cr.result.getAsJsonObject(); JsonObject jo = cr.result.getAsJsonObject();
NetworkManager.instance.updateAgentRouter( NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString()); jo.get("masterAddress").getAsString());
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null); NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
return "success"; return "success";
} catch (Exception e) { } catch (Exception e) {
@ -379,18 +386,17 @@ public class NodeCenterClientController implements NodeCenterConn {
if (!cr.result.equals(JsonNull.INSTANCE)) { if (!cr.result.equals(JsonNull.INSTANCE)) {
try { try {
JsonObject jo = cr.result.getAsJsonObject(); JsonObject jo = cr.result.getAsJsonObject();
NetworkManager.instance.updateAgentRouter( NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString()); jo.get("masterAddress").getAsString());
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null); NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
LOGGER.info( LOGGER.info(String.format("the master of contract %s: pubKey=%s address=%s",
String.format("the master of contract %s: pubKey=%s address=%s", contractID, jo.get("pubKey").getAsString(),
contractID, jo.get("masterAddress").getAsString()));
jo.get("pubKey").getAsString(),
jo.get("masterAddress").getAsString()));
contractID2PubKey.put(contractID, jo.get("pubKey").getAsString()); contractID2PubKey.put(contractID, jo.get("pubKey").getAsString());
return jo.get("pubKey").getAsString(); return jo.get("pubKey").getAsString();
} catch (Exception e) { } 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) { } catch (Exception e) {
@ -409,8 +415,8 @@ public class NodeCenterClientController implements NodeCenterConn {
return; return;
} }
startCheck = true; startCheck = true;
for (MultiContractMeta meta : for (MultiContractMeta meta : CMActions.manager.multiContractRecorder.getStatus()
CMActions.manager.multiContractRecorder.getStatus().values()) { .values()) {
String contractID = meta.getContractID(); String contractID = meta.getContractID();
LOGGER.info("check master of contract " + contractID); LOGGER.info("check master of contract " + contractID);
ContractMeta cmeta = CMActions.manager.statusRecorder.getContractMeta(meta.getID()); ContractMeta cmeta = CMActions.manager.statusRecorder.getContractMeta(meta.getID());
@ -424,20 +430,21 @@ public class NodeCenterClientController implements NodeCenterConn {
// 该合约可能在这个节点崩溃期间已经被终止不存在了这个节点不用恢复这个合约了直接从数据库中删除 // 该合约可能在这个节点崩溃期间已经被终止不存在了这个节点不用恢复这个合约了直接从数据库中删除
LOGGER.info("该合约在集群已经不存在!"); LOGGER.info("该合约在集群已经不存在!");
KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), contractID); KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), contractID);
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.CheckPointLastHash.toString(),
CMTables.CheckPointLastHash.toString(), contractID)) { contractID)) {
KeyValueDBUtil.instance.delete( KeyValueDBUtil.instance.delete(CMTables.CheckPointLastHash.toString(),
CMTables.CheckPointLastHash.toString(), contractID); contractID);
} }
if (KeyValueDBUtil.instance.containsKey( if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
CMTables.LastExeSeq.toString(), contractID)) { contractID)) {
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID); KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID);
} }
continue; continue;
} }
MasterClientRecoverMechAction.recoverSet.add(contractID); 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(); RecoverMechTimeRecorder.queryMasterFinish = System.currentTimeMillis();
queryUnitContractsID2(contractID, master); queryUnitContractsID2(contractID, master);
} }
@ -478,9 +485,7 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action(async = true) @Action(async = true)
public void requestLog(JsonObject json, ResultCallback rc) { public void requestLog(JsonObject json, ResultCallback rc) {
if (!json.has("requestID") if (!json.has("requestID") || !json.has("contractID") || !json.has("offset")
|| !json.has("contractID")
|| !json.has("offset")
|| !json.has("count")) { || !json.has("count")) {
LOGGER.debug( LOGGER.debug(
"[CMClientController] missing arguments, requestID / contractID / offset / count"); "[CMClientController] missing arguments, requestID / contractID / offset / count");
@ -518,69 +523,20 @@ public class NodeCenterClientController implements NodeCenterConn {
queryNCRepoDOI(json, result); queryNCRepoDOI(json, result);
} }
@Action(async = true) @Action(async = false)
public void receiveProject(JsonObject args, final ResultCallback rc) { public synchronized void receiveProject(JsonObject args, final ResultCallback rc) {
LOGGER.debug("position----7"); try {
receiveQueue.add(args);
String fileName = args.get("fileName").getAsString(); synchronized (receiveFileThread) {
boolean isAppend = args.get("isAppend").getAsBoolean(); receiveFileThread.notify();
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();
} }
} catch (Exception e) {
e.printStackTrace();
} }
} }
public void queryNCRepoDOI(JsonObject json, ResultCallback result) { public void queryNCRepoDOI(JsonObject json, ResultCallback result) {
LOGGER.debug("sendProject: position ---- 3");
String projectName = json.get("projectName").getAsString(); String projectName = json.get("projectName").getAsString();
LOGGER.debug("sendProject: position ---- 3\tprojectName=" + projectName);
String parPath = GlobalConf.instance.publicCompiledDir; String parPath = GlobalConf.instance.publicCompiledDir;
boolean isPrivate = false; boolean isPrivate = false;
if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) { if (json.has("isPrivate") && json.get("isPrivate").getAsBoolean()) {
@ -601,11 +557,11 @@ public class NodeCenterClientController implements NodeCenterConn {
LOGGER.debug("tempZip = " + tempZip.getAbsolutePath()); LOGGER.debug("tempZip = " + tempZip.getAbsolutePath());
try { try {
FileInputStream fin = new FileInputStream(tempZip); FileInputStream fin = new FileInputStream(tempZip);
byte[] buff = new byte[30 * 1024]; byte[] buff = new byte[500 * 1024];
long total = tempZip.length(); long total = tempZip.length();
LOGGER.debug("temp length = " + total); LOGGER.debug("temp length = " + total);
long count = 0; long count = 0;
for (int len; (len = (fin.read(buff))) > 0; ) { for (int len; (len = (fin.read(buff))) > 0;) {
LOGGER.debug("read len = " + len); LOGGER.debug("read len = " + len);
req.put("data", ByteUtil.encodeBASE64(buff, len)); req.put("data", ByteUtil.encodeBASE64(buff, len));
count += len; count += len;
@ -669,8 +625,8 @@ public class NodeCenterClientController implements NodeCenterConn {
MasterElectTimeRecorder.slaveConnectFinish = System.currentTimeMillis(); MasterElectTimeRecorder.slaveConnectFinish = System.currentTimeMillis();
// 开启master恢复 // 开启master恢复
MasterServerRecoverMechAction.newMasterRecover( MasterServerRecoverMechAction.newMasterRecover(contractID,
contractID, json.get("members").getAsString(), onlineMembers); json.get("members").getAsString(), onlineMembers);
} }
private boolean waitForConnection(List<String> nodeNames) { private boolean waitForConnection(List<String> nodeNames) {
@ -690,7 +646,8 @@ public class NodeCenterClientController implements NodeCenterConn {
e.printStackTrace(); e.printStackTrace();
} }
} }
} else return true; } else
return true;
} }
return false; return false;
} }
@ -701,18 +658,34 @@ public class NodeCenterClientController implements NodeCenterConn {
} }
@Action(async = true) @Action(async = true)
public void onDistribute(JsonObject json, ResultCallback rc) { public void onDistribute(JsonObject json, ResultCallback rc) throws IOException {
if (json.has("over")) { 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); ResultCallback to = distributeReqMap.get(distributeID);
distributeReqMap.remove(distributeID); distributeReqMap.remove(distributeID);
to.onResult(json.get("content").getAsString()); to.onResult(json.get("content").getAsString());
if (to instanceof Closeable) {
((Closeable) to).close();
}
} else { } else {
String distributeID = json.get("distributeID").getAsString(); String distributeID = json.get("distributeID").getAsString();
distributeReqMap.get(distributeID).onResult(json.get("content").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) @Action(async = true)
public void transferInstance(JsonObject jo, ResultCallback result) { public void transferInstance(JsonObject jo, ResultCallback result) {
LOGGER.info("transferInstance"); LOGGER.info("transferInstance");
@ -743,13 +716,11 @@ public class NodeCenterClientController implements NodeCenterConn {
public void NCStartElect(JsonObject jo, ResultCallback result) { public void NCStartElect(JsonObject jo, ResultCallback result) {
String conID = jo.get("contractID").getAsString(); String conID = jo.get("contractID").getAsString();
String uniNumber = null; 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); ContractClient cc = CMActions.manager.getClient(conID);
LOGGER.info( LOGGER.info("[CMClientController] NCStartElect : contractID=" + conID + " client==null:"
"[CMClientController] NCStartElect : contractID=" + (null == cc));
+ conID
+ " client==null:"
+ (null == cc));
// 不是自己本地的合约 // 不是自己本地的合约
if (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();
}
}
}
}
} }

View File

@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.bdware.server.CMHttpServer.pluginLoader; import static org.bdware.server.CMHttpServer.pluginLoader;
@ -31,7 +30,7 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
public boolean hasPermission; public boolean hasPermission;
private final NodeCenterClientController controller; private final NodeCenterClientController controller;
Channel channel; Channel channel;
// UDPTrustfulExecutor udpExecutor; // UDPTrustfulExecutor udpExecutor;
// RecoverMechExecutor recoverMechExecutor; // RecoverMechExecutor recoverMechExecutor;
ActionExecutor<ResultCallback, JsonObject> ae; ActionExecutor<ResultCallback, JsonObject> ae;
private boolean isConnected; private boolean isConnected;
@ -82,8 +81,8 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
} }
public synchronized void sendMsg(String msg) { public synchronized void sendMsg(String msg) {
// logger.debug("[CMClientHandler] sendMsg, TID:" + // logger.debug("[CMClientHandler] sendMsg, TID:" +
// Thread.currentThread().getId() + msg+" isActive:"+channel.isActive()+" // Thread.currentThread().getId() + msg+" isActive:"+channel.isActive()+"
// "+channel.isOpen()); // "+channel.isOpen());
ByteBuf buf = Unpooled.wrappedBuffer(msg.getBytes()); ByteBuf buf = Unpooled.wrappedBuffer(msg.getBytes());
channel.writeAndFlush(buf); channel.writeAndFlush(buf);
@ -100,19 +99,16 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
final JsonObject arg = final JsonObject arg =
JsonParser.parseReader(new InputStreamReader(new ByteArrayInputStream(bytes))) JsonParser.parseReader(new InputStreamReader(new ByteArrayInputStream(bytes)))
.getAsJsonObject(); .getAsJsonObject();
// logger.debug("[CMClient] receive:" + arg.toString()); // logger.debug("[CMClient] receive:" + arg.toString());
if (arg.has("action")) { if (arg.has("action")) {
final String action = arg.get("action").getAsString(); final String action = arg.get("action").getAsString();
ae.handle( ae.handle(action, arg, new ResultCallback() {
action, @Override
arg, public void onResult(String str) {
new ResultCallback() { sendMsg(str);
@Override }
public void onResult(String str) { });
sendMsg(str);
}
});
} }
} catch (java.lang.IllegalArgumentException e) { } catch (java.lang.IllegalArgumentException e) {
@ -136,7 +132,8 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler<Object>
public void close() { public void close() {
try { try {
isConnected = false; isConnected = false;
if (channel != null) channel.close(); if (channel != null)
channel.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {

View File

@ -40,16 +40,10 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
public TCPClientFrameHandler(String masterPubkey) { public TCPClientFrameHandler(String masterPubkey) {
master = masterPubkey; master = masterPubkey;
aliveCheckClientAction = new AliveCheckClientAction(masterPubkey); aliveCheckClientAction = new AliveCheckClientAction(masterPubkey);
ae = new ActionExecutor<>( ae = new ActionExecutor<>(executorService, aliveCheckClientAction,
executorService, new MasterClientTCPAction(), new MasterClientRecoverMechAction(),
aliveCheckClientAction, MasterClientTransferAction.instance, new MasterServerRecoverMechAction(),
new MasterClientTCPAction(), new MasterServerTransferAction(), new MasterServerTCPAction(), new EventActions());
new MasterClientRecoverMechAction(),
MasterClientTransferAction.instance,
new MasterServerRecoverMechAction(),
new MasterServerTransferAction(),
new MasterServerTCPAction(),
new EventActions());
for (String str : clientToAgentPlugins) { for (String str : clientToAgentPlugins) {
Object obj = createInstanceByClzName(str); Object obj = createInstanceByClzName(str);
ae.appendHandler(obj); ae.appendHandler(obj);
@ -66,21 +60,22 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
} }
// public void updateContractID2Client(String contract) { // public void updateContractID2Client(String contract) {
// // 如果该合约正在和旧的master保持连接 // // 如果该合约正在和旧的master保持连接
// if (MasterClientTCPAction.contractID2MasterInfo.containsKey(contract)) { // if (MasterClientTCPAction.contractID2MasterInfo.containsKey(contract)) {
// MasterClientTCPAction former = // MasterClientTCPAction former =
// MasterClientTCPAction.contractID2MasterInfo.get(contract); // MasterClientTCPAction.contractID2MasterInfo.get(contract);
// former.closeMaster(); // former.closeMaster();
// } // }
// //
// MasterClientTCPAction.contractID2MasterInfo.put(contract, actions); // MasterClientTCPAction.contractID2MasterInfo.put(contract, actions);
// } // }
public void close() { public void close() {
try { try {
aliveCheckClientAction.closeMaster(); aliveCheckClientAction.closeMaster();
if (channel != null) channel.close(); if (channel != null)
channel.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -108,7 +103,8 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
ByteBuf bb = (ByteBuf) frame; ByteBuf bb = (ByteBuf) frame;
JsonObject arg; JsonObject arg;
try { try {
arg = JsonUtil.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb))); arg = JsonUtil
.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb)));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Response response = new Response(); Response response = new Response();
@ -119,7 +115,6 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
} }
Response response; Response response;
try { try {
//LOGGER.info("====== TCPClientFrameHandler:" + arg.toString());
final String action = arg.get("action").getAsString(); final String action = arg.get("action").getAsString();
PubkeyResultCallback pc = new PubkeyResultCallback(master, new ResultCallback() { PubkeyResultCallback pc = new PubkeyResultCallback(master, new ResultCallback() {
@Override @Override
@ -127,10 +122,7 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
sendMsg(ret); sendMsg(ret);
} }
}); });
ae.handle( ae.handle(action, arg, pc);
action,
arg, pc
);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
response = new Response(); response = new Response();
response.action = "onException"; response.action = "onException";
@ -146,12 +138,9 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler<Object> {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
int count = 0; int count = 0;
for (String s : strs) { for (String s : strs) {
if (s.contains("sun.reflect") if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|| s.contains("java.lang.reflect") || s.contains("org.apache") || s.contains("java.util")
|| s.contains("org.apache") || s.contains("java.lang") || s.contains("io.netty")) {
|| s.contains("java.util")
|| s.contains("java.lang")
|| s.contains("io.netty")) {
continue; continue;
} }
ret.append(s); ret.append(s);

View File

@ -31,52 +31,44 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
public TCPServerFrameHandler() { public TCPServerFrameHandler() {
checkAction = new AliveCheckServerAction(this); checkAction = new AliveCheckServerAction(this);
ae = ae = new ActionExecutor<ResultCallback, JsonObject>(executorService, checkAction,
new ActionExecutor<ResultCallback, JsonObject>( new MasterClientTCPAction(), new MasterClientRecoverMechAction(),
executorService, MasterClientTransferAction.instance, new MasterServerRecoverMechAction(),
checkAction, new MasterServerTransferAction(), new MasterServerTCPAction(), new EventActions()) {
new MasterClientTCPAction(), @Override
new MasterClientRecoverMechAction(), public boolean checkPermission(Action a, final JsonObject args, long permission) {
MasterClientTransferAction.instance, return true;
new MasterServerRecoverMechAction(), // long val = a.userPermission();
new MasterServerTransferAction(), //
new MasterServerTCPAction(), // boolean flag;
new EventActions()) { // String status = "refuse";
@Override // String action = args.get("action");
public boolean checkPermission( //
Action a, final JsonObject args, long permission) { // if (val == 0) {
return true; // flag = true;
// long val = a.userPermission(); // status = "accept";
// // } else if ((permission & val) == val) {
// boolean flag; // flag = true;
// String status = "refuse"; // status = "accept";
// String action = args.get("action"); // } else {
// // flag = false;
// if (val == 0) { // }
// flag = true; //
// status = "accept"; // TimeDBUtil.instance.put(
// } else if ((permission & val) == val) { // NCTables.CMLog.toString(),
// flag = true; // "{\"action\":\""
// status = "accept"; // + action
// } else { // + "\",\"pubKey\":\""
// flag = false; // + pubKey
// } // + "\",\"status\":\""
// // + status
// TimeDBUtil.instance.put( // + "\",\"date\":"
// NCTables.CMLog.toString(), // + System.currentTimeMillis()
// "{\"action\":\"" // + "}");
// + action //
// + "\",\"pubKey\":\"" // return flag;
// + pubKey }
// + "\",\"status\":\"" };
// + status
// + "\",\"date\":"
// + System.currentTimeMillis()
// + "}");
//
// return flag;
}
};
} }
@ -98,8 +90,9 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
ByteBuf bb = (ByteBuf) frame; ByteBuf bb = (ByteBuf) frame;
JsonObject arg; JsonObject arg;
try { try {
arg = JsonUtil.parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb))); arg = JsonUtil
// logger.info("[MasterServer] receive:" + arg.toString()); .parseReaderAsJsonObject(new InputStreamReader(new ByteBufInputStream(bb)));
// logger.info("[MasterServer] receive:" + arg.toString());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -114,16 +107,14 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
final String action = arg.get("action").getAsString(); final String action = arg.get("action").getAsString();
PubkeyResultCallback pubkeyResultCallback = new PubkeyResultCallback(checkAction.pubKey, new ResultCallback() { PubkeyResultCallback pubkeyResultCallback =
@Override new PubkeyResultCallback(checkAction.pubKey, new ResultCallback() {
public void onResult(String ret) { @Override
sendMsg(ret); public void onResult(String ret) {
} sendMsg(ret);
}); }
ae.handle( });
action, ae.handle(action, arg, pubkeyResultCallback);
arg, pubkeyResultCallback
);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
response = new Response(); response = new Response();
@ -141,12 +132,9 @@ public class TCPServerFrameHandler extends SimpleChannelInboundHandler<Object> {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
int count = 0; int count = 0;
for (String s : strs) { for (String s : strs) {
if (s.contains("sun.reflect") if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|| s.contains("java.lang.reflect") || s.contains("org.apache") || s.contains("java.util")
|| s.contains("org.apache") || s.contains("java.lang") || s.contains("io.netty")) {
|| s.contains("java.util")
|| s.contains("java.lang")
|| s.contains("io.netty")) {
continue; continue;
} }
ret.append(s); ret.append(s);

View File

@ -33,28 +33,24 @@ public class AgentManager implements AgentPeerManagerIntf {
@Override @Override
public void executeByOtherNodeAsync(String pubKey, ContractRequest c, ResultCallback cb) { 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(); int maxMasterProxyLoad = CongestionControl.masterProxyLoad.incrementAndGet();
if (maxMasterProxyLoad > CongestionControl.maxMasterProxyLoad) if (maxMasterProxyLoad > CongestionControl.maxMasterProxyLoad)
CongestionControl.maxMasterProxyLoad = maxMasterProxyLoad; CongestionControl.maxMasterProxyLoad = maxMasterProxyLoad;
if (CongestionControl.slaveControl()) { if (CongestionControl.slaveControl()) {
ContractResult cr = ContractResult cr = new ContractResult(ContractResult.Status.Error,
new ContractResult( new JsonPrimitive("canceled because of queue too long"));
ContractResult.Status.Error,
new JsonPrimitive("canceled because of queue too long"));
cb.onResult(JsonUtil.parseObjectAsJsonObject(cr)); cb.onResult(JsonUtil.parseObjectAsJsonObject(cr));
CongestionControl.masterProxyLoad.decrementAndGet(); CongestionControl.masterProxyLoad.decrementAndGet();
return; return;
} }
MasterServerTCPAction.sync.sleep( MasterServerTCPAction.sync.sleep(c.getRequestID(), new ResultCallback() {
c.getRequestID(), @Override
new ResultCallback() { public void onResult(String str) {
@Override cb.onResult(JsonUtil.parseStringAsJsonObject(str));
public void onResult(String str) { CongestionControl.masterProxyLoad.decrementAndGet();
cb.onResult(JsonUtil.parseStringAsJsonObject(str)); }
CongestionControl.masterProxyLoad.decrementAndGet(); });
}
});
Map<String, Object> req = new HashMap<>(); Map<String, Object> req = new HashMap<>();
req.put("action", "requestContractExecutionServer"); req.put("action", "requestContractExecutionServer");
req.put("requestID", c.getRequestID()); req.put("requestID", c.getRequestID());
@ -93,8 +89,8 @@ public class AgentManager implements AgentPeerManagerIntf {
@Override @Override
public void transferToOtherNode(String pubKey, String contractID) { public void transferToOtherNode(String pubKey, String contractID) {
// TODO 问题1合约的ypk或者script怎么获取ypk如果是包含私有路径的可以在Contract中设置一个字段启动的时候写入吗 目前认为这个从前端传入 // TODO 问题1合约的ypk或者script怎么获取ypk如果是包含私有路径的可以在Contract中设置一个字段启动的时候写入吗 目前认为这个从前端传入
// TODO 问题2 转移过程中进度返回给哪个前端显示需要显示吗 // TODO 问题2 转移过程中进度返回给哪个前端显示需要显示吗
LOGGER.info("transferToOtherNode : pubKey=" + pubKey + " contractID=" + contractID); LOGGER.info("transferToOtherNode : pubKey=" + pubKey + " contractID=" + contractID);
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID); ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
MasterClientTransferAction.instance.transferInstance(pubKey, meta.getID()); MasterClientTransferAction.instance.transferInstance(pubKey, meta.getID());

View File

@ -9,7 +9,7 @@ import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class KillUnitContractResultCollector extends ResultCallback { 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; int total;
String requestID; String requestID;
AtomicInteger count = new AtomicInteger(0); AtomicInteger count = new AtomicInteger(0);
@ -24,9 +24,9 @@ public class KillUnitContractResultCollector extends ResultCallback {
@Override @Override
public void onResult(String str) { public void onResult(String str) {
//解析str判断该节点是否kill成功 // 解析str判断该节点是否kill成功
Map<String, String> map = JsonUtil.fromJson(str, new TypeToken<Map<String, String>>() { Map<String, String> map =
}.getType()); JsonUtil.fromJson(str, new TypeToken<Map<String, String>>() {}.getType());
String data = map.get("data"); String data = map.get("data");
if (data != null && data.equals("success")) { if (data != null && data.equals("success")) {
// //

View File

@ -5,9 +5,9 @@ import org.bdware.sc.bean.ContractExecType;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
//在master上 // 在master上
public class MultiPointContractInfo { public class MultiPointContractInfo {
public List<String> members; //pubKey public List<String> members; // pubKey
public String masterNode; public String masterNode;
public ContractExecType type; public ContractExecType type;
public ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode; public ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode;

View File

@ -5,10 +5,10 @@ import org.bdware.sc.bean.ContractExecType;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
//改名 MultiPointCooperateContractInfo // 改名 MultiPointCooperateContractInfo
public class MultiPointCooperateContractInfo extends MultiPointContractInfo { public class MultiPointCooperateContractInfo extends MultiPointContractInfo {
public List<String> members; //pubKey public List<String> members; // pubKey
public List<Integer> shardingId; //sharding id 初始化时被分配 public List<Integer> shardingId; // sharding id 初始化时被分配
public String masterNode; public String masterNode;
public ContractExecType type; public ContractExecType type;
public ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode; public ContractUnitStatus unitStatus = ContractUnitStatus.CommonMode;

View File

@ -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();
}
}

View File

@ -6,7 +6,7 @@ import org.bdware.server.action.p2p.MasterServerTCPAction;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class ResultCollector extends ResultCallback { 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; int total;
String requestID; String requestID;
AtomicInteger count = new AtomicInteger(0); AtomicInteger count = new AtomicInteger(0);

View File

@ -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;
}
}
}

View File

@ -29,7 +29,7 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
public FileActions fileAction; public FileActions fileAction;
CMActions cmAction; CMActions cmAction;
//TemporyTestAction temAction; // TemporyTestAction temAction;
StringBuilder dataCache = new StringBuilder(); StringBuilder dataCache = new StringBuilder();
// TODO optimize ae-> too much method? // TODO optimize ae-> too much method?
@ -45,60 +45,41 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
public void initActionExecutor() { public void initActionExecutor() {
CongestionControl.wsCounter.getAndIncrement(); CongestionControl.wsCounter.getAndIncrement();
cmAction = new CMActions(this); cmAction = new CMActions(this);
//temAction = new TemporyTestAction(); // temAction = new TemporyTestAction();
fileAction = new FileActions(this); fileAction = new FileActions(this);
userManagerAction = new UserManagerAction(this); userManagerAction = new UserManagerAction(this);
ae = ae = new ActionExecutor<ResultCallback, JsonObject>(executorService, cmAction,
new ActionExecutor<ResultCallback, JsonObject>( // temAction,
executorService, fileAction, ManagerActions.instance, userManagerAction, new MasterWSAction(this), // 多节点执行
cmAction, new CMLogAction(), new ProcessAction(), GRPCPool.instance, new EventWSActions()) {
//temAction, @Override
fileAction, public boolean checkPermission(Action a, JsonObject arg, long permission) {
ManagerActions.instance, long val = a.userPermission();
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();
boolean flag; boolean flag;
String status = "refuse"; String status = "refuse";
String action = arg.get("action").getAsString(); String action = arg.get("action").getAsString();
if (val == 0) { if (val == 0) {
flag = true; flag = true;
status = "accept"; status = "accept";
} else if ((permission & val) == val) { } else if ((permission & val) == val) {
flag = true; flag = true;
status = "accept"; status = "accept";
} else { } else {
flag = false; flag = false;
} }
LOGGER.debug( LOGGER.debug(String.format("%s val:%d permission:%d status:%s", action, val,
String.format( permission, status));
"%s val:%d permission:%d status:%s",
action,
val,
permission,
status));
CMHttpServer.nodeLogDB.put( CMHttpServer.nodeLogDB.put(action, String.format(
action, "{\"action\":\"%s\",\"pubKey\":\"%s\",\"status\":\"%s\",\"date\":%d}",
String.format( action, userManagerAction.getPubKey(), status, System.currentTimeMillis()));
"{\"action\":\"%s\",\"pubKey\":\"%s\",\"status\":\"%s\",\"date\":%d}", // TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(),
action, // sb.toString());
userManagerAction.getPubKey(), return flag;
status, }
System.currentTimeMillis())); };
// TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(),
// sb.toString());
return flag;
}
};
for (String str : CMHttpHandler.wsPluginActions) { for (String str : CMHttpHandler.wsPluginActions) {
Object obj = createInstanceByClzName(str); Object obj = createInstanceByClzName(str);
ae.appendHandler(obj); ae.appendHandler(obj);
@ -156,8 +137,7 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
dataCache.append(map.get("data").getAsString()); dataCache.append(map.get("data").getAsString());
response = new Response(); response = new Response();
response.action = "sendNextSegment"; response.action = "sendNextSegment";
ctx.channel() ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response)));
.writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response)));
return; return;
} else { } else {
if (dataCache.length() > 0) { if (dataCache.length() > 0) {
@ -170,41 +150,39 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
} }
String action = map.get("action").getAsString(); String action = map.get("action").getAsString();
map.remove("verifiedPubKey"); map.remove("verifiedPubKey");
if (ae == null) initActionExecutor(); if (ae == null)
initActionExecutor();
if (userManagerAction.getPubKey() != null if (userManagerAction.getPubKey() != null
&& userManagerAction.getPubKey().length() > 0) { && userManagerAction.getPubKey().length() > 0) {
map.addProperty("verifiedPubKey", userManagerAction.getPubKey()); map.addProperty("verifiedPubKey", userManagerAction.getPubKey());
} }
final JsonObject jmap = map; final JsonObject jmap = map;
ae.handle( ae.handle(action, map, new ResultCallback(ctx.channel()) {
action, @Override
map, public void onResult(Map jo) {
new ResultCallback(ctx.channel()) { if (jmap.has("requestID")) {
@Override jo.put("responseID", jmap.get("requestID").getAsString());
public void onResult(Map jo) { }
if (jmap.has("requestID")) { onResult(JsonUtil.toJson(jo));
jo.put("responseID", jmap.get("requestID").getAsString()); }
}
onResult(JsonUtil.toJson(jo));
}
@Override @Override
public void onResult(JsonObject jo) { public void onResult(JsonObject jo) {
if (jmap.has("requestID")) { if (jmap.has("requestID")) {
jo.add("responseID", jmap.get("requestID")); jo.add("responseID", jmap.get("requestID"));
} }
onResult(jo.toString()); onResult(jo.toString());
} }
@Override @Override
public void onResult(String ret) { public void onResult(String ret) {
if (ret != null) { if (ret != null) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(ret)); ctx.channel().writeAndFlush(new TextWebSocketFrame(ret));
} }
} }
}); });
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
response = new Response(); response = new Response();
response.action = "onException"; response.action = "onException";
@ -239,7 +217,8 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler<Web
} }
ret.append(s); ret.append(s);
ret.append("\n"); ret.append("\n");
if (count++ > 5) break; if (count++ > 5)
break;
} }
response.data = ret.toString(); response.data = ret.toString();
ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response))); ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response)));

View File

@ -16,26 +16,30 @@ import org.apache.logging.log4j.Logger;
/** /**
* An HTTP server which serves Web Socket requests at: * 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 * <p>
* demo page will be loaded and a Web Socket connection will be made automatically. * 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 * <p>
* work with: * This server illustrates support for the different web socket specification versions and will work
* with:
* *
* <ul> * <ul>
* <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00) * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
* <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00) * <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
* <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10) * <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
* <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17) * <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
* <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10) * <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
* <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17) * <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
* </ul> * </ul>
*/ */
public final class WebSocketServer { public final class WebSocketServer {
private static final boolean SSL = System.getProperty("ssl") != null; 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); private static final Logger LOGGER = LogManager.getLogger(WebSocketServer.class);
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
@ -52,19 +56,14 @@ public final class WebSocketServer {
EventLoopGroup workerGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup();
try { try {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO)) .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new WebSocketServerInitializer(sslCtx)); .childHandler(new WebSocketServerInitializer(sslCtx));
Channel ch = b.bind(PORT).sync().channel(); Channel ch = b.bind(PORT).sync().channel();
LOGGER.info( LOGGER.info("Open your web browser and navigate to " + (SSL ? "https" : "http")
"Open your web browser and navigate to " + "://127.0.0.1:" + PORT + '/');
+ (SSL ? "https" : "http")
+ "://127.0.0.1:"
+ PORT
+ '/');
ch.closeFuture().sync(); ch.closeFuture().sync();
} finally { } finally {

View File

@ -13,25 +13,25 @@ import io.netty.handler.ssl.SslContext;
*/ */
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> { 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) { public WebSocketServerInitializer(SslContext sslCtx) {
this.sslCtx = sslCtx; this.sslCtx = sslCtx;
} }
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) { if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc())); pipeline.addLast(sslCtx.newHandler(ch.alloc()));
} }
pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerCompressionHandler()); pipeline.addLast(new WebSocketServerCompressionHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true)); pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH)); pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
pipeline.addLast(new ContractManagerFrameHandler()); pipeline.addLast(new ContractManagerFrameHandler());
} }
} }

View File

@ -41,10 +41,11 @@ import java.util.concurrent.TimeUnit;
* @author OliveDS (Shuang Deng) * @author OliveDS (Shuang Deng)
*/ */
public class NetworkManager { public class NetworkManager {
//Manage server->client connection; // Manage server->client connection;
public static final Map<String, AgentConnector> CONNECTORS = new ConcurrentHashMap<>(); public static final Map<String, AgentConnector> CONNECTORS = new ConcurrentHashMap<>();
//Manage client->server connection; // Manage client->server connection;
public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS = new ConcurrentHashMap<>(); public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS =
new ConcurrentHashMap<>();
public static final String NODE_CENTER_CLIENT = "NODE_CENTER_CLIENT"; public static final String NODE_CENTER_CLIENT = "NODE_CENTER_CLIENT";
public static final String P2P_GRPC_CLIENT = "P2P_GRPC_CLIENT"; public static final String P2P_GRPC_CLIENT = "P2P_GRPC_CLIENT";
private static final Map<String, String> slaverRouter = new HashMap<>(); private static final Map<String, String> slaverRouter = new HashMap<>();
@ -59,18 +60,13 @@ public class NetworkManager {
} }
public static void reconnectAgent(String master) { public static void reconnectAgent(String master) {
LOGGER.debug( LOGGER.debug(String.format("master=%s\t%s", master, JsonUtil.toJson(slaverRouter)));
String.format("master=%s\t%s",
master,
JsonUtil.toJson(slaverRouter)));
try { try {
NetworkManager.AgentConnector conn; NetworkManager.AgentConnector conn;
synchronized (conn = NetworkManager.CONNECTORS.get(master)) { synchronized (conn = NetworkManager.CONNECTORS.get(master)) {
if (!conn.handler.isOpen()) { if (!conn.handler.isOpen()) {
String[] ipAndPort = slaverRouter.get(master).split(":"); String[] ipAndPort = slaverRouter.get(master).split(":");
conn.bootstrap conn.bootstrap.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])).sync()
.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1]))
.sync()
.channel(); .channel();
} }
} }
@ -94,46 +90,36 @@ public class NetworkManager {
nodeCenterClientHandler = ControllerManager.createNodeCenterClientHandler(); nodeCenterClientHandler = ControllerManager.createNodeCenterClientHandler();
EventLoopGroup group = new NioEventLoopGroup(); EventLoopGroup group = new NioEventLoopGroup();
b.group(group); b.group(group);
b.channel(NioSocketChannel.class) b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
.handler( @Override
new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) {
@Override ChannelPipeline p = ch.pipeline();
protected void initChannel(SocketChannel ch) { p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler);
ChannelPipeline p = ch.pipeline(); }
p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler); });
} ContractManager.scheduledThreadPool.scheduleWithFixedDelay(() -> {
}); try {
ContractManager.scheduledThreadPool.scheduleWithFixedDelay( // manager.clearCache();
() -> { String URL = GlobalConf.getNodeCenterUrl();
try { // LOGGER.debug("GlobalConf.getNodeCenterUrl() -> URL=" + URL);
// manager.clearCache(); URI uri = null;
String URL = GlobalConf.getNodeCenterUrl(); try {
// LOGGER.debug("GlobalConf.getNodeCenterUrl() -> URL=" + URL); uri = new URI(URL);
URI uri = null; } catch (URISyntaxException e) {
try { LOGGER.error("creating uri failed! " + e.getMessage());
uri = new URI(URL); }
} catch (URISyntaxException e) { if (!nodeCenterClientHandler.isConnected()
LOGGER.error("creating uri failed! " + e.getMessage()); || !ControllerManager.getNodeCenterController().syncPing()) {
} nodeCenterClientHandler.close();
if (!nodeCenterClientHandler.isConnected() assert null != uri;
|| !ControllerManager.getNodeCenterController().syncPing()) { b.connect(uri.getHost(), uri.getPort()).sync().channel();
nodeCenterClientHandler.close(); LOGGER.info("connect to node center: " + uri.getHost() + ":" + uri.getPort());
assert null != uri; }
b.connect(uri.getHost(), uri.getPort()).sync().channel(); } catch (Exception e) {
LOGGER.info( // e.printStackTrace();
"connect to node center: " // LOGGER.warn("connecting to node center failed! " + e.getMessage());
+ uri.getHost() }
+ ":" }, 4, 30 + (int) (20 * Math.random()), TimeUnit.SECONDS);
+ uri.getPort());
}
} catch (Exception e) {
// e.printStackTrace();
LOGGER.warn("connecting to node center failed! " + e.getMessage());
}
},
0,
10,
TimeUnit.SECONDS);
} }
@ -142,20 +128,16 @@ public class NetworkManager {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup bossGroup = new NioEventLoopGroup(1);
b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b.group(bossGroup, workerGroup) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).localAddress(port)
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(port)
.childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler( .childHandler(new ChannelInitializer<SocketChannel>() {
new ChannelInitializer<SocketChannel>() { @Override
@Override protected void initChannel(SocketChannel arg0) {
protected void initChannel(SocketChannel arg0) { arg0.pipeline().addLast(new DelimiterCodec())
arg0.pipeline() .addLast(new TCPServerFrameHandler());
.addLast(new DelimiterCodec()) }
.addLast(new TCPServerFrameHandler()); });
}
});
b.bind(port).sync().channel(); b.bind(port).sync().channel();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -164,7 +146,7 @@ public class NetworkManager {
} }
//TODO Remove in future // TODO Remove in future
public void sendToNodeCenter(String msg) { public void sendToNodeCenter(String msg) {
nodeCenterClientHandler.sendMsg(msg); nodeCenterClientHandler.sendMsg(msg);
} }
@ -174,9 +156,8 @@ public class NetworkManager {
} }
public void waitForNodeCenterConnected() { public void waitForNodeCenterConnected() {
for (int i = 0; for (int i = 0; i < 10 && null != nodeCenterClientHandler
i < 10 && null != nodeCenterClientHandler && !nodeCenterClientHandler.isConnected(); && !nodeCenterClientHandler.isConnected(); i++) {
i++) {
try { try {
Thread.sleep(200); Thread.sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -191,7 +172,7 @@ public class NetworkManager {
} }
} }
//----------AgentNetworkManagement // ----------AgentNetworkManagement
public void updateAgentRouter(String nodeID, String address) { public void updateAgentRouter(String nodeID, String address) {
@ -217,7 +198,6 @@ public class NetworkManager {
public void connectToAgent(String master, String contractID) { public void connectToAgent(String master, String contractID) {
LOGGER.info("master=" + master); LOGGER.info("master=" + master);
// LOGGER.debug("master=" + master + "\n\trouter=" + slaverRouter.get(master));
Bootstrap b; Bootstrap b;
AgentConnector connector = null; AgentConnector connector = null;
synchronized (CONNECTORS) { synchronized (CONNECTORS) {
@ -234,15 +214,13 @@ public class NetworkManager {
connector.handler = handler; connector.handler = handler;
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
b.group(CMHttpServer.workerGroup); b.group(CMHttpServer.workerGroup);
b.channel(NioSocketChannel.class) b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
.handler( @Override
new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) {
@Override ChannelPipeline p = ch.pipeline();
protected void initChannel(SocketChannel ch) { p.addLast(new DelimiterCodec()).addLast(handler);
ChannelPipeline p = ch.pipeline(); }
p.addLast(new DelimiterCodec()).addLast(handler); });
}
});
} }
reconnectAgent(master); reconnectAgent(master);
} }
@ -293,8 +271,8 @@ public class NetworkManager {
return CONNECTORS.containsKey(pubKey); return CONNECTORS.containsKey(pubKey);
} }
//-------UNUSED TOMerge------------ // -------UNUSED TOMerge------------
//UNUSED // UNUSED
public TCPClientFrameHandler createTCPClient(String peer, String ipPort) public TCPClientFrameHandler createTCPClient(String peer, String ipPort)
throws InterruptedException { throws InterruptedException {
if (peer.equals(GlobalConf.instance.peerID)) { if (peer.equals(GlobalConf.instance.peerID)) {
@ -307,30 +285,28 @@ public class NetworkManager {
NioEventLoopGroup group = new NioEventLoopGroup(); NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap(); Bootstrap b = new Bootstrap();
final TCPClientFrameHandler handler = new TCPClientFrameHandler(peer); final TCPClientFrameHandler handler = new TCPClientFrameHandler(peer);
//tcpClientMap.put(peer, handler); // tcpClientMap.put(peer, handler);
b.group(group) b.group(group).channel(NioSocketChannel.class)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port)) .remoteAddress(new InetSocketAddress(host, port))
.option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.TCP_NODELAY, true)
.handler( .handler(new ChannelInitializer<SocketChannel>() {
new ChannelInitializer<SocketChannel>() { @Override
@Override public void initChannel(SocketChannel ch) throws Exception {
public void initChannel(SocketChannel ch) throws Exception { ch.pipeline()
ch.pipeline() // .addLast(new
// .addLast(new // IdleStateHandler(0, 4, 0, TimeUnit.SECONDS))
// IdleStateHandler(0, 4, 0, TimeUnit.SECONDS)) .addLast(new DelimiterCodec())
.addLast(new DelimiterCodec()) .addLast(new IdleStateHandler(0, 5, 0))
.addLast(new IdleStateHandler(0, 5, 0)) // .addLast(new
// .addLast(new // HeartBeatEncode())
// HeartBeatEncode()) .addLast(handler);
.addLast(handler); }
} });
});
ChannelFuture f = b.connect().sync(); ChannelFuture f = b.connect().sync();
if (f.isSuccess()) { if (f.isSuccess()) {
LOGGER.info("TCP Client " + peer + " started"); LOGGER.info("TCP Client " + peer + " started");
} }
// channel = (SocketChannel) future.channel(); // channel = (SocketChannel) future.channel();
return handler; 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 * @param msg unit message
*/ */
public void send(UnitMessage msg) { public void send(UnitMessage msg) {
Iterator<String> iterator = msg.getReceiverList().iterator(); Iterator<String> iterator = msg.getReceiverList().iterator();
String peer; String peer;
// boolean sendByP2P = false; // boolean sendByP2P = false;
TCPClientFrameHandler tcpClientFrameHandler; TCPClientFrameHandler tcpClientFrameHandler;
while (iterator.hasNext()) { while (iterator.hasNext()) {
peer = iterator.next(); peer = iterator.next();
@ -369,17 +345,16 @@ public class NetworkManager {
LOGGER.info("send msg to itself " + msg); LOGGER.info("send msg to itself " + msg);
continue; continue;
} }
// tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer, null); // tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer,
// null);
if (peerID2TCPAddress.containsKey(peer)) { if (peerID2TCPAddress.containsKey(peer)) {
//recreateTCPClient(peer); // recreateTCPClient(peer);
// instance.tcpClientMap.put(peer, tcpClientFrameHandler); // instance.tcpClientMap.put(peer, tcpClientFrameHandler);
UnitMessage unitMessage = UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
msg.toBuilder().clearReceiver().addReceiver(peer).build();
LOGGER.info("send msg by p2p to " + peer); LOGGER.info("send msg by p2p to " + peer);
JavaContractServiceGrpcServer.sendMsg(unitMessage); JavaContractServiceGrpcServer.sendMsg(unitMessage);
} else { } else {
UnitMessage unitMessage = UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
msg.toBuilder().clearReceiver().addReceiver(peer).build();
LOGGER.info("send msg by p2p to " + peer); LOGGER.info("send msg by p2p to " + peer);
JavaContractServiceGrpcServer.sendMsg(unitMessage); JavaContractServiceGrpcServer.sendMsg(unitMessage);
} }

View File

@ -1,6 +1,5 @@
package org.bdware.units.beans; package org.bdware.units.beans;
import org.bdware.sc.bean.ContractExecType;
import org.bdware.units.enums.ConsensusType; import org.bdware.units.enums.ConsensusType;
import org.bdware.units.enums.NetworkType; import org.bdware.units.enums.NetworkType;
import org.bdware.units.enums.ResponseType; import org.bdware.units.enums.ResponseType;
@ -11,29 +10,33 @@ public class MultiPointContractInfo {
public Set<String> members; public Set<String> members;
public Map<String, String> member2PubKey; public Map<String, String> member2PubKey;
public String master; public String master;
// public ContractType contractType; // public ContractType contractType;
public String contractId; public String contractId;
public ConsensusType consensusType; public ConsensusType consensusType;
public ResponseType responseType; public ResponseType responseType;
public NetworkType networkType; public NetworkType networkType;
public MultiPointContractInfo(){ public MultiPointContractInfo() {
// member2PubKey = new HashMap<>(10); // member2PubKey = new HashMap<>(10);
// members = new HashSet<>(10); // members = new HashSet<>(10);
} }
public Set<String> getMembers() { public Set<String> getMembers() {
return members; return members;
} }
public String getContractId() { public String getContractId() {
return contractId; return contractId;
} }
public String getMaster() { public String getMaster() {
return master; return master;
} }
public boolean hasPeer(String peerID) { public boolean hasPeer(String peerID) {
return members.contains(peerID); return members.contains(peerID);
} }
public String getPubKey(String peerID) { public String getPubKey(String peerID) {
return member2PubKey.get(peerID); return member2PubKey.get(peerID);

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
/** /**
* ExecutionManager内部消息封装在UnitMessage中作为content * ExecutionManager内部消息封装在UnitMessage中作为content
*
* @author oliveds * @author oliveds
*/ */
public class UnitContractMessage implements Serializable { public class UnitContractMessage implements Serializable {

View File

@ -8,7 +8,7 @@ public interface ConsensusCenter {
void handle(UnitSequencingMessage unitSequencingMessage); void handle(UnitSequencingMessage unitSequencingMessage);
// void updateMaster(String master); // void updateMaster(String master);
// UnitSequencingMessage formConsensusRequest(UnitContractMessage msg); // UnitSequencingMessage formConsensusRequest(UnitContractMessage msg);
} }

View File

@ -23,8 +23,8 @@ public class PBFTCenter implements ConsensusCenter {
MultiPointContractInfo contractInfo; MultiPointContractInfo contractInfo;
Map<Long, PBFTInfo> infos; Map<Long, PBFTInfo> infos;
Map<Long, UnitContractMessage> committedMsg; Map<Long, UnitContractMessage> committedMsg;
// Map<String, PBFTInfo> original; // Map<String, PBFTInfo> original;
// String master; // String master;
boolean isMaster; boolean isMaster;
private String contractID; private String contractID;
private NetworkType networkType; private NetworkType networkType;
@ -36,12 +36,12 @@ public class PBFTCenter implements ConsensusCenter {
networkType = NetworkType.getType(msg.network); networkType = NetworkType.getType(msg.network);
infos = new HashMap<>(); infos = new HashMap<>();
committedMsg = new HashMap<>(); committedMsg = new HashMap<>();
// original = new HashMap<>(); // original = new HashMap<>();
// receive init, then insert // receive init, then insert
// members = ContractUnitManager.instance.getContractInfo(contractID); // members = ContractUnitManager.instance.getContractInfo(contractID);
// if (members.master.equals(GlobalConf.instance.peerID)) { // if (members.master.equals(GlobalConf.instance.peerID)) {
// isMaster = true; // isMaster = true;
// } // }
} }
@Override @Override
@ -59,32 +59,33 @@ public class PBFTCenter implements ConsensusCenter {
PBFTInfo temp; PBFTInfo temp;
switch (ConsensusStage.fromByte(unitSequencingMessage.stageType)) { switch (ConsensusStage.fromByte(unitSequencingMessage.stageType)) {
case Init: case Init:
MultiPointContractInfo contractInfo = MultiPointContractInfo contractInfo = JsonUtil
JsonUtil.fromJson(unitSequencingMessage.content, MultiPointContractInfo.class); .fromJson(unitSequencingMessage.content, MultiPointContractInfo.class);
updateUnitInfo(contractInfo); updateUnitInfo(contractInfo);
break; break;
case AddMember: case AddMember:
// PBFTMember member = PBFTMember.parse(pbftMessage.content); // PBFTMember member = PBFTMember.parse(pbftMessage.content);
// if (member != null) // if (member != null)
// members.put(sender, member); // members.put(sender, member);
// case DeleteMember: // case DeleteMember:
break; break;
case Request: case Request:
// master form preprepare, full content // master form preprepare, full content
// int hash = unitSequencingMessage.content.hashCode(); // int hash = unitSequencingMessage.content.hashCode();
// original.put(hash, new Pair<Node, PBFTMessage>(sender, pbftMessage)); // original.put(hash, new Pair<Node, PBFTMessage>(sender, pbftMessage));
if (isMaster) { if (isMaster) {
temp = new PBFTInfo(); temp = new PBFTInfo();
temp.order = allocatedID.incrementAndGet(); temp.order = allocatedID.incrementAndGet();
temp.request = unitSequencingMessage.content; temp.request = unitSequencingMessage.content;
infos.put(temp.order, temp); infos.put(temp.order, temp);
// original.put(unitSequencingMessage.requestID, temp); // original.put(unitSequencingMessage.requestID, temp);
unitSequencingMessage.stageType = PBFTType.PrePrepare.toInt(); unitSequencingMessage.stageType = PBFTType.PrePrepare.toInt();
unitSequencingMessage.order = temp.order; unitSequencingMessage.order = temp.order;
// unitSequencingMessage.content = unitSequencingMessage.content.hashCode() + ""; // unitSequencingMessage.content = unitSequencingMessage.content.hashCode() +
// "";
broadcast(unitSequencingMessage); broadcast(unitSequencingMessage);
} else { } else {
// sendToMaster(unitSequencingMessage); // sendToMaster(unitSequencingMessage);
} }
break; break;
case PrePrepare: case PrePrepare:
@ -111,7 +112,7 @@ public class PBFTCenter implements ConsensusCenter {
} }
if (temp.updatePrepare(unitSequencingMessage, this)) { if (temp.updatePrepare(unitSequencingMessage, this)) {
UnitSequencingMessage commitMsg = temp.formCommit(unitSequencingMessage); UnitSequencingMessage commitMsg = temp.formCommit(unitSequencingMessage);
// temp.updateCommit(commitMsg, this); // temp.updateCommit(commitMsg, this);
broadcast(commitMsg); broadcast(commitMsg);
} }
break; break;
@ -138,7 +139,7 @@ public class PBFTCenter implements ConsensusCenter {
unitSequencingMessage.stageType = ConsensusStage.PrePrepare.toInt(); unitSequencingMessage.stageType = ConsensusStage.PrePrepare.toInt();
unitSequencingMessage.sender = GlobalConf.instance.peerID; unitSequencingMessage.sender = GlobalConf.instance.peerID;
String content = JsonUtil.toJson(unitSequencingMessage); String content = JsonUtil.toJson(unitSequencingMessage);
SequencingManager.instance.send(content, new String[]{peer}); SequencingManager.instance.send(content, new String[] {peer});
} }
break; break;
default: default:
@ -152,7 +153,8 @@ public class PBFTCenter implements ConsensusCenter {
} }
private void broadcast(UnitSequencingMessage msg) { 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 { class PBFTInfo {
@ -175,7 +177,8 @@ public class PBFTCenter implements ConsensusCenter {
isPrePrepareReceived = false; isPrePrepareReceived = false;
} }
public synchronized boolean updatePrepare(UnitSequencingMessage message, PBFTCenter center) { public synchronized boolean updatePrepare(UnitSequencingMessage message,
PBFTCenter center) {
if (isSendCommit) { if (isSendCommit) {
return false; return false;
} }
@ -209,18 +212,15 @@ public class PBFTCenter implements ConsensusCenter {
} }
public String getDisplayStr() { public String getDisplayStr() {
return String.format( return String.format("pSize=%d cSize=%d isSendCommit=%b isSendReply=%b buffSize=%d",
"pSize=%d cSize=%d isSendCommit=%b isSendReply=%b buffSize=%d", null == prepare ? -1 : prepare.size(), null == commit ? -1 : commit.size(),
null == prepare ? -1 : prepare.size(), isSendCommit, isSendReply, buff.size());
null == commit ? -1 : commit.size(),
isSendCommit,
isSendReply,
buff.size());
} }
public UnitSequencingMessage formPrepare(UnitSequencingMessage unitSequencingMessage) { public UnitSequencingMessage formPrepare(UnitSequencingMessage unitSequencingMessage) {
// UnitSequencingMessage prepareMsg = new UnitSequencingMessage(ConsensusStage.Prepare.toInt()); // UnitSequencingMessage prepareMsg = new
// prepareMsg.order = this.order; // UnitSequencingMessage(ConsensusStage.Prepare.toInt());
// prepareMsg.order = this.order;
unitSequencingMessage.content = String.valueOf(this.request.hashCode()); unitSequencingMessage.content = String.valueOf(this.request.hashCode());
unitSequencingMessage.sender = GlobalConf.instance.peerID; unitSequencingMessage.sender = GlobalConf.instance.peerID;
unitSequencingMessage.stageType = ConsensusStage.Prepare.toInt(); unitSequencingMessage.stageType = ConsensusStage.Prepare.toInt();
@ -228,17 +228,19 @@ public class PBFTCenter implements ConsensusCenter {
} }
public void requestPrePrepareFromMaster() { public void requestPrePrepareFromMaster() {
UnitSequencingMessage requestMsg = new UnitSequencingMessage(ConsensusStage.ReSend.toInt()); UnitSequencingMessage requestMsg =
new UnitSequencingMessage(ConsensusStage.ReSend.toInt());
requestMsg.order = this.order; requestMsg.order = this.order;
requestMsg.sender = GlobalConf.instance.peerID; requestMsg.sender = GlobalConf.instance.peerID;
// return requestMsg; // return requestMsg;
String content = JsonUtil.toJson(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) { public UnitSequencingMessage formCommit(UnitSequencingMessage unitSequencingMessage) {
// UnitSequencingMessage commitMsg = new UnitSequencingMessage(ConsensusStage.Commit.toInt()); // UnitSequencingMessage commitMsg = new
// unitSequencingMessage.order = this.order; // UnitSequencingMessage(ConsensusStage.Commit.toInt());
// unitSequencingMessage.order = this.order;
unitSequencingMessage.content = String.valueOf(this.request.hashCode()); unitSequencingMessage.content = String.valueOf(this.request.hashCode());
unitSequencingMessage.sender = GlobalConf.instance.peerID; unitSequencingMessage.sender = GlobalConf.instance.peerID;
unitSequencingMessage.stageType = ConsensusStage.Commit.toInt(); unitSequencingMessage.stageType = ConsensusStage.Commit.toInt();
@ -246,7 +248,8 @@ public class PBFTCenter implements ConsensusCenter {
} }
public void onCommit() { public void onCommit() {
UnitContractMessage unitContractMessage = JsonUtil.fromJson(request, UnitContractMessage.class); UnitContractMessage unitContractMessage =
JsonUtil.fromJson(request, UnitContractMessage.class);
if (this.order == (committedID.get() + 1L)) { if (this.order == (committedID.get() + 1L)) {
execute(unitContractMessage, this.order); execute(unitContractMessage, this.order);
} else { } else {

View File

@ -10,8 +10,7 @@ public class RAFTCenter implements ConsensusCenter {
private static ConsensusType PBFT; private static ConsensusType PBFT;
private String contractID; private String contractID;
public RAFTCenter(UnitContractMessage msg) { public RAFTCenter(UnitContractMessage msg) {}
}
@Override @Override
public void updateUnitInfo(MultiPointContractInfo contractInfo) { public void updateUnitInfo(MultiPointContractInfo contractInfo) {

View File

@ -35,12 +35,12 @@ public class SingleCenter implements ConsensusCenter {
networkType = NetworkType.getType(msg.network); networkType = NetworkType.getType(msg.network);
infos = new HashMap<>(); infos = new HashMap<>();
committedMsg = new HashMap<>(); committedMsg = new HashMap<>();
// original = new HashMap<>(); // original = new HashMap<>();
// receive init, then insert // receive init, then insert
// members = ContractUnitManager.instance.getContractInfo(contractID); // members = ContractUnitManager.instance.getContractInfo(contractID);
// if (members.master.equals(GlobalConf.instance.peerID)) { // if (members.master.equals(GlobalConf.instance.peerID)) {
// isMaster = true; // isMaster = true;
// } // }
} }
@Override @Override
@ -58,8 +58,8 @@ public class SingleCenter implements ConsensusCenter {
SingleInfo temp; SingleInfo temp;
switch (ConsensusStage.fromByte(unitSequencingMessage.stageType)) { switch (ConsensusStage.fromByte(unitSequencingMessage.stageType)) {
case Init: case Init:
MultiPointContractInfo contractInfo = MultiPointContractInfo contractInfo = JsonUtil
JsonUtil.fromJson(unitSequencingMessage.content, MultiPointContractInfo.class); .fromJson(unitSequencingMessage.content, MultiPointContractInfo.class);
updateUnitInfo(contractInfo); updateUnitInfo(contractInfo);
break; break;
case Request: case Request:
@ -72,7 +72,7 @@ public class SingleCenter implements ConsensusCenter {
unitSequencingMessage.order = temp.order; unitSequencingMessage.order = temp.order;
broadcast(unitSequencingMessage); broadcast(unitSequencingMessage);
} else { } else {
// sendToMaster(unitSequencingMessage); // sendToMaster(unitSequencingMessage);
} }
break; break;
case Commit: case Commit:
@ -93,7 +93,7 @@ public class SingleCenter implements ConsensusCenter {
unitSequencingMessage.stageType = ConsensusStage.PrePrepare.toInt(); unitSequencingMessage.stageType = ConsensusStage.PrePrepare.toInt();
unitSequencingMessage.sender = GlobalConf.instance.peerID; unitSequencingMessage.sender = GlobalConf.instance.peerID;
String content = JsonUtil.toJson(unitSequencingMessage); String content = JsonUtil.toJson(unitSequencingMessage);
SequencingManager.instance.send(content, new String[]{peer}); SequencingManager.instance.send(content, new String[] {peer});
} }
break; break;
default: default:
@ -102,8 +102,7 @@ public class SingleCenter implements ConsensusCenter {
} }
private void broadcast(UnitSequencingMessage msg) { private void broadcast(UnitSequencingMessage msg) {
SequencingManager.instance.send( SequencingManager.instance.send(JsonUtil.toJson(msg),
JsonUtil.toJson(msg),
contractInfo.getMembers().toArray(new String[contractInfo.members.size()])); contractInfo.getMembers().toArray(new String[contractInfo.members.size()]));
} }
@ -116,7 +115,8 @@ public class SingleCenter implements ConsensusCenter {
isSendReply = false; isSendReply = false;
} }
public synchronized boolean updateCommit(UnitSequencingMessage message, SingleCenter center) { public synchronized boolean updateCommit(UnitSequencingMessage message,
SingleCenter center) {
if (isSendReply) { if (isSendReply) {
return false; return false;
} }
@ -126,7 +126,8 @@ public class SingleCenter implements ConsensusCenter {
} }
public void onCommit() { public void onCommit() {
UnitContractMessage unitContractMessage = JsonUtil.fromJson(request, UnitContractMessage.class); UnitContractMessage unitContractMessage =
JsonUtil.fromJson(request, UnitContractMessage.class);
if (this.order == (committedID.get() + 1L)) { if (this.order == (committedID.get() + 1L)) {
execute(unitContractMessage, this.order); execute(unitContractMessage, this.order);
} else { } else {

View File

@ -1,7 +1,8 @@
package org.bdware.units.enums; package org.bdware.units.enums;
public enum ConsensusStage { 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; private int type;

View File

@ -1,12 +1,8 @@
package org.bdware.units.enums; package org.bdware.units.enums;
public enum UnitContractMessageType { public enum UnitContractMessageType {
ContractUnitRequest(0), ContractUnitRequest(0), ContractUnitResponse(1), GetMyPeerID(2), MyPeerIDResponse(
ContractUnitResponse (1), 3), ContractStatusRequest(4), ContractStatusResponse(5);
GetMyPeerID(2),
MyPeerIDResponse(3),
ContractStatusRequest(4),
ContractStatusResponse (5);
private final int value; private final int value;

View File

@ -1,8 +1,7 @@
package org.bdware.units.enums; package org.bdware.units.enums;
public enum UnitContractNetworkType { public enum UnitContractNetworkType {
TCP(0), TCP(0), P2P(1);
P2P (1);
private final int value; private final int value;

View File

@ -1,11 +1,7 @@
package org.bdware.units.enums; package org.bdware.units.enums;
public enum UnitContractRequestType { public enum UnitContractRequestType {
START(0), START(0), STOP(1), EXECUTE(2), REPLY(3), REQUEST(4);
STOP (1),
EXECUTE(2),
REPLY(3),
REQUEST(4);
private final int value; private final int value;

View File

@ -26,7 +26,8 @@ public class BaseFunctionManager {
public void send(String msg, String[] peers) { public void send(String msg, String[] peers) {
String requestId = getRequestId(); String requestId = getRequestId();
BDLedgerContract.UnitMessage unitMessage = this.addRequestIdAndGetMessage(requestId, msg, peers); BDLedgerContract.UnitMessage unitMessage =
this.addRequestIdAndGetMessage(requestId, msg, peers);
NetworkManager.instance.send(unitMessage); NetworkManager.instance.send(unitMessage);
} }
@ -39,7 +40,8 @@ public class BaseFunctionManager {
public void send(String msg, String[] peers, ResultCallback resultCallback) { public void send(String msg, String[] peers, ResultCallback resultCallback) {
String requestId = getRequestId(); String requestId = getRequestId();
BDLedgerContract.UnitMessage unitMessage = this.addRequestIdAndGetMessage(requestId, msg, peers); BDLedgerContract.UnitMessage unitMessage =
this.addRequestIdAndGetMessage(requestId, msg, peers);
NetworkManager.instance.send(unitMessage); NetworkManager.instance.send(unitMessage);
requestCallbacks.put(requestId, resultCallback); requestCallbacks.put(requestId, resultCallback);
} }
@ -58,10 +60,8 @@ public class BaseFunctionManager {
System.out.println("[BaseFunctionManager] handleResponse null"); System.out.println("[BaseFunctionManager] handleResponse null");
return; return;
} }
if (responseCenter.updateReply( if (responseCenter.updateReply(arg.get("content").getAsString(),
arg.get("content").getAsString(), arg.get("sender").getAsString(), arg.get("pubkey").getAsString())) {
arg.get("sender").getAsString(),
arg.get("pubkey").getAsString())) {
responseCenter.formReply(); responseCenter.formReply();
} }
} }
@ -74,12 +74,12 @@ public class BaseFunctionManager {
return UNIT_MESSAGE_TYPE; 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 jsonObject = (JsonObject) JsonParser.parseString(msg);
jsonObject.addProperty(REQUEST_ID, requestId); jsonObject.addProperty(REQUEST_ID, requestId);
BDLedgerContract.UnitMessage.Builder builder = BDLedgerContract.UnitMessage.newBuilder() BDLedgerContract.UnitMessage.Builder builder = BDLedgerContract.UnitMessage.newBuilder()
.setMsgType(getMessageType()) .setMsgType(getMessageType()).setSender(GlobalConf.instance.peerID)
.setSender(GlobalConf.instance.peerID)
.setContent(ByteString.copyFromUtf8(jsonObject.toString())); .setContent(ByteString.copyFromUtf8(jsonObject.toString()));
for (String peer : peers) { for (String peer : peers) {
builder.addReceiver(peer); builder.addReceiver(peer);
@ -87,16 +87,12 @@ public class BaseFunctionManager {
return builder.build(); return builder.build();
} }
private BDLedgerContract.UnitMessage addRequestIdAndGetMessage( private BDLedgerContract.UnitMessage addRequestIdAndGetMessage(String requestId, String msg,
String requestId, String[] peers, BDLedgerContract.UnitMessageType unitMessageType) {
String msg,
String[] peers,
BDLedgerContract.UnitMessageType unitMessageType) {
JsonObject jsonObject = (JsonObject) JsonParser.parseString(msg); JsonObject jsonObject = (JsonObject) JsonParser.parseString(msg);
jsonObject.addProperty(REQUEST_ID, requestId); jsonObject.addProperty(REQUEST_ID, requestId);
BDLedgerContract.UnitMessage.Builder builder = BDLedgerContract.UnitMessage.newBuilder() BDLedgerContract.UnitMessage.Builder builder = BDLedgerContract.UnitMessage.newBuilder()
.setMsgType(unitMessageType) .setMsgType(unitMessageType).setSender(GlobalConf.instance.peerID)
.setSender(GlobalConf.instance.peerID)
.setContent(ByteString.copyFromUtf8(jsonObject.toString())); .setContent(ByteString.copyFromUtf8(jsonObject.toString()));
for (String peer : peers) { for (String peer : peers) {
builder.addReceiver(peer); builder.addReceiver(peer);

View File

@ -34,9 +34,10 @@ public class CommunicationManager extends BaseFunctionManager {
private static final String MANAGER_SYMBOL = "COMM"; private static final String MANAGER_SYMBOL = "COMM";
private static final Logger LOGGER = LogManager.getLogger(CommunicationManager.class); private static final Logger LOGGER = LogManager.getLogger(CommunicationManager.class);
public static ExecutorService executorService = Executors.newFixedThreadPool(10); 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; 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 ActionExecutor<ResultCallback, JsonObject> ae;
private NodeCenterClientController nodeCenterClientController; private NodeCenterClientController nodeCenterClientController;
private String[] p2pPeers; private String[] p2pPeers;
@ -49,11 +50,8 @@ public class CommunicationManager extends BaseFunctionManager {
instance = new CommunicationManager(); instance = new CommunicationManager();
instance.nodeCenterClientController = ControllerManager.getNodeCenterController(); instance.nodeCenterClientController = ControllerManager.getNodeCenterController();
instance.communicationAction = new CommunicationAction(); instance.communicationAction = new CommunicationAction();
instance.ae = new ActionExecutor<ResultCallback, JsonObject>( instance.ae = new ActionExecutor<ResultCallback, JsonObject>(executorService,
executorService, instance.nodeCenterClientController, instance.communicationAction) {
instance.nodeCenterClientController,
instance.communicationAction
) {
@Override @Override
public boolean checkPermission(Action a, final JsonObject args, long permission) { public boolean checkPermission(Action a, final JsonObject args, long permission) {
return true; return true;
@ -67,11 +65,9 @@ public class CommunicationManager extends BaseFunctionManager {
} }
public static void send(String msg, String symbol) { public static void send(String msg, String symbol) {
UnitMessage unitMessage = UnitMessage.newBuilder() UnitMessage unitMessage = UnitMessage.newBuilder().setSender(GlobalConf.instance.peerID)
.setSender(GlobalConf.instance.peerID)
.setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage) .setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage)
.setContent(ByteString.copyFromUtf8(msg)) .setContent(ByteString.copyFromUtf8(msg)).build();
.build();
NetworkManager.instance.sendTo(unitMessage, symbol); NetworkManager.instance.sendTo(unitMessage, symbol);
} }
@ -81,39 +77,38 @@ public class CommunicationManager extends BaseFunctionManager {
} }
public void handle(String str, String sender) { public void handle(String str, String sender) {
// JsonObject jsonObject = (JsonObject) JsonParser.parseStringAsJsonObject(str); // JsonObject jsonObject = (JsonObject) JsonParser.parseStringAsJsonObject(str);
// if(jsonObject.has("action")) { // if(jsonObject.has("action")) {
// String action = jsonObject.get("action").getAsString(); // String action = jsonObject.get("action").getAsString();
// switch (action) { // switch (action) {
// case "myPeerId": // case "myPeerId":
// GlobalConf.setPeerID(jsonObject.get("peerId").getAsString()); // GlobalConf.setPeerID(jsonObject.get("peerId").getAsString());
// break; // break;
// default: // default:
// logger.debug("[CommunicationManager] not handled " + jsonObject.toString()); // logger.debug("[CommunicationManager] not handled " + jsonObject.toString());
// } // }
// } // }
JsonObject arg = (JsonObject) JsonParser.parseString(str); JsonObject arg = (JsonObject) JsonParser.parseString(str);
Response response; Response response;
try { try {
final String action = arg.get("action").getAsString(); final String action = arg.get("action").getAsString();
// if (isResponse(action)) { // if (isResponse(action)) {
// handleResponse(arg); // handleResponse(arg);
// return; // return;
// } // }
// logger.info("[CommunicationManager] handle:" + arg.toString()); // logger.info("[CommunicationManager] handle:" + arg.toString());
ae.handle(action, arg, ae.handle(action, arg, new ResultCallback() {
new ResultCallback() { @Override
@Override public void onResult(String arg) {
public void onResult(String arg) { System.out.println("[CommunicationManager] onResult");
System.out.println("[CommunicationManager] onResult"); // CommunicationManager.instance.send(arg, new String[]{sender});
// CommunicationManager.instance.send(arg, new String[]{sender}); }
} });
});
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
response = new Response(); response = new Response();
response.action = "onException"; response.action = "onException";
response.data = e.getMessage(); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
@ -124,12 +119,9 @@ public class CommunicationManager extends BaseFunctionManager {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
int count = 0; int count = 0;
for (String s : strs) { for (String s : strs) {
if (s.contains("sun.reflect") if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|| s.contains("java.lang.reflect") || s.contains("org.apache") || s.contains("java.util")
|| s.contains("org.apache") || s.contains("java.lang") || s.contains("io.netty")) {
|| s.contains("java.util")
|| s.contains("java.lang")
|| s.contains("io.netty")) {
continue; continue;
} }
ret.append(s); ret.append(s);
@ -149,11 +141,9 @@ public class CommunicationManager extends BaseFunctionManager {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(ACTION, "getP2PNodes"); jsonObject.addProperty(ACTION, "getP2PNodes");
jsonObject.addProperty(REQUEST_ID, requestID); jsonObject.addProperty(REQUEST_ID, requestID);
UnitMessage unitMessage = UnitMessage.newBuilder() UnitMessage unitMessage = UnitMessage.newBuilder().setSender(GlobalConf.instance.peerID)
.setSender(GlobalConf.instance.peerID)
.setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage) .setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage)
.setContent(ByteString.copyFromUtf8(jsonObject.toString())) .setContent(ByteString.copyFromUtf8(jsonObject.toString())).build();
.build();
NetworkManager.instance.send(unitMessage, NetworkType.P2P); NetworkManager.instance.send(unitMessage, NetworkType.P2P);
} }
@ -186,11 +176,9 @@ public class CommunicationManager extends BaseFunctionManager {
public void askForNextContractID() { public void askForNextContractID() {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(ACTION, "askForNextContractID"); jsonObject.addProperty(ACTION, "askForNextContractID");
UnitMessage unitMessage = UnitMessage.newBuilder() UnitMessage unitMessage = UnitMessage.newBuilder().setSender(GlobalConf.instance.peerID)
.setSender(GlobalConf.instance.peerID)
.setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage) .setMsgType(BDLedgerContract.UnitMessageType.UnitCommunicateMessage)
.setContent(ByteString.copyFromUtf8(jsonObject.toString())) .setContent(ByteString.copyFromUtf8(jsonObject.toString())).build();
.build();
NetworkManager.instance.send(unitMessage, NetworkType.P2P); NetworkManager.instance.send(unitMessage, NetworkType.P2P);
} }
@ -206,5 +194,4 @@ public class CommunicationManager extends BaseFunctionManager {
} }

View File

@ -26,15 +26,17 @@ public class ContractUnitManager extends BaseFunctionManager {
private static final String MANAGER_SYMBOL = "UNIT"; private static final String MANAGER_SYMBOL = "UNIT";
private static final Logger LOGGER = LogManager.getLogger(ExecutionManager.class); private static final Logger LOGGER = LogManager.getLogger(ExecutionManager.class);
public static ExecutorService executorService = Executors.newFixedThreadPool(10); 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(); 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 ActionExecutor<ResultCallback, JsonObject> actionExecutor;
protected UnitsInfoAction unitsInfoActions; protected UnitsInfoAction unitsInfoActions;
private Map<String, RequestCache> requestCache = new ConcurrentHashMap<>(); private Map<String, RequestCache> requestCache = new ConcurrentHashMap<>();
private Map<String, CMNodeBean> nodeinfos; private Map<String, CMNodeBean> nodeinfos;
public ContractUnitManager() {
} public ContractUnitManager() {}
public static ContractUnitManager init() { public static ContractUnitManager init() {
if (instance != null) { if (instance != null) {
@ -42,7 +44,8 @@ public class ContractUnitManager extends BaseFunctionManager {
} }
instance = new ContractUnitManager(); instance = new ContractUnitManager();
instance.unitsInfoActions = new UnitsInfoAction(); instance.unitsInfoActions = new UnitsInfoAction();
instance.actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(executorService, instance.unitsInfoActions) { instance.actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(executorService,
instance.unitsInfoActions) {
@Override @Override
public boolean checkPermission(Action a, final JsonObject args, long permission) { public boolean checkPermission(Action a, final JsonObject args, long permission) {
return true; return true;
@ -62,18 +65,17 @@ public class ContractUnitManager extends BaseFunctionManager {
return; return;
} }
LOGGER.info("[ContractUnitManager] handle:" + arg); LOGGER.info("[ContractUnitManager] handle:" + arg);
actionExecutor.handle(action, arg, actionExecutor.handle(action, arg, new ResultCallback() {
new ResultCallback() { @Override
@Override public void onResult(String arg) {
public void onResult(String arg) { ContractUnitManager.instance.send(arg, new String[] {sender});
ContractUnitManager.instance.send(arg, new String[]{sender}); }
} });
});
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
response = new Response(); response = new Response();
response.action = "onException"; response.action = "onException";
response.data = e.getMessage(); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
@ -84,12 +86,9 @@ public class ContractUnitManager extends BaseFunctionManager {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
int count = 0; int count = 0;
for (String s : strs) { for (String s : strs) {
if (s.contains("sun.reflect") if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|| s.contains("java.lang.reflect") || s.contains("org.apache") || s.contains("java.util")
|| s.contains("org.apache") || s.contains("java.lang") || s.contains("io.netty")) {
|| s.contains("java.util")
|| s.contains("java.lang")
|| s.contains("io.netty")) {
continue; continue;
} }
ret.append(s); ret.append(s);

View File

@ -46,34 +46,31 @@ public class ExecutionManager extends BaseFunctionManager {
private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE = private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE =
BDLedgerContract.UnitMessageType.UnitContractMessage; BDLedgerContract.UnitMessageType.UnitContractMessage;
public Map<String, String> contractID2Sequencing = new HashMap<>(); public Map<String, String> contractID2Sequencing = new HashMap<>();
// protected MasterServerTCPAction masterActions; // protected MasterServerTCPAction masterActions;
// protected MasterClientTCPAction clientActions; // protected MasterClientTCPAction clientActions;
protected ActionExecutor<ResultCallback, JsonObject> actionExecutor; protected ActionExecutor<ResultCallback, JsonObject> actionExecutor;
private Map<String, String> contractID2PubKey = new HashMap<>(); private Map<String, String> contractID2PubKey = new HashMap<>();
private Map<String, RequestCache> requestCaches = new ConcurrentHashMap<>(); private Map<String, RequestCache> requestCaches = new ConcurrentHashMap<>();
private String nextContractID; private String nextContractID;
public static ExecutionManager instance = init(); public static ExecutionManager instance = init();
public ExecutionManager() { public ExecutionManager() {}
}
public static ExecutionManager init() { public static ExecutionManager init() {
if (instance != null) { if (instance != null) {
return instance; return instance;
} }
instance = new ExecutionManager(); instance = new ExecutionManager();
// instance.masterActions = new MasterServerTCPAction(); // instance.masterActions = new MasterServerTCPAction();
// instance.clientActions = new MasterClientTCPAction(); // instance.clientActions = new MasterClientTCPAction();
instance.actionExecutor = instance.actionExecutor = new ActionExecutor<ResultCallback, JsonObject>(executorService) { // ,
new ActionExecutor<ResultCallback, JsonObject>( // instance.masterActions,
executorService) { // , instance.masterActions, // instance.clientActions) {
// instance.clientActions) { @Override
@Override public boolean checkPermission(Action a, final JsonObject args, long permission) {
public boolean checkPermission( return true;
Action a, final JsonObject args, long permission) { }
return true; };
}
};
return instance; return instance;
} }
@ -82,33 +79,29 @@ public class ExecutionManager extends BaseFunctionManager {
arg.addProperty("sender", sender); arg.addProperty("sender", sender);
Response response; Response response;
try { try {
// if (arg.get("msgType").getAsInt() == 1) { // if (arg.get("msgType").getAsInt() == 1) {
// handleResponse(arg); // handleResponse(arg);
// return; // return;
// } // }
final String action = arg.get("action").getAsString(); final String action = arg.get("action").getAsString();
LOGGER.info("[ExecutionManager] handle:" + arg.toString()); LOGGER.info("[ExecutionManager] handle:" + arg.toString());
// requestID2Sender.put(arg.get("requestID").getAsString(), sender); // requestID2Sender.put(arg.get("requestID").getAsString(), sender);
actionExecutor.handle( actionExecutor.handle(action, arg, new ResultCallback() {
action, @Override
arg, public void onResult(String msg) {
new ResultCallback() { try {
@Override UnitMessage unitMessage = UnitMessage.parseFrom(ByteUtil.decodeBASE64(msg));
public void onResult(String msg) { NetworkManager.instance.send(unitMessage);
try { } catch (InvalidProtocolBufferException e) {
UnitMessage unitMessage = e.printStackTrace();
UnitMessage.parseFrom(ByteUtil.decodeBASE64(msg)); }
NetworkManager.instance.send(unitMessage); }
} catch (InvalidProtocolBufferException e) { });
e.printStackTrace();
}
}
});
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
response = new Response(); response = new Response();
response.action = "onException"; response.action = "onException";
response.data = e.getMessage(); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ByteArrayOutputStream bo = new ByteArrayOutputStream(); ByteArrayOutputStream bo = new ByteArrayOutputStream();
@ -119,12 +112,9 @@ public class ExecutionManager extends BaseFunctionManager {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
int count = 0; int count = 0;
for (String s : strs) { for (String s : strs) {
if (s.contains("sun.reflect") if (s.contains("sun.reflect") || s.contains("java.lang.reflect")
|| s.contains("java.lang.reflect") || s.contains("org.apache") || s.contains("java.util")
|| s.contains("org.apache") || s.contains("java.lang") || s.contains("io.netty")) {
|| s.contains("java.util")
|| s.contains("java.lang")
|| s.contains("io.netty")) {
continue; continue;
} }
ret.append(s); ret.append(s);
@ -143,11 +133,11 @@ public class ExecutionManager extends BaseFunctionManager {
*/ */
public void updateLocalContractToNodeCenter() { public void updateLocalContractToNodeCenter() {
ControllerManager.getNodeCenterController().updateContract(); ControllerManager.getNodeCenterController().updateContract();
// List<?> info = CMActions.manager.getContractDespList(); // List<?> info = CMActions.manager.getContractDespList();
// JsonObject jo = new JsonObject(); // JsonObject jo = new JsonObject();
// jo.addProperty("action", "updateContract"); // jo.addProperty("action", "updateContract");
// jo.addProperty("contracts", JsonUtil.toJson(info)); // jo.addProperty("contracts", JsonUtil.toJson(info));
// send(jo.toString(), new String[]{GlobalConf.getNodeCenterAddress()}); // send(jo.toString(), new String[]{GlobalConf.getNodeCenterAddress()});
} }
public void killContractRequest(String contract, ResultCallback rc) { public void killContractRequest(String contract, ResultCallback rc) {
@ -164,21 +154,15 @@ public class ExecutionManager extends BaseFunctionManager {
if (info.consensusType == ConsensusType.None) { if (info.consensusType == ConsensusType.None) {
peers = info.getMembers().toArray(new String[info.members.size()]); peers = info.getMembers().toArray(new String[info.members.size()]);
} else { } else {
peers = new String[]{info.master}; peers = new String[] {info.master};
} }
ResponseCenter responseCenter = ResponseCenter responseCenter = new ResponseCenter(requestId, rc, peers.length,
new ResponseCenter( UnitContractRequestType.STOP, ResponseType.ALL.getValue());
requestId,
rc,
peers.length,
UnitContractRequestType.STOP,
ResponseType.ALL.getValue());
responseCenter.setInfos(contract); responseCenter.setInfos(contract);
this.responseCenters.put(requestId, responseCenter); this.responseCenters.put(requestId, responseCenter);
// respCaches.put(requestId, new RespCache(num)); // respCaches.put(requestId, new RespCache(num));
UnitContractMessage unitContractMessage = UnitContractMessage unitContractMessage =
new UnitContractMessage( new UnitContractMessage(UnitContractMessageType.ContractUnitRequest.getValue(),
UnitContractMessageType.ContractUnitRequest.getValue(),
UnitContractRequestType.STOP.getValue()); UnitContractRequestType.STOP.getValue());
unitContractMessage.action = "stopContractAtSlave"; unitContractMessage.action = "stopContractAtSlave";
unitContractMessage.requestID = requestId; unitContractMessage.requestID = requestId;
@ -190,49 +174,38 @@ public class ExecutionManager extends BaseFunctionManager {
unitContractMessage.collector = GlobalConf.instance.peerID; unitContractMessage.collector = GlobalConf.instance.peerID;
// Sequencing // Sequencing
if (consensusType != ConsensusType.None) { if (consensusType != ConsensusType.None) {
SequencingManager.instance.sequencingRequest( SequencingManager.instance.sequencingRequest(unitContractMessage.contractID,
unitContractMessage.contractID, unitContractMessage, peers, rc); unitContractMessage, peers, rc);
} else { } else {
String msgContent = JsonUtil.toJson(unitContractMessage); String msgContent = JsonUtil.toJson(unitContractMessage);
UnitMessage unitMessage; UnitMessage unitMessage;
unitMessage = unitMessage = UnitMessage.newBuilder()
UnitMessage.newBuilder() .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .setContent(ByteString.copyFromUtf8(msgContent))
.setContent(ByteString.copyFromUtf8(msgContent)) .setSender(GlobalConf.instance.peerID).addAllReceiver(Arrays.asList(peers))
.setSender(GlobalConf.instance.peerID) .build();
.addAllReceiver(Arrays.asList(peers))
.build();
NetworkManager.instance.send(unitMessage, networkType); NetworkManager.instance.send(unitMessage, networkType);
} }
} }
public void startContractRequest( public void startContractRequest(boolean tcpType, String contract, int num, String[] peers,
boolean tcpType, String content, int sequencing, int response, ResultCallback resultCallback) {
String contract,
int num,
String[] peers,
String content,
int sequencing,
int response,
ResultCallback resultCallback) {
LOGGER.info("[startContractRequest] " + contract + " " + num); LOGGER.info("[startContractRequest] " + contract + " " + num);
String requestId = getRequestId(); String requestId = getRequestId();
// this.requestCallbacks.put(requestId, resultCallback); // this.requestCallbacks.put(requestId, resultCallback);
ResponseCenter responseCenter = ResponseCenter responseCenter = new ResponseCenter(requestId, resultCallback, num,
new ResponseCenter( UnitContractRequestType.START, response);
requestId, resultCallback, num, UnitContractRequestType.START, response);
responseCenter.setType(ResponseType.ALL); responseCenter.setType(ResponseType.ALL);
this.responseCenters.put(requestId, responseCenter); this.responseCenters.put(requestId, responseCenter);
// respCaches.put(requestId, new RespCache(num)); // respCaches.put(requestId, new RespCache(num));
UnitContractMessage unitContractMessage = UnitContractMessage unitContractMessage =
new UnitContractMessage( new UnitContractMessage(UnitContractMessageType.ContractUnitRequest.getValue(),
UnitContractMessageType.ContractUnitRequest.getValue(),
UnitContractRequestType.START.getValue()); UnitContractRequestType.START.getValue());
unitContractMessage.action = "startContractAtSlave"; unitContractMessage.action = "startContractAtSlave";
unitContractMessage.requestID = requestId; unitContractMessage.requestID = requestId;
unitContractMessage.contractID = getNextContractID(); unitContractMessage.contractID = getNextContractID();
ExecutionManager.instance.askForNextContractID(); ExecutionManager.instance.askForNextContractID();
// logger.info("hashcode " + nextContractID.hashCode()); // logger.info("hashcode " + nextContractID.hashCode());
unitContractMessage.content = content; unitContractMessage.content = content;
unitContractMessage.sequencing = sequencing; unitContractMessage.sequencing = sequencing;
unitContractMessage.rcheck = response; unitContractMessage.rcheck = response;
@ -242,22 +215,18 @@ public class ExecutionManager extends BaseFunctionManager {
UnitMessage unitMessage; UnitMessage unitMessage;
if (null != peers && peers.length > 0) { if (null != peers && peers.length > 0) {
responseCenter.updatePeers(peers); responseCenter.updatePeers(peers);
unitMessage = unitMessage = UnitMessage.newBuilder()
UnitMessage.newBuilder() .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .setContent(ByteString.copyFromUtf8(msgContent))
.setContent(ByteString.copyFromUtf8(msgContent)) .addAllReceiver(Arrays.asList(peers)).setSender(GlobalConf.instance.peerID)
.addAllReceiver(Arrays.asList(peers)) .build();
.setSender(GlobalConf.instance.peerID)
.build();
} else { } 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 // No specified receiver, send to p2p nodes randomly
unitMessage = unitMessage = UnitMessage.newBuilder()
UnitMessage.newBuilder() .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .setContent(ByteString.copyFromUtf8(msgContent))
.setContent(ByteString.copyFromUtf8(msgContent)) .setSender(GlobalConf.instance.peerID).build();
.setSender(GlobalConf.instance.peerID)
.build();
} }
NetworkManager.instance.send(unitMessage, tcpType ? NetworkType.TCP : NetworkType.P2P); NetworkManager.instance.send(unitMessage, tcpType ? NetworkType.TCP : NetworkType.P2P);
} }
@ -272,32 +241,24 @@ public class ExecutionManager extends BaseFunctionManager {
nextContractID = content; nextContractID = content;
} }
public void executeContractRequest( public void executeContractRequest(String contract, String operation, String arg, int network,
String contract, int sequencing, int responseType, ResultCallback rc) {
String operation,
String arg,
int network,
int sequencing,
int responseType,
ResultCallback rc) {
String requestId = getRequestId(); String requestId = getRequestId();
// this.requestCallbacks.put(requestId, resultCallback); // this.requestCallbacks.put(requestId, resultCallback);
String[] peers; String[] peers;
MultiPointContractInfo info = ContractUnitManager.instance.getContractInfo(contract); MultiPointContractInfo info = ContractUnitManager.instance.getContractInfo(contract);
if (info.consensusType == ConsensusType.None) { if (info.consensusType == ConsensusType.None) {
peers = info.getMembers().toArray(new String[info.members.size()]); peers = info.getMembers().toArray(new String[info.members.size()]);
} else { } else {
peers = new String[]{info.master}; peers = new String[] {info.master};
} }
ResponseCenter responseCenter = ResponseCenter responseCenter = new ResponseCenter(requestId, rc, peers.length,
new ResponseCenter( UnitContractRequestType.EXECUTE, responseType);
requestId, rc, peers.length, UnitContractRequestType.EXECUTE, responseType);
responseCenter.setInfos(contract); responseCenter.setInfos(contract);
this.responseCenters.put(requestId, responseCenter); this.responseCenters.put(requestId, responseCenter);
// respCaches.put(requestId, new RespCache(num)); // respCaches.put(requestId, new RespCache(num));
UnitContractMessage unitContractMessage = UnitContractMessage unitContractMessage =
new UnitContractMessage( new UnitContractMessage(UnitContractMessageType.ContractUnitRequest.getValue(),
UnitContractMessageType.ContractUnitRequest.getValue(),
UnitContractRequestType.EXECUTE.getValue()); UnitContractRequestType.EXECUTE.getValue());
unitContractMessage.action = "executeContractAtSlave"; unitContractMessage.action = "executeContractAtSlave";
unitContractMessage.requestID = requestId; unitContractMessage.requestID = requestId;
@ -310,61 +271,54 @@ public class ExecutionManager extends BaseFunctionManager {
// Sequencing // Sequencing
ConsensusType consensusType = ConsensusType.getType(sequencing); ConsensusType consensusType = ConsensusType.getType(sequencing);
if (consensusType != ConsensusType.None) { if (consensusType != ConsensusType.None) {
SequencingManager.instance.sequencingRequest( SequencingManager.instance.sequencingRequest(unitContractMessage.contractID,
unitContractMessage.contractID, unitContractMessage, peers, rc); unitContractMessage, peers, rc);
} else { } else {
String msgContent = JsonUtil.toJson(unitContractMessage); String msgContent = JsonUtil.toJson(unitContractMessage);
UnitMessage unitMessage; UnitMessage unitMessage;
unitMessage = unitMessage = UnitMessage.newBuilder()
UnitMessage.newBuilder() .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
.setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) .setContent(ByteString.copyFromUtf8(msgContent))
.setContent(ByteString.copyFromUtf8(msgContent)) .setSender(GlobalConf.instance.peerID).addAllReceiver(Arrays.asList(peers))
.setSender(GlobalConf.instance.peerID) .build();
.addAllReceiver(Arrays.asList(peers))
.build();
NetworkManager.instance.send(unitMessage, NetworkType.getType(network)); NetworkManager.instance.send(unitMessage, NetworkType.getType(network));
} }
} }
public void stopContractRequest( public void stopContractRequest(String contract, String operation, String arg, int network,
String contract, int sequencing, ResultCallback rc) {
String operation, // String requestId = getRequestId();
String arg, //// this.requestCallbacks.put(requestId, resultCallback);
int network, // MultiPointContractInfo info =
int sequencing,
ResultCallback rc) {
// String requestId = getRequestId();
//// this.requestCallbacks.put(requestId, resultCallback);
// MultiPointContractInfo info =
// ContractUnitManager.instance.getContractInfo(contract); // ContractUnitManager.instance.getContractInfo(contract);
// String[] peers; // String[] peers;
// if (info.consensusType == ConsensusType.None) { // if (info.consensusType == ConsensusType.None) {
// peers = info.getMembers().toArray(new String[info.members.size()]); // peers = info.getMembers().toArray(new String[info.members.size()]);
// } else { // } else {
// peers = new String[]{info.master}; // peers = new String[]{info.master};
// } // }
// this.responseCenters.put(requestId, new ResponseCenter(requestId, rc, // this.responseCenters.put(requestId, new ResponseCenter(requestId, rc,
// peers.length, UnitContractRequestType.EXECUTE, sequencing)); // peers.length, UnitContractRequestType.EXECUTE, sequencing));
//// respCaches.put(requestId, new RespCache(num)); //// respCaches.put(requestId, new RespCache(num));
// UnitContractMessage unitContractMessage = new UnitContractMessage( // UnitContractMessage unitContractMessage = new UnitContractMessage(
// UnitContractMessageType.ContractUnitRequest.getValue(), // UnitContractMessageType.ContractUnitRequest.getValue(),
// UnitContractRequestType.EXECUTE.getValue()); // UnitContractRequestType.EXECUTE.getValue());
// unitContractMessage.action = "executeContractAtSlave"; // unitContractMessage.action = "executeContractAtSlave";
// unitContractMessage.requestID = requestId; // unitContractMessage.requestID = requestId;
// unitContractMessage.contractID = contract; // unitContractMessage.contractID = contract;
// unitContractMessage.operation = operation; // unitContractMessage.operation = operation;
// unitContractMessage.arg = arg; // unitContractMessage.arg = arg;
// unitContractMessage.sequencing = sequencing; // unitContractMessage.sequencing = sequencing;
// unitContractMessage.network = network; // unitContractMessage.network = network;
// String msgContent = gson.toJson(unitContractMessage); // String msgContent = gson.toJson(unitContractMessage);
// UnitMessage unitMessage; // UnitMessage unitMessage;
// unitMessage = UnitMessage.newBuilder() // unitMessage = UnitMessage.newBuilder()
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) // .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
// .setContent(ByteString.copyFromUtf8(msgContent)) // .setContent(ByteString.copyFromUtf8(msgContent))
// .setSender(GlobalConf.instance.peerID) // .setSender(GlobalConf.instance.peerID)
// .addAllReceiver(Arrays.asList(peers)) // .addAllReceiver(Arrays.asList(peers))
// .build(); // .build();
// NetworkManager.instance.send(unitMessage, NetworkType.getType(network)); // NetworkManager.instance.send(unitMessage, NetworkType.getType(network));
} }
public void askForNextContractID() { public void askForNextContractID() {
@ -389,5 +343,4 @@ public class ExecutionManager extends BaseFunctionManager {
} }

View File

@ -9,7 +9,7 @@ public class RecoveryManager extends BaseFunctionManager {
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
jo.addProperty("action", "NCStartElect"); jo.addProperty("action", "NCStartElect");
jo.addProperty("contractID", contractID); jo.addProperty("contractID", contractID);
//NetworkManager.instance.cmClientHandler.sendMsg(JsonUtil.toJson(jo)); // NetworkManager.instance.cmClientHandler.sendMsg(JsonUtil.toJson(jo));
} }

View File

@ -23,16 +23,15 @@ import java.util.Map;
public class SequencingManager extends BaseFunctionManager { public class SequencingManager extends BaseFunctionManager {
private static final String MANAGER_SYMBOL = "SEQUENCING"; private static final String MANAGER_SYMBOL = "SEQUENCING";
private static final Logger LOGGER = LogManager.getLogger(SequencingManager.class); private static final Logger LOGGER = LogManager.getLogger(SequencingManager.class);
// protected SequencingActions sequencingActions; // protected SequencingActions sequencingActions;
// protected ActionExecutor<ResultCallback, JsonObject> actionExecutor; // protected ActionExecutor<ResultCallback, JsonObject> actionExecutor;
private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE = private final BDLedgerContract.UnitMessageType UNIT_MESSAGE_TYPE =
BDLedgerContract.UnitMessageType.UnitSequencingMessage; 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<>(); private final Map<String, ConsensusCenter> consensusCenters = new HashMap<>();
public static SequencingManager instance = init(); public static SequencingManager instance = init();
public SequencingManager() { public SequencingManager() {}
}
public static SequencingManager init() { public static SequencingManager init() {
if (null != instance) { if (null != instance) {
@ -51,8 +50,8 @@ public class SequencingManager extends BaseFunctionManager {
String contractID = unitSequencingMessage.contractID; String contractID = unitSequencingMessage.contractID;
ConsensusCenter consensusCenter = consensusCenters.get(contractID); ConsensusCenter consensusCenter = consensusCenters.get(contractID);
if (null == consensusCenter) { if (null == consensusCenter) {
// consensusCenter = createConsensusCenter(unitSequencingMessage); // consensusCenter = createConsensusCenter(unitSequencingMessage);
// this.consensusCenters.put(contractID, consensusCenter); // this.consensusCenters.put(contractID, consensusCenter);
} }
// create null, then no dingxu // create null, then no dingxu
if (null == consensusCenter) { if (null == consensusCenter) {
@ -60,125 +59,124 @@ public class SequencingManager extends BaseFunctionManager {
return; return;
} }
consensusCenter.handle(unitSequencingMessage); consensusCenter.handle(unitSequencingMessage);
// try { // try {
//// if (arg.get("msgType").getAsInt() == 1) { //// if (arg.get("msgType").getAsInt() == 1) {
//// handleResponse(arg); //// handleResponse(arg);
//// return; //// return;
//// } //// }
// final String action = arg.get("action").getAsString(); // final String action = arg.get("action").getAsString();
// logger.info("[SequencingManager] handle:" + arg.toString()); // logger.info("[SequencingManager] handle:" + arg.toString());
// actionExecutor.handle(action, arg, // actionExecutor.handle(action, arg,
// new ResultCallback() { // new ResultCallback() {
// @Override // @Override
// public void onResult(String msg) { // public void onResult(String msg) {
// try { // try {
// BDLedgerContract.UnitMessage unitMessage = // BDLedgerContract.UnitMessage unitMessage =
// BDLedgerContract.UnitMessage.parseFrom(Base64.decode(msg)); // BDLedgerContract.UnitMessage.parseFrom(Base64.decode(msg));
// NetworkManager.instance.send(unitMessage); // NetworkManager.instance.send(unitMessage);
// } catch (InvalidProtocolBufferException e) { // } catch (InvalidProtocolBufferException e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
// } // }
// }); // });
// } catch (IllegalArgumentException e) { // } catch (IllegalArgumentException e) {
// response = new SequencingManager.Response(); // response = new SequencingManager.Response();
// response.action = "onException"; // response.action = "onException";
// response.data = e.getMessage(); // 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) { // } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
// ByteArrayOutputStream bo = new ByteArrayOutputStream(); // ByteArrayOutputStream bo = new ByteArrayOutputStream();
// e.printStackTrace(new PrintStream(bo)); // e.printStackTrace(new PrintStream(bo));
// response = new SequencingManager.Response(); // response = new SequencingManager.Response();
// response.action = "onException"; // response.action = "onException";
// String[] strs = bo.toString().split("\n"); // String[] strs = bo.toString().split("\n");
// StringBuilder ret = new StringBuilder(); // StringBuilder ret = new StringBuilder();
// int count = 0; // int count = 0;
// for (String s : strs) { // for (String s : strs) {
// if (s.contains("sun.reflect") // if (s.contains("sun.reflect")
// || s.contains("java.lang.reflect") // || s.contains("java.lang.reflect")
// || s.contains("org.apache") // || s.contains("org.apache")
// || s.contains("java.util") // || s.contains("java.util")
// || s.contains("java.lang") // || s.contains("java.lang")
// || s.contains("io.netty")) { // || s.contains("io.netty")) {
// continue; // continue;
// } // }
// ret.append(s); // ret.append(s);
// ret.append("\n"); // ret.append("\n");
// if (count++ > 10) { // if (count++ > 10) {
// break; // break;
// } // }
// } // }
// response.data = ret.toString(); // response.data = ret.toString();
// logger.debug(JsonUtil.toJson(response)); // logger.debug(JsonUtil.toJson(response));
// } // }
} }
public void createConsensusCenterToStore(UnitContractMessage msg) { public void createConsensusCenterToStore(UnitContractMessage msg) {
//create local center // create local center
String contractID = msg.contractID; String contractID = msg.contractID;
ConsensusCenter consensusCenter = createConsensusCenter(msg); ConsensusCenter consensusCenter = createConsensusCenter(msg);
consensusCenters.put(contractID, consensusCenter); consensusCenters.put(contractID, consensusCenter);
} }
public void sequencingRequest( public void sequencingRequest(String contractID, UnitContractMessage msg, String[] peers,
String contractID,
UnitContractMessage msg,
String[] peers,
ResultCallback resultCallback) { ResultCallback resultCallback) {
// content is UnitContractMessage // content is UnitContractMessage
// ConsensusCenter consensusCenter = consensusCenters.get(contractID); // ConsensusCenter consensusCenter = consensusCenters.get(contractID);
// if (null == consensusCenter) { // if (null == consensusCenter) {
// consensusCenter = createConsensusCenter(msg); // consensusCenter = createConsensusCenter(msg);
// this.consensusCenters.put(contractID, consensusCenter); // this.consensusCenters.put(contractID, consensusCenter);
// } // }
// // create null, then no dingxu // // create null, then no dingxu
// if (consensusCenter == null) { // if (consensusCenter == null) {
// ExecutionManager.instance.send(gson.toJson(msg), peers); // ExecutionManager.instance.send(gson.toJson(msg), peers);
// return; // return;
// } // }
String requestID = msg.requestID; String requestID = msg.requestID;
requestCallbacks.put(requestID, resultCallback); requestCallbacks.put(requestID, resultCallback);
UnitSequencingMessage sequencingMessage = new UnitSequencingMessage(ConsensusStage.Request.toInt()); UnitSequencingMessage sequencingMessage =
new UnitSequencingMessage(ConsensusStage.Request.toInt());
sequencingMessage.sequencing = msg.sequencing; sequencingMessage.sequencing = msg.sequencing;
sequencingMessage.network = msg.network; sequencingMessage.network = msg.network;
sequencingMessage.contractID = contractID; sequencingMessage.contractID = contractID;
sequencingMessage.requestID = requestID; sequencingMessage.requestID = requestID;
sequencingMessage.content = JsonUtil.toJson(msg); sequencingMessage.content = JsonUtil.toJson(msg);
sequencingMessage.master = ContractUnitManager.instance.getMaster(contractID); 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, // this.responseCenters.put(requestId,
// new ResponseCenter(requestId, resultCallback, num, UnitContractRequestType.START, sequencing)); // new ResponseCenter(requestId, resultCallback, num, UnitContractRequestType.START,
//// respCaches.put(requestId, new RespCache(num)); // sequencing));
// UnitContractMessage unitContractMessage = new UnitContractMessage( //// respCaches.put(requestId, new RespCache(num));
// UnitContractMessageType.ContractUnitRequest.getValue(), // UnitContractMessage unitContractMessage = new UnitContractMessage(
// UnitContractRequestType.START.getValue()); // UnitContractMessageType.ContractUnitRequest.getValue(),
// unitContractMessage.action = "startContractAtSlave"; // UnitContractRequestType.START.getValue());
// unitContractMessage.requestID = requestId; // unitContractMessage.action = "startContractAtSlave";
//// unitContractMessage.contractID = contract; // unitContractMessage.requestID = requestId;
// unitContractMessage.content = content; //// unitContractMessage.contractID = contract;
// unitContractMessage.sequencing = sequencing; // unitContractMessage.content = content;
// unitContractMessage.num = num; // unitContractMessage.sequencing = sequencing;
// unitContractMessage.network = tcpType?0:1; // unitContractMessage.num = num;
// String msgContent = gson.toJson(unitContractMessage); // unitContractMessage.network = tcpType?0:1;
// BDLedgerContract.UnitMessage unitMessage; // String msgContent = gson.toJson(unitContractMessage);
// if (null != peers && peers.length > 0) { // BDLedgerContract.UnitMessage unitMessage;
// unitMessage = BDLedgerContract.UnitMessage.newBuilder() // if (null != peers && peers.length > 0) {
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) // unitMessage = BDLedgerContract.UnitMessage.newBuilder()
// .setContent(ByteString.copyFromUtf8(msgContent)) // .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
// .addAllReceiver(Arrays.asList(peers)) // .setContent(ByteString.copyFromUtf8(msgContent))
// .setSender(GlobalConf.instance.peerID) // .addAllReceiver(Arrays.asList(peers))
// .build(); // .setSender(GlobalConf.instance.peerID)
// } else { // .build();
// // No specified receiver, send to p2p nodes randomly // } else {
// unitMessage = BDLedgerContract.UnitMessage.newBuilder() // // No specified receiver, send to p2p nodes randomly
// .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage) // unitMessage = BDLedgerContract.UnitMessage.newBuilder()
// .setContent(ByteString.copyFromUtf8(msgContent)) // .setMsgType(BDLedgerContract.UnitMessageType.UnitContractMessage)
// .setSender(GlobalConf.instance.peerID) // .setContent(ByteString.copyFromUtf8(msgContent))
// .build(); // .setSender(GlobalConf.instance.peerID)
// } // .build();
// NetworkManager.instance.send(unitMessage, tcpType?null:NetworkManager.P2P_GRPC_CLIENT); // }
// NetworkManager.instance.send(unitMessage, tcpType?null:NetworkManager.P2P_GRPC_CLIENT);
} }
private ConsensusCenter createConsensusCenter(UnitContractMessage msg) { private ConsensusCenter createConsensusCenter(UnitContractMessage msg) {
@ -195,19 +193,21 @@ public class SequencingManager extends BaseFunctionManager {
} }
public void updateContractInfo2Unit(int network, String contractID, String firstSuccessPeer) { public void updateContractInfo2Unit(int network, String contractID, String firstSuccessPeer) {
MultiPointContractInfo contractInfo = ContractUnitManager.instance.getContractInfo(contractID); MultiPointContractInfo contractInfo =
ContractUnitManager.instance.getContractInfo(contractID);
contractInfo.master = firstSuccessPeer; contractInfo.master = firstSuccessPeer;
UnitContractMessage unitContractMessage = new UnitContractMessage(0); UnitContractMessage unitContractMessage = new UnitContractMessage(0);
unitContractMessage.content = JsonUtil.toJson(contractInfo); unitContractMessage.content = JsonUtil.toJson(contractInfo);
unitContractMessage.contractID = contractID; unitContractMessage.contractID = contractID;
// unitContractMessage.network = network; // unitContractMessage.network = network;
unitContractMessage.action = "updateContractStartInfo"; unitContractMessage.action = "updateContractStartInfo";
send(JsonUtil.toJson(unitContractMessage), send(JsonUtil.toJson(unitContractMessage), contractInfo.getMembers().toArray(new String[0]),
contractInfo.getMembers().toArray(new String[0]), BDLedgerContract.UnitMessageType.UnitUnitMessage); BDLedgerContract.UnitMessageType.UnitUnitMessage);
} }
public void updateConsensusCenter(String contractID, MultiPointContractInfo info) { 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); ConsensusCenter consensusCenter = consensusCenters.get(contractID);
while (null == consensusCenter) { while (null == consensusCenter) {
try { try {
@ -232,5 +232,4 @@ public class SequencingManager extends BaseFunctionManager {
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,216 +1,207 @@
package org.bdware.units.grpc; package org.bdware.units.grpc;
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall; import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
import static io.grpc.stub.ClientCalls.futureUnaryCall;
import static io.grpc.MethodDescriptor.generateFullMethodName; import static io.grpc.MethodDescriptor.generateFullMethodName;
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall; import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall; import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
/** /**
*/ */
@javax.annotation.Generated( @javax.annotation.Generated(value = "by gRPC proto compiler (version 1.4.1-SNAPSHOT)",
value = "by gRPC proto compiler (version 1.4.1-SNAPSHOT)", comments = "Source: BDLedgerContract.proto")
comments = "Source: BDLedgerContract.proto")
public final class BDLedgerContractServiceGrpc { public final class BDLedgerContractServiceGrpc {
private BDLedgerContractServiceGrpc() {} private BDLedgerContractServiceGrpc() {}
public static final String SERVICE_NAME = "BDLedgerContractService"; public static final String SERVICE_NAME = "BDLedgerContractService";
// Static method descriptors that strictly reflect the proto. // Static method descriptors that strictly reflect the proto.
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901") @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
public static final io.grpc.MethodDescriptor<BDLedgerContract.UnitMessage, public static final io.grpc.MethodDescriptor<BDLedgerContract.UnitMessage, BDLedgerContract.UnitMessage> METHOD_BDLEDGER_CONTRACT_STREAM =
BDLedgerContract.UnitMessage> METHOD_BDLEDGER_CONTRACT_STREAM = io.grpc.MethodDescriptor.<BDLedgerContract.UnitMessage, BDLedgerContract.UnitMessage>newBuilder()
io.grpc.MethodDescriptor.<BDLedgerContract.UnitMessage, BDLedgerContract.UnitMessage>newBuilder() .setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) .setFullMethodName(generateFullMethodName("BDLedgerContractService",
.setFullMethodName(generateFullMethodName( "BDLedgerContractStream"))
"BDLedgerContractService", "BDLedgerContractStream")) .setRequestMarshaller(io.grpc.protobuf.ProtoUtils
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( .marshaller(BDLedgerContract.UnitMessage.getDefaultInstance()))
BDLedgerContract.UnitMessage.getDefaultInstance())) .setResponseMarshaller(io.grpc.protobuf.ProtoUtils
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller( .marshaller(BDLedgerContract.UnitMessage.getDefaultInstance()))
BDLedgerContract.UnitMessage.getDefaultInstance())) .build();
.build();
/**
* Creates a new async stub that supports all call types for the service
*/
public static BDLedgerContractServiceStub newStub(io.grpc.Channel channel) {
return new BDLedgerContractServiceStub(channel);
}
/**
* Creates a new blocking-style stub that supports unary and streaming output calls on the service
*/
public static BDLedgerContractServiceBlockingStub newBlockingStub(
io.grpc.Channel channel) {
return new BDLedgerContractServiceBlockingStub(channel);
}
/**
* Creates a new ListenableFuture-style stub that supports unary calls on the service
*/
public static BDLedgerContractServiceFutureStub newFutureStub(
io.grpc.Channel channel) {
return new BDLedgerContractServiceFutureStub(channel);
}
/**
*/
public static abstract class BDLedgerContractServiceImplBase implements io.grpc.BindableService {
/** /**
* Creates a new async stub that supports all call types for the service
*/ */
public io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> bDLedgerContractStream( public static BDLedgerContractServiceStub newStub(io.grpc.Channel channel) {
io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> responseObserver) { return new BDLedgerContractServiceStub(channel);
return asyncUnimplementedStreamingCall(METHOD_BDLEDGER_CONTRACT_STREAM, responseObserver);
} }
@Override public final io.grpc.ServerServiceDefinition bindService() { /**
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) * Creates a new blocking-style stub that supports unary and streaming output calls on the
.addMethod( * service
METHOD_BDLEDGER_CONTRACT_STREAM, */
asyncBidiStreamingCall( public static BDLedgerContractServiceBlockingStub newBlockingStub(io.grpc.Channel channel) {
new MethodHandlers< return new BDLedgerContractServiceBlockingStub(channel);
BDLedgerContract.UnitMessage,
BDLedgerContract.UnitMessage>(
this, METHODID_BDLEDGER_CONTRACT_STREAM)))
.build();
}
}
/**
*/
public static final class BDLedgerContractServiceStub extends io.grpc.stub.AbstractStub<BDLedgerContractServiceStub> {
private BDLedgerContractServiceStub(io.grpc.Channel channel) {
super(channel);
} }
private BDLedgerContractServiceStub(io.grpc.Channel channel, /**
io.grpc.CallOptions callOptions) { * Creates a new ListenableFuture-style stub that supports unary calls on the service
super(channel, callOptions); */
} public static BDLedgerContractServiceFutureStub newFutureStub(io.grpc.Channel channel) {
return new BDLedgerContractServiceFutureStub(channel);
@Override
protected BDLedgerContractServiceStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new BDLedgerContractServiceStub(channel, callOptions);
} }
/** /**
*/ */
public io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> bDLedgerContractStream( public static abstract class BDLedgerContractServiceImplBase
io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> responseObserver) { implements io.grpc.BindableService {
return asyncBidiStreamingCall(
getChannel().newCall(METHOD_BDLEDGER_CONTRACT_STREAM, getCallOptions()), responseObserver);
}
}
/** /**
*/ */
public static final class BDLedgerContractServiceBlockingStub extends io.grpc.stub.AbstractStub<BDLedgerContractServiceBlockingStub> { public io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> bDLedgerContractStream(
private BDLedgerContractServiceBlockingStub(io.grpc.Channel channel) { io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> responseObserver) {
super(channel); return asyncUnimplementedStreamingCall(METHOD_BDLEDGER_CONTRACT_STREAM,
} responseObserver);
}
private BDLedgerContractServiceBlockingStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) { @Override
super(channel, callOptions); public final io.grpc.ServerServiceDefinition bindService() {
} return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(METHOD_BDLEDGER_CONTRACT_STREAM, asyncBidiStreamingCall(
@Override new MethodHandlers<BDLedgerContract.UnitMessage, BDLedgerContract.UnitMessage>(
protected BDLedgerContractServiceBlockingStub build(io.grpc.Channel channel, this, METHODID_BDLEDGER_CONTRACT_STREAM)))
io.grpc.CallOptions callOptions) { .build();
return new BDLedgerContractServiceBlockingStub(channel, callOptions);
}
}
/**
*/
public static final class BDLedgerContractServiceFutureStub extends io.grpc.stub.AbstractStub<BDLedgerContractServiceFutureStub> {
private BDLedgerContractServiceFutureStub(io.grpc.Channel channel) {
super(channel);
}
private BDLedgerContractServiceFutureStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@Override
protected BDLedgerContractServiceFutureStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new BDLedgerContractServiceFutureStub(channel, callOptions);
}
}
private static final int METHODID_BDLEDGER_CONTRACT_STREAM = 0;
private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
private final BDLedgerContractServiceImplBase serviceImpl;
private final int methodId;
MethodHandlers(BDLedgerContractServiceImplBase serviceImpl, int methodId) {
this.serviceImpl = serviceImpl;
this.methodId = methodId;
}
@Override
@SuppressWarnings("unchecked")
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
default:
throw new AssertionError();
}
}
@Override
@SuppressWarnings("unchecked")
public io.grpc.stub.StreamObserver<Req> invoke(
io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
case METHODID_BDLEDGER_CONTRACT_STREAM:
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.bDLedgerContractStream(
(io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage>) responseObserver);
default:
throw new AssertionError();
}
}
}
private static final class BDLedgerContractServiceDescriptorSupplier implements io.grpc.protobuf.ProtoFileDescriptorSupplier {
@Override
public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() {
return BDLedgerContract.getDescriptor();
}
}
private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
io.grpc.ServiceDescriptor result = serviceDescriptor;
if (result == null) {
synchronized (BDLedgerContractServiceGrpc.class) {
result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME)
.setSchemaDescriptor(new BDLedgerContractServiceDescriptorSupplier())
.addMethod(METHOD_BDLEDGER_CONTRACT_STREAM)
.build();
} }
}
} }
return result;
} /**
*/
public static final class BDLedgerContractServiceStub
extends io.grpc.stub.AbstractStub<BDLedgerContractServiceStub> {
private BDLedgerContractServiceStub(io.grpc.Channel channel) {
super(channel);
}
private BDLedgerContractServiceStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@Override
protected BDLedgerContractServiceStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new BDLedgerContractServiceStub(channel, callOptions);
}
/**
*/
public io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> bDLedgerContractStream(
io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage> responseObserver) {
return asyncBidiStreamingCall(
getChannel().newCall(METHOD_BDLEDGER_CONTRACT_STREAM, getCallOptions()),
responseObserver);
}
}
/**
*/
public static final class BDLedgerContractServiceBlockingStub
extends io.grpc.stub.AbstractStub<BDLedgerContractServiceBlockingStub> {
private BDLedgerContractServiceBlockingStub(io.grpc.Channel channel) {
super(channel);
}
private BDLedgerContractServiceBlockingStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@Override
protected BDLedgerContractServiceBlockingStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new BDLedgerContractServiceBlockingStub(channel, callOptions);
}
}
/**
*/
public static final class BDLedgerContractServiceFutureStub
extends io.grpc.stub.AbstractStub<BDLedgerContractServiceFutureStub> {
private BDLedgerContractServiceFutureStub(io.grpc.Channel channel) {
super(channel);
}
private BDLedgerContractServiceFutureStub(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
@Override
protected BDLedgerContractServiceFutureStub build(io.grpc.Channel channel,
io.grpc.CallOptions callOptions) {
return new BDLedgerContractServiceFutureStub(channel, callOptions);
}
}
private static final int METHODID_BDLEDGER_CONTRACT_STREAM = 0;
private static final class MethodHandlers<Req, Resp>
implements io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
private final BDLedgerContractServiceImplBase serviceImpl;
private final int methodId;
MethodHandlers(BDLedgerContractServiceImplBase serviceImpl, int methodId) {
this.serviceImpl = serviceImpl;
this.methodId = methodId;
}
@Override
@SuppressWarnings("unchecked")
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
default:
throw new AssertionError();
}
}
@Override
@SuppressWarnings("unchecked")
public io.grpc.stub.StreamObserver<Req> invoke(
io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
case METHODID_BDLEDGER_CONTRACT_STREAM:
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.bDLedgerContractStream(
(io.grpc.stub.StreamObserver<BDLedgerContract.UnitMessage>) responseObserver);
default:
throw new AssertionError();
}
}
}
private static final class BDLedgerContractServiceDescriptorSupplier
implements io.grpc.protobuf.ProtoFileDescriptorSupplier {
@Override
public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() {
return BDLedgerContract.getDescriptor();
}
}
private static volatile io.grpc.ServiceDescriptor serviceDescriptor;
public static io.grpc.ServiceDescriptor getServiceDescriptor() {
io.grpc.ServiceDescriptor result = serviceDescriptor;
if (result == null) {
synchronized (BDLedgerContractServiceGrpc.class) {
result = serviceDescriptor;
if (result == null) {
serviceDescriptor = result = io.grpc.ServiceDescriptor.newBuilder(SERVICE_NAME)
.setSchemaDescriptor(new BDLedgerContractServiceDescriptorSupplier())
.addMethod(METHOD_BDLEDGER_CONTRACT_STREAM).build();
}
}
}
return result;
}
} }

View File

@ -76,15 +76,13 @@ public class JavaContractServiceGrpcServer {
public void start() throws IOException { public void start() throws IOException {
server.start(); server.start();
logger.info("[JavaContractServiceGrpcServer] Server started, listening on " + port); logger.info("[JavaContractServiceGrpcServer] Server started, listening on " + port);
Runtime.getRuntime() Runtime.getRuntime().addShutdownHook(new Thread(() -> {
.addShutdownHook( // Use stderr here since the logger may has been reset by its JVM
new Thread(() -> { // shutdown hook.
// Use stderr here since the logger may has been reset by its JVM System.err.println("*** shutting down gRPC server since JVM is shutting down");
// shutdown hook. JavaContractServiceGrpcServer.this.stop();
System.err.println("*** shutting down gRPC server since JVM is shutting down"); System.err.println("*** server shut down");
JavaContractServiceGrpcServer.this.stop(); }));
System.err.println("*** server shut down");
}));
} }
/** /**
@ -94,9 +92,9 @@ public class JavaContractServiceGrpcServer {
if (server != null) { if (server != null) {
server.shutdown(); server.shutdown();
} }
// if (sendTask != null) { // if (sendTask != null) {
// sendTask.shutdown(); // sendTask.shutdown();
// } // }
} }
/** /**
@ -111,7 +109,8 @@ public class JavaContractServiceGrpcServer {
/** /**
* Our implementation of RouteGuide service. * Our implementation of RouteGuide service.
* *
* <p>See route_guide.proto for details of the methods. * <p>
* See route_guide.proto for details of the methods.
*/ */
private static class JavaContractService private static class JavaContractService
extends BDLedgerContractServiceGrpc.BDLedgerContractServiceImplBase { extends BDLedgerContractServiceGrpc.BDLedgerContractServiceImplBase {
@ -134,7 +133,7 @@ public class JavaContractServiceGrpcServer {
javaServer = new StreamObserver<BDLedgerContract.UnitMessage>() { javaServer = new StreamObserver<BDLedgerContract.UnitMessage>() {
@Override @Override
public void onNext(BDLedgerContract.UnitMessage msg) { public void onNext(BDLedgerContract.UnitMessage msg) {
// System.out.println("P2P receive "+ msg.toString()); // System.out.println("P2P receive "+ msg.toString());
UnitMessageHandler.instance.handle(msg); UnitMessageHandler.instance.handle(msg);
} }

Some files were not shown because too many files have changed in this diff Show More