Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #60] add SDK manager #62

Merged
merged 9 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.client;

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,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.client;

public enum ClientTypeEnum {

RUNTIME,

Lambert-Rao marked this conversation as resolved.
Show resolved Hide resolved
STORAGE_ROCKETMQ_REMOTING,

STORAGE_ROCKETMQ_PRODUCER,

STORAGE_ROCKETMQ_CONSUMER,

STORAGE_REDIS,

CENTER_NACOS,
CENTER_NACOS_CONFIG,

CENTER_NACOS_NAMING,

Lambert-Rao marked this conversation as resolved.
Show resolved Hide resolved

}
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.client;

import org.apache.eventmesh.dashboard.console.function.client.config.CreateClientConfig;
import org.apache.eventmesh.dashboard.console.function.client.operation.NacosConfigSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.operation.NacosNamingSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.operation.NacosSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.operation.RedisSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.operation.RocketMQProduceSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.operation.RocketMQPushConsumerSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.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 CreateClientConfig#getUniqueKey()
*/

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

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

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

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

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

clientCreateOperationMap.put(ClientTypeEnum.CENTER_NACOS, new NacosSDKOperation());
clientCreateOperationMap.put(ClientTypeEnum.CENTER_NACOS_CONFIG, new NacosConfigSDKOperation());
clientCreateOperationMap.put(ClientTypeEnum.CENTER_NACOS_NAMING, new NacosNamingSDKOperation());

}

private SDKManager() {
}

public <T> SimpleEntry<String, T> createClient(ClientTypeEnum clientTypeEnum, CreateClientConfig config) {
return createClient(clientTypeEnum, config.getUniqueKey(), config);
}

public <T> SimpleEntry<String, T> createClient(ClientTypeEnum clientTypeEnum, String uniqueKey, CreateClientConfig 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(ClientTypeEnum 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(ClientTypeEnum 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.client;

import org.apache.eventmesh.dashboard.console.function.client.config.CreateClientConfig;

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(CreateClientConfig clientConfig);


public void close(Object client);

}
Lambert-Rao marked this conversation as resolved.
Show resolved Hide resolved
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.client.config;

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

String getUniqueKey();
}
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.client.config;

import lombok.Data;

@Data
public class CreateNacosConfig implements CreateClientConfig {

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.client.config;

import lombok.Data;

@Data
public class CreateRedisConfig implements CreateClientConfig {

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.client.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 CreateClientConfig {

// 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
@@ -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.client.operation;

import org.apache.eventmesh.dashboard.console.function.client.AbstractSDKOperation;
import org.apache.eventmesh.dashboard.console.function.client.config.CreateClientConfig;

import java.util.AbstractMap.SimpleEntry;

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

@Override
public void close(Object client) {

}
}
Loading
Loading