From d75efc218048a26a218b95addfd8703443551421 Mon Sep 17 00:00:00 2001 From: kaideng Date: Tue, 3 Jan 2023 09:57:37 +0800 Subject: [PATCH] add osx Signed-off-by: kaideng --- java/osx/bin/common.sh | 28 +- java/osx/broker/pom.xml | 106 +- .../main/java/com/osx/broker/Bootstrap.java | 93 +- .../java/com/osx/broker/ServiceContainer.java | 201 ++-- .../broker/authenticator/Authenticator.java | 30 +- .../authenticator/FateCloudAuthenticator.java | 354 +++---- .../com/osx/broker/buffer/ReadResult.java | 72 +- .../com/osx/broker/buffer/ReadStatus.java | 6 +- .../com/osx/broker/buffer/WriteResult.java | 77 +- .../com/osx/broker/buffer/WriteStatus.java | 6 +- .../osx/broker/callback/CompleteCallback.java | 3 +- .../osx/broker/callback/DestoryCallback.java | 1 + .../osx/broker/callback/ErrorCallback.java | 2 +- .../com/osx/broker/constants/Direction.java | 6 +- .../osx/broker/consumer/ConsumerManager.java | 173 ++-- .../broker/consumer/LocalQueueConsumer.java | 91 +- .../osx/broker/consumer/RedirectConsumer.java | 33 +- .../osx/broker/consumer/StreamConsumer.java | 2 +- .../osx/broker/consumer/UnaryConsumer.java | 151 ++- .../com/osx/broker/eggroll/BaseProto.java | 10 +- .../broker/eggroll/ClusterManagerClient.java | 62 +- .../com/osx/broker/eggroll/CommandClient.java | 31 +- .../com/osx/broker/eggroll/CommandURI.java | 34 +- .../com/osx/broker/eggroll/ErEndpoint.java | 37 +- .../com/osx/broker/eggroll/ErFunctor.java | 37 +- .../java/com/osx/broker/eggroll/ErJob.java | 92 +- .../com/osx/broker/eggroll/ErPartition.java | 47 +- .../com/osx/broker/eggroll/ErProcessor.java | 58 +- .../osx/broker/eggroll/ErRollSiteHeader.java | 93 +- .../com/osx/broker/eggroll/ErSession.java | 101 +- .../com/osx/broker/eggroll/ErSessionMeta.java | 60 +- .../java/com/osx/broker/eggroll/ErStore.java | 86 +- .../osx/broker/eggroll/ErStoreLocator.java | 88 +- .../java/com/osx/broker/eggroll/ErTask.java | 53 +- .../java/com/osx/broker/eggroll/IdUtils.java | 11 +- .../com/osx/broker/eggroll/MetaCommnads.java | 6 +- .../eggroll/PutBatchSinkPushRespSO.java | 19 +- .../osx/broker/eggroll/PutBatchSinkUtil.java | 30 +- .../java/com/osx/broker/eggroll/RollPair.java | 34 +- .../osx/broker/eggroll/RollPairContext.java | 47 +- .../com/osx/broker/eggroll/SerdesTypes.java | 6 +- .../osx/broker/eggroll/SessionCommands.java | 22 +- .../com/osx/broker/eggroll/SessionStatus.java | 2 +- .../com/osx/broker/eggroll/TimeUtils.java | 9 +- .../broker/flow/ClusterMetricStatistics.java | 23 +- .../com/osx/broker/flow/ClusterRuleUtil.java | 6 +- .../flow/CurrentConcurrencyManager.java | 6 +- .../com/osx/broker/grpc/ClusterService.java | 27 +- .../grpc/ContextPrepareInterceptor.java | 20 +- .../java/com/osx/broker/grpc/ContextUtil.java | 9 +- .../osx/broker/grpc/ForwardPullRespSO.java | 26 +- .../osx/broker/grpc/ForwardPushRespSO.java | 23 +- .../java/com/osx/broker/grpc/MessageFlag.java | 28 +- .../com/osx/broker/grpc/PcpGrpcService.java | 102 +- .../com/osx/broker/grpc/ProxyGrpcService.java | 25 +- .../osx/broker/grpc/PullRequestDataWrap.java | 7 +- .../broker/grpc/PushReqStreamObserver.java | 260 +++-- .../osx/broker/grpc/PushRequestDataWrap.java | 4 +- .../grpc/QueuePushReqStreamObserver.java | 338 +++--- .../com/osx/broker/grpc/TransferConf.java | 51 +- .../interceptor/PpcRouterInterceptor.java | 8 +- .../interceptor/RequestHandleInterceptor.java | 98 +- .../interceptor/WhiteListInterceptor.java | 3 +- .../message/AllocateMappedFileService.java | 30 +- .../broker/message/AppendMessageHandler.java | 2 - .../broker/message/AppendMessageResult.java | 21 +- .../broker/message/AppendMessageStatus.java | 1 - .../message/DefaultAppendMessageHandler.java | 281 +++-- .../java/com/osx/broker/message/Message.java | 27 +- .../osx/broker/message/MessageDecoder.java | 86 +- .../com/osx/broker/message/MessageExt.java | 68 +- .../broker/message/MessageExtBrokerInner.java | 3 +- .../broker/message/MessageQueueManager.java | 1 - .../broker/message/MessageStoreConfig.java | 7 +- .../osx/broker/message/MessageSysFlag.java | 1 - .../com/osx/broker/message/MessageWraper.java | 8 +- .../broker/metric/ClusterMetricLeapArray.java | 3 - .../broker/ptp/AbstractPtpServiceAdaptor.java | 10 +- .../com/osx/broker/ptp/PtpAckService.java | 58 +- .../broker/ptp/PtpCancelTransferService.java | 25 +- .../ptp/PtpClusterQueueApplyService.java | 11 +- .../com/osx/broker/ptp/PtpConsumeService.java | 72 +- .../osx/broker/ptp/PtpForwardPushRespSO.java | 38 +- .../com/osx/broker/ptp/PtpProduceService.java | 99 +- .../broker/ptp/PtpPushReqStreamObserver.java | 183 ++-- .../ptp/PtpQueryTransferQueueService.java | 48 +- .../osx/broker/ptp/PtpUnaryCallService.java | 34 +- .../java/com/osx/broker/queue/Consumer.java | 4 +- .../osx/broker/queue/CreateQueueResult.java | 67 +- .../java/com/osx/broker/queue/MappedFile.java | 45 +- .../com/osx/broker/queue/MappedFileQueue.java | 54 +- .../broker/queue/PutMessageReentrantLock.java | 1 - .../osx/broker/queue/PutMessageResult.java | 17 +- .../osx/broker/queue/PutMessageStatus.java | 1 - .../osx/broker/queue/ReferenceResource.java | 1 - .../com/osx/broker/queue/TransferQueue.java | 297 +++--- .../broker/queue/TransferQueueApplyInfo.java | 17 +- .../broker/queue/TransferQueueManager.java | 569 +++++------ .../queue/TransferQueueMonitorService.java | 8 +- .../router/DefaultFateRouterServiceImpl.java | 208 ++-- .../osx/broker/router/FateRouterService.java | 9 +- .../broker/router/RemoteRouterDataSource.java | 2 +- .../com/osx/broker/router/RouterMetric.java | 51 +- .../java/com/osx/broker/server/OsxServer.java | 152 ++- .../com/osx/broker/service/PushService2.java | 16 +- .../osx/broker/service/RegisterService.java | 12 +- .../osx/broker/service/TokenApplyService.java | 68 +- .../osx/broker/service/UnaryCallService.java | 26 +- .../java/com/osx/broker/store/IndexQueue.java | 70 +- .../com/osx/broker/store/MessageStore.java | 105 +- .../osx/broker/token/DefaultTokenService.java | 30 +- .../java/com/osx/broker/util/DateUtils.java | 959 +++++++++--------- .../main/java/com/osx/broker/util/LibC.java | 1 - .../com/osx/broker/util/MessageConst.java | 1 - .../java/com/osx/broker/util/MessageId.java | 1 - .../com/osx/broker/util/ResourceUtil.java | 12 +- .../broker/util/TransferExceptionUtil.java | 1 - .../com/osx/broker/util/TransferUtil.java | 240 +++-- .../java/com/osx/broker/util/UtilAll.java | 61 +- .../broker/zk/AbstractZookeeperClient.java | 5 +- .../osx/broker/zk/CuratorZookeeperClient.java | 10 +- .../main/java/com/osx/broker/zk/ZkConfig.java | 16 +- .../com/osx/broker/zk/ZookeeperClient.java | 1 + .../osx/tech/provider/FateTechProvider.java | 169 ++- .../tech/provider/TechProviderRegister.java | 23 +- .../src/main/resources/broker.properties | 2 +- .../src/main/resources/route_table.json | 4 +- .../java/com/osx/broker/mock/MockServer.java | 165 +-- .../com/osx/broker/test/grpc/OldFateTest.java | 81 +- .../com/osx/broker/test/grpc/QueueTest.java | 336 +++--- java/osx/core/pom.xml | 65 +- .../main/java/com/osx/core/config/Config.java | 64 +- .../com/osx/core/config/GrpcChannelInfo.java | 22 +- .../java/com/osx/core/config/MasterInfo.java | 6 +- .../java/com/osx/core/config/MetaInfo.java | 186 ++-- .../main/java/com/osx/core/constant/Dict.java | 232 ++--- .../java/com/osx/core/constant/Protocol.java | 2 +- .../com/osx/core/constant/StatusCode.java | 8 +- .../com/osx/core/constant/TransferStatus.java | 2 +- .../java/com/osx/core/context/Context.java | 168 ++- .../core/datasource/AbstractDataSource.java | 2 +- .../datasource/AutoRefreshDataSource.java | 6 +- .../datasource/FileRefreshableDataSource.java | 19 +- .../core/datasource/NamedThreadFactory.java | 2 +- .../core/exceptions/AckIndexException.java | 10 +- .../exceptions/ConsumeNoMessageException.java | 11 +- .../exceptions/ConsumerNotExistException.java | 11 +- .../osx/core/exceptions/ErrorMessageUtil.java | 11 +- .../osx/core/exceptions/ExceptionInfo.java | 51 +- .../InvalidRedirectInfoException.java | 6 +- .../core/exceptions/MappedFileException.java | 2 +- .../exceptions/MessageParseException.java | 6 +- .../exceptions/NoRouterInfoException.java | 6 +- .../core/exceptions/ProduceMsgExcption.java | 2 +- .../core/exceptions/PutMessageException.java | 2 +- .../TransferQueueAlreadyExistException.java | 2 +- .../TransferQueueInvalidStatusException.java | 7 +- .../TransferQueueNotExistException.java | 10 +- .../java/com/osx/core/flow/AbstractRule.java | 3 +- .../com/osx/core/flow/ClusterFlowChecker.java | 32 +- .../osx/core/flow/ClusterFlowRuleManager.java | 85 +- .../java/com/osx/core/flow/ClusterMetric.java | 19 +- .../osx/core/flow/ClusterMetricBucket.java | 2 +- .../core/flow/ClusterMetricStatistics.java | 20 +- .../osx/core/flow/ClusterRuleConstant.java | 4 +- .../com/osx/core/flow/ClusterRuleUtil.java | 5 +- .../core/flow/CurrentConcurrencyManager.java | 3 +- .../com/osx/core/flow/DynamicProperty.java | 9 +- .../com/osx/core/flow/FileMetricReport.java | 2 +- .../com/osx/core/flow/FlowCounterManager.java | 28 +- .../main/java/com/osx/core/flow/FlowRule.java | 155 +-- .../main/java/com/osx/core/flow/Function.java | 1 - .../osx/core/flow/GlobalRequestLimiter.java | 7 +- .../java/com/osx/core/flow/MetricBucket.java | 1 - .../java/com/osx/core/flow/MetricEvent.java | 1 - .../java/com/osx/core/flow/MetricWriter.java | 3 +- .../java/com/osx/core/flow/MetricsReader.java | 4 +- .../osx/core/flow/NamespaceFlowProperty.java | 1 - .../java/com/osx/core/flow/OccupySupport.java | 1 - .../main/java/com/osx/core/flow/Property.java | 1 - .../com/osx/core/flow/RequestLimiter.java | 12 +- .../java/com/osx/core/flow/RuleConstant.java | 4 +- .../java/com/osx/core/flow/TokenService.java | 5 +- .../com/osx/core/flow/UnaryLeapArray.java | 2 - .../com/osx/core/frame/CountDownLatch.java | 4 +- .../osx/core/frame/GrpcConnectionFactory.java | 96 +- .../java/com/osx/core/frame/Lifecycle.java | 8 +- .../com/osx/core/frame/ServiceThread.java | 15 +- .../java/com/osx/core/jvm/JVMGCUtils.java | 2 +- .../java/com/osx/core/jvm/JVMMemoryUtils.java | 375 +++---- .../java/com/osx/core/jvm/JVMThreadUtils.java | 6 +- .../main/java/com/osx/core/jvm/JvmInfo.java | 1 - .../java/com/osx/core/jvm/JvmInfoCounter.java | 2 +- .../main/java/com/osx/core/log/Appender.java | 24 +- .../com/osx/core/log/InnerLoggerFactory.java | 1 - .../java/com/osx/core/log/InternalLogger.java | 2 - .../osx/core/log/InternalLoggerFactory.java | 28 +- .../main/java/com/osx/core/log/Layout.java | 2 - .../src/main/java/com/osx/core/log/Level.java | 35 +- .../main/java/com/osx/core/log/Logger.java | 38 +- .../java/com/osx/core/log/LoggingBuilder.java | 153 ++- .../java/com/osx/core/log/LoggingEvent.java | 13 +- .../com/osx/core/log/Slf4jLoggerFactory.java | 2 - .../main/java/com/osx/core/log/SysLogger.java | 8 +- .../com/osx/core/provider/TechProvider.java | 21 +- .../java/com/osx/core/ptp/TargetMethod.java | 2 +- .../core/queue/ClusterTransferQueueInfo.java | 70 +- .../com/osx/core/queue/TranferQueueInfo.java | 3 +- .../java/com/osx/core/router/RouterInfo.java | 11 +- .../core/service/AbstractServiceAdaptor.java | 35 +- .../core/service/DefaultInterceptorChain.java | 3 +- .../com/osx/core/service/InboundPackage.java | 1 - .../com/osx/core/service/OutboundPackage.java | 7 +- .../com/osx/core/service/ServiceAdaptor.java | 1 - .../com/osx/core/token/TokenResultStatus.java | 2 +- .../java/com/osx/core/utils/AssertUtil.java | 4 +- .../java/com/osx/core/utils/EncryptUtils.java | 1 + .../com/osx/core/utils/FlowLogPrinter.java | 2 +- .../java/com/osx/core/utils/FlowLogUtil.java | 70 +- .../com/osx/core/utils/GetSystemInfo.java | 18 +- .../java/com/osx/core/utils/JsonUtil.java | 31 +- .../java/com/osx/core/utils/NetUtils.java | 5 +- .../com/osx/core/utils/ParameterUtils.java | 26 +- .../java/com/osx/core/utils/RouterUtil.java | 9 +- .../java/com/osx/core/utils/ServerUtil.java | 15 +- .../com/osx/core/utils/ToStringUtils.java | 14 +- java/osx/deploy/osx/bin/common.sh | 28 +- .../deploy/osx/conf/broker/broker.properties | 2 +- .../deploy/osx/conf/broker/route_table.json | 4 +- java/osx/pom.xml | 610 +++++------ java/osx/proto/osx.proto | 63 +- 231 files changed, 5890 insertions(+), 5923 deletions(-) diff --git a/java/osx/bin/common.sh b/java/osx/bin/common.sh index 8c58349b9b..3101efaa39 100644 --- a/java/osx/bin/common.sh +++ b/java/osx/bin/common.sh @@ -89,16 +89,17 @@ JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}" set -e getpid() { - if [ -e "./bin/$1.pid" ]; then - pid=$(cat ./bin/$1.pid) + if [ -e "./bin/broker.pid" ]; then + pid=$(cat ./bin/broker.pid) fi if [[ -n ${pid} ]]; then count=$(ps -ef | grep $pid | grep -v "grep" | wc -l) if [[ ${count} -eq 0 ]]; then - rm ./bin/$1.pid + rm ./bin/broker.pid unset pid fi fi + } mklogsdir() { @@ -114,27 +115,6 @@ start() { getpid $module if [[ ! -n ${pid} ]]; then JAVA_OPT="${JAVA_OPT} " mklogsdir - -# case "${module}" in -# broker) -# main_class=com.osx.broker.Bootstrap -# ;; -# cluster-manager) -# main_class=com.firework.cluster.server.bootstrap.Bootstrap -# ;; -# dashboard) -# main_class=com.firework.admin.Bootstrap -# ;; -# cli) -# main_class=com.firework.cli.bootstrap.Bootstrap -# ;; -# *) -# echo "module: $1 {transfer|cluster-manager|dashboard|cli}" -# exit 1 -# esac - - - # if [[ -e "${module}.jar" ]]; then # rm ${module}.jar # fi diff --git a/java/osx/broker/pom.xml b/java/osx/broker/pom.xml index 8207621e15..89f1e88fb6 100644 --- a/java/osx/broker/pom.xml +++ b/java/osx/broker/pom.xml @@ -16,28 +16,53 @@ core ${osx.version} - - - - - - - org.apache.logging.log4j - log4j-api - - - org.apache.logging.log4j - log4j-core + + org.eclipse.jetty + jetty-server + + + com.google.guava + guava + - + + org.apache.commons + commons-collections4 + + + org.apache.commons + commons-lang3 + + + io.grpc + grpc-api + + + io.grpc + grpc-core + + + org.eclipse.jetty + jetty-server + + + io.grpc + grpc-netty-shaded + + + io.grpc + grpc-protobuf + + + io.grpc + grpc-stub + org.apache.curator curator-recipes - - org.apache.zookeeper zookeeper @@ -48,29 +73,31 @@ + + org.apache.logging.log4j + log4j-api - + + + org.slf4j + slf4j-api + - com.google.code.gson - gson + com.lmax + disruptor + org.apache.logging.log4j log4j-slf4j-impl + + + org.apache.logging.log4j + log4j-core - - - - - - - - - - - + org.junit.platform junit-platform-launcher @@ -89,9 +116,30 @@ 4.12.1 test + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-servlet + + + net.java.dev.jna + jna + + + commons-validator + commons-validator + + + org.apache.httpcomponents + httpclient + + diff --git a/java/osx/broker/src/main/java/com/osx/broker/Bootstrap.java b/java/osx/broker/src/main/java/com/osx/broker/Bootstrap.java index 37b830b9ca..cc3fca7320 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/Bootstrap.java +++ b/java/osx/broker/src/main/java/com/osx/broker/Bootstrap.java @@ -18,8 +18,8 @@ import com.google.common.collect.Lists; -import com.osx.core.constant.Dict; import com.osx.core.config.MetaInfo; +import com.osx.core.constant.Dict; import com.osx.core.constant.StreamLimitMode; import com.osx.core.jvm.JvmInfoCounter; import com.osx.core.utils.JsonUtil; @@ -40,7 +40,7 @@ public class Bootstrap { static Logger logger = LoggerFactory.getLogger(Bootstrap.class); - static CommandLine commandLine; + static CommandLine commandLine; public static void main(String[] args) { try { @@ -48,8 +48,8 @@ public static void main(String[] args) { commandLine = ServerUtil.parseCmdLine("Transfer", args, buildCommandlineOptions(options), new PosixParser()); String filePath = commandLine.getOptionValue('c'); - logger.info("try to parse config file {}",filePath); - if(StringUtils.isEmpty(filePath)){ + logger.info("try to parse config file {}", filePath); + if (StringUtils.isEmpty(filePath)) { System.err.println("config file is not set ,please use -c to set the config file path"); System.exit(-1); } @@ -61,7 +61,7 @@ public static void main(String[] args) { shutDownThread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { - logger.error("0000000000000000",e); + logger.error("0000000000000000", e); } }); @@ -101,47 +101,59 @@ public static void parseConfig(String configFilePath) { MetaInfo.PROPERTY_SERVER_CERTCHAIN_FILE = environment.getProperty(Dict.PROPERTY_SERVER_CERTCHAIN_FILE); MetaInfo.PROPERTY_SERVER_PRIVATEKEY_FILE = environment.getProperty(Dict.PROPERTY_SERVER_PRIVATEKEY_FILE); MetaInfo.PROPERTY_SERVER_CA_FILE = environment.getProperty(Dict.PROPERTY_SERVER_CA_FILE); - MetaInfo.PROPERTY_TLS_PORT = Integer.valueOf(environment.getProperty(Dict.PROPERTY_TLS_PORT,"9883")); - MetaInfo.PROPERTY_PORT = Integer.valueOf(environment.getProperty(Dict.PORT,"9889")); + MetaInfo.PROPERTY_TLS_PORT = Integer.valueOf(environment.getProperty(Dict.PROPERTY_TLS_PORT, "9883")); + MetaInfo.PROPERTY_PORT = Integer.valueOf(environment.getProperty(Dict.PORT, "9889")); + MetaInfo.PROPERTY_HTTP_PORT = Integer.valueOf(environment.getProperty(Dict.HTTP_PORT,"8762")); MetaInfo.PROPERTY_PRINT_INPUT_DATA = Boolean.valueOf(environment.getProperty(Dict.PROPERTY_PRINT_INPUT_DATA, "false")); MetaInfo.PROPERTY_PRINT_OUTPUT_DATA = Boolean.valueOf(environment.getProperty(Dict.PROPERTY_PRINT_OUTPUT_DATA, "false")); - MetaInfo.PROPERTY_USER_HOME = System.getProperty("user.home") ; - MetaInfo.PROPERTY_NEGOTIATIONTYPE = environment.getProperty(Dict.PROPERTY_NEGOTIATIONTYPE,"PLAINTEXT"); - MetaInfo.PROPERTY_TRANSFER_FILE_PATH_PRE = environment.getProperty(Dict.PROPERTY_TRANSFER_FILE_PATH,MetaInfo.PROPERTY_USER_HOME + "/.fate/transfer_file"); - MetaInfo.PROPERTY_TRANSFER_FILE_CACHE_SIZE = environment.getProperty(Dict.PROPERTY_TRANSFER_FILE_CACHE_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_TRANSFER_FILE_CACHE_SIZE)) : 1<<27; + MetaInfo.PROPERTY_USER_HOME = System.getProperty("user.home"); + MetaInfo.PROPERTY_NEGOTIATIONTYPE = environment.getProperty(Dict.PROPERTY_NEGOTIATIONTYPE, "PLAINTEXT"); + MetaInfo.PROPERTY_TRANSFER_FILE_PATH_PRE = environment.getProperty(Dict.PROPERTY_TRANSFER_FILE_PATH, MetaInfo.PROPERTY_USER_HOME + "/.fate/transfer_file"); + MetaInfo.PROPERTY_TRANSFER_FILE_CACHE_SIZE = environment.getProperty(Dict.PROPERTY_TRANSFER_FILE_CACHE_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_TRANSFER_FILE_CACHE_SIZE)) : 1 << 27; MetaInfo.PROPERTY_USE_DIRECT_CACHE = Boolean.parseBoolean(environment.getProperty(Dict.PROPERTY_USE_DIRECT_CACHE, "false")); - MetaInfo.PROPERTY_MAX_TRANSFER_CACHE_SIZE = environment.getProperty(Dict.PROPERTY_MAX_TRANSFER_CACHE_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_MAX_TRANSFER_CACHE_SIZE)) : 1<<30; - MetaInfo.PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT,"600")); - MetaInfo.PROPERTY_USE_QUEUE_MODEL = Boolean.valueOf(environment.getProperty(Dict.PROPERTY_USE_QUEUE_MODEL, "false")); + MetaInfo.PROPERTY_MAX_TRANSFER_CACHE_SIZE = environment.getProperty(Dict.PROPERTY_MAX_TRANSFER_CACHE_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_MAX_TRANSFER_CACHE_SIZE)) : 1 << 30; + MetaInfo.PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT, "600")); + // MetaInfo.PROPERTY_USE_QUEUE_MODEL = Boolean.valueOf(environment.getProperty(Dict.PROPERTY_USE_QUEUE_MODEL, "false")); MetaInfo.PROPERTY_STREAM_LIMIT_MODE = environment.getProperty(Dict.PROPERTY_STREAM_LIMIT_MODE, StreamLimitMode.LOCAL.name()); - MetaInfo.PROPERTY_STREAM_LIMIT_MAX_TRY_TIME = Integer.parseInt(environment.getProperty(Dict.PROPERTY_STREAM_LIMIT_MAX_TRY_TIME,"10")); - MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION,"1000")); - MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE = environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE)) : (2<<30)-1; - MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE = environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE)) : 128<<20; - MetaInfo.PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW = environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW)) : 128<<20; - MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC,"7200")); - MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC,"3600")); - MetaInfo.PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC,"120")); - MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED = Boolean.parseBoolean(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED, "false")); - MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC,"86400")); - MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC,"86400")); - MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC,"86400")); - MetaInfo.TRANSFER_FATECLOUD_AHTHENTICATION_ENABLED = Boolean.valueOf(environment.getProperty(Dict.TRANSFER_FATECLOUD_AHTHENTICATION_ENABLED,"false")); - MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG = Boolean.valueOf(environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG,"false")); - MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE,"guest"); + MetaInfo.PROPERTY_STREAM_LIMIT_MAX_TRY_TIME = Integer.parseInt(environment.getProperty(Dict.PROPERTY_STREAM_LIMIT_MAX_TRY_TIME, "10")); + MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONCURRENT_CALL_PER_CONNECTION = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_CONCURRENT_CALL_PER_CONNECTION, "1000")); + MetaInfo.PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE = environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE)) : (2 << 30) - 1; + MetaInfo.PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE = environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE)) : 128 << 20; + MetaInfo.PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW = environment.getProperty(Dict.PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW) != null ? Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW)) : 128 << 20; + MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC, "7200")); + MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC, "3600")); + MetaInfo.PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC, "120")); + MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED = Boolean.parseBoolean(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED, "false")); + MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC, "86400")); + MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC, "86400")); + MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC = Integer.parseInt(environment.getProperty(Dict.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC, "86400")); + MetaInfo.TRANSFER_FATECLOUD_AHTHENTICATION_ENABLED = Boolean.valueOf(environment.getProperty(Dict.TRANSFER_FATECLOUD_AHTHENTICATION_ENABLED, "false")); + MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG = Boolean.valueOf(environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG, "false")); + MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE, "guest"); MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URI = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_URI, "/cloud-manager/api/site/rollsite/checkPartyId"); MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_APPKEY = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_APPKEY, ""); MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_APPSERCRET = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_APPSERCRET, ""); - MetaInfo.TRANSFER_FATECLOUD_SECRET_INFO_URL = environment.getProperty(Dict.TRANSFER_FATECLOUD_SECRET_INFO_URL,"http://localhost:9091/fate-manager/api/site/secretinfo"); - MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URL = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_URL,"http://localhost:8999/cloud-manager/api/site/rollsite/checkPartyId"); - MetaInfo.PROPERTY_SELF_PARTY.addAll(Lists.newArrayList(environment.getProperty(Dict.PROPERTY_SELF_PARTY,"").split(",")));; - MetaInfo.PRPPERTY_QUEUE_MAX_FREE_TIME = Integer.parseInt(environment.getProperty(Dict.PRPPERTY_QUEUE_MAX_FREE_TIME,"60000000")); - MetaInfo.INSTANCE_ID = NetUtils.getLocalHost()+":"+MetaInfo.PROPERTY_PORT; - MetaInfo.PROPERTY_DEPLOY_MODE=environment.getProperty(Dict.PROPERTY_DEPLOY_MODE); + MetaInfo.TRANSFER_FATECLOUD_SECRET_INFO_URL = environment.getProperty(Dict.TRANSFER_FATECLOUD_SECRET_INFO_URL, "http://localhost:9091/fate-manager/api/site/secretinfo"); + MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URL = environment.getProperty(Dict.TRANSFER_FATECLOUD_AUTHENTICATION_URL, "http://localhost:8999/cloud-manager/api/site/rollsite/checkPartyId"); + MetaInfo.PROPERTY_SELF_PARTY.addAll(Lists.newArrayList(environment.getProperty(Dict.PROPERTY_SELF_PARTY, "").split(","))); + ; + MetaInfo.HTTP_CLIENT_CONFIG_CONN_REQ_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_CONFIG_CONN_REQ_TIME_OUT,"500")); + MetaInfo.HTTP_CLIENT_CONFIG_CONN_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_CONFIG_CONN_TIME_OUT,"2000")); + MetaInfo.HTTP_CLIENT_CONFIG_SOCK_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_CONFIG_SOCK_TIME_OUT,"3000")); + MetaInfo.HTTP_CLIENT_INIT_POOL_MAX_TOTAL = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_INIT_POOL_MAX_TOTAL,"500")); + MetaInfo.HTTP_CLIENT_INIT_POOL_DEF_MAX_PER_ROUTE = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_INIT_POOL_DEF_MAX_PER_ROUTE,"200")); + MetaInfo.HTTP_CLIENT_INIT_POOL_SOCK_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_INIT_POOL_SOCK_TIME_OUT,"10000")); + MetaInfo.HTTP_CLIENT_INIT_POOL_CONN_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_INIT_POOL_CONN_TIME_OUT,"10000")); + MetaInfo.HTTP_CLIENT_INIT_POOL_CONN_REQ_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_INIT_POOL_CONN_REQ_TIME_OUT,"10000")); + MetaInfo.HTTP_CLIENT_TRAN_CONN_REQ_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_TRAN_CONN_REQ_TIME_OUT,"60000")); + MetaInfo.HTTP_CLIENT_TRAN_CONN_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_TRAN_CONN_TIME_OUT,"60000")); + MetaInfo.HTTP_CLIENT_TRAN_SOCK_TIME_OUT = Integer.valueOf(environment.getProperty(Dict.HTTP_CLIENT_TRAN_SOCK_TIME_OUT,"60000")); + + MetaInfo.PRPPERTY_QUEUE_MAX_FREE_TIME = Integer.parseInt(environment.getProperty(Dict.PRPPERTY_QUEUE_MAX_FREE_TIME, "60000000")); + MetaInfo.INSTANCE_ID = NetUtils.getLocalHost() + ":" + MetaInfo.PROPERTY_PORT; + MetaInfo.PROPERTY_DEPLOY_MODE = environment.getProperty(Dict.PROPERTY_DEPLOY_MODE); MetaInfo.PROPERTY_CLUSTER_MANAGER_ADDRESS = environment.getProperty(Dict.PROPERTY_CLUSTER_MANAGER_ADDRESS); - MetaInfo.PROPERTY_DLEDGER_PEER = environment.getProperty(Dict.PROPERTY_DLEDGER_PEER); - MetaInfo.PROPERTY_DLEDGER_SELF = environment.getProperty(Dict.PROPERTY_DLEDGER_SELF); - MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP = environment.getProperty(Dict.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP); + MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP = environment.getProperty(Dict.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP); MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT = Integer.parseInt(environment.getProperty(Dict.PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT)); MetaInfo.PROPERTY_ZK_URL = environment.getProperty(Dict.PROPERTY_ZK_URL); } catch (Exception e) { @@ -154,14 +166,11 @@ public static void parseConfig(String configFilePath) { public void start(String[] args) { ServiceContainer.init(); JvmInfoCounter.start(); - - - } public void stop() { logger.info("try to shutdown server ..."); - if(ServiceContainer.transferQueueManager!=null){ + if (ServiceContainer.transferQueueManager != null) { ServiceContainer.transferQueueManager.destroyAll(); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ServiceContainer.java b/java/osx/broker/src/main/java/com/osx/broker/ServiceContainer.java index ce502e29f4..50c2b1f7dd 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ServiceContainer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ServiceContainer.java @@ -1,34 +1,28 @@ package com.osx.broker; - -import com.osx.broker.interceptor.RequestHandleInterceptor; -import com.osx.core.config.MetaInfo; -import com.osx.core.flow.ClusterFlowRuleManager; -import com.osx.core.flow.FlowCounterManager; -import com.osx.core.service.AbstractServiceAdaptor; -import com.osx.tech.provider.TechProviderRegister; - -//import com.osx.transfer.consumer.RedirectSinker; - +import com.osx.broker.consumer.ConsumerManager; import com.osx.broker.grpc.ClusterService; - import com.osx.broker.grpc.PcpGrpcService; import com.osx.broker.grpc.ProxyGrpcService; -//import com.osx.transfer.grpc.QueueGrpcService; +import com.osx.broker.interceptor.RequestHandleInterceptor; import com.osx.broker.message.AllocateMappedFileService; -import com.osx.broker.consumer.ConsumerManager; -//import com.osx.transfer.ptp.QueryTransferQueueService; import com.osx.broker.queue.TransferQueueManager; import com.osx.broker.router.DefaultFateRouterServiceImpl; import com.osx.broker.router.FateRouterService; - import com.osx.broker.server.OsxServer; -import com.osx.broker.service.*; +import com.osx.broker.service.PushService2; +import com.osx.broker.service.TokenApplyService; +import com.osx.broker.service.UnaryCallService; import com.osx.broker.store.MessageStore; import com.osx.broker.token.DefaultTokenService; import com.osx.broker.zk.CuratorZookeeperClient; import com.osx.broker.zk.ZkConfig; +import com.osx.core.config.MetaInfo; +import com.osx.core.flow.ClusterFlowRuleManager; +import com.osx.core.flow.FlowCounterManager; +import com.osx.core.service.AbstractServiceAdaptor; +import com.osx.tech.provider.TechProviderRegister; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,112 +31,104 @@ import java.util.Map; public class ServiceContainer { - static Logger logger = LoggerFactory.getLogger(ServiceContainer.class); static public ConsumerManager consumerManager; static public PcpGrpcService pcpGrpcService; - - - static public TransferQueueManager transferQueueManager; - static public AllocateMappedFileService allocateMappedFileService; + static public TransferQueueManager transferQueueManager; + static public AllocateMappedFileService allocateMappedFileService; static public FlowCounterManager flowCounterManager; - // static public ZookeeperRegistry zookeeperRegistry; + // static public ZookeeperRegistry zookeeperRegistry; static public OsxServer transferServer; - static public ProxyGrpcService proxyGrpcService; + static public ProxyGrpcService proxyGrpcService; static public FateRouterService fateRouterService; -// static public QueueGrpcService queueGrpcservice; - // static public CommonService commonService; - static public ClusterService clusterService; - //static public DLedgerServer dLedgerServer; - + // static public QueueGrpcService queueGrpcservice; + // static public CommonService commonService; + static public ClusterService clusterService; //static public ProducerStreamService producerStreamService; - static public Map serviceAdaptorMap = new HashMap(); - static public TokenApplyService tokenApplyService ; - static public PushService2 pushService2 ; - static public UnaryCallService unaryCallService ; - - static public RequestHandleInterceptor requestHandleInterceptor; -// static public DefaultRouterInterceptor defaultRouterInterceptor; - // static public ProducerUnaryService producerUnaryService; - // static public SyncQueueService syncQueueService; - - - // static public ConsumeUnaryService consumeUnaryService; + static public Map serviceAdaptorMap = new HashMap(); + //static public DLedgerServer dLedgerServer; + static public TokenApplyService tokenApplyService; + static public PushService2 pushService2; + static public UnaryCallService unaryCallService; + static public RequestHandleInterceptor requestHandleInterceptor; + // static public ConsumeUnaryService consumeUnaryService; // static public CancelTransferService cancelTransferService; // static public AckService ackService; // static public QueryTransferQueueService queryTransferQueueService; - static public MessageStore messageStore ; + static public MessageStore messageStore; +// static public DefaultRouterInterceptor defaultRouterInterceptor; + // static public ProducerUnaryService producerUnaryService; + // static public SyncQueueService syncQueueService; //static public ClusterClientEndpoint clusterClientEndpoint; //static public ReportService reportService; // static public RedirectSinker redirectSinker; static public ClusterFlowRuleManager clusterFlowRuleManager; static public DefaultTokenService defaultTokenService; //static public TokenApplyService tokenApplyService; - static public CuratorZookeeperClient zkClient; -// static public ClusterQueueApplyService clusterQueueApplyService; - + static public CuratorZookeeperClient zkClient; static public TechProviderRegister techProviderRegister; +// static public ClusterQueueApplyService clusterQueueApplyService; + static Logger logger = LoggerFactory.getLogger(ServiceContainer.class); - - public static void init(){ + public static void init() { flowCounterManager = createFlowCounterManager(); - clusterFlowRuleManager = createClusterFlowRuleManager(); + clusterFlowRuleManager = createClusterFlowRuleManager(); //zookeeperRegistry = createServiceRegistry(); allocateMappedFileService = createAllocateMappedFileService(); messageStore = createMessageStore(allocateMappedFileService); zkClient = createCuratorZookeeperClient(); - transferQueueManager = createTransferQueueManager(); + transferQueueManager = createTransferQueueManager(); consumerManager = createTransferQueueConsumerManager(); fateRouterService = createFateRouterService(); tokenApplyService = createTokenApplyService(); pushService2 = createPushService2(); - // consumeUnaryService = createConsumeUnaryService(); - // cancelTransferService = createCancelTransferService(transferQueueManager,consumerManager); + // consumeUnaryService = createConsumeUnaryService(); + // cancelTransferService = createCancelTransferService(transferQueueManager,consumerManager); //producerStreamService = createProducerStreamService(tokenApplyService,fateRouterService,consumerManager,transferQueueManager); - // producerUnaryService = createProducerUnaryService(fateRouterService,consumerManager,transferQueueManager); + // producerUnaryService = createProducerUnaryService(fateRouterService,consumerManager,transferQueueManager); //queryTransferQueueService = new QueryTransferQueueService(transferQueueManager); - // queueGrpcservice = createQueueGrpcservice(); + // queueGrpcservice = createQueueGrpcservice(); requestHandleInterceptor = createDefaulRequestInterceptor(fateRouterService); // defaultRouterInterceptor = createDefaultRouterInterceptor(fateRouterService); unaryCallService = createUnaryCallService(requestHandleInterceptor); - proxyGrpcService = new ProxyGrpcService(pushService2,unaryCallService); - transferServer = new OsxServer(); + proxyGrpcService = new ProxyGrpcService(pushService2, unaryCallService); + transferServer = new OsxServer(); //clusterClientEndpoint = createClusterClientEndpoint(); //reportService = createReportService(); //ackService = createAckService(); - // commonService = createCommonService(); + // commonService = createCommonService(); // redirectSinker = createRedirectSinker(); - defaultTokenService = createDefaultTokenService(); + defaultTokenService = createDefaultTokenService(); tokenApplyService = createTokenApplyService(); //syncQueueService = createSyncQueueService(); - // clusterQueueApplyService = createClusterQueueApplyService(); + // clusterQueueApplyService = createClusterQueueApplyService(); clusterService = createClusterService(); - // dLedgerServer = createDLedgerServer(); + // dLedgerServer = createDLedgerServer(); pcpGrpcService = createPcpGrpcService(); - techProviderRegister= createTechProviderRegister(); - if(!transferServer.start()){ + techProviderRegister = createTechProviderRegister(); + if (!transferServer.start()) { System.exit(-1); - }else{ - - }; + } else { + } + ; } - public static TechProviderRegister createTechProviderRegister(){ - TechProviderRegister techProviderRegister = new TechProviderRegister(); + public static TechProviderRegister createTechProviderRegister() { + TechProviderRegister techProviderRegister = new TechProviderRegister(); techProviderRegister.init(); return techProviderRegister; } - public static PcpGrpcService createPcpGrpcService(){ + public static PcpGrpcService createPcpGrpcService() { - return new PcpGrpcService(); + return new PcpGrpcService(); } - public static ClusterService createClusterService(){ - return new ClusterService(); + public static ClusterService createClusterService() { + return new ClusterService(); } // public static ClusterQueueApplyService createClusterQueueApplyService(){ @@ -150,27 +136,26 @@ public static ClusterService createClusterService(){ // }; - //public static SyncQueueService createSyncQueueService(){ // return new SyncQueueService(); // } - public static CuratorZookeeperClient createCuratorZookeeperClient(){ - if(MetaInfo.isCluster()){ - ZkConfig zkConfig= new ZkConfig(MetaInfo.PROPERTY_ZK_URL,5000); + public static CuratorZookeeperClient createCuratorZookeeperClient() { + if (MetaInfo.isCluster()) { + ZkConfig zkConfig = new ZkConfig(MetaInfo.PROPERTY_ZK_URL, 5000); return new CuratorZookeeperClient(zkConfig); } return null; } - public static TokenApplyService createTokenApplyService(){ - TokenApplyService tokenApplyService = new TokenApplyService(); + public static TokenApplyService createTokenApplyService() { + TokenApplyService tokenApplyService = new TokenApplyService(); tokenApplyService.start(); - return tokenApplyService; + return tokenApplyService; } - public static DefaultTokenService createDefaultTokenService(){ - return new DefaultTokenService(); + public static DefaultTokenService createDefaultTokenService() { + return new DefaultTokenService(); } // // public static CommonService createCommonService(){ // return new CommonService(); @@ -180,7 +165,9 @@ public static DefaultTokenService createDefaultTokenService(){ // return new RedirectSinker(); // } - public static ClusterFlowRuleManager createClusterFlowRuleManager(){return new ClusterFlowRuleManager();} + public static ClusterFlowRuleManager createClusterFlowRuleManager() { + return new ClusterFlowRuleManager(); + } // public static AckService createAckService(){ // AckService ackService = new AckService(); @@ -201,12 +188,12 @@ public static DefaultTokenService createDefaultTokenService(){ public static MessageStore createMessageStore( - AllocateMappedFileService allocateMappedFileService){ - // TransferQueueManager transferQueueManager ,AllocateMappedFileService allocateMappedFileService,String path){ - MessageStore messageStore = new MessageStore(allocateMappedFileService - ,MetaInfo.PROPERTY_TRANSFER_FILE_PATH_PRE+ File.separator+MetaInfo.INSTANCE_ID+File.separator+"message-store"); + AllocateMappedFileService allocateMappedFileService) { + // TransferQueueManager transferQueueManager ,AllocateMappedFileService allocateMappedFileService,String path){ + MessageStore messageStore = new MessageStore(allocateMappedFileService + , MetaInfo.PROPERTY_TRANSFER_FILE_PATH_PRE + File.separator + MetaInfo.INSTANCE_ID + File.separator + "message-store"); messageStore.start(); - return messageStore; + return messageStore; } @@ -216,31 +203,30 @@ public static MessageStore createMessageStore( // return new QueueGrpcService(); // } - public static RequestHandleInterceptor createDefaulRequestInterceptor(FateRouterService routerService){ - RequestHandleInterceptor requestHandleInterceptor= new RequestHandleInterceptor(routerService); - return requestHandleInterceptor; + public static RequestHandleInterceptor createDefaulRequestInterceptor(FateRouterService routerService) { + RequestHandleInterceptor requestHandleInterceptor = new RequestHandleInterceptor(routerService); + return requestHandleInterceptor; } - static FlowCounterManager createFlowCounterManager(){ - FlowCounterManager flowCounterManager = new FlowCounterManager("transfer"); + static FlowCounterManager createFlowCounterManager() { + FlowCounterManager flowCounterManager = new FlowCounterManager("transfer"); flowCounterManager.startReport(); - return flowCounterManager; + return flowCounterManager; } - static UnaryCallService createUnaryCallService(RequestHandleInterceptor requestHandleInterceptor){ - UnaryCallService unaryCallService = new UnaryCallService(); + static UnaryCallService createUnaryCallService(RequestHandleInterceptor requestHandleInterceptor) { + UnaryCallService unaryCallService = new UnaryCallService(); unaryCallService.addPreProcessor(requestHandleInterceptor); return unaryCallService; } - static PushService2 createPushService2(){ - PushService2 pushService2 = new PushService2(); + static PushService2 createPushService2() { + PushService2 pushService2 = new PushService2(); return pushService2; } - // static ZookeeperRegistry createServiceRegistry() { // Preconditions.checkArgument(StringUtils.isNotEmpty(MetaInfo.PROPERTY_ZK_URL)); // return ZookeeperRegistry.createRegistry(MetaInfo.PROPERTY_ZK_URL, Dict.SERVICE_FIREWORK, Dict.ONLINE_ENVIRONMENT, MetaInfo.PROPERTY_PORT); @@ -254,7 +240,6 @@ static PushService2 createPushService2(){ // } - // static ProducerUnaryService createProducerUnaryService( // FateRouterService fateRouterService, // ConsumerManager consumerManager, @@ -283,9 +268,6 @@ static PushService2 createPushService2(){ // } - - - // static ProducerStreamService createProducerStreamService(TokenApplyService tokenApplyService, // FateRouterService fateRouterService, // ConsumerManager consumerManager, @@ -298,8 +280,8 @@ static PushService2 createPushService2(){ // return producerService; // } - static ConsumerManager createTransferQueueConsumerManager(){ - ConsumerManager consumerManager = new ConsumerManager(); + static ConsumerManager createTransferQueueConsumerManager() { + ConsumerManager consumerManager = new ConsumerManager(); return consumerManager; } @@ -310,21 +292,21 @@ static ConsumerManager createTransferQueueConsumerManager(){ // return tokenApplyService; // } - static FateRouterService createFateRouterService(){ - DefaultFateRouterServiceImpl fateRouterService = new DefaultFateRouterServiceImpl(); + static FateRouterService createFateRouterService() { + DefaultFateRouterServiceImpl fateRouterService = new DefaultFateRouterServiceImpl(); fateRouterService.start(); - return fateRouterService; + return fateRouterService; } - static TransferQueueManager createTransferQueueManager( ){ - TransferQueueManager transferQueueManager = new TransferQueueManager(); + static TransferQueueManager createTransferQueueManager() { + TransferQueueManager transferQueueManager = new TransferQueueManager(); return transferQueueManager; } - static AllocateMappedFileService createAllocateMappedFileService(){ - AllocateMappedFileService allocateMappedFileService = new AllocateMappedFileService(); + static AllocateMappedFileService createAllocateMappedFileService() { + AllocateMappedFileService allocateMappedFileService = new AllocateMappedFileService(); allocateMappedFileService.start(); - return allocateMappedFileService; + return allocateMappedFileService; } @@ -374,7 +356,6 @@ static AllocateMappedFileService createAllocateMappedFileService(){ // } - // public ApplicationListener registerComponent(ZookeeperRegistry zookeeperRegistry) { // return applicationReadyEvent -> zookeeperRegistry.registerComponent(); // } diff --git a/java/osx/broker/src/main/java/com/osx/broker/authenticator/Authenticator.java b/java/osx/broker/src/main/java/com/osx/broker/authenticator/Authenticator.java index 86b327f30d..1303690fae 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/authenticator/Authenticator.java +++ b/java/osx/broker/src/main/java/com/osx/broker/authenticator/Authenticator.java @@ -1,15 +1,15 @@ -package com.osx.broker.authenticator; - -import com.webank.ai.eggroll.api.networking.proxy.Proxy; -import org.json.JSONObject; - - -public interface Authenticator { - - JSONObject getSecretInfo() throws Exception; - - String sign() throws Exception; - - Boolean authenticate(Proxy.PollingFrame req) throws Exception; - -} +//package com.osx.broker.authenticator; +// +//import com.webank.ai.eggroll.api.networking.proxy.Proxy; +//import org.json.JSONObject; +// +// +//public interface Authenticator { +// +// JSONObject getSecretInfo() throws Exception; +// +// String sign() throws Exception; +// +// Boolean authenticate(Proxy.PollingFrame req) throws Exception; +// +//} diff --git a/java/osx/broker/src/main/java/com/osx/broker/authenticator/FateCloudAuthenticator.java b/java/osx/broker/src/main/java/com/osx/broker/authenticator/FateCloudAuthenticator.java index 9f6bb7dd05..5a00d73ee8 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/authenticator/FateCloudAuthenticator.java +++ b/java/osx/broker/src/main/java/com/osx/broker/authenticator/FateCloudAuthenticator.java @@ -1,177 +1,177 @@ -package com.osx.broker.authenticator; - - -import com.osx.core.config.MetaInfo; -import com.webank.ai.eggroll.api.networking.proxy.Proxy; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.reflect.MethodUtils; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.security.sasl.AuthenticationException; -import java.lang.reflect.Method; -import java.net.ConnectException; -import java.util.HashMap; -import java.util.UUID; - -public class FateCloudAuthenticator implements Authenticator { - Logger logger = LoggerFactory.getLogger(FateCloudAuthenticator.class); - - Method getSecretInfoMethod,signMethod,authenticateMethod; - Object fateCloud; - - public FateCloudAuthenticator() throws Exception { - Class clazz = Class.forName("com.webank.ai.fate.cloud.sdk.sdk.Fatecloud"); - this.fateCloud = clazz.newInstance(); - - this.getSecretInfoMethod = MethodUtils.getMatchingMethod(clazz, "getSecretInfo", String.class, String.class); - this.signMethod = MethodUtils.getMatchingAccessibleMethod(clazz, "generateSignature", - String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class); - this.authenticateMethod = MethodUtils.getMatchingMethod(clazz, "checkPartyId", String.class, HashMap.class, String.class); - - } - - @Override - public JSONObject getSecretInfo() throws Exception{ - // generate signature - int myPartyId = MetaInfo.ROLLSITE_PARTY_ID; - String secretInfoUrl = MetaInfo.TRANSFER_FATECLOUD_SECRET_INFO_URL; - String appSecret; - String appKey; - String role; - if (MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG) { - if (logger.isDebugEnabled()) { - logger.debug("manual configuration enabled, getting appKey, appSecret and party role from eggroll.properties"); - } - appKey = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_APPKEY; - appSecret = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_APPSERCRET; - if (appKey == null || appSecret == null) { - throw new IllegalArgumentException("failed to get appKey or appSecret or party role from eggroll.properties"); - } - switch (MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE.toLowerCase()) { - case "guest" : - role = "1"; - case "host": - role = "2"; - default: - throw new AuthenticationException("unsupported role:{}" + MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE); - } - - } else { - String args = secretInfoUrl + "," + myPartyId; - logger.info("getSecretInfo of fateCloud calling, args:{}",args); - - Object result = getSecretInfoMethod.invoke(fateCloud, secretInfoUrl, String.valueOf(myPartyId)); - - logger.info("getSecretInfo of fateCloud called"); - if (result == null || StringUtils.isBlank(result.toString())) { - throw new AuthenticationException("result of getSecretInfo is empty"); - } - - JSONObject secretInfo = new JSONObject(result.toString()); - if (secretInfo.get("data") == JSONObject.NULL) { - logger.error("partyID:${myPartyId} not registered"); - throw new AuthenticationException("partyID= "+ myPartyId +" is not registered"); - } - - appSecret = secretInfo.getJSONObject("data").getString("appSecret"); - appKey = secretInfo.getJSONObject("data").getString("appKey"); - if (logger.isDebugEnabled()) { - logger.debug("role of {} is {}" , myPartyId, secretInfo.getJSONObject("data").getString("role")); - } - role = "guest".equals(secretInfo.getJSONObject("data").getString("role").toLowerCase()) ?"1" : "2"; - } - JSONObject secretInfo= new JSONObject(); - secretInfo.put("appSecret", appSecret); - secretInfo.put("appKey", appKey); - secretInfo.put("role", role); - return secretInfo; - } - - @Override - public String sign() throws Exception { - - JSONObject secretInfoGen = getSecretInfo(); - String appSecret = (String) secretInfoGen.get("appSecret"); - String appKey = (String) secretInfoGen.get("appKey"); - String role = (String) secretInfoGen.get("role"); - String myPartyId = String.valueOf(MetaInfo.ROLLSITE_PARTY_ID); - String time = String.valueOf(System.currentTimeMillis()); - String uuid = UUID.randomUUID().toString(); - String nonce = uuid.replaceAll("-", ""); - String httpURI = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URI; - String body = ""; - if (logger.isDebugEnabled()) { - logger.debug("generateSignature of fateCloud calling"); - } - Object signature = signMethod.invoke(fateCloud, appSecret, myPartyId, role, appKey, time, nonce, httpURI, body); - if (logger.isDebugEnabled()) { - logger.debug("generateSignature of fateCloud called, signature={}",signature); - } - - if (signature == null) { - throw new AuthenticationException("signature generated is null, please check args appSecret="+ appSecret +" , partyId="+ myPartyId +" , " + - "role="+ role+" , appKey="+ appKey+" , time="+ time+" , nonce="+ nonce+" , httpURI="+ httpURI+" , body="+ body); - } - - JSONObject authInfo = new JSONObject(); - authInfo.put("signature", signature.toString()); - authInfo.put("appKey", appKey); - authInfo.put("timestamp", time); - authInfo.put("nonce", nonce); - authInfo.put("role", role); - authInfo.put("httpUri", httpURI); - - if (logger.isDebugEnabled()) { - logger.debug("authInfo to be sent " + authInfo); - } - - return authInfo.toString(); - } - - @Override - public Boolean authenticate(Proxy.PollingFrame req) throws Exception { - - String authUrl = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URL; - String authString = req.getMetadata().getTask().getModel().getDataKey(); - if (logger.isDebugEnabled()) { - logger.debug("req metaData recv={}",req.getMetadata().toString()); - } - if ("".equals(authString)) { - throw new AuthenticationException("failed to get authentication info from header="+req.getMetadata().toString()); - } - JSONObject authInfo = new JSONObject(authString); - HashMap heads = new HashMap<>(); - heads.put("TIMESTAMP", authInfo.getString("timestamp")); - heads.put("PARTY_ID", req.getMetadata().getDst().getPartyId()); - heads.put("NONCE", authInfo.getString("nonce")); - heads.put("ROLE", authInfo.getString("role")); - heads.put("APP_KEY", authInfo.getString("appKey")); - heads.put("URI", authInfo.getString("httpUri")); - heads.put("SIGNATURE", authInfo.getString("signature")); - String body = ""; - if (logger.isDebugEnabled()) { - logger.debug("auth heads:"+heads.toString()); - logger.debug("checkPartyId of fateCloud calling"); - } - - try { - Boolean result = (Boolean) authenticateMethod.invoke(fateCloud, authUrl, heads, body); - if (logger.isDebugEnabled()) { - logger.debug("checkPartyId of fateCloud called"); - } - return result; - } catch (Exception e) { - if (e instanceof ConnectException) { - logger.error("server authenticate failed to connect to {}", authUrl); - throw new ConnectException(" server authenticate failed to connect to authentication server"); - } else if (e instanceof AuthenticationException) { - throw new AuthenticationException("failed to authenticate, please check client authentication info="+ authString, e); - } else { - throw new Exception("failed to authenticate, please check client authentication info="+ authString, e); - } - } - - } -} +//package com.osx.broker.authenticator; +// +// +//import com.osx.core.config.MetaInfo; +//import com.webank.ai.eggroll.api.networking.proxy.Proxy; +//import org.apache.commons.lang3.StringUtils; +//import org.apache.commons.lang3.reflect.MethodUtils; +//import org.json.JSONObject; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import javax.security.sasl.AuthenticationException; +//import java.lang.reflect.Method; +//import java.net.ConnectException; +//import java.util.HashMap; +//import java.util.UUID; +// +//public class FateCloudAuthenticator implements Authenticator { +// Logger logger = LoggerFactory.getLogger(FateCloudAuthenticator.class); +// +// Method getSecretInfoMethod, signMethod, authenticateMethod; +// Object fateCloud; +// +// public FateCloudAuthenticator() throws Exception { +// Class clazz = Class.forName("com.webank.ai.fate.cloud.sdk.sdk.Fatecloud"); +// this.fateCloud = clazz.newInstance(); +// +// this.getSecretInfoMethod = MethodUtils.getMatchingMethod(clazz, "getSecretInfo", String.class, String.class); +// this.signMethod = MethodUtils.getMatchingAccessibleMethod(clazz, "generateSignature", +// String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class); +// this.authenticateMethod = MethodUtils.getMatchingMethod(clazz, "checkPartyId", String.class, HashMap.class, String.class); +// +// } +// +// @Override +// public JSONObject getSecretInfo() throws Exception { +// // generate signature +// int myPartyId = MetaInfo.ROLLSITE_PARTY_ID; +// String secretInfoUrl = MetaInfo.TRANSFER_FATECLOUD_SECRET_INFO_URL; +// String appSecret; +// String appKey; +// String role; +// if (MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG) { +// if (logger.isDebugEnabled()) { +// logger.debug("manual configuration enabled, getting appKey, appSecret and party role from eggroll.properties"); +// } +// appKey = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_APPKEY; +// appSecret = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_APPSERCRET; +// if (appKey == null || appSecret == null) { +// throw new IllegalArgumentException("failed to get appKey or appSecret or party role from eggroll.properties"); +// } +// switch (MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE.toLowerCase()) { +// case "guest": +// role = "1"; +// case "host": +// role = "2"; +// default: +// throw new AuthenticationException("unsupported role:{}" + MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_ROLE); +// } +// +// } else { +// String args = secretInfoUrl + "," + myPartyId; +// logger.info("getSecretInfo of fateCloud calling, args:{}", args); +// +// Object result = getSecretInfoMethod.invoke(fateCloud, secretInfoUrl, String.valueOf(myPartyId)); +// +// logger.info("getSecretInfo of fateCloud called"); +// if (result == null || StringUtils.isBlank(result.toString())) { +// throw new AuthenticationException("result of getSecretInfo is empty"); +// } +// +// JSONObject secretInfo = new JSONObject(result.toString()); +// if (secretInfo.get("data") == JSONObject.NULL) { +// logger.error("partyID:${myPartyId} not registered"); +// throw new AuthenticationException("partyID= " + myPartyId + " is not registered"); +// } +// +// appSecret = secretInfo.getJSONObject("data").getString("appSecret"); +// appKey = secretInfo.getJSONObject("data").getString("appKey"); +// if (logger.isDebugEnabled()) { +// logger.debug("role of {} is {}", myPartyId, secretInfo.getJSONObject("data").getString("role")); +// } +// role = "guest".equals(secretInfo.getJSONObject("data").getString("role").toLowerCase()) ? "1" : "2"; +// } +// JSONObject secretInfo = new JSONObject(); +// secretInfo.put("appSecret", appSecret); +// secretInfo.put("appKey", appKey); +// secretInfo.put("role", role); +// return secretInfo; +// } +// +// @Override +// public String sign() throws Exception { +// +// JSONObject secretInfoGen = getSecretInfo(); +// String appSecret = (String) secretInfoGen.get("appSecret"); +// String appKey = (String) secretInfoGen.get("appKey"); +// String role = (String) secretInfoGen.get("role"); +// String myPartyId = String.valueOf(MetaInfo.ROLLSITE_PARTY_ID); +// String time = String.valueOf(System.currentTimeMillis()); +// String uuid = UUID.randomUUID().toString(); +// String nonce = uuid.replaceAll("-", ""); +// String httpURI = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URI; +// String body = ""; +// if (logger.isDebugEnabled()) { +// logger.debug("generateSignature of fateCloud calling"); +// } +// Object signature = signMethod.invoke(fateCloud, appSecret, myPartyId, role, appKey, time, nonce, httpURI, body); +// if (logger.isDebugEnabled()) { +// logger.debug("generateSignature of fateCloud called, signature={}", signature); +// } +// +// if (signature == null) { +// throw new AuthenticationException("signature generated is null, please check args appSecret=" + appSecret + " , partyId=" + myPartyId + " , " + +// "role=" + role + " , appKey=" + appKey + " , time=" + time + " , nonce=" + nonce + " , httpURI=" + httpURI + " , body=" + body); +// } +// +// JSONObject authInfo = new JSONObject(); +// authInfo.put("signature", signature.toString()); +// authInfo.put("appKey", appKey); +// authInfo.put("timestamp", time); +// authInfo.put("nonce", nonce); +// authInfo.put("role", role); +// authInfo.put("httpUri", httpURI); +// +// if (logger.isDebugEnabled()) { +// logger.debug("authInfo to be sent " + authInfo); +// } +// +// return authInfo.toString(); +// } +// +// @Override +// public Boolean authenticate(Proxy.PollingFrame req) throws Exception { +// +// String authUrl = MetaInfo.TRANSFER_FATECLOUD_AUTHENTICATION_URL; +// String authString = req.getMetadata().getTask().getModel().getDataKey(); +// if (logger.isDebugEnabled()) { +// logger.debug("req metaData recv={}", req.getMetadata().toString()); +// } +// if ("".equals(authString)) { +// throw new AuthenticationException("failed to get authentication info from header=" + req.getMetadata().toString()); +// } +// JSONObject authInfo = new JSONObject(authString); +// HashMap heads = new HashMap<>(); +// heads.put("TIMESTAMP", authInfo.getString("timestamp")); +// heads.put("PARTY_ID", req.getMetadata().getDst().getPartyId()); +// heads.put("NONCE", authInfo.getString("nonce")); +// heads.put("ROLE", authInfo.getString("role")); +// heads.put("APP_KEY", authInfo.getString("appKey")); +// heads.put("URI", authInfo.getString("httpUri")); +// heads.put("SIGNATURE", authInfo.getString("signature")); +// String body = ""; +// if (logger.isDebugEnabled()) { +// logger.debug("auth heads:" + heads.toString()); +// logger.debug("checkPartyId of fateCloud calling"); +// } +// +// try { +// Boolean result = (Boolean) authenticateMethod.invoke(fateCloud, authUrl, heads, body); +// if (logger.isDebugEnabled()) { +// logger.debug("checkPartyId of fateCloud called"); +// } +// return result; +// } catch (Exception e) { +// if (e instanceof ConnectException) { +// logger.error("server authenticate failed to connect to {}", authUrl); +// throw new ConnectException(" server authenticate failed to connect to authentication server"); +// } else if (e instanceof AuthenticationException) { +// throw new AuthenticationException("failed to authenticate, please check client authentication info=" + authString, e); +// } else { +// throw new Exception("failed to authenticate, please check client authentication info=" + authString, e); +// } +// } +// +// } +//} diff --git a/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadResult.java b/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadResult.java index 40fa0dbe55..706438609a 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadResult.java +++ b/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadResult.java @@ -1,38 +1,38 @@ package com.osx.broker.buffer; -public class ReadResult{ - public ReadResult(ReadStatus status,byte[] data,int readIndex){ - this.status = status; - this.data = data; - this.readIndex = readIndex; - } - ReadStatus status; - - public ReadStatus getStatus() { - return status; - } - - public void setStatus(ReadStatus status) { - this.status = status; - } - - public byte[] getData() { - return data; - } - - public void setData(byte[] data) { - this.data = data; - } - - public int getReadIndex() { - return readIndex; - } - - public void setReadIndex(int readIndex) { - this.readIndex = readIndex; - } - - byte[] data; - int readIndex; - - } \ No newline at end of file +public class ReadResult { + ReadStatus status; + byte[] data; + int readIndex; + + public ReadResult(ReadStatus status, byte[] data, int readIndex) { + this.status = status; + this.data = data; + this.readIndex = readIndex; + } + + public ReadStatus getStatus() { + return status; + } + + public void setStatus(ReadStatus status) { + this.status = status; + } + + public byte[] getData() { + return data; + } + + public void setData(byte[] data) { + this.data = data; + } + + public int getReadIndex() { + return readIndex; + } + + public void setReadIndex(int readIndex) { + this.readIndex = readIndex; + } + +} \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadStatus.java b/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadStatus.java index 7187834061..0074faa394 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadStatus.java +++ b/java/osx/broker/src/main/java/com/osx/broker/buffer/ReadStatus.java @@ -1,6 +1,6 @@ package com.osx.broker.buffer; -public enum ReadStatus{ - OK,ERROR,DISCARD; +public enum ReadStatus { + OK, ERROR, DISCARD; - } \ No newline at end of file +} \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteResult.java b/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteResult.java index 4c626fb872..35fd159c5c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteResult.java +++ b/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteResult.java @@ -3,41 +3,42 @@ import com.osx.core.utils.JsonUtil; -public class WriteResult{ - public WriteResult(WriteStatus status, int dataSize, int writeIndex){ - this.status = status; - this.dataSize = dataSize; - this.writeIndex = writeIndex; - } - - public WriteStatus getStatus() { - return status; - } - - public void setStatus(WriteStatus status) { - this.status = status; - } - - public int getDataSize() { - return dataSize; - } - - public void setDataSize(int dataSize) { - this.dataSize = dataSize; - } - - public int getWriteIndex() { - return writeIndex; - } - - public void setWriteIndex(int writeIndex) { - this.writeIndex = writeIndex; - } - - WriteStatus status; - int dataSize; - int writeIndex; - public String toString(){ - return JsonUtil.object2Json(this); - } - } \ No newline at end of file +public class WriteResult { + WriteStatus status; + int dataSize; + int writeIndex; + + public WriteResult(WriteStatus status, int dataSize, int writeIndex) { + this.status = status; + this.dataSize = dataSize; + this.writeIndex = writeIndex; + } + + public WriteStatus getStatus() { + return status; + } + + public void setStatus(WriteStatus status) { + this.status = status; + } + + public int getDataSize() { + return dataSize; + } + + public void setDataSize(int dataSize) { + this.dataSize = dataSize; + } + + public int getWriteIndex() { + return writeIndex; + } + + public void setWriteIndex(int writeIndex) { + this.writeIndex = writeIndex; + } + + public String toString() { + return JsonUtil.object2Json(this); + } +} \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteStatus.java b/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteStatus.java index 5dad88c1f3..a091d812e1 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteStatus.java +++ b/java/osx/broker/src/main/java/com/osx/broker/buffer/WriteStatus.java @@ -1,5 +1,5 @@ package com.osx.broker.buffer; -public enum WriteStatus{ - OK,FULL,ERROR - } \ No newline at end of file +public enum WriteStatus { + OK, FULL, ERROR +} \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/callback/CompleteCallback.java b/java/osx/broker/src/main/java/com/osx/broker/callback/CompleteCallback.java index 2330849b21..5dc7fc1e59 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/callback/CompleteCallback.java +++ b/java/osx/broker/src/main/java/com/osx/broker/callback/CompleteCallback.java @@ -1,5 +1,6 @@ package com.osx.broker.callback; + @FunctionalInterface public interface CompleteCallback { - public void callback(); + public void callback(); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/callback/DestoryCallback.java b/java/osx/broker/src/main/java/com/osx/broker/callback/DestoryCallback.java index 08ea310b86..69f2cee060 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/callback/DestoryCallback.java +++ b/java/osx/broker/src/main/java/com/osx/broker/callback/DestoryCallback.java @@ -1,4 +1,5 @@ package com.osx.broker.callback; + @FunctionalInterface public interface DestoryCallback { void callback(); diff --git a/java/osx/broker/src/main/java/com/osx/broker/callback/ErrorCallback.java b/java/osx/broker/src/main/java/com/osx/broker/callback/ErrorCallback.java index 374f288b2f..a6b4e8db57 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/callback/ErrorCallback.java +++ b/java/osx/broker/src/main/java/com/osx/broker/callback/ErrorCallback.java @@ -1,7 +1,7 @@ package com.osx.broker.callback; public interface ErrorCallback { - public void callback(Throwable e); + public void callback(Throwable e); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/constants/Direction.java b/java/osx/broker/src/main/java/com/osx/broker/constants/Direction.java index bcdce5ff69..3b0e0bb7f7 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/constants/Direction.java +++ b/java/osx/broker/src/main/java/com/osx/broker/constants/Direction.java @@ -1,11 +1,11 @@ package com.osx.broker.constants; public enum Direction { -// RECEIVE, + // RECEIVE, // RESPONSE, // BACKSEND, // BACKRECE, - UP, - DOWN + UP, + DOWN } diff --git a/java/osx/broker/src/main/java/com/osx/broker/consumer/ConsumerManager.java b/java/osx/broker/src/main/java/com/osx/broker/consumer/ConsumerManager.java index 121ce4fdc5..4ff2290694 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/consumer/ConsumerManager.java +++ b/java/osx/broker/src/main/java/com/osx/broker/consumer/ConsumerManager.java @@ -1,7 +1,6 @@ package com.osx.broker.consumer; - import com.google.common.collect.Maps; import com.osx.core.frame.ServiceThread; import org.slf4j.Logger; @@ -14,35 +13,64 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -public class ConsumerManager { +public class ConsumerManager { Logger logger = LoggerFactory.getLogger(ConsumerManager.class); ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1); -// ConcurrentHashMap> transferQueueConsumerMap = new ConcurrentHashMap<>(); + // ConcurrentHashMap> transferQueueConsumerMap = new ConcurrentHashMap<>(); ConcurrentHashMap unaryConsumerMap = new ConcurrentHashMap<>(); - // ConcurrentHashMap pushConsumerMap = new ConcurrentHashMap<>(); + // ConcurrentHashMap pushConsumerMap = new ConcurrentHashMap<>(); ConcurrentHashMap streamConsumerMap = new ConcurrentHashMap<>(); - ConcurrentHashMap redirectConsumerMap = new ConcurrentHashMap<>(); - AtomicLong consumerIdIndex = new AtomicLong(0); + ConcurrentHashMap redirectConsumerMap = new ConcurrentHashMap<>(); + AtomicLong consumerIdIndex = new AtomicLong(0); // public Map getTransferQueueConsumerSet(String transferId){ // return transferQueueConsumerMap.get(transferId); // } + ServiceThread longPullingThread = new ServiceThread() { + @Override + public String getServiceName() { + return "longPullingThread"; + } + @Override + public void run() { + int interval = 200; + final AtomicInteger longPullingWaitingSize = new AtomicInteger(0); + final AtomicInteger answerCount = new AtomicInteger(0); + while (true) { + try { + longPullingWaitingSize.set(0); + answerCount.set(0); + unaryConsumerMap.forEach((transferId, unaryConsumer) -> { + try { + answerCount.addAndGet(unaryConsumer.answerLongPulling()); + longPullingWaitingSize.addAndGet(unaryConsumer.getLongPullingQueueSize()); + } catch (Exception e) { + e.printStackTrace(); + } + }); + if (longPullingWaitingSize.get() > 0) { + interval = 500; + } else { + interval = 1000; + } + } catch (Exception igore) { + igore.printStackTrace(); + } + this.waitForRunning(interval); + } + } + }; - public ConsumerManager(){ + public ConsumerManager() { // scheduledExecutorService.scheduleAtFixedRate(()->{ // checkAndClean(); // },1,1, TimeUnit.SECONDS); longPullingThread.start(); - // monitorThread.start(); - } - - - public Map getUnaryConsumerMap(){ - return Maps.newHashMap( this.unaryConsumerMap); + // monitorThread.start(); } @@ -63,12 +91,11 @@ public Map getUnaryConsumerMap(){ // } // }; - public static class ReportData{ - - + public Map getUnaryConsumerMap() { + return Maps.newHashMap(this.unaryConsumerMap); } - public void report(){ + public void report() { AtomicInteger longPullingSize = new AtomicInteger(0); longPullingSize.set(0); unaryConsumerMap.forEach((transferId, unaryConsumer) -> { @@ -78,68 +105,41 @@ public void report(){ } - - ServiceThread longPullingThread = new ServiceThread() { - @Override - public String getServiceName() { - return "longPullingThread"; - } - @Override - public void run() { - int interval = 200; - final AtomicInteger longPullingWaitingSize = new AtomicInteger(0); - final AtomicInteger answerCount = new AtomicInteger(0); - while(true){ - try { - longPullingWaitingSize.set(0); - answerCount.set(0); - unaryConsumerMap.forEach((transferId, unaryConsumer) -> { - try { - answerCount.addAndGet(unaryConsumer.answerLongPulling()); - longPullingWaitingSize.addAndGet(unaryConsumer.getLongPullingQueueSize()); - }catch(Exception e){ - e.printStackTrace(); - } - }); - if (longPullingWaitingSize.get() > 0) { - interval = 500; - } else { - interval = 1000; - } - }catch(Exception igore){ - igore.printStackTrace(); - } - this.waitForRunning(interval); - } - - } - }; - - public UnaryConsumer getUnaryConsumer(String transferId){ - return unaryConsumerMap.get(transferId); + public UnaryConsumer getUnaryConsumer(String transferId) { + return unaryConsumerMap.get(transferId); } - public UnaryConsumer getOrCreateUnaryConsumer(String transferId){ - if (unaryConsumerMap.get(transferId) == null) { - UnaryConsumer unaryConsumer = - new UnaryConsumer(consumerIdIndex.get(), transferId); - unaryConsumerMap.putIfAbsent(transferId,unaryConsumer); - return unaryConsumerMap.get(transferId); - } else { - return unaryConsumerMap.get(transferId); - } + public UnaryConsumer getOrCreateUnaryConsumer(String transferId) { + if (unaryConsumerMap.get(transferId) == null) { + UnaryConsumer unaryConsumer = + new UnaryConsumer(consumerIdIndex.get(), transferId); + unaryConsumerMap.putIfAbsent(transferId, unaryConsumer); + return unaryConsumerMap.get(transferId); + } else { + return unaryConsumerMap.get(transferId); + } } + public StreamConsumer getOrCreateStreamConsumer(String transferId) { + if (streamConsumerMap.get(transferId) == null) { + StreamConsumer streamConsumer = new StreamConsumer(consumerIdIndex.get(), transferId); + streamConsumerMap.putIfAbsent(transferId, streamConsumer); + return streamConsumerMap.get(transferId); + } else { + return streamConsumerMap.get(transferId); + } + } - public StreamConsumer getOrCreateStreamConsumer(String transferId){ - - if(streamConsumerMap.get(transferId)==null){ - StreamConsumer streamConsumer = new StreamConsumer(consumerIdIndex.get(),transferId); - streamConsumerMap.putIfAbsent(transferId,streamConsumer); - return streamConsumerMap.get(transferId); - }else{ - return streamConsumerMap.get(transferId); + public synchronized RedirectConsumer getOrCreateRedirectConsumer(String resource) { + logger.info("getOrCreateRedirectConsumer {}", resource); + if (unaryConsumerMap.get(resource) == null) { + RedirectConsumer redirectConsumer = + new RedirectConsumer(consumerIdIndex.get(), resource); + unaryConsumerMap.putIfAbsent(resource, redirectConsumer); + return (RedirectConsumer) unaryConsumerMap.get(resource); + } else { + return (RedirectConsumer) unaryConsumerMap.get(resource); } } @@ -155,31 +155,20 @@ public StreamConsumer getOrCreateStreamConsumer(String transferId){ // } // } - public synchronized RedirectConsumer getOrCreateRedirectConsumer(String resource){ - logger.info("getOrCreateRedirectConsumer {}",resource); - if (unaryConsumerMap.get(resource) == null) { - RedirectConsumer redirectConsumer = - new RedirectConsumer(consumerIdIndex.get(), resource); - unaryConsumerMap.putIfAbsent(resource,redirectConsumer); - return (RedirectConsumer)unaryConsumerMap.get(resource); - } else { - return (RedirectConsumer) unaryConsumerMap.get(resource); - } - } - - - - - - public void onComplete(String transferId){ + public void onComplete(String transferId) { this.unaryConsumerMap.remove(transferId); - logger.info("remove consumer {}",transferId); + logger.info("remove consumer {}", transferId); } /** * */ - private void checkAndClean(){ + private void checkAndClean() { + } + + public static class ReportData { + + } diff --git a/java/osx/broker/src/main/java/com/osx/broker/consumer/LocalQueueConsumer.java b/java/osx/broker/src/main/java/com/osx/broker/consumer/LocalQueueConsumer.java index b5ab77be1c..09cdaed29e 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/consumer/LocalQueueConsumer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/consumer/LocalQueueConsumer.java @@ -1,26 +1,31 @@ package com.osx.broker.consumer; -import com.osx.core.constant.StatusCode; -import com.osx.core.constant.TransferStatus; -import com.osx.core.context.Context; -import com.osx.core.exceptions.AckIndexException; import com.osx.broker.ServiceContainer; import com.osx.broker.message.SelectMappedBufferResult; import com.osx.broker.queue.Consumer; import com.osx.broker.queue.TransferQueue; +import com.osx.core.constant.StatusCode; +import com.osx.core.constant.TransferStatus; +import com.osx.core.context.Context; +import com.osx.core.exceptions.AckIndexException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.atomic.AtomicLong; -public class LocalQueueConsumer implements Consumer { +public class LocalQueueConsumer implements Consumer { + protected long consumerId; Logger logger = LoggerFactory.getLogger(LocalQueueConsumer.class); + String transferId; + AtomicLong consumeOffset = new AtomicLong(1); + volatile TransferStatus transferStatus = TransferStatus.INIT; + long createTimestamp = System.currentTimeMillis(); - public LocalQueueConsumer(long consumerId,String transferId){ - this.consumerId = consumerId; - this.transferId = transferId; + public LocalQueueConsumer(long consumerId, String transferId) { + this.consumerId = consumerId; + this.transferId = transferId; } public long getConsumerId() { @@ -31,8 +36,6 @@ public void setConsumerId(long consumerId) { this.consumerId = consumerId; } - protected long consumerId ; - public String getTransferId() { return transferId; } @@ -41,34 +44,15 @@ public void setTransferId(String transferId) { this.transferId = transferId; } - public void setConsumeOffset(AtomicLong consumeOffset) { - this.consumeOffset = consumeOffset; - } - - String transferId; - - - AtomicLong consumeOffset = new AtomicLong(1); - - volatile TransferStatus transferStatus = TransferStatus.INIT; - - long createTimestamp = System.currentTimeMillis(); - - public boolean checkMsgIsArrive(long consumeOffset){ + public boolean checkMsgIsArrive(long consumeOffset) { TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(transferId); - if(transferQueue!=null) { + if (transferQueue != null) { long indexFileOffset = transferQueue.getIndexQueue().getLogicOffset().get(); - return consumeOffset<=indexFileOffset; + return consumeOffset <= indexFileOffset; } - return false; + return false; } - - - - - - public TransferStatus getTransferStatus() { return transferStatus; } @@ -85,40 +69,43 @@ public void setCreateTimestamp(long createTimestamp) { this.createTimestamp = createTimestamp; } - - public long addConsumeCount(int size) { + public long addConsumeCount(int size) { return this.consumeOffset.addAndGet(size); } - public long ack(long index){ - long currentIndex = this.consumeOffset.get(); - if(index!=currentIndex){ - throw new AckIndexException("ack invalid index ,current : "+currentIndex+" ack : "+index); - }else{ + public long ack(long index) { + long currentIndex = this.consumeOffset.get(); + if (index != currentIndex) { + throw new AckIndexException("ack invalid index ,current : " + currentIndex + " ack : " + index); + } else { return this.consumeOffset.addAndGet(1); } } - public long getConsumeOffset(){ - return this.consumeOffset.get(); + public long getConsumeOffset() { + return this.consumeOffset.get(); + } + + public void setConsumeOffset(AtomicLong consumeOffset) { + this.consumeOffset = consumeOffset; } - public synchronized TransferQueue.TransferQueueConsumeResult consume(Context context , long beginOffset){ - TransferQueue.TransferQueueConsumeResult result ; + public synchronized TransferQueue.TransferQueueConsumeResult consume(Context context, long beginOffset) { + TransferQueue.TransferQueueConsumeResult result; long offset = beginOffset; TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(transferId); - if(transferQueue!=null) { + if (transferQueue != null) { SelectMappedBufferResult selectMappedBufferResult = null; - if(offset<=0) { - offset = consumeOffset.get(); + if (offset <= 0) { + offset = consumeOffset.get(); } - result = transferQueue.consumeOneMessage(context,offset); + result = transferQueue.consumeOneMessage(context, offset); - }else{ - logger.error("transfer Id {} is not found",transferId); - result = new TransferQueue.TransferQueueConsumeResult(StatusCode.TRANSFER_QUEUE_NOT_FIND,null,beginOffset,0); + } else { + logger.error("transfer Id {} is not found", transferId); + result = new TransferQueue.TransferQueueConsumeResult(StatusCode.TRANSFER_QUEUE_NOT_FIND, null, beginOffset, 0); } - return result; + return result; } diff --git a/java/osx/broker/src/main/java/com/osx/broker/consumer/RedirectConsumer.java b/java/osx/broker/src/main/java/com/osx/broker/consumer/RedirectConsumer.java index 15f08605d0..84042e686b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/consumer/RedirectConsumer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/consumer/RedirectConsumer.java @@ -1,12 +1,23 @@ package com.osx.broker.consumer; -import com.osx.core.router.RouterInfo; import com.osx.core.constant.TransferStatus; +import com.osx.core.router.RouterInfo; import java.util.concurrent.atomic.AtomicBoolean; public class RedirectConsumer extends UnaryConsumer { + RouterInfo routerInfo; + TransferStatus transferStatus; + AtomicBoolean isWorking = new AtomicBoolean(false); + + + public RedirectConsumer(long consumerId, String transferId + ) { + super(consumerId, transferId); + transferStatus = TransferStatus.TRANSFERING; + } + public RouterInfo getRouterInfo() { return routerInfo; } @@ -15,9 +26,6 @@ public void setRouterInfo(RouterInfo routerInfo) { this.routerInfo = routerInfo; } - RouterInfo routerInfo; - - @Override public TransferStatus getTransferStatus() { return transferStatus; @@ -28,24 +36,13 @@ public void setTransferStatus(TransferStatus transferStatus) { this.transferStatus = transferStatus; } - TransferStatus transferStatus ; - - public boolean getIsWorking() { + public boolean getIsWorking() { return isWorking.get(); } - public boolean setIsWorking(boolean pre,boolean update) { - return isWorking.compareAndSet(pre,update); + public boolean setIsWorking(boolean pre, boolean update) { + return isWorking.compareAndSet(pre, update); } - AtomicBoolean isWorking = new AtomicBoolean(false); - - public RedirectConsumer(long consumerId,String transferId - ){ - super( consumerId, transferId); - transferStatus = TransferStatus.TRANSFERING; - } - - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/consumer/StreamConsumer.java b/java/osx/broker/src/main/java/com/osx/broker/consumer/StreamConsumer.java index c896a038c8..8341aecc5c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/consumer/StreamConsumer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/consumer/StreamConsumer.java @@ -1,6 +1,6 @@ package com.osx.broker.consumer; -public class StreamConsumer extends LocalQueueConsumer{ +public class StreamConsumer extends LocalQueueConsumer { public StreamConsumer(long consumerId, String transferId) { diff --git a/java/osx/broker/src/main/java/com/osx/broker/consumer/UnaryConsumer.java b/java/osx/broker/src/main/java/com/osx/broker/consumer/UnaryConsumer.java index 05953a1e4d..4df81cbd3f 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/consumer/UnaryConsumer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/consumer/UnaryConsumer.java @@ -1,12 +1,14 @@ package com.osx.broker.consumer; + +import com.osx.broker.ServiceContainer; +import com.osx.broker.queue.TransferQueue; +import com.osx.core.constant.ActionType; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; import com.osx.core.utils.FlowLogUtil; -import com.osx.broker.ServiceContainer; -import com.osx.broker.queue.TransferQueue; import io.grpc.stub.StreamObserver; import lombok.Data; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,111 +21,108 @@ public class UnaryConsumer extends LocalQueueConsumer { Logger logger = LoggerFactory.getLogger(UnaryConsumer.class); + ConcurrentLinkedQueue longPullingQueue; - public UnaryConsumer(long consumerId,String transferId + public UnaryConsumer(long consumerId, String transferId - ){ - super(consumerId,transferId); + ) { + super(consumerId, transferId); TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(transferId); - if(transferQueue!=null) { + if (transferQueue != null) { transferQueue.registeDestoryCallback(() -> { ServiceContainer.consumerManager.onComplete(transferId); }); } - longPullingQueue= new ConcurrentLinkedQueue(); - } - public int getLongPullingQueueSize(){ - return longPullingQueue.size(); + longPullingQueue = new ConcurrentLinkedQueue(); } - public LongPullingHold pollLongPullingQueue(){ - return longPullingQueue.poll(); + public int getLongPullingQueueSize() { + return longPullingQueue.size(); } - - - @Data - public static class LongPullingHold{ - Context context; - StreamObserver streamObserver; - long needOffset; + public LongPullingHold pollLongPullingQueue() { + return longPullingQueue.poll(); } - public void addLongPullingQueue(LongPullingHold longPullingHold){ + public void addLongPullingQueue(LongPullingHold longPullingHold) { longPullingQueue.add(longPullingHold); - logger.info("add long pulling queue , queue size {}",longPullingQueue.size()); + logger.info("add long pulling queue , queue size {}", longPullingQueue.size()); } - ConcurrentLinkedQueue longPullingQueue; - - - public synchronized int answerLongPulling(){ + public synchronized int answerLongPulling() { /* * 这里需要改为ack 后才加1 ,要不然这里会丢消息 */ int answerCount = 0; - TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(transferId); - List reputList= null; - while(this.longPullingQueue.size()>0){ - try { - long indexFileOffset = transferQueue.getIndexQueue().getLogicOffset().get(); - LongPullingHold longPullingHold = this.longPullingQueue.poll(); - //StreamObserver streamObserver = longPullingHold.getStreamObserver(); - long needOffset = longPullingHold.getNeedOffset(); - Context context = longPullingHold.getContext(); - context.setActionType("long-pulling-answer"); - TransferQueue.TransferQueueConsumeResult consumeResult = null; - if(needOffset<=0){ - long consumeOffset = this.consumeOffset.get(); - if(this.checkMsgIsArrive(consumeOffset)){ - /* - * 服务器记录的消费进度小于等于 index,则可以消费 - */ - consumeResult = this.consume(context,needOffset); - } - }else{ - if(this.checkMsgIsArrive(needOffset)){ - /* - * client 传入的offset 小于等于index,可以消费 - */ - consumeResult = this.consume(context,needOffset); - - } + TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(transferId); + List reputList = null; + while (this.longPullingQueue.size() > 0) { + try { + long indexFileOffset = transferQueue.getIndexQueue().getLogicOffset().get(); + LongPullingHold longPullingHold = this.longPullingQueue.poll(); + //StreamObserver streamObserver = longPullingHold.getStreamObserver(); + long needOffset = longPullingHold.getNeedOffset(); + Context context = longPullingHold.getContext(); + context.setActionType(ActionType.LONG_PULLING_ANSWER.getAlias()); + TransferQueue.TransferQueueConsumeResult consumeResult = null; + if (needOffset <= 0) { + long consumeOffset = this.consumeOffset.get(); + if (this.checkMsgIsArrive(consumeOffset)) { + /* + * 服务器记录的消费进度小于等于 index,则可以消费 + */ + consumeResult = this.consume(context, needOffset); } + } else { + if (this.checkMsgIsArrive(needOffset)) { + /* + * client 传入的offset 小于等于index,可以消费 + */ + consumeResult = this.consume(context, needOffset); + + } + } - //logger.info("{} longpulling consume {} return {}",transferId,consumeOffset.get(),message); - if (consumeResult != null) { - if(consumeResult.getMessage()!=null&&consumeResult.getMessage().getBody()!=null) - context.setDataSize(consumeResult.getMessage().getBody().length); - Pcp.Outbound consumeResponse = buildResponse(StatusCode.SUCCESS, "success",consumeResult); - answerCount++; - longPullingHold.getStreamObserver().onNext(consumeResponse); - longPullingHold.getStreamObserver().onCompleted(); + //logger.info("{} longpulling consume {} return {}",transferId,consumeOffset.get(),message); + if (consumeResult != null) { + if (consumeResult.getMessage() != null && consumeResult.getMessage().getBody() != null) + context.setDataSize(consumeResult.getMessage().getBody().length); + Osx.Outbound consumeResponse = buildResponse(StatusCode.SUCCESS, "success", consumeResult); + answerCount++; + longPullingHold.getStreamObserver().onNext(consumeResponse); + longPullingHold.getStreamObserver().onCompleted(); // if(needOffset<=0){ // this.addConsumeCount(1); 改成了由ack自增 // } - context.setReturnCode(StatusCode.SUCCESS); - FlowLogUtil.printFlowLogForConsumer(context); + context.setReturnCode(StatusCode.SUCCESS); + FlowLogUtil.printFlowLogForConsumer(context); - }else{ - /* - * 若没有消息,则放回队列 - */ - if(reputList==null) - reputList = new ArrayList<>(); - reputList.add(longPullingHold); + } else { + /* + * 若没有消息,则放回队列 + */ + if (reputList == null) + reputList = new ArrayList<>(); + reputList.add(longPullingHold); - } - }catch(Exception igore){ - igore.printStackTrace(); } + } catch (Exception igore) { + igore.printStackTrace(); } - if(reputList!=null){ - this.longPullingQueue.addAll(reputList); - } + } + if (reputList != null) { + this.longPullingQueue.addAll(reputList); + } return answerCount; } + @Data + public static class LongPullingHold { + Context context; + StreamObserver streamObserver; + long needOffset; + } + } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/BaseProto.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/BaseProto.java index 8486a7bb0c..65f653ec29 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/BaseProto.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/BaseProto.java @@ -3,11 +3,11 @@ import com.osx.core.utils.JsonUtil; -public abstract class BaseProto { +public abstract class BaseProto { - abstract T toProto(); + abstract T toProto(); - public String toString(){ - return JsonUtil.object2Json(this); - } + public String toString() { + return JsonUtil.object2Json(this); + } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ClusterManagerClient.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ClusterManagerClient.java index fc6d8effae..45ec47cb26 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ClusterManagerClient.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ClusterManagerClient.java @@ -10,83 +10,81 @@ import java.util.List; - public class ClusterManagerClient { Logger logger = LoggerFactory.getLogger(ClusterManagerClient.class); + CommandClient commandClient; - public ClusterManagerClient(CommandClient commandClient){ + public ClusterManagerClient(CommandClient commandClient) { this.commandClient = commandClient; } - CommandClient commandClient; - - public ErSessionMeta getOrCreateSession(ErSessionMeta sessionMeta){ - if(sessionMeta==null) - return null; - ErSessionMeta resultErSessionMeta = null; - Command.CommandResponse commandResponse = commandClient.call(SessionCommands.getOrCreateSession,sessionMeta); - logger.info("getOrCreateSession ======================={}",commandResponse); + public ErSessionMeta getOrCreateSession(ErSessionMeta sessionMeta) { + if (sessionMeta == null) + return null; + ErSessionMeta resultErSessionMeta = null; + Command.CommandResponse commandResponse = commandClient.call(SessionCommands.getOrCreateSession, sessionMeta); + logger.info("getOrCreateSession ======================={}", commandResponse); List result = commandResponse.getResultsList(); - if(result!=null){ + if (result != null) { try { - resultErSessionMeta = ErSessionMeta.parseFromPb( Meta.SessionMeta.parseFrom(result.get(0))); + resultErSessionMeta = ErSessionMeta.parseFromPb(Meta.SessionMeta.parseFrom(result.get(0))); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } } - logger.info("========= getOrCreateSession param {} result {}",sessionMeta,resultErSessionMeta); + logger.info("========= getOrCreateSession param {} result {}", sessionMeta, resultErSessionMeta); return resultErSessionMeta; } - public ErSessionMeta registerSession(ErSessionMeta sessionMeta ){ + public ErSessionMeta registerSession(ErSessionMeta sessionMeta) { - Command.CommandResponse commandResponse = commandClient.call(SessionCommands.registerSession,sessionMeta); + Command.CommandResponse commandResponse = commandClient.call(SessionCommands.registerSession, sessionMeta); List result = commandResponse.getResultsList(); ErSessionMeta resultErSessionMeta = null; - if(result!=null){ + if (result != null) { try { - resultErSessionMeta = ErSessionMeta.parseFromPb( Meta.SessionMeta.parseFrom(result.get(0))); + resultErSessionMeta = ErSessionMeta.parseFromPb(Meta.SessionMeta.parseFrom(result.get(0))); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } } - logger.info("========= registerSession param {} result {}",sessionMeta,resultErSessionMeta); + logger.info("========= registerSession param {} result {}", sessionMeta, resultErSessionMeta); return resultErSessionMeta; } - public ErSessionMeta getSession(ErSessionMeta sessionMeta ){ - Command.CommandResponse commandResponse = commandClient.call(SessionCommands.getSession,sessionMeta); + public ErSessionMeta getSession(ErSessionMeta sessionMeta) { + Command.CommandResponse commandResponse = commandClient.call(SessionCommands.getSession, sessionMeta); List result = commandResponse.getResultsList(); - ErSessionMeta resultErSessionMeta = null; - if(result!=null){ + ErSessionMeta resultErSessionMeta = null; + if (result != null) { try { - resultErSessionMeta = ErSessionMeta.parseFromPb( Meta.SessionMeta.parseFrom(result.get(0))); + resultErSessionMeta = ErSessionMeta.parseFromPb(Meta.SessionMeta.parseFrom(result.get(0))); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } } - logger.info("========= getSession param {} result {}",sessionMeta,resultErSessionMeta); + logger.info("========= getSession param {} result {}", sessionMeta, resultErSessionMeta); return resultErSessionMeta; } - public ErStore getOrCreateStore(ErStore input){ + public ErStore getOrCreateStore(ErStore input) { - Command.CommandResponse commandResponse = commandClient.call(MetaCommnads.getOrCreateStore,input); + Command.CommandResponse commandResponse = commandClient.call(MetaCommnads.getOrCreateStore, input); List result = commandResponse.getResultsList(); - logger.info("==========kkkkkkkkkkkk {}" ,commandResponse); - ErStore resultErStore = null; - if(result!=null){ + logger.info("==========kkkkkkkkkkkk {}", commandResponse); + ErStore resultErStore = null; + if (result != null) { try { - Meta.Store oriStore = Meta.Store.parseFrom(result.get(0)); - // logger.info("========== oriStore {}",oriStore); - resultErStore = ErStore.parseFromPb(oriStore); + Meta.Store oriStore = Meta.Store.parseFrom(result.get(0)); + // logger.info("========== oriStore {}",oriStore); + resultErStore = ErStore.parseFromPb(oriStore); //logger.info("=========getOrCreateStore param {} result {}",input,resultErStore); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandClient.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandClient.java index 1e2b1622ea..85eae5e454 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandClient.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandClient.java @@ -15,15 +15,13 @@ public class CommandClient { Logger logger = LoggerFactory.getLogger(CommandClient.class); + ErEndpoint erEndpoint; - public CommandClient(ErEndpoint erEndpoint){ + public CommandClient(ErEndpoint erEndpoint) { this.erEndpoint = erEndpoint; } - ErEndpoint erEndpoint; - - - private ManagedChannel buildManagedChannel(String ip ,int port){ + private ManagedChannel buildManagedChannel(String ip, int port) { NettyChannelBuilder channelBuilder = NettyChannelBuilder .forAddress(ip, port) .keepAliveTime(60, TimeUnit.MINUTES) @@ -36,28 +34,27 @@ private ManagedChannel buildManagedChannel(String ip ,int port){ .enableRetry() .retryBufferSize(16 << 20) .maxRetryAttempts(20); - channelBuilder.usePlaintext(); + channelBuilder.usePlaintext(); return channelBuilder.build(); } + public Command.CommandResponse call(CommandURI commandUri, BaseProto... baseProtos) { - public Command.CommandResponse call(CommandURI commandUri,BaseProto... baseProtos){ - - String id = System.currentTimeMillis() +"_"+commandUri.uri.toString(); - Command.CommandRequest commandRequest = Command.CommandRequest.newBuilder() - .setId(id) - .setUri(commandUri.uri.toString()) - .addAllArgs(Arrays.stream(baseProtos). - map((element)->((AbstractMessageLite)element.toProto()).toByteString()).collect(Collectors.toList())) + String id = System.currentTimeMillis() + "_" + commandUri.uri.toString(); + Command.CommandRequest commandRequest = Command.CommandRequest.newBuilder() + .setId(id) + .setUri(commandUri.uri.toString()) + .addAllArgs(Arrays.stream(baseProtos). + map((element) -> ((AbstractMessageLite) element.toProto()).toByteString()).collect(Collectors.toList())) .build(); - logger.info("===call {} {} id {}",erEndpoint.host,erEndpoint.port,id); + logger.info("===call {} {} id {}", erEndpoint.host, erEndpoint.port, id); - ManagedChannel managedChannel = buildManagedChannel(erEndpoint.host,erEndpoint.port); + ManagedChannel managedChannel = buildManagedChannel(erEndpoint.host, erEndpoint.port); CommandServiceGrpc.CommandServiceBlockingStub stub = CommandServiceGrpc.newBlockingStub(managedChannel); - Command.CommandResponse commandResponse = stub.call(commandRequest); + Command.CommandResponse commandResponse = stub.call(commandRequest); return commandResponse; } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandURI.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandURI.java index ee43dc0c0b..999b52d360 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandURI.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/CommandURI.java @@ -13,16 +13,17 @@ public class CommandURI { URI uri; String queryString; - Map queryPairs; + Map queryPairs; - public CommandURI(String prefix,String name ){ - this(prefix+"/"+name); + public CommandURI(String prefix, String name) { + this(prefix + "/" + name); } - public CommandURI(String uriString ){ + + public CommandURI(String uriString) { try { uri = new URI(uriString); queryString = uri.getQuery(); - queryPairs = new HashMap<>(); + queryPairs = new HashMap<>(); if (StringUtils.isBlank(queryString)) { queryPairs.put(Dict.ROUTE, uriString); } else { @@ -32,37 +33,34 @@ public CommandURI(String uriString ){ String key = pair; String value = Dict.EMPTY; if (idx > 0) - key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8.name()) ; - else - if (idx > 0 && pair.length() > idx + 1) - value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8.name()); + key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8.name()); + else if (idx > 0 && pair.length() > idx + 1) + value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8.name()); queryPairs.put(key, value); } } - } catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } } - - String getQueryValue(String key){ - return queryPairs.get(key); + String getQueryValue(String key) { + return queryPairs.get(key); } - String getRoute(){ - return queryPairs.get(Dict.ROUTE); + String getRoute() { + return queryPairs.get(Dict.ROUTE); } - String getName() { - return StringUtils.substringAfterLast(uri.getPath(), Dict.SLASH); + String getName() { + return StringUtils.substringAfterLast(uri.getPath(), Dict.SLASH); } - // class CommandURI(val uriString: String) { // val uri = new URI(uriString) // val queryString = uri.getQuery diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErEndpoint.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErEndpoint.java index 3270ed2a44..a45bb8819b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErEndpoint.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErEndpoint.java @@ -2,7 +2,23 @@ import com.webank.eggroll.core.meta.Meta; -public class ErEndpoint extends BaseProto{ +public class ErEndpoint extends BaseProto { + + String host; + int port; + + public ErEndpoint(String ip, int port) { + this.host = ip; + this.port = port; + } + + public static ErEndpoint parseFromPb(Meta.Endpoint endpoint) { + if (endpoint != null) { + ErEndpoint erEndpoint = new ErEndpoint(endpoint.getHost(), endpoint.getPort()); + return erEndpoint; + } + return null; + } public String getHost() { return host; @@ -12,12 +28,6 @@ public void setHost(String host) { this.host = host; } - public ErEndpoint(String ip, int port){ - this.host=ip; - this.port= port; - } - String host; - public int getPort() { return port; } @@ -26,20 +36,9 @@ public void setPort(int port) { this.port = port; } - int port; - - - public static ErEndpoint parseFromPb(Meta.Endpoint endpoint){ - if(endpoint!=null){ - ErEndpoint erEndpoint = new ErEndpoint(endpoint.getHost(), endpoint.getPort()); - return erEndpoint; - } - return null; - } - @Override Meta.Endpoint toProto() { - return Meta.Endpoint.newBuilder().setHost(host).setPort(port).build(); + return Meta.Endpoint.newBuilder().setHost(host).setPort(port).build(); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErFunctor.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErFunctor.java index 74e00ff903..edc3d1d107 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErFunctor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErFunctor.java @@ -5,20 +5,30 @@ import java.util.Map; -public class ErFunctor extends BaseProto{ +public class ErFunctor extends BaseProto { - public ErFunctor(String name,String serdes,byte[] body,Map options){ - this.name = name; + String name; + String serdes; + byte[] body; + Map options; + public ErFunctor(String name, String serdes, byte[] body, Map options) { + this.name = name; this.serdes = serdes; - this.body = body; + this.body = body; this.options = options; } - String name; - String serdes; - byte[] body; - Map options; + public static ErFunctor parseFromPb(Meta.Functor functor) { + if (functor == null) + return null; + String name = functor.getName(); + ByteString bodyByteString = functor.getBody(); + Map options = functor.getOptionsMap(); + String serdes = functor.getSerdes(); + ErFunctor erFunctor = new ErFunctor(name, serdes, bodyByteString != null ? bodyByteString.toByteArray() : null, options); + return erFunctor; + } @Override Meta.Functor toProto() { @@ -27,17 +37,6 @@ Meta.Functor toProto() { setSerdes(this.serdes).putAllOptions(options). setBody(ByteString.copyFrom(body)).build(); } - - public static ErFunctor parseFromPb(Meta.Functor functor){ - if(functor==null) - return null; - String name = functor.getName(); - ByteString bodyByteString = functor.getBody(); - Map options = functor.getOptionsMap(); - String serdes = functor.getSerdes(); - ErFunctor erFunctor= new ErFunctor(name,serdes,bodyByteString!=null?bodyByteString.toByteArray():null,options); - return erFunctor; - } } //case class ErFunctor(name: String = StringConstants.EMPTY, diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErJob.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErJob.java index 9a97757b85..3b822f3475 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErJob.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErJob.java @@ -3,30 +3,61 @@ import com.google.common.collect.Lists; import com.webank.eggroll.core.meta.Meta; - import java.util.List; import java.util.Map; import java.util.stream.Collectors; -public class ErJob extends BaseProto { +public class ErJob extends BaseProto { + String id; + String name; + List inputs; + List outputs; + List functors; + Map options; - public ErJob(String id,String name,List inputs, - List outputs, - List functors, - Map options){ + public ErJob(String id, String name, List inputs, + List outputs, + List functors, + Map options) { this.id = id; this.name = name; this.inputs = inputs; this.outputs = outputs; - this.functors = functors; + this.functors = functors; this.options = options; } - String id; - String name; - List inputs; - List outputs; + + public static ErJob parseFromPb(Meta.Job job) { + + if (job == null) + return null; + String id = job.getId(); + String name = job.getName(); + Map options = job.getOptionsMap(); + List inputMeta = job.getInputsList(); + List input = Lists.newArrayList(); + if (inputMeta != null) { + input = inputMeta.stream().map(ErStore::parseFromPb).collect(Collectors.toList()); + } + List outputMeta = job.getOutputsList(); + List output = Lists.newArrayList(); + if (output != null) { + output = outputMeta.stream().map(ErStore::parseFromPb).collect(Collectors.toList()); + } + List functors = Lists.newArrayList(); + List functorMeta = job.getFunctorsList(); + if (functorMeta != null) { + functors = functorMeta.stream().map(ErFunctor::parseFromPb).collect(Collectors.toList()); + } + + ErJob erJob = new ErJob(id, name, input, + output, + functors, + job.getOptionsMap()); + return erJob; + } public String getId() { return id; @@ -76,52 +107,17 @@ public void setOptions(Map options) { this.options = options; } - List functors; - Map options; - @Override Meta.Job toProto() { - return Meta.Job.newBuilder().setId(id).setName(name) + return Meta.Job.newBuilder().setId(id).setName(name) .addAllFunctors(this.functors.stream().map(ErFunctor::toProto).collect(Collectors.toList())). - addAllInputs(inputs.stream().map(ErStore::toProto).collect(Collectors.toList())).putAllOptions(options).build(); - - } - - public static ErJob parseFromPb(Meta.Job job){ - - if(job ==null) - return null; - String id =job.getId(); - String name = job.getName(); - Map options = job.getOptionsMap(); - List inputMeta = job.getInputsList(); - List input = Lists.newArrayList(); - if(inputMeta!=null) { - input = inputMeta.stream().map(ErStore::parseFromPb).collect(Collectors.toList()); - } - List outputMeta = job.getOutputsList(); - List output = Lists.newArrayList(); - if(output!=null){ - output = outputMeta.stream().map(ErStore::parseFromPb).collect(Collectors.toList()); - } - List functors = Lists.newArrayList(); - List functorMeta = job.getFunctorsList(); - if(functorMeta!=null){ - functors = functorMeta.stream().map(ErFunctor::parseFromPb).collect(Collectors.toList()); - } + addAllInputs(inputs.stream().map(ErStore::toProto).collect(Collectors.toList())).putAllOptions(options).build(); - ErJob erJob = new ErJob( id, name,input, - output, - functors, - job.getOptionsMap()); - return erJob; } } - - //case class ErJob(id: String, // name: String = StringConstants.EMPTY, // inputs: Array[ErStore], diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErPartition.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErPartition.java index a22665a760..40218f2ee1 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErPartition.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErPartition.java @@ -2,9 +2,14 @@ import com.webank.eggroll.core.meta.Meta; -public class ErPartition extends BaseProto{ +public class ErPartition extends BaseProto { - public ErPartition(Integer id ,ErStoreLocator storeLocator,ErProcessor processor,int rankInNode ){ + Integer id; + ErStoreLocator storeLocator; + ErProcessor processor; + int rankInNode; + + public ErPartition(Integer id, ErStoreLocator storeLocator, ErProcessor processor, int rankInNode) { this.id = id; this.storeLocator = storeLocator; this.processor = processor; @@ -12,9 +17,18 @@ public ErPartition(Integer id ,ErStoreLocator storeLocator,ErProcessor process } + public static ErPartition parseFromPb(Meta.Partition partition) { - Integer id; - ErStoreLocator storeLocator; + if (partition != null) { + Meta.StoreLocator storeLocator = partition.getStoreLocator(); + ErStoreLocator erStoreLocator = ErStoreLocator.parseFromPb(storeLocator); + ErProcessor erProcessor = ErProcessor.parseFromPb(partition.getProcessor()); + ErPartition erPartition = new ErPartition(partition.getId(), erStoreLocator, erProcessor, partition.getRankInNode()); + return erPartition; + } else { + return null; + } + } public Integer getId() { return id; @@ -48,40 +62,23 @@ public void setRankInNode(int rankInNode) { this.rankInNode = rankInNode; } - ErProcessor processor; - int rankInNode; - - public String toPath(String delim){ + public String toPath(String delim) { return String.join(delim, storeLocator.toPath(delim = delim), id.toString()); } @Override Meta.Partition toProto() { - Meta.Partition.Builder builder = Meta.Partition.newBuilder(); + Meta.Partition.Builder builder = Meta.Partition.newBuilder(); builder.setId(id); builder.setRankInNode(rankInNode); - if(storeLocator!=null) + if (storeLocator != null) builder.setStoreLocator(storeLocator.toProto()); - if(processor!=null){ + if (processor != null) { builder.setProcessor(this.processor.toProto()); } return builder.build(); } - - - public static ErPartition parseFromPb(Meta.Partition partition ){ - - if(partition!=null) { - Meta.StoreLocator storeLocator = partition.getStoreLocator(); - ErStoreLocator erStoreLocator = ErStoreLocator.parseFromPb(storeLocator); - ErProcessor erProcessor = ErProcessor.parseFromPb(partition.getProcessor()); - ErPartition erPartition = new ErPartition(partition.getId(), erStoreLocator, erProcessor, partition.getRankInNode()); - return erPartition; - }else{ - return null; - } - } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErProcessor.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErProcessor.java index 54d5e9fc9d..fd52a07771 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErProcessor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErProcessor.java @@ -5,9 +5,33 @@ import java.util.concurrent.ConcurrentHashMap; -public class ErProcessor extends BaseProto{ +public class ErProcessor extends BaseProto { - long id = -1; + long id = -1; + long serverNodeId = -1; + String name = Dict.EMPTY; + String processorType = Dict.EMPTY; + String status = Dict.EMPTY; + ErEndpoint commandEndpoint; + ErEndpoint transferEndpoint; + int pid = -1; + ConcurrentHashMap options; + + public static ErProcessor parseFromPb(Meta.Processor processor) { + if (processor != null) { + ErProcessor erProcessor = new ErProcessor(); + erProcessor.setId(processor.getId()); + erProcessor.setServerNodeId(processor.getServerNodeId()); + erProcessor.setName(processor.getName()); + erProcessor.setProcessorType(processor.getProcessorType()); + erProcessor.setCommandEndpoint(ErEndpoint.parseFromPb(processor.getCommandEndpoint())); + erProcessor.setTransferEndpoint(ErEndpoint.parseFromPb(processor.getTransferEndpoint())); + erProcessor.setPid(processor.getPid()); + return erProcessor; + } else { + return null; + } + } public long getId() { return id; @@ -81,16 +105,7 @@ public void setOptions(ConcurrentHashMap options) { this.options = options; } - long serverNodeId = -1; - String name= Dict.EMPTY; - String processorType = Dict.EMPTY; - String status = Dict.EMPTY; - ErEndpoint commandEndpoint ; - ErEndpoint transferEndpoint; - int pid = -1; - ConcurrentHashMap options; - - public Meta.Processor toProto(){ + public Meta.Processor toProto() { return Meta.Processor.newBuilder(). setId(this.id). @@ -103,25 +118,6 @@ public Meta.Processor toProto(){ } - - - public static ErProcessor parseFromPb(Meta.Processor processor){ - if(processor!=null) { - ErProcessor erProcessor = new ErProcessor(); - erProcessor.setId(processor.getId()); - erProcessor.setServerNodeId(processor.getServerNodeId()); - erProcessor.setName(processor.getName()); - erProcessor.setProcessorType(processor.getProcessorType()); - erProcessor.setCommandEndpoint(ErEndpoint.parseFromPb(processor.getCommandEndpoint())); - erProcessor.setTransferEndpoint(ErEndpoint.parseFromPb(processor.getTransferEndpoint())); - erProcessor.setPid(processor.getPid()); - return erProcessor; - }else{ - return null; - } - } - - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErRollSiteHeader.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErRollSiteHeader.java index ba90b2606c..5893ef1f89 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErRollSiteHeader.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErRollSiteHeader.java @@ -1,6 +1,6 @@ package com.osx.broker.eggroll; -import com.beust.jcommander.internal.Lists; +import com.google.common.collect.Lists; import com.webank.eggroll.core.transfer.Transfer; import java.util.List; @@ -8,6 +8,48 @@ public class ErRollSiteHeader extends BaseProto { + String rollSiteSessionId; + String name; + String tag; + String srcRole; + String srcPartyId; + String dstRole; + String dstPartyId; + String dataType; + Map options; + Integer totalPartitions; + Integer partitionId; + Long totalStreams; + Long totalBatches; + Long streamSeq; + Long batchSeq; + String stage; + + public static ErRollSiteHeader parseFromPb(Transfer.RollSiteHeader rollSiteHeader) { + if (rollSiteHeader != null) { + ErRollSiteHeader erRollSiteHeader = new ErRollSiteHeader(); + erRollSiteHeader.rollSiteSessionId = rollSiteHeader.getRollSiteSessionId(); + erRollSiteHeader.name = rollSiteHeader.getName(); + erRollSiteHeader.tag = rollSiteHeader.getTag(); + erRollSiteHeader.srcRole = rollSiteHeader.getSrcRole(); + erRollSiteHeader.srcPartyId = rollSiteHeader.getSrcPartyId(); + erRollSiteHeader.dstRole = rollSiteHeader.getDstRole(); + erRollSiteHeader.dstPartyId = rollSiteHeader.getDstPartyId(); + erRollSiteHeader.dataType = rollSiteHeader.getDataType(); + erRollSiteHeader.options = rollSiteHeader.getOptionsMap(); + erRollSiteHeader.totalPartitions = rollSiteHeader.getTotalPartitions(); + erRollSiteHeader.partitionId = rollSiteHeader.getPartitionId(); + erRollSiteHeader.totalStreams = rollSiteHeader.getTotalStreams(); + erRollSiteHeader.streamSeq = rollSiteHeader.getStreamSeq(); + erRollSiteHeader.batchSeq = rollSiteHeader.getBatchSeq(); + erRollSiteHeader.stage = rollSiteHeader.getStage(); + return erRollSiteHeader; + } else { + return null; + } + + } + public String getRollSiteSessionId() { return rollSiteSessionId; } @@ -136,53 +178,10 @@ public void setStage(String stage) { this.stage = stage; } - String rollSiteSessionId; - String name; - String tag; - String srcRole; - String srcPartyId; - String dstRole; - String dstPartyId; - String dataType; - Map options; - Integer totalPartitions; - Integer partitionId; - Long totalStreams; - Long totalBatches; - Long streamSeq; - Long batchSeq; - String stage; - - public static ErRollSiteHeader parseFromPb(Transfer.RollSiteHeader rollSiteHeader){ - if(rollSiteHeader!=null) { - ErRollSiteHeader erRollSiteHeader = new ErRollSiteHeader(); - erRollSiteHeader.rollSiteSessionId = rollSiteHeader.getRollSiteSessionId(); - erRollSiteHeader.name = rollSiteHeader.getName(); - erRollSiteHeader.tag = rollSiteHeader.getTag(); - erRollSiteHeader.srcRole = rollSiteHeader.getSrcRole(); - erRollSiteHeader.srcPartyId = rollSiteHeader.getSrcPartyId(); - erRollSiteHeader.dstRole = rollSiteHeader.getDstRole(); - erRollSiteHeader.dstPartyId = rollSiteHeader.getDstPartyId(); - erRollSiteHeader.dataType = rollSiteHeader.getDataType(); - erRollSiteHeader.options = rollSiteHeader.getOptionsMap(); - erRollSiteHeader.totalPartitions = rollSiteHeader.getTotalPartitions(); - erRollSiteHeader.partitionId = rollSiteHeader.getPartitionId(); - erRollSiteHeader.totalStreams = rollSiteHeader.getTotalStreams(); - erRollSiteHeader.streamSeq = rollSiteHeader.getStreamSeq(); - erRollSiteHeader.batchSeq = rollSiteHeader.getBatchSeq(); - erRollSiteHeader.stage = rollSiteHeader.getStage(); - return erRollSiteHeader; - }else { - return null; - } - - } - - - public String getRsKey(String delim ,String prefix){ + public String getRsKey(String delim, String prefix) { List finalArray = - Lists.newArrayList(prefix,rollSiteSessionId, name, tag, srcRole, srcPartyId, dstRole, dstPartyId); - return String.join(delim, finalArray); + Lists.newArrayList(prefix, rollSiteSessionId, name, tag, srcRole, srcPartyId, dstRole, dstPartyId); + return String.join(delim, finalArray); } @Override diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSession.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSession.java index 3df2085886..c6a991019b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSession.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSession.java @@ -11,7 +11,7 @@ import static com.osx.core.config.MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP; import static com.osx.core.config.MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT; -public class ErSession { +public class ErSession { Logger logger = LoggerFactory.getLogger(ErSession.class); // class ErSession(val sessionId: String = s"er_session_jvm_${TimeUtils.getNowMs()}_${RuntimeUtils.siteLocalAddress}", @@ -20,13 +20,24 @@ public class ErSession { // createIfNotExists: Boolean = true, // var processors: Array[ErProcessor] = Array(), // options: Map[String, String] = Map()) + SessionStatus status = SessionStatus.NEW; + String sessionId; + String tag = ""; + String name = ""; + boolean createIfNotExists = true; + List processors = Lists.newArrayList(); + Map options = Maps.newHashMap(); + ErSessionMeta erSessionMeta; + ClusterManagerClient clusterManagerClient; + List rollsBuffer; + Map> eggBuffer = Maps.newHashMap(); - public ErSession(String sessionId,boolean createIfNotExists){ + public ErSession(String sessionId, boolean createIfNotExists) { this.sessionId = sessionId; this.createIfNotExists = createIfNotExists; - clusterManagerClient = new ClusterManagerClient(new CommandClient(new ErEndpoint(PROPERTY_EGGROLL_CLUSTER_MANANGER_IP,PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT.intValue()))); - ErSessionMeta erSessionMetaArgs = new ErSessionMeta(); + clusterManagerClient = new ClusterManagerClient(new CommandClient(new ErEndpoint(PROPERTY_EGGROLL_CLUSTER_MANANGER_IP, PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT.intValue()))); + ErSessionMeta erSessionMetaArgs = new ErSessionMeta(); erSessionMetaArgs.setId(sessionId); erSessionMetaArgs.setName(name); @@ -34,23 +45,23 @@ public ErSession(String sessionId,boolean createIfNotExists){ erSessionMetaArgs.setTag(tag); erSessionMetaArgs.setProcessors(this.processors); erSessionMetaArgs.setOptions(options); - logger.info("create ErSession ============{}",erSessionMetaArgs); - if(createIfNotExists){ - if(processors.isEmpty()){ - erSessionMeta= clusterManagerClient.getOrCreateSession(erSessionMetaArgs); - }else{ + logger.info("create ErSession ============{}", erSessionMetaArgs); + if (createIfNotExists) { + if (processors.isEmpty()) { + erSessionMeta = clusterManagerClient.getOrCreateSession(erSessionMetaArgs); + } else { - erSessionMeta = clusterManagerClient.registerSession(erSessionMetaArgs); + erSessionMeta = clusterManagerClient.registerSession(erSessionMetaArgs); } - }else{ - erSessionMeta = clusterManagerClient.getSession(erSessionMetaArgs); + } else { + erSessionMeta = clusterManagerClient.getSession(erSessionMetaArgs); } - logger.info("===============dddddd=============={} ",erSessionMeta); + logger.info("===============dddddd=============={} ", erSessionMeta); - processors = erSessionMeta.getProcessors(); + processors = erSessionMeta.getProcessors(); status = SessionStatus.valueOf(erSessionMeta.getStatus()); // processors.foreach(p => { // val processorType = p.processorType @@ -63,30 +74,26 @@ public ErSession(String sessionId,boolean createIfNotExists){ // } // }) - processors.forEach((processor->{ - if(processor.getProcessorType().toLowerCase().startsWith("egg_")){ - - if(eggBuffer.get(processor.getServerNodeId())!=null){ - eggBuffer.get(processor.getServerNodeId()).add(processor); - }else{ - List list = Lists.newArrayList(processor); - eggBuffer.put(processor.getServerNodeId(),list); - }; - }else if( processor.getProcessorType().toLowerCase().startsWith("roll_")){ + processors.forEach((processor -> { + if (processor.getProcessorType().toLowerCase().startsWith("egg_")) { + + if (eggBuffer.get(processor.getServerNodeId()) != null) { + eggBuffer.get(processor.getServerNodeId()).add(processor); + } else { + List list = Lists.newArrayList(processor); + eggBuffer.put(processor.getServerNodeId(), list); + } + ; + } else if (processor.getProcessorType().toLowerCase().startsWith("roll_")) { rollsBuffer.add(processor); + } else { + throw new IllegalArgumentException("processor type ${processorType} not supported in roll pair"); } - else { - throw new IllegalArgumentException("processor type ${processorType} not supported in roll pair"); - } })); - - } - SessionStatus status = SessionStatus.NEW; - public String getSessionId() { return sessionId; } @@ -95,10 +102,6 @@ public void setSessionId(String sessionId) { this.sessionId = sessionId; } - String sessionId; - String tag=""; - String name=""; - public SessionStatus getStatus() { return status; } @@ -107,7 +110,6 @@ public void setStatus(SessionStatus status) { this.status = status; } - public String getTag() { return tag; } @@ -167,22 +169,18 @@ public void setClusterManagerClient(ClusterManagerClient clusterManagerClient) { public List getRollsBuffer() { return rollsBuffer; } + public void setRollsBuffer(List rollsBuffer) { this.rollsBuffer = rollsBuffer; } + public Map> getEggBuffer() { return eggBuffer; } + public void setEggBuffer(Map> eggBuffer) { this.eggBuffer = eggBuffer; } - boolean createIfNotExists= true; - List processors= Lists.newArrayList(); - Map options= Maps.newHashMap(); - ErSessionMeta erSessionMeta; - ClusterManagerClient clusterManagerClient; - List rollsBuffer; - Map> eggBuffer = Maps.newHashMap(); // private val rolls_buffer = ArrayBuffer[ErProcessor]() // private val eggs_buffer = mutable.Map[Long, ArrayBuffer[ErProcessor]]() // def routeToEgg(partition: ErPartition): ErProcessor = { @@ -193,23 +191,18 @@ public void setEggBuffer(Map> eggBuffer) { // eggs(serverNodeId)(eggIdx) // } + public ErProcessor routeToEgg(ErPartition erPartition) { + long getServerNodeId = erPartition.processor.getServerNodeId(); + if (eggBuffer.get(getServerNodeId) != null) { - public ErProcessor routeToEgg(ErPartition erPartition){ - long getServerNodeId = erPartition.processor.getServerNodeId(); - if( eggBuffer.get(getServerNodeId)!=null){ - - int eggCountOnServerNode = eggBuffer.get(getServerNodeId).size(); - int eggIdx = erPartition.id / eggBuffer.size() % eggCountOnServerNode; - return eggBuffer.get(getServerNodeId).get(eggIdx); + int eggCountOnServerNode = eggBuffer.get(getServerNodeId).size(); + int eggIdx = erPartition.id / eggBuffer.size() % eggCountOnServerNode; + return eggBuffer.get(getServerNodeId).get(eggIdx); } return null; } - - - - // private var status = SessionStatus.NEW // val clusterManagerClient = new ClusterManagerClient(options) // private var sessionMetaArg = ErSessionMeta( diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSessionMeta.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSessionMeta.java index 415b126275..77cf5dc5a5 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSessionMeta.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErSessionMeta.java @@ -9,6 +9,30 @@ import java.util.stream.Collectors; public class ErSessionMeta extends BaseProto { + String id; + String status; + String name; + int totalProcCount; + int activeProcCount; + String tag; + List processors; + Map options; + + static ErSessionMeta parseFromPb(Meta.SessionMeta sessionMeta) { + if (sessionMeta == null) { + return null; + } + + ErSessionMeta erSessionMeta = new ErSessionMeta(); + erSessionMeta.id = sessionMeta.getId(); + erSessionMeta.status = sessionMeta.getStatus(); + erSessionMeta.name = sessionMeta.getName(); + erSessionMeta.tag = sessionMeta.getTag(); + erSessionMeta.options = sessionMeta.getOptionsMap(); + erSessionMeta.processors = sessionMeta.getProcessorsList().stream().map(ErProcessor::parseFromPb).collect(Collectors.toList()); + return erSessionMeta; + } + public String getId() { return id; } @@ -73,19 +97,8 @@ public void setOptions(Map options) { this.options = options; } - String id; - String status; - String name; - int totalProcCount; - int activeProcCount; - String tag; - List processors; - Map options; - - - - public Meta.SessionMeta toProto(){ - Meta.SessionMeta.Builder builder = Meta.SessionMeta.newBuilder() + public Meta.SessionMeta toProto() { + Meta.SessionMeta.Builder builder = Meta.SessionMeta.newBuilder() .setId(this.id) .setName(this.name) .setStatus(this.status) @@ -93,32 +106,15 @@ public Meta.SessionMeta toProto(){ .addAllProcessors(this.processors.stream().map(ErProcessor::toProto).collect(Collectors.toList())) .putAllOptions(this.options); - return builder.build(); + return builder.build(); } - - static ErSessionMeta parseFromPb( Meta.SessionMeta sessionMeta) { - if(sessionMeta==null){ - return null; - } - - ErSessionMeta erSessionMeta = new ErSessionMeta(); - erSessionMeta.id = sessionMeta.getId(); - erSessionMeta.status = sessionMeta.getStatus(); - erSessionMeta.name = sessionMeta.getName(); - erSessionMeta.tag = sessionMeta.getTag(); - erSessionMeta.options = sessionMeta.getOptionsMap(); - erSessionMeta.processors = sessionMeta.getProcessorsList().stream().map(ErProcessor::parseFromPb).collect(Collectors.toList()); - return erSessionMeta; - } - - public String toString(){ + public String toString() { return JsonUtil.object2Json(this); } } - // override def toProto[T >: PbMessage](): Meta.SessionMeta = { // val builder = Meta.SessionMeta.newBuilder() // .setId(src.id) diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStore.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStore.java index 2d2f333882..83af0bc892 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStore.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStore.java @@ -6,23 +6,55 @@ import com.osx.core.utils.JsonUtil; import com.webank.eggroll.core.meta.Meta; - import java.util.List; import java.util.Map; import java.util.stream.Collectors; -public class ErStore extends BaseProto{ +public class ErStore extends BaseProto { - public String toString(){ - return JsonUtil.object2Json(this); - } + ErStoreLocator storeLocator; + List partitions = Lists.newArrayList(); + Map options = Maps.newHashMap(); - public ErStore(ErStoreLocator storeLocator,List partitions,Map options){ + public ErStore(ErStoreLocator storeLocator, List partitions, Map options) { this.storeLocator = storeLocator; this.partitions = partitions; this.options = options; } + public static ErStore parseFromPb(Meta.Store store) { + ErStoreLocator erStoreLocator = ErStoreLocator.parseFromPb(store.getStoreLocator()); + + List erPartitions = store.getPartitionsList().stream().map(ErPartition::parseFromPb).collect(Collectors.toList()); + ErStore erStore = new ErStore(erStoreLocator, erPartitions, store.getOptionsMap()); + return erStore; + } + + public static void main(String[] args) { + +// String namespace ,String name,String storeType, +// int totalPartitions ,String partitioner, +// String serdes + + ErStoreLocator erStoreLocator = new ErStoreLocator("mynamespace", + "myname", "mypath", "mystoreType", 1, "xxxx", "myserdes"); + + List partitions = Lists.newArrayList(); + ErPartition erPartition = new ErPartition(11, null, null, 33); + partitions.add(erPartition); + + ErStore erStore = new ErStore(erStoreLocator, partitions, Maps.newHashMap()); + + System.err.println(erStore.toProto()); + + System.err.println(ErStore.parseFromPb(erStore.toProto())); + + } + + public String toString() { + return JsonUtil.object2Json(this); + } + public ErStoreLocator getStoreLocator() { return storeLocator; } @@ -47,59 +79,21 @@ public void setOptions(Map options) { this.options = options; } - ErStoreLocator storeLocator; - List partitions= Lists.newArrayList(); - Map options= Maps.newHashMap(); - - - public ErPartition getPartition(int index){ + public ErPartition getPartition(int index) { return partitions.get(index); } - @Override Meta.Store toProto() { Meta.Store.Builder builder = Meta.Store.newBuilder() .setStoreLocator(this.storeLocator.toProto()) .addAllPartitions(this.partitions.stream().map(ErPartition::toProto).collect(Collectors.toList())) .putAllOptions(this.options); - return builder.build(); - } - - - public static ErStore parseFromPb(Meta.Store store){ - ErStoreLocator erStoreLocator = ErStoreLocator.parseFromPb(store.getStoreLocator()); - - List erPartitions = store.getPartitionsList().stream().map(ErPartition::parseFromPb).collect(Collectors.toList()); - ErStore erStore = new ErStore(erStoreLocator,erPartitions,store.getOptionsMap()); - return erStore; - } - - - public static void main(String[] args){ - -// String namespace ,String name,String storeType, -// int totalPartitions ,String partitioner, -// String serdes - - ErStoreLocator erStoreLocator = new ErStoreLocator("mynamespace", - "myname","mypath","mystoreType",1,"xxxx","myserdes"); - - List partitions = Lists.newArrayList(); - ErPartition erPartition = new ErPartition(11,null,null,33); - partitions.add(erPartition); - - ErStore erStore = new ErStore(erStoreLocator,partitions,Maps.newHashMap()); - - System.err.println(erStore.toProto()); - - System.err.println(ErStore.parseFromPb(erStore.toProto())); - + return builder.build(); } } - // implicit class ErStoreToPbMessage(src: ErStore) extends PbMessageSerializer { // override def toProto[T >: PbMessage](): Meta.Store = { // val builder = Meta.Store.newBuilder() diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStoreLocator.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStoreLocator.java index 8feffe2221..5b355db363 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStoreLocator.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErStoreLocator.java @@ -5,15 +5,33 @@ import com.webank.eggroll.core.meta.Meta; import org.apache.commons.lang3.StringUtils; -public class ErStoreLocator extends BaseProto{ +public class ErStoreLocator extends BaseProto { - public ErStoreLocator(String namespace ,String name, + String storeType; + + +// ( +// namespace = namespace, +// name = name, +// storeType = storeType, +// totalPartitions = totalPartitions, +// partitioner = options.getOrElse(StringConstants.PARTITIONER, PartitionerTypes.BYTESTRING_HASH), +// serdes = options.getOrElse(StringConstants.SERDES, defaultSerdesType) +// ) + String namespace; + String name; + String path; + int totalPartitions; + String partitioner; + String serdes; + + public ErStoreLocator(String namespace, String name, String path, - String storeType, - int totalPartitions ,String partitioner, - String serdes - ){ + String storeType, + int totalPartitions, String partitioner, + String serdes + ) { this.namespace = namespace; this.name = name; this.path = path; @@ -24,18 +42,20 @@ public ErStoreLocator(String namespace ,String name, } + public static ErStoreLocator parseFromPb(Meta.StoreLocator storeLocator) { -// ( -// namespace = namespace, -// name = name, -// storeType = storeType, -// totalPartitions = totalPartitions, -// partitioner = options.getOrElse(StringConstants.PARTITIONER, PartitionerTypes.BYTESTRING_HASH), -// serdes = options.getOrElse(StringConstants.SERDES, defaultSerdesType) -// ) + ErStoreLocator erStoreLocator = new ErStoreLocator(storeLocator.getNamespace(), + storeLocator.getName(), + storeLocator.getPath(), + storeLocator.getStoreType(), + storeLocator.getTotalPartitions(), + storeLocator.getPartitioner(), + storeLocator.getSerdes() + ); + return erStoreLocator; - String storeType; + } public String getStoreType() { return storeType; @@ -93,30 +113,23 @@ public void setSerdes(String serdes) { this.serdes = serdes; } - String namespace; - String name; - String path; - int totalPartitions; - String partitioner; - String serdes; - - String toPath(String delim){ + String toPath(String delim) { if (!StringUtils.isBlank(path)) { return path; } else { - return String.join(delim, storeType, namespace, name); + return String.join(delim, storeType, namespace, name); } } @Override Meta.StoreLocator toProto() { - Meta.StoreLocator.Builder builder = Meta.StoreLocator.newBuilder(); + Meta.StoreLocator.Builder builder = Meta.StoreLocator.newBuilder(); return builder.setName(name) - .setNamespace(namespace) - .setPartitioner(partitioner) - .setStoreType(storeType). + .setNamespace(namespace) + .setPartitioner(partitioner) + .setStoreType(storeType). setPath(path). setTotalPartitions(totalPartitions). setSerdes(serdes).build(); @@ -124,28 +137,11 @@ Meta.StoreLocator toProto() { } - public static ErStoreLocator parseFromPb(Meta.StoreLocator storeLocator){ - - - ErStoreLocator erStoreLocator = new ErStoreLocator(storeLocator.getNamespace(), - storeLocator.getName(), - storeLocator.getPath(), - storeLocator.getStoreType(), - storeLocator.getTotalPartitions(), - storeLocator.getPartitioner(), - storeLocator.getSerdes() - ); - return erStoreLocator; - - } - - - public String toString(){ + public String toString() { return JsonUtil.object2Json(this); } - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErTask.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErTask.java index 9962dd7f9f..b7bb00af59 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErTask.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/ErTask.java @@ -10,6 +10,12 @@ public class ErTask extends BaseProto { + String id; + String name = Dict.EMPTY; + List inputs = new ArrayList<>(); + List outputs = new ArrayList<>(); + ErJob job; + public ErTask(String id, String name, List inputs, List outputs, ErJob erJob) { this.id = id; this.name = name; @@ -18,6 +24,25 @@ public ErTask(String id, String name, List inputs, List inputs = null; + if (task.getInputsList() != null) { + inputs = task.getInputsList().stream().map(ErPartition::parseFromPb).collect(Collectors.toList()); + } + List outputs = null; + if (task.getOutputsList() != null) { + outputs = task.getOutputsList().stream().map(ErPartition::parseFromPb).collect(Collectors.toList()); + } + ErJob erJob = ErJob.parseFromPb(task.getJob()); + //String id, String name, List inputs, List outputs, ErJob erJob + ErTask erTask = new ErTask(id, name, inputs, outputs, erJob); + return erTask; + } + public String getId() { return id; } @@ -58,12 +83,6 @@ public void setJob(ErJob job) { this.job = job; } - String id; - String name = Dict.EMPTY; - List inputs = new ArrayList<>(); - List outputs = new ArrayList<>(); - ErJob job; - @Override Meta.Task toProto() { return Meta.Task.newBuilder().setId(this.id).setName(name).addAllInputs(inputs.stream().map(ErPartition::toProto).collect(Collectors.toList())) @@ -71,28 +90,6 @@ Meta.Task toProto() { } - public static ErTask parseFromPb(Meta.Task task) { - if(task==null) - return null; - String id = task.getId(); - String name = task.getName(); - List inputs =null; - if(task.getInputsList()!=null) { - inputs = task.getInputsList().stream().map(ErPartition::parseFromPb).collect(Collectors.toList()); - } - List outputs=null; - if(task.getOutputsList()!=null){ - outputs = task.getOutputsList().stream().map(ErPartition::parseFromPb).collect(Collectors.toList()); - } - ErJob erJob = ErJob.parseFromPb(task.getJob()); - //String id, String name, List inputs, List outputs, ErJob erJob - ErTask erTask = new ErTask(id,name,inputs,outputs,erJob); - return erTask; - } - - - - //case class ErTask(id: String, // name: String = StringConstants.EMPTY, diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/IdUtils.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/IdUtils.java index c590fa43ae..5fd040871f 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/IdUtils.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/IdUtils.java @@ -6,23 +6,20 @@ public class IdUtils { private static String job = "job"; private static String task = "task"; - public static String generateJobId(String sessionId,String tag, String delim){ + public static String generateJobId(String sessionId, String tag, String delim) { String result = String.join(delim, sessionId, "scala", job, TimeUtils.getNowMs(null)); if (StringUtils.isBlank(tag)) { return result; + } else { + return result + "_" + tag; } - else{ - return result+"_"+tag; - } - //s"${result}_${tag}" + //s"${result}_${tag}" } - } - // // object IdUtils { //private val job = "job" diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/MetaCommnads.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/MetaCommnads.java index d2a977d8d1..6f8c192328 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/MetaCommnads.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/MetaCommnads.java @@ -3,9 +3,7 @@ public class MetaCommnads { - - - static String prefix = "v1/cluster-manager/metadata"; - static CommandURI getOrCreateStore =new CommandURI(prefix , "getOrCreateStore"); + static String prefix = "v1/cluster-manager/metadata"; + static CommandURI getOrCreateStore = new CommandURI(prefix, "getOrCreateStore"); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkPushRespSO.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkPushRespSO.java index e2a3bee107..48e924e353 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkPushRespSO.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkPushRespSO.java @@ -14,6 +14,13 @@ public class PutBatchSinkPushRespSO implements StreamObserver { + StreamObserver eggSiteServicerPushRespSO; + Proxy.Metadata reqHeader; + Future commandFuture; + CountDownLatch finishLatch; + Logger logger = LoggerFactory.getLogger(PutBatchSinkPushRespSO.class); + + public PutBatchSinkPushRespSO(Proxy.Metadata reqHeader, Future commandFuture, StreamObserver eggSiteServicerPushRespSO, @@ -25,16 +32,6 @@ public PutBatchSinkPushRespSO(Proxy.Metadata reqHeader, this.finishLatch = finishLatch; } - StreamObserver eggSiteServicerPushRespSO; - - Proxy.Metadata reqHeader; - - Future commandFuture; - CountDownLatch finishLatch; - - - Logger logger = LoggerFactory.getLogger(PutBatchSinkPushRespSO.class); - @Override public void onNext(Transfer.TransferBatch resp) { // logTrace(s"PutBatchSinkPushRespSO.onNext calling. rsKey=${rsKey}, rsHeader=${rsHeader}, transferHeader=${oneLineStringTransferHeader}") @@ -58,7 +55,7 @@ public void onNext(Transfer.TransferBatch resp) { eggSiteServicerPushRespSO.onCompleted(); } catch (Exception e) { e.printStackTrace(); - logger.error("==========" ,e); + logger.error("==========", e); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkUtil.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkUtil.java index a9e01c00cb..99b49269bf 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkUtil.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/PutBatchSinkUtil.java @@ -7,26 +7,24 @@ import java.util.concurrent.TimeUnit; public class PutBatchSinkUtil { - public static LoadingCache sessionCache = + public static LoadingCache sessionCache = - CacheBuilder.newBuilder() - .maximumSize(2000) - .expireAfterWrite(10, TimeUnit.MINUTES) - .concurrencyLevel(100) - .recordStats() - .softValues() - .build(new CacheLoader() { - @Override - public ErSession load(String sessionId) throws Exception { - return new ErSession(sessionId , false); - } - - - } - ); + CacheBuilder.newBuilder() + .maximumSize(2000) + .expireAfterWrite(10, TimeUnit.MINUTES) + .concurrencyLevel(100) + .recordStats() + .softValues() + .build(new CacheLoader() { + @Override + public ErSession load(String sessionId) throws Exception { + return new ErSession(sessionId, false); + } + } + ); // object PutBatchSinkUtils { diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPair.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPair.java index 5c3ef3fe9f..4e8d5152df 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPair.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPair.java @@ -3,18 +3,21 @@ import java.util.Map; public class RollPair { - public static final String PUT_BATCH = "putBatch"; - public static final String ROLL_PAIR_URI_PREFIX = "v1/roll-pair"; - public static final String EGG_PAIR_URI_PREFIX = "v1/egg-pair"; - - public static final String RUN_JOB = "runJob"; - public static final String RUN_TASK = "runTask"; - public static CommandURI EGG_RUN_TASK_COMMAND = new CommandURI(EGG_PAIR_URI_PREFIX+"/"+RUN_TASK); - public static CommandURI ROLL_RUN_JOB_COMMAND = new CommandURI(ROLL_PAIR_URI_PREFIX+"/"+RUN_JOB); - - public RollPair(ErStore store,RollPairContext ctx, Map options){ + public static final String PUT_BATCH = "putBatch"; + public static final String ROLL_PAIR_URI_PREFIX = "v1/roll-pair"; + public static final String EGG_PAIR_URI_PREFIX = "v1/egg-pair"; + + public static final String RUN_JOB = "runJob"; + public static final String RUN_TASK = "runTask"; + public static CommandURI EGG_RUN_TASK_COMMAND = new CommandURI(EGG_PAIR_URI_PREFIX + "/" + RUN_TASK); + public static CommandURI ROLL_RUN_JOB_COMMAND = new CommandURI(ROLL_PAIR_URI_PREFIX + "/" + RUN_JOB); + ErStore store; + RollPairContext ctx; + Map options; + + public RollPair(ErStore store, RollPairContext ctx, Map options) { this.store = store; - this.ctx = ctx; + this.ctx = ctx; this.options = options; } @@ -42,10 +45,6 @@ public void setOptions(Map options) { this.options = options; } - ErStore store; - RollPairContext ctx; - Map options; - // // val transferFutures = new Array[Future[Transfer.TransferBatch]](totalPartitions) // @@ -90,10 +89,6 @@ public void setOptions(Map options) { } - - - - // //class RollPair(val store: ErStore, // val ctx: RollPairContext, @@ -177,5 +172,4 @@ public void setOptions(Map options) { // } - //} diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPairContext.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPairContext.java index 61e2ae0321..22b9652102 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPairContext.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/RollPairContext.java @@ -12,13 +12,22 @@ public class RollPairContext { - public RollPairContext(ErSession erSession){ - this.erSession = erSession; - }; + public static ExecutorService executor = Executors.newCachedThreadPool(); + ; Logger logger = LoggerFactory.getLogger(RollPairContext.class); + private String sessionId; + private ErSession erSession; + private ErSessionMeta sessionMeta; + // val EGGROLL_ROLLPAIR_DEFAULT_STORE_TYPE = ErConfKey("eggroll.rollpair.default.store.type", "ROLLPAIR_LMDB") + private String defaultStoreType = "ROLLPAIR_LMDB"; + private String defaultSerdesType = "PICKLE"; + + public RollPairContext(ErSession erSession) { + this.erSession = erSession; + } - public RollPair load(String namespace, String name, Map options){ + public RollPair load(String namespace, String name, Map options) { // def load(namespace: String, name: String, options: Map[String,String] = Map()): RollPair = { @@ -38,30 +47,25 @@ public RollPair load(String namespace, String name, Map options // new RollPair(loaded, this) // } - String defaultStoreTypeValue = defaultStoreType.split("_")[1]; + String defaultStoreTypeValue = defaultStoreType.split("_")[1]; String storeType = options.getOrDefault(Dict.STORE_TYPE, options.getOrDefault(Dict.STORE_TYPE_SNAKECASE, defaultStoreTypeValue)); - int totalPartitions =Integer.parseInt( options.getOrDefault(Dict.TOTAL_PARTITIONS, options.getOrDefault(Dict.TOTAL_PARTITIONS_SNAKECASE, "1"))); + int totalPartitions = Integer.parseInt(options.getOrDefault(Dict.TOTAL_PARTITIONS, options.getOrDefault(Dict.TOTAL_PARTITIONS_SNAKECASE, "1"))); - ErStoreLocator erStoreLocator = new ErStoreLocator(namespace,name,Dict.EMPTY,storeType,totalPartitions, + ErStoreLocator erStoreLocator = new ErStoreLocator(namespace, name, Dict.EMPTY, storeType, totalPartitions, options.getOrDefault(Dict.PARTITIONER, PartitionerTypes.BYTESTRING_HASH.name()), options.getOrDefault(Dict.SERDES, defaultSerdesType)); - ErStore store = new ErStore(erStoreLocator, Lists.newArrayList(),options); + ErStore store = new ErStore(erStoreLocator, Lists.newArrayList(), options); - logger.info("===================ppppppp==={}",store); + logger.info("===================ppppppp==={}", store); ErStore loaded = erSession.clusterManagerClient.getOrCreateStore(store); - logger.info("loaded erStore {}" ,loaded); + logger.info("loaded erStore {}", loaded); - return new RollPair(loaded, this, Maps.newHashMap()); + return new RollPair(loaded, this, Maps.newHashMap()); } - - public static ExecutorService executor = Executors.newCachedThreadPool(); - - private String sessionId ; - public String getSessionId() { return sessionId; } @@ -102,12 +106,6 @@ public void setErSession(ErSession erSession) { this.erSession = erSession; } - private ErSession erSession; - private ErSessionMeta sessionMeta ; - // val EGGROLL_ROLLPAIR_DEFAULT_STORE_TYPE = ErConfKey("eggroll.rollpair.default.store.type", "ROLLPAIR_LMDB") - private String defaultStoreType = "ROLLPAIR_LMDB"; - private String defaultSerdesType = "PICKLE"; - // // public RollPair load(); @@ -115,11 +113,6 @@ public void setErSession(ErSession erSession) { } - - - - - //class RollPairContext(val session: ErSession, // defaultStoreType: String = RollPairConfKeys.EGGROLL_ROLLPAIR_DEFAULT_STORE_TYPE.get(), // defaultSerdesType: String = SerdesTypes.PICKLE) extends Logging { diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/SerdesTypes.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/SerdesTypes.java index a064da73a6..e23396281b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/SerdesTypes.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/SerdesTypes.java @@ -2,9 +2,9 @@ public enum SerdesTypes { - PICKLE , - PROTOBUF , - CLOUD_PICKLE , + PICKLE, + PROTOBUF, + CLOUD_PICKLE, EMPTY } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionCommands.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionCommands.java index 442d496738..264faa22d4 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionCommands.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionCommands.java @@ -1,17 +1,17 @@ package com.osx.broker.eggroll; public class SessionCommands { - static String prefix = "v1/cluster-manager/session"; - static CommandURI getOrCreateSession =new CommandURI(prefix , "getOrCreateSession"); - static CommandURI getSession = new CommandURI(prefix , "getSession"); - static CommandURI registerSession = new CommandURI(prefix ,"registerSession"); - static CommandURI getSessionServerNodes = new CommandURI(prefix , "getSessionServerNodes"); - static CommandURI getSessionRolls = new CommandURI(prefix , "getSessionRolls"); - static CommandURI getSessionEggs = new CommandURI(prefix , "getSessionEggs"); - static CommandURI heartbeat = new CommandURI(prefix , "heartbeat"); - static CommandURI stopSession = new CommandURI(prefix , "stopSession"); - static CommandURI killSession = new CommandURI(prefix , "killSession"); - static CommandURI killAllSessions = new CommandURI(prefix , "killAllSessions"); + static String prefix = "v1/cluster-manager/session"; + static CommandURI getOrCreateSession = new CommandURI(prefix, "getOrCreateSession"); + static CommandURI getSession = new CommandURI(prefix, "getSession"); + static CommandURI registerSession = new CommandURI(prefix, "registerSession"); + static CommandURI getSessionServerNodes = new CommandURI(prefix, "getSessionServerNodes"); + static CommandURI getSessionRolls = new CommandURI(prefix, "getSessionRolls"); + static CommandURI getSessionEggs = new CommandURI(prefix, "getSessionEggs"); + static CommandURI heartbeat = new CommandURI(prefix, "heartbeat"); + static CommandURI stopSession = new CommandURI(prefix, "stopSession"); + static CommandURI killSession = new CommandURI(prefix, "killSession"); + static CommandURI killAllSessions = new CommandURI(prefix, "killAllSessions"); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionStatus.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionStatus.java index 5fbbece5ac..0ba6f34a65 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionStatus.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/SessionStatus.java @@ -1,5 +1,5 @@ package com.osx.broker.eggroll; public enum SessionStatus { - NEW ,ACTIVE,CLOSED,KILLED,ERROR + NEW, ACTIVE, CLOSED, KILLED, ERROR } diff --git a/java/osx/broker/src/main/java/com/osx/broker/eggroll/TimeUtils.java b/java/osx/broker/src/main/java/com/osx/broker/eggroll/TimeUtils.java index 8cd9478845..582f5f5253 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/eggroll/TimeUtils.java +++ b/java/osx/broker/src/main/java/com/osx/broker/eggroll/TimeUtils.java @@ -8,13 +8,14 @@ public class TimeUtils { static DateTimeFormatter noSeparatorFormatter = DateTimeFormatter.ofPattern("yyyyMMdd.HHmmss.SSS"); - public static String getNowMs(String dateFormat) { + + public static String getNowMs(String dateFormat) { LocalDateTime now = LocalDateTime.now(); if (StringUtils.isBlank(dateFormat)) { - return noSeparatorFormatter.format(now); + return noSeparatorFormatter.format(now); } else { - return new SimpleDateFormat(dateFormat).format(now); - } + return new SimpleDateFormat(dateFormat).format(now); } } +} diff --git a/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterMetricStatistics.java b/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterMetricStatistics.java index 1d1518afd7..0bcb3920ab 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterMetricStatistics.java +++ b/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterMetricStatistics.java @@ -1,8 +1,6 @@ - package com.osx.broker.flow; - import com.osx.core.flow.ClusterMetric; import com.osx.core.utils.AssertUtil; @@ -10,16 +8,18 @@ import java.util.concurrent.ConcurrentHashMap; - public final class ClusterMetricStatistics { private static final Map METRIC_MAP = new ConcurrentHashMap<>(); + private ClusterMetricStatistics() { + } + public static void clear() { METRIC_MAP.clear(); } - public static void putMetric(String resource, ClusterMetric metric) { + public static void putMetric(String resource, ClusterMetric metric) { AssertUtil.notNull(metric, "Cluster metric cannot be null"); METRIC_MAP.put(resource, metric); } @@ -33,18 +33,14 @@ public static boolean putMetricIfAbsent(String resource, ClusterMetric metric) { return true; } - public static void removeMetric(String resource) { + public static void removeMetric(String resource) { METRIC_MAP.remove(resource); } - public static ClusterMetric getMetric(String resource) { + public static ClusterMetric getMetric(String resource) { return METRIC_MAP.get(resource); } - public static Map getMetricMap(){ - return METRIC_MAP; - } - // public static void resetFlowMetrics() { // Set keySet = METRIC_MAP.keySet(); // for (Long id : keySet) { @@ -53,10 +49,9 @@ public static Map getMetricMap(){ // } // } - - - private ClusterMetricStatistics() {} - + public static Map getMetricMap() { + return METRIC_MAP; + } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterRuleUtil.java b/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterRuleUtil.java index af878fc29a..5b89cceb66 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterRuleUtil.java +++ b/java/osx/broker/src/main/java/com/osx/broker/flow/ClusterRuleUtil.java @@ -1,11 +1,11 @@ - package com.osx.broker.flow; public final class ClusterRuleUtil { + private ClusterRuleUtil() { + } + public static boolean validId(Long id) { return id != null && id > 0; } - - private ClusterRuleUtil() {} } diff --git a/java/osx/broker/src/main/java/com/osx/broker/flow/CurrentConcurrencyManager.java b/java/osx/broker/src/main/java/com/osx/broker/flow/CurrentConcurrencyManager.java index 4d8b8f9f75..3736218f8c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/flow/CurrentConcurrencyManager.java +++ b/java/osx/broker/src/main/java/com/osx/broker/flow/CurrentConcurrencyManager.java @@ -1,9 +1,6 @@ - package com.osx.broker.flow; - - import com.osx.core.datasource.NamedThreadFactory; import java.util.Set; @@ -28,7 +25,8 @@ public final class CurrentConcurrencyManager { // SCHEDULER.scheduleAtFixedRate(logTask, 0, 1, TimeUnit.SECONDS); // } - /**, + /** + * , * add current concurrency. */ public static void addConcurrency(Long flowId, Integer acquireCount) { diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/ClusterService.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/ClusterService.java index 0ee39be91a..50cdacd576 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/ClusterService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/ClusterService.java @@ -2,20 +2,17 @@ import com.firework.cluster.rpc.Firework; import com.firework.cluster.rpc.FireworkServiceGrpc; - - - +import com.osx.broker.ServiceContainer; import com.osx.core.context.Context; import com.osx.core.service.InboundPackage; import com.osx.core.service.OutboundPackage; -import com.osx.broker.ServiceContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ClusterService extends FireworkServiceGrpc.FireworkServiceImplBase { +public class ClusterService extends FireworkServiceGrpc.FireworkServiceImplBase { - Logger logger = LoggerFactory.getLogger(ClusterService.class); + Logger logger = LoggerFactory.getLogger(ClusterService.class); //ZookeeperRegistry zookeeperRegistry; @@ -24,12 +21,9 @@ public class ClusterService extends FireworkServiceGrpc.FireworkServiceImplBase // DataTransferService dataTransferService; - //TransferQueueApplyService transferQueueApplyService; - - // public TokenService getTokenService() { // return tokenService; // } @@ -60,21 +54,18 @@ public class ClusterService extends FireworkServiceGrpc.FireworkServiceImplBase // } - - public void applyToken(com.firework.cluster.rpc.Firework.TokenRequest request, io.grpc.stub.StreamObserver responseObserver) { Context context = buidlContext(); InboundPackage inboundPackage = new InboundPackage(); inboundPackage.setBody(request); - OutboundPackage outboundPackage = ServiceContainer.defaultTokenService.service(context,inboundPackage); + OutboundPackage outboundPackage = ServiceContainer.defaultTokenService.service(context, inboundPackage); Firework.TokenResponse tokenResponse = outboundPackage.getData(); responseObserver.onNext(tokenResponse); responseObserver.onCompleted(); } - public void queryRouter(com.firework.cluster.rpc.Firework.QueryRouterRequest request, io.grpc.stub.StreamObserver responseObserver) { @@ -102,7 +93,6 @@ public void queryRouter(com.firework.cluster.rpc.Firework.QueryRouterRequest req // } - // public void unRegisterTransferQueue(com.firework.cluster.rpc.Firework.UnRegisterTransferQueueRequest request, // io.grpc.stub.StreamObserver responseObserver) { // @@ -112,6 +102,7 @@ public void queryRouter(com.firework.cluster.rpc.Firework.QueryRouterRequest req // } /** + * */ public void cancelClusterTransfer(com.firework.cluster.rpc.Firework.CancelClusterTransferRequest request, io.grpc.stub.StreamObserver responseObserver) { @@ -119,10 +110,10 @@ public void cancelClusterTransfer(com.firework.cluster.rpc.Firework.CancelCluste } - private Context buidlContext(){ - Context context = new Context(); - // context.setSourceIp(sourceIp.get()!=null?sourceIp.get().toString():""); - return context; + private Context buidlContext() { + Context context = new Context(); + // context.setSourceIp(sourceIp.get()!=null?sourceIp.get().toString():""); + return context; } // private Server buildServer( ){ diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextPrepareInterceptor.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextPrepareInterceptor.java index 21af4eecac..ab76751a48 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextPrepareInterceptor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextPrepareInterceptor.java @@ -1,20 +1,22 @@ package com.osx.broker.grpc; + import io.grpc.*; public class ContextPrepareInterceptor implements ServerInterceptor { - public static Context.Key sourceIp = Context.key("sourceIp"); - @Override - public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler next) { - String remoteAddr = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString(); + public static Context.Key sourceIp = Context.key("sourceIp"); + + @Override + public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler next) { + String remoteAddr = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString(); // System.err.println("pppppppppppp"+call.getAttributes()); // System.err.println("========metadat"+headers); - String[] remoteAddrSplited = remoteAddr.split(":"); - String remoteIp = remoteAddrSplited[0].replaceAll("\\/",""); - Context context = Context.current().withValue(sourceIp,remoteIp); - return Contexts.interceptCall(context,call,headers,next); - } + String[] remoteAddrSplited = remoteAddr.split(":"); + String remoteIp = remoteAddrSplited[0].replaceAll("\\/", ""); + Context context = Context.current().withValue(sourceIp, remoteIp); + return Contexts.interceptCall(context, call, headers, next); + } // override def interceptCall[ReqT, RespT](call: ServerCall[ReqT, RespT], headers: Metadata, // next: ServerCallHandler[ReqT, RespT]): ServerCall.Listener[ReqT] = { // val remoteAddr = call.getAttributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextUtil.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextUtil.java index 4a34d8f195..49ba6a52b5 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextUtil.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/ContextUtil.java @@ -1,14 +1,15 @@ package com.osx.broker.grpc; + import com.osx.core.context.Context; import java.util.UUID; public class ContextUtil { - public static Context buildContext(){ - Context context = new Context(); - context.setSourceIp(ContextPrepareInterceptor.sourceIp.get()!=null? ContextPrepareInterceptor.sourceIp.get().toString():""); + public static Context buildContext() { + Context context = new Context(); + context.setSourceIp(ContextPrepareInterceptor.sourceIp.get() != null ? ContextPrepareInterceptor.sourceIp.get().toString() : ""); context.setCaseId(UUID.randomUUID().toString()); - return context; + return context; } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPullRespSO.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPullRespSO.java index 8496cbb17a..f35d9ef600 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPullRespSO.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPullRespSO.java @@ -1,15 +1,15 @@ package com.osx.broker.grpc; -import com.osx.core.context.Context; + +import com.google.common.base.Preconditions; import com.osx.broker.constants.Direction; import com.osx.broker.util.ResourceUtil; -//import com.firework.transfer.service.TokenApplyService; -import com.google.common.base.Preconditions; +import com.osx.core.context.Context; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.stub.StreamObserver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ForwardPullRespSO implements StreamObserver{ +public class ForwardPullRespSO implements StreamObserver { Logger logger = LoggerFactory.getLogger(ForwardPullRespSO.class); @@ -17,29 +17,29 @@ public class ForwardPullRespSO implements StreamObserver{ Context context; - StreamObserver backStreamObserver; + StreamObserver backStreamObserver; - public ForwardPullRespSO(Context context ,StreamObserver backStreamObserver){ - //,TokenApplyService tokenApplyService){ + public ForwardPullRespSO(Context context, StreamObserver backStreamObserver) { + //,TokenApplyService tokenApplyService){ - // Preconditions.checkArgument(tokenApplyService!=null); - Preconditions.checkArgument(backStreamObserver!=null); - Preconditions.checkArgument(context!=null); + // Preconditions.checkArgument(tokenApplyService!=null); + Preconditions.checkArgument(backStreamObserver != null); + Preconditions.checkArgument(context != null); this.context = context; - // this.tokenApplyService = tokenApplyService; + // this.tokenApplyService = tokenApplyService; this.backStreamObserver = backStreamObserver; } @Override public void onNext(Proxy.Packet value) { - String resource = ResourceUtil.buildResource(context.getRouterInfo(), Direction.DOWN); + String resource = ResourceUtil.buildResource(context.getRouterInfo(), Direction.DOWN); //tokenApplyService.applyToken(context,resource,value.toByteArray().length); backStreamObserver.onNext(value); } @Override public void onError(Throwable t) { - logger.error("error",t); + logger.error("error", t); t.printStackTrace(); backStreamObserver.onError(t); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPushRespSO.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPushRespSO.java index 75aecc28b8..6d996ec079 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPushRespSO.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/ForwardPushRespSO.java @@ -1,9 +1,8 @@ package com.osx.broker.grpc; -import com.osx.core.context.Context; import com.osx.broker.callback.CompleteCallback; import com.osx.broker.callback.ErrorCallback; -//import com.firework.transfer.service.TokenApplyService; +import com.osx.core.context.Context; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.stub.StreamObserver; import org.slf4j.Logger; @@ -12,22 +11,18 @@ public class ForwardPushRespSO implements StreamObserver { Logger logger = LoggerFactory.getLogger(ForwardPushRespSO.class); + StreamObserver backPushRespSO; + CompleteCallback completeCallback; + ErrorCallback errorCallback; + Context context; - public ForwardPushRespSO(Context context, StreamObserver backPushRespSO , CompleteCallback completeCallback, ErrorCallback errorCallback){ + public ForwardPushRespSO(Context context, StreamObserver backPushRespSO, CompleteCallback completeCallback, ErrorCallback errorCallback) { this.backPushRespSO = backPushRespSO; this.context = context; this.completeCallback = completeCallback; this.errorCallback = errorCallback; } - StreamObserver backPushRespSO; - - CompleteCallback completeCallback; - - ErrorCallback errorCallback; - - Context context; - public StreamObserver getBackPushRespSO() { return backPushRespSO; } @@ -52,8 +47,8 @@ public void onNext(Proxy.Metadata value) { @Override public void onError(Throwable t) { - logger.error("onError {}",t); - if(errorCallback!=null) { + logger.error("onError {}", t); + if (errorCallback != null) { errorCallback.callback(t); } backPushRespSO.onError(t); @@ -62,7 +57,7 @@ public void onError(Throwable t) { @Override public void onCompleted() { - if(completeCallback!=null){ + if (completeCallback != null) { completeCallback.callback(); } backPushRespSO.onCompleted(); diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/MessageFlag.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/MessageFlag.java index f71d496106..e43b9da32e 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/MessageFlag.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/MessageFlag.java @@ -2,28 +2,30 @@ public enum MessageFlag { - MSG(0),ERROR(1),COMPELETED(2); - - public int getFlag() { - return flag; - } + MSG(0), ERROR(1), COMPELETED(2); private int flag; - private MessageFlag(int flag){ + private MessageFlag(int flag) { this.flag = flag; } - static public MessageFlag getMessageFlag(int flag){ - switch ( flag){ - case 0 : return MSG; - case 1 : return ERROR; - case 2 : return COMPELETED; - default: return null; + static public MessageFlag getMessageFlag(int flag) { + switch (flag) { + case 0: + return MSG; + case 1: + return ERROR; + case 2: + return COMPELETED; + default: + return null; } } - + public int getFlag() { + return flag; + } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/PcpGrpcService.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/PcpGrpcService.java index a66e8b5da0..6ce0240894 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/PcpGrpcService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/PcpGrpcService.java @@ -1,93 +1,111 @@ package com.osx.broker.grpc; -import com.osx.core.provider.TechProvider; + import com.osx.broker.ServiceContainer; +import com.osx.core.exceptions.SysException; +import com.osx.core.provider.TechProvider; import io.grpc.stub.StreamObserver; +import org.ppc.ptp.Osx; import org.ppc.ptp.PrivateTransferProtocolGrpc; -import org.ppc.ptp.Pcp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.Map; public class PcpGrpcService extends PrivateTransferProtocolGrpc.PrivateTransferProtocolImplBase { - - /** * 流式接口 + * * @param responseObserver * @return */ - public io.grpc.stub.StreamObserver transport( - io.grpc.stub.StreamObserver responseObserver) { - return new PcpStreamObserver(responseObserver); + public io.grpc.stub.StreamObserver transport( + io.grpc.stub.StreamObserver responseObserver) { + return new PcpStreamObserver(responseObserver); } + /** * 同步接口 + * * @param request * @param responseObserver */ - public void invoke(org.ppc.ptp.Pcp.Inbound request, - io.grpc.stub.StreamObserver responseObserver) { - TechProvider techProvider = ServiceContainer.techProviderRegister.select(request); - if(techProvider!=null) { - techProvider.processInvoke(request, responseObserver); + public void invoke(Osx.Inbound request, + io.grpc.stub.StreamObserver responseObserver) { + + + Map metaDataMap = request.getMetadataMap(); + // String version = metaDataMap.get(Pcp.Header.Version.name()); + String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name()); + + TechProvider techProvider = ServiceContainer.techProviderRegister.select(techProviderCode); + if (techProvider != null) { + techProvider.processGrpcInvoke(request, responseObserver); } } - public class PcpStreamObserver implements io.grpc.stub.StreamObserver { - public PcpStreamObserver(StreamObserver responseObserver){ + public class PcpStreamObserver implements io.grpc.stub.StreamObserver { + + Logger logger = LoggerFactory.getLogger(PcpStreamObserver.class); + boolean inited = false; + TechProvider techProvider; + StreamObserver responseObserver; + StreamObserver requestObserver; + public PcpStreamObserver(StreamObserver responseObserver) { this.responseObserver = responseObserver; } - boolean init = false; - TechProvider techProvider; - StreamObserver responseObserver; - StreamObserver requestObserver; - private void init(Pcp.Inbound inbound){ - techProvider = ServiceContainer.techProviderRegister.select(inbound); - if(techProvider!=null) { - requestObserver = techProvider.processTransport(inbound,responseObserver); - } - else { - //抛出异常 + private void init(Osx.Inbound inbound) { + + Map metaDataMap = inbound.getMetadataMap(); + // String version = metaDataMap.get(Pcp.Header.Version.name()); + + logger.info("PcpStreamObserver init {}",metaDataMap); + // System.err.println("pppppppppppppppppppppp "+metaDataMap); + String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name()); + techProvider = ServiceContainer.techProviderRegister.select(techProviderCode); + if (techProvider != null) { + requestObserver = techProvider.processGrpcTransport(inbound, responseObserver); + } else { + //抛出异常 + logger.error("can not found TechProvider of {}",techProviderCode); + throw new SysException("invalid TechProviderCode "+techProviderCode); } + inited = true; + logger.info("PcpStreamObserver init over"); } @Override - public void onNext(Pcp.Inbound inbound) { - if(!init) { + public void onNext(Osx.Inbound inbound) { + if (!inited) { init(inbound); } - if(requestObserver!=null){ + if (requestObserver != null) { requestObserver.onNext(inbound); - }else{ - throw new RuntimeException(); + } else { + throw new RuntimeException(); } - - } @Override public void onError(Throwable throwable) { - if(requestObserver!=null){ + if (requestObserver != null) { requestObserver.onError(throwable); - }else{ - throw new RuntimeException(); + } else { + throw new RuntimeException(); } } @Override public void onCompleted() { - if(requestObserver!=null){ + if (requestObserver != null) { requestObserver.onCompleted(); - }else{ - throw new RuntimeException(); + } else { + throw new RuntimeException(); } } } - - - - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/ProxyGrpcService.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/ProxyGrpcService.java index 546bbeb38b..b5d29731eb 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/ProxyGrpcService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/ProxyGrpcService.java @@ -1,13 +1,12 @@ - package com.osx.broker.grpc; //import com.firework.transfer.service.PullService; +import com.osx.broker.service.PushService2; +import com.osx.broker.service.UnaryCallService; import com.osx.core.context.Context; import com.osx.core.service.InboundPackage; import com.osx.core.service.OutboundPackage; -import com.osx.broker.service.PushService2; -import com.osx.broker.service.UnaryCallService; import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.stub.StreamObserver; @@ -17,19 +16,19 @@ public class ProxyGrpcService extends DataTransferServiceGrpc.DataTransferServiceImplBase { Logger logger = LoggerFactory.getLogger(ProxyGrpcService.class); - + UnaryCallService unaryCallService; + PushService2 pushService2; public ProxyGrpcService(PushService2 pushService2, UnaryCallService unaryCallService - ){ + ) { this.pushService2 = pushService2; this.unaryCallService = unaryCallService; } - UnaryCallService unaryCallService; - PushService2 pushService2; /** + * */ public io.grpc.stub.StreamObserver push( @@ -43,7 +42,7 @@ public io.grpc.stub.StreamObserver outboundPackage = pushService2.service(context, data); return outboundPackage.getData(); - }catch(Exception e){ + } catch (Exception e) { e.printStackTrace(); } return null; @@ -65,6 +64,7 @@ public io.grpc.stub.StreamObserver data = new InboundPackage<>(); data.setBody(request); context.setDataSize(request.getSerializedSize()); - OutboundPackage outboundPackage = unaryCallService.service(context,data); + OutboundPackage outboundPackage = unaryCallService.service(context, data); Proxy.Packet result = outboundPackage.getData(); - Throwable throwable = outboundPackage.getThrowable(); - if(throwable!=null) { + Throwable throwable = outboundPackage.getThrowable(); + if (throwable != null) { responseObserver.onError(throwable); - }else { + } else { responseObserver.onNext(result); responseObserver.onCompleted(); } @@ -91,5 +91,4 @@ public io.grpc.stub.StreamObserver { - - Logger logger = LoggerFactory.getLogger(PushReqStreamObserver.class); - - boolean init = false; - - Context context ; - - CountDownLatch finishLatch; - -// public TokenApplyService getTokenApplyService() { -// return tokenApplyService; +//package com.osx.broker.grpc; +// +//import com.osx.broker.constants.Direction; +//import com.osx.broker.router.FateRouterService; +//import com.osx.broker.util.ResourceUtil; +//import com.osx.core.config.MetaInfo; +//import com.osx.core.context.Context; +//import com.osx.core.exceptions.NoRouterInfoException; +//import com.osx.core.frame.GrpcConnectionFactory; +//import com.osx.core.router.RouterInfo; +//import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; +//import com.webank.ai.eggroll.api.networking.proxy.Proxy; +//import io.grpc.ManagedChannel; +//import io.grpc.stub.StreamObserver; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import java.util.concurrent.CountDownLatch; +//import java.util.concurrent.TimeUnit; +//import java.util.concurrent.TimeoutException; +// +//public class PushReqStreamObserver implements StreamObserver { +// +// Logger logger = LoggerFactory.getLogger(PushReqStreamObserver.class); +// +// boolean init = false; +// +// Context context; +// +// CountDownLatch finishLatch; +// +//// public TokenApplyService getTokenApplyService() { +//// return tokenApplyService; +//// } +//// +//// public void setTokenApplyService(TokenApplyService tokenApplyService) { +//// this.tokenApplyService = tokenApplyService; +//// } +//// +//// TokenApplyService tokenApplyService; +// FateRouterService fateRouterService; +// private StreamObserver forwardPushReqSO; +// private StreamObserver backRespSO; +// +// +// public PushReqStreamObserver(Context context, StreamObserver backRespSO, CountDownLatch finishLatch, +// FateRouterService fateRouterService +// //, TokenApplyService tokenApplyService +// ) { +// this.backRespSO = backRespSO; +// this.context = context.subContext(); +// this.context.setServiceName("pushTransfer"); +// this.finishLatch = finishLatch; +// this.fateRouterService = fateRouterService; +// // this.tokenApplyService = tokenApplyService; +// } +// +// public FateRouterService getFateRouterService() { +// return fateRouterService; // } // -// public void setTokenApplyService(TokenApplyService tokenApplyService) { -// this.tokenApplyService = tokenApplyService; +// public void setFateRouterService(FateRouterService fateRouterService) { +// this.fateRouterService = fateRouterService; // } // -// TokenApplyService tokenApplyService; - - public FateRouterService getFateRouterService() { - return fateRouterService; - } - - public void setFateRouterService(FateRouterService fateRouterService) { - this.fateRouterService = fateRouterService; - } - - FateRouterService fateRouterService; - - private StreamObserver forwardPushReqSO; - - private StreamObserver backRespSO; - - private Disruptor disruptor ; - - - public PushReqStreamObserver(Context context, StreamObserver backRespSO, CountDownLatch finishLatch, - FateRouterService fateRouterService - //, TokenApplyService tokenApplyService - ){ - this.backRespSO = backRespSO; - this.context = context.subContext(); - this.context.setServiceName("pushTransfer"); - this.finishLatch = finishLatch; - this.fateRouterService = fateRouterService; - // this.tokenApplyService = tokenApplyService; - } - - private void init(Proxy.Packet value ){ - RouterInfo routerInfo = fateRouterService.route(value); - if(routerInfo!=null) { - context.setRouterInfo(routerInfo); - }else{ - throw new NoRouterInfoException("no router"); - } - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo()); - DataTransferServiceGrpc.DataTransferServiceStub stub = DataTransferServiceGrpc.newStub(managedChannel); - ForwardPushRespSO forwardPushRespSO = new ForwardPushRespSO(context,backRespSO,()->{finishLatch.countDown();},(t)->{finishLatch.countDown();}); - // forwardPushRespSO.setTokenApplyService(tokenApplyService); - forwardPushReqSO = stub.push(forwardPushRespSO); - init =true; - } - - - - - - - @Override - public void onNext(Proxy.Packet value) { - if(!init){ - init(value); - } - byte[] data = value.toByteArray(); - int size = data.length; - String resource = ResourceUtil.buildResource(context.getRouterInfo(), Direction.UP); - // tokenApplyService.applyToken(context,resource,size); - forwardPushReqSO.onNext(value); - } - - - @Override - public void onError(Throwable t) { - logger.info("onError"); - if(forwardPushReqSO!=null) { - forwardPushReqSO.onError(t); - } - context.printFlowLog(); - } - - @Override - public void onCompleted() { - - boolean needPrintFlow = true; - if(forwardPushReqSO!=null) { - forwardPushReqSO.onCompleted(); - try { - if(!finishLatch.await(MetaInfo.PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT, TimeUnit.SECONDS)){ - onError(new TimeoutException()); - needPrintFlow = false; - } - } catch (InterruptedException e) { - onError(e); - needPrintFlow = false; - } - } - if(needPrintFlow){ - context.printFlowLog(); - } - - logger.info("receive completed !!!!"); - } -} +// private void init(Proxy.Packet value) { +// RouterInfo routerInfo = fateRouterService.route(value); +// if (routerInfo != null) { +// context.setRouterInfo(routerInfo); +// } else { +// throw new NoRouterInfoException("no router"); +// } +// ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo()); +// DataTransferServiceGrpc.DataTransferServiceStub stub = DataTransferServiceGrpc.newStub(managedChannel); +// ForwardPushRespSO forwardPushRespSO = new ForwardPushRespSO(context, backRespSO, () -> { +// finishLatch.countDown(); +// }, (t) -> { +// finishLatch.countDown(); +// }); +// // forwardPushRespSO.setTokenApplyService(tokenApplyService); +// forwardPushReqSO = stub.push(forwardPushRespSO); +// init = true; +// } +// +// +// @Override +// public void onNext(Proxy.Packet value) { +// if (!init) { +// init(value); +// } +// byte[] data = value.toByteArray(); +// int size = data.length; +// String resource = ResourceUtil.buildResource(context.getRouterInfo(), Direction.UP); +// // tokenApplyService.applyToken(context,resource,size); +// forwardPushReqSO.onNext(value); +// } +// +// +// @Override +// public void onError(Throwable t) { +// logger.info("onError"); +// if (forwardPushReqSO != null) { +// forwardPushReqSO.onError(t); +// } +// context.printFlowLog(); +// } +// +// @Override +// public void onCompleted() { +// +// boolean needPrintFlow = true; +// if (forwardPushReqSO != null) { +// forwardPushReqSO.onCompleted(); +// try { +// if (!finishLatch.await(MetaInfo.PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT, TimeUnit.SECONDS)) { +// onError(new TimeoutException()); +// needPrintFlow = false; +// } +// } catch (InterruptedException e) { +// onError(e); +// needPrintFlow = false; +// } +// } +// if (needPrintFlow) { +// context.printFlowLog(); +// } +// +// logger.info("receive completed !!!!"); +// } +//} diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/PushRequestDataWrap.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/PushRequestDataWrap.java index e248a3f4fe..4393bfe12a 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/PushRequestDataWrap.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/PushRequestDataWrap.java @@ -4,8 +4,8 @@ import io.grpc.stub.StreamObserver; public class PushRequestDataWrap { - Proxy.Packet packet; - StreamObserver streamObserver; + Proxy.Packet packet; + StreamObserver streamObserver; public Proxy.Packet getPacket() { return packet; diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/QueuePushReqStreamObserver.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/QueuePushReqStreamObserver.java index 1bc468f6a8..73534a2941 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/QueuePushReqStreamObserver.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/QueuePushReqStreamObserver.java @@ -1,27 +1,24 @@ package com.osx.broker.grpc; -import com.google.common.base.Preconditions; -import com.osx.core.exceptions.*; -import com.osx.core.frame.GrpcConnectionFactory; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; +import com.osx.broker.ServiceContainer; +import com.osx.broker.eggroll.*; +import com.osx.broker.ptp.PtpForwardPushRespSO; +import com.osx.broker.util.TransferUtil; import com.osx.core.config.MetaInfo; +import com.osx.core.constant.ActionType; import com.osx.core.constant.Dict; -import com.osx.core.router.RouterInfo; import com.osx.core.constant.TransferStatus; import com.osx.core.context.Context; +import com.osx.core.exceptions.*; +import com.osx.core.frame.GrpcConnectionFactory; import com.osx.core.ptp.TargetMethod; -import com.osx.core.service.AbstractServiceAdaptor; +import com.osx.core.router.RouterInfo; import com.osx.core.utils.FlowLogUtil; import com.osx.core.utils.ToStringUtils; -import com.osx.broker.ServiceContainer; -//import com.osx.transfer.consumer.PushConsumer; -import com.osx.broker.eggroll.*; -import com.osx.broker.ptp.PtpForwardPushRespSO; - -//import com.firework.transfer.service.TokenApplyService; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.protobuf.ByteString; -import com.google.protobuf.InvalidProtocolBufferException; -import com.osx.broker.util.TransferUtil; import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import com.webank.eggroll.core.command.Command; @@ -29,10 +26,9 @@ import com.webank.eggroll.core.transfer.Transfer; import com.webank.eggroll.core.transfer.TransferServiceGrpc; import io.grpc.ManagedChannel; - import io.grpc.stub.StreamObserver; import org.apache.commons.lang3.StringUtils; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.ppc.ptp.PrivateTransferProtocolGrpc; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,28 +40,30 @@ public class QueuePushReqStreamObserver implements StreamObserver { + static public ConcurrentHashMap queueIdMap = new ConcurrentHashMap<>(); + static AtomicInteger seq = new AtomicInteger(0); Logger logger = LoggerFactory.getLogger(QueuePushReqStreamObserver.class); Context context; - ErRollSiteHeader rsHeader=null; - private boolean isDst=false; - private boolean needPrintFlow = true; - private StreamObserver forwardPushReqSO; - private StreamObserver backRespSO; - private String transferId; + ErRollSiteHeader rsHeader = null; TransferStatus transferStatus = TransferStatus.INIT; - CountDownLatch finishLatch= new CountDownLatch(1); - private Integer queueId ; - - static public ConcurrentHashMap queueIdMap = new ConcurrentHashMap<>(); - static AtomicInteger seq = new AtomicInteger(0); + CountDownLatch finishLatch = new CountDownLatch(1); + StreamObserver putBatchSinkPushReqSO; + RouterInfo routerInfo; + Proxy.Metadata metadata; + String brokerTag; + private boolean isDst = false; + private boolean needPrintFlow = true; + private StreamObserver forwardPushReqSO; + private StreamObserver backRespSO; + private String transferId; + private Integer queueId; - StreamObserver putBatchSinkPushReqSO; - public QueuePushReqStreamObserver(Context context, StreamObserver backRespSO - ){ + public QueuePushReqStreamObserver(Context context, StreamObserver backRespSO + ) { //this.fateRouterService = fateRouterService; - this.backRespSO = backRespSO; + this.backRespSO = backRespSO; this.context = context.subContext(); this.context.setNeedPrintFlowLog(true); // this.transferQueueManager = transferQueueManager; @@ -84,113 +82,106 @@ public void setForwardPushReqSO(StreamObserver forwardPushReqSO) { this.forwardPushReqSO = forwardPushReqSO; } - - RouterInfo routerInfo ; - - public void init(Proxy.Packet packet ) throws Exception{ + public void init(Proxy.Packet packet) throws Exception { Proxy.Metadata metadata = packet.getHeader(); String desPartyId = metadata.getDst().getPartyId(); - String srcPartyId = metadata.getSrc().getPartyId(); + String srcPartyId = metadata.getSrc().getPartyId(); ByteString encodedRollSiteHeader = metadata.getExt(); - rsHeader= ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); + rsHeader = ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); - Integer partitionId = rsHeader.getPartitionId(); - brokerTag = "putBatch-"+rsHeader.getRsKey("#", "__rsk")+"-"+partitionId; - logger.info("=========ErRollSiteHeader {}",rsHeader); + Integer partitionId = rsHeader.getPartitionId(); + brokerTag = "putBatch-" + rsHeader.getRsKey("#", "__rsk") + "-" + partitionId; + logger.info("=========ErRollSiteHeader {}", rsHeader); context.setSessionId(rsHeader.getRollSiteSessionId()); context.setTopic(brokerTag); - if (MetaInfo.PROPERTY_SELF_PARTY.contains(desPartyId)) { - isDst = true; - } + if (MetaInfo.PROPERTY_SELF_PARTY.contains(desPartyId)) { + isDst = true; + } - logger.info("========= init ======={} to {} set {} isDst {}",srcPartyId,desPartyId,MetaInfo.PROPERTY_SELF_PARTY,isDst); + logger.info("========= init ======={} to {} set {} isDst {}", srcPartyId, desPartyId, MetaInfo.PROPERTY_SELF_PARTY, isDst); - /** - * 检查目的地是否为自己 - */ - if (!isDst) { - routerInfo = ServiceContainer.fateRouterService.route(packet); - if (routerInfo != null) { - this.transferId = routerInfo.getResource(); + /** + * 检查目的地是否为自己 + */ + if (!isDst) { + routerInfo = ServiceContainer.fateRouterService.route(packet); + if (routerInfo != null) { + this.transferId = routerInfo.getResource(); - } else { - throw new NoRouterInfoException("no router"); - } - } - if (isDst) { - initEggroll(packet); - } else { + } else { + throw new NoRouterInfoException("no router"); + } + } + if (isDst) { + initEggroll(packet); + } else { + context.setActionType(ActionType.PUSH_REMOTE.getAlias()); + context.setRouterInfo(routerInfo); + context.setSrcPartyId(routerInfo.getSourcePartyId()); + context.setDesPartyId(routerInfo.getDesPartyId()); + + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo(),true); + + + //forwardPushRespSO.setTokenApplyService(ServiceContainer.tokenApplyService); + if (TransferUtil.isOldVersionFate(routerInfo.getVersion())) { + DataTransferServiceGrpc.DataTransferServiceStub stub = DataTransferServiceGrpc.newStub(managedChannel); + ForwardPushRespSO forwardPushRespSO = new ForwardPushRespSO(context, backRespSO, () -> { + finishLatch.countDown(); + }, (t) -> { + finishLatch.countDown(); + }); + forwardPushReqSO = stub.push(forwardPushRespSO); + } else { + PtpForwardPushRespSO ptpForwardPushRespSO = new PtpForwardPushRespSO(context, backRespSO, "proxy", () -> { + finishLatch.countDown(); + }, (t) -> { + finishLatch.countDown(); + }); + + PrivateTransferProtocolGrpc.PrivateTransferProtocolStub stub = PrivateTransferProtocolGrpc.newStub(managedChannel); + + StreamObserver ptpForwardPushReqSO = stub.transport(ptpForwardPushRespSO); + + forwardPushReqSO = new StreamObserver() { + @Override + public void onNext(Proxy.Packet packet) { + Osx.Inbound inbound = TransferUtil.buildInboundFromPushingPacket(packet, TargetMethod.PUSH.name()); + ptpForwardPushReqSO.onNext(inbound); + } - context.setActionType("push"); - context.setRouterInfo(routerInfo); - context.setSrcPartyId(routerInfo.getSourcePartyId()); - context.setDesPartyId(routerInfo.getDesPartyId()); - - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo()); - - - //forwardPushRespSO.setTokenApplyService(ServiceContainer.tokenApplyService); - if (TransferUtil.isOldVersionFate(routerInfo.getVersion())) { - DataTransferServiceGrpc.DataTransferServiceStub stub = DataTransferServiceGrpc.newStub(managedChannel); - ForwardPushRespSO forwardPushRespSO = new ForwardPushRespSO(context, backRespSO, () -> { - finishLatch.countDown(); - }, (t) -> { - finishLatch.countDown(); - }); - forwardPushReqSO = stub.push(forwardPushRespSO); - } else { - PtpForwardPushRespSO ptpForwardPushRespSO = new PtpForwardPushRespSO(context, backRespSO, "proxy", () -> { - finishLatch.countDown(); - }, (t) -> { - finishLatch.countDown(); - }); - - PrivateTransferProtocolGrpc.PrivateTransferProtocolStub stub = PrivateTransferProtocolGrpc.newStub(managedChannel); - - StreamObserver ptpForwardPushReqSO = stub.transport(ptpForwardPushRespSO); - - forwardPushReqSO = new StreamObserver() { - @Override - public void onNext(Proxy.Packet packet) { - Pcp.Inbound inbound = TransferUtil.buildInboundFromPushingPacket(packet, TargetMethod.PUSH.name()); - ptpForwardPushReqSO.onNext(inbound); - } - - @Override - public void onError(Throwable throwable) { - ptpForwardPushReqSO.onError(throwable); - } - - @Override - public void onCompleted() { - ptpForwardPushReqSO.onCompleted(); - } - }; + @Override + public void onError(Throwable throwable) { + ptpForwardPushReqSO.onError(throwable); + } + @Override + public void onCompleted() { + ptpForwardPushReqSO.onCompleted(); } - } - transferStatus = TransferStatus.TRANSFERING; + }; + } + } + transferStatus = TransferStatus.TRANSFERING; - } - Proxy.Metadata metadata; - String brokerTag; + } - private void initEggroll(Proxy.Packet firstRequest){ - if(StringUtils.isEmpty(MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP)){ - throw new SysException("eggroll cluter manager ip is not found"); + private void initEggroll(Proxy.Packet firstRequest) { + if (StringUtils.isEmpty(MetaInfo.PROPERTY_EGGROLL_CLUSTER_MANANGER_IP)) { + throw new SysException("eggroll cluter manager ip is not found"); } metadata = firstRequest.getHeader(); - logger.info("init eggroll begin {}",firstRequest); + logger.info("init eggroll begin {}", firstRequest); String oneLineStringMetadata = ToStringUtils.toOneLineString(metadata); // ByteString encodedRollSiteHeader = metadata.getExt(); - context.setActionType("push-eggroll"); + context.setActionType(ActionType.PUSH_EGGROLL.getAlias()); // ErRollSiteHeader rsHeader=null; // try { // rsHeader= ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); @@ -200,15 +191,15 @@ private void initEggroll(Proxy.Packet firstRequest){ // } // logger.info("=========ErRollSiteHeader {}",rsHeader); //"#", prefix: Array[String] = Array("__rsk") - String rsKey = rsHeader.getRsKey("#","__rsk"); - String sessionId = String.join("_", rsHeader.getRollSiteSessionId() , rsHeader.getDstRole(), rsHeader.getDstPartyId()); + String rsKey = rsHeader.getRsKey("#", "__rsk"); + String sessionId = String.join("_", rsHeader.getRollSiteSessionId(), rsHeader.getDstRole(), rsHeader.getDstPartyId()); context.setSessionId(sessionId); ErSession session = null; try { session = PutBatchSinkUtil.sessionCache.get(sessionId); } catch (ExecutionException e) { e.printStackTrace(); - logger.error("get session error ",e); + logger.error("get session error ", e); } if (!SessionStatus.ACTIVE.name().equals(session.getErSessionMeta().getStatus())) { IllegalStateException error = new IllegalStateException("session=${sessionId} with illegal status. expected=${SessionStatus.ACTIVE}, actual=${session.sessionMeta.status}"); @@ -217,16 +208,16 @@ private void initEggroll(Proxy.Packet firstRequest){ } String namespace = rsHeader.getRollSiteSessionId(); - String name = rsKey; - RollPairContext ctx = new RollPairContext(session); + String name = rsKey; + RollPairContext ctx = new RollPairContext(session); Map rpOptions = Maps.newHashMap(); rpOptions.putAll(rsHeader.getOptions()); - rpOptions.put(Dict.TOTAL_PARTITIONS_SNAKECASE ,rsHeader.getTotalPartitions().toString()); + rpOptions.put(Dict.TOTAL_PARTITIONS_SNAKECASE, rsHeader.getTotalPartitions().toString()); //var rpOptions = rsHeader.options ++ Map(StringConstants.TOTAL_PARTITIONS_SNAKECASE -> rsHeader.totalPartitions.toString) if (rsHeader.getDataType().equals("object")) { - rpOptions.put(Dict.SERDES ,SerdesTypes.EMPTY.name()); + rpOptions.put(Dict.SERDES, SerdesTypes.EMPTY.name()); } else { - rpOptions.put(Dict.SERDES ,rsHeader.getOptions().getOrDefault("serdes", SerdesTypes.PICKLE.name())); + rpOptions.put(Dict.SERDES, rsHeader.getOptions().getOrDefault("serdes", SerdesTypes.PICKLE.name())); } // table creates here @@ -235,61 +226,61 @@ private void initEggroll(Proxy.Packet firstRequest){ Integer partitionId = rsHeader.getPartitionId(); ErPartition partition = rp.getStore().getPartition(partitionId); ErProcessor egg = ctx.getErSession().routeToEgg(partition); - logger.info("egg ========{}",egg); + logger.info("egg ========{}", egg); // RollPair.PUT_BATCH // delim: String = "#", prefix: Array[String] = Array("__rsk") // val PUT_BATCH = "putBatch" - // s"${}-${rsHeader.getRsKey()}-${partitionId}" + // s"${}-${rsHeader.getRsKey()}-${partitionId}" // brokerTag = // "putBatch-"+rsHeader.getRsKey("#", "__rsk")+"-"+partitionId; - // logger.info("======= brokerTag ======{}",brokerTag); - String jobId = IdUtils.generateJobId(ctx.getErSession().getSessionId(), brokerTag,"-"); - logger.info("======jobId ======={}",jobId); - Map jobOptions = new HashMap<>(); + // logger.info("======= brokerTag ======{}",brokerTag); + String jobId = IdUtils.generateJobId(ctx.getErSession().getSessionId(), brokerTag, "-"); + logger.info("======jobId ======={}", jobId); + Map jobOptions = new HashMap<>(); jobOptions.putAll(rsHeader.getOptions()); jobOptions.put(SessionConfKeys.CONFKEY_SESSION_ID, ctx.getErSession().getSessionId()); ErJob job = new ErJob( jobId, RollPair.PUT_BATCH, - Lists.newArrayList(rp.getStore()), - Lists.newArrayList(rp.getStore()), - Lists.newArrayList(), + Lists.newArrayList(rp.getStore()), + Lists.newArrayList(rp.getStore()), + Lists.newArrayList(), jobOptions); ErTask task = new ErTask(brokerTag, - RollPair.PUT_BATCH, - Lists.newArrayList(partition), + RollPair.PUT_BATCH, + Lists.newArrayList(partition), Lists.newArrayList(partition), job); - Future commandFuture = RollPairContext.executor.submit(() -> { - CommandClient commandClient = new CommandClient(egg.getCommandEndpoint()); - logger.info("before call EGG_RUN_TASK_COMMAND {}",task); - Command.CommandResponse commandResponse = commandClient.call(RollPair.EGG_RUN_TASK_COMMAND, task); - logger.info("============== EGG_RUN_TASK_COMMAND {}",commandResponse); - - long begin = System.currentTimeMillis(); - try { - Meta.Task taskMeta = Meta.Task.parseFrom(commandResponse.getResultsList().get(0)); - ErTask erTask = ErTask.parseFromPb(taskMeta); - long now = System.currentTimeMillis(); - logger.info("task ===cost===={}",now -begin); - - return erTask; - } catch (InvalidProtocolBufferException e) { - e.printStackTrace(); - } - return null; - }); + Future commandFuture = RollPairContext.executor.submit(() -> { + CommandClient commandClient = new CommandClient(egg.getCommandEndpoint()); + logger.info("before call EGG_RUN_TASK_COMMAND {}", task); + Command.CommandResponse commandResponse = commandClient.call(RollPair.EGG_RUN_TASK_COMMAND, task); + logger.info("============== EGG_RUN_TASK_COMMAND {}", commandResponse); + + long begin = System.currentTimeMillis(); + try { + Meta.Task taskMeta = Meta.Task.parseFrom(commandResponse.getResultsList().get(0)); + ErTask erTask = ErTask.parseFromPb(taskMeta); + long now = System.currentTimeMillis(); + logger.info("task ===cost===={}", now - begin); + + return erTask; + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + } + return null; + }); routerInfo = new RouterInfo(); context.setRouterInfo(routerInfo); routerInfo.setHost(egg.getTransferEndpoint().getHost()); routerInfo.setPort(egg.getTransferEndpoint().getPort()); context.setSrcPartyId(routerInfo.getSourcePartyId()); context.setDesPartyId(routerInfo.getDesPartyId()); - ManagedChannel channel = GrpcConnectionFactory.createManagedChannel(routerInfo); + ManagedChannel channel = GrpcConnectionFactory.createManagedChannel(routerInfo,false); TransferServiceGrpc.TransferServiceStub stub = TransferServiceGrpc.newStub(channel); putBatchSinkPushReqSO = stub.send(new PutBatchSinkPushRespSO(metadata, commandFuture, backRespSO, finishLatch)); @@ -300,13 +291,13 @@ private void initEggroll(Proxy.Packet firstRequest){ @Override public void onNext(Proxy.Packet value) { try { - long seq = value.getHeader().getSeq(); + long seq = value.getHeader().getSeq(); context.setDataSize(value.getSerializedSize()); context.setCaseId(Long.toString(seq)); if (transferStatus.equals(TransferStatus.INIT)) { init(value); } - if(transferStatus.equals(TransferStatus.TRANSFERING)) { + if (transferStatus.equals(TransferStatus.TRANSFERING)) { if (isDst) { Transfer.TransferHeader.Builder transferHeaderBuilder = Transfer.TransferHeader.newBuilder(); Transfer.TransferHeader tbHeader = transferHeaderBuilder.setId((int) metadata.getSeq()) @@ -323,14 +314,13 @@ public void onNext(Proxy.Packet value) { } - - }catch (Exception e){ - logger.error("push error",e); - ExceptionInfo exceptionInfo = ErrorMessageUtil.handleExceptionExceptionInfo(context,e); + } catch (Exception e) { + logger.error("push error", e); + ExceptionInfo exceptionInfo = ErrorMessageUtil.handleExceptionExceptionInfo(context, e); context.setException(e); context.setReturnCode(exceptionInfo.getCode()); - throw new BaseException(exceptionInfo.getCode(),exceptionInfo.getMessage()); - }finally { + throw new BaseException(exceptionInfo.getCode(), exceptionInfo.getMessage()); + } finally { FlowLogUtil.printFlowLog(context); } } @@ -341,19 +331,19 @@ public void onError(Throwable t) { /** * 传递错误 */ - logger.info("onError",t); + logger.info("onError", t); context.setException(t); // TODO: 2021/12/21这里需要补充逻辑 /** * 1.停止消费者,需要考虑产生异常时消费者并没有接入。 * 2.销毁队列 */ - if(isDst) { + if (isDst) { //transferQueue.onError(t); putBatchSinkPushReqSO.onError(t); - }else{ + } else { // if(MetaInfo.PROPERTY_USE_QUEUE_MODEL){ // if(transferQueue!=null){ @@ -367,31 +357,29 @@ public void onError(Throwable t) { // }else { - if(forwardPushReqSO!=null) { + if (forwardPushReqSO != null) { forwardPushReqSO.onError(t); } } } - - } @Override public void onCompleted() { - logger.info("transferId {} receive completed",transferId); - if(isDst) { + logger.info("transferId {} receive completed", transferId); + if (isDst) { // if(transferQueue!=null) { // transferQueue.setWriteOver(true); // } - if(putBatchSinkPushReqSO!=null) { - putBatchSinkPushReqSO.onCompleted(); - } - }else { + if (putBatchSinkPushReqSO != null) { + putBatchSinkPushReqSO.onCompleted(); + } + } else { - if(forwardPushReqSO!=null) { + if (forwardPushReqSO != null) { // if(MetaInfo.PROPERTY_USE_QUEUE_MODEL){ // /** diff --git a/java/osx/broker/src/main/java/com/osx/broker/grpc/TransferConf.java b/java/osx/broker/src/main/java/com/osx/broker/grpc/TransferConf.java index 6adb34683d..169be8c634 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/grpc/TransferConf.java +++ b/java/osx/broker/src/main/java/com/osx/broker/grpc/TransferConf.java @@ -2,6 +2,31 @@ public class TransferConf { + int maxConcurrentCallPerConnection = 10; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION.getWith(options).toInt + int maxInboundMessageSize = 3888888; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_INBOUND_MESSAGE_SIZE.getWith(options).toInt + int maxInboundMetadataSize = 3888883; + // = CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_INBOUND_METADATA_SIZE.getWith(options).toInt + int flowControlWindow = 100; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_FLOW_CONTROL_WINDOW.getWith(options).toInt + int channelKeepAliveTimeSec = 10; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_KEEPALIVE_TIME_SEC.getWith(options).toLong + int channelKeepAliveTimeoutSec = 10; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_KEEPALIVE_TIMEOUT_SEC.getWith(options).toLong + int channelPermitKeepAliveTime = 100; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC.getWith(options).toLong + int channelKeepAliveWithoutCallsEnabled; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED.getWith(options).toBoolean + long maxConnectionIdle = 10; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONNECTION_IDLE_SEC.getWith(options).toLong + long maxConnectionAge = 10; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONNECTION_AGE_SEC.getWith(options).toLong + long maxConnectionAgeGrace = 100; + //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC.getWith(options).toLong + int port = 9002; + String host = "localhost"; + public int getMaxConcurrentCallPerConnection() { return maxConcurrentCallPerConnection; } @@ -90,30 +115,6 @@ public void setMaxConnectionAgeGrace(long maxConnectionAgeGrace) { this.maxConnectionAgeGrace = maxConnectionAgeGrace; } - int maxConcurrentCallPerConnection =10; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION.getWith(options).toInt - int maxInboundMessageSize =3888888; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_INBOUND_MESSAGE_SIZE.getWith(options).toInt - int maxInboundMetadataSize =3888883; - // = CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_INBOUND_METADATA_SIZE.getWith(options).toInt - int flowControlWindow =100; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_FLOW_CONTROL_WINDOW.getWith(options).toInt - int channelKeepAliveTimeSec =10; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_KEEPALIVE_TIME_SEC.getWith(options).toLong - int channelKeepAliveTimeoutSec = 10 ; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_KEEPALIVE_TIMEOUT_SEC.getWith(options).toLong - int channelPermitKeepAliveTime = 100; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC.getWith(options).toLong - int channelKeepAliveWithoutCallsEnabled ; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED.getWith(options).toBoolean - long maxConnectionIdle = 10; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONNECTION_IDLE_SEC.getWith(options).toLong - long maxConnectionAge =10; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONNECTION_AGE_SEC.getWith(options).toLong - long maxConnectionAgeGrace =100 ; - //= CoreConfKeys.CONFKEY_CORE_GRPC_SERVER_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC.getWith(options).toLong - int port =9002; - public int getPort() { return port; } @@ -130,6 +131,4 @@ public void setHost(String host) { this.host = host; } - String host="localhost"; - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/interceptor/PpcRouterInterceptor.java b/java/osx/broker/src/main/java/com/osx/broker/interceptor/PpcRouterInterceptor.java index 4260a9b96d..659600e86a 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/interceptor/PpcRouterInterceptor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/interceptor/PpcRouterInterceptor.java @@ -5,12 +5,12 @@ import com.osx.core.service.InboundPackage; import com.osx.core.service.Interceptor; import com.osx.core.service.OutboundPackage; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; -public class PpcRouterInterceptor implements Interceptor { - public void doPreProcess(Context context, InboundPackage inboundPackage, OutboundPackage outboundPackage) throws Exception { - +public class PpcRouterInterceptor implements Interceptor { + + public void doPreProcess(Context context, InboundPackage inboundPackage, OutboundPackage outboundPackage) throws Exception { } diff --git a/java/osx/broker/src/main/java/com/osx/broker/interceptor/RequestHandleInterceptor.java b/java/osx/broker/src/main/java/com/osx/broker/interceptor/RequestHandleInterceptor.java index 00a43f2e9f..6bbd8da8a7 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/interceptor/RequestHandleInterceptor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/interceptor/RequestHandleInterceptor.java @@ -1,39 +1,37 @@ package com.osx.broker.interceptor; -import com.osx.core.router.RouterInfo; + +import com.osx.broker.grpc.PushRequestDataWrap; +import com.osx.broker.router.FateRouterService; import com.osx.core.context.Context; +import com.osx.core.exceptions.NoRouterInfoException; +import com.osx.core.router.RouterInfo; import com.osx.core.service.InboundPackage; import com.osx.core.service.Interceptor; -import com.osx.core.exceptions.NoRouterInfoException; -import com.osx.broker.grpc.PushRequestDataWrap; -import com.osx.broker.router.FateRouterService; +import com.webank.ai.eggroll.api.networking.proxy.Proxy; import org.apache.commons.lang3.StringUtils; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.webank.ai.eggroll.api.networking.proxy.Proxy; import java.util.Map; public class RequestHandleInterceptor implements Interceptor { - Logger logger = LoggerFactory.getLogger(RequestHandleInterceptor.class); - - public RequestHandleInterceptor( FateRouterService fateRouterService){ + FateRouterService fateRouterService; + public RequestHandleInterceptor(FateRouterService fateRouterService) { this.fateRouterService = fateRouterService; } - - FateRouterService fateRouterService; - public void doPreProcess(Context context, InboundPackage inboundPackage) throws Exception { - Object body = inboundPackage.getBody(); + Object body = inboundPackage.getBody(); - if(body instanceof PushRequestDataWrap){ + if (body instanceof PushRequestDataWrap) { - PushRequestDataWrap pushRequestDataWrap = (PushRequestDataWrap) body; - Proxy.Packet packet = pushRequestDataWrap.getPacket(); - context.putData("pushStreamObserver",pushRequestDataWrap.getStreamObserver()); - RouterInfo routerInfo = fateRouterService.route(packet); - if(packet!=null&&packet.getHeader()!=null&&packet.getHeader().getTask()!=null) { + PushRequestDataWrap pushRequestDataWrap = (PushRequestDataWrap) body; + Proxy.Packet packet = pushRequestDataWrap.getPacket(); + context.putData("pushStreamObserver", pushRequestDataWrap.getStreamObserver()); + RouterInfo routerInfo = fateRouterService.route(packet); + if (packet != null && packet.getHeader() != null && packet.getHeader().getTask() != null) { context.setTopic(packet.getHeader().getTask().getTaskId()); } //Preconditions.checkArgument(routerInfo!=null); @@ -41,28 +39,28 @@ public void doPreProcess(Context context, InboundPackage inboundPackage) throws context.setSrcPartyId(routerInfo.getSourcePartyId()); context.setRouterInfo(routerInfo); } - if(body instanceof Pcp.Inbound){ - Pcp.Inbound request = (Pcp.Inbound)body; - Map metaDataMap = request.getMetadataMap(); + if (body instanceof Osx.Inbound) { + Osx.Inbound request = (Osx.Inbound) body; + Map metaDataMap = request.getMetadataMap(); - String version = metaDataMap.get(Pcp.Header.Version.name()); - String techProviderCode = metaDataMap.get(Pcp.Header.TechProviderCode.name()); - String traceId = metaDataMap.get(Pcp.Header.TraceID.name()); - String token = metaDataMap.get(Pcp.Header.Token.name()); - String sourceNodeId = metaDataMap.get(Pcp.Header.SourceNodeID.name()); - String targetNodeId = metaDataMap.get(Pcp.Header.TargetNodeID.name()); - String sourceInstId = metaDataMap.get(Pcp.Header.SourceInstID.name()); - String targetInstId = metaDataMap.get(Pcp.Header.TargetInstID.name()); - String sessionId = metaDataMap.get(Pcp.Header.SessionID.name()); - String targetMethod = metaDataMap.get(Pcp.Metadata.TargetMethod.name()); - String targetComponentName = metaDataMap.get(Pcp.Metadata.TargetComponentName.name()); - String sourceComponentName = metaDataMap.get(Pcp.Metadata.SourceComponentName.name()); - String sourcePartyId= StringUtils.isEmpty(sourceInstId)?sourceNodeId:sourceInstId+"."+sourceNodeId; - String targetPartyId =StringUtils.isEmpty(targetInstId)? targetNodeId:targetInstId+"."+targetNodeId; - String topic = metaDataMap.get(Pcp.Metadata.MessageTopic.name()); - String offsetString = metaDataMap.get(Pcp.Metadata.MessageOffSet.name()); - RouterInfo routerInfo = fateRouterService.route(sourcePartyId,sourceComponentName,targetPartyId,targetComponentName); - Long offset = StringUtils.isNotEmpty(offsetString)?Long.parseLong(offsetString):null; + String version = metaDataMap.get(Osx.Header.Version.name()); + String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name()); + String traceId = metaDataMap.get(Osx.Header.TraceID.name()); + String token = metaDataMap.get(Osx.Header.Token.name()); + String sourceNodeId = metaDataMap.get(Osx.Header.SourceNodeID.name()); + String targetNodeId = metaDataMap.get(Osx.Header.TargetNodeID.name()); + String sourceInstId = metaDataMap.get(Osx.Header.SourceInstID.name()); + String targetInstId = metaDataMap.get(Osx.Header.TargetInstID.name()); + String sessionId = metaDataMap.get(Osx.Header.SessionID.name()); + String targetMethod = metaDataMap.get(Osx.Metadata.TargetMethod.name()); + String targetComponentName = metaDataMap.get(Osx.Metadata.TargetComponentName.name()); + String sourceComponentName = metaDataMap.get(Osx.Metadata.SourceComponentName.name()); + String sourcePartyId = StringUtils.isEmpty(sourceInstId) ? sourceNodeId : sourceInstId + "." + sourceNodeId; + String targetPartyId = StringUtils.isEmpty(targetInstId) ? targetNodeId : targetInstId + "." + targetNodeId; + String topic = metaDataMap.get(Osx.Metadata.MessageTopic.name()); + String offsetString = metaDataMap.get(Osx.Metadata.MessageOffSet.name()); + RouterInfo routerInfo = fateRouterService.route(sourcePartyId, sourceComponentName, targetPartyId, targetComponentName); + Long offset = StringUtils.isNotEmpty(offsetString) ? Long.parseLong(offsetString) : null; context.setDesPartyId(targetPartyId); context.setSrcPartyId(sourcePartyId); @@ -71,29 +69,29 @@ public void doPreProcess(Context context, InboundPackage inboundPackage) throws context.setRequestMsgIndex(offset); context.setSessionId(sessionId); - logger.info("=========== sessionId {}pppppppppppp{}",context.getSessionId(),sessionId); - logger.info("metaDataMap {}",metaDataMap); - return ; + logger.info("=========== sessionId {}pppppppppppp{}", context.getSessionId(), sessionId); + logger.info("metaDataMap {}", metaDataMap); + return; } /** * 旧版本 */ - if(body instanceof Proxy.Packet){ - Proxy.Packet packet = (Proxy.Packet) body; - RouterInfo routerInfo = fateRouterService.route((Proxy.Packet)body); - if(routerInfo==null){ + if (body instanceof Proxy.Packet) { + Proxy.Packet packet = (Proxy.Packet) body; + RouterInfo routerInfo = fateRouterService.route((Proxy.Packet) body); + if (routerInfo == null) { throw new NoRouterInfoException("no router info"); } - if(packet!=null&&packet.getHeader()!=null&&packet.getHeader().getTask()!=null) { + if (packet != null && packet.getHeader() != null && packet.getHeader().getTask() != null) { context.setTopic(packet.getHeader().getTask().getTaskId()); } context.setDesPartyId(routerInfo.getDesPartyId()); context.setSrcPartyId(routerInfo.getSourcePartyId()); context.setRouterInfo(routerInfo); - }else{ - throw new RuntimeException(); + } else { + throw new RuntimeException(); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/interceptor/WhiteListInterceptor.java b/java/osx/broker/src/main/java/com/osx/broker/interceptor/WhiteListInterceptor.java index ddcc245768..87f1dba33c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/interceptor/WhiteListInterceptor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/interceptor/WhiteListInterceptor.java @@ -9,10 +9,9 @@ import org.slf4j.LoggerFactory; - - public class WhiteListInterceptor implements Interceptor { Logger logger = LoggerFactory.getLogger(WhiteListInterceptor.class); + public void doPreProcess(Context context, InboundPackage inboundPackage, OutboundPackage outboundPackage) throws Exception { // logger.info("====================== {} {}",context.getServiceName(),context.getActionType()); diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/AllocateMappedFileService.java b/java/osx/broker/src/main/java/com/osx/broker/message/AllocateMappedFileService.java index 9b2fba1921..54c79893e0 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/AllocateMappedFileService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/AllocateMappedFileService.java @@ -1,20 +1,15 @@ - package com.osx.broker.message; -import com.osx.core.frame.ServiceThread; import com.osx.broker.queue.MappedFile; import com.osx.broker.util.UtilAll; +import com.osx.core.frame.ServiceThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.PriorityBlockingQueue; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; /** @@ -24,9 +19,9 @@ public class AllocateMappedFileService extends ServiceThread { private static final Logger log = LoggerFactory.getLogger(AllocateMappedFileService.class); private static int waitTimeOut = 1000 * 5; private ConcurrentMap requestTable = - new ConcurrentHashMap(); + new ConcurrentHashMap(); private PriorityBlockingQueue requestQueue = - new PriorityBlockingQueue(); + new PriorityBlockingQueue(); private volatile boolean hasException = false; // private DefaultMessageStore messageStore; // @@ -34,6 +29,11 @@ public class AllocateMappedFileService extends ServiceThread { // this.messageStore = messageStore; // } + public static void main(String[] args) { + + + } + public MappedFile putRequestAndReturnMappedFile(String nextFilePath, int fileSize) { int canSubmitRequests = 2; // if (this.messageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { @@ -137,12 +137,12 @@ private boolean mmapOperation() { AllocateRequest expectedRequest = this.requestTable.get(req.getFilePath()); if (null == expectedRequest) { log.warn("this mmap request expired, maybe cause timeout " + req.getFilePath() + " " - + req.getFileSize()); + + req.getFileSize()); return true; } if (expectedRequest != req) { log.warn("never expected here, maybe cause timeout " + req.getFilePath() + " " - + req.getFileSize() + ", req:" + req + ", expectedRequest:" + expectedRequest); + + req.getFileSize() + ", req:" + req + ", expectedRequest:" + expectedRequest); return true; } @@ -167,7 +167,7 @@ private boolean mmapOperation() { if (elapsedTime > 10) { int queueSize = this.requestQueue.size(); log.warn("create mappedFile spent time(ms) " + elapsedTime + " queue size " + queueSize - + " " + req.getFilePath() + " " + req.getFileSize()); + + " " + req.getFilePath() + " " + req.getFileSize()); } // pre write mappedFile @@ -298,10 +298,4 @@ public boolean equals(Object obj) { return true; } } - - - public static void main(String[] args){ - - - } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageHandler.java b/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageHandler.java index 5d757da7c0..8d568c9578 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageHandler.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageHandler.java @@ -1,10 +1,8 @@ - package com.osx.broker.message; import java.nio.ByteBuffer; - public interface AppendMessageHandler { diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageResult.java b/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageResult.java index df4213dbb9..ecb635eb99 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageResult.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageResult.java @@ -1,4 +1,3 @@ - package com.osx.broker.message; /** @@ -26,7 +25,7 @@ public AppendMessageResult(AppendMessageStatus status) { } public AppendMessageResult(AppendMessageStatus status, long wroteOffset, int wroteBytes, String msgId, - long storeTimestamp, long logicsOffset, long pagecacheRT) { + long storeTimestamp, long logicsOffset, long pagecacheRT) { this.status = status; this.wroteOffset = wroteOffset; this.wroteBytes = wroteBytes; @@ -107,14 +106,14 @@ public void setMsgNum(int msgNum) { @Override public String toString() { return "AppendMessageResult{" + - "status=" + status + - ", wroteOffset=" + wroteOffset + - ", wroteBytes=" + wroteBytes + - ", msgId='" + msgId + '\'' + - ", storeTimestamp=" + storeTimestamp + - ", logicsOffset=" + logicsOffset + - ", pagecacheRT=" + pagecacheRT + - ", msgNum=" + msgNum + - '}'; + "status=" + status + + ", wroteOffset=" + wroteOffset + + ", wroteBytes=" + wroteBytes + + ", msgId='" + msgId + '\'' + + ", storeTimestamp=" + storeTimestamp + + ", logicsOffset=" + logicsOffset + + ", pagecacheRT=" + pagecacheRT + + ", msgNum=" + msgNum + + '}'; } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageStatus.java b/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageStatus.java index 6ee5e1b3ca..1ac7daf71b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageStatus.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/AppendMessageStatus.java @@ -1,4 +1,3 @@ - package com.osx.broker.message; public enum AppendMessageStatus { diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/DefaultAppendMessageHandler.java b/java/osx/broker/src/main/java/com/osx/broker/message/DefaultAppendMessageHandler.java index 408860324a..77e1e1664c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/DefaultAppendMessageHandler.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/DefaultAppendMessageHandler.java @@ -7,115 +7,141 @@ public class DefaultAppendMessageHandler implements AppendMessageHandler { - Logger log = LoggerFactory.getLogger(DefaultAppendMessageHandler.class); - // File at the end of the minimum fixed length empty - private static final int END_FILE_MIN_BLANK_LENGTH = 4 + 4; - private final ByteBuffer msgIdMemory; - private final ByteBuffer msgIdV6Memory; - // Store the message content - private final ByteBuffer msgStoreItemMemory; - // The maximum length of the message - private final int maxMessageSize; - // Build Message Key - private final StringBuilder keyBuilder = new StringBuilder(); - - private final StringBuilder msgIdBuilder = new StringBuilder(); - - public DefaultAppendMessageHandler(final int size) { - this.msgIdMemory = ByteBuffer.allocate(4 + 4 + 8); - this.msgIdV6Memory = ByteBuffer.allocate(16 + 4 + 8); - log.info("======================{}",size); - this.msgStoreItemMemory = ByteBuffer.allocate(size + END_FILE_MIN_BLANK_LENGTH); - this.maxMessageSize = size; - } + // File at the end of the minimum fixed length empty + private static final int END_FILE_MIN_BLANK_LENGTH = 4 + 4; + private final ByteBuffer msgIdMemory; + private final ByteBuffer msgIdV6Memory; + // Store the message content + private final ByteBuffer msgStoreItemMemory; + // The maximum length of the message + private final int maxMessageSize; + // Build Message Key + private final StringBuilder keyBuilder = new StringBuilder(); + private final StringBuilder msgIdBuilder = new StringBuilder(); + Logger log = LoggerFactory.getLogger(DefaultAppendMessageHandler.class); + + public DefaultAppendMessageHandler(final int size) { + this.msgIdMemory = ByteBuffer.allocate(4 + 4 + 8); + this.msgIdV6Memory = ByteBuffer.allocate(16 + 4 + 8); + log.info("======================{}", size); + this.msgStoreItemMemory = ByteBuffer.allocate(size + END_FILE_MIN_BLANK_LENGTH); + this.maxMessageSize = size; + } - public ByteBuffer getMsgStoreItemMemory() { - return msgStoreItemMemory; - } + protected static int calMsgLength(int sysFlag, int srcPartyIdLength, int desPartyIdLength, int bodyLength, int topicLength, int propertiesLength) { + int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; + int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; + final int msgLen = 4 //TOTALSIZE + + 4 //MAGICCODE + + 4 //BODYCRC + + 4 //QUEUEID + + 4 //FLAG + //+ 8 //QUEUEOFFSET + + 1 + (srcPartyIdLength > 0 ? srcPartyIdLength : 0) - public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer, final int maxBlank, - final MessageExtBrokerInner msgInner) { + // + 8 //PHYSICALOFFSET + + 1 + (desPartyIdLength > 0 ? desPartyIdLength : 0) + + 4 //SYSFLAG + + 8 //BORNTIMESTAMP + // + bornhostLength //BORNHOST + // + 8 //STORETIMESTAMP + // + storehostAddressLength //STOREHOSTADDRESS + // + 4 //RECONSUMETIMES + // + 8 //Prepared Transaction Offset + + 4 + (bodyLength > 0 ? bodyLength : 0) //BODY + + 2 + topicLength //TOPIC + + 2 + (propertiesLength > 0 ? propertiesLength : 0) //propertiesLength + + 0; + return msgLen; + } + + public ByteBuffer getMsgStoreItemMemory() { + return msgStoreItemMemory; + } + public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer, final int maxBlank, + final MessageExtBrokerInner msgInner) { - long wroteOffset = fileFromOffset + byteBuffer.position(); - String msgId = Long.toString(wroteOffset); - Long queueOffset=new Long(0); - final byte[] propertiesData = + + long wroteOffset = fileFromOffset + byteBuffer.position(); + String msgId = Long.toString(wroteOffset); + Long queueOffset = new Long(0); + final byte[] propertiesData = msgInner.getPropertiesString() == null ? null : msgInner.getPropertiesString().getBytes(MessageDecoder.CHARSET_UTF8); - final int propertiesLength = propertiesData == null ? 0 : propertiesData.length; + final int propertiesLength = propertiesData == null ? 0 : propertiesData.length; - if (propertiesLength > Short.MAX_VALUE) { - log.warn("putMessage message properties length too long. length={}", propertiesData.length); - return new AppendMessageResult(AppendMessageStatus.PROPERTIES_SIZE_EXCEEDED); - } + if (propertiesLength > Short.MAX_VALUE) { + log.warn("putMessage message properties length too long. length={}", propertiesData.length); + return new AppendMessageResult(AppendMessageStatus.PROPERTIES_SIZE_EXCEEDED); + } - final byte[] topicData = msgInner.getTopic().getBytes(MessageDecoder.CHARSET_UTF8); + final byte[] topicData = msgInner.getTopic().getBytes(MessageDecoder.CHARSET_UTF8); - final byte[] srcPartyId = - msgInner.getSrcPartyId() == null ? null : msgInner.getSrcPartyId().getBytes(MessageDecoder.CHARSET_UTF8); - final int srcPartyIdLength = srcPartyId!=null?srcPartyId.length:0; + final byte[] srcPartyId = + msgInner.getSrcPartyId() == null ? null : msgInner.getSrcPartyId().getBytes(MessageDecoder.CHARSET_UTF8); + final int srcPartyIdLength = srcPartyId != null ? srcPartyId.length : 0; - final byte[] desPartyId = - msgInner.getDesPartyId() == null ? null : msgInner.getDesPartyId().getBytes(MessageDecoder.CHARSET_UTF8); - final int desPartyIdLength = desPartyId!=null?desPartyId.length:0; + final byte[] desPartyId = + msgInner.getDesPartyId() == null ? null : msgInner.getDesPartyId().getBytes(MessageDecoder.CHARSET_UTF8); + final int desPartyIdLength = desPartyId != null ? desPartyId.length : 0; - final int topicLength = topicData.length; + final int topicLength = topicData.length; - final int bodyLength = msgInner.getBody() == null ? 0 : msgInner.getBody().length; + final int bodyLength = msgInner.getBody() == null ? 0 : msgInner.getBody().length; - final int msgLen = calMsgLength(msgInner.getSysFlag(),srcPartyIdLength,desPartyIdLength, bodyLength, topicLength, propertiesLength); + final int msgLen = calMsgLength(msgInner.getSysFlag(), srcPartyIdLength, desPartyIdLength, bodyLength, topicLength, propertiesLength); - // Exceeds the maximum message - if (msgLen > this.maxMessageSize) { - return new AppendMessageResult(AppendMessageStatus.MESSAGE_SIZE_EXCEEDED); - } + // Exceeds the maximum message + if (msgLen > this.maxMessageSize) { + return new AppendMessageResult(AppendMessageStatus.MESSAGE_SIZE_EXCEEDED); + } - // Determines whether there is sufficient free space - if ((msgLen + END_FILE_MIN_BLANK_LENGTH) > maxBlank) { - this.resetByteBuffer(this.msgStoreItemMemory, maxBlank); - // 1 TOTALSIZE - this.msgStoreItemMemory.putInt(maxBlank); + // Determines whether there is sufficient free space + if ((msgLen + END_FILE_MIN_BLANK_LENGTH) > maxBlank) { + this.resetByteBuffer(this.msgStoreItemMemory, maxBlank); + // 1 TOTALSIZE + this.msgStoreItemMemory.putInt(maxBlank); // // 2 MAGICCODE // this.msgStoreItemMemory.putInt(1111); - // 3 The remaining space may be any value - // Here the length of the specially set maxBlank - final long beginTimeMills = System.currentTimeMillis(); - byteBuffer.put(this.msgStoreItemMemory.array(), 0, maxBlank); - return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgId, msgInner.getStoreTimestamp(), + // 3 The remaining space may be any value + // Here the length of the specially set maxBlank + final long beginTimeMills = System.currentTimeMillis(); + byteBuffer.put(this.msgStoreItemMemory.array(), 0, maxBlank); + return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgId, msgInner.getStoreTimestamp(), queueOffset, System.currentTimeMillis() - beginTimeMills); - } - - // Initialization of storage space - this.resetByteBuffer(msgStoreItemMemory, msgLen); + } - // 1 TOTALSIZE - this.msgStoreItemMemory.putInt(msgLen); - // log.info("msgLen {}",msgLen); - // 2 MAGICCODE - this.msgStoreItemMemory.putInt(1000); - // 3 BODYCRC - this.msgStoreItemMemory.putInt(msgInner.getBodyCRC()); - // 4 QUEUEID - this.msgStoreItemMemory.putInt(msgInner.getQueueId()); - // 5 FLAG - this.msgStoreItemMemory.putInt(msgInner.getFlag()); - // 6 QUEUEOFFSET - - this.msgStoreItemMemory.put((byte)srcPartyIdLength); - if(srcPartyId!=null) - this.msgStoreItemMemory.put(srcPartyId); - - this.msgStoreItemMemory.put((byte)desPartyIdLength); - if(desPartyId!=null) - this.msgStoreItemMemory.put(desPartyId); - - // this.msgStoreItemMemory.putLong(fileFromOffset + byteBuffer.position()); - // 8 SYSFLAG - this.msgStoreItemMemory.putInt(msgInner.getSysFlag()); - // 9 BORNTIMESTAMP - this.msgStoreItemMemory.putLong(msgInner.getBornTimestamp()); + // Initialization of storage space + this.resetByteBuffer(msgStoreItemMemory, msgLen); + + // 1 TOTALSIZE + this.msgStoreItemMemory.putInt(msgLen); + // log.info("msgLen {}",msgLen); + // 2 MAGICCODE + this.msgStoreItemMemory.putInt(1000); + // 3 BODYCRC + this.msgStoreItemMemory.putInt(msgInner.getBodyCRC()); + // 4 QUEUEID + this.msgStoreItemMemory.putInt(msgInner.getQueueId()); + // 5 FLAG + this.msgStoreItemMemory.putInt(msgInner.getFlag()); + // 6 QUEUEOFFSET + + this.msgStoreItemMemory.put((byte) srcPartyIdLength); + if (srcPartyId != null) + this.msgStoreItemMemory.put(srcPartyId); + + this.msgStoreItemMemory.put((byte) desPartyIdLength); + if (desPartyId != null) + this.msgStoreItemMemory.put(desPartyId); + + // this.msgStoreItemMemory.putLong(fileFromOffset + byteBuffer.position()); + // 8 SYSFLAG + this.msgStoreItemMemory.putInt(msgInner.getSysFlag()); + // 9 BORNTIMESTAMP + this.msgStoreItemMemory.putLong(msgInner.getBornTimestamp()); // // 10 BORNHOST // this.resetByteBuffer(bornHostHolder, bornHostLength); // this.msgStoreItemMemory.put(msgInner.getBornHostBytes(bornHostHolder)); @@ -128,60 +154,31 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer // this.msgStoreItemMemory.putInt(msgInner.getReconsumeTimes()); // // 14 Prepared Transaction Offset // this.msgStoreItemMemory.putLong(msgInner.getPreparedTransactionOffset()); - // 15 BODY - this.msgStoreItemMemory.putInt(bodyLength); - if (bodyLength > 0) - this.msgStoreItemMemory.put(msgInner.getBody()); - // 16 TOPIC - this.msgStoreItemMemory.putShort((short) topicLength); - this.msgStoreItemMemory.put(topicData); - // 17 PROPERTIES - this.msgStoreItemMemory.putShort((short) propertiesLength); - if (propertiesLength > 0) - this.msgStoreItemMemory.put(propertiesData); - - final long beginTimeMills = System.currentTimeMillis(); - // Write messages to the queue buffer - byteBuffer.put(this.msgStoreItemMemory.array(), 0, msgLen); - - AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgId, - msgInner.getStoreTimestamp(), queueOffset, 0); - return result; - } - - - - private void resetByteBuffer(final ByteBuffer byteBuffer, final int limit) { - byteBuffer.flip(); - byteBuffer.limit(limit); - } - - protected static int calMsgLength(int sysFlag, int srcPartyIdLength ,int desPartyIdLength,int bodyLength, int topicLength, int propertiesLength) { - int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; - int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; - final int msgLen = 4 //TOTALSIZE - + 4 //MAGICCODE - + 4 //BODYCRC - + 4 //QUEUEID - + 4 //FLAG - //+ 8 //QUEUEOFFSET - + 1 + (srcPartyIdLength > 0 ? srcPartyIdLength : 0) + // 15 BODY + this.msgStoreItemMemory.putInt(bodyLength); + if (bodyLength > 0) + this.msgStoreItemMemory.put(msgInner.getBody()); + // 16 TOPIC + this.msgStoreItemMemory.putShort((short) topicLength); + this.msgStoreItemMemory.put(topicData); + // 17 PROPERTIES + this.msgStoreItemMemory.putShort((short) propertiesLength); + if (propertiesLength > 0) + this.msgStoreItemMemory.put(propertiesData); + + final long beginTimeMills = System.currentTimeMillis(); + // Write messages to the queue buffer + byteBuffer.put(this.msgStoreItemMemory.array(), 0, msgLen); + + AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgId, + msgInner.getStoreTimestamp(), queueOffset, 0); + return result; + } - // + 8 //PHYSICALOFFSET - + 1 + (desPartyIdLength > 0 ? desPartyIdLength : 0) - + 4 //SYSFLAG - + 8 //BORNTIMESTAMP - // + bornhostLength //BORNHOST - // + 8 //STORETIMESTAMP - // + storehostAddressLength //STOREHOSTADDRESS - // + 4 //RECONSUMETIMES - // + 8 //Prepared Transaction Offset - + 4 + (bodyLength > 0 ? bodyLength : 0) //BODY - + 2 + topicLength //TOPIC - + 2 + (propertiesLength > 0 ? propertiesLength : 0) //propertiesLength - + 0; - return msgLen; + private void resetByteBuffer(final ByteBuffer byteBuffer, final int limit) { + byteBuffer.flip(); + byteBuffer.limit(limit); } - } \ No newline at end of file +} \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/Message.java b/java/osx/broker/src/main/java/com/osx/broker/message/Message.java index 86cfa7c88b..c3a7f98024 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/Message.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/Message.java @@ -1,4 +1,3 @@ - package com.osx.broker.message; import com.osx.broker.util.MessageConst; @@ -48,10 +47,6 @@ public Message(String topic, String tags, String keys, byte[] body) { this(topic, tags, keys, 0, body, true); } - public void setKeys(String keys) { - this.putProperty(MessageConst.PROPERTY_KEYS, keys); - } - void putProperty(final String name, final String value) { if (null == this.properties) { this.properties = new HashMap(); @@ -69,13 +64,13 @@ void clearProperty(final String name) { public void putUserProperty(final String name, final String value) { if (MessageConst.STRING_HASH_SET.contains(name)) { throw new RuntimeException(String.format( - "The Property<%s> is used by system, input another please", name)); + "The Property<%s> is used by system, input another please", name)); } if (value == null || value.trim().isEmpty() - || name == null || name.trim().isEmpty()) { + || name == null || name.trim().isEmpty()) { throw new IllegalArgumentException( - "The name or value of property can not be null or blank string!" + "The name or value of property can not be null or blank string!" ); } @@ -114,6 +109,10 @@ public String getKeys() { return this.getProperty(MessageConst.PROPERTY_KEYS); } + public void setKeys(String keys) { + this.putProperty(MessageConst.PROPERTY_KEYS, keys); + } + public void setKeys(Collection keys) { StringBuffer sb = new StringBuffer(); for (String k : keys) { @@ -196,11 +195,11 @@ public void setTransactionId(String transactionId) { @Override public String toString() { return "Message{" + - "topic='" + topic + '\'' + - ", flag=" + flag + - ", properties=" + properties + - ", body=" + Arrays.toString(body) + - ", transactionId='" + transactionId + '\'' + - '}'; + "topic='" + topic + '\'' + + ", flag=" + flag + + ", properties=" + properties + + ", body=" + Arrays.toString(body) + + ", transactionId='" + transactionId + '\'' + + '}'; } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageDecoder.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageDecoder.java index db7067b3a3..80f96ca1af 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageDecoder.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageDecoder.java @@ -1,11 +1,10 @@ - package com.osx.broker.message; -import java.net.Inet4Address; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.net.UnknownHostException; +import com.osx.broker.grpc.MessageFlag; +import com.osx.broker.util.MessageId; +import com.osx.broker.util.UtilAll; + +import java.net.*; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; @@ -13,11 +12,6 @@ import java.util.List; import java.util.Map; - -import com.osx.broker.grpc.MessageFlag; -import com.osx.broker.util.MessageId; -import com.osx.broker.util.UtilAll; - public class MessageDecoder { // public final static int MSG_ID_LENGTH = 8 + 8; @@ -58,9 +52,9 @@ public static String createMessageId(final ByteBuffer input, final ByteBuffer ad return UtilAll.bytes2string(input.array()); } - public static MessageExtBrokerInner buildMessageExtBrokerInner(String topic , byte[] body, - int queueId, MessageFlag flag ,String srcPartyId,String desPartyId){ - MessageExtBrokerInner messageExtBrokerInner = new MessageExtBrokerInner(); + public static MessageExtBrokerInner buildMessageExtBrokerInner(String topic, byte[] body, + int queueId, MessageFlag flag, String srcPartyId, String desPartyId) { + MessageExtBrokerInner messageExtBrokerInner = new MessageExtBrokerInner(); messageExtBrokerInner.setQueueId(queueId); messageExtBrokerInner.setBody(body); messageExtBrokerInner.setTopic(topic); @@ -68,7 +62,7 @@ public static MessageExtBrokerInner buildMessageExtBrokerInner(String topic messageExtBrokerInner.setBornTimestamp(System.currentTimeMillis()); messageExtBrokerInner.setDesPartyId(srcPartyId); messageExtBrokerInner.setSrcPartyId(desPartyId); - return messageExtBrokerInner; + return messageExtBrokerInner; } public static String createMessageId(SocketAddress socketAddress, long transactionIdhashCode) { @@ -111,19 +105,19 @@ public static Map decodeProperties(ByteBuffer byteBuffer) { int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; int bodySizePosition = 4 // 1 TOTALSIZE - + 4 // 2 MAGICCODE - + 4 // 3 BODYCRC - + 4 // 4 QUEUEID - + 4 // 5 FLAG - + 8 // 6 QUEUEOFFSET - + 8 // 7 PHYSICALOFFSET - + 4 // 8 SYSFLAG - + 8 // 9 BORNTIMESTAMP - + bornhostLength // 10 BORNHOST - + 8 // 11 STORETIMESTAMP - + storehostAddressLength // 12 STOREHOSTADDRESS - + 4 // 13 RECONSUMETIMES - + 8; // 14 Prepared Transaction Offset + + 4 // 2 MAGICCODE + + 4 // 3 BODYCRC + + 4 // 4 QUEUEID + + 4 // 5 FLAG + + 8 // 6 QUEUEOFFSET + + 8 // 7 PHYSICALOFFSET + + 4 // 8 SYSFLAG + + 8 // 9 BORNTIMESTAMP + + bornhostLength // 10 BORNHOST + + 8 // 11 STORETIMESTAMP + + storehostAddressLength // 12 STOREHOSTADDRESS + + 4 // 13 RECONSUMETIMES + + 8; // 14 Prepared Transaction Offset int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition); byte topicLength = byteBuffer.get(topicLengthPosition); @@ -265,12 +259,12 @@ public static MessageExt decode(ByteBuffer byteBuffer, final boolean readBody) { // } public static MessageExt decode( - ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody) { + ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody) { return decode(byteBuffer, readBody, deCompressBody, false); } public static MessageExt decode( - ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody, final boolean isClient) { + ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody, final boolean isClient) { try { MessageExt msgExt; @@ -301,13 +295,13 @@ public static MessageExt decode( msgExt.setFlag(flag); // 6 QUEUEOFFSET - int srcPartyIdLength = byteBuffer.get(); - if(srcPartyIdLength>0){ - byte[] srcPartyBytes =new byte[srcPartyIdLength]; - byteBuffer.get(srcPartyBytes); - String srcPartyId = new String(srcPartyBytes); - msgExt.setSrcPartyId(srcPartyId); - } + int srcPartyIdLength = byteBuffer.get(); + if (srcPartyIdLength > 0) { + byte[] srcPartyBytes = new byte[srcPartyIdLength]; + byteBuffer.get(srcPartyBytes); + String srcPartyId = new String(srcPartyBytes); + msgExt.setSrcPartyId(srcPartyId); + } // long queueOffset = byteBuffer.getLong(); // msgExt.setQueueOffset(queueOffset); @@ -317,11 +311,11 @@ public static MessageExt decode( // msgExt.setCommitLogOffset(physicOffset); - int desPartyIdLength = byteBuffer.get(); - if(desPartyIdLength>0){ - byte[] desPartyIdBytes =new byte[desPartyIdLength]; + int desPartyIdLength = byteBuffer.get(); + if (desPartyIdLength > 0) { + byte[] desPartyIdBytes = new byte[desPartyIdLength]; byteBuffer.get(desPartyIdBytes); - String desPartyId = new String(desPartyIdBytes); + String desPartyId = new String(desPartyIdBytes); msgExt.setDesPartyId(desPartyId); } @@ -475,11 +469,11 @@ public static byte[] encodeMessage(Message message) { short propertiesLength = (short) propertiesBytes.length; int sysFlag = message.getFlag(); int storeSize = 4 // 1 TOTALSIZE - + 4 // 2 MAGICCOD - + 4 // 3 BODYCRC - + 4 // 4 FLAG - + 4 + bodyLen // 4 BODY - + 2 + propertiesLength; + + 4 // 2 MAGICCOD + + 4 // 3 BODYCRC + + 4 // 4 FLAG + + 4 + bodyLen // 4 BODY + + 2 + propertiesLength; ByteBuffer byteBuffer = ByteBuffer.allocate(storeSize); // 1 TOTALSIZE byteBuffer.putInt(storeSize); diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageExt.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageExt.java index a24b62552e..f118c5f3d5 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageExt.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageExt.java @@ -1,4 +1,3 @@ - package com.osx.broker.message; import java.net.Inet4Address; @@ -17,7 +16,7 @@ public class MessageExt extends Message { private int storeSize; - // private long queueOffset; + // private long queueOffset; private int sysFlag; @@ -32,31 +31,14 @@ public class MessageExt extends Message { private int reconsumeTimes; private long preparedTransactionOffset; - - public String getSrcPartyId() { - return srcPartyId; - } - - public void setSrcPartyId(String srcPartyId) { - this.srcPartyId = srcPartyId; - } - - public String getDesPartyId() { - return desPartyId; - } - - public void setDesPartyId(String desPartyId) { - this.desPartyId = desPartyId; - } - - private String srcPartyId; - private String desPartyId; + private String srcPartyId; + private String desPartyId; public MessageExt() { } public MessageExt(int queueId, long bornTimestamp, SocketAddress bornHost, long storeTimestamp, - SocketAddress storeHost, String msgId) { + SocketAddress storeHost, String msgId) { this.queueId = queueId; this.bornTimestamp = bornTimestamp; this.bornHost = bornHost; @@ -65,14 +47,6 @@ public MessageExt(int queueId, long bornTimestamp, SocketAddress bornHost, long this.msgId = msgId; } -// public static TopicFilterType parseTopicFilterType(final int sysFlag) { -// if ((sysFlag & MessageSysFlag.MULTI_TAGS_FLAG) == MessageSysFlag.MULTI_TAGS_FLAG) { -// return TopicFilterType.MULTI_TAG; -// } -// -// return TopicFilterType.SINGLE_TAG; -// } - public static ByteBuffer socketAddress2ByteBuffer(final SocketAddress socketAddress, final ByteBuffer byteBuffer) { InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; InetAddress address = inetSocketAddress.getAddress(); @@ -98,6 +72,30 @@ public static ByteBuffer socketAddress2ByteBuffer(SocketAddress socketAddress) { return socketAddress2ByteBuffer(socketAddress, byteBuffer); } + public String getSrcPartyId() { + return srcPartyId; + } + + public void setSrcPartyId(String srcPartyId) { + this.srcPartyId = srcPartyId; + } + +// public static TopicFilterType parseTopicFilterType(final int sysFlag) { +// if ((sysFlag & MessageSysFlag.MULTI_TAGS_FLAG) == MessageSysFlag.MULTI_TAGS_FLAG) { +// return TopicFilterType.MULTI_TAG; +// } +// +// return TopicFilterType.SINGLE_TAG; +// } + + public String getDesPartyId() { + return desPartyId; + } + + public void setDesPartyId(String desPartyId) { + this.desPartyId = desPartyId; + } + public ByteBuffer getBornHostBytes() { return socketAddress2ByteBuffer(this.bornHost); } @@ -253,10 +251,10 @@ public void setPreparedTransactionOffset(long preparedTransactionOffset) { @Override public String toString() { return "MessageExt [brokerName=" + brokerName + ", queueId=" + queueId + ", storeSize=" + storeSize - + ", sysFlag=" + sysFlag + ", bornTimestamp=" + bornTimestamp + ", bornHost=" + bornHost - + ", storeTimestamp=" + storeTimestamp + ", storeHost=" + storeHost + ", msgId=" + msgId - + ", commitLogOffset=" + commitLogOffset + ", bodyCRC=" + bodyCRC + ", reconsumeTimes=" - + reconsumeTimes + ", preparedTransactionOffset=" + preparedTransactionOffset - + ", toString()=" + super.toString() + "]"; + + ", sysFlag=" + sysFlag + ", bornTimestamp=" + bornTimestamp + ", bornHost=" + bornHost + + ", storeTimestamp=" + storeTimestamp + ", storeHost=" + storeHost + ", msgId=" + msgId + + ", commitLogOffset=" + commitLogOffset + ", bodyCRC=" + bodyCRC + ", reconsumeTimes=" + + reconsumeTimes + ", preparedTransactionOffset=" + preparedTransactionOffset + + ", toString()=" + super.toString() + "]"; } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageExtBrokerInner.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageExtBrokerInner.java index 495060fa68..37c5b8e112 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageExtBrokerInner.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageExtBrokerInner.java @@ -1,12 +1,11 @@ - package com.osx.broker.message; - public class MessageExtBrokerInner extends MessageExt { private static final long serialVersionUID = 7256001576878700634L; private String propertiesString; private long tagsCode; + public String getPropertiesString() { return propertiesString; } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageQueueManager.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageQueueManager.java index 1f229cea89..8fca3c6184 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageQueueManager.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageQueueManager.java @@ -3,5 +3,4 @@ public class MessageQueueManager { - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageStoreConfig.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageStoreConfig.java index 8b015dbc6d..996a17fe56 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageStoreConfig.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageStoreConfig.java @@ -1,8 +1,6 @@ - package com.osx.broker.message; - import java.io.File; @@ -14,7 +12,7 @@ public class MessageStoreConfig { //The directory in which the commitlog is kept private String storePathCommitLog = System.getProperty("user.home") + File.separator + "store" - + File.separator + "commitlog"; + + File.separator + "commitlog"; // CommitLog file size,default is 1G private int mappedFileSizeCommitLog = 1024 * 1024 * 1024; @@ -110,7 +108,7 @@ public class MessageStoreConfig { //private BrokerRole brokerRole = BrokerRole.ASYNC_MASTER; -// private FlushDiskType flushDiskType = FlushDiskType.ASYNC_FLUSH; + // private FlushDiskType flushDiskType = FlushDiskType.ASYNC_FLUSH; private int syncFlushTimeout = 1000 * 5; private String messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h"; private long flushDelayOffsetInterval = 1000 * 10; @@ -610,7 +608,6 @@ public void setDefaultQueryMaxNum(int defaultQueryMaxNum) { // return transientStorePoolEnable && FlushDiskType.ASYNC_FLUSH == getFlushDiskType() // && BrokerRole.SLAVE != getBrokerRole(); // } - public void setTransientStorePoolEnable(final boolean transientStorePoolEnable) { this.transientStorePoolEnable = transientStorePoolEnable; } diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageSysFlag.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageSysFlag.java index 60a8e07854..cd855b975b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageSysFlag.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageSysFlag.java @@ -1,4 +1,3 @@ - package com.osx.broker.message; public class MessageSysFlag { diff --git a/java/osx/broker/src/main/java/com/osx/broker/message/MessageWraper.java b/java/osx/broker/src/main/java/com/osx/broker/message/MessageWraper.java index 685d975a67..a32598714a 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/message/MessageWraper.java +++ b/java/osx/broker/src/main/java/com/osx/broker/message/MessageWraper.java @@ -2,7 +2,9 @@ public class MessageWraper { - MessageExt message; + MessageExt message; + long indexFileOffset; + long indexFileTotal; public MessageExt getMessage() { return message; @@ -20,8 +22,6 @@ public void setIndexFileOffset(long indexFileOffset) { this.indexFileOffset = indexFileOffset; } - long indexFileOffset; - public long getIndexFileTotal() { return indexFileTotal; } @@ -29,6 +29,4 @@ public long getIndexFileTotal() { public void setIndexFileTotal(long indexFileTotal) { this.indexFileTotal = indexFileTotal; } - - long indexFileTotal; } diff --git a/java/osx/broker/src/main/java/com/osx/broker/metric/ClusterMetricLeapArray.java b/java/osx/broker/src/main/java/com/osx/broker/metric/ClusterMetricLeapArray.java index d6b133b9f3..dcdbe7d510 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/metric/ClusterMetricLeapArray.java +++ b/java/osx/broker/src/main/java/com/osx/broker/metric/ClusterMetricLeapArray.java @@ -16,9 +16,6 @@ package com.osx.broker.metric; - - - import com.osx.core.flow.ClusterFlowEvent; import com.osx.core.flow.ClusterMetricBucket; import com.osx.core.flow.LeapArray; diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/AbstractPtpServiceAdaptor.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/AbstractPtpServiceAdaptor.java index 8f92802d34..753ffd0130 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/AbstractPtpServiceAdaptor.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/AbstractPtpServiceAdaptor.java @@ -3,16 +3,16 @@ import com.osx.core.context.Context; import com.osx.core.exceptions.ExceptionInfo; import com.osx.core.service.AbstractServiceAdaptor; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; -public abstract class AbstractPtpServiceAdaptor extends AbstractServiceAdaptor { +public abstract class AbstractPtpServiceAdaptor extends AbstractServiceAdaptor { @Override - protected Pcp.Outbound transformExceptionInfo(Context context, ExceptionInfo exceptionInfo) { - Pcp.Outbound.Builder builder = Pcp.Outbound.newBuilder(); + protected Osx.Outbound transformExceptionInfo(Context context, ExceptionInfo exceptionInfo) { + Osx.Outbound.Builder builder = Osx.Outbound.newBuilder(); builder.setCode(exceptionInfo.getCode()); builder.setMessage(exceptionInfo.getMessage()); - return builder.build(); + return builder.build(); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpAckService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpAckService.java index b33add6e02..88373104d0 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpAckService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpAckService.java @@ -1,75 +1,77 @@ package com.osx.broker.ptp; -import com.osx.core.constant.Dict; +import com.osx.broker.ServiceContainer; +import com.osx.broker.consumer.UnaryConsumer; +import com.osx.broker.queue.TransferQueue; +import com.osx.broker.queue.TransferQueueApplyInfo; import com.osx.core.config.MetaInfo; -import com.osx.core.router.RouterInfo; +import com.osx.core.constant.ActionType; +import com.osx.core.constant.Dict; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; import com.osx.core.exceptions.ConsumerNotExistException; import com.osx.core.exceptions.InvalidRedirectInfoException; import com.osx.core.exceptions.TransferQueueNotExistException; +import com.osx.core.router.RouterInfo; import com.osx.core.service.InboundPackage; -import com.osx.broker.ServiceContainer; -import com.osx.broker.consumer.UnaryConsumer; -import com.osx.broker.queue.TransferQueue; -import com.osx.broker.queue.TransferQueueApplyInfo; import org.apache.commons.lang3.StringUtils; -import org.ppc.ptp.Pcp; + +import org.ppc.ptp.Osx; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static com.osx.broker.util.TransferUtil.redirect; -public class PtpAckService extends AbstractPtpServiceAdaptor{ +public class PtpAckService extends AbstractPtpServiceAdaptor { Logger logger = LoggerFactory.getLogger(PtpAckService.class); @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { - context.setActionType("local-ack"); - Pcp.Inbound inbound = data.getBody(); - Pcp.Outbound.Builder outboundBuilder = Pcp.Outbound.newBuilder(); - String sessionId=context.getSessionId(); + protected Osx.Outbound doService(Context context, InboundPackage data) { + context.setActionType(ActionType.LOCAL_ACK.getAlias()); + Osx.Inbound inbound = data.getBody(); + Osx.Outbound.Builder outboundBuilder = Osx.Outbound.newBuilder(); + String sessionId = context.getSessionId(); String topic = context.getTopic(); Long offset = context.getRequestMsgIndex(); TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(topic); /** * 若本地queue不存在,则检查是否在集群中其他节点 */ - if(transferQueue==null) { + if (transferQueue == null) { if (MetaInfo.isCluster()) { - TransferQueueApplyInfo transferQueueApplyInfo = ServiceContainer.transferQueueManager.queryGlobleQueue(topic); - if(transferQueueApplyInfo==null){ + TransferQueueApplyInfo transferQueueApplyInfo = ServiceContainer.transferQueueManager.queryGlobleQueue(topic); + if (transferQueueApplyInfo == null) { throw new TransferQueueNotExistException(); - }else{ - context.setActionType("redirect-ack"); - String[] ipport =transferQueueApplyInfo.getInstanceId().split(":"); + } else { + + context.setActionType(ActionType.REDIRECT_ACK.getAlias()); + String[] ipport = transferQueueApplyInfo.getInstanceId().split(":"); RouterInfo redirectRouterInfo = new RouterInfo(); String redirectIp = ipport[0]; int redirectPort = Integer.parseInt(ipport[1]); - if(StringUtils.isEmpty(redirectIp)||redirectPort==0){ - logger.error("invalid redirect info {}:{}",redirectIp,redirectPort); + if (StringUtils.isEmpty(redirectIp) || redirectPort == 0) { + logger.error("invalid redirect info {}:{}", redirectIp, redirectPort); throw new InvalidRedirectInfoException(); } redirectRouterInfo.setHost(redirectIp); redirectRouterInfo.setPort(redirectPort); //context.setRouterInfo(redirectRouterInfo); - context.setActionType("inner-redirect"); - return redirect(context,inbound,redirectRouterInfo,false); + return redirect(context, inbound, redirectRouterInfo, false); } } else { throw new TransferQueueNotExistException(); } } UnaryConsumer unaryConsumer = ServiceContainer.consumerManager.getUnaryConsumer(topic); - if(unaryConsumer!=null){ - long currentMsgIndex = unaryConsumer.ack(offset); + if (unaryConsumer != null) { + long currentMsgIndex = unaryConsumer.ack(offset); context.setCurrentMsgIndex(currentMsgIndex); outboundBuilder.setCode(StatusCode.SUCCESS); outboundBuilder.setMessage(Dict.SUCCESS); - return outboundBuilder.build(); - }else{ - throw new ConsumerNotExistException("consumer is not exist"); + return outboundBuilder.build(); + } else { + throw new ConsumerNotExistException("consumer is not exist"); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpCancelTransferService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpCancelTransferService.java index 318750cb4d..1e02f27ceb 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpCancelTransferService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpCancelTransferService.java @@ -1,32 +1,33 @@ package com.osx.broker.ptp; +import com.osx.broker.ServiceContainer; import com.osx.core.constant.Dict; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; import com.osx.core.service.InboundPackage; -import com.osx.broker.ServiceContainer; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; + import java.util.List; public class PtpCancelTransferService extends AbstractPtpServiceAdaptor { - public PtpCancelTransferService(){ + public PtpCancelTransferService() { this.setServiceName("cansel-unary"); } @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { - - String sessionId = context.getSessionId(); - String topic = context.getTopic(); - List cleanedTransferId = ServiceContainer.transferQueueManager.cleanByParam(sessionId,topic); - if(cleanedTransferId!=null){ - for(String transferIdClean :cleanedTransferId) { - ServiceContainer. consumerManager.onComplete(transferIdClean); + protected Osx.Outbound doService(Context context, InboundPackage data) { + + String sessionId = context.getSessionId(); + String topic = context.getTopic(); + List cleanedTransferId = ServiceContainer.transferQueueManager.cleanByParam(sessionId, topic); + if (cleanedTransferId != null) { + for (String transferIdClean : cleanedTransferId) { + ServiceContainer.consumerManager.onComplete(transferIdClean); } } - Pcp.Outbound.Builder outBoundBuilder = Pcp.Outbound.newBuilder(); + Osx.Outbound.Builder outBoundBuilder = Osx.Outbound.newBuilder(); outBoundBuilder.setCode(StatusCode.SUCCESS).setMessage(Dict.SUCCESS); return outBoundBuilder.build(); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpClusterQueueApplyService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpClusterQueueApplyService.java index 10d8977ade..68cd061eac 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpClusterQueueApplyService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpClusterQueueApplyService.java @@ -1,17 +1,18 @@ package com.osx.broker.ptp; +import com.osx.broker.ServiceContainer; import com.osx.core.context.Context; import com.osx.core.service.InboundPackage; -import com.osx.broker.ServiceContainer; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; + public class PtpClusterQueueApplyService extends AbstractPtpServiceAdaptor { @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { + protected Osx.Outbound doService(Context context, InboundPackage data) { - Pcp.Inbound inbound + Osx.Inbound inbound = data.getBody(); - Pcp.Outbound outbound = ServiceContainer.transferQueueManager.applyFromMaster(context ,inbound); + Osx.Outbound outbound = ServiceContainer.transferQueueManager.applyFromMaster(context, inbound); return outbound; } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpConsumeService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpConsumeService.java index b0180aa379..da97a092f5 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpConsumeService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpConsumeService.java @@ -1,45 +1,41 @@ package com.osx.broker.ptp; import com.google.common.base.Preconditions; -import com.osx.core.constant.Dict; -import com.osx.core.frame.GrpcConnectionFactory; -import com.osx.core.router.RouterInfo; -import com.osx.core.constant.StatusCode; -import com.osx.core.context.Context; -import com.osx.core.exceptions.TransferQueueNotExistException; -import com.osx.core.service.InboundPackage; import com.osx.broker.ServiceContainer; import com.osx.broker.consumer.UnaryConsumer; import com.osx.broker.queue.TransferQueue; import com.osx.broker.queue.TransferQueueApplyInfo; import com.osx.broker.util.TransferUtil; +import com.osx.core.constant.ActionType; +import com.osx.core.constant.Dict; +import com.osx.core.constant.StatusCode; +import com.osx.core.context.Context; +import com.osx.core.exceptions.ParameterException; +import com.osx.core.exceptions.TransferQueueNotExistException; +import com.osx.core.frame.GrpcConnectionFactory; +import com.osx.core.router.RouterInfo; +import com.osx.core.service.InboundPackage; import io.grpc.ManagedChannel; import io.grpc.stub.StreamObserver; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.ppc.ptp.PrivateTransferProtocolGrpc; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PtpConsumeService extends AbstractPtpServiceAdaptor { +public class PtpConsumeService extends AbstractPtpServiceAdaptor { + Logger logger = LoggerFactory.getLogger(PtpConsumeService.class); - public PtpConsumeService(){ + public PtpConsumeService() { this.setServiceName("consume-unary"); } - static final String CLIENT_CONSUME="client-consume"; - - static final String DEFUALT_CONSUME= "default-consume"; - - static final String REDIRECT_CONSUME = "redirect-consume"; - - @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { - context.setActionType(DEFUALT_CONSUME); - Pcp.Inbound inbound = data.getBody(); - String topic = context.getTopic(); + protected Osx.Outbound doService(Context context, InboundPackage data) { + context.setActionType(ActionType.DEFUALT_CONSUME.getAlias()); + Osx.Inbound inbound = data.getBody(); + String topic = context.getTopic(); // FireworkTransfer.ConsumeRequest consumeRequest = data.getBody(); @@ -50,11 +46,11 @@ protected Pcp.Outbound doService(Context context, InboundPackage da TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(topic); - if(transferQueue==null){ + if (transferQueue == null) { TransferQueueApplyInfo transferQueueApplyInfo = ServiceContainer.transferQueueManager.queryGlobleQueue(topic); - if(transferQueueApplyInfo==null) { + if (transferQueueApplyInfo == null) { throw new TransferQueueNotExistException(); - }else{ + } else { String[] args = transferQueueApplyInfo.getInstanceId().split(":"); String ip = args[0]; int port = Integer.parseInt(args[1]); @@ -62,14 +58,17 @@ protected Pcp.Outbound doService(Context context, InboundPackage da routerInfo.setHost(ip); routerInfo.setPort(port); context.setRouterInfo(routerInfo); - return redirect(context,inbound); + return redirect(context, inbound); } } - StreamObserver streamObserver= (StreamObserver) context.getData(Dict.RESPONSE_STREAM_OBSERVER); - Long offset = context.getRequestMsgIndex(); - Preconditions.checkArgument(offset!=null); - if(offset>0) { - context.setActionType(CLIENT_CONSUME); + StreamObserver streamObserver = (StreamObserver) context.getData(Dict.RESPONSE_STREAM_OBSERVER); + Long offset = context.getRequestMsgIndex(); + Preconditions.checkArgument(offset != null); + if(offset==null){ + throw new ParameterException("offset is null"); + } + if (offset > 0) { + context.setActionType(ActionType.CUSTOMER_CONSUME.getAlias()); } UnaryConsumer consumer = ServiceContainer.consumerManager.getOrCreateUnaryConsumer(topic); TransferQueue.TransferQueueConsumeResult transferQueueConsumeResult = consumer.consume(context, offset); @@ -78,7 +77,7 @@ protected Pcp.Outbound doService(Context context, InboundPackage da /* * 由其他扫描线程应答 */ - if(offset<0) { + if (offset < 0) { UnaryConsumer.LongPullingHold longPullingHold = new UnaryConsumer.LongPullingHold(); longPullingHold.setNeedOffset(offset); longPullingHold.setStreamObserver(streamObserver); @@ -87,18 +86,17 @@ protected Pcp.Outbound doService(Context context, InboundPackage da return null; } } - Pcp.Outbound consumeResponse = TransferUtil.buildResponse(transferQueueConsumeResult.getCode(), "", transferQueueConsumeResult); - return consumeResponse; + Osx.Outbound consumeResponse = TransferUtil.buildResponse(transferQueueConsumeResult.getCode(), "", transferQueueConsumeResult); + return consumeResponse; } - private Pcp.Outbound redirect(Context context , Pcp.Inbound inbound ){ - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo()); - context.setActionType(REDIRECT_CONSUME); + private Osx.Outbound redirect(Context context, Osx.Inbound inbound) { + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo(),true); + context.setActionType(ActionType.REDIRECT_CONSUME.getAlias()); PrivateTransferProtocolGrpc.PrivateTransferProtocolBlockingStub stub = PrivateTransferProtocolGrpc.newBlockingStub(managedChannel); return stub.invoke(inbound); } - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpForwardPushRespSO.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpForwardPushRespSO.java index 8bb4a13a23..78ebcd377e 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpForwardPushRespSO.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpForwardPushRespSO.java @@ -1,21 +1,25 @@ package com.osx.broker.ptp; -import com.osx.core.context.Context; import com.osx.broker.callback.CompleteCallback; import com.osx.broker.callback.ErrorCallback; -//import com.firework.transfer.service.TokenApplyService; import com.osx.broker.util.TransferUtil; +import com.osx.core.context.Context; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.stub.StreamObserver; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PtpForwardPushRespSO implements StreamObserver { +public class PtpForwardPushRespSO implements StreamObserver { Logger logger = LoggerFactory.getLogger(PtpForwardPushRespSO.class); + String sourceType; + StreamObserver backPushRespSO; + CompleteCallback completeCallback; + ErrorCallback errorCallback; + Context context; - public PtpForwardPushRespSO(Context context, StreamObserver backPushRespSO ,String sourceType, CompleteCallback completeCallback, ErrorCallback errorCallback){ + public PtpForwardPushRespSO(Context context, StreamObserver backPushRespSO, String sourceType, CompleteCallback completeCallback, ErrorCallback errorCallback) { this.sourceType = sourceType; this.backPushRespSO = backPushRespSO; @@ -23,15 +27,6 @@ public PtpForwardPushRespSO(Context context, StreamObserver backPushRespSO ,St this.completeCallback = completeCallback; this.errorCallback = errorCallback; } - String sourceType; - - StreamObserver backPushRespSO; - - CompleteCallback completeCallback; - - ErrorCallback errorCallback; - - Context context; public StreamObserver getBackPushRespSO() { return backPushRespSO; @@ -52,22 +47,21 @@ public void setBackPushRespSO(StreamObserver backPushRespSO) { //TokenApplyService tokenApplyService; - @Override - public void onNext(Pcp.Outbound value) { + public void onNext(Osx.Outbound value) { - if(sourceType.equals("proxy")){ - Proxy.Metadata metadata = TransferUtil.buildProxyMetadataFromOutbound(value); + if (sourceType.equals("proxy")) { + Proxy.Metadata metadata = TransferUtil.buildProxyMetadataFromOutbound(value); backPushRespSO.onNext(metadata); - }else{ + } else { backPushRespSO.onNext(value); } } @Override public void onError(Throwable t) { - logger.error("onError {}",t); - if(errorCallback!=null) { + logger.error("onError {}", t); + if (errorCallback != null) { errorCallback.callback(t); } backPushRespSO.onError(t); @@ -76,7 +70,7 @@ public void onError(Throwable t) { @Override public void onCompleted() { - if(completeCallback!=null){ + if (completeCallback != null) { completeCallback.callback(); } backPushRespSO.onCompleted(); diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpProduceService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpProduceService.java index 1bd84b99b9..e8fa8b677a 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpProduceService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpProduceService.java @@ -1,15 +1,5 @@ package com.osx.broker.ptp; -import com.osx.core.config.MetaInfo; -import com.osx.core.constant.DeployMode; -import com.osx.core.constant.StatusCode; -import com.osx.core.context.Context; -import com.osx.core.exceptions.InvalidRedirectInfoException; -import com.osx.core.exceptions.ParameterException; -import com.osx.core.exceptions.ProduceMsgExcption; -import com.osx.core.exceptions.PutMessageException; -import com.osx.core.router.RouterInfo; -import com.osx.core.service.InboundPackage; import com.osx.broker.ServiceContainer; import com.osx.broker.constants.Direction; import com.osx.broker.grpc.MessageFlag; @@ -20,9 +10,19 @@ import com.osx.broker.queue.PutMessageStatus; import com.osx.broker.queue.TransferQueue; import com.osx.broker.util.ResourceUtil; +import com.osx.core.config.MetaInfo; +import com.osx.core.constant.ActionType; +import com.osx.core.constant.DeployMode; +import com.osx.core.constant.StatusCode; +import com.osx.core.context.Context; +import com.osx.core.exceptions.InvalidRedirectInfoException; +import com.osx.core.exceptions.ParameterException; +import com.osx.core.exceptions.ProduceMsgExcption; +import com.osx.core.exceptions.PutMessageException; +import com.osx.core.router.RouterInfo; +import com.osx.core.service.InboundPackage; import org.apache.commons.lang3.StringUtils; - -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,49 +33,50 @@ public class PtpProduceService extends AbstractPtpServiceAdaptor { Logger logger = LoggerFactory.getLogger(PtpProduceService.class); @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { + protected Osx.Outbound doService(Context context, InboundPackage data) { String topic = context.getTopic(); - boolean isDst=false; - RouterInfo routerInfo = context.getRouterInfo(); - String srcPartyId = context.getSrcPartyId(); - String sessionId = context.getSessionId(); - Pcp.Inbound produceRequest = data.getBody(); - if(MetaInfo.PROPERTY_SELF_PARTY.contains(context.getDesPartyId())){ - isDst= true; + boolean isDst = false; + RouterInfo routerInfo = context.getRouterInfo(); + String srcPartyId = context.getSrcPartyId(); + String sessionId = context.getSessionId(); + Osx.Inbound produceRequest = data.getBody(); + if (MetaInfo.PROPERTY_SELF_PARTY.contains(context.getDesPartyId())) { + isDst = true; } - if(!isDst) { + if (!isDst) { /** * 向外转发 */ - return redirect(context, produceRequest, routerInfo, false); - } - else{ + return redirect(context, produceRequest, routerInfo, false); + } else { /** * 本地处理 */ - if(StringUtils.isEmpty(topic)){ - throw new ParameterException(StatusCode.PARAM_ERROR,"topic is null"); + if (StringUtils.isEmpty(topic)) { + throw new ParameterException(StatusCode.PARAM_ERROR, "topic is null"); } - if(StringUtils.isEmpty(sessionId)){ - throw new ParameterException(StatusCode.PARAM_ERROR,"sessionId is null"); + if (StringUtils.isEmpty(sessionId)) { + throw new ParameterException(StatusCode.PARAM_ERROR, "sessionId is null"); } - context.setActionType("download"); - CreateQueueResult createQueueResult = ServiceContainer.transferQueueManager.createNewQueue(topic,sessionId,false); - if(createQueueResult==null){ + context.setActionType(ActionType.MSG_DOWNLOAD.getAlias()); + context.setRouterInfo(null); + CreateQueueResult createQueueResult = ServiceContainer.transferQueueManager.createNewQueue(topic, sessionId, false); + if (createQueueResult == null) { throw new RuntimeException("transfer queue is null"); } - String resource = ResourceUtil.buildResource(srcPartyId+"-"+MetaInfo.PROPERTY_SELF_PARTY, Direction.UP); - if(createQueueResult==null){ + String resource = ResourceUtil.buildResource(srcPartyId + "-" + MetaInfo.PROPERTY_SELF_PARTY, Direction.UP); + + if (createQueueResult == null) { throw new RuntimeException("transfer queue is null"); } //String resource = ResourceUtil.buildResource(context.getSrcPartyId()+"-"+MetaInfo.PROPERTY_SELF_PARTY, Direction.UP); // ServiceContainer.tokenApplyService.applyToken(context,resource,dataSize); //ServiceContainer.flowCounterManager.pass(resource,dataSize); - TransferQueue transferQueue = createQueueResult.getTransferQueue(); - if(transferQueue!=null) { + TransferQueue transferQueue = createQueueResult.getTransferQueue(); + if (transferQueue != null) { //MessageExtBrokerInner messageExtBrokerInner = new MessageExtBrokerInner(); byte[] msgBytes = produceRequest.getPayload().toByteArray(); //context.(msgBytes.length); @@ -85,41 +86,41 @@ protected Pcp.Outbound doService(Context context, InboundPackage da * 这里写成blank 是因为topic长度太长 ,如果调整字节后是可以在这里设置的 */ // messageExtBrokerInner.setTopic("blank"); - MessageExtBrokerInner messageExtBrokerInner = MessageDecoder.buildMessageExtBrokerInner(topic,msgBytes,0, MessageFlag.MSG,context.getSrcPartyId(), + MessageExtBrokerInner messageExtBrokerInner = MessageDecoder.buildMessageExtBrokerInner(topic, msgBytes, 0, MessageFlag.MSG, context.getSrcPartyId(), context.getDesPartyId()); PutMessageResult putMessageResult = transferQueue.putMessage(messageExtBrokerInner); - if(putMessageResult.getPutMessageStatus()!= PutMessageStatus.PUT_OK){ - throw new PutMessageException("put status "+putMessageResult.getPutMessageStatus()); + if (putMessageResult.getPutMessageStatus() != PutMessageStatus.PUT_OK) { + throw new PutMessageException("put status " + putMessageResult.getPutMessageStatus()); } - long logicOffset = putMessageResult.getMsgLogicOffset(); + long logicOffset = putMessageResult.getMsgLogicOffset(); //context.setDataSize(logicOffset); // FireworkTransfer.ProduceResponse.Builder produceResponseBuilder = FireworkTransfer.ProduceResponse.newBuilder(); // produceResponseBuilder.setCode(StatusCode.SUCCESS); // produceResponseBuilder.setMsg("SUCCESS"); - Pcp.Outbound.Builder outBoundBuilder = Pcp.Outbound.newBuilder(); + Osx.Outbound.Builder outBoundBuilder = Osx.Outbound.newBuilder(); outBoundBuilder.setCode(StatusCode.SUCCESS); outBoundBuilder.setMessage("SUCCESS"); return outBoundBuilder.build(); - }else{ + } else { /** * 集群内转发 */ - if(MetaInfo.PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name())) { + if (MetaInfo.PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name())) { RouterInfo redirectRouterInfo = new RouterInfo(); String redirectIp = createQueueResult.getRedirectIp(); int redirectPort = createQueueResult.getPort(); - if(StringUtils.isEmpty(redirectIp)||redirectPort==0){ - logger.error("invalid redirect info {}:{}",redirectIp,redirectPort); + if (StringUtils.isEmpty(redirectIp) || redirectPort == 0) { + logger.error("invalid redirect info {}:{}", redirectIp, redirectPort); throw new InvalidRedirectInfoException(); } redirectRouterInfo.setHost(redirectIp); redirectRouterInfo.setPort(redirectPort); context.setRouterInfo(redirectRouterInfo); - context.setActionType("inner-redirect"); - return redirect(context ,produceRequest, redirectRouterInfo,true); - }else{ - logger.error("create topic {} error",topic); + context.setActionType(ActionType.INNER_REDIRECT.getAlias()); + return redirect(context, produceRequest, redirectRouterInfo, true); + } else { + logger.error("create topic {} error", topic); throw new ProduceMsgExcption(); } } @@ -127,6 +128,4 @@ protected Pcp.Outbound doService(Context context, InboundPackage da } - - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpPushReqStreamObserver.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpPushReqStreamObserver.java index a92ac9a3c6..ef7af70951 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpPushReqStreamObserver.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpPushReqStreamObserver.java @@ -4,15 +4,16 @@ import com.google.common.collect.Maps; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; -import com.osx.core.constant.Dict; +import com.osx.broker.eggroll.*; +import com.osx.broker.util.TransferUtil; import com.osx.core.config.MetaInfo; -import com.osx.core.frame.GrpcConnectionFactory; -import com.osx.core.router.RouterInfo; +import com.osx.core.constant.ActionType; +import com.osx.core.constant.Dict; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; +import com.osx.core.frame.GrpcConnectionFactory; +import com.osx.core.router.RouterInfo; import com.osx.core.utils.ToStringUtils; -import com.osx.broker.eggroll.*; -import com.osx.broker.util.TransferUtil; import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import com.webank.eggroll.core.command.Command; @@ -22,7 +23,8 @@ import io.grpc.ManagedChannel; import io.grpc.stub.StreamObserver; import org.apache.commons.lang3.StringUtils; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; + import org.ppc.ptp.PrivateTransferProtocolGrpc; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,65 +37,65 @@ import static com.osx.broker.ServiceContainer.fateRouterService; -public class PtpPushReqStreamObserver implements StreamObserver { +public class PtpPushReqStreamObserver implements StreamObserver { Logger logger = LoggerFactory.getLogger(PtpPushReqStreamObserver.class); RouterInfo routerInfo; - boolean inited = false; - private boolean isDst=false; + boolean inited = false; Context context; Proxy.Metadata metadata; String brokerTag; - private StreamObserver backRespSO; - CountDownLatch finishLatch= new CountDownLatch(1); - StreamObserver ptpForwardPushReqSO; - StreamObserver oldPushReqSO; - StreamObserver putBatchSinkPushReqSO; - - private void init(Pcp.Inbound inbound){ - Map metaDataMap = inbound.getMetadataMap(); - logger.info("metaDataMap {}",metaDataMap); - String version = metaDataMap.get(Pcp.Header.Version.name()); - String techProviderCode = metaDataMap.get(Pcp.Header.TechProviderCode.name()); - String traceId = metaDataMap.get(Pcp.Header.TraceID.name()); - String token = metaDataMap.get(Pcp.Header.Token.name()); - String sourceNodeId = metaDataMap.get(Pcp.Header.SourceNodeID.name()); - String targetNodeId = metaDataMap.get(Pcp.Header.TargetNodeID.name()); - String sourceInstId = metaDataMap.get(Pcp.Header.SourceInstID.name()); - String targetInstId = metaDataMap.get(Pcp.Header.TargetInstID.name()); - String sessionId = metaDataMap.get(Pcp.Header.SessionID.name()); - String targetMethod = metaDataMap.get(Pcp.Metadata.TargetMethod.name()); - String targetComponentName = metaDataMap.get(Pcp.Metadata.TargetComponentName.name()); - String sourceComponentName = metaDataMap.get(Pcp.Metadata.SourceComponentName.name()); - String sourcePartyId= StringUtils.isEmpty(sourceInstId)?sourceNodeId:sourceInstId+"."+sourceNodeId; - String targetPartyId =StringUtils.isEmpty(targetInstId)? targetNodeId:targetInstId+"."+targetNodeId; - String topic = metaDataMap.get(Pcp.Metadata.MessageTopic.name()); - String offsetString = metaDataMap.get(Pcp.Metadata.MessageOffSet.name()); - routerInfo = fateRouterService.route(sourcePartyId,sourceComponentName,targetPartyId,targetComponentName); - inited =true; + CountDownLatch finishLatch = new CountDownLatch(1); + StreamObserver ptpForwardPushReqSO; + StreamObserver oldPushReqSO; + StreamObserver putBatchSinkPushReqSO; + private boolean isDst = false; + private StreamObserver backRespSO; + + private void init(Osx.Inbound inbound) { + Map metaDataMap = inbound.getMetadataMap(); + logger.info("metaDataMap {}", metaDataMap); + String version = metaDataMap.get(Osx.Header.Version.name()); + String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name()); + String traceId = metaDataMap.get(Osx.Header.TraceID.name()); + String token = metaDataMap.get(Osx.Header.Token.name()); + String sourceNodeId = metaDataMap.get(Osx.Header.SourceNodeID.name()); + String targetNodeId = metaDataMap.get(Osx.Header.TargetNodeID.name()); + String sourceInstId = metaDataMap.get(Osx.Header.SourceInstID.name()); + String targetInstId = metaDataMap.get(Osx.Header.TargetInstID.name()); + String sessionId = metaDataMap.get(Osx.Header.SessionID.name()); + String targetMethod = metaDataMap.get(Osx.Metadata.TargetMethod.name()); + String targetComponentName = metaDataMap.get(Osx.Metadata.TargetComponentName.name()); + String sourceComponentName = metaDataMap.get(Osx.Metadata.SourceComponentName.name()); + String sourcePartyId = StringUtils.isEmpty(sourceInstId) ? sourceNodeId : sourceInstId + "." + sourceNodeId; + String targetPartyId = StringUtils.isEmpty(targetInstId) ? targetNodeId : targetInstId + "." + targetNodeId; + String topic = metaDataMap.get(Osx.Metadata.MessageTopic.name()); + String offsetString = metaDataMap.get(Osx.Metadata.MessageOffSet.name()); + routerInfo = fateRouterService.route(sourcePartyId, sourceComponentName, targetPartyId, targetComponentName); + inited = true; if (MetaInfo.PROPERTY_SELF_PARTY.contains(targetNodeId)) { isDst = true; } - if(isDst){ - Proxy.Packet packet=null; + if (isDst) { + Proxy.Packet packet = null; try { - packet = Proxy.Packet.parseFrom(inbound.getPayload()); + packet = Proxy.Packet.parseFrom(inbound.getPayload()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } - if(packet!=null) { + if (packet != null) { initEggroll(packet); } - }else{ - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(routerInfo,null); - if(TransferUtil.isOldVersionFate(routerInfo.getVersion())) { + } else { + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(routerInfo, null); + if (TransferUtil.isOldVersionFate(routerInfo.getVersion())) { DataTransferServiceGrpc.DataTransferServiceStub stub = DataTransferServiceGrpc.newStub(managedChannel); oldPushReqSO = stub.push(new StreamObserver() { @Override public void onNext(Proxy.Metadata metadata) { - Pcp.Outbound outbound = Pcp.Outbound.newBuilder().setCode(StatusCode.SUCCESS).setMessage(Dict.SUCCESS).setPayload(metadata.toByteString()).build(); + Osx.Outbound outbound = Osx.Outbound.newBuilder().setCode(StatusCode.SUCCESS).setMessage(Dict.SUCCESS).setPayload(metadata.toByteString()).build(); backRespSO.onNext(outbound); } @@ -101,15 +103,16 @@ public void onNext(Proxy.Metadata metadata) { public void onError(Throwable throwable) { backRespSO.onError(throwable); } + @Override public void onCompleted() { backRespSO.onCompleted(); } }); - }else{ - PtpForwardPushRespSO ptpForwardPushRespSO = new PtpForwardPushRespSO(context,backRespSO,"proxy" , null, null); - PrivateTransferProtocolGrpc.PrivateTransferProtocolStub stub =PrivateTransferProtocolGrpc.newStub(managedChannel); - ptpForwardPushReqSO =stub.transport(ptpForwardPushRespSO); + } else { + PtpForwardPushRespSO ptpForwardPushRespSO = new PtpForwardPushRespSO(context, backRespSO, "proxy", null, null); + PrivateTransferProtocolGrpc.PrivateTransferProtocolStub stub = PrivateTransferProtocolGrpc.newStub(managedChannel); + ptpForwardPushReqSO = stub.transport(ptpForwardPushRespSO); } } @@ -118,27 +121,27 @@ public void onCompleted() { } @Override - public void onNext(Pcp.Inbound inbound) { - if(!inited){ + public void onNext(Osx.Inbound inbound) { + if (!inited) { init(inbound); } - if(isDst){ - Proxy.Packet packet = null; + if (isDst) { + Proxy.Packet packet = null; try { packet = Proxy.Packet.parseFrom(inbound.getPayload()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } - Transfer.TransferHeader.Builder transferHeaderBuilder= Transfer.TransferHeader.newBuilder(); - Transfer.TransferHeader tbHeader = transferHeaderBuilder.setId((int)metadata.getSeq()) + Transfer.TransferHeader.Builder transferHeaderBuilder = Transfer.TransferHeader.newBuilder(); + Transfer.TransferHeader tbHeader = transferHeaderBuilder.setId((int) metadata.getSeq()) .setTag(brokerTag) .setExt(packet.getHeader().getExt()).build(); - Transfer.TransferBatch.Builder transferBatchBuilder = Transfer.TransferBatch.newBuilder(); + Transfer.TransferBatch.Builder transferBatchBuilder = Transfer.TransferBatch.newBuilder(); Transfer.TransferBatch tbBatch = transferBatchBuilder.setHeader(tbHeader) .setData(packet.getBody().getValue()) .build(); putBatchSinkPushReqSO.onNext(tbBatch); - }else { + } else { if (ptpForwardPushReqSO != null) { ptpForwardPushReqSO.onNext(inbound); @@ -156,12 +159,12 @@ public void onNext(Pcp.Inbound inbound) { @Override public void onError(Throwable throwable) { - if(isDst){ + if (isDst) { putBatchSinkPushReqSO.onError(throwable); - }else{ + } else { if (ptpForwardPushReqSO != null) { ptpForwardPushReqSO.onError(throwable); - } else if(oldPushReqSO!=null){ + } else if (oldPushReqSO != null) { oldPushReqSO.onError(throwable); } } @@ -169,35 +172,35 @@ public void onError(Throwable throwable) { @Override public void onCompleted() { - if(isDst){ + if (isDst) { putBatchSinkPushReqSO.onCompleted(); - }else{ + } else { if (ptpForwardPushReqSO != null) { ptpForwardPushReqSO.onCompleted(); - } else if(oldPushReqSO!=null){ + } else if (oldPushReqSO != null) { oldPushReqSO.onCompleted(); } } } - private void initEggroll(Proxy.Packet firstRequest){ + private void initEggroll(Proxy.Packet firstRequest) { logger.info("init eggroll begin"); metadata = firstRequest.getHeader(); String oneLineStringMetadata = ToStringUtils.toOneLineString(metadata); ByteString encodedRollSiteHeader = metadata.getExt(); - context.setActionType("push-eggroll"); - ErRollSiteHeader rsHeader=null; + context.setActionType(ActionType.PUSH_EGGROLL.getAlias()); + ErRollSiteHeader rsHeader = null; try { - rsHeader= ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); + rsHeader = ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } //logger.info("=========ErRollSiteHeader {}",rsHeader); //"#", prefix: Array[String] = Array("__rsk") - String rsKey = rsHeader.getRsKey("#","__rsk"); - String sessionId = String.join("_", rsHeader.getRollSiteSessionId() , rsHeader.getDstRole(), rsHeader.getDstPartyId()); + String rsKey = rsHeader.getRsKey("#", "__rsk"); + String sessionId = String.join("_", rsHeader.getRollSiteSessionId(), rsHeader.getDstRole(), rsHeader.getDstPartyId()); context.setSessionId(sessionId); ErSession session = null; try { @@ -212,16 +215,16 @@ private void initEggroll(Proxy.Packet firstRequest){ } String namespace = rsHeader.getRollSiteSessionId(); - String name = rsKey; + String name = rsKey; RollPairContext ctx = new RollPairContext(session); Map rpOptions = Maps.newHashMap(); rpOptions.putAll(rsHeader.getOptions()); - rpOptions.put(Dict.TOTAL_PARTITIONS_SNAKECASE ,rsHeader.getTotalPartitions().toString()); + rpOptions.put(Dict.TOTAL_PARTITIONS_SNAKECASE, rsHeader.getTotalPartitions().toString()); //var rpOptions = rsHeader.options ++ Map(StringConstants.TOTAL_PARTITIONS_SNAKECASE -> rsHeader.totalPartitions.toString) if (rsHeader.getDataType().equals("object")) { - rpOptions.put(Dict.SERDES ,SerdesTypes.EMPTY.name()); + rpOptions.put(Dict.SERDES, SerdesTypes.EMPTY.name()); } else { - rpOptions.put(Dict.SERDES ,rsHeader.getOptions().getOrDefault("serdes", SerdesTypes.PICKLE.name())); + rpOptions.put(Dict.SERDES, rsHeader.getOptions().getOrDefault("serdes", SerdesTypes.PICKLE.name())); } // table creates here @@ -230,17 +233,17 @@ private void initEggroll(Proxy.Packet firstRequest){ Integer partitionId = rsHeader.getPartitionId(); ErPartition partition = rp.getStore().getPartition(partitionId); ErProcessor egg = ctx.getErSession().routeToEgg(partition); - logger.info("egg ========{}",egg); + logger.info("egg ========{}", egg); // RollPair.PUT_BATCH // delim: String = "#", prefix: Array[String] = Array("__rsk") // val PUT_BATCH = "putBatch" // s"${}-${rsHeader.getRsKey()}-${partitionId}" brokerTag = - "putBatch-"+rsHeader.getRsKey("#", "__rsk")+"-"+partitionId; - logger.info("======= brokerTag ======{}",brokerTag); - String jobId = IdUtils.generateJobId(ctx.getErSession().getSessionId(), brokerTag,"-"); - logger.info("======jobId ======={}",jobId); - Map jobOptions = new HashMap<>(); + "putBatch-" + rsHeader.getRsKey("#", "__rsk") + "-" + partitionId; + logger.info("======= brokerTag ======{}", brokerTag); + String jobId = IdUtils.generateJobId(ctx.getErSession().getSessionId(), brokerTag, "-"); + logger.info("======jobId ======={}", jobId); + Map jobOptions = new HashMap<>(); jobOptions.putAll(rsHeader.getOptions()); jobOptions.put(SessionConfKeys.CONFKEY_SESSION_ID, ctx.getErSession().getSessionId()); @@ -260,18 +263,18 @@ private void initEggroll(Proxy.Packet firstRequest){ Future commandFuture = RollPairContext.executor.submit(() -> { CommandClient commandClient = new CommandClient(egg.getCommandEndpoint()); - logger.info("before call EGG_RUN_TASK_COMMAND {}",task); - Command.CommandResponse commandResponse = commandClient.call(RollPair.EGG_RUN_TASK_COMMAND, task); - logger.info("============== EGG_RUN_TASK_COMMAND {}",commandResponse); + logger.info("before call EGG_RUN_TASK_COMMAND {}", task); + Command.CommandResponse commandResponse = commandClient.call(RollPair.EGG_RUN_TASK_COMMAND, task); + logger.info("============== EGG_RUN_TASK_COMMAND {}", commandResponse); - long begin = System.currentTimeMillis(); + long begin = System.currentTimeMillis(); try { - Meta.Task taskMeta = Meta.Task.parseFrom(commandResponse.getResultsList().get(0)); - ErTask erTask = ErTask.parseFromPb(taskMeta); - long now = System.currentTimeMillis(); - logger.info("task ===cost===={}",now -begin); + Meta.Task taskMeta = Meta.Task.parseFrom(commandResponse.getResultsList().get(0)); + ErTask erTask = ErTask.parseFromPb(taskMeta); + long now = System.currentTimeMillis(); + logger.info("task ===cost===={}", now - begin); - return erTask; + return erTask; } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } @@ -283,7 +286,7 @@ private void initEggroll(Proxy.Packet firstRequest){ routerInfo.setPort(egg.getTransferEndpoint().getPort()); context.setSrcPartyId(routerInfo.getSourcePartyId()); context.setDesPartyId(routerInfo.getDesPartyId()); - ManagedChannel channel = GrpcConnectionFactory.createManagedChannel(routerInfo); + ManagedChannel channel = GrpcConnectionFactory.createManagedChannel(routerInfo,false); TransferServiceGrpc.TransferServiceStub stub = TransferServiceGrpc.newStub(channel); putBatchSinkPushReqSO = stub.send(new PutBatchSinkPushRespSO(metadata, commandFuture, new StreamObserver() { @Override @@ -291,11 +294,11 @@ public void onNext(Proxy.Metadata metadata) { /** * 这里需要转换 */ - Pcp.Outbound.Builder outboundBuilder = Pcp.Outbound.newBuilder(); + Osx.Outbound.Builder outboundBuilder = Osx.Outbound.newBuilder(); outboundBuilder.setCode(StatusCode.SUCCESS); outboundBuilder.setMessage(Dict.SUCCESS); outboundBuilder.setPayload(metadata.toByteString()); - Pcp.Outbound outbound = outboundBuilder.build(); + Osx.Outbound outbound = outboundBuilder.build(); backRespSO.onNext(outbound); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpQueryTransferQueueService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpQueryTransferQueueService.java index 6f02d1a5dd..51d5fca217 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpQueryTransferQueueService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpQueryTransferQueueService.java @@ -1,36 +1,36 @@ package com.osx.broker.ptp; +import com.osx.broker.ServiceContainer; +import com.osx.broker.queue.CreateQueueResult; +import com.osx.broker.queue.TransferQueue; +import com.osx.broker.queue.TransferQueueApplyInfo; import com.osx.core.config.MetaInfo; +import com.osx.core.constant.ActionType; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; import com.osx.core.service.InboundPackage; import com.osx.core.utils.NetUtils; -import com.osx.federation.rpc.Osx; -import com.osx.broker.ServiceContainer; -import com.osx.broker.queue.CreateQueueResult; -import com.osx.broker.queue.TransferQueue; -import com.osx.broker.queue.TransferQueueApplyInfo; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Map; -public class PtpQueryTransferQueueService extends AbstractPtpServiceAdaptor { +public class PtpQueryTransferQueueService extends AbstractPtpServiceAdaptor { Logger logger = LoggerFactory.getLogger(PtpQueryTransferQueueService.class); - public PtpQueryTransferQueueService(){ + public PtpQueryTransferQueueService() { this.setServiceName("query"); } @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { + protected Osx.Outbound doService(Context context, InboundPackage data) { - Pcp.Inbound request = data.getBody(); - Pcp.Outbound.Builder outboundBuilder = Pcp.Outbound.newBuilder(); + Osx.Inbound request = data.getBody(); + Osx.Outbound.Builder outboundBuilder = Osx.Outbound.newBuilder(); Map metaDataMap = request.getMetadataMap(); - Osx.TopicInfo topicInfo ; + Osx.TopicInfo topicInfo; // String version = metaDataMap.get(Pcp.Header.Version.name()); // String techProviderCode = metaDataMap.get(Pcp.Header.TechProviderCode.name()); // String traceId = metaDataMap.get(Pcp.Header.TraceID.name()); @@ -39,15 +39,15 @@ protected Pcp.Outbound doService(Context context, InboundPackage da // String targetNodeId = metaDataMap.get(Pcp.Header.TargetNodeID.name()); // String sourceInstId = metaDataMap.get(Pcp.Header.SourceInstID.name()); // String targetInstId = metaDataMap.get(Pcp.Header.TargetInstID.name()); - String sessionId = metaDataMap.get(Pcp.Header.SessionID.name()); + String sessionId = metaDataMap.get(Osx.Header.SessionID.name()); // String targetMethod = metaDataMap.get(Pcp.Metadata.TargetMethod.name()); // String targetComponentName = metaDataMap.get(Pcp.Metadata.TargetComponentName.name()); // String sourceComponentName = metaDataMap.get(Pcp.Metadata.SourceComponentName.name()); // String sourcePartyId= sourceInstId+"."+sourceNodeId; // String targetPartyId = targetNodeId+"."+targetNodeId; - String topic = metaDataMap.get(Pcp.Metadata.MessageTopic.name()); - // String offsetString = metaDataMap.get(Pcp.Metadata.MessageOffSet.name()); - context.setActionType("query"); + String topic = metaDataMap.get(Osx.Metadata.MessageTopic.name()); + // String offsetString = metaDataMap.get(Pcp.Metadata.MessageOffSet.name()); + context.setActionType(ActionType.QUERY_TOPIC.getAlias()); // FireworkTransfer.QueryTransferQueueInfoRequest queryTransferQueueInfoRequest = data.getBody(); // String transferId = queryTransferQueueInfoRequest.getTransferId(); // String sessionId = queryTransferQueueInfoRequest.getSessionId(); @@ -57,30 +57,30 @@ protected Pcp.Outbound doService(Context context, InboundPackage da //Preconditions.checkArgument(StringUtils.isNotEmpty(transferId)); TransferQueue transferQueue = ServiceContainer.transferQueueManager.getQueue(topic); //FireworkTransfer.QueryTransferQueueInfoResponse response; - Osx.TopicInfo.Builder topicInfoBuilder = Osx.TopicInfo.newBuilder(); - if(transferQueue!=null){ - topicInfo = topicInfoBuilder.setTopic(transferQueue.getTransferId()). + Osx.TopicInfo.Builder topicInfoBuilder = Osx.TopicInfo.newBuilder(); + if (transferQueue != null) { + topicInfo = topicInfoBuilder.setTopic(transferQueue.getTransferId()). setCreateTimestamp(transferQueue.getCreateTimestamp()) .setIp(NetUtils.getLocalHost()) .setPort(MetaInfo.PROPERTY_PORT).build(); - }else { + } else { /** * 全局topic信息 */ TransferQueueApplyInfo transferQueueApplyInfo = ServiceContainer.transferQueueManager.queryGlobleQueue(topic); - if(transferQueueApplyInfo!=null){ + if (transferQueueApplyInfo != null) { String instanceId = transferQueueApplyInfo.getInstanceId(); String[] instanceElements = instanceId.split(":"); topicInfoBuilder.setTopic(transferQueueApplyInfo.getTransferId()). setCreateTimestamp(transferQueueApplyInfo.getApplyTimestamp()) .setIp(instanceElements[0]) .setPort(Integer.parseInt(instanceElements[1])); - topicInfo = topicInfoBuilder.build(); - }else{ + topicInfo = topicInfoBuilder.build(); + } else { /** * 由查询创建队列 */ - CreateQueueResult createQueueResult = ServiceContainer.transferQueueManager.createNewQueue(topic,sessionId,false); + CreateQueueResult createQueueResult = ServiceContainer.transferQueueManager.createNewQueue(topic, sessionId, false); topicInfo = topicInfoBuilder .setTopic(topic) .setCreateTimestamp(System.currentTimeMillis()) diff --git a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpUnaryCallService.java b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpUnaryCallService.java index de62958e30..20ab8d4608 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpUnaryCallService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/ptp/PtpUnaryCallService.java @@ -1,16 +1,38 @@ package com.osx.broker.ptp; +import com.osx.core.constant.ActionType; import com.osx.core.context.Context; +import com.osx.core.exceptions.RemoteRpcException; +import com.osx.core.frame.GrpcConnectionFactory; +import com.osx.core.router.RouterInfo; import com.osx.core.service.InboundPackage; -import org.ppc.ptp.Pcp; +import io.grpc.ManagedChannel; +import org.ppc.ptp.Osx; +import org.ppc.ptp.PrivateTransferProtocolGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class PtpUnaryCallService extends AbstractPtpServiceAdaptor { + + Logger logger = LoggerFactory.getLogger(PtpUnaryCallService.class); @Override - protected Pcp.Outbound doService(Context context, InboundPackage data) { - context.setActionType("cancel"); - Pcp.Inbound inbound = data.getBody(); - String topic = context.getTopic(); - return null; + protected Osx.Outbound doService(Context context, InboundPackage data) { + context.setActionType(ActionType.UNARY_CALL_NEW.getAlias()); + RouterInfo routerInfo = context.getRouterInfo(); + Osx.Inbound inbound = data.getBody(); + String host = routerInfo.getHost(); + Integer port = routerInfo.getPort(); + ManagedChannel managedChannel=GrpcConnectionFactory.createManagedChannel(routerInfo,true); + PrivateTransferProtocolGrpc.PrivateTransferProtocolBlockingStub blockingStub = PrivateTransferProtocolGrpc.newBlockingStub(managedChannel); + Osx.Outbound outbound= null; + try { + outbound = blockingStub.invoke(inbound); + }catch(io.grpc.StatusRuntimeException e){ + logger.error("remote rpc error :router info {}",routerInfo); + throw new RemoteRpcException("remote rpc error"); + } + return outbound; } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/Consumer.java b/java/osx/broker/src/main/java/com/osx/broker/queue/Consumer.java index 5bb7544191..1d33e818ff 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/Consumer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/Consumer.java @@ -21,7 +21,7 @@ public interface Consumer { // // public void setCreateTimestamp(long createTimestamp) ; - // public MessageWraper consume(Context context, long offset); - public T consume(Context context, long offset); + // public MessageWraper consume(Context context, long offset); + public T consume(Context context, long offset); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/CreateQueueResult.java b/java/osx/broker/src/main/java/com/osx/broker/queue/CreateQueueResult.java index 4e3dd2bcb5..fbc5fa289b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/CreateQueueResult.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/CreateQueueResult.java @@ -2,36 +2,37 @@ import com.osx.core.utils.JsonUtil; -public class CreateQueueResult{ - TransferQueue transferQueue; - String redirectIp; - int port; - public TransferQueue getTransferQueue() { - return transferQueue; - } - - public void setTransferQueue(TransferQueue transferQueue) { - this.transferQueue = transferQueue; - } - - public String getRedirectIp() { - return redirectIp; - } - - public void setRedirectIp(String redirectIp) { - this.redirectIp = redirectIp; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public String toString(){ - return JsonUtil.object2Json(this); - } - - } \ No newline at end of file +public class CreateQueueResult { + TransferQueue transferQueue; + String redirectIp; + int port; + + public TransferQueue getTransferQueue() { + return transferQueue; + } + + public void setTransferQueue(TransferQueue transferQueue) { + this.transferQueue = transferQueue; + } + + public String getRedirectIp() { + return redirectIp; + } + + public void setRedirectIp(String redirectIp) { + this.redirectIp = redirectIp; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String toString() { + return JsonUtil.object2Json(this); + } + +} \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFile.java b/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFile.java index f115fddd65..629e41307c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFile.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFile.java @@ -1,4 +1,3 @@ - package com.osx.broker.queue; import com.osx.broker.message.*; @@ -6,6 +5,10 @@ import com.osx.broker.util.UtilAll; import com.sun.jna.NativeLong; import com.sun.jna.Pointer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.nio.ch.DirectBuffer; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -20,10 +23,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import sun.nio.ch.DirectBuffer; - public class MappedFile extends ReferenceResource { public static final int OS_PAGE_SIZE = 1024 * 4; protected static final Logger log = LoggerFactory.getLogger(MappedFile.class); @@ -40,7 +39,7 @@ public class MappedFile extends ReferenceResource { * Message will put to here first, and then reput to FileChannel if writeBuffer is not null. */ protected ByteBuffer writeBuffer = null; - // protected TransientStorePool transientStorePool = null; + // protected TransientStorePool transientStorePool = null; private String fileName; private long fileFromOffset; private File file; @@ -91,7 +90,7 @@ public Object run() { } private static Method method(Object target, String methodName, Class[] args) - throws NoSuchMethodException { + throws NoSuchMethodException { try { return target.getClass().getMethod(methodName, args); } catch (NoSuchMethodException e) { @@ -372,11 +371,11 @@ public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " - + this.fileFromOffset); + + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size - + ", fileFromOffset: " + this.fileFromOffset+", readPosition: "+readPosition); + + ", fileFromOffset: " + this.fileFromOffset + ", readPosition: " + readPosition); } return null; @@ -402,13 +401,13 @@ public SelectMappedBufferResult selectMappedBuffer(int pos) { public boolean cleanup(final long currentRef) { if (this.isAvailable()) { log.error("this file[REF:" + currentRef + "] " + this.fileName - + " have not shutdown, stop unmapping."); + + " have not shutdown, stop unmapping."); return false; } if (this.isCleanupOver()) { log.error("this file[REF:" + currentRef + "] " + this.fileName - + " have cleanup, do not do it again."); + + " have cleanup, do not do it again."); return true; } @@ -422,28 +421,28 @@ public boolean cleanup(final long currentRef) { public boolean destroy(final long intervalForcibly) { this.shutdown(intervalForcibly); - // if (this.isCleanupOver()) { - try { - this.fileChannel.close(); - log.info("close file channel " + this.fileName + " OK"); + // if (this.isCleanupOver()) { + try { + this.fileChannel.close(); + log.info("close file channel " + this.fileName + " OK"); - long beginTime = System.currentTimeMillis(); - boolean result = this.file.delete(); - log.info("delete file[REF:" + this.getRefCount() + "] " + this.fileName + long beginTime = System.currentTimeMillis(); + boolean result = this.file.delete(); + log.info("delete file[REF:" + this.getRefCount() + "] " + this.fileName + (result ? " OK, " : " Failed, ") + "W:" + this.getWrotePosition() + " M:" + this.getFlushedPosition() + ", " + UtilAll.computeElapsedTimeMilliseconds(beginTime)); - } catch (Exception e) { - log.warn("close file channel " + this.fileName + " Failed. ", e); - } + } catch (Exception e) { + log.warn("close file channel " + this.fileName + " Failed. ", e); + } - return true; + return true; // } else { // log.warn("destroy mapped file[REF:" + this.getRefCount() + "] " + this.fileName // + " Failed. cleanupOver: " + this.cleanupOver); // } - // return false; + // return false; } public int getWrotePosition() { diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFileQueue.java b/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFileQueue.java index 517ebe5580..850571c994 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFileQueue.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/MappedFileQueue.java @@ -1,21 +1,16 @@ - package com.osx.broker.queue; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.concurrent.CopyOnWriteArrayList; - import com.osx.broker.message.AllocateMappedFileService; import com.osx.broker.message.SelectMappedBufferResult; import com.osx.broker.util.UtilAll; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; + public class MappedFileQueue { private static final Logger log = LoggerFactory.getLogger(MappedFileQueue.class); private static final int DELETE_FILES_BATCH_MAX = 10; @@ -30,7 +25,7 @@ public class MappedFileQueue { private volatile long storeTimestamp = 0; public MappedFileQueue(final String storePath, int mappedFileSize, - AllocateMappedFileService allocateMappedFileService) { + AllocateMappedFileService allocateMappedFileService) { this.storePath = storePath; this.mappedFileSize = mappedFileSize; this.allocateMappedFileService = allocateMappedFileService; @@ -47,7 +42,7 @@ public void checkSelf() { if (pre != null) { if (cur.getFileFromOffset() - pre.getFileFromOffset() != this.mappedFileSize) { log.error("[BUG]The mappedFile queue's data is damaged, the adjacent mappedFile's offset don't match. pre file {}, cur file {}", - pre.getFileName(), cur.getFileName()); + pre.getFileName(), cur.getFileName()); } } pre = cur; @@ -135,7 +130,7 @@ public boolean load() { if (file.length() != this.mappedFileSize) { log.warn(file + "\t" + file.length() - + " length not matched message store config value, please check it manually"); + + " length not matched message store config value, please check it manually"); return false; } @@ -187,11 +182,11 @@ public MappedFile getLastMappedFile(final long startOffset, boolean needCreate) if (createOffset != -1 && needCreate) { String nextFilePath = this.storePath + File.separator + UtilAll.offset2FileName(createOffset); String nextNextFilePath = this.storePath + File.separator - + UtilAll.offset2FileName(createOffset + this.mappedFileSize); + + UtilAll.offset2FileName(createOffset + this.mappedFileSize); MappedFile mappedFile = null; if (this.allocateMappedFileService != null) { - mappedFile = this.allocateMappedFileService.putRequestAndReturnMappedFile(nextFilePath , this.mappedFileSize); + mappedFile = this.allocateMappedFileService.putRequestAndReturnMappedFile(nextFilePath, this.mappedFileSize); } else { try { mappedFile = new MappedFile(nextFilePath, this.mappedFileSize); @@ -240,7 +235,7 @@ public boolean resetOffset(long offset) { if (mappedFileLast != null) { long lastOffset = mappedFileLast.getFileFromOffset() + - mappedFileLast.getWrotePosition(); + mappedFileLast.getWrotePosition(); long diff = lastOffset - offset; final int maxDiff = this.mappedFileSize * 2; @@ -314,9 +309,9 @@ public void deleteLastMappedFile() { } public int deleteExpiredFileByTime(final long expiredTime, - final int deleteFilesInterval, - final long intervalForcibly, - final boolean cleanImmediately) { + final int deleteFilesInterval, + final long intervalForcibly, + final boolean cleanImmediately) { Object[] mfs = this.copyMappedFiles(0); if (null == mfs) @@ -378,7 +373,7 @@ public int deleteExpiredFileByOffset(long offset, int unitSize) { destroy = maxOffsetInLogicQueue < offset; if (destroy) { log.info("physic min offset " + offset + ", logics in current mappedFile max offset " - + maxOffsetInLogicQueue + ", delete it"); + + maxOffsetInLogicQueue + ", delete it"); } } else if (!mappedFile.isAvailable()) { // Handle hanged file. log.warn("Found a hanged consume queue file, attempting to delete it."); @@ -435,7 +430,7 @@ public boolean commit(final int commitLeastPages) { /** * Finds a mapped file by offset. * - * @param offset Offset. + * @param offset Offset. * @param returnFirstOnNotFound If the mapped file is not found, then return the first one. * @return Mapped file or null (when not found and returnFirstOnNotFound is false). */ @@ -446,11 +441,11 @@ public MappedFile findMappedFileByOffset(final long offset, final boolean return if (firstMappedFile != null && lastMappedFile != null) { if (offset < firstMappedFile.getFileFromOffset() || offset >= lastMappedFile.getFileFromOffset() + this.mappedFileSize) { log.warn("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}", - offset, - firstMappedFile.getFileFromOffset(), - lastMappedFile.getFileFromOffset() + this.mappedFileSize, - this.mappedFileSize, - this.mappedFiles.size()); + offset, + firstMappedFile.getFileFromOffset(), + lastMappedFile.getFileFromOffset() + this.mappedFileSize, + this.mappedFileSize, + this.mappedFiles.size()); } else { int index = (int) ((offset / this.mappedFileSize) - (firstMappedFile.getFileFromOffset() / this.mappedFileSize)); MappedFile targetFile = null; @@ -460,13 +455,13 @@ public MappedFile findMappedFileByOffset(final long offset, final boolean return } if (targetFile != null && offset >= targetFile.getFileFromOffset() - && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { + && offset < targetFile.getFileFromOffset() + this.mappedFileSize) { return targetFile; } for (MappedFile tmpMappedFile : this.mappedFiles) { if (offset >= tmpMappedFile.getFileFromOffset() - && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { + && offset < tmpMappedFile.getFileFromOffset() + this.mappedFileSize) { return tmpMappedFile; } } @@ -557,7 +552,7 @@ public void destroy() { File file = new File(storePath); if (file.isDirectory()) { - log.info("delete {}",storePath); + log.info("delete {}", storePath); file.delete(); } } @@ -591,7 +586,6 @@ public void setCommittedWhere(final long committedWhere) { } - // public static void main(String[] args){ // AllocateMappedFileService allocateMappedFileService = new AllocateMappedFileService(); // allocateMappedFileService.start(); diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageReentrantLock.java b/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageReentrantLock.java index 88a163c6d7..499e820d1c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageReentrantLock.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageReentrantLock.java @@ -1,4 +1,3 @@ - package com.osx.broker.queue; import java.util.concurrent.locks.ReentrantLock; diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageResult.java b/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageResult.java index d2369baca9..51338ad683 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageResult.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageResult.java @@ -1,4 +1,3 @@ - package com.osx.broker.queue; import com.osx.broker.message.AppendMessageResult; @@ -6,6 +5,12 @@ public class PutMessageResult { private PutMessageStatus putMessageStatus; private AppendMessageResult appendMessageResult; + private long msgLogicOffset; + + public PutMessageResult(PutMessageStatus putMessageStatus, AppendMessageResult appendMessageResult) { + this.putMessageStatus = putMessageStatus; + this.appendMessageResult = appendMessageResult; + } public long getMsgLogicOffset() { return msgLogicOffset; @@ -15,14 +20,6 @@ public void setMsgLogicOffset(long msgLogicOffset) { this.msgLogicOffset = msgLogicOffset; } - private long msgLogicOffset; - - - public PutMessageResult(PutMessageStatus putMessageStatus, AppendMessageResult appendMessageResult) { - this.putMessageStatus = putMessageStatus; - this.appendMessageResult = appendMessageResult; - } - public boolean isOk() { return this.appendMessageResult != null && this.appendMessageResult.isOk(); } @@ -46,7 +43,7 @@ public void setPutMessageStatus(PutMessageStatus putMessageStatus) { @Override public String toString() { return "PutMessageResult [putMessageStatus=" + putMessageStatus + ", appendMessageResult=" - + appendMessageResult + "]"; + + appendMessageResult + "]"; } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageStatus.java b/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageStatus.java index 938732fd49..d74dbe5a62 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageStatus.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/PutMessageStatus.java @@ -1,4 +1,3 @@ - package com.osx.broker.queue; public enum PutMessageStatus { diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/ReferenceResource.java b/java/osx/broker/src/main/java/com/osx/broker/queue/ReferenceResource.java index a8dec9602e..aa5bda77b1 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/ReferenceResource.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/ReferenceResource.java @@ -1,4 +1,3 @@ - package com.osx.broker.queue; import java.util.concurrent.atomic.AtomicLong; diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueue.java b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueue.java index 1dd7c56166..7850e0942d 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueue.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueue.java @@ -1,31 +1,55 @@ package com.osx.broker.queue; -import com.osx.core.config.MetaInfo; -import com.osx.core.constant.StatusCode; -import com.osx.core.constant.TransferStatus; -import com.osx.core.context.Context; -import com.osx.core.exceptions.TransferQueueInvalidStatusException; -import com.osx.core.queue.TranferQueueInfo; import com.osx.broker.ServiceContainer; import com.osx.broker.callback.CompleteCallback; import com.osx.broker.callback.DestoryCallback; import com.osx.broker.callback.ErrorCallback; - import com.osx.broker.message.MessageDecoder; import com.osx.broker.message.MessageExt; import com.osx.broker.message.MessageExtBrokerInner; import com.osx.broker.message.SelectMappedBufferResult; import com.osx.broker.store.IndexQueue; +import com.osx.core.config.MetaInfo; +import com.osx.core.constant.StatusCode; +import com.osx.core.constant.TransferStatus; +import com.osx.core.context.Context; +import com.osx.core.exceptions.TransferQueueInvalidStatusException; +import com.osx.core.queue.TranferQueueInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class TransferQueue { - Logger logger = LoggerFactory.getLogger(TransferQueue.class); + protected final AtomicInteger wrotePosition = new AtomicInteger(0); + Logger logger = LoggerFactory.getLogger(TransferQueue.class); String transferId; + String sessionId; + String srcPartyId; + String desPartyId; + volatile TransferStatus transferStatus = TransferStatus.INIT; + List errorCallbacks = new ArrayList<>(); + List completeCallbacks = new ArrayList<>(); + List destoryCallbacks = new ArrayList(); + long createTimestamp; + long lastStatusChangeTimestamp; + long lastWriteTimestamp; + long lastReadTimestamp; + boolean writeOver = false; + IndexQueue indexQueue; + TransferQueueManager transferQueueManager; + public TransferQueue(String transferId, TransferQueueManager transferQueueManager, String path) { + this.transferId = transferId; + this.transferQueueManager = transferQueueManager; + this.createTimestamp = System.currentTimeMillis(); + this.lastStatusChangeTimestamp = this.createTimestamp; + this.lastWriteTimestamp = this.createTimestamp; + this.indexQueue = new IndexQueue(transferId, path, MetaInfo.PROPERTY_INDEX_MAP_FILE_SIZE); + + } public String getSessionId() { return sessionId; @@ -51,22 +75,6 @@ public void setDesPartyId(String desPartyId) { this.desPartyId = desPartyId; } - String sessionId; - String srcPartyId; - String desPartyId; - - volatile TransferStatus transferStatus = TransferStatus.INIT; - List errorCallbacks = new ArrayList<>(); - List completeCallbacks = new ArrayList<>(); - List destoryCallbacks = new ArrayList(); - long createTimestamp; - long lastStatusChangeTimestamp; - long lastWriteTimestamp; - long lastReadTimestamp; - boolean writeOver= false; - - - public IndexQueue getIndexQueue() { return indexQueue; } @@ -75,139 +83,61 @@ public void setIndexQueue(IndexQueue indexQueue) { this.indexQueue = indexQueue; } - IndexQueue indexQueue; - - protected final AtomicInteger wrotePosition = new AtomicInteger(0); - TransferQueueManager transferQueueManager; - public TransferQueue(String transferId,TransferQueueManager transferQueueManager ,String path){ - this.transferId = transferId; - this.transferQueueManager = transferQueueManager; - this.createTimestamp = System.currentTimeMillis(); - this.lastStatusChangeTimestamp = this.createTimestamp; - this.lastWriteTimestamp = this.createTimestamp; - this.indexQueue = new IndexQueue(transferId,path, MetaInfo.PROPERTY_INDEX_MAP_FILE_SIZE); - - } - - public synchronized PutMessageResult putMessage(final MessageExtBrokerInner msg) { - if(transferStatus== TransferStatus.TRANSFERING) { + public synchronized PutMessageResult putMessage(final MessageExtBrokerInner msg) { + if (transferStatus == TransferStatus.TRANSFERING) { this.lastWriteTimestamp = System.currentTimeMillis(); - PutMessageResult putMessageResult = ServiceContainer.messageStore.putMessage(msg); - if(putMessageResult.isOk()){ - long beginWriteOffset = putMessageResult.getAppendMessageResult().getWroteOffset(); - int size = putMessageResult.getAppendMessageResult().getWroteBytes(); - logger.info("store begin offset {},size {}",beginWriteOffset,size); - putMessageResult.setMsgLogicOffset( indexQueue.putMessagePositionInfoWrapper(beginWriteOffset,size)); - }else{ + PutMessageResult putMessageResult = ServiceContainer.messageStore.putMessage(msg); + if (putMessageResult.isOk()) { + long beginWriteOffset = putMessageResult.getAppendMessageResult().getWroteOffset(); + int size = putMessageResult.getAppendMessageResult().getWroteBytes(); + logger.info("store begin offset {},size {}", beginWriteOffset, size); + putMessageResult.setMsgLogicOffset(indexQueue.putMessagePositionInfoWrapper(beginWriteOffset, size)); + } else { throw new RuntimeException(); } return putMessageResult; - }else{ - throw new TransferQueueInvalidStatusException( "invalid queue status : "+transferStatus); + } else { + throw new TransferQueueInvalidStatusException("invalid queue status : " + transferStatus); } } - public static class TransferQueueConsumeResult { - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public TransferQueueConsumeResult(String code, - SelectMappedBufferResult selectMappedBufferResult, - long requestIndex, - long logicIndex){ - this.code = code; - this.selectMappedBufferResult = selectMappedBufferResult; - this.requestIndex = requestIndex; - this.logicIndexTotal = logicIndex; - } - - public SelectMappedBufferResult getSelectMappedBufferResult() { - return selectMappedBufferResult; - } - - public void setSelectMappedBufferResult(SelectMappedBufferResult selectMappedBufferResult) { - this.selectMappedBufferResult = selectMappedBufferResult; - } - - public long getRequestIndex() { - return requestIndex; - } + public TransferQueueConsumeResult consumeOneMessage(Context context, long requestIndex) { + TransferQueueConsumeResult transferQueueConsumeResult; - public void setRequestIndex(long requestIndex) { - this.requestIndex = requestIndex; - } - - SelectMappedBufferResult selectMappedBufferResult; - long requestIndex; - - public long getLogicIndexTotal() { - return logicIndexTotal; - } - - public void setLogicIndexTotal(long logicIndexTotal) { - this.logicIndexTotal = logicIndexTotal; - } - - long logicIndexTotal; - - String code="-1"; - - public MessageExt getMessage() { - return message; - } - - public void setMessage(MessageExt message) { - this.message = message; - } - - MessageExt message; - } - - public TransferQueueConsumeResult consumeOneMessage(Context context, long requestIndex) { - TransferQueueConsumeResult transferQueueConsumeResult; - - if(transferStatus==TransferStatus.TRANSFERING) { + if (transferStatus == TransferStatus.TRANSFERING) { this.lastReadTimestamp = System.currentTimeMillis(); long logicIndex = indexQueue.getLogicOffset().get(); context.setRequestMsgIndex(requestIndex); context.setCurrentMsgIndex(logicIndex); - if(requestIndex<= logicIndex) { + if (requestIndex <= logicIndex) { SelectMappedBufferResult indexBufferResult = this.indexQueue.getIndexBuffer(requestIndex); - if(indexBufferResult!=null) { + if (indexBufferResult != null) { long pyOffset = indexBufferResult.getByteBuffer().getLong(); SelectMappedBufferResult msgBufferResult = ServiceContainer.messageStore.consumeOneMessage(pyOffset); - transferQueueConsumeResult = new TransferQueueConsumeResult(StatusCode.SUCCESS,msgBufferResult,requestIndex,logicIndex); + transferQueueConsumeResult = new TransferQueueConsumeResult(StatusCode.SUCCESS, msgBufferResult, requestIndex, logicIndex); MessageExt message = MessageDecoder.decode(transferQueueConsumeResult.getSelectMappedBufferResult().getByteBuffer()); transferQueueConsumeResult.setMessage(message); - }else{ - transferQueueConsumeResult = new TransferQueueConsumeResult(StatusCode.INVALID_INDEXFILE_DETAIL,null,requestIndex,logicIndex); + } else { + transferQueueConsumeResult = new TransferQueueConsumeResult(StatusCode.INVALID_INDEXFILE_DETAIL, null, requestIndex, logicIndex); } - }else{ - transferQueueConsumeResult= new TransferQueueConsumeResult(StatusCode.CONSUME_NO_MESSAGE,null,requestIndex,logicIndex); + } else { + transferQueueConsumeResult = new TransferQueueConsumeResult(StatusCode.CONSUME_NO_MESSAGE, null, requestIndex, logicIndex); } - }else{ - throw new TransferQueueInvalidStatusException("transfer queue invalid status : "+transferStatus); + } else { + throw new TransferQueueInvalidStatusException("transfer queue invalid status : " + transferStatus); } - return transferQueueConsumeResult; + return transferQueueConsumeResult; } - - - - public synchronized void destory(){ - logger.info("try to destory transfer queue {} ",transferId); + public synchronized void destory() { + logger.info("try to destory transfer queue {} ", transferId); this.indexQueue.destroy(); logger.info("destroy index file"); destoryCallbacks.forEach(destoryCallback -> { - try{ + try { destoryCallback.callback(); - }catch(Exception e){ - logger.error("destory call back error",e); + } catch (Exception e) { + logger.error("destory call back error", e); } }); } @@ -220,54 +150,54 @@ public void setCreateTimestamp(long createTimestamp) { this.createTimestamp = createTimestamp; } - public synchronized void onCompeleted(){ - if(transferStatus==TransferStatus.TRANSFERING) { + public synchronized void onCompeleted() { + if (transferStatus == TransferStatus.TRANSFERING) { transferStatus = TransferStatus.FINISH; } completeCallbacks.forEach(completeCallback -> { - try{ + try { completeCallback.callback(); - }catch (Exception e){ + } catch (Exception e) { } }); } - public synchronized void onError(Throwable throwable){ - logger.error("transfer queue {} productor error",transferId,throwable); - if(transferStatus==TransferStatus.TRANSFERING) { + public synchronized void onError(Throwable throwable) { + logger.error("transfer queue {} productor error", transferId, throwable); + if (transferStatus == TransferStatus.TRANSFERING) { transferStatus = TransferStatus.ERROR; } errorCallbacks.forEach(errorCallback -> { try { errorCallback.callback(throwable); - }catch(Exception e){ - logger.error("error call back ",e); + } catch (Exception e) { + logger.error("error call back ", e); } }); } - public synchronized void registeErrorCallback(ErrorCallback errorCallback){ - if(transferStatus==TransferStatus.TRANSFERING) { + public synchronized void registeErrorCallback(ErrorCallback errorCallback) { + if (transferStatus == TransferStatus.TRANSFERING) { errorCallbacks.add(errorCallback); - }else{ - throw new TransferQueueInvalidStatusException("status is "+transferStatus); + } else { + throw new TransferQueueInvalidStatusException("status is " + transferStatus); } } - public synchronized void registeDestoryCallback(DestoryCallback destoryCallback){ - if(transferStatus==TransferStatus.TRANSFERING) + public synchronized void registeDestoryCallback(DestoryCallback destoryCallback) { + if (transferStatus == TransferStatus.TRANSFERING) destoryCallbacks.add(destoryCallback); else - throw new TransferQueueInvalidStatusException("status is "+transferStatus); + throw new TransferQueueInvalidStatusException("status is " + transferStatus); } public TransferStatus getTransferStatus() { return transferStatus; } -// public void setTransferStatus(TransferStatus transferStatus) { + // public void setTransferStatus(TransferStatus transferStatus) { // this.transferStatus = transferStatus; // } public AtomicInteger getWrotePosition() { @@ -290,12 +220,13 @@ public void setTransferId(String transferId) { this.transferId = transferId; } - public synchronized void start(){ - logger.info("transfer queue {} start ",transferId); - if(this.transferStatus==TransferStatus.INIT){ + public synchronized void start() { + logger.info("transfer queue {} start ", transferId); + if (this.transferStatus == TransferStatus.INIT) { this.transferStatus = TransferStatus.TRANSFERING; } } + public long getLastReadTimestamp() { return lastReadTimestamp; } @@ -312,15 +243,73 @@ public void setLastWriteTimestamp(long lastWriteTimestamp) { this.lastWriteTimestamp = lastWriteTimestamp; } - public TranferQueueInfo getTransferQueueInfo(){ - TranferQueueInfo transferQueueInfo = new TranferQueueInfo(); + public TranferQueueInfo getTransferQueueInfo() { + TranferQueueInfo transferQueueInfo = new TranferQueueInfo(); transferQueueInfo.setTransferId(transferId); transferQueueInfo.setCreateTimestamp(createTimestamp); transferQueueInfo.setLastReadTimestamp(lastReadTimestamp); transferQueueInfo.setLastWriteTimestamp(lastWriteTimestamp); transferQueueInfo.setTransferStatus(transferStatus); transferQueueInfo.setLogicOffset(indexQueue.getLogicOffset().get()); - return transferQueueInfo; + return transferQueueInfo; + } + + public static class TransferQueueConsumeResult { + SelectMappedBufferResult selectMappedBufferResult; + long requestIndex; + long logicIndexTotal; + String code = "-1"; + MessageExt message; + + public TransferQueueConsumeResult(String code, + SelectMappedBufferResult selectMappedBufferResult, + long requestIndex, + long logicIndex) { + this.code = code; + this.selectMappedBufferResult = selectMappedBufferResult; + this.requestIndex = requestIndex; + this.logicIndexTotal = logicIndex; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public SelectMappedBufferResult getSelectMappedBufferResult() { + return selectMappedBufferResult; + } + + public void setSelectMappedBufferResult(SelectMappedBufferResult selectMappedBufferResult) { + this.selectMappedBufferResult = selectMappedBufferResult; + } + + public long getRequestIndex() { + return requestIndex; + } + + public void setRequestIndex(long requestIndex) { + this.requestIndex = requestIndex; + } + + public long getLogicIndexTotal() { + return logicIndexTotal; + } + + public void setLogicIndexTotal(long logicIndexTotal) { + this.logicIndexTotal = logicIndexTotal; + } + + public MessageExt getMessage() { + return message; + } + + public void setMessage(MessageExt message) { + this.message = message; + } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueApplyInfo.java b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueApplyInfo.java index b8f557417f..fae284ad46 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueApplyInfo.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueApplyInfo.java @@ -3,13 +3,14 @@ import com.osx.core.utils.JsonUtil; -public class TransferQueueApplyInfo{ +public class TransferQueueApplyInfo { String transferId; String instanceId; - String ip ; + String ip; String sessionId; - long applyTimestamp; + long applyTimestamp; + public String getTransferId() { return transferId; } @@ -30,6 +31,10 @@ public String getIp() { return ip; } + public void setIp(String ip) { + this.ip = ip; + } + public String getSessionId() { return sessionId; } @@ -38,10 +43,6 @@ public void setSessionId(String sessionId) { this.sessionId = sessionId; } - public void setIp(String ip) { - this.ip = ip; - } - public long getApplyTimestamp() { return applyTimestamp; } @@ -50,7 +51,7 @@ public void setApplyTimestamp(long applyTimestamp) { this.applyTimestamp = applyTimestamp; } - public String toString(){ + public String toString() { return JsonUtil.object2Json(this); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueManager.java b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueManager.java index 694d176593..12604c0c91 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueManager.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueManager.java @@ -2,9 +2,11 @@ import com.firework.cluster.rpc.Firework; import com.firework.cluster.rpc.FireworkServiceGrpc; - - -import com.osx.core.frame.*; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import com.osx.broker.ServiceContainer; import com.osx.core.config.MasterInfo; import com.osx.core.config.MetaInfo; import com.osx.core.constant.DeployMode; @@ -12,25 +14,20 @@ import com.osx.core.constant.StatusCode; import com.osx.core.constant.TransferStatus; import com.osx.core.context.Context; +import com.osx.core.frame.GrpcConnectionFactory; +import com.osx.core.frame.ServiceThread; import com.osx.core.router.RouterInfo; import com.osx.core.utils.JsonUtil; -import com.osx.broker.ServiceContainer; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import io.grpc.ManagedChannel; import org.apache.commons.lang3.StringUtils; import org.apache.zookeeper.KeeperException; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.ppc.ptp.PrivateTransferProtocolGrpc; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.util.*; - - import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadPoolExecutor; @@ -38,47 +35,61 @@ import java.util.concurrent.locks.ReentrantLock; public class TransferQueueManager { - ThreadPoolExecutor errorCallBackExecutor = new ThreadPoolExecutor(1,2,1000, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(100)); - ThreadPoolExecutor completeCallBackExecutor = new ThreadPoolExecutor(1,2,1000, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(100)); - ThreadPoolExecutor destroyCallBackExecutor = new ThreadPoolExecutor(1,2,1000, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(100)); - Logger logger = LoggerFactory.getLogger(TransferQueueManager.class); - - volatile Map transferQueueApplyInfoMap = new ConcurrentHashMap<>(); - volatile Map masterQueueApplyInfoMap = new ConcurrentHashMap<>(); - Map clusterTransferQueueCountMap= Maps.newHashMap(); final String ZK_QUEUE_PREFIX = "/FATE-TRANSFER/QUEUE"; final String MASTER_PATH = "/FATE-TRANSFER/MASTER"; final String ZK_COMPONENTS_PREFIX = "/FATE-COMPONENTS/firework"; + ThreadPoolExecutor errorCallBackExecutor = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100)); + ThreadPoolExecutor completeCallBackExecutor = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100)); + ThreadPoolExecutor destroyCallBackExecutor = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100)); + Logger logger = LoggerFactory.getLogger(TransferQueueManager.class); + volatile Map transferQueueApplyInfoMap = new ConcurrentHashMap<>(); + volatile Map masterQueueApplyInfoMap = new ConcurrentHashMap<>(); + Map clusterTransferQueueCountMap = Maps.newHashMap(); volatile Set instanceIds = new HashSet<>(); - ConcurrentHashMap transferQueueMap= new ConcurrentHashMap<>(); - ConcurrentHashMap> sessionQueueMap = new ConcurrentHashMap<>(); + ConcurrentHashMap transferQueueMap = new ConcurrentHashMap<>(); + ConcurrentHashMap> sessionQueueMap = new ConcurrentHashMap<>(); ConcurrentHashMap transferIdLockMap = new ConcurrentHashMap(); - volatile long transferApplyInfoVersion=-1; + volatile long transferApplyInfoVersion = -1; + private ServiceThread cleanTask = new ServiceThread() { + @Override + public void run() { + while (true) { + this.waitForRunning(1000); + checkAndClean(); + } + } + + @Override + public String getServiceName() { + return "TransferQueueCleanTask"; + } + }; - public TransferQueueManager( ){ + + public TransferQueueManager() { instanceIds.add(MetaInfo.INSTANCE_ID); - if(MetaInfo.isCluster()){ - boolean pathExists = ServiceContainer.zkClient.checkExists(ZK_QUEUE_PREFIX); - if(!pathExists){ - ServiceContainer.zkClient.create(ZK_QUEUE_PREFIX,false); + if (MetaInfo.isCluster()) { + boolean pathExists = ServiceContainer.zkClient.checkExists(ZK_QUEUE_PREFIX); + if (!pathExists) { + ServiceContainer.zkClient.create(ZK_QUEUE_PREFIX, false); } - List initApplyInfo= ServiceContainer.zkClient.addChildListener(ZK_QUEUE_PREFIX, (path, children) -> { - parseApplyInfo(children); + List initApplyInfo = ServiceContainer.zkClient.addChildListener(ZK_QUEUE_PREFIX, (path, children) -> { + parseApplyInfo(children); }); parseApplyInfo(initApplyInfo); - ServiceContainer.zkClient.create(ZK_COMPONENTS_PREFIX+"/"+MetaInfo.INSTANCE_ID,true); - List initInstanceIds = ServiceContainer.zkClient.addChildListener(ZK_COMPONENTS_PREFIX,(path, children)->{ + ServiceContainer.zkClient.create(ZK_COMPONENTS_PREFIX + "/" + MetaInfo.INSTANCE_ID, true); + List initInstanceIds = ServiceContainer.zkClient.addChildListener(ZK_COMPONENTS_PREFIX, (path, children) -> { handleClusterInstanceId(children); }); - ServiceContainer.zkClient.addDataListener(MASTER_PATH,(path, data, type)->{ - logger.info("master event {} {}",type,data); - if(data!=null) { + ServiceContainer.zkClient.addDataListener(MASTER_PATH, (path, data, type) -> { + logger.info("master event {} {}", type, data); + if (data != null) { try { MetaInfo.masterInfo = parseMasterInfo((String) data); - }catch(Exception e){ - logger.info("parse master info error",e); + } catch (Exception e) { + logger.info("parse master info error", e); } - }else { + } else { electMaster(); } }); @@ -87,117 +98,117 @@ public TransferQueueManager( ){ cleanTask.start(); } - public Set getInstanceIds() { return instanceIds; } + public void setInstanceIds(Set instanceIds) { this.instanceIds = instanceIds; } + public Map getGlobalTransferQueueMap() { return transferQueueApplyInfoMap; } - public boolean isMaster(){ - return MetaInfo.INSTANCE_ID.equals(MetaInfo.masterInfo.getInstanceId()); + public boolean isMaster() { + return MetaInfo.INSTANCE_ID.equals(MetaInfo.masterInfo.getInstanceId()); } - - - private void electMaster(){ + private void electMaster() { try { - MasterInfo electMasterInfo = new MasterInfo(); + MasterInfo electMasterInfo = new MasterInfo(); electMasterInfo.setInstanceId(MetaInfo.INSTANCE_ID); logger.info("try to elect master !!!"); - ServiceContainer.zkClient.createEphemeral(MASTER_PATH,JsonUtil.object2Json(electMasterInfo)); + ServiceContainer.zkClient.createEphemeral(MASTER_PATH, JsonUtil.object2Json(electMasterInfo)); logger.info("this instance is master !!!"); - this.masterQueueApplyInfoMap = this.transferQueueApplyInfoMap; - new ServiceThread(){ + this.masterQueueApplyInfoMap = this.transferQueueApplyInfoMap; + new ServiceThread() { @Override public void run() { - while(true){ + while (true) { doMasterWork(); waitForRunning(1000); } } + @Override public String getServiceName() { return "MasterWorkThread"; } }.start(); - }catch (Exception e){ - if(e instanceof KeeperException.NodeExistsException){ + } catch (Exception e) { + if (e instanceof KeeperException.NodeExistsException) { logger.info("master is already elected"); } } } - /** * 平衡的策略暂时没有开发 + * * @param instanceId * @return */ - private String doClusterBalance(String transferId, - String instanceId, - String sessionId){ - return instanceId; + private String doClusterBalance(String transferId, + String instanceId, + String sessionId) { + return instanceId; } - - - - - private void doMasterWork(){ - long current = System.currentTimeMillis(); + private void doMasterWork() { + long current = System.currentTimeMillis(); //统计各个实例中队列数 - Map temp = Maps.newHashMap(); - transferQueueApplyInfoMap.forEach((k,v)->{ - String instanceId = v.getInstanceId(); - if(instanceIds.contains(instanceId)) { + Map temp = Maps.newHashMap(); + transferQueueApplyInfoMap.forEach((k, v) -> { + String instanceId = v.getInstanceId(); + if (instanceIds.contains(instanceId)) { Integer count = temp.get(instanceId); if (count == null) { temp.put(instanceId, 1); } else { temp.put(instanceId, count + 1); - }; + } + ; } }); this.clusterTransferQueueCountMap = temp; - masterQueueApplyInfoMap.forEach((k,v)->{ - if(current - v.getApplyTimestamp()>10000){ - if(transferQueueApplyInfoMap.get(k)==null){ - logger.info("===============master remove {}",k); + masterQueueApplyInfoMap.forEach((k, v) -> { + if (current - v.getApplyTimestamp() > 10000) { + if (transferQueueApplyInfoMap.get(k) == null) { + logger.info("===============master remove {}", k); masterQueueApplyInfoMap.remove(k); } - }; + } + ; }); - // logger.info("==========cluster count info ============{}",clusterTransferQueueCountMap); + // logger.info("==========cluster count info ============{}",clusterTransferQueueCountMap); } - private MasterInfo parseMasterInfo(String masterContent){ - MasterInfo masterInfo = JsonUtil.json2Object(masterContent,MasterInfo.class); - return masterInfo; + private MasterInfo parseMasterInfo(String masterContent) { + MasterInfo masterInfo = JsonUtil.json2Object(masterContent, MasterInfo.class); + return masterInfo; } - private void handleClusterInstanceId(List children){ + + private void handleClusterInstanceId(List children) { this.instanceIds.clear(); this.instanceIds.addAll(children); - logger.info("instance change : {}",instanceIds); + logger.info("instance change : {}", instanceIds); } - private synchronized void parseApplyInfo(List children){ + + private synchronized void parseApplyInfo(List children) { Set childSet = Sets.newHashSet(children); - Set intersecitonSet = Sets.intersection(transferQueueApplyInfoMap.keySet(),childSet); + Set intersecitonSet = Sets.intersection(transferQueueApplyInfoMap.keySet(), childSet); Set needAddSet = null; - if(intersecitonSet!=null) - needAddSet = Sets.difference(childSet,intersecitonSet); - Set needRemoveSet = Sets.difference(transferQueueApplyInfoMap.keySet(),intersecitonSet); - logger.info("cluster apply info add {} remove {}",needAddSet,needRemoveSet); - if(needRemoveSet!=null) { + if (intersecitonSet != null) + needAddSet = Sets.difference(childSet, intersecitonSet); + Set needRemoveSet = Sets.difference(transferQueueApplyInfoMap.keySet(), intersecitonSet); + logger.info("cluster apply info add {} remove {}", needAddSet, needRemoveSet); + if (needRemoveSet != null) { needRemoveSet.forEach(k -> { transferQueueApplyInfoMap.remove(k); }); } - if(needAddSet!=null) { + if (needAddSet != null) { needAddSet.forEach(k -> { try { String content = ServiceContainer.zkClient.getContent(ZK_QUEUE_PREFIX + "/" + k); @@ -211,24 +222,11 @@ private synchronized void parseApplyInfo(List children){ }); } } + ; - private ServiceThread cleanTask = new ServiceThread(){ - @Override - public void run() { - while(true) { - this.waitForRunning(1000); - checkAndClean(); - } - } - @Override - public String getServiceName() { - return "TransferQueueCleanTask"; - } - };; - - public List cleanByParam(String sessionId,String paramTransferId){ - List result= Lists.newArrayList(); - if(StringUtils.isEmpty(paramTransferId)) { + public List cleanByParam(String sessionId, String paramTransferId) { + List result = Lists.newArrayList(); + if (StringUtils.isEmpty(paramTransferId)) { Set transferIdSets = this.sessionQueueMap.get(sessionId); if (transferIdSets != null) { List transferIdList = Lists.newArrayList(transferIdSets); @@ -236,14 +234,14 @@ public List cleanByParam(String sessionId,String paramTransferId){ try { if (transferQueueMap.get(transferId) != null) destroy(transferId); - result.add(transferId); + result.add(transferId); } catch (Exception e) { logger.error("destroyInner error {}", transferId); e.printStackTrace(); } } } - }else{ + } else { try { if (transferQueueMap.get(paramTransferId) != null) { destroy(paramTransferId); @@ -257,12 +255,12 @@ public List cleanByParam(String sessionId,String paramTransferId){ return result; } - private void destroyInner(TransferQueue transferQueue){ + private void destroyInner(TransferQueue transferQueue) { transferQueue.destory(); transferQueueMap.remove(transferQueue.getTransferId()); - String sessionId = transferQueue.getSessionId(); + String sessionId = transferQueue.getSessionId(); Set transferIdSets = this.sessionQueueMap.get(sessionId); - if(transferIdSets!=null){ + if (transferIdSets != null) { transferIdSets.remove(transferQueue.getTransferId()); if (transferIdSets.size() == 0) { sessionQueueMap.remove(sessionId); @@ -270,9 +268,9 @@ private void destroyInner(TransferQueue transferQueue){ } } - private void checkAndClean(){ - long now = System.currentTimeMillis(); - transferQueueMap.forEach((transferId,transferQueue)->{ + private void checkAndClean() { + long now = System.currentTimeMillis(); + transferQueueMap.forEach((transferId, transferQueue) -> { try { long lastReadTimestamp = transferQueue.getLastReadTimestamp(); long lastWriteTimestamp = transferQueue.getLastWriteTimestamp(); @@ -286,7 +284,7 @@ private void checkAndClean(){ destroy(transferId); return; } - }catch(Exception igrone){ + } catch (Exception igrone) { igrone.printStackTrace(); } }); @@ -294,356 +292,352 @@ private void checkAndClean(){ } - - public Enumeration getAllTransferIds(){ + public Enumeration getAllTransferIds() { return transferQueueMap.keys(); } - public List getTransferQueues(List transferIds){ - List result= Lists.newArrayList(); - for(String transferId:transferIds){ - result.add(this.transferQueueMap.get(transferId)); - } - return result; + public List getTransferQueues(List transferIds) { + List result = Lists.newArrayList(); + for (String transferId : transferIds) { + result.add(this.transferQueueMap.get(transferId)); + } + return result; } + public synchronized TransferQueueApplyInfo handleClusterApply(String transferId, + String instanceId, + String sessionId) { - public synchronized TransferQueueApplyInfo handleClusterApply(String transferId, - String instanceId, - String sessionId){ - - TransferQueueApplyInfo transferQueueApplyInfo = this.masterQueueApplyInfoMap.get(transferId); - if(transferQueueApplyInfo!=null){ - return transferQueueApplyInfo; - }else{ - long current = System.currentTimeMillis(); - TransferQueueApplyInfo newTransferQueueApplyInfo = new TransferQueueApplyInfo(); - String intanceId = doClusterBalance(transferId,instanceId,sessionId); + TransferQueueApplyInfo transferQueueApplyInfo = this.masterQueueApplyInfoMap.get(transferId); + if (transferQueueApplyInfo != null) { + return transferQueueApplyInfo; + } else { + long current = System.currentTimeMillis(); + TransferQueueApplyInfo newTransferQueueApplyInfo = new TransferQueueApplyInfo(); + String intanceId = doClusterBalance(transferId, instanceId, sessionId); newTransferQueueApplyInfo.setTransferId(transferId); newTransferQueueApplyInfo.setInstanceId(instanceId); newTransferQueueApplyInfo.setSessionId(sessionId); newTransferQueueApplyInfo.setApplyTimestamp(current); - this.masterQueueApplyInfoMap.put(transferId,newTransferQueueApplyInfo); - return newTransferQueueApplyInfo; + this.masterQueueApplyInfoMap.put(transferId, newTransferQueueApplyInfo); + return newTransferQueueApplyInfo; } } - public CreateQueueResult createNewQueue( String transferId, String sessionId,boolean forceCreateLocal){ - Preconditions.checkArgument(StringUtils.isNotEmpty(transferId)); - CreateQueueResult createQueueResult = new CreateQueueResult(); - ReentrantLock transferCreateLock = transferIdLockMap.get(transferId); - if(transferCreateLock==null){ - transferIdLockMap.putIfAbsent(transferId,new ReentrantLock(false)); - } + public CreateQueueResult createNewQueue(String transferId, String sessionId, boolean forceCreateLocal) { + Preconditions.checkArgument(StringUtils.isNotEmpty(transferId)); + CreateQueueResult createQueueResult = new CreateQueueResult(); + ReentrantLock transferCreateLock = transferIdLockMap.get(transferId); + if (transferCreateLock == null) { + transferIdLockMap.putIfAbsent(transferId, new ReentrantLock(false)); + } transferCreateLock = transferIdLockMap.get(transferId); transferCreateLock.lock(); try { - boolean exist = this.transferQueueMap.get(transferId) != null ? true : false; - if(exist){ + boolean exist = this.transferQueueMap.get(transferId) != null ? true : false; + if (exist) { createQueueResult.setTransferQueue(this.transferQueueMap.get(transferId)); String[] elements = MetaInfo.INSTANCE_ID.split(":"); createQueueResult.setPort(Integer.parseInt(elements[1])); createQueueResult.setRedirectIp(elements[0]); return createQueueResult; } - if (MetaInfo.PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name())&&!forceCreateLocal) { + if (MetaInfo.PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name()) && !forceCreateLocal) { + /** + * 缓存的集群信息中能够找到,直接返回信息 + */ + if (this.transferQueueApplyInfoMap.get(transferId) != null) { + TransferQueueApplyInfo transferQueueApplyInfo = this.transferQueueApplyInfoMap.get(transferId); + if (!transferQueueApplyInfo.getInstanceId().equals(MetaInfo.INSTANCE_ID)) { + String instanceId = transferQueueApplyInfo.getInstanceId(); + String[] args = instanceId.split(":"); + String ip = args[0]; + String portString = args[1]; + createQueueResult.setPort(Integer.parseInt(portString)); + createQueueResult.setRedirectIp(ip); + return createQueueResult; + } else { /** - * 缓存的集群信息中能够找到,直接返回信息 + * 这种情况存在于本地已删除,而集群信息未同步更新,可能存在延迟,这时重走申请流程 */ - if(this.transferQueueApplyInfoMap.get(transferId)!=null){ - TransferQueueApplyInfo transferQueueApplyInfo = this.transferQueueApplyInfoMap.get(transferId); - if(!transferQueueApplyInfo.getInstanceId().equals(MetaInfo.INSTANCE_ID)){ - String instanceId = transferQueueApplyInfo.getInstanceId(); - String[] args = instanceId.split(":"); - String ip = args[0]; - String portString = args[1]; - createQueueResult.setPort(Integer.parseInt(portString)); - createQueueResult.setRedirectIp(ip); - return createQueueResult; - }else{ - /** - * 这种情况存在于本地已删除,而集群信息未同步更新,可能存在延迟,这时重走申请流程 - */ - } - }; - - + } + } + ; - Firework.ApplyTransferQueueRequest request = Firework.ApplyTransferQueueRequest.newBuilder(). - setTransferId(transferId). - setInstanceId(MetaInfo.INSTANCE_ID). - setSessionId(sessionId).build(); + Firework.ApplyTransferQueueRequest request = Firework.ApplyTransferQueueRequest.newBuilder(). + setTransferId(transferId). + setInstanceId(MetaInfo.INSTANCE_ID). + setSessionId(sessionId).build(); - Firework.ApplyTransferQueueResponse applyTransferQueueResponse = this.applyFromMaster(request); - logger.info("apply transfer queue response {}",applyTransferQueueResponse); + Firework.ApplyTransferQueueResponse applyTransferQueueResponse = this.applyFromMaster(request); + logger.info("apply transfer queue response {}", applyTransferQueueResponse); - if(applyTransferQueueResponse!=null) { + if (applyTransferQueueResponse != null) { int applyReturnCode = applyTransferQueueResponse.getCode(); - // TransferQueueApplyInfo transferQueueApplyInfo=applyTransferQueueResponse.getTransferQueueApplyInfo(); - - /** - * 从clustermananger 返回的结果中比对instantceId ,如果为本实例,则在本地建Q - */ - if(MetaInfo.INSTANCE_ID.equals(applyTransferQueueResponse.getInstanceId())){ - - String[] elements = MetaInfo.INSTANCE_ID.split(":"); - createQueueResult.setPort(Integer.parseInt(elements[1])); - createQueueResult.setRedirectIp(elements[0]); - createQueueResult.setTransferQueue( localCreate(transferId,sessionId)); - registerTransferQueue(transferId,sessionId); - //createQueueResult = applyFromCluster(transferId,sessionId); - - }else{ - String instanceId = applyTransferQueueResponse.getInstanceId(); - String[] args = instanceId.split(":"); - String ip = args[0]; - String portString = args[1]; - int grpcPort = Integer.parseInt(portString); - createQueueResult.setRedirectIp(ip); - createQueueResult.setPort(grpcPort); - }; - }else{ - throw new RuntimeException(); + // TransferQueueApplyInfo transferQueueApplyInfo=applyTransferQueueResponse.getTransferQueueApplyInfo(); + + /** + * 从clustermananger 返回的结果中比对instantceId ,如果为本实例,则在本地建Q + */ + if (MetaInfo.INSTANCE_ID.equals(applyTransferQueueResponse.getInstanceId())) { + + String[] elements = MetaInfo.INSTANCE_ID.split(":"); + createQueueResult.setPort(Integer.parseInt(elements[1])); + createQueueResult.setRedirectIp(elements[0]); + createQueueResult.setTransferQueue(localCreate(transferId, sessionId)); + registerTransferQueue(transferId, sessionId); + //createQueueResult = applyFromCluster(transferId,sessionId); + + } else { + String instanceId = applyTransferQueueResponse.getInstanceId(); + String[] args = instanceId.split(":"); + String ip = args[0]; + String portString = args[1]; + int grpcPort = Integer.parseInt(portString); + createQueueResult.setRedirectIp(ip); + createQueueResult.setPort(grpcPort); + } + ; + } else { + throw new RuntimeException(); } - }else{ + } else { /** * 单机版部署,直接本地建Q */ - createQueueResult.setTransferQueue( localCreate(transferId,sessionId)); - String[] args = MetaInfo.INSTANCE_ID.split(":"); - String ip = args[0]; - String portString = args[1]; + createQueueResult.setTransferQueue(localCreate(transferId, sessionId)); + String[] args = MetaInfo.INSTANCE_ID.split(":"); + String ip = args[0]; + String portString = args[1]; createQueueResult.setPort(Integer.parseInt(portString)); createQueueResult.setRedirectIp(ip); } - return createQueueResult; - }finally { + return createQueueResult; + } finally { transferCreateLock.unlock(); } } - private void registerTransferQueue(String transferId,String sessionId){ - StringBuffer sb = new StringBuffer(); + private void registerTransferQueue(String transferId, String sessionId) { + StringBuffer sb = new StringBuffer(); sb.append(ZK_QUEUE_PREFIX).append("/"); sb.append(transferId); - String path = sb.toString(); - TransferQueueApplyInfo transferQueueApplyInfo = new TransferQueueApplyInfo(); + String path = sb.toString(); + TransferQueueApplyInfo transferQueueApplyInfo = new TransferQueueApplyInfo(); transferQueueApplyInfo.setTransferId(transferId); transferQueueApplyInfo.setSessionId(sessionId); transferQueueApplyInfo.setInstanceId(MetaInfo.INSTANCE_ID); transferQueueApplyInfo.setApplyTimestamp(System.currentTimeMillis()); try { - ServiceContainer.zkClient.create(path,JsonUtil.object2Json(transferQueueApplyInfo),true); + ServiceContainer.zkClient.create(path, JsonUtil.object2Json(transferQueueApplyInfo), true); } catch (KeeperException.NodeExistsException e) { e.printStackTrace(); } } - private CreateQueueResult applyFromCluster(String transferId,String sessionId){ - CreateQueueResult createQueueResult = null ; + private CreateQueueResult applyFromCluster(String transferId, String sessionId) { + CreateQueueResult createQueueResult = null; - if(MetaInfo.PROPERTY_USE_ZOOKEEPER){ - createQueueResult = new CreateQueueResult(); - StringBuffer sb = new StringBuffer(); + if (MetaInfo.PROPERTY_USE_ZOOKEEPER) { + createQueueResult = new CreateQueueResult(); + StringBuffer sb = new StringBuffer(); sb.append(ZK_QUEUE_PREFIX).append("/"); sb.append(transferId); - String path = sb.toString(); - boolean exist = ServiceContainer.zkClient.checkExists(path); - if(exist){ - String content = ServiceContainer.zkClient.getContent(path); - TransferQueueApplyInfo transferQueueApplyInfo = JsonUtil.json2Object(content,TransferQueueApplyInfo.class); - } - else{ + String path = sb.toString(); + boolean exist = ServiceContainer.zkClient.checkExists(path); + if (exist) { + String content = ServiceContainer.zkClient.getContent(path); + TransferQueueApplyInfo transferQueueApplyInfo = JsonUtil.json2Object(content, TransferQueueApplyInfo.class); + } else { /** * 如何平均 */ - TransferQueueApplyInfo transferQueueApplyInfo = new TransferQueueApplyInfo(); + TransferQueueApplyInfo transferQueueApplyInfo = new TransferQueueApplyInfo(); transferQueueApplyInfo.setTransferId(transferId); transferQueueApplyInfo.setSessionId(sessionId); transferQueueApplyInfo.setInstanceId(MetaInfo.INSTANCE_ID); transferQueueApplyInfo.setApplyTimestamp(System.currentTimeMillis()); try { - ServiceContainer.zkClient.create(path,JsonUtil.object2Json(transferQueueApplyInfo),true); + ServiceContainer.zkClient.create(path, JsonUtil.object2Json(transferQueueApplyInfo), true); } catch (KeeperException.NodeExistsException e) { e.printStackTrace(); } String content = ServiceContainer.zkClient.getContent(path); - transferQueueApplyInfo = JsonUtil.json2Object(content,TransferQueueApplyInfo.class); - if(MetaInfo.INSTANCE_ID.equals(transferQueueApplyInfo.getInstanceId())){ - createQueueResult.setTransferQueue( localCreate(transferId,sessionId)); - }else{ + transferQueueApplyInfo = JsonUtil.json2Object(content, TransferQueueApplyInfo.class); + if (MetaInfo.INSTANCE_ID.equals(transferQueueApplyInfo.getInstanceId())) { + createQueueResult.setTransferQueue(localCreate(transferId, sessionId)); + } else { String[] elements = MetaInfo.INSTANCE_ID.split(":"); createQueueResult.setPort(Integer.parseInt(elements[1])); createQueueResult.setRedirectIp(elements[0]); } } } - return createQueueResult; + return createQueueResult; } - public Pcp.Outbound applyFromMaster(Context context , Pcp.Inbound inbound){ - if(!isMaster()){ - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(this.getMasterAddress()); + public Osx.Outbound applyFromMaster(Context context, Osx.Inbound inbound) { + if (!isMaster()) { + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(this.getMasterAddress(),true); PrivateTransferProtocolGrpc.PrivateTransferProtocolBlockingStub stub = PrivateTransferProtocolGrpc.newBlockingStub(managedChannel); return stub.invoke(inbound); - }else{ - - String topic = inbound.getMetadataMap().get(Pcp.Metadata.MessageTopic); - String instanceId = inbound.getMetadataMap().get(Pcp.Metadata.InstanceId); - String sessionId = inbound.getMetadataMap().get(Pcp.Header.SessionID); - TransferQueueApplyInfo transferQueueApplyInfo = this.handleClusterApply(topic, - instanceId,sessionId); - Pcp.Outbound.Builder outboundBuilder = Pcp.Outbound.newBuilder(); - outboundBuilder.getMetadataMap().put(Pcp.Metadata.MessageTopic.name(), topic); - outboundBuilder.getMetadataMap().put(Pcp.Metadata.InstanceId.name(), instanceId); - outboundBuilder.getMetadataMap().put(Pcp.Metadata.Timestamp.name(),Long.toString( transferQueueApplyInfo.getApplyTimestamp())); + } else { + + String topic = inbound.getMetadataMap().get(Osx.Metadata.MessageTopic); + String instanceId = inbound.getMetadataMap().get(Osx.Metadata.InstanceId); + String sessionId = inbound.getMetadataMap().get(Osx.Header.SessionID); + TransferQueueApplyInfo transferQueueApplyInfo = this.handleClusterApply(topic, + instanceId, sessionId); + Osx.Outbound.Builder outboundBuilder = Osx.Outbound.newBuilder(); + outboundBuilder.getMetadataMap().put(Osx.Metadata.MessageTopic.name(), topic); + outboundBuilder.getMetadataMap().put(Osx.Metadata.InstanceId.name(), instanceId); + outboundBuilder.getMetadataMap().put(Osx.Metadata.Timestamp.name(), Long.toString(transferQueueApplyInfo.getApplyTimestamp())); outboundBuilder.setCode(StatusCode.SUCCESS); outboundBuilder.setMessage(Dict.SUCCESS); - return outboundBuilder.build(); + return outboundBuilder.build(); } } - - public Firework.ApplyTransferQueueResponse applyFromMaster(Firework.ApplyTransferQueueRequest produceRequest){ - if(!isMaster()){ - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(getMasterAddress()); + public Firework.ApplyTransferQueueResponse applyFromMaster(Firework.ApplyTransferQueueRequest produceRequest) { + if (!isMaster()) { + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(getMasterAddress(),true); FireworkServiceGrpc.FireworkServiceBlockingStub stub = FireworkServiceGrpc.newBlockingStub(managedChannel); return stub.applyTransferQueue(produceRequest); - }else{ - TransferQueueApplyInfo transferQueueApplyInfo = this.handleClusterApply(produceRequest.getTransferId(), - produceRequest.getInstanceId(),produceRequest.getSessionId()); - Firework.ApplyTransferQueueResponse.Builder resultBuilder = Firework.ApplyTransferQueueResponse.newBuilder(); + } else { + TransferQueueApplyInfo transferQueueApplyInfo = this.handleClusterApply(produceRequest.getTransferId(), + produceRequest.getInstanceId(), produceRequest.getSessionId()); + Firework.ApplyTransferQueueResponse.Builder resultBuilder = Firework.ApplyTransferQueueResponse.newBuilder(); resultBuilder.setTransferId(transferQueueApplyInfo.getTransferId()); resultBuilder.setApplyTimestamp(transferQueueApplyInfo.getApplyTimestamp()); resultBuilder.setCode(0); resultBuilder.setInstanceId(transferQueueApplyInfo.getInstanceId()); - return resultBuilder.build(); + return resultBuilder.build(); } } - private RouterInfo getMasterAddress(){ + private RouterInfo getMasterAddress() { RouterInfo routerInfo = new RouterInfo(); String[] args = MetaInfo.masterInfo.getInstanceId().split(Dict.COLON); routerInfo.setHost(args[0]); routerInfo.setPort(Integer.parseInt(args[1])); - return routerInfo; + return routerInfo; } - - - private void unRegisterCluster(String transferId){ - logger.info("unRegister transferId {}",transferId); + private void unRegisterCluster(String transferId) { + logger.info("unRegister transferId {}", transferId); // ManagedChannel managedChannel = GrpcConnectionPool.getPool().getManagedChannel(MetaInfo.getClusterManagerHost(), MetaInfo.getClusterManagerPort()); // FireworkServiceGrpc.FireworkServiceBlockingStub stub = FireworkServiceGrpc.newBlockingStub(managedChannel); // Firework.UnRegisterTransferQueueRequest.Builder unRegisterTransferQueueRequestBuilder = Firework.UnRegisterTransferQueueRequest.newBuilder(); // unRegisterTransferQueueRequestBuilder.setTransferId(transferId); // stub.unRegisterTransferQueue(unRegisterTransferQueueRequestBuilder.build()); - if(MetaInfo.isCluster()){ - ServiceContainer.zkClient.delete(ZK_QUEUE_PREFIX+"/"+transferId); + if (MetaInfo.isCluster()) { + ServiceContainer.zkClient.delete(ZK_QUEUE_PREFIX + "/" + transferId); } } - private TransferQueue localCreate(String transferId, String sessionId){ - TransferQueue transferQueue = new TransferQueue(transferId, this, MetaInfo.PROPERTY_TRANSFER_FILE_PATH_PRE +File.separator+MetaInfo.INSTANCE_ID ); + private TransferQueue localCreate(String transferId, String sessionId) { + TransferQueue transferQueue = new TransferQueue(transferId, this, MetaInfo.PROPERTY_TRANSFER_FILE_PATH_PRE + File.separator + MetaInfo.INSTANCE_ID); transferQueue.setSessionId(sessionId); transferQueue.start(); - transferQueue.registeDestoryCallback(()->{ + transferQueue.registeDestoryCallback(() -> { this.transferQueueMap.remove(transferId); - if(this.sessionQueueMap.get(sessionId)!=null){ + if (this.sessionQueueMap.get(sessionId) != null) { this.sessionQueueMap.get(sessionId).remove(transferId); } }); transferQueueMap.put(transferId, transferQueue); - sessionQueueMap.putIfAbsent(sessionId,new HashSet<>()); + sessionQueueMap.putIfAbsent(sessionId, new HashSet<>()); sessionQueueMap.get(sessionId).add(transferId); return transferQueue; } - public TransferQueue getQueue(String transferId){ + public TransferQueue getQueue(String transferId) { return transferQueueMap.get(transferId); } - public Map getAllLocalQueue(){ + public Map getAllLocalQueue() { return this.transferQueueMap; } - private void destroy(String transferId){ + private void destroy(String transferId) { Preconditions.checkArgument(StringUtils.isNotEmpty(transferId)); ReentrantLock transferIdLock = this.transferIdLockMap.get(transferId); - if(transferIdLock!=null){ + if (transferIdLock != null) { transferIdLock.lock(); - }try { + } + try { TransferQueue transferQueue = getQueue(transferId); if (transferQueue != null) { - destroyInner(transferQueue); + destroyInner(transferQueue); transferIdLockMap.remove(transferId); } - }finally { - if(transferIdLock!=null){ + } finally { + if (transferIdLock != null) { transferIdLock.unlock(); } } } - - public void onError(String transferId,Throwable throwable){ - TransferQueue transferQueue = transferQueueMap.get(transferId); - if(transferQueue!=null){ + public void onError(String transferId, Throwable throwable) { + TransferQueue transferQueue = transferQueueMap.get(transferId); + if (transferQueue != null) { /** * 这里需要处理的问题是,当异常发生时,消费者并没有接入,等触发之后才接入 */ - errorCallBackExecutor.execute(()->{transferQueue.onError(throwable); + errorCallBackExecutor.execute(() -> { + transferQueue.onError(throwable); }); } this.destroy(transferId); } - public void onCompleted(String transferId){ - logger.info("transfer queue {} prepare to destory",transferId); + public void onCompleted(String transferId) { + logger.info("transfer queue {} prepare to destory", transferId); TransferQueue transferQueue = transferQueueMap.get(transferId); - if(transferQueue!=null){ + if (transferQueue != null) { transferQueue.onCompeleted(); } this.destroy(transferId); - logger.info("transfer queue {} destoryed",transferId); + logger.info("transfer queue {} destoryed", transferId); } - public TransferQueueApplyInfo queryGlobleQueue(String transferId){ - return this.transferQueueApplyInfoMap.get(transferId); + public TransferQueueApplyInfo queryGlobleQueue(String transferId) { + return this.transferQueueApplyInfoMap.get(transferId); } - public void destroyAll(){ - logger.info("prepare to destory {}",transferQueueMap); - if(MetaInfo.isCluster()){ + public void destroyAll() { + logger.info("prepare to destory {}", transferQueueMap); + if (MetaInfo.isCluster()) { try { - if(this.isMaster()){ + if (this.isMaster()) { ServiceContainer.zkClient.delete(MASTER_PATH); System.err.println("=========quit master"); } - ServiceContainer.zkClient.close();; - }catch(Exception e){ + ServiceContainer.zkClient.close(); + ; + } catch (Exception e) { e.printStackTrace(); } logger.info("unregister component over"); } - this.transferQueueMap.forEach((transferId,transferQueue)->{ + this.transferQueueMap.forEach((transferId, transferQueue) -> { transferQueue.destory(); System.err.println("kkkkkkkkkkkkkkkkk"); @@ -684,5 +678,4 @@ public void destroyAll(){ // } - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueMonitorService.java b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueMonitorService.java index e0ab9129b8..e7a2b648d4 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueMonitorService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/queue/TransferQueueMonitorService.java @@ -5,9 +5,9 @@ public class TransferQueueMonitorService extends ServiceThread { - TransferQueueManager transferQueueManager; + TransferQueueManager transferQueueManager; - public TransferQueueMonitorService(TransferQueueManager transferQueueManager){ + public TransferQueueMonitorService(TransferQueueManager transferQueueManager) { this.transferQueueManager = transferQueueManager; } @@ -20,9 +20,5 @@ public String getServiceName() { public void run() { - - - - } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/router/DefaultFateRouterServiceImpl.java b/java/osx/broker/src/main/java/com/osx/broker/router/DefaultFateRouterServiceImpl.java index a39ed3c22f..497b8805c3 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/router/DefaultFateRouterServiceImpl.java +++ b/java/osx/broker/src/main/java/com/osx/broker/router/DefaultFateRouterServiceImpl.java @@ -1,4 +1,5 @@ package com.osx.broker.router; + import com.google.common.base.Preconditions; import com.google.common.collect.Maps; import com.google.gson.JsonArray; @@ -6,10 +7,9 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.protobuf.InvalidProtocolBufferException; -import com.osx.core.context.Context; -import com.osx.core.router.RouterInfo; import com.osx.core.datasource.FileRefreshableDataSource; import com.osx.core.flow.PropertyListener; +import com.osx.core.router.RouterInfo; import com.osx.core.utils.JsonUtil; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import com.webank.eggroll.core.transfer.Transfer; @@ -24,9 +24,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class DefaultFateRouterServiceImpl implements FateRouterService { +public class DefaultFateRouterServiceImpl implements FateRouterService { - Logger logger = LoggerFactory.getLogger(DefaultFateRouterServiceImpl.class); private static final String IP = "ip"; private static final String PORT = "port"; private static final String URL = "url"; @@ -38,27 +37,26 @@ public class DefaultFateRouterServiceImpl implements FateRouterService { private static final String caFile = "caFile"; private static final String DEFAULT = "default"; private static final String VERSION = "version"; - - - Map> routerInfoMap = new ConcurrentHashMap>(); - Map>> endPointMap = new ConcurrentHashMap<>(); - FileRefreshableDataSource fileRefreshableDataSource ; + Logger logger = LoggerFactory.getLogger(DefaultFateRouterServiceImpl.class); + Map> routerInfoMap = new ConcurrentHashMap>(); + Map>> endPointMap = new ConcurrentHashMap<>(); + FileRefreshableDataSource fileRefreshableDataSource; @Override - public RouterInfo route( Proxy.Packet packet) { - Preconditions.checkArgument(packet!=null); - // logger.info("====================== {}",packet); - RouterInfo routerInfo = null; - Proxy.Metadata metadata = packet.getHeader(); + public RouterInfo route(Proxy.Packet packet) { + Preconditions.checkArgument(packet != null); + // logger.info("====================== {}",packet); + RouterInfo routerInfo = null; + Proxy.Metadata metadata = packet.getHeader(); Transfer.RollSiteHeader rollSiteHeader = null; try { - rollSiteHeader = Transfer.RollSiteHeader.parseFrom(metadata.getExt()); + rollSiteHeader = Transfer.RollSiteHeader.parseFrom(metadata.getExt()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } - String dstPartyId = rollSiteHeader.getDstPartyId(); + String dstPartyId = rollSiteHeader.getDstPartyId(); - if(StringUtils.isEmpty(dstPartyId)){ + if (StringUtils.isEmpty(dstPartyId)) { dstPartyId = metadata.getDst().getPartyId(); } @@ -66,44 +64,47 @@ public RouterInfo route( Proxy.Packet packet) { String desRole = metadata.getDst().getRole(); String srcRole = metadata.getSrc().getRole(); String srcPartyId = metadata.getSrc().getPartyId(); - routerInfo = this.route(srcPartyId,srcRole,dstPartyId,desRole); - logger.info("query router info {} to {} {} return {}",srcPartyId,dstPartyId,desRole,routerInfo); - return routerInfo; + routerInfo = this.route(srcPartyId, srcRole, dstPartyId, desRole); + logger.info("query router info {} to {} {} return {}", srcPartyId, dstPartyId, desRole, routerInfo); + return routerInfo; } + public RouterInfo route(String srcPartyId, String srcRole, String dstPartyId, String desRole) { + RouterInfo routerInfo = null; + Map> partyIdMap = this.endPointMap.get(dstPartyId); + if (partyIdMap != null) { - public RouterInfo route(String srcPartyId,String srcRole,String dstPartyId,String desRole){ - RouterInfo routerInfo= null; - Map> partyIdMap = this.endPointMap.get(dstPartyId); - if(partyIdMap!=null){ - - if(partyIdMap.get(desRole)!=null){ - List ips = partyIdMap.getOrDefault(desRole,null); - if(ips!=null&&ips.size()>0){ - Map endpoint = ips.get((int)(System.currentTimeMillis()%ips.size())); + if (StringUtils.isNotEmpty(desRole)&&partyIdMap.get(desRole) != null) { + List ips = partyIdMap.getOrDefault(desRole, null); + if (ips != null && ips.size() > 0) { + Map endpoint = ips.get((int) (System.currentTimeMillis() % ips.size())); routerInfo = new RouterInfo(); routerInfo.setHost(endpoint.get(IP).toString()); - routerInfo.setPort(((Number)endpoint.get(PORT)).intValue()); + routerInfo.setPort(((Number) endpoint.get(PORT)).intValue()); routerInfo.setDesPartyId(dstPartyId); routerInfo.setSourcePartyId(srcPartyId); - routerInfo.setVersion(endpoint.get(VERSION)!=null?endpoint.get(VERSION).toString():null); + routerInfo.setVersion(endpoint.get(VERSION) != null ? endpoint.get(VERSION).toString() : null); } - }else { + } else { + List ips = partyIdMap.getOrDefault(DEFAULT, null); if (ips != null && ips.size() > 0) { Map endpoint = ips.get((int) (System.currentTimeMillis() % ips.size())); routerInfo = new RouterInfo(); routerInfo.setHost(endpoint.get(IP).toString()); - routerInfo.setPort(((Number)endpoint.get(PORT)).intValue()); + routerInfo.setPort(((Number) endpoint.get(PORT)).intValue()); routerInfo.setDesPartyId(dstPartyId); routerInfo.setSourcePartyId(srcPartyId); - routerInfo.setVersion(endpoint.get(VERSION)!=null?endpoint.get(VERSION).toString():null); + routerInfo.setVersion(endpoint.get(VERSION) != null ? endpoint.get(VERSION).toString() : null); + } + if(StringUtils.isNotEmpty(desRole)){ + logger.warn("role {} is not found,return default router info ",desRole); } } } - // logger.info("query router info {} return {}",dstPartyId,routerInfo); - return routerInfo; + // logger.info("query router info {} return {}",dstPartyId,routerInfo); + return routerInfo; } // @Override @@ -117,27 +118,28 @@ public RouterInfo route(String srcPartyId,String srcRole,String dstPartyId,Stri // } - - Map>> initRouteTable(JsonObject confJson) { + Map>> initRouteTable(Map confJson) { // BasicMeta.Endpoint.Builder endpointBuilder = BasicMeta.Endpoint.newBuilder(); Map>> newRouteTable = new ConcurrentHashMap<>(); // loop through coordinator - for (Map.Entry coordinatorEntry : confJson.entrySet()) { - String coordinatorKey = coordinatorEntry.getKey(); - JsonObject coordinatorValue = coordinatorEntry.getValue().getAsJsonObject(); - // logger.info("coordinatorKey {} : {}",coordinatorKey,coordinatorValue); + + confJson.forEach((k,v)->{ + String coordinatorKey = k.toString(); + Map coordinatorValue = (Map)v; + Map> serviceTable = newRouteTable.get(coordinatorKey); if (serviceTable == null) { serviceTable = new ConcurrentHashMap<>(4); newRouteTable.put(coordinatorKey, serviceTable); } // loop through role in coordinator - for (Map.Entry roleEntry : coordinatorValue.entrySet()) { - String roleKey = roleEntry.getKey(); - if(roleKey.equals("createTime")||roleKey.equals("updateTime")){ + for (Object roleEntryObject : coordinatorValue.entrySet()) { + Map.Entry roleEntry = (Map.Entry)roleEntryObject; + String roleKey = roleEntry.getKey().toString(); + if (roleKey.equals("createTime") || roleKey.equals("updateTime")) { continue; } - JsonArray roleValue = roleEntry.getValue().getAsJsonArray(); + List roleValue = (List)roleEntry.getValue(); List endpoints = serviceTable.get(roleKey); if (endpoints == null) { @@ -146,107 +148,109 @@ Map>> initRouteTable(JsonObject confJson) { } // loop through endpoints - for (JsonElement endpointElement : roleValue) { + for (Object endpointElement : roleValue) { - Map element = Maps.newHashMap(); + Map element = Maps.newHashMap(); - JsonObject endpointJson = endpointElement.getAsJsonObject(); + Map endpointJson = (Map)endpointElement; - if (endpointJson.has(IP)) { - String targetIp = endpointJson.get(IP).getAsString(); - element.put(IP,targetIp); + if (endpointJson.get(IP)!=null) { + String targetIp = endpointJson.get(IP).toString(); + element.put(IP, targetIp); } - if (endpointJson.has(PORT)) { - int targetPort = endpointJson.get(PORT).getAsInt(); - element.put(PORT,targetPort); + if (endpointJson.get(PORT)!=null) { + int targetPort = Integer.parseInt(endpointJson.get(PORT).toString()); + element.put(PORT, targetPort); } // if(endpointJson.has(URL)){ // String url = endpointJson.get(URL).getAsString(); // endpointBuilder.setUrl(url); // } - if (endpointJson.has(USE_SSL)) { - boolean targetUseSSL = endpointJson.get(USE_SSL).getAsBoolean(); - element.put(USE_SSL,targetUseSSL); + if (endpointJson.get(USE_SSL)!=null) { + boolean targetUseSSL = Boolean.getBoolean(endpointJson.get(USE_SSL).toString()); + element.put(USE_SSL, targetUseSSL); } - if (endpointJson.has(HOSTNAME)) { - String targetHostname = endpointJson.get(HOSTNAME).getAsString(); - element.put(HOSTNAME,targetHostname); + if (endpointJson.get(HOSTNAME)!=null) { + String targetHostname = endpointJson.get(HOSTNAME).toString(); + element.put(HOSTNAME, targetHostname); } - if (endpointJson.has(negotiationType)) { - String targetNegotiationType = endpointJson.get(negotiationType).getAsString(); - element.put(negotiationType,targetNegotiationType); + if (endpointJson.get(negotiationType)!=null) { + String targetNegotiationType = endpointJson.get(negotiationType).toString(); + element.put(negotiationType, targetNegotiationType); } - if (endpointJson.has(certChainFile)) { - String targetCertChainFile = endpointJson.get(certChainFile).getAsString(); - element.put(certChainFile,targetCertChainFile); + if (endpointJson.get(certChainFile)!=null) { + String targetCertChainFile = endpointJson.get(certChainFile).toString(); + element.put(certChainFile, targetCertChainFile); } - if (endpointJson.has(privateKeyFile)) { - String targetPrivateKeyFile = endpointJson.get(privateKeyFile).getAsString(); - element.put(privateKeyFile,targetPrivateKeyFile); + if (endpointJson.get(privateKeyFile)!=null) { + String targetPrivateKeyFile = endpointJson.get(privateKeyFile).toString(); + element.put(privateKeyFile, targetPrivateKeyFile); } - if (endpointJson.has(caFile)) { - String targetCaFile = endpointJson.get(caFile).getAsString(); - element.put(caFile,targetCaFile); + if (endpointJson.get(caFile)!=null) { + String targetCaFile = endpointJson.get(caFile).toString(); + element.put(caFile, targetCaFile); } - if(endpointJson.has(VERSION)){ - String targetVersion = endpointJson.get(VERSION).getAsString(); - element.put(VERSION,targetVersion); + if (endpointJson.get(VERSION)!=null) { + String targetVersion = endpointJson.get(VERSION).toString(); + element.put(VERSION, targetVersion); } //BasicMeta.Endpoint endpoint = endpointBuilder.build(); endpoints.add(element); } } - } + + }); + return newRouteTable; } + public void start() { + String currentPath = Thread.currentThread().getContextClassLoader().getResource("route_table.json").getPath(); + logger.info("load router file {}", currentPath); + File confFile = new File(currentPath); + FileRefreshableDataSource fileRefreshableDataSource = null; + try { + fileRefreshableDataSource = new FileRefreshableDataSource(confFile, (source) -> { + logger.info("read route_table {}", source); + return source; + }); + fileRefreshableDataSource.getProperty().addListener(new RouterTableListener()); + } catch (FileNotFoundException e) { + logger.error("router file {} is not found", currentPath); + } + } - - private class RouterTableListener implements PropertyListener { + private class RouterTableListener implements PropertyListener { @Override public void configUpdate(String value) { - // logger.info("fire router table update {}",value); - JsonObject confJson = JsonParser.parseString(value).getAsJsonObject(); - JsonObject content = confJson.get("route_table").getAsJsonObject(); + // logger.info("fire router table update {}",value); + Map confJson = JsonUtil.json2Object(value,Map.class); + // JsonObject confJson = JsonParser.parseString(value).getAsJsonObject(); + Map content =(Map) confJson.get("route_table"); endPointMap = initRouteTable(content); } @Override public void configLoad(String value) { - // logger.info("fire router table load {}",value); - JsonObject confJson = JsonParser.parseString(value).getAsJsonObject(); - JsonObject content = confJson.get("route_table").getAsJsonObject(); + + // logger.info("fire router table load {}",value); + Map confJson = JsonUtil.json2Object(value,Map.class); + // JsonObject confJson = JsonParser.parseString(value).getAsJsonObject(); + Map content =(Map) confJson.get("route_table"); endPointMap = initRouteTable(content); logger.info("load router config {}", JsonUtil.formatJson(JsonUtil.object2Json(endPointMap))); } } - public void start( ) { - String currentPath = Thread.currentThread().getContextClassLoader().getResource("route_table.json").getPath(); - logger.info("load router file {}",currentPath); - File confFile = new File(currentPath); - FileRefreshableDataSource fileRefreshableDataSource = null; - try { - fileRefreshableDataSource = new FileRefreshableDataSource(confFile,(source)->{ - logger.info("read route_table {}",source); - return source; - }); - fileRefreshableDataSource.getProperty().addListener(new RouterTableListener()); - - } catch (FileNotFoundException e) { - logger.error("router file {} is not found",currentPath); - } - } - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/router/FateRouterService.java b/java/osx/broker/src/main/java/com/osx/broker/router/FateRouterService.java index 542e2074f2..bcd867738b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/router/FateRouterService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/router/FateRouterService.java @@ -1,20 +1,17 @@ package com.osx.broker.router; - -import com.osx.core.context.Context; import com.osx.core.router.RouterInfo; import com.webank.ai.eggroll.api.networking.proxy.Proxy; - public interface FateRouterService { - RouterInfo route(String srcPartyId,String srcRole,String dstPartyId,String desRole); + RouterInfo route(String srcPartyId, String srcRole, String dstPartyId, String desRole); - RouterInfo route( Proxy.Packet packet); + RouterInfo route(Proxy.Packet packet); - //RouterInfo route(FireworkTransfer.RouteInfo routeInfo); + //RouterInfo route(FireworkTransfer.RouteInfo routeInfo); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/router/RemoteRouterDataSource.java b/java/osx/broker/src/main/java/com/osx/broker/router/RemoteRouterDataSource.java index 63754dfdb5..acec39c49f 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/router/RemoteRouterDataSource.java +++ b/java/osx/broker/src/main/java/com/osx/broker/router/RemoteRouterDataSource.java @@ -4,7 +4,7 @@ import com.osx.core.datasource.AutoRefreshDataSource; import com.osx.core.datasource.Converter; -public class RemoteRouterDataSource extends AutoRefreshDataSource { +public class RemoteRouterDataSource extends AutoRefreshDataSource { public RemoteRouterDataSource(Converter configParser) { super(configParser); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/router/RouterMetric.java b/java/osx/broker/src/main/java/com/osx/broker/router/RouterMetric.java index 4d44c7d9c6..90cbcef190 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/router/RouterMetric.java +++ b/java/osx/broker/src/main/java/com/osx/broker/router/RouterMetric.java @@ -1,17 +1,24 @@ package com.osx.broker.router; - - - - import com.osx.core.utils.JsonUtil; import java.util.concurrent.atomic.AtomicLong; public class RouterMetric { - long lastCheckTimestamp; + long lastCheckTimestamp; + AtomicLong sourceReceiveBytesCount = new AtomicLong(0); + AtomicLong sourceSendBytesCount = new AtomicLong(0); + AtomicLong sinkReceiveBytesCount = new AtomicLong(0); + AtomicLong sinkSendBytesCount = new AtomicLong(0); + long lastUpstreamBytesCount; + long lastDownstreamBytesCount; + + public static void main(String[] args) { + RouterMetric routerMetric = new RouterMetric(); + System.err.println("xxxxxxxxxxxxxxxxxxxxx" + routerMetric); + } public long getLastCheckTimestamp() { return lastCheckTimestamp; @@ -69,44 +76,24 @@ public void setLastDownstreamBytesCount(long lastDownstreamBytesCount) { this.lastDownstreamBytesCount = lastDownstreamBytesCount; } - AtomicLong sourceReceiveBytesCount=new AtomicLong(0); - - AtomicLong sourceSendBytesCount = new AtomicLong(0); - - AtomicLong sinkReceiveBytesCount = new AtomicLong(0); - - AtomicLong sinkSendBytesCount = new AtomicLong(0); - - long lastUpstreamBytesCount; - - long lastDownstreamBytesCount; - - public long addSourceReceive(int size){ + public long addSourceReceive(int size) { return sourceReceiveBytesCount.addAndGet(size); } - public long addSourceSend(int size){ - return sourceSendBytesCount.addAndGet(size); + public long addSourceSend(int size) { + return sourceSendBytesCount.addAndGet(size); } - - public long addSinkReceive(int size){ + public long addSinkReceive(int size) { return sinkReceiveBytesCount.addAndGet(size); } - public long addSinkSend(int size){ - return sinkSendBytesCount.addAndGet(size); + public long addSinkSend(int size) { + return sinkSendBytesCount.addAndGet(size); } - - - public String toString(){ + public String toString() { return JsonUtil.object2Json(this); } - public static void main(String[] args){ - RouterMetric routerMetric = new RouterMetric(); - System.err.println("xxxxxxxxxxxxxxxxxxxxx"+routerMetric); - } - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/server/OsxServer.java b/java/osx/broker/src/main/java/com/osx/broker/server/OsxServer.java index 56d3b946a1..b9f6ff2834 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/server/OsxServer.java +++ b/java/osx/broker/src/main/java/com/osx/broker/server/OsxServer.java @@ -1,42 +1,124 @@ package com.osx.broker.server; -import com.osx.core.config.MetaInfo; import com.osx.broker.ServiceContainer; import com.osx.broker.grpc.ContextPrepareInterceptor; import com.osx.broker.grpc.ServiceExceptionHandler; -import io.grpc.Server; +import com.osx.broker.http.DispatchServlet; +import com.osx.core.config.MetaInfo; + import io.grpc.ServerBuilder; import io.grpc.ServerInterceptors; import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; +import org.eclipse.jetty.server.*; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -public class OsxServer { - Logger logger = LoggerFactory.getLogger(OsxServer.class); - Server server; - Server tlsServer; +/** + * http1.X + grpc + */ +public class OsxServer { - private void init() { + + Logger logger = LoggerFactory.getLogger(OsxServer.class); + io.grpc.Server server; + io.grpc.Server tlsServer; + org.eclipse.jetty.server.Server httpServer; + + private void init() { server = buildServer(); - // tlsServer = buildTlsServer(); + httpServer = buildHttpServer(); + + // tlsServer = buildTlsServer(); } + public Server buildHttpServer(){ + Server server = new Server(); + try { + // The number of acceptor threads. + int acceptors = 1; +// The number of selectors. + int selectors = 1; +// Create a ServerConnector instance. + ServerConnector connector = new ServerConnector(server, acceptors, selectors, new HttpConnectionFactory()); + +// Configure TCP/IP parameters. + +// The port to listen to. + logger.info("http server try to start listen port {}", MetaInfo.PROPERTY_HTTP_PORT); + connector.setPort(MetaInfo.PROPERTY_HTTP_PORT); +// The address to bind to. + connector.setHost("127.0.0.1"); +// The TCP accept queue size. + connector.setAcceptQueueSize(128); + server.addConnector(connector); + server.setHandler(buildServlet()); + + + + return server; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + ServletContextHandler buildServlet(){ + // Create a ServletContextHandler with contextPath. + ServletContextHandler context = new ServletContextHandler(); + context.setContextPath(MetaInfo.PROPERTY_HTTP_CONTEXT_PATH); +// Add the Servlet implementing the cart functionality to the context. + ServletHolder servletHolder = context.addServlet(DispatchServlet.class, MetaInfo.PROPERTY_HTTP_SERVLET_PATH);//"/*" +// Configure the Servlet with init-parameters. + // servletHolder.setInitParameter("maxItems", "128"); + +// Add the CrossOriginFilter to protect from CSRF attacks. + // FilterHolder filterHolder = context.addFilter(CrossOriginFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); +// Configure the filter. + // filterHolder.setAsyncSupported(true); + + return context; + } + +// Handler buildHandler(){ +// HandlerList handlerList = new HandlerList(); +//// Create a ContextHandler with contextPath. +// ServletContextHandler servletContextHandler = this.buildServlet(); +// ContextHandler contextHandler = new ContextHandler(); +// contextHandler.setContextPath("/shop"); +// // contextHandler.setHandler(new ShopHandler()); +// handlerList.addHandler(contextHandler); +// return handlerList; +// } + public boolean start() { init(); try { - logger.info("try to start listen port {}",MetaInfo.PROPERTY_PORT); + logger.info("grpc server try to start, listen port {}", MetaInfo.PROPERTY_PORT); server.start(); - if(tlsServer!=null){ + if(httpServer!=null){ + logger.info("http server try to start, listen port {}", MetaInfo.PROPERTY_HTTP_PORT); + httpServer.start(); + + } + if (tlsServer != null) { tlsServer.start(); } - logger.info("listen port {} success",MetaInfo.PROPERTY_PORT); - } catch (Exception e) { - if(e.getCause() instanceof java.net.BindException){ - logger.error("port {} already in use, please try to choose another one !!!!",MetaInfo.PROPERTY_PORT); + logger.info("listen http port {} success", MetaInfo.PROPERTY_HTTP_PORT); + logger.info("listen grpc port {} success", MetaInfo.PROPERTY_PORT); + } catch (Exception e) { + if (e.getCause() instanceof java.net.BindException) { + logger.error("port {} already in use, please try to choose another one !!!!", MetaInfo.PROPERTY_PORT); } return false; } @@ -79,38 +161,44 @@ public boolean start() { // } - private Server buildServer( ){ + private io.grpc.Server buildServer() { NettyServerBuilder nettyServerBuilder = (NettyServerBuilder) ServerBuilder.forPort(MetaInfo.PROPERTY_PORT); - nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.proxyGrpcService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); - nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.pcpGrpcService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); + nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.proxyGrpcService, new ServiceExceptionHandler(), new ContextPrepareInterceptor())); + nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.pcpGrpcService, new ServiceExceptionHandler(), new ContextPrepareInterceptor())); //nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.commonService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); - //nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.clusterService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); + //nettyServerBuilder.addService(ServerInterceptors.intercept(ServiceContainer.clusterService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); // nettyServerBuilder.addService(ServerInterceptors.intercept(dLedgerServer,new ServiceExceptionHandler(), new ContextPrepareInterceptor() )); nettyServerBuilder .executor(Executors.newCachedThreadPool()) - .maxConcurrentCallsPerConnection(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION) - .maxInboundMessageSize(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE) - .maxInboundMetadataSize(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE) - .flowControlWindow(MetaInfo.PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW); + .maxConcurrentCallsPerConnection(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONCURRENT_CALL_PER_CONNECTION) + .maxInboundMessageSize(MetaInfo.PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE) + .maxInboundMetadataSize(MetaInfo.PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE) + .flowControlWindow(MetaInfo.PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW); // PERMIT_KEEPALIVE_TIME //,默认 5 minutes // PERMIT_KEEPALIVE_WITHOUT_CALLS //,默认 false - // nettyServerBuilder.withChildOption(c) - if (MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC > 0) nettyServerBuilder.keepAliveTime(MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC > 0) nettyServerBuilder.keepAliveTimeout(MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC > 0) { + // nettyServerBuilder.withChildOption(c) + if (MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC > 0) + nettyServerBuilder.keepAliveTime(MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC > 0) + nettyServerBuilder.keepAliveTimeout(MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC > 0) { - nettyServerBuilder.permitKeepAliveTime(MetaInfo.PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); + nettyServerBuilder.permitKeepAliveTime(MetaInfo.PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); } - if (MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED) nettyServerBuilder.permitKeepAliveWithoutCalls(MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC > 0) nettyServerBuilder.maxConnectionIdle(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC > 0) nettyServerBuilder.maxConnectionAge(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC > 0) nettyServerBuilder.maxConnectionAgeGrace(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC, TimeUnit.SECONDS); - return nettyServerBuilder.build(); + if (MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED) + nettyServerBuilder.permitKeepAliveWithoutCalls(MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED); + if (MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC > 0) + nettyServerBuilder.maxConnectionIdle(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC > 0) + nettyServerBuilder.maxConnectionAge(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC > 0) + nettyServerBuilder.maxConnectionAgeGrace(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC, TimeUnit.SECONDS); + return nettyServerBuilder.build(); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/service/PushService2.java b/java/osx/broker/src/main/java/com/osx/broker/service/PushService2.java index abcfd5b97e..b15e8be31a 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/service/PushService2.java +++ b/java/osx/broker/src/main/java/com/osx/broker/service/PushService2.java @@ -1,12 +1,11 @@ package com.osx.broker.service; + +import com.osx.broker.grpc.PushRequestDataWrap; +import com.osx.broker.grpc.QueuePushReqStreamObserver; import com.osx.core.context.Context; import com.osx.core.exceptions.ExceptionInfo; import com.osx.core.service.AbstractServiceAdaptor; import com.osx.core.service.InboundPackage; -import com.osx.broker.grpc.PushRequestDataWrap; -//import com.osx.transfer.grpc.QueuePushReqStreamObserver; -//import com.osx.transfer.grpc.QueuePushReqStreamObserver; -import com.osx.broker.grpc.QueuePushReqStreamObserver; import io.grpc.stub.StreamObserver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,26 +13,25 @@ public class PushService2 extends AbstractServiceAdaptor { - Logger logger = LoggerFactory.getLogger(PushService2.class); @Override protected StreamObserver doService(Context context, InboundPackage data - ) { + ) { PushRequestDataWrap pushRequestDataWrap = data.getBody(); - StreamObserver backRespSO = pushRequestDataWrap.getStreamObserver(); + StreamObserver backRespSO = pushRequestDataWrap.getStreamObserver(); context.setNeedPrintFlowLog(false); QueuePushReqStreamObserver queuePushReqStreamObserver = new QueuePushReqStreamObserver(context, backRespSO); - return queuePushReqStreamObserver; + return queuePushReqStreamObserver; } @Override protected StreamObserver transformExceptionInfo(Context context, ExceptionInfo exceptionInfo) { - logger.error("PushService error {}",exceptionInfo); + logger.error("PushService error {}", exceptionInfo); throw new RuntimeException("xxxxx"); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/service/RegisterService.java b/java/osx/broker/src/main/java/com/osx/broker/service/RegisterService.java index 3325d63c0a..6d05c0fe30 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/service/RegisterService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/service/RegisterService.java @@ -6,17 +6,17 @@ public class RegisterService { - public RegisterService(ZkConfig zkConfig){ + CuratorZookeeperClient curatorZookeeperClient; + + public RegisterService(ZkConfig zkConfig) { curatorZookeeperClient = new CuratorZookeeperClient(zkConfig); } - CuratorZookeeperClient curatorZookeeperClient ; - - - public void registerTransferQueue(){ + public void registerTransferQueue() { } - public void registerComponent(){ + + public void registerComponent() { } diff --git a/java/osx/broker/src/main/java/com/osx/broker/service/TokenApplyService.java b/java/osx/broker/src/main/java/com/osx/broker/service/TokenApplyService.java index 0f72afb353..ff779b3ce2 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/service/TokenApplyService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/service/TokenApplyService.java @@ -1,19 +1,19 @@ package com.osx.broker.service; + import com.firework.cluster.rpc.Firework; import com.firework.cluster.rpc.FireworkServiceGrpc; - -import com.osx.core.frame.Lifecycle; +import com.osx.broker.ServiceContainer; import com.osx.core.config.MetaInfo; import com.osx.core.constant.StreamLimitMode; import com.osx.core.context.Context; import com.osx.core.flow.FlowRule; +import com.osx.core.frame.Lifecycle; import com.osx.core.token.TokenResult; import com.osx.core.token.TokenResultStatus; import com.osx.core.utils.JsonUtil; -import com.osx.broker.ServiceContainer; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; @@ -21,17 +21,15 @@ public class TokenApplyService implements Lifecycle { - static Logger logger = LoggerFactory.getLogger(TokenApplyService.class); + static Logger logger = LoggerFactory.getLogger(TokenApplyService.class); + ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + private FireworkServiceGrpc.FireworkServiceBlockingStub blockingStub; - public TokenApplyService(){ + public TokenApplyService() { } - ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); - - private FireworkServiceGrpc.FireworkServiceBlockingStub blockingStub ; - - public FireworkServiceGrpc.FireworkServiceBlockingStub buildBlockingStub(String address){ + public FireworkServiceGrpc.FireworkServiceBlockingStub buildBlockingStub(String address) { // String[] ipports= address.split(":"); // ManagedChannel channel = GrpcConnectionFactory.getManagedChannel(ipports[0],Integer.parseInt(ipports[1])); // FireworkServiceGrpc.FireworkServiceBlockingStub blockingStub = FireworkServiceGrpc.newBlockingStub(channel); @@ -39,16 +37,15 @@ public FireworkServiceGrpc.FireworkServiceBlockingStub buildBlockingStub(String return null; } - public void applyToken(Context context , String resource, int count) { + public void applyToken(Context context, String resource, int count) { - if(MetaInfo.PROPERTY_STREAM_LIMIT_MODE.equals(StreamLimitMode.LOCAL.name())|| MetaInfo.PROPERTY_STREAM_LIMIT_MODE.equals(StreamLimitMode.CLUSTER.name())) - { - TokenResult localTokenResult =tryLocalLimit(resource,count); - logger.info("request token {} count {} result {}",resource,count,localTokenResult); + if (MetaInfo.PROPERTY_STREAM_LIMIT_MODE.equals(StreamLimitMode.LOCAL.name()) || MetaInfo.PROPERTY_STREAM_LIMIT_MODE.equals(StreamLimitMode.CLUSTER.name())) { + TokenResult localTokenResult = tryLocalLimit(resource, count); + logger.info("request token {} count {} result {}", resource, count, localTokenResult); /** * 集群限流 */ - if(MetaInfo.PROPERTY_STREAM_LIMIT_MODE.equals(StreamLimitMode.CLUSTER.name())) { + if (MetaInfo.PROPERTY_STREAM_LIMIT_MODE.equals(StreamLimitMode.CLUSTER.name())) { /** * 先尝试本地限流,当本地返回通过时再尝试全局限流 */ @@ -58,9 +55,9 @@ public void applyToken(Context context , String resource, int count) { tryClusterLimit(resource, count); } // flowCounterManager.pass(resource, count); - // }else{ - //// logger.info("service {} resource {} has no flow rule",context.getServiceName(),resource); - // } + // }else{ + //// logger.info("service {} resource {} has no flow rule",context.getServiceName(),resource); + // } } ServiceContainer.flowCounterManager.pass(resource, count); @@ -68,12 +65,12 @@ public void applyToken(Context context , String resource, int count) { } - private TokenResult tryLocalLimit(String resource,int count){ + private TokenResult tryLocalLimit(String resource, int count) { boolean needLoop = false; int tryTime = 0; - TokenResult tokenResult ; + TokenResult tokenResult; do { - tokenResult = ServiceContainer.defaultTokenService.requestToken(resource,count,true); + tokenResult = ServiceContainer.defaultTokenService.requestToken(resource, count, true); if (tokenResult != null) { ++tryTime; //logger.info("prepare to apply token {} {} result {}", resource, count,tokenResult); @@ -96,7 +93,7 @@ private TokenResult tryLocalLimit(String resource,int count){ case TokenResultStatus.BLOCKED: try { sleepMs = tokenResult.getWaitInMs(); - if(sleepMs>0) { + if (sleepMs > 0) { Thread.sleep(sleepMs); } logger.info("should block {} ms try time {}", sleepMs, tryTime); @@ -113,15 +110,15 @@ private TokenResult tryLocalLimit(String resource,int count){ } } while (needLoop && tryTime < MetaInfo.PROPERTY_STREAM_LIMIT_MAX_TRY_TIME); - return tokenResult; + return tokenResult; } - private void tryClusterLimit(String resource ,int count){ + private void tryClusterLimit(String resource, int count) { Firework.TokenRequest.Builder tokenRequestBuilder = Firework.TokenRequest.newBuilder(); tokenRequestBuilder.setCount(count); tokenRequestBuilder.setResource(resource); - if(blockingStub==null){ + if (blockingStub == null) { blockingStub = buildBlockingStub(MetaInfo.masterInfo.getInstanceId()); } boolean needLoop = false; @@ -151,7 +148,7 @@ private void tryClusterLimit(String resource ,int count){ case TokenResultStatus.BLOCKED: try { sleepMs = tokenResult.getWaitInMs(); - if(sleepMs>0) { + if (sleepMs > 0) { Thread.sleep(sleepMs); logger.info("should block {} ms try time {}", sleepMs, tryTime); } @@ -170,18 +167,17 @@ private void tryClusterLimit(String resource ,int count){ } - - private Map parseFlowRule(String content){ - Map temp = JsonUtil.json2Object(content,Map.class); - Map result = new HashMap(); - temp.forEach((k,v)->{ + private Map parseFlowRule(String content) { + Map temp = JsonUtil.json2Object(content, Map.class); + Map result = new HashMap(); + temp.forEach((k, v) -> { try { result.put(k, JsonUtil.json2Object(JsonUtil.object2Json(v), FlowRule.class)); - }catch (Exception e){ - logger.error("parse flowRule error",e); + } catch (Exception e) { + logger.error("parse flowRule error", e); } }); - return result; + return result; } @Override diff --git a/java/osx/broker/src/main/java/com/osx/broker/service/UnaryCallService.java b/java/osx/broker/src/main/java/com/osx/broker/service/UnaryCallService.java index 6a5c42608c..3726ae04a1 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/service/UnaryCallService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/service/UnaryCallService.java @@ -1,12 +1,12 @@ package com.osx.broker.service; +import com.osx.core.constant.ActionType; +import com.osx.core.context.Context; import com.osx.core.exceptions.ExceptionInfo; import com.osx.core.frame.GrpcConnectionFactory; -import com.osx.core.context.Context; import com.osx.core.service.AbstractServiceAdaptor; import com.osx.core.service.InboundPackage; - import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.Deadline; @@ -17,22 +17,22 @@ /** * 用于兼容旧版FATE */ -public class UnaryCallService extends AbstractServiceAdaptor { +public class UnaryCallService extends AbstractServiceAdaptor { - Logger logger = LoggerFactory.getLogger(UnaryCallService.class); + Logger logger = LoggerFactory.getLogger(UnaryCallService.class); - public UnaryCallService( ){ + public UnaryCallService() { } @Override protected Proxy.Packet doService(Context context, InboundPackage data) { - context.setActionType("unary-call"); - Proxy.Packet req = (Proxy.Packet)data.getBody(); - Proxy.Packet resp = unaryCall( context , req); - logger.info("uncary req {} resp {}",req,resp); - return resp; + context.setActionType(ActionType.UNARY_CALL.getAlias()); + Proxy.Packet req = (Proxy.Packet) data.getBody(); + Proxy.Packet resp = unaryCall(context, req); + //logger.info("uncary req {} resp {}", req, resp); + return resp; } @@ -42,14 +42,15 @@ protected Proxy.Packet transformExceptionInfo(Context context, ExceptionInfo exc /** * 非流式传输 + * * @param context * @param */ - public Proxy.Packet unaryCall(Context context , Proxy.Packet req){ + public Proxy.Packet unaryCall(Context context, Proxy.Packet req) { Deadline endDeadline = null; boolean isPolling = false; - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo()); + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(context.getRouterInfo(),true); DataTransferServiceGrpc.DataTransferServiceBlockingStub stub = DataTransferServiceGrpc.newBlockingStub(managedChannel); Proxy.Packet result = null; result = stub.unaryCall(req); @@ -57,5 +58,4 @@ public Proxy.Packet unaryCall(Context context , Proxy.Packet req){ } - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/store/IndexQueue.java b/java/osx/broker/src/main/java/com/osx/broker/store/IndexQueue.java index 6316cf7080..eefb3aca6b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/store/IndexQueue.java +++ b/java/osx/broker/src/main/java/com/osx/broker/store/IndexQueue.java @@ -1,25 +1,20 @@ - package com.osx.broker.store; -import java.io.File; -import java.nio.ByteBuffer; -import java.util.concurrent.atomic.AtomicLong; - import com.osx.broker.message.SelectMappedBufferResult; import com.osx.broker.queue.MappedFile; import com.osx.broker.queue.MappedFileQueue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.nio.ByteBuffer; +import java.util.concurrent.atomic.AtomicLong; -public class IndexQueue { - private static final Logger log = LoggerFactory.getLogger(IndexQueue.class); +public class IndexQueue { public static final int CQ_STORE_UNIT_SIZE = 12; + private static final Logger log = LoggerFactory.getLogger(IndexQueue.class); //private static final InternalLogger LOG_ERROR = InternalLoggerFactory.getLogger(LoggerName.STORE_ERROR_LOGGER_NAME); - - - private final MappedFileQueue mappedFileQueue; private final String transferId; @@ -29,32 +24,29 @@ public class IndexQueue { private final int mappedFileSize; private long maxPhysicOffset = -1; private volatile long minLogicOffset = 0; - - public AtomicLong getLogicOffset() { - return logicOffset; - } - - public void setLogicOffset(AtomicLong logicOffset) { - this.logicOffset = logicOffset; - } - - private AtomicLong logicOffset = new AtomicLong(0); - - + private AtomicLong logicOffset = new AtomicLong(0); public IndexQueue( - final String transferId, - final String storePath, - final int mappedFileSize) { + final String transferId, + final String storePath, + final int mappedFileSize) { this.storePath = storePath; this.mappedFileSize = mappedFileSize; this.transferId = transferId; String queueDir = this.storePath - + File.separator + transferId; + + File.separator + transferId; this.mappedFileQueue = new MappedFileQueue(queueDir, mappedFileSize, null); this.byteBufferIndex = ByteBuffer.allocate(CQ_STORE_UNIT_SIZE); } + public AtomicLong getLogicOffset() { + return logicOffset; + } + + public void setLogicOffset(AtomicLong logicOffset) { + this.logicOffset = logicOffset; + } + public boolean load() { boolean result = this.mappedFileQueue.load(); return result; @@ -135,15 +127,15 @@ public long getMinOffsetInQueue() { return this.minLogicOffset / CQ_STORE_UNIT_SIZE; } - public long putMessagePositionInfoWrapper( long offset ,int msgSize) { + public long putMessagePositionInfoWrapper(long offset, int msgSize) { final int maxRetries = 30; - for (int i = 0; i < maxRetries ; i++) { + for (int i = 0; i < maxRetries; i++) { boolean result = this.putMessagePositionInfo(offset, - msgSize, this.logicOffset.get()+1); + msgSize, this.logicOffset.get() + 1); - if(result){ + if (result) { return logicOffset.addAndGet(1); } @@ -153,7 +145,7 @@ public long putMessagePositionInfoWrapper( long offset ,int msgSize) { } private boolean putMessagePositionInfo(final long offset, final int size, - final long cqOffset) { + final long cqOffset) { if (offset + size <= this.maxPhysicOffset) { log.warn("Maybe try to build consume queue repeatedly maxPhysicOffset={} phyOffset={}", maxPhysicOffset, offset); @@ -177,14 +169,14 @@ private boolean putMessagePositionInfo(final long offset, final int size, this.mappedFileQueue.setCommittedWhere(expectLogicOffset); this.fillPreBlank(mappedFile, expectLogicOffset); log.info("fill pre blank space " + mappedFile.getFileName() + " " + expectLogicOffset + " " - + mappedFile.getWrotePosition()); + + mappedFile.getWrotePosition()); } if (cqOffset != 0) { long currentLogicOffset = mappedFile.getWrotePosition() + mappedFile.getFileFromOffset(); if (expectLogicOffset < currentLogicOffset) { - return true; + return true; } if (expectLogicOffset != currentLogicOffset) { @@ -218,14 +210,11 @@ public SelectMappedBufferResult getIndexBuffer(final long startIndex) { return result; } } - log.info("start index {} {} return null",startIndex,logicOffset); + log.info("start index {} {} return null", startIndex, logicOffset); return null; } - - - public long getMinLogicOffset() { return minLogicOffset; } @@ -241,9 +230,6 @@ public long rollNextFile(final long index) { } - - - public long getMaxPhysicOffset() { return maxPhysicOffset; } @@ -253,7 +239,7 @@ public void setMaxPhysicOffset(long maxPhysicOffset) { } public void destroy() { - + this.maxPhysicOffset = -1; this.minLogicOffset = 0; this.mappedFileQueue.destroy(); @@ -274,6 +260,4 @@ public void checkSelf() { } - - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/store/MessageStore.java b/java/osx/broker/src/main/java/com/osx/broker/store/MessageStore.java index b5044b19f3..850ca90dab 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/store/MessageStore.java +++ b/java/osx/broker/src/main/java/com/osx/broker/store/MessageStore.java @@ -1,12 +1,12 @@ package com.osx.broker.store; +import com.osx.broker.message.*; +import com.osx.broker.queue.*; import com.osx.core.config.MetaInfo; -import com.osx.core.frame.ServiceThread; import com.osx.core.constant.TransferStatus; import com.osx.core.exceptions.MappedFileException; import com.osx.core.exceptions.TransferQueueInvalidStatusException; -import com.osx.broker.message.*; -import com.osx.broker.queue.*; +import com.osx.core.frame.ServiceThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,53 +16,26 @@ import static com.osx.core.config.MetaInfo.MAP_FILE_SIZE; public class MessageStore { - + + protected final AtomicInteger wrotePosition = new AtomicInteger(0); Logger logger = LoggerFactory.getLogger(MessageStore.class); volatile TransferStatus transferStatus = TransferStatus.INIT; long createTimestamp; long lastStatusChangeTimestamp; long lastWriteTimestamp; long lastReadTimestamp; - - protected final AtomicInteger wrotePosition = new AtomicInteger(0); TransferQueueManager transferQueueManager; MappedFileQueue mappedFileQueue; ReentrantLock putMessageLock = new ReentrantLock(); long beginTimeInLock; AppendMessageHandler appendMessageCallback = new DefaultAppendMessageHandler(MAP_FILE_SIZE); - AllocateMappedFileService allocateMappedFileService ; - - CleanMappedFileThread cleanMappedFileThread = new CleanMappedFileThread(); - - /** - * 定期扫描过期文件 - */ - private class CleanMappedFileThread extends ServiceThread { - - @Override - public String getServiceName() { - return "CleanMappedFileThread"; - } - - @Override - public void run() { - while(true) { - this.waitForRunning(3600000); - try{ - int count = mappedFileQueue.deleteExpiredFileByTime(MetaInfo.PROPERTY_MAPPED_FILE_EXPIRE_TIME,1000,1000,false); - logger.info("CleanMappedFileThread clean expired mapped file ,count {}",count); - }catch(Exception e){ - logger.error("CleanMappedFileThread clean error",e); - } - } - } - } + AllocateMappedFileService allocateMappedFileService; + CleanMappedFileThread cleanMappedFileThread = new CleanMappedFileThread(); - - public MessageStore(AllocateMappedFileService allocateMappedFileService, String path){ + public MessageStore(AllocateMappedFileService allocateMappedFileService, String path) { allocateMappedFileService = this.allocateMappedFileService; - mappedFileQueue = new MappedFileQueue(path,MAP_FILE_SIZE,allocateMappedFileService); + mappedFileQueue = new MappedFileQueue(path, MAP_FILE_SIZE, allocateMappedFileService); this.createTimestamp = System.currentTimeMillis(); this.lastStatusChangeTimestamp = this.createTimestamp; this.lastWriteTimestamp = this.createTimestamp; @@ -70,13 +43,13 @@ public MessageStore(AllocateMappedFileService allocateMappedFileService, String } public PutMessageResult putMessage(final MessageExtBrokerInner msg) { - if(transferStatus== TransferStatus.TRANSFERING) { + if (transferStatus == TransferStatus.TRANSFERING) { long timestamp = System.currentTimeMillis(); msg.setStoreTimestamp(timestamp); if (logger.isTraceEnabled()) { logger.trace("put message {}", msg); } - lastWriteTimestamp=timestamp; + lastWriteTimestamp = timestamp; AppendMessageResult result = null; String topic = msg.getTopic(); int queueId = msg.getQueueId(); @@ -141,37 +114,37 @@ public PutMessageResult putMessage(final MessageExtBrokerInner msg) { wrotePosition.addAndGet(result.getWroteBytes()); PutMessageResult putMessageResult = new PutMessageResult(PutMessageStatus.PUT_OK, result); return putMessageResult; - }else{ - throw new TransferQueueInvalidStatusException( "invalid queue status : "+transferStatus); + } else { + throw new TransferQueueInvalidStatusException("invalid queue status : " + transferStatus); } } - public SelectMappedBufferResult consumeOneMessage(long offset) { - if(transferStatus==TransferStatus.TRANSFERING) { + public SelectMappedBufferResult consumeOneMessage(long offset) { + if (transferStatus == TransferStatus.TRANSFERING) { Message result = null; SelectMappedBufferResult selectMappedBufferResult = this.selectOneMessageByOffset(offset); return selectMappedBufferResult; - }else{ - throw new TransferQueueInvalidStatusException("transfer queue invalid status : "+transferStatus); + } else { + throw new TransferQueueInvalidStatusException("transfer queue invalid status : " + transferStatus); } } public SelectMappedBufferResult getMessage(final long offset, final int size) { - if(transferStatus==TransferStatus.TRANSFERING) { + if (transferStatus == TransferStatus.TRANSFERING) { this.lastReadTimestamp = System.currentTimeMillis(); - if(this.mappedFileQueue!=null) { + if (this.mappedFileQueue != null) { MappedFile mappedFile = this.mappedFileQueue.findMappedFileByOffset(offset, offset == 0); if (mappedFile != null) { int pos = (int) (offset % MAP_FILE_SIZE); return mappedFile.selectMappedBuffer(pos, size); } return null; - }else{ + } else { throw new MappedFileException(); } - }else { - throw new TransferQueueInvalidStatusException("transfer queue invalid status : "+transferStatus); + } else { + throw new TransferQueueInvalidStatusException("transfer queue invalid status : " + transferStatus); } } @@ -190,8 +163,6 @@ public SelectMappedBufferResult selectOneMessageByOffset(long commitLogOffset) { return null; } - - public long getCreateTimestamp() { return createTimestamp; } @@ -200,8 +171,6 @@ public void setCreateTimestamp(long createTimestamp) { this.createTimestamp = createTimestamp; } - - public TransferStatus getTransferStatus() { return transferStatus; } @@ -213,12 +182,12 @@ public AtomicInteger getWrotePosition() { return wrotePosition; } - - public synchronized void start(){ - if(this.transferStatus==TransferStatus.INIT){ + public synchronized void start() { + if (this.transferStatus == TransferStatus.INIT) { this.transferStatus = TransferStatus.TRANSFERING; } } + public long getLastReadTimestamp() { return lastReadTimestamp; } @@ -234,4 +203,28 @@ public long getLastWriteTimestamp() { public void setLastWriteTimestamp(long lastWriteTimestamp) { this.lastWriteTimestamp = lastWriteTimestamp; } + + /** + * 定期扫描过期文件 + */ + private class CleanMappedFileThread extends ServiceThread { + + @Override + public String getServiceName() { + return "CleanMappedFileThread"; + } + + @Override + public void run() { + while (true) { + this.waitForRunning(3600000); + try { + int count = mappedFileQueue.deleteExpiredFileByTime(MetaInfo.PROPERTY_MAPPED_FILE_EXPIRE_TIME, 1000, 1000, false); + logger.info("CleanMappedFileThread clean expired mapped file ,count {}", count); + } catch (Exception e) { + logger.error("CleanMappedFileThread clean error", e); + } + } + } + } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/token/DefaultTokenService.java b/java/osx/broker/src/main/java/com/osx/broker/token/DefaultTokenService.java index aa1f6dcfc7..8cbf093ba2 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/token/DefaultTokenService.java +++ b/java/osx/broker/src/main/java/com/osx/broker/token/DefaultTokenService.java @@ -1,10 +1,8 @@ package com.osx.broker.token; import com.firework.cluster.rpc.Firework; - - -import com.osx.core.constant.Dict; import com.osx.core.config.MetaInfo; +import com.osx.core.constant.Dict; import com.osx.core.context.Context; import com.osx.core.exceptions.ExceptionInfo; import com.osx.core.flow.*; @@ -19,24 +17,22 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; -public class DefaultTokenService extends AbstractServiceAdaptor implements TokenService { +public class DefaultTokenService extends AbstractServiceAdaptor implements TokenService { Logger logger = LoggerFactory.getLogger(DefaultTokenService.class); + ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); + MetricReport metricReport = new FileMetricReport(Dict.SERVICE_FIREWORK_CLUSTERMANAGER); - public DefaultTokenService(){ + public DefaultTokenService() { } - ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); - - MetricReport metricReport = new FileMetricReport(Dict.SERVICE_FIREWORK_CLUSTERMANAGER); - @Override protected Firework.TokenResponse doService(Context context, InboundPackage data) { Firework.TokenRequest tokenRequest = data.getBody(); - TokenResult tokenResult = this.requestToken(tokenRequest.getResource(),tokenRequest.getCount(),true); + TokenResult tokenResult = this.requestToken(tokenRequest.getResource(), tokenRequest.getCount(), true); // logger.info("resource {} require {} result {}",tokenRequest.getResource(),tokenRequest.getCount(),tokenResult); - Firework.TokenResponse.Builder tokenResponseBuilder = Firework.TokenResponse.newBuilder(); + Firework.TokenResponse.Builder tokenResponseBuilder = Firework.TokenResponse.newBuilder(); tokenResponseBuilder.setStatus(tokenResult.getStatus()); tokenResponseBuilder.setWaitInMs(tokenResult.getWaitInMs()); return tokenResponseBuilder.build(); @@ -57,16 +53,16 @@ protected Firework.TokenResponse transformExceptionInfo(Context context, Excepti @Override public TokenResult requestToken(String resource, int acquireCount, boolean prioritized) { - if(StringUtils.isEmpty(resource)){ + if (StringUtils.isEmpty(resource)) { return badRequest(); } FlowRule rule = ClusterFlowRuleManager.getFlowRuleByResource(resource); if (rule == null) { - logger.error("resource {} no rule",resource); + logger.error("resource {} no rule", resource); ClusterMetric clusterMetric = ClusterMetricStatistics.getMetric(resource); - if(clusterMetric==null){ - ClusterMetricStatistics.putMetricIfAbsent(resource,new ClusterMetric(MetaInfo.PROPERTY_SAMPLE_COUNT,MetaInfo.PROPERTY_INTERVAL_MS)); - clusterMetric =ClusterMetricStatistics.getMetric(resource); + if (clusterMetric == null) { + ClusterMetricStatistics.putMetricIfAbsent(resource, new ClusterMetric(MetaInfo.PROPERTY_SAMPLE_COUNT, MetaInfo.PROPERTY_INTERVAL_MS)); + clusterMetric = ClusterMetricStatistics.getMetric(resource); } clusterMetric.add(ClusterFlowEvent.PASS, acquireCount); clusterMetric.add(ClusterFlowEvent.PASS_REQUEST, 1); @@ -84,7 +80,7 @@ public void releaseConcurrentToken(Long tokenId) { if (tokenId == null) { return; } - // ConcurrentClusterFlowChecker.releaseConcurrentToken(tokenId); + // ConcurrentClusterFlowChecker.releaseConcurrentToken(tokenId); } private boolean notValidRequest(Long id, int count) { diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/DateUtils.java b/java/osx/broker/src/main/java/com/osx/broker/util/DateUtils.java index 359d6fe34a..918c661595 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/DateUtils.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/DateUtils.java @@ -1,482 +1,494 @@ package com.osx.broker.util; +import org.apache.commons.lang3.time.DateFormatUtils; + import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; -import org.apache.commons.lang3.time.DateFormatUtils; public class DateUtils { - - /** - * 仅显示年月日,例如 2015-08-11. - */ - public static final String DATE_FORMAT = "yyyy-MM-dd"; - /** - * 显示年月日时分秒,例如 2015-08-11 09:51:53. - */ - public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; - - /** - * 仅显示时分秒,例如 09:51:53. - */ - public static final String TIME_FORMAT = "HH:mm:ss"; - - /** - * 每天的毫秒数 8640000. - */ - public static final long MILLISECONDS_PER_DAY = 86400000L; - - /** - * 每周的天数. - */ - public static final long DAYS_PER_WEEK = 7L; - - /** - * 每小时毫秒数. - */ - public static final long MILLISECONDS_PER_HOUR = 3600000L; - - /** - * 每分钟秒数. - */ - public static final long SECONDS_PER_MINUTE = 60L; - - /** - * 每小时秒数. - */ - public static final long SECONDS_PER_HOUR = 3600L; - - /** - * 每天秒数. - */ - public static final long SECONDS_PER_DAY = 86400L; - - /** - * 每个月秒数,默认每月30天. - */ - public static final long SECONDS_PER_MONTH = 2592000L; - - /** - * 每年秒数,默认每年365天. - */ - public static final long SECONDS_PER_YEAR = 31536000L; - - /** - * 常用的时间格式. - */ - private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", - "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }; - - /** - * 得到当前日期字符串. - * @return String 日期字符串,例如2015-08-11 - * @since 1.0 - */ - public static String getDate() { - return getDate(DateUtils.DATE_FORMAT); - } - - /** - * 得到当前时间字符串. - * @return String 时间字符串,例如 09:51:53 - * @since 1.0 - */ - public static String getTime() { - return formatDate(new Date(), DateUtils.TIME_FORMAT); - } - - /** - * 得到当前日期和时间字符串. - * @return String 日期和时间字符串,例如 2015-08-11 09:51:53 - * @since 1.0 - */ - public static String getDateTime() { - return formatDate(new Date(), DateUtils.DATETIME_FORMAT); - } - - /** - * 获取当前时间指定格式下的字符串. - * @param pattern - * 转化后时间展示的格式,例如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等 - * @return String 格式转换之后的时间字符串. - * @since 1.0 - */ - public static String getDate(String pattern) { - return DateFormatUtils.format(new Date(), pattern); - } - - /** - * 获取指定日期的字符串格式. - * @param date 需要格式化的时间,不能为空 - * @param pattern 时间格式,例如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等 - * @return String 格式转换之后的时间字符串. - * @since 1.0 - */ - public static String getDate(Date date, String pattern) { - return DateFormatUtils.format(date, pattern); - } - - /** - * 获取日期时间字符串,默认格式为(yyyy-MM-dd). - * @param date 需要转化的日期时间 - * @param pattern 时间格式,例如"yyyy-MM-dd" "HH:mm:ss" "E"等 - * @return String 格式转换后的时间字符串 - * @since 1.0 - */ - public static String formatDate(Date date, Object... pattern) { - String formatDate = null; - if (pattern != null && pattern.length > 0) { - formatDate = DateFormatUtils.format(date, pattern[0].toString()); - } else { - formatDate = DateFormatUtils.format(date, DateUtils.DATE_FORMAT); - } - return formatDate; - } - - /** - * 获取当前年份字符串. - * @return String 当前年份字符串,例如 2015 - * @since 1.0 - */ - public static String getYear() { - return formatDate(new Date(), "yyyy"); - } - - /** - * 获取当前月份字符串. - * @return String 当前月份字符串,例如 08 - * @since 1.0 - */ - public static String getMonth() { - return formatDate(new Date(), "MM"); - } - - /** - * 获取当前天数字符串. - * @return String 当前天数字符串,例如 11 - * @since 1.0 - */ - public static String getDay() { - return formatDate(new Date(), "dd"); - } - - /** - * 获取当前星期字符串. - * @return String 当前星期字符串,例如星期二 - * @since 1.0 - */ - public static String getWeek() { - return formatDate(new Date(), "E"); - } - - /** - * 将日期型字符串转换为日期格式. - * 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", - * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" - * @param str - * @return Date - * @since 1.0 - */ - public static Date parseDate(Object str) { - if (str == null) { - return null; - } - try { - return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns); - } catch (ParseException e) { - return null; - } - } - - /** - * 获取当前日期与指定日期相隔的天数. - * @param date 给定的日期 - * @return long 日期间隔天数,正数表示给定日期在当前日期之前,负数表示在当前日期之后 - * @since 1.0 - */ - public static long pastDays(Date date) { - // 将指定日期转换为yyyy-MM-dd格式 - date = DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.DATE_FORMAT)); - // 当前日期转换为yyyy-MM-dd格式 - Date currentDate = DateUtils.parseDate(DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT)); - long t=0; - if(date!=null&¤tDate!=null){ - t = (currentDate.getTime() - date.getTime()) / DateUtils.MILLISECONDS_PER_DAY; - } - return t; - } - - /** - * 获取当前日期指定天数之后的日期. - * @param num 相隔天数 - * @return Date 日期 - * @since 1.0 - */ - public static Date nextDay(int num) { - Calendar curr = Calendar.getInstance(); - curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) + num); - return curr.getTime(); - } - - /** - * 获取当前日期指定月数之后的日期. - * @param num 间隔月数 - * @return Date 日期 - * @since 1.0 - */ - public static Date nextMonth(int num) { - Calendar curr = Calendar.getInstance(); - curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + num); - return curr.getTime(); - } - - /** - * 获取当前日期指定年数之后的日期. - * @param num 间隔年数 - * @return Date 日期 - * @since 1.0 - */ - public static Date nextYear(int num) { - Calendar curr = Calendar.getInstance(); - curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + num); - return curr.getTime(); - } - - /** - * 将 Date 日期转化为 Calendar 类型日期. - * @param date 给定的时间,若为null,则默认为当前时间 - * @return Calendar Calendar对象 - * @since 1.0 - */ - public static Calendar getCalendar(Date date) { - Calendar calendar = Calendar.getInstance(); - // calendar.setFirstDayOfWeek(Calendar.SUNDAY);//每周从周日开始 - // calendar.setMinimalDaysInFirstWeek(1); // 设置每周最少为1天 - if (date != null) { - calendar.setTime(date); - } - return calendar; - } - - /** - * 计算两个日期之间相差天数. - * @param start 计算开始日期 - * @param end 计算结束日期 - * @return long 相隔天数 - * @since 1.0 - */ - public static long getDaysBetween(Date start, Date end) { - // 将指定日期转换为yyyy-MM-dd格式 - start = DateUtils.parseDate(DateUtils.formatDate(start, DateUtils.DATE_FORMAT)); - // 当前日期转换为yyyy-MM-dd格式 - end = DateUtils.parseDate(DateUtils.formatDate(end, DateUtils.DATE_FORMAT)); - - long diff=0; - if(start!=null&&end!=null) { - diff = (end.getTime() - start.getTime()) / DateUtils.MILLISECONDS_PER_DAY; - } - return diff; - } - - /** - * 计算两个日期之前相隔多少周. - * @param start 计算开始时间 - * @param end 计算结束时间 - * @return long 相隔周数,向下取整 - * @since 1.0 - */ - public static long getWeeksBetween(Date start, Date end) { - return getDaysBetween(start, end) / DateUtils.DAYS_PER_WEEK; - } - - /** - * 获取与指定日期间隔给定天数的日期. - * @param specifiedDay 给定的字符串格式日期,支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", - * "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", - * "yyyy/MM/dd HH:mm" - * @param num 间隔天数 - * @return String 间隔指定天数之后的日期 - * @since 1.0 - */ - public static String getSpecifiedDayAfter(String specifiedDay, int num) { - Date specifiedDate = parseDate(specifiedDay); - Calendar c = Calendar.getInstance(); - c.setTime(specifiedDate); - int day = c.get(Calendar.DATE); - c.set(Calendar.DATE, day + num); - String dayAfter = formatDate(c.getTime(), DateUtils.DATE_FORMAT); - return dayAfter; - } - - /** - * 计算两个日期之前间隔的小时数. - * - * @param date1 - * 结束时间 - * @param date2 - * 开始时间 - * @return String 相差的小时数,保留一位小数 - * @since 1.0 - */ - public static String dateMinus(Date date1, Date date2) { - if (date1 == null || date2 == null) { - return "0"; - } - Long r = date1.getTime() - date2.getTime(); - DecimalFormat df = new DecimalFormat("#.0"); - double result = r * 1.0 / DateUtils.MILLISECONDS_PER_HOUR; - return df.format(result); - } - - /** - * 获取当前季度 . - * - * @return Integer 当前季度数 - * @since 1.0 - */ - public static Integer getCurrentSeason() { - Calendar calendar = Calendar.getInstance(); - Integer month = calendar.get(Calendar.MONTH) + 1; - int season = 0; - if (month >= 1 && month <= 3) { - season = 1; - } else if (month >= 4 && month <= 6) { - season = 2; - } else if (month >= 7 && month <= 9) { - season = 3; - } else if (month >= 10 && month <= 12) { - season = 4; - } - return season; - } - - /** - * 将以秒为单位的时间转换为其他单位. - * - * @param seconds - * 秒数 - * @return String 例如 16分钟前、2小时前、3天前、4月前、5年前等 - * @since 1.0 - */ - public static String getIntervalBySeconds(long seconds) { - StringBuffer buffer = new StringBuffer(); - if (seconds < SECONDS_PER_MINUTE) { - buffer.append(seconds).append("秒前"); - } else if (seconds < SECONDS_PER_HOUR) { - buffer.append(seconds / SECONDS_PER_MINUTE).append("分钟前"); - } else if (seconds < SECONDS_PER_DAY) { - buffer.append(seconds / SECONDS_PER_HOUR).append("小时前"); - } else if (seconds < SECONDS_PER_MONTH) { - buffer.append(seconds / SECONDS_PER_DAY).append("天前"); - } else if (seconds < SECONDS_PER_YEAR) { - buffer.append(seconds / SECONDS_PER_MONTH).append("月前"); - } else { - buffer.append(seconds / DateUtils.SECONDS_PER_YEAR).append("年前"); - } - return buffer.toString(); - } - - /** - * - * getNowTimeBefore(记录时间相当于目前多久之前) - * - * @param seconds - * 秒 - * @return - * @exception @since - * 1.0 - * @author rlliu - */ - public static String getNowTimeBefore(long seconds) { - StringBuffer buffer = new StringBuffer(); - buffer.append("上传于"); - if (seconds < 3600) { - buffer.append((long) Math.floor(seconds / 60.0)).append("分钟前"); - } else if (seconds < 86400) { - buffer.append((long) Math.floor(seconds / 3600.0)).append("小时前"); - } else if (seconds < 604800) { - buffer.append((long) Math.floor(seconds / 86400.0)).append("天前"); - } else if (seconds < 2592000) { - buffer.append((long) Math.floor(seconds / 604800.0)).append("周前"); - } else if (seconds < 31104000) { - buffer.append((long) Math.floor(seconds / 2592000.0)).append("月前"); - } else { - buffer.append((long) Math.floor(seconds / 31104000.0)).append("年前"); - } - return buffer.toString(); - } - - /** - * - * getMonthsBetween(查询两个日期相隔的月份) - * - * @param startDate 开始日期1 (格式yyyy-MM-dd) - * @param endDate 截止日期2 (格式yyyy-MM-dd) - * @return - */ - public static int getMonthsBetween(String startDate, String endDate) { - Calendar c1 = Calendar.getInstance(); - Calendar c2 = Calendar.getInstance(); - c1.setTime(DateUtils.parseDate(startDate)); - c2.setTime(DateUtils.parseDate(endDate)); - int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); - int month = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH); - return Math.abs(year * 12 + month); - } - - /** - * - * getDayOfWeek(获取当前日期是星期几) - * - * @param dateStr 日期 - * @return 星期几 - */ - public static String getDayOfWeek(String dateStr) { - String[] weekOfDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; - Date date = parseDate(dateStr); - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - int num = calendar.get(Calendar.DAY_OF_WEEK) - 1; - return weekOfDays[num]; - } - - /** - * sns 格式 如几秒前,几分钟前,几小时前,几天前,几个月前,几年后, ... 精细,类如某个明星几秒钟之前发表了一篇微博 - * - * @param createTime - * @return - */ - public static String snsFormat(long createTime) { - long now = System.currentTimeMillis() / 1000; - long differ = now - createTime / 1000; - String dateStr = ""; - if (differ <= 60) { - dateStr = "刚刚"; - } else if (differ <= 3600) { - dateStr = (differ / 60) + "分钟前"; - } else if (differ <= 3600 * 24) { - dateStr = (differ / 3600) + "小时前"; - } else if (differ <= 3600 * 24 * 30) { - dateStr = (differ / (3600 * 24)) + "天前"; - } else { - Date date = new Date(createTime); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - dateStr = sdf.format(date); - } - return dateStr; - } - + + /** + * 仅显示年月日,例如 2015-08-11. + */ + public static final String DATE_FORMAT = "yyyy-MM-dd"; + /** + * 显示年月日时分秒,例如 2015-08-11 09:51:53. + */ + public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; + + /** + * 仅显示时分秒,例如 09:51:53. + */ + public static final String TIME_FORMAT = "HH:mm:ss"; + + /** + * 每天的毫秒数 8640000. + */ + public static final long MILLISECONDS_PER_DAY = 86400000L; + + /** + * 每周的天数. + */ + public static final long DAYS_PER_WEEK = 7L; + + /** + * 每小时毫秒数. + */ + public static final long MILLISECONDS_PER_HOUR = 3600000L; + + /** + * 每分钟秒数. + */ + public static final long SECONDS_PER_MINUTE = 60L; + + /** + * 每小时秒数. + */ + public static final long SECONDS_PER_HOUR = 3600L; + + /** + * 每天秒数. + */ + public static final long SECONDS_PER_DAY = 86400L; + + /** + * 每个月秒数,默认每月30天. + */ + public static final long SECONDS_PER_MONTH = 2592000L; + + /** + * 每年秒数,默认每年365天. + */ + public static final long SECONDS_PER_YEAR = 31536000L; + + /** + * 常用的时间格式. + */ + private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", + "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"}; + + /** + * 得到当前日期字符串. + * + * @return String 日期字符串,例如2015-08-11 + * @since 1.0 + */ + public static String getDate() { + return getDate(DateUtils.DATE_FORMAT); + } + + /** + * 得到当前时间字符串. + * + * @return String 时间字符串,例如 09:51:53 + * @since 1.0 + */ + public static String getTime() { + return formatDate(new Date(), DateUtils.TIME_FORMAT); + } + + /** + * 得到当前日期和时间字符串. + * + * @return String 日期和时间字符串,例如 2015-08-11 09:51:53 + * @since 1.0 + */ + public static String getDateTime() { + return formatDate(new Date(), DateUtils.DATETIME_FORMAT); + } + + /** + * 获取当前时间指定格式下的字符串. + * + * @param pattern 转化后时间展示的格式,例如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等 + * @return String 格式转换之后的时间字符串. + * @since 1.0 + */ + public static String getDate(String pattern) { + return DateFormatUtils.format(new Date(), pattern); + } + + /** + * 获取指定日期的字符串格式. + * + * @param date 需要格式化的时间,不能为空 + * @param pattern 时间格式,例如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等 + * @return String 格式转换之后的时间字符串. + * @since 1.0 + */ + public static String getDate(Date date, String pattern) { + return DateFormatUtils.format(date, pattern); + } + + /** + * 获取日期时间字符串,默认格式为(yyyy-MM-dd). + * + * @param date 需要转化的日期时间 + * @param pattern 时间格式,例如"yyyy-MM-dd" "HH:mm:ss" "E"等 + * @return String 格式转换后的时间字符串 + * @since 1.0 + */ + public static String formatDate(Date date, Object... pattern) { + String formatDate = null; + if (pattern != null && pattern.length > 0) { + formatDate = DateFormatUtils.format(date, pattern[0].toString()); + } else { + formatDate = DateFormatUtils.format(date, DateUtils.DATE_FORMAT); + } + return formatDate; + } + + /** + * 获取当前年份字符串. + * + * @return String 当前年份字符串,例如 2015 + * @since 1.0 + */ + public static String getYear() { + return formatDate(new Date(), "yyyy"); + } + + /** + * 获取当前月份字符串. + * + * @return String 当前月份字符串,例如 08 + * @since 1.0 + */ + public static String getMonth() { + return formatDate(new Date(), "MM"); + } + + /** + * 获取当前天数字符串. + * + * @return String 当前天数字符串,例如 11 + * @since 1.0 + */ + public static String getDay() { + return formatDate(new Date(), "dd"); + } + + /** + * 获取当前星期字符串. + * + * @return String 当前星期字符串,例如星期二 + * @since 1.0 + */ + public static String getWeek() { + return formatDate(new Date(), "E"); + } + + /** + * 将日期型字符串转换为日期格式. + * 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", + * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" + * + * @param str + * @return Date + * @since 1.0 + */ + public static Date parseDate(Object str) { + if (str == null) { + return null; + } + try { + return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns); + } catch (ParseException e) { + return null; + } + } + + /** + * 获取当前日期与指定日期相隔的天数. + * + * @param date 给定的日期 + * @return long 日期间隔天数,正数表示给定日期在当前日期之前,负数表示在当前日期之后 + * @since 1.0 + */ + public static long pastDays(Date date) { + // 将指定日期转换为yyyy-MM-dd格式 + date = DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.DATE_FORMAT)); + // 当前日期转换为yyyy-MM-dd格式 + Date currentDate = DateUtils.parseDate(DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT)); + long t = 0; + if (date != null && currentDate != null) { + t = (currentDate.getTime() - date.getTime()) / DateUtils.MILLISECONDS_PER_DAY; + } + return t; + } + + /** + * 获取当前日期指定天数之后的日期. + * + * @param num 相隔天数 + * @return Date 日期 + * @since 1.0 + */ + public static Date nextDay(int num) { + Calendar curr = Calendar.getInstance(); + curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) + num); + return curr.getTime(); + } + + /** + * 获取当前日期指定月数之后的日期. + * + * @param num 间隔月数 + * @return Date 日期 + * @since 1.0 + */ + public static Date nextMonth(int num) { + Calendar curr = Calendar.getInstance(); + curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + num); + return curr.getTime(); + } + + /** + * 获取当前日期指定年数之后的日期. + * + * @param num 间隔年数 + * @return Date 日期 + * @since 1.0 + */ + public static Date nextYear(int num) { + Calendar curr = Calendar.getInstance(); + curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + num); + return curr.getTime(); + } + + /** + * 将 Date 日期转化为 Calendar 类型日期. + * + * @param date 给定的时间,若为null,则默认为当前时间 + * @return Calendar Calendar对象 + * @since 1.0 + */ + public static Calendar getCalendar(Date date) { + Calendar calendar = Calendar.getInstance(); + // calendar.setFirstDayOfWeek(Calendar.SUNDAY);//每周从周日开始 + // calendar.setMinimalDaysInFirstWeek(1); // 设置每周最少为1天 + if (date != null) { + calendar.setTime(date); + } + return calendar; + } + + /** + * 计算两个日期之间相差天数. + * + * @param start 计算开始日期 + * @param end 计算结束日期 + * @return long 相隔天数 + * @since 1.0 + */ + public static long getDaysBetween(Date start, Date end) { + // 将指定日期转换为yyyy-MM-dd格式 + start = DateUtils.parseDate(DateUtils.formatDate(start, DateUtils.DATE_FORMAT)); + // 当前日期转换为yyyy-MM-dd格式 + end = DateUtils.parseDate(DateUtils.formatDate(end, DateUtils.DATE_FORMAT)); + + long diff = 0; + if (start != null && end != null) { + diff = (end.getTime() - start.getTime()) / DateUtils.MILLISECONDS_PER_DAY; + } + return diff; + } + + /** + * 计算两个日期之前相隔多少周. + * + * @param start 计算开始时间 + * @param end 计算结束时间 + * @return long 相隔周数,向下取整 + * @since 1.0 + */ + public static long getWeeksBetween(Date start, Date end) { + return getDaysBetween(start, end) / DateUtils.DAYS_PER_WEEK; + } + + /** + * 获取与指定日期间隔给定天数的日期. + * + * @param specifiedDay 给定的字符串格式日期,支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", + * "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", + * "yyyy/MM/dd HH:mm" + * @param num 间隔天数 + * @return String 间隔指定天数之后的日期 + * @since 1.0 + */ + public static String getSpecifiedDayAfter(String specifiedDay, int num) { + Date specifiedDate = parseDate(specifiedDay); + Calendar c = Calendar.getInstance(); + c.setTime(specifiedDate); + int day = c.get(Calendar.DATE); + c.set(Calendar.DATE, day + num); + String dayAfter = formatDate(c.getTime(), DateUtils.DATE_FORMAT); + return dayAfter; + } + + /** + * 计算两个日期之前间隔的小时数. + * + * @param date1 结束时间 + * @param date2 开始时间 + * @return String 相差的小时数,保留一位小数 + * @since 1.0 + */ + public static String dateMinus(Date date1, Date date2) { + if (date1 == null || date2 == null) { + return "0"; + } + Long r = date1.getTime() - date2.getTime(); + DecimalFormat df = new DecimalFormat("#.0"); + double result = r * 1.0 / DateUtils.MILLISECONDS_PER_HOUR; + return df.format(result); + } + + /** + * 获取当前季度 . + * + * @return Integer 当前季度数 + * @since 1.0 + */ + public static Integer getCurrentSeason() { + Calendar calendar = Calendar.getInstance(); + Integer month = calendar.get(Calendar.MONTH) + 1; + int season = 0; + if (month >= 1 && month <= 3) { + season = 1; + } else if (month >= 4 && month <= 6) { + season = 2; + } else if (month >= 7 && month <= 9) { + season = 3; + } else if (month >= 10 && month <= 12) { + season = 4; + } + return season; + } + + /** + * 将以秒为单位的时间转换为其他单位. + * + * @param seconds 秒数 + * @return String 例如 16分钟前、2小时前、3天前、4月前、5年前等 + * @since 1.0 + */ + public static String getIntervalBySeconds(long seconds) { + StringBuffer buffer = new StringBuffer(); + if (seconds < SECONDS_PER_MINUTE) { + buffer.append(seconds).append("秒前"); + } else if (seconds < SECONDS_PER_HOUR) { + buffer.append(seconds / SECONDS_PER_MINUTE).append("分钟前"); + } else if (seconds < SECONDS_PER_DAY) { + buffer.append(seconds / SECONDS_PER_HOUR).append("小时前"); + } else if (seconds < SECONDS_PER_MONTH) { + buffer.append(seconds / SECONDS_PER_DAY).append("天前"); + } else if (seconds < SECONDS_PER_YEAR) { + buffer.append(seconds / SECONDS_PER_MONTH).append("月前"); + } else { + buffer.append(seconds / DateUtils.SECONDS_PER_YEAR).append("年前"); + } + return buffer.toString(); + } + + /** + * getNowTimeBefore(记录时间相当于目前多久之前) + * + * @param seconds 秒 + * @return + * @throws @since 1.0 + * @author rlliu + */ + public static String getNowTimeBefore(long seconds) { + StringBuffer buffer = new StringBuffer(); + buffer.append("上传于"); + if (seconds < 3600) { + buffer.append((long) Math.floor(seconds / 60.0)).append("分钟前"); + } else if (seconds < 86400) { + buffer.append((long) Math.floor(seconds / 3600.0)).append("小时前"); + } else if (seconds < 604800) { + buffer.append((long) Math.floor(seconds / 86400.0)).append("天前"); + } else if (seconds < 2592000) { + buffer.append((long) Math.floor(seconds / 604800.0)).append("周前"); + } else if (seconds < 31104000) { + buffer.append((long) Math.floor(seconds / 2592000.0)).append("月前"); + } else { + buffer.append((long) Math.floor(seconds / 31104000.0)).append("年前"); + } + return buffer.toString(); + } + + /** + * getMonthsBetween(查询两个日期相隔的月份) + * + * @param startDate 开始日期1 (格式yyyy-MM-dd) + * @param endDate 截止日期2 (格式yyyy-MM-dd) + * @return + */ + public static int getMonthsBetween(String startDate, String endDate) { + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + c1.setTime(DateUtils.parseDate(startDate)); + c2.setTime(DateUtils.parseDate(endDate)); + int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); + int month = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH); + return Math.abs(year * 12 + month); + } + + /** + * getDayOfWeek(获取当前日期是星期几) + * + * @param dateStr 日期 + * @return 星期几 + */ + public static String getDayOfWeek(String dateStr) { + String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; + Date date = parseDate(dateStr); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + int num = calendar.get(Calendar.DAY_OF_WEEK) - 1; + return weekOfDays[num]; + } + + /** + * sns 格式 如几秒前,几分钟前,几小时前,几天前,几个月前,几年后, ... 精细,类如某个明星几秒钟之前发表了一篇微博 + * + * @param createTime + * @return + */ + public static String snsFormat(long createTime) { + long now = System.currentTimeMillis() / 1000; + long differ = now - createTime / 1000; + String dateStr = ""; + if (differ <= 60) { + dateStr = "刚刚"; + } else if (differ <= 3600) { + dateStr = (differ / 60) + "分钟前"; + } else if (differ <= 3600 * 24) { + dateStr = (differ / 3600) + "小时前"; + } else if (differ <= 3600 * 24 * 30) { + dateStr = (differ / (3600 * 24)) + "天前"; + } else { + Date date = new Date(createTime); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + dateStr = sdf.format(date); + } + return dateStr; + } + /** * 得到UTC时间,类型为字符串,格式为"yyyy-MM-dd HH:mm" * 如果获取失败,返回null + * * @return */ public static String getUTCTimeStr() { StringBuffer UTCTimeBuffer = new StringBuffer(); // 1、取得本地时间: - Calendar cal = Calendar.getInstance() ; + Calendar cal = Calendar.getInstance(); // 2、取得时间偏移量: int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); // 3、取得夏令时差: @@ -484,20 +496,19 @@ public static String getUTCTimeStr() { // 4、从本地时间里扣除这些差量,即可以取得UTC时间: cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); int year = cal.get(Calendar.YEAR); - int month = cal.get(Calendar.MONTH)+1; + int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); int hour = cal.get(Calendar.HOUR_OF_DAY); - int minute = cal.get(Calendar.MINUTE); - UTCTimeBuffer.append(year).append("-").append(month).append("-").append(day) ; - UTCTimeBuffer.append(" ").append(hour).append(":").append(minute) ; - try{ - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - sdf.parse(UTCTimeBuffer.toString()) ; - return UTCTimeBuffer.toString() ; - }catch(ParseException e) - { - e.printStackTrace() ; + int minute = cal.get(Calendar.MINUTE); + UTCTimeBuffer.append(year).append("-").append(month).append("-").append(day); + UTCTimeBuffer.append(" ").append(hour).append(":").append(minute); + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + sdf.parse(UTCTimeBuffer.toString()); + return UTCTimeBuffer.toString(); + } catch (ParseException e) { + e.printStackTrace(); } - return null ; + return null; } } \ No newline at end of file diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/LibC.java b/java/osx/broker/src/main/java/com/osx/broker/util/LibC.java index d41a59ef1b..0e066326af 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/LibC.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/LibC.java @@ -1,4 +1,3 @@ - package com.osx.broker.util; import com.sun.jna.*; diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/MessageConst.java b/java/osx/broker/src/main/java/com/osx/broker/util/MessageConst.java index 71bde20089..f3009e8f0e 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/MessageConst.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/MessageConst.java @@ -1,4 +1,3 @@ - package com.osx.broker.util; import java.util.HashSet; diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/MessageId.java b/java/osx/broker/src/main/java/com/osx/broker/util/MessageId.java index dd8f42f4e0..af20af727f 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/MessageId.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/MessageId.java @@ -1,4 +1,3 @@ - package com.osx.broker.util; import java.net.SocketAddress; diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/ResourceUtil.java b/java/osx/broker/src/main/java/com/osx/broker/util/ResourceUtil.java index 04fbff11fb..cd58e83f5b 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/ResourceUtil.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/ResourceUtil.java @@ -1,8 +1,8 @@ package com.osx.broker.util; -import com.osx.core.router.RouterInfo; import com.osx.broker.constants.Direction; +import com.osx.core.router.RouterInfo; public class ResourceUtil { @@ -11,19 +11,15 @@ public class ResourceUtil { // return ""; // } - static public String buildResource(RouterInfo routerInfo, Direction direction){ + static public String buildResource(RouterInfo routerInfo, Direction direction) { return new StringBuilder().append(routerInfo.getResource()). append("-").append(direction.name()).toString(); } - static public String buildResource(String resource,Direction direction){ + + static public String buildResource(String resource, Direction direction) { return new StringBuilder().append(resource). append("-").append(direction.name()).toString(); } - - - - - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/TransferExceptionUtil.java b/java/osx/broker/src/main/java/com/osx/broker/util/TransferExceptionUtil.java index 0fbdfeb112..6f9525de0c 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/TransferExceptionUtil.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/TransferExceptionUtil.java @@ -3,7 +3,6 @@ public class TransferExceptionUtil { - } diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/TransferUtil.java b/java/osx/broker/src/main/java/com/osx/broker/util/TransferUtil.java index 028b2292cb..c4baeee03e 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/TransferUtil.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/TransferUtil.java @@ -1,41 +1,50 @@ package com.osx.broker.util; //import com.firework.cluster.rpc.FireworkTransfer; + +import com.google.common.collect.Maps; import com.google.protobuf.ByteString; -import com.osx.core.constant.StatusCode; -import com.osx.core.exceptions.RemoteRpcException; -import com.osx.core.frame.*; +import com.google.protobuf.InvalidProtocolBufferException; +import com.osx.broker.eggroll.ErRollSiteHeader; +import com.osx.broker.http.HttpClientPool; +import com.osx.broker.http.PtpHttpResponse; +import com.osx.broker.queue.TransferQueue; import com.osx.core.config.MetaInfo; import com.osx.core.constant.Protocol; +import com.osx.core.constant.PtpHttpHeader; +import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; import com.osx.core.exceptions.NoRouterInfoException; +import com.osx.core.exceptions.RemoteRpcException; +import com.osx.core.frame.GrpcConnectionFactory; import com.osx.core.router.RouterInfo; -import com.osx.federation.rpc.Osx; -import com.osx.broker.eggroll.ErRollSiteHeader; -import com.osx.broker.queue.TransferQueue; -import com.google.protobuf.InvalidProtocolBufferException; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import com.webank.eggroll.core.transfer.Transfer; import io.grpc.ManagedChannel; import io.grpc.StatusRuntimeException; -import org.ppc.ptp.Pcp; +import org.apache.commons.lang3.StringUtils; +import org.ppc.ptp.Osx; import org.ppc.ptp.PrivateTransferProtocolGrpc; +import javax.servlet.http.HttpServletRequest; +import java.util.Map; + public class TransferUtil { /** * 2.0之前版本 + * * @param version * @return */ - public static boolean isOldVersionFate(String version){ - if(version==null) + public static boolean isOldVersionFate(String version) { + if (version == null) return true; int versionInteger = Integer.parseInt(version); - if(versionInteger>=200){ + if (versionInteger >= 200) { System.err.println("isOldVersionFate return false"); return false; - }else{ + } else { System.err.println("isOldVersionFate return true"); return true; } @@ -43,109 +52,202 @@ public static boolean isOldVersionFate(String version){ } - public static Proxy.Metadata buildProxyMetadataFromOutbound(Pcp.Outbound outbound){ + public static Proxy.Metadata buildProxyMetadataFromOutbound(Osx.Outbound outbound) { try { - return Proxy.Metadata.parseFrom(outbound.getPayload()); + return Proxy.Metadata.parseFrom(outbound.getPayload()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } return null; - }; + } + + ; - public static Pcp.Inbound buildInboundFromPushingPacket(Proxy.Packet packet,String targetMethod){ - System.err.println("==================buildInboundFromPushingPacket=========================="); - Pcp.Inbound.Builder inboundBuilder = Pcp.Inbound.newBuilder(); - Proxy.Topic srcTopic =packet.getHeader().getSrc(); - String srcPartyId = srcTopic.getPartyId(); + public static Osx.Inbound buildInboundFromPushingPacket(Proxy.Packet packet, String targetMethod) { + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + Proxy.Topic srcTopic = packet.getHeader().getSrc(); + String srcPartyId = srcTopic.getPartyId(); Proxy.Metadata metadata = packet.getHeader(); - // String oneLineStringMetadata = ToStringUtils.toOneLineString(metadata); + // String oneLineStringMetadata = ToStringUtils.toOneLineString(metadata); ByteString encodedRollSiteHeader = metadata.getExt(); //context.setActionType("push-eggroll"); - ErRollSiteHeader rsHeader=null; + ErRollSiteHeader rsHeader = null; try { - rsHeader= ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); - - + rsHeader = ErRollSiteHeader.parseFromPb(Transfer.RollSiteHeader.parseFrom(encodedRollSiteHeader)); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } //logger.info("=========ErRollSiteHeader {}",rsHeader); //"#", prefix: Array[String] = Array("__rsk") - String sessionId =""; - if(rsHeader!=null) { - sessionId = String.join("_", rsHeader.getRollSiteSessionId() , rsHeader.getDstRole(), rsHeader.getDstPartyId()); + String sessionId = ""; + if (rsHeader != null) { + sessionId = String.join("_", rsHeader.getRollSiteSessionId(), rsHeader.getDstRole(), rsHeader.getDstPartyId()); } Proxy.Topic desTopic = packet.getHeader().getDst(); String desPartyId = desTopic.getPartyId(); String desRole = desTopic.getRole(); inboundBuilder.setPayload(packet.toByteString()); - inboundBuilder.putMetadata(Pcp.Header.Version.name(), Long.toString(MetaInfo.CURRENT_VERSION)); - inboundBuilder.putMetadata(Pcp.Header.TechProviderCode.name(),"FT"); - inboundBuilder.putMetadata(Pcp.Header.Token.name(),"testToken"); - inboundBuilder.putMetadata(Pcp.Header.SourceNodeID.name(),srcPartyId); - inboundBuilder.putMetadata(Pcp.Header.TargetNodeID.name(),desPartyId); - inboundBuilder.putMetadata(Pcp.Header.SourceInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.TargetInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.SessionID.name(),sessionId); - inboundBuilder.putMetadata(Pcp.Metadata.TargetMethod.name(), targetMethod); - inboundBuilder.putMetadata(Pcp.Metadata.TargetComponentName.name(),desRole); - inboundBuilder.putMetadata(Pcp.Metadata.SourceComponentName.name(),""); - return inboundBuilder.build(); + inboundBuilder.putMetadata(Osx.Header.Version.name(), Long.toString(MetaInfo.CURRENT_VERSION)); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), "FT"); + inboundBuilder.putMetadata(Osx.Header.Token.name(), "testToken"); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), srcPartyId); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), desPartyId); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), sessionId); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), targetMethod); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), desRole); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), ""); + return inboundBuilder.build(); //inboundBuilder.putMetadata(Pcp.Metadata.MessageTopic.name(),transferId); + } + + ; - }; + static public void buildHttpFromPb(Osx.Inbound inbound){ - static public Pcp.Outbound redirect(Context context , Pcp.Inbound - produceRequest, RouterInfo routerInfo, boolean forceSend){ - Pcp.Outbound result = null; - // context.setActionType("redirect"); + + } + + + static public Osx.Inbound.Builder buildPbFromHttpRequest(HttpServletRequest request){ + + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + String Version = request.getHeader(PtpHttpHeader.Version); + String TechProviderCode = request.getHeader(PtpHttpHeader.TechProviderCode); + String TraceID = request.getHeader(PtpHttpHeader.TraceID); + String Token = request.getHeader(PtpHttpHeader.Token); + String SourceNodeID = request.getHeader(PtpHttpHeader.SourceNodeID); + String TargetNodeID = request.getHeader(PtpHttpHeader.TargetNodeID); + String SourceInstID = request.getHeader(PtpHttpHeader.SourceInstID); + String TargetInstID = request.getHeader(PtpHttpHeader.TargetInstID); + String SessionID = request.getHeader(PtpHttpHeader.SessionID); + String MessageTopic = request.getHeader(PtpHttpHeader.MessageTopic); + String MessageCode = request.getHeader(PtpHttpHeader.MessageCode); + String SourceComponentName = request.getHeader(PtpHttpHeader.SourceComponentName); + String TargetComponentName = request.getHeader(PtpHttpHeader.TargetComponentName); + String TargetMethod = request.getHeader(PtpHttpHeader.TargetMethod); + String MessageOffSet = request.getHeader(PtpHttpHeader.MessageOffSet); + String InstanceId = request.getHeader(PtpHttpHeader.InstanceId); + String Timestamp = request.getHeader(PtpHttpHeader.Timestamp); + + inboundBuilder.putMetadata(Osx.Header.Version.name(), Version != null ? Version : ""); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), TechProviderCode != null ? TechProviderCode : ""); + inboundBuilder.putMetadata(Osx.Header.Token.name(), Token != null ? Token : ""); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), SourceNodeID != null ? SourceNodeID : ""); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), TargetNodeID != null ? TargetNodeID : ""); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), SourceInstID != null ? SourceInstID : ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), TargetInstID != null ? TargetInstID : ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), SessionID != null ? SessionID : ""); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), TargetMethod != null ? TargetMethod : ""); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), TargetComponentName != null ? TargetComponentName : ""); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), SourceComponentName != null ? SourceComponentName : ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageTopic.name(), MessageTopic != null ? MessageTopic : ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageOffSet.name(), MessageOffSet != null ? MessageOffSet : ""); + inboundBuilder.putMetadata(Osx.Metadata.InstanceId.name(), InstanceId != null ? InstanceId : ""); + inboundBuilder.putMetadata(Osx.Metadata.Timestamp.name(), Timestamp != null ? Timestamp : ""); + return inboundBuilder; + + + } + + + + static public Osx.Outbound redirect(Context context, Osx.Inbound + produceRequest, RouterInfo routerInfo, boolean forceSend) { + Osx.Outbound result = null; + // context.setActionType("redirect"); // 目的端协议为grpc - if(routerInfo==null){ - throw new NoRouterInfoException(""); + if (routerInfo == null) { + throw new NoRouterInfoException("can not find router info"); } - if(routerInfo.getProtocol()==null||routerInfo.getProtocol().equals(Protocol.GRPC)) { - ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(routerInfo); + if (routerInfo.getProtocol() == null || routerInfo.getProtocol().equals(Protocol.GRPC)) { + ManagedChannel managedChannel = GrpcConnectionFactory.createManagedChannel(routerInfo,true); PrivateTransferProtocolGrpc.PrivateTransferProtocolBlockingStub stub = PrivateTransferProtocolGrpc.newBlockingStub(managedChannel); - try { result = stub.invoke(produceRequest); - }catch (StatusRuntimeException e){ - throw new RemoteRpcException(StatusCode.NET_ERROR,"send to "+routerInfo.toKey()+" error"); + } catch (StatusRuntimeException e) { + throw new RemoteRpcException(StatusCode.NET_ERROR, "send to " + routerInfo.toKey() + " error"); } // ServiceContainer.tokenApplyService.applyToken(context,routerInfo.getResource(),produceRequest.getSerializedSize()); + }else{ + if(routerInfo.getProtocol().equals(Protocol.HTTP)){ + String url = routerInfo.getUrl(); + + Map metaDataMap = produceRequest.getMetadataMap(); + + String version = metaDataMap.get(Osx.Header.Version.name()); + String techProviderCode = metaDataMap.get(Osx.Header.TechProviderCode.name()); + String traceId = metaDataMap.get(Osx.Header.TraceID.name()); + String token = metaDataMap.get(Osx.Header.Token.name()); + String sourceNodeId = metaDataMap.get(Osx.Header.SourceNodeID.name()); + String targetNodeId = metaDataMap.get(Osx.Header.TargetNodeID.name()); + String sourceInstId = metaDataMap.get(Osx.Header.SourceInstID.name()); + String targetInstId = metaDataMap.get(Osx.Header.TargetInstID.name()); + String sessionId = metaDataMap.get(Osx.Header.SessionID.name()); + String targetMethod = metaDataMap.get(Osx.Metadata.TargetMethod.name()); + String targetComponentName = metaDataMap.get(Osx.Metadata.TargetComponentName.name()); + String sourceComponentName = metaDataMap.get(Osx.Metadata.SourceComponentName.name()); + String sourcePartyId = StringUtils.isEmpty(sourceInstId) ? sourceNodeId : sourceInstId + "." + sourceNodeId; + String targetPartyId = StringUtils.isEmpty(targetInstId) ? targetNodeId : targetInstId + "." + targetNodeId; + String topic = metaDataMap.get(Osx.Metadata.MessageTopic.name()); + String offsetString = metaDataMap.get(Osx.Metadata.MessageOffSet.name()); + String InstanceId = metaDataMap.get(Osx.Metadata.InstanceId.name()); + String timestamp = metaDataMap.get(Osx.Metadata.Timestamp.name()); + String messageCode = metaDataMap.get(Osx.Metadata.MessageCode.name()); + Map header = Maps.newHashMap(); + header.put(PtpHttpHeader.Version,version!=null?version:""); + header.put(PtpHttpHeader.TechProviderCode,techProviderCode!=null?techProviderCode:""); + header.put(PtpHttpHeader.TraceID,traceId!=null?traceId:""); + header.put(PtpHttpHeader.Token,token!=null?token:""); + header.put(PtpHttpHeader.SourceNodeID,sourceNodeId!=null?sourceNodeId:""); + header.put(PtpHttpHeader.TargetNodeID,targetNodeId!=null?targetNodeId:""); + header.put(PtpHttpHeader.SourceInstID,sourceInstId!=null?sourceInstId:""); + header.put(PtpHttpHeader.TargetInstID,targetInstId!=null?targetInstId:""); + header.put(PtpHttpHeader.SessionID,sessionId!=null?sessionId:""); + header.put(PtpHttpHeader.MessageTopic,topic!=null?topic:""); + header.put(PtpHttpHeader.MessageCode,messageCode); + header.put(PtpHttpHeader.SourceComponentName,sourceComponentName!=null?sourceComponentName:""); + header.put(PtpHttpHeader.TargetComponentName,targetComponentName!=null?targetComponentName:""); + header.put(PtpHttpHeader.TargetMethod,targetMethod!=null?targetMethod:""); + header.put(PtpHttpHeader.MessageOffSet,offsetString!=null?offsetString:""); + header.put(PtpHttpHeader.InstanceId,InstanceId!=null?InstanceId:""); + header.put(PtpHttpHeader.Timestamp,timestamp!=null?timestamp:""); + result = HttpClientPool.sendPtpPost(url,produceRequest.getPayload().toByteArray(),header); + } } - return result; + return result; } - public static Pcp.Outbound buildResponse(String code , String msgReturn , TransferQueue.TransferQueueConsumeResult messageWraper){ - // FireworkTransfer.ConsumeResponse.Builder consumeResponseBuilder = FireworkTransfer.ConsumeResponse.newBuilder(); - Pcp.Outbound.Builder builder = Pcp.Outbound.newBuilder(); - - builder.setCode(code); - builder.setMessage(msgReturn); - if(messageWraper!=null) { - Osx.Message message = null; - try { - message = Osx.Message.parseFrom(messageWraper.getMessage().getBody()); - } catch (InvalidProtocolBufferException e) { - e.printStackTrace(); - } - builder.setPayload(message.toByteString()); - builder.putMetadata(Pcp.Metadata.MessageOffSet.name(),Long.toString(messageWraper.getRequestIndex()) ); + public static Osx.Outbound buildResponse(String code, String msgReturn, TransferQueue.TransferQueueConsumeResult messageWraper) { + // FireworkTransfer.ConsumeResponse.Builder consumeResponseBuilder = FireworkTransfer.ConsumeResponse.newBuilder(); + Osx.Outbound.Builder builder = Osx.Outbound.newBuilder(); + + builder.setCode(code); + builder.setMessage(msgReturn); + if (messageWraper != null) { + Osx.Message message = null; + try { + message = Osx.Message.parseFrom(messageWraper.getMessage().getBody()); + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + } + builder.setPayload(message.toByteString()); + builder.putMetadata(Osx.Metadata.MessageOffSet.name(), Long.toString(messageWraper.getRequestIndex())); // FireworkTransfer.Message msg = produceRequest.getMessage(); // consumeResponseBuilder.setTransferId(produceRequest.getTransferId()); // consumeResponseBuilder.setMessage(msg); // consumeResponseBuilder.setStartOffset(messageWraper.getRequestIndex()); // consumeResponseBuilder.setTotalOffset(messageWraper.getLogicIndexTotal()); - } + } return builder.build(); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/util/UtilAll.java b/java/osx/broker/src/main/java/com/osx/broker/util/UtilAll.java index 0ceade0bbc..ed21c3f1ab 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/util/UtilAll.java +++ b/java/osx/broker/src/main/java/com/osx/broker/util/UtilAll.java @@ -1,6 +1,10 @@ - package com.osx.broker.util; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.validator.routines.InetAddressValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -14,27 +18,18 @@ import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.Enumeration; -import java.util.List; +import java.util.*; import java.util.zip.CRC32; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.validator.routines.InetAddressValidator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class UtilAll { - private static final Logger log = LoggerFactory.getLogger(UtilAll.class); - public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd#HH:mm:ss:SSS"; public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); + private static final Logger log = LoggerFactory.getLogger(UtilAll.class); public static int getPid() { RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); @@ -104,8 +99,8 @@ public static String timeMillisToHumanString(final long t) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(t); return String.format("%04d%02d%02d%02d%02d%02d%03d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, - cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), - cal.get(Calendar.MILLISECOND)); + cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), + cal.get(Calendar.MILLISECOND)); } public static long computeNextMorningTimeMillis() { @@ -160,25 +155,25 @@ public static String timeMillisToHumanString2(final long t) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(t); return String.format("%04d-%02d-%02d %02d:%02d:%02d,%03d", - cal.get(Calendar.YEAR), - cal.get(Calendar.MONTH) + 1, - cal.get(Calendar.DAY_OF_MONTH), - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - cal.get(Calendar.SECOND), - cal.get(Calendar.MILLISECOND)); + cal.get(Calendar.YEAR), + cal.get(Calendar.MONTH) + 1, + cal.get(Calendar.DAY_OF_MONTH), + cal.get(Calendar.HOUR_OF_DAY), + cal.get(Calendar.MINUTE), + cal.get(Calendar.SECOND), + cal.get(Calendar.MILLISECOND)); } public static String timeMillisToHumanString3(final long t) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(t); return String.format("%04d%02d%02d%02d%02d%02d", - cal.get(Calendar.YEAR), - cal.get(Calendar.MONTH) + 1, - cal.get(Calendar.DAY_OF_MONTH), - cal.get(Calendar.HOUR_OF_DAY), - cal.get(Calendar.MINUTE), - cal.get(Calendar.SECOND)); + cal.get(Calendar.YEAR), + cal.get(Calendar.MONTH) + 1, + cal.get(Calendar.DAY_OF_MONTH), + cal.get(Calendar.HOUR_OF_DAY), + cal.get(Calendar.MINUTE), + cal.get(Calendar.SECOND)); } public static double getDiskPartitionSpaceUsedPercent(final String path) { @@ -431,9 +426,9 @@ public static boolean isInternalIP(byte[] ip) { public static boolean isInternalV6IP(InetAddress inetAddr) { if (inetAddr.isAnyLocalAddress() // Wild card ipv6 - || inetAddr.isLinkLocalAddress() // Single broadcast ipv6 address: fe80:xx:xx... - || inetAddr.isLoopbackAddress() //Loopback ipv6 address - || inetAddr.isSiteLocalAddress()) { // Site local ipv6 address: fec0:xx:xx... + || inetAddr.isLinkLocalAddress() // Single broadcast ipv6 address: fe80:xx:xx... + || inetAddr.isLoopbackAddress() //Loopback ipv6 address + || inetAddr.isSiteLocalAddress()) { // Site local ipv6 address: fec0:xx:xx... return true; } return false; @@ -443,7 +438,7 @@ private static boolean ipCheck(byte[] ip) { if (ip.length != 4) { throw new RuntimeException("illegal ipv4 bytes"); } - + InetAddressValidator validator = InetAddressValidator.getInstance(); return validator.isValidInet4Address(ipToIPv4Str(ip)); } @@ -462,8 +457,8 @@ public static String ipToIPv4Str(byte[] ip) { return null; } return new StringBuilder().append(ip[0] & 0xFF).append(".").append( - ip[1] & 0xFF).append(".").append(ip[2] & 0xFF) - .append(".").append(ip[3] & 0xFF).toString(); + ip[1] & 0xFF).append(".").append(ip[2] & 0xFF) + .append(".").append(ip[3] & 0xFF).toString(); } public static String ipToIPv6Str(byte[] ip) { diff --git a/java/osx/broker/src/main/java/com/osx/broker/zk/AbstractZookeeperClient.java b/java/osx/broker/src/main/java/com/osx/broker/zk/AbstractZookeeperClient.java index 9a06649baf..78028c2774 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/zk/AbstractZookeeperClient.java +++ b/java/osx/broker/src/main/java/com/osx/broker/zk/AbstractZookeeperClient.java @@ -17,7 +17,6 @@ package com.osx.broker.zk; - import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,13 +42,11 @@ public abstract class AbstractZookeeperClient acls = new ArrayList<>(); - public CuratorZookeeperClient(ZkConfig zkConfig) { + public CuratorZookeeperClient(ZkConfig zkConfig) { super(zkConfig); try { int timeout = zkConfig.getTimeout(); @@ -180,7 +179,7 @@ public void createEphemeral(String path, String data) throws NodeExistsException } } catch (NodeExistsException e) { - throw e; + throw e; // try { //// if (aclEnable) { //// Stat stat = client.checkExists().forPath(path); @@ -213,11 +212,12 @@ public void delete(String path) { } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException(e.getMessage(), e); - }catch(Throwable e){ + } catch (Throwable e) { e.printStackTrace(); } } - public void close(){ + + public void close() { client.close(); } diff --git a/java/osx/broker/src/main/java/com/osx/broker/zk/ZkConfig.java b/java/osx/broker/src/main/java/com/osx/broker/zk/ZkConfig.java index 30ba278288..b0ee05a0fd 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/zk/ZkConfig.java +++ b/java/osx/broker/src/main/java/com/osx/broker/zk/ZkConfig.java @@ -7,26 +7,30 @@ public class ZkConfig { - - public ZkConfig(String address,int timeout){ + String address; + int timeout; + public ZkConfig(String address, int timeout) { this.address = address; this.timeout = timeout; } - String address; - int timeout; + public int getTimeout() { return timeout; } + public void setTimeout(int timeout) { this.timeout = timeout; } + public String getAddress() { return address; } + public void setAddress(String address) { this.address = address; } - public List toIpPortList(){ - return Lists.newArrayList(address.split(",")); + + public List toIpPortList() { + return Lists.newArrayList(address.split(",")); } } diff --git a/java/osx/broker/src/main/java/com/osx/broker/zk/ZookeeperClient.java b/java/osx/broker/src/main/java/com/osx/broker/zk/ZookeeperClient.java index ff4aa42511..dd6c1f2053 100644 --- a/java/osx/broker/src/main/java/com/osx/broker/zk/ZookeeperClient.java +++ b/java/osx/broker/src/main/java/com/osx/broker/zk/ZookeeperClient.java @@ -15,6 +15,7 @@ */ package com.osx.broker.zk; + import org.apache.zookeeper.KeeperException; import java.util.List; diff --git a/java/osx/broker/src/main/java/com/osx/tech/provider/FateTechProvider.java b/java/osx/broker/src/main/java/com/osx/tech/provider/FateTechProvider.java index 46305c0824..f6558f2306 100644 --- a/java/osx/broker/src/main/java/com/osx/tech/provider/FateTechProvider.java +++ b/java/osx/broker/src/main/java/com/osx/tech/provider/FateTechProvider.java @@ -2,50 +2,156 @@ import com.google.common.base.Preconditions; -import com.osx.core.frame.Lifecycle; +import com.google.common.collect.Sets; +import com.google.protobuf.ByteString; +import com.osx.broker.ServiceContainer; +import com.osx.broker.grpc.ContextUtil; +import com.osx.broker.interceptor.RequestHandleInterceptor; +import com.osx.broker.ptp.*; +import com.osx.broker.service.UnaryCallService; +import com.osx.broker.util.TransferExceptionUtil; +import com.osx.broker.util.TransferUtil; +import com.osx.core.config.MetaInfo; +import com.osx.core.constant.Dict; +import com.osx.core.constant.PtpHttpHeader; import com.osx.core.context.Context; +import com.osx.core.exceptions.ErrorMessageUtil; +import com.osx.core.exceptions.ExceptionInfo; +import com.osx.core.exceptions.ParameterException; +import com.osx.core.frame.Lifecycle; import com.osx.core.provider.TechProvider; import com.osx.core.ptp.TargetMethod; import com.osx.core.service.InboundPackage; import com.osx.core.service.OutboundPackage; import com.osx.core.service.ServiceAdaptor; -import com.osx.broker.ServiceContainer; -import com.osx.broker.grpc.ContextUtil; -import com.osx.broker.interceptor.RequestHandleInterceptor; -import com.osx.broker.ptp.*; - -import com.osx.broker.service.*; +import com.osx.core.utils.JsonUtil; import io.grpc.stub.StreamObserver; -import org.ppc.ptp.Pcp; +import org.apache.commons.io.IOUtils; +import org.eclipse.jetty.http.HttpHeader; +import org.ppc.ptp.Osx; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static java.lang.System.arraycopy; + /** * FATE 相关实现 */ public class FateTechProvider implements TechProvider, Lifecycle { - ConcurrentMap serviceAdaptorConcurrentMap = new ConcurrentHashMap<>(); + ConcurrentMap serviceAdaptorConcurrentMap = new ConcurrentHashMap<>(); + + RequestHandleInterceptor requestHandleInterceptor; + + private Set httpAllowedMethod= Sets.newHashSet(TargetMethod.PRODUCE_MSG.name(),TargetMethod.UNARY_CALL.name()); - RequestHandleInterceptor requestHandleInterceptor ; + private void checkHttpAllowedMethod(String targetMethod){ + + if(!httpAllowedMethod.contains(targetMethod)){ + throw new ParameterException("target method :"+targetMethod+"is not allowed"); + } + + } @Override - public void processInvoke(Pcp.Inbound request, StreamObserver responseObserver) { + public void processHttpInvoke(HttpServletRequest request, HttpServletResponse response) { + Context context = ContextUtil.buildContext(); + Osx.Inbound.Builder inboundBuilder ; + ServiceAdaptor serviceAdaptor=null; + try { + String Version = request.getHeader(PtpHttpHeader.Version); + String TechProviderCode = request.getHeader(PtpHttpHeader.TechProviderCode); + String TraceID = request.getHeader(PtpHttpHeader.TraceID); + String Token = request.getHeader(PtpHttpHeader.Token); + String SourceNodeID = request.getHeader(PtpHttpHeader.SourceNodeID); + String TargetNodeID = request.getHeader(PtpHttpHeader.TargetNodeID); + String SourceInstID = request.getHeader(PtpHttpHeader.SourceInstID); + String TargetInstID = request.getHeader(PtpHttpHeader.TargetInstID); + String SessionID = request.getHeader(PtpHttpHeader.SessionID); + String MessageTopic = request.getHeader(PtpHttpHeader.MessageTopic); + String MessageCode = request.getHeader(PtpHttpHeader.MessageCode); + String SourceComponentName = request.getHeader(PtpHttpHeader.SourceComponentName); + String TargetComponentName = request.getHeader(PtpHttpHeader.TargetComponentName); + String TargetMethod = request.getHeader(PtpHttpHeader.TargetMethod); + String MessageOffSet = request.getHeader(PtpHttpHeader.MessageOffSet); + String InstanceId = request.getHeader(PtpHttpHeader.InstanceId); + String Timestamp = request.getHeader(PtpHttpHeader.Timestamp); + context.setSrcPartyId(SourceNodeID); + context.setDesPartyId(TargetNodeID); + context.setSessionId(SessionID); + context.setTopic(MessageTopic); + context.setActionType(TargetMethod); + inboundBuilder = TransferUtil.buildPbFromHttpRequest(request); + String targetMethod = inboundBuilder.getMetadataMap().get(Osx.Metadata.TargetMethod.name()); + checkHttpAllowedMethod(TargetMethod); + serviceAdaptor = this.getServiceAdaptor(TargetMethod); + byte[] buffer = new byte[MetaInfo.PROPERTY_HTTP_REQUEST_BODY_MAX_SIZE]; + int length = IOUtils.read(request.getInputStream(), buffer); + byte[] data = new byte[length]; + System.arraycopy(buffer, 0, data, 0, length); + inboundBuilder.setPayload(ByteString.copyFrom(data)); + }catch(Exception e){ + ExceptionInfo exceptionInfo = ErrorMessageUtil.handleExceptionExceptionInfo(context,e); + this.writeHttpRespose(response, exceptionInfo.getCode(),exceptionInfo.getMessage(),null); + context.setReturnCode(exceptionInfo.getCode()); + context.setReturnMsg(exceptionInfo.getMessage()); + context.printFlowLog(); + return ; + } + InboundPackage inboundPackage = new InboundPackage(); + inboundPackage.setBody(inboundBuilder.build()); + OutboundPackage outboundPackage = serviceAdaptor.service(context, inboundPackage); + Osx.Outbound outbound = outboundPackage.getData(); + response.setContentType(Dict.CONTENT_TYPE_JSON_UTF8); + this.writeHttpRespose(response,outbound.getCode(),outbound.getMessage(),outbound.getPayload().toByteArray() ); + } + + private void writeHttpRespose(HttpServletResponse response,String code, + String msg, + byte[] content){ + try { + response.setHeader(PtpHttpHeader.ReturnCode,code); + response.setHeader(PtpHttpHeader.MessageCode,msg); + OutputStream outputStream = response.getOutputStream(); + if(content!=null) { + outputStream.write(content); + } + outputStream.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + @Override + public void processGrpcInvoke(Osx.Inbound request, StreamObserver responseObserver) { Map metaDataMap = request.getMetadataMap(); - String targetMethod = metaDataMap.get(Pcp.Metadata.forNumber(4).name()); - Context context = ContextUtil.buildContext(); + String targetMethod = metaDataMap.get(Osx.Metadata.forNumber(4).name()); + //RouterInfo routerInfo = ServiceContainer.fateRouterService.route(sourcePartyId,sourceComponentName,targetPartyId,targetComponentName); //context.setRouterInfo(routerInfo); - ServiceAdaptor serviceAdaptor = this.getServiceAdaptor(targetMethod); - InboundPackage inboundPackage = new InboundPackage(); + ServiceAdaptor serviceAdaptor = this.getServiceAdaptor(targetMethod); + if(serviceAdaptor==null){ + /** + * 异常 + */ + } + Context context = ContextUtil.buildContext(); + InboundPackage inboundPackage = new InboundPackage(); inboundPackage.setBody(request); - OutboundPackage outboundPackage = serviceAdaptor.service(context,inboundPackage); - if(outboundPackage.getData()!=null) { + OutboundPackage outboundPackage = serviceAdaptor.service(context, inboundPackage); + if (outboundPackage.getData() != null) { responseObserver.onNext(outboundPackage.getData()); responseObserver.onCompleted(); } @@ -57,26 +163,19 @@ public String getProviderId() { } - @Override - public StreamObserver processTransport(Pcp.Inbound fristPackage, StreamObserver responseObserver) { + public StreamObserver processGrpcTransport(Osx.Inbound fristPackage, StreamObserver responseObserver) { return null; } @Override public void init() { - Preconditions.checkArgument(ServiceContainer.fateRouterService!=null); + Preconditions.checkArgument(ServiceContainer.fateRouterService != null); requestHandleInterceptor = new RequestHandleInterceptor(ServiceContainer.fateRouterService); registerServiceAdaptor(); } - - public FateTechProvider setRouterService(){ - - return this; - } - @Override public void start() { @@ -87,17 +186,17 @@ public void destroy() { } - private ServiceAdaptor getServiceAdaptor(String name){ - return this.serviceAdaptorConcurrentMap.get(name); + private ServiceAdaptor getServiceAdaptor(String name) { + return this.serviceAdaptorConcurrentMap.get(name); } - private void registerServiceAdaptor(){ - this.serviceAdaptorConcurrentMap.put(TargetMethod.UNARY_CALL.name(), new UnaryCallService()); - this.serviceAdaptorConcurrentMap.put(TargetMethod.PRODUCE_MSG.name(),new PtpProduceService().addPreProcessor(requestHandleInterceptor)); - this.serviceAdaptorConcurrentMap.put(TargetMethod.ACK_MSG.name(),new PtpAckService().addPreProcessor(requestHandleInterceptor)); - this.serviceAdaptorConcurrentMap.put(TargetMethod.CONSUME_MSG.name(),new PtpConsumeService().addPreProcessor(requestHandleInterceptor)); - this.serviceAdaptorConcurrentMap.put(TargetMethod.QUERY_TOPIC.name(),new PtpQueryTransferQueueService().addPreProcessor(requestHandleInterceptor)); + private void registerServiceAdaptor() { + this.serviceAdaptorConcurrentMap.put(TargetMethod.UNARY_CALL.name(), new PtpUnaryCallService().addPreProcessor(requestHandleInterceptor)); + this.serviceAdaptorConcurrentMap.put(TargetMethod.PRODUCE_MSG.name(), new PtpProduceService().addPreProcessor(requestHandleInterceptor)); + this.serviceAdaptorConcurrentMap.put(TargetMethod.ACK_MSG.name(), new PtpAckService().addPreProcessor(requestHandleInterceptor)); + this.serviceAdaptorConcurrentMap.put(TargetMethod.CONSUME_MSG.name(), new PtpConsumeService().addPreProcessor(requestHandleInterceptor)); + this.serviceAdaptorConcurrentMap.put(TargetMethod.QUERY_TOPIC.name(), new PtpQueryTransferQueueService().addPreProcessor(requestHandleInterceptor)); this.serviceAdaptorConcurrentMap.put(TargetMethod.CANCEL_TOPIC.name(), new PtpCancelTransferService().addPreProcessor(requestHandleInterceptor)); - this.serviceAdaptorConcurrentMap.put(TargetMethod.PUSH.name(), new PtpPushService()); + this.serviceAdaptorConcurrentMap.put(TargetMethod.PUSH.name(), new PtpPushService()); } } diff --git a/java/osx/broker/src/main/java/com/osx/tech/provider/TechProviderRegister.java b/java/osx/broker/src/main/java/com/osx/tech/provider/TechProviderRegister.java index 5f07009ce7..67f59c1b8c 100644 --- a/java/osx/broker/src/main/java/com/osx/tech/provider/TechProviderRegister.java +++ b/java/osx/broker/src/main/java/com/osx/tech/provider/TechProviderRegister.java @@ -3,7 +3,6 @@ import com.google.common.base.Preconditions; import com.osx.core.frame.Lifecycle; import com.osx.core.provider.TechProvider; -import org.ppc.ptp.Pcp; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -14,20 +13,22 @@ */ public class TechProviderRegister implements Lifecycle { - ConcurrentMap registerMap = new ConcurrentHashMap<>(); - public TechProvider select(Pcp.Inbound inbound){ - Map metaDataMap = inbound.getMetadataMap(); - String version = metaDataMap.get(Pcp.Header.Version.name()); - String techProviderCode = metaDataMap.get(Pcp.Header.TechProviderCode.name()); - Preconditions.checkArgument(techProviderCode!=null); + ConcurrentMap registerMap = new ConcurrentHashMap<>(); - return this.registerMap.get(techProviderCode); + //public TechProvider select(Pcp.Inbound inbound) { + + public TechProvider select(String techProviderCode ) { + Preconditions.checkArgument(techProviderCode != null); + + return this.registerMap.get(techProviderCode); } - public void init(){ - FateTechProvider fateTechProvider = new FateTechProvider(); + + + public void init() { + FateTechProvider fateTechProvider = new FateTechProvider(); fateTechProvider.init(); - this.registerMap.put(fateTechProvider.getProviderId(),fateTechProvider); + this.registerMap.put(fateTechProvider.getProviderId(), fateTechProvider); } @Override diff --git a/java/osx/broker/src/main/resources/broker.properties b/java/osx/broker/src/main/resources/broker.properties index ff16ffc8c3..dd24064c02 100644 --- a/java/osx/broker/src/main/resources/broker.properties +++ b/java/osx/broker/src/main/resources/broker.properties @@ -1,4 +1,5 @@ #-------------PLAINTEXT grpc port +http.port=8080 port= 9370 ############ TLS port tls.port = 9883 @@ -7,7 +8,6 @@ self.party=9999 ############standalone/cluster deploy.model=standalone #cluster.manager.address = localhost:9678 -use.queue.model=false ############TLS/PLAINTEXT #server.negotiationType=PLAINTEXT zk.url=localhost:2181 diff --git a/java/osx/broker/src/main/resources/route_table.json b/java/osx/broker/src/main/resources/route_table.json index 59b8782336..6615d092ae 100755 --- a/java/osx/broker/src/main/resources/route_table.json +++ b/java/osx/broker/src/main/resources/route_table.json @@ -18,8 +18,8 @@ }, "10000":{ "default":[{ - "port": 9370, - "ip": "10.42.0.1", + "port": 9889, + "ip": "localhost", "version": 197 }] diff --git a/java/osx/broker/src/test/java/com/osx/broker/mock/MockServer.java b/java/osx/broker/src/test/java/com/osx/broker/mock/MockServer.java index 5c473895d0..424aa14ed6 100644 --- a/java/osx/broker/src/test/java/com/osx/broker/mock/MockServer.java +++ b/java/osx/broker/src/test/java/com/osx/broker/mock/MockServer.java @@ -3,12 +3,12 @@ //import com.firework.cluster.rpc .FireworkQueueServiceGrpc; //import com.firework.cluster.rpc.FireworkTransfer; -import com.osx.core.constant.Dict; -import com.osx.core.config.MetaInfo; -import com.osx.core.constant.StatusCode; +import com.google.protobuf.ByteString; import com.osx.broker.grpc.ContextPrepareInterceptor; import com.osx.broker.grpc.ServiceExceptionHandler; -import com.google.protobuf.ByteString; +import com.osx.core.config.MetaInfo; +import com.osx.core.constant.Dict; +import com.osx.core.constant.StatusCode; import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.Server; @@ -16,7 +16,8 @@ import io.grpc.ServerInterceptors; import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; import io.grpc.stub.StreamObserver; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; + import org.ppc.ptp.PrivateTransferProtocolGrpc; import java.io.IOException; @@ -24,7 +25,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import static com.osx.broker.ServiceContainer.*; +import static com.osx.broker.ServiceContainer.proxyGrpcService; public class MockServer { @@ -42,27 +43,81 @@ public class MockServer { // // } - private static class PtpService extends PrivateTransferProtocolGrpc.PrivateTransferProtocolImplBase{ + public static void main(String[] args) { + InetSocketAddress addr = new InetSocketAddress("localhost", 9375); + System.err.println("grpc address : " + addr); + NettyServerBuilder nettyServerBuilder = NettyServerBuilder.forAddress(addr); + nettyServerBuilder.addService(new MockService()); + nettyServerBuilder.addService(new PtpService()); + //nettyServerBuilder.addService(new QueueMockService()); + //nettyServerBuilder.addService(ServerInterceptors.intercept(new QueueMockService(), new ServiceExceptionHandler(),new ContextPrepareInterceptor())); + Server server = nettyServerBuilder.build(); + try { + server.start(); + try { + server.awaitTermination(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.err.println("================"); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private static Server buildServer() { + NettyServerBuilder nettyServerBuilder = (NettyServerBuilder) ServerBuilder.forPort(9375); + nettyServerBuilder.addService(ServerInterceptors.intercept(proxyGrpcService, new ServiceExceptionHandler(), new ContextPrepareInterceptor())); + // nettyServerBuilder.addService(ServerInterceptors.intercept(queueGrpcservice, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); + // nettyServerBuilder.addService(ServerInterceptors.intercept(commonService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); + + nettyServerBuilder + .executor(Executors.newCachedThreadPool()) + .maxConcurrentCallsPerConnection(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONCURRENT_CALL_PER_CONNECTION) + .maxInboundMessageSize(MetaInfo.PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE) + .maxInboundMetadataSize(MetaInfo.PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE) + .flowControlWindow(MetaInfo.PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW); + if (MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC > 0) + nettyServerBuilder.keepAliveTime(MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC > 0) + nettyServerBuilder.keepAliveTimeout(MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC > 0) + nettyServerBuilder.permitKeepAliveTime(MetaInfo.PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED) + nettyServerBuilder.permitKeepAliveWithoutCalls(MetaInfo.PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED); + if (MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC > 0) + nettyServerBuilder.maxConnectionIdle(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC > 0) + nettyServerBuilder.maxConnectionAge(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC, TimeUnit.SECONDS); + if (MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC > 0) + nettyServerBuilder.maxConnectionAgeGrace(MetaInfo.PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC, TimeUnit.SECONDS); + return nettyServerBuilder.build(); + } - public PtpService(){ + private static class PtpService extends PrivateTransferProtocolGrpc.PrivateTransferProtocolImplBase { + + public PtpService() { } - public StreamObserver transport( - StreamObserver responseObserver) { - return new StreamObserver(){ + public StreamObserver transport( + StreamObserver responseObserver) { + return new StreamObserver() { @Override - public void onNext(Pcp.Inbound inbound) { + public void onNext(Osx.Inbound inbound) { System.err.println(inbound); } + @Override public void onError(Throwable throwable) { - throwable.printStackTrace(); + throwable.printStackTrace(); } + @Override public void onCompleted() { - Proxy.Metadata metadata= Proxy.Metadata.newBuilder().build(); - Pcp.Outbound.Builder outBoundBuilder = Pcp.Outbound.newBuilder(); + Proxy.Metadata metadata = Proxy.Metadata.newBuilder().build(); + Osx.Outbound.Builder outBoundBuilder = Osx.Outbound.newBuilder(); outBoundBuilder.setCode(StatusCode.SUCCESS); outBoundBuilder.setMessage(Dict.SUCCESS); outBoundBuilder.setPayload(metadata.toByteString()); @@ -73,11 +128,12 @@ public void onCompleted() { } /** + * */ - public void invoke(Pcp.Inbound request, - StreamObserver responseObserver) { - System.err.println("invoke : "+request); - Pcp.Outbound out = org.ppc.ptp.Pcp.Outbound.newBuilder().setCode(StatusCode.SUCCESS).setMessage(Dict.SUCCESS).build(); + public void invoke(Osx.Inbound request, + StreamObserver responseObserver) { + System.err.println("invoke : " + request); + Osx.Outbound out = Osx.Outbound.newBuilder().setCode(StatusCode.SUCCESS).setMessage(Dict.SUCCESS).build(); responseObserver.onNext(out); responseObserver.onCompleted(); @@ -85,13 +141,12 @@ public void invoke(Pcp.Inbound request, } - - private static class MockService extends DataTransferServiceGrpc.DataTransferServiceImplBase{ + private static class MockService extends DataTransferServiceGrpc.DataTransferServiceImplBase { public void unaryCall(com.webank.ai.eggroll.api.networking.proxy.Proxy.Packet request, io.grpc.stub.StreamObserver responseObserver) { System.err.println("receive unaryCall"); - Proxy.Packet.Builder builder = Proxy.Packet.newBuilder(); + Proxy.Packet.Builder builder = Proxy.Packet.newBuilder(); builder.setBody(Proxy.Data.newBuilder().setValue(ByteString.copyFromUtf8("my name is god")).build()); responseObserver.onNext(builder.build()); responseObserver.onCompleted(); @@ -100,14 +155,14 @@ public void unaryCall(com.webank.ai.eggroll.api.networking.proxy.Proxy.Packet re public void pull(com.webank.ai.eggroll.api.networking.proxy.Proxy.Metadata request, io.grpc.stub.StreamObserver responseObserver) { - System.err.println("receive pull"); - for(int i=0;i<10;i++){ - Proxy.Packet.Builder builder = Proxy.Packet.newBuilder(); - builder.setBody(Proxy.Data.newBuilder().setValue(ByteString.copyFromUtf8("xiaoxiao"+i)).build()); - responseObserver.onNext(builder.build()); - System.err.println("send"); - } - responseObserver.onCompleted(); + System.err.println("receive pull"); + for (int i = 0; i < 10; i++) { + Proxy.Packet.Builder builder = Proxy.Packet.newBuilder(); + builder.setBody(Proxy.Data.newBuilder().setValue(ByteString.copyFromUtf8("xiaoxiao" + i)).build()); + responseObserver.onNext(builder.build()); + System.err.println("send"); + } + responseObserver.onCompleted(); } @@ -116,7 +171,7 @@ public io.grpc.stub.StreamObserver() { @Override public void onNext(Proxy.Packet value) { - System.err.println("mock receive"+ value); + System.err.println("mock receive" + value); } @Override @@ -128,7 +183,7 @@ public void onError(Throwable t) { @Override public void onCompleted() { System.err.println("mock receive onCompleted"); - Proxy.Metadata.Builder builder = Proxy.Metadata.newBuilder(); + Proxy.Metadata.Builder builder = Proxy.Metadata.newBuilder(); builder.setExt(ByteString.copyFrom("this is hearven".getBytes())); responseObserver.onNext(builder.build()); responseObserver.onCompleted(); @@ -136,50 +191,4 @@ public void onCompleted() { }; } } - - public static void main(String[] args){ - InetSocketAddress addr = new InetSocketAddress("localhost", 9375); - System.err.println("grpc address : "+addr); - NettyServerBuilder nettyServerBuilder = NettyServerBuilder.forAddress(addr); - nettyServerBuilder.addService(new MockService()); - nettyServerBuilder.addService(new PtpService()); - //nettyServerBuilder.addService(new QueueMockService()); - //nettyServerBuilder.addService(ServerInterceptors.intercept(new QueueMockService(), new ServiceExceptionHandler(),new ContextPrepareInterceptor())); - Server server = nettyServerBuilder.build(); - try { - server.start(); - try { - server.awaitTermination(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.err.println("================"); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - - private static Server buildServer( ){ - NettyServerBuilder nettyServerBuilder = (NettyServerBuilder) ServerBuilder.forPort(9375); - nettyServerBuilder.addService(ServerInterceptors.intercept(proxyGrpcService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); - // nettyServerBuilder.addService(ServerInterceptors.intercept(queueGrpcservice, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); - // nettyServerBuilder.addService(ServerInterceptors.intercept(commonService, new ServiceExceptionHandler(),new ContextPrepareInterceptor())); - - nettyServerBuilder - .executor(Executors.newCachedThreadPool()) - .maxConcurrentCallsPerConnection(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION) - .maxInboundMessageSize(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE) - .maxInboundMetadataSize(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE) - .flowControlWindow(MetaInfo.PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC > 0) nettyServerBuilder.keepAliveTime(MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC > 0) nettyServerBuilder.keepAliveTimeout(MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC > 0) nettyServerBuilder.permitKeepAliveTime(MetaInfo.PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED) nettyServerBuilder.permitKeepAliveWithoutCalls(MetaInfo.PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC > 0) nettyServerBuilder.maxConnectionIdle(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC > 0) nettyServerBuilder.maxConnectionAge(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC, TimeUnit.SECONDS); - if (MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC > 0) nettyServerBuilder.maxConnectionAgeGrace(MetaInfo.PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC, TimeUnit.SECONDS); - return nettyServerBuilder.build(); - } } diff --git a/java/osx/broker/src/test/java/com/osx/broker/test/grpc/OldFateTest.java b/java/osx/broker/src/test/java/com/osx/broker/test/grpc/OldFateTest.java index fe53438dcb..8de9ad5f2e 100644 --- a/java/osx/broker/src/test/java/com/osx/broker/test/grpc/OldFateTest.java +++ b/java/osx/broker/src/test/java/com/osx/broker/test/grpc/OldFateTest.java @@ -19,10 +19,11 @@ public class OldFateTest { static int port = 9889;//9371 - static String ip= "localhost"; + static String ip = "localhost"; static Logger logger = LoggerFactory.getLogger(OldFateTest.class); static boolean useSSL = false; + public static ManagedChannel createManagedChannel(String ip, int port) { try { NettyChannelBuilder channelBuilder = NettyChannelBuilder @@ -38,8 +39,7 @@ public static ManagedChannel createManagedChannel(String ip, int port) { .retryBufferSize(16 << 20) .maxRetryAttempts(20); - if (useSSL) - { + if (useSSL) { SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient() .keyManager(new File("/Users/kaideng/work/cert/cjt.crt"), new File("/Users/kaideng/work/cert/cjt.key")) @@ -49,7 +49,6 @@ public static ManagedChannel createManagedChannel(String ip, int port) { // .trustManager(new File("/Users/kaideng/work/cert/yl/fdn-ca.crt")) - .sessionTimeout(3600 << 4) .sessionCacheSize(65536); channelBuilder.sslContext(sslContextBuilder.build()).useTransportSecurity(); @@ -61,28 +60,28 @@ public static ManagedChannel createManagedChannel(String ip, int port) { channelBuilder.usePlaintext(); } return channelBuilder.build(); - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); - // logger.error("create channel error : " ,e); + // logger.error("create channel error : " ,e); //e.printStackTrace(); } return null; } - public static void testUnaryCall(){ + + public static void testUnaryCall() { logger.info("test unary call"); - ManagedChannel managedChannel = createManagedChannel(ip,port); + ManagedChannel managedChannel = createManagedChannel(ip, port); DataTransferServiceGrpc.DataTransferServiceBlockingStub stub = DataTransferServiceGrpc.newBlockingStub(managedChannel); - Proxy.Packet.Builder builder = Proxy.Packet.newBuilder(); + Proxy.Packet.Builder builder = Proxy.Packet.newBuilder(); Transfer.RollSiteHeader.Builder headerBuilder = Transfer.RollSiteHeader.newBuilder(); headerBuilder.setDstPartyId("10001"); builder.setHeader(Proxy.Metadata.newBuilder().setExt(headerBuilder.build().toByteString())); - Proxy.Data.Builder dataBuilder = Proxy.Data.newBuilder(); + Proxy.Data.Builder dataBuilder = Proxy.Data.newBuilder(); dataBuilder.setKey("name"); dataBuilder.setValue(ByteString.copyFrom(("xiaoxiao").getBytes())); builder.setBody(Proxy.Data.newBuilder().setValue(ByteString.copyFromUtf8("kaideng"))); Proxy.Packet result = stub.unaryCall(builder.build()); - System.err.println(result); + System.err.println(result); } @@ -114,13 +113,13 @@ public static void testUnaryCall(){ // } - public static void testPush(){ + public static void testPush() { - ManagedChannel managedChannel = createManagedChannel("localhost",port); + ManagedChannel managedChannel = createManagedChannel("localhost", port); DataTransferServiceGrpc.DataTransferServiceStub stub = DataTransferServiceGrpc.newStub(managedChannel); - StreamObserver responseOb = new StreamObserver() { + StreamObserver responseOb = new StreamObserver() { @Override public void onNext(Proxy.Metadata value) { System.err.println("response onNext"); @@ -128,8 +127,8 @@ public void onNext(Proxy.Metadata value) { @Override public void onError(Throwable t) { - logger.error("on Error",t); - t.printStackTrace(); + logger.error("on Error", t); + t.printStackTrace(); } @Override @@ -148,43 +147,41 @@ public void onCompleted() { // for (int t = 0; t < 1; t++) { - // new Thread(() -> { - StreamObserver requestOb = stub.push(responseOb); - for (int i = 0; i < 3; i++) { - Proxy.Packet.Builder packetBuilder = Proxy.Packet.newBuilder(); - packetBuilder.setHeader(Proxy.Metadata.newBuilder().setSrc(Proxy.Topic.newBuilder().setPartyId("9999")).setDst(Proxy.Topic.newBuilder().setPartyId("10000").setName("kaidengTestTopic").build()).build()); + StreamObserver requestOb = stub.push(responseOb); + for (int i = 0; i < 3; i++) { + Proxy.Packet.Builder packetBuilder = Proxy.Packet.newBuilder(); + packetBuilder.setHeader(Proxy.Metadata.newBuilder().setSrc(Proxy.Topic.newBuilder().setPartyId("9999")).setDst(Proxy.Topic.newBuilder().setPartyId("10000").setName("kaidengTestTopic").build()).build()); // Transfer.RollSiteHeader.Builder headerBuilder = Transfer.RollSiteHeader.newBuilder(); // headerBuilder.setDstPartyId("10000"); - // packetBuilder.setHeader(Proxy.Metadata.newBuilder().setExt(headerBuilder.build().toByteString())); - Proxy.Data.Builder dataBuilder = Proxy.Data.newBuilder(); - dataBuilder.setKey("name"); - dataBuilder.setValue(ByteString.copyFrom(("xiaoxiao" + i).getBytes())); - packetBuilder.setBody(dataBuilder.build()); - - if(i==99){ - // throw new RuntimeException(); - } - requestOb.onNext(packetBuilder.build()); - System.err.println("test send !!!!!!!!!!!!!!!!!!!!!!"); - } - requestOb.onCompleted(); + // packetBuilder.setHeader(Proxy.Metadata.newBuilder().setExt(headerBuilder.build().toByteString())); + Proxy.Data.Builder dataBuilder = Proxy.Data.newBuilder(); + dataBuilder.setKey("name"); + dataBuilder.setValue(ByteString.copyFrom(("xiaoxiao" + i).getBytes())); + packetBuilder.setBody(dataBuilder.build()); + + if (i == 99) { + // throw new RuntimeException(); + } + requestOb.onNext(packetBuilder.build()); + System.err.println("test send !!!!!!!!!!!!!!!!!!!!!!"); + } + requestOb.onCompleted(); // }).start(); - // } + // } } } - - public static void main(String[] args){ - System.err.println("==============="); - testUnaryCall(); - CountDownLatch countDownLatch = new CountDownLatch( 1); + public static void main(String[] args) { + System.err.println("==============="); + testUnaryCall(); + CountDownLatch countDownLatch = new CountDownLatch(1); try { - countDownLatch.await() ; + countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/java/osx/broker/src/test/java/com/osx/broker/test/grpc/QueueTest.java b/java/osx/broker/src/test/java/com/osx/broker/test/grpc/QueueTest.java index 3ad184c154..cf3d97c221 100644 --- a/java/osx/broker/src/test/java/com/osx/broker/test/grpc/QueueTest.java +++ b/java/osx/broker/src/test/java/com/osx/broker/test/grpc/QueueTest.java @@ -1,87 +1,97 @@ package com.osx.broker.test.grpc; //import com.firework.cluster.rpc.FireworkQueueServiceGrpc; //import com.firework.cluster.rpc.FireworkTransfer; + import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import com.osx.core.ptp.TargetMethod; -import com.osx.federation.rpc.Osx; -import com.webank.ai.eggroll.api.networking.proxy.DataTransferServiceGrpc; -import com.webank.ai.eggroll.api.networking.proxy.Proxy; import io.grpc.ManagedChannel; import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; -import io.grpc.stub.StreamObserver; -import org.junit.After; +import org.junit.Before; import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.Before; +import org.junit.Test; import org.junit.runners.MethodSorters; -import org.ppc.ptp.Pcp; +import org.ppc.ptp.Osx; import org.ppc.ptp.PrivateTransferProtocolGrpc; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.UUID; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.*; - @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class QueueTest { Logger logger = LoggerFactory.getLogger(QueueTest.class); - String ip= "localhost"; + String ip = "localhost"; //int port = 8250;//nginx - int port = 9889;//nginx + int port = 9370;//nginx String desPartyId = "10000"; String desRole = ""; String srcPartyId = "9999"; String srcRole = ""; String transferId = "testTransferId"; - String sessionId = "testSessionId"; - - - - + String sessionId = "testSessionId"; PrivateTransferProtocolGrpc.PrivateTransferProtocolBlockingStub blockingStub; // FireworkQueueServiceGrpc.FireworkQueueServiceBlockingStub blockingStub; - @Before - public void init (){ - ManagedChannel managedChannel = createManagedChannel(ip,port); - // stub = PrivateTransferProtocolGrpc.newBlockingStub(); - ManagedChannel managedChannel2 = createManagedChannel(ip,port); - blockingStub = PrivateTransferProtocolGrpc.newBlockingStub(managedChannel2); + public static ManagedChannel createManagedChannel(String ip, int port) { + try { + NettyChannelBuilder channelBuilder = NettyChannelBuilder + .forAddress(ip, port) + .keepAliveTime(12, TimeUnit.SECONDS) + .keepAliveTimeout(1, TimeUnit.SECONDS) + .keepAliveWithoutCalls(true) + //.idleTimeout(60, TimeUnit.SECONDS) + .perRpcBufferLimit(128 << 20) + .flowControlWindow(32 << 20) + .maxInboundMessageSize(32 << 20) + .enableRetry() + .retryBufferSize(16 << 20) + .maxRetryAttempts(20); + + channelBuilder.usePlaintext(); + return channelBuilder.build(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; } + @Before + public void init() { + ManagedChannel managedChannel = createManagedChannel(ip, port); + // stub = PrivateTransferProtocolGrpc.newBlockingStub(); + ManagedChannel managedChannel2 = createManagedChannel(ip, port); + blockingStub = PrivateTransferProtocolGrpc.newBlockingStub(managedChannel2); + } @Test - public void test02Query() { - Pcp.Inbound.Builder inboundBuilder = Pcp.Inbound.newBuilder(); - inboundBuilder.putMetadata(Pcp.Header.Version.name(),"123"); - inboundBuilder.putMetadata(Pcp.Header.TechProviderCode.name(),"FT"); - inboundBuilder.putMetadata(Pcp.Header.Token.name(),"testToken"); - inboundBuilder.putMetadata(Pcp.Header.SourceNodeID.name(),"9999"); - inboundBuilder.putMetadata(Pcp.Header.TargetNodeID.name(),"10000"); - inboundBuilder.putMetadata(Pcp.Header.SourceInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.TargetInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.SessionID.name(),"testSessionID"); - inboundBuilder.putMetadata(Pcp.Metadata.TargetMethod.name(), TargetMethod.QUERY_TOPIC.name()); - inboundBuilder.putMetadata(Pcp.Metadata.TargetComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.SourceComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.MessageTopic.name(),transferId); - - - Pcp.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); - Osx.TopicInfo topicInfo= null; + public void test02Query() { + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + inboundBuilder.putMetadata(Osx.Header.Version.name(), "123"); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), "FT"); + inboundBuilder.putMetadata(Osx.Header.Token.name(), "testToken"); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), "9999"); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), "10000"); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), "testSessionID"); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), TargetMethod.QUERY_TOPIC.name()); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageTopic.name(), transferId); + + + Osx.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); + Osx.TopicInfo topicInfo = null; try { topicInfo = Osx.TopicInfo.parseFrom(outbound.getPayload()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } - System.err.println("response " +topicInfo); + System.err.println("response " + topicInfo); try { Thread.sleep(100); } catch (InterruptedException e) { @@ -90,73 +100,29 @@ public void test02Query() { } - - - - -// @Test -// public void test03Consume(){ -// System.err.println("testConsume"); -// CountDownLatch countDownLatch = new CountDownLatch(1); -// -// FireworkTransfer.ConsumeRequest consumeRequest = FireworkTransfer.ConsumeRequest.newBuilder().setTransferId(this.transferId).build(); -// -// stub.consume(consumeRequest, new StreamObserver() { -// -// int count =0; -// -// @Override -// public void onNext(FireworkTransfer.ConsumeResponse value) { -// count++; -// System.err.println("receive : "+value); -// } -// -// @Override -// public void onError(Throwable t) { -// t.printStackTrace(); -// System.err.println("error"); -// countDownLatch.countDown(); -// } -// -// @Override -// public void onCompleted() { -// System.err.println("onCompleted"); -// countDownLatch.countDown(); -// } -// }); -// try { -// countDownLatch.await(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// -// } - - @Test - public void test04UnaryProduce(){ + public void test04UnaryProduce() { for (int i = 0; i < 10; i++) { - Pcp.Inbound.Builder inboundBuilder = Pcp.Inbound.newBuilder(); - inboundBuilder.putMetadata(Pcp.Header.Version.name(),"123"); - inboundBuilder.putMetadata(Pcp.Header.TechProviderCode.name(),"FT"); - inboundBuilder.putMetadata(Pcp.Header.Token.name(),"testToken"); - inboundBuilder.putMetadata(Pcp.Header.SourceNodeID.name(),"9999"); - inboundBuilder.putMetadata(Pcp.Header.TargetNodeID.name(),"10000"); - inboundBuilder.putMetadata(Pcp.Header.SourceInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.TargetInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.SessionID.name(),"testSessionID"); - inboundBuilder.putMetadata(Pcp.Metadata.TargetMethod.name(),"PRODUCE_MSG"); - inboundBuilder.putMetadata(Pcp.Metadata.TargetComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.SourceComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.MessageTopic.name(),transferId); + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + inboundBuilder.putMetadata(Osx.Header.Version.name(), "123"); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), "FT"); + inboundBuilder.putMetadata(Osx.Header.Token.name(), "testToken"); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), "9999"); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), "10000"); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), "testSessionID"); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), "PRODUCE_MSG"); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageTopic.name(), transferId); //inboundBuilder.getMetadataMap().put(Pcp.Metadata.MessageOffSet.name(),); - Osx.Message.Builder messageBuilder = Osx.Message.newBuilder(); - messageBuilder.setBody(ByteString.copyFrom(("test body element "+i).getBytes())); - messageBuilder.setHead(ByteString.copyFrom(("test head "+i).getBytes())); + Osx.Message.Builder messageBuilder = Osx.Message.newBuilder(); + messageBuilder.setBody(ByteString.copyFrom(("test body element " + i).getBytes())); + messageBuilder.setHead(ByteString.copyFrom(("test head " + i).getBytes())); inboundBuilder.setPayload(messageBuilder.build().toByteString()); - Pcp.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); - System.err.println("response " +outbound); + Osx.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); + System.err.println("response " + outbound); try { Thread.sleep(100); } catch (InterruptedException e) { @@ -165,119 +131,85 @@ public void test04UnaryProduce(){ } } - - - - - - public void test07Ack(long index){ - - Pcp.Inbound.Builder inboundBuilder = Pcp.Inbound.newBuilder(); - inboundBuilder.putMetadata(Pcp.Header.Version.name(),"123"); - inboundBuilder.putMetadata(Pcp.Header.TechProviderCode.name(),"FT"); - inboundBuilder.putMetadata(Pcp.Header.Token.name(),"testToken"); - inboundBuilder.putMetadata(Pcp.Header.SourceNodeID.name(),"9999"); - inboundBuilder.putMetadata(Pcp.Header.TargetNodeID.name(),"10000"); - inboundBuilder.putMetadata(Pcp.Header.SourceInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.TargetInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.SessionID.name(),"testSessionID"); - inboundBuilder.putMetadata(Pcp.Metadata.TargetMethod.name(),TargetMethod.ACK_MSG.name()); - inboundBuilder.putMetadata(Pcp.Metadata.TargetComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.SourceComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.MessageTopic.name(),transferId); - inboundBuilder.putMetadata(Pcp.Metadata.MessageOffSet.name(), Long.toString(index)); - Pcp.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); - System.err.println("ack response:"+outbound); + public void test07Ack(long index) { + + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + inboundBuilder.putMetadata(Osx.Header.Version.name(), "123"); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), "FT"); + inboundBuilder.putMetadata(Osx.Header.Token.name(), "testToken"); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), "9999"); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), "10000"); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), "testSessionID"); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), TargetMethod.ACK_MSG.name()); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageTopic.name(), transferId); + inboundBuilder.putMetadata(Osx.Metadata.MessageOffSet.name(), Long.toString(index)); + Osx.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); + System.err.println("ack response:" + outbound); } - - - @Test - public void test06UnaryConsume(){ - boolean needContinue= true; - Pcp.Outbound consumeResponse; - int count=0; + public void test06UnaryConsume() { + boolean needContinue = true; + Osx.Outbound consumeResponse; + int count = 0; do { System.err.println("==================="); - Pcp.Inbound.Builder inboundBuilder = Pcp.Inbound.newBuilder(); - inboundBuilder.putMetadata(Pcp.Header.Version.name(),"123"); - inboundBuilder.putMetadata(Pcp.Header.TechProviderCode.name(),"FT"); - inboundBuilder.putMetadata(Pcp.Header.Token.name(),"testToken"); - inboundBuilder.putMetadata(Pcp.Header.SourceNodeID.name(),"9999"); - inboundBuilder.putMetadata(Pcp.Header.TargetNodeID.name(),"10000"); - inboundBuilder.putMetadata(Pcp.Header.SourceInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.TargetInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.SessionID.name(),"testSessionID"); - inboundBuilder.putMetadata(Pcp.Metadata.TargetMethod.name(),TargetMethod.CONSUME_MSG.name()); - inboundBuilder.putMetadata(Pcp.Metadata.TargetComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.SourceComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.MessageTopic.name(),transferId); - inboundBuilder.putMetadata(Pcp.Metadata.MessageOffSet.name(), "-1"); + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + inboundBuilder.putMetadata(Osx.Header.Version.name(), "123"); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), "FT"); + inboundBuilder.putMetadata(Osx.Header.Token.name(), "testToken"); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), "9999"); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), "10000"); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), "testSessionID"); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), TargetMethod.CONSUME_MSG.name()); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageTopic.name(), transferId); + inboundBuilder.putMetadata(Osx.Metadata.MessageOffSet.name(), "-1"); consumeResponse = blockingStub.invoke(inboundBuilder.build()); System.err.println(consumeResponse); - String indexString = consumeResponse.getMetadataMap().get(Pcp.Metadata.MessageOffSet.name()); - Long index = Long.parseLong(indexString); + String indexString = consumeResponse.getMetadataMap().get(Osx.Metadata.MessageOffSet.name()); + Long index = Long.parseLong(indexString); test07Ack(index); - String code = consumeResponse.getCode(); - String msg = consumeResponse.getMessage(); - if(code.equals("0")) { + String code = consumeResponse.getCode(); + String msg = consumeResponse.getMessage(); + if (code.equals("0")) { try { - Osx.Message message = Osx.Message.parseFrom(consumeResponse.getPayload()); + Osx.Message message = Osx.Message.parseFrom(consumeResponse.getPayload()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } } - index ++; + index++; count++; - }while(count<10); + } while (count < 10); } - @Test - public void test07CancelTransfer(){ - - Pcp.Inbound.Builder inboundBuilder = Pcp.Inbound.newBuilder(); - inboundBuilder.putMetadata(Pcp.Header.Version.name(),"123"); - inboundBuilder.putMetadata(Pcp.Header.TechProviderCode.name(),"FT"); - inboundBuilder.putMetadata(Pcp.Header.Token.name(),"testToken"); - inboundBuilder.putMetadata(Pcp.Header.SourceNodeID.name(),"9999"); - inboundBuilder.putMetadata(Pcp.Header.TargetNodeID.name(),"10000"); - inboundBuilder.putMetadata(Pcp.Header.SourceInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.TargetInstID.name(),""); - inboundBuilder.putMetadata(Pcp.Header.SessionID.name(),"testSessionID"); - inboundBuilder.putMetadata(Pcp.Metadata.TargetMethod.name(),TargetMethod.CONSUME_MSG.name()); - inboundBuilder.putMetadata(Pcp.Metadata.TargetComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.SourceComponentName.name(),""); - inboundBuilder.putMetadata(Pcp.Metadata.MessageTopic.name(),transferId); - Pcp.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); - System.err.println("cancel result :"+outbound); - } - - - public static ManagedChannel createManagedChannel(String ip, int port) { - try { - NettyChannelBuilder channelBuilder = NettyChannelBuilder - .forAddress(ip, port) - .keepAliveTime(12, TimeUnit.SECONDS) - .keepAliveTimeout(1, TimeUnit.SECONDS) - .keepAliveWithoutCalls(true) - //.idleTimeout(60, TimeUnit.SECONDS) - .perRpcBufferLimit(128 << 20) - .flowControlWindow(32 << 20) - .maxInboundMessageSize(32 << 20) - .enableRetry() - .retryBufferSize(16 << 20) - .maxRetryAttempts(20); - - channelBuilder.usePlaintext(); - return channelBuilder.build(); - } - catch (Exception e) { - e.printStackTrace(); - } - return null; + public void test07CancelTransfer() { + + Osx.Inbound.Builder inboundBuilder = Osx.Inbound.newBuilder(); + inboundBuilder.putMetadata(Osx.Header.Version.name(), "123"); + inboundBuilder.putMetadata(Osx.Header.TechProviderCode.name(), "FT"); + inboundBuilder.putMetadata(Osx.Header.Token.name(), "testToken"); + inboundBuilder.putMetadata(Osx.Header.SourceNodeID.name(), "9999"); + inboundBuilder.putMetadata(Osx.Header.TargetNodeID.name(), "10000"); + inboundBuilder.putMetadata(Osx.Header.SourceInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.TargetInstID.name(), ""); + inboundBuilder.putMetadata(Osx.Header.SessionID.name(), "testSessionID"); + inboundBuilder.putMetadata(Osx.Metadata.TargetMethod.name(), TargetMethod.CONSUME_MSG.name()); + inboundBuilder.putMetadata(Osx.Metadata.TargetComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.SourceComponentName.name(), ""); + inboundBuilder.putMetadata(Osx.Metadata.MessageTopic.name(), transferId); + Osx.Outbound outbound = blockingStub.invoke(inboundBuilder.build()); + System.err.println("cancel result :" + outbound); } } diff --git a/java/osx/core/pom.xml b/java/osx/core/pom.xml index a5830aa6e0..edcf57f354 100644 --- a/java/osx/core/pom.xml +++ b/java/osx/core/pom.xml @@ -22,11 +22,74 @@ slf4j-api + + com.fasterxml.jackson.core + jackson-databind + + + + com.google.protobuf + protobuf-java-util + + commons-cli commons-cli - 1.5.0 + + org.apache.commons + commons-collections4 + + + org.apache.commons + commons-lang3 + + + com.google.guava + guava + + + + org.projectlombok + lombok + + + + org.apache.zookeeper + zookeeper + + + slf4j-log4j12 + org.slf4j + + + + + org.eclipse.jetty + jetty-servlet + + + io.grpc + grpc-netty-shaded + + + io.grpc + grpc-protobuf + + + io.grpc + grpc-stub + + + + commons-io + commons-io + + + + + + diff --git a/java/osx/core/src/main/java/com/osx/core/config/Config.java b/java/osx/core/src/main/java/com/osx/core/config/Config.java index 318a048ea4..adaa572c8f 100644 --- a/java/osx/core/src/main/java/com/osx/core/config/Config.java +++ b/java/osx/core/src/main/java/com/osx/core/config/Config.java @@ -1,33 +1,31 @@ -package com.osx.core.config; -import java.util.HashMap; -import java.util.Map; -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; - -public class Config { - - PropertiesConfiguration config; - - public Config(String configFile) { - initConfig(configFile); - } - - public String getString( String property) { - String value = config.getString(property); - return value; - } - - public int getInteger( String property) { - Integer value = config.getInt(property); - return value; - } - - private void initConfig(String configFile) { - try { - config = new PropertiesConfiguration(configFile); - } - catch (ConfigurationException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file +//package com.osx.core.config; +// +//import org.apache.commons.configuration.ConfigurationException; +//import org.apache.commons.configuration.PropertiesConfiguration; +// +//public class Config { +// +// PropertiesConfiguration config; +// +// public Config(String configFile) { +// initConfig(configFile); +// } +// +// public String getString(String property) { +// String value = config.getString(property); +// return value; +// } +// +// public int getInteger(String property) { +// Integer value = config.getInt(property); +// return value; +// } +// +// private void initConfig(String configFile) { +// try { +// config = new PropertiesConfiguration(configFile); +// } catch (ConfigurationException e) { +// e.printStackTrace(); +// } +// } +//} \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/config/GrpcChannelInfo.java b/java/osx/core/src/main/java/com/osx/core/config/GrpcChannelInfo.java index e9012c00ee..88c98091dc 100644 --- a/java/osx/core/src/main/java/com/osx/core/config/GrpcChannelInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/config/GrpcChannelInfo.java @@ -1,19 +1,19 @@ package com.osx.core.config; -import io.grpc.netty.shaded.io.grpc.netty.NegotiationType; import lombok.Data; -import java.util.concurrent.TimeUnit; - @Data public class GrpcChannelInfo { - int keepAliveTime; - boolean keepAliveWithoutCalls= true; - int idelTimeOut; - int perRpcBufferLimit; - int flowControlWindow; - int maxInboundMessageSize; - int retryBufferSize; - int maxRetryAttemps; + int keepAliveTime; + int keepAliveTimeout; + boolean keepAliveWithoutCalls = true; + int idelTimeOut; + int perRpcBufferLimit; + int flowControlWindow; + int maxInboundMessageSize; + int retryBufferSize; + int maxRetryAttemps; + + } diff --git a/java/osx/core/src/main/java/com/osx/core/config/MasterInfo.java b/java/osx/core/src/main/java/com/osx/core/config/MasterInfo.java index 15f3b3bc09..3267664a18 100644 --- a/java/osx/core/src/main/java/com/osx/core/config/MasterInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/config/MasterInfo.java @@ -3,6 +3,6 @@ import lombok.Data; @Data -public class MasterInfo{ - String instanceId; - } \ No newline at end of file +public class MasterInfo { + String instanceId; +} \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/config/MetaInfo.java b/java/osx/core/src/main/java/com/osx/core/config/MetaInfo.java index d7892c5bfb..806a205382 100644 --- a/java/osx/core/src/main/java/com/osx/core/config/MetaInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/config/MetaInfo.java @@ -16,77 +16,98 @@ package com.osx.core.config; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.osx.core.constant.DeployMode; import com.osx.core.constant.Dict; import com.osx.core.constant.StreamLimitMode; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; - import java.lang.reflect.Field; import java.util.Map; import java.util.Set; public class MetaInfo { - public static volatile MasterInfo masterInfo; - public static int PROPERTY_GRPC_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION = 1000; - public static int PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE = 128<<20; - public static int PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE = (2 << 30) - 1; - public static int PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW = 128<<20; - public static int PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC = 7200; - public static int PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC = 3600; - public static int PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC = 10; - public static boolean PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED = true; - public static int PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC = 86400; - public static int PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC = 86400; - public static int PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC = 86400; - public static int PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT= 600; - public static boolean PROPERTY_USE_DIRECT_CACHE = false; - public static int PROPERTY_TRANSFER_FILE_CACHE_SIZE = 1<<27; - - public static int PROPERTY_TRANSFER_RETRY_COUNT=1; - - public static int MAP_FILE_SIZE = 1<<25; - public static int PROPERTY_INDEX_MAP_FILE_SIZE = 1<<21; - public static Boolean TRANSFER_FATECLOUD_AHTHENTICATION_ENABLED ; - public static Boolean TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG ; - public static String TRANSFER_FATECLOUD_AUTHENTICATION_URI ; - public static String TRANSFER_FATECLOUD_AUTHENTICATION_APPKEY ; - public static String TRANSFER_FATECLOUD_AUTHENTICATION_APPSERCRET ; - public static String TRANSFER_FATECLOUD_AUTHENTICATION_ROLE ; - public static String TRANSFER_FATECLOUD_SECRET_INFO_URL ; - public static String TRANSFER_FATECLOUD_AUTHENTICATION_URL ; - public static String PROPERTY_SERVER_CERTCHAIN_FILE; - public static String PROPERTY_SERVER_PRIVATEKEY_FILE; - public static String PROPERTY_SERVER_CA_FILE; - public static int ROLLSITE_PARTY_ID ; public static final long CURRENT_VERSION = 100; + public static volatile MasterInfo masterInfo; + public static int PROPERTY_GRPC_SERVER_MAX_CONCURRENT_CALL_PER_CONNECTION = 1000; + public static int PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE = 128 << 20; + public static int PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE = (2 << 30) - 1; + public static int PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW = 128 << 20; + public static int PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC = 7200; + public static int PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC = 3600; + public static int PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC = 10; + public static boolean PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED = true; + public static int PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC = 86400; + public static int PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC = 86400; + public static int PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC = 86400; + public static int PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT = 600; + + + + public static int PROPERTY_GRPC_CLIENT_MAX_CONCURRENT_CALL_PER_CONNECTION = 1000; + public static int PROPERTY_GRPC_CLIENT_MAX_INBOUND_METADATA_SIZE = 128 << 20; + public static int PROPERTY_GRPC_CLIENT_MAX_INBOUND_MESSAGE_SIZE = (2 << 30) - 1; + public static int PROPERTY_GRPC_CLIENT_FLOW_CONTROL_WINDOW = 128 << 20; + public static int PROPERTY_GRPC_CLIENT_KEEPALIVE_TIME_SEC = 7200; + public static int PROPERTY_GRPC_CLIENT_KEEPALIVE_TIMEOUT_SEC = 3600; + public static int PROPERTY_GRPC_CLIENT_PERMIT_KEEPALIVE_TIME_SEC = 10; + public static boolean PROPERTY_GRPC_CLIENT_KEEPALIVE_WITHOUT_CALLS_ENABLED = true; + public static int PROPERTY_GRPC_CLIENT_MAX_CONNECTION_IDLE_SEC = 86400; + public static int PROPERTY_GRPC_CLIENT_MAX_CONNECTION_AGE_SEC = 86400; + public static int PROPERTY_GRPC_CLIENT_MAX_CONNECTION_AGE_GRACE_SEC = 86400; + public static int PROPERTY_GRPC_CLIENT_PER_RPC_BUFFER_LIMIT=86400; + + public static int PROPERTY_GRPC_CLIENT_RETRY_BUFFER_SIZE = 86400; + + + + public static boolean PROPERTY_USE_DIRECT_CACHE = false; + public static int PROPERTY_TRANSFER_FILE_CACHE_SIZE = 1 << 27; + public static int PROPERTY_TRANSFER_RETRY_COUNT = 1; + public static int MAP_FILE_SIZE = 1 << 25; + public static int PROPERTY_INDEX_MAP_FILE_SIZE = 1 << 21; + public static Boolean TRANSFER_FATECLOUD_AHTHENTICATION_ENABLED; + public static Boolean TRANSFER_FATECLOUD_AUTHENTICATION_USE_CONFIG; + public static String TRANSFER_FATECLOUD_AUTHENTICATION_URI; + public static String TRANSFER_FATECLOUD_AUTHENTICATION_APPKEY; + public static String TRANSFER_FATECLOUD_AUTHENTICATION_APPSERCRET; + public static String TRANSFER_FATECLOUD_AUTHENTICATION_ROLE; + public static String TRANSFER_FATECLOUD_SECRET_INFO_URL; + public static String TRANSFER_FATECLOUD_AUTHENTICATION_URL; + public static String PROPERTY_SERVER_CERTCHAIN_FILE; + public static String PROPERTY_SERVER_PRIVATEKEY_FILE; + public static String PROPERTY_SERVER_CA_FILE; + public static int ROLLSITE_PARTY_ID; public static Integer PROPERTY_PORT; + public static Integer PROPERTY_HTTP_PORT; + public static int PROPERTY_HTTP_REQUEST_BODY_MAX_SIZE=4096; + public static String PROPERTY_HTTP_CONTEXT_PATH="/osx"; + public static String PROPERTY_HTTP_SERVLET_PATH="/*"; public static Integer PROPERTY_TLS_PORT; public static String PROPERTY_ZK_URL; public static Boolean PROPERTY_USE_DISRUPTOR = true; public static int PROPERTY_STREAM_LIMIT_MAX_TRY_TIME = 3; - public static String PROPERTY_USER_HOME=""; + public static String PROPERTY_USER_HOME = ""; - public static Integer PROPERTY_SAMPLE_COUNT=10; - public static Integer PROPERTY_INTERVAL_MS = 1000; - public static Boolean PROPERTY_USE_QUEUE_MODEL = false; - public static String PROPERTY_STREAM_LIMIT_MODE = StreamLimitMode.NOLIMIT.name(); + public static Integer PROPERTY_SAMPLE_COUNT = 10; + public static Integer PROPERTY_INTERVAL_MS = 1000; + //public static Boolean PROPERTY_USE_QUEUE_MODEL = false; + public static String PROPERTY_STREAM_LIMIT_MODE = StreamLimitMode.NOLIMIT.name(); - public static Integer PROPERTY_CONSUMER_TIMEOUT = 30000; - public static Integer PROPERTY_QUEUE_MAX_FREE_TIME ; - public static Integer PROPERTY_MAPPED_FILE_EXPIRE_TIME=3600*1000*36; - public static Integer PROPERTY_MAX_CONSUME_EMPTY_TRY_COUNT= 30; + public static Integer PROPERTY_CONSUMER_TIMEOUT = 30000; + public static Integer PROPERTY_QUEUE_MAX_FREE_TIME; + public static Integer PROPERTY_MAPPED_FILE_EXPIRE_TIME = 3600 * 1000 * 36; + public static Integer PROPERTY_MAX_CONSUME_EMPTY_TRY_COUNT = 30; - public static Integer PROPERTY_MAX_TRANSFER_CACHE_SIZE = 1<<30; - public static String PROPERTY_TRANSFER_FILE_PATH_PRE ; - public static String PROPERTY_DEPLOY_MODE="standalone"; - public static String PROPERTY_TRANSFER_APPLY_CACHE="/tmp/cachetest"; + public static Integer PROPERTY_MAX_TRANSFER_CACHE_SIZE = 1 << 30; + public static String PROPERTY_TRANSFER_FILE_PATH_PRE; + public static String PROPERTY_DEPLOY_MODE = "standalone"; + public static String PROPERTY_TRANSFER_APPLY_CACHE = "/tmp/cachetest"; - public static Set PROPERTY_SELF_PARTY= Sets.newHashSet();// + public static Set PROPERTY_SELF_PARTY = Sets.newHashSet();// - public static Integer PROPERTY_APPLY_EXPIRE_TIME = 3000; + public static Integer PROPERTY_APPLY_EXPIRE_TIME = 3000; public static Integer PROPERTY_COORDINATOR; public static Integer PROPERTY_SERVER_PORT; public static String PROPERTY_INFERENCE_SERVICE_NAME; @@ -95,7 +116,7 @@ public class MetaInfo { public static String PROPERTY_FLOW_RULE_TABLE; public static String PROPERTY_AUTH_FILE; - public static Boolean PROPERTY_ACL_ENABLE=false; + public static Boolean PROPERTY_ACL_ENABLE = false; public static String PROPERTY_ACL_USERNAME; public static String PROPERTY_ACL_PASSWORD; public static String PROPERTY_ROOT_PATH; @@ -117,49 +138,70 @@ public class MetaInfo { public static String INSTANCE_ID; public static String PROPERTY_EGGROLL_CLUSTER_MANANGER_IP; - public static Integer PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT; + public static Integer PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT; + public static Integer PROPERTY_CONSUME_SPIN_TIME = 500; - public static Integer PROPERTY_CONSUME_SPIN_TIME= 500; - - public static String PROPERTY_CLUSTER_MANAGER_ADDRESS; + public static String PROPERTY_CLUSTER_MANAGER_ADDRESS; public static Integer PROPERTY_NETTY_CLIENT_TIMEOUT = 3000; - public static Integer PROPERTY_HEARTBEAT_INTERVAL =10000; + public static Integer PROPERTY_HEARTBEAT_INTERVAL = 10000; - public static String PROPERTY_CLUSTER_MANAGER_HOST; + public static String PROPERTY_CLUSTER_MANAGER_HOST; public static Integer PROPERTY_CLUSTER_MANAGER_PORT; - public static Boolean PROPERTY_USE_ZOOKEEPER= true; + public static Boolean PROPERTY_USE_ZOOKEEPER = true; + + /** + * 从连接池中申请连接的超时时间 + */ + public static Integer HTTP_CLIENT_CONFIG_CONN_REQ_TIME_OUT; + /** + * 建立连接的超时时间 + */ + public static Integer HTTP_CLIENT_CONFIG_CONN_TIME_OUT; + /** + * 等待数据 + */ + public static Integer HTTP_CLIENT_CONFIG_SOCK_TIME_OUT; + public static Integer HTTP_CLIENT_INIT_POOL_MAX_TOTAL; + public static Integer HTTP_CLIENT_INIT_POOL_DEF_MAX_PER_ROUTE; + public static Integer HTTP_CLIENT_INIT_POOL_SOCK_TIME_OUT; + public static Integer HTTP_CLIENT_INIT_POOL_CONN_TIME_OUT; + public static Integer HTTP_CLIENT_INIT_POOL_CONN_REQ_TIME_OUT; + public static Integer HTTP_CLIENT_TRAN_CONN_REQ_TIME_OUT; + public static Integer HTTP_CLIENT_TRAN_CONN_TIME_OUT; + public static Integer HTTP_CLIENT_TRAN_SOCK_TIME_OUT; - public static String PROPERTY_DLEDGER_PEER; - public static String PROPERTY_DLEDGER_SELF; - public static String getClusterManagerHost(){ - if(PROPERTY_CLUSTER_MANAGER_HOST!=null){ - return PROPERTY_CLUSTER_MANAGER_HOST; - }else{ + + + public static String getClusterManagerHost() { + if (PROPERTY_CLUSTER_MANAGER_HOST != null) { + return PROPERTY_CLUSTER_MANAGER_HOST; + } else { PROPERTY_CLUSTER_MANAGER_HOST = PROPERTY_CLUSTER_MANAGER_ADDRESS.split(":")[0]; PROPERTY_CLUSTER_MANAGER_PORT = Integer.parseInt(PROPERTY_CLUSTER_MANAGER_ADDRESS.split(":")[1]); - return PROPERTY_CLUSTER_MANAGER_HOST; + return PROPERTY_CLUSTER_MANAGER_HOST; } } - public static Integer getClusterManagerPort(){ - if(PROPERTY_CLUSTER_MANAGER_PORT!=null){ - return PROPERTY_CLUSTER_MANAGER_PORT; - }else{ + + public static Integer getClusterManagerPort() { + if (PROPERTY_CLUSTER_MANAGER_PORT != null) { + return PROPERTY_CLUSTER_MANAGER_PORT; + } else { PROPERTY_CLUSTER_MANAGER_HOST = PROPERTY_CLUSTER_MANAGER_ADDRESS.split(":")[0]; PROPERTY_CLUSTER_MANAGER_PORT = Integer.parseInt(PROPERTY_CLUSTER_MANAGER_ADDRESS.split(":")[1]); - return PROPERTY_CLUSTER_MANAGER_PORT; + return PROPERTY_CLUSTER_MANAGER_PORT; } } - public static boolean isCluster(){ - return PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name()); + public static boolean isCluster() { + return PROPERTY_DEPLOY_MODE.equals(DeployMode.cluster.name()); } public static Map toMap() { diff --git a/java/osx/core/src/main/java/com/osx/core/constant/Dict.java b/java/osx/core/src/main/java/com/osx/core/constant/Dict.java index af644e5e46..a83a7e5dcf 100644 --- a/java/osx/core/src/main/java/com/osx/core/constant/Dict.java +++ b/java/osx/core/src/main/java/com/osx/core/constant/Dict.java @@ -16,6 +16,8 @@ package com.osx.core.constant; +import com.osx.core.config.MetaInfo; + public class Dict { public static final String ORIGIN_REQUEST = "origin_request"; @@ -24,6 +26,8 @@ public class Dict { public static final String NONE = "NONE"; public static final String GET_REMOTE_PARTY_RESULT = "getRemotePartyResult"; public static final String PORT = "port"; + public static final String HTTP_PORT ="http.port"; + public static final String INSTANCE_ID = "instanceId"; public static final String HIT_CACHE = "hitCache"; @@ -134,31 +138,44 @@ public class Dict { public static final String PROPERTY_PROXY_ASYNC_MAXSIZE = "proxy.async.maxsize"; public static final String PROPERTY_PROXY_GRPC_BATCH_INFERENCE_TIMEOUT = "proxy.grpc.batch.inference.timeout"; public static final String PROPERTY_MODEL_CACHE_PATH = "model.cache.path"; - public static final String PROPERTY_LR_USE_PARALLEL="lr.use.parallel"; + public static final String PROPERTY_LR_USE_PARALLEL = "lr.use.parallel"; public static final String PROPERTY_ALLOW_HEALTH_CHECK = "health.check.allow"; public static final String PROPERTY_TRANSFER_FILE_CACHE_SIZE = "transfer.file.cache.size"; public static final String PROPERTY_MAX_TRANSFER_CACHE_SIZE = "max.transfer.cache.size"; public static final String PROPERTY_USE_DIRECT_CACHE = "use.direct.cache"; public static final String PROPERTY_GRPC_ONCOMPLETED_WAIT_TIMEOUT = "grpc.oncompleted.wait.timeout"; - public static final String PROPERTY_USE_QUEUE_MODEL = "use.queue.model"; +// public static final String PROPERTY_USE_QUEUE_MODEL = "use.queue.model"; public static final String PROPERTY_STREAM_LIMIT_MODE = "stream.limit.mode"; public static final String PROPERTY_STREAM_LIMIT_MAX_TRY_TIME = "stream.limit.max.try.time"; - public static final String PROPERTY_GRPC_CHANNEL_MAX_CONCURRENT_CALL_PER_CONNECTION = "grpc.channel.max.concurrent.call.per.connection"; - public static final String PROPERTY_GRPC_CHANNEL_MAX_INBOUND_MESSAGE_SIZE = "grpc.channel.max.inbound.message.size"; - public static final String PROPERTY_GRPC_CHANNEL_MAX_INBOUND_METADATA_SIZE = "grpc.channel.max.inbound.metadata.size"; - public static final String PROPERTY_GRPC_CHANNEL_FLOW_CONTROL_WINDOW = "grpc.channel.flow.control.window"; - public static final String PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIME_SEC = "grpc.channel.keepalive.time.sec"; - public static final String PROPERTY_GRPC_CHANNEL_KEEPALIVE_TIMEOUT_SEC = "grpc.channel.keepalive.timeout.sec"; - public static final String PROPERTY_GRPC_CHANNEL_PERMIT_KEEPALIVE_TIME_SEC = "grpc.channel.permit.keepalive.time.sec"; - public static final String PROPERTY_GRPC_CHANNEL_KEEPALIVE_WITHOUT_CALLS_ENABLED = "grpc.channel.keepalive.without.calls.enabled"; - public static final String PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_IDLE_SEC = "grpc.channel.max.connection.idle.sec"; - public static final String PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_SEC = "grpc.channel.max.connection.age.sec"; - public static final String PROPERTY_GRPC_CHANNEL_MAX_CONNECTION_AGE_GRACE_SEC = "grpc.channel.max.connection.age.grace.sec"; + public static final String PROPERTY_GRPC_SERVER_MAX_CONCURRENT_CALL_PER_CONNECTION = "grpc.server.max.concurrent.call.per.connection"; + public static final String PROPERTY_GRPC_SERVER_MAX_INBOUND_MESSAGE_SIZE = "grpc.server.max.inbound.message.size"; + public static final String PROPERTY_GRPC_SERVER_MAX_INBOUND_METADATA_SIZE = "grpc.server.max.inbound.metadata.size"; + public static final String PROPERTY_GRPC_SERVER_FLOW_CONTROL_WINDOW = "grpc.server.flow.control.window"; + public static final String PROPERTY_GRPC_SERVER_KEEPALIVE_TIME_SEC = "grpc.server.keepalive.time.sec"; + public static final String PROPERTY_GRPC_SERVER_KEEPALIVE_TIMEOUT_SEC = "grpc.server.keepalive.timeout.sec"; + public static final String PROPERTY_GRPC_SERVER_PERMIT_KEEPALIVE_TIME_SEC = "grpc.server.permit.keepalive.time.sec"; + public static final String PROPERTY_GRPC_SERVER_KEEPALIVE_WITHOUT_CALLS_ENABLED = "grpc.server.keepalive.without.calls.enabled"; + public static final String PROPERTY_GRPC_SERVER_MAX_CONNECTION_IDLE_SEC = "grpc.server.max.connection.idle.sec"; + public static final String PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_SEC = "grpc.server.max.connection.age.sec"; + public static final String PROPERTY_GRPC_SERVER_MAX_CONNECTION_AGE_GRACE_SEC = "grpc.server.max.connection.age.grace.sec"; public static final String PROPERTY_INTERVAL_MS = "interval.ms"; public static final String PROPERTY_SAMPLE_COUNT = "sample.count"; public static final String PRPPERTY_QUEUE_MAX_FREE_TIME = "queue.max.free.time"; + public static final String HTTP_CLIENT_CONFIG_CONN_REQ_TIME_OUT = "httpclinet.config.connection.req.timeout"; + public static final String HTTP_CLIENT_CONFIG_CONN_TIME_OUT = "httpclient.config.connection.timeout"; + public static final String HTTP_CLIENT_CONFIG_SOCK_TIME_OUT = "httpclient.config.sockect.timeout"; + public static final String HTTP_CLIENT_INIT_POOL_MAX_TOTAL = "httpclient.init.pool.maxtotal"; + public static final String HTTP_CLIENT_INIT_POOL_DEF_MAX_PER_ROUTE = "httpclient.init.pool.def.max.pre.route"; + public static final String HTTP_CLIENT_INIT_POOL_SOCK_TIME_OUT = "httpclient.init.pool.sockect.timeout"; + public static final String HTTP_CLIENT_INIT_POOL_CONN_TIME_OUT = "httpclient.init.pool.connection.timeout"; + public static final String HTTP_CLIENT_INIT_POOL_CONN_REQ_TIME_OUT = "httpclient.init.pool.connection.req.timeout"; + public static final String HTTP_CLIENT_TRAN_CONN_REQ_TIME_OUT = "httpclient.tran.connection.req.timeout"; + public static final String HTTP_CLIENT_TRAN_CONN_TIME_OUT = "httpclient.tran.connection.timeout"; + public static final String HTTP_CLIENT_TRAN_SOCK_TIME_OUT = "httpclient.tran.sockect.timeout"; + + public static final String ACTION_TYPE_ASYNC_EXECUTE = "ASYNC_EXECUTE"; public static final String RET_CODE = "retcode"; @@ -205,8 +222,6 @@ public class Dict { public static final String ENCRYPT_TYPE = "encrypt_type"; - - public static final String CASE_ID = "caseid"; public static final String CODE = "code"; public static final String MESSAGE = "message"; @@ -227,10 +242,6 @@ public class Dict { public static final String BODY = "body"; - - - - public static final String SBT_TREE_NODE_ID_ARRAY = "sbtTreeNodeIdArray"; public static final String REMOTE_METHOD_BATCH = "batch"; @@ -239,7 +250,7 @@ public class Dict { public static final String REGISTER_ENVIRONMENT = "online"; public static final String SERVICE_FIREWORK = "firework"; - public static final String SERVICE_FIREWORK_CLUSTERMANAGER = "firework_cluster_manager"; + public static final String SERVICE_FIREWORK_CLUSTERMANAGER = "firework_cluster_manager"; public static final String SERVICE_PROXY = "proxy"; public static final String SERVICE_ADMIN = "admin"; public static final String FAILED = "failed"; @@ -252,8 +263,8 @@ public class Dict { public static final String PARAMS_SERVICE_ID = "service_id"; public static final String BATCH_INFERENCE_SPLIT_SIZE = "batch.inference.split.size"; public static final String WARN_LIST = "warnList"; - public static final String ERROR_LIST = "errorList"; - public static final String HEALTH_INFO = "healthInfo"; + public static final String ERROR_LIST = "errorList"; + public static final String HEALTH_INFO = "healthInfo"; public static final String PROPERTY_ADMIN_HEALTH_CHECK_TIME = "health.check.time"; @@ -272,119 +283,88 @@ public class Dict { public static final String TRANSFER_FATECLOUD_SECRET_INFO_URL = "transfer.fateCloud.secret.info.url"; public static final String TRANSFER_FATECLOUD_AUTHENTICATION_URL = "transfer.fateCloud.authentication.url"; public static final String NETTY_CHANNEL = "netty.channel"; - public static final String RESPONSE_STREAM_OBSERVER= "response.stream.observer"; + public static final String RESPONSE_STREAM_OBSERVER = "response.stream.observer"; - public static final String REQUEST_INDEX="request.index"; + public static final String REQUEST_INDEX = "request.index"; public static final String CURRENT_INDEX = "current.index"; public static final String TRANSFER_ID = "transferId"; public static final String TOPIC = "topic"; - public static final String PROPERTY_DEPLOY_MODE = "deploy.model"; - public static final String PROPERTY_CLUSTER_MANAGER_ADDRESS = "cluster.manager.address"; - - - public static final String PROPERTY_EGGROLL_CLUSTER_MANANGER_IP = "eggroll.cluster.manager.ip"; - public static final String PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT = "eggroll.cluster.manager.port"; - - - public static String PROPERTY_DLEDGER_PEER = "dledger.peer"; - public static String PROPERTY_DLEDGER_SELF = "dledger.self"; - - - - - - - public final static String UNKNOWN = "UNKNOWN"; - - public final static String PROTOBUF = "PROTOBUF"; - - public final static String SLASH = "/"; - - public final static String GRPC_PARSE_FROM = "parseFrom"; - - public final static String AT = "@"; - public final static String AND = "&"; - public final static String EQUAL = "="; - - public final static String DOLLAR = "$"; - - public final static String DOT = "."; - public final static String COLON = ":"; - public final static String SEMICOLON = ";"; - public final static String DASH = "-"; - public final static String UNDERLINE = "_"; - public final static String DOUBLE_UNDERLINES = "__"; - public final static String COMMA = ","; - public final static String HASH = "#"; + public static final String PROPERTY_DEPLOY_MODE = "deploy.model"; + public static final String PROPERTY_CLUSTER_MANAGER_ADDRESS = "cluster.manager.address"; + + + public static final String PROPERTY_EGGROLL_CLUSTER_MANANGER_IP = "eggroll.cluster.manager.ip"; + public static final String PROPERTY_EGGROLL_CLUSTER_MANANGER_PORT = "eggroll.cluster.manager.port"; + public final static String UNKNOWN = "UNKNOWN"; + public final static String PROTOBUF = "PROTOBUF"; + public final static String SLASH = "/"; + public final static String GRPC_PARSE_FROM = "parseFrom"; + public final static String AT = "@"; + public final static String AND = "&"; + public final static String EQUAL = "="; + public final static String DOLLAR = "$"; + public final static String DOT = "."; + public final static String COLON = ":"; + public final static String SEMICOLON = ";"; + public final static String DASH = "-"; + public final static String UNDERLINE = "_"; + public final static String DOUBLE_UNDERLINES = "__"; + public final static String COMMA = ","; + public final static String HASH = "#"; + public final static String META = "meta"; + public final static String SEND_START = "send_start"; // public final static String HOST = "host"; // public final static String PORT = "port"; - - public final static String META = "meta"; - public final static String SEND_START = "send_start"; - public final static String SEND_END = "send_end"; - - public final static String DEFAULT = "default"; - - public final static String ROLE_EGG = "egg"; - public final static String ROLE_ROLL = "roll"; - public final static String ROLE_EGGROLL = "eggroll"; - public final static String EGGROLL_COMPATIBLE_ENABLED = "eggroll.compatible.enabled"; - public final static String FALSE = "false"; - public final static String TRUE = "true"; - - public final static String CLUSTER_COMM = "__clustercomm__"; - public final static String FEDERATION = "__federation__"; - public final static String EGGROLL = "eggroll"; - - public final static String COMPUTING = "computing"; - public final static String STORAGE = "storage"; - - public final static String EMPTY = ""; - public final static String SPACE = " "; - - public final static String LOGGING_A_THROWABLE = "logging a Throwable"; - - public final static String ROUTE = "route"; - - public final static String NULL = "null"; - public final static String NULL_WITH_BRACKETS = "[${NULL}]"; - - public final static String LF = "\n"; - public final static String LFLF = "\n\n"; - - - - public final static String PATH = "path"; - public final static String TYPE = "type"; - public final static String SIZE = "size"; - - public final static String ROLL_PAIR = "rollpair"; - public final static String ROLL_FRAME = "rollframe"; - public final static String LMDB = "lmdb"; - public final static String LEVELDB = "leveldb"; - public final static String FILE = "file"; - public final static String HDFS = "hdfs"; - public final static String NETWORK = "network"; - public final static String CACHE = "cache"; - public final static String QUEUE = "queue"; - public final static String TOTAL = "total"; - - public final static String LOCALHOST = "localhost"; - - public final static String STORE_TYPE = "storeType"; - public final static String STORE_TYPE_SNAKECASE = "store_type"; - public final static String NAMESPACE = "namespace"; - public final static String NAME = "name"; - public final static String TOTAL_PARTITIONS = "totalPartitions"; - public final static String TOTAL_PARTITIONS_SNAKECASE = "total_partitions"; - public final static String PARTITION_ID_SNAKECASE = "partition_id"; - public final static String PARTITIONER = "partitioner"; - public final static String SERDES = "serdes"; - public final static String TRANSFER_BROKER_NAME = "transfer_broker_name"; - + public final static String SEND_END = "send_end"; + public final static String DEFAULT = "default"; + public final static String ROLE_EGG = "egg"; + public final static String ROLE_ROLL = "roll"; + public final static String ROLE_EGGROLL = "eggroll"; + public final static String EGGROLL_COMPATIBLE_ENABLED = "eggroll.compatible.enabled"; + public final static String FALSE = "false"; + public final static String TRUE = "true"; + public final static String CLUSTER_COMM = "__clustercomm__"; + public final static String FEDERATION = "__federation__"; + public final static String EGGROLL = "eggroll"; + public final static String COMPUTING = "computing"; + public final static String STORAGE = "storage"; + public final static String EMPTY = ""; + public final static String SPACE = " "; + public final static String LOGGING_A_THROWABLE = "logging a Throwable"; + public final static String ROUTE = "route"; + public final static String NULL = "null"; + public final static String NULL_WITH_BRACKETS = "[${NULL}]"; + public final static String LF = "\n"; + public final static String LFLF = "\n\n"; + public final static String PATH = "path"; + public final static String TYPE = "type"; + public final static String SIZE = "size"; + public final static String ROLL_PAIR = "rollpair"; + public final static String ROLL_FRAME = "rollframe"; + public final static String LMDB = "lmdb"; + public final static String LEVELDB = "leveldb"; + public final static String FILE = "file"; + public final static String HDFS = "hdfs"; + public final static String NETWORK = "network"; + public final static String CACHE = "cache"; + public final static String QUEUE = "queue"; + public final static String TOTAL = "total"; + public final static String LOCALHOST = "localhost"; + public final static String STORE_TYPE = "storeType"; + public final static String STORE_TYPE_SNAKECASE = "store_type"; + public final static String NAMESPACE = "namespace"; + public final static String NAME = "name"; + public final static String TOTAL_PARTITIONS = "totalPartitions"; + public final static String TOTAL_PARTITIONS_SNAKECASE = "total_partitions"; + public final static String PARTITION_ID_SNAKECASE = "partition_id"; + public final static String PARTITIONER = "partitioner"; + public final static String SERDES = "serdes"; + public final static String TRANSFER_BROKER_NAME = "transfer_broker_name"; + public static String PROPERTY_DLEDGER_PEER = "dledger.peer"; + public static String PROPERTY_DLEDGER_SELF = "dledger.self"; } diff --git a/java/osx/core/src/main/java/com/osx/core/constant/Protocol.java b/java/osx/core/src/main/java/com/osx/core/constant/Protocol.java index 7d41c1f048..b083d79dec 100644 --- a/java/osx/core/src/main/java/com/osx/core/constant/Protocol.java +++ b/java/osx/core/src/main/java/com/osx/core/constant/Protocol.java @@ -1,5 +1,5 @@ package com.osx.core.constant; public enum Protocol { - GRPC ,HTTP + GRPC, HTTP } diff --git a/java/osx/core/src/main/java/com/osx/core/constant/StatusCode.java b/java/osx/core/src/main/java/com/osx/core/constant/StatusCode.java index e1c537c978..4f05400004 100644 --- a/java/osx/core/src/main/java/com/osx/core/constant/StatusCode.java +++ b/java/osx/core/src/main/java/com/osx/core/constant/StatusCode.java @@ -22,14 +22,14 @@ public class StatusCode { public static final String NET_ERROR = "105"; public static final String SYSTEM_ERROR = "110"; public static final String PARAM_ERROR = "120"; - public static final String TRANSFER_APPLYINFO_SYNC_ERROR="129"; - public static final String PROXY_ROUTER_ERROR="130"; + public static final String TRANSFER_APPLYINFO_SYNC_ERROR = "129"; + public static final String PROXY_ROUTER_ERROR = "130"; public static final String PROXY_LOAD_ROUTER_TABLE_ERROR = "132"; public static final String PROXY_UPDATE_ROUTER_TABLE_ERROR = "133"; - public static final String INVALID_RESPONSE = "135"; + public static final String INVALID_RESPONSE = "135"; public static final String CONSUME_NO_MESSAGE = "136"; public static final String MESSAGE_PARSE_ERROR = "137"; public static final String TRANSFER_QUEUE_NOT_FIND = "138"; @@ -40,6 +40,4 @@ public class StatusCode { public static final String INVALID_INDEXFILE_DETAIL = "143"; - - } diff --git a/java/osx/core/src/main/java/com/osx/core/constant/TransferStatus.java b/java/osx/core/src/main/java/com/osx/core/constant/TransferStatus.java index fc9139626f..dcf002c1d5 100644 --- a/java/osx/core/src/main/java/com/osx/core/constant/TransferStatus.java +++ b/java/osx/core/src/main/java/com/osx/core/constant/TransferStatus.java @@ -1,5 +1,5 @@ package com.osx.core.constant; public enum TransferStatus { - INIT,TRANSFERING,ERROR,FINISH,DESTROY + INIT, TRANSFERING, ERROR, FINISH, DESTROY } diff --git a/java/osx/core/src/main/java/com/osx/core/context/Context.java b/java/osx/core/src/main/java/com/osx/core/context/Context.java index 7e7d6f1f7e..6c90a5df6b 100644 --- a/java/osx/core/src/main/java/com/osx/core/context/Context.java +++ b/java/osx/core/src/main/java/com/osx/core/context/Context.java @@ -15,9 +15,9 @@ */ import com.google.common.collect.Maps; +import com.google.common.util.concurrent.ListenableFuture; import com.osx.core.constant.Dict; import com.osx.core.router.RouterInfo; -import com.google.common.util.concurrent.ListenableFuture; import com.osx.core.utils.FlowLogPrinter; import com.osx.core.utils.FlowLogUtil; import org.apache.commons.lang3.StringUtils; @@ -34,40 +34,22 @@ public class Context { private static final Logger logger = LoggerFactory.getLogger(LOGGER_NAME); protected long timestamp = System.currentTimeMillis(); - public Context(long timestamp,Map dataMap){ - - } - - public Context(){} + protected boolean needAssembleException = false; + protected String actionType; // public BaseContext setNeedAssembleException(boolean needAssembleException) { // this.needAssembleException = needAssembleException; // return this; // } - - protected boolean needAssembleException= false; - protected String actionType; protected String sessionId; - protected boolean needPrintFlowLog=true; - - - public long getDataSize() { - return dataSize; - } - - - public String getTopic( ){ - if( dataMap.get(Dict.TOPIC)!=null) - return dataMap.get(Dict.TOPIC).toString(); - return null; - } - - public void setDataSize(long dataSize) { - this.dataSize = dataSize; - } - - protected long dataSize; + protected boolean needPrintFlowLog = true; + protected Long dataSize; + protected Map dataMap = Maps.newHashMap(); + long costTime; + String resourceName; + Throwable t; + FlowLogPrinter flowLogPrinter = FlowLogUtil::printFlowLog; // public String getTransferId() { // if( dataMap.get(Dict.TRANSFER_ID)!=null) @@ -75,49 +57,57 @@ public void setDataSize(long dataSize) { // return null; // } - public void setTopic(String topic) { - this.dataMap.put(Dict.TOPIC,topic); - } + public Context(long timestamp, Map dataMap) { + } - public String getInstanceId() { - return (String)dataMap.get(Dict.INSTANCE_ID); + public Context() { } // public void setOffset(long offset){ // dataMap.put(Dict.ST); // } - - public void setInstanceId(String instanceId) { - this.dataMap.put(Dict.INSTANCE_ID,instanceId); + public Long getDataSize() { + return dataSize; } + public void setDataSize(long dataSize) { + this.dataSize = dataSize; + } - protected Map dataMap = Maps.newHashMap(); - long costTime; - String resourceName; - Throwable t; + public String getTopic() { + if (dataMap.get(Dict.TOPIC) != null) + return dataMap.get(Dict.TOPIC).toString(); + return null; + } - public Throwable getException() { - return t; + public void setTopic(String topic) { + this.dataMap.put(Dict.TOPIC, topic); } + public String getInstanceId() { + return (String) dataMap.get(Dict.INSTANCE_ID); - public void setSessionId(String sessionId) { - this.sessionId = sessionId; } + public void setInstanceId(String instanceId) { + this.dataMap.put(Dict.INSTANCE_ID, instanceId); + } - public String getSessionId() { - return this.sessionId; + public Throwable getException() { + return t; } - public void setException(Throwable t) { + public void setException(Throwable t) { this.t = t; } + public String getSessionId() { + return this.sessionId; + } + // public BaseContext() { // timestamp = System.currentTimeMillis(); // } @@ -132,12 +122,14 @@ public void setException(Throwable t) { // this.dataMap = dataMap; // } + public void setSessionId(String sessionId) { + this.sessionId = sessionId; + } public String getActionType() { return actionType; } - public void setActionType(String actionType) { this.actionType = actionType; } @@ -166,111 +158,93 @@ public void setCaseId(String caseId) { dataMap.put(Dict.CASEID, caseId); } - public long getTimeStamp() { return timestamp; } - - - - - - public Context subContext() { Map newDataMap = Maps.newHashMap(dataMap); return new Context(this.timestamp, newDataMap); } - - - public boolean needPrintFlowLog() { return needPrintFlowLog; } - public void setNeedPrintFlowLog(boolean needPrintFlowLog) { this.needPrintFlowLog = needPrintFlowLog; } - - public Long getRequestMsgIndex() { - return (Long)this.dataMap.get(Dict.REQUEST_INDEX); + return (Long) this.dataMap.get(Dict.REQUEST_INDEX); } - public Long getCurrentMsgIndex() { - return (Long)this.dataMap.get(Dict.CURRENT_INDEX); + public void setRequestMsgIndex(Long index) { + this.dataMap.put(Dict.REQUEST_INDEX, index); } - - public void setRequestMsgIndex(Long index) { - this.dataMap.put(Dict.REQUEST_INDEX,index); + public Long getCurrentMsgIndex() { + return (Long) this.dataMap.get(Dict.CURRENT_INDEX); } public void setCurrentMsgIndex(Long index) { - this.dataMap.put(Dict.CURRENT_INDEX,index); + this.dataMap.put(Dict.CURRENT_INDEX, index); } - public long getCostTime() { return costTime; } - - public String getSrcPartyId() { - return (String)dataMap.get(Dict.SOURCE_PARTY_ID); + return (String) dataMap.get(Dict.SOURCE_PARTY_ID); } - public void setSrcPartyId(String guestAppId) { - dataMap.put(Dict.SOURCE_PARTY_ID,guestAppId); + dataMap.put(Dict.SOURCE_PARTY_ID, guestAppId); } - public String getDesPartyId() { return (String) dataMap.get(Dict.DES_PARTY_ID); } - public void setDesPartyId(String hostAppid) { - dataMap.put(Dict.DES_PARTY_ID,hostAppid); + dataMap.put(Dict.DES_PARTY_ID, hostAppid); } - - public RouterInfo getRouterInfo() { return (RouterInfo) dataMap.get(Dict.ROUTER_INFO); } - public void setRouterInfo(RouterInfo routerInfo) { dataMap.put(Dict.ROUTER_INFO, routerInfo); } - public Object getResultData() { return dataMap.get(Dict.RESULT_DATA); } - public void setResultData(Object resultData) { dataMap.put(Dict.RESULT_DATA, resultData); } - public String getReturnCode() { return (String) dataMap.get(Dict.RETURN_CODE); } - public void setReturnCode(String returnCode) { dataMap.put(Dict.RETURN_CODE, returnCode); } + public String getReturnMsg() { + return (String) dataMap.get(Dict.RET_MSG); + } + + public void setReturnMsg(String returnMsg) { + dataMap.put(Dict.RET_MSG, returnMsg); + } + + public long getDownstreamCost() { if (dataMap.get(Dict.DOWN_STREAM_COST) != null) { @@ -279,61 +253,46 @@ public long getDownstreamCost() { return 0; } - public void setDownstreamCost(long downstreamCost) { dataMap.put(Dict.DOWN_STREAM_COST, downstreamCost); } - public long getDownstreamBegin() { - return dataMap.get(Dict.DOWN_STREAM_BEGIN)!=null? (long) dataMap.get(Dict.DOWN_STREAM_BEGIN):0; + return dataMap.get(Dict.DOWN_STREAM_BEGIN) != null ? (long) dataMap.get(Dict.DOWN_STREAM_BEGIN) : 0; } - public void setDownstreamBegin(long downstreamBegin) { dataMap.put(Dict.DOWN_STREAM_BEGIN, downstreamBegin); } - public String getSourceIp() { return (String) dataMap.get(Dict.SOURCE_IP); } - public void setSourceIp(String sourceIp) { dataMap.put(Dict.SOURCE_IP, sourceIp); } - public String getServiceName() { return (String) dataMap.get(Dict.SERVICE_NAME); } - public void setServiceName(String serviceName) { dataMap.put(Dict.SERVICE_NAME, serviceName); } - public String getCallName() { return (String) dataMap.get(Dict.CALL_NAME); } - public void setCallName(String callName) { dataMap.put(Dict.CALL_NAME, callName); } - - - - - public void setRemoteFuture(ListenableFuture future) { this.dataMap.put(Dict.FUTURE, future); } - public String getResourceName() { if (StringUtils.isNotEmpty(resourceName)) { return resourceName; @@ -343,7 +302,6 @@ public String getResourceName() { return resourceName; } - public boolean needAssembleException() { return needAssembleException; } @@ -357,17 +315,11 @@ public Context setFlowLogPrinter(FlowLogPrinter flowLogPrinter) { return this; } - FlowLogPrinter flowLogPrinter = FlowLogUtil::printFlowLog; - - public void printFlowLog() { - if(needPrintFlowLog) { + if (needPrintFlowLog) { flowLogPrinter.print(this); } } - - - } diff --git a/java/osx/core/src/main/java/com/osx/core/datasource/AbstractDataSource.java b/java/osx/core/src/main/java/com/osx/core/datasource/AbstractDataSource.java index cf86e79bc2..fef8c55f1a 100644 --- a/java/osx/core/src/main/java/com/osx/core/datasource/AbstractDataSource.java +++ b/java/osx/core/src/main/java/com/osx/core/datasource/AbstractDataSource.java @@ -1,5 +1,5 @@ - package com.osx.core.datasource; + import com.osx.core.flow.DynamicProperty; import com.osx.core.flow.Property; diff --git a/java/osx/core/src/main/java/com/osx/core/datasource/AutoRefreshDataSource.java b/java/osx/core/src/main/java/com/osx/core/datasource/AutoRefreshDataSource.java index 10a18d177d..a7c022ff6e 100644 --- a/java/osx/core/src/main/java/com/osx/core/datasource/AutoRefreshDataSource.java +++ b/java/osx/core/src/main/java/com/osx/core/datasource/AutoRefreshDataSource.java @@ -1,4 +1,3 @@ - package com.osx.core.datasource; import org.slf4j.Logger; @@ -11,10 +10,9 @@ public abstract class AutoRefreshDataSource extends AbstractDataSource { + protected long recommendRefreshMs = 3000; Logger logger = LoggerFactory.getLogger(AutoRefreshDataSource.class); - private ScheduledExecutorService service; - protected long recommendRefreshMs = 3000; public AutoRefreshDataSource(Converter configParser) { super(configParser); @@ -33,7 +31,7 @@ public AutoRefreshDataSource(Converter configParser, final long recommendR @SuppressWarnings("PMD.ThreadPoolCreationRule") private void startTimerService() { service = Executors.newScheduledThreadPool(1, - new NamedThreadFactory("sentinel-datasource-auto-refresh-task", true)); + new NamedThreadFactory("sentinel-datasource-auto-refresh-task", true)); service.scheduleAtFixedRate(new Runnable() { @Override public void run() { diff --git a/java/osx/core/src/main/java/com/osx/core/datasource/FileRefreshableDataSource.java b/java/osx/core/src/main/java/com/osx/core/datasource/FileRefreshableDataSource.java index 51cca9b487..25d8ae5aaf 100644 --- a/java/osx/core/src/main/java/com/osx/core/datasource/FileRefreshableDataSource.java +++ b/java/osx/core/src/main/java/com/osx/core/datasource/FileRefreshableDataSource.java @@ -11,19 +11,16 @@ import java.nio.charset.Charset; - public class FileRefreshableDataSource extends AutoRefreshDataSource { - Logger logger = LoggerFactory.getLogger(FileRefreshableDataSource.class); private static final int MAX_SIZE = 1024 * 1024 * 4; private static final long DEFAULT_REFRESH_MS = 3000; private static final int DEFAULT_BUF_SIZE = 1024 * 1024; private static final Charset DEFAULT_CHAR_SET = Charset.forName("utf-8"); - - private byte[] buf; private final Charset charset; private final File file; - + Logger logger = LoggerFactory.getLogger(FileRefreshableDataSource.class); + private byte[] buf; private long lastModified = 0L; @@ -36,12 +33,12 @@ public FileRefreshableDataSource(String fileName, Converter configPar } public FileRefreshableDataSource(File file, Converter configParser, int bufSize) - throws FileNotFoundException { + throws FileNotFoundException { this(file, configParser, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET); } public FileRefreshableDataSource(File file, Converter configParser, Charset charset) - throws FileNotFoundException { + throws FileNotFoundException { this(file, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset); } @@ -84,15 +81,15 @@ public String readSource() throws Exception { try { inputStream = new FileInputStream(file); FileChannel channel = inputStream.getChannel(); - logger.info("file channel size {}",channel.size()); + logger.info("file channel size {}", channel.size()); if (channel.size() > buf.length) { throw new IllegalStateException(file.getAbsolutePath() + " file size=" + channel.size() - + ", is bigger than bufSize=" + buf.length + ". Can't read"); + + ", is bigger than bufSize=" + buf.length + ". Can't read"); } int len = inputStream.read(buf); - if(len>0) { + if (len > 0) { return new String(buf, 0, len, charset); - }else{ + } else { return ""; } } finally { diff --git a/java/osx/core/src/main/java/com/osx/core/datasource/NamedThreadFactory.java b/java/osx/core/src/main/java/com/osx/core/datasource/NamedThreadFactory.java index d372b50eda..d925390756 100644 --- a/java/osx/core/src/main/java/com/osx/core/datasource/NamedThreadFactory.java +++ b/java/osx/core/src/main/java/com/osx/core/datasource/NamedThreadFactory.java @@ -15,7 +15,7 @@ public NamedThreadFactory(String namePrefix, boolean daemon) { this.daemon = daemon; SecurityManager s = System.getSecurityManager(); group = (s != null) ? s.getThreadGroup() : - Thread.currentThread().getThreadGroup(); + Thread.currentThread().getThreadGroup(); this.namePrefix = namePrefix; } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/AckIndexException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/AckIndexException.java index 1b224054be..9176969cc1 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/AckIndexException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/AckIndexException.java @@ -2,13 +2,13 @@ import com.osx.core.constant.StatusCode; -public class AckIndexException extends BaseException{ - public AckIndexException(){ - super(StatusCode.ACK_INDEX_ERROR,"ACK_INDEX_ERROR"); +public class AckIndexException extends BaseException { + public AckIndexException() { + super(StatusCode.ACK_INDEX_ERROR, "ACK_INDEX_ERROR"); } - public AckIndexException(String msg){ - super(StatusCode.ACK_INDEX_ERROR,msg); + public AckIndexException(String msg) { + super(StatusCode.ACK_INDEX_ERROR, msg); } } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumeNoMessageException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumeNoMessageException.java index 47a214b235..8f0d820086 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumeNoMessageException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumeNoMessageException.java @@ -2,12 +2,13 @@ import com.osx.core.constant.StatusCode; -public class ConsumeNoMessageException extends BaseException{ +public class ConsumeNoMessageException extends BaseException { - public ConsumeNoMessageException(String code ,String msg){ - super(code,msg); + public ConsumeNoMessageException(String code, String msg) { + super(code, msg); } - public ConsumeNoMessageException(String msg){ - super(StatusCode.CONSUME_NO_MESSAGE,msg); + + public ConsumeNoMessageException(String msg) { + super(StatusCode.CONSUME_NO_MESSAGE, msg); } } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumerNotExistException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumerNotExistException.java index 71d4697576..a895b47b7b 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumerNotExistException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/ConsumerNotExistException.java @@ -2,13 +2,14 @@ import com.osx.core.constant.StatusCode; -public class ConsumerNotExistException extends BaseException{ +public class ConsumerNotExistException extends BaseException { - public ConsumerNotExistException(String msg){ - super(StatusCode.CONSUMER_NOT_EXIST,msg); + public ConsumerNotExistException(String msg) { + super(StatusCode.CONSUMER_NOT_EXIST, msg); } - public ConsumerNotExistException(String code ,String msg){ - super(code,msg); + + public ConsumerNotExistException(String code, String msg) { + super(code, msg); } } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/ErrorMessageUtil.java b/java/osx/core/src/main/java/com/osx/core/exceptions/ErrorMessageUtil.java index b0d682b2f1..af483b2f8b 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/ErrorMessageUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/ErrorMessageUtil.java @@ -19,9 +19,6 @@ import com.osx.core.constant.Dict; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; -import com.osx.core.service.AbstractServiceAdaptor; - - import io.grpc.Status; import io.grpc.StatusRuntimeException; import org.slf4j.Logger; @@ -68,8 +65,8 @@ public static String getLocalExceptionCode(Exception e) { return retcode; } - public static StatusRuntimeException throwableToException(Context context, Throwable throwable){ - if(throwable instanceof StatusRuntimeException ){ + public static StatusRuntimeException throwableToException(Context context, Throwable throwable) { + if (throwable instanceof StatusRuntimeException) { return (StatusRuntimeException) throwable; } /** @@ -80,7 +77,7 @@ public static StatusRuntimeException throwableToException(Context context, Throw } - public static ExceptionInfo handleExceptionExceptionInfo(Context context , Throwable e) { + public static ExceptionInfo handleExceptionExceptionInfo(Context context, Throwable e) { ExceptionInfo exceptionInfo = new ExceptionInfo(); if (e instanceof BaseException) { BaseException baseException = (BaseException) e; @@ -91,7 +88,7 @@ public static ExceptionInfo handleExceptionExceptionInfo(Context context , Throw exceptionInfo.setMessage(e.getMessage()); } exceptionInfo.setThrowable(e); - if(context.needAssembleException()) { + if (context.needAssembleException()) { exceptionInfo.setThrowable(throwableToException(context, e)); } return exceptionInfo; diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/ExceptionInfo.java b/java/osx/core/src/main/java/com/osx/core/exceptions/ExceptionInfo.java index aa3af12fe1..59b2e5be59 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/ExceptionInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/ExceptionInfo.java @@ -1,27 +1,29 @@ package com.osx.core.exceptions; +import com.google.common.collect.Maps; +import com.osx.core.constant.Dict; import com.osx.core.utils.JsonUtil; -public class ExceptionInfo { +import java.util.Map; +public class ExceptionInfo { - String code; - String message; - - public Throwable getThrowable() { - return throwable; - } - public void setThrowable(Throwable throwable) { - this.throwable = throwable; - } + String code; + String message; + Throwable throwable; - Throwable throwable; + public ExceptionInfo() { - public ExceptionInfo() { + } - } + public Throwable getThrowable() { + return throwable; + } + public void setThrowable(Throwable throwable) { + this.throwable = throwable; + } public String getCode() { return code; @@ -31,15 +33,18 @@ public void setCode(String code) { this.code = code; } - public String getMessage() { - return message != null ? message : ""; - } + public String getMessage() { + return message != null ? message : ""; + } - public void setMessage(String message) { - this.message = message; - } + public void setMessage(String message) { + this.message = message; + } - public String toString(){ - return JsonUtil.object2Json(this); - } - } \ No newline at end of file + public String toString() { + Map data = Maps.newHashMap(); + data.put(Dict.CODE,code); + data.put(Dict.MESSAGE,message); + return JsonUtil.object2Json(data); + } +} \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/InvalidRedirectInfoException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/InvalidRedirectInfoException.java index 90289d9eef..67c690deb9 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/InvalidRedirectInfoException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/InvalidRedirectInfoException.java @@ -2,8 +2,8 @@ import com.osx.core.constant.StatusCode; -public class InvalidRedirectInfoException extends BaseException{ - public InvalidRedirectInfoException(){ - super(StatusCode.INVALID_REDIRECT_INFO,"redirect Info is invalid"); +public class InvalidRedirectInfoException extends BaseException { + public InvalidRedirectInfoException() { + super(StatusCode.INVALID_REDIRECT_INFO, "redirect Info is invalid"); } } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/MappedFileException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/MappedFileException.java index ce4df3c401..ffe3fe5626 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/MappedFileException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/MappedFileException.java @@ -1,4 +1,4 @@ package com.osx.core.exceptions; -public class MappedFileException extends BaseException{ +public class MappedFileException extends BaseException { } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/MessageParseException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/MessageParseException.java index 6ade161d36..388bfd74c9 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/MessageParseException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/MessageParseException.java @@ -2,8 +2,8 @@ import com.osx.core.constant.StatusCode; -public class MessageParseException extends BaseException{ - public MessageParseException(String msg){ - super(StatusCode.MESSAGE_PARSE_ERROR,msg); +public class MessageParseException extends BaseException { + public MessageParseException(String msg) { + super(StatusCode.MESSAGE_PARSE_ERROR, msg); } } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/NoRouterInfoException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/NoRouterInfoException.java index 6ee1fa37ab..55e0a4d738 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/NoRouterInfoException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/NoRouterInfoException.java @@ -3,9 +3,9 @@ import com.osx.core.constant.StatusCode; -public class NoRouterInfoException extends BaseException{ +public class NoRouterInfoException extends BaseException { - public NoRouterInfoException(String msg){ - super(StatusCode.PROXY_ROUTER_ERROR,msg); + public NoRouterInfoException(String msg) { + super(StatusCode.PROXY_ROUTER_ERROR, msg); } } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/ProduceMsgExcption.java b/java/osx/core/src/main/java/com/osx/core/exceptions/ProduceMsgExcption.java index 32cd695358..784ecdeb10 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/ProduceMsgExcption.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/ProduceMsgExcption.java @@ -1,4 +1,4 @@ package com.osx.core.exceptions; -public class ProduceMsgExcption extends BaseException{ +public class ProduceMsgExcption extends BaseException { } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/PutMessageException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/PutMessageException.java index 7211c9bda9..fe175878bd 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/PutMessageException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/PutMessageException.java @@ -2,7 +2,7 @@ import com.osx.core.constant.StatusCode; -public class PutMessageException extends BaseException{ +public class PutMessageException extends BaseException { public PutMessageException(String retCode, String message) { super(retCode, message); diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueAlreadyExistException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueAlreadyExistException.java index 32fed344ce..9e95cfdfed 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueAlreadyExistException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueAlreadyExistException.java @@ -1,4 +1,4 @@ package com.osx.core.exceptions; -public class TransferQueueAlreadyExistException extends BaseException{ +public class TransferQueueAlreadyExistException extends BaseException { } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueInvalidStatusException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueInvalidStatusException.java index 10859e602d..104c69ed1d 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueInvalidStatusException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueInvalidStatusException.java @@ -2,12 +2,11 @@ import com.osx.core.constant.StatusCode; -public class TransferQueueInvalidStatusException extends BaseException { +public class TransferQueueInvalidStatusException extends BaseException { - public TransferQueueInvalidStatusException(String msg){ - super(StatusCode.QUEUE_INVALID_STATUS,msg); + public TransferQueueInvalidStatusException(String msg) { + super(StatusCode.QUEUE_INVALID_STATUS, msg); } - } diff --git a/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueNotExistException.java b/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueNotExistException.java index d3711bac60..3d2f82e994 100644 --- a/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueNotExistException.java +++ b/java/osx/core/src/main/java/com/osx/core/exceptions/TransferQueueNotExistException.java @@ -2,12 +2,12 @@ import com.osx.core.constant.StatusCode; -public class TransferQueueNotExistException extends BaseException{ - public TransferQueueNotExistException(){ - super(StatusCode.TRANSFER_QUEUE_NOT_FIND,"TRANSFER_QUEUE_NOT_FIND"); +public class TransferQueueNotExistException extends BaseException { + public TransferQueueNotExistException() { + super(StatusCode.TRANSFER_QUEUE_NOT_FIND, "TRANSFER_QUEUE_NOT_FIND"); } - public TransferQueueNotExistException(String code ,String msg){ - super(code,msg); + public TransferQueueNotExistException(String code, String msg) { + super(code, msg); } } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/AbstractRule.java b/java/osx/core/src/main/java/com/osx/core/flow/AbstractRule.java index 26e9a90c09..8f7b74937b 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/AbstractRule.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/AbstractRule.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; public abstract class AbstractRule implements Rule { @@ -71,7 +70,7 @@ public AbstractRule setLimitApp(String limitApp) { // } public T as(Class clazz) { - return (T)this; + return (T) this; } // @Override diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowChecker.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowChecker.java index 337410e9f2..c64eb9fbd5 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowChecker.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowChecker.java @@ -1,15 +1,15 @@ package com.osx.core.flow; - - - import com.osx.core.token.TokenResult; import com.osx.core.token.TokenResultStatus; final public class ClusterFlowChecker { + private ClusterFlowChecker() { + } + private static double calcGlobalThreshold(FlowRule rule) { double count = rule.getCount(); // switch (rule.getClusterConfig().getThresholdType()) { @@ -21,7 +21,7 @@ private static double calcGlobalThreshold(FlowRule rule) { // return count * connectedCount; // } - return count; + return count; } static boolean allowProceed(long flowId) { @@ -42,7 +42,7 @@ static public TokenResult acquireClusterToken(/*@Valid*/ FlowRule rule, int acqu } double latestQps = metric.getAvg(ClusterFlowEvent.PASS); - double globalThreshold = calcGlobalThreshold(rule) ; + double globalThreshold = calcGlobalThreshold(rule); double nextRemaining = globalThreshold - latestQps - acquireCount; if (nextRemaining >= 0) { @@ -55,8 +55,8 @@ static public TokenResult acquireClusterToken(/*@Valid*/ FlowRule rule, int acqu } // Remaining count is cut down to a smaller integer. return new TokenResult(TokenResultStatus.OK) - .setRemaining((int) nextRemaining) - .setWaitInMs(0); + .setRemaining((int) nextRemaining) + .setWaitInMs(0); } else { if (prioritized) { // Try to occupy incoming buckets. @@ -67,8 +67,8 @@ static public TokenResult acquireClusterToken(/*@Valid*/ FlowRule rule, int acqu if (waitInMs > 0) { //ClusterServerStatLogUtil.log("flow|waiting|" + id); return new TokenResult(TokenResultStatus.SHOULD_WAIT) - .setRemaining(0) - .setWaitInMs(waitInMs); + .setRemaining(0) + .setWaitInMs(waitInMs); } // Or else occupy failed, should be blocked. } @@ -76,24 +76,22 @@ static public TokenResult acquireClusterToken(/*@Valid*/ FlowRule rule, int acqu // Blocked. metric.add(ClusterFlowEvent.BLOCK, acquireCount); metric.add(ClusterFlowEvent.BLOCK_REQUEST, 1); - // ClusterServerStatLogUtil.log("flow|block|" + id, acquireCount); - // ClusterServerStatLogUtil.log("flow|block_request|" + id, 1); + // ClusterServerStatLogUtil.log("flow|block|" + id, acquireCount); + // ClusterServerStatLogUtil.log("flow|block_request|" + id, 1); if (prioritized) { // Add prioritized block. metric.add(ClusterFlowEvent.OCCUPIED_BLOCK, acquireCount); - // ClusterServerStatLogUtil.log("flow|occupied_block|" + id, 1); + // ClusterServerStatLogUtil.log("flow|occupied_block|" + id, 1); } return blockedResult(rule); } } - private static TokenResult blockedResult(FlowRule rule ){ + private static TokenResult blockedResult(FlowRule rule) { return new TokenResult(TokenResultStatus.BLOCKED) - .setRemaining(0) - .setWaitInMs(0) + .setRemaining(0) + .setWaitInMs(0) .setWaitInMs(rule.getMaxQueueingTimeMs()); } - - private ClusterFlowChecker() {} } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowRuleManager.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowRuleManager.java index 3603835a91..de3c8bb049 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowRuleManager.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterFlowRuleManager.java @@ -20,8 +20,6 @@ public final class ClusterFlowRuleManager { - static Logger logger = LoggerFactory.getLogger(ClusterFlowRuleManager.class); - /** * The default cluster flow rule property supplier that creates a new dynamic property * for a specific namespace to do rule management manually. @@ -33,7 +31,6 @@ public Property> apply(String namespace) { return new DynamicProperty<>(); } }; - /** * (flowId, clusterRule) */ @@ -41,7 +38,7 @@ public Property> apply(String namespace) { /** * */ - private static final Map RESOURCE_RULES = new ConcurrentHashMap<>(); + private static final Map RESOURCE_RULES = new ConcurrentHashMap<>(); /** * (namespace, [flowId...]) */ @@ -55,33 +52,35 @@ public Property> apply(String namespace) { * */ private static final Map FLOW_NAMESPACE_MAP = new ConcurrentHashMap<>(); - /** * (namespace, property-listener wrapper) */ private static final Map> PROPERTY_MAP = new ConcurrentHashMap<>(); + private static final Object UPDATE_LOCK = new Object(); + static Logger logger = LoggerFactory.getLogger(ClusterFlowRuleManager.class); /** * Cluster flow rule property supplier for a specific namespace. */ private static volatile Function>> propertySupplier = DEFAULT_PROPERTY_SUPPLIER; - private static final Object UPDATE_LOCK = new Object(); - static { initDefaultProperty(); } + public ClusterFlowRuleManager() { + } + private static void initDefaultProperty() { logger.info("ClusterFlowRuleManager initDefaultProperty"); Property> defaultProperty = new DynamicProperty<>(); String defaultNamespace = "default"; PropertyListener> listener = new FlowRulePropertyListener(defaultNamespace); - registerPropertyInternal(defaultNamespace, defaultProperty,listener); + registerPropertyInternal(defaultNamespace, defaultProperty, listener); String currentPath = null; - if( MetaInfo.PROPERTY_FLOW_RULE_TABLE!=null){ + if (MetaInfo.PROPERTY_FLOW_RULE_TABLE != null) { currentPath = MetaInfo.PROPERTY_FLOW_RULE_TABLE; - }else { + } else { URL url = Thread.currentThread().getContextClassLoader().getResource("flowRule.json"); if (url != null) { @@ -91,7 +90,7 @@ private static void initDefaultProperty() { } } logger.info("load flow rule {}", currentPath); - if(currentPath!=null) { + if (currentPath != null) { File confFile = new File(currentPath); FileRefreshableDataSource fileRefreshableDataSource = null; try { @@ -99,8 +98,8 @@ private static void initDefaultProperty() { List content = JsonUtil.json2List((String) source, new TypeReference>() { }); - logger.info("load flow rule content {}",content); - return content; + logger.info("load flow rule content {}", content); + return content; }); fileRefreshableDataSource.getProperty().addListener(listener); } catch (FileNotFoundException e) { @@ -179,7 +178,7 @@ private static void registerPropertyInternal(/*@NonNull*/ String namespace, /*@V private static void registerPropertyInternal(/*@NonNull*/ String namespace, /*@Valid*/ Property> property, PropertyListener listener - ) { + ) { NamespaceFlowProperty oldProperty = PROPERTY_MAP.get(namespace); if (oldProperty != null) { oldProperty.getProperty().removeListener(oldProperty.getListener()); @@ -237,14 +236,13 @@ public static FlowRule getFlowRuleById(Long id) { return FLOW_RULES.get(id); } - public static FlowRule getFlowRuleByResource(String resource){ + public static FlowRule getFlowRuleByResource(String resource) { return RESOURCE_RULES.get(resource); } - - public static Map getResourceRules(){ - return RESOURCE_RULES; + public static Map getResourceRules() { + return RESOURCE_RULES; } public static Set getFlowIdSet(String namespace) { @@ -304,6 +302,23 @@ private static void resetNamespaceFlowIdMapFor(/*@Valid*/ String namespace) { NAMESPACE_FLOW_ID_MAP.put(namespace, new HashSet()); } +// private static void clearAndResetRulesConditional(/*@Valid*/ String namespace, Predicate predicate) { +// Set oldIdSet = NAMESPACE_FLOW_ID_MAP.get(namespace); +// if (oldIdSet != null && !oldIdSet.isEmpty()) { +// for (Long flowId : oldIdSet) { +// if (predicate.test(flowId)) { +// FLOW_RULES.remove(flowId); +// FLOW_NAMESPACE_MAP.remove(flowId); +// ClusterMetricStatistics.removeMetric(flowId); +// if (CurrentConcurrencyManager.containsFlowId(flowId)) { +// CurrentConcurrencyManager.remove(flowId); +// } +// } +// } +// oldIdSet.clear(); +// } +// } + /** * Clear all rules of the provided namespace and reset map. * @@ -325,23 +340,6 @@ private static void clearAndResetRulesFor(/*@Valid*/ String namespace) { } } -// private static void clearAndResetRulesConditional(/*@Valid*/ String namespace, Predicate predicate) { -// Set oldIdSet = NAMESPACE_FLOW_ID_MAP.get(namespace); -// if (oldIdSet != null && !oldIdSet.isEmpty()) { -// for (Long flowId : oldIdSet) { -// if (predicate.test(flowId)) { -// FLOW_RULES.remove(flowId); -// FLOW_NAMESPACE_MAP.remove(flowId); -// ClusterMetricStatistics.removeMetric(flowId); -// if (CurrentConcurrencyManager.containsFlowId(flowId)) { -// CurrentConcurrencyManager.remove(flowId); -// } -// } -// } -// oldIdSet.clear(); -// } -// } - /** * Get connected count for associated namespace of given {@code flowId}. * @@ -358,7 +356,6 @@ private static void clearAndResetRulesFor(/*@Valid*/ String namespace) { // } // return ConnectionManager.getConnectedCount(namespace); // } - public static String getNamespace(long flowId) { return FLOW_NAMESPACE_MAP.get(flowId); } @@ -373,11 +370,11 @@ private static void applyClusterFlowRule(List list, /*@Valid*/ String Set flowIdSet = new HashSet<>(); for (FlowRule rule : list) { - System.err.println("==================="+rule); + System.err.println("===================" + rule); if (!rule.isClusterMode()) { continue; } - RESOURCE_RULES.put(rule.getResource(),rule); + RESOURCE_RULES.put(rule.getResource(), rule); // if (!FlowRuleUtil.isValidRule(rule)) { //// RecordLog.warn( //// "[ClusterFlowRuleManager] Ignoring invalid flow rule when loading new flow rules: " + rule); @@ -388,7 +385,7 @@ private static void applyClusterFlowRule(List list, /*@Valid*/ String // } // Flow id should not be null after filtered. - // ClusterFlowConfig clusterConfig = rule.getClusterConfig(); + // ClusterFlowConfig clusterConfig = rule.getClusterConfig(); // Long flowId = clusterConfig.getFlowId(); // if (flowId == null) { @@ -419,9 +416,6 @@ private static void applyClusterFlowRule(List list, /*@Valid*/ String } - - - private static final class FlowRulePropertyListener implements PropertyListener> { private final String namespace; @@ -432,7 +426,7 @@ public FlowRulePropertyListener(String namespace) { @Override public synchronized void configUpdate(List conf) { - logger.info("config update {}" ,conf); + logger.info("config update {}", conf); applyClusterFlowRule(conf, namespace); // RecordLog.info("[ClusterFlowRuleManager] Cluster flow rules received for namespace <{}>: {}", // namespace, FLOW_RULES); @@ -442,12 +436,9 @@ public synchronized void configUpdate(List conf) { public synchronized void configLoad(List conf) { applyClusterFlowRule(conf, namespace); - logger.info("flow rule load {}" ,JsonUtil.formatJson(JsonUtil.object2Json(RESOURCE_RULES))); + logger.info("flow rule load {}", JsonUtil.formatJson(JsonUtil.object2Json(RESOURCE_RULES))); // RecordLog.info("[ClusterFlowRuleManager] Cluster flow rules loaded for namespace <{}>: {}", // namespace, FLOW_RULES); } } - - public ClusterFlowRuleManager() { - } } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetric.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetric.java index 84d2292aed..6f82e0a084 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetric.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetric.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; @@ -28,20 +27,18 @@ public long getCurrentCount(ClusterFlowEvent event) { } public long getCurrentCountForReport(ClusterFlowEvent event) { - metric.currentWindow(); - List list = metric.values(); - AtomicLong result = new AtomicLong(); - list.forEach(clusterMetricBucket -> { - long count = clusterMetricBucket.get(event); - result.set(result.get() + count); - }); - return result.get(); + metric.currentWindow(); + List list = metric.values(); + AtomicLong result = new AtomicLong(); + list.forEach(clusterMetricBucket -> { + long count = clusterMetricBucket.get(event); + result.set(result.get() + count); + }); + return result.get(); } - - /** * Get total sum for provided event in {@code intervalInSec}. * diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricBucket.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricBucket.java index c8b4115df7..217d6d225a 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricBucket.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricBucket.java @@ -8,7 +8,7 @@ public class ClusterMetricBucket { private final LongAdder[] counters; public ClusterMetricBucket() { - ClusterFlowEvent[] events = ClusterFlowEvent.values(); + ClusterFlowEvent[] events = ClusterFlowEvent.values(); this.counters = new LongAdder[events.length]; for (ClusterFlowEvent event : events) { counters[event.ordinal()] = new LongAdder(); diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricStatistics.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricStatistics.java index 444b24c555..5b85e57a10 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricStatistics.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterMetricStatistics.java @@ -9,11 +9,14 @@ public final class ClusterMetricStatistics { private static final Map METRIC_MAP = new ConcurrentHashMap<>(); + private ClusterMetricStatistics() { + } + public static void clear() { METRIC_MAP.clear(); } - public static void putMetric(String resource, ClusterMetric metric) { + public static void putMetric(String resource, ClusterMetric metric) { AssertUtil.notNull(metric, "Cluster metric cannot be null"); METRIC_MAP.put(resource, metric); } @@ -27,18 +30,14 @@ public static boolean putMetricIfAbsent(String resource, ClusterMetric metric) { return true; } - public static void removeMetric(String resource) { + public static void removeMetric(String resource) { METRIC_MAP.remove(resource); } - public static ClusterMetric getMetric(String resource) { + public static ClusterMetric getMetric(String resource) { return METRIC_MAP.get(resource); } - public static Map getMetricMap(){ - return METRIC_MAP; - } - // public static void resetFlowMetrics() { // Set keySet = METRIC_MAP.keySet(); // for (Long id : keySet) { @@ -47,10 +46,9 @@ public static Map getMetricMap(){ // } // } - - - private ClusterMetricStatistics() {} - + public static Map getMetricMap() { + return METRIC_MAP; + } } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleConstant.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleConstant.java index b379ba5207..5e35641476 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleConstant.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleConstant.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; public final class ClusterRuleConstant { @@ -11,5 +10,6 @@ public final class ClusterRuleConstant { public static final int DEFAULT_CLUSTER_SAMPLE_COUNT = 10; - private ClusterRuleConstant() {} + private ClusterRuleConstant() { + } } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleUtil.java b/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleUtil.java index 844e0cdefa..48cc7f875a 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/ClusterRuleUtil.java @@ -2,9 +2,10 @@ public final class ClusterRuleUtil { + private ClusterRuleUtil() { + } + public static boolean validId(Long id) { return id != null && id > 0; } - - private ClusterRuleUtil() {} } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/CurrentConcurrencyManager.java b/java/osx/core/src/main/java/com/osx/core/flow/CurrentConcurrencyManager.java index 7a853f2e6f..8615529b9a 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/CurrentConcurrencyManager.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/CurrentConcurrencyManager.java @@ -25,7 +25,8 @@ public final class CurrentConcurrencyManager { // SCHEDULER.scheduleAtFixedRate(logTask, 0, 1, TimeUnit.SECONDS); // } - /**, + /** + * , * add current concurrency. */ public static void addConcurrency(Long flowId, Integer acquireCount) { diff --git a/java/osx/core/src/main/java/com/osx/core/flow/DynamicProperty.java b/java/osx/core/src/main/java/com/osx/core/flow/DynamicProperty.java index 7a5de6c233..a71bf9a269 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/DynamicProperty.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/DynamicProperty.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; import org.slf4j.Logger; @@ -9,11 +8,9 @@ import java.util.Set; - public class DynamicProperty implements Property { - Logger logger = LoggerFactory.getLogger(DynamicProperty.class); - protected Set> listeners = Collections.synchronizedSet(new HashSet>()); + Logger logger = LoggerFactory.getLogger(DynamicProperty.class); private T value = null; public DynamicProperty() { @@ -28,7 +25,7 @@ public DynamicProperty(T value) { public void addListener(PropertyListener listener) { listeners.add(listener); - // logger.info("fire listener config load {}",value); + // logger.info("fire listener config load {}",value); listener.configLoad(value); } @@ -42,7 +39,7 @@ public boolean updateValue(T newValue) { if (isEqual(value, newValue)) { return false; } - // RecordLog.info("[DynamicSentinelProperty] Config will be updated to: {}", newValue); + // RecordLog.info("[DynamicSentinelProperty] Config will be updated to: {}", newValue); value = newValue; for (PropertyListener listener : listeners) { diff --git a/java/osx/core/src/main/java/com/osx/core/flow/FileMetricReport.java b/java/osx/core/src/main/java/com/osx/core/flow/FileMetricReport.java index 51a5651ebc..37da21a12c 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/FileMetricReport.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/FileMetricReport.java @@ -40,7 +40,7 @@ public void rmAllFile() throws Exception { @Override public void report(List data) { try { - // logger.info("report {}",data); + // logger.info("report {}",data); metricWriter.write(TimeUtil.currentTimeMillis(), data); } catch (Exception e) { e.printStackTrace(); diff --git a/java/osx/core/src/main/java/com/osx/core/flow/FlowCounterManager.java b/java/osx/core/src/main/java/com/osx/core/flow/FlowCounterManager.java index edd62dcadc..5a5d7b1b52 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/FlowCounterManager.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/FlowCounterManager.java @@ -17,16 +17,13 @@ package com.osx.core.flow; import com.fasterxml.jackson.core.type.TypeReference; - -import com.osx.core.utils.GetSystemInfo; -import com.osx.core.utils.JsonUtil; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.osx.core.utils.GetSystemInfo; +import com.osx.core.utils.JsonUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - - import java.io.*; import java.util.Comparator; import java.util.HashMap; @@ -40,11 +37,8 @@ public class FlowCounterManager { - public FlowCounterManager(){ - this("default"); - } public static final boolean USE_PID = true; - private static final String DEFAULT_CONFIG_FILE = "/test"+ File.separator + ".fate" + File.separator + "flowRules.json"; + private static final String DEFAULT_CONFIG_FILE = "/test" + File.separator + ".fate" + File.separator + "flowRules.json"; Logger logger = LoggerFactory.getLogger(FlowCounterManager.class); String appName; MetricSearcher metricSearcher; @@ -53,9 +47,14 @@ public FlowCounterManager(){ ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); Map sourceQpsAllowMap = new HashMap<>(); private ConcurrentHashMap passMap = new ConcurrentHashMap<>(); + public FlowCounterManager() { + this("default"); + } + public FlowCounterManager(String appName) { this(appName, false); } + public FlowCounterManager(String appName, Boolean countModelRequest) { this.appName = appName; String baseFileName = appName + "-metrics.log"; @@ -219,13 +218,13 @@ public void startReport() { // metricNode.setExceptionQps(exceptionCounter != null ? new Double(exceptionCounter.getQps()).longValue() : 0); // metricNode.setSuccessQps(successCounter != null ? new Double(successCounter.getQps()).longValue() : 0); - reportList.add(metricNode); + reportList.add(metricNode); - //modelReportList.add(metricNode); + //modelReportList.add(metricNode); }); - //logger.info("try to report {}",reportList); + //logger.info("try to report {}",reportList); metricReport.report(reportList); // if (modelMetricReport != null) { // modelMetricReport.report(modelReportList); @@ -252,7 +251,7 @@ public void rmAllFiles() { * init rules */ private void initialize() throws IOException { - file = new File(DEFAULT_CONFIG_FILE); + file = new File(DEFAULT_CONFIG_FILE); logger.info("try to load flow counter rules, {}", file.getAbsolutePath()); if (file.exists()) { @@ -267,7 +266,8 @@ private void initialize() throws IOException { result += tempString; } - List list = JsonUtil.json2Object(result, new TypeReference>() {}); + List list = JsonUtil.json2Object(result, new TypeReference>() { + }); if (list != null) { list.forEach(map -> { sourceQpsAllowMap.put((String) map.get("source"), Double.valueOf(String.valueOf(map.get("allow_qps")))); diff --git a/java/osx/core/src/main/java/com/osx/core/flow/FlowRule.java b/java/osx/core/src/main/java/com/osx/core/flow/FlowRule.java index f1b08e6e90..cda04138c1 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/FlowRule.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/FlowRule.java @@ -1,73 +1,77 @@ - package com.osx.core.flow; -import com.beust.jcommander.internal.Lists; + +import com.google.common.collect.Lists; import com.osx.core.utils.JsonUtil; import java.util.List; public class FlowRule extends AbstractRule { - public FlowRule() { - super(); - // setLimitApp(RuleConstant.LIMIT_APP_DEFAULT); - } - - public FlowRule(String resourceName) { - super(); - setResource(resourceName); - // setLimitApp(RuleConstant.LIMIT_APP_DEFAULT); - } - /** * The threshold type of flow control (0: thread count, 1: QPS). */ private int grade = RuleConstant.FLOW_GRADE_QPS; - /** * Flow control threshold count. */ - private double count; - + private double count; /** * Flow control strategy based on invocation chain. - * + *

* {@link RuleConstant#STRATEGY_DIRECT} for direct flow control (by origin); * {@link RuleConstant#STRATEGY_RELATE} for relevant flow control (with relevant resource); * {@link RuleConstant#STRATEGY_CHAIN} for chain flow control (by entrance resource). */ private int strategy = RuleConstant.STRATEGY_DIRECT; - /** * Reference resource in flow control with relevant resource or context. */ private String refResource; - /** * Rate limiter control behavior. * 0. default(reject directly), 1. warm up, 2. rate limiter, 3. warm up + rate limiter */ private int controlBehavior = RuleConstant.CONTROL_BEHAVIOR_DEFAULT; - private int warmUpPeriodSec = 10; - /** * Max queueing time in rate limiter behavior. */ private int maxQueueingTimeMs = 500; - private boolean clusterMode; /** * Flow rule config for cluster mode. */ private ClusterFlowConfig clusterConfig; + public FlowRule() { + super(); + // setLimitApp(RuleConstant.LIMIT_APP_DEFAULT); + } + public FlowRule(String resourceName) { + super(); + setResource(resourceName); + // setLimitApp(RuleConstant.LIMIT_APP_DEFAULT); + } + + public static void main(String[] args) { + FlowRule flowRule = new FlowRule(); + flowRule.setResource("test"); + flowRule.setCount(10000); + flowRule.setClusterMode(true); + flowRule.setStrategy(0); + + List list = Lists.newArrayList(); + + list.add(flowRule); + System.err.println(JsonUtil.object2Json(list)); + } + /** * The traffic shaping (throttling) controller. */ - // private TrafficShapingController controller; - + // private TrafficShapingController controller; public int getControlBehavior() { return controlBehavior; } @@ -81,11 +85,6 @@ public int getMaxQueueingTimeMs() { return maxQueueingTimeMs; } - public FlowRule setMaxQueueingTimeMs(int maxQueueingTimeMs) { - this.maxQueueingTimeMs = maxQueueingTimeMs; - return this; - } - // FlowRule setRater(TrafficShapingController rater) { // this.controller = rater; // return this; @@ -95,6 +94,11 @@ public FlowRule setMaxQueueingTimeMs(int maxQueueingTimeMs) { // return controller; // } + public FlowRule setMaxQueueingTimeMs(int maxQueueingTimeMs) { + this.maxQueueingTimeMs = maxQueueingTimeMs; + return this; + } + public int getWarmUpPeriodSec() { return warmUpPeriodSec; } @@ -160,20 +164,42 @@ public FlowRule setClusterConfig(ClusterFlowConfig clusterConfig) { @Override public boolean equals(Object o) { - if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { return false; } - if (!super.equals(o)) { return false; } - - FlowRule rule = (FlowRule)o; - - if (grade != rule.grade) { return false; } - if (Double.compare(rule.count, count) != 0) { return false; } - if (strategy != rule.strategy) { return false; } - if (controlBehavior != rule.controlBehavior) { return false; } - if (warmUpPeriodSec != rule.warmUpPeriodSec) { return false; } - if (maxQueueingTimeMs != rule.maxQueueingTimeMs) { return false; } - if (clusterMode != rule.clusterMode) { return false; } - if (refResource != null ? !refResource.equals(rule.refResource) : rule.refResource != null) { return false; } + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + + FlowRule rule = (FlowRule) o; + + if (grade != rule.grade) { + return false; + } + if (Double.compare(rule.count, count) != 0) { + return false; + } + if (strategy != rule.strategy) { + return false; + } + if (controlBehavior != rule.controlBehavior) { + return false; + } + if (warmUpPeriodSec != rule.warmUpPeriodSec) { + return false; + } + if (maxQueueingTimeMs != rule.maxQueueingTimeMs) { + return false; + } + if (clusterMode != rule.clusterMode) { + return false; + } + if (refResource != null ? !refResource.equals(rule.refResource) : rule.refResource != null) { + return false; + } return clusterConfig != null ? clusterConfig.equals(rule.clusterConfig) : rule.clusterConfig == null; } @@ -183,7 +209,7 @@ public int hashCode() { long temp; result = 31 * result + grade; temp = Double.doubleToLongBits(count); - result = 31 * result + (int)(temp ^ (temp >>> 32)); + result = 31 * result + (int) (temp ^ (temp >>> 32)); result = 31 * result + strategy; result = 31 * result + (refResource != null ? refResource.hashCode() : 0); result = 31 * result + controlBehavior; @@ -197,33 +223,18 @@ public int hashCode() { @Override public String toString() { return "FlowRule{" + - "resource=" + getResource() + - ", limitApp=" + getLimitApp() + - ", grade=" + grade + - ", count=" + count + - ", strategy=" + strategy + - ", refResource=" + refResource + - ", controlBehavior=" + controlBehavior + - ", warmUpPeriodSec=" + warmUpPeriodSec + - ", maxQueueingTimeMs=" + maxQueueingTimeMs + - ", clusterMode=" + clusterMode + - ", clusterConfig=" + clusterConfig + - // ", controller=" + controller + - '}'; - } - - - - public static void main(String[] args){ - FlowRule flowRule = new FlowRule(); - flowRule.setResource("test"); - flowRule.setCount(10000); - flowRule.setClusterMode(true); - flowRule.setStrategy(0); - - List list = Lists.newArrayList(); - - list .add(flowRule); - System.err.println(JsonUtil.object2Json(list)); + "resource=" + getResource() + + ", limitApp=" + getLimitApp() + + ", grade=" + grade + + ", count=" + count + + ", strategy=" + strategy + + ", refResource=" + refResource + + ", controlBehavior=" + controlBehavior + + ", warmUpPeriodSec=" + warmUpPeriodSec + + ", maxQueueingTimeMs=" + maxQueueingTimeMs + + ", clusterMode=" + clusterMode + + ", clusterConfig=" + clusterConfig + + // ", controller=" + controller + + '}'; } } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/Function.java b/java/osx/core/src/main/java/com/osx/core/flow/Function.java index 7c1e16119a..43c36cad5e 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/Function.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/Function.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; /** diff --git a/java/osx/core/src/main/java/com/osx/core/flow/GlobalRequestLimiter.java b/java/osx/core/src/main/java/com/osx/core/flow/GlobalRequestLimiter.java index 9c5706921e..22d911a522 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/GlobalRequestLimiter.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/GlobalRequestLimiter.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; import com.osx.core.utils.AssertUtil; @@ -7,11 +6,13 @@ import java.util.concurrent.ConcurrentHashMap; - public final class GlobalRequestLimiter { private static final Map GLOBAL_QPS_LIMITER_MAP = new ConcurrentHashMap<>(); + private GlobalRequestLimiter() { + } + public static void initIfAbsent(String namespace) { AssertUtil.notEmpty(namespace, "namespace cannot be empty"); if (!GLOBAL_QPS_LIMITER_MAP.containsKey(namespace)) { @@ -61,6 +62,4 @@ public static void applyMaxQpsChange(double maxAllowedQps) { } } } - - private GlobalRequestLimiter() {} } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/MetricBucket.java b/java/osx/core/src/main/java/com/osx/core/flow/MetricBucket.java index a79c00d7a0..1b1d7180c8 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/MetricBucket.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/MetricBucket.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; diff --git a/java/osx/core/src/main/java/com/osx/core/flow/MetricEvent.java b/java/osx/core/src/main/java/com/osx/core/flow/MetricEvent.java index 08a3337209..9ff7831868 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/MetricEvent.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/MetricEvent.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; /** diff --git a/java/osx/core/src/main/java/com/osx/core/flow/MetricWriter.java b/java/osx/core/src/main/java/com/osx/core/flow/MetricWriter.java index 8c5ce3bcf3..5588977fa2 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/MetricWriter.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/MetricWriter.java @@ -18,7 +18,6 @@ import com.osx.core.config.MetaInfo; - import com.osx.core.utils.GetSystemInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,7 +32,7 @@ public class MetricWriter { - public static final String METRIC_BASE_DIR = MetaInfo.PROPERTY_USER_HOME + "/.fate/metric/"; + public static final String METRIC_BASE_DIR = MetaInfo.PROPERTY_USER_HOME + "/.fate/metric/"; public static final String METRIC_FILE = "metrics.log"; public static final String METRIC_FILE_INDEX_SUFFIX = ".idx"; public static final Comparator METRIC_FILE_NAME_CMP = new MetricFileNameComparator(); diff --git a/java/osx/core/src/main/java/com/osx/core/flow/MetricsReader.java b/java/osx/core/src/main/java/com/osx/core/flow/MetricsReader.java index 38cd4bb9e9..fbe9d97f80 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/MetricsReader.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/MetricsReader.java @@ -121,7 +121,7 @@ void readMetricsInOneFile(List list, String fileName, * identity will be read. */ List readMetricsByEndTime(List fileNames, int pos, long offset, - long beginTimeMs, long endTimeMs, String identity) throws Exception { + long beginTimeMs, long endTimeMs, String identity) throws Exception { List list = new ArrayList(1024); if (readMetricsInOneFileByEndTime(list, fileNames.get(pos++), offset, beginTimeMs, endTimeMs, identity)) { while (pos < fileNames.size() @@ -132,7 +132,7 @@ && readMetricsInOneFileByEndTime(list, fileNames.get(pos++), 0, beginTimeMs, end } List readMetrics(List fileNames, int pos, - long offset, int recommendLines) throws Exception { + long offset, int recommendLines) throws Exception { List list = new ArrayList(recommendLines); readMetricsInOneFile(list, fileNames.get(pos++), offset, recommendLines); while (list.size() < recommendLines && pos < fileNames.size()) { diff --git a/java/osx/core/src/main/java/com/osx/core/flow/NamespaceFlowProperty.java b/java/osx/core/src/main/java/com/osx/core/flow/NamespaceFlowProperty.java index 92e3696acc..fa5c66ddbb 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/NamespaceFlowProperty.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/NamespaceFlowProperty.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; import java.util.List; diff --git a/java/osx/core/src/main/java/com/osx/core/flow/OccupySupport.java b/java/osx/core/src/main/java/com/osx/core/flow/OccupySupport.java index d18e469828..e911c0df93 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/OccupySupport.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/OccupySupport.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; /** diff --git a/java/osx/core/src/main/java/com/osx/core/flow/Property.java b/java/osx/core/src/main/java/com/osx/core/flow/Property.java index 1ef7f9c152..9fab989963 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/Property.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/Property.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; diff --git a/java/osx/core/src/main/java/com/osx/core/flow/RequestLimiter.java b/java/osx/core/src/main/java/com/osx/core/flow/RequestLimiter.java index 537fcd34c1..01c9019d9a 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/RequestLimiter.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/RequestLimiter.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; import com.osx.core.utils.AssertUtil; @@ -8,9 +7,8 @@ public class RequestLimiter { - private double qpsAllowed; - private final LeapArray data; + private double qpsAllowed; public RequestLimiter(double qpsAllowed) { this(new UnaryLeapArray(10, 1000), qpsAllowed); @@ -49,15 +47,15 @@ public double getQpsAllowed() { return qpsAllowed; } - public boolean canPass() { - return getQps() + 1 <= qpsAllowed; - } - public RequestLimiter setQpsAllowed(double qpsAllowed) { this.qpsAllowed = qpsAllowed; return this; } + public boolean canPass() { + return getQps() + 1 <= qpsAllowed; + } + public boolean tryPass() { if (canPass()) { add(1); diff --git a/java/osx/core/src/main/java/com/osx/core/flow/RuleConstant.java b/java/osx/core/src/main/java/com/osx/core/flow/RuleConstant.java index 9ce3eb5936..ae3c8bacff 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/RuleConstant.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/RuleConstant.java @@ -1,4 +1,3 @@ - package com.osx.core.flow; public final class RuleConstant { @@ -45,5 +44,6 @@ public final class RuleConstant { public static final int DEFAULT_SAMPLE_COUNT = 2; public static final int DEFAULT_WINDOW_INTERVAL_MS = 1000; - private RuleConstant() {} + private RuleConstant() { + } } diff --git a/java/osx/core/src/main/java/com/osx/core/flow/TokenService.java b/java/osx/core/src/main/java/com/osx/core/flow/TokenService.java index 381ef68521..02efe2aae4 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/TokenService.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/TokenService.java @@ -1,12 +1,8 @@ package com.osx.core.flow; - - import com.osx.core.token.TokenResult; -import java.util.Collection; - public interface TokenService { TokenResult requestToken(String resource, int acquireCount, boolean prioritized); @@ -41,6 +37,7 @@ public interface TokenService { * @return result of the token request */ //TokenResult requestConcurrentToken(String clientAddress,Long ruleId,int acquireCount); + /** * Request release concurrent tokens from remote token server asynchronously. * diff --git a/java/osx/core/src/main/java/com/osx/core/flow/UnaryLeapArray.java b/java/osx/core/src/main/java/com/osx/core/flow/UnaryLeapArray.java index bad1bff344..55455eeb32 100644 --- a/java/osx/core/src/main/java/com/osx/core/flow/UnaryLeapArray.java +++ b/java/osx/core/src/main/java/com/osx/core/flow/UnaryLeapArray.java @@ -1,5 +1,3 @@ - - package com.osx.core.flow; import java.util.concurrent.atomic.LongAdder; diff --git a/java/osx/core/src/main/java/com/osx/core/frame/CountDownLatch.java b/java/osx/core/src/main/java/com/osx/core/frame/CountDownLatch.java index a5fd833301..f26e5ede63 100644 --- a/java/osx/core/src/main/java/com/osx/core/frame/CountDownLatch.java +++ b/java/osx/core/src/main/java/com/osx/core/frame/CountDownLatch.java @@ -1,5 +1,3 @@ - - package com.osx.core.frame; import java.util.concurrent.TimeUnit; @@ -21,7 +19,7 @@ public void await() throws InterruptedException { } public boolean await(long timeout, TimeUnit unit) - throws InterruptedException { + throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } diff --git a/java/osx/core/src/main/java/com/osx/core/frame/GrpcConnectionFactory.java b/java/osx/core/src/main/java/com/osx/core/frame/GrpcConnectionFactory.java index 8bf7650a94..b1f0653184 100644 --- a/java/osx/core/src/main/java/com/osx/core/frame/GrpcConnectionFactory.java +++ b/java/osx/core/src/main/java/com/osx/core/frame/GrpcConnectionFactory.java @@ -15,11 +15,10 @@ */ package com.osx.core.frame; -import com.osx.core.config.*; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; + +import com.osx.core.config.GrpcChannelInfo; +import com.osx.core.exceptions.SysException; import com.osx.core.router.RouterInfo; -import io.grpc.ConnectivityState; import io.grpc.ManagedChannel; import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts; import io.grpc.netty.shaded.io.grpc.netty.NegotiationType; @@ -30,70 +29,71 @@ import org.slf4j.LoggerFactory; import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; + +import static com.osx.core.config.MetaInfo.*; public class GrpcConnectionFactory { private static final Logger logger = LoggerFactory.getLogger(GrpcConnectionFactory.class); + static ConcurrentHashMap managedChannelPool = new ConcurrentHashMap<>(); + + static GrpcChannelInfo defaultfGrpcChannelInfo; private GrpcConnectionFactory() { } - static ConcurrentHashMap managedChannelPool = new ConcurrentHashMap<>(); - -// -// public static synchronized ManagedChannel createManagedChannel(RouterInfo routerInfo){ -// -// -// -// } - static GrpcChannelInfo defaultfGrpcChannelInfo; - - public static synchronized ManagedChannel createManagedChannel(RouterInfo routerInfo) { - if(managedChannelPool.get(routerInfo.toKey())!=null){ - return managedChannelPool.get(routerInfo.toKey()); - }else { - ManagedChannel managedChannel = createManagedChannel(routerInfo, defaultfGrpcChannelInfo); - managedChannelPool.put(routerInfo.toKey(),managedChannel); + public static synchronized ManagedChannel createManagedChannel(RouterInfo routerInfo,boolean usePooled) { + + + if(usePooled) { + if (managedChannelPool.get(routerInfo.toKey()) != null) { + return managedChannelPool.get(routerInfo.toKey()); + } else { + ManagedChannel managedChannel = createManagedChannel(routerInfo, buildDefaultGrpcChannelInfo()); + managedChannelPool.put(routerInfo.toKey(), managedChannel); + return managedChannel; + } + }else{ + ManagedChannel managedChannel = createManagedChannel(routerInfo, buildDefaultGrpcChannelInfo()); return managedChannel; } } + private static GrpcChannelInfo buildDefaultGrpcChannelInfo(){ + GrpcChannelInfo grpcChannelInfo = new GrpcChannelInfo(); + grpcChannelInfo.setKeepAliveTime(PROPERTY_GRPC_CLIENT_KEEPALIVE_TIME_SEC); + grpcChannelInfo.setKeepAliveTimeout(PROPERTY_GRPC_CLIENT_KEEPALIVE_TIMEOUT_SEC); + grpcChannelInfo.setKeepAliveWithoutCalls(PROPERTY_GRPC_CLIENT_KEEPALIVE_WITHOUT_CALLS_ENABLED); + grpcChannelInfo.setFlowControlWindow(PROPERTY_GRPC_CLIENT_FLOW_CONTROL_WINDOW); + grpcChannelInfo.setMaxInboundMessageSize(PROPERTY_GRPC_CLIENT_MAX_INBOUND_MESSAGE_SIZE); + grpcChannelInfo.setRetryBufferSize(PROPERTY_GRPC_CLIENT_RETRY_BUFFER_SIZE); + grpcChannelInfo.setIdelTimeOut(PROPERTY_GRPC_CLIENT_MAX_CONNECTION_IDLE_SEC); + grpcChannelInfo.setPerRpcBufferLimit(PROPERTY_GRPC_CLIENT_PER_RPC_BUFFER_LIMIT); + return grpcChannelInfo; + } - - - - - - - public static synchronized ManagedChannel createManagedChannel(RouterInfo routerInfo, GrpcChannelInfo channelInfo) { + public static synchronized ManagedChannel createManagedChannel(RouterInfo routerInfo, GrpcChannelInfo channelInfo) { try { - if (logger.isDebugEnabled()) { - logger.debug("create ManagedChannel"); + if(channelInfo==null){ + throw new SysException("grpc channel info is null"); } - NettyChannelBuilder channelBuilder = NettyChannelBuilder .forAddress(routerInfo.getHost(), routerInfo.getPort()) - .keepAliveTime(60, TimeUnit.MINUTES) - .keepAliveTimeout(60, TimeUnit.MINUTES) - .keepAliveWithoutCalls(true) - .idleTimeout(60, TimeUnit.MINUTES) - .perRpcBufferLimit(128 << 20) - .flowControlWindow(32 << 20) - .maxInboundMessageSize(32 << 20) + .keepAliveTime(channelInfo.getKeepAliveTime(), TimeUnit.MINUTES) + .keepAliveTimeout(channelInfo.getKeepAliveTimeout(), TimeUnit.MINUTES) + .keepAliveWithoutCalls(channelInfo.isKeepAliveWithoutCalls()) + .idleTimeout(channelInfo.getIdelTimeOut(), TimeUnit.MINUTES) + .perRpcBufferLimit(channelInfo.getPerRpcBufferLimit()) + .flowControlWindow(channelInfo.getFlowControlWindow()) + .maxInboundMessageSize(channelInfo.getMaxInboundMessageSize()) .enableRetry() - .retryBufferSize(16 << 20) - .maxRetryAttempts(20); + .retryBufferSize(channelInfo.getRetryBufferSize()) + .maxRetryAttempts(channelInfo.getMaxRetryAttemps()); if (routerInfo != null && NegotiationType.TLS.name().equals(routerInfo.getNegotiationType()) && StringUtils.isNotBlank(routerInfo.getCertChainFile()) @@ -111,14 +111,12 @@ public static synchronized ManagedChannel createManagedChannel(RouterInfo route channelBuilder.usePlaintext(); } return channelBuilder.build(); - } - catch (Exception e) { - logger.error("create channel error : " ,e); + } catch (Exception e) { + logger.error("create channel error : ", e); //e.printStackTrace(); } return null; } - } \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/frame/Lifecycle.java b/java/osx/core/src/main/java/com/osx/core/frame/Lifecycle.java index e74229c66f..503465e96d 100644 --- a/java/osx/core/src/main/java/com/osx/core/frame/Lifecycle.java +++ b/java/osx/core/src/main/java/com/osx/core/frame/Lifecycle.java @@ -1,7 +1,9 @@ package com.osx.core.frame; public interface Lifecycle { - public void init(); - public void start(); - public void destroy(); + public void init(); + + public void start(); + + public void destroy(); } diff --git a/java/osx/core/src/main/java/com/osx/core/frame/ServiceThread.java b/java/osx/core/src/main/java/com/osx/core/frame/ServiceThread.java index 4acc11b42e..2c220ffcfa 100644 --- a/java/osx/core/src/main/java/com/osx/core/frame/ServiceThread.java +++ b/java/osx/core/src/main/java/com/osx/core/frame/ServiceThread.java @@ -1,24 +1,21 @@ - package com.osx.core.frame; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + public abstract class ServiceThread implements Runnable { private static final Logger log = LoggerFactory.getLogger(ServiceThread.class); private static final long JOIN_TIME = 90 * 1000; - - private Thread thread; protected final CountDownLatch waitPoint = new CountDownLatch(1); + private final AtomicBoolean started = new AtomicBoolean(false); protected volatile AtomicBoolean hasNotified = new AtomicBoolean(false); protected volatile boolean stopped = false; protected boolean isDaemon = false; - - private final AtomicBoolean started = new AtomicBoolean(false); + private Thread thread; public ServiceThread() { @@ -64,7 +61,7 @@ public void shutdown(final boolean interrupt) { } long elapsedTime = System.currentTimeMillis() - beginTime; log.info("join thread " + this.getServiceName() + " elapsed time(ms) " + elapsedTime + " " - + this.getJointime()); + + this.getJointime()); } catch (InterruptedException e) { log.error("Interrupted", e); } diff --git a/java/osx/core/src/main/java/com/osx/core/jvm/JVMGCUtils.java b/java/osx/core/src/main/java/com/osx/core/jvm/JVMGCUtils.java index 9061cbb132..44ed817fd6 100644 --- a/java/osx/core/src/main/java/com/osx/core/jvm/JVMGCUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/jvm/JVMGCUtils.java @@ -15,7 +15,7 @@ */ package com.osx.core.jvm; - + import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.ArrayList; diff --git a/java/osx/core/src/main/java/com/osx/core/jvm/JVMMemoryUtils.java b/java/osx/core/src/main/java/com/osx/core/jvm/JVMMemoryUtils.java index 3e9299150d..b8b93d58da 100644 --- a/java/osx/core/src/main/java/com/osx/core/jvm/JVMMemoryUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/jvm/JVMMemoryUtils.java @@ -15,17 +15,15 @@ */ package com.osx.core.jvm; - + import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; -import java.util.ArrayList; import java.util.List; - + /** * 类描述:JVM内存信息工具类 - * **/ public class JVMMemoryUtils { static private MemoryMXBean memoryMXBean; @@ -34,265 +32,190 @@ public class JVMMemoryUtils { static private MemoryPoolMXBean oldGenMxBean; static private MemoryPoolMXBean permGenMxBean; static private MemoryPoolMXBean codeCacheMxBean; - - /** - * JVM内存区域使用情况。
- *

-	 * init:初始内存大小(字节)
-	 * used:当前使用内存大小(字节)
-	 * committed:已经申请分配的内存大小(字节)
-	 * max:最大内存大小(字节)
-	 * usedPercent:已经申请分配内存与最大内存大小的百分比
-	 * 
- * @author tangjiyu - */ - static public class JVMMemoryUsage { - //初始内存大小(字节) - private long init; - //当前使用内存大小(字节) - private long used; - //已经申请分配的内存大小(字节) - private long committed; - //最大内存大小(字节) - private long max; - //已经申请分配内存与最大内存大小的百分比 - private float usedPercent; - - public JVMMemoryUsage() { - } - - public JVMMemoryUsage(MemoryUsage memoryUsage) { - this.setMemoryUsage(memoryUsage); - //this(memoryUsage.getInit(), memoryUsage.getUsed(), memoryUsage.getCommitted(), memoryUsage.getMax()); - } - - public JVMMemoryUsage(long init, long used, long committed, long max) { - super(); - this.setMemoryUsage(init, used, committed, max); - } - - private void setMemoryUsage(MemoryUsage memoryUsage) { - if(memoryUsage!=null) { - this.setMemoryUsage(memoryUsage.getInit(), memoryUsage.getUsed(), memoryUsage.getCommitted(), memoryUsage.getMax()); - } else { - this.setMemoryUsage(0, 0, 0, 0); - } - } - - private void setMemoryUsage(long init, long used, long committed, long max) { - this.init = init; - this.used = used; - this.committed = committed; - this.max = max; - if(this.used>0 && max>0) { - this.usedPercent = used * Float.valueOf("1.0") / max; - } else { - this.usedPercent = 0; - } - } - - public long getInit() { - return init; - } - public long getUsed() { - return used; - } - public long getCommitted() { - return committed; - } - public long getMax() { - return max; - } - public float getUsedPercent() { - return usedPercent; - } - - @Override - public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append("init = " + init + "(" + (init >> 10) + "K) "); - buf.append("used = " + used + "(" + (used >> 10) + "K) "); - buf.append("committed = " + committed + "(" + - (committed >> 10) + "K) " ); - buf.append("max = " + max + "(" + (max >> 10) + "K)"); - buf.append("usedPercent = " + usedPercent); - return buf.toString(); - } - } - + static { memoryMXBean = ManagementFactory.getMemoryMXBean(); - - List memoryPoolMXBeanList = ManagementFactory.getMemoryPoolMXBeans(); + + List memoryPoolMXBeanList = ManagementFactory.getMemoryPoolMXBeans(); for (final MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeanList) { - String poolName = memoryPoolMXBean.getName(); - if(poolName==null) { - continue; - } - // 官方JVM(HotSpot)提供的MemoryPoolMXBean - // JDK1.7/1.8 Eden区内存池名称: "Eden Space" 或 "PS Eden Space"、 “G1 Eden Space”(和垃圾收集器有关) - // JDK1.7/1.8 Survivor区内存池名称:"Survivor Space" 或 "PS Survivor Space"、“G1 Survivor Space”(和垃圾收集器有关) - // JDK1.7 老区内存池名称: "Tenured Gen" - // JDK1.8 老区内存池名称:"Old Gen" 或 "PS Old Gen"、“G1 Old Gen”(和垃圾收集器有关) - // JDK1.7 方法/永久区内存池名称: "Perm Gen" 或 "PS Perm Gen"(和垃圾收集器有关) - // JDK1.8 方法/永久区内存池名称:"Metaspace"(注意:不在堆内存中) - // JDK1.7/1.8 CodeCache区内存池名称: "Code Cache" - if (edenSpaceMxBean==null && poolName.endsWith("Eden Space")) { - edenSpaceMxBean = memoryPoolMXBean; - } else if (survivorSpaceMxBean==null && poolName.endsWith("Survivor Space")) { - survivorSpaceMxBean = memoryPoolMXBean; - } else if (oldGenMxBean==null && (poolName.endsWith("Tenured Gen") || poolName.endsWith("Old Gen"))) { - oldGenMxBean = memoryPoolMXBean; - } else if (permGenMxBean==null && (poolName.endsWith("Perm Gen") || poolName.endsWith("Metaspace"))) { - permGenMxBean = memoryPoolMXBean; - } else if (codeCacheMxBean==null && poolName.endsWith("Code Cache")) { - codeCacheMxBean = memoryPoolMXBean; - } - } + String poolName = memoryPoolMXBean.getName(); + if (poolName == null) { + continue; + } + // 官方JVM(HotSpot)提供的MemoryPoolMXBean + // JDK1.7/1.8 Eden区内存池名称: "Eden Space" 或 "PS Eden Space"、 “G1 Eden Space”(和垃圾收集器有关) + // JDK1.7/1.8 Survivor区内存池名称:"Survivor Space" 或 "PS Survivor Space"、“G1 Survivor Space”(和垃圾收集器有关) + // JDK1.7 老区内存池名称: "Tenured Gen" + // JDK1.8 老区内存池名称:"Old Gen" 或 "PS Old Gen"、“G1 Old Gen”(和垃圾收集器有关) + // JDK1.7 方法/永久区内存池名称: "Perm Gen" 或 "PS Perm Gen"(和垃圾收集器有关) + // JDK1.8 方法/永久区内存池名称:"Metaspace"(注意:不在堆内存中) + // JDK1.7/1.8 CodeCache区内存池名称: "Code Cache" + if (edenSpaceMxBean == null && poolName.endsWith("Eden Space")) { + edenSpaceMxBean = memoryPoolMXBean; + } else if (survivorSpaceMxBean == null && poolName.endsWith("Survivor Space")) { + survivorSpaceMxBean = memoryPoolMXBean; + } else if (oldGenMxBean == null && (poolName.endsWith("Tenured Gen") || poolName.endsWith("Old Gen"))) { + oldGenMxBean = memoryPoolMXBean; + } else if (permGenMxBean == null && (poolName.endsWith("Perm Gen") || poolName.endsWith("Metaspace"))) { + permGenMxBean = memoryPoolMXBean; + } else if (codeCacheMxBean == null && poolName.endsWith("Code Cache")) { + codeCacheMxBean = memoryPoolMXBean; + } + } }// static - - + /** * 获取堆内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getHeapMemoryUsage() { - if(memoryMXBean!=null) { - final MemoryUsage usage =memoryMXBean.getHeapMemoryUsage(); - if(usage!=null) { - return new JVMMemoryUsage(usage); - } - } + if (memoryMXBean != null) { + final MemoryUsage usage = memoryMXBean.getHeapMemoryUsage(); + if (usage != null) { + return new JVMMemoryUsage(usage); + } + } return null; } - + /** * 获取堆外内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getNonHeapMemoryUsage() { - if(memoryMXBean!=null) { - final MemoryUsage usage =memoryMXBean.getNonHeapMemoryUsage(); - if(usage!=null) { - return new JVMMemoryUsage(usage); - } - } + if (memoryMXBean != null) { + final MemoryUsage usage = memoryMXBean.getNonHeapMemoryUsage(); + if (usage != null) { + return new JVMMemoryUsage(usage); + } + } return null; } - + /** * 获取Eden区内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getEdenSpaceMemoryUsage() { - return getMemoryPoolUsage(edenSpaceMxBean); + return getMemoryPoolUsage(edenSpaceMxBean); } - + /** * 获取Eden区内存峰值(从启动或上一次重置开始统计),并重置 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getAndResetEdenSpaceMemoryPeakUsage() { - return getAndResetMemoryPoolPeakUsage(edenSpaceMxBean); + return getAndResetMemoryPoolPeakUsage(edenSpaceMxBean); } - + /** * 获取Survivor区内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getSurvivorSpaceMemoryUsage() { - return getMemoryPoolUsage(survivorSpaceMxBean); + return getMemoryPoolUsage(survivorSpaceMxBean); } - + /** * 获取Survivor区内存峰值(从启动或上一次重置开始统计),并重置 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getAndResetSurvivorSpaceMemoryPeakUsage() { - return getAndResetMemoryPoolPeakUsage(survivorSpaceMxBean); + return getAndResetMemoryPoolPeakUsage(survivorSpaceMxBean); } - + /** * 获取老区内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getOldGenMemoryUsage() { - return getMemoryPoolUsage(oldGenMxBean); + return getMemoryPoolUsage(oldGenMxBean); } - + /** * 获取老区内存峰值(从启动或上一次重置开始统计),并重置 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getAndResetOldGenMemoryPeakUsage() { - return getAndResetMemoryPoolPeakUsage(oldGenMxBean); + return getAndResetMemoryPoolPeakUsage(oldGenMxBean); } - + /** * 获取永久区/方法区内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getPermGenMemoryUsage() { - return getMemoryPoolUsage(permGenMxBean); + return getMemoryPoolUsage(permGenMxBean); } - + /** * 获取永久区/方法区内存峰值(从启动或上一次重置开始统计),并重置 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getAndResetPermGenMemoryPeakUsage() { - return getAndResetMemoryPoolPeakUsage(permGenMxBean); + return getAndResetMemoryPoolPeakUsage(permGenMxBean); } - + /** * 获取CodeCache区内存情况 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getCodeCacheMemoryUsage() { - return getMemoryPoolUsage(codeCacheMxBean); + return getMemoryPoolUsage(codeCacheMxBean); } - + /** * 获取CodeCache区内存峰值(从启动或上一次重置开始统计),并重置 + * * @return 不能获取到返回null */ static public JVMMemoryUsage getAndResetCodeCacheMemoryPeakUsage() { - return getAndResetMemoryPoolPeakUsage(codeCacheMxBean); + return getAndResetMemoryPoolPeakUsage(codeCacheMxBean); } - + static private JVMMemoryUsage getMemoryPoolUsage(MemoryPoolMXBean memoryPoolMXBean) { - if(memoryPoolMXBean!=null) { - final MemoryUsage usage = memoryPoolMXBean.getUsage(); - if(usage!=null) { - return new JVMMemoryUsage(usage); - } - } - return null; - } - - - static public String getProcessId(){ - String processName = ManagementFactory.getRuntimeMXBean().getName(); - String processId = processName.substring(0,processName.indexOf('@')); - return processId; - } - + if (memoryPoolMXBean != null) { + final MemoryUsage usage = memoryPoolMXBean.getUsage(); + if (usage != null) { + return new JVMMemoryUsage(usage); + } + } + return null; + } + + static public String getProcessId() { + String processName = ManagementFactory.getRuntimeMXBean().getName(); + String processId = processName.substring(0, processName.indexOf('@')); + return processId; + } + static private JVMMemoryUsage getAndResetMemoryPoolPeakUsage(MemoryPoolMXBean memoryPoolMXBean) { - if(memoryPoolMXBean!=null) { - final MemoryUsage usage = memoryPoolMXBean.getPeakUsage(); - if(usage!=null) { - memoryPoolMXBean.resetPeakUsage(); - return new JVMMemoryUsage(usage); - } - } - return null; - } - + if (memoryPoolMXBean != null) { + final MemoryUsage usage = memoryPoolMXBean.getPeakUsage(); + if (usage != null) { + memoryPoolMXBean.resetPeakUsage(); + return new JVMMemoryUsage(usage); + } + } + return null; + } + public static void main(String[] args) { - System.err.println(JVMMemoryUtils.getProcessId()); - // List> listRoot = new ArrayList>(); - // for(;;) { + System.err.println(JVMMemoryUtils.getProcessId()); + // List> listRoot = new ArrayList>(); + // for(;;) { // System.out.println("======================================================================="); // System.out.println("getHeapMemoryUsage: " + JVMMemoryUtils.getHeapMemoryUsage()); // System.out.println("getNonHeapMemoryUsage: " + JVMMemoryUtils.getNonHeapMemoryUsage()); @@ -320,4 +243,94 @@ public static void main(String[] args) { // Runtime.getRuntime().gc(); // } } + + /** + * JVM内存区域使用情况。
+ *
+     * init:初始内存大小(字节)
+     * used:当前使用内存大小(字节)
+     * committed:已经申请分配的内存大小(字节)
+     * max:最大内存大小(字节)
+     * usedPercent:已经申请分配内存与最大内存大小的百分比
+     * 
+ * + * @author tangjiyu + */ + static public class JVMMemoryUsage { + //初始内存大小(字节) + private long init; + //当前使用内存大小(字节) + private long used; + //已经申请分配的内存大小(字节) + private long committed; + //最大内存大小(字节) + private long max; + //已经申请分配内存与最大内存大小的百分比 + private float usedPercent; + + public JVMMemoryUsage() { + } + + public JVMMemoryUsage(MemoryUsage memoryUsage) { + this.setMemoryUsage(memoryUsage); + //this(memoryUsage.getInit(), memoryUsage.getUsed(), memoryUsage.getCommitted(), memoryUsage.getMax()); + } + + public JVMMemoryUsage(long init, long used, long committed, long max) { + super(); + this.setMemoryUsage(init, used, committed, max); + } + + private void setMemoryUsage(MemoryUsage memoryUsage) { + if (memoryUsage != null) { + this.setMemoryUsage(memoryUsage.getInit(), memoryUsage.getUsed(), memoryUsage.getCommitted(), memoryUsage.getMax()); + } else { + this.setMemoryUsage(0, 0, 0, 0); + } + } + + private void setMemoryUsage(long init, long used, long committed, long max) { + this.init = init; + this.used = used; + this.committed = committed; + this.max = max; + if (this.used > 0 && max > 0) { + this.usedPercent = used * Float.valueOf("1.0") / max; + } else { + this.usedPercent = 0; + } + } + + public long getInit() { + return init; + } + + public long getUsed() { + return used; + } + + public long getCommitted() { + return committed; + } + + public long getMax() { + return max; + } + + public float getUsedPercent() { + return usedPercent; + } + + @Override + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("init = " + init + "(" + (init >> 10) + "K) "); + buf.append("used = " + used + "(" + (used >> 10) + "K) "); + buf.append("committed = " + committed + "(" + + (committed >> 10) + "K) "); + buf.append("max = " + max + "(" + (max >> 10) + "K)"); + buf.append("usedPercent = " + usedPercent); + return buf.toString(); + } + } } diff --git a/java/osx/core/src/main/java/com/osx/core/jvm/JVMThreadUtils.java b/java/osx/core/src/main/java/com/osx/core/jvm/JVMThreadUtils.java index 6e92d2cc0e..ad8762dc6a 100644 --- a/java/osx/core/src/main/java/com/osx/core/jvm/JVMThreadUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/jvm/JVMThreadUtils.java @@ -21,7 +21,6 @@ /** * 类描述:JVM 线程信息工具类 - * **/ public class JVMThreadUtils { static private ThreadMXBean threadMXBean; @@ -32,6 +31,7 @@ public class JVMThreadUtils { /** * Daemon线程总量 + * * @return */ static public int getDaemonThreadCount() { @@ -40,6 +40,7 @@ static public int getDaemonThreadCount() { /** * 当前线程总量 + * * @return */ static public int getThreadCount() { @@ -48,6 +49,7 @@ static public int getThreadCount() { /** * 获取线程数量峰值(从启动或resetPeakThreadCount()方法重置开始统计) + * * @return */ static public int getPeakThreadCount() { @@ -56,6 +58,7 @@ static public int getPeakThreadCount() { /** * 获取线程数量峰值(从启动或resetPeakThreadCount()方法重置开始统计),并重置 + * * @return * @Throws java.lang.SecurityException - if a security manager exists and the caller does not have ManagementPermission("control"). */ @@ -76,6 +79,7 @@ static public void resetPeakThreadCount() { /** * 死锁线程总量 + * * @return * @Throws IllegalStateException 没有权限或JVM不支持的操作 */ diff --git a/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfo.java b/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfo.java index e984235839..844e22b0d5 100644 --- a/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfo.java @@ -17,7 +17,6 @@ package com.osx.core.jvm; - public class JvmInfo { long timestamp; JVMMemoryUtils.JVMMemoryUsage heap; diff --git a/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfoCounter.java b/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfoCounter.java index 115f2854d9..ebe7cef686 100644 --- a/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfoCounter.java +++ b/java/osx/core/src/main/java/com/osx/core/jvm/JvmInfoCounter.java @@ -16,9 +16,9 @@ package com.osx.core.jvm; +import com.google.common.collect.Lists; import com.osx.core.flow.LeapArray; import com.osx.core.flow.TimeUtil; -import com.google.common.collect.Lists; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/java/osx/core/src/main/java/com/osx/core/log/Appender.java b/java/osx/core/src/main/java/com/osx/core/log/Appender.java index ee4fee63c2..90e5a15f0d 100755 --- a/java/osx/core/src/main/java/com/osx/core/log/Appender.java +++ b/java/osx/core/src/main/java/com/osx/core/log/Appender.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; @@ -15,14 +13,10 @@ public abstract class Appender { public static final int CODE_FILE_OPEN_FAILURE = 4; public final static String LINE_SEP = System.getProperty("line.separator"); - - boolean firstTime = true; - protected Layout layout; - protected String name; - protected boolean closed = false; + boolean firstTime = true; public void activateOptions() { } @@ -47,10 +41,18 @@ public Layout getLayout() { return layout; } + public void setLayout(Layout layout) { + this.layout = layout; + } + public final String getName() { return this.name; } + public void setName(String name) { + this.name = name; + } + public synchronized void doAppend(LoggingEvent event) { if (closed) { SysLogger.error("Attempted to append to closed appender named [" + name + "]."); @@ -59,14 +61,6 @@ public synchronized void doAppend(LoggingEvent event) { this.append(event); } - public void setLayout(Layout layout) { - this.layout = layout; - } - - public void setName(String name) { - this.name = name; - } - public abstract void close(); public void handleError(String message, Exception e, int errorCode) { diff --git a/java/osx/core/src/main/java/com/osx/core/log/InnerLoggerFactory.java b/java/osx/core/src/main/java/com/osx/core/log/InnerLoggerFactory.java index 4e5274f0df..08dec40032 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/InnerLoggerFactory.java +++ b/java/osx/core/src/main/java/com/osx/core/log/InnerLoggerFactory.java @@ -1,4 +1,3 @@ - package com.osx.core.log; diff --git a/java/osx/core/src/main/java/com/osx/core/log/InternalLogger.java b/java/osx/core/src/main/java/com/osx/core/log/InternalLogger.java index 8172e03aa2..f30e6c9f62 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/InternalLogger.java +++ b/java/osx/core/src/main/java/com/osx/core/log/InternalLogger.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; public interface InternalLogger { diff --git a/java/osx/core/src/main/java/com/osx/core/log/InternalLoggerFactory.java b/java/osx/core/src/main/java/com/osx/core/log/InternalLoggerFactory.java index 11231beae9..0561b07690 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/InternalLoggerFactory.java +++ b/java/osx/core/src/main/java/com/osx/core/log/InternalLoggerFactory.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; import java.util.concurrent.ConcurrentHashMap; @@ -16,6 +14,19 @@ public abstract class InternalLoggerFactory { private static ConcurrentHashMap loggerFactoryCache = new ConcurrentHashMap(); + static { + try { + new Slf4jLoggerFactory(); + } catch (Throwable e) { + //ignore + } + try { + new InnerLoggerFactory(); + } catch (Throwable e) { + //ignore + } + } + public static InternalLogger getLogger(Class clazz) { return getLogger(clazz.getName()); } @@ -45,19 +56,6 @@ public static void setCurrentLoggerType(String type) { loggerType = type; } - static { - try { - new Slf4jLoggerFactory(); - } catch (Throwable e) { - //ignore - } - try { - new InnerLoggerFactory(); - } catch (Throwable e) { - //ignore - } - } - protected void doRegister() { String loggerType = getLoggerType(); if (loggerFactoryCache.get(loggerType) != null) { diff --git a/java/osx/core/src/main/java/com/osx/core/log/Layout.java b/java/osx/core/src/main/java/com/osx/core/log/Layout.java index 8deab43aef..6563338dea 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/Layout.java +++ b/java/osx/core/src/main/java/com/osx/core/log/Layout.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; public abstract class Layout { diff --git a/java/osx/core/src/main/java/com/osx/core/log/Level.java b/java/osx/core/src/main/java/com/osx/core/log/Level.java index b86f94268d..c163a3ea87 100755 --- a/java/osx/core/src/main/java/com/osx/core/log/Level.java +++ b/java/osx/core/src/main/java/com/osx/core/log/Level.java @@ -1,48 +1,31 @@ - - package com.osx.core.log; import java.io.Serializable; public class Level implements Serializable { - transient int level; - transient String levelStr; - transient int syslogEquivalent; - public final static int OFF_INT = Integer.MAX_VALUE; public final static int ERROR_INT = 40000; public final static int WARN_INT = 30000; public final static int INFO_INT = 20000; public final static int DEBUG_INT = 10000; public final static int ALL_INT = Integer.MIN_VALUE; - - + static final long serialVersionUID = 3491141966387921974L; private static final String ALL_NAME = "ALL"; - + final static public Level ALL = new Level(ALL_INT, ALL_NAME, 7); private static final String DEBUG_NAME = "DEBUG"; - + final static public Level DEBUG = new Level(DEBUG_INT, DEBUG_NAME, 7); private static final String INFO_NAME = "INFO"; - + final static public Level INFO = new Level(INFO_INT, INFO_NAME, 6); private static final String WARN_NAME = "WARN"; - + final static public Level WARN = new Level(WARN_INT, WARN_NAME, 4); private static final String ERROR_NAME = "ERROR"; - + final static public Level ERROR = new Level(ERROR_INT, ERROR_NAME, 3); private static final String OFF_NAME = "OFF"; - final static public Level OFF = new Level(OFF_INT, OFF_NAME, 0); - - final static public Level ERROR = new Level(ERROR_INT, ERROR_NAME, 3); - - final static public Level WARN = new Level(WARN_INT, WARN_NAME, 4); - - final static public Level INFO = new Level(INFO_INT, INFO_NAME, 6); - - final static public Level DEBUG = new Level(DEBUG_INT, DEBUG_NAME, 7); - - final static public Level ALL = new Level(ALL_INT, ALL_NAME, 7); - - static final long serialVersionUID = 3491141966387921974L; + transient int level; + transient String levelStr; + transient int syslogEquivalent; protected Level(int level, String levelStr, int syslogEquivalent) { this.level = level; diff --git a/java/osx/core/src/main/java/com/osx/core/log/Logger.java b/java/osx/core/src/main/java/com/osx/core/log/Logger.java index 6eb0723a87..8e8530c17f 100755 --- a/java/osx/core/src/main/java/com/osx/core/log/Logger.java +++ b/java/osx/core/src/main/java/com/osx/core/log/Logger.java @@ -1,4 +1,3 @@ - package com.osx.core.log; import java.util.Enumeration; @@ -11,25 +10,22 @@ public class Logger implements Appender.AppenderPipeline { private static final String FQCN = Logger.class.getName(); private static final DefaultLoggerRepository REPOSITORY = new DefaultLoggerRepository(new RootLogger(Level.DEBUG)); - - public static LoggerRepository getRepository() { - return REPOSITORY; - } - + Appender.AppenderPipelineImpl appenderPipeline; private String name; volatile private Level level; volatile private Logger parent; - - Appender.AppenderPipelineImpl appenderPipeline; - private boolean additive = true; private Logger(String name) { this.name = name; } + public static LoggerRepository getRepository() { + return REPOSITORY; + } + static public Logger getLogger(String name) { return getRepository().getLogger(name); } @@ -158,6 +154,9 @@ final public Level getLevel() { return this.level; } + public void setLevel(Level level) { + this.level = level; + } public void info(Object message) { if (getRepository().isDisabled(Level.INFO_INT)) { @@ -206,10 +205,6 @@ public void setAdditivity(boolean additive) { this.additive = additive; } - public void setLevel(Level level) { - this.level = level; - } - public void warn(Object message) { if (getRepository().isDisabled(Level.WARN_INT)) { return; @@ -233,12 +228,12 @@ public interface LoggerRepository { boolean isDisabled(int level); - void setLogLevel(Level level); - void emitNoAppenderWarning(Logger cat); Level getLogLevel(); + void setLogLevel(Level level); + Logger getLogger(String name); Logger getRootLogger(); @@ -260,7 +255,7 @@ public static class ProvisionNode extends Vector { public static class DefaultLoggerRepository implements LoggerRepository { - final Hashtable ht = new Hashtable(); + final Hashtable ht = new Hashtable(); Logger root; int logLevelInt; @@ -290,6 +285,10 @@ public Logger exists(String name) { } } + public Level getLogLevel() { + return logLevel; + } + public void setLogLevel(Level l) { if (l != null) { logLevelInt = l.level; @@ -297,11 +296,6 @@ public void setLogLevel(Level l) { } } - public Level getLogLevel() { - return logLevel; - } - - public Logger getLogger(String name) { CategoryKey key = new CategoryKey(name); Logger logger; @@ -338,7 +332,7 @@ public Enumeration getCurrentLoggers() { while (elems.hasMoreElements()) { Object o = elems.nextElement(); if (o instanceof Logger) { - Logger logger = (Logger)o; + Logger logger = (Logger) o; loggers.addElement(logger); } } diff --git a/java/osx/core/src/main/java/com/osx/core/log/LoggingBuilder.java b/java/osx/core/src/main/java/com/osx/core/log/LoggingBuilder.java index 28d584ae72..7a2c053469 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/LoggingBuilder.java +++ b/java/osx/core/src/main/java/com/osx/core/log/LoggingBuilder.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; import java.io.*; @@ -19,6 +17,10 @@ public static AppenderBuilder newAppenderBuilder() { return new AppenderBuilder(); } + public static LayoutBuilder newLayoutBuilder() { + return new LayoutBuilder(); + } + public static class AppenderBuilder { private AsyncAppender asyncAppender; @@ -113,13 +115,9 @@ public static class AsyncAppender extends Appender implements Appender.AppenderP private final List buffer = new ArrayList(); private final Map discardMap = new HashMap(); - - private int bufferSize = DEFAULT_BUFFER_SIZE; - private final AppenderPipelineImpl appenderPipeline; - private final Thread dispatcher; - + private int bufferSize = DEFAULT_BUFFER_SIZE; private boolean blocking = true; public AsyncAppender() { @@ -167,8 +165,8 @@ public void append(final LoggingEvent event) { boolean discard = true; if (blocking - && !Thread.interrupted() - && Thread.currentThread() != dispatcher) { + && !Thread.interrupted() + && Thread.currentThread() != dispatcher) { try { buffer.wait(); discard = false; @@ -205,8 +203,8 @@ public void close() { } catch (InterruptedException e) { Thread.currentThread().interrupt(); SysLogger.error( - "Got an InterruptedException while waiting for the " - + "dispatcher to finish.", e); + "Got an InterruptedException while waiting for the " + + "dispatcher to finish.", e); } synchronized (appenderPipeline) { @@ -258,6 +256,10 @@ public void removeAppender(final String name) { } } + public int getBufferSize() { + return bufferSize; + } + public void setBufferSize(final int size) { if (size < 0) { throw new NegativeArraySizeException("size"); @@ -269,8 +271,8 @@ public void setBufferSize(final int size) { } } - public int getBufferSize() { - return bufferSize; + public boolean getBlocking() { + return blocking; } public void setBlocking(final boolean value) { @@ -280,10 +282,6 @@ public void setBlocking(final boolean value) { } } - public boolean getBlocking() { - return blocking; - } - private final class DiscardSummary { private LoggingEvent maxEvent; @@ -304,16 +302,16 @@ public void add(final LoggingEvent event) { public LoggingEvent createEvent() { String msg = - MessageFormat.format( - "Discarded {0} messages due to full event buffer including: {1}", - count, maxEvent.getMessage()); + MessageFormat.format( + "Discarded {0} messages due to full event buffer including: {1}", + count, maxEvent.getMessage()); return new LoggingEvent( - "AsyncAppender.DONT_REPORT_LOCATION", - Logger.getLogger(maxEvent.getLoggerName()), - maxEvent.getLevel(), - msg, - null); + "AsyncAppender.DONT_REPORT_LOCATION", + Logger.getLogger(maxEvent.getLoggerName()), + maxEvent.getLevel(), + msg, + null); } } @@ -328,8 +326,8 @@ private class Dispatcher implements Runnable { private final AppenderPipelineImpl appenderPipeline; public Dispatcher( - final AsyncAppender parent, final List buffer, final Map discardMap, - final AppenderPipelineImpl appenderPipeline) { + final AsyncAppender parent, final List buffer, final Map discardMap, + final AppenderPipelineImpl appenderPipeline) { this.parent = parent; this.buffer = buffer; @@ -400,7 +398,7 @@ public void write(String string) { out.write(string); } catch (Exception e) { appender.handleError("Failed to write [" + string + "].", e, - Appender.CODE_WRITE_FAILURE); + Appender.CODE_WRITE_FAILURE); } } } @@ -410,7 +408,7 @@ public void flush() { out.flush(); } catch (Exception e) { appender.handleError("Failed to flush writer,", e, - Appender.CODE_FLUSH_FAILURE); + Appender.CODE_FLUSH_FAILURE); } } } @@ -429,15 +427,14 @@ public WriterAppender() { } - public void setImmediateFlush(boolean value) { - immediateFlush = value; - } - - public boolean getImmediateFlush() { return immediateFlush; } + public void setImmediateFlush(boolean value) { + immediateFlush = value; + } + public void activateOptions() { } @@ -457,7 +454,7 @@ protected boolean checkEntryConditions() { if (this.qw == null) { handleError("No output stream or file set for the appender named [" + - name + "]."); + name + "]."); return false; } @@ -567,7 +564,6 @@ protected boolean shouldFlush(final LoggingEvent event) { } } - public static class FileAppender extends WriterAppender { protected boolean fileAppend = true; @@ -582,30 +578,34 @@ public FileAppender() { } public FileAppender(Layout layout, String filename, boolean append) - throws IOException { + throws IOException { this.layout = layout; this.setFile(filename, append, false, bufferSize); } - public void setFile(String file) { - fileName = file.trim(); - } - public boolean getAppend() { return fileAppend; } + public void setAppend(boolean flag) { + fileAppend = flag; + } + public String getFile() { return fileName; } + public void setFile(String file) { + fileName = file.trim(); + } + public void activateOptions() { if (fileName != null) { try { setFile(fileName, fileAppend, bufferedIO, bufferSize); } catch (IOException e) { handleError("setFile(" + fileName + "," + fileAppend + ") call failed.", - e, CODE_FILE_OPEN_FAILURE); + e, CODE_FILE_OPEN_FAILURE); } } else { SysLogger.warn("File option not set for appender [" + name + "]."); @@ -630,14 +630,6 @@ public boolean getBufferedIO() { return this.bufferedIO; } - public int getBufferSize() { - return this.bufferSize; - } - - public void setAppend(boolean flag) { - fileAppend = flag; - } - public void setBufferedIO(boolean bufferedIO) { this.bufferedIO = bufferedIO; if (bufferedIO) { @@ -645,12 +637,16 @@ public void setBufferedIO(boolean bufferedIO) { } } + public int getBufferSize() { + return this.bufferSize; + } + public void setBufferSize(int bufferSize) { this.bufferSize = bufferSize; } public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) - throws IOException { + throws IOException { SysLogger.debug("setFile called: " + fileName + ", " + append); if (bufferedIO) { @@ -698,7 +694,6 @@ protected void reset() { } } - public static class RollingFileAppender extends FileAppender { protected long maxFileSize = 10 * 1024 * 1024; @@ -715,10 +710,18 @@ public int getMaxBackupIndex() { return maxBackupIndex; } + public void setMaxBackupIndex(int maxBackups) { + this.maxBackupIndex = maxBackups; + } + public long getMaximumFileSize() { return maxFileSize; } + public void setMaximumFileSize(long maxFileSize) { + this.maxFileSize = maxFileSize; + } + public void rollOver() { File target; File file; @@ -782,7 +785,7 @@ public void rollOver() { } public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) - throws IOException { + throws IOException { super.setFile(fileName, append, this.bufferedIO, this.bufferSize); if (append) { File f = new File(fileName); @@ -790,14 +793,6 @@ public synchronized void setFile(String fileName, boolean append, boolean buffer } } - public void setMaxBackupIndex(int maxBackups) { - this.maxBackupIndex = maxBackups; - } - - public void setMaximumFileSize(long maxFileSize) { - this.maxFileSize = maxFileSize; - } - protected void setQWForFiles(Writer writer) { this.qw = new CountingQuietWriter(writer, this); } @@ -840,7 +835,6 @@ public void setCount(long count) { } } - public static class DailyRollingFileAppender extends FileAppender { static final int TOP_OF_TROUBLE = -1; @@ -850,35 +844,26 @@ public static class DailyRollingFileAppender extends FileAppender { static final int TOP_OF_DAY = 3; static final int TOP_OF_WEEK = 4; static final int TOP_OF_MONTH = 5; - - + final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT"); + Date now = new Date(); + SimpleDateFormat sdf; + RollingCalendar rc = new RollingCalendar(); /** * The date pattern. By default, the pattern is set to * "'.'yyyy-MM-dd" meaning daily rollover. */ private String datePattern = "'.'yyyy-MM-dd"; - private String scheduledFilename; - private long nextCheck = System.currentTimeMillis() - 1; - Date now = new Date(); - - SimpleDateFormat sdf; - - RollingCalendar rc = new RollingCalendar(); - - final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT"); - + public String getDatePattern() { + return datePattern; + } public void setDatePattern(String pattern) { datePattern = pattern; } - public String getDatePattern() { - return datePattern; - } - public void activateOptions() { super.activateOptions(); if (datePattern != null && fileName != null) { @@ -1078,6 +1063,10 @@ public static class ConsoleAppender extends WriterAppender { public ConsoleAppender() { } + public String getTarget() { + return target; + } + public void setTarget(String value) { String v = value.trim(); @@ -1090,10 +1079,6 @@ public void setTarget(String value) { } } - public String getTarget() { - return target; - } - void targetWarn(String val) { SysLogger.warn("[" + val + "] should be System.out or System.err."); SysLogger.warn("Using previously set target, System.out by default."); @@ -1113,10 +1098,6 @@ protected final void closeWriter() { } } - public static LayoutBuilder newLayoutBuilder() { - return new LayoutBuilder(); - } - public static class LayoutBuilder { private Layout layout; diff --git a/java/osx/core/src/main/java/com/osx/core/log/LoggingEvent.java b/java/osx/core/src/main/java/com/osx/core/log/LoggingEvent.java index 6058b2b55c..788d665b94 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/LoggingEvent.java +++ b/java/osx/core/src/main/java/com/osx/core/log/LoggingEvent.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; import java.io.*; @@ -8,19 +6,12 @@ public class LoggingEvent implements Serializable { transient public final String fqnOfCategoryClass; - + public final long timeStamp; transient private Object message; - transient private Level level; - transient private Logger logger; - private String renderedMessage; - private String threadName; - - public final long timeStamp; - private Throwable throwable; public LoggingEvent(String fqnOfCategoryClass, Logger logger, @@ -83,7 +74,7 @@ public String[] getThrowableStr() { } pw.flush(); LineNumberReader reader = new LineNumberReader( - new StringReader(sw.toString())); + new StringReader(sw.toString())); ArrayList lines = new ArrayList(); try { String line = reader.readLine(); diff --git a/java/osx/core/src/main/java/com/osx/core/log/Slf4jLoggerFactory.java b/java/osx/core/src/main/java/com/osx/core/log/Slf4jLoggerFactory.java index c15f1a7cfc..8f1896419d 100644 --- a/java/osx/core/src/main/java/com/osx/core/log/Slf4jLoggerFactory.java +++ b/java/osx/core/src/main/java/com/osx/core/log/Slf4jLoggerFactory.java @@ -1,5 +1,3 @@ - - package com.osx.core.log; import org.slf4j.Logger; diff --git a/java/osx/core/src/main/java/com/osx/core/log/SysLogger.java b/java/osx/core/src/main/java/com/osx/core/log/SysLogger.java index dec270bf57..ff3df9500a 100755 --- a/java/osx/core/src/main/java/com/osx/core/log/SysLogger.java +++ b/java/osx/core/src/main/java/com/osx/core/log/SysLogger.java @@ -1,16 +1,12 @@ - - package com.osx.core.log; public class SysLogger { - protected static boolean debugEnabled = false; - - private static boolean quietMode = false; - private static final String PREFIX = "RocketMQLog: "; private static final String ERR_PREFIX = "RocketMQLog:ERROR "; private static final String WARN_PREFIX = "RocketMQLog:WARN "; + protected static boolean debugEnabled = false; + private static boolean quietMode = false; public static void setInternalDebugging(boolean enabled) { debugEnabled = enabled; diff --git a/java/osx/core/src/main/java/com/osx/core/provider/TechProvider.java b/java/osx/core/src/main/java/com/osx/core/provider/TechProvider.java index 09f54b04e3..65010dabc4 100644 --- a/java/osx/core/src/main/java/com/osx/core/provider/TechProvider.java +++ b/java/osx/core/src/main/java/com/osx/core/provider/TechProvider.java @@ -1,12 +1,21 @@ package com.osx.core.provider; + import io.grpc.stub.StreamObserver; -import org.ppc.ptp.Pcp; -import org.ppc.ptp.Pcp.Inbound; +import org.ppc.ptp.Osx; + + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + public interface TechProvider { - void processInvoke(Inbound request, - io.grpc.stub.StreamObserver responseObserver); - String getProviderId(); - public StreamObserver processTransport(Pcp.Inbound inbound,io.grpc.stub.StreamObserver responseObserver); + void processHttpInvoke(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse); + + void processGrpcInvoke(Osx.Inbound request, + io.grpc.stub.StreamObserver responseObserver); + + String getProviderId(); + + public StreamObserver processGrpcTransport(Osx.Inbound inbound, io.grpc.stub.StreamObserver responseObserver); } diff --git a/java/osx/core/src/main/java/com/osx/core/ptp/TargetMethod.java b/java/osx/core/src/main/java/com/osx/core/ptp/TargetMethod.java index d911c70f68..4d351326a7 100644 --- a/java/osx/core/src/main/java/com/osx/core/ptp/TargetMethod.java +++ b/java/osx/core/src/main/java/com/osx/core/ptp/TargetMethod.java @@ -2,7 +2,7 @@ public enum TargetMethod { -// this.serviceAdaptorConcurrentMap.put("UNARY_CALL", new UnaryCallService()); + // this.serviceAdaptorConcurrentMap.put("UNARY_CALL", new UnaryCallService()); // this.serviceAdaptorConcurrentMap.put("PRODUCE_MSG",new PtpProduceService().addPreProcessor(requestHandleInterceptor)); // this.serviceAdaptorConcurrentMap.put("ACK_MSG",new PtpAckService().addPreProcessor(requestHandleInterceptor)); // this.serviceAdaptorConcurrentMap.put("CONSUME_MSG",new PtpConsumeService().addPreProcessor(requestHandleInterceptor)); diff --git a/java/osx/core/src/main/java/com/osx/core/queue/ClusterTransferQueueInfo.java b/java/osx/core/src/main/java/com/osx/core/queue/ClusterTransferQueueInfo.java index 27b306551c..73ceabe71a 100644 --- a/java/osx/core/src/main/java/com/osx/core/queue/ClusterTransferQueueInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/queue/ClusterTransferQueueInfo.java @@ -3,50 +3,50 @@ import com.osx.core.utils.JsonUtil; -public class ClusterTransferQueueInfo { +public class ClusterTransferQueueInfo { - public ClusterTransferQueueInfo(){ + String tranferId; + String ip; + int port; + long createTimestamp; + public ClusterTransferQueueInfo() { - } - String tranferId; - String ip; - int port; - long createTimestamp; + } - public String getTranferId() { - return tranferId; - } + public String getTranferId() { + return tranferId; + } - public void setTranferId(String tranferId) { - this.tranferId = tranferId; - } + public void setTranferId(String tranferId) { + this.tranferId = tranferId; + } - public String getIp() { - return ip; - } + public String getIp() { + return ip; + } - public void setIp(String ip) { - this.ip = ip; - } + public void setIp(String ip) { + this.ip = ip; + } - public int getPort() { - return port; - } + public int getPort() { + return port; + } - public void setPort(int port) { - this.port = port; - } + public void setPort(int port) { + this.port = port; + } - public long getCreateTimestamp() { - return createTimestamp; - } + public long getCreateTimestamp() { + return createTimestamp; + } - public void setCreateTimestamp(long createTimestamp) { - this.createTimestamp = createTimestamp; - } + public void setCreateTimestamp(long createTimestamp) { + this.createTimestamp = createTimestamp; + } - public String toString(){ - return JsonUtil.object2Json(this); - } + public String toString() { + return JsonUtil.object2Json(this); + } - } \ No newline at end of file +} \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/queue/TranferQueueInfo.java b/java/osx/core/src/main/java/com/osx/core/queue/TranferQueueInfo.java index 968088667a..f1c7b24b87 100644 --- a/java/osx/core/src/main/java/com/osx/core/queue/TranferQueueInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/queue/TranferQueueInfo.java @@ -10,6 +10,7 @@ public class TranferQueueInfo { long lastStatusChangeTimestamp; long lastWriteTimestamp; long lastReadTimestamp; + long logicOffset; public long getLogicOffset() { return logicOffset; @@ -19,8 +20,6 @@ public void setLogicOffset(long logicOffset) { this.logicOffset = logicOffset; } - long logicOffset; - public String getTransferId() { return transferId; } diff --git a/java/osx/core/src/main/java/com/osx/core/router/RouterInfo.java b/java/osx/core/src/main/java/com/osx/core/router/RouterInfo.java index 33fd9449c1..5197a8ecf6 100644 --- a/java/osx/core/src/main/java/com/osx/core/router/RouterInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/router/RouterInfo.java @@ -29,7 +29,7 @@ public class RouterInfo { private String url; private String host; private Integer port; - private boolean useSSL=false; + private boolean useSSL = false; private String negotiationType; private String certChainFile; private String privateKeyFile; @@ -37,9 +37,9 @@ public class RouterInfo { private String caFile; private String version; - public String toKey(){ - StringBuffer sb = new StringBuffer(); - return sb.append(host).append("_").append(port).append("_").append(negotiationType).toString(); + public String toKey() { + StringBuffer sb = new StringBuffer(); + return sb.append(host).append("_").append(port).append("_").append(negotiationType).toString(); } @Override @@ -47,12 +47,11 @@ public String toString() { return JsonUtil.object2Json(this); } - public String getResource(){ + public String getResource() { StringBuilder sb = new StringBuilder(); sb.append(sourcePartyId).append("-").append(desPartyId); return sb.toString(); } - } \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/service/AbstractServiceAdaptor.java b/java/osx/core/src/main/java/com/osx/core/service/AbstractServiceAdaptor.java index b29e475777..4a81e442e7 100644 --- a/java/osx/core/src/main/java/com/osx/core/service/AbstractServiceAdaptor.java +++ b/java/osx/core/src/main/java/com/osx/core/service/AbstractServiceAdaptor.java @@ -16,18 +16,15 @@ package com.osx.core.service; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.osx.core.constant.Dict; import com.osx.core.constant.StatusCode; import com.osx.core.context.Context; import com.osx.core.exceptions.ErrorMessageUtil; import com.osx.core.exceptions.ExceptionInfo; import com.osx.core.utils.JsonUtil; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - - import io.grpc.stub.AbstractStub; -import lombok.Data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,7 +38,7 @@ * @Author **/ - public abstract class AbstractServiceAdaptor implements ServiceAdaptor { +public abstract class AbstractServiceAdaptor implements ServiceAdaptor { static public AtomicInteger requestInHandle = new AtomicInteger(0); @@ -54,12 +51,13 @@ public abstract class AbstractServiceAdaptor implements ServiceAdapto InterceptorChain postChain = new DefaultInterceptorChain(); private Map methodMap = Maps.newHashMap(); private AbstractStub serviceStub; + public AbstractServiceAdaptor() { } - public void registerMethod(String actionType,Method method){ - this.methodMap.put(actionType,method); + public void registerMethod(String actionType, Method method) { + this.methodMap.put(actionType, method); } @@ -71,7 +69,7 @@ public void setMethodMap(Map methodMap) { this.methodMap = methodMap; } - public AbstractServiceAdaptor addPreProcessor(Interceptor interceptor) { + public AbstractServiceAdaptor addPreProcessor(Interceptor interceptor) { preChain.addInterceptor(interceptor); return this; } @@ -116,13 +114,13 @@ public void setServiceName(String serviceName) { public OutboundPackage service(Context context, InboundPackage data) throws RuntimeException { OutboundPackage outboundPackage = new OutboundPackage(); - // context.preProcess(); + // context.preProcess(); List exceptions = Lists.newArrayList(); context.setReturnCode(StatusCode.SUCCESS); // if (!isOpen) { // return this.serviceFailInner(context, data, new ShowDownRejectException()); // } - if(data.getBody()!=null) { + if (data.getBody() != null) { context.putData(Dict.INPUT_DATA, data.getBody()); } @@ -146,29 +144,29 @@ public OutboundPackage service(Context context, InboundPackage data) } catch (Throwable e) { exceptions.add(e); - logger.error("service error",e); + logger.error("service error", e); } finally { requestInHandle.decrementAndGet(); try { if (exceptions.size() != 0) { outboundPackage = this.serviceFail(context, data, exceptions); } - }finally { + } finally { printFlowLog(context); } - // int returnCode = context.getReturnCode(); + // int returnCode = context.getReturnCode(); // if(outboundPackage.getData()!=null) { // context.putData(Dict.OUTPUT_DATA, outboundPackage.getData()); // } - // context.postProcess(data, outboundPackage); + // context.postProcess(data, outboundPackage); } return outboundPackage; } protected void printFlowLog(Context context) { - + context.printFlowLog(); // flowLogger.info("{}|{}|{}|{}|" + @@ -183,8 +181,9 @@ protected void printFlowLog(Context context) { protected OutboundPackage serviceFailInner(Context context, InboundPackage data, Throwable e) { OutboundPackage outboundPackage = new OutboundPackage(); - ExceptionInfo exceptionInfo = ErrorMessageUtil.handleExceptionExceptionInfo(context ,e); + ExceptionInfo exceptionInfo = ErrorMessageUtil.handleExceptionExceptionInfo(context, e); context.setReturnCode(exceptionInfo.getCode()); + context.setReturnMsg(exceptionInfo.getMessage()); resp rsp = transformExceptionInfo(context, exceptionInfo); outboundPackage.setData(rsp); outboundPackage.setThrowable(exceptionInfo.getThrowable()); @@ -198,7 +197,7 @@ public OutboundPackage serviceFail(Context context, InboundPackage da return serviceFailInner(context, data, e); } - protected abstract resp transformExceptionInfo(Context context, ExceptionInfo exceptionInfo); + protected abstract resp transformExceptionInfo(Context context, ExceptionInfo exceptionInfo); } \ No newline at end of file diff --git a/java/osx/core/src/main/java/com/osx/core/service/DefaultInterceptorChain.java b/java/osx/core/src/main/java/com/osx/core/service/DefaultInterceptorChain.java index 89749180e9..c1bfc5d9eb 100644 --- a/java/osx/core/src/main/java/com/osx/core/service/DefaultInterceptorChain.java +++ b/java/osx/core/src/main/java/com/osx/core/service/DefaultInterceptorChain.java @@ -16,9 +16,8 @@ package com.osx.core.service; -import com.osx.core.context.Context; import com.google.common.collect.Lists; - +import com.osx.core.context.Context; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/java/osx/core/src/main/java/com/osx/core/service/InboundPackage.java b/java/osx/core/src/main/java/com/osx/core/service/InboundPackage.java index c4f7091a6e..6d3e0da782 100644 --- a/java/osx/core/src/main/java/com/osx/core/service/InboundPackage.java +++ b/java/osx/core/src/main/java/com/osx/core/service/InboundPackage.java @@ -17,7 +17,6 @@ package com.osx.core.service; import com.osx.core.router.RouterInfo; - import io.grpc.ManagedChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/java/osx/core/src/main/java/com/osx/core/service/OutboundPackage.java b/java/osx/core/src/main/java/com/osx/core/service/OutboundPackage.java index bfcba8d746..2487ef2b77 100644 --- a/java/osx/core/src/main/java/com/osx/core/service/OutboundPackage.java +++ b/java/osx/core/src/main/java/com/osx/core/service/OutboundPackage.java @@ -22,6 +22,9 @@ **/ public class OutboundPackage { + Throwable throwable; + T data; + public Throwable getThrowable() { return throwable; } @@ -30,10 +33,6 @@ public void setThrowable(Throwable throwable) { this.throwable = throwable; } - Throwable throwable; - - T data; - public T getData() { return data; } diff --git a/java/osx/core/src/main/java/com/osx/core/service/ServiceAdaptor.java b/java/osx/core/src/main/java/com/osx/core/service/ServiceAdaptor.java index b3641f5cdc..d526f4c468 100644 --- a/java/osx/core/src/main/java/com/osx/core/service/ServiceAdaptor.java +++ b/java/osx/core/src/main/java/com/osx/core/service/ServiceAdaptor.java @@ -17,7 +17,6 @@ package com.osx.core.service; - import com.osx.core.context.Context; import java.util.List; diff --git a/java/osx/core/src/main/java/com/osx/core/token/TokenResultStatus.java b/java/osx/core/src/main/java/com/osx/core/token/TokenResultStatus.java index 83e9d991e5..93be406bdc 100644 --- a/java/osx/core/src/main/java/com/osx/core/token/TokenResultStatus.java +++ b/java/osx/core/src/main/java/com/osx/core/token/TokenResultStatus.java @@ -48,7 +48,7 @@ public final class TokenResultStatus { /** * Token already is released before the request arrives. */ - public static final int ALREADY_RELEASE=7; + public static final int ALREADY_RELEASE = 7; private TokenResultStatus() { } diff --git a/java/osx/core/src/main/java/com/osx/core/utils/AssertUtil.java b/java/osx/core/src/main/java/com/osx/core/utils/AssertUtil.java index ddf5a5b4f6..491988b5d9 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/AssertUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/AssertUtil.java @@ -1,4 +1,3 @@ - package com.osx.core.utils; @@ -8,7 +7,8 @@ public class AssertUtil { - private AssertUtil(){} + private AssertUtil() { + } public static void notEmpty(String string, String message) { if (StringUtils.isEmpty(string)) { diff --git a/java/osx/core/src/main/java/com/osx/core/utils/EncryptUtils.java b/java/osx/core/src/main/java/com/osx/core/utils/EncryptUtils.java index fea5a4e46f..7c676ad2fc 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/EncryptUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/EncryptUtils.java @@ -17,6 +17,7 @@ package com.osx.core.utils; import com.osx.core.constant.EncryptMethod; + import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; diff --git a/java/osx/core/src/main/java/com/osx/core/utils/FlowLogPrinter.java b/java/osx/core/src/main/java/com/osx/core/utils/FlowLogPrinter.java index 20de6b579b..e35122f660 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/FlowLogPrinter.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/FlowLogPrinter.java @@ -4,5 +4,5 @@ public interface FlowLogPrinter { - public void print(Context context); + public void print(Context context); } diff --git a/java/osx/core/src/main/java/com/osx/core/utils/FlowLogUtil.java b/java/osx/core/src/main/java/com/osx/core/utils/FlowLogUtil.java index 6b0c2dfcc7..0e17f0da01 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/FlowLogUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/FlowLogUtil.java @@ -1,21 +1,55 @@ package com.osx.core.utils; + import com.osx.core.context.Context; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FlowLogUtil { static Logger logger = LoggerFactory.getLogger("flow"); + static final String SPLIT= "|"; + public static void printFlowLog(Context context) { + + + StringBuffer stringBuffer = new StringBuffer(); + if(context.getActionType()!=null){ + stringBuffer.append(context.getActionType()).append(SPLIT); + } + if(context.getSessionId()!=null){ + stringBuffer.append("session:").append(context.getSessionId()).append(SPLIT); + } + if(context.getCaseId()!=null){ + stringBuffer.append("seq:").append(context.getCaseId()).append(SPLIT); + } + if(context.getSrcPartyId()!=null){ + stringBuffer.append("src:").append(context.getSrcPartyId()).append(SPLIT); + } + if(context.getDesPartyId()!=null){ + stringBuffer.append("des:").append(context.getDesPartyId()).append(SPLIT); + } + if(context.getReturnCode()!=null){ + stringBuffer.append("code:").append(context.getReturnCode()).append(SPLIT); + } + stringBuffer.append("cost:").append(System.currentTimeMillis() - context.getTimeStamp()).append(SPLIT); + if(context.getRouterInfo()!=null){ + stringBuffer.append("router_info:").append(context.getRouterInfo().getHost() + ":" + context.getRouterInfo().getPort()).append(SPLIT); + } + if(context.getDataSize()!=null){ + stringBuffer.append("size:").append(context.getDataSize()).append(SPLIT); + } - public static void printFlowLog(Context context) { + if(context.getReturnMsg()!=null){ + stringBuffer.append("msg:").append(context.getReturnMsg()); + } + logger.info(stringBuffer.toString()); - logger.info("{}|{}|seq:{}|topic:{}|src:{}|des:{}|" + - "code:{}|cost:{}|send_to:{}|" + - "size:{}|msg:{}", - context.getActionType(),context.getSessionId(),context.getCaseId(), context.getTopic(), context.getSrcPartyId(), - context.getDesPartyId(),context.getReturnCode(),(System.currentTimeMillis()-context.getTimeStamp()) - , context.getRouterInfo() != null ? context.getRouterInfo().getHost()+":"+context.getRouterInfo().getPort() : "" - ,context.getDataSize() ,context.getException()!=null?context.getException().getMessage():"","" - ); +// logger.info("{}|{}|seq:{}|topic:{}|src:{}|des:{}|" + +// "code:{}|cost:{}|send_to:{}|" + +// "size:{}|msg:{}", +// context.getActionType(), context.getSessionId(), context.getCaseId(), context.getTopic(), context.getSrcPartyId(), +// context.getDesPartyId(), context.getReturnCode(), (System.currentTimeMillis() - context.getTimeStamp()) +// , context.getRouterInfo() != null ? context.getRouterInfo().getHost() + ":" + context.getRouterInfo().getPort() : "" +// , context.getDataSize(), context.getReturnMsg()!=null? context.getReturnMsg():"" +// ); } @@ -27,31 +61,29 @@ public static void printFlowLog(Context context) { // ); // } - public static void printFlowLogForConsumer(Context context) { + public static void printFlowLogForConsumer(Context context) { logger.info("{}|{}|{}|{}|" + "{}|{}|{}|" + "{}|{}", context.getActionType(), context.getTopic(), context.getRequestMsgIndex(), - context.getCurrentMsgIndex(),context.getReturnCode(),System.currentTimeMillis()-context.getTimeStamp() - , context.getRouterInfo() != null ? context.getRouterInfo().getHost()+":"+context.getRouterInfo().getPort() : "" - ,context.getDataSize() ,context.getException()!=null?context.getException().getMessage():"","" + context.getCurrentMsgIndex(), context.getReturnCode(), System.currentTimeMillis() - context.getTimeStamp() + , context.getRouterInfo() != null ? context.getRouterInfo().getHost() + ":" + context.getRouterInfo().getPort() : "" + , context.getDataSize(), context.getException() != null ? context.getException().getMessage() : "", "" ); } - public static void printFlowLogForAck(Context context) { + public static void printFlowLogForAck(Context context) { logger.info("{}|{}|{}|{}|" + "{}|{}|{}|" + "{}|{}", context.getActionType(), context.getTopic(), context.getRequestMsgIndex(), - context.getCurrentMsgIndex(),context.getReturnCode(),System.currentTimeMillis()-context.getTimeStamp() - , context.getRouterInfo() != null ? context.getRouterInfo().getHost()+":"+context.getRouterInfo().getPort() : "" - ,context.getDataSize() ,context.getException()!=null?context.getException().getMessage():"","" + context.getCurrentMsgIndex(), context.getReturnCode(), System.currentTimeMillis() - context.getTimeStamp() + , context.getRouterInfo() != null ? context.getRouterInfo().getHost() + ":" + context.getRouterInfo().getPort() : "" + , context.getDataSize(), context.getException() != null ? context.getException().getMessage() : "", "" ); } - - } diff --git a/java/osx/core/src/main/java/com/osx/core/utils/GetSystemInfo.java b/java/osx/core/src/main/java/com/osx/core/utils/GetSystemInfo.java index 7e0fd0165f..4192462b6f 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/GetSystemInfo.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/GetSystemInfo.java @@ -16,11 +16,9 @@ package com.osx.core.utils; -import com.sun.management.OperatingSystemMXBean; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -//import sun.management.ManagementFactoryHelper; import java.io.IOException; import java.lang.management.GarbageCollectorMXBean; @@ -235,7 +233,7 @@ static boolean isValidV4Address(InetAddress address) { //// } - public static void getJVMRuntimeParam(){ + public static void getJVMRuntimeParam() { // RuntimeMXBean runtimeMXBean = ManagementFactoryHelper.getRuntimeMXBean(); //JVM启动参数 @@ -261,7 +259,7 @@ private static void reportGC() { long fullCount = 0, fullTime = 0, youngCount = 0, youngTime = 0; List gcs = ManagementFactory.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean gc : gcs) { - System.err.println(gc.getName() +"" +gc.getCollectionCount()); + System.err.println(gc.getName() + "" + gc.getCollectionCount()); } @@ -279,10 +277,8 @@ private static void reportGC() { // youngTime += gc.getCollectionTime(); // break; // } - //todo your deal code, perfcounter report or write log here - } - - + //todo your deal code, perfcounter report or write log here + } // public static void getSystemLoad(){ @@ -298,16 +294,14 @@ private static void reportGC() { // } - public static void main(String[] args){ + public static void main(String[] args) { - // getSystemLoad(); + // getSystemLoad(); reportGC(); - } - } diff --git a/java/osx/core/src/main/java/com/osx/core/utils/JsonUtil.java b/java/osx/core/src/main/java/com/osx/core/utils/JsonUtil.java index 912a502334..2b9e026859 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/JsonUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/JsonUtil.java @@ -22,7 +22,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.MessageOrBuilder; import org.apache.commons.lang3.StringUtils; @@ -108,29 +107,29 @@ public static T json2Object(String json, TypeReference tr) { // return JsonParser.parseString(json).getAsJsonObject(); // } - public static T json2Object(JsonObject source,Class clazz){ + public static T json2Object(JsonObject source, Class clazz) { String json = source.toString(); - return json2Object(json,clazz); + return json2Object(json, clazz); } - public static T object2Objcet(Object source,Class clazz){ + public static T object2Objcet(Object source, Class clazz) { String json = object2Json(source); - return json2Object(json,clazz); + return json2Object(json, clazz); } - public static T object2Objcet(Object source,TypeReference tr){ + public static T object2Objcet(Object source, TypeReference tr) { String json = object2Json(source); - return json2Object(json,tr); + return json2Object(json, tr); } public static String formatJson(String jsonStr) { - return formatJson(jsonStr," "); + return formatJson(jsonStr, " "); } /*** * format json string */ - public static String formatJson(String jsonStr,String formatChar) { + public static String formatJson(String jsonStr, String formatChar) { if (null == jsonStr || "".equals(jsonStr)) return ""; jsonStr = jsonStr.replace("\\n", ""); StringBuilder sb = new StringBuilder(); @@ -154,7 +153,7 @@ public static String formatJson(String jsonStr,String formatChar) { if (!isInQuotationMarks) { sb.append('\n'); indent++; - addIndentTab(sb, indent,formatChar); + addIndentTab(sb, indent, formatChar); } break; case '}': @@ -162,7 +161,7 @@ public static String formatJson(String jsonStr,String formatChar) { if (!isInQuotationMarks) { sb.append('\n'); indent--; - addIndentTab(sb, indent,formatChar); + addIndentTab(sb, indent, formatChar); } sb.append(current); break; @@ -170,7 +169,7 @@ public static String formatJson(String jsonStr,String formatChar) { sb.append(current); if (last != '\\' && !isInQuotationMarks) { sb.append('\n'); - addIndentTab(sb, indent,formatChar); + addIndentTab(sb, indent, formatChar); } break; case ' ': @@ -189,19 +188,19 @@ public static String formatJson(String jsonStr,String formatChar) { return sb.toString(); } - private static void addIndentTab(StringBuilder sb, int indent,String formatChar) { + private static void addIndentTab(StringBuilder sb, int indent, String formatChar) { for (int i = 0; i < indent; i++) { sb.append(formatChar); } } - public static String pbToJson(MessageOrBuilder message){ + public static String pbToJson(MessageOrBuilder message) { try { - return com.google.protobuf.util.JsonFormat.printer().print(message); + return com.google.protobuf.util.JsonFormat.printer().print(message); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } - return ""; + return ""; } diff --git a/java/osx/core/src/main/java/com/osx/core/utils/NetUtils.java b/java/osx/core/src/main/java/com/osx/core/utils/NetUtils.java index 050174dc36..8834c29d36 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/NetUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/NetUtils.java @@ -65,9 +65,10 @@ public static int getRandomPort() { } - public static boolean isLocalhostAddress(String address){ - return LOCALHOST_PATTERN.matcher(address).matches(); + public static boolean isLocalhostAddress(String address) { + return LOCALHOST_PATTERN.matcher(address).matches(); } + public static int getAvailablePort() { try (ServerSocket ss = new ServerSocket()) { ss.bind(null); diff --git a/java/osx/core/src/main/java/com/osx/core/utils/ParameterUtils.java b/java/osx/core/src/main/java/com/osx/core/utils/ParameterUtils.java index cec544e170..b3ea80448a 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/ParameterUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/ParameterUtils.java @@ -1,13 +1,13 @@ -package com.osx.core.utils; - -import com.osx.core.exceptions.ParameterException; -import org.checkerframework.checker.nullness.qual.Nullable; - - -public class ParameterUtils { - public static void checkArgument(boolean expression, @Nullable String errorMessage) { - if (!expression) { - throw new ParameterException(errorMessage); - } - } -} +//package com.osx.core.utils; +// +//import com.osx.core.exceptions.ParameterException; +//import org.checkerframework.checker.nullness.qual.Nullable; +// +// +//public class ParameterUtils { +// public static void checkArgument(boolean expression, @Nullable String errorMessage) { +// if (!expression) { +// throw new ParameterException(errorMessage); +// } +// } +//} diff --git a/java/osx/core/src/main/java/com/osx/core/utils/RouterUtil.java b/java/osx/core/src/main/java/com/osx/core/utils/RouterUtil.java index 77125e8cb5..56535c2dd9 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/RouterUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/RouterUtil.java @@ -17,10 +17,11 @@ public class RouterUtil { // return "receive_"+destination; // } - public static String getReceiveKey(String key){ - return "receive_"+key; + public static String getReceiveKey(String key) { + return "receive_" + key; } - public static String getSendKey(String key){ - return "to_"+key; + + public static String getSendKey(String key) { + return "to_" + key; } } diff --git a/java/osx/core/src/main/java/com/osx/core/utils/ServerUtil.java b/java/osx/core/src/main/java/com/osx/core/utils/ServerUtil.java index c352de5ea7..55b692be95 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/ServerUtil.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/ServerUtil.java @@ -1,13 +1,8 @@ - package com.osx.core.utils; +import org.apache.commons.cli.*; + import java.util.Properties; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; public class ServerUtil { @@ -17,8 +12,8 @@ public static Options buildCommandlineOptions(final Options options) { options.addOption(opt); opt = - new Option("n", "namesrvAddr", true, - "Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876"); + new Option("n", "namesrvAddr", true, + "Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876"); opt.setRequired(false); options.addOption(opt); @@ -26,7 +21,7 @@ public static Options buildCommandlineOptions(final Options options) { } public static CommandLine parseCmdLine(final String appName, String[] args, Options options, - CommandLineParser parser) { + CommandLineParser parser) { HelpFormatter hf = new HelpFormatter(); hf.setWidth(110); CommandLine commandLine = null; diff --git a/java/osx/core/src/main/java/com/osx/core/utils/ToStringUtils.java b/java/osx/core/src/main/java/com/osx/core/utils/ToStringUtils.java index e099141a79..029dda7e51 100644 --- a/java/osx/core/src/main/java/com/osx/core/utils/ToStringUtils.java +++ b/java/osx/core/src/main/java/com/osx/core/utils/ToStringUtils.java @@ -1,32 +1,30 @@ package com.osx.core.utils; -import com.osx.core.constant.Dict; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.MessageOrBuilder; import com.google.protobuf.util.JsonFormat; +import com.osx.core.constant.Dict; public class ToStringUtils { - private static JsonFormat.Printer protoPrinter = JsonFormat.printer().preservingProtoFieldNames() + private static JsonFormat.Printer protoPrinter = JsonFormat.printer().preservingProtoFieldNames() .includingDefaultValueFields() .omittingInsignificantWhitespace(); - public static String toOneLineString(MessageOrBuilder target){ + public static String toOneLineString(MessageOrBuilder target) { if (target != null) { try { - return protoPrinter.print(target); + return protoPrinter.print(target); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } - } - else { + } else { return Dict.NULL_WITH_BRACKETS; } - return null; + return null; } - } diff --git a/java/osx/deploy/osx/bin/common.sh b/java/osx/deploy/osx/bin/common.sh index 8c58349b9b..3101efaa39 100644 --- a/java/osx/deploy/osx/bin/common.sh +++ b/java/osx/deploy/osx/bin/common.sh @@ -89,16 +89,17 @@ JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}" set -e getpid() { - if [ -e "./bin/$1.pid" ]; then - pid=$(cat ./bin/$1.pid) + if [ -e "./bin/broker.pid" ]; then + pid=$(cat ./bin/broker.pid) fi if [[ -n ${pid} ]]; then count=$(ps -ef | grep $pid | grep -v "grep" | wc -l) if [[ ${count} -eq 0 ]]; then - rm ./bin/$1.pid + rm ./bin/broker.pid unset pid fi fi + } mklogsdir() { @@ -114,27 +115,6 @@ start() { getpid $module if [[ ! -n ${pid} ]]; then JAVA_OPT="${JAVA_OPT} " mklogsdir - -# case "${module}" in -# broker) -# main_class=com.osx.broker.Bootstrap -# ;; -# cluster-manager) -# main_class=com.firework.cluster.server.bootstrap.Bootstrap -# ;; -# dashboard) -# main_class=com.firework.admin.Bootstrap -# ;; -# cli) -# main_class=com.firework.cli.bootstrap.Bootstrap -# ;; -# *) -# echo "module: $1 {transfer|cluster-manager|dashboard|cli}" -# exit 1 -# esac - - - # if [[ -e "${module}.jar" ]]; then # rm ${module}.jar # fi diff --git a/java/osx/deploy/osx/conf/broker/broker.properties b/java/osx/deploy/osx/conf/broker/broker.properties index ff16ffc8c3..dd24064c02 100644 --- a/java/osx/deploy/osx/conf/broker/broker.properties +++ b/java/osx/deploy/osx/conf/broker/broker.properties @@ -1,4 +1,5 @@ #-------------PLAINTEXT grpc port +http.port=8080 port= 9370 ############ TLS port tls.port = 9883 @@ -7,7 +8,6 @@ self.party=9999 ############standalone/cluster deploy.model=standalone #cluster.manager.address = localhost:9678 -use.queue.model=false ############TLS/PLAINTEXT #server.negotiationType=PLAINTEXT zk.url=localhost:2181 diff --git a/java/osx/deploy/osx/conf/broker/route_table.json b/java/osx/deploy/osx/conf/broker/route_table.json index 59b8782336..6615d092ae 100755 --- a/java/osx/deploy/osx/conf/broker/route_table.json +++ b/java/osx/deploy/osx/conf/broker/route_table.json @@ -18,8 +18,8 @@ }, "10000":{ "default":[{ - "port": 9370, - "ip": "10.42.0.1", + "port": 9889, + "ip": "localhost", "version": 197 }] diff --git a/java/osx/pom.xml b/java/osx/pom.xml index f0d4db5754..b75b07526c 100644 --- a/java/osx/pom.xml +++ b/java/osx/pom.xml @@ -33,7 +33,7 @@ 5.26 0.6.1 1.6.1 - + 1.7.36 2.13.2.2 2.9.0 true @@ -45,314 +45,18 @@ 1.10 - - - javax.annotation - javax.annotation-api - 1.3.2 - - - - - - org.projectlombok - lombok - ${lombok.version} - - - - - - - - - - com.lmax - disruptor - 3.4.2 - - - - net.java.dev.jna - jna - 4.2.2 - - - org.apache.commons - commons-lang3 - 3.4 - - - com.google.guava - guava - 19.0 - - - - - - - - - - - - - - - commons-codec - commons-codec - 1.9 - - - - - commons-validator - commons-validator - 1.7 - - - - org.assertj - assertj-core - 2.6.0 - test - - - - org.junit.platform - junit-platform-launcher - 1.0.1 - test - - - org.junit.jupiter - junit-jupiter-engine - 5.0.1 - test - - - org.junit.vintage - junit-vintage-engine - 4.12.1 - test - - - - com.google.guava - guava - ${guava.version} - - - - commons-configuration - commons-configuration - ${commons-configuration.version} - - - - org.apache.commons - commons-lang3 - 3.8.1 - - - - commons-codec - commons-codec - 1.12 - - - - commons-io - commons-io - 2.6 - - - - org.apache.commons - commons-text - 1.4 - - - - com.google.protobuf - protobuf-java-util - ${protobuf.version} - - - - org.json - json - 20151123 - compile - - - - - com.beust - jcommander - ${jcommander.version} - - - - com.fasterxml.jackson.core - jackson-annotations - 2.13.2 - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - com.google.flatbuffers - flatbuffers-java - 1.10.0 - - - - com.google.flatbuffers - flatbuffers-java-grpc - 1.10.0 - - - - io.grpc - grpc-core - ${grpc.version} - - - - io.grpc - grpc-netty-shaded - ${grpc.version} - - - - io.grpc - grpc-protobuf - ${grpc.version} - - - - io.grpc - grpc-stub - ${grpc.version} - - - - - - - - - - com.google.protobuf - protobuf-java-util - ${protobuf.version} - - - - org.json - json - 20151123 - compile - - - - com.fasterxml.jackson.core - jackson-annotations - 2.13.2 - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - io.grpc - grpc-core - ${grpc.version} - - - - io.grpc - grpc-netty-shaded - ${grpc.version} - - - - io.grpc - grpc-protobuf - ${grpc.version} - - - com.google.protobuf - protobuf-java-util - ${protobuf.version} - - - - io.grpc - grpc-stub - ${grpc.version} - - - - - - - - - - - - - - - - - - - commons-net - commons-net - 3.8.0 - - - - com.googlecode.protobuf-java-format - protobuf-java-format - 1.2 - - - - - + + + + + - com.google.code.gson - gson - 2.9.0 - - - - - org.apache.logging.log4j - log4j-core - ${log4j2.version} - - - - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j2.version} + org.slf4j + slf4j-api + 1.7.30 - org.apache.logging.log4j log4j-api @@ -363,28 +67,50 @@ log4j-core ${log4j2.version} + org.apache.logging.log4j log4j-slf4j-impl ${log4j2.version} - org.apache.logging.log4j - log4j-jul - ${log4j2.version} + net.java.dev.jna + jna + 4.2.2 - - - - - - + + commons-validator + commons-validator + 1.7 + org.apache.zookeeper zookeeper 3.5.5 + + commons-cli + commons-cli + 1.5.0 + + + org.eclipse.jetty + jetty-server + 9.4.50.v20221201 + + + org.eclipse.jetty + jetty-servlet + 9.4.50.v20221201 + + + + commons-io + commons-io + 2.11.0 + + org.apache.commons @@ -395,14 +121,32 @@ org.apache.curator curator-framework - 2.13.0 + 5.4.0 org.apache.curator curator-recipes - 2.13.0 + 5.4.0 + + + + + + + + + org.projectlombok + lombok + ${lombok.version} + + + + + + + com.lmax @@ -410,69 +154,243 @@ 3.4.2 + + + + + - org.slf4j - slf4j-api - 1.7.30 + org.apache.commons + commons-lang3 + 3.4 + + + com.google.guava + guava + 31.1-jre + + + commons-codec + commons-codec + 1.9 + + + + + commons-validator + commons-validator + 1.7 + + + + org.assertj + assertj-core + 2.6.0 + test + + + + org.junit.platform + junit-platform-launcher + 1.0.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.0.1 + test + + + org.junit.vintage + junit-vintage-engine + 4.12.1 + test + + + + com.google.guava + guava + ${guava.version} + + + + commons-configuration + commons-configuration + ${commons-configuration.version} + + + + org.apache.commons + commons-lang3 + 3.8.1 + + commons-codec + commons-codec + 1.12 + + + commons-io + commons-io + 2.6 + - io.springfox - springfox-swagger-ui - 2.9.2 + org.apache.commons + commons-text + 1.4 - org.yaml - snakeyaml - 1.25 + com.google.protobuf + protobuf-java-util + ${protobuf.version} + + + + org.json + json + 20151123 compile + - io.lettuce - lettuce-core - 5.3.1.RELEASE + com.beust + jcommander + ${jcommander.version} - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - 2.9.4 + com.fasterxml.jackson.core + jackson-annotations + 2.13.2 - com.github.oshi - oshi-core - 3.5.0 + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} - commons-net - commons-net - 3.8.0 + com.google.flatbuffers + flatbuffers-java + 1.10.0 + + + + com.google.flatbuffers + flatbuffers-java-grpc + 1.10.0 + + + io.grpc + grpc-api + ${grpc.version} + + + io.grpc + grpc-core + ${grpc.version} + + + + io.grpc + grpc-netty-shaded + ${grpc.version} + + + + io.grpc + grpc-protobuf + ${grpc.version} - com.fasterxml.jackson.dataformat - jackson-dataformat-properties + io.grpc + grpc-stub + ${grpc.version} + + + com.google.protobuf + protobuf-java-util + ${protobuf.version} + + + + org.json + json + 20151123 + compile + + + + com.fasterxml.jackson.core + jackson-annotations 2.13.2 - - com.alibaba - fastjson - 2.0.2 + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + io.grpc + grpc-core + ${grpc.version} + + + + io.grpc + grpc-netty-shaded + ${grpc.version} + + + + io.grpc + grpc-protobuf + ${grpc.version} + + + com.google.protobuf + protobuf-java-util + ${protobuf.version} + + + + io.grpc + grpc-stub + ${grpc.version} + + + + org.apache.httpcomponents + httpclient + 4.5.7 + + + commons-net + commons-net + 3.8.0 + + + + com.googlecode.protobuf-java-format + protobuf-java-format + 1.2 - io.netty - netty-all - 4.1.68.Final + org.apache.httpcomponents + httpclient + 4.5.13 diff --git a/java/osx/proto/osx.proto b/java/osx/proto/osx.proto index 58d8ff5cf3..d1e93510db 100644 --- a/java/osx/proto/osx.proto +++ b/java/osx/proto/osx.proto @@ -16,7 +16,7 @@ syntax = "proto3"; -package com.osx.federation.rpc; +package org.ppc.ptp; message Message{ @@ -24,8 +24,6 @@ message Message{ bytes head = 2;//消息头部 bytes body = 3;//消息体 } - - message TopicInfo{ string topic=1; string ip = 2; @@ -34,7 +32,66 @@ message TopicInfo{ int32 status = 5; } +// PTP Private transfer protocol +// 通用报头名称编码,4层无Header以二进制填充到报头,7层以Header传输 +enum Header { + Version = 0; // 协议版本 对应7层协议头x-ptp-version + TechProviderCode = 1; // 厂商编码 对应7层协议头x-ptp-tech-provider-code + TraceID = 4; // 链路追踪ID 对应7层协议头x-ptp-trace-id + Token = 5; // 认证令牌 对应7层协议头x-ptp-token + SourceNodeID = 6; // 发送端节点编号 对应7层协议头x-ptp-source-node-id + TargetNodeID = 7; // 接收端节点编号 对应7层协议头x-ptp-target-node-id + SourceInstID = 8; // 发送端机构编号 对应7层协议头x-ptp-source-inst-id + TargetInstID = 9; // 接收端机构编号 对应7层协议头x-ptp-target-inst-id + SessionID = 10; // 通信会话号,全网唯一 对应7层协议头x-ptp-session-id +} + +// 通信扩展元数据编码,扩展信息均在metadata扩展 +enum Metadata { + MessageTopic = 0; // 消息话题,异步场景 + MessageCode = 1; // 消息编码,异步场景 + SourceComponentName = 2; // 源组件名称 + TargetComponentName = 3; // 目标组件名称 + TargetMethod = 4; // 目标方法 + MessageOffSet = 5; // 消息序列号 + InstanceId = 6; // 实例ID + Timestamp = 7; // 时间戳 +} + +// 通信传输层输入报文编码 +message Inbound { + map metadata = 1; // 报头,可选,预留扩展,Dict,序列化协议由通信层统一实现 + bytes payload = 2; // 报文,上层通信内容承载,序列化协议由上层基于SPI可插拔 +} +// 通信传输层输出报文编码 +message Outbound { + map metadata = 1; // 报头,可选,预留扩展,Dict,序列化协议由通信层统一实现 + bytes payload = 2; // 报文,上层通信内容承载,序列化协议由上层基于SPI可插拔 + string code = 3; // 状态码 + string message = 4; // 状态说明 +} + +// 互联互通如果使用异步传输协议作为标准参考,Header会复用metadata传输互联互通协议报头,且metadata中会传输异步场景下的消息相关属性 +// 互联互通如果使用其他协议作为参考标准,Header会复用metadata传输互联互通协议报头 +// 互联互通如果使用GRPC作为参考标准,Header会复用HTTP2的报头传输互联互通协议报头 + +//service PPCTransferService { +// rpc push (stream Inbound) returns (Outbound); +//// rpc pull (Metadata) returns (stream Packet); +// rpc unaryCall (Inbound) returns (Outbound); +// // rpc polling (stream PollingFrame) returns (stream PollingFrame); +// +// +// +// +//} + + +service PrivateTransferProtocol { + rpc transport (stream Inbound) returns (stream Outbound); + rpc invoke (Inbound) returns (Outbound); +}