Search in sources :

Example 11 with ConnectionConfig

use of co.cask.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class RemoteSparkManager method getServiceURL.

@Override
public URL getServiceURL(long timeout, TimeUnit timeoutUnit) {
    try {
        Tasks.waitFor(true, () -> {
            try {
                checkAvailability();
                return true;
            } catch (ServiceUnavailableException e) {
                return false;
            }
        }, timeout, timeoutUnit);
        ConnectionConfig connectionConfig = clientConfig.getConnectionConfig();
        return ServiceDiscoverable.createServiceBaseURL(connectionConfig.getHostname(), connectionConfig.getPort(), connectionConfig.isSSLEnabled(), programId);
    } catch (TimeoutException e) {
        return null;
    } catch (Exception e) {
        LOG.warn("Exception raised when waiting for Spark service to be available", e);
        return null;
    }
}
Also used : ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) ConnectionConfig(co.cask.cdap.client.config.ConnectionConfig) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) NotFoundException(co.cask.cdap.common.NotFoundException) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with ConnectionConfig

use of co.cask.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class RuntimeMonitorTest method testRunTimeMonitor.

@Test
public void testRunTimeMonitor() throws Exception {
    Map<String, String> topics = new HashMap<>();
    topics.put(Constants.AppFabric.PROGRAM_STATUS_RECORD_EVENT_TOPIC, "status");
    httpServer.createContext("/v3/runtime/monitor/topics", new HttpHandler() {

        public void handle(HttpExchange exchange) throws IOException {
            byte[] response = GSON.toJson(topics).getBytes();
            exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
            exchange.getResponseBody().write(response);
            exchange.close();
        }
    });
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(address.getHostName()).setPort(1234).setSSLEnabled(false).build();
    ClientConfig.Builder clientConfigBuilder = ClientConfig.builder().setDefaultReadTimeout(20000).setConnectionConfig(connectionConfig);
    int limit = 2;
    MessagingContext messagingContext = new MultiThreadMessagingContext(messagingService);
    RuntimeMonitor runtimeMonitor = new RuntimeMonitor(new ProgramRunId("test", "app1", ProgramType.WORKFLOW, "p1", "run1"), cConf, messagingContext.getMessagePublisher(), clientConfigBuilder.build());
    Map<String, List<MonitorMessage>> messages = new LinkedHashMap<>();
    ArrayList<MonitorMessage> list = new ArrayList<>();
    list.add(new MonitorMessage("1", "message1"));
    list.add(new MonitorMessage("2", "message2"));
    list.add(new MonitorMessage("3", "message3"));
    list.add(new MonitorMessage("4", "message4"));
    list.add(new MonitorMessage("5", "message5"));
    list.add(new MonitorMessage("6", "message6"));
    list.add(new MonitorMessage("7", "message7"));
    list.add(new MonitorMessage("8", "message8"));
    list.add(new MonitorMessage("9", "message9"));
    list.add(new MonitorMessage("10", "message10"));
    messages.put("status", list);
    httpServer.createContext("/v3/runtime/metadata", new HttpHandler() {

        int count = 0;

        public void handle(HttpExchange exchange) throws IOException {
            Map<String, List<MonitorMessage>> toSend = new LinkedHashMap<>();
            ArrayList<MonitorMessage> list = new ArrayList<>();
            int start = count;
            int i = 0;
            for (MonitorMessage message : messages.get("status")) {
                if (start <= i && i < start + limit) {
                    list.add(message);
                    count++;
                }
                i++;
            }
            toSend.put("status", list);
            byte[] response = GSON.toJson(toSend).getBytes();
            exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
            exchange.getResponseBody().write(response);
            exchange.close();
        }
    });
    HashSet<String> expected = new LinkedHashSet<>();
    expected.add("message1");
    expected.add("message2");
    expected.add("message3");
    expected.add("message4");
    expected.add("message5");
    expected.add("message6");
    expected.add("message7");
    expected.add("message8");
    expected.add("message9");
    expected.add("message10");
    HashSet<String> actual = new LinkedHashSet<>();
    final String[] messageId = { null };
    runtimeMonitor.startAndWait();
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            MessageFetcher messageFetcher = messagingContext.getMessageFetcher();
            try (CloseableIterator<Message> iter = messageFetcher.fetch(NamespaceId.SYSTEM.getNamespace(), cConf.get(Constants.AppFabric.PROGRAM_STATUS_EVENT_TOPIC), 2, messageId[0])) {
                while (iter.hasNext()) {
                    Message message = iter.next();
                    messageId[0] = message.getId();
                    actual.add(message.getPayloadAsString());
                }
            }
            return expected.size() == actual.size() && expected.equals(actual);
        }
    }, 5, TimeUnit.MINUTES);
    runtimeMonitor.stopAndWait();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MessageFetcher(co.cask.cdap.api.messaging.MessageFetcher) Message(co.cask.cdap.api.messaging.Message) MonitorMessage(co.cask.cdap.internal.app.runtime.monitor.MonitorMessage) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) MultiThreadMessagingContext(co.cask.cdap.internal.app.runtime.messaging.MultiThreadMessagingContext) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfig(co.cask.cdap.client.config.ClientConfig) ConnectionConfig(co.cask.cdap.client.config.ConnectionConfig) HttpHandler(com.sun.net.httpserver.HttpHandler) CloseableIterator(co.cask.cdap.api.dataset.lib.CloseableIterator) RuntimeMonitor(co.cask.cdap.internal.app.runtime.monitor.RuntimeMonitor) HttpExchange(com.sun.net.httpserver.HttpExchange) MonitorMessage(co.cask.cdap.internal.app.runtime.monitor.MonitorMessage) IOException(java.io.IOException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) MessagingContext(co.cask.cdap.api.messaging.MessagingContext) MultiThreadMessagingContext(co.cask.cdap.internal.app.runtime.messaging.MultiThreadMessagingContext) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ConnectionConfig (co.cask.cdap.client.config.ConnectionConfig)12 ClientConfig (co.cask.cdap.client.config.ClientConfig)4 IOException (java.io.IOException)3 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)2 EndpointStrategy (co.cask.cdap.common.discovery.EndpointStrategy)2 RandomEndpointStrategy (co.cask.cdap.common.discovery.RandomEndpointStrategy)2 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 TimeoutException (java.util.concurrent.TimeoutException)2 Discoverable (org.apache.twill.discovery.Discoverable)2 Before (org.junit.Before)2 StandaloneTester (co.cask.cdap.StandaloneTester)1 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)1 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)1 Message (co.cask.cdap.api.messaging.Message)1 MessageFetcher (co.cask.cdap.api.messaging.MessageFetcher)1 MessagingContext (co.cask.cdap.api.messaging.MessagingContext)1