Skip to content

Commit

Permalink
INTERNAL: Adjust @NonNullApi and @NonNullFields using package-info.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhm0311 committed Aug 12, 2024
1 parent a6a2040 commit b00a75f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.navercorp.arcus.spring;

import javax.annotation.Nullable;

import net.spy.memcached.ArcusClient;
import net.spy.memcached.ArcusClientPool;
import net.spy.memcached.ConnectionFactoryBuilder;
Expand All @@ -29,13 +27,17 @@
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

public class ArcusClientFactoryBean implements FactoryBean<ArcusClientPool>,
DisposableBean, InitializingBean {

@Nullable
private ArcusClientPool client;
@Nullable
private String url;
@Nullable
private String serviceCode;
private int poolSize = 4;
private int frontCacheExpireTime = DefaultConnectionFactory.DEFAULT_FRONTCACHE_EXPIRETIME;
Expand All @@ -48,6 +50,7 @@ public class ArcusClientFactoryBean implements FactoryBean<ArcusClientPool>,
/**
* global transcoder for key/value store.
*/
@Nullable
private Transcoder<Object> globalTranscoder;

public void setPoolSize(int poolSize) {
Expand Down Expand Up @@ -99,19 +102,7 @@ public void destroy() {

@Override
public ArcusClientPool getObject() {
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);
this.afterPropertiesSet();
return client;
}

Expand All @@ -132,5 +123,21 @@ public void afterPropertiesSet() {
Assert.isTrue(this.poolSize > 0, "PoolSize property must be larger than 0.");
Assert.isTrue(this.timeoutExceptionThreshold > 0, "TimeoutExceptionThreshold must be larger than 0.");
Assert.isTrue(this.maxReconnectDelay > 0, "MaxReconnectDelay must be larger than 0.");

ConnectionFactoryBuilder cfb = new ConnectionFactoryBuilder()
.setFrontCacheExpireTime(frontCacheExpireTime)
.setTimeoutExceptionThreshold(timeoutExceptionThreshold)
.setFrontCacheCopyOnRead(frontCacheCopyOnRead)
.setFrontCacheCopyOnWrite(frontCacheCopyOnWrite)
.setMaxReconnectDelay(maxReconnectDelay);

if (maxFrontCacheElements > 0) {
cfb.setMaxFrontCacheElements(maxFrontCacheElements);
}
if (globalTranscoder != null) {
cfb.setTranscoder(globalTranscoder);
}

client = ArcusClient.createArcusClientPool(url, serviceCode, cfb, poolSize);
}
}
33 changes: 26 additions & 7 deletions src/main/java/com/navercorp/arcus/spring/cache/ArcusCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

import net.spy.memcached.ArcusClientPool;
import net.spy.memcached.internal.GetFuture;
import net.spy.memcached.internal.OperationFuture;
Expand All @@ -38,6 +36,7 @@

import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.support.AbstractValueAdaptingCache;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;

Expand Down Expand Up @@ -87,21 +86,26 @@ public class ArcusCache extends AbstractValueAdaptingCache implements Initializi
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private String name;
@Nullable
private String prefix;
private String serviceId;
private int expireSeconds;
private int frontExpireSeconds;
private long timeoutMilliSeconds = DEFAULT_TIMEOUT_MILLISECONDS;
private ArcusClientPool arcusClient;
private ArcusClientPool arcusClient = new ArcusClientPoolPlaceholder();
@Deprecated
private boolean wantToGetException = DEFAULT_WANT_TO_GET_EXCEPTION;
private boolean forceFrontCaching;
@Nullable
private Transcoder<Object> operationTranscoder;
private KeyLockProvider keyLockProvider = new DefaultKeyLockProvider();
@Nullable
private ArcusFrontCache arcusFrontCache;

public ArcusCache() {
super(DEFAULT_ALLOW_NULL_VALUES);
this.name = this.getNamePlaceholder();
this.serviceId = this.getServiceIdPlaceholder();
}

ArcusCache(String name, ArcusClientPool clientPool, ArcusCacheConfiguration configuration) {
Expand Down Expand Up @@ -180,7 +184,7 @@ private <T> T loadValue(Object key, Callable<T> valueLoader) {
}

@Override
public void put(final Object key, final Object value) {
public void put(final Object key, @Nullable final Object value) {
if (value == null && !isAllowNullValues()) {
throw new IllegalArgumentException(String.format("Cache '%s' does not allow 'null' values. " +
"Avoid storing null via '@Cacheable(unless=\"#result == null\")' or configure ArcusCache " +
Expand Down Expand Up @@ -208,7 +212,7 @@ public void put(final Object key, final Object value) {
*/
@Nullable
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
if (value == null && !isAllowNullValues()) {
logger.info(String.format("Cache '%s' does not allow 'null' values. " +
"Avoid storing null via '@Cacheable(unless=\"#result == null\")' or configure ArcusCache " +
Expand Down Expand Up @@ -336,10 +340,11 @@ public void setArcusClient(ArcusClientPool arcusClient) {

@Override
public void afterPropertiesSet() {
if (name == null && prefix == null) {
if (getNamePlaceholder().equals(name) && prefix == null) {
throw new IllegalArgumentException("ArcusCache's 'name' or 'prefix' property must have a value.");
}
Assert.notNull(serviceId, "ArcusCache's serviceId property must have a value.");
Assert.isTrue(!(arcusClient instanceof ArcusClientPoolPlaceholder), "arcusClient property must have a value.");
Assert.isTrue(!getServiceIdPlaceholder().equals(serviceId), "serviceId property must have a value.");
}

public String getServiceId() {
Expand Down Expand Up @@ -499,6 +504,7 @@ private void putValue(String arcusKey, Object value) throws Exception {
}
}

@Nullable
private ValueWrapper putIfAbsentValue(String arcusKey, Object value) throws Exception {
logger.debug("trying to add(putIfAbsent) key: {}", arcusKey);

Expand All @@ -520,4 +526,17 @@ private ValueWrapper putIfAbsentValue(String arcusKey, Object value) throws Exce
return success ? null : toValueWrapper(getValue(arcusKey));
}

private String getNamePlaceholder() {
return ArcusCacheManager.getStringFieldPlaceholder("name", this);
}

private String getServiceIdPlaceholder() {
return ArcusCacheManager.getStringFieldPlaceholder("serviceId", this);
}

static class ArcusClientPoolPlaceholder extends ArcusClientPool {
public ArcusClientPoolPlaceholder() {
super(0, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@

import com.navercorp.arcus.spring.cache.front.ArcusFrontCache;

import javax.annotation.Nullable;

import net.spy.memcached.transcoders.Transcoder;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

@SuppressWarnings("DeprecatedIsStillUsed")
public class ArcusCacheConfiguration implements InitializingBean {

private String serviceId;
@Nullable
private String prefix;
private int expireSeconds;
private int frontExpireSeconds;
private long timeoutMilliSeconds = ArcusCache.DEFAULT_TIMEOUT_MILLISECONDS;
@Nullable
private Transcoder<Object> operationTranscoder;
@Nullable
private ArcusFrontCache arcusFrontCache;
@Deprecated
private boolean wantToGetException = ArcusCache.DEFAULT_WANT_TO_GET_EXCEPTION;
private boolean forceFrontCaching;
private boolean allowNullValues = ArcusCache.DEFAULT_ALLOW_NULL_VALUES;

public ArcusCacheConfiguration() {
this.serviceId = this.getServiceIdPlaceholder();
}

public String getServiceId() {
return serviceId;
}
Expand Down Expand Up @@ -126,8 +132,13 @@ public void setAllowNullValues(boolean allowNullValues) {
this.allowNullValues = allowNullValues;
}

private String getServiceIdPlaceholder() {
return ArcusCacheManager.getStringFieldPlaceholder("serviceId", this);
}

@Override
public void afterPropertiesSet() {
Assert.isTrue(!getServiceIdPlaceholder().equals(serviceId), "ServiceId must be set.");
Assert.isTrue(timeoutMilliSeconds > 0, "TimeoutMilliSeconds must be larger than 0.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ public void destroy() {
client.shutdown();
}
}

static String getStringFieldPlaceholder(String fieldName, Object o) {
return fieldName + ":" + o.getClass().getName() + "@" + o.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

import java.lang.reflect.Method;

import javax.annotation.Nullable;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.lang.Nullable;

public class SimpleStringKeyGenerator implements KeyGenerator {
private static final String DEFAULT_SEPARATOR = ",";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

import java.lang.reflect.Method;

import javax.annotation.Nullable;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.lang.Nullable;

/**
* 스프링 Cache의 KeyGenerator 구현체.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package com.navercorp.arcus.spring.cache.front;

import javax.annotation.Nullable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.lang.Nullable;

public class EhArcusFrontCache implements ArcusFrontCache {

private final Cache cache;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package com.navercorp.arcus.spring.cache;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.navercorp.arcus.spring.concurrent;

import java.util.Objects;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

Expand Down Expand Up @@ -49,10 +50,7 @@ public ReadWriteLock getLockForKey(Object key) {
}

private int selectLock(Object key) {
if (key == null) {
return 0;
}
return key.hashCode() & (mutexes.length - 1);
return Objects.hashCode(key) & (mutexes.length - 1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package com.navercorp.arcus.spring.concurrent;
3 changes: 3 additions & 0 deletions src/main/java/com/navercorp/arcus/spring/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
package com.navercorp.arcus.spring;

0 comments on commit b00a75f

Please sign in to comment.