net.spy.memcached.transcoders.SerializingTranscoder Java Examples

The following examples show how to use net.spy.memcached.transcoders.SerializingTranscoder. 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: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 6 votes vote down vote up
public void testStupidlyLargeSetAndSizeOverride() throws Exception {
    Random r = new Random();
    SerializingTranscoder st = new SerializingTranscoder(Integer.MAX_VALUE);

    st.setCompressionThreshold(Integer.MAX_VALUE);

    byte[] data = new byte[21 * 1024 * 1024];
    r.nextBytes(data);

    try {
        client.set("bigassthing", 60, data, st).get();
        fail("Didn't fail setting bigass thing.");
    } catch (ExecutionException e) {
        System.err.println("Successful failure setting bigassthing.  Ass size "
                + data.length + " bytes doesn't fit.");
        e.printStackTrace();
        OperationException oe = (OperationException) e.getCause();
        assertSame(OperationErrorType.SERVER, oe.getType());
    }

    // But I should still be able to do something.
    client.set("k", 5, "Blah");
    assertEquals("Blah", client.get("k"));
}
 
Example #2
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 6 votes vote down vote up
public void testStupidlyLargeSet() throws Exception {
    Random r = new Random();
    SerializingTranscoder st = new SerializingTranscoder();
    st.setCompressionThreshold(Integer.MAX_VALUE);

    byte[] data = new byte[21 * 1024 * 1024];
    r.nextBytes(data);

    try {
        client.set("bigassthing", 60, data, st).get();
        fail("Didn't fail setting bigass thing.");
    } catch (IllegalArgumentException e) {
        assertEquals("Cannot cache data larger than " + CachedData.MAX_SIZE
                + " bytes " + "(you tried to cache a " + data.length
                + " byte object)", e.getMessage());
    }

    // But I should still be able to do something.
    client.set("k", 5, "Blah");
    assertEquals("Blah", client.get("k"));
}
 
Example #3
Source File: ExceptionSwallowingMemcachedClient.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
/**
 * Method to allow CAS 
 * @param <T>
 * @param key
 * @param mutation
 * @param value
 * @return
 */
public <T> T cas(String key,CASMutation<T> mutation,T value,int expireSecs)
{
	Transcoder transcoder = new SerializingTranscoder();
	// The mutator who'll do all the low-level stuff.
	// Set number of retries to limit time taken..its not essential this succeeds
	CASMutator<T> mutator = new CASMutator<>(memcachedClient, transcoder,MAX_CAS_RETRIES);

	// This returns whatever value was successfully stored within the
	// cache -- either the initial list as above, or a mutated existing
	// one
	try 
	{
		return mutator.cas(hashKey(key), value, expireSecs, mutation);
	} 
	catch (Exception e) 
	{
		logger.error("Failed up update hits in cache ",e);
		return null;
	}
}
 
Example #4
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 #5
Source File: MemCachePeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
/**
 * Method to allow CAS 
 * @param <T>
 * @param key
 * @param mutation
 * @param value
 * @return
 */
public static <T> T cas(String key,CASMutation<T> mutation,T value,int expireSecs)
{
	MemcachedClient client = getClient();
	 if (client != null)
	 {
		 Transcoder transcoder = new SerializingTranscoder();
		 // The mutator who'll do all the low-level stuff.
		 // Set number of retries to limit time taken..its not essential this succeeds
		 CASMutator<T> mutator = new CASMutator<>(client, transcoder,MAX_CAS_RETRIES);

		 // This returns whatever value was successfully stored within the
		 // cache -- either the initial list as above, or a mutated existing
		 // one
		 try 
		 {
			 return mutator.cas(hashKey(key), value, expireSecs, mutation);
		 } 
		 catch (Exception e) 
		 {
			 logger.error("Failed up update hits in cache ",e);
			 return null;
		 }
	 }
	 else
		 return null;
}
 
Example #6
Source File: MemcacheImpl.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public T get( String key )
{
    Object value = client.get( prefix( key ), new SerializingTranscoder() );
    client.touch( prefix( key ), expiration );
    if( value == null )
    {
        return null;
    }
    return valueType.cast( value );
}
 
Example #7
Source File: MemcacheImpl.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public T remove( String key )
{
    String prefixedKey = prefix( key );
    Object old = client.get( prefixedKey, new SerializingTranscoder() );
    if( old != null )
    {
        client.delete( prefixedKey );
    }
    return valueType.cast( old );
}
 
Example #8
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 #9
Source File: ArcusCacheManagerTest.java    From arcus-spring with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testGetPreDefinedCache() {
  ArcusCache cache = (ArcusCache)this.arcusCacheManagerFromClient.getCache(PRE_DEFINED_CACHE_NAME);

  assertEquals(PRE_DEFINED_CACHE_NAME, cache.getName());
  assertEquals(SERVICE_ID, cache.getServiceId());
  assertEquals(SERVICE_PREFIX, cache.getPrefix());
  assertEquals(PRE_DEFINED_EXPIRE_SECONDS, cache.getExpireSeconds());
  assertEquals(TIMEOUT_MILLIS, cache.getTimeoutMilliSeconds());
  assertTrue(cache.getOperationTranscoder() instanceof SerializingTranscoder);
  assertEquals(WANT_TO_GET_EXCEPTION, cache.isWantToGetException());
}
 
Example #10
Source File: ArcusCacheManagerTest.java    From arcus-spring with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testGetMissingCache() {
  String nonDefinedCache = "non-defined-cache";
  ArcusCache cache = (ArcusCache)this.arcusCacheManagerFromClient.getCache(nonDefinedCache);

  assertEquals(nonDefinedCache, cache.getName());
  assertEquals(SERVICE_ID, cache.getServiceId());
  assertEquals(SERVICE_PREFIX, cache.getPrefix());
  assertEquals(DEFAULT_EXPIRE_SECONDS, cache.getExpireSeconds());
  assertEquals(TIMEOUT_MILLIS, cache.getTimeoutMilliSeconds());
  assertTrue(cache.getOperationTranscoder() instanceof SerializingTranscoder);
  assertEquals(WANT_TO_GET_EXCEPTION, cache.isWantToGetException());
}
 
Example #11
Source File: ConnectFactoryBuilderTestFile.java    From KodeBeagle with Apache License 2.0 4 votes vote down vote up
public void testDefaults() throws Exception {
    ConnectionFactory f = b.build();
    assertEquals(DefaultConnectionFactory.DEFAULT_OPERATION_TIMEOUT,
            f.getOperationTimeout());
    assertEquals(DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE,
            f.getReadBufSize());
    assertSame(DefaultConnectionFactory.DEFAULT_HASH, f.getHashAlg());
    assertTrue(f.getDefaultTranscoder() instanceof SerializingTranscoder);
    assertSame(DefaultConnectionFactory.DEFAULT_FAILURE_MODE,
            f.getFailureMode());
    assertEquals(0, f.getInitialObservers().size());
    assertTrue(f.getOperationFactory() instanceof AsciiOperationFactory);

    BlockingQueue<Operation> opQueue = f.createOperationQueue();
    assertTrue(opQueue instanceof ArrayBlockingQueue<?>);
    assertEquals(DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN,
            opQueue.remainingCapacity());

    BlockingQueue<Operation> readOpQueue = f.createReadOperationQueue();
    assertTrue(readOpQueue instanceof LinkedBlockingQueue<?>);

    BlockingQueue<Operation> writeOpQueue = f.createWriteOperationQueue();
    assertTrue(writeOpQueue instanceof LinkedBlockingQueue<?>);

    MemcachedNode n = (MemcachedNode) mock(MemcachedNode.class).proxy();
    assertTrue(f.createLocator(Collections.singletonList(n))
            instanceof ArrayModNodeLocator);

    SocketChannel sc = SocketChannel.open();
    try {
        assertTrue(f.createMemcachedNode(
                InetSocketAddress.createUnresolved("localhost",
                        TestConfig.PORT_NUMBER), sc, 1)
                instanceof AsciiMemcachedNodeImpl);
    } finally {
        sc.close();
    }

    assertFalse(f.isDaemon());
    assertFalse(f.shouldOptimize());
    assertFalse(f.useNagleAlgorithm());
    assertEquals(f.getOpQueueMaxBlockTime(),
            DefaultConnectionFactory.DEFAULT_OP_QUEUE_MAX_BLOCK_TIME);
    assertEquals(f.getAuthWaitTime(),
            DefaultConnectionFactory.DEFAULT_AUTH_WAIT_TIME);
}
 
Example #12
Source File: MemcacheImpl.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
@Override
public void put( String key, T value )
{
    client.set( prefix( key ), expiration, value, new SerializingTranscoder() );
}
 
Example #13
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
EVCacheClient(String appName, String zone, int id, EVCacheServerGroupConfig config,
            List<InetSocketAddress> memcachedNodesInZone, int maxQueueSize, Property<Integer> maxReadQueueSize,
                  Property<Integer> readTimeout, Property<Integer> bulkReadTimeout,
                  Property<Integer> opQueueMaxBlockTime,
                  Property<Integer> operationTimeout, EVCacheClientPool pool, boolean isDuetClient) throws IOException {
        this.memcachedNodesInZone = memcachedNodesInZone;
        this.id = id;
        this.appName = appName;
        this.zone = zone;
        this.config = config;
        this.serverGroup = config.getServerGroup();
        this.readTimeout = readTimeout;
        this.bulkReadTimeout = bulkReadTimeout;
        this.maxReadQueueSize = maxReadQueueSize;
//        this.operationTimeout = operationTimeout;
        this.pool = pool;
        this.isDuetClient = isDuetClient;

        final List<Tag> tagList = new ArrayList<Tag>(4);
        EVCacheMetricsFactory.getInstance().addAppNameTags(tagList, appName);
        tagList.add(new BasicTag(EVCacheMetricsFactory.CONNECTION_ID, String.valueOf(id)));
        tagList.add(new BasicTag(EVCacheMetricsFactory.SERVERGROUP, serverGroup.getName()));
        this.tags = Collections.<Tag>unmodifiableList(new ArrayList(tagList));

        tagList.add(new BasicTag(EVCacheMetricsFactory.STAT_NAME, EVCacheMetricsFactory.POOL_OPERATIONS));
        operationsCounter = EVCacheMetricsFactory.getInstance().getCounter(EVCacheMetricsFactory.INTERNAL_STATS, tagList);

        this.enableChunking = EVCacheConfig.getInstance().getPropertyRepository().get(this.serverGroup.getName()+ ".chunk.data", Boolean.class).orElseGet(appName + ".chunk.data").orElse(false);
        this.chunkSize = EVCacheConfig.getInstance().getPropertyRepository().get(this.serverGroup.getName() + ".chunk.size", Integer.class).orElseGet(appName + ".chunk.size").orElse(1180);
        this.writeBlock = EVCacheConfig.getInstance().getPropertyRepository().get(appName + "." + this.serverGroup.getName() + ".write.block.duration", Integer.class).orElseGet(appName + ".write.block.duration").orElse(25);
        this.chunkingTranscoder = new ChunkTranscoder();
        this.maxWriteQueueSize = maxQueueSize;
        this.ignoreTouch = EVCacheConfig.getInstance().getPropertyRepository().get(appName + "." + this.serverGroup.getName() + ".ignore.touch", Boolean.class).orElseGet(appName + ".ignore.touch").orElse(false);

        this.connectionFactory = pool.getEVCacheClientPoolManager().getConnectionFactoryProvider().getConnectionFactory(this);
        this.connectionObserver = new EVCacheConnectionObserver(this);
        this.ignoreInactiveNodes = EVCacheConfig.getInstance().getPropertyRepository().get(appName + ".ignore.inactive.nodes", Boolean.class).orElse(true);

        this.evcacheMemcachedClient = new EVCacheMemcachedClient(connectionFactory, memcachedNodesInZone, readTimeout, this);
        this.evcacheMemcachedClient.addObserver(connectionObserver);

        this.decodingTranscoder = new SerializingTranscoder(Integer.MAX_VALUE);
        decodingTranscoder.setCompressionThreshold(Integer.MAX_VALUE);

        this.evcacheValueTranscoder = new EVCacheTranscoder();
        evcacheValueTranscoder.setCompressionThreshold(Integer.MAX_VALUE);

        this.hashKeyByApp = EVCacheConfig.getInstance().getPropertyRepository().get(appName + ".hash.key", Boolean.class).orElseGet(appName + ".auto.hash.keys").orElseGet("evcache.auto.hash.keys").orElse(false);
        this.hashKeyByServerGroup = EVCacheConfig.getInstance().getPropertyRepository().get(this.serverGroup.getName() + ".hash.key", Boolean.class).orElse(false);
        this.hashingAlgo = EVCacheConfig.getInstance().getPropertyRepository().get(this.serverGroup.getName() + ".hash.algo", String.class).orElseGet(appName + ".hash.algo").orElse("siphash24");
        ping();
    }
 
Example #14
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public SerializingTranscoder getDecodingTranscoder() {
    return decodingTranscoder;
}