Search in sources :

Example 16 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class TransactionalDbCloner method cloneIndex.

private void cloneIndex(String dstDb, MongoConnection remoteConnection, WriteMongodTransaction transaction, CloneOptions opts, String fromCol, CollectionOptions collOptions) throws CloningException {
    try {
        String fromDb = opts.getDbToClone();
        HostAndPort remoteAddress = remoteConnection.getClientOwner().getAddress();
        String remoteAddressString = remoteAddress != null ? remoteAddress.toString() : "local";
        LOGGER.info("copying indexes from {}.{} on {} to {}.{} on local server", fromDb, fromCol, remoteAddressString, dstDb, fromCol);
        Status<?> status;
        List<IndexOptions> indexes = Lists.newArrayList(ListIndexesRequester.getListCollections(remoteConnection, dstDb, fromCol).getFirstBatch());
        if (indexes.isEmpty()) {
            return;
        }
        status = transaction.execute(new Request(dstDb, null, true, null), CreateIndexesCommand.INSTANCE, new CreateIndexesArgument(fromCol, indexes));
        if (!status.isOk()) {
            throw new CloningException("Error while trying to fetch indexes from remote: " + status);
        }
    } catch (MongoException ex) {
        throw new CloningException("Error while trying to fetch indexes from remote", ex);
    }
}
Also used : HostAndPort(com.google.common.net.HostAndPort) MongoException(com.eightkdata.mongowp.exceptions.MongoException) CreateIndexesArgument(com.torodb.mongodb.commands.signatures.admin.CreateIndexesCommand.CreateIndexesArgument) IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) Request(com.eightkdata.mongowp.server.api.Request)

Example 17 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class Main method main.

public static void main(String[] args) throws Exception {
    try {
        Console console = JCommander.getConsole();
        ResourceBundle cliBundle = PropertyResourceBundle.getBundle("CliMessages");
        final CliConfig cliConfig = new CliConfig();
        JCommander jCommander = new JCommander(cliConfig, cliBundle, args);
        jCommander.setColumnSize(Integer.MAX_VALUE);
        if (cliConfig.isVersion()) {
            BuildProperties buildProperties = new DefaultBuildProperties();
            console.println(buildProperties.getFullVersion());
            System.exit(0);
        }
        if (cliConfig.isHelp()) {
            jCommander.usage();
            System.exit(0);
        }
        if (cliConfig.isHelpParam()) {
            console.println(cliBundle.getString("cli.help-param-header"));
            ConfigUtils.printParamDescriptionFromConfigSchema(Config.class, cliBundle, console, 0);
            System.exit(0);
        }
        cliConfig.addParams();
        final Config config = CliConfigUtils.readConfig(cliConfig);
        if (cliConfig.isPrintConfig()) {
            ConfigUtils.printYamlConfig(config, console);
            System.exit(0);
        }
        if (cliConfig.isPrintXmlConfig()) {
            ConfigUtils.printXmlConfig(config, console);
            System.exit(0);
        }
        if (cliConfig.isPrintParam()) {
            JsonNode jsonNode = ConfigUtils.getParam(config, cliConfig.getPrintParamPath());
            if (jsonNode != null) {
                console.print(jsonNode.asText());
            }
            System.exit(0);
        }
        configureLogger(cliConfig, config);
        config.getBackend().getBackendImplementation().accept(new BackendImplementationVisitor() {

            @Override
            public void visit(AbstractDerby value) {
                parseToropassFile(value);
            }

            @Override
            public void visit(AbstractPostgres value) {
                parseToropassFile(value);
            }

            public void parseToropassFile(BackendPasswordConfig value) {
                try {
                    ConfigUtils.parseToropassFile(value);
                } catch (Exception ex) {
                    throw new SystemException(ex);
                }
            }
        });
        AbstractReplication replication = config.getReplication();
        if (replication.getAuth().getUser() != null) {
            HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource()).withDefaultPort(27017);
            ConfigUtils.parseMongopassFile(new MongoPasswordConfig() {

                @Override
                public void setPassword(String password) {
                    replication.getAuth().setPassword(password);
                }

                @Override
                public String getUser() {
                    return replication.getAuth().getUser();
                }

                @Override
                public Integer getPort() {
                    return syncSource.getPort();
                }

                @Override
                public String getPassword() {
                    return replication.getAuth().getPassword();
                }

                @Override
                public String getMongopassFile() {
                    return config.getReplication().getMongopassFile();
                }

                @Override
                public String getHost() {
                    return syncSource.getHostText();
                }

                @Override
                public String getDatabase() {
                    return replication.getAuth().getSource();
                }
            });
        }
        if (config.getBackend().isLike(AbstractPostgres.class)) {
            AbstractPostgres postgres = config.getBackend().as(AbstractPostgres.class);
            if (cliConfig.isAskForPassword()) {
                console.print("Type database user " + postgres.getUser() + "'s password:");
                postgres.setPassword(readPwd());
            }
            if (postgres.getPassword() == null) {
                throw new SystemException("No password provided for database user " + postgres.getUser() + ".\n\n" + "Please add following line to file " + postgres.getToropassFile() + ":\n" + postgres.getHost() + ":" + postgres.getPort() + ":" + postgres.getDatabase() + ":" + postgres.getUser() + ":<password>\n" + "Replace <password> for database user " + postgres.getUser() + "'s password");
            }
        }
        try {
            Clock clock = Clock.systemDefaultZone();
            Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

                @Override
                @SuppressFBWarnings(value = "DM_EXIT", justification = "Since is really hard to stop cleanly all threads when an OOME is thrown we must " + "exit to avoid no more action is performed that could lead to an unespected " + "state")
                public void uncaughtException(Thread t, Throwable e) {
                    if (e instanceof OutOfMemoryError) {
                        try {
                            LOGGER.error("Fatal out of memory: " + e.getLocalizedMessage(), e);
                        } finally {
                            System.exit(1);
                        }
                    }
                }
            });
            Service stampedeService = StampedeBootstrap.createStampedeService(config, clock);
            stampedeService.startAsync();
            stampedeService.awaitTerminated();
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                stampedeService.stopAsync();
                stampedeService.awaitTerminated();
            }));
        } catch (CreationException ex) {
            ex.getErrorMessages().stream().forEach(m -> {
                if (m.getCause() != null) {
                    LOGGER.error(m.getCause().getMessage());
                } else {
                    LOGGER.error(m.getMessage());
                }
            });
            LogManager.shutdown();
            System.exit(1);
        }
    } catch (Throwable ex) {
        LOGGER.debug("Fatal error on initialization", ex);
        Throwable rootCause = Throwables.getRootCause(ex);
        String causeMessage = rootCause.getMessage();
        LogManager.shutdown();
        JCommander.getConsole().println("Fatal error while ToroDB was starting: " + causeMessage);
        System.exit(1);
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Config(com.torodb.stampede.config.model.Config) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) ConfigUtils(com.torodb.packaging.config.util.ConfigUtils) ResourceBundle(java.util.ResourceBundle) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) JsonNode(com.fasterxml.jackson.databind.JsonNode) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) Charsets(com.google.common.base.Charsets) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) JCommander(com.beust.jcommander.JCommander) Throwables(com.google.common.base.Throwables) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) Console(com.beust.jcommander.internal.Console) HostAndPort(com.google.common.net.HostAndPort) Service(com.google.common.util.concurrent.Service) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) Logger(org.apache.logging.log4j.Logger) BuildProperties(com.torodb.core.BuildProperties) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) Clock(java.time.Clock) Log4jUtils(com.torodb.packaging.util.Log4jUtils) LogManager(org.apache.logging.log4j.LogManager) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) InputStream(java.io.InputStream) Config(com.torodb.stampede.config.model.Config) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode) Clock(java.time.Clock) HostAndPort(com.google.common.net.HostAndPort) SystemException(com.torodb.core.exceptions.SystemException) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Service(com.google.common.util.concurrent.Service) CreationException(com.google.inject.CreationException) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) IOException(java.io.IOException) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) DefaultBuildProperties(com.torodb.packaging.DefaultBuildProperties) BuildProperties(com.torodb.core.BuildProperties) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig)

Example 18 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class MongoClientConfigurationFactory method getMongoClientConfiguration.

public static MongoClientConfiguration getMongoClientConfiguration(AbstractReplication replication) {
    HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource()).withDefaultPort(27017);
    MongoClientConfiguration.Builder mongoClientConfigurationBuilder = new MongoClientConfiguration.Builder(syncSource);
    Ssl ssl = replication.getSsl();
    mongoClientConfigurationBuilder.setSslEnabled(ssl.getEnabled());
    if (ssl.getEnabled()) {
        try {
            mongoClientConfigurationBuilder.setSslAllowInvalidHostnames(ssl.getAllowInvalidHostnames());
            TrustManager[] tms = getTrustManagers(ssl);
            KeyManager[] kms = getKeyManagers(ssl);
            SSLContext sslContext;
            if (ssl.getFipsMode()) {
                sslContext = SSLContext.getInstance("TLS", "SunPKCS11-NSS");
            } else {
                sslContext = SSLContext.getInstance("TLS");
            }
            sslContext.init(kms, tms, null);
            mongoClientConfigurationBuilder.setSocketFactory(sslContext.getSocketFactory());
        } catch (CertificateException | KeyManagementException | KeyStoreException | UnrecoverableKeyException | NoSuchProviderException | NoSuchAlgorithmException | IOException exception) {
            throw new SystemException(exception);
        }
    }
    Auth auth = replication.getAuth();
    if (auth.getMode().isEnabled()) {
        MongoAuthenticationConfiguration mongoAuthenticationConfiguration = getMongoAuthenticationConfiguration(auth, ssl);
        mongoClientConfigurationBuilder.addAuthenticationConfiguration(mongoAuthenticationConfiguration);
    }
    return mongoClientConfigurationBuilder.build();
}
Also used : MongoAuthenticationConfiguration(com.eightkdata.mongowp.client.wrapper.MongoAuthenticationConfiguration) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MongoClientConfiguration(com.eightkdata.mongowp.client.wrapper.MongoClientConfiguration) Ssl(com.torodb.packaging.config.model.protocol.mongo.Ssl) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) HostAndPort(com.google.common.net.HostAndPort) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SystemException(com.torodb.core.exceptions.SystemException) Auth(com.torodb.packaging.config.model.protocol.mongo.Auth) NoSuchProviderException(java.security.NoSuchProviderException) KeyManager(javax.net.ssl.KeyManager)

Example 19 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class Main method main.

public static void main(String[] args) throws Exception {
    Console console = JCommander.getConsole();
    ResourceBundle cliBundle = PropertyResourceBundle.getBundle("CliMessages");
    final CliConfig cliConfig = new CliConfig();
    JCommander jCommander = new JCommander(cliConfig, cliBundle, args);
    jCommander.setColumnSize(Integer.MAX_VALUE);
    if (cliConfig.isHelp()) {
        jCommander.usage();
        System.exit(0);
    }
    if (cliConfig.isHelpParam()) {
        console.println(cliBundle.getString("help-param-header"));
        ResourceBundle configBundle = PropertyResourceBundle.getBundle("ConfigMessages");
        ConfigUtils.printParamDescriptionFromConfigSchema(Config.class, configBundle, console, 0);
        System.exit(0);
    }
    final Config config = CliConfigUtils.readConfig(cliConfig);
    if (cliConfig.isPrintConfig()) {
        ConfigUtils.printYamlConfig(config, console);
        System.exit(0);
    }
    if (cliConfig.isPrintXmlConfig()) {
        ConfigUtils.printXmlConfig(config, console);
        System.exit(0);
    }
    configureLogger(cliConfig, config);
    config.getBackend().getBackendImplementation().accept(new BackendImplementationVisitor() {

        @Override
        public void visit(AbstractDerby value) {
            parseToropassFile(value);
        }

        @Override
        public void visit(AbstractPostgres value) {
            parseToropassFile(value);
        }

        public void parseToropassFile(BackendPasswordConfig value) {
            try {
                ConfigUtils.parseToropassFile(value);
            } catch (Exception ex) {
                throw new SystemException(ex);
            }
        }
    });
    if (config.getProtocol().getMongo().getReplication() != null) {
        for (AbstractReplication replication : config.getProtocol().getMongo().getReplication()) {
            if (replication.getAuth().getUser() != null) {
                HostAndPort syncSource = HostAndPort.fromString(replication.getSyncSource()).withDefaultPort(27017);
                ConfigUtils.parseMongopassFile(new MongoPasswordConfig() {

                    @Override
                    public void setPassword(String password) {
                        replication.getAuth().setPassword(password);
                    }

                    @Override
                    public String getUser() {
                        return replication.getAuth().getUser();
                    }

                    @Override
                    public Integer getPort() {
                        return syncSource.getPort();
                    }

                    @Override
                    public String getPassword() {
                        return replication.getAuth().getPassword();
                    }

                    @Override
                    public String getMongopassFile() {
                        return config.getProtocol().getMongo().getMongopassFile();
                    }

                    @Override
                    public String getHost() {
                        return syncSource.getHostText();
                    }

                    @Override
                    public String getDatabase() {
                        return replication.getAuth().getSource();
                    }
                });
            }
        }
    }
    if (config.getBackend().isLike(AbstractPostgres.class)) {
        AbstractPostgres postgres = config.getBackend().as(AbstractPostgres.class);
        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + postgres.getUser() + " password:");
            postgres.setPassword(readPwd());
        }
    } else if (config.getBackend().isLike(AbstractDerby.class)) {
        AbstractDerby derby = config.getBackend().as(AbstractDerby.class);
        if (cliConfig.isAskForPassword()) {
            console.print("Database user " + derby.getUser() + " password:");
            derby.setPassword(readPwd());
        }
    }
    try {
        Clock clock = Clock.systemDefaultZone();
        Service server;
        if (config.getProtocol().getMongo().getReplication() == null || config.getProtocol().getMongo().getReplication().isEmpty()) {
            Service toroDbServer = ToroDbBootstrap.createStandaloneService(config, clock);
            toroDbServer.startAsync();
            toroDbServer.awaitRunning();
            server = toroDbServer;
        } else {
            throw new UnsupportedOperationException("Replication not supported yet!");
        }
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            server.stopAsync();
            server.awaitTerminated();
        }));
    } catch (CreationException ex) {
        ex.getErrorMessages().stream().forEach(m -> {
            if (m.getCause() != null) {
                LOGGER.error(m.getCause().getMessage());
            } else {
                LOGGER.error(m.getMessage());
            }
        });
        System.exit(1);
    } catch (Throwable ex) {
        LOGGER.error("Fatal error on initialization", ex);
        Throwable rootCause = Throwables.getRootCause(ex);
        String causeMessage = rootCause.getMessage();
        JCommander.getConsole().println("Fatal error while ToroDB was starting: " + causeMessage);
        System.exit(1);
    }
}
Also used : Charsets(com.google.common.base.Charsets) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) Config(com.torodb.standalone.config.model.Config) JCommander(com.beust.jcommander.JCommander) Throwables(com.google.common.base.Throwables) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) Console(com.beust.jcommander.internal.Console) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) HostAndPort(com.google.common.net.HostAndPort) Service(com.google.common.util.concurrent.Service) SystemException(com.torodb.core.exceptions.SystemException) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) ConfigUtils(com.torodb.packaging.config.util.ConfigUtils) CreationException(com.google.inject.CreationException) Logger(org.apache.logging.log4j.Logger) ResourceBundle(java.util.ResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) Clock(java.time.Clock) Log4jUtils(com.torodb.packaging.util.Log4jUtils) LogManager(org.apache.logging.log4j.LogManager) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) InputStream(java.io.InputStream) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig) Config(com.torodb.standalone.config.model.Config) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) BackendImplementationVisitor(com.torodb.packaging.config.visitor.BackendImplementationVisitor) MongoPasswordConfig(com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig) Service(com.google.common.util.concurrent.Service) CreationException(com.google.inject.CreationException) AbstractReplication(com.torodb.packaging.config.model.protocol.mongo.AbstractReplication) AbstractPostgres(com.torodb.packaging.config.model.backend.postgres.AbstractPostgres) Clock(java.time.Clock) IOException(java.io.IOException) SystemException(com.torodb.core.exceptions.SystemException) CreationException(com.google.inject.CreationException) HostAndPort(com.google.common.net.HostAndPort) SystemException(com.torodb.core.exceptions.SystemException) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) AbstractDerby(com.torodb.packaging.config.model.backend.derby.AbstractDerby) BackendPasswordConfig(com.torodb.packaging.config.model.backend.BackendPasswordConfig)

Example 20 with HostAndPort

use of com.google.common.net.HostAndPort in project commons by twitter.

the class AngryBirdZooKeeperServer method parseEndpoint.

private TestEndpoint parseEndpoint(String data) throws ParseException {
    ImmutableList<String> endpointComponents = ImmutableList.copyOf(AT_SPLITTER.split(data));
    if (endpointComponents.size() != 2) {
        throw new ParseException("Unknown znode data: Expected format id@host:port", 0);
    }
    String nodeId = endpointComponents.get(0);
    HostAndPort pair;
    try {
        pair = HostAndPort.fromString(endpointComponents.get(1));
    } catch (IllegalArgumentException e) {
        throw new ParseException("Failed to parse endpoint data: " + endpointComponents.get(1), data.indexOf('@'));
    }
    TestEndpoint endpoint = new TestEndpoint();
    endpoint.setNodeId(nodeId);
    endpoint.setHost(pair.getHostText());
    endpoint.setPort(pair.getPort());
    return endpoint;
}
Also used : HostAndPort(com.google.common.net.HostAndPort) TestEndpoint(com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint) ParseException(java.text.ParseException)

Aggregations

HostAndPort (com.google.common.net.HostAndPort)45 IOException (java.io.IOException)8 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 Request (com.metamx.http.client.Request)3 SequenceInputStreamResponseHandler (com.metamx.http.client.response.SequenceInputStreamResponseHandler)3 SystemException (com.torodb.core.exceptions.SystemException)3 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2 OpTime (com.eightkdata.mongowp.OpTime)2 UnreachableMongoServerException (com.eightkdata.mongowp.client.core.UnreachableMongoServerException)2 MongoException (com.eightkdata.mongowp.exceptions.MongoException)2 Charsets (com.google.common.base.Charsets)2 Throwables (com.google.common.base.Throwables)2 Service (com.google.common.util.concurrent.Service)2 CreationException (com.google.inject.CreationException)2 NoSyncSourceFoundException (com.torodb.mongodb.repl.exceptions.NoSyncSourceFoundException)2 BackendPasswordConfig (com.torodb.packaging.config.model.backend.BackendPasswordConfig)2