Skip to content

Commit

Permalink
[#11503] Remove unnecessary IdAllocator
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Sep 20, 2024
1 parent ed3b57c commit fc6a9c4
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.Result;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
import com.navercorp.pinpoint.profiler.metadata.ApiMetaData;
Expand All @@ -32,7 +31,7 @@
*/
public class MockApiMetaDataService implements ApiMetaDataService {

private final SimpleCache<String> apiCache = new SimpleCache<>(new IdAllocator.ZigZagAllocator());
private final SimpleCache<String> apiCache = new SimpleCache<>();

private final EnhancedDataSender<MetaDataType> enhancedDataSender;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,23 @@

import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.Objects;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @author emeroad
*/
public class SimpleCache<T> implements Cache<T, Result<Integer>> {
// zero means not exist.
private final ConcurrentMap<T, Result<Integer>> cache;
private final IdAllocator idAllocator;
private final AtomicInteger idGen = new AtomicInteger();

public SimpleCache(IdAllocator idAllocator) {
this(idAllocator, 1024);
public SimpleCache() {
this(1024);
}

public SimpleCache(IdAllocator idAllocator, int cacheSize) {
public SimpleCache(int cacheSize) {
this.cache = createCache(cacheSize);
this.idAllocator = Objects.requireNonNull(idAllocator, "idTransformer");

}

private ConcurrentMap<T, Result<Integer>> createCache(int maxCacheSize) {
Expand Down Expand Up @@ -65,6 +63,6 @@ public Result<Integer> put(T value) {
}

private int nextId() {
return this.idAllocator.allocate();
return this.idGen.incrementAndGet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,22 @@

package com.navercorp.pinpoint.profiler.context.provider.metadata;

import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;

import java.util.Objects;

/**
* @author Woonduk Kang(emeroad)
*/
public class SimpleCacheFactory {

private final IdAllocator.ID_TYPE type;

public SimpleCacheFactory(IdAllocator.ID_TYPE type) {
this.type = Objects.requireNonNull(type, "type");
public SimpleCacheFactory() {
}

public <T> SimpleCache<T> newSimpleCache() {
IdAllocator idAllocator = newIdAllocator(type, 1);
return new SimpleCache<>(idAllocator);
}

public <T> SimpleCache<T> newSimpleCache(int size) {
IdAllocator idAllocator = newIdAllocator(type, size);
return new SimpleCache<>(idAllocator, size);
return new SimpleCache<>();
}

private IdAllocator newIdAllocator(IdAllocator.ID_TYPE type, int size) {
switch (type) {
case BYPASS:
return new IdAllocator.BypassAllocator(size);
case ZIGZAG:
return new IdAllocator.ZigZagAllocator(size);
}
throw new RuntimeException("Unknown SimpleCache.ID_TYPE:" + type);
public <T> SimpleCache<T> newSimpleCache(int cacheSize) {
return new SimpleCache<>(cacheSize);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,23 @@
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.navercorp.pinpoint.bootstrap.config.TransportModule;
import com.navercorp.pinpoint.profiler.cache.IdAllocator;

/**
* @author Woonduk Kang(emeroad)
*/
public class SimpleCacheFactoryProvider implements Provider<SimpleCacheFactory> {

private final IdAllocator.ID_TYPE type;

@Inject
public SimpleCacheFactoryProvider(TransportModule transportModule) {
// if (TransportModule.THRIFT == transportModule) {
// this.type = IdAllocator.ID_TYPE.ZIGZAG;
// }
if (TransportModule.GRPC == transportModule) {
this.type = IdAllocator.ID_TYPE.BYPASS;
// do nothing
} else {
throw new IllegalStateException("Unsupported transportModule:" + transportModule);
}
}

@Override
public SimpleCacheFactory get() {
return new SimpleCacheFactory(type);
return new SimpleCacheFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,27 @@ public class SimpleCacheTest {

@Test
public void startKey0() {
SimpleCache<String> cache = new SimpleCache<>(new IdAllocator.ZigZagAllocator(0), 1024);
Result<Integer> test = cache.put("test");
Assertions.assertEquals(0, test.getId());
SimpleCache<String> cache = new SimpleCache<>();
Result<Integer> test1 = cache.put("test1");
Result<Integer> test2 = cache.put("test2");
Assertions.assertEquals(1, test1.getId());
Assertions.assertEquals(2, test2.getId());
}

@Test
public void startKey1() {
SimpleCache<String> cache = new SimpleCache<>(new IdAllocator.ZigZagAllocator(), 1);
Result<Integer> test = cache.put("test");
Assertions.assertEquals(-1, test.getId());
}

@Test
public void put() {
SimpleCache<String> cache = new SimpleCache<>(new IdAllocator.ZigZagAllocator());
SimpleCache<String> cache = new SimpleCache<>();
Result<Integer> test = cache.put("test");
Assertions.assertEquals(-1, test.getId());
Assertions.assertEquals(1, test.getId());
Assertions.assertTrue(test.isNewValue());

Result<Integer> recheck = cache.put("test");
Assertions.assertEquals(test.getId(), recheck.getId());
Assertions.assertFalse(recheck.isNewValue());

Result<Integer> newValue = cache.put("new");
Assertions.assertEquals(1, newValue.getId());
Assertions.assertEquals(2, newValue.getId());
Assertions.assertTrue(newValue.isNewValue());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
import com.navercorp.pinpoint.profiler.context.DefaultMethodDescriptor;
import org.junit.jupiter.api.Assertions;
Expand All @@ -36,7 +35,7 @@ public class DefaultApiMetaDataServiceTest {
@Test
public void cacheApi() {
EnhancedDataSender<MetaDataType> dataSender = mock(EnhancedDataSender.class);
SimpleCache<String> cache = new SimpleCache<>(new IdAllocator.ZigZagAllocator(1));
SimpleCache<String> cache = new SimpleCache<>();
ApiMetaDataService apiMetaDataService = new DefaultApiMetaDataService(dataSender, cache);

MethodDescriptor methodDescriptor = new DefaultMethodDescriptor("clazz", "method",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.navercorp.pinpoint.profiler.metadata;

import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -34,7 +33,7 @@ public class DefaultStringMetaDataServiceTest {
@Test
public void cacheString() {
EnhancedDataSender<MetaDataType> dataSender = mock(EnhancedDataSender.class);
SimpleCache<String> stringCache = new SimpleCache<>(new IdAllocator.ZigZagAllocator());
SimpleCache<String> stringCache = new SimpleCache<>();
StringMetaDataService stringMetaDataService = new DefaultStringMetaDataService(dataSender, stringCache);

String str = "test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.navercorp.pinpoint.profiler.metadata;

import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,6 +62,6 @@ public void testNormalizedSql_cache_expire() {
}

private SimpleCache<String> newCache(int size) {
return new SimpleCache<>(new IdAllocator.ZigZagAllocator(), size);
return new SimpleCache<>(size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.navercorp.pinpoint.profiler.metadata;

import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -45,7 +44,7 @@ public class SqlCacheServiceTest {

@BeforeEach
public void setUp() {
SimpleCache<String> sqlCache = new SimpleCache<>(new IdAllocator.ZigZagAllocator(), 100);
SimpleCache<String> sqlCache = new SimpleCache<>(100);
SimpleCachingSqlNormalizer cachingSqlNormalizer = new SimpleCachingSqlNormalizer(sqlCache);
sut = new SqlCacheService<>(dataSender, cachingSqlNormalizer, MAX_LENGTH);
}
Expand Down

0 comments on commit fc6a9c4

Please sign in to comment.