com.netflix.astyanax.connectionpool.Host Java Examples

The following examples show how to use com.netflix.astyanax.connectionpool.Host. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HostSupplier.java    From staash with Apache License 2.0 6 votes vote down vote up
public List<Host> get() {
        // TODO Auto-generated method stub
        List<Host>  list = new ArrayList<Host>();
//        Host h1 = new Host("ec2-54-235-224-8.compute-1.amazonaws.com",7102);
        Host h1 = new Host("54.235.224.8",7102);

//        Host h2 = new Host("ec2-54-224-106-243.compute-1.amazonaws.com",7102);
        Host h2 = new Host("54.224.106.243",7102);

//        Host h3 = new Host("ec2-54-242-127-138.compute-1.amazonaws.com",7102);
        Host h3 = new Host("54.242.127.138",7102);

        list.add(h1);
        list.add(h2);
        list.add(h3);
        return list;
    }
 
Example #2
Source File: InstanceDataDAOCassandra.java    From Raigad with Apache License 2.0 6 votes vote down vote up
private Supplier<List<Host>> getSupplier() {
    return new Supplier<List<Host>>() {
        @Override
        public List<Host> get() {
            List<Host> hosts = new ArrayList<>();
            List<String> cassandraHostnames = new ArrayList<>(Arrays.asList(StringUtils.split(config.getCommaSeparatedCassandraHostNames(), ",")));

            if (cassandraHostnames.size() == 0) {
                throw new RuntimeException("Cassandra host names can not be blank, at least one host is needed." +
                        "Please use getCommaSeparatedCassandraHostNames() property.");
            }

            for (String cassHost : cassandraHostnames) {
                logger.info("Adding Cassandra host {}", cassHost);
                hosts.add(new Host(cassHost, thriftPortForAstyanax));
            }

            return hosts;
        }
    };
}
 
Example #3
Source File: PinnedConnectionPool.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public <R> OperationResult<R> executeWithFailover(Operation<T, R> operation, RetryPolicy retryPolicy)
        throws ConnectionException, OperationException {
    Operation<T, R> pinnedOperation = new AbstractOperationFilter<T, R>(operation) {
        @Override
        public Host getPinnedHost() {
            return _host;
        }
    };

    return _delegate.executeWithFailover(pinnedOperation, retryPolicy);
}
 
Example #4
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 5 votes vote down vote up
private void trackError(Host host, Exception reason) {
    if (reason instanceof PoolTimeoutException) {
        this.poolExhaustedMeter.mark();
    }
    else if (reason instanceof TimeoutException) {
        this.socketTimeoutMeter.mark();
    }
    else if (reason instanceof OperationTimeoutException) {
        this.operationTimeoutMeter.mark();
    }
    else if (reason instanceof BadRequestException) {
        this.badRequestMeter.mark();
    }
    else if (reason instanceof NoAvailableHostsException) {
        this.noHostsMeter.mark();
    }
    else if (reason instanceof InterruptedOperationException) {
        this.interruptedMeter.mark();
    }
    else if (reason instanceof HostDownException) {
        this.hostDownMeter.mark();
    }
    else if (reason instanceof TransportException) {
        this.transportErrorMeter.mark();
    }
    else {
        log.error(reason.toString(), reason);
        this.unknownErrorMeter.mark();
    }
}
 
Example #5
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Override
public void incOperationFailure(Host host, Exception reason) {
    if (reason instanceof NotFoundException) {
        this.notFoundMeter.mark();
        return;
    }

    this.operationFailureMeter.mark();
    trackError(host, reason);
}
 
Example #6
Source File: KeyspaceUtil.java    From emodb with Apache License 2.0 5 votes vote down vote up
public Keyspace toHost(String hostName) throws ConnectionException {
    Host host = _keyspace.getConnectionPool().getPools().stream()
            .map(HostConnectionPool::getHost)
            .filter(poolHost -> hostName.equals(poolHost.getHostName()) || hostName.equals(poolHost.getIpAddress()))
            .findFirst().orElseThrow(() -> new NoAvailableHostsException("No hosts pools found"));

    return pinToVerifiedHost(host);
}
 
Example #7
Source File: KeyspaceUtil.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a view of the provided Keyspace that pins all operations to the provided host.
 */
public Keyspace toHost(Host host) throws ConnectionException {
    if (!_keyspace.getConnectionPool().getPools().stream()
                    .map(HostConnectionPool::getHost)
                    .anyMatch(poolHost -> poolHost.equals(host))) {
        throw new NoAvailableHostsException("Host not found in pool");
    }

    return pinToVerifiedHost(host);
}
 
Example #8
Source File: EurekaAstyanaxHostSupplier.java    From staash with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<List<Host>> getSupplier(final String clusterName) {
    return new Supplier<List<Host>>() {

        @Override
        public List<Host> get() {
            Application app = eurekaClient.getApplication(clusterName.toUpperCase());
            List<Host> hosts = Lists.newArrayList();
            if (app == null) {
                LOG.warn("Cluster '{}' not found in eureka", new Object[]{clusterName});
            }
            else {
                List<InstanceInfo> ins = app.getInstances();
                if (ins != null && !ins.isEmpty()) {
                    hosts = Lists.newArrayList(Collections2.transform(
                                    Collections2.filter(ins, new Predicate<InstanceInfo>() {
                                        @Override
                                        public boolean apply(InstanceInfo input) {
                                            return input.getStatus() == InstanceInfo.InstanceStatus.UP;
                                        }
                                    }), new Function<InstanceInfo, Host>() {
                                        @Override
                                        public Host apply(InstanceInfo info) {
                                            String[] parts = StringUtils.split(
                                                    StringUtils.split(info.getHostName(), ".")[0], '-');
    
                                            Host host = new Host(info.getHostName(), info.getPort())
                                                    .addAlternateIpAddress(
                                                            StringUtils.join(new String[] { parts[1], parts[2], parts[3],
                                                                    parts[4] }, "."))
                                                    .addAlternateIpAddress(info.getIPAddr())
                                                    .setId(info.getId());
                                            
                                            try {
                                                if (info.getDataCenterInfo() instanceof AmazonInfo) {
                                                    AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
                                                    host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
                                                }
                                            }
                                            catch (Throwable t) {
                                                LOG.error("Error getting rack for host " + host.getName(), t);
                                            }
    
                                            return host;
                                        }
                                    }));
                }
                else {
                    LOG.warn("Cluster '{}' found in eureka but has no instances", new Object[]{clusterName});
                }
            }
            return hosts;
        }
    };
}
 
Example #9
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public Map<Host, HostStats> getHostStats() {
    throw new UnsupportedOperationException("Not supported");
}
 
Example #10
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incOperationSuccess(Host host, long latency) {
    this.operationSuccessMeter.mark();
}
 
Example #11
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incConnectionCreated(Host host) {
    this.connectionCreateMeter.mark();
}
 
Example #12
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incConnectionClosed(Host host, Exception reason) {
    this.connectionClosedMeter.mark();
}
 
Example #13
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incConnectionCreateFailed(Host host, Exception reason) {
    this.connectionCreateFailureMeter.mark();
}
 
Example #14
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incConnectionBorrowed(Host host, long delay) {
    this.connectionBorrowMeter.mark();
}
 
Example #15
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incConnectionReturned(Host host) {
    this.connectionReturnMeter.mark();
}
 
Example #16
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void incFailover(Host host, Exception reason) {
    this.operationFailoverMeter.mark();
    trackError(host, reason);
}
 
Example #17
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void onHostAdded(Host host, HostConnectionPool<?> pool) {
    log.info("AddHost: " + host.getHostName());
    this.hostAddedMeter.mark();
}
 
Example #18
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void onHostRemoved(Host host) {
    log.info("RemoveHost: " + host.getHostName());
    this.hostRemovedMeter.mark();
}
 
Example #19
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void onHostDown(Host host, Exception reason) {
    log.info("Host down: " + host.getIpAddress() + " because ", reason);
    this.hostDownMeter.mark();
}
 
Example #20
Source File: InstrumentedConnectionPoolMonitor.java    From blueflood with Apache License 2.0 4 votes vote down vote up
@Override
public void onHostReactivated(Host host, HostConnectionPool<?> pool) {
    log.info("Reactivating " + host.getHostName());
    this.hostReactivatedMeter.mark();
}
 
Example #21
Source File: PinnedConnectionPool.java    From emodb with Apache License 2.0 4 votes vote down vote up
public PinnedConnectionPool(ConnectionPool<T> delegate, Host host) {
    checkArgument(delegate.hasHost(host), "Cannot pin to host not in pool");
    _delegate = delegate;
    _host = host;
}
 
Example #22
Source File: LocalHostSupplierProvider.java    From staash with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<List<Host>> getSupplier(String clusterName) {
    return Suppliers.ofInstance(localhost);
}
 
Example #23
Source File: LocalHostSupplierProvider.java    From staash with Apache License 2.0 4 votes vote down vote up
public LocalHostSupplierProvider() {
    localhost = Lists.newArrayList(new Host("localhost", 9160));
}
 
Example #24
Source File: EurekaAstyanaxHostSupplier.java    From staash with Apache License 2.0 4 votes vote down vote up
public Supplier<List<Host>> getSupplier(final String clusterName) {
    return new Supplier<List<Host>>() {

        public List<Host> get() {
            Application app = eurekaClient.getApplication(clusterName.toUpperCase());
            List<Host> hosts = Lists.newArrayList();
            if (app == null) {
                LOG.warn("Cluster '{}' not found in eureka", new Object[]{clusterName});
            }
            else {
                List<InstanceInfo> ins = app.getInstances();
                if (ins != null && !ins.isEmpty()) {
                    hosts = Lists.newArrayList(Collections2.transform(
                                    Collections2.filter(ins, new Predicate<InstanceInfo>() {
                                        public boolean apply(InstanceInfo input) {
                                            return input.getStatus() == InstanceInfo.InstanceStatus.UP;
                                        }
                                    }), new Function<InstanceInfo, Host>() {
                                        public Host apply(InstanceInfo info) {
                                            String[] parts = StringUtils.split(
                                                    StringUtils.split(info.getHostName(), ".")[0], '-');
    
                                            Host host = new Host(info.getHostName(), info.getPort())
                                                    .addAlternateIpAddress(
                                                            StringUtils.join(new String[] { parts[1], parts[2], parts[3],
                                                                    parts[4] }, "."))
                                                    .addAlternateIpAddress(info.getIPAddr())
                                                    .setId(info.getId());
                                            
                                            try {
                                                if (info.getDataCenterInfo() instanceof AmazonInfo) {
                                                    AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
                                                    host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
                                                }
                                            }
                                            catch (Throwable t) {
                                                LOG.error("Error getting rack for host " + host.getName(), t);
                                            }
    
                                            return host;
                                        }
                                    }));
                }
                else {
                    LOG.warn("Cluster '{}' found in eureka but has no instances", new Object[]{clusterName});
                }
            }
            return hosts;
        }
    };
}
 
Example #25
Source File: EurekaHostsSupplier.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<List<Host>> getSupplier(final String clusterName)
{
    return new Supplier<List<Host>>() {

        @Override
        public List<Host> get() {

            if (discoveryClient == null) {
                LOG.error("Discovery client cannot be null");
                throw new RuntimeException("EurekaHostsSupplier needs a non-null DiscoveryClient");
            }

            LOG.debug("Raigad fetching instance list for app: " + clusterName);

            Application app = discoveryClient.getApplication(clusterName.toUpperCase());
            List<Host> hosts = new ArrayList<Host>();

            if (app == null) {
                LOG.warn("Cluster '{}' not found in eureka", clusterName);
                return hosts;
            }

            List<InstanceInfo> ins = app.getInstances();

            if (ins == null || ins.isEmpty()) {
                LOG.warn("Cluster '{}' found in eureka but has no instances", clusterName);
                return hosts;
            }

            hosts = Lists.newArrayList(Collections2.transform(
                    Collections2.filter(ins, new Predicate<InstanceInfo>() {
                        @Override
                        public boolean apply(InstanceInfo input) {
                            return input.getStatus() == InstanceInfo.InstanceStatus.UP;
                        }
                    }), new Function<InstanceInfo, Host>() {
                        @Override
                        public Host apply(InstanceInfo info) {
                            String[] parts = StringUtils.split(
                                    StringUtils.split(info.getHostName(), ".")[0], '-');

                            Host host = new Host(info.getHostName(), info.getPort())
                                    .addAlternateIpAddress(
                                            StringUtils.join(new String[] { parts[1], parts[2], parts[3],
                                                    parts[4] }, "."))
                                    .addAlternateIpAddress(info.getIPAddr())
                                    .setId(info.getId());

                            try {
                                if (info.getDataCenterInfo() instanceof AmazonInfo) {
                                    AmazonInfo amazonInfo = (AmazonInfo)info.getDataCenterInfo();
                                    host.setRack(amazonInfo.get(MetaDataKey.availabilityZone));
                                }
                            }
                            catch (Throwable t) {
                                LOG.error("Error getting rack for host " + host.getName(), t);
                            }

                            return host;
                        }
                    }));

            LOG.debug("Raigad found hosts from eureka - num hosts: " + hosts.size());

            return hosts;
        }
    };
}
 
Example #26
Source File: AstyanaxStoreManager.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private AstyanaxContext.Builder getContextBuilder(Configuration config, int maxConnsPerHost, String usedFor) {

        final ConnectionPoolType poolType = ConnectionPoolType.valueOf(config.get(CONNECTION_POOL_TYPE));

        final NodeDiscoveryType discType = NodeDiscoveryType.valueOf(config.get(NODE_DISCOVERY_TYPE));

        final int maxConnections = config.get(MAX_CONNECTIONS);

        final int maxOperationsPerConnection = config.get(MAX_OPERATIONS_PER_CONNECTION);

        final int connectionTimeout = (int) connectionTimeoutMS.toMillis();

        ConnectionPoolConfigurationImpl cpool =
                new ConnectionPoolConfigurationImpl(usedFor + "TitanConnectionPool")
                        .setPort(port)
                        .setMaxOperationsPerConnection(maxOperationsPerConnection)
                        .setMaxConnsPerHost(maxConnsPerHost)
                        .setRetryDelaySlice(retryDelaySlice)
                        .setRetryMaxDelaySlice(retryMaxDelaySlice)
                        .setRetrySuspendWindow(retrySuspendWindow)
                        .setSocketTimeout(connectionTimeout)
                        .setConnectTimeout(connectionTimeout)
                        .setSeeds(StringUtils.join(hostnames, ","));

        if (null != retryBackoffStrategy) {
            cpool.setRetryBackoffStrategy(retryBackoffStrategy);
            log.debug("Custom RetryBackoffStrategy {}", cpool.getRetryBackoffStrategy());
        } else {
            log.debug("Default RetryBackoffStrategy {}", cpool.getRetryBackoffStrategy());
        }

        if (StringUtils.isNotBlank(localDatacenter)) {
            cpool.setLocalDatacenter(localDatacenter);
            log.debug("Set local datacenter: {}", cpool.getLocalDatacenter());
        }

        AstyanaxConfigurationImpl aconf =
                new AstyanaxConfigurationImpl()
                        .setConnectionPoolType(poolType)
                        .setDiscoveryType(discType)
                        .setTargetCassandraVersion("1.2")
                        .setMaxThriftSize(thriftFrameSizeBytes);

        if (0 < maxConnections) {
            cpool.setMaxConns(maxConnections);
        }

        if (hasAuthentication()) {
            cpool.setAuthenticationCredentials(new SimpleAuthenticationCredentials(username, password));
        }

        if (config.get(SSL_ENABLED)) {
            cpool.setSSLConnectionContext(new SSLConnectionContext(config.get(SSL_TRUSTSTORE_LOCATION), config.get(SSL_TRUSTSTORE_PASSWORD)));
        }

        AstyanaxContext.Builder ctxBuilder = new AstyanaxContext.Builder();

        // Standard context builder options
        ctxBuilder
            .forCluster(clusterName)
            .forKeyspace(keySpaceName)
            .withAstyanaxConfiguration(aconf)
            .withConnectionPoolConfiguration(cpool)
            .withConnectionPoolMonitor(new CountingConnectionPoolMonitor());

        // Conditional context builder option: host supplier
        if (config.has(HOST_SUPPLIER)) {
            String hostSupplier = config.get(HOST_SUPPLIER);
            Supplier<List<Host>> supplier = null;
            if (hostSupplier != null) {
                try {
                    supplier = (Supplier<List<Host>>) Class.forName(hostSupplier).newInstance();
                    ctxBuilder.withHostSupplier(supplier);
                } catch (Exception e) {
                    log.warn("Problem with host supplier class " + hostSupplier + ", going to use default.", e);
                }
            }
        }

        return ctxBuilder;
    }
 
Example #27
Source File: KeyspaceUtil.java    From emodb with Apache License 2.0 4 votes vote down vote up
private Keyspace pinToVerifiedHost(Host host) throws ConnectionException {
    //noinspection unchecked
    PinnedConnectionPool<Cassandra.Client> pinnedPool = new PinnedConnectionPool(_keyspace.getConnectionPool(), host);
    return new ThriftKeyspaceImpl(
            _keyspace.getKeyspaceName(), pinnedPool, _keyspace.getConfig(), EmptyKeyspaceTracerFactory.getInstance());
}
 
Example #28
Source File: PinnedConnectionPool.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public HostConnectionPool<T> getHostPool(Host host) {
    return _delegate.getHostPool(host);
}
 
Example #29
Source File: PinnedConnectionPool.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public void setHosts(Collection<Host> collection) {
    _delegate.setHosts(collection);
}
 
Example #30
Source File: PinnedConnectionPool.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasHost(Host host) {
    return _delegate.hasHost(host);
}