net.spy.memcached.ConnectionFactoryBuilder Java Examples

The following examples show how to use net.spy.memcached.ConnectionFactoryBuilder. 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: ExceptionSwallowingMemcachedClient.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Autowired
public ExceptionSwallowingMemcachedClient(GlobalConfigHandler globalConfigHandler, ZkCuratorHandler zkCuratorHandler) throws Exception {
    logger.info("Initializing...");
    Stat stat = zkCuratorHandler.getCurator().checkExists().forPath(ZK_CONFIG_KEY_MEMCACHED_SERVERS_FPATH);
    if (stat != null) 
    {
        ObjectMapper mapper = new ObjectMapper();
        byte[] bytes = zkCuratorHandler.getCurator().getData().forPath(ZK_CONFIG_KEY_MEMCACHED_SERVERS_FPATH);
        MemcacheConfig config = mapper.readValue(bytes,MemcacheConfig.class);
        logger.info(config.toString());
        memcachedClient = new MemcachedClient(new ConnectionFactoryBuilder(new DefaultConnectionFactory()).setOpTimeout(MEMCACHE_OP_TIMEOUT).build(),
                AddrUtil.getAddresses(config.servers));
        logger.info(String.format("MemcachedClient initialized using %s[%s]", ZK_CONFIG_KEY_MEMCACHED_SERVERS, config.servers));
        
        MemCachePeer.initialise(config.servers,config.numClients);
    }

    if (memcachedClient == null) {
        throw new Exception("*Warning* Memcached NOT initialized!");
    }
    globalConfigHandler.addSubscriber(ZK_CONFIG_KEY_MEMCACHED_SERVERS, this);
}
 
Example #2
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 6 votes vote down vote up
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
    // currently its works because this factory creates clients with the same connection settings, only memcached
    // addresses can be changed
    if (connectionFactory == null) {
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

        if (conf.isConsistentHashing()) {
            builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
            builder.setLocatorType(Locator.CONSISTENT);
        }

        builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
        if (conf.getOperationTimeout() != null) {
            builder.setOpTimeout(conf.getOperationTimeout());
        }

        if (conf instanceof SpymemcachedConfiguration) {
            setProviderSpecificSettings(builder, (SpymemcachedConfiguration) conf);
        }

        connectionFactory = builder.build();
    }

    return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
}
 
Example #3
Source File: ArcusCacheManager.java    From arcus-spring with Apache License 2.0 6 votes vote down vote up
/**
 * 캐시 매니저 내부에서 Arcus 클라이언트를 생성 및 소멸을 관리하기 위한 생성자.
 *
 * @param adminAddress             Arcus 클라이언트를 생성하기 위해 필요한 캐시의 주소
 * @param serviceCode              Arcus 클라이언트를 생성하기 위해 필요한 서비스 코드
 * @param connectionFactoryBuilder Arcus 클라이언트를 생성하기 위해 필요한 ConnectionFactory 빌더
 * @param poolSize                 Arcus 클라이언트를 생성하기 위해 필요한 클라이언트 풀 사이즈
 * @param defaultConfiguration     정의되지 않은 캐시의 기본 설정
 * @param initialCacheConfigs      생성할 캐시들의 이름과 설정들의 집합
 */
public ArcusCacheManager(
  String adminAddress,
  String serviceCode,
  ConnectionFactoryBuilder connectionFactoryBuilder,
  int poolSize,
  ArcusCacheConfiguration defaultConfiguration,
  Map<String, ArcusCacheConfiguration> initialCacheConfigs
) {
  this(
    ArcusClient.createArcusClientPool(adminAddress, serviceCode, connectionFactoryBuilder, poolSize),
    defaultConfiguration,
    initialCacheConfigs
  );
  this.internalClient = true;
}
 
Example #4
Source File: ArcusClientFactoryBean.java    From arcus-spring with Apache License 2.0 6 votes vote down vote up
@Override
public ArcusClientPool getObject() throws Exception {
  ConnectionFactoryBuilder cfb = new ConnectionFactoryBuilder();
  cfb.setFrontCacheExpireTime(frontCacheExpireTime);
  cfb.setTimeoutExceptionThreshold(timeoutExceptionThreshold);
  cfb.setFrontCacheCopyOnRead(frontCacheCopyOnRead);
  cfb.setFrontCacheCopyOnWrite(frontCacheCopyOnWrite);
  cfb.setMaxReconnectDelay(maxReconnectDelay);
  if (maxFrontCacheElements > 0) {
    cfb.setMaxFrontCacheElements(maxFrontCacheElements);
  }
  if (globalTranscoder != null) {
    cfb.setTranscoder(globalTranscoder);
  }
  client = ArcusClient.createArcusClientPool(url, serviceCode, cfb,
          poolSize);
  return client;
}
 
Example #5
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 #6
Source File: MemcachePoolMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public void activateService()
    throws Exception
{
    MemcacheConfiguration config = configuration.get();
    expiration = ( config.expiration().get() == null )
                 ? 3600
                 : config.expiration().get();
    String addresses = ( config.addresses().get() == null )
                       ? "localhost:11211"
                       : config.addresses().get();
    Protocol protocol = ( config.protocol().get() == null )
                        ? Protocol.TEXT
                        : Protocol.valueOf( config.protocol().get().toUpperCase() );
    String username = config.username().get();
    String password = config.password().get();
    String authMech = config.authMechanism().get() == null
                      ? "PLAIN"
                      : config.authMechanism().get();

    ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
    builder.setProtocol( protocol );
    if( username != null && !username.isEmpty() )
    {
        String[] authType = { authMech };
        AuthDescriptor to = new AuthDescriptor( authType, new PlainCallbackHandler( username, password ) );
        builder.setAuthDescriptor( to );
    }

    client = new MemcachedClient( builder.build(), AddrUtil.getAddresses( addresses ) );
}
 
Example #7
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 #8
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 #9
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 5 votes vote down vote up
@Override
public CacheClient create(final List<InetSocketAddress> addrs, final CacheConfiguration conf) throws IOException {
    // currently its works because this factory creates clients with the same connection settings, only memcached
    // addresses can be changed
    if (connectionFactory == null) {
        ElastiCacheConfiguration elasticacheConf = null;
        if (conf instanceof ElastiCacheConfiguration) {
            elasticacheConf = (ElastiCacheConfiguration) conf;
        }
        
        if (elasticacheConf != null && Boolean.TRUE.equals(elasticacheConf.getUseAutoDiscovery())) {
            // there is no way to use custom client settings and auto discovery together
            LOGGER.info("All cache settings will be ignored because useAutoDiscovery is true");
            return new MemcacheClientWrapper(new MemcachedClient(addrs));
        }
        
        
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();

        if (conf.isConsistentHashing()) {
            builder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
            builder.setLocatorType(Locator.CONSISTENT);
        }

        builder.setProtocol(conf.isUseBinaryProtocol() ? Protocol.BINARY : Protocol.TEXT);
        if (conf.getOperationTimeout() != null) {
            builder.setOpTimeout(conf.getOperationTimeout());
        }

        if (elasticacheConf != null) {
            setProviderSpecificSettings(builder, elasticacheConf);
        }

        connectionFactory = builder.build();
    }

    return new MemcacheClientWrapper(new MemcachedClient(connectionFactory, addrs));
}
 
Example #10
Source File: MemcacheClientTest.java    From ob1k with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupBeforeClass() throws IOException {
  final ConnectionFactory cf = new ConnectionFactoryBuilder()
    .setProtocol(ConnectionFactoryBuilder.Protocol.TEXT)
    .setTranscoder(new WhalinTranscoder())
    .setOpTimeout(1000)
    .build();

  spyClient = new MemcachedClient(cf, Collections.singletonList(new InetSocketAddress("localhost", MEMCACHED_PORT)));
}
 
Example #11
Source File: SpymemcachedFactoryTest.java    From pippo with Apache License 2.0 5 votes vote down vote up
/**
 * Test of create method, of class SpymemcachedUtil.
 */
@Test
public void testCreate_5args() {
    System.out.println("create");
    ConnectionFactoryBuilder.Protocol protocol = ConnectionFactoryBuilder.Protocol.BINARY;
    String user = "";
    String pass = "";
    String[] authMechanisms = new String[]{"PLAIN"};
    MemcachedClient result = SpymemcachedFactory.create(HOST, protocol, user, pass, authMechanisms);
    assertNotNull(result);
    result.shutdown();
}
 
Example #12
Source File: MemcachedDriver.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Override
public void startVendorInstance() throws Exception {
    String[] nodes = get("nodes").split(",");
    List<InetSocketAddress> addresses = new ArrayList<>();
    for (String node : nodes) {
        String[] addressParts = node.split(":");
        if (addressParts.length == 0 || addressParts.length > 2) {
            throw new IllegalArgumentException("Invalid node address. Example: localhost:11211");
        }

        int port = 11211; //default memcached port
        if (addressParts.length == 2) {
            port = Integer.parseInt(addressParts[1]);
        }
        addresses.add(new InetSocketAddress(addressParts[0], port));
    }

    if (get("MEMCACHED_USERNAME") != null && get("MEMCACHED_PASSWORD") != null) {
        AuthDescriptor authDescriptor =
                new AuthDescriptor(new String[]{"PLAIN"},
                        new PlainCallbackHandler(get("MEMCACHED_USERNAME"), get("MEMCACHED_PASSWORD")));
        this.client = new MemcachedClient(new ConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(authDescriptor).build(), addresses);
    } else {
        this.client = new MemcachedClient(addresses);
    }
}
 
Example #13
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 #14
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 4 votes vote down vote up
private void setProviderSpecificSettings(final ConnectionFactoryBuilder builder, final SpymemcachedConfiguration conf) {
    if (conf.getDaemon() != null) {
        builder.setDaemon(conf.getDaemon());
    }

    if (conf.getFailureMode() != null) {
        builder.setFailureMode(conf.getFailureMode());
    }

    if (conf.getHashAlg() != null) {
        builder.setHashAlg(conf.getHashAlg());
    }

    if (conf.getLocatorType() != null) {
        builder.setLocatorType(conf.getLocatorType());
    }

    if (conf.getMaxReconnectDelay() != null) {
        builder.setMaxReconnectDelay(conf.getMaxReconnectDelay());
    }

    if (conf.getOpQueueMaxBlockTime() != null) {
        builder.setOpQueueMaxBlockTime(conf.getOpQueueMaxBlockTime());
    }

    if (conf.getReadBufferSize() != null) {
        builder.setReadBufferSize(conf.getReadBufferSize());
    }

    if (conf.getShouldOptimize() != null) {
        builder.setShouldOptimize(conf.getShouldOptimize());
    }

    if (conf.getTimeoutExceptionThreshold() != null) {
        builder.setTimeoutExceptionThreshold(conf.getTimeoutExceptionThreshold());
    }

    if (conf.getUseNagleAlgorithm() != null) {
        builder.setUseNagleAlgorithm(conf.getUseNagleAlgorithm());
    }

    if (conf.getDefaultTranscoder() != null) {
        builder.setTranscoder(conf.getDefaultTranscoder());
    }

    if (conf.getAuthDescriptor() != null) {
        builder.setAuthDescriptor(conf.getAuthDescriptor());
    }
    
    if (conf.getAuthWaitTime() != null) {
        builder.setAuthWaitTime(conf.getAuthWaitTime());
    }

    if (conf.getInitialObservers() != null) {
        builder.setInitialObservers(conf.getInitialObservers());
    }

    if (conf.getMetricCollector() != null) {
        builder.setMetricCollector(conf.getMetricCollector());
    }

    if (conf.getMetricType() != null) {
        builder.setEnableMetrics(conf.getMetricType());
    }
    
    if (conf.getExecutorService() != null) {
        builder.setListenerExecutorService(conf.getExecutorService());
    }
}
 
Example #15
Source File: MemcacheClientFactoryImpl.java    From simple-spring-memcached with MIT License 4 votes vote down vote up
private void setProviderSpecificSettings(final ConnectionFactoryBuilder builder, final ElastiCacheConfiguration conf) {
    if (conf.getDaemon() != null) {
        builder.setDaemon(conf.getDaemon());
    }

    if (conf.getFailureMode() != null) {
        builder.setFailureMode(conf.getFailureMode());
    }

    if (conf.getHashAlg() != null) {
        builder.setHashAlg(conf.getHashAlg());
    }

    if (conf.getLocatorType() != null) {
        builder.setLocatorType(conf.getLocatorType());
    }

    if (conf.getMaxReconnectDelay() != null) {
        builder.setMaxReconnectDelay(conf.getMaxReconnectDelay());
    }

    if (conf.getOpQueueMaxBlockTime() != null) {
        builder.setOpQueueMaxBlockTime(conf.getOpQueueMaxBlockTime());
    }
    
    if (conf.getOpTimeout() != null) {
        builder.setOpTimeout(conf.getOpTimeout());
    }

    if (conf.getReadBufferSize() != null) {
        builder.setReadBufferSize(conf.getReadBufferSize());
    }

    if (conf.getShouldOptimize() != null) {
        builder.setShouldOptimize(conf.getShouldOptimize());
    }

    if (conf.getTimeoutExceptionThreshold() != null) {
        builder.setTimeoutExceptionThreshold(conf.getTimeoutExceptionThreshold());
    }

    if (conf.getUseNagleAlgorithm() != null) {
        builder.setUseNagleAlgorithm(conf.getUseNagleAlgorithm());
    }

    if (conf.getDefaultTranscoder() != null) {
        builder.setTranscoder(conf.getDefaultTranscoder());
    }

    if (conf.getAuthDescriptor() != null) {
        builder.setAuthDescriptor(conf.getAuthDescriptor());
    }
    
    if (conf.getAuthWaitTime() != null) {
        builder.setAuthWaitTime(conf.getAuthWaitTime());
    }

    if (conf.getInitialObservers() != null) {
        builder.setInitialObservers(conf.getInitialObservers());
    }
    
    if (conf.getClientMode() != null) {
        builder.setClientMode(conf.getClientMode());
    }
    
    if (conf.getEnableMetricType() != null) {
        builder.setEnableMetrics(conf.getEnableMetricType());
    }
    
    if (conf.getMetricCollector() != null) {
        builder.setMetricCollector(conf.getMetricCollector());
    }

}