Search in sources :

Example 1 with MemcacheSetRequest

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest in project appengine-java-standard by GoogleCloudPlatform.

the class LocalMemcacheService method set.

public MemcacheSetResponse set(Status status, MemcacheSetRequest req) {
    MemcacheSetResponse.Builder result = MemcacheSetResponse.newBuilder();
    final String namespace = req.getNameSpace();
    for (int i = 0; i < req.getItemCount(); i++) {
        MemcacheSetRequest.Item item = req.getItem(i);
        Key key = new Key(item.getKey().toByteArray());
        SetPolicy policy = item.getSetPolicy();
        Map<Key, Long> timeoutMap = getOrMakeSubMap(deleteHold, namespace);
        Long timeout = timeoutMap.get(key);
        if (timeout != null && policy == SetPolicy.SET) {
            // A SET operation overrides and clears any timeout that may exist
            timeout = null;
            timeoutMap.remove(key);
        }
        if ((timeout != null && clock.getCurrentTime() < timeout) || (policy == SetPolicy.CAS && !item.hasCasId())) {
            result.addSetStatus(SetStatusCode.NOT_STORED);
            continue;
        }
        synchronized (mockCache) {
            CacheEntry existingEntry = getWithExpiration(namespace, key);
            if ((policy == SetPolicy.REPLACE && existingEntry == null) || (policy == SetPolicy.ADD && existingEntry != null) || (policy == SetPolicy.CAS && existingEntry == null)) {
                result.addSetStatus(SetStatusCode.NOT_STORED);
            } else if (policy == SetPolicy.CAS && (!existingEntry.hasCasId() || existingEntry.getCasId() != item.getCasId())) {
                result.addSetStatus(SetStatusCode.EXISTS);
            } else {
                long expiry = item.hasExpirationTime() ? (long) item.getExpirationTime() : 0L;
                byte[] value = item.getValue().toByteArray();
                int flags = item.getFlags();
                // We create a new cacheEntry every time (rather than updating existing ones) to
                // avoid having to synchronize on reads. (Otherwise a reader on another thread
                // could be exposed to a partially modified entry).
                CacheEntry newEntry = new CacheEntry(namespace, key, value, flags, expiry * 1000);
                internalSet(namespace, key, newEntry);
                result.addSetStatus(SetStatusCode.STORED);
            }
        }
    }
    status.setSuccessful(true);
    return result.build();
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) SetPolicy(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest.SetPolicy) AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteString(com.google.protobuf.ByteString) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)

Example 2 with MemcacheSetRequest

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testPutIfUntouched.

@Test
public void testPutIfUntouched() {
    MemcacheServiceImpl memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    long casId = 5;
    for (String key : new String[] { ONE, null }) {
        byte[] pbKey = makePbKey(key);
        MemcacheSerialization.ValueAndFlags oneVaf = serialize(ONE);
        MemcacheSerialization.ValueAndFlags twoVaf = serialize(TWO);
        MemcacheGetRequest getRequest = MemcacheGetRequest.newBuilder().setNameSpace(namespace).setForCas(true).addKey(ByteString.copyFrom(pbKey)).build();
        MemcacheGetResponse getResponse = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(oneVaf.flags.ordinal()).setKey(ByteString.copyFrom(pbKey)).setValue(ByteString.copyFrom(oneVaf.value)).setCasId(casId)).build();
        expectAsyncCall("Get", getRequest, getResponse);
        MemcacheSetRequest setRequest = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setValue(ByteString.copyFrom(twoVaf.value)).setFlags(twoVaf.flags.ordinal()).setKey(ByteString.copyFrom(pbKey)).setExpirationTime(0).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(casId)).build();
        MemcacheSetResponse setResponse = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.STORED).build();
        expectAsyncCallWithoutReset("Set", setRequest, setResponse);
        IdentifiableValueImpl v = (IdentifiableValueImpl) memcache.getIdentifiable(key);
        assertThat(v.getCasId()).isEqualTo(casId);
        assertThat(v.getValue()).isEqualTo(ONE);
        assertThat(memcache.putIfUntouched(key, v, TWO)).isTrue();
        verifyAsyncCall("Set", setRequest);
    }
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) ByteString(com.google.protobuf.ByteString) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 3 with MemcacheSetRequest

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testMultiPutWithOversizeValue.

@Test
public void testMultiPutWithOversizeValue() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    NamespaceManager.set("");
    int expirySecs = (int) (System.currentTimeMillis() / 1000) + 600;
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    String namespace = "";
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date((long) expirySecs * 1000));
    byte[] oneKey = makePbKey(ONE);
    byte[] twoKey = makePbKey(TWO);
    byte[] threeKey = makePbKey(THREE);
    String oneValue = "first value";
    String theVeryLongValue = Strings.repeat("x", 110 * AsyncMemcacheServiceImpl.ITEM_SIZE_LIMIT / 100);
    String threeValue = "third value";
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(oneKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(oneValue).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(twoKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(theVeryLongValue).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(threeKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(threeValue).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    expectAsyncCall("Set", request, new ApiProxy.ApplicationException(MemcacheServiceError.ErrorCode.UNSPECIFIED_ERROR_VALUE, "Error"));
    // ImmutableMap has deterministic ordering so the order of insertion matches the request PB
    Map<Serializable, String> collection = ImmutableMap.of(ONE, oneValue, TWO, theVeryLongValue, THREE, threeValue);
    // Make sure that an RPC application error of INVALID_REQUEST will result
    // in an exception with a message containing "bigger than maximum allowed"
    memcache.setErrorHandler(ErrorHandlers.getStrict());
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.putAll(collection, expiration, setPolicy));
    assertWithMessage("Expected exception to mention that maximum value exceeded but got " + ex).that(ex).hasMessageThat().contains("bigger than maximum allowed");
}
Also used : Serializable(java.io.Serializable) ApiProxy(com.google.apphosting.api.ApiProxy) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) Test(org.junit.Test)

Example 4 with MemcacheSetRequest

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testSetTooLargeSingleItemError.

@Test
public void testSetTooLargeSingleItemError() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    NamespaceManager.set(namespace);
    int expirySecs = (int) (MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600);
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date(SECONDS.toMillis(expirySecs)));
    String testkey = "testkey";
    byte[] key = makePbKey(testkey);
    String value = Strings.repeat("v", (AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key.length + 1));
    int itemSize = value.length() + key.length;
    String expectedMessage = String.format("Memcache put: Item may not be more than %d bytes in " + "length; received %d bytes.", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE, itemSize);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.ERROR).build();
    expectAsyncCall("Set", request, response);
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.put(key, value, expiration, setPolicy));
    assertThat(ex).hasMessageThat().isEqualTo(expectedMessage);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 5 with MemcacheSetRequest

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testSetTooLargeMultiError.

@Test
public void testSetTooLargeMultiError() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    NamespaceManager.set(namespace);
    int expirySecs = (int) (MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600);
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date(SECONDS.toMillis(expirySecs)));
    String expectedMessage = "Memcache put: 1 items failed for exceeding " + AsyncMemcacheServiceImpl.MAX_ITEM_SIZE + " bytes; keys: two. ";
    byte[] key1 = makePbKey(ONE);
    byte[] key2 = makePbKey(TWO);
    byte[] key3 = makePbKey(THREE);
    // Only the second item will fail.
    String value1 = Strings.repeat("v", 1000);
    String value2 = Strings.repeat("v", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key2.length + 1);
    String value3 = Strings.repeat("v", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key3.length);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key1)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value1).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key2)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value2).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key3)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value3).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse.SetStatusCode[] responses = new MemcacheSetResponse.SetStatusCode[] { MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.ERROR, MemcacheSetResponse.SetStatusCode.STORED };
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(responses[0]).addSetStatus(responses[1]).addSetStatus(responses[2]).build();
    Map<Serializable, String> collection = ImmutableMap.of(ONE, value1, TWO, value2, THREE, value3);
    expectAsyncCall("Set", request, response);
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.putAll(collection, expiration, setPolicy));
    assertThat(ex).hasMessageThat().isEqualTo(expectedMessage);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) Serializable(java.io.Serializable) SetStatusCode(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) Test(org.junit.Test)

Aggregations

MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)12 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)8 ByteString (com.google.protobuf.ByteString)6 Date (java.util.Date)5 Test (org.junit.Test)5 MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)4 SetPolicy (com.google.appengine.api.memcache.MemcacheService.SetPolicy)3 ApiProxy (com.google.apphosting.api.ApiProxy)3 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)2 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)2 MemcacheBatchIncrementRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementRequest)2 MemcacheDeleteRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteRequest)2 MemcacheFlushRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest)2 MemcacheGetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse)2 MemcacheIncrementRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest)2 SetStatusCode (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode)2 Serializable (java.io.Serializable)2 LinkedHashMap (java.util.LinkedHashMap)2 GetAccessTokenRequest (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenRequest)1 GetAccessTokenResponse (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenResponse)1