Search in sources :

Example 1 with HelixRestNamespace

use of org.apache.helix.rest.common.HelixRestNamespace in project helix by apache.

the class AbstractTestClass method getTestContainerFactory.

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
    return new TestContainerFactory() {

        @Override
        public TestContainer create(final URI baseUri, DeploymentContext deploymentContext) {
            return new TestContainer() {

                private HelixRestServer _helixRestServer;

                @Override
                public ClientConfig getClientConfig() {
                    return null;
                }

                @Override
                public URI getBaseUri() {
                    return baseUri;
                }

                @Override
                public void start() {
                    // Create namespace manifest map
                    List<HelixRestNamespace> namespaces = new ArrayList<>();
                    // Add test namespace
                    namespaces.add(new HelixRestNamespace(TEST_NAMESPACE, HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, _zkAddrTestNS, false));
                    // Add default namesapce
                    namespaces.add(new HelixRestNamespace(ZK_ADDR));
                    try {
                        _helixRestServer = new HelixRestServer(namespaces, baseUri.getPort(), baseUri.getPath(), Arrays.<AuditLogger>asList(_auditLogger));
                        _helixRestServer.start();
                    } catch (Exception ex) {
                        throw new TestContainerException(ex);
                    }
                }

                @Override
                public void stop() {
                    _helixRestServer.shutdown();
                }
            };
        }
    };
}
Also used : DeploymentContext(org.glassfish.jersey.test.DeploymentContext) AuditLogger(org.apache.helix.rest.server.auditlog.AuditLogger) TestContainer(org.glassfish.jersey.test.spi.TestContainer) ArrayList(java.util.ArrayList) HelixRestNamespace(org.apache.helix.rest.common.HelixRestNamespace) URI(java.net.URI) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) IOException(java.io.IOException) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory)

Example 2 with HelixRestNamespace

use of org.apache.helix.rest.common.HelixRestNamespace in project helix by apache.

the class TestHelixRestServer method testInvalidHelixRestServerInitialization.

@Test
public void testInvalidHelixRestServerInitialization() {
    // Namespace manifests has invalid metadata store type should generate failure
    try {
        List<HelixRestNamespace> invalidManifest1 = new ArrayList<>();
        invalidManifest1.add(new HelixRestNamespace("test1", HelixRestNamespace.HelixMetadataStoreType.valueOf("InvalidMetadataStore"), ZK_ADDR, false));
        HelixRestServer svr = new HelixRestServer(invalidManifest1, 10250, "/", Collections.<AuditLogger>emptyList());
        Assert.assertFalse(true, "InvalidManifest1 test failed");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // Namespace manifests has invalid namespace name shall generate failure
    try {
        List<HelixRestNamespace> invalidManifest2 = new ArrayList<>();
        invalidManifest2.add(new HelixRestNamespace("", HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, ZK_ADDR, true));
        HelixRestServer svr = new HelixRestServer(invalidManifest2, 10250, "/", Collections.<AuditLogger>emptyList());
        Assert.assertFalse(true, "InvalidManifest2 test failed");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // Duplicated namespace shall cause exception
    try {
        List<HelixRestNamespace> invalidManifest3 = new ArrayList<>();
        invalidManifest3.add(new HelixRestNamespace("DuplicatedName", HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, ZK_ADDR, true));
        invalidManifest3.add(new HelixRestNamespace("DuplicatedName", HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, ZK_ADDR, false));
        HelixRestServer svr = new HelixRestServer(invalidManifest3, 10250, "/", Collections.<AuditLogger>emptyList());
        Assert.assertFalse(true, "InvalidManifest3 test failed");
    } catch (IllegalArgumentException e) {
    // OK
    }
    // More than 1 default namespace shall cause failure
    try {
        List<HelixRestNamespace> invalidManifest4 = new ArrayList<>();
        invalidManifest4.add(new HelixRestNamespace("test4-1", HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, ZK_ADDR, true));
        invalidManifest4.add(new HelixRestNamespace("test4-2", HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, ZK_ADDR, true));
        HelixRestServer svr = new HelixRestServer(invalidManifest4, 10250, "/", Collections.<AuditLogger>emptyList());
        Assert.assertFalse(true, "InvalidManifest4 test failed");
    } catch (IllegalStateException e) {
    // OK
    }
}
Also used : ArrayList(java.util.ArrayList) HelixRestNamespace(org.apache.helix.rest.common.HelixRestNamespace) Test(org.testng.annotations.Test)

Example 3 with HelixRestNamespace

use of org.apache.helix.rest.common.HelixRestNamespace in project helix by apache.

the class HelixRestMain method processCommandLineArgs.

private static void processCommandLineArgs(String[] cliArgs) throws Exception {
    CommandLineParser cliParser = new GnuParser();
    Options cliOptions = constructCommandLineOptions();
    CommandLine cmd = null;
    try {
        cmd = cliParser.parse(cliOptions, cliArgs);
    } catch (ParseException pe) {
        LOG.error("RestAdminApplication: failed to parse command-line options: " + pe.toString());
        printUsage(cliOptions);
        System.exit(1);
    }
    int port = DEFAULT_PORT;
    String zkAddr;
    List<HelixRestNamespace> namespaces = new ArrayList<>();
    if (cmd.hasOption(HELP)) {
        printUsage(cliOptions);
        return;
    } else {
        if (cmd.hasOption(PORT)) {
            port = Integer.parseInt(cmd.getOptionValue(PORT));
        }
        zkAddr = String.valueOf(cmd.getOptionValue(ZKSERVERADDRESS));
        namespaces.add(new HelixRestNamespace(zkAddr));
        if (cmd.hasOption(NAMESPACE_MANIFEST_FILE)) {
            constructNamespaceFromConfigFile(String.valueOf(cmd.getOptionValue(NAMESPACE_MANIFEST_FILE)), namespaces);
        }
    }
    final HelixRestServer restServer = new HelixRestServer(namespaces, port, URI_PREFIX, Arrays.<AuditLogger>asList(new FileBasedAuditLogger()));
    try {
        restServer.start();
        restServer.join();
    } catch (HelixException ex) {
        LOG.error("Failed to start Helix rest server, " + ex);
    } finally {
        restServer.shutdown();
    }
}
Also used : Options(org.apache.commons.cli.Options) FileBasedAuditLogger(org.apache.helix.rest.server.auditlog.auditloggers.FileBasedAuditLogger) GnuParser(org.apache.commons.cli.GnuParser) ArrayList(java.util.ArrayList) HelixException(org.apache.helix.HelixException) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) HelixRestNamespace(org.apache.helix.rest.common.HelixRestNamespace)

Example 4 with HelixRestNamespace

use of org.apache.helix.rest.common.HelixRestNamespace in project helix by apache.

the class HelixRestServer method init.

private void init(List<HelixRestNamespace> namespaces, int port, String urlPrefix, List<AuditLogger> auditLoggers) {
    if (namespaces.size() == 0) {
        throw new IllegalArgumentException("No namespace specified! Please provide ZOOKEEPER address or namespace manifest.");
    }
    _port = port;
    _urlPrefix = urlPrefix;
    _server = new Server(_port);
    _auditLoggers = auditLoggers;
    _resourceConfigMap = new HashMap<>();
    _servletContextHandler = new ServletContextHandler(_server, _urlPrefix);
    _helixNamespaces = namespaces;
    // Initialize all namespaces
    try {
        for (HelixRestNamespace namespace : _helixNamespaces) {
            LOG.info("Initializing namespace " + namespace.getName());
            prepareServlet(namespace, ServletType.COMMON_SERVLET);
            if (namespace.isDefault()) {
                LOG.info("Creating default servlet for default namespace");
                prepareServlet(namespace, ServletType.DEFAULT_SERVLET);
            }
        }
    } catch (Exception e) {
        LOG.error("Failed to initialize helix rest server. Tearing down.");
        cleanupResourceConfigs();
        throw e;
    }
    // Start special servlet for serving namespaces
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            shutdown();
        }
    }));
}
Also used : Server(org.eclipse.jetty.server.Server) HelixRestNamespace(org.apache.helix.rest.common.HelixRestNamespace) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) HelixException(org.apache.helix.HelixException)

Example 5 with HelixRestNamespace

use of org.apache.helix.rest.common.HelixRestNamespace in project helix by apache.

the class NamespacesAccessor method getHelixRestNamespaces.

@GET
public Response getHelixRestNamespaces() {
    @SuppressWarnings("unchecked") List<HelixRestNamespace> allNamespaces = (List<HelixRestNamespace>) _application.getProperties().get(ContextPropertyKeys.ALL_NAMESPACES.name());
    List<Map<String, String>> ret = new ArrayList<>();
    for (HelixRestNamespace namespace : allNamespaces) {
        ret.add(namespace.getRestInfo());
    }
    return JSONRepresentation(ret);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HelixRestNamespace(org.apache.helix.rest.common.HelixRestNamespace) Map(java.util.Map) GET(javax.ws.rs.GET)

Aggregations

HelixRestNamespace (org.apache.helix.rest.common.HelixRestNamespace)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)2 HelixException (org.apache.helix.HelixException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 List (java.util.List)1 GET (javax.ws.rs.GET)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 GnuParser (org.apache.commons.cli.GnuParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 AuditLogger (org.apache.helix.rest.server.auditlog.AuditLogger)1 FileBasedAuditLogger (org.apache.helix.rest.server.auditlog.auditloggers.FileBasedAuditLogger)1 Server (org.eclipse.jetty.server.Server)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 DeploymentContext (org.glassfish.jersey.test.DeploymentContext)1