From da14c5c12782d02a7a14bb87212dacc1472934c3 Mon Sep 17 00:00:00 2001 From: Synex-wh <241809311@qq.com> Date: Mon, 3 Feb 2020 19:27:05 +0800 Subject: [PATCH] New snapshot (#81) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix temp push * update version 5.2.1-SNAPSHOT * fix test case * fix jetty version,and fix rest api for dataInfoIds * fix hashcode test * fix working to init bug * fix start task log * fix Watcher can't get providate data,retry and finally return new * add data server list api * add server list api * remove log * fix isssue 21 * add query by id function * fix issue 22 * delay client off process and sync data process to working status * fix data connet meta error * fix inject NotifyDataSyncHandler * fix start log * add send sub log * fix subscriber to send log * bugfix: https://github.com/sofastack/sofa-registry/issues/27 * bugfix: https://github.com/sofastack/sofa-registry/issues/27 * feature: Add monitoring logs https://github.com/sofastack/sofa-registry/issues/29 * feature: Add monitoring logs https://github.com/sofastack/sofa-registry/issues/29 (1) bugfix CommonResponse (2) format * bugfix: During meta startup, leader may not register itself https://github.com/sofastack/sofa-registry/issues/30 * bugfix: Sometimes receive "Not leader" response from leader in OnStartingFollowing() https://github.com/sofastack/sofa-registry/issues/31 * temp add * add renew request * data snapshot module * add calculate digest service * fix word cache clientid * data renew module * data renew/expired module * add renew datuem request * add WriteDataAcceptor * session renew/expired module * 1. bugfix ReNewDatumHandler: getByConnectId -> getOwnByConnectId 2. reactor DatumCache from static to instance * add blacklist wrapper and filter * upgrade jraft version to 1.2.5 * blacklist ut * add clientoff delay time * bugfix: The timing of snapshot construction is not right * rename: ReNew -> Renew * fix blacklist test case * rename: unpub -> unPub * add threadSize and queueSize limit * bugfix: revert SessionRegistry * fix sub fetch retry all error,and reset datainfoid version * fix client fast chain breakage data can not be cleaned up” * (1) remove logback.xml DEBUG level; (2) dataServerBootstrapConfig rename; (3) print conf when startup * update log * fix update zero version,and fix log * add clientOffDelayMs default value * fix clientOffDelayMs * Task(DatumSnapshot/Pub/UnPub) add retry strategy * bugfix DataNodeServiceImpl: retryTimes * (1)cancelDataTaskListener duplicate (2)bugfix DataNodeServiceImpl and SessionRegistry * refactor datum version * add hessian black list * bugfix: log "retryTimes" * bugfix DatumLeaseManager: Consider the situation of connectId lose after data restart; ownConnectId should calculate dynamically * add jvm blacklist api * fix file name * some code optimization * data:refactor snapshot * fix jetty version * bugfix DatumLeaseManager: If in a non-working state, cannot clean up because the renew request cannot be received at this time. * remove SessionSerialFilterResource * WriteDataProcessor add TaskEvent log; Cache print task update * data bugfix: snapshot must notify session * fix SubscriberPushEmptyTask default implement * merge new * fix protect * 1. When the pub of connectId is 0, no clearance action is triggered. 2. Print map. size regularly 3. Delete the log: "ConnectId (% s) expired, lastRenewTime is% s, pub. size is 0" * DataNodeExchanger: print but ignore if from renew module, cause renew request is too much * reduce log of renew * data bugfix: Data coverage is also allowed when versions are equal. Consistent with session design. * DatumCache bugfix: Index coverage should be updated after pubMap update * DatumSnapshotHandler: limit print; do not call dataChangeEventCenter.onChange if no diff * bugfix unpub npe (pub maybe already clean by DatumLeaseManager);LIMITED_LIST_SIZE_FOR_PRINT change to 30 * some code refactor * add code comment * fix data working to init,and fix empty push version * consider unpub is isWriteRequest, Reduce Snapshot frequency * RefreshUpdateTime is at the top, otherwise multiple snapshot can be issued concurrently * update config: reduce retryTimes, increase delayTime, the purpose is to reduce performance consumption * put resume() in finally code block, avoid lock leak * modify renewDatumWheelTaskDelay and datumTimeToLiveSec * When session receives a connection and generates renew tasks, it randomly delays different times to avoid everyone launching renew at the same time. * data: add executor for handler session: bugfix snapshot session: refactor wheelTimer of renew to add executor * add get data log * snapshot and lastUpdateTimestamp: Specific to dataServerIP * 1. DataServer: RenewDatumHandler must return GenericResponse but not CommonResponse, or else session will class cast exception 2. No need to update timestamp after renew 3. snapshot: Need to specify DataServerIP * add logs * 1. dataServer: reduce log of snapshotHandler 2. update logs * dataServer: renew logic should delay for some time after status is WORKING, cause Data is processed asynchronously after synchronization from other DataServer * bugfix bean; update log * ignore renew request log * fix UT * fix .travis.yml * fix version 5.3.0-SNAPSHOT * fix online notify connect error * fix push confirm error,and fix datum update version,pub threadpool config,add accesslimit service * fix push confirm error,and fix datum update version,pub threadpool config,add accesslimit service (#45) * add switch renew and expire * implement renew enable/disable switch * fix data client exechange log * fix datum fetch connect error * bugfix CacheService: set version zero when first sub and get datum error * fix clean task for fetch * bugfix DatumCache: Forget to clean up the index in datumCache.putSnapshot * Session&Data increase WordCache use * code optimize * WordCache: registerId do not add WordCache * fix fetch datum word cache * fix NotifyFetchDatumHandler npe * fix test case time * fix test cast * fix test case * fix tast case * fix ut case: StopPushDataSwitchTest * ut case:renew module * fix ut case:TempPublisherTest * fix version,and merge new * bugfix ut case: increase sleep time * fix ut case:RenewTest * fix version and fix callback executor,fix log error * fix ut case:RenewTest format * fix pom version * fix ut case:do not run parallelly * refactor providerdata process * Memory optimization:Datum.processDatum * add session notify test * copy from mybank: 1. Update Subscriber: support for push context 2. increase queueSize of checkPushExecutor 3. fix the isolation function of Gzone and Rzone * Modify the deny policy of accessDataExecutor of SessionServer * remove useless code * fix call back * fix meta methodhandle cache * fix push confirm success * Change the communication between session and data to multi connection * resolve compile error * fix processor * BoltClient: the creation of ConnectionEventAdapter should be inheritable * fix currentTimeMillis product from source * add client Invalid check task * use multiple RpcClient instances instead of one RpcClient with multiple connections,and start a heartbeat thread to ensure connection pool because bolt does not maintain the number of connection pools * refactor TaskListener and use map instead of list in DefaultTaskListenerManager; refactor getSingleTaskDispatcher() * DataChangeRequestHandler:optimize performance * refactor: Heartbeat between session and data * fix: https://github.com/Synex-wh/sofa-registry/pull/20#pullrequestreview-324716028 * update * BoltClient use just one RpcClient; remove heartbeat between session and data; * SyncDataCallback reduce ThreadSize for saving cpu * reduce NOTIFY_SESSION_CALLBACK_EXECUTOR threadSize * fix version in DataChangeFetchTask * 1. filter out the unPubs of datum when first put, Otherwise, "syncData" or "fetchData" get Datum may contains unPubs, which will result something error 2. add jul-to-slf4j for some lib which use jul log, e.g. hessian * fix meta mem * fix test case * fix temp case * fix syncConfigRetryInterval 60s * fix format Co-authored-by: wukezhu --- pom.xml | 9 +- .../common/model/dataserver/Datum.java | 12 +- .../registry/common/model/store/BaseInfo.java | 8 +- .../common/model/store/Publisher.java | 15 +- .../common/model/store/Subscriber.java | 129 ++++-- .../registry/common/model/store/Watcher.java | 19 + .../common/model/store/WordCache.java | 8 +- .../listener/DefaultTaskListenerManager.java | 18 +- .../registry/task/listener/TaskEvent.java | 64 +-- .../registry/task/listener/TaskListener.java | 7 +- .../task/listener/TaskListenerManager.java | 5 +- .../task/listener/TaskListenerTest.java | 20 +- .../consistency/hash/ConsistentHash.java | 55 +++ .../registry/remoting/CallbackHandler.java | 8 + .../sofa/registry/remoting/Channel.java | 5 +- .../alipay/sofa/registry/remoting/Client.java | 49 ++- .../sofa/registry/remoting/Endpoint.java | 44 +- .../alipay/sofa/registry/remoting/Server.java | 32 +- .../registry/remoting/exchange/Exchange.java | 12 + .../registry/remoting/bolt/BoltChannel.java | 17 +- .../registry/remoting/bolt/BoltClient.java | 373 +++++++--------- .../registry/remoting/bolt/BoltServer.java | 58 +-- .../remoting/bolt/exchange/BoltExchange.java | 49 +-- .../remoting/bolt/BoltServerTest.java | 14 +- .../registry/remoting/bolt/MockChannel.java | 10 +- .../remoting/jersey/JerseyChannel.java | 14 +- .../remoting/jersey/JerseyClient.java | 90 ++-- .../remoting/jersey/JerseyJettyServer.java | 46 +- .../jersey/exchange/JerseyExchange.java | 18 +- .../remoting/jersey/JerseyExchangeTest.java | 41 +- .../DataServerBeanConfiguration.java | 8 +- .../data/bootstrap/DataServerConfig.java | 23 +- .../server/data/cache/DatumCache.java | 67 ++- .../change/notify/SessionServerNotifier.java | 47 +- .../change/notify/TempPublisherNotifier.java | 20 +- .../server/data/executor/ExecutorFactory.java | 13 +- .../data/remoting/DataNodeExchanger.java | 15 +- .../data/remoting/MetaNodeExchanger.java | 19 +- .../dataserver/DataServerNodeFactory.java | 16 +- .../dataserver/GetSyncDataHandler.java | 7 + .../remoting/dataserver/SyncDataCallback.java | 27 +- .../handler/NotifyFetchDatumHandler.java | 25 +- .../SessionServerConnectionFactory.java | 166 +++---- .../disconnect/DisconnectEventHandler.java | 21 +- .../handler/DataServerConnectionHandler.java | 5 +- .../handler/DatumSnapshotHandler.java | 34 +- .../handler/GetDataVersionsHandler.java | 8 +- .../handler/PublishDataHandler.java | 17 +- .../handler/RenewDatumHandler.java | 6 +- .../handler/SessionServerRegisterHandler.java | 11 +- .../handler/UnPublishDataHandler.java | 4 +- .../data/resource/DataDigestResource.java | 2 +- server/server/integration/pom.xml | 2 +- .../meta/bootstrap/MetaServerConfig.java | 2 + .../meta/bootstrap/MetaServerConfigBean.java | 20 + .../DataNodeChangePushTaskListener.java | 7 +- ...rsistenceDataChangeNotifyTaskListener.java | 7 +- ...eceiveStatusConfirmNotifyTaskListener.java | 7 +- .../SessionNodeChangePushTaskListener.java | 7 +- .../meta/remoting/MetaClientExchanger.java | 28 +- .../server/meta/remoting/RaftExchanger.java | 12 + .../meta/repository/RepositoryService.java | 18 +- .../service/DataRepositoryService.java | 14 +- .../service/MetaRepositoryService.java | 16 +- .../service/SessionRepositoryService.java | 8 +- .../server/meta/resource/HealthResource.java | 20 +- .../server/meta/store/DataStoreService.java | 12 - .../DataNodeChangePushTaskListenerMock.java | 14 +- .../ReceiveStatusConfirmNotifyTaskMock.java | 4 +- .../SessionNodeChangePushTaskMock.java | 14 +- server/server/session/pom.xml | 4 + .../bootstrap/SessionServerBootstrap.java | 106 ++--- .../bootstrap/SessionServerConfig.java | 3 + .../bootstrap/SessionServerConfigBean.java | 37 +- .../bootstrap/SessionServerConfiguration.java | 61 ++- .../listener/CancelDataTaskListener.java | 24 +- .../DataChangeFetchCloudTaskListener.java | 36 +- .../listener/DataChangeFetchTaskListener.java | 43 +- .../listener/DataPushTaskListener.java | 27 +- .../listener/DatumSnapshotTaskListener.java | 4 +- .../ProvideDataChangeFetchTaskListener.java | 35 +- .../listener/PublishDataTaskListener.java | 7 +- .../ReceivedConfigDataPushTaskListener.java | 26 +- .../ReceivedDataMultiPushTaskListener.java | 4 +- .../listener/RenewDatumTaskListener.java | 11 +- .../SessionRegisterDataTaskListener.java | 34 +- .../SubscriberMultiFetchTaskListener.java | 23 +- .../SubscriberPushEmptyTaskListener.java | 23 +- .../SubscriberRegisterFetchTaskListener.java | 23 +- .../listener/UnPublishDataTaskListener.java | 28 +- .../WatcherRegisterFetchTaskListener.java | 27 +- .../session/node/service/DataNodeService.java | 10 - .../node/service/DataNodeServiceImpl.java | 28 +- .../provideData/ProvideDataProcessor.java | 33 ++ .../ProvideDataProcessorManager.java | 59 +++ .../BlackListProvideDataProcessor.java | 139 ++++++ .../RenewSnapshotProvideDataProcessor.java | 74 ++++ .../StopPushProvideDataProcessor.java | 171 ++++++++ .../session/remoting/DataNodeExchanger.java | 62 ++- .../session/remoting/MetaNodeExchanger.java | 25 +- .../handler/DataChangeRequestHandler.java | 30 +- .../handler/DataNodeConnectionHandler.java | 10 +- .../remoting/handler/WatcherHandler.java | 14 +- .../session/scheduler/ExecutorManager.java | 128 +++--- .../scheduler/SessionThreadPoolExecutor.java | 39 +- .../scheduler/task/AbstractSessionTask.java | 11 +- .../task/DataChangeFetchCloudTask.java | 22 +- .../scheduler/task/DataChangeFetchTask.java | 32 +- .../session/scheduler/task/DataPushTask.java | 20 +- .../task/ProvideDataChangeFetchTask.java | 200 +-------- .../scheduler/task/PushTaskClosure.java | 13 +- .../task/ReceivedConfigDataPushTask.java | 6 + .../task/ReceivedDataMultiPushTask.java | 32 +- .../task/SessionRegisterDataTask.java | 46 +- .../session/store/SessionDataStore.java | 30 +- .../session/store/SessionInterests.java | 27 +- .../server/session/store/SessionWatchers.java | 16 +- .../impl/DefaultSessionRegistryStrategy.java | 23 +- .../registry/jraft/bootstrap/RaftServer.java | 37 ++ .../jraft/bootstrap/RaftServerConfig.java | 20 + .../jraft/handler/RaftServerHandler.java | 1 + .../registry/jraft/processor/Processor.java | 86 +++- .../jraft/processor/ProxyHandler.java | 5 +- .../registry/test/BaseIntegrationTest.java | 51 ++- .../test/pubsub/TempPublisherTest.java | 81 ++-- .../registry/test/sync/SessionNotifyTest.java | 406 ++++++++++++++++++ .../test/sync/SyncDataHandlerTest.java | 4 +- 127 files changed, 2813 insertions(+), 1786 deletions(-) create mode 100644 server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessor.java create mode 100644 server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessorManager.java create mode 100644 server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/BlackListProvideDataProcessor.java create mode 100644 server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/RenewSnapshotProvideDataProcessor.java create mode 100644 server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/StopPushProvideDataProcessor.java create mode 100644 test/src/test/java/com/alipay/sofa/registry/test/sync/SessionNotifyTest.java diff --git a/pom.xml b/pom.xml index 23a3c9032..e4f048e1d 100644 --- a/pom.xml +++ b/pom.xml @@ -2,8 +2,8 @@ + 4.0.0 - com.alipay.sofa registry-parent 5.4.0-SNAPSHOT @@ -75,7 +75,7 @@ 1.5.2 1.10.19 1.6.6 - 1.2.5 + 1.2.7.beta1 4.0.2 2.4 [9.4.17.v20190418,9.4.19.v20190610] @@ -85,6 +85,11 @@ + + org.slf4j + jul-to-slf4j + 1.7.9 + org.springframework.boot spring-boot-starter-parent diff --git a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/dataserver/Datum.java b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/dataserver/Datum.java index 246363fa0..80d0b9e0c 100644 --- a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/dataserver/Datum.java +++ b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/dataserver/Datum.java @@ -62,7 +62,7 @@ public Datum() { * @param dataCenter */ public Datum(String dataInfoId, String dataCenter) { - this.dataInfoId = dataInfoId; + this.dataInfoId = WordCache.getInstance().getWordCache(dataInfoId); this.dataCenter = WordCache.getInstance().getWordCache(dataCenter); updateVersion(); } @@ -244,7 +244,7 @@ public void setContainsUnPub(boolean containsUnPub) { this.containsUnPub = containsUnPub; } - public static Datum processDatum(Datum datum) { + public static Datum internDatum(Datum datum) { datum.setDataCenter(datum.getDataCenter()); datum.setDataInfoId(datum.getDataInfoId()); datum.setDataId(datum.getDataId()); @@ -253,7 +253,13 @@ public static Datum processDatum(Datum datum) { Map pubMap = datum.getPubMap(); if (pubMap != null && !pubMap.isEmpty()) { - pubMap.forEach((registerId, publisher) -> Publisher.processPublisher(publisher)); + pubMap.forEach((registerId, publisher) -> { + // let registerId == pub.getRegisterId in every , for reducing old gen memory + // because this Datum is put into Memory directly, by DatumCache.coverDatum + publisher.setRegisterId(registerId); + // change publisher word cache + Publisher.internPublisher(publisher); + }); } return datum; diff --git a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/BaseInfo.java b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/BaseInfo.java index 2242e5354..81b470778 100644 --- a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/BaseInfo.java +++ b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/BaseInfo.java @@ -16,12 +16,12 @@ */ package com.alipay.sofa.registry.common.model.store; -import com.fasterxml.jackson.annotation.JsonIgnore; - import java.io.Serializable; import java.util.HashMap; import java.util.Map; +import com.fasterxml.jackson.annotation.JsonIgnore; + /** * * @author shangyu.wh @@ -136,7 +136,7 @@ public String getProcessId() { * @param processId value to be assigned to property processId */ public void setProcessId(String processId) { - this.processId = processId; + this.processId = WordCache.getInstance().getWordCache(processId); } /** @@ -273,7 +273,7 @@ public String getClientId() { * @param clientId value to be assigned to property clientId */ public void setClientId(String clientId) { - this.clientId = clientId; + this.clientId = WordCache.getInstance().getWordCache(clientId); } /** diff --git a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Publisher.java b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Publisher.java index 8ecd7566b..a216732aa 100644 --- a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Publisher.java +++ b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Publisher.java @@ -16,12 +16,12 @@ */ package com.alipay.sofa.registry.common.model.store; +import java.util.List; + import com.alipay.sofa.registry.common.model.PublishType; import com.alipay.sofa.registry.common.model.ServerDataBox; import com.fasterxml.jackson.annotation.JsonIgnore; -import java.util.List; - /** * * @author shangyu.wh @@ -90,8 +90,8 @@ protected String getOtherInfo() { * @param publisher * @return */ - public static Publisher processPublisher(Publisher publisher) { - + public static Publisher internPublisher(Publisher publisher) { + publisher.setRegisterId(publisher.getRegisterId()); publisher.setDataInfoId(publisher.getDataInfoId()); publisher.setInstanceId(publisher.getInstanceId()); publisher.setGroup(publisher.getGroup()); @@ -101,13 +101,6 @@ public static Publisher processPublisher(Publisher publisher) { publisher.setProcessId(publisher.getProcessId()); publisher.setAppName(publisher.getAppName()); - if (publisher.getSourceAddress() != null) { - publisher.setSourceAddress(new URL(publisher.getSourceAddress().getIpAddress(), - publisher.getSourceAddress().getPort())); - } - - publisher.setAttributes(publisher.getAttributes()); - return publisher; } diff --git a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Subscriber.java b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Subscriber.java index 2f0b8b78d..3d9b532b7 100644 --- a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Subscriber.java +++ b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Subscriber.java @@ -16,13 +16,14 @@ */ package com.alipay.sofa.registry.common.model.store; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import com.alipay.sofa.registry.common.model.ElementType; +import com.alipay.sofa.registry.common.model.constants.ValueConstants; import com.alipay.sofa.registry.core.model.ScopeEnum; import com.fasterxml.jackson.annotation.JsonIgnore; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - /** * * @author shangyu.wh @@ -31,16 +32,16 @@ public class Subscriber extends BaseInfo { /** UID */ - private static final long serialVersionUID = 98433360274932292L; + private static final long serialVersionUID = 98433360274932292L; /** */ - private ScopeEnum scope; + private ScopeEnum scope; /** */ - private ElementType elementType; + private ElementType elementType; /** - * all dataCenter push dataInfo version + * last push context */ - private Map lastPushVersions = new ConcurrentHashMap<>(); + private Map lastPushContexts = new ConcurrentHashMap<>(); /** * Getter method for property scope. @@ -71,7 +72,11 @@ public ElementType getElementType() { */ public boolean checkVersion(String dataCenter, Long version) { - Long oldVersion = lastPushVersions.get(dataCenter); + PushContext lastPushContext = lastPushContexts.get(dataCenter); + if (lastPushContext == null) { + return version != null; + } + Long oldVersion = lastPushContext.pushVersion; if (oldVersion == null) { return version != null; } else { @@ -88,15 +93,26 @@ public boolean checkVersion(String dataCenter, Long version) { * @return */ public void checkAndUpdateVersion(String dataCenter, Long version) { + checkAndUpdateVersion(dataCenter, version, -1); + } + + /** + * check version input greater or equal to current version + * @param version + * @return + */ + public void checkAndUpdateVersion(String dataCenter, Long version, int pubCount) { while (true) { - Long oldVersion = lastPushVersions.putIfAbsent(dataCenter, version); + PushContext pushContext = new PushContext(version, pubCount); + PushContext oldPushContext = lastPushContexts.putIfAbsent(dataCenter, pushContext); // Add firstly - if (oldVersion == null) { + if (oldPushContext == null) { break; } else { - if (version > oldVersion) { - if (lastPushVersions.replace(dataCenter, oldVersion, version)) { + if (oldPushContext.pushVersion == null + || (pushContext.pushVersion != null && pushContext.pushVersion > oldPushContext.pushVersion)) { + if (lastPushContexts.replace(dataCenter, oldPushContext, pushContext)) { break; } } else { @@ -106,6 +122,23 @@ public void checkAndUpdateVersion(String dataCenter, Long version) { } } + /** + * If the pushed data is empty, check the last push, for avoid continuous empty datum push + */ + public boolean allowPush(String dataCenter, int pubCount) { + boolean allowPush = true; + // condition of no push: + // 1. last push count is 0 and this time is also 0 + // 2. last push is a valid push (version > 1) + if (pubCount == 0) { + PushContext pushContext = lastPushContexts.get(dataCenter); + allowPush = !(pushContext != null && pushContext.pushPubCount == 0 + //last push is a valid push + && pushContext.pushVersion != null && pushContext.pushVersion > ValueConstants.DEFAULT_NO_DATUM_VERSION); + } + return allowPush; + } + /** * Setter method for property elementType. * @@ -126,28 +159,10 @@ protected String getOtherInfo() { final StringBuilder sb = new StringBuilder("scope="); sb.append(scope).append(","); sb.append("elementType=").append(elementType).append(","); - sb.append("lastPushVersion=").append(lastPushVersions); + sb.append("pushVersion=").append(lastPushContexts); return sb.toString(); } - /** - * Getter method for property lastPushVersions. - * - * @return property value of lastPushVersions - */ - public Map getLastPushVersions() { - return lastPushVersions; - } - - /** - * Setter method for property lastPushVersions . - * - * @param lastPushVersions value to be assigned to property lastPushVersions - */ - public void setLastPushVersions(Map lastPushVersions) { - this.lastPushVersions = lastPushVersions; - } - /** * @see Object#toString() */ @@ -156,9 +171,57 @@ public String toString() { final StringBuilder sb = new StringBuilder("Subscriber{"); sb.append("scope=").append(scope); sb.append(", elementType=").append(elementType); - sb.append(", lastPushVersions=").append(lastPushVersions); + sb.append(", lastPushContexts=").append(lastPushContexts); sb.append(", super=").append(super.toString()); sb.append('}'); return sb.toString(); } + + /** + * change subscriber word cache + * @param subscriber + * @return + */ + public static Subscriber internSubscriber(Subscriber subscriber) { + subscriber.setRegisterId(subscriber.getRegisterId()); + subscriber.setDataInfoId(subscriber.getDataInfoId()); + subscriber.setInstanceId(subscriber.getInstanceId()); + subscriber.setGroup(subscriber.getGroup()); + subscriber.setDataId(subscriber.getDataId()); + subscriber.setClientId(subscriber.getClientId()); + subscriber.setCell(subscriber.getCell()); + subscriber.setProcessId(subscriber.getProcessId()); + subscriber.setAppName(subscriber.getAppName()); + + return subscriber; + } + + static class PushContext { + /** + * last pushed dataInfo version + */ + private Long pushVersion; + + /** + * push pushed dataInfo pubCount + */ + private int pushPubCount; + + public PushContext(Long pushVersion, int pushPubCount) { + this.pushVersion = pushVersion; + this.pushPubCount = pushPubCount; + } + + /** + * @see Object#toString() + */ + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("PushContext{"); + sb.append("pushVersion=").append(pushVersion); + sb.append(", pushPubCount=").append(pushPubCount); + sb.append('}'); + return sb.toString(); + } + } } \ No newline at end of file diff --git a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Watcher.java b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Watcher.java index 6ffa610f9..f369d5e4e 100644 --- a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Watcher.java +++ b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/Watcher.java @@ -27,4 +27,23 @@ public class Watcher extends BaseInfo { public DataType getDataType() { return DataType.WATCHER; } + + /** + * change watcher word cache + * @param watcher + * @return + */ + public static Watcher internWatcher(Watcher watcher) { + watcher.setRegisterId(watcher.getRegisterId()); + watcher.setDataInfoId(watcher.getDataInfoId()); + watcher.setInstanceId(watcher.getInstanceId()); + watcher.setGroup(watcher.getGroup()); + watcher.setDataId(watcher.getDataId()); + watcher.setClientId(watcher.getClientId()); + watcher.setCell(watcher.getCell()); + watcher.setProcessId(watcher.getProcessId()); + watcher.setAppName(watcher.getAppName()); + + return watcher; + } } \ No newline at end of file diff --git a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/WordCache.java b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/WordCache.java index 2734c8ac0..f00e3c938 100644 --- a/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/WordCache.java +++ b/server/common/model/src/main/java/com/alipay/sofa/registry/common/model/store/WordCache.java @@ -16,7 +16,8 @@ */ package com.alipay.sofa.registry.common.model.store; -import java.util.concurrent.ConcurrentHashMap; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; /** * @@ -45,7 +46,7 @@ public static WordCache getInstance() { /** * word cache map */ - private ConcurrentHashMap map = new ConcurrentHashMap<>(); + private Interner interners = Interners.newWeakInterner(); /** * @@ -56,8 +57,7 @@ public String getWordCache(String s) { if (s == null) { return null; } - String oldValue = map.putIfAbsent(s, s); - return oldValue == null ? s : oldValue; + return interners.intern(s); } } \ No newline at end of file diff --git a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/DefaultTaskListenerManager.java b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/DefaultTaskListenerManager.java index 8e6b23077..a0aa07b2f 100644 --- a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/DefaultTaskListenerManager.java +++ b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/DefaultTaskListenerManager.java @@ -16,33 +16,35 @@ */ package com.alipay.sofa.registry.task.listener; -import java.util.ArrayList; import java.util.Collection; +import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; + /** * @author xuanbei * @since 2018/12/28 */ public class DefaultTaskListenerManager implements TaskListenerManager { - private Collection taskListeners = new ArrayList<>(); + + private Multimap taskListeners = ArrayListMultimap.create(); @Override - public Collection getTaskListeners() { + public Multimap getTaskListeners() { return taskListeners; } @Override public void addTaskListener(TaskListener taskListener) { - taskListeners.add(taskListener); + taskListeners.put(taskListener.support(), taskListener); } @Override public void sendTaskEvent(TaskEvent taskEvent) { - + Collection taskListeners = this.taskListeners.get(taskEvent.getTaskType()); for (TaskListener taskListener : taskListeners) { - if (taskListener.support(taskEvent)) { - taskListener.handleEvent(taskEvent); - } + taskListener.handleEvent(taskEvent); } } } diff --git a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskEvent.java b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskEvent.java index d081446b9..7b505d878 100644 --- a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskEvent.java +++ b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskEvent.java @@ -17,8 +17,8 @@ package com.alipay.sofa.registry.task.listener; import java.util.Map; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import com.alipay.sofa.registry.task.TaskClosure; @@ -31,36 +31,39 @@ public class TaskEvent { public enum TaskType { //Session task - SUBSCRIBER_REGISTER_FETCH_TASK("SubscriberRegisterFetchTask"), SUBSCRIBER_PUSH_EMPTY_TASK( - "SubscriberPushEmptyTask"), WATCHER_REGISTER_FETCH_TASK( - "WatcherRegisterFetchTask"), DATA_CHANGE_FETCH_TASK( - "DataChangeFetchTask"), DATA_PUSH_TASK( - "DataPushTask"), DATA_CHANGE_FETCH_CLOUD_TASK( - "DataChangeFetchCloudTask"), RECEIVED_DATA_MULTI_PUSH_TASK( - "ReceivedDataMultiPushTask"), RECEIVED_DATA_CONFIG_PUSH_TASK( - "ReceivedDataConfigPushTask"), CANCEL_DATA_TASK( - "CancelDataTask"), SYNC_PUBLISHER_TASK( - "SyncPublisherTask"), SYNC_SUBSCRIBER_TASK( - "SyncSubscriberTask"), SESSION_REGISTER_DATA_TASK( - "SessionRegisterDataTask"), PROVIDE_DATA_CHANGE_FETCH_TASK( - "ProvideDataChangeFetchTask"), SUBSCRIBER_MULTI_FETCH_TASK( - "SubscriberMultiFetchTask"), PUBLISH_DATA_TASK( - "PublishDataTask"), UN_PUBLISH_DATA_TASK( - "UnPublishDataTask"), RENEW_DATUM_TASK( - "RenewDatumTask"), DATUM_SNAPSHOT_TASK( - "DatumSnapshotTask"), + SUBSCRIBER_REGISTER_FETCH_TASK("SubscriberRegisterFetchTask"), // + SUBSCRIBER_PUSH_EMPTY_TASK("SubscriberPushEmptyTask"), // + WATCHER_REGISTER_FETCH_TASK("WatcherRegisterFetchTask"), // + DATA_CHANGE_FETCH_TASK("DataChangeFetchTask"), // + DATA_PUSH_TASK("DataPushTask"), // + DATA_CHANGE_FETCH_CLOUD_TASK("DataChangeFetchCloudTask"), // + RECEIVED_DATA_MULTI_PUSH_TASK("ReceivedDataMultiPushTask"), // + RECEIVED_DATA_CONFIG_PUSH_TASK("ReceivedDataConfigPushTask"), // + CANCEL_DATA_TASK("CancelDataTask"), // + SYNC_PUBLISHER_TASK("SyncPublisherTask"), // + SYNC_SUBSCRIBER_TASK("SyncSubscriberTask"), // + SESSION_REGISTER_DATA_TASK("SessionRegisterDataTask"), // + PROVIDE_DATA_CHANGE_FETCH_TASK("ProvideDataChangeFetchTask"), // + SUBSCRIBER_MULTI_FETCH_TASK("SubscriberMultiFetchTask"), // + PUBLISH_DATA_TASK("PublishDataTask"), // + UN_PUBLISH_DATA_TASK("UnPublishDataTask"), // + RENEW_DATUM_TASK("RenewDatumTask"), // + DATUM_SNAPSHOT_TASK("DatumSnapshotTask"), // //Session Adapter task - USER_DATA_ELEMENT_PUSH_TASK("UserDataElementPushTask"), USER_DATA_ELEMENT_MULTI_PUSH_TASK( - "UserDataElementMultiPushTask"), + USER_DATA_ELEMENT_PUSH_TASK("UserDataElementPushTask"), // + USER_DATA_ELEMENT_MULTI_PUSH_TASK("UserDataElementMultiPushTask"), // //MetaServer task - SESSION_NODE_CHANGE_PUSH_TASK("SessionNodeChangePushTask"), DATA_NODE_CHANGE_PUSH_TASK( - "DataNodeChangePushTask"), RECEIVE_STATUS_CONFIRM_NOTIFY_TASK( - "ReceiveStatusConfirmNotifyTask"), PERSISTENCE_DATA_CHANGE_NOTIFY_TASK( - "PersistenceDataChangeNotifyTask"), ; + SESSION_NODE_CHANGE_PUSH_TASK("SessionNodeChangePushTask"), // + DATA_NODE_CHANGE_PUSH_TASK("DataNodeChangePushTask"), // + RECEIVE_STATUS_CONFIRM_NOTIFY_TASK("ReceiveStatusConfirmNotifyTask"), // + PERSISTENCE_DATA_CHANGE_NOTIFY_TASK("PersistenceDataChangeNotifyTask"), // + ; - private String name; + private String name; + + private AtomicInteger nextId = new AtomicInteger(0); TaskType(String name) { this.name = name; @@ -83,7 +86,7 @@ public String getName() { private final String taskId; - private final Map attributes = new ConcurrentHashMap(); + private final Map attributes = new ConcurrentHashMap(); /** * constructor @@ -92,7 +95,8 @@ public String getName() { public TaskEvent(TaskType taskType) { this.taskType = taskType; this.createTime = System.currentTimeMillis(); - taskId = UUID.randomUUID().toString(); + this.taskId = String.format("%s-%s-%s", taskType.name, this.createTime, + taskType.nextId.getAndIncrement()); } /** @@ -101,10 +105,8 @@ public TaskEvent(TaskType taskType) { * @param taskType */ public TaskEvent(Object eventObj, TaskType taskType) { + this(taskType); this.eventObj = eventObj; - this.taskType = taskType; - this.createTime = System.currentTimeMillis(); - taskId = UUID.randomUUID().toString(); } /** diff --git a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListener.java b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListener.java index 547d3b154..5601031eb 100644 --- a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListener.java +++ b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.task.listener; +import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; + /** * * @author shangyu.wh @@ -26,10 +28,9 @@ public interface TaskListener { /** * com.alipay.sofa.registry.server.meta.listener type check * - * @param event - * @return true or false。 + * @return type */ - boolean support(TaskEvent event); + TaskType support(); /** * event execute diff --git a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListenerManager.java b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListenerManager.java index dc19c661d..58be148f8 100644 --- a/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListenerManager.java +++ b/server/common/util/src/main/java/com/alipay/sofa/registry/task/listener/TaskListenerManager.java @@ -16,7 +16,8 @@ */ package com.alipay.sofa.registry.task.listener; -import java.util.Collection; +import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; +import com.google.common.collect.Multimap; /** * @@ -29,7 +30,7 @@ public interface TaskListenerManager { * * @return */ - Collection getTaskListeners(); + Multimap getTaskListeners(); /** * diff --git a/server/common/util/src/test/java/com/alipay/sofa/registry/task/listener/TaskListenerTest.java b/server/common/util/src/test/java/com/alipay/sofa/registry/task/listener/TaskListenerTest.java index 9443f5d44..1e7d0acd5 100644 --- a/server/common/util/src/test/java/com/alipay/sofa/registry/task/listener/TaskListenerTest.java +++ b/server/common/util/src/test/java/com/alipay/sofa/registry/task/listener/TaskListenerTest.java @@ -16,13 +16,15 @@ */ package com.alipay.sofa.registry.task.listener; +import static com.alipay.sofa.registry.task.listener.TaskEvent.TaskType.CANCEL_DATA_TASK; +import static com.alipay.sofa.registry.task.listener.TaskEvent.TaskType.DATA_PUSH_TASK; +import static com.alipay.sofa.registry.task.listener.TaskEvent.TaskType.WATCHER_REGISTER_FETCH_TASK; + import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import static com.alipay.sofa.registry.task.listener.TaskEvent.TaskType.CANCEL_DATA_TASK; -import static com.alipay.sofa.registry.task.listener.TaskEvent.TaskType.DATA_PUSH_TASK; -import static com.alipay.sofa.registry.task.listener.TaskEvent.TaskType.WATCHER_REGISTER_FETCH_TASK; +import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; /** * @author xuanbei @@ -65,8 +67,8 @@ public void doTest() { private static class DataPushTaskListener implements TaskListener { @Override - public boolean support(TaskEvent event) { - return DATA_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return DATA_PUSH_TASK; } @Override @@ -77,8 +79,8 @@ public void handleEvent(TaskEvent event) { private static class CancelDataTaskListener implements TaskListener { @Override - public boolean support(TaskEvent event) { - return TaskEvent.TaskType.CANCEL_DATA_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.CANCEL_DATA_TASK; } @Override @@ -89,8 +91,8 @@ public void handleEvent(TaskEvent event) { private static class WatcherRegisterFetchTaskListener implements TaskListener { @Override - public boolean support(TaskEvent event) { - return TaskEvent.TaskType.WATCHER_REGISTER_FETCH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.WATCHER_REGISTER_FETCH_TASK; } @Override diff --git a/server/consistency/src/main/java/com/alipay/sofa/registry/consistency/hash/ConsistentHash.java b/server/consistency/src/main/java/com/alipay/sofa/registry/consistency/hash/ConsistentHash.java index 3c6949236..fdcbd2ad6 100644 --- a/server/consistency/src/main/java/com/alipay/sofa/registry/consistency/hash/ConsistentHash.java +++ b/server/consistency/src/main/java/com/alipay/sofa/registry/consistency/hash/ConsistentHash.java @@ -148,4 +148,59 @@ public List getNUniqueNodesFor(Object key, int n) { } return list; } + + /** + * This returns the closest n unique nodes in order for the object. + * + * This will return a list that has all nodes if n > number of nodes. + * + * @param key the key + * @param n the n + * @param disasterList disaster region name + * @return the n unique nodes for + */ + public List getNUniqueNodesFor(Object key, int n, List disasterList) { + if (circle.isEmpty()) { + return Collections.emptyList(); + } + + if (n > realNodes.size()) { + n = realNodes.size(); + } + + List disasters = disasterList != null && !disasterList.isEmpty() ? disasterList + : new ArrayList<>(); + List list = new ArrayList<>(n); + int hash = hashFunction.hash(key); + for (int i = 0; i < n; i++) { + if (!circle.containsKey(hash)) { + // go to next element. + SortedMap tailMap = circle.tailMap(hash); + hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); + } + T candidate = circle.get(hash); + if (!list.contains(candidate)) { + + while (!disasters.isEmpty() && !disasters.contains(candidate.getNodeName())) { + hash++; + if (!circle.containsKey(hash)) { + // go to next element. + SortedMap tailMap = circle.tailMap(hash); + hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); + } + candidate = circle.get(hash); + } + list.add(candidate); + if (!disasters.isEmpty()) { + disasters.remove(candidate.getNodeName()); + } + + } else { + i--; // try again. + } + // find the next element in the circle + hash++; + } + return list; + } } diff --git a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/CallbackHandler.java b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/CallbackHandler.java index 2b32e72a0..aa73eeb46 100644 --- a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/CallbackHandler.java +++ b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/CallbackHandler.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.remoting; +import java.util.concurrent.Executor; + /** * * @author shangyu.wh @@ -38,4 +40,10 @@ public interface CallbackHandler { * @param exception */ void onException(Channel channel, Throwable exception); + + /** + * override executor + * @return + */ + Executor getExecutor(); } \ No newline at end of file diff --git a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Channel.java b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Channel.java index 4f51ffed2..162c59ca2 100644 --- a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Channel.java +++ b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Channel.java @@ -16,9 +16,10 @@ */ package com.alipay.sofa.registry.remoting; -import javax.ws.rs.client.WebTarget; import java.net.InetSocketAddress; +import javax.ws.rs.client.WebTarget; + /** * * @author shangyu.wh @@ -68,4 +69,6 @@ public interface Channel { * @return */ WebTarget getWebTarget(); + + void close(); } \ No newline at end of file diff --git a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Client.java b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Client.java index f19aba5a6..aa08d9c3e 100644 --- a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Client.java +++ b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Client.java @@ -18,9 +18,6 @@ import com.alipay.sofa.registry.common.model.store.URL; -import java.net.InetSocketAddress; -import java.util.Collection; - /** * * @author shangyu.wh @@ -28,21 +25,6 @@ */ public interface Client extends Endpoint { - /** - * get channels. - * - * @return channels - */ - Collection getChannels(); - - /** - * get channel. - * - * @param remoteAddress - * @return channel - */ - Channel getChannel(InetSocketAddress remoteAddress); - /** * get channel by url. * @@ -57,4 +39,35 @@ public interface Client extends Endpoint { * @return */ Channel connect(URL url); + + /** + * Sync send + * + * @param url the url + * @param message the message + * @param timeoutMillis the timeout millis + * @return object + */ + Object sendSync(final URL url, final Object message, final int timeoutMillis); + + /** + * Sync send + * + * @param channel the channel + * @param message the message + * @param timeoutMillis the timeout millis + * @return object + */ + Object sendSync(final Channel channel, final Object message, final int timeoutMillis); + + /** + * send with callback handler + * + * @param url the url + * @param message the message + * @param callbackHandler the callback handler + * @param timeoutMillis the timeout millis + */ + void sendCallback(final URL url, final Object message, CallbackHandler callbackHandler, + final int timeoutMillis); } \ No newline at end of file diff --git a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Endpoint.java b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Endpoint.java index 10cff4f22..d1b23ab5f 100644 --- a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Endpoint.java +++ b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Endpoint.java @@ -17,22 +17,16 @@ package com.alipay.sofa.registry.remoting; import java.net.InetSocketAddress; -import java.util.List; /** * The interface Endpoint. + * * @author shangyu.wh + * @author kezhu.wukz * @version $Id : Endpoint.java, v 0.1 2017-11-20 20:03 shangyu.wh Exp $ */ public interface Endpoint { - /** - * get channel handlers. - * - * @return channel handlers - */ - List getChannelHandlers(); - /** * get local address. * @@ -45,13 +39,6 @@ public interface Endpoint { */ void close(); - /** - * Close. - * - * @param channel the channel - */ - void close(final Channel channel); - /** * is closed. * @@ -59,31 +46,4 @@ public interface Endpoint { */ boolean isClosed(); - /** - * oneway send - * @param channel the channel - * @param message the message - */ - void sendOneway(final Channel channel, final Object message); - - /** - * Sync send - * - * @param channel the channel - * @param message the message - * @param timeoutMillis the timeout millis - * @return object - */ - Object sendSync(final Channel channel, final Object message, final int timeoutMillis); - - /** - * send with callback handler - * - * @param channel the channel - * @param message the message - * @param callbackHandler the callback handler - * @param timeoutMillis the timeout millis - */ - void sendCallback(final Channel channel, final Object message, CallbackHandler callbackHandler, - final int timeoutMillis); } \ No newline at end of file diff --git a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Server.java b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Server.java index 900bedbcd..586e97d94 100644 --- a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Server.java +++ b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/Server.java @@ -16,11 +16,11 @@ */ package com.alipay.sofa.registry.remoting; +import com.alipay.sofa.registry.common.model.store.URL; + import java.net.InetSocketAddress; import java.util.Collection; -import com.alipay.sofa.registry.common.model.store.URL; - /** * * @author shangyu.wh @@ -58,5 +58,33 @@ public interface Server extends Endpoint { */ Channel getChannel(URL url); + /** + * close the channel. + * + * @param channel + */ + void close(Channel channel); + int getChannelCount(); + + /** + * Sync send + * + * @param channel the channel + * @param message the message + * @param timeoutMillis the timeout millis + * @return object + */ + Object sendSync(final Channel channel, final Object message, final int timeoutMillis); + + /** + * send with callback handler + * + * @param channel the channel + * @param message the message + * @param callbackHandler the callback handler + * @param timeoutMillis the timeout millis + */ + void sendCallback(final Channel channel, final Object message, CallbackHandler callbackHandler, + final int timeoutMillis); } \ No newline at end of file diff --git a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/exchange/Exchange.java b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/exchange/Exchange.java index 961167107..f0f286413 100644 --- a/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/exchange/Exchange.java +++ b/server/remoting/api/src/main/java/com/alipay/sofa/registry/remoting/exchange/Exchange.java @@ -41,6 +41,18 @@ public interface Exchange { */ Client connect(String serverType, URL serverUrl, T... channelHandlers); + /** + * connect same type server,one server ip one connection + * such as different server on data server,serverOne and serverTwo,different type server must match different channelHandlers, + * so we must connect by serverType,and get Client instance by serverType + * @param serverType + * @param connNum connection number per serverUrl + * @param serverUrl + * @param channelHandlers + * @return + */ + Client connect(String serverType, int connNum, URL serverUrl, T... channelHandlers); + /** * bind server by server port in url parameter,one port must by same server type * @param url diff --git a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltChannel.java b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltChannel.java index 0e857268d..9a4ddd334 100644 --- a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltChannel.java +++ b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltChannel.java @@ -16,19 +16,21 @@ */ package com.alipay.sofa.registry.remoting.bolt; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import javax.ws.rs.client.WebTarget; + import com.alipay.remoting.AsyncContext; import com.alipay.remoting.BizContext; import com.alipay.remoting.Connection; import com.alipay.sofa.registry.remoting.Channel; -import javax.ws.rs.client.WebTarget; -import java.net.InetSocketAddress; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - /** * * @author shangyu.wh + * @author kezhu.wukz * @version $Id: BoltChannel.java, v 0.1 2017-11-24 16:46 shangyu.wh Exp $ */ public class BoltChannel implements Channel { @@ -84,6 +86,11 @@ public WebTarget getWebTarget() { return null; } + @Override + public void close() { + this.connection.close(); + } + /** * Getter method for property connection. * diff --git a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltClient.java b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltClient.java index ad09d9b31..cb206d35c 100644 --- a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltClient.java +++ b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltClient.java @@ -16,11 +16,19 @@ */ package com.alipay.sofa.registry.remoting.bolt; +import java.net.InetSocketAddress; +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; + import com.alipay.remoting.Connection; +import com.alipay.remoting.ConnectionEventProcessor; import com.alipay.remoting.ConnectionEventType; import com.alipay.remoting.InvokeCallback; +import com.alipay.remoting.Url; import com.alipay.remoting.exception.RemotingException; import com.alipay.remoting.rpc.RpcClient; +import com.alipay.remoting.rpc.protocol.RpcProtocol; import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; @@ -32,151 +40,137 @@ import com.alipay.sofa.registry.remoting.ChannelHandler.InvokeType; import com.alipay.sofa.registry.remoting.Client; -import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; -import java.util.concurrent.atomic.AtomicBoolean; - /** * The type Bolt client. + * + * @author kezhu.wukz * @author shangyu.wh * @version $Id : BoltClient.java, v 0.1 2017-11-27 14:46 shangyu.wh Exp $ */ public class BoltClient implements Client { - private static final Logger LOGGER = LoggerFactory.getLogger(BoltClient.class); - private RpcClient boltClient; - private List channelHandlers = new ArrayList<>(); - private Map channels = new HashMap<>(); - private AtomicBoolean initHandler = new AtomicBoolean(false); + private static final Logger LOGGER = LoggerFactory.getLogger(BoltClient.class); + + private RpcClient rpcClient; + + private AtomicBoolean closed = new AtomicBoolean(false); + + private int connectTimeout = 2000; + + private final int connNum; /** * Instantiates a new Bolt client. */ - public BoltClient() { - boltClient = new RpcClient(); - boltClient.init(); - } + public BoltClient(int connNum) { + rpcClient = new RpcClient(); + rpcClient.init(); - @Override - public Channel connect(URL targetUrl) { + this.connNum = connNum; + } - if (targetUrl == null) { - throw new IllegalArgumentException("Create connection targetUrl can not be null!"); - } - InetSocketAddress address = URL.toInetSocketAddress(targetUrl); - Channel c = getChannel(address); - if (c != null && c.isConnected()) { - LOGGER.info("Target url:" + targetUrl + " has been connected!", targetUrl); - return c; + /** + * Setter method for property channelHandlers. + * + * @param channelHandlers value to be assigned to property channelHandlers + */ + public void initHandlers(List channelHandlers) { + ChannelHandler connectionEventHandler = null; + for (ChannelHandler channelHandler : channelHandlers) { + if (HandlerType.LISENTER.equals(channelHandler.getType())) { + connectionEventHandler = channelHandler; + break; + } } - initHandler(); - - try { - Connection connection = boltClient - .getConnection(NetUtil.toAddressString(address), 1000); - if (connection != null) { - BoltChannel channel = new BoltChannel(); - channel.setConnection(connection); - channels.put(connection.getUrl().getOriginUrl(), channel); - return channel; - } else { - throw new RuntimeException("Bolt client connect server get none connection!"); + rpcClient.addConnectionEventProcessor(ConnectionEventType.CONNECT, + newConnectionEventAdapter(connectionEventHandler, ConnectionEventType.CONNECT)); + rpcClient.addConnectionEventProcessor(ConnectionEventType.CLOSE, + newConnectionEventAdapter(connectionEventHandler, ConnectionEventType.CLOSE)); + rpcClient.addConnectionEventProcessor(ConnectionEventType.EXCEPTION, + newConnectionEventAdapter(connectionEventHandler, ConnectionEventType.EXCEPTION)); + + for (ChannelHandler channelHandler : channelHandlers) { + if (HandlerType.PROCESSER.equals(channelHandler.getType())) { + if (InvokeType.SYNC.equals(channelHandler.getInvokeType())) { + rpcClient.registerUserProcessor(newSyncProcessor(channelHandler)); + } else { + rpcClient.registerUserProcessor(newAsyncProcessor(channelHandler)); + } } - - } catch (RemotingException e) { - LOGGER.error("Bolt client connect server got a RemotingException! target url:" - + targetUrl, e); - throw new RuntimeException("Bolt client connect server got a RemotingException!", e); - } catch (InterruptedException e) { - LOGGER.error("Bolt client connect server has been Interrupted!", e); - throw new RuntimeException("Bolt client connect server has been Interrupted!", e); } } - private void initHandler() { - - if (initHandler.compareAndSet(false, true)) { - boltClient.addConnectionEventProcessor(ConnectionEventType.CONNECT, - new ConnectionEventAdapter(ConnectionEventType.CONNECT, - getConnectionEventHandler(), null)); - boltClient.addConnectionEventProcessor(ConnectionEventType.CLOSE, - new ConnectionEventAdapter(ConnectionEventType.CLOSE, getConnectionEventHandler(), - null)); - boltClient.addConnectionEventProcessor(ConnectionEventType.EXCEPTION, - new ConnectionEventAdapter(ConnectionEventType.EXCEPTION, - getConnectionEventHandler(), null)); + protected ConnectionEventProcessor newConnectionEventAdapter(ChannelHandler connectionEventHandler, + ConnectionEventType connectEventType) { + return new ConnectionEventAdapter(connectEventType, connectionEventHandler, null); + } - registerUserProcessorHandler(); - } + protected AsyncUserProcessorAdapter newAsyncProcessor(ChannelHandler channelHandler) { + return new AsyncUserProcessorAdapter(channelHandler); } - private void registerUserProcessorHandler() { - if (channelHandlers != null) { - for (ChannelHandler channelHandler : channelHandlers) { - if (HandlerType.PROCESSER.equals(channelHandler.getType())) { - if (InvokeType.SYNC.equals(channelHandler.getInvokeType())) { - boltClient.registerUserProcessor(new SyncUserProcessorAdapter( - channelHandler)); - } else { - boltClient.registerUserProcessor(new AsyncUserProcessorAdapter( - channelHandler)); - } - } - } - } + protected SyncUserProcessorAdapter newSyncProcessor(ChannelHandler channelHandler) { + return new SyncUserProcessorAdapter(channelHandler); } @Override - public Collection getChannels() { - - Collection chs = new HashSet<>(); - for (Channel channel : this.channels.values()) { - if (channel.isConnected()) { - chs.add(channel); - } else { - channels.remove(NetUtil.toAddressString(channel.getRemoteAddress())); - } + public Channel connect(URL url) { + if (url == null) { + throw new IllegalArgumentException("Create connection url can not be null!"); } - return chs; - } + try { + getBoltConnection(rpcClient, url); + Connection connection = getBoltConnection(rpcClient, url); + BoltChannel channel = new BoltChannel(); + channel.setConnection(connection); + return channel; - @Override - public Channel getChannel(InetSocketAddress remoteAddress) { - Channel c = channels.get(NetUtil.toAddressString(remoteAddress)); - if (c == null || !c.isConnected()) { - return null; + } catch (RemotingException e) { + LOGGER + .error("Bolt client connect server got a RemotingException! target url:" + url, e); + throw new RuntimeException("Bolt client connect server got a RemotingException!", e); } - return c; } - @Override - public Channel getChannel(URL url) { - Channel c = channels.get(url.getAddressString()); - if (c == null || !c.isConnected()) { - return null; + protected Connection getBoltConnection(RpcClient rpcClient, URL url) throws RemotingException { + Url boltUrl = createBoltUrl(url); + try { + Connection connection = rpcClient.getConnection(boltUrl, connectTimeout); + if (connection == null || !connection.isFine()) { + if (connection != null) { + connection.close(); + } + throw new RemotingException("Get bolt connection failed for boltUrl: " + boltUrl); + } + return connection; + } catch (InterruptedException e) { + throw new RuntimeException( + "BoltClient rpcClient.getConnection InterruptedException! target boltUrl:" + + boltUrl, e); } - return c; } - @Override - public List getChannelHandlers() { - return channelHandlers; + protected Url createBoltUrl(URL url) { + Url boltUrl = new Url(url.getIpAddress(), url.getPort()); + boltUrl.setProtocol(RpcProtocol.PROTOCOL_CODE); + boltUrl.setConnNum(connNum); + boltUrl.setConnWarmup(true); + return boltUrl; } - /** - * Setter method for property channelHandlers. - * - * @param channelHandlers value to be assigned to property channelHandlers - */ - public void setChannelHandlers(List channelHandlers) { - this.channelHandlers = channelHandlers; + @Override + public Channel getChannel(URL url) { + try { + Connection connection = getBoltConnection(rpcClient, url); + BoltChannel channel = new BoltChannel(); + channel.setConnection(connection); + return channel; + } catch (RemotingException e) { + LOGGER + .error("Bolt client connect server got a RemotingException! target url:" + url, e); + throw new RuntimeException("Bolt client connect server got a RemotingException!", e); + } } @Override @@ -184,100 +178,46 @@ public InetSocketAddress getLocalAddress() { return NetUtil.getLocalSocketAddress(); } - /** - * Gets connection event handler. - * - * @return the connection event handler - */ - public ChannelHandler getConnectionEventHandler() { - if (channelHandlers != null) { - for (ChannelHandler channelHandler : channelHandlers) { - if (HandlerType.LISENTER.equals(channelHandler.getType())) { - return channelHandler; - } - } - } - return null; - } - @Override public void close() { - Collection chs = getChannels(); - if (chs != null && chs.size() > 0) { - for (Channel ch : chs) { - if (ch != null) { - boltClient.closeStandaloneConnection(((BoltChannel) ch).getConnection()); - } - } - } - } - - @Override - public void close(Channel channel) { - if (channel != null) { - Connection connection = ((BoltChannel) channel).getConnection(); - if (null != connection.getUrl() && null != connection.getUrl().getOriginUrl()) { - channels.remove(connection.getUrl().getOriginUrl()); - } - boltClient.closeStandaloneConnection(connection); + if (closed.compareAndSet(false, true)) { + rpcClient.shutdown(); } } @Override public boolean isClosed() { - boolean ret = false; - Collection chs = getChannels(); - if (chs != null && chs.size() > 0) { - for (Channel ch : chs) { - if (ch != null && !ch.isConnected()) { - ret = true; - break; - } - } - } else { - //has no channels - return true; - } - return ret; + return closed.get(); } @Override - public void sendOneway(Channel channel, Object message) { - if (channel != null && channel.isConnected()) { - if (channel instanceof BoltChannel) { - BoltChannel boltChannel = (BoltChannel) channel; - try { - boltClient.oneway(boltChannel.getConnection(), message); - } catch (RemotingException e) { - LOGGER.error("Bolt Client oneway request RemotingException! target url: {}", - boltChannel.getRemoteAddress(), e); - } - } + public Object sendSync(URL url, Object message, int timeoutMillis) { + try { + return rpcClient.invokeSync(createBoltUrl(url), message, timeoutMillis); + } catch (RemotingException e) { + String msg = "Bolt Client sendSync message RemotingException! target url:" + url; + LOGGER.error(msg, e); + throw new RuntimeException(msg, e); + } catch (InterruptedException e) { + throw new RuntimeException( + "Bolt Client sendSync message InterruptedException! target url:" + url, e); } } @Override public Object sendSync(Channel channel, Object message, int timeoutMillis) { - if (channel != null && channel.isConnected()) { - if (channel instanceof BoltChannel) { - BoltChannel boltChannel = (BoltChannel) channel; - try { - return boltClient.invokeSync(boltChannel.getConnection(), message, - timeoutMillis); - } catch (RemotingException e) { - LOGGER.error("Bolt Client sendSync message RemotingException! target url:" - + boltChannel.getRemoteAddress(), e); - throw new RuntimeException("Bolt Client sendSync message RemotingException!", e); - } catch (InterruptedException e) { - LOGGER.error("Bolt Client sendSync message InterruptedException! target url:" - + boltChannel.getRemoteAddress(), e); - throw new RuntimeException( - "Bolt Client sendSync message InterruptedException!", e); - } - } else { - throw new IllegalArgumentException("Input channel instance error! instance class:" - + channel.getClass().getName()); + BoltChannel boltChannel = (BoltChannel) channel; + try { + return rpcClient.invokeSync(boltChannel.getConnection(), message, timeoutMillis); + } catch (RemotingException e) { + LOGGER.error("Bolt Client sendSync message RemotingException! target boltUrl:" + + boltChannel.getRemoteAddress(), e); + throw new RuntimeException("Bolt Client sendSync message RemotingException!", e); + } catch (InterruptedException e) { + LOGGER.error("Bolt Client sendSync message InterruptedException! target boltUrl:" + + boltChannel.getRemoteAddress(), e); + throw new RuntimeException("Bolt Client sendSync message InterruptedException!", e); } } throw new IllegalArgumentException( @@ -286,44 +226,35 @@ public Object sendSync(Channel channel, Object message, int timeoutMillis) { } @Override - public void sendCallback(Channel channel, Object message, CallbackHandler callbackHandler, + public void sendCallback(URL url, Object message, CallbackHandler callbackHandler, int timeoutMillis) { - if (channel != null && channel.isConnected()) { - if (channel instanceof BoltChannel) { - BoltChannel boltChannel = (BoltChannel) channel; - try { - boltClient.invokeWithCallback(boltChannel.getConnection(), message, - new InvokeCallback() { - - @Override - public void onResponse(Object result) { - callbackHandler.onCallback(channel, result); - } + try { + Connection connection = getBoltConnection(rpcClient, url); + BoltChannel channel = new BoltChannel(); + channel.setConnection(connection); + rpcClient.invokeWithCallback(connection, message, new InvokeCallback() { + + @Override + public void onResponse(Object result) { + callbackHandler.onCallback(channel, result); + } - @Override - public void onException(Throwable e) { - callbackHandler.onException(channel, e); - } + @Override + public void onException(Throwable e) { + callbackHandler.onException(channel, e); + } - @Override - public Executor getExecutor() { - return null; - } - }, timeoutMillis); - return; - } catch (RemotingException e) { - LOGGER.error("Bolt Client sendSync message RemotingException! target url:" - + boltChannel.getRemoteAddress(), e); - throw new RuntimeException("Bolt Client sendSync message RemotingException!", e); + @Override + public Executor getExecutor() { + return callbackHandler.getExecutor(); } - } else { - throw new IllegalArgumentException("Input channel instance error! instance class:" - + channel.getClass().getName()); - } + }, timeoutMillis); + return; + } catch (RemotingException e) { + String msg = "Bolt Client sendSync message RemotingException! target url:" + url; + LOGGER.error(msg, e); + throw new RuntimeException(msg, e); } - throw new IllegalArgumentException( - "Input channel: " + channel - + " error! channel cannot be null,or channel must be connected!"); } } \ No newline at end of file diff --git a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltServer.java b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltServer.java index af2c3d71a..547c073e8 100644 --- a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltServer.java +++ b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/BoltServer.java @@ -206,11 +206,6 @@ public Channel getChannel(URL url) { return null; } - @Override - public List getChannelHandlers() { - return channelHandlers; - } - @Override public InetSocketAddress getLocalAddress() { return new InetSocketAddress(url.getPort()); @@ -240,49 +235,27 @@ public boolean isClosed() { return !isStarted.get(); } - @Override - public void sendOneway(Channel channel, Object message) { - if (channel != null && channel.isConnected()) { - try { - Url url = new Url(channel.getRemoteAddress().getAddress().getHostAddress(), channel - .getRemoteAddress().getPort()); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Bolt Server one way message:{} , target url:{}", message, url); - } - boltServer.oneway(url, message); - } catch (RemotingException e) { - LOGGER.error("Bolt Server one way message RemotingException! target url:" + url, e); - throw new RuntimeException("Bolt Server one way message RemotingException!", e); - } catch (InterruptedException e) { - LOGGER.error("Bolt Server one way message InterruptedException! target url:" + url, - e); - throw new RuntimeException("Bolt Server one way message InterruptedException!", e); - } - } - throw new IllegalArgumentException( - "Send message connection can not be null or connection not be connected!"); - } - @Override public Object sendSync(Channel channel, Object message, int timeoutMillis) { if (channel != null && channel.isConnected()) { + Url boltUrl = null; try { - Url url = new Url(channel.getRemoteAddress().getAddress().getHostAddress(), channel + boltUrl = new Url(channel.getRemoteAddress().getAddress().getHostAddress(), channel .getRemoteAddress().getPort()); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Bolt Server sendSync message:{} , target url:{}", message, url); + LOGGER.debug("Bolt Server sendSync message:{} , target url:{}", message, + boltUrl); } - return boltServer.invokeSync(url, message, timeoutMillis); + return boltServer.invokeSync(boltUrl, message, timeoutMillis); } catch (RemotingException e) { - LOGGER - .error("Bolt Server sendSync message RemotingException! target url:" + url, e); + LOGGER.error("Bolt Server sendSync message RemotingException! target url:" + + boltUrl, e); throw new RuntimeException("Bolt Server sendSync message RemotingException!", e); } catch (InterruptedException e) { - LOGGER.error( - "Bolt Server sendSync message InterruptedException! target url:" + url, e); + LOGGER.error("Bolt Server sendSync message InterruptedException! target url:" + + boltUrl, e); throw new RuntimeException("Bolt Server sendSync message InterruptedException!", e); } } @@ -294,14 +267,16 @@ public Object sendSync(Channel channel, Object message, int timeoutMillis) { public void sendCallback(Channel channel, Object message, CallbackHandler callbackHandler, int timeoutMillis) { if (channel != null && channel.isConnected()) { + Url boltUrl = null; try { - Url url = new Url(channel.getRemoteAddress().getAddress().getHostAddress(), channel + boltUrl = new Url(channel.getRemoteAddress().getAddress().getHostAddress(), channel .getRemoteAddress().getPort()); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Bolt Server sendSync message:{} , target url:{}", message, url); + LOGGER.debug("Bolt Server sendSync message:{} , target url:{}", message, + boltUrl); } - boltServer.invokeWithCallback(url, message, new InvokeCallback() { + boltServer.invokeWithCallback(boltUrl, message, new InvokeCallback() { @Override public void onResponse(Object result) { callbackHandler.onCallback(channel, result); @@ -314,7 +289,7 @@ public void onException(Throwable e) { @Override public Executor getExecutor() { - return null; + return callbackHandler.getExecutor(); } }, timeoutMillis); return; @@ -322,7 +297,8 @@ public Executor getExecutor() { throw new RuntimeException("Bolt Server invoke with callback RemotingException!", e); } catch (InterruptedException e) { PUSH_LOGGER.error( - "Bolt Server invoke with callback InterruptedException! target url:" + url, e); + "Bolt Server invoke with callback InterruptedException! target url:" + boltUrl, + e); throw new RuntimeException( "Bolt Server invoke with callback InterruptedException!", e); } diff --git a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/exchange/BoltExchange.java b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/exchange/BoltExchange.java index 701729786..52627f664 100644 --- a/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/exchange/BoltExchange.java +++ b/server/remoting/bolt/src/main/java/com/alipay/sofa/registry/remoting/bolt/exchange/BoltExchange.java @@ -16,8 +16,11 @@ */ package com.alipay.sofa.registry.remoting.bolt.exchange; +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import com.alipay.sofa.registry.common.model.store.URL; -import com.alipay.sofa.registry.remoting.Channel; import com.alipay.sofa.registry.remoting.ChannelHandler; import com.alipay.sofa.registry.remoting.Client; import com.alipay.sofa.registry.remoting.Server; @@ -25,10 +28,6 @@ import com.alipay.sofa.registry.remoting.bolt.BoltServer; import com.alipay.sofa.registry.remoting.exchange.Exchange; -import java.util.Arrays; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - /** * * @author shangyu.wh @@ -42,38 +41,26 @@ public class BoltExchange implements Exchange { @Override public Client connect(String serverType, URL serverUrl, ChannelHandler... channelHandlers) { + return this.connect(serverType, 1, serverUrl, channelHandlers); + } + @Override + public Client connect(String serverType, int connNum, URL serverUrl, ChannelHandler... channelHandlers) { if (channelHandlers == null) { throw new IllegalArgumentException("channelHandlers cannot be null!"); } - Client client = clients.get(serverType); - if (client == null) { - BoltClient boltClient = new BoltClient(); - boltClient.setChannelHandlers(Arrays.asList(channelHandlers)); - boltClient.connect(serverUrl); - client = clients.putIfAbsent(serverType, boltClient); - if (client == null) { - client = boltClient; - } - } else { - Channel channel = client.getChannel(serverUrl); - if (channel == null) { - BoltClient boltClient = (BoltClient) client; - boltClient.setChannelHandlers(Arrays.asList(channelHandlers)); - boltClient.connect(serverUrl); - } - } + Client client = clients.computeIfAbsent(serverType, key -> newBoltClient(connNum, channelHandlers)); + client.connect(serverUrl); return client; } @Override public Server open(URL url, ChannelHandler... channelHandlers) { - if (channelHandlers == null) { throw new IllegalArgumentException("channelHandlers cannot be null!"); } - BoltServer server = new BoltServer(url, Arrays.asList(channelHandlers)); + BoltServer server = createBoltServer(url, channelHandlers); setServer(server, url); server.startServer(); return server; @@ -97,4 +84,18 @@ public Server getServer(Integer port) { public void setServer(Server server, URL url) { serverMap.putIfAbsent(url.getPort(), server); } + + private BoltClient newBoltClient(int connNum, ChannelHandler[] channelHandlers) { + BoltClient boltClient = createBoltClient(connNum); + boltClient.initHandlers(Arrays.asList(channelHandlers)); + return boltClient; + } + + protected BoltClient createBoltClient(int connNum) { + return new BoltClient(connNum); + } + + protected BoltServer createBoltServer(URL url, ChannelHandler[] channelHandlers) { + return new BoltServer(url, Arrays.asList(channelHandlers)); + } } \ No newline at end of file diff --git a/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/BoltServerTest.java b/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/BoltServerTest.java index 36e8e3830..cfb25f7c8 100644 --- a/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/BoltServerTest.java +++ b/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/BoltServerTest.java @@ -16,13 +16,14 @@ */ package com.alipay.sofa.registry.remoting.bolt; -import com.alipay.sofa.registry.remoting.Channel; -import com.alipay.sofa.registry.remoting.bolt.BoltServer; +import java.net.InetSocketAddress; + +import javax.ws.rs.client.WebTarget; + import org.junit.Assert; import org.junit.Test; -import javax.ws.rs.client.WebTarget; -import java.net.InetSocketAddress; +import com.alipay.sofa.registry.remoting.Channel; /** * @@ -67,6 +68,11 @@ public void setAttribute(String key, Object value) { public WebTarget getWebTarget() { return null; } + + @Override + public void close() { + + } }); } sessionServer.getChannels(); diff --git a/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/MockChannel.java b/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/MockChannel.java index b36f74838..d00c5d5b9 100644 --- a/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/MockChannel.java +++ b/server/remoting/bolt/src/test/java/com/alipay/sofa/registry/remoting/bolt/MockChannel.java @@ -16,10 +16,11 @@ */ package com.alipay.sofa.registry.remoting.bolt; -import com.alipay.sofa.registry.remoting.Channel; +import java.net.InetSocketAddress; import javax.ws.rs.client.WebTarget; -import java.net.InetSocketAddress; + +import com.alipay.sofa.registry.remoting.Channel; /** * @author xuanbei @@ -55,4 +56,9 @@ public void setAttribute(String key, Object value) { public WebTarget getWebTarget() { return null; } + + @Override + public void close() { + + } } diff --git a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyChannel.java b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyChannel.java index 54154d40f..5a061273a 100644 --- a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyChannel.java +++ b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyChannel.java @@ -16,13 +16,14 @@ */ package com.alipay.sofa.registry.remoting.jersey; -import com.alipay.sofa.registry.net.NetUtil; -import com.alipay.sofa.registry.remoting.Channel; +import java.net.InetSocketAddress; +import java.net.URI; import javax.ws.rs.client.Client; import javax.ws.rs.client.WebTarget; -import java.net.InetSocketAddress; -import java.net.URI; + +import com.alipay.sofa.registry.net.NetUtil; +import com.alipay.sofa.registry.remoting.Channel; /** * @@ -77,6 +78,11 @@ public WebTarget getWebTarget() { return webTarget; } + @Override + public void close() { + client.close(); + } + /** * Setter method for property webTarget. * diff --git a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyClient.java b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyClient.java index 825a7dd51..c682a8cf0 100644 --- a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyClient.java +++ b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyClient.java @@ -16,28 +16,27 @@ */ package com.alipay.sofa.registry.remoting.jersey; +import java.net.InetSocketAddress; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.UriBuilder; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.HttpUrlConnectorProvider; +import org.glassfish.jersey.jackson.JacksonFeature; + import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.net.NetUtil; import com.alipay.sofa.registry.remoting.CallbackHandler; import com.alipay.sofa.registry.remoting.Channel; -import com.alipay.sofa.registry.remoting.ChannelHandler; import com.alipay.sofa.registry.remoting.Client; -import org.glassfish.jersey.client.ClientConfig; -import org.glassfish.jersey.client.HttpUrlConnectorProvider; -import org.glassfish.jersey.jackson.JacksonFeature; - -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.UriBuilder; -import java.net.InetSocketAddress; -import java.net.URI; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; /** * @@ -49,7 +48,9 @@ public class JerseyClient implements Client { private static final Logger LOGGER = LoggerFactory .getLogger(JerseyClient.class); private volatile static JerseyClient instance; + private final AtomicReference client = new AtomicReference<>(null); + private Map channels = new HashMap<>(); /** @@ -89,6 +90,22 @@ public Channel connect(URL url) { } } + @Override + public Object sendSync(URL url, Object message, int timeoutMillis) { + return null; + } + + @Override + public Object sendSync(Channel channel, Object message, int timeoutMillis) { + return null; + } + + @Override + public void sendCallback(URL url, Object message, CallbackHandler callbackHandler, + int timeoutMillis) { + + } + private WebTarget getTarget(URL targetUrl) { return getClient().target(getBaseUri(targetUrl)); } @@ -125,24 +142,6 @@ public URI getBaseUri(URL targetUrl) { return uri; } - @Override - public Collection getChannels() { - return null; - } - - @Override - public Channel getChannel(InetSocketAddress remoteAddress) { - Channel c = channels.get(NetUtil.toAddressString(remoteAddress)); - if (c == null) { - return null; - } else { - if (!c.isConnected()) { - connect(new URL(remoteAddress)); - } - } - return c; - } - @Override public Channel getChannel(URL url) { Channel c = channels.get(url.getAddressString()); @@ -156,11 +155,6 @@ public Channel getChannel(URL url) { return c; } - @Override - public List getChannelHandlers() { - return null; - } - @Override public InetSocketAddress getLocalAddress() { return NetUtil.getLocalSocketAddress(); @@ -171,29 +165,9 @@ public void close() { } - @Override - public void close(Channel channel) { - - } - @Override public boolean isClosed() { return false; } - @Override - public void sendOneway(Channel channel, Object message) { - - } - - @Override - public Object sendSync(Channel channel, Object message, int timeoutMillis) { - return null; - } - - @Override - public void sendCallback(Channel channel, Object message, CallbackHandler callbackHandler, - int timeoutMillis) { - - } } \ No newline at end of file diff --git a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyJettyServer.java b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyJettyServer.java index 9907c2f31..672422037 100644 --- a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyJettyServer.java +++ b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/JerseyJettyServer.java @@ -16,15 +16,14 @@ */ package com.alipay.sofa.registry.remoting.jersey; -import java.net.InetSocketAddress; -import java.net.URI; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.ws.rs.ProcessingException; - +import com.alipay.sofa.registry.common.model.store.URL; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.remoting.CallbackHandler; +import com.alipay.sofa.registry.remoting.Channel; +import com.alipay.sofa.registry.remoting.ChannelHandler; +import com.alipay.sofa.registry.remoting.Server; +import com.alipay.sofa.registry.remoting.jersey.jetty.server.HttpConnectionCustomFactory; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.util.thread.QueuedThreadPool; @@ -36,14 +35,13 @@ import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spi.Container; -import com.alipay.sofa.registry.common.model.store.URL; -import com.alipay.sofa.registry.log.Logger; -import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.remoting.CallbackHandler; -import com.alipay.sofa.registry.remoting.Channel; -import com.alipay.sofa.registry.remoting.ChannelHandler; -import com.alipay.sofa.registry.remoting.Server; -import com.alipay.sofa.registry.remoting.jersey.jetty.server.HttpConnectionCustomFactory; +import javax.ws.rs.ProcessingException; +import java.net.InetSocketAddress; +import java.net.URI; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicBoolean; /** * @@ -165,8 +163,8 @@ public Channel getChannel(URL url) { } @Override - public List getChannelHandlers() { - return null; + public void close(Channel channel) { + throw new UnsupportedOperationException("Jersey Server don't support close Channel."); } @Override @@ -188,11 +186,6 @@ public void close() { throw new RuntimeException("Jersey Server has not started!Server Channel has not created!"); } - @Override - public void close(Channel channel) { - throw new UnsupportedOperationException("Jersey Server don't support close Channel."); - } - @Override public boolean isClosed() { if (server != null) { @@ -201,11 +194,6 @@ public boolean isClosed() { return true; } - @Override - public void sendOneway(Channel channel, Object message) { - - } - @Override public Object sendSync(Channel channel, Object message, int timeoutMillis) { return null; diff --git a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/exchange/JerseyExchange.java b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/exchange/JerseyExchange.java index be001a92b..f73c8b05f 100644 --- a/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/exchange/JerseyExchange.java +++ b/server/remoting/http/src/main/java/com/alipay/sofa/registry/remoting/jersey/exchange/JerseyExchange.java @@ -16,6 +16,13 @@ */ package com.alipay.sofa.registry.remoting.jersey.exchange; +import java.net.URI; +import java.util.concurrent.ConcurrentHashMap; + +import javax.ws.rs.core.UriBuilder; + +import org.glassfish.jersey.server.ResourceConfig; + import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; @@ -24,11 +31,6 @@ import com.alipay.sofa.registry.remoting.exchange.Exchange; import com.alipay.sofa.registry.remoting.jersey.JerseyClient; import com.alipay.sofa.registry.remoting.jersey.JerseyJettyServer; -import org.glassfish.jersey.server.ResourceConfig; - -import javax.ws.rs.core.UriBuilder; -import java.net.URI; -import java.util.concurrent.ConcurrentHashMap; /** * @@ -51,6 +53,12 @@ public Client connect(String serverType, URL serverUrl, ResourceConfig... channe return jerseyClient; } + @Override + public Client connect(String serverType, int connNum, URL serverUrl, + ResourceConfig... channelHandlers) { + throw new UnsupportedOperationException(); + } + @Override public Server open(URL url, ResourceConfig... resources) { diff --git a/server/remoting/http/src/test/java/com/alipay/sofa/registry/remoting/jersey/JerseyExchangeTest.java b/server/remoting/http/src/test/java/com/alipay/sofa/registry/remoting/jersey/JerseyExchangeTest.java index 22713c6d2..ee005fc98 100644 --- a/server/remoting/http/src/test/java/com/alipay/sofa/registry/remoting/jersey/JerseyExchangeTest.java +++ b/server/remoting/http/src/test/java/com/alipay/sofa/registry/remoting/jersey/JerseyExchangeTest.java @@ -16,19 +16,21 @@ */ package com.alipay.sofa.registry.remoting.jersey; -import com.alipay.sofa.registry.common.model.store.URL; -import com.alipay.sofa.registry.net.NetUtil; -import com.alipay.sofa.registry.remoting.CallbackHandler; -import com.alipay.sofa.registry.remoting.Channel; -import com.alipay.sofa.registry.remoting.jersey.exchange.JerseyExchange; +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; + +import java.net.InetSocketAddress; +import java.util.concurrent.Executor; + import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.junit.Assert; import org.junit.Test; -import java.net.InetSocketAddress; - -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; +import com.alipay.sofa.registry.common.model.store.URL; +import com.alipay.sofa.registry.net.NetUtil; +import com.alipay.sofa.registry.remoting.CallbackHandler; +import com.alipay.sofa.registry.remoting.Channel; +import com.alipay.sofa.registry.remoting.jersey.exchange.JerseyExchange; /** * @author xuanbei @@ -51,6 +53,11 @@ public void onCallback(Channel channel, Object message) { @Override public void onException(Channel channel, Throwable exception) { } + + @Override + public Executor getExecutor() { + return null; + } }; JerseyExchange jerseyExchange = new JerseyExchange(); @@ -80,20 +87,11 @@ private void testJerseyJettyServer(URL url, JerseyJettyServer jerseyJettyServer, Assert.assertNull(jerseyJettyServer.getChannels()); Assert.assertNull(jerseyJettyServer.getChannel(new InetSocketAddress(9663))); Assert.assertNull(jerseyJettyServer.getChannel(url)); - Assert.assertNull(jerseyJettyServer.getChannelHandlers()); Assert.assertEquals(new InetSocketAddress(JERSEY_TEST_PORT), jerseyJettyServer.getLocalAddress()); Assert.assertFalse(jerseyJettyServer.isClosed()); - boolean isException = false; - try { - jerseyJettyServer.close(new JerseyChannel()); - } catch (Throwable t) { - isException = true; - } - Assert.assertTrue(isException); jerseyJettyServer.sendCallback(new JerseyChannel(), new Object(), callbackHandler, 1000); - jerseyJettyServer.sendOneway(new JerseyChannel(), new Object()); Assert.assertNull(jerseyJettyServer.sendSync(new JerseyChannel(), new Object(), 1000)); } @@ -101,15 +99,10 @@ private void testJerseyClient(URL url, JerseyClient jerseyClient, CallbackHandler callbackHandler) { Assert.assertEquals(NetUtil.getLocalSocketAddress(), jerseyClient.getLocalAddress()); Assert.assertFalse(jerseyClient.isClosed()); - Assert.assertNull(jerseyClient.getChannels()); - Assert.assertNull(jerseyClient.getChannel(new InetSocketAddress(9663))); Assert.assertNull(jerseyClient.getChannel(url)); - Assert.assertNull(jerseyClient.getChannelHandlers()); - Assert.assertNull(jerseyClient.sendSync(new JerseyChannel(), new Object(), 1000)); + Assert.assertNull(jerseyClient.sendSync(new URL(), new Object(), 1000)); jerseyClient.close(); - jerseyClient.close(new JerseyChannel()); - jerseyClient.sendOneway(new JerseyChannel(), new Object()); - jerseyClient.sendCallback(new JerseyChannel(), new Object(), callbackHandler, 1000); + jerseyClient.sendCallback(new URL(), new Object(), callbackHandler, 1000); } private void testJerseyChannel(JerseyChannel jerseyChannel) { diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerBeanConfiguration.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerBeanConfiguration.java index 155861aab..7b8a2d590 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerBeanConfiguration.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerBeanConfiguration.java @@ -127,7 +127,7 @@ public CommonConfig commonConfig() { } @Bean - @ConditionalOnMissingBean(name = "dataServerConfig") + @ConditionalOnMissingBean public DataServerConfig dataServerConfig(CommonConfig commonConfig) { return new DataServerConfig(commonConfig); } @@ -207,7 +207,7 @@ public MetaServerConnectionFactory metaServerConnectionFactory() { } @Bean(name = "serverHandlers") - public Collection serverHandlers(DataServerConfig dataServerConfig) { + public Collection serverHandlers() { Collection list = new ArrayList<>(); list.add(getDataHandler()); list.add(clientOffHandler()); @@ -222,7 +222,7 @@ public Collection serverHandlers(DataServerConfig dataSer } @Bean(name = "serverSyncHandlers") - public Collection serverSyncHandlers(DataServerConfig dataServerConfig) { + public Collection serverSyncHandlers() { Collection list = new ArrayList<>(); list.add(getDataHandler()); list.add(publishDataProcessor()); @@ -370,7 +370,7 @@ public SnapshotBackUpNotifier snapshotBackUpNotifier() { } @Bean(name = "dataChangeNotifiers") - public List dataChangeNotifiers(DataServerConfig dataServerBootstrapConfig) { + public List dataChangeNotifiers() { List list = new ArrayList<>(); list.add(sessionServerNotifier()); list.add(tempPublisherNotifier()); diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerConfig.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerConfig.java index 6b47d4717..375656dc2 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerConfig.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/bootstrap/DataServerConfig.java @@ -16,18 +16,17 @@ */ package com.alipay.sofa.registry.server.data.bootstrap; +import com.alipay.sofa.registry.net.NetUtil; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.springframework.boot.context.properties.ConfigurationProperties; + import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.springframework.boot.context.properties.ConfigurationProperties; - -import com.alipay.sofa.registry.net.NetUtil; - /** * * @@ -89,19 +88,19 @@ public class DataServerConfig { private long notifyDataSyncExecutorKeepAliveTime = 60; - private long notifySessionRetryFirstDelay = 1000; + private long notifySessionRetryFirstDelay = 3000; - private long notifySessionRetryIncrementDelay = 1000; + private long notifySessionRetryIncrementDelay = 3000; - private int notifySessionRetryTimes = 10; + private int notifySessionRetryTimes = 5; - private int publishExecutorMinPoolSize = 80; + private int publishExecutorMinPoolSize = 200; private int publishExecutorMaxPoolSize = 400; private int publishExecutorQueueSize = 10000; - private int renewDatumExecutorMinPoolSize = 50; + private int renewDatumExecutorMinPoolSize = 100; private int renewDatumExecutorMaxPoolSize = 400; @@ -115,7 +114,7 @@ public class DataServerConfig { private int sessionServerNotifierRetryExecutorThreadSize = 10; - private int sessionServerNotifierRetryExecutorQueueSize = 1000000; + private int sessionServerNotifierRetryExecutorQueueSize = 10000; private int renewEnableDelaySec = 30; diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/cache/DatumCache.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/cache/DatumCache.java index 2b076924c..493811b7e 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/cache/DatumCache.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/cache/DatumCache.java @@ -26,6 +26,17 @@ import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import com.alipay.sofa.registry.common.model.dataserver.Datum; +import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.WordCache; +import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; +import com.alipay.sofa.registry.server.data.change.DataChangeTypeEnum; +import com.alipay.sofa.registry.server.data.node.DataServerNode; +import com.alipay.sofa.registry.server.data.remoting.dataserver.DataServerNodeFactory; + +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.dataserver.Datum; import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; @@ -37,7 +48,7 @@ * cache of datum, providing query function to the upper module * * @author qian.lqlq - * @version $Id: this.java, v 0.1 2017-12-06 20:50 qian.lqlq Exp $ + * @version $Id: DatumCache.java, v 0.1 2017-12-06 20:50 qian.lqlq Exp $ */ public class DatumCache { @@ -70,11 +81,9 @@ public class DatumCache { * @return */ public Datum get(String dataCenter, String dataInfoId) { - if (DATUM_MAP.containsKey(dataCenter)) { - Map map = DATUM_MAP.get(dataCenter); - if (map.containsKey(dataInfoId)) { - return map.get(dataInfoId); - } + Map map = DATUM_MAP.get(dataCenter); + if (map != null) { + return map.get(dataInfoId); } return null; } @@ -88,8 +97,9 @@ public Datum get(String dataCenter, String dataInfoId) { public Map get(String dataInfoId) { Map datumMap = new HashMap<>(); DATUM_MAP.forEach((dataCenter, datums) -> { - if (datums.containsKey(dataInfoId)) { - datumMap.put(dataCenter, datums.get(dataInfoId)); + Datum datum = datums.get(dataInfoId); + if (datum != null) { + datumMap.put(dataCenter, datum); } }); @@ -174,24 +184,21 @@ public MergeResult putDatum(DataChangeTypeEnum changeType, Datum datum) { return mergeResult; } - Datum ret = map.putIfAbsent(dataInfoId, datum); - if (ret == null) { - Set> entries = datum.getPubMap().entrySet(); - Iterator> iterator = entries.iterator(); + // filter out the unPubs of datum when first put. + // Otherwise, "syncData" or "fetchData" when get Datum with unPubs, which will result something error + boolean[] exists = { true }; + Datum cacheDatum = map.computeIfAbsent(dataInfoId, k -> filterUnPubs(exists, datum)); + if (!exists[0]) { + Iterator> iterator = datum.getPubMap().entrySet().iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); Publisher publisher = entry.getValue(); - if (!(publisher instanceof UnPublisher)) { - addToIndex(publisher); - } else { - //first put to cache,UnPublisher data must remove,not so got error pub data exist - iterator.remove(); - } + addToIndex(publisher); } mergeResult = new MergeResult(null, true); } else { if (changeType == DataChangeTypeEnum.MERGE) { - mergeResult = mergeDatum(datum); + mergeResult = mergeDatum(cacheDatum, datum); } else { Long lastVersion = coverDatum(datum); mergeResult = new MergeResult(lastVersion, true); @@ -200,6 +207,23 @@ public MergeResult putDatum(DataChangeTypeEnum changeType, Datum datum) { return mergeResult; } + /** + * remove unPubs from datum + */ + private Datum filterUnPubs(boolean[] exists, Datum datum) { + Iterator> iterator = datum.getPubMap().entrySet().iterator(); + while (iterator.hasNext()) { + Entry entry = iterator.next(); + Publisher publisher = entry.getValue(); + if (publisher instanceof UnPublisher) { + //first put to cache,UnPublisher data must remove,not so got error pub data exist + iterator.remove(); + } + } + exists[0] = false; + return datum; + } + private Map getDatumMapByDataCenter(String dataCenter) { Map map = DATUM_MAP.get(dataCenter); if (map == null) { @@ -247,9 +271,8 @@ public boolean cleanDatum(String dataCenter, String dataInfoId) { * @param datum * @return */ - private MergeResult mergeDatum(Datum datum) { + private MergeResult mergeDatum(Datum cacheDatum, Datum datum) { boolean isChanged = false; - Datum cacheDatum = DATUM_MAP.get(datum.getDataCenter()).get(datum.getDataInfoId()); Map cachePubMap = cacheDatum.getPubMap(); Map pubMap = datum.getPubMap(); for (Entry pubEntry : pubMap.entrySet()) { @@ -396,7 +419,7 @@ private void addToIndex(Publisher publisher) { } private String getConnectId(Publisher cachePub) { - return cachePub.getSourceAddress().getAddressString(); + return WordCache.getInstance().getWordCache(cachePub.getSourceAddress().getAddressString()); } /** diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/SessionServerNotifier.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/SessionServerNotifier.java index 451f89804..f66fb747e 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/SessionServerNotifier.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/SessionServerNotifier.java @@ -19,6 +19,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import javax.annotation.PostConstruct; @@ -38,6 +39,7 @@ import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; import com.alipay.sofa.registry.server.data.cache.DatumCache; import com.alipay.sofa.registry.server.data.change.DataSourceTypeEnum; +import com.alipay.sofa.registry.server.data.executor.ExecutorFactory; import com.alipay.sofa.registry.server.data.remoting.sessionserver.SessionServerConnectionFactory; import com.alipay.sofa.registry.timer.AsyncHashedWheelTimer; import com.alipay.sofa.registry.timer.AsyncHashedWheelTimer.TaskFailedCallback; @@ -73,7 +75,7 @@ public void init() { ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder(); threadFactoryBuilder.setDaemon(true); asyncHashedWheelTimer = new AsyncHashedWheelTimer(threadFactoryBuilder.setNameFormat( - "Registry-SessionServerNotifier-WheelTimer").build(), 100, TimeUnit.MILLISECONDS, 1024, + "Registry-SessionServerNotifier-WheelTimer").build(), 500, TimeUnit.MILLISECONDS, 1024, dataServerConfig.getSessionServerNotifierRetryExecutorThreadSize(), dataServerConfig.getSessionServerNotifierRetryExecutorQueueSize(), threadFactoryBuilder .setNameFormat("Registry-SessionServerNotifier-WheelExecutor-%d").build(), @@ -103,7 +105,7 @@ public Set getSuitableSource() { public void notify(Datum datum, Long lastVersion) { DataChangeRequest request = new DataChangeRequest(datum.getDataInfoId(), datum.getDataCenter(), datum.getVersion()); - List connections = sessionServerConnectionFactory.getConnections(); + List connections = sessionServerConnectionFactory.getSessionConnections(); for (Connection connection : connections) { doNotify(new NotifyCallback(connection, request)); } @@ -115,13 +117,11 @@ private void doNotify(NotifyCallback notifyCallback) { try { //check connection active if (!connection.isFine()) { - if (LOGGER.isInfoEnabled()) { - LOGGER - .info(String - .format( - "connection from sessionServer(%s) is not fine, so ignore notify, retryTimes=%s,request=%s", - connection.getRemoteAddress(), notifyCallback.retryTimes, request)); - } + LOGGER + .info(String + .format( + "connection from sessionServer(%s) is not fine, so ignore notify, retryTimes=%s,request=%s", + connection.getRemoteAddress(), notifyCallback.retryTimes, request)); return; } Server sessionServer = boltExchange.getServer(dataServerConfig.getPort()); @@ -139,26 +139,32 @@ private void doNotify(NotifyCallback notifyCallback) { * on failed, retry if necessary */ private void onFailed(NotifyCallback notifyCallback) { + DataChangeRequest request = notifyCallback.request; Connection connection = notifyCallback.connection; notifyCallback.retryTimes++; + //check version, if it's fall behind, stop retry + long _currentVersion = datumCache.get(request.getDataCenter(), request.getDataInfoId()).getVersion(); + if (request.getVersion() != _currentVersion) { + LOGGER.info(String.format( + "current version change %s, retry version is %s, stop before retry! retryTimes=%s, request=%s", + _currentVersion, request.getVersion(), notifyCallback.retryTimes, request)); + return; + } + if (notifyCallback.retryTimes <= dataServerConfig.getNotifySessionRetryTimes()) { this.asyncHashedWheelTimer.newTimeout(timeout -> { - if (LOGGER.isInfoEnabled()) { - LOGGER.info(String.format("retrying notify sessionServer(%s), retryTimes=%s, request=%s", - connection.getRemoteAddress(), notifyCallback.retryTimes, request)); - } + LOGGER.info(String.format("retrying notify sessionServer(%s), retryTimes=%s, request=%s", + connection.getRemoteAddress(), notifyCallback.retryTimes, request)); //check version, if it's fall behind, stop retry long currentVersion = datumCache.get(request.getDataCenter(), request.getDataInfoId()).getVersion(); if (request.getVersion() == currentVersion) { doNotify(notifyCallback); } else { - if (LOGGER.isInfoEnabled()) { - LOGGER.info(String.format( - "current version change %s, retry version is %s, stop retry! retryTimes=%s, request=%s", - currentVersion, request.getVersion(), notifyCallback.retryTimes, request)); - } + LOGGER.info(String.format( + "current version change %s, retry version is %s, stop retry! retryTimes=%s, request=%s", + currentVersion, request.getVersion(), notifyCallback.retryTimes, request)); } }, getDelayTimeForRetry(notifyCallback.retryTimes), TimeUnit.MILLISECONDS); } else { @@ -209,6 +215,11 @@ public void onException(Channel channel, Throwable e) { onFailed(this); } + @Override + public Executor getExecutor() { + return ExecutorFactory.NOTIFY_SESSION_CALLBACK_EXECUTOR; + } + } } \ No newline at end of file diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/TempPublisherNotifier.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/TempPublisherNotifier.java index f4f5f5e9a..64d61ed90 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/TempPublisherNotifier.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/change/notify/TempPublisherNotifier.java @@ -16,13 +16,6 @@ */ package com.alipay.sofa.registry.server.data.change.notify; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.Executor; - -import org.springframework.beans.factory.annotation.Autowired; - import com.alipay.remoting.Connection; import com.alipay.remoting.InvokeCallback; import com.alipay.sofa.registry.common.model.CommonResponse; @@ -38,6 +31,12 @@ import com.alipay.sofa.registry.server.data.change.DataSourceTypeEnum; import com.alipay.sofa.registry.server.data.executor.ExecutorFactory; import com.alipay.sofa.registry.server.data.remoting.sessionserver.SessionServerConnectionFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Executor; /** * @@ -73,7 +72,7 @@ public Set getSuitableSource() { @Override public void notify(Datum datum, Long lastVersion) { DataPushRequest request = new DataPushRequest(datum); - List connections = sessionServerConnectionFactory.getConnections(); + List connections = sessionServerConnectionFactory.getSessionConnections(); for (Connection connection : connections) { doNotify(new NotifyPushDataCallback(connection, request)); } @@ -97,6 +96,11 @@ public void onCallback(Channel channel, Object message) { public void onException(Channel channel, Throwable exception) { notifyPushdataCallback.onException(exception); } + + @Override + public Executor getExecutor() { + return notifyPushdataCallback.getExecutor(); + } }, dataServerConfig.getRpcTimeout()); } catch (Exception e) { LOGGER.error("[TempPublisherNotifier] notify sessionserver {} failed, {}", diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/executor/ExecutorFactory.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/executor/ExecutorFactory.java index 233e6013c..af08f25b1 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/executor/ExecutorFactory.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/executor/ExecutorFactory.java @@ -16,10 +16,6 @@ */ package com.alipay.sofa.registry.server.data.executor; -import com.alipay.sofa.registry.log.Logger; -import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.util.NamedThreadFactory; - import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; @@ -29,6 +25,10 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.util.NamedThreadFactory; + /** * the factory to create executor * @@ -38,6 +38,7 @@ public class ExecutorFactory { public static final ThreadPoolExecutor EXECUTOR; + public static final ThreadPoolExecutor NOTIFY_SESSION_CALLBACK_EXECUTOR; private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorFactory.class); static { @@ -57,6 +58,10 @@ protected void afterExecute(Runnable r, Throwable t) { } } }; + + NOTIFY_SESSION_CALLBACK_EXECUTOR = new ThreadPoolExecutor(10, 20, 300, TimeUnit.SECONDS, + new LinkedBlockingQueue<>(100000), new NamedThreadFactory( + "NotifySessionCallback-executor", true)); } /** diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/DataNodeExchanger.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/DataNodeExchanger.java index 69a4dc7e4..8d7555000 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/DataNodeExchanger.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/DataNodeExchanger.java @@ -16,12 +16,6 @@ */ package com.alipay.sofa.registry.server.data.remoting; -import java.util.Collection; - -import javax.annotation.Resource; - -import org.springframework.beans.factory.annotation.Autowired; - import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; @@ -34,6 +28,10 @@ import com.alipay.sofa.registry.remoting.exchange.message.Response; import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; import com.alipay.sofa.registry.server.data.remoting.handler.AbstractClientHandler; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.Resource; +import java.util.Collection; /** * @author xuanbei @@ -54,18 +52,17 @@ public class DataNodeExchanger implements NodeExchanger { @Override public Response request(Request request) { - Channel channel = this.connect(request.getRequestUrl()); Client client = boltExchange.getClient(Exchange.DATA_SERVER_TYPE); LOGGER.info("DataNode Exchanger request={},url={},callbackHandler={}", request.getRequestBody(), request.getRequestUrl(), request.getCallBackHandler()); if (null != request.getCallBackHandler()) { - client.sendCallback(channel, request.getRequestBody(), + client.sendCallback(request.getRequestUrl(), request.getRequestBody(), request.getCallBackHandler(), dataServerConfig.getRpcTimeout()); return () -> Response.ResultStatus.SUCCESSFUL; } else { - final Object result = client.sendSync(channel, request.getRequestBody(), + final Object result = client.sendSync(request.getRequestUrl(), request.getRequestBody(), dataServerConfig.getRpcTimeout()); return () -> result; } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/MetaNodeExchanger.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/MetaNodeExchanger.java index c79b8dabf..faca923de 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/MetaNodeExchanger.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/MetaNodeExchanger.java @@ -16,12 +16,6 @@ */ package com.alipay.sofa.registry.server.data.remoting; -import java.util.Collection; - -import javax.annotation.Resource; - -import org.springframework.beans.factory.annotation.Autowired; - import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; @@ -35,6 +29,10 @@ import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; import com.alipay.sofa.registry.server.data.remoting.handler.AbstractClientHandler; import com.alipay.sofa.registry.server.data.remoting.metaserver.IMetaServerService; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.Resource; +import java.util.Collection; /** * @author xuanbei @@ -58,26 +56,21 @@ public class MetaNodeExchanger implements NodeExchanger { @Override public Response request(Request request) { - Channel channel = connect(request.getRequestUrl()); Client client = boltExchange.getClient(Exchange.META_SERVER_TYPE); LOGGER.info("MetaNode Exchanger request={},url={},callbackHandler={}", request.getRequestBody(), request.getRequestUrl(), request.getCallBackHandler()); try { - final Object result = client.sendSync(channel, request.getRequestBody(), + final Object result = client.sendSync(request.getRequestUrl(), request.getRequestBody(), dataServerConfig.getRpcTimeout()); return () -> result; } catch (Exception e) { //retry URL url = new URL(metaServerService.refreshLeader().getIp(), dataServerConfig.getMetaServerPort()); - channel = client.getChannel(url); - if (channel == null) { - channel = client.connect(url); - } LOGGER.warn("MetaNode Exchanger request send error!It will be retry once!Request url:{}", url); - final Object result = client.sendSync(channel, request.getRequestBody(), + final Object result = client.sendSync(url, request.getRequestBody(), dataServerConfig.getRpcTimeout()); return () -> result; } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/DataServerNodeFactory.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/DataServerNodeFactory.java index f13e88355..fb511a683 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/DataServerNodeFactory.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/DataServerNodeFactory.java @@ -16,12 +16,6 @@ */ package com.alipay.sofa.registry.server.data.remoting.dataserver; -import com.alipay.remoting.Connection; -import com.alipay.sofa.registry.consistency.hash.ConsistentHash; -import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; -import com.alipay.sofa.registry.server.data.node.DataServerNode; -import com.google.common.collect.Lists; - import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -30,6 +24,12 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import com.alipay.remoting.Connection; +import com.alipay.sofa.registry.consistency.hash.ConsistentHash; +import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; +import com.alipay.sofa.registry.server.data.node.DataServerNode; +import com.google.common.collect.Lists; + /** * the factory to hold other dataservers and connection connected to them * @@ -194,7 +194,7 @@ public static void remove(String dataCenter) { public static DataServerNode computeDataServerNode(String dataCenter, String dataInfoId) { ConsistentHash consistentHash = CONSISTENT_HASH_MAP.get(dataCenter); if (consistentHash != null) { - return CONSISTENT_HASH_MAP.get(dataCenter).getNodeFor(dataInfoId); + return consistentHash.getNodeFor(dataInfoId); } return null; } @@ -203,7 +203,7 @@ public static List computeDataServerNodes(String dataCenter, Str int backupNodes) { ConsistentHash consistentHash = CONSISTENT_HASH_MAP.get(dataCenter); if (consistentHash != null) { - return CONSISTENT_HASH_MAP.get(dataCenter).getNUniqueNodesFor(dataInfoId, backupNodes); + return consistentHash.getNUniqueNodesFor(dataInfoId, backupNodes); } return null; } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/GetSyncDataHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/GetSyncDataHandler.java index c315110ee..62456108e 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/GetSyncDataHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/GetSyncDataHandler.java @@ -25,6 +25,8 @@ import com.alipay.sofa.registry.server.data.remoting.DataNodeExchanger; import org.springframework.beans.factory.annotation.Autowired; +import java.util.concurrent.Executor; + /** * * @author qian.lqlq @@ -71,6 +73,11 @@ public void onCallback(Channel channel, Object message) { public void onException(Channel channel, Throwable exception) { callback.onException(exception); } + + @Override + public Executor getExecutor() { + return callback.getExecutor(); + } }; } }); diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/SyncDataCallback.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/SyncDataCallback.java index a5785f314..5f673218c 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/SyncDataCallback.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/SyncDataCallback.java @@ -16,24 +16,23 @@ */ package com.alipay.sofa.registry.server.data.remoting.dataserver; +import java.util.Collection; +import java.util.concurrent.Executor; + +import org.springframework.util.CollectionUtils; + import com.alipay.remoting.Connection; import com.alipay.remoting.InvokeCallback; import com.alipay.sofa.registry.common.model.GenericResponse; import com.alipay.sofa.registry.common.model.dataserver.Datum; import com.alipay.sofa.registry.common.model.dataserver.SyncData; import com.alipay.sofa.registry.common.model.dataserver.SyncDataRequest; -import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.server.data.change.DataChangeTypeEnum; import com.alipay.sofa.registry.server.data.change.DataSourceTypeEnum; import com.alipay.sofa.registry.server.data.change.event.DataChangeEventCenter; import com.alipay.sofa.registry.server.data.executor.ExecutorFactory; -import org.springframework.util.CollectionUtils; - -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.Executor; /** * @@ -44,7 +43,7 @@ public class SyncDataCallback implements InvokeCallback { private static final Logger LOGGER = LoggerFactory.getLogger(SyncDataCallback.class); - private static final Executor EXECUTOR = ExecutorFactory.newFixedThreadPool(20, + private static final Executor EXECUTOR = ExecutorFactory.newFixedThreadPool(5, SyncDataCallback.class.getSimpleName()); private static final int RETRY_COUNT = 3; @@ -96,7 +95,7 @@ public void onResponse(Object obj) { datum.setDataInfoId(syncData.getDataInfoId()); datum.setDataCenter(syncData.getDataCenter()); } - processDatum(datum); + Datum.internDatum(datum); dataChangeEventCenter.sync(DataChangeTypeEnum.COVER, dataSourceTypeEnum, datum); break; } @@ -105,7 +104,7 @@ public void onResponse(Object obj) { if (!CollectionUtils.isEmpty(datums)) { for (Datum datum : datums) { if (datum != null) { - processDatum(datum); + Datum.internDatum(datum); dataChangeEventCenter.sync(DataChangeTypeEnum.MERGE, dataSourceTypeEnum, datum); } @@ -117,16 +116,6 @@ public void onResponse(Object obj) { } } - private void processDatum(Datum datum) { - if (datum != null) { - Map publisherMap = datum.getPubMap(); - - if (publisherMap != null && !publisherMap.isEmpty()) { - publisherMap.forEach((registerId, publisher) -> Publisher.processPublisher(publisher)); - } - } - } - @Override public void onException(Throwable e) { GenericResponse genericResponse = new GenericResponse(); diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/handler/NotifyFetchDatumHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/handler/NotifyFetchDatumHandler.java index 828452533..068b57e35 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/handler/NotifyFetchDatumHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/dataserver/handler/NotifyFetchDatumHandler.java @@ -16,6 +16,11 @@ */ package com.alipay.sofa.registry.server.data.remoting.dataserver.handler; +import java.util.Map; +import java.util.Map.Entry; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.remoting.Connection; import com.alipay.sofa.registry.common.model.CommonResponse; import com.alipay.sofa.registry.common.model.GenericResponse; @@ -23,7 +28,6 @@ import com.alipay.sofa.registry.common.model.dataserver.Datum; import com.alipay.sofa.registry.common.model.dataserver.GetDataRequest; import com.alipay.sofa.registry.common.model.dataserver.NotifyFetchDatumRequest; -import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; @@ -41,10 +45,6 @@ import com.alipay.sofa.registry.server.data.renew.LocalDataServerCleanHandler; import com.alipay.sofa.registry.server.data.util.TimeUtil; import com.alipay.sofa.registry.util.ParaCheckUtil; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.Map; -import java.util.Map.Entry; /** * @@ -157,8 +157,11 @@ private void fetchDatum(String targetIp, String dataCenter, String dataInfoId) { dataServerConfig.getRpcTimeout()); if (response.isSuccess()) { Datum datum = response.getData().get(dataCenter); + if (datum != null) { - processDatum(datum); + // wrap by WordCache + datum = Datum.internDatum(datum); + dataChangeEventCenter.sync(DataChangeTypeEnum.COVER, DataSourceTypeEnum.BACKUP, datum); LOGGER @@ -177,16 +180,6 @@ private void fetchDatum(String targetIp, String dataCenter, String dataInfoId) { } } - private void processDatum(Datum datum) { - if (datum != null) { - Map publisherMap = datum.getPubMap(); - - if (publisherMap != null && !publisherMap.isEmpty()) { - publisherMap.forEach((registerId, publisher) -> Publisher.processPublisher(publisher)); - } - } - } - @Override public CommonResponse buildFailedResponse(String msg) { return CommonResponse.buildFailedResponse(msg); diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/SessionServerConnectionFactory.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/SessionServerConnectionFactory.java index 0ee31edf1..7c8a76901 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/SessionServerConnectionFactory.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/SessionServerConnectionFactory.java @@ -16,13 +16,14 @@ */ package com.alipay.sofa.registry.server.data.remoting.sessionserver; -import java.util.Collections; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; +import java.util.concurrent.atomic.AtomicInteger; import org.springframework.beans.factory.annotation.Autowired; @@ -34,35 +35,36 @@ import com.alipay.sofa.registry.server.data.remoting.sessionserver.disconnect.SessionServerDisconnectEvent; /** - * the factory to hold sesseionserver connections + * the factory to hold SessionServer connections * * @author qian.lqlq + * @author kezhu.wukz * @version $Id: SessionServerConnectionFactory.java, v 0.1 2017-12-06 15:48 qian.lqlq Exp $ */ public class SessionServerConnectionFactory { - private static final Logger LOGGER = LoggerFactory - .getLogger(SessionServerConnectionFactory.class); + private static final Logger LOGGER = LoggerFactory + .getLogger(SessionServerConnectionFactory.class); - private static final int DELAY = 30 * 1000; + private static final int DELAY = 30 * 1000; + private static final Map EMPTY_MAP = new HashMap(0); /** - * collection of connections - * key : processId - * value : connection + * key : SessionServer address + * value: SessionServer processId */ - private final Map MAP = new ConcurrentHashMap<>(); + private final Map SESSION_CONN_PROCESS_ID_MAP = new ConcurrentHashMap<>(); /** - * key : sessionserver host - * value: sesseionserver processId + * key : SessionServer processId + * value: ip:port of clients */ - private final Map PROCESS_ID_MAP = new ConcurrentHashMap<>(); + private final Map> PROCESS_ID_CONNECT_ID_MAP = new ConcurrentHashMap<>(); /** - * key : sessionserver processId - * value: ip:port of clients + * key : SessionServer processId + * value: pair(SessionServer address, SessionServer connection) */ - private final Map> PROCESS_ID_CONNECT_ID_MAP = new ConcurrentHashMap<>(); + private final Map PROCESS_ID_SESSION_CONN_MAP = new ConcurrentHashMap<>(); @Autowired private DisconnectEventHandler disconnectEventHandler; @@ -74,18 +76,22 @@ public class SessionServerConnectionFactory { * @param connectIds * @param connection */ - public void register(String processId, Set connectIds, Connection connection) { - String serverHost = NetUtil.toAddressString(connection.getRemoteAddress()); - if (LOGGER.isInfoEnabled()) { - LOGGER.info("session({}, processId={}) registered", serverHost, processId); - } - MAP.put(processId, new Pair(serverHost, connection)); - Set ret = PROCESS_ID_CONNECT_ID_MAP.getOrDefault(processId, null); - if (ret == null) { - PROCESS_ID_CONNECT_ID_MAP.putIfAbsent(processId, new HashSet<>()); + public void registerSession(String processId, Set connectIds, Connection connection) { + if (!connection.isFine()) { + return; } - PROCESS_ID_CONNECT_ID_MAP.get(processId).addAll(connectIds); - PROCESS_ID_MAP.put(serverHost, processId); + String sessionConnAddress = NetUtil.toAddressString(connection.getRemoteAddress()); + LOGGER.info("session({}, processId={}) registered", sessionConnAddress, processId); + + SESSION_CONN_PROCESS_ID_MAP.put(sessionConnAddress, processId); + + Set connectIdSet = PROCESS_ID_CONNECT_ID_MAP + .computeIfAbsent(processId, k -> ConcurrentHashMap.newKeySet()); + connectIdSet.addAll(connectIds); + + Pair pair = PROCESS_ID_SESSION_CONN_MAP.computeIfAbsent(processId, k -> new Pair(new ConcurrentHashMap<>())); + pair.getConnections().put(sessionConnAddress, connection); + } /** @@ -94,24 +100,33 @@ public void register(String processId, Set connectIds, Connection connec * @param connectId */ public void registerConnectId(String processId, String connectId) { - Set ret = PROCESS_ID_CONNECT_ID_MAP.getOrDefault(processId, null); - if (ret == null) { - PROCESS_ID_CONNECT_ID_MAP.putIfAbsent(processId, new HashSet<>()); - } - PROCESS_ID_CONNECT_ID_MAP.get(processId).add(connectId); + Set connectIdSet = PROCESS_ID_CONNECT_ID_MAP + .computeIfAbsent(processId, k -> ConcurrentHashMap.newKeySet()); + connectIdSet.add(connectId); } /** - * remove connection by specific host + * session disconnected, The SessionServerDisconnectEvent is triggered only when the last connections is removed */ - public void removeProcess(String sessionServerHost) { - String processId = PROCESS_ID_MAP.remove(sessionServerHost); + public void sessionDisconnected(String sessionConnAddress) { + String processId = SESSION_CONN_PROCESS_ID_MAP.remove(sessionConnAddress); if (LOGGER.isInfoEnabled()) { - LOGGER.info("session({}, processId={}) unregistered", sessionServerHost, processId); + LOGGER.info("session({}, processId={}) unregistered", sessionConnAddress, processId); } if (processId != null) { - disconnectEventHandler.receive(new SessionServerDisconnectEvent(processId, - sessionServerHost, DELAY)); + Pair pair = PROCESS_ID_SESSION_CONN_MAP.get(processId); + + // remove connection + if (pair != null) { + pair.getConnections().remove(sessionConnAddress); + pair.lastDisconnectedSession = sessionConnAddress; + } + + // The SessionServerDisconnectEvent is triggered only when the last connection is broken + if (pair == null || pair.getConnections().isEmpty()) { + disconnectEventHandler.receive(new SessionServerDisconnectEvent(processId, + sessionConnAddress, DELAY)); + } } } @@ -124,63 +139,62 @@ public Set removeConnectIds(String processId) { } /** - * - * @param processId - * @return + * If the number of connections is 0, and lastDisconnectedSession matched, he ProcessId can be deleted */ - public boolean removeProcessIfMatch(String processId, String sessionServerHost) { - return MAP.remove(processId, new Pair(sessionServerHost, null)); + public boolean removeProcessIfMatch(String processId, String sessionConnAddress) { + Pair emptyPair = new Pair(EMPTY_MAP); + emptyPair.lastDisconnectedSession = sessionConnAddress; + return PROCESS_ID_SESSION_CONN_MAP.remove(processId, emptyPair); } /** - * get all connections - * - * @return + * get connections of SessionServer ( Randomly select a connection for each session ) */ - public List getConnections() { - return MAP.size() <= 0 ? - Collections.EMPTY_LIST : - MAP.values().stream().map(Pair::getConnection).collect(Collectors.toList()); + public List getSessionConnections() { + List list = new ArrayList<>(PROCESS_ID_SESSION_CONN_MAP.size()); + Collection pairs = PROCESS_ID_SESSION_CONN_MAP.values(); + if (pairs != null) { + for (Pair pair : pairs) { + Object[] conns = pair.getConnections().values().toArray(); + if (conns.length > 0) { + int n = pair.roundRobin.incrementAndGet(); + if (n < 0) { + pair.roundRobin.compareAndSet(n, 0); + n = (n == Integer.MIN_VALUE) ? 0 : Math.abs(n); + } + n = n % conns.length; + list.add((Connection) conns[n]); + } + } + } + return list; } /** - * convenient class to store sessionServerHost and connection + * convenient class to store sessionConnAddress and connection */ private static class Pair { - private String sessionServerHost; - private Connection connection; + private AtomicInteger roundRobin = new AtomicInteger(-1); + private Map connections; + private String lastDisconnectedSession; - private Pair(String sessionServerHost, Connection connection) { - this.sessionServerHost = sessionServerHost; - this.connection = connection; + private Pair(Map connections) { + this.connections = connections; } @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Pair pair = (Pair) o; - - return sessionServerHost.equals(pair.sessionServerHost); - } - - @Override - public int hashCode() { - return sessionServerHost.hashCode(); + return connections.equals(((Pair) o).getConnections()) + && (((Pair) o).lastDisconnectedSession.equals(lastDisconnectedSession)); } /** - * Getter method for property connection. + * Getter method for property connections. * - * @return property value of connection + * @return property value of connections */ - private Connection getConnection() { - return connection; + private Map getConnections() { + return connections; } } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/disconnect/DisconnectEventHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/disconnect/DisconnectEventHandler.java index d386f21f0..43c49622d 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/disconnect/DisconnectEventHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/disconnect/DisconnectEventHandler.java @@ -16,16 +16,6 @@ */ package com.alipay.sofa.registry.server.data.remoting.sessionserver.disconnect; -import java.util.Set; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.DelayQueue; -import java.util.concurrent.Executor; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; - -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; - import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.server.data.bootstrap.DataServerConfig; @@ -36,6 +26,15 @@ import com.alipay.sofa.registry.server.data.node.DataNodeStatus; import com.alipay.sofa.registry.server.data.remoting.sessionserver.SessionServerConnectionFactory; import com.alipay.sofa.registry.server.data.util.LocalServerStatusEnum; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; /** * @author qian.lqlq @@ -134,7 +133,7 @@ public void afterPropertiesSet() { //check processId confirm remove,and not be registered again when delay time String sessionServerHost = event.getSessionServerHost(); if (sessionServerConnectionFactory - .removeProcessIfMatch(processId, sessionServerHost)) { + .removeProcessIfMatch(processId,sessionServerHost)) { Set connectIds = sessionServerConnectionFactory .removeConnectIds(processId); diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DataServerConnectionHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DataServerConnectionHandler.java index 2340fa18d..02316aee8 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DataServerConnectionHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DataServerConnectionHandler.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.data.remoting.sessionserver.handler; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.Node; import com.alipay.sofa.registry.common.model.Node.NodeType; import com.alipay.sofa.registry.net.NetUtil; @@ -24,7 +26,6 @@ import com.alipay.sofa.registry.remoting.RemotingException; import com.alipay.sofa.registry.server.data.remoting.handler.AbstractServerHandler; import com.alipay.sofa.registry.server.data.remoting.sessionserver.SessionServerConnectionFactory; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -51,7 +52,7 @@ public void connected(Channel channel) throws RemotingException { @Override public void disconnected(Channel channel) throws RemotingException { super.disconnected(channel); - sessionServerConnectionFactory.removeProcess(NetUtil.toAddressString(channel + sessionServerConnectionFactory.sessionDisconnected(NetUtil.toAddressString(channel .getRemoteAddress())); } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DatumSnapshotHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DatumSnapshotHandler.java index 3c43fc486..6b281aff1 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DatumSnapshotHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/DatumSnapshotHandler.java @@ -18,12 +18,12 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; @@ -33,6 +33,7 @@ import com.alipay.sofa.registry.common.model.PublisherDigestUtil; import com.alipay.sofa.registry.common.model.constants.ValueConstants; import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.WordCache; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; @@ -85,16 +86,26 @@ public void checkParam(DatumSnapshotRequest request) throws RuntimeException { public Object doHandle(Channel channel, DatumSnapshotRequest request) { RENEW_LOGGER.info("Received datumSnapshotRequest: {}", request); - Map pubMap = request.getPublishers().stream() - .collect(Collectors.toMap(p -> p.getRegisterId(), p -> p)); + String connectId = WordCache.getInstance().getWordCache(request.getConnectId()); + + // convert to pubMap, and wrap it by WordCache + Map pubMap = new HashMap<>(); + List publishers = request.getPublishers(); + if (publishers != null) { + for (Publisher publisher : publishers) { + Publisher.internPublisher(publisher); + pubMap.put(publisher.getRegisterId(), publisher); + } + } // diff the cache and snapshot boolean isDiff = true; - Map cachePubMap = datumCache.getOwnByConnectId(request.getConnectId()); + Map cachePubMap = datumCache.getOwnByConnectId(connectId); if (cachePubMap == null) { RENEW_LOGGER - .info(">>>>>>> connectId={}, cachePubMap.size=0, pubMap.size={}, isDiff={}, the diff is: pubMap={}", - request.getConnectId(), pubMap.size(), isDiff, limitedToString(pubMap.values())); + .info( + ">>>>>>> connectId={}, cachePubMap.size=0, pubMap.size={}, isDiff={}, the diff is: pubMap={}", + connectId, pubMap.size(), isDiff, limitedToString(pubMap.values())); } else { List diffPub1 = subtract(pubMap, cachePubMap); List diffPub2 = subtract(cachePubMap, pubMap); @@ -102,18 +113,19 @@ public Object doHandle(Channel channel, DatumSnapshotRequest request) { isDiff = false; } RENEW_LOGGER - .info(">>>>>>> connectId={}, cachePubMap.size={}, pubMap.size={}, isDiff={}, the diff is: pubMap-cachePubMap=(size:{}){}, cachePubMap-pubMap=(size:{}){}", - request.getConnectId(), cachePubMap.size(), pubMap.size(), isDiff, diffPub1.size(), - limitedToString(diffPub1), diffPub2.size(), limitedToString(diffPub2)); + .info( + ">>>>>>> connectId={}, cachePubMap.size={}, pubMap.size={}, isDiff={}, the diff is: pubMap-cachePubMap=(size:{}){}, cachePubMap-pubMap=(size:{}){}", + connectId, cachePubMap.size(), pubMap.size(), isDiff, diffPub1.size(), + limitedToString(diffPub1), diffPub2.size(), limitedToString(diffPub2)); } if (isDiff) { // build DatumSnapshotEvent and send to eventCenter - dataChangeEventCenter.onChange(new DatumSnapshotEvent(request.getConnectId(), cachePubMap, pubMap)); + dataChangeEventCenter.onChange(new DatumSnapshotEvent(connectId, cachePubMap, pubMap)); } // record the renew timestamp - datumLeaseManager.renew(request.getConnectId()); + datumLeaseManager.renew(connectId); return CommonResponse.buildSuccessResponse(); } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/GetDataVersionsHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/GetDataVersionsHandler.java index 268425b14..756280f63 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/GetDataVersionsHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/GetDataVersionsHandler.java @@ -74,10 +74,12 @@ public Object doHandle(Channel channel, GetDataVersionRequest request) { String dataCenter = entry.getKey(); Datum datum = entry.getValue(); if (datum != null) { - if (!map.containsKey(dataCenter)) { - map.put(dataCenter, new HashMap<>()); + Map dataInfoIdToVersionMap = map.get(dataCenter); + if (dataInfoIdToVersionMap == null) { + dataInfoIdToVersionMap = new HashMap<>(dataInfoIds.size()); + map.put(dataCenter, dataInfoIdToVersionMap); } - map.get(dataCenter).put(dataInfoId, datum.getVersion()); + dataInfoIdToVersionMap.put(dataInfoId, datum.getVersion()); } } } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/PublishDataHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/PublishDataHandler.java index ffbbaab46..4412bcbcc 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/PublishDataHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/PublishDataHandler.java @@ -16,16 +16,12 @@ */ package com.alipay.sofa.registry.server.data.remoting.sessionserver.handler; -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; - -import org.springframework.beans.factory.annotation.Autowired; - import com.alipay.sofa.registry.common.model.CommonResponse; import com.alipay.sofa.registry.common.model.Node; import com.alipay.sofa.registry.common.model.PublishType; import com.alipay.sofa.registry.common.model.dataserver.PublishDataRequest; import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.WordCache; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; @@ -36,6 +32,12 @@ import com.alipay.sofa.registry.server.data.remoting.sessionserver.forward.ForwardService; import com.alipay.sofa.registry.server.data.renew.DatumLeaseManager; import com.alipay.sofa.registry.util.ParaCheckUtil; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * processor to publish data @@ -85,7 +87,7 @@ public void checkParam(PublishDataRequest request) throws RuntimeException { @Override public Object doHandle(Channel channel, PublishDataRequest request) { - Publisher publisher = Publisher.processPublisher(request.getPublisher()); + Publisher publisher = Publisher.internPublisher(request.getPublisher()); if (forwardService.needForward()) { LOGGER.warn("[forward] Publish request refused, request: {}", request); CommonResponse response = new CommonResponse(); @@ -97,7 +99,8 @@ public Object doHandle(Channel channel, PublishDataRequest request) { dataChangeEventCenter.onChange(publisher, dataServerConfig.getLocalDataCenter()); if (publisher.getPublishType() != PublishType.TEMPORARY) { - String connectId = publisher.getSourceAddress().getAddressString(); + String connectId = WordCache.getInstance().getWordCache( + publisher.getSourceAddress().getAddressString()); sessionServerConnectionFactory.registerConnectId(request.getSessionServerProcessId(), connectId); // record the renew timestamp diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/RenewDatumHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/RenewDatumHandler.java index 3004b3bdd..8462f244b 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/RenewDatumHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/RenewDatumHandler.java @@ -32,6 +32,7 @@ import com.alipay.sofa.registry.common.model.RenewDatumRequest; import com.alipay.sofa.registry.common.model.constants.ValueConstants; import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.WordCache; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; @@ -94,8 +95,7 @@ public Object doHandle(Channel channel, RenewDatumRequest request) { } if (!renewEnabled.get()) { - LOGGER.warn("[forward] Renew request refused, renewEnabled is false, request: {}", - request); + LOGGER.warn("Renew request refused, renewEnabled is false, request: {}", request); GenericResponse response = new GenericResponse(); response.setSuccess(false); response.setMessage("Renew request refused, renewEnabled is false yet"); @@ -132,7 +132,7 @@ protected Node.NodeType getConnectNodeType() { * 2. Compare checksum: Get all pubs corresponding to the connId from datumCache and calculate checksum. */ private boolean renewDatum(RenewDatumRequest request) { - String connectId = request.getConnectId(); + String connectId = WordCache.getInstance().getWordCache(request.getConnectId()); String renewDigest = request.getDigestSum(); // Get all pubs corresponding to the connectId from datumCache diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/SessionServerRegisterHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/SessionServerRegisterHandler.java index d4ff45166..166a34ce4 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/SessionServerRegisterHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/SessionServerRegisterHandler.java @@ -16,11 +16,6 @@ */ package com.alipay.sofa.registry.server.data.remoting.sessionserver.handler; -import java.util.HashSet; -import java.util.Set; - -import org.springframework.beans.factory.annotation.Autowired; - import com.alipay.sofa.registry.common.model.CommonResponse; import com.alipay.sofa.registry.common.model.Node; import com.alipay.sofa.registry.common.model.dataserver.SessionServerRegisterRequest; @@ -29,6 +24,10 @@ import com.alipay.sofa.registry.server.data.remoting.handler.AbstractServerHandler; import com.alipay.sofa.registry.server.data.remoting.sessionserver.SessionServerConnectionFactory; import com.alipay.sofa.registry.util.ParaCheckUtil; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.HashSet; +import java.util.Set; /** * @@ -52,7 +51,7 @@ public Object doHandle(Channel channel, SessionServerRegisterRequest request) { if (connectIds == null) { connectIds = new HashSet<>(); } - sessionServerConnectionFactory.register(request.getProcessId(), connectIds, + sessionServerConnectionFactory.registerSession(request.getProcessId(), connectIds, ((BoltChannel) channel).getConnection()); return CommonResponse.buildSuccessResponse(); } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/UnPublishDataHandler.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/UnPublishDataHandler.java index 42e335664..1a44a0850 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/UnPublishDataHandler.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/remoting/sessionserver/handler/UnPublishDataHandler.java @@ -27,6 +27,8 @@ import com.alipay.sofa.registry.common.model.dataserver.Datum; import com.alipay.sofa.registry.common.model.dataserver.UnPublishDataRequest; import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.WordCache; +import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; @@ -94,7 +96,7 @@ public Object doHandle(Channel channel, UnPublishDataRequest request) { .getRegisterTimestamp()), dataServerConfig.getLocalDataCenter()); // Attempt to get connectId from datumCache (Datum may not exist), and record the renew timestamp - String connectId = getConnectId(request); + String connectId = WordCache.getInstance().getWordCache(getConnectId(request)); if (connectId != null) { datumLeaseManager.renew(connectId); } diff --git a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/resource/DataDigestResource.java b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/resource/DataDigestResource.java index de514156e..6671dad81 100644 --- a/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/resource/DataDigestResource.java +++ b/server/server/data/src/main/java/com/alipay/sofa/registry/server/data/resource/DataDigestResource.java @@ -175,7 +175,7 @@ public Map> getServerListAll(@PathParam("type") String type } public List getSessionServerList() { - List connections = sessionServerConnectionFactory.getConnections().stream() + List connections = sessionServerConnectionFactory.getSessionConnections().stream() .filter(connection -> connection != null && connection.isFine()) .map(connection -> connection.getRemoteIP() + ":" + connection.getRemotePort()) .collect(Collectors.toList()); diff --git a/server/server/integration/pom.xml b/server/server/integration/pom.xml index 870f4b48a..378afa2f8 100644 --- a/server/server/integration/pom.xml +++ b/server/server/integration/pom.xml @@ -63,4 +63,4 @@ - + \ No newline at end of file diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfig.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfig.java index d4b09b38b..cc7694cf4 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfig.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfig.java @@ -80,6 +80,8 @@ public interface MetaServerConfig { boolean isEnableMetrics(); + int getRockDBCacheSize(); + /** * decision mode enum */ diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfigBean.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfigBean.java index 68864e817..1685c94e5 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfigBean.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/bootstrap/MetaServerConfigBean.java @@ -94,6 +94,8 @@ public class MetaServerConfigBean implements MetaServerConfig { + File.separator + "raftData"; + private int rockDBCacheSize = 64; //64M + @Override public int getSessionServerPort() { return sessionServerPort; @@ -603,6 +605,24 @@ public void setEnableMetrics(boolean enableMetrics) { this.enableMetrics = enableMetrics; } + /** + * Getter method for property RockDBCacheSize. + * + * @return property value of RockDBCacheSize + */ + public int getRockDBCacheSize() { + return rockDBCacheSize; + } + + /** + * Setter method for property RockDBCacheSize. + * + * @param rockDBCacheSize value to be assigned to property RockDBCacheSize + */ + public void setRockDBCacheSize(int rockDBCacheSize) { + this.rockDBCacheSize = rockDBCacheSize; + } + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/DataNodeChangePushTaskListener.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/DataNodeChangePushTaskListener.java index a146d7721..de336874f 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/DataNodeChangePushTaskListener.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/DataNodeChangePushTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.meta.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.Node.NodeType; import com.alipay.sofa.registry.server.meta.bootstrap.MetaServerConfig; import com.alipay.sofa.registry.server.meta.task.Constant; @@ -27,7 +29,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -57,8 +58,8 @@ public DataNodeChangePushTaskListener(TaskProcessor dataNodeSingleTaskProcessor) } @Override - public boolean support(TaskEvent event) { - return TaskType.DATA_NODE_CHANGE_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.DATA_NODE_CHANGE_PUSH_TASK; } @Override diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/PersistenceDataChangeNotifyTaskListener.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/PersistenceDataChangeNotifyTaskListener.java index f2e5714ed..b72725e4f 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/PersistenceDataChangeNotifyTaskListener.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/PersistenceDataChangeNotifyTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.meta.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.meta.bootstrap.MetaServerConfig; import com.alipay.sofa.registry.server.meta.task.MetaServerTask; import com.alipay.sofa.registry.server.meta.task.PersistenceDataChangeNotifyTask; @@ -25,7 +27,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -49,8 +50,8 @@ public PersistenceDataChangeNotifyTaskListener(TaskProcessor sessionNodeSingleTa } @Override - public boolean support(TaskEvent event) { - return TaskType.PERSISTENCE_DATA_CHANGE_NOTIFY_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.PERSISTENCE_DATA_CHANGE_NOTIFY_TASK; } @Override diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/ReceiveStatusConfirmNotifyTaskListener.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/ReceiveStatusConfirmNotifyTaskListener.java index 66b8dbe6b..d910d247f 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/ReceiveStatusConfirmNotifyTaskListener.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/ReceiveStatusConfirmNotifyTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.meta.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.meta.bootstrap.MetaServerConfig; import com.alipay.sofa.registry.server.meta.node.DataNodeService; import com.alipay.sofa.registry.server.meta.node.NodeService; @@ -27,7 +29,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -53,8 +54,8 @@ public ReceiveStatusConfirmNotifyTaskListener(TaskProcessor dataNodeSingleTaskPr } @Override - public boolean support(TaskEvent event) { - return TaskType.RECEIVE_STATUS_CONFIRM_NOTIFY_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.RECEIVE_STATUS_CONFIRM_NOTIFY_TASK; } @Override diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/SessionNodeChangePushTaskListener.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/SessionNodeChangePushTaskListener.java index 24dd13e3a..fb7edd296 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/SessionNodeChangePushTaskListener.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/listener/SessionNodeChangePushTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.meta.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.meta.bootstrap.MetaServerConfig; import com.alipay.sofa.registry.server.meta.task.MetaServerTask; import com.alipay.sofa.registry.server.meta.task.SessionNodeChangePushTask; @@ -25,7 +27,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -49,8 +50,8 @@ public SessionNodeChangePushTaskListener(TaskProcessor sessionNodeSingleTaskProc } @Override - public boolean support(TaskEvent event) { - return TaskType.SESSION_NODE_CHANGE_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.SESSION_NODE_CHANGE_PUSH_TASK; } @Override diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/MetaClientExchanger.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/MetaClientExchanger.java index 4c48f713e..f326ec153 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/MetaClientExchanger.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/MetaClientExchanger.java @@ -16,13 +16,22 @@ */ package com.alipay.sofa.registry.server.meta.remoting; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.Node.NodeType; import com.alipay.sofa.registry.common.model.metaserver.MetaNode; import com.alipay.sofa.registry.common.model.metaserver.NodeChangeResult; import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.remoting.Channel; import com.alipay.sofa.registry.remoting.ChannelHandler; import com.alipay.sofa.registry.remoting.Client; import com.alipay.sofa.registry.remoting.exchange.Exchange; @@ -34,15 +43,6 @@ import com.alipay.sofa.registry.server.meta.bootstrap.NodeConfig; import com.alipay.sofa.registry.server.meta.bootstrap.ServiceFactory; import com.alipay.sofa.registry.server.meta.store.StoreService; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; /** * @@ -82,13 +82,7 @@ public Response request(Request request) throws RequestException { metaClient = boltExchange.connect(Exchange.META_SERVER_TYPE, url, new ChannelHandler[0]); } - Channel channel = metaClient.getChannel(url); - if (channel == null) { - LOGGER.warn("MetaClient Exchanger get channel {} error or disconnected!", url); - channel = metaClient.connect(url); - } - - final Object result = metaClient.sendSync(channel, request.getRequestBody(), + final Object result = metaClient.sendSync(url, request.getRequestBody(), metaServerConfig.getMetaNodeExchangeTimeout()); response = () -> result; } catch (Exception e) { diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/RaftExchanger.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/RaftExchanger.java index f64c15ced..d044558fd 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/RaftExchanger.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/remoting/RaftExchanger.java @@ -141,6 +141,9 @@ public void stopProcess(PeerId leader) { RaftServerConfig raftServerConfig = new RaftServerConfig(); raftServerConfig.setMetricsLogger(METRICS_LOGGER); raftServerConfig.setEnableMetrics(metaServerConfig.isEnableMetrics()); + if (metaServerConfig.getRockDBCacheSize() > 0) { + raftServerConfig.setRockDBCacheSize(metaServerConfig.getRockDBCacheSize()); + } raftServer.start(raftServerConfig); } @@ -384,4 +387,13 @@ public AtomicBoolean getServerStart() { public AtomicBoolean getClsStart() { return clsStart; } + + /** + * Getter method for property raftServer. + * + * @return property value of raftServer + */ + public RaftServer getRaftServer() { + return raftServer; + } } \ No newline at end of file diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/RepositoryService.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/RepositoryService.java index 414325dd6..396d7a9f2 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/RepositoryService.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/RepositoryService.java @@ -27,11 +27,23 @@ */ public interface RepositoryService { - V put(K key, V value); + default V put(K key, V value){ + return put(key,value,System.currentTimeMillis()); + } - V remove(Object key); + default V remove(Object key){ + return remove(key,System.currentTimeMillis()); + } - V replace(K key, V value); + default V replace(K key, V value){ + return replace(key,value,System.currentTimeMillis()); + } + + V put(K key, V value,Long currentTimeMillis); + + V remove(Object key,Long currentTimeMillis); + + V replace(K key, V value,Long currentTimeMillis); @ReadOnLeader V get(Object key); diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/DataRepositoryService.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/DataRepositoryService.java index 91c02428c..f7c190529 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/DataRepositoryService.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/DataRepositoryService.java @@ -85,7 +85,8 @@ public SnapshotProcess copy() { } @Override - public RenewDecorate put(String ipAddress, RenewDecorate dataNode) { + public RenewDecorate put(String ipAddress, RenewDecorate dataNode, + Long currentTimeMillis) { write.lock(); try { @@ -94,14 +95,14 @@ public RenewDecorate put(String ipAddress, RenewDecorate dat NodeRepository dataNodeRepository = registry.get(dataCenter); if (dataNodeRepository == null) { NodeRepository nodeRepository = new NodeRepository<>(dataCenter, - new ConcurrentHashMap<>(), System.currentTimeMillis()); + new ConcurrentHashMap<>(), currentTimeMillis); dataNodeRepository = registry.put(dataCenter, nodeRepository); if (dataNodeRepository == null) { dataNodeRepository = nodeRepository; } } - dataNodeRepository.setVersion(System.currentTimeMillis()); + dataNodeRepository.setVersion(currentTimeMillis); Map> dataNodes = dataNodeRepository .getNodeMap(); @@ -123,7 +124,7 @@ public RenewDecorate put(String ipAddress, RenewDecorate dat } @Override - public RenewDecorate remove(Object key) { + public RenewDecorate remove(Object key, Long currentTimeMillis) { write.lock(); try { @@ -142,7 +143,7 @@ public RenewDecorate remove(Object key) { return null; } - dataNodeRepository.setVersion(System.currentTimeMillis()); + dataNodeRepository.setVersion(currentTimeMillis); return oldRenewDecorate; } } @@ -156,7 +157,8 @@ public RenewDecorate remove(Object key) { } @Override - public RenewDecorate replace(String ipAddress, RenewDecorate dataNode) { + public RenewDecorate replace(String ipAddress, RenewDecorate dataNode, + Long currentTimeMillis) { write.lock(); try { diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/MetaRepositoryService.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/MetaRepositoryService.java index a7f5bcdd2..fa88ac867 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/MetaRepositoryService.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/MetaRepositoryService.java @@ -85,7 +85,8 @@ public SnapshotProcess copy() { } @Override - public RenewDecorate put(String ipAddress, RenewDecorate metaNode) { + public RenewDecorate put(String ipAddress, RenewDecorate metaNode, + Long currentTimeMillis) { write.lock(); try { String dataCenter = metaNode.getRenewal().getDataCenter(); @@ -93,14 +94,14 @@ public RenewDecorate put(String ipAddress, RenewDecorate met NodeRepository metaNodeRepository = registry.get(dataCenter); if (metaNodeRepository == null) { NodeRepository nodeRepository = new NodeRepository<>(dataCenter, - new ConcurrentHashMap<>(), System.currentTimeMillis()); + new ConcurrentHashMap<>(), currentTimeMillis); metaNodeRepository = registry.put(dataCenter, nodeRepository); if (metaNodeRepository == null) { metaNodeRepository = nodeRepository; } } - metaNodeRepository.setVersion(System.currentTimeMillis()); + metaNodeRepository.setVersion(currentTimeMillis); Map> metaNodes = metaNodeRepository .getNodeMap(); @@ -122,7 +123,7 @@ public RenewDecorate put(String ipAddress, RenewDecorate met } @Override - public RenewDecorate remove(Object key) { + public RenewDecorate remove(Object key, Long currentTimeMillis) { write.lock(); try { String ipAddress = (String) key; @@ -140,7 +141,7 @@ public RenewDecorate remove(Object key) { return null; } - metaNodeRepository.setVersion(System.currentTimeMillis()); + metaNodeRepository.setVersion(currentTimeMillis); return oldRenewDecorate; } } @@ -154,7 +155,8 @@ public RenewDecorate remove(Object key) { } @Override - public RenewDecorate replace(String ipAddress, RenewDecorate metaNode) { + public RenewDecorate replace(String ipAddress, RenewDecorate metaNode, + Long currentTimeMillis) { write.lock(); try { String dataCenter = metaNode.getRenewal().getDataCenter(); @@ -171,7 +173,7 @@ public RenewDecorate replace(String ipAddress, RenewDecorate oldRenewDecorate.setRenewal(metaNode.getRenewal()); oldRenewDecorate.renew(); - metaNodeRepository.setVersion(System.currentTimeMillis()); + metaNodeRepository.setVersion(currentTimeMillis); } else { LOGGER.error("Meta node with ipAddress {} has not existed!", ipAddress); throw new RuntimeException(String.format( diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/SessionRepositoryService.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/SessionRepositoryService.java index 445c194ac..1b2b3daf2 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/SessionRepositoryService.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/repository/service/SessionRepositoryService.java @@ -78,7 +78,8 @@ public SnapshotProcess copy() { } @Override - public RenewDecorate put(String ipAddress, RenewDecorate sessionNode) { + public RenewDecorate put(String ipAddress, RenewDecorate sessionNode, + Long currentTimeMillis) { try { RenewDecorate oldRenewDecorate = registry.get(ipAddress); if (oldRenewDecorate != null && oldRenewDecorate.getRenewal() != null) { @@ -94,7 +95,7 @@ public RenewDecorate put(String ipAddress, RenewDecorate remove(Object key) { + public RenewDecorate remove(Object key, Long currentTimeMillis) { try { String ipAddress = (String) key; RenewDecorate oldRenewDecorate = registry.remove(ipAddress); @@ -112,7 +113,8 @@ public RenewDecorate remove(Object key) { @Override public RenewDecorate replace(String ipAddress, - RenewDecorate sessionNode) { + RenewDecorate sessionNode, + Long currentTimeMillis) { RenewDecorate oldRenewDecorate = registry.get(ipAddress); if (oldRenewDecorate != null && oldRenewDecorate.getRenewal() != null) { oldRenewDecorate.setRenewal(sessionNode.getRenewal()); diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/resource/HealthResource.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/resource/HealthResource.java index e284a05d9..b2ade6d8f 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/resource/HealthResource.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/resource/HealthResource.java @@ -16,6 +16,15 @@ */ package com.alipay.sofa.registry.server.meta.resource; +import com.alipay.sofa.registry.common.model.CommonResponse; +import com.alipay.sofa.registry.jraft.bootstrap.ServiceStateMachine; +import com.alipay.sofa.registry.metrics.ReporterUtils; +import com.alipay.sofa.registry.server.meta.bootstrap.MetaServerBootstrap; +import com.alipay.sofa.registry.server.meta.remoting.RaftExchanger; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.MetricRegistry; +import org.springframework.beans.factory.annotation.Autowired; + import javax.annotation.PostConstruct; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -25,16 +34,6 @@ import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; -import org.springframework.beans.factory.annotation.Autowired; - -import com.alipay.sofa.registry.common.model.CommonResponse; -import com.alipay.sofa.registry.jraft.bootstrap.ServiceStateMachine; -import com.alipay.sofa.registry.metrics.ReporterUtils; -import com.alipay.sofa.registry.server.meta.bootstrap.MetaServerBootstrap; -import com.alipay.sofa.registry.server.meta.remoting.RaftExchanger; -import com.codahale.metrics.Gauge; -import com.codahale.metrics.MetricRegistry; - /** * * @author shangyu.wh @@ -121,6 +120,7 @@ private CommonResponse getHealthCheckResult() { } else { response = CommonResponse.buildFailedResponse(sb.toString()); } + return response; } } \ No newline at end of file diff --git a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/store/DataStoreService.java b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/store/DataStoreService.java index cbde82c74..5b448d5b2 100644 --- a/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/store/DataStoreService.java +++ b/server/server/meta/src/main/java/com/alipay/sofa/registry/server/meta/store/DataStoreService.java @@ -86,8 +86,6 @@ public class DataStoreService implements StoreService { private AtomicLong localDataCenterInitVersion = new AtomicLong( -1L); - private static final long COMPARE_TIME_COST = 1000L; - @Override public NodeType getNodeType() { return NodeType.DATA; @@ -104,7 +102,6 @@ public NodeChangeResult addNode(DataNode dataNode) { String ipAddress = dataNode.getNodeUrl().getIpAddress(); - long startAll = System.currentTimeMillis(); write.lock(); try { @@ -120,10 +117,6 @@ public NodeChangeResult addNode(DataNode dataNode) { } finally { write.unlock(); } - long cost = System.currentTimeMillis() - startAll; - if (cost >= COMPARE_TIME_COST) { - LOGGER.info("dataRepositoryService.addNode cost:{} ", cost); - } return nodeChangeResult; } @@ -173,7 +166,6 @@ public void removeNodes(Collection nodes) { @Override public void renew(DataNode dataNode, int duration) { - long startAll = System.currentTimeMillis(); write.lock(); try { String ipAddress = dataNode.getNodeUrl().getIpAddress(); @@ -192,10 +184,6 @@ public void renew(DataNode dataNode, int duration) { } } - long cost = System.currentTimeMillis() - startAll; - if (cost >= COMPARE_TIME_COST) { - LOGGER.info("dataRepositoryService.renew.all cost:{} ", cost); - } } finally { write.unlock(); } diff --git a/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/DataNodeChangePushTaskListenerMock.java b/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/DataNodeChangePushTaskListenerMock.java index 162df86cd..609415090 100644 --- a/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/DataNodeChangePushTaskListenerMock.java +++ b/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/DataNodeChangePushTaskListenerMock.java @@ -16,6 +16,11 @@ */ package com.alipay.sofa.registry.server.meta.test.confirm; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import com.alipay.sofa.registry.common.model.Node.NodeType; import com.alipay.sofa.registry.common.model.metaserver.DataNode; import com.alipay.sofa.registry.server.meta.store.DataStoreService; @@ -24,11 +29,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - /** * * @author shangyu.wh @@ -47,8 +47,8 @@ public DataNodeChangePushTaskListenerMock(DataStoreService dataStoreService, } @Override - public boolean support(TaskEvent event) { - return TaskType.DATA_NODE_CHANGE_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.DATA_NODE_CHANGE_PUSH_TASK; } @Override diff --git a/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/ReceiveStatusConfirmNotifyTaskMock.java b/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/ReceiveStatusConfirmNotifyTaskMock.java index bbdf33c62..5d2dcc302 100644 --- a/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/ReceiveStatusConfirmNotifyTaskMock.java +++ b/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/ReceiveStatusConfirmNotifyTaskMock.java @@ -34,8 +34,8 @@ public class ReceiveStatusConfirmNotifyTaskMock implements TaskListener { ReceiveStatusConfirmNotifyTaskMock.class, "[Task]"); @Override - public boolean support(TaskEvent event) { - return TaskType.RECEIVE_STATUS_CONFIRM_NOTIFY_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.RECEIVE_STATUS_CONFIRM_NOTIFY_TASK; } @Override diff --git a/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/SessionNodeChangePushTaskMock.java b/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/SessionNodeChangePushTaskMock.java index 8861c623a..35f5e4840 100644 --- a/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/SessionNodeChangePushTaskMock.java +++ b/server/server/meta/src/test/java/com/alipay/sofa/registry/server/meta/test/confirm/SessionNodeChangePushTaskMock.java @@ -16,6 +16,11 @@ */ package com.alipay.sofa.registry.server.meta.test.confirm; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import com.alipay.sofa.registry.common.model.metaserver.SessionNode; import com.alipay.sofa.registry.server.meta.store.SessionStoreService; import com.alipay.sofa.registry.server.meta.task.Constant; @@ -23,11 +28,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - /** * * @author shangyu.wh @@ -46,8 +46,8 @@ public SessionNodeChangePushTaskMock(SessionStoreService sessionStoreService, } @Override - public boolean support(TaskEvent event) { - return TaskType.SESSION_NODE_CHANGE_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.SESSION_NODE_CHANGE_PUSH_TASK; } @Override diff --git a/server/server/session/pom.xml b/server/server/session/pom.xml index 705df3e34..bd11d0913 100644 --- a/server/server/session/pom.xml +++ b/server/server/session/pom.xml @@ -17,6 +17,10 @@ + + org.slf4j + jul-to-slf4j + org.springframework.boot spring-boot-starter diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerBootstrap.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerBootstrap.java index 3ed3279cd..f99aaa4ba 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerBootstrap.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerBootstrap.java @@ -16,21 +16,6 @@ */ package com.alipay.sofa.registry.server.session.bootstrap; -import java.lang.annotation.Annotation; -import java.util.Collection; -import java.util.Date; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.annotation.Resource; -import javax.ws.rs.Path; -import javax.ws.rs.ext.Provider; - -import org.glassfish.jersey.server.ResourceConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.util.CollectionUtils; - import com.alipay.sofa.registry.common.model.Node; import com.alipay.sofa.registry.common.model.constants.ValueConstants; import com.alipay.sofa.registry.common.model.metaserver.FetchProvideDataRequest; @@ -51,11 +36,23 @@ import com.alipay.sofa.registry.server.session.node.NodeManagerFactory; import com.alipay.sofa.registry.server.session.node.RaftClientManager; import com.alipay.sofa.registry.server.session.node.SessionProcessIdGenerator; -import com.alipay.sofa.registry.server.session.registry.Registry; -import com.alipay.sofa.registry.server.session.remoting.handler.AbstractClientHandler; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; import com.alipay.sofa.registry.server.session.remoting.handler.AbstractServerHandler; import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; import com.alipay.sofa.registry.task.batcher.TaskDispatchers; +import org.glassfish.jersey.server.ResourceConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.util.CollectionUtils; + +import javax.annotation.Resource; +import javax.ws.rs.Path; +import javax.ws.rs.ext.Provider; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; /** * The type Session server bootstrap. @@ -82,18 +79,15 @@ public class SessionServerBootstrap { @Resource(name = "serverHandlers") private Collection serverHandlers; - @Resource(name = "dataClientHandlers") - private Collection dataClientHandlers; - - @Autowired - private NodeManager dataNodeManager; - @Autowired private NodeManager metaNodeManager; @Autowired protected NodeExchanger metaNodeExchanger; + @Autowired + private NodeExchanger dataNodeExchanger; + @Autowired private ResourceConfig jerseyResourceConfig; @@ -107,7 +101,7 @@ public class SessionServerBootstrap { private BlacklistManager blacklistManager; @Autowired - private Registry sessionRegistry; + private ProvideDataProcessor provideDataProcessorManager; private Server server; @@ -170,7 +164,6 @@ private void doStop() { executorManager.stopScheduler(); TaskDispatchers.stopDefaultSingleTaskDispatcher(); - closeClients(); stopHttpServer(); stopServer(); } catch (Throwable e) { @@ -220,28 +213,7 @@ private void openSessionServer() { private void connectDataServer() { try { if (dataStart.compareAndSet(false, true)) { - Collection dataNodes = dataNodeManager.getDataCenterNodes(); - if (CollectionUtils.isEmpty(dataNodes)) { - dataNodeManager.getAllDataCenterNodes(); - dataNodes = dataNodeManager.getDataCenterNodes(); - } - if (!CollectionUtils.isEmpty(dataNodes)) { - for (Node dataNode : dataNodes) { - if (dataNode.getNodeUrl() == null - || dataNode.getNodeUrl().getIpAddress() == null) { - LOGGER - .error("get data node address error!url{}", dataNode.getNodeUrl()); - continue; - } - dataClient = boltExchange.connect( - Exchange.DATA_SERVER_TYPE, - new URL(dataNode.getNodeUrl().getIpAddress(), sessionServerConfig - .getDataServerPort()), dataClientHandlers - .toArray(new ChannelHandler[dataClientHandlers.size()])); - } - LOGGER.info("Data server connected {} server! port:{}", dataNodes.size(), - sessionServerConfig.getDataServerPort()); - } + dataNodeExchanger.connectServer(); } } catch (Exception e) { dataStart.set(false); @@ -261,8 +233,6 @@ private void connectMetaServer() { if (metaStart.compareAndSet(false, true)) { metaClient = metaNodeExchanger.connectServer(); - int size = metaClient.getChannels().size(); - URL leaderUrl = new URL(raftClientManager.getLeader().getIp(), sessionServerConfig.getMetaServerPort()); @@ -276,7 +246,7 @@ private void connectMetaServer() { fetchBlackList(); - LOGGER.info("MetaServer connected {} server! Port:{}", size, + LOGGER.info("MetaServer connected meta server! Port:{}", sessionServerConfig.getMetaServerPort()); } } catch (Exception e) { @@ -309,20 +279,7 @@ private void fetchStopPushSwitch(URL leaderUrl) { Object ret = sendMetaRequest(fetchProvideDataRequest, leaderUrl); if (ret instanceof ProvideData) { ProvideData provideData = (ProvideData) ret; - if (provideData.getProvideData() == null - || provideData.getProvideData().getObject() == null) { - LOGGER.info("Fetch session stop push switch no data existed,config not change!"); - return; - } - String data = (String) provideData.getProvideData().getObject(); - sessionServerConfig.setStopPushSwitch(Boolean.valueOf(data)); - if (data != null) { - if (!Boolean.valueOf(data)) { - //stop push init on,then begin fetch data schedule task - sessionServerConfig.setBeginDataFetchTask(true); - } - } - LOGGER.info("Fetch session stop push data switch {} success!", data); + provideDataProcessorManager.fetchDataProcess(provideData); } else { LOGGER.info("Fetch session stop push switch data null,config not change!"); } @@ -334,16 +291,7 @@ private void fetchEnableDataRenewSnapshot(URL leaderUrl) { Object data = sendMetaRequest(fetchProvideDataRequest, leaderUrl); if (data instanceof ProvideData) { ProvideData provideData = (ProvideData) data; - if (provideData == null || provideData.getProvideData() == null - || provideData.getProvideData().getObject() == null) { - LOGGER - .info("Fetch enableDataRenewSnapshot but no data existed, current config not change!"); - return; - } - boolean enableDataRenewSnapshot = Boolean.parseBoolean((String) provideData - .getProvideData().getObject()); - LOGGER.info("Fetch enableDataRenewSnapshot {} success!", enableDataRenewSnapshot); - this.sessionRegistry.setEnableDataRenewSnapshot(enableDataRenewSnapshot); + provideDataProcessorManager.fetchDataProcess(provideData); } } @@ -354,13 +302,13 @@ private void fetchBlackList() { private Object sendMetaRequest(Object request, URL leaderUrl) { Object ret; try { - ret = metaClient.sendSync(metaClient.getChannel(leaderUrl), request, + ret = metaClient.sendSync(leaderUrl, request, sessionServerConfig.getMetaNodeExchangeTimeOut()); } catch (Exception e) { URL leaderUrlNew = new URL(raftClientManager.refreshLeader().getIp(), sessionServerConfig.getMetaServerPort()); LOGGER.warn("request send error!It will be retry once to new leader {}!", leaderUrlNew); - ret = metaClient.sendSync(metaClient.getChannel(leaderUrlNew), request, + ret = metaClient.sendSync(leaderUrlNew, request, sessionServerConfig.getMetaNodeExchangeTimeOut()); } return ret; @@ -411,12 +359,6 @@ private void stopServer() { } } - private void closeClients() { - if (dataClient != null && !dataClient.isClosed()) { - dataClient.close(); - } - } - private void stopHttpServer() { if (httpServer != null && httpServer.isOpen()) { httpServer.close(); diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfig.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfig.java index d0c5277a4..4bc1db130 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfig.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfig.java @@ -214,4 +214,7 @@ public interface SessionServerConfig { long getPublishDataExecutorKeepAliveTime(); double getAccessLimitRate(); + + int getDataClientConnNum(); + } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfigBean.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfigBean.java index aba075070..47890eb73 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfigBean.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfigBean.java @@ -16,14 +16,14 @@ */ package com.alipay.sofa.registry.server.session.bootstrap; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.springframework.boot.context.properties.ConfigurationProperties; - import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.springframework.boot.context.properties.ConfigurationProperties; + /** * The type Session server config bean. * @author shangyu.wh @@ -67,13 +67,13 @@ public class SessionServerConfigBean implements SessionServerConfig { private int schedulerConnectMetaFirstDelay = 5; - private int schedulerConnectMetaExpBackOffBound = 10; + private int schedulerConnectMetaExpBackOffBound = 3; - private int schedulerConnectDataTimeout = 3; + private int schedulerConnectDataTimeout = 10; - private int schedulerConnectDataFirstDelay = 3; + private int schedulerConnectDataFirstDelay = 10; - private int schedulerConnectDataExpBackOffBound = 10; + private int schedulerConnectDataExpBackOffBound = 3; private int schedulerCleanInvalidClientTimeOut = 3; @@ -238,6 +238,8 @@ public class SessionServerConfigBean implements SessionServerConfig { private int dataNodeRetryExecutorThreadSize = 100; + private int dataClientConnNum = 10; + //end config for enterprise version private CommonConfig commonConfig; @@ -2029,6 +2031,25 @@ public void setAccessLimitRate(double accessLimitRate) { this.accessLimitRate = accessLimitRate; } + /** + * Getter method for property dataClientConnNum. + * + * @return property value of dataClientConnNum + */ + @Override + public int getDataClientConnNum() { + return dataClientConnNum; + } + + /** + * Setter method for property dataClientConnNum . + * + * @param dataClientConnNum value to be assigned to property dataClientConnNum + */ + public void setDataClientConnNum(int dataClientConnNum) { + this.dataClientConnNum = dataClientConnNum; + } + @Override public boolean isInvalidForeverZone(String zoneId) { diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfiguration.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfiguration.java index 1faa2628c..db4ea5991 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfiguration.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/bootstrap/SessionServerConfiguration.java @@ -16,6 +16,17 @@ */ package com.alipay.sofa.registry.server.session.bootstrap; +import java.util.ArrayList; +import java.util.Collection; + +import org.glassfish.jersey.jackson.JacksonFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + import com.alipay.sofa.registry.remoting.bolt.exchange.BoltExchange; import com.alipay.sofa.registry.remoting.exchange.Exchange; import com.alipay.sofa.registry.remoting.exchange.NodeExchanger; @@ -68,6 +79,11 @@ import com.alipay.sofa.registry.server.session.node.service.DataNodeServiceImpl; import com.alipay.sofa.registry.server.session.node.service.MetaNodeService; import com.alipay.sofa.registry.server.session.node.service.MetaNodeServiceImpl; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessorManager; +import com.alipay.sofa.registry.server.session.provideData.processor.BlackListProvideDataProcessor; +import com.alipay.sofa.registry.server.session.provideData.processor.RenewSnapshotProvideDataProcessor; +import com.alipay.sofa.registry.server.session.provideData.processor.StopPushProvideDataProcessor; import com.alipay.sofa.registry.server.session.registry.Registry; import com.alipay.sofa.registry.server.session.registry.SessionRegistry; import com.alipay.sofa.registry.server.session.remoting.ClientNodeExchanger; @@ -133,16 +149,6 @@ import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; import com.alipay.sofa.registry.util.PropertySplitter; -import org.glassfish.jersey.jackson.JacksonFeature; -import org.glassfish.jersey.server.ResourceConfig; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; - -import java.util.ArrayList; -import java.util.Collection; /** * @@ -168,7 +174,7 @@ public CommonConfig commonConfig() { } @Bean - @ConditionalOnMissingBean(name = "sessionServerConfig") + @ConditionalOnMissingBean public SessionServerConfig sessionServerConfig(CommonConfig commonConfig) { return new SessionServerConfigBean(commonConfig); } @@ -738,4 +744,37 @@ public RenewService renewService() { return new DefaultRenewService(); } } + + @Configuration + public static class SessionProvideDataConfiguration { + + @Bean + public ProvideDataProcessor provideDataProcessorManager() { + return new ProvideDataProcessorManager(); + } + + @Bean + public ProvideDataProcessor blackListProvideDataProcessor(ProvideDataProcessor provideDataProcessorManager) { + ProvideDataProcessor blackListProvideDataProcessor = new BlackListProvideDataProcessor(); + ((ProvideDataProcessorManager) provideDataProcessorManager) + .addProvideDataProcessor(blackListProvideDataProcessor); + return blackListProvideDataProcessor; + } + + @Bean + public ProvideDataProcessor renewSnapshotProvideDataProcessor(ProvideDataProcessor provideDataProcessorManager) { + ProvideDataProcessor renewSnapshotProvideDataProcessor = new RenewSnapshotProvideDataProcessor(); + ((ProvideDataProcessorManager) provideDataProcessorManager) + .addProvideDataProcessor(renewSnapshotProvideDataProcessor); + return renewSnapshotProvideDataProcessor; + } + + @Bean + public ProvideDataProcessor stopPushProvideDataProcessor(ProvideDataProcessor provideDataProcessorManager) { + ProvideDataProcessor stopPushProvideDataProcessor = new StopPushProvideDataProcessor(); + ((ProvideDataProcessorManager) provideDataProcessorManager) + .addProvideDataProcessor(stopPushProvideDataProcessor); + return stopPushProvideDataProcessor; + } + } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/CancelDataTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/CancelDataTaskListener.java index 0bae12a2e..0197fd8b5 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/CancelDataTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/CancelDataTaskListener.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.server.session.scheduler.task.CancelDataTask; @@ -29,7 +33,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -67,18 +70,16 @@ public class CancelDataTaskListener implements TaskListener { @Autowired private TaskProcessor dataNodeSingleTaskProcessor; - public TaskDispatcher getSingleTaskDispatcher() { - if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( - TaskDispatchers.getDispatcherName(TaskType.CANCEL_DATA_TASK.getName()), 10000, 80, - 1000, 100, dataNodeSingleTaskProcessor); - } - return singleTaskDispatcher; + @PostConstruct + public void init() { + singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( + TaskDispatchers.getDispatcherName(TaskType.CANCEL_DATA_TASK.getName()), 10000, 80, + 1000, 100, dataNodeSingleTaskProcessor); } @Override - public boolean support(TaskEvent event) { - return TaskType.CANCEL_DATA_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.CANCEL_DATA_TASK; } @Override @@ -88,7 +89,8 @@ public void handleEvent(TaskEvent event) { sessionWatchers, dataNodeService, sessionServerConfig); cancelDataTask.setTaskEvent(event); - getSingleTaskDispatcher().dispatch(cancelDataTask.getTaskId(), cancelDataTask, + singleTaskDispatcher.dispatch(cancelDataTask.getTaskId(), cancelDataTask, cancelDataTask.getExpiryTime()); } + } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchCloudTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchCloudTaskListener.java index e558c98f5..65674b062 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchCloudTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchCloudTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.session.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.cache.CacheService; import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; @@ -29,7 +31,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -39,26 +40,26 @@ public class DataChangeFetchCloudTaskListener implements TaskListener { @Autowired - private Interests sessionInterests; + private Interests sessionInterests; @Autowired - private SessionServerConfig sessionServerConfig; + private SessionServerConfig sessionServerConfig; /** * trigger task com.alipay.sofa.registry.server.meta.listener process */ @Autowired - private TaskListenerManager taskListenerManager; + private TaskListenerManager taskListenerManager; @Autowired - private ExecutorManager executorManager; + private ExecutorManager executorManager; @Autowired - private CacheService sessionCacheService; + private CacheService sessionCacheService; - private TaskDispatcher singleTaskDispatcher; + private volatile TaskDispatcher singleTaskDispatcher; - private TaskProcessor dataNodeSingleTaskProcessor; + private TaskProcessor dataNodeSingleTaskProcessor; public DataChangeFetchCloudTaskListener(TaskProcessor dataNodeSingleTaskProcessor) { this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; @@ -66,18 +67,22 @@ public DataChangeFetchCloudTaskListener(TaskProcessor dataNodeSingleTaskProcesso public TaskDispatcher getSingleTaskDispatcher() { if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( - TaskDispatchers.getDispatcherName(TaskType.DATA_CHANGE_FETCH_CLOUD_TASK.getName()), - sessionServerConfig.getDataChangeFetchTaskMaxBufferSize(), - sessionServerConfig.getDataChangeFetchTaskWorkerSize(), 1000, 100, - dataNodeSingleTaskProcessor); + synchronized (this) { + if (singleTaskDispatcher == null) { + singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( + TaskDispatchers.getDispatcherName(TaskType.DATA_CHANGE_FETCH_CLOUD_TASK + .getName()), sessionServerConfig.getDataChangeFetchTaskMaxBufferSize(), + sessionServerConfig.getDataChangeFetchTaskWorkerSize(), 1000, 100, + dataNodeSingleTaskProcessor); + } + } } return singleTaskDispatcher; } @Override - public boolean support(TaskEvent event) { - return TaskType.DATA_CHANGE_FETCH_CLOUD_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.DATA_CHANGE_FETCH_CLOUD_TASK; } @Override @@ -88,4 +93,5 @@ public void handleEvent(TaskEvent event) { getSingleTaskDispatcher().dispatch(dataChangeFetchTask.getTaskId(), dataChangeFetchTask, dataChangeFetchTask.getExpiryTime()); } + } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchTaskListener.java index 434e6a61c..a2517509a 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataChangeFetchTaskListener.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.registry.server.session.listener; +import org.springframework.beans.factory.annotation.Autowired; + +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.cache.CacheService; import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; @@ -29,7 +33,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -38,27 +41,30 @@ */ public class DataChangeFetchTaskListener implements TaskListener { + private final static Logger LOGGER = LoggerFactory + .getLogger(DataChangeFetchTaskListener.class); + @Autowired - private SessionServerConfig sessionServerConfig; + private SessionServerConfig sessionServerConfig; @Autowired - private Interests sessionInterests; + private Interests sessionInterests; @Autowired - private ExecutorManager executorManager; + private ExecutorManager executorManager; @Autowired - private CacheService sessionCacheService; + private CacheService sessionCacheService; /** * trigger task com.alipay.sofa.registry.server.meta.listener process */ @Autowired - private TaskListenerManager taskListenerManager; + private TaskListenerManager taskListenerManager; - private TaskDispatcher singleTaskDispatcher; + private volatile TaskDispatcher singleTaskDispatcher; - private TaskProcessor dataNodeSingleTaskProcessor; + private TaskProcessor dataNodeSingleTaskProcessor; public DataChangeFetchTaskListener(TaskProcessor dataNodeSingleTaskProcessor) { this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; @@ -66,18 +72,23 @@ public DataChangeFetchTaskListener(TaskProcessor dataNodeSingleTaskProcessor) { public TaskDispatcher getSingleTaskDispatcher() { if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( - TaskDispatchers.getDispatcherName(TaskType.DATA_CHANGE_FETCH_TASK.getName()), - sessionServerConfig.getDataChangeFetchTaskMaxBufferSize(), - sessionServerConfig.getDataChangeFetchTaskWorkerSize(), 1000, 100, - dataNodeSingleTaskProcessor); + synchronized (this) { + if (singleTaskDispatcher == null) { + singleTaskDispatcher = TaskDispatchers + .createSingleTaskDispatcher(TaskDispatchers + .getDispatcherName(TaskType.DATA_CHANGE_FETCH_TASK.getName()), + sessionServerConfig.getDataChangeFetchTaskMaxBufferSize(), + sessionServerConfig.getDataChangeFetchTaskWorkerSize(), 1000, 100, + dataNodeSingleTaskProcessor); + } + } } return singleTaskDispatcher; } @Override - public boolean support(TaskEvent event) { - return TaskType.DATA_CHANGE_FETCH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.DATA_CHANGE_FETCH_TASK; } @Override @@ -85,8 +96,10 @@ public void handleEvent(TaskEvent event) { SessionTask dataChangeFetchTask = new DataChangeFetchTask(sessionServerConfig, taskListenerManager, executorManager, sessionInterests, sessionCacheService); dataChangeFetchTask.setTaskEvent(event); + getSingleTaskDispatcher().dispatch(dataChangeFetchTask.getTaskId(), dataChangeFetchTask, dataChangeFetchTask.getExpiryTime()); + } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataPushTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataPushTaskListener.java index c1afe3314..f27db140e 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataPushTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DataPushTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.session.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; import com.alipay.sofa.registry.server.session.scheduler.task.DataPushTask; @@ -28,7 +30,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -38,23 +39,23 @@ public class DataPushTaskListener implements TaskListener { @Autowired - private SessionServerConfig sessionServerConfig; + private SessionServerConfig sessionServerConfig; @Autowired - private Interests sessionInterests; + private Interests sessionInterests; /** * trigger task com.alipay.sofa.registry.server.meta.listener process */ @Autowired - private TaskListenerManager taskListenerManager; + private TaskListenerManager taskListenerManager; @Autowired - private ExecutorManager executorManager; + private ExecutorManager executorManager; - private TaskDispatcher singleTaskDispatcher; + private volatile TaskDispatcher singleTaskDispatcher; - private TaskProcessor dataNodeSingleTaskProcessor; + private TaskProcessor dataNodeSingleTaskProcessor; public DataPushTaskListener(TaskProcessor dataNodeSingleTaskProcessor) { @@ -63,15 +64,19 @@ public DataPushTaskListener(TaskProcessor dataNodeSingleTaskProcessor) { public TaskDispatcher getSingleTaskDispatcher() { if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( - TaskType.DATA_PUSH_TASK.getName(), dataNodeSingleTaskProcessor); + synchronized (this) { + if (singleTaskDispatcher == null) { + singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( + TaskType.DATA_PUSH_TASK.getName(), dataNodeSingleTaskProcessor); + } + } } return singleTaskDispatcher; } @Override - public boolean support(TaskEvent event) { - return TaskType.DATA_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.DATA_PUSH_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DatumSnapshotTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DatumSnapshotTaskListener.java index be81dc7d0..b86795cba 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DatumSnapshotTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/DatumSnapshotTaskListener.java @@ -53,8 +53,8 @@ public void init() { } @Override - public boolean support(TaskEvent event) { - return TaskType.DATUM_SNAPSHOT_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.DATUM_SNAPSHOT_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ProvideDataChangeFetchTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ProvideDataChangeFetchTaskListener.java index aeb82529b..234030672 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ProvideDataChangeFetchTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ProvideDataChangeFetchTaskListener.java @@ -16,14 +16,16 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.remoting.exchange.Exchange; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; -import com.alipay.sofa.registry.server.session.filter.blacklist.BlacklistManager; import com.alipay.sofa.registry.server.session.node.service.MetaNodeService; -import com.alipay.sofa.registry.server.session.registry.Registry; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; import com.alipay.sofa.registry.server.session.scheduler.task.ProvideDataChangeFetchTask; import com.alipay.sofa.registry.server.session.scheduler.task.SessionTask; -import com.alipay.sofa.registry.server.session.store.Interests; import com.alipay.sofa.registry.server.session.store.Watchers; import com.alipay.sofa.registry.task.batcher.TaskDispatcher; import com.alipay.sofa.registry.task.batcher.TaskDispatchers; @@ -32,7 +34,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -59,17 +60,11 @@ public class ProvideDataChangeFetchTaskListener implements TaskListener { @Autowired private Exchange boltExchange; - @Autowired - private Interests sessionInterests; - @Autowired private Watchers sessionWatchers; @Autowired - private Registry sessionRegistry; - - @Autowired - private BlacklistManager blacklistManager; + private ProvideDataProcessor provideDataProcessorManager; private TaskDispatcher singleTaskDispatcher; @@ -79,17 +74,15 @@ public ProvideDataChangeFetchTaskListener(TaskProcessor dataNodeSingleTaskProces this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; } - public TaskDispatcher getSingleTaskDispatcher() { - if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( - TaskType.PROVIDE_DATA_CHANGE_FETCH_TASK.getName(), dataNodeSingleTaskProcessor); - } - return singleTaskDispatcher; + @PostConstruct + public void init() { + singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( + TaskType.PROVIDE_DATA_CHANGE_FETCH_TASK.getName(), dataNodeSingleTaskProcessor); } @Override - public boolean support(TaskEvent event) { - return TaskType.PROVIDE_DATA_CHANGE_FETCH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.PROVIDE_DATA_CHANGE_FETCH_TASK; } @Override @@ -97,11 +90,11 @@ public void handleEvent(TaskEvent event) { SessionTask provideDataChangeFetchTask = new ProvideDataChangeFetchTask( sessionServerConfig, taskListenerManager, metaNodeService, sessionWatchers, - boltExchange, sessionInterests, sessionRegistry, blacklistManager); + boltExchange, provideDataProcessorManager); provideDataChangeFetchTask.setTaskEvent(event); - getSingleTaskDispatcher().dispatch(provideDataChangeFetchTask.getTaskId(), + singleTaskDispatcher.dispatch(provideDataChangeFetchTask.getTaskId(), provideDataChangeFetchTask, provideDataChangeFetchTask.getExpiryTime()); } diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/PublishDataTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/PublishDataTaskListener.java index e2d0f7ef8..5b5d8be7d 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/PublishDataTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/PublishDataTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.session.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; import com.alipay.sofa.registry.server.session.scheduler.task.PublishDataTask; @@ -24,7 +26,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -43,8 +44,8 @@ public class PublishDataTaskListener implements TaskListener { private ExecutorManager executorManager; @Override - public boolean support(TaskEvent event) { - return TaskType.PUBLISH_DATA_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.PUBLISH_DATA_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedConfigDataPushTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedConfigDataPushTaskListener.java index 17cc54b5e..5103ae201 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedConfigDataPushTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedConfigDataPushTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.session.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.node.service.ClientNodeService; import com.alipay.sofa.registry.server.session.scheduler.task.ReceivedConfigDataPushTask; @@ -27,7 +29,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -37,17 +38,17 @@ public class ReceivedConfigDataPushTaskListener implements TaskListener { @Autowired - private SessionServerConfig sessionServerConfig; + private SessionServerConfig sessionServerConfig; @Autowired - private ClientNodeService clientNodeService; + private ClientNodeService clientNodeService; @Autowired - private ReceivedConfigDataPushTaskStrategy receivedConfigDataPushTaskStrategy; + private ReceivedConfigDataPushTaskStrategy receivedConfigDataPushTaskStrategy; - private TaskDispatcher singleTaskDispatcher; + private volatile TaskDispatcher singleTaskDispatcher; - private TaskProcessor clientNodeSingleTaskProcessor; + private TaskProcessor clientNodeSingleTaskProcessor; public ReceivedConfigDataPushTaskListener(TaskProcessor clientNodeSingleTaskProcessor) { @@ -56,15 +57,20 @@ public ReceivedConfigDataPushTaskListener(TaskProcessor clientNodeSingleTaskProc public TaskDispatcher getSingleTaskDispatcher() { if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( - TaskType.RECEIVED_DATA_CONFIG_PUSH_TASK.getName(), clientNodeSingleTaskProcessor); + synchronized (this) { + if (singleTaskDispatcher == null) { + singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( + TaskType.RECEIVED_DATA_CONFIG_PUSH_TASK.getName(), + clientNodeSingleTaskProcessor); + } + } } return singleTaskDispatcher; } @Override - public boolean support(TaskEvent event) { - return TaskType.RECEIVED_DATA_CONFIG_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.RECEIVED_DATA_CONFIG_PUSH_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedDataMultiPushTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedDataMultiPushTaskListener.java index 93a706bc5..3d036f647 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedDataMultiPushTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/ReceivedDataMultiPushTaskListener.java @@ -106,8 +106,8 @@ public void executionFailed(Throwable e) { } @Override - public boolean support(TaskEvent event) { - return TaskType.RECEIVED_DATA_MULTI_PUSH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.RECEIVED_DATA_MULTI_PUSH_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/RenewDatumTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/RenewDatumTaskListener.java index a5f497090..d1624659d 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/RenewDatumTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/RenewDatumTaskListener.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.server.session.registry.SessionRegistry; @@ -27,9 +31,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.annotation.PostConstruct; /** * @@ -60,8 +61,8 @@ public void init() { } @Override - public boolean support(TaskEvent event) { - return TaskType.RENEW_DATUM_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.RENEW_DATUM_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SessionRegisterDataTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SessionRegisterDataTaskListener.java index 38563b430..2cec75755 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SessionRegisterDataTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SessionRegisterDataTaskListener.java @@ -16,9 +16,12 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.remoting.exchange.Exchange; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; -import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.server.session.scheduler.task.SessionRegisterDataTask; import com.alipay.sofa.registry.server.session.scheduler.task.SessionTask; import com.alipay.sofa.registry.task.batcher.TaskDispatcher; @@ -27,7 +30,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -36,12 +38,6 @@ */ public class SessionRegisterDataTaskListener implements TaskListener { - /** - * DataNode service - */ - @Autowired - private DataNodeService dataNodeService; - @Autowired private Exchange boltExchange; @@ -57,26 +53,24 @@ public SessionRegisterDataTaskListener(TaskProcessor dataNodeSingleTaskProcessor this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; } - public TaskDispatcher getSingleTaskDispatcher() { - if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( - TaskDispatchers.getDispatcherName(TaskType.SESSION_REGISTER_DATA_TASK.getName()), - 60, 5, 1000, 100, dataNodeSingleTaskProcessor); - } - return singleTaskDispatcher; + @PostConstruct + public void init() { + singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( + TaskDispatchers.getDispatcherName(TaskType.SESSION_REGISTER_DATA_TASK.getName()), 60, + 5, 1000, 100, dataNodeSingleTaskProcessor); } @Override - public boolean support(TaskEvent event) { - return TaskType.SESSION_REGISTER_DATA_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.SESSION_REGISTER_DATA_TASK; } @Override public void handleEvent(TaskEvent event) { SessionTask sessionRegisterDataTask = new SessionRegisterDataTask(boltExchange, - dataNodeService, sessionServerConfig); + sessionServerConfig); sessionRegisterDataTask.setTaskEvent(event); - getSingleTaskDispatcher().dispatch(sessionRegisterDataTask.getTaskId(), - sessionRegisterDataTask, sessionRegisterDataTask.getExpiryTime()); + singleTaskDispatcher.dispatch(sessionRegisterDataTask.getTaskId(), sessionRegisterDataTask, + sessionRegisterDataTask.getExpiryTime()); } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberMultiFetchTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberMultiFetchTaskListener.java index 773e5e367..827f20fab 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberMultiFetchTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberMultiFetchTaskListener.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.cache.CacheService; import com.alipay.sofa.registry.server.session.scheduler.task.SessionTask; @@ -28,7 +32,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -61,18 +64,16 @@ public SubscriberMultiFetchTaskListener(TaskProcessor dataNodeSingleTaskProcesso this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; } - public TaskDispatcher getSingleTaskDispatcher() { - if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( - TaskDispatchers.getDispatcherName(TaskType.SUBSCRIBER_MULTI_FETCH_TASK.getName()), - 100000, 80, 0, 0, dataNodeSingleTaskProcessor); - } - return singleTaskDispatcher; + @PostConstruct + public void init() { + singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( + TaskDispatchers.getDispatcherName(TaskType.SUBSCRIBER_MULTI_FETCH_TASK.getName()), + 100000, 80, 0, 0, dataNodeSingleTaskProcessor); } @Override - public boolean support(TaskEvent event) { - return TaskType.SUBSCRIBER_MULTI_FETCH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.SUBSCRIBER_MULTI_FETCH_TASK; } @Override @@ -83,7 +84,7 @@ public void handleEvent(TaskEvent event) { subscriberMultiFetchTask.setTaskEvent(event); - getSingleTaskDispatcher().dispatch(subscriberMultiFetchTask.getTaskId(), + singleTaskDispatcher.dispatch(subscriberMultiFetchTask.getTaskId(), subscriberMultiFetchTask, subscriberMultiFetchTask.getExpiryTime()); } diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberPushEmptyTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberPushEmptyTaskListener.java index 955af4062..d3b36e119 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberPushEmptyTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberPushEmptyTaskListener.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.scheduler.task.SessionTask; import com.alipay.sofa.registry.server.session.scheduler.task.SubscriberPushEmptyTask; @@ -26,7 +30,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -49,17 +52,15 @@ public class SubscriberPushEmptyTaskListener implements TaskListener { @Autowired private TaskProcessor dataNodeSingleTaskProcessor; - public TaskDispatcher getSingleTaskDispatcher() { - if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( - TaskType.SUBSCRIBER_PUSH_EMPTY_TASK.getName(), dataNodeSingleTaskProcessor); - } - return singleTaskDispatcher; + @PostConstruct + public void init() { + singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( + TaskType.SUBSCRIBER_PUSH_EMPTY_TASK.getName(), dataNodeSingleTaskProcessor); } @Override - public boolean support(TaskEvent event) { - return TaskType.SUBSCRIBER_PUSH_EMPTY_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.SUBSCRIBER_PUSH_EMPTY_TASK; } @Override @@ -70,8 +71,8 @@ public void handleEvent(TaskEvent event) { subscriberPushEmptyTask.setTaskEvent(event); - getSingleTaskDispatcher().dispatch(subscriberPushEmptyTask.getTaskId(), - subscriberPushEmptyTask, subscriberPushEmptyTask.getExpiryTime()); + singleTaskDispatcher.dispatch(subscriberPushEmptyTask.getTaskId(), subscriberPushEmptyTask, + subscriberPushEmptyTask.getExpiryTime()); } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberRegisterFetchTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberRegisterFetchTaskListener.java index 68b66cf63..6feb653a2 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberRegisterFetchTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/SubscriberRegisterFetchTaskListener.java @@ -16,6 +16,10 @@ */ package com.alipay.sofa.registry.server.session.listener; +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.cache.CacheService; import com.alipay.sofa.registry.server.session.node.service.DataNodeService; @@ -29,7 +33,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -67,18 +70,16 @@ public SubscriberRegisterFetchTaskListener(TaskProcessor dataNodeSingleTaskProce this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; } - public TaskDispatcher getSingleTaskDispatcher() { - if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher(TaskDispatchers - .getDispatcherName(TaskType.SUBSCRIBER_REGISTER_FETCH_TASK.getName()), 200000, 80, - 1000, 100, dataNodeSingleTaskProcessor); - } - return singleTaskDispatcher; + @PostConstruct + public void init() { + singleTaskDispatcher = TaskDispatchers.createSingleTaskDispatcher( + TaskDispatchers.getDispatcherName(TaskType.SUBSCRIBER_REGISTER_FETCH_TASK.getName()), + 200000, 80, 1000, 100, dataNodeSingleTaskProcessor); } @Override - public boolean support(TaskEvent event) { - return TaskType.SUBSCRIBER_REGISTER_FETCH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.SUBSCRIBER_REGISTER_FETCH_TASK; } @Override @@ -90,7 +91,7 @@ public void handleEvent(TaskEvent event) { subscriberRegisterFetchTask.setTaskEvent(event); - getSingleTaskDispatcher().dispatch(subscriberRegisterFetchTask.getTaskId(), + singleTaskDispatcher.dispatch(subscriberRegisterFetchTask.getTaskId(), subscriberRegisterFetchTask, subscriberRegisterFetchTask.getExpiryTime()); } diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/UnPublishDataTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/UnPublishDataTaskListener.java index 566910f07..40cd02a0c 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/UnPublishDataTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/UnPublishDataTaskListener.java @@ -20,13 +20,10 @@ import org.springframework.beans.factory.annotation.Autowired; -import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.server.session.scheduler.task.SessionTask; import com.alipay.sofa.registry.server.session.scheduler.task.UnPublishDataTask; -import com.alipay.sofa.registry.server.session.store.DataStore; -import com.alipay.sofa.registry.server.session.store.Interests; -import com.alipay.sofa.registry.server.session.store.Watchers; + import com.alipay.sofa.registry.task.batcher.TaskDispatcher; import com.alipay.sofa.registry.task.batcher.TaskDispatchers; import com.alipay.sofa.registry.task.batcher.TaskProcessor; @@ -40,31 +37,12 @@ * @version $Id: UnPublishDataTaskListener.java, v 0.1 2019-06-17 12:02 kezhu.wukz Exp $ */ public class UnPublishDataTaskListener implements TaskListener { - - /** - * store subscribers - */ - @Autowired - private Interests sessionInterests; - - /** - * store publishers - */ - @Autowired - private DataStore sessionDataStore; - - @Autowired - private Watchers sessionWatchers; - /** * transfer data to DataNode */ @Autowired private DataNodeService dataNodeService; - @Autowired - private SessionServerConfig sessionServerConfig; - private TaskDispatcher singleTaskDispatcher; @Autowired @@ -78,8 +56,8 @@ public void init() { } @Override - public boolean support(TaskEvent event) { - return TaskType.UN_PUBLISH_DATA_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.UN_PUBLISH_DATA_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/WatcherRegisterFetchTaskListener.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/WatcherRegisterFetchTaskListener.java index 68e4cd6f9..45e14542c 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/WatcherRegisterFetchTaskListener.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/listener/WatcherRegisterFetchTaskListener.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.registry.server.session.listener; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.node.service.MetaNodeService; import com.alipay.sofa.registry.server.session.scheduler.task.SessionTask; @@ -27,7 +29,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListener; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -37,23 +38,23 @@ public class WatcherRegisterFetchTaskListener implements TaskListener { @Autowired - private SessionServerConfig sessionServerConfig; + private SessionServerConfig sessionServerConfig; /** * trigger push client process */ @Autowired - private TaskListenerManager taskListenerManager; + private TaskListenerManager taskListenerManager; /** * MetaNode service */ @Autowired - private MetaNodeService metaNodeService; + private MetaNodeService metaNodeService; - private TaskDispatcher singleTaskDispatcher; + private volatile TaskDispatcher singleTaskDispatcher; - private TaskProcessor dataNodeSingleTaskProcessor; + private TaskProcessor dataNodeSingleTaskProcessor; public WatcherRegisterFetchTaskListener(TaskProcessor dataNodeSingleTaskProcessor) { this.dataNodeSingleTaskProcessor = dataNodeSingleTaskProcessor; @@ -61,15 +62,21 @@ public WatcherRegisterFetchTaskListener(TaskProcessor dataNodeSingleTaskProcesso public TaskDispatcher getSingleTaskDispatcher() { if (singleTaskDispatcher == null) { - singleTaskDispatcher = TaskDispatchers.createDefaultSingleTaskDispatcher( - TaskType.WATCHER_REGISTER_FETCH_TASK.getName(), dataNodeSingleTaskProcessor); + synchronized (this) { + if (singleTaskDispatcher == null) { + singleTaskDispatcher = TaskDispatchers + .createDefaultSingleTaskDispatcher( + TaskType.WATCHER_REGISTER_FETCH_TASK.getName(), + dataNodeSingleTaskProcessor); + } + } } return singleTaskDispatcher; } @Override - public boolean support(TaskEvent event) { - return TaskType.WATCHER_REGISTER_FETCH_TASK.equals(event.getTaskType()); + public TaskType support() { + return TaskType.WATCHER_REGISTER_FETCH_TASK; } @Override diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeService.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeService.java index 544c88010..030265299 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeService.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeService.java @@ -23,7 +23,6 @@ import com.alipay.sofa.registry.common.model.DatumSnapshotRequest; import com.alipay.sofa.registry.common.model.RenewDatumRequest; import com.alipay.sofa.registry.common.model.dataserver.Datum; -import com.alipay.sofa.registry.common.model.dataserver.SessionServerRegisterRequest; import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.common.model.store.URL; @@ -90,15 +89,6 @@ public interface DataNodeService { */ Map getDatumMap(String dataInfoId, String dataCenterId); - /** - * register session process id when connect to data node - * process id see SessionProcessIdGenerator - * @param sessionServerRegisterRequest - * @param dataUrl - */ - void registerSessionProcessId(SessionServerRegisterRequest sessionServerRegisterRequest, - URL dataUrl); - /** * check publisher digest same as session current store,and renew the lastUpdateTime of this connectId */ diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeServiceImpl.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeServiceImpl.java index afd2bf2ff..e46552c33 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeServiceImpl.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/node/service/DataNodeServiceImpl.java @@ -37,7 +37,6 @@ import com.alipay.sofa.registry.common.model.dataserver.GetDataRequest; import com.alipay.sofa.registry.common.model.dataserver.GetDataVersionRequest; import com.alipay.sofa.registry.common.model.dataserver.PublishDataRequest; -import com.alipay.sofa.registry.common.model.dataserver.SessionServerRegisterRequest; import com.alipay.sofa.registry.common.model.dataserver.UnPublishDataRequest; import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.common.model.store.URL; @@ -222,28 +221,6 @@ public AtomicInteger getRetryTimes() { }; } - @Override - public void registerSessionProcessId(final SessionServerRegisterRequest sessionServerRegisterRequest, - final URL dataUrl) { - try { - Request request = new Request() { - @Override - public SessionServerRegisterRequest getRequestBody() { - return sessionServerRegisterRequest; - } - - @Override - public URL getRequestUrl() { - return dataUrl; - } - }; - dataNodeExchanger.request(request); - } catch (RequestException e) { - throw new RuntimeException("DataNodeService register processId error! " - + e.getMessage(), e); - } - } - @Override public Map> fetchDataVersion(URL dataNodeUrl, Collection dataInfoIdList) { @@ -311,6 +288,7 @@ public Map getDatumMap(String dataInfoId, String dataCenterId) { Map map; try { + GetDataRequest getDataRequest = new GetDataRequest(); //dataCenter null means all dataCenters @@ -339,9 +317,9 @@ public URL getRequestUrl() { if (genericResponse.isSuccess()) { map = (Map) genericResponse.getData(); if (map == null || map.isEmpty()) { - LOGGER.warn("GetDataRequest get response contains no datum!dataInfoId={}", dataCenterId); + LOGGER.warn("GetDataRequest get response contains no datum!dataInfoId={}", dataInfoId); } else { - map.forEach((dataCenter, datum) -> Datum.processDatum(datum)); + map.forEach((dataCenter, datum) -> Datum.internDatum(datum)); } } else { throw new RuntimeException( diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessor.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessor.java new file mode 100644 index 000000000..91566fa98 --- /dev/null +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessor.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.registry.server.session.provideData; + +import com.alipay.sofa.registry.common.model.metaserver.ProvideData; + +/** + * + * @author shangyu.wh + * @version 1.0: ProvideDataProcessor.java, v 0.1 2019-10-09 17:26 shangyu.wh Exp $ + */ +public interface ProvideDataProcessor { + + void changeDataProcess(ProvideData provideData); + + void fetchDataProcess(ProvideData provideData); + + boolean support(ProvideData provideData); +} \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessorManager.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessorManager.java new file mode 100644 index 000000000..c68dafc46 --- /dev/null +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/ProvideDataProcessorManager.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.registry.server.session.provideData; + +import com.alipay.sofa.registry.common.model.metaserver.ProvideData; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * + * @author shangyu.wh + * @version 1.0: ProvideDataProcessorManager.java, v 0.1 2019-10-09 17:39 shangyu.wh Exp $ + */ +public class ProvideDataProcessorManager implements ProvideDataProcessor { + + private Collection provideDataProcessors = new ArrayList<>(); + + public void addProvideDataProcessor(ProvideDataProcessor provideDataProcessor) { + provideDataProcessors.add(provideDataProcessor); + } + + @Override + public void changeDataProcess(ProvideData provideData) { + for (ProvideDataProcessor provideDataProcessor : provideDataProcessors) { + if (provideDataProcessor.support(provideData)) { + provideDataProcessor.changeDataProcess(provideData); + } + } + } + + @Override + public void fetchDataProcess(ProvideData provideData) { + for (ProvideDataProcessor provideDataProcessor : provideDataProcessors) { + if (provideDataProcessor.support(provideData)) { + provideDataProcessor.fetchDataProcess(provideData); + } + } + } + + @Override + public boolean support(ProvideData provideData) { + return false; + } +} \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/BlackListProvideDataProcessor.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/BlackListProvideDataProcessor.java new file mode 100644 index 000000000..9a002962a --- /dev/null +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/BlackListProvideDataProcessor.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.registry.server.session.provideData.processor; + +import com.alipay.sofa.registry.common.model.constants.ValueConstants; +import com.alipay.sofa.registry.common.model.metaserver.ProvideData; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.net.NetUtil; +import com.alipay.sofa.registry.remoting.Channel; +import com.alipay.sofa.registry.remoting.Server; +import com.alipay.sofa.registry.remoting.exchange.Exchange; +import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; +import com.alipay.sofa.registry.server.session.filter.blacklist.BlacklistConstants; +import com.alipay.sofa.registry.server.session.filter.blacklist.BlacklistManager; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; +import com.alipay.sofa.registry.server.session.registry.Registry; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * + * @author shangyu.wh + * @version 1.0: BlackListProvideDataProcessor.java, v 0.1 2019-10-09 20:21 shangyu.wh Exp $ + */ +public class BlackListProvideDataProcessor implements ProvideDataProcessor { + + private static final Logger LOGGER = LoggerFactory + .getLogger(BlackListProvideDataProcessor.class); + + @Autowired + private SessionServerConfig sessionServerConfig; + + @Autowired + private Registry sessionRegistry; + + @Autowired + private Exchange boltExchange; + + @Autowired + private BlacklistManager blacklistManager; + + @Override + public void changeDataProcess(ProvideData provideData) { + //black list data + if (provideData.getProvideData() == null + || provideData.getProvideData().getObject() == null) { + LOGGER.info("Fetch session blacklist no data existed,current config not change!"); + return; + } + String data = (String) provideData.getProvideData().getObject(); + if (data != null) { + Map>> blacklistConfigMap = blacklistManager + .convertBlacklistConfig(data); + clientOffBlackIp(blacklistConfigMap); + LOGGER.info("Fetch session blacklist data switch {} success!", data); + } else { + LOGGER.info("Fetch session blacklist data null,current config not change!"); + } + return; + } + + private void clientOffBlackIp(Map>> blacklistConfigMap) { + + if (blacklistConfigMap != null) { + Set ipSet = new HashSet(); + + for (Map.Entry>> configEntry : blacklistConfigMap + .entrySet()) { + if (BlacklistConstants.FORBIDDEN_PUB.equals(configEntry.getKey()) + || BlacklistConstants.FORBIDDEN_SUB_BY_PREFIX.equals(configEntry.getKey())) { + Map> typeMap = configEntry.getValue(); + if (typeMap != null) { + for (Map.Entry> typeEntry : typeMap.entrySet()) { + if (BlacklistConstants.IP_FULL.equals(typeEntry.getKey())) { + if (typeEntry.getValue() != null) { + ipSet.addAll(typeEntry.getValue()); + } + } + } + } + } + + } + + sessionRegistry.remove(getIpConnects(ipSet)); + } + } + + public List getIpConnects(Set _ipList) { + + Server sessionServer = boltExchange.getServer(sessionServerConfig.getServerPort()); + + List connections = new ArrayList<>(); + + if (sessionServer != null) { + Collection channels = sessionServer.getChannels(); + for (Channel channel : channels) { + String key = NetUtil.toAddressString(channel.getRemoteAddress()); + String ip = key.substring(0, key.indexOf(":")); + if (_ipList.contains(ip)) { + connections.add(key); + } + } + } + + return connections; + } + + @Override + public void fetchDataProcess(ProvideData provideData) { + + } + + @Override + public boolean support(ProvideData provideData) { + return ValueConstants.BLACK_LIST_DATA_ID.equals(provideData.getDataInfoId()); + } +} \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/RenewSnapshotProvideDataProcessor.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/RenewSnapshotProvideDataProcessor.java new file mode 100644 index 000000000..6b829f0a3 --- /dev/null +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/RenewSnapshotProvideDataProcessor.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.registry.server.session.provideData.processor; + +import com.alipay.sofa.registry.common.model.constants.ValueConstants; +import com.alipay.sofa.registry.common.model.metaserver.ProvideData; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; +import com.alipay.sofa.registry.server.session.registry.Registry; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * + * @author shangyu.wh + * @version 1.0: RenewSnapshotProvideDataProcessor.java, v 0.1 2019-10-09 20:30 shangyu.wh Exp $ + */ +public class RenewSnapshotProvideDataProcessor implements ProvideDataProcessor { + + private static final Logger LOGGER = LoggerFactory + .getLogger(RenewSnapshotProvideDataProcessor.class); + + @Autowired + private Registry sessionRegistry; + + @Override + public void changeDataProcess(ProvideData provideData) { + //stop renew switch + if (provideData == null || provideData.getProvideData() == null + || provideData.getProvideData().getObject() == null) { + LOGGER + .info("Fetch enableDataRenewSnapshot but no data existed, current config not change!"); + return; + } + boolean enableDataRenewSnapshot = Boolean.parseBoolean((String) provideData + .getProvideData().getObject()); + LOGGER.info("Fetch enableDataRenewSnapshot {} success!", enableDataRenewSnapshot); + this.sessionRegistry.setEnableDataRenewSnapshot(enableDataRenewSnapshot); + return; + } + + @Override + public void fetchDataProcess(ProvideData provideData) { + if (provideData == null || provideData.getProvideData() == null + || provideData.getProvideData().getObject() == null) { + LOGGER + .info("Fetch enableDataRenewSnapshot but no data existed, current config not change!"); + return; + } + boolean enableDataRenewSnapshot = Boolean.parseBoolean((String) provideData + .getProvideData().getObject()); + LOGGER.info("Fetch enableDataRenewSnapshot {} success!", enableDataRenewSnapshot); + this.sessionRegistry.setEnableDataRenewSnapshot(enableDataRenewSnapshot); + } + + @Override + public boolean support(ProvideData provideData) { + return ValueConstants.ENABLE_DATA_RENEW_SNAPSHOT.equals(provideData.getDataInfoId()); + } +} \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/StopPushProvideDataProcessor.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/StopPushProvideDataProcessor.java new file mode 100644 index 000000000..316fae70f --- /dev/null +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/provideData/processor/StopPushProvideDataProcessor.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.registry.server.session.provideData.processor; + +import com.alipay.sofa.registry.common.model.constants.ValueConstants; +import com.alipay.sofa.registry.common.model.metaserver.ProvideData; +import com.alipay.sofa.registry.common.model.store.Subscriber; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; +import com.alipay.sofa.registry.server.session.registry.Registry; +import com.alipay.sofa.registry.server.session.scheduler.task.Constant; +import com.alipay.sofa.registry.server.session.store.Interests; +import com.alipay.sofa.registry.server.session.store.ReSubscribers; +import com.alipay.sofa.registry.task.listener.TaskEvent; +import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; +import com.alipay.sofa.registry.task.listener.TaskListenerManager; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.CollectionUtils; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * + * @author shangyu.wh + * @version 1.0: StopPushProvideDataProcessor.java, v 0.1 2019-10-09 18:53 shangyu.wh Exp $ + */ +public class StopPushProvideDataProcessor implements ProvideDataProcessor { + + private static final Logger TASK_LOGGER = LoggerFactory.getLogger( + StopPushProvideDataProcessor.class, "[Task]"); + + private static final Logger LOGGER = LoggerFactory + .getLogger(StopPushProvideDataProcessor.class); + + @Autowired + private SessionServerConfig sessionServerConfig; + + @Autowired + private Registry sessionRegistry; + + @Autowired + private Interests sessionInterests; + + @Autowired + private TaskListenerManager taskListenerManager; + + @Override + public void changeDataProcess(ProvideData provideData) { + //push stop switch + if (provideData != null) { + if (provideData.getProvideData() == null + || provideData.getProvideData().getObject() == null) { + LOGGER.info("Fetch session stop push switch no data existed,config not change!"); + return; + } + String data = (String) provideData.getProvideData().getObject(); + LOGGER.info("Fetch session stop push data switch {} success!", data); + + //receive stop push switch off + if (data != null) { + boolean switchData = Boolean.valueOf(data); + boolean ifChange = sessionServerConfig.isStopPushSwitch() != switchData; + sessionServerConfig.setStopPushSwitch(switchData); + if (!switchData) { + //avoid duplicate false receive + if (ifChange) { + fireReSubscriber(); + } + } else { + //stop push and stop fetch data task + sessionServerConfig.setBeginDataFetchTask(false); + } + } else { + LOGGER.error("Fetch session stop push data switch is null!"); + } + return; + } else { + LOGGER.info("Fetch session stop push switch data null,config not change!"); + } + return; + } + + /** + * open push switch to push all reSubscribers + */ + private void fireReSubscriber() { + + //try catch avoid to error cancel beginDataFetchTask switch on + try { + //begin push fire data fetch task first,avoid reSubscriber push duplicate + sessionRegistry.fetchChangDataProcess(); + } catch (Throwable e) { + LOGGER.error("Open push switch first fetch task execute error", e); + } + + try { + //wait 1 MINUTES for dataFetch task evict duplicate subscriber push + TimeUnit.MINUTES.sleep(1); + } catch (InterruptedException e) { + LOGGER.error("Wait for dataFetch Task Interrupted!"); + } + + //fetch task process 1 minutes,can schedule execute fetch task + sessionServerConfig.setBeginDataFetchTask(true); + + if (sessionInterests instanceof ReSubscribers) { + ReSubscribers reSubscriber = (ReSubscribers) sessionInterests; + + Map> reSubscribers = reSubscriber + .getReSubscribers(); + + if (reSubscribers != null && !reSubscribers.isEmpty()) { + reSubscribers.forEach( + (dataInfoId, subscribers) -> fireSubscriberMultiFetchTask(dataInfoId, subscribers.values())); + reSubscriber.clearReSubscribers(); + } + } + } + + private void fireSubscriberMultiFetchTask(String dataInfoId, Collection subscribers) { + //trigger fetch data for subscriber,and push to client node + if (!CollectionUtils.isEmpty(subscribers)) { + TaskEvent taskEvent = new TaskEvent(dataInfoId, TaskType.SUBSCRIBER_MULTI_FETCH_TASK); + taskEvent.setAttribute(Constant.PUSH_CLIENT_SUBSCRIBERS, subscribers); + TASK_LOGGER.info("send " + taskEvent.getTaskType() + + " subscribersSize:{},dataInfoId:{}", subscribers.size(), dataInfoId); + taskListenerManager.sendTaskEvent(taskEvent); + } + } + + @Override + public void fetchDataProcess(ProvideData provideData) { + if (provideData.getProvideData() == null + || provideData.getProvideData().getObject() == null) { + LOGGER.info("Fetch session stop push switch no data existed,config not change!"); + return; + } + String data = (String) provideData.getProvideData().getObject(); + sessionServerConfig.setStopPushSwitch(Boolean.valueOf(data)); + if (data != null) { + if (!Boolean.valueOf(data)) { + //stop push init on,then begin fetch data schedule task + sessionServerConfig.setBeginDataFetchTask(true); + } + } + LOGGER.info("Fetch session stop push data switch {} success!", data); + } + + @Override + public boolean support(ProvideData provideData) { + return ValueConstants.STOP_PUSH_DATA_SWITCH_DATA_ID.equals(provideData.getDataInfoId()); + } +} \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/DataNodeExchanger.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/DataNodeExchanger.java index c3b2e2aef..30e17ab97 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/DataNodeExchanger.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/DataNodeExchanger.java @@ -17,6 +17,7 @@ package com.alipay.sofa.registry.server.session.remoting; import java.util.Collection; +import java.util.StringJoiner; import javax.annotation.Resource; @@ -27,7 +28,6 @@ import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.remoting.Channel; import com.alipay.sofa.registry.remoting.ChannelHandler; import com.alipay.sofa.registry.remoting.Client; import com.alipay.sofa.registry.remoting.exchange.Exchange; @@ -72,21 +72,7 @@ public Response request(Request request) throws RequestException { Response response; URL url = request.getRequestUrl(); try { - Client sessionClient = boltExchange.getClient(Exchange.DATA_SERVER_TYPE); - - if (sessionClient == null) { - LOGGER.warn( - "DataNode Exchanger get dataServer connection {} error! Connection can not be null or disconnected!", - url); - //first start session maybe case sessionClient null,try to auto connect - sessionClient = boltExchange.connect(Exchange.DATA_SERVER_TYPE, url, - dataClientHandlers.toArray(new ChannelHandler[dataClientHandlers.size()])); - } - //try to connect data - Channel channel = sessionClient.getChannel(url); - if (channel == null) { - channel = sessionClient.connect(url); - } + Client sessionClient = getClient(url); // print but ignore if from renew module, cause renew request is too much if (!(request.getRequestBody() instanceof RenewDatumRequest)) { @@ -94,7 +80,7 @@ public Response request(Request request) throws RequestException { } final Object result = sessionClient - .sendSync(channel, request.getRequestBody(), sessionServerConfig.getDataNodeExchangeTimeOut()); + .sendSync(url, request.getRequestBody(), sessionServerConfig.getDataNodeExchangeTimeOut()); if (result == null) { throw new RequestException("DataNode Exchanger request data get null result!", request); } @@ -109,7 +95,7 @@ public Response request(Request request) throws RequestException { } @Override - public Client connectServer() { + public synchronized Client connectServer() { Collection dataNodes = dataNodeManager.getDataCenterNodes(); if (dataNodes == null || dataNodes.isEmpty()) { @@ -117,7 +103,11 @@ public Client connectServer() { dataNodes = dataNodeManager.getDataCenterNodes(); } + boolean connectedAll = true; + StringJoiner errorMsg = new StringJoiner(";"); + Client dataClient = null; + for (Node dataNode : dataNodes) { if (dataNode.getNodeUrl() == null || dataNode.getNodeUrl().getIpAddress() == null) { LOGGER.error("get data node address error!url{}", dataNode.getNodeUrl()); @@ -127,25 +117,33 @@ public Client connectServer() { URL url = new URL(dataNode.getNodeUrl().getIpAddress(), sessionServerConfig.getDataServerPort()); try { - dataClient = boltExchange.getClient(Exchange.DATA_SERVER_TYPE); - if (dataClient == null) { - dataClient = boltExchange.connect(Exchange.DATA_SERVER_TYPE, url, - dataClientHandlers.toArray(new ChannelHandler[dataClientHandlers.size()])); - } + dataClient = getClient(url); + // make sure there are connections to DataServer + dataClient.connect(url); } catch (Exception e) { - LOGGER.error("DataNode Exchanger connect DataServer error!url:" + url, e); + String msg = "DataNode Exchanger connect DataServer error!url:" + url; + LOGGER.error(msg, e); + connectedAll = false; + errorMsg.add(msg); continue; } + } - try { - Channel channel = dataClient.getChannel(url); - if (channel == null) { - dataClient.connect(url); - } - } catch (Exception e) { - LOGGER.error("DataNode Exchanger connect channel error!url:" + url, e); - } + if (!connectedAll) { + throw new RuntimeException("Data server connected server error: " + errorMsg.toString()); } return dataClient; } + + protected Client getClient(URL url) { + Client sessionClient = boltExchange.getClient(Exchange.DATA_SERVER_TYPE); + if (sessionClient == null) { + //first start session maybe case sessionClient null,try to auto connect + sessionClient = boltExchange.connect(Exchange.DATA_SERVER_TYPE, + sessionServerConfig.getDataClientConnNum(), url, + dataClientHandlers.toArray(new ChannelHandler[dataClientHandlers.size()])); + } + return sessionClient; + } + } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/MetaNodeExchanger.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/MetaNodeExchanger.java index 5f66bf863..1d2274be7 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/MetaNodeExchanger.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/MetaNodeExchanger.java @@ -16,6 +16,12 @@ */ package com.alipay.sofa.registry.server.session.remoting; +import java.util.Collection; + +import javax.annotation.Resource; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.metaserver.MetaNode; import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; @@ -32,10 +38,6 @@ import com.alipay.sofa.registry.server.session.node.NodeManager; import com.alipay.sofa.registry.server.session.node.RaftClientManager; import com.alipay.sofa.registry.server.session.remoting.handler.AbstractClientHandler; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.annotation.Resource; -import java.util.Collection; /** * The type Data node exchanger. @@ -80,14 +82,9 @@ public Response request(Request request) throws RequestException { sessionClient = boltExchange.connect(Exchange.META_SERVER_TYPE, url, metaClientHandlers.toArray(new ChannelHandler[metaClientHandlers.size()])); } - //try to connect data - Channel channel = sessionClient.getChannel(url); - if (channel == null) { - channel = sessionClient.connect(url); - } try { - final Object result = sessionClient.sendSync(channel, request.getRequestBody(), + final Object result = sessionClient.sendSync(url, request.getRequestBody(), sessionServerConfig.getDataNodeExchangeTimeOut()); response = () -> result; @@ -95,13 +92,9 @@ public Response request(Request request) throws RequestException { //retry url = new URL(raftClientManager.refreshLeader().getIp(), sessionServerConfig.getMetaServerPort()); - channel = sessionClient.getChannel(url); - if (channel == null) { - channel = sessionClient.connect(url); - } LOGGER.warn("MetaNode Exchanger request send error!It will be retry once!Request url:{}", url); - final Object result = sessionClient.sendSync(channel, request.getRequestBody(), + final Object result = sessionClient.sendSync(url, request.getRequestBody(), sessionServerConfig.getDataNodeExchangeTimeOut()); response = () -> result; } @@ -115,7 +108,7 @@ public Response request(Request request) throws RequestException { } @Override - public Client connectServer() { + public synchronized Client connectServer() { Collection nodes = metaNodeManager.getDataCenterNodes(); if (nodes == null || nodes.isEmpty()) { diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataChangeRequestHandler.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataChangeRequestHandler.java index bc70a50c9..cc1e4a509 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataChangeRequestHandler.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataChangeRequestHandler.java @@ -16,25 +16,23 @@ */ package com.alipay.sofa.registry.server.session.remoting.handler; +import java.util.concurrent.Executor; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.Node.NodeType; import com.alipay.sofa.registry.common.model.sessionserver.DataChangeRequest; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; -import com.alipay.sofa.registry.server.session.cache.CacheService; -import com.alipay.sofa.registry.server.session.cache.DatumKey; -import com.alipay.sofa.registry.server.session.cache.Key; -import com.alipay.sofa.registry.server.session.cache.Key.KeyType; import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; import com.alipay.sofa.registry.server.session.store.Interests; import com.alipay.sofa.registry.server.session.strategy.DataChangeRequestHandlerStrategy; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.concurrent.Executor; /** * + * @author kezhu.wukz * @author shangyu.wh * @version $Id: DataChangeRequestHandler.java, v 0.1 2017-12-12 15:09 shangyu.wh Exp $ */ @@ -58,9 +56,6 @@ public class DataChangeRequestHandler extends AbstractClientHandler { @Autowired private ExecutorManager executorManager; - @Autowired - private CacheService sessionCacheService; - @Autowired private DataChangeRequestHandlerStrategy dataChangeRequestHandlerStrategy; @@ -81,24 +76,17 @@ public Executor getExecutor() { @Override public Object reply(Channel channel, Object message) { - - DataChangeRequest dataChangeRequest = (DataChangeRequest) message; - - dataChangeRequest.setDataCenter(dataChangeRequest.getDataCenter()); - dataChangeRequest.setDataInfoId(dataChangeRequest.getDataInfoId()); - - //update cache when change - sessionCacheService.invalidate(new Key(KeyType.OBJ, DatumKey.class.getName(), new DatumKey( - dataChangeRequest.getDataInfoId(), dataChangeRequest.getDataCenter()))); - if (sessionServerConfig.isStopPushSwitch()) { - LOGGER.info("Stop Push data with switch on,dataChangeRequest: {}", dataChangeRequest); return null; } + + DataChangeRequest dataChangeRequest = (DataChangeRequest) message; + try { boolean result = sessionInterests.checkInterestVersions( dataChangeRequest.getDataCenter(), dataChangeRequest.getDataInfoId(), dataChangeRequest.getVersion()); + if (!result) { return null; } diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataNodeConnectionHandler.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataNodeConnectionHandler.java index e5f29820a..3e1ed841c 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataNodeConnectionHandler.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/DataNodeConnectionHandler.java @@ -16,8 +16,9 @@ */ package com.alipay.sofa.registry.server.session.remoting.handler; +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.Node.NodeType; -import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.remoting.Channel; @@ -26,7 +27,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -52,7 +52,7 @@ public HandlerType getType() { @Override public void connected(Channel channel) throws RemotingException { super.connected(channel); - fireRegisterProcessIdTask(new URL(channel.getRemoteAddress())); + fireRegisterProcessIdTask(channel); } @Override @@ -60,8 +60,8 @@ protected NodeType getConnectNodeType() { return NodeType.DATA; } - private void fireRegisterProcessIdTask(URL dataURL) { - TaskEvent taskEvent = new TaskEvent(dataURL, TaskType.SESSION_REGISTER_DATA_TASK); + private void fireRegisterProcessIdTask(Channel boltChannel) { + TaskEvent taskEvent = new TaskEvent(boltChannel, TaskType.SESSION_REGISTER_DATA_TASK); taskLogger.info("send " + taskEvent.getTaskType() + " taskEvent:{}", taskEvent); taskListenerManager.sendTaskEvent(taskEvent); } diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/WatcherHandler.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/WatcherHandler.java index 07fd647ab..aa8825973 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/WatcherHandler.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/remoting/handler/WatcherHandler.java @@ -16,11 +16,15 @@ */ package com.alipay.sofa.registry.server.session.remoting.handler; +import java.util.concurrent.Executor; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.core.model.ConfiguratorRegister; import com.alipay.sofa.registry.core.model.RegisterResponse; import com.alipay.sofa.registry.remoting.Channel; +import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; import com.alipay.sofa.registry.server.session.strategy.WatcherHandlerStrategy; -import org.springframework.beans.factory.annotation.Autowired; /** * @@ -28,6 +32,9 @@ * @version $Id: SubscriberHandler.java, v 0.1 2017-11-30 15:01 shangyu.wh Exp $ */ public class WatcherHandler extends AbstractServerHandler { + @Autowired + private ExecutorManager executorManager; + @Autowired private WatcherHandlerStrategy watcherHandlerStrategy; @@ -49,4 +56,9 @@ public Class interest() { return ConfiguratorRegister.class; } + @Override + public Executor getExecutor() { + return executorManager.getAccessDataExecutor(); + } + } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/ExecutorManager.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/ExecutorManager.java index 13ab127a1..ec6c6876b 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/ExecutorManager.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/ExecutorManager.java @@ -66,6 +66,7 @@ public class ExecutorManager { private final ThreadPoolExecutor pushTaskExecutor; private final ThreadPoolExecutor connectClientExecutor; private final ThreadPoolExecutor publishDataExecutor; + private final ThreadPoolExecutor cleanInvalidClientExecutor; private final AsyncHashedWheelTimer pushTaskCheckAsyncHashedWheelTimer; @@ -109,34 +110,41 @@ public ExecutorManager(SessionServerConfig sessionServerConfig) { this.sessionServerConfig = sessionServerConfig; - scheduler = new ScheduledThreadPoolExecutor(7, new NamedThreadFactory("SessionScheduler")); + scheduler = new ScheduledThreadPoolExecutor(8, new NamedThreadFactory("SessionScheduler")); - fetchDataExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, - new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-fetchData")); + fetchDataExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, new SynchronousQueue<>(), + new NamedThreadFactory("SessionScheduler-fetchData")); renNewDataExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, - new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-renewData")); + new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-reNewData")); - getSessionNodeExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, - new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-getSessionNode")); + getSessionNodeExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, new SynchronousQueue<>(), + new NamedThreadFactory("SessionScheduler-getSessionNode")); standaloneCheckVersionExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-standaloneCheckVersion")); - connectMetaExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, - new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-connectMetaServer")); + connectMetaExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, new SynchronousQueue<>(), + new NamedThreadFactory("SessionScheduler-connectMetaServer")); - connectDataExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, - new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-connectDataServer")); + connectDataExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, new SynchronousQueue<>(), + new NamedThreadFactory("SessionScheduler-connectDataServer")); - accessDataExecutor = reportExecutors - .computeIfAbsent(ACCESS_DATA_EXECUTOR, k -> new SessionThreadPoolExecutor(ACCESS_DATA_EXECUTOR, + cleanInvalidClientExecutor = new ThreadPoolExecutor(1, 2/*CONFIG*/, 0, TimeUnit.SECONDS, + new SynchronousQueue<>(), new NamedThreadFactory("SessionScheduler-cleanInvalidClient")); + + accessDataExecutor = reportExecutors.computeIfAbsent(ACCESS_DATA_EXECUTOR, + k -> new SessionThreadPoolExecutor(ACCESS_DATA_EXECUTOR, sessionServerConfig.getAccessDataExecutorMinPoolSize(), sessionServerConfig.getAccessDataExecutorMaxPoolSize(), - sessionServerConfig.getAccessDataExecutorKeepAliveTime(), - TimeUnit.SECONDS, + sessionServerConfig.getAccessDataExecutorKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(sessionServerConfig.getAccessDataExecutorQueueSize()), - new NamedThreadFactory("AccessData-executor", true))); + new NamedThreadFactory("AccessData-executor", true), (r, executor) -> { + String msg = String + .format("Task(%s) %s rejected from %s, just ignore it to let client timeout.", + r.getClass(), r, executor); + LOGGER.error(msg); + })); pushTaskExecutor = reportExecutors.computeIfAbsent(PUSH_TASK_EXECUTOR, k -> new ThreadPoolExecutor(sessionServerConfig.getPushTaskExecutorMinPoolSize(), @@ -151,32 +159,29 @@ public ExecutorManager(SessionServerConfig sessionServerConfig) { k -> new SessionThreadPoolExecutor(DATA_CHANGE_REQUEST_EXECUTOR, sessionServerConfig.getDataChangeExecutorMinPoolSize(), sessionServerConfig.getDataChangeExecutorMaxPoolSize(), - sessionServerConfig.getDataChangeExecutorKeepAliveTime(), - TimeUnit.SECONDS, + sessionServerConfig.getDataChangeExecutorKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(sessionServerConfig.getDataChangeExecutorQueueSize()), - new NamedThreadFactory( - "DataChangeRequestHandler-executor", true))); - - checkPushExecutor = reportExecutors - .computeIfAbsent(USER_DATA_ELEMENT_PUSH_TASK_CHECK_EXECUTOR, k -> new SessionThreadPoolExecutor( - USER_DATA_ELEMENT_PUSH_TASK_CHECK_EXECUTOR, 100, 600, 60L, - TimeUnit.SECONDS, - new LinkedBlockingQueue(100000), + new NamedThreadFactory("DataChangeRequestHandler-executor", true))); + + checkPushExecutor = reportExecutors.computeIfAbsent(USER_DATA_ELEMENT_PUSH_TASK_CHECK_EXECUTOR, + k -> new SessionThreadPoolExecutor(USER_DATA_ELEMENT_PUSH_TASK_CHECK_EXECUTOR, 100, 600, 60L, + TimeUnit.SECONDS, new LinkedBlockingQueue(150000), new NamedThreadFactory("UserDataElementPushCheck-executor", true))); - connectClientExecutor = reportExecutors.computeIfAbsent(CONNECT_CLIENT_EXECUTOR,k->new SessionThreadPoolExecutor( - CONNECT_CLIENT_EXECUTOR, sessionServerConfig.getConnectClientExecutorMinPoolSize(), - sessionServerConfig.getConnectClientExecutorMaxPoolSize(), 60L, - TimeUnit.SECONDS, - new LinkedBlockingQueue(sessionServerConfig.getConnectClientExecutorQueueSize()), - new NamedThreadFactory("DisconnectClientExecutor", true))); + connectClientExecutor = reportExecutors.computeIfAbsent(CONNECT_CLIENT_EXECUTOR, + k -> new SessionThreadPoolExecutor(CONNECT_CLIENT_EXECUTOR, + sessionServerConfig.getConnectClientExecutorMinPoolSize(), + sessionServerConfig.getConnectClientExecutorMaxPoolSize(), 60L, TimeUnit.SECONDS, + new LinkedBlockingQueue(sessionServerConfig.getConnectClientExecutorQueueSize()), + new NamedThreadFactory("DisconnectClientExecutor", true))); - pushTaskCheckAsyncHashedWheelTimer = new AsyncHashedWheelTimer(new NamedThreadFactory("PushTaskConfirmCheck-executor", true), + pushTaskCheckAsyncHashedWheelTimer = new AsyncHashedWheelTimer( + new NamedThreadFactory("PushTaskConfirmCheck-executor", true), sessionServerConfig.getPushTaskConfirmCheckWheelTicksDuration(), TimeUnit.MILLISECONDS, sessionServerConfig.getPushTaskConfirmCheckWheelTicksSize(), sessionServerConfig.getPushTaskConfirmCheckExecutorThreadSize(), - sessionServerConfig.getPushTaskConfirmCheckExecutorQueueSize(), new ThreadFactoryBuilder() - .setNameFormat("PushTaskConfirmCheck-executor-%d").build(), + sessionServerConfig.getPushTaskConfirmCheckExecutorQueueSize(), + new ThreadFactoryBuilder().setNameFormat("PushTaskConfirmCheck-executor-%d").build(), new TaskFailedCallback() { @Override public void executionRejected(Throwable e) { @@ -188,55 +193,50 @@ public void executionFailed(Throwable e) { LOGGER.error("executionFailed: " + e.getMessage(), e); } }); - publishDataExecutor = reportExecutors - .computeIfAbsent(PUBLISH_DATA_EXECUTOR, k -> new SessionThreadPoolExecutor(PUBLISH_DATA_EXECUTOR, + publishDataExecutor = reportExecutors.computeIfAbsent(PUBLISH_DATA_EXECUTOR, + k -> new SessionThreadPoolExecutor(PUBLISH_DATA_EXECUTOR, sessionServerConfig.getPublishDataExecutorMinPoolSize(), sessionServerConfig.getPublishDataExecutorMaxPoolSize(), - sessionServerConfig.getPublishDataExecutorKeepAliveTime(), - TimeUnit.SECONDS, + sessionServerConfig.getPublishDataExecutorKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(sessionServerConfig.getPublishDataExecutorQueueSize()), new NamedThreadFactory("PublishData-executor", true))); } public void startScheduler() { - scheduler.schedule( - new TimedSupervisorTask("FetchData", scheduler, fetchDataExecutor, + scheduler.schedule(new TimedSupervisorTask("FetchData", scheduler, fetchDataExecutor, sessionServerConfig.getSchedulerFetchDataTimeout(), TimeUnit.MINUTES, - sessionServerConfig.getSchedulerFetchDataExpBackOffBound(), - () -> sessionRegistry.fetchChangData()), + sessionServerConfig.getSchedulerFetchDataExpBackOffBound(), () -> sessionRegistry.fetchChangData()), sessionServerConfig.getSchedulerFetchDataFirstDelay(), TimeUnit.SECONDS); - scheduler.schedule( - new TimedSupervisorTask("RenewData", scheduler, renNewDataExecutor, + scheduler.schedule(new TimedSupervisorTask("RenewData", scheduler, renNewDataExecutor, sessionServerConfig.getSchedulerHeartbeatTimeout(), TimeUnit.SECONDS, - sessionServerConfig.getSchedulerHeartbeatExpBackOffBound(), - () -> sessionNodeManager.renewNode()), + sessionServerConfig.getSchedulerHeartbeatExpBackOffBound(), () -> sessionNodeManager.renewNode()), sessionServerConfig.getSchedulerHeartbeatFirstDelay(), TimeUnit.SECONDS); - scheduler.schedule( - new TimedSupervisorTask("GetSessionNode", scheduler, getSessionNodeExecutor, - sessionServerConfig.getSchedulerGetSessionNodeTimeout(), TimeUnit.SECONDS, - sessionServerConfig.getSchedulerGetSessionNodeExpBackOffBound(), - () -> { - sessionNodeManager.getAllDataCenterNodes(); - dataNodeManager.getAllDataCenterNodes(); - metaNodeManager.getAllDataCenterNodes(); - }), - sessionServerConfig.getSchedulerGetSessionNodeFirstDelay(), TimeUnit.SECONDS); + scheduler.schedule(new TimedSupervisorTask("GetSessionNode", scheduler, getSessionNodeExecutor, + sessionServerConfig.getSchedulerGetSessionNodeTimeout(), TimeUnit.SECONDS, + sessionServerConfig.getSchedulerGetSessionNodeExpBackOffBound(), () -> { + sessionNodeManager.getAllDataCenterNodes(); + dataNodeManager.getAllDataCenterNodes(); + metaNodeManager.getAllDataCenterNodes(); + }), sessionServerConfig.getSchedulerGetSessionNodeFirstDelay(), TimeUnit.SECONDS); - scheduler.schedule( - new TimedSupervisorTask("ConnectMetaServer", scheduler, connectMetaExecutor, + scheduler.schedule(new TimedSupervisorTask("ConnectMetaServer", scheduler, connectMetaExecutor, sessionServerConfig.getSchedulerConnectMetaTimeout(), TimeUnit.SECONDS, - sessionServerConfig.getSchedulerConnectMetaExpBackOffBound(), - () -> metaNodeExchanger.connectServer()), + sessionServerConfig.getSchedulerConnectMetaExpBackOffBound(), () -> metaNodeExchanger.connectServer()), sessionServerConfig.getSchedulerConnectMetaFirstDelay(), TimeUnit.SECONDS); - scheduler.schedule( - new TimedSupervisorTask("ConnectDataServer", scheduler, connectDataExecutor, + scheduler.schedule(new TimedSupervisorTask("ConnectDataServer", scheduler, connectDataExecutor, sessionServerConfig.getSchedulerConnectDataTimeout(), TimeUnit.SECONDS, - sessionServerConfig.getSchedulerConnectDataExpBackOffBound(), - () -> dataNodeExchanger.connectServer()), + sessionServerConfig.getSchedulerConnectDataExpBackOffBound(), () -> dataNodeExchanger.connectServer()), sessionServerConfig.getSchedulerConnectDataFirstDelay(), TimeUnit.SECONDS); + + scheduler.schedule( + new TimedSupervisorTask("CleanInvalidClient", scheduler, cleanInvalidClientExecutor, + sessionServerConfig.getSchedulerCleanInvalidClientTimeOut(), TimeUnit.MINUTES, + sessionServerConfig.getSchedulerCleanInvalidClientBackOffBound(), + () -> sessionRegistry.cleanClientConnect()), + sessionServerConfig.getSchedulerCleanInvalidClientFirstDelay(), TimeUnit.MINUTES); } public void stopScheduler() { diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/SessionThreadPoolExecutor.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/SessionThreadPoolExecutor.java index 18652a22c..5887c2bc7 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/SessionThreadPoolExecutor.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/SessionThreadPoolExecutor.java @@ -16,16 +16,17 @@ */ package com.alipay.sofa.registry.server.session.scheduler; -import com.alipay.sofa.registry.log.Logger; -import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.metrics.TaskMetrics; - import java.util.concurrent.BlockingQueue; import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.metrics.TaskMetrics; + /** * * @author shangyu.wh @@ -39,34 +40,32 @@ public class SessionThreadPoolExecutor extends ThreadPoolExecutor { public SessionThreadPoolExecutor(String executorName, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, - BlockingQueue workQueue, ThreadFactory threadFactory) { + BlockingQueue workQueue, + ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); this.executorName = executorName; registerTaskMetrics(); + this.setRejectedExecutionHandler(handler); } - private void registerTaskMetrics() { + public SessionThreadPoolExecutor(String executorName, int corePoolSize, int maximumPoolSize, long keepAliveTime, + TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) { + this(executorName, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, + (r, executor) -> { + String msg = String.format("Task(%s) %s rejected from %s, throw RejectedExecutionException.", + r.getClass(), r, executor); + LOGGER.error(msg); + throw new RejectedExecutionException(msg); + }); + } + private void registerTaskMetrics() { TaskMetrics.getInstance().registerThreadExecutor(executorName, this); } @Override public String toString() { - return (new StringBuilder(executorName).append(" ").append(super.toString())).toString(); } - @Override - public void execute(Runnable command) { - - try { - super.execute(command); - } catch (Exception e) { - if (e instanceof RejectedExecutionException) { - LOGGER.error("Processor session executor {} Rejected Execution!command {}", this, - command.getClass(), e); - } - throw e; - } - } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/AbstractSessionTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/AbstractSessionTask.java index 47d9e853b..ae41f743e 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/AbstractSessionTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/AbstractSessionTask.java @@ -16,16 +16,17 @@ */ package com.alipay.sofa.registry.server.session.scheduler.task; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; + import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.task.Retryable; -import java.util.UUID; -import java.util.concurrent.atomic.AtomicInteger; - /** * * @author shangyu.wh + * @author kezhu.wukz * @version $Id: AbstractSessionTask.java, v 0.1 2018-01-15 14:35 shangyu.wh Exp $ */ public abstract class AbstractSessionTask implements SessionTask, Retryable { @@ -62,6 +63,10 @@ protected boolean checkRetryTimes(int configTimes) { return false; } + protected int getExecCount() { + return execCount.get(); + } + @Override public long getExpiryTime() { return -1; diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchCloudTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchCloudTask.java index 29c00dca3..fcbefe679 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchCloudTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchCloudTask.java @@ -16,15 +16,6 @@ */ package com.alipay.sofa.registry.server.session.scheduler.task; -import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - import com.alipay.sofa.registry.common.model.Node.NodeType; import com.alipay.sofa.registry.common.model.dataserver.Datum; import com.alipay.sofa.registry.common.model.store.BaseInfo.ClientVersion; @@ -52,6 +43,15 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListenerManager; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + /** * * @author shangyu.wh @@ -152,6 +152,7 @@ public void execute() { } public PushTaskClosure getTaskClosure(Map datumMap) { + PushTaskClosure pushTaskClosure = new PushTaskClosure(executorManager.getPushTaskCheckAsyncHashedWheelTimer(), sessionServerConfig, fetchDataInfoId); pushTaskClosure.setTaskClosure((status, task) -> { @@ -205,6 +206,9 @@ private Map getDatumsCache() { new DatumKey(fetchDataInfoId, dataCenter))). collect(Collectors.toList()); + // remove cache + keys.stream().forEach(key -> sessionCacheService.invalidate(key)); + Map values = null; try { values = sessionCacheService.getValues(keys); diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchTask.java index 626088eb5..1160686eb 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataChangeFetchTask.java @@ -100,7 +100,7 @@ public void execute() { Datum datum = getDatumCache(); if (datum != null) { - PushTaskClosure pushTaskClosure = getTaskClosure(); + PushTaskClosure pushTaskClosure = getTaskClosure(datum.getVersion()); for (ScopeEnum scopeEnum : ScopeEnum.values()) { Map> map = getCache(scopeEnum); @@ -188,33 +188,33 @@ private Collection subscribersVersionCheck(Collection su return subscribersSend; } - public PushTaskClosure getTaskClosure() { + public PushTaskClosure getTaskClosure(Long version) { //this for all this dataInfoId push result get and call back to change version PushTaskClosure pushTaskClosure = new PushTaskClosure(executorManager.getPushTaskCheckAsyncHashedWheelTimer(), sessionServerConfig, dataChangeRequest.getDataInfoId()); pushTaskClosure.setTaskClosure((status, task) -> { String dataCenter = dataChangeRequest.getDataCenter(); String dataInfoId = dataChangeRequest.getDataInfoId(); - Long version = dataChangeRequest.getVersion(); + Long changeVersion = dataChangeRequest.getVersion(); if (status == ProcessingResult.Success) { if (sessionServerConfig.isStopPushSwitch()) { - LOGGER.info("Stop Push switch on,dataCenter {} dataInfoId {} version {} can not be update!", - dataCenter, dataInfoId, version); + LOGGER.info("Stop Push switch on, dataCenter:{}, dataInfoId:{}, changeVersion:{}, pushVersion:{}, can not be update!", + dataCenter, dataInfoId, changeVersion, version); return; } boolean result = sessionInterests.checkAndUpdateInterestVersions(dataCenter, dataInfoId, version); if (result) { - LOGGER.info("Push all tasks success,dataCenter:{} dataInfoId:{} version:{} update!", dataCenter, - dataInfoId, version); + LOGGER.info("Push all tasks success, dataCenter:{}, dataInfoId:{}, changeVersion:{}, pushVersion:{}, update!", dataCenter, + dataInfoId, changeVersion, version); } else { - LOGGER.info("Push all tasks success,but dataCenter:{} dataInfoId:{} version:{} need not update!", - dataCenter, dataInfoId, version); + LOGGER.info("Push all tasks success, but dataCenter:{}, dataInfoId:{}, changeVersion:{}, pushVersion:{}, need not update!", + dataCenter, dataInfoId, changeVersion, version); } } else { LOGGER.warn( - "Push tasks found error,subscribers version can not be update!dataCenter:{} dataInfoId:{} version:{}", - dataCenter, dataInfoId, version); + "Push tasks found error, subscribers version can not be update! dataCenter:{}, dataInfoId:{}, changeVersion:{}, pushVersion:{}", + dataCenter, dataInfoId, changeVersion, version); } }); return pushTaskClosure; @@ -237,7 +237,7 @@ private void fireReceivedDataMultiPushTask(Datum datum, List subscriberR // zone scope subscribe only return zone list return true; - } else if (ScopeEnum.dataCenter == scopeEnum) { + } else if (ScopeEnum.dataCenter == scopeEnum || ScopeEnum.global == scopeEnum) { // disable zone config return sessionServerConfig.isInvalidForeverZone(zone) && !sessionServerConfig .isInvalidIgnored(dataId); @@ -265,10 +265,15 @@ private Map> getCache(ScopeEnum scope } private Datum getDatumCache() { + // build key DatumKey datumKey = new DatumKey(dataChangeRequest.getDataInfoId(), dataChangeRequest.getDataCenter()); - Key key = new Key(KeyType.OBJ, datumKey.getClass().getName(), datumKey); + Key key = new Key(KeyType.OBJ, DatumKey.class.getName(), datumKey); + // remove cache + sessionCacheService.invalidate(key); + + // get from cache (it will fetch from backend server) Value value = null; try { value = sessionCacheService.getValue(key); @@ -326,7 +331,6 @@ public long getExpiryTime() { @Override public void setTaskEvent(TaskEvent taskEvent) { - //taskId create from event if (taskEvent.getTaskId() != null) { setTaskId(taskEvent.getTaskId()); diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataPushTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataPushTask.java index 8e768b46f..8e57586be 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataPushTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/DataPushTask.java @@ -16,6 +16,15 @@ */ package com.alipay.sofa.registry.server.session.scheduler.task; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.Predicate; + import com.alipay.sofa.registry.common.model.dataserver.Datum; import com.alipay.sofa.registry.common.model.sessionserver.DataPushRequest; import com.alipay.sofa.registry.common.model.store.BaseInfo.ClientVersion; @@ -33,15 +42,6 @@ import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.Predicate; - /** * * @author shangyu.wh @@ -154,7 +154,7 @@ private void fireReceivedDataMultiPushTask(Datum datum, List subscriberR // zone scope subscribe only return zone list return true; - } else if (ScopeEnum.dataCenter == scopeEnum) { + } else if (ScopeEnum.dataCenter == scopeEnum || ScopeEnum.global == scopeEnum) { // disable zone config if (sessionServerConfig.isInvalidForeverZone(zone) && !sessionServerConfig.isInvalidIgnored(dataId)) { diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ProvideDataChangeFetchTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ProvideDataChangeFetchTask.java index 3f398d1aa..351068da0 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ProvideDataChangeFetchTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ProvideDataChangeFetchTask.java @@ -16,24 +16,10 @@ */ package com.alipay.sofa.registry.server.session.scheduler.task; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -import org.springframework.util.CollectionUtils; - -import com.alipay.sofa.registry.common.model.constants.ValueConstants; import com.alipay.sofa.registry.common.model.metaserver.DataOperator; import com.alipay.sofa.registry.common.model.metaserver.NotifyProvideDataChange; import com.alipay.sofa.registry.common.model.metaserver.ProvideData; import com.alipay.sofa.registry.common.model.store.DataInfo; -import com.alipay.sofa.registry.common.model.store.Subscriber; import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.common.model.store.Watcher; import com.alipay.sofa.registry.core.model.ReceivedConfigData; @@ -45,17 +31,19 @@ import com.alipay.sofa.registry.remoting.exchange.Exchange; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.converter.ReceivedDataConverter; -import com.alipay.sofa.registry.server.session.filter.blacklist.BlacklistConstants; -import com.alipay.sofa.registry.server.session.filter.blacklist.BlacklistManager; import com.alipay.sofa.registry.server.session.node.service.MetaNodeService; -import com.alipay.sofa.registry.server.session.registry.Registry; -import com.alipay.sofa.registry.server.session.store.Interests; -import com.alipay.sofa.registry.server.session.store.ReSubscribers; +import com.alipay.sofa.registry.server.session.provideData.ProvideDataProcessor; import com.alipay.sofa.registry.server.session.store.Watchers; import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskEvent.TaskType; import com.alipay.sofa.registry.task.listener.TaskListenerManager; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * * @author shangyu.wh @@ -83,27 +71,21 @@ public class ProvideDataChangeFetchTask extends AbstractSessionTask { private final Exchange boltExchange; - private final Interests sessionInterests; - - private final Registry sessionRegistry; - - private final BlacklistManager blacklistManager; - private NotifyProvideDataChange notifyProvideDataChange; + private ProvideDataProcessor provideDataProcessorManager; + public ProvideDataChangeFetchTask(SessionServerConfig sessionServerConfig, TaskListenerManager taskListenerManager, MetaNodeService metaNodeService, Watchers sessionWatchers, - Exchange boltExchange, Interests sessionInterests, - Registry sessionRegistry, BlacklistManager blacklistManager) { + Exchange boltExchange, + ProvideDataProcessor provideDataProcessorManager) { this.sessionServerConfig = sessionServerConfig; this.taskListenerManager = taskListenerManager; this.metaNodeService = metaNodeService; this.sessionWatchers = sessionWatchers; this.boltExchange = boltExchange; - this.sessionInterests = sessionInterests; - this.sessionRegistry = sessionRegistry; - this.blacklistManager = blacklistManager; + this.provideDataProcessorManager = provideDataProcessorManager; } @Override @@ -130,71 +112,12 @@ public void execute() { if (notifyProvideDataChange.getDataOperator() != DataOperator.REMOVE) { provideData = metaNodeService.fetchData(dataInfoId); - if (ValueConstants.STOP_PUSH_DATA_SWITCH_DATA_ID.equals(dataInfoId)) { - //push stop switch - if (provideData != null) { - if (provideData.getProvideData() == null || provideData.getProvideData().getObject() == null) { - LOGGER.info("Fetch session stop push switch no data existed,config not change!"); - return; - } - String data = (String) provideData.getProvideData().getObject(); - LOGGER.info("Fetch session stop push data switch {} success!", data); - - //receive stop push switch off - if (data != null) { - boolean switchData = Boolean.valueOf(data); - boolean ifChange = sessionServerConfig.isStopPushSwitch() != switchData; - sessionServerConfig.setStopPushSwitch(switchData); - if (!switchData) { - //avoid duplicate false receive - if (ifChange) { - fireReSubscriber(); - } - } else { - //stop push and stop fetch data task - sessionServerConfig.setBeginDataFetchTask(false); - } - } else { - LOGGER.error("Fetch session stop push data switch is null!"); - } - return; - } else { - LOGGER.info("Fetch session stop push switch data null,config not change!"); - } - return; - } else if (ValueConstants.BLACK_LIST_DATA_ID.equals(dataInfoId)) { - //black list data - if (provideData.getProvideData() == null || provideData.getProvideData().getObject() == null) { - LOGGER.info("Fetch session blacklist no data existed,current config not change!"); - return; - } - String data = (String) provideData.getProvideData().getObject(); - if (data != null) { - Map>> blacklistConfigMap = blacklistManager - .convertBlacklistConfig(data); - clientOffBlackIp(blacklistConfigMap); - LOGGER.info("Fetch session blacklist data switch {} success!", data); - } else { - LOGGER.info("Fetch session blacklist data null,current config not change!"); - } - return; - } else if (ValueConstants.ENABLE_DATA_RENEW_SNAPSHOT.equals(dataInfoId)) { - //stop renew switch - if (provideData == null || provideData.getProvideData() == null - || provideData.getProvideData().getObject() == null) { - LOGGER.info("Fetch enableDataRenewSnapshot but no data existed, current config not change!"); - return; - } - boolean enableDataRenewSnapshot = Boolean.parseBoolean((String) provideData.getProvideData().getObject()); - LOGGER.info("Fetch enableDataRenewSnapshot {} success!", enableDataRenewSnapshot); - this.sessionRegistry.setEnableDataRenewSnapshot(enableDataRenewSnapshot); - return; - } - if (provideData == null) { LOGGER.warn("Notify provider data Change request {} fetch no provider data!", notifyProvideDataChange); return; } + provideDataProcessorManager.changeDataProcess(provideData); + } DataInfo dataInfo = DataInfo.valueOf(dataInfoId); @@ -228,54 +151,6 @@ public void execute() { } } - /** - * open push switch to push all reSubscribers - */ - private void fireReSubscriber() { - - //try catch avoid to error cancel beginDataFetchTask switch on - try { - //begin push fire data fetch task first,avoid reSubscriber push duplicate - sessionRegistry.fetchChangDataProcess(); - } catch (Throwable e) { - LOGGER.error("Open push switch first fetch task execute error", e); - } - - try { - //wait 1 MINUTES for dataFetch task evict duplicate subscriber push - TimeUnit.MINUTES.sleep(1); - } catch (InterruptedException e) { - LOGGER.error("Wait for dataFetch Task Interrupted!"); - } - - //fetch task process 1 minutes,can schedule execute fetch task - sessionServerConfig.setBeginDataFetchTask(true); - - if (this.sessionInterests instanceof ReSubscribers) { - ReSubscribers reSubscriber = (ReSubscribers) sessionInterests; - - Map> reSubscribers = reSubscriber - .getReSubscribers(); - - if (reSubscribers != null && !reSubscribers.isEmpty()) { - reSubscribers.forEach( - (dataInfoId, subscribers) -> fireSubscriberMultiFetchTask(dataInfoId, subscribers.values())); - reSubscriber.clearReSubscribers(); - } - } - } - - private void fireSubscriberMultiFetchTask(String dataInfoId, Collection subscribers) { - //trigger fetch data for subscriber,and push to client node - if (!CollectionUtils.isEmpty(subscribers)) { - TaskEvent taskEvent = new TaskEvent(dataInfoId, TaskType.SUBSCRIBER_MULTI_FETCH_TASK); - taskEvent.setAttribute(Constant.PUSH_CLIENT_SUBSCRIBERS, subscribers); - TASK_LOGGER.info("send " + taskEvent.getTaskType() - + " subscribersSize:{},dataInfoId:{}", subscribers.size(), dataInfoId); - taskListenerManager.sendTaskEvent(taskEvent); - } - } - private void firePushTask(ReceivedConfigData receivedConfigData, URL clientUrl) { Map parameter = new HashMap<>(); @@ -285,53 +160,6 @@ private void firePushTask(ReceivedConfigData receivedConfigData, URL clientUrl) taskListenerManager.sendTaskEvent(taskEvent); } - private void clientOffBlackIp(Map>> blacklistConfigMap) { - - if (blacklistConfigMap != null) { - Set ipSet = new HashSet(); - - for (Map.Entry>> configEntry : blacklistConfigMap - .entrySet()) { - if (BlacklistConstants.FORBIDDEN_PUB.equals(configEntry.getKey()) - || BlacklistConstants.FORBIDDEN_SUB_BY_PREFIX.equals(configEntry.getKey())) { - Map> typeMap = configEntry.getValue(); - if (typeMap != null) { - for (Map.Entry> typeEntry : typeMap.entrySet()) { - if (BlacklistConstants.IP_FULL.equals(typeEntry.getKey())) { - if (typeEntry.getValue() != null) { - ipSet.addAll(typeEntry.getValue()); - } - } - } - } - } - - } - - sessionRegistry.remove(getIpConnects(ipSet)); - } - } - - public List getIpConnects(Set _ipList) { - - Server sessionServer = boltExchange.getServer(sessionServerConfig.getServerPort()); - - List connections = new ArrayList<>(); - - if (sessionServer != null) { - Collection channels = sessionServer.getChannels(); - for (Channel channel : channels) { - String key = NetUtil.toAddressString(channel.getRemoteAddress()); - String ip = key.substring(0, key.indexOf(":")); - if (_ipList.contains(ip)) { - connections.add(key); - } - } - } - - return connections; - } - private Map getCache(String connectId) { Map map = sessionWatchers.queryByConnectId(connectId); return map == null ? new ConcurrentHashMap<>() : map; diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/PushTaskClosure.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/PushTaskClosure.java index a2683dcda..9d9910a27 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/PushTaskClosure.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/PushTaskClosure.java @@ -66,6 +66,12 @@ public void run(ProcessingResult processingResult, Task task) { if (result == null) { if (processingResult == ProcessingResult.Success) { tasks.remove(task.getTaskId()); + if (tasks.isEmpty()) { + LOGGER.info("Push all tasks success,dataInfoId={}", dataInfoId); + if (taskClosure != null) { + taskClosure.run(ProcessingResult.Success, null); + } + } } } } @@ -81,12 +87,7 @@ public void start() { pushTaskCheckAsyncHashedWheelTimer.newTimeout(timeout -> { - if (tasks.isEmpty()) { - LOGGER.info("Push all tasks success,dataInfoId={}",dataInfoId); - if (taskClosure != null) { - taskClosure.run(ProcessingResult.Success, null); - } - } else { + if (!tasks.isEmpty()) { LOGGER.warn("Push tasks found error tasks {},dataInfoId={}!", tasks.size(),dataInfoId); if (taskClosure != null) { taskClosure.run(ProcessingResult.PermanentError, null); diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedConfigDataPushTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedConfigDataPushTask.java index 11f6df8b6..fdbb11250 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedConfigDataPushTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedConfigDataPushTask.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.Executor; /** * @@ -83,6 +84,11 @@ public void onException(Channel channel, Throwable exception) { receivedConfigData.getDataId(), receivedConfigData.getGroup(), receivedConfigData.getInstanceId(), url); } + + @Override + public Executor getExecutor() { + return null; + } }; clientNodeService.pushWithCallback( diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedDataMultiPushTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedDataMultiPushTask.java index 4539fed32..2752e70f7 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedDataMultiPushTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/ReceivedDataMultiPushTask.java @@ -16,6 +16,13 @@ */ package com.alipay.sofa.registry.server.session.scheduler.task; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; + import com.alipay.sofa.registry.common.model.PushDataRetryRequest; import com.alipay.sofa.registry.common.model.store.DataInfo; import com.alipay.sofa.registry.common.model.store.Subscriber; @@ -33,24 +40,17 @@ import com.alipay.sofa.registry.server.session.scheduler.ExecutorManager; import com.alipay.sofa.registry.server.session.store.Interests; import com.alipay.sofa.registry.server.session.strategy.ReceivedDataMultiPushTaskStrategy; -import com.alipay.sofa.registry.task.Task; import com.alipay.sofa.registry.task.TaskClosure; import com.alipay.sofa.registry.task.batcher.TaskProcessor.ProcessingResult; import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.timer.AsyncHashedWheelTimer; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.TimeUnit; - /** * * @author shangyu.wh * @version $Id: SubscriberRegisterPushTask.java, v 0.1 2017-12-11 20:57 shangyu.wh Exp $ */ -public class ReceivedDataMultiPushTask extends AbstractSessionTask implements TaskClosure { +public class ReceivedDataMultiPushTask extends AbstractSessionTask { private static final Logger LOGGER = LoggerFactory.getLogger("SESSION-PUSH", "[Receive]"); @@ -139,6 +139,11 @@ public void onException(Channel channel, Throwable exception) { } } } + + @Override + public Executor getExecutor() { + return null; + } }; clientNodeService.pushWithCallback(receivedDataPush, url, callbackHandler); @@ -188,6 +193,11 @@ public void onException(Channel channel, Throwable exception) { dataPush, retryTimes); retrySendReceiveData(pushDataRetryRequest); } + + @Override + public Executor getExecutor() { + return null; + } }); } catch (Exception e) { @@ -333,10 +343,4 @@ public boolean checkRetryTimes() { return checkRetryTimes(sessionServerConfig.getReceivedDataMultiPushTaskRetryTimes()); } - @Override - public void run(ProcessingResult processingResult, Task task) { - if (taskClosure != null) { - taskClosure.run(processingResult, task); - } - } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/SessionRegisterDataTask.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/SessionRegisterDataTask.java index 732d3fb64..0521144f1 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/SessionRegisterDataTask.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/scheduler/task/SessionRegisterDataTask.java @@ -21,21 +21,22 @@ import java.util.Set; import com.alipay.sofa.registry.common.model.dataserver.SessionServerRegisterRequest; -import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.net.NetUtil; import com.alipay.sofa.registry.remoting.Channel; +import com.alipay.sofa.registry.remoting.Client; import com.alipay.sofa.registry.remoting.Server; +import com.alipay.sofa.registry.remoting.bolt.BoltChannel; import com.alipay.sofa.registry.remoting.exchange.Exchange; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; import com.alipay.sofa.registry.server.session.node.SessionProcessIdGenerator; -import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.task.listener.TaskEvent; /** * * @author shangyu.wh + * @author kezhu.wukz * @version $Id: SessionRegisterDataTask.java, v 0.1 2018-04-16 16:07 shangyu.wh Exp $ */ public class SessionRegisterDataTask extends AbstractSessionTask { @@ -44,16 +45,13 @@ public class SessionRegisterDataTask extends AbstractSessionTask { SessionRegisterDataTask.class, "[Task]"); private final Exchange boltExchange; - private final DataNodeService dataNodeService; private final SessionServerConfig sessionServerConfig; private SessionServerRegisterRequest sessionServerRegisterRequest; - private URL dataUrl; + private BoltChannel channel; - public SessionRegisterDataTask(Exchange boltExchange, DataNodeService dataNodeService, - SessionServerConfig sessionServerConfig) { + public SessionRegisterDataTask(Exchange boltExchange, SessionServerConfig sessionServerConfig) { this.boltExchange = boltExchange; - this.dataNodeService = dataNodeService; this.sessionServerConfig = sessionServerConfig; } @@ -72,9 +70,8 @@ public void setTaskEvent(TaskEvent taskEvent) { Object obj = taskEvent.getEventObj(); - if (obj instanceof URL) { - - this.dataUrl = (URL) obj; + if (obj instanceof BoltChannel) { + this.channel = (BoltChannel) obj; } else { throw new IllegalArgumentException("Input task event object error!"); @@ -99,7 +96,30 @@ public void setTaskEvent(TaskEvent taskEvent) { @Override public void execute() { - dataNodeService.registerSessionProcessId(sessionServerRegisterRequest, dataUrl); + if (!channel.isConnected()) { + return; + } + + Client sessionClient = boltExchange.getClient(Exchange.DATA_SERVER_TYPE); + try { + sessionClient.sendSync(channel, sessionServerRegisterRequest, + sessionServerConfig.getDataNodeExchangeTimeOut()); + } catch (Exception e) { + if (isLastRetry()) { + LOGGER + .error( + "Register to DataServer({}/{}:{}) error for multiple times, so close this channel (let bolt reconnect it)", + channel.getLocalAddress(), channel.getRemoteAddress().getHostString(), + channel.getRemoteAddress().getPort()); + channel.close(); + } + throw e; + } + + } + + protected boolean isLastRetry() { + return getExecCount() >= sessionServerConfig.getSessionRegisterDataServerTaskRetryTimes(); } @Override @@ -107,6 +127,8 @@ public String toString() { return "SESSION_REGISTER_DATA_TASK{" + "taskId='" + taskId + '\'' + ", sessionServerRegisterRequest=" + sessionServerRegisterRequest.getProcessId() + ", clientList=" + sessionServerRegisterRequest.getConnectIds().size() - + ", dataUrl=" + dataUrl + '}'; + + ", channel=" + channel.getLocalAddress() + "/" + + channel.getRemoteAddress().getHostString() + ':' + + channel.getRemoteAddress().getPort() + '}'; } } \ No newline at end of file diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionDataStore.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionDataStore.java index 6bd16b497..aab15036a 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionDataStore.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionDataStore.java @@ -16,10 +16,6 @@ */ package com.alipay.sofa.registry.server.session.store; -import com.alipay.sofa.registry.common.model.store.Publisher; -import com.alipay.sofa.registry.log.Logger; -import com.alipay.sofa.registry.log.LoggerFactory; - import java.util.Collection; import java.util.Iterator; import java.util.Map; @@ -28,6 +24,11 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.WordCache; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; + /** * * @author shangyu.wh @@ -51,6 +52,8 @@ public class SessionDataStore implements DataStore { @Override public void add(Publisher publisher) { + Publisher.internPublisher(publisher); + write.lock(); try { Map publishers = registry.get(publisher.getDataInfoId()); @@ -118,15 +121,14 @@ public boolean deleteById(String registerId, String dataInfoId) { Map publishers = registry.get(dataInfoId); if (publishers == null) { - LOGGER.error( - "Delete failed because publisher is not registered for dataInfoId: {}", + LOGGER.warn("Delete failed because publisher is not registered for dataInfoId: {}", dataInfoId); return false; } else { Publisher publisherTodelete = publishers.remove(registerId); if (publisherTodelete == null) { - LOGGER.error( + LOGGER.warn( "Delete failed because publisher is not registered for registerId: {}", registerId); return false; @@ -188,7 +190,7 @@ public Publisher queryById(String registerId, String dataInfoId) { Map publishers = registry.get(dataInfoId); if (publishers == null) { - LOGGER.error("Publisher is not registered for dataInfoId: {}", dataInfoId); + LOGGER.warn("Publisher is not registered for dataInfoId: {}", dataInfoId); return null; } return publishers.get(registerId); @@ -209,8 +211,9 @@ public long count() { } private void addToConnectIndex(Publisher publisher) { + String connectId = WordCache.getInstance().getWordCache( + publisher.getSourceAddress().getAddressString()); - String connectId = publisher.getSourceAddress().getAddressString(); Map publisherMap = connectIndex.get(connectId); if (publisherMap == null) { Map newPublisherMap = new ConcurrentHashMap<>(); @@ -233,15 +236,6 @@ private void removeFromConnectIndex(Publisher publisher) { } } - private void invalidateIndex(Publisher publisher) { - String connectId = publisher.getSourceAddress().getAddressString(); - invalidateConnectIndex(connectId); - } - - private void invalidateConnectIndex(String connectId) { - connectIndex.remove(connectId); - } - @Override public Map> getConnectPublishers() { return connectIndex; diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionInterests.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionInterests.java index fd5cb9595..a0ab91844 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionInterests.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionInterests.java @@ -16,15 +16,6 @@ */ package com.alipay.sofa.registry.server.session.store; -import com.alipay.sofa.registry.common.model.store.Subscriber; -import com.alipay.sofa.registry.core.model.ScopeEnum; -import com.alipay.sofa.registry.log.Logger; -import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; -import com.alipay.sofa.registry.server.session.cache.SubscriberResult; -import com.alipay.sofa.registry.util.VersionsMapUtils; -import org.springframework.beans.factory.annotation.Autowired; - import java.net.InetSocketAddress; import java.util.Collection; import java.util.Iterator; @@ -34,6 +25,17 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.springframework.beans.factory.annotation.Autowired; + +import com.alipay.sofa.registry.common.model.store.Subscriber; +import com.alipay.sofa.registry.common.model.store.WordCache; +import com.alipay.sofa.registry.core.model.ScopeEnum; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; +import com.alipay.sofa.registry.server.session.cache.SubscriberResult; +import com.alipay.sofa.registry.util.VersionsMapUtils; + /** * * @author shangyu.wh @@ -71,6 +73,7 @@ public class SessionInterests implements Interests, ReSubscribers { @Override public void add(Subscriber subscriber) { + Subscriber.internSubscriber(subscriber); write.lock(); try { @@ -206,8 +209,6 @@ public boolean checkInterestVersions(String dataCenter, String dataInfoId, Long Map subscribers = interests.get(dataInfoId); if (subscribers == null || subscribers.isEmpty()) { - LOGGER.info("There are not Subscriber Existed! Who are interest with dataInfoId {} !", - dataInfoId); return false; } @@ -231,6 +232,7 @@ public boolean checkInterestVersions(String dataCenter, String dataInfoId, Long public boolean checkAndUpdateInterestVersions(String dataCenter, String dataInfoId, Long version) { read.lock(); try { + dataInfoId = WordCache.getInstance().getWordCache(dataInfoId); Map subscribers = interests.get(dataInfoId); @@ -285,8 +287,9 @@ private void invalidateIndex(Subscriber subscriber) { } private void addConnectIndex(Subscriber subscriber) { - String connectId = subscriber.getSourceAddress().getAddressString(); + connectId = WordCache.getInstance().getWordCache(connectId); + Map subscriberMap = connectIndex.get(connectId); if (subscriberMap == null) { Map newSubscriberMap = new ConcurrentHashMap<>(); diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionWatchers.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionWatchers.java index 381dbebe8..91edef853 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionWatchers.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/store/SessionWatchers.java @@ -16,11 +16,6 @@ */ package com.alipay.sofa.registry.server.session.store; -import com.alipay.sofa.registry.common.model.store.Watcher; -import com.alipay.sofa.registry.log.Logger; -import com.alipay.sofa.registry.log.LoggerFactory; -import com.alipay.sofa.registry.util.VersionsMapUtils; - import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -30,6 +25,12 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import com.alipay.sofa.registry.common.model.store.Watcher; +import com.alipay.sofa.registry.common.model.store.WordCache; +import com.alipay.sofa.registry.log.Logger; +import com.alipay.sofa.registry.log.LoggerFactory; +import com.alipay.sofa.registry.util.VersionsMapUtils; + /** * * @author shangyu.wh @@ -60,6 +61,8 @@ public class SessionWatchers implements Watchers { @Override public void add(Watcher watcher) { + Watcher.internWatcher(watcher); + write.lock(); try { Map watcherMap = watchers.get(watcher.getDataInfoId()); @@ -198,8 +201,9 @@ public long count() { } private void addConnectIndex(Watcher watcher) { - String connectId = watcher.getSourceAddress().getAddressString(); + connectId = WordCache.getInstance().getWordCache(connectId); + Map subscriberMap = connectIndex.get(connectId); if (subscriberMap == null) { Map newSubscriberMap = new ConcurrentHashMap<>(); diff --git a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/strategy/impl/DefaultSessionRegistryStrategy.java b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/strategy/impl/DefaultSessionRegistryStrategy.java index c68889f3e..e7ab95ae3 100644 --- a/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/strategy/impl/DefaultSessionRegistryStrategy.java +++ b/server/server/session/src/main/java/com/alipay/sofa/registry/server/session/strategy/impl/DefaultSessionRegistryStrategy.java @@ -16,26 +16,25 @@ */ package com.alipay.sofa.registry.server.session.strategy.impl; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; + import com.alipay.sofa.registry.common.model.store.Publisher; import com.alipay.sofa.registry.common.model.store.Subscriber; import com.alipay.sofa.registry.common.model.store.Watcher; import com.alipay.sofa.registry.log.Logger; import com.alipay.sofa.registry.log.LoggerFactory; import com.alipay.sofa.registry.server.session.bootstrap.SessionServerConfig; -import com.alipay.sofa.registry.server.session.cache.CacheService; -import com.alipay.sofa.registry.server.session.cache.DatumKey; -import com.alipay.sofa.registry.server.session.cache.Key; import com.alipay.sofa.registry.server.session.store.Interests; import com.alipay.sofa.registry.server.session.strategy.SessionRegistryStrategy; import com.alipay.sofa.registry.task.listener.TaskEvent; import com.alipay.sofa.registry.task.listener.TaskListenerManager; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; /** + * @author kezhu.wukz * @author xuanbei * @since 2019/2/15 */ @@ -61,9 +60,6 @@ public class DefaultSessionRegistryStrategy implements SessionRegistryStrategy { @Autowired private SessionServerConfig sessionServerConfig; - @Autowired - private CacheService sessionCacheService; - @Override public void doFetchChangDataProcess(Map> dataInfoIdVersions) { //diff dataCenter same dataInfoId sent once fetch on cloud mode @@ -72,11 +68,6 @@ public void doFetchChangDataProcess(Map { if (checkInterestVersions(dataCenter, dataInfoID, version)) { - - //update cache - sessionCacheService.invalidate(new Key( - Key.KeyType.OBJ, DatumKey.class.getName(), new DatumKey(dataInfoID, dataCenter))); - changeDataInfoIds.add(dataInfoID); } }); diff --git a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServer.java b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServer.java index 2aad27198..66fc6a40f 100644 --- a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServer.java +++ b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServer.java @@ -26,6 +26,8 @@ import com.alipay.sofa.jraft.option.NodeOptions; import com.alipay.sofa.jraft.rpc.RaftRpcServerFactory; import com.alipay.sofa.jraft.rpc.impl.AbstractBoltClientService; +import com.alipay.sofa.jraft.storage.impl.RocksDBLogStorage; +import com.alipay.sofa.jraft.util.StorageOptionsFactory; import com.alipay.sofa.registry.common.model.store.URL; import com.alipay.sofa.registry.jraft.command.NotifyLeaderChange; import com.alipay.sofa.registry.jraft.handler.NotifyLeaderChangeHandler; @@ -42,6 +44,11 @@ import com.alipay.sofa.registry.remoting.bolt.BoltServer; import com.alipay.sofa.registry.remoting.bolt.SyncUserProcessorAdapter; import com.alipay.sofa.registry.util.FileUtils; +import org.rocksdb.BlockBasedTableConfig; +import org.rocksdb.BloomFilter; +import org.rocksdb.IndexType; +import org.rocksdb.RocksDB; +import org.rocksdb.util.SizeUnit; import java.io.File; import java.io.IOException; @@ -56,6 +63,10 @@ */ public class RaftServer { + static { + RocksDB.loadLibrary(); + } + private static final Logger LOGGER = LoggerFactory.getLogger(RaftServer.class); private RaftGroupService raftGroupService; @@ -170,6 +181,24 @@ private NodeOptions initNodeOptions(RaftServerConfig raftServerConfig) { nodeOptions.setEnableMetrics(raftServerConfig.isEnableMetrics()); } + // See https://github.com/sofastack/sofa-jraft/pull/156 + final BlockBasedTableConfig conf = new BlockBasedTableConfig() // + // Begin to use partitioned index filters + // https://github.com/facebook/rocksdb/wiki/Partitioned-Index-Filters#how-to-use-it + .setIndexType(IndexType.kTwoLevelIndexSearch) // + .setFilter(new BloomFilter(16, false)) // + .setPartitionFilters(true) // + .setMetadataBlockSize(8 * SizeUnit.KB) // + .setCacheIndexAndFilterBlocks(false) // + .setCacheIndexAndFilterBlocksWithHighPriority(true) // + .setPinL0FilterAndIndexBlocksInCache(true) // + // End of partitioned index filters settings. + .setBlockSize(4 * SizeUnit.KB)// + .setBlockCacheSize(raftServerConfig.getRockDBCacheSize() * SizeUnit.MB) // + .setCacheNumShardBits(8); + + StorageOptionsFactory.registerRocksDBTableFormatConfig(RocksDBLogStorage.class, conf); + return nodeOptions; } @@ -255,4 +284,12 @@ public void setFollowerProcessListener(FollowerProcessListener followerProcessLi this.followerProcessListener = followerProcessListener; } + /** + * Getter method for property serverHandlers. + * + * @return property value of serverHandlers + */ + public List getServerHandlers() { + return serverHandlers; + } } \ No newline at end of file diff --git a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServerConfig.java b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServerConfig.java index 9e68e1e66..f739bc81b 100644 --- a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServerConfig.java +++ b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/bootstrap/RaftServerConfig.java @@ -50,6 +50,8 @@ public class RaftServerConfig { */ private int snapshotIntervalSecs = 3600; + private int RockDBCacheSize = 64; //64M + private Logger metricsLogger; /** @@ -141,4 +143,22 @@ public Logger getMetricsLogger() { public void setMetricsLogger(Logger metricsLogger) { this.metricsLogger = metricsLogger; } + + /** + * Getter method for property RockDBCacheSize. + * + * @return property value of RockDBCacheSize + */ + public int getRockDBCacheSize() { + return RockDBCacheSize; + } + + /** + * Setter method for property RockDBCacheSize. + * + * @param RockDBCacheSize value to be assigned to property RockDBCacheSize + */ + public void setRockDBCacheSize(int rockDBCacheSize) { + RockDBCacheSize = rockDBCacheSize; + } } \ No newline at end of file diff --git a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/handler/RaftServerHandler.java b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/handler/RaftServerHandler.java index 12bbba9a6..e4524acc2 100644 --- a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/handler/RaftServerHandler.java +++ b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/handler/RaftServerHandler.java @@ -159,4 +159,5 @@ public Object reply(Channel channel, Object message) { public InvokeType getInvokeType() { return InvokeType.ASYNC; } + } \ No newline at end of file diff --git a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/Processor.java b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/Processor.java index a395a365d..fab3c1cd1 100644 --- a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/Processor.java +++ b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/Processor.java @@ -28,6 +28,7 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * @@ -36,12 +37,16 @@ */ public class Processor { - private static final Logger LOG = LoggerFactory - .getLogger(Processor.class); + private static final Logger LOG = LoggerFactory + .getLogger(Processor.class); - private Map> workerMethods = new HashMap<>(); + private Map> workerMethods = new HashMap<>(); - private Map workers = new HashMap<>(); + private Map workers = new HashMap<>(); + + private Map methodHandleMap = new ConcurrentHashMap<>(); + + private final static String SERVICE_METHOD_SPLIT = "#@#"; private static volatile Processor instance; @@ -97,15 +102,24 @@ public ProcessResponse process(ProcessRequest request) { for (int i = 0; i < sig.length; i++) { methodKeyBuffer.append(sig[i]); } - Method appServiceMethod = workerMethods.get(serviceId).get(methodKeyBuffer.toString()); + String methodKey = methodKeyBuffer.toString(); + Method appServiceMethod = workerMethods.get(serviceId).get(methodKey); if (appServiceMethod == null) { LOG.error("Can not find method {} from processor by serviceId {}", methodName, serviceId); throw new NoSuchMethodException("Can not find method from processor!"); } - Object[] methodArg = request.getMethodArgs(); - MethodHandle methodHandle = MethodHandles.lookup().unreflect(appServiceMethod); + String methodHandleKey = getMethodHandleKey(serviceId, methodKey); + + MethodHandle methodHandle = methodHandleMap.computeIfAbsent(methodHandleKey,k-> { + try { + return MethodHandles.lookup().unreflect(appServiceMethod); + } catch (IllegalAccessException e) { + throw new RuntimeException("Process service request lookup method error!",e); + } + }); + Object ret = methodHandle.bindTo(target).invokeWithArguments(methodArg); if (ret != null) { return ProcessResponse.ok(ret).build(); @@ -133,7 +147,23 @@ public ProcessResponse process(Method method, ProcessRequest request) { try { Object[] methodArg = request.getMethodArgs(); - MethodHandle methodHandle = MethodHandles.lookup().unreflect(method); + + StringBuilder methodKeyBuffer = new StringBuilder(); + methodKeyBuffer.append(methodName); + String[] sig = request.getMethodArgSigs(); + for (int i = 0; i < sig.length; i++) { + methodKeyBuffer.append(sig[i]); + } + String methodKey = methodKeyBuffer.toString(); + String methodHandleKey = getMethodHandleKey(serviceId, methodKey); + MethodHandle methodHandle = methodHandleMap.computeIfAbsent(methodHandleKey,k-> { + try { + return MethodHandles.lookup().unreflect(method); + } catch (IllegalAccessException e) { + throw new RuntimeException("Process service request lookup method error!",e); + } + }); + Object ret = methodHandle.bindTo(target).invokeWithArguments(methodArg); if (ret != null) { return ProcessResponse.ok(ret).build(); @@ -174,6 +204,42 @@ public Method getWorkMethod(ProcessRequest request) { } } + public MethodHandle getWorkMethodHandle(ProcessRequest request) { + String methodName = request.getMethodName(); + String serviceId = request.getServiceName(); + try { + + StringBuilder methodKeyBuffer = new StringBuilder(); + methodKeyBuffer.append(methodName); + String[] sig = request.getMethodArgSigs(); + for (int i = 0; i < sig.length; i++) { + methodKeyBuffer.append(sig[i]); + } + String methodKey = methodKeyBuffer.toString(); + Method appServiceMethod = workerMethods.get(serviceId).get(methodKey); + if (appServiceMethod == null) { + LOG.error("Can not find method {} from processor by serviceId {}", methodName, + serviceId); + throw new NoSuchMethodException("Can not find method from processor!"); + } + + String methodHandleKey = getMethodHandleKey(serviceId, methodKey); + + MethodHandle methodHandle = methodHandleMap.computeIfAbsent(methodHandleKey,k-> { + try { + return MethodHandles.lookup().unreflect(appServiceMethod); + } catch (IllegalAccessException e) { + throw new RuntimeException("Process service request lookup method error!",e); + } + }); + return methodHandle; + } catch (Exception e) { + LOG.error("Process request {} get WorkMethod error!", request, e); + throw new RuntimeException(String.format("Process request %s get WorkMethod error!", + request)); + } + } + public Map getWorkers() { return workers; } @@ -185,4 +251,8 @@ public boolean isLeaderReadMethod(Method method) { return false; } + public String getMethodHandleKey(String serviceId, String methodKey) { + return serviceId + SERVICE_METHOD_SPLIT + methodKey; + } + } diff --git a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/ProxyHandler.java b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/ProxyHandler.java index f96098cf2..09b9e63c5 100644 --- a/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/ProxyHandler.java +++ b/server/store/jraft/src/main/java/com/alipay/sofa/registry/jraft/processor/ProxyHandler.java @@ -22,7 +22,6 @@ import com.alipay.sofa.registry.log.LoggerFactory; import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -87,9 +86,7 @@ private Object doInvokeMethod(ProcessRequest request) { serviceId)); } - Method method = Processor.getInstance().getWorkMethod(request); - - MethodHandle methodHandle = MethodHandles.lookup().unreflect(method); + MethodHandle methodHandle = Processor.getInstance().getWorkMethodHandle(request); return methodHandle.bindTo(target).invokeWithArguments(request.getMethodArgs()); } catch (Throwable e) { LOGGER.error("Directly invoke read only service {} method {} error!", diff --git a/test/src/test/java/com/alipay/sofa/registry/test/BaseIntegrationTest.java b/test/src/test/java/com/alipay/sofa/registry/test/BaseIntegrationTest.java index 83c238b6c..e14332bd5 100644 --- a/test/src/test/java/com/alipay/sofa/registry/test/BaseIntegrationTest.java +++ b/test/src/test/java/com/alipay/sofa/registry/test/BaseIntegrationTest.java @@ -16,31 +16,6 @@ */ package com.alipay.sofa.registry.test; -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; - -import org.junit.Before; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.SpringBootConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.ConfigurableApplicationContext; - import com.alipay.remoting.Connection; import com.alipay.sofa.registry.client.api.RegistryClientConfig; import com.alipay.sofa.registry.client.api.SubscriberDataObserver; @@ -60,6 +35,29 @@ import com.alipay.sofa.registry.remoting.jersey.JerseyClient; import com.alipay.sofa.registry.server.data.cache.DatumCache; import com.alipay.sofa.registry.server.test.TestRegistryMain; +import org.junit.Before; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ConfigurableApplicationContext; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; +import static org.junit.Assert.assertTrue; /** * @author xuanbei 18/12/1 @@ -131,7 +129,8 @@ public static void startServerIfNecessary() throws Exception { private static void initRegistryClientAndChannel() { if (registryClient1 == null) { RegistryClientConfig config = DefaultRegistryClientConfigBuilder.start() - .setAppName("testApp1").setDataCenter(LOCAL_DATACENTER).setZone(LOCAL_REGION) + .setSyncConfigRetryInterval(60000).setAppName("testApp1") + .setDataCenter(LOCAL_DATACENTER).setZone(LOCAL_REGION) .setRegistryEndpoint(LOCAL_ADDRESS).setRegistryEndpointPort(sessionPort).build(); registryClient1 = new DefaultRegistryClient(config); registryClient1.init(); diff --git a/test/src/test/java/com/alipay/sofa/registry/test/pubsub/TempPublisherTest.java b/test/src/test/java/com/alipay/sofa/registry/test/pubsub/TempPublisherTest.java index 45401d224..e824570d0 100644 --- a/test/src/test/java/com/alipay/sofa/registry/test/pubsub/TempPublisherTest.java +++ b/test/src/test/java/com/alipay/sofa/registry/test/pubsub/TempPublisherTest.java @@ -16,19 +16,8 @@ */ package com.alipay.sofa.registry.test.pubsub; -import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_GROUP; -import static com.alipay.sofa.registry.common.model.constants.ValueConstants.DEFAULT_INSTANCE_ID; -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.junit4.SpringRunner; - import com.alipay.sofa.registry.client.api.model.RegistryType; +import com.alipay.sofa.registry.client.api.model.UserData; import com.alipay.sofa.registry.client.api.registration.PublisherRegistration; import com.alipay.sofa.registry.client.api.registration.SubscriberRegistration; import com.alipay.sofa.registry.common.model.PublishType; @@ -38,6 +27,18 @@ import com.alipay.sofa.registry.core.model.ScopeEnum; import com.alipay.sofa.registry.server.session.node.service.DataNodeService; import com.alipay.sofa.registry.test.BaseIntegrationTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; + +import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_GROUP; +import static com.alipay.sofa.registry.common.model.constants.ValueConstants.DEFAULT_INSTANCE_ID; +import static org.junit.Assert.assertEquals; /** * @author xuanbei @@ -47,17 +48,22 @@ public class TempPublisherTest extends BaseIntegrationTest { @Test public synchronized void doTest() throws Exception { - String dataId = "test-dataId-" + System.currentTimeMillis(); + String dataId = "test-dataId-" +this.getClass().getName()+ System.currentTimeMillis(); String value = "test publish temp data"; - + AtomicReference dataIdRef = new AtomicReference<>(); + AtomicReference userDataRef = new AtomicReference<>(); // register SubscriberRegistration SubscriberRegistration subReg = new SubscriberRegistration(dataId, - new MySubscriberDataObserver()); + (dataIdIn, data) -> { + dataIdRef.set(dataIdIn); + userDataRef.set(data); + }); + subReg.setScopeEnum(ScopeEnum.zone); registryClient1.register(subReg); - Thread.sleep(2000L); - assertEquals(dataId, this.dataId); - assertEquals(0, userData.getZoneData().size()); + Thread.sleep(5000L); + assertEquals(dataId, dataIdRef.get()); + assertEquals(0, userDataRef.get().getZoneData().size()); // publish temp data Publisher tempPublisher = new Publisher(); @@ -78,19 +84,25 @@ public synchronized void doTest() throws Exception { sessionApplicationContext.getBean(DataNodeService.class).register(tempPublisher); // data size is 1 - Thread.sleep(2000L); - assertEquals(dataId, this.dataId); - assertEquals(1, userData.getZoneData().size()); - userData = null; + Thread.sleep(5000L); + assertEquals(dataId, dataIdRef.get()); + assertEquals(1, userDataRef.get().getZoneData().size()); + + userDataRef.set(null); + dataIdRef.set(null); registryClient1.unregister(dataId, DEFAULT_GROUP, RegistryType.SUBSCRIBER); // register another SubscriberRegistration, data size is 0 - subReg = new SubscriberRegistration(dataId, new MySubscriberDataObserver()); + subReg = new SubscriberRegistration(dataId, + (dataIdIn, data) -> { + dataIdRef.set(dataIdIn); + userDataRef.set(data); + }); subReg.setScopeEnum(ScopeEnum.zone); registryClient1.register(subReg); - Thread.sleep(2000L); - assertEquals(dataId, this.dataId); - assertEquals(0, userData.getZoneData().size()); + Thread.sleep(5000L); + assertEquals(dataId, dataIdRef.get()); + assertEquals(0, userDataRef.get().getZoneData().size()); registryClient1.unregister(dataId, DEFAULT_GROUP, RegistryType.SUBSCRIBER); } @@ -98,18 +110,23 @@ public synchronized void doTest() throws Exception { public synchronized void doTestPubAndTempPubSameTime() throws Exception { String dataId = "test-same-time-pub&tempPub-" + System.currentTimeMillis(); String value = "test same time publish"; + AtomicReference dataIdRef = new AtomicReference<>(); + AtomicReference userDataRef = new AtomicReference<>(); SubscriberRegistration subReg = new SubscriberRegistration(dataId, - new MySubscriberDataObserver()); + (dataIdIn, data) -> { + dataIdRef.set(dataIdIn); + userDataRef.set(data); + }); subReg.setScopeEnum(ScopeEnum.zone); registryClient1.register(subReg); - Thread.sleep(2000L); + Thread.sleep(5000L); // publish data PublisherRegistration registration = new PublisherRegistration(dataId); registryClient1.register(registration, "test publish"); - Thread.sleep(2000L); + Thread.sleep(5000L); // publish temp data Publisher tempPublisher = new Publisher(); @@ -129,10 +146,10 @@ public synchronized void doTestPubAndTempPubSameTime() throws Exception { tempPublisher.setDataList(dataBoxData); sessionApplicationContext.getBean(DataNodeService.class).register(tempPublisher); - Thread.sleep(2000L); + Thread.sleep(5000L); - assertEquals(1, userData.getZoneData().size()); - assertEquals(2, userData.getZoneData().get(LOCAL_REGION).size()); + assertEquals(1, userDataRef.get().getZoneData().size()); + assertEquals(2, userDataRef.get().getZoneData().get(LOCAL_REGION).size()); registryClient1.unregister(dataId, DEFAULT_GROUP, RegistryType.SUBSCRIBER); registryClient1.unregister(dataId, DEFAULT_GROUP, RegistryType.PUBLISHER); diff --git a/test/src/test/java/com/alipay/sofa/registry/test/sync/SessionNotifyTest.java b/test/src/test/java/com/alipay/sofa/registry/test/sync/SessionNotifyTest.java new file mode 100644 index 000000000..4349a808e --- /dev/null +++ b/test/src/test/java/com/alipay/sofa/registry/test/sync/SessionNotifyTest.java @@ -0,0 +1,406 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.registry.test.sync; + +import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_DATA_CENTER; +import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_GROUP; +import static com.alipay.sofa.registry.common.model.constants.ValueConstants.DEFAULT_INSTANCE_ID; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringRunner; + +import com.alipay.remoting.Connection; +import com.alipay.sofa.registry.common.model.CommonResponse; +import com.alipay.sofa.registry.common.model.GenericResponse; +import com.alipay.sofa.registry.common.model.Node; +import com.alipay.sofa.registry.common.model.ServerDataBox; +import com.alipay.sofa.registry.common.model.constants.ValueConstants; +import com.alipay.sofa.registry.common.model.dataserver.Datum; +import com.alipay.sofa.registry.common.model.dataserver.GetDataRequest; +import com.alipay.sofa.registry.common.model.sessionserver.DataChangeRequest; +import com.alipay.sofa.registry.common.model.store.BaseInfo.ClientVersion; +import com.alipay.sofa.registry.common.model.store.DataInfo; +import com.alipay.sofa.registry.common.model.store.Publisher; +import com.alipay.sofa.registry.common.model.store.URL; +import com.alipay.sofa.registry.net.NetUtil; +import com.alipay.sofa.registry.remoting.CallbackHandler; +import com.alipay.sofa.registry.remoting.Channel; +import com.alipay.sofa.registry.remoting.ChannelHandler; +import com.alipay.sofa.registry.remoting.Client; +import com.alipay.sofa.registry.remoting.Server; +import com.alipay.sofa.registry.remoting.bolt.BoltChannel; +import com.alipay.sofa.registry.remoting.bolt.BoltClient; +import com.alipay.sofa.registry.remoting.bolt.exchange.BoltExchange; +import com.alipay.sofa.registry.remoting.exchange.Exchange; +import com.alipay.sofa.registry.server.data.remoting.dataserver.DataServerConnectionFactory; +import com.alipay.sofa.registry.server.data.remoting.dataserver.handler.DataSyncServerConnectionHandler; +import com.alipay.sofa.registry.server.data.remoting.handler.AbstractServerHandler; +import com.alipay.sofa.registry.server.session.cache.DatumKey; +import com.alipay.sofa.registry.server.session.cache.Key; +import com.alipay.sofa.registry.server.session.cache.Key.KeyType; +import com.alipay.sofa.registry.server.session.cache.SessionCacheService; +import com.alipay.sofa.registry.server.session.store.SessionInterests; +import com.alipay.sofa.registry.test.BaseIntegrationTest; +import com.alipay.sofa.registry.util.DatumVersionUtil; +import com.alipay.sofa.registry.util.NamedThreadFactory; +import com.alipay.sofa.registry.util.ParaCheckUtil; + +/** + * @author xuanbei + * @since 2019/1/16 + */ +@RunWith(SpringRunner.class) +public class SessionNotifyTest extends BaseIntegrationTest { + private static final int TEST_SYNC_PORT = 9677; + private static DataServerConnectionFactory dataServerConnectionFactory; + private static Server dataSyncServer; + private static String remoteIP; + private static BoltChannel boltChannel; + + private static Map boltChannelMap = new ConcurrentHashMap<>(); + + private static Map boltChannelMapInt = new ConcurrentHashMap<>(); + + private static final int connectNum = 20; + + private static final int threadNum = 20; + + private static final int idNum = 100; + + private static SessionCacheService sessionCacheService; + + private static SessionInterests sessionInterests; + + private static Datum datum; + + private static BoltClient boltClientFetch = new BoltClient(1); + + private static ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 100, + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), + new NamedThreadFactory( + "TestSessionNotifyFetch")); + + @BeforeClass + public static void beforeClass() throws Exception { + startServerIfNecessary(); + BoltExchange boltExchange = (BoltExchange) dataApplicationContext.getBean("boltExchange"); + dataServerConnectionFactory = dataApplicationContext.getBean("dataServerConnectionFactory", + DataServerConnectionFactory.class); + + sessionCacheService = sessionApplicationContext.getBean("sessionCacheService", + SessionCacheService.class); + + sessionInterests = sessionApplicationContext.getBean("sessionInterests", + SessionInterests.class); + + Map publisherMap = new HashMap<>(); + + for (int i = 0; i < 10; i++) { + Publisher publisher = new Publisher(); + + String connectId = "123.123.123." + i; + publisher.setAppName("APP"); + //ZONE MUST BE CURRENT SESSION ZONE + publisher.setCell("CELL"); + publisher.setClientId(connectId); + publisher.setDataId(MockSyncDataHandler.dataId); + publisher.setGroup(DEFAULT_GROUP); + publisher.setInstanceId(DEFAULT_INSTANCE_ID); + publisher.setRegisterId(UUID.randomUUID().toString()); + publisher.setProcessId(connectId); + publisher.setVersion(Long.valueOf(i)); + + //registerTimestamp must happen from server,client time maybe different cause pub and unPublisher fail + publisher.setRegisterTimestamp(System.currentTimeMillis()); + + publisher.setClientRegisterTimestamp(System.currentTimeMillis()); + publisher.setSourceAddress(new URL(connectId, 5050)); + + publisher.setClientVersion(ClientVersion.StoreData); + + DataInfo dataInfo = new DataInfo(DEFAULT_INSTANCE_ID, MockSyncDataHandler.dataId, + DEFAULT_GROUP); + publisher.setDataInfoId(dataInfo.getDataInfoId()); + + ServerDataBox serverDataBox = new ServerDataBox(""); + List list = new ArrayList<>(); + list.add(serverDataBox); + + publisher.setDataList(list); + publisherMap.put(publisher.getRegisterId(), publisher); + } + + datum = new Datum(); + datum.setDataId(MockSyncDataHandler.dataId); + datum.setInstanceId(DEFAULT_INSTANCE_ID); + datum.setGroup(DEFAULT_GROUP); + //no datum set version current timestamp + datum.setVersion(DatumVersionUtil.nextId()); + datum.setPubMap(publisherMap); + datum.setDataCenter(ValueConstants.DEFAULT_DATA_CENTER); + + // open sync port and connect it + dataSyncServer = boltExchange.open(new URL(NetUtil.getLocalAddress().getHostAddress(), + TEST_SYNC_PORT), new ChannelHandler[] { new MockGetDataHandler(), + dataApplicationContext.getBean(DataSyncServerConnectionHandler.class) }); + + for (int i = 0; i < connectNum; i++) { + + URL urltemp = new URL(LOCAL_ADDRESS, TEST_SYNC_PORT); + + Client client = boltExchange.connect(Exchange.DATA_SERVER_TYPE + i, urltemp, + new ChannelHandler[] { new MockDataChangeRequestHandler() }); + + BoltChannel boltChannel = (BoltChannel) client.getChannel(urltemp); + + String key = boltChannel.getConnection().getLocalIP() + ":" + + boltChannel.getConnection().getLocalPort(); + boltChannelMap.put(key, boltChannel); + System.out.println("testsyncserver remote connect remote:" + + boltChannel.getConnection().getRemoteAddress() + " local:" + key); + } + + for (int i = 0; i < connectNum; i++) { + + URL urltemp = new URL(LOCAL_ADDRESS, TEST_SYNC_PORT); + + Client client = boltExchange.connect(Exchange.DATA_SERVER_TYPE + i + 10, urltemp, + new ChannelHandler[] { new MockDataChangeRequestHandler() }); + + BoltChannel boltChannel = (BoltChannel) client.getChannel(urltemp); + + String key = boltChannel.getConnection().getLocalIP() + ":" + + boltChannel.getConnection().getLocalPort(); + boltChannelMapInt.put(i, boltChannel); + System.out.println("testsyncserver remote connect remote:" + + boltChannel.getConnection().getRemoteAddress() + " local:" + key); + } + + Thread.sleep(500); + } + + @Ignore + @Test + public void doTest() throws Exception { + + ThreadPoolExecutor executor = new ThreadPoolExecutor(threadNum, threadNum, 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(), new NamedThreadFactory("TestSessionNotify")); + for (int i = 0; i < idNum; i++) { + + int finalI = i; + executor.submit(()->{ + + // post sync data request + DataChangeRequest request = new DataChangeRequest(DataInfo.toDataInfoId( + MockSyncDataHandler.dataId, DEFAULT_INSTANCE_ID, DEFAULT_GROUP), LOCAL_DATACENTER, + finalI); + + boltChannelMap.forEach((connect,boltChannel)->{ + + CommonResponse commonResponse = null; + try { + dataSyncServer.sendCallback(dataSyncServer + .getChannel(new URL(boltChannel.getConnection().getLocalIP(), boltChannel.getConnection().getLocalPort())), request, + new NotifyCallback(boltChannel.getConnection(),request),3000); + //assertTrue(commonResponse.isSuccess()); + } catch (Exception e) { + e.printStackTrace(); + } + }); + }); + } + + while (true){ + TimeUnit.SECONDS.sleep(10); + } + + } + + public static class MockDataChangeRequestHandler extends + AbstractServerHandler { + + @Override + public void checkParam(DataChangeRequest request) throws RuntimeException { + + } + + @Override + public Object doHandle(Channel channel, DataChangeRequest request) { + + DataChangeRequest dataChangeRequest = (DataChangeRequest) request; + + dataChangeRequest.setDataCenter(dataChangeRequest.getDataCenter()); + dataChangeRequest.setDataInfoId(dataChangeRequest.getDataInfoId()); + long version = dataChangeRequest.getVersion(); + + + //update cache when change + sessionCacheService.invalidate(new Key(KeyType.OBJ, DatumKey.class.getName(), new DatumKey( + dataChangeRequest.getDataInfoId(), dataChangeRequest.getDataCenter()))); + + + try { + boolean resultT = sessionInterests.checkInterestVersions( + dataChangeRequest.getDataCenter(), dataChangeRequest.getDataInfoId(), + dataChangeRequest.getVersion()); + //if (!resultT) { + // return null; + //} + + System.out.println(String.format( + "Data version has change,and will fetch to update!Request=%s,URL=%s", + dataChangeRequest, channel.getRemoteAddress())); + //TimeUnit.MILLISECONDS.sleep(2000); + + //fireChangFetch(dataChangeRequest); + //if(version%3==0) { + executor.submit(() -> { + + Object result = boltClientFetch.sendSync(boltChannelMapInt.get(Math.abs((MockSyncDataHandler.dataId+version).hashCode() % connectNum)), + new GetDataRequest(MockSyncDataHandler.dataId, DEFAULT_DATA_CENTER), 1000); + GenericResponse genericResponse = (GenericResponse) result; + if (genericResponse.isSuccess()) { + Map map = (Map) genericResponse.getData(); + if (map == null || map.isEmpty()) { + System.out.println(String.format("GetDataRequest get response contains no datum!dataInfoId=%s", + MockSyncDataHandler.dataId)); + } else { + System.out.println(String.format("GetDataRequest get response contains datum!dataInfoId=%s,size=%s", + MockSyncDataHandler.dataId, map.get(DEFAULT_DATA_CENTER).getPubMap().size())); + } + } else { + throw new RuntimeException( + String.format("GetDataRequest has got fail response!dataInfoId:%s msg:%s", MockSyncDataHandler.dataId, + genericResponse.getMessage())); + } + }); + //} + + } catch (Exception e) { + //LOGGER.error("DataChange Request error!", e); + throw new RuntimeException("DataChangeRequest Request error!", e); + } + //System.out.println(String.format("There are not Subscriber Existed! Who are interest with dataInfoId %s !", + // request.getDataInfoId())); + return null; + } + + @Override + public GenericResponse buildFailedResponse(String msg) { + return new GenericResponse().fillFailed(msg); + } + + @Override + public HandlerType getType() { + return HandlerType.PROCESSER; + } + + @Override + public Class interest() { + return DataChangeRequest.class; + } + } + + private static class MockGetDataHandler extends AbstractServerHandler { + public void checkParam(GetDataRequest request) throws RuntimeException { + ParaCheckUtil.checkNotBlank(request.getDataInfoId(), "GetDataRequest.dataInfoId"); + } + + @Override + public Object doHandle(Channel channel, GetDataRequest request) { + Map map = new HashMap<>(); + map.put(DEFAULT_DATA_CENTER, datum); + + return new GenericResponse>().fillSucceed(map); + } + + @Override + public GenericResponse> buildFailedResponse(String msg) { + return new GenericResponse>().fillFailed(msg); + } + + @Override + public HandlerType getType() { + return HandlerType.PROCESSER; + } + + @Override + public Class interest() { + return GetDataRequest.class; + } + + @Override + protected Node.NodeType getConnectNodeType() { + return Node.NodeType.DATA; + } + } + + private class NotifyCallback implements CallbackHandler { + + private int retryTimes = 0; + private Connection connection; + private DataChangeRequest request; + + public NotifyCallback(Connection connection, DataChangeRequest request) { + this.connection = connection; + this.request = request; + } + + @Override + public void onCallback(Channel channel, Object message) { + CommonResponse result = (CommonResponse) message; + if (result != null && !result.isSuccess()) { + System.out + .println(String + .format( + "response not success when notify sessionServer(%s), retryTimes=%s, request=%s, response=%s", + connection.getRemoteAddress(), retryTimes, request, result)); + //onFailed(this); + } + } + + @Override + public void onException(Channel channel, Throwable e) { + System.err.println(String.format( + "exception when notify sessionServer(%s), retryTimes=%s, request=%s,error=%s", + connection.getRemoteAddress(), retryTimes, request, e)); + e.printStackTrace(); + //onFailed(this); + } + + @Override + public Executor getExecutor() { + return null; + } + + } +} diff --git a/test/src/test/java/com/alipay/sofa/registry/test/sync/SyncDataHandlerTest.java b/test/src/test/java/com/alipay/sofa/registry/test/sync/SyncDataHandlerTest.java index f9fc97667..e9985e30b 100644 --- a/test/src/test/java/com/alipay/sofa/registry/test/sync/SyncDataHandlerTest.java +++ b/test/src/test/java/com/alipay/sofa/registry/test/sync/SyncDataHandlerTest.java @@ -56,6 +56,8 @@ public void doTest() throws Exception { // request syncData DataNodeExchanger dataNodeExchanger = dataApplicationContext.getBean("dataNodeExchanger", DataNodeExchanger.class); + URL url = new URL(LOCAL_ADDRESS, syncDataPort); + dataNodeExchanger.connect(url); GenericResponse genericResponse = (GenericResponse) dataNodeExchanger.request( new Request() { @Override @@ -67,7 +69,7 @@ public Object getRequestBody() { @Override public URL getRequestUrl() { - return new URL(LOCAL_ADDRESS, syncDataPort); + return url; } }).getResult();