Skip to content

Commit

Permalink
[ISSUE apache#60] add SDK manager (apache#62)
Browse files Browse the repository at this point in the history
* feat: add SDK manager

* fix: SDKManagerTest

* fix: use SimpleEntry to replace Pair

* style: better name for SDK codes

* style

* style

* style: just rename SDK

* fix: checkstyle
  • Loading branch information
Lambert-Rao authored Mar 15, 2024
1 parent a165e04 commit 058ba06
Show file tree
Hide file tree
Showing 24 changed files with 998 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK;

public abstract class AbstractSDKOperation<T> implements SDKOperation<T> {

protected T castClient(Object client) {
try {
return (T) client;
} catch (ClassCastException e) {
throw new IllegalArgumentException("Client is not of the expected type", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK;

import org.apache.eventmesh.dashboard.console.function.SDK.config.CreateSDKConfig;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.NacosConfigSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.NacosNamingSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.NacosSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RedisSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RocketMQProduceSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RocketMQPushConsumerSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.operation.RocketMQRemotingSDKOperation;

import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;



/**
* SDK manager is a singleton to manage all SDK clients, it is a facade to create, delete and get a client.
*/
public class SDKManager {

private static final SDKManager INSTANCE = new SDKManager();


public static SDKManager getInstance() {
return INSTANCE;
}

/**
* inner key is the unique key of a client, such as (ip + port) they are defined in CreateClientConfig
*
* @see CreateSDKConfig#getUniqueKey()
*/

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

private final Map<SDKTypeEnum, SDKOperation<?>> clientCreateOperationMap = new ConcurrentHashMap<>();

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

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

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

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

}

private SDKManager() {
}

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) {

Map<String, Object> clients = this.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);
result = clientCreateOperation.createClient(config);
clients.put(result.getKey(), result.getValue());
}
try {
return (SimpleEntry<String, T>) result;
} catch (Exception e) {
throw new RuntimeException("create client error", e);
}
}

public void deleteClient(SDKTypeEnum clientTypeEnum, String uniqueKey) {
Map<String, Object> clients = this.clientMap.get(clientTypeEnum);
SDKOperation<?> operation = this.clientCreateOperationMap.get(clientTypeEnum);
try {
operation.close(clients.get(uniqueKey));
} catch (Exception e) {
throw new RuntimeException("close client error", e);
}
clients.remove(uniqueKey);
}

public Object getClient(SDKTypeEnum clientTypeEnum, String uniqueKey) {
return this.clientMap.get(clientTypeEnum).get(uniqueKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK;

import org.apache.eventmesh.dashboard.console.function.SDK.config.CreateSDKConfig;

import java.util.AbstractMap.SimpleEntry;

/**
* Operation to create and close a client, the operations will be store in the SDKManager
*
* @param <T> SDK client
*/
public interface SDKOperation<T> {

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


public void close(Object client);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK;

public enum SDKTypeEnum {

RUNTIME,

STORAGE_ROCKETMQ_REMOTING,

STORAGE_ROCKETMQ_PRODUCER,

STORAGE_ROCKETMQ_CONSUMER,

STORAGE_REDIS,

META_NACOS,
META_NACOS_CONFIG,

META_NACOS_NAMING,


}
Original file line number Diff line number Diff line change
@@ -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 org.apache.eventmesh.dashboard.console.function.SDK.config;

import lombok.Data;

@Data
public class CreateNacosConfig implements CreateSDKConfig {

private String serverAddress;

@Override
public String getUniqueKey() {
return serverAddress;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK.config;

import lombok.Data;

@Data
public class CreateRedisConfig implements CreateSDKConfig {

private String redisUrl;

@Override
public String getUniqueKey() {
return redisUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK.config;

import org.apache.rocketmq.client.consumer.listener.MessageListener;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import lombok.Data;

@Data
public class CreateRocketmqConfig implements CreateSDKConfig {

// common
private String nameServerUrl;
private String brokerUrl;

//consumer
private String consumerGroup;
private MessageModel messageModel = MessageModel.CLUSTERING;

//producer
private String producerGroup;

//topic
private String topic;
private String subExpression = "*";

private MessageListener messageListener;


@Override
public String getUniqueKey() {
if (nameServerUrl != null) {
return nameServerUrl;
} else if (brokerUrl != null) {
return brokerUrl;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
* limitations under the License.
*/

package org.apache.eventmesh.dashboard.console.function.health.check.impl;
package org.apache.eventmesh.dashboard.console.function.SDK.config;

public class StorageRocketmqCheck {
/**
* Config to create an SDK client, usually contains an address url.
*/
public interface CreateSDKConfig {

String getUniqueKey();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 org.apache.eventmesh.dashboard.console.function.SDK.operation;

import org.apache.eventmesh.dashboard.console.function.SDK.AbstractSDKOperation;
import org.apache.eventmesh.dashboard.console.function.SDK.config.CreateSDKConfig;

import java.util.AbstractMap.SimpleEntry;

public class EtcdSDKOperation extends AbstractSDKOperation {
@Override
public SimpleEntry createClient(CreateSDKConfig clientConfig) {
return null;
}

@Override
public void close(Object client) {

}
}
Loading

0 comments on commit 058ba06

Please sign in to comment.