Skip to content

Commit

Permalink
Add SDKManager JavaDoc and some code optimisation (#121)
Browse files Browse the repository at this point in the history
* SDKManager optimisation and doc

* doc

* doc

* doc
  • Loading branch information
Alonexc authored Apr 17, 2024
1 parent e38db0f commit b43812a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 44 deletions.
21 changes: 0 additions & 21 deletions eventmesh-dashboard-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,6 @@
<version>0.7.7</version>
</dependency>

<!-- health check client -->
<!-- EventMesh SDK -->
<!-- <dependency>-->
<!-- <groupId>org.apache.eventmesh</groupId>-->
<!-- <artifactId>eventmesh-sdk-java</artifactId>-->
<!-- <version>1.10.0-release</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit-dep</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-slf4j-impl</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- storage redis client -->
<dependency>
<groupId>io.lettuce</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class SDKManager {

private static volatile SDKManager INSTANCE = null;


public static synchronized SDKManager getInstance() {
if (INSTANCE == null) {
synchronized (SDKManager.class) {
Expand All @@ -62,60 +61,77 @@ public static synchronized SDKManager getInstance() {

/**
* inner key is the unique key of a client, such as (ip + port) they are defined in CreateClientConfig
*
* <p>
* key: SDKTypeEnum
* value: A map collection is used with key being (ip+port) and value being client.
* @see CreateSDKConfig#getUniqueKey()
*/
private static final Map<SDKTypeEnum, Map<String, Object>> clientMap = new ConcurrentHashMap<>();

private final Map<SDKTypeEnum, Map<String, Object>> clientMap = new ConcurrentHashMap<>();

private final Map<SDKTypeEnum, SDKOperation<?>> clientCreateOperationMap = new ConcurrentHashMap<>();
/**
* Initialise the SDKOperation object instance according to SDKTypeEnum.
* <p>
* key: SDKTypeEnum
* value: SDKOperation
* @see SDKTypeEnum
* @see SDKOperation
*/
private static final Map<SDKTypeEnum, SDKOperation<?>> clientCreateOperationMap = new ConcurrentHashMap<>();

// register all client create operation
{
static {
for (SDKTypeEnum clientTypeEnum : SDKTypeEnum.values()) {
clientMap.put(clientTypeEnum, new ConcurrentHashMap<>());
}

// redis
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_REDIS, new RedisSDKOperation());

// rocketmq
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_ROCKETMQ_REMOTING, new RocketMQRemotingSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_ROCKETMQ_PRODUCER, new RocketMQProduceSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.STORAGE_ROCKETMQ_CONSUMER, new RocketMQPushConsumerSDKOperation());

// nacos
clientCreateOperationMap.put(SDKTypeEnum.META_NACOS, new NacosSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.META_NACOS_CONFIG, new NacosConfigSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.META_NACOS_NAMING, new NacosNamingSDKOperation());

// etcd
clientCreateOperationMap.put(SDKTypeEnum.META_ETCD, new EtcdSDKOperation());

// eventmesh_runtime
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_EVENTMESH_CLIENT, new RuntimeSDKOperation());

// eventmesh_runtime_tcp
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_TCP_CLOUDEVENT_CLIENT, new RuntimeTcpCloudEventSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_TCP_EVENTMESH_CLIENT, new RuntimeTcpEventMeshSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_TCP_OPENMESSAGE_CLIENT, new RuntimeTcpOpenMessageSDKOperation());

// eventmesh_runtime_http
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_HTTP_PRODUCER, new RuntimeHttpProducerSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_HTTP_CONSUMER, new RuntimeHttpConsumerSDKOperation());

// eventmesh_runtime_grpc
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_GRPC_PRODUCER, new RuntimeGrpcProducerSDKOperation());
clientCreateOperationMap.put(SDKTypeEnum.RUNTIME_GRPC_CONSUMER, new RuntimeGrpcConsumerSDKOperation());
}

private SDKManager() {
}

/**
* Create SDK client through (SDKTypeEnum) clientTypeEnum, (CreateSDKConfig) config.
*/
public <T> SimpleEntry<String, T> createClient(SDKTypeEnum clientTypeEnum, CreateSDKConfig config) {
return createClient(clientTypeEnum, config.getUniqueKey(), config);
}

public <T> SimpleEntry<String, T> createClient(SDKTypeEnum clientTypeEnum, String uniqueKey, CreateSDKConfig config) {
final String uniqueKey = config.getUniqueKey();

Map<String, Object> clients = this.clientMap.get(clientTypeEnum);
Map<String, Object> clients = clientMap.get(clientTypeEnum);

Object client = clients.get(uniqueKey);
SimpleEntry<String, ?> result = new SimpleEntry<>(uniqueKey, client);
if (Objects.isNull(client)) {
SDKOperation<?> clientCreateOperation = this.clientCreateOperationMap.get(clientTypeEnum);
SDKOperation<?> clientCreateOperation = clientCreateOperationMap.get(clientTypeEnum);
result = clientCreateOperation.createClient(config);
clients.put(result.getKey(), result.getValue());
}
Expand All @@ -127,8 +143,8 @@ public <T> SimpleEntry<String, T> createClient(SDKTypeEnum clientTypeEnum, Strin
}

public void deleteClient(SDKTypeEnum clientTypeEnum, String uniqueKey) {
Map<String, Object> clients = this.clientMap.get(clientTypeEnum);
SDKOperation<?> operation = this.clientCreateOperationMap.get(clientTypeEnum);
Map<String, Object> clients = clientMap.get(clientTypeEnum);
SDKOperation<?> operation = clientCreateOperationMap.get(clientTypeEnum);
try {
operation.close(clients.get(uniqueKey));
} catch (Exception e) {
Expand All @@ -138,11 +154,11 @@ public void deleteClient(SDKTypeEnum clientTypeEnum, String uniqueKey) {
}

public Object getClient(SDKTypeEnum clientTypeEnum, String uniqueKey) {
return this.clientMap.get(clientTypeEnum).get(uniqueKey);
return clientMap.get(clientTypeEnum).get(uniqueKey);
}

// get all client
public Map<String, Object> getClients(SDKTypeEnum clientTypeEnum) {
return this.clientMap.get(clientTypeEnum);
return clientMap.get(clientTypeEnum);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
*/
public interface SDKOperation<T> {

public SimpleEntry<String, T> createClient(CreateSDKConfig clientConfig);
SimpleEntry<String, T> createClient(CreateSDKConfig clientConfig);


public void close(Object client);
void close(Object client);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

package org.apache.eventmesh.dashboard.core.function.SDK.config;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreateNacosConfig implements CreateSDKConfig {

private String serverAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Properties;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;

Expand All @@ -39,7 +40,7 @@ public SimpleEntry<String, ConfigService> createClient(CreateSDKConfig clientCon
CreateNacosConfig config = (CreateNacosConfig) clientConfig;
try {
Properties properties = new Properties();
properties.put("serverAddr", config.getServerAddress());
properties.put(PropertyKeyConst.SERVER_ADDR, config.getServerAddress());
configService = NacosFactory.createConfigService(properties);
} catch (NacosException e) {
log.error("NacosCheck init failed caused by {}", e.getErrMsg());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Properties;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
Expand All @@ -40,7 +41,7 @@ public SimpleEntry<String, NamingService> createClient(CreateSDKConfig clientCon
CreateNacosConfig config = (CreateNacosConfig) clientConfig;
try {
Properties properties = new Properties();
properties.put("serverAddr", config.getServerAddress());
properties.put(PropertyKeyConst.SERVER_ADDR, config.getServerAddress());
namingService = NacosFactory.createNamingService(properties);
} catch (NacosException e) {
log.error("NacosCheck init failed caused by {}", e.getErrMsg());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void setUp() {
.password("")
.timeOut(30)
.build();
// createRedisConfig.setRedisUrl("redis://localhost:6379");
redisKey = SDKManager.getInstance().createClient(SDKTypeEnum.STORAGE_REDIS, createRedisConfig).getKey();
} catch (Exception e) {
log.warn("SDK manager test init failed, possible reason: redis-server is offline. {}", this.getClass().getSimpleName(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public interface ConnectorRemotingService {

public GetConnectorResult getConnectors(GetConnectorRequest getConnectorRequest);
GetConnectorResult getConnectors(GetConnectorRequest getConnectorRequest);

public CreateConnectorResult createConnector(CreateConnectorRequest createConnectorRequest);
CreateConnectorResult createConnector(CreateConnectorRequest createConnectorRequest);
}

0 comments on commit b43812a

Please sign in to comment.