Search in sources :

Example 21 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class ZooKeeperServerFactory method activateInternal.

private Destroyable activateInternal(BundleContext context, Map<String, ?> configuration) throws Exception {
    LOGGER.info("Creating zookeeper server with: {}", configuration);
    Properties props = new Properties();
    for (Entry<String, ?> entry : configuration.entrySet()) {
        props.put(entry.getKey(), entry.getValue());
    }
    // Remove the dependency on the current dir from dataDir
    String dataDir = props.getProperty("dataDir");
    if (dataDir != null && !Paths.get(dataDir).isAbsolute()) {
        dataDir = runtimeProperties.get().getDataPath().resolve(dataDir).toFile().getAbsolutePath();
        props.setProperty("dataDir", dataDir);
    }
    props.put("clientPortAddress", bootstrapConfiguration.get().getBindAddress());
    // Create myid file
    String serverId = (String) props.get("server.id");
    if (serverId != null) {
        props.remove("server.id");
        File myId = new File(dataDir, "myid");
        if (myId.exists() && !myId.delete()) {
            throw new IOException("Failed to delete " + myId);
        }
        if (myId.getParentFile() == null || (!myId.getParentFile().exists() && !myId.getParentFile().mkdirs())) {
            throw new IOException("Failed to create " + myId.getParent());
        }
        FileOutputStream fos = new FileOutputStream(myId);
        try {
            fos.write((serverId + "\n").getBytes());
        } finally {
            fos.close();
        }
    }
    QuorumPeerConfig peerConfig = getPeerConfig(props);
    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());
        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());
        try {
            LOGGER.debug("Starting quorum peer \"{}\" on address {}", quorumPeer.getMyid(), peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"{}\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn("Failed to start quorum peer \"{}\", reason : {} ", quorumPeer.getMyid(), e.getMessage());
            quorumPeer.shutdown();
            throw e;
        }
        // Register stats provider
        ClusteredServer server = new ClusteredServer(quorumPeer);
        registration = context.registerService(QuorumStats.Provider.class, server, null);
        return server;
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);
        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()), new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {

            protected void configureSaslLogin() throws IOException {
            }
        };
        cnxnFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());
        try {
            LOGGER.debug("Starting ZooKeeper server on address {}", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn("Failed to start ZooKeeper server, reason : {}", e);
            cnxnFactory.shutdown();
            throw e;
        }
        // Register stats provider
        SimpleServer server = new SimpleServer(zkServer, cnxnFactory);
        registration = context.registerService(ServerStats.Provider.class, server, null);
        startCleanupManager(serverConfig, props);
        return server;
    }
}
Also used : IOException(java.io.IOException) RuntimeProperties(io.fabric8.api.RuntimeProperties) Properties(java.util.Properties) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) QuorumPeerConfig(org.apache.zookeeper.server.quorum.QuorumPeerConfig) FileOutputStream(java.io.FileOutputStream) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) File(java.io.File)

Example 22 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class InvocationTest method testUnderLoadAsyncProto.

@Test(timeout = 30 * 1000)
public void testUnderLoadAsyncProto() throws Exception {
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    DispatchQueue queue = Dispatch.createQueue();
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        final HelloImpl helloImpl = new HelloImpl();
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return helloImpl;
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        final Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        final AtomicInteger requests = new AtomicInteger(0);
        final AtomicInteger failures = new AtomicInteger(0);
        final long[] latencies = new long[BENCHMARK_CLIENTS * BENCHMARK_INVOCATIONS_PER_CLIENT];
        final long start = System.nanoTime();
        AsyncClient[] threads = new AsyncClient[BENCHMARK_CLIENTS];
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t] = new AsyncClient(t, BENCHMARK_INVOCATIONS_PER_CLIENT, hello, failures, requests, latencies);
            threads[t].start();
        }
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t].join();
        }
        final long end = System.nanoTime();
        long latency_sum = 0;
        for (int t = 0; t < latencies.length; t++) {
            if (latencies[t] != -1) {
                latency_sum += latencies[t];
            }
        }
        double latency_avg = ((latency_sum * 1.0d) / requests.get()) / MILLIS_IN_A_NANO;
        double request_rate = ((requests.get() * 1.0d) / (end - start)) * SECONDS_IN_A_NANO;
        System.err.println(String.format("Requests/Second: %,.2f", request_rate));
        System.err.println(String.format("Average request latency: %,.2f ms", latency_avg));
        System.err.println("Error Ratio: " + failures.get() + " / " + requests.get());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 23 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class InvocationTest method testOverflow.

@Test(timeout = 30 * 1000)
public void testOverflow() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        final Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        final AtomicInteger requests = new AtomicInteger(0);
        final AtomicInteger responses = new AtomicInteger(0);
        final AtomicInteger failures = new AtomicInteger(0);
        char[] chars = new char[65 * 1024];
        final String payload = new String(chars);
        Thread[] threads = new Thread[BENCHMARK_CLIENTS];
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t] = new Thread() {

                public void run() {
                    try {
                        requests.incrementAndGet();
                        hello.hello(payload);
                        responses.incrementAndGet();
                    } catch (Throwable t) {
                        failures.incrementAndGet();
                    }
                }
            };
            threads[t].start();
        }
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t].join(10000);
            System.err.format("REQUEST: %d of %d%n", requests.get(), BENCHMARK_CLIENTS);
            System.err.format("RESPONSES: %d of %d%n", responses.get(), BENCHMARK_CLIENTS);
            assertEquals(threads[t].isAlive(), false);
        }
        assertEquals(BENCHMARK_CLIENTS, requests.get());
        assertEquals(BENCHMARK_CLIENTS, responses.get());
        assertEquals(0, failures.get());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 24 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class InvocationTest method testOverflowAsync.

@Test(timeout = 30 * 1000)
public void testOverflowAsync() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        char[] chars = new char[65 * 1024];
        String payload = new String(chars);
        final List<AsyncCallbackFuture<String>> futures = new ArrayList<AsyncCallbackFuture<String>>();
        for (int i = 0; i < 100; i++) {
            AsyncCallbackFuture<String> future = new AsyncCallbackFuture<String>();
            hello.hello(payload, future);
            futures.add(future);
        }
        for (Future<String> f : futures) {
            f.get(3, TimeUnit.SECONDS);
        }
    // future2.get(2, TimeUnit.SECONDS);
    // assertEquals("Hello Hiram!", future1.get(2, TimeUnit.SECONDS));
    // assertEquals("Hello Hiram!", hello.protobuf(stringValue(payload)).getValue());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 25 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class InvocationTest method testInvoke.

@Test(timeout = 30 * 1000)
public void testInvoke() throws Exception {
    DispatchQueue queue = Dispatch.createQueue();
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    map.put("protobuf", new ProtobufSerializationStrategy());
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return new HelloImpl();
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        assertEquals("Hello Fabric!", hello.hello("Fabric"));
        assertEquals("Hello World!", hello.helloworld());
        // Verification the we can pick the right overloaded method even if using a mixure
        // of primitives / objects and array dimensions.
        assertEquals('a', hello.mix(0));
        assertEquals('b', hello.mix(new int[] { 0 }));
        assertEquals('c', hello.mix(new Integer(0)));
        assertEquals('d', hello.mix(new Integer[] { new Integer(0) }));
        assertEquals('e', hello.mix(new int[0][0]));
        assertEquals('f', hello.mix(new Integer[0][0]));
        AsyncCallbackFuture<String> future1 = new AsyncCallbackFuture<String>();
        hello.hello("Hiram", future1);
        assertEquals("Hello Hiram!", future1.get(2, TimeUnit.SECONDS));
        assertEquals("Hello Hiram!", hello.protobuf(stringValue("Hiram")).getValue());
        AsyncCallbackFuture<StringValue.Getter> future2 = new AsyncCallbackFuture<StringValue.Getter>();
        hello.protobuf(stringValue("Hiram Async"), future2);
        assertEquals("Hello Hiram Async!", future2.get(2, TimeUnit.SECONDS).getValue());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)20 Test (org.junit.Test)19 IOException (java.io.IOException)18 File (java.io.File)9 Map (java.util.Map)9 ClientInvokerImpl (io.fabric8.dosgi.tcp.ClientInvokerImpl)8 ServerInvokerImpl (io.fabric8.dosgi.tcp.ServerInvokerImpl)8 ArrayList (java.util.ArrayList)8 Profile (io.fabric8.api.Profile)7 ServerInvoker (io.fabric8.dosgi.io.ServerInvoker)7 InvocationHandler (java.lang.reflect.InvocationHandler)7 DispatchQueue (org.fusesource.hawtdispatch.DispatchQueue)7 Container (io.fabric8.api.Container)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)6 Version (io.fabric8.api.Version)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 FabricException (io.fabric8.api.FabricException)4 RuntimeProperties (io.fabric8.api.RuntimeProperties)4 InputStream (java.io.InputStream)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3