EricJoy2048 commented on code in PR #2301: URL: https://github.com/apache/incubator-seatunnel/pull/2301#discussion_r935377458
########## seatunnel-engine/seatunnel-engine-client/src/main/java/org/apache/seatunnel/engine/client/SeaTunnelHazelcastClient.java: ########## @@ -0,0 +1,106 @@ +/* + * 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.seatunnel.engine.client; + +import org.apache.seatunnel.engine.common.utils.ExceptionUtil; + +import com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl; +import com.hazelcast.client.impl.clientside.HazelcastClientProxy; +import com.hazelcast.client.impl.protocol.ClientMessage; +import com.hazelcast.client.impl.spi.impl.ClientInvocation; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.internal.serialization.SerializationService; +import com.hazelcast.internal.util.Preconditions; +import com.hazelcast.logging.ILogger; +import lombok.NonNull; + +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +public class SeaTunnelHazelcastClient { + private final HazelcastClientInstanceImpl hazelcastClient; + private final SerializationService serializationService; + + public SeaTunnelHazelcastClient(@NonNull SeaTunnelClientConfig seaTunnelClientConfig) { + Preconditions.checkNotNull(seaTunnelClientConfig, "config"); + this.hazelcastClient = + ((HazelcastClientProxy) com.hazelcast.client.HazelcastClient.newHazelcastClient( + seaTunnelClientConfig)).client; + this.serializationService = hazelcastClient.getSerializationService(); + ExceptionUtil.registerSeaTunnelExceptions(hazelcastClient.getClientExceptionFactory()); + } + + public SerializationService getSerializationService() { + return serializationService; + } + + /** + * Returns the underlying Hazelcast IMDG instance used by SeaTunnel Engine Client. It will + * be a client, depending on the type of this + */ + @NonNull + public HazelcastInstance getHazelcastInstance() { + return hazelcastClient; + } + + public ILogger getLogger(Class<?> clazz) { + return hazelcastClient.getLoggingService().getLogger(clazz); + } + + public <S> S requestOnMasterAndDecodeResponse(@NonNull ClientMessage request, + @NonNull Function<ClientMessage, Object> decoder) { + UUID masterUuid = hazelcastClient.getClientClusterService().getMasterMember().getUuid(); + return requestAndDecodeResponse(masterUuid, request, decoder); + } + + public <S> S requestOnAnyMemberAndDecodeResponse(@NonNull ClientMessage request, + @NonNull Function<ClientMessage, Object> decoder) { + return requestAndDecodeResponse(null, request, decoder); + } + + public <S> S requestAndDecodeResponse(UUID uuid, ClientMessage request, + Function<ClientMessage, Object> decoder) { + ClientInvocation invocation = new ClientInvocation(hazelcastClient, request, null, uuid); + try { + ClientMessage response = invocation.invoke().get(); + return serializationService.toObject(decoder.apply(response)); + } catch (Throwable t) { + throw ExceptionUtil.rethrow(t); + } Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
