-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the capability to query the Publisher information of dataInfoId i…
…n the Session
- Loading branch information
Showing
7 changed files
with
306 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../main/java/com/alipay/sofa/registry/common/model/sessionserver/QueryPublisherRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.alipay.sofa.registry.common.model.sessionserver; | ||
|
||
import com.alipay.sofa.registry.util.StringFormatter; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @author huicha | ||
* @date 2024/12/23 | ||
*/ | ||
public class QueryPublisherRequest implements Serializable { | ||
|
||
private static final long serialVersionUID = 5295572570779995725L; | ||
|
||
private final String dataInfoId; | ||
|
||
public QueryPublisherRequest(String dataInfoId) { | ||
this.dataInfoId = dataInfoId; | ||
} | ||
|
||
public String getDataInfoId() { | ||
return dataInfoId; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringFormatter.format("QueryPublisherRequest={}}", dataInfoId); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...el/src/main/java/com/alipay/sofa/registry/common/model/sessionserver/SimplePublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.alipay.sofa.registry.common.model.sessionserver; | ||
|
||
import com.alipay.sofa.registry.util.StringFormatter; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @author huicha | ||
* @date 2024/12/23 | ||
*/ | ||
public final class SimplePublisher implements Serializable { | ||
|
||
private static final long serialVersionUID = 6861155219172594665L; | ||
|
||
private final String clientId; | ||
|
||
private final String sourceAddress; | ||
|
||
private final String appName; | ||
|
||
public SimplePublisher(String clientId, String sourceAddress, String appName) { | ||
this.clientId = clientId; | ||
this.sourceAddress = sourceAddress; | ||
this.appName = appName; | ||
} | ||
|
||
public String getClientId() { | ||
return clientId; | ||
} | ||
|
||
public String getSourceAddress() { | ||
return sourceAddress; | ||
} | ||
|
||
public String getAppName() { | ||
return appName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringFormatter.format( | ||
"SimplePublisher{app={},clientId={},add={}}", appName, clientId, sourceAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...y/sofa/registry/server/session/remoting/console/handler/QueryPublisherRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.alipay.sofa.registry.server.session.remoting.console.handler; | ||
|
||
import com.alipay.sofa.registry.common.model.GenericResponse; | ||
import com.alipay.sofa.registry.common.model.PublisherUtils; | ||
import com.alipay.sofa.registry.common.model.sessionserver.QueryPublisherRequest; | ||
import com.alipay.sofa.registry.common.model.store.Publisher; | ||
import com.alipay.sofa.registry.remoting.Channel; | ||
import com.alipay.sofa.registry.server.session.bootstrap.ExecutorManager; | ||
import com.alipay.sofa.registry.server.session.store.DataStore; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* @author huicha | ||
* @date 2024/12/23 | ||
*/ | ||
public class QueryPublisherRequestHandler extends AbstractConsoleHandler<QueryPublisherRequest> { | ||
|
||
@Autowired | ||
protected DataStore sessionDataStore; | ||
|
||
@Override | ||
public Object doHandle(Channel channel, QueryPublisherRequest request) { | ||
Collection<Publisher> publishers = sessionDataStore.getDatas(request.getDataInfoId()); | ||
return new GenericResponse().fillSucceed(PublisherUtils.convert(publishers)); | ||
} | ||
|
||
@Override | ||
public Object buildFailedResponse(String msg) { | ||
return new GenericResponse().fillFailed(msg); | ||
} | ||
|
||
@Override | ||
public Class interest() { | ||
return QueryPublisherRequest.class; | ||
} | ||
|
||
@VisibleForTesting | ||
public QueryPublisherRequestHandler setSessionDataStore(DataStore sessionDataStore) { | ||
this.sessionDataStore = sessionDataStore; | ||
return this; | ||
} | ||
|
||
@VisibleForTesting | ||
public QueryPublisherRequestHandler setExecutorManager(ExecutorManager executorManager) { | ||
this.executorManager = executorManager; | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...fa/registry/server/session/remoting/console/handler/QueryPublisherRequestHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.alipay.sofa.registry.server.session.remoting.console.handler; | ||
|
||
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.sessionserver.QueryPublisherRequest; | ||
import com.alipay.sofa.registry.common.model.sessionserver.SimplePublisher; | ||
import com.alipay.sofa.registry.common.model.store.Publisher; | ||
import com.alipay.sofa.registry.common.model.store.URL; | ||
import com.alipay.sofa.registry.remoting.ChannelHandler; | ||
import com.alipay.sofa.registry.server.session.TestUtils; | ||
import com.alipay.sofa.registry.server.session.bootstrap.ExecutorManager; | ||
import com.alipay.sofa.registry.server.session.store.DataStore; | ||
import org.apache.commons.collections.CollectionUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author huicha | ||
* @date 2024/12/23 | ||
*/ | ||
public class QueryPublisherRequestHandlerTest { | ||
|
||
|
||
@Test | ||
public void testHandle() { | ||
String dataInfoId = "test-data-info-id"; | ||
|
||
List<Publisher> mockPublishers = new ArrayList<>(); | ||
for (int index = 0; index < 3; index++) { | ||
Publisher mockPublisher = new Publisher(); | ||
mockPublisher.setDataInfoId(dataInfoId); | ||
mockPublisher.setClientId("ClientId-" + index); | ||
mockPublisher.setSourceAddress(URL.valueOf("127.0.0." + index + ":1234")); | ||
mockPublisher.setAppName("App"); | ||
mockPublishers.add(mockPublisher); | ||
} | ||
|
||
DataStore dataStore = mock(DataStore.class); | ||
when(dataStore.getDatas(Mockito.eq(dataInfoId))).thenReturn(mockPublishers); | ||
|
||
QueryPublisherRequestHandler handler = new QueryPublisherRequestHandler(); | ||
handler | ||
.setExecutorManager(new ExecutorManager(TestUtils.newSessionConfig("testDc"))) | ||
.setSessionDataStore(dataStore); | ||
|
||
Assert.assertNotNull(handler.getExecutor()); | ||
Assert.assertEquals(handler.interest(), QueryPublisherRequest.class); | ||
Assert.assertEquals(handler.getConnectNodeType(), Node.NodeType.CONSOLE); | ||
Assert.assertEquals(handler.getType(), ChannelHandler.HandlerType.PROCESSER); | ||
Assert.assertEquals(handler.getInvokeType(), ChannelHandler.InvokeType.SYNC); | ||
Assert.assertFalse(((CommonResponse) handler.buildFailedResponse("msg")).isSuccess()); | ||
|
||
QueryPublisherRequest notExistReq = new QueryPublisherRequest("not-exist"); | ||
GenericResponse<List<SimplePublisher>> notExistResp = (GenericResponse) handler.doHandle(null, notExistReq); | ||
Assert.assertTrue(notExistResp.isSuccess()); | ||
List<SimplePublisher> notExistPublishers = notExistResp.getData(); | ||
Assert.assertTrue(CollectionUtils.isEmpty(notExistPublishers)); | ||
|
||
QueryPublisherRequest existReq = new QueryPublisherRequest(dataInfoId); | ||
GenericResponse<List<SimplePublisher>> existResp = (GenericResponse) handler.doHandle(null, existReq); | ||
Assert.assertTrue(existResp.isSuccess()); | ||
List<SimplePublisher> existPublishers = existResp.getData(); | ||
Assert.assertFalse(CollectionUtils.isEmpty(existPublishers)); | ||
Assert.assertEquals(3, existPublishers.size()); | ||
|
||
for (int index = 0; index < existPublishers.size(); index++) { | ||
SimplePublisher existPublisher = existPublishers.get(index); | ||
|
||
String clientId = existPublisher.getClientId(); | ||
String sourceAddr = existPublisher.getSourceAddress(); | ||
String appName = existPublisher.getAppName(); | ||
|
||
Assert.assertEquals("ClientId-" + index, clientId); | ||
Assert.assertEquals("127.0.0." + index + ":1234", sourceAddr); | ||
Assert.assertEquals("App", appName); | ||
} | ||
} | ||
|
||
} |