net.spy.memcached.FailureMode Java Examples

The following examples show how to use net.spy.memcached.FailureMode. 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: MemcacheClientFactoryImplTest.java    From simple-spring-memcached with MIT License 6 votes vote down vote up
@Test
public void createWithSpecificConf() throws IOException {
    SpymemcachedConfiguration conf = new SpymemcachedConfiguration();
    conf.setConsistentHashing(true);
    conf.setOperationTimeout(1000);
    conf.setUseBinaryProtocol(false);
    conf.setFailureMode(FailureMode.Cancel);
    conf.setShouldOptimize(true);
    conf.setMaxReconnectDelay(1000L);
    conf.setTimeoutExceptionThreshold(100);
    conf.setUseNagleAlgorithm(false);
    conf.setMetricCollector(new NoopMetricCollector());
    conf.setMetricType(MetricType.PERFORMANCE);
    CacheClient client = factory.create(addrs, conf);
    assertNotNull(client);
    client.shutdown();
}
 
Example #2
Source File: MemcacheClientFactoryImplTest.java    From simple-spring-memcached with MIT License 6 votes vote down vote up
@Test
public void createWithSpecificConf() throws IOException {
    ElastiCacheConfiguration conf = new ElastiCacheConfiguration();
    conf.setConsistentHashing(true);
    conf.setOperationTimeout(100);
    conf.setUseBinaryProtocol(false);
    conf.setFailureMode(FailureMode.Retry);
    conf.setShouldOptimize(true);
    conf.setMaxReconnectDelay(1000L);
    conf.setTimeoutExceptionThreshold(100);
    conf.setUseNagleAlgorithm(false);

    CacheClient client = factory.create(addrs, conf);

    assertNotNull(client);
    client.shutdown();
}
 
Example #3
Source File: MemcachedCache.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static MemcachedCache create(final MemcachedCacheConfig config, String memcachedPrefix, int timeToLive) {
    try {
        SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize());
        // always no compression inside, we compress/decompress outside
        transcoder.setCompressionThreshold(Integer.MAX_VALUE);

        OperationQueueFactory opQueueFactory;
        int maxQueueSize = config.getMaxOperationQueueSize();
        if (maxQueueSize > 0) {
            opQueueFactory = new ArrayOperationQueueFactory(maxQueueSize);
        } else {
            opQueueFactory = new LinkedOperationQueueFactory();
        }
        String hostsStr = config.getHosts();
        ConnectionFactory connectionFactory = new MemcachedConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH)
                .setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT).setDaemon(true)
                .setFailureMode(FailureMode.Redistribute).setTranscoder(transcoder).setShouldOptimize(true)
                .setOpQueueMaxBlockTime(config.getTimeout()).setOpTimeout(config.getTimeout())
                .setReadBufferSize(config.getReadBufferSize()).setOpQueueFactory(opQueueFactory).build();
        return new MemcachedCache(new MemcachedClient(new MemcachedConnectionFactory(connectionFactory),
                AddrUtil.getAddresses(hostsStr)), config, memcachedPrefix, timeToLive);
    } catch (IOException e) {
        logger.error("Unable to create MemcachedCache instance.", e);
        throw Throwables.propagate(e);
    }
}
 
Example #4
Source File: MemcachedCache.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static MemcachedCache create(final MemcachedCacheConfig config, String memcachedPrefix, int timeToLive) {
    try {
        SerializingTranscoder transcoder = new SerializingTranscoder(config.getMaxObjectSize());
        // always no compression inside, we compress/decompress outside
        transcoder.setCompressionThreshold(Integer.MAX_VALUE);

        OperationQueueFactory opQueueFactory;
        int maxQueueSize = config.getMaxOperationQueueSize();
        if (maxQueueSize > 0) {
            opQueueFactory = new ArrayOperationQueueFactory(maxQueueSize);
        } else {
            opQueueFactory = new LinkedOperationQueueFactory();
        }
        String hostsStr = config.getHosts();
        ConnectionFactory connectionFactory = new MemcachedConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setHashAlg(DefaultHashAlgorithm.FNV1A_64_HASH)
                .setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT).setDaemon(true)
                .setFailureMode(FailureMode.Redistribute).setTranscoder(transcoder).setShouldOptimize(true)
                .setOpQueueMaxBlockTime(config.getTimeout()).setOpTimeout(config.getTimeout())
                .setReadBufferSize(config.getReadBufferSize()).setOpQueueFactory(opQueueFactory).build();
        return new MemcachedCache(new MemcachedClient(new MemcachedConnectionFactory(connectionFactory),
                getResolvedAddrList(hostsStr)), config, memcachedPrefix, timeToLive);
    } catch (IOException e) {
        logger.error("Unable to create MemcachedCache instance.", e);
        throw Throwables.propagate(e);
    }
}
 
Example #5
Source File: TCPMemcachedNodeImpl.java    From kylin with Apache License 2.0 5 votes vote down vote up
public final void addOp(Operation op) {
    try {
        if (!authLatch.await(authWaitTime, TimeUnit.MILLISECONDS)) {
            FailureMode mode = connectionFactory.getFailureMode();
            if (mode == FailureMode.Redistribute || mode == FailureMode.Retry) {
                getLogger().debug("Redistributing Operation " + op + " because auth " + "latch taken longer than "
                        + authWaitTime + " milliseconds to " + "complete on node " + getSocketAddress());
                connection.retryOperation(op);
            } else {
                op.cancel();
                getLogger().warn("Operation canceled because authentication "
                        + "or reconnection and authentication has " + "taken more than " + authWaitTime
                        + " milliseconds to " + "complete on node " + this);
                getLogger().debug("Canceled operation %s", op.toString());
            }
            return;
        }
        if (!inputQueue.offer(op, opQueueMaxBlockTime, TimeUnit.MILLISECONDS)) {
            throw new IllegalStateException(
                    "Timed out waiting to add " + op + "(max wait=" + opQueueMaxBlockTime + "ms)");
        }
    } catch (InterruptedException e) {
        // Restore the interrupted status
        Thread.currentThread().interrupt();
        throw new IllegalStateException("Interrupted while waiting to add " + op);
    }
}
 
Example #6
Source File: MemcachedBlockCache.java    From hbase with Apache License 2.0 5 votes vote down vote up
public MemcachedBlockCache(Configuration c) throws IOException {
  LOG.info("Creating MemcachedBlockCache");

  long opTimeout = c.getLong(MEMCACHED_OPTIMEOUT_KEY, MEMCACHED_DEFAULT_TIMEOUT);
  long queueTimeout = c.getLong(MEMCACHED_TIMEOUT_KEY, opTimeout + MEMCACHED_DEFAULT_TIMEOUT);
  boolean optimize = c.getBoolean(MEMCACHED_OPTIMIZE_KEY, MEMCACHED_OPTIMIZE_DEFAULT);

  ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder()
      .setOpTimeout(opTimeout)
      .setOpQueueMaxBlockTime(queueTimeout) // Cap the max time before anything times out
      .setFailureMode(FailureMode.Redistribute)
      .setShouldOptimize(optimize)
      .setDaemon(true)                      // Don't keep threads around past the end of days.
      .setUseNagleAlgorithm(false)          // Ain't nobody got time for that
      .setReadBufferSize(HConstants.DEFAULT_BLOCKSIZE * 4 * 1024); // Much larger just in case

  // Assume only the localhost is serving memecached.
  // A la mcrouter or co-locating memcached with split regionservers.
  //
  // If this config is a pool of memecached servers they will all be used according to the
  // default hashing scheme defined by the memcache client. Spy Memecache client in this
  // case.
  String serverListString = c.get(MEMCACHED_CONFIG_KEY,"localhost:11211");
  String[] servers = serverListString.split(",");
  List<InetSocketAddress> serverAddresses = new ArrayList<>(servers.length);
  for (String s:servers) {
    serverAddresses.add(Addressing.createInetSocketAddressFromHostAndPortStr(s));
  }

  client = new MemcachedClient(builder.build(), serverAddresses);
}
 
Example #7
Source File: ApiMemcached.java    From iaf with Apache License 2.0 5 votes vote down vote up
public ApiMemcached() {
	AppConstants ac = AppConstants.getInstance();
	String address = ac.getProperty("etag.cache.server", "localhost:11211");
	String username = ac.getProperty("etag.cache.username", "");
	String password = ac.getProperty("etag.cache.password", "");
	int timeout = ac.getInt("etag.cache.timeout", 10);

	List<InetSocketAddress> addresses = AddrUtil.getAddresses(address);

	ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder()
		.setProtocol(Protocol.BINARY)
		.setOpTimeout(timeout)
		.setInitialObservers(Collections.singleton(obs));

	if(addresses.size()  > 1)
		connectionFactoryBuilder.setFailureMode(FailureMode.Redistribute);
	else
		connectionFactoryBuilder.setFailureMode(FailureMode.Retry);

	if(!username.isEmpty())
		connectionFactoryBuilder.setAuthDescriptor(AuthDescriptor.typical(username, password));

	ConnectionFactory cf = connectionFactoryBuilder.build();

	try {
		client = new MemcachedClient(cf, addresses);
		//Fetching a none-existing key to test the connection
		Future<Object> future = client.asyncGet("test-connection");
		future.get(timeout, TimeUnit.MILLISECONDS);
	} catch (Exception e) {
		ConfigurationWarnings.add(log, "Unable to connect to one or more memcached servers.");
	}
}
 
Example #8
Source File: BaseConnectionFactory.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public FailureMode getFailureMode() {
    try {
        return FailureMode.valueOf(failureMode.get());
    } catch (IllegalArgumentException ex) {
        return FailureMode.Cancel;
    }
}
 
Example #9
Source File: BaseAsciiConnectionFactory.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public FailureMode getFailureMode() {
    try {
        return FailureMode.valueOf(failureMode.get());
    } catch (IllegalArgumentException ex) {
        return FailureMode.Cancel;
    }
}
 
Example #10
Source File: MemcachedConnectionFactory.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public FailureMode getFailureMode() {
    return underlying.getFailureMode();
}
 
Example #11
Source File: MemcachedConnectionFactory.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public FailureMode getFailureMode() {
    return underlying.getFailureMode();
}