Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2259 Fix potential thread safe problem in InterceptorScope
- Loading branch information
Showing
8 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/ConcurrentPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2016 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.navercorp.pinpoint.profiler.context.scope; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class ConcurrentPool<K, V> implements Pool<K, V> { | ||
|
||
private final ConcurrentMap<K, V> pool = new ConcurrentHashMap<K, V>(); | ||
|
||
private final PoolObjectFactory<K, V> objectFactory; | ||
|
||
public ConcurrentPool(PoolObjectFactory<K, V> objectFactory) { | ||
if (objectFactory == null) { | ||
throw new NullPointerException("objectFactory must not be null"); | ||
} | ||
this.objectFactory = objectFactory; | ||
} | ||
|
||
@Override | ||
public V get(K key) { | ||
if (key == null) { | ||
throw new IllegalArgumentException("key must not be null"); | ||
} | ||
|
||
return pool.get(key); | ||
} | ||
|
||
@Override | ||
public V add(K key) { | ||
if (key == null) { | ||
throw new IllegalArgumentException("name must not be null"); | ||
} | ||
|
||
final V alreadyExist = this.pool.get(key); | ||
if (alreadyExist != null) { | ||
return alreadyExist; | ||
} | ||
|
||
final V newValue = this.objectFactory.create(key); | ||
final V oldValue = this.pool.putIfAbsent(key, newValue); | ||
if (oldValue != null) { | ||
return oldValue; | ||
} | ||
return newValue; | ||
} | ||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
.../src/main/java/com/navercorp/pinpoint/profiler/context/scope/InterceptorScopeFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2016 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.navercorp.pinpoint.profiler.context.scope; | ||
|
||
import com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScope; | ||
import com.navercorp.pinpoint.profiler.interceptor.scope.DefaultInterceptorScope; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class InterceptorScopeFactory implements PoolObjectFactory<String, InterceptorScope> { | ||
@Override | ||
public InterceptorScope create(String scopeName) { | ||
return new DefaultInterceptorScope(scopeName); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/Pool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2016 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.navercorp.pinpoint.profiler.context.scope; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public interface Pool<K, V> { | ||
|
||
V get(K key); | ||
|
||
V add(K key); | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
profiler/src/main/java/com/navercorp/pinpoint/profiler/context/scope/PoolObjectFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2016 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.navercorp.pinpoint.profiler.context.scope; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public interface PoolObjectFactory<K, V> { | ||
V create(K key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
profiler/src/test/java/com/navercorp/pinpoint/profiler/context/scope/ConcurrentPoolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2016 NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.navercorp.pinpoint.profiler.context.scope; | ||
|
||
import com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScope; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author Woonduk Kang(emeroad) | ||
*/ | ||
public class ConcurrentPoolTest { | ||
@Test | ||
public void testConcurrentPool() throws Exception { | ||
|
||
InterceptorScopeFactory traceScopeFactory = new InterceptorScopeFactory(); | ||
Pool<String, InterceptorScope> pool = new ConcurrentPool<String, InterceptorScope>(traceScopeFactory); | ||
|
||
final String OBJECT_NAME = "test"; | ||
|
||
Assert.assertNull(pool.get(OBJECT_NAME)); | ||
|
||
InterceptorScope addedScope = pool.add(OBJECT_NAME); | ||
Assert.assertSame(pool.get(OBJECT_NAME), addedScope); | ||
|
||
InterceptorScope exist = pool.add(OBJECT_NAME); | ||
Assert.assertSame(exist, addedScope); | ||
} | ||
|
||
} |