-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-13033 Java thin client: Service invocation #7908
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
modules/core/src/main/java/org/apache/ignite/client/ClientServices.java
This file contains hidden or 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,56 @@ | ||
| /* | ||
| * 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.ignite.client; | ||
|
|
||
| /** | ||
| * Thin client services facade. | ||
| */ | ||
| public interface ClientServices { | ||
| /** | ||
| * Gets the cluster group to which this {@code ClientServices} instance belongs. | ||
| * | ||
| * @return Cluster group to which this {@code ClientServices} instance belongs. | ||
| */ | ||
| public ClientClusterGroup clusterGroup(); | ||
|
|
||
| /** | ||
| * Gets a remote handle on the service. | ||
| * <p> | ||
| * Note: There are no guarantees that each method invocation for the same proxy will always contact the same remote | ||
| * service (on the same remote node). | ||
| * | ||
| * @param name Service name. | ||
| * @param svcItf Interface for the service. | ||
| * @return Proxy over remote service. | ||
| */ | ||
| public <T> T serviceProxy(String name, Class<? super T> svcItf); | ||
|
|
||
| /** | ||
| * Gets a remote handle on the service with timeout. | ||
| * <p> | ||
| * Note: There are no guarantees that each method invocation for the same proxy will always contact the same remote | ||
| * service (on the same remote node). | ||
| * | ||
| * @param name Service name. | ||
| * @param svcItf Interface for the service. | ||
| * @param timeout If greater than 0 created proxy will wait for service availability only specified time, | ||
| * and will limit remote service invocation time. | ||
| * @return Proxy over remote service. | ||
| */ | ||
| public <T> T serviceProxy(String name, Class<? super T> svcItf, long timeout); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
180 changes: 180 additions & 0 deletions
180
modules/core/src/main/java/org/apache/ignite/internal/client/thin/ClientServicesImpl.java
This file contains hidden or 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,180 @@ | ||
| /* | ||
| * 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.ignite.internal.client.thin; | ||
|
|
||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Proxy; | ||
| import java.util.Collection; | ||
| import java.util.UUID; | ||
| import org.apache.ignite.client.ClientClusterGroup; | ||
| import org.apache.ignite.client.ClientException; | ||
| import org.apache.ignite.client.ClientServices; | ||
| import org.apache.ignite.internal.binary.BinaryRawWriterEx; | ||
| import org.apache.ignite.internal.util.typedef.F; | ||
| import org.apache.ignite.internal.util.typedef.internal.A; | ||
| import org.apache.ignite.platform.PlatformServiceMethod; | ||
|
|
||
| /** | ||
| * Implementation of {@link ClientServices}. | ||
| */ | ||
| class ClientServicesImpl implements ClientServices { | ||
| /** Channel. */ | ||
| private final ReliableChannel ch; | ||
|
|
||
| /** Binary marshaller. */ | ||
| private final ClientBinaryMarshaller marsh; | ||
|
|
||
| /** Utils for serialization/deserialization. */ | ||
| private final ClientUtils utils; | ||
|
|
||
| /** Cluster group. */ | ||
| private final ClientClusterGroupImpl grp; | ||
|
|
||
| /** Constructor. */ | ||
| ClientServicesImpl(ReliableChannel ch, ClientBinaryMarshaller marsh, ClientClusterGroupImpl grp) { | ||
| this.ch = ch; | ||
| this.marsh = marsh; | ||
| this.grp = grp; | ||
|
|
||
| utils = new ClientUtils(marsh); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public ClientClusterGroup clusterGroup() { | ||
| return grp; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public <T> T serviceProxy(String name, Class<? super T> svcItf) { | ||
| return serviceProxy(name, svcItf, 0); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public <T> T serviceProxy(String name, Class<? super T> svcItf, long timeout) { | ||
| A.notNullOrEmpty(name, "name"); | ||
| A.notNull(svcItf, "svcItf"); | ||
|
|
||
| return (T)Proxy.newProxyInstance(svcItf.getClassLoader(), new Class[] {svcItf}, | ||
| new ServiceInvocationHandler<>(name, timeout, grp)); | ||
| } | ||
|
|
||
| /** | ||
| * Gets services facade over the specified cluster group. | ||
| * | ||
| * @param grp Cluster group. | ||
| */ | ||
| ClientServices withClusterGroup(ClientClusterGroupImpl grp) { | ||
| A.notNull(grp, "grp"); | ||
|
|
||
| return new ClientServicesImpl(ch, marsh, grp); | ||
| } | ||
|
|
||
| /** | ||
| * Service invocation handler. | ||
| */ | ||
| private class ServiceInvocationHandler<T> implements InvocationHandler { | ||
| /** Flag "Has parameter types" mask. */ | ||
| private static final byte FLAG_PARAMETER_TYPES_MASK = 0x02; | ||
|
|
||
| /** Service name. */ | ||
| private final String name; | ||
|
|
||
| /** Timeout. */ | ||
| private final long timeout; | ||
|
|
||
| /** Cluster group. */ | ||
| private final ClientClusterGroupImpl grp; | ||
|
|
||
| /** | ||
| * @param name Service name. | ||
| * @param timeout Timeout. | ||
| */ | ||
| private ServiceInvocationHandler(String name, long timeout, ClientClusterGroupImpl grp) { | ||
| this.name = name; | ||
| this.timeout = timeout; | ||
| this.grp = grp; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
| try { | ||
| Collection<UUID> nodeIds = grp.nodeIds(); | ||
|
|
||
| if (nodeIds != null && nodeIds.isEmpty()) | ||
| throw new ClientException("Cluster group is empty."); | ||
|
|
||
| return ch.service(ClientOperation.SERVICE_INVOKE, | ||
| req -> writeServiceInvokeRequest(req, nodeIds, method, args), | ||
| res -> utils.readObject(res.in(), false) | ||
| ); | ||
| } | ||
| catch (ClientError e) { | ||
| throw new ClientException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param ch Payload output channel. | ||
| */ | ||
| private void writeServiceInvokeRequest( | ||
| PayloadOutputChannel ch, | ||
| Collection<UUID> nodeIds, | ||
| Method method, | ||
| Object[] args | ||
| ) { | ||
| ch.clientChannel().protocolCtx().checkFeatureSupported(ProtocolBitmaskFeature.SERVICE_INVOKE); | ||
|
|
||
| try (BinaryRawWriterEx writer = utils.createBinaryWriter(ch.out())) { | ||
| writer.writeString(name); | ||
| writer.writeByte(FLAG_PARAMETER_TYPES_MASK); // Flags. | ||
| writer.writeLong(timeout); | ||
|
|
||
| if (nodeIds == null) | ||
| writer.writeInt(0); | ||
| else { | ||
| writer.writeInt(nodeIds.size()); | ||
|
|
||
| for (UUID nodeId : nodeIds) { | ||
| writer.writeLong(nodeId.getMostSignificantBits()); | ||
| writer.writeLong(nodeId.getLeastSignificantBits()); | ||
| } | ||
| } | ||
|
|
||
| PlatformServiceMethod ann = method.getDeclaredAnnotation(PlatformServiceMethod.class); | ||
|
|
||
| writer.writeString(ann != null ? ann.value() : method.getName()); | ||
|
|
||
| Class<?>[] paramTypes = method.getParameterTypes(); | ||
|
|
||
| if (F.isEmpty(args)) | ||
| writer.writeInt(0); | ||
| else { | ||
| writer.writeInt(args.length); | ||
|
|
||
| assert args.length == paramTypes.length : "args=" + args.length + ", types=" + paramTypes.length; | ||
|
|
||
| for (int i = 0; i < args.length; i++) { | ||
| writer.writeInt(marsh.context().typeId(paramTypes[i].getName())); | ||
| writer.writeObject(args[i]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.