|
| 1 | +/** |
| 2 | + * Copyright (c) 2019. Qubole Inc |
| 3 | + * Licensed under the Apache License, Version 2.0 (the License); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an AS IS BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. See accompanying LICENSE file. |
| 12 | + */ |
| 13 | +package com.qubole.rubix.client.robotframework.container.client; |
| 14 | + |
| 15 | +import com.qubole.rubix.client.robotframework.container.server.RequestServer; |
| 16 | + |
| 17 | +import java.rmi.NotBoundException; |
| 18 | +import java.rmi.RemoteException; |
| 19 | +import java.rmi.registry.LocateRegistry; |
| 20 | +import java.rmi.registry.Registry; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +public class ContainerRequestClient |
| 25 | +{ |
| 26 | + private static final String REQUEST_SERVER_NAME = "ContainerRequestServer"; |
| 27 | + private static final int REGISTRY_PORT = 1099; |
| 28 | + private static final String FILE_SCHEME = "file:"; |
| 29 | + |
| 30 | + public ContainerRequestClient() |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Get the current cache metrics from the BookKeeper server on a particular node. |
| 36 | + * |
| 37 | + * @param host The hostname of the container to connect to. |
| 38 | + * @return A map of metrics describing cache statistics and interactions for that node. |
| 39 | + */ |
| 40 | + public Map<String, Double> getCacheMetricsForNode(String host) |
| 41 | + { |
| 42 | + try { |
| 43 | + final RequestServer containerServer = getRequestServer(host); |
| 44 | + return containerServer.getCacheMetrics(new GetCacheMetricsRequest()); |
| 45 | + } |
| 46 | + catch (RemoteException | NotBoundException e) { |
| 47 | + System.err.println("ContainerRequestClient exception:"); |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + return new HashMap<>(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Read data from a given file into the BookKeeper cache using a client caching file system. |
| 55 | + * |
| 56 | + * @param host The hostname of the container to connect to. |
| 57 | + * @param remotePath The remote path location. |
| 58 | + * @param readStart The block to start reading from. |
| 59 | + * @param length The amount of data to read. |
| 60 | + * @param fileSize The length of the file. |
| 61 | + * @param lastModified The time at which the file was last modified. |
| 62 | + * @param clusterType The type id of cluster being used. |
| 63 | + * @return True if the data was read into the cache correctly, false otherwise. |
| 64 | + */ |
| 65 | + public boolean cacheDataUsingClientFileSystemForNode( |
| 66 | + String host, |
| 67 | + String remotePath, |
| 68 | + long readStart, |
| 69 | + int length, |
| 70 | + long fileSize, |
| 71 | + long lastModified, |
| 72 | + int clusterType) |
| 73 | + { |
| 74 | + ReadDataRequestParams params = new ReadDataRequestParams( |
| 75 | + getPathWithFileScheme(remotePath), |
| 76 | + readStart, |
| 77 | + length, |
| 78 | + fileSize, |
| 79 | + lastModified, |
| 80 | + clusterType); |
| 81 | + |
| 82 | + try { |
| 83 | + RequestServer containerServer = getRequestServer(host); |
| 84 | + return containerServer.cacheDataUsingClientFileSystem(new ReadDataWithFileSystemRequest(), params); |
| 85 | + } |
| 86 | + catch (RemoteException | NotBoundException e) { |
| 87 | + System.err.println("ContainerRequestClient exception:"); |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Locates a {@link RequestServer} for executing requests on a particular container. |
| 95 | + * |
| 96 | + * @param host The hostname for the container to connect to. |
| 97 | + * @return The {@link RequestServer} used for executing requests. |
| 98 | + * @throws RemoteException if the registry could not be located or communicated with. |
| 99 | + * @throws NotBoundException if the registry has not been bould. |
| 100 | + */ |
| 101 | + private static RequestServer getRequestServer(String host) throws RemoteException, NotBoundException |
| 102 | + { |
| 103 | + Registry registry = LocateRegistry.getRegistry(host, REGISTRY_PORT); |
| 104 | + return (RequestServer) registry.lookup(REQUEST_SERVER_NAME); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Add the file scheme to the provided path for proper execution with the BookKeeper server. |
| 109 | + * |
| 110 | + * @param path The path to update. |
| 111 | + * @return The provided path with the file scheme. |
| 112 | + */ |
| 113 | + private String getPathWithFileScheme(String path) |
| 114 | + { |
| 115 | + return FILE_SCHEME + path; |
| 116 | + } |
| 117 | +} |
0 commit comments