LOG4J2-1349 added concrete ThreadContextMap implementations
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/5c6acbf1 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/5c6acbf1 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/5c6acbf1 Branch: refs/heads/LOG4J2-1349-gcfree-threadcontext Commit: 5c6acbf15529f83f26da6b14051c8dcaff4ddc5d Parents: 2ab025e Author: rpopma <[email protected]> Authored: Sat Aug 20 14:36:22 2016 +0900 Committer: rpopma <[email protected]> Committed: Tue Aug 23 00:31:10 2016 +0900 ---------------------------------------------------------------------- ...AbstractCopyOnWriteMutableThreadContext.java | 13 +++++- ...AbstractGarbageFreeMutableThreadContext.java | 13 +++++- .../CopyOnWriteOpenHashMapThreadContextMap.java | 39 ++++++++++++++++++ .../CopyOnWriteSortedArrayThreadContextMap.java | 39 ++++++++++++++++++ .../GarbageFreeOpenHashMapThreadContextMap.java | 42 ++++++++++++++++++++ .../GarbageFreeSortedArrayThreadContextMap.java | 42 ++++++++++++++++++++ 6 files changed, 186 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c6acbf1/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractCopyOnWriteMutableThreadContext.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractCopyOnWriteMutableThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractCopyOnWriteMutableThreadContext.java index 42529f7..9837ba8 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractCopyOnWriteMutableThreadContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractCopyOnWriteMutableThreadContext.java @@ -31,6 +31,17 @@ import org.apache.logging.log4j.util.PropertiesUtil; * @since 2.7 */ public abstract class AbstractCopyOnWriteMutableThreadContext implements ThreadContextMap, ThreadContextMap2 { + + /** + * The default initial capacity. + */ + protected static final int DEFAULT_INITIAL_CAPACITY = 16; + + /** + * System property name that can be used to control the data structure's initial capacity. + */ + protected static final String PROPERTY_NAME_INITIAL_CAPACITY = "log4j2.ThreadContext.initial.capacity"; + /** * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain * {@code ThreadLocal} (value is not "true") in the implementation. @@ -62,7 +73,7 @@ public abstract class AbstractCopyOnWriteMutableThreadContext implements ThreadC protected abstract MutableContextData createMutableContextData(); - protected abstract MutableContextData createMutableContextData(final MutableContextData original); + protected abstract MutableContextData createMutableContextData(final ContextData original); @Override public void put(final String key, final String value) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c6acbf1/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractGarbageFreeMutableThreadContext.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractGarbageFreeMutableThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractGarbageFreeMutableThreadContext.java index 73636a9..7a0e6f8 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractGarbageFreeMutableThreadContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractGarbageFreeMutableThreadContext.java @@ -31,6 +31,17 @@ import org.apache.logging.log4j.util.PropertiesUtil; * @since 2.7 */ public abstract class AbstractGarbageFreeMutableThreadContext implements ThreadContextMap, ThreadContextMap2 { + + /** + * The default initial capacity. + */ + protected static final int DEFAULT_INITIAL_CAPACITY = 16; + + /** + * System property name that can be used to control the data structure's initial capacity. + */ + protected static final String PROPERTY_NAME_INITIAL_CAPACITY = "log4j2.ThreadContext.initial.capacity"; + /** * Property name ({@value} ) for selecting {@code InheritableThreadLocal} (value "true") or plain * {@code ThreadLocal} (value is not "true") in the implementation. @@ -62,7 +73,7 @@ public abstract class AbstractGarbageFreeMutableThreadContext implements ThreadC protected abstract MutableContextData createMutableContextData(); - protected abstract MutableContextData createMutableContextData(final MutableContextData original); + protected abstract MutableContextData createMutableContextData(final ContextData original); private MutableContextData getThreadLocalMap() { MutableContextData map = localMap.get(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c6acbf1/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteOpenHashMapThreadContextMap.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteOpenHashMapThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteOpenHashMapThreadContextMap.java new file mode 100644 index 0000000..a1b82bb --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteOpenHashMapThreadContextMap.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.logging.log4j.spi; + +import org.apache.logging.log4j.util.PropertiesUtil; + +/** + * {@code OpenHashMapContextData}-based implementation of the {@code ThreadContextMap} interface that creates a copy of + * the data structure on every modification. Any particular instance of the data structure is a snapshot of the + * ThreadContext at some point in time and can safely be passed off to other threads + * + * @since 2.7 + */ +public class CopyOnWriteOpenHashMapThreadContextMap extends AbstractCopyOnWriteMutableThreadContext { + @Override + protected MutableContextData createMutableContextData() { + return new OpenHashMapContextData<>(PropertiesUtil.getProperties().getIntegerProperty( + PROPERTY_NAME_INITIAL_CAPACITY, DEFAULT_INITIAL_CAPACITY)); + } + + @Override + protected MutableContextData createMutableContextData(final ContextData original) { + return new OpenHashMapContextData<>(original); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c6acbf1/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java new file mode 100644 index 0000000..f1bf509 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/CopyOnWriteSortedArrayThreadContextMap.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.logging.log4j.spi; + +import org.apache.logging.log4j.util.PropertiesUtil; + +/** + * {@code SortedArrayContextData}-based implementation of the {@code ThreadContextMap} interface that creates a copy of + * the data structure on every modification. Any particular instance of the data structure is a snapshot of the + * ThreadContext at some point in time and can safely be passed off to other threads + * + * @since 2.7 + */ +public class CopyOnWriteSortedArrayThreadContextMap extends AbstractCopyOnWriteMutableThreadContext { + @Override + protected MutableContextData createMutableContextData() { + return new ArrayContextData(PropertiesUtil.getProperties().getIntegerProperty( + PROPERTY_NAME_INITIAL_CAPACITY, DEFAULT_INITIAL_CAPACITY)); + } + + @Override + protected MutableContextData createMutableContextData(final ContextData original) { + return new ArrayContextData(original); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c6acbf1/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeOpenHashMapThreadContextMap.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeOpenHashMapThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeOpenHashMapThreadContextMap.java new file mode 100644 index 0000000..09ebb15 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeOpenHashMapThreadContextMap.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.logging.log4j.spi; + +import org.apache.logging.log4j.util.PropertiesUtil; + +/** + * {@code OpenHashMapContextData}-based implementation of the {@code ThreadContextMap} interface that attempts not to + * create temporary objects. Adding and removing key-value pairs will not create temporary objects. + * <p> + * Since the underlying data structure is modified directly it is not suitable for passing by reference to other + * threads. Instead, client code needs to copy the contents when interacting with another thread. + * </p> + * + * @since 2.7 + */ +public class GarbageFreeOpenHashMapThreadContextMap extends AbstractGarbageFreeMutableThreadContext { + @Override + protected MutableContextData createMutableContextData() { + return new OpenHashMapContextData<>(PropertiesUtil.getProperties().getIntegerProperty( + PROPERTY_NAME_INITIAL_CAPACITY, DEFAULT_INITIAL_CAPACITY)); + } + + @Override + protected MutableContextData createMutableContextData(final ContextData original) { + return new OpenHashMapContextData<>(original); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c6acbf1/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java new file mode 100644 index 0000000..241af5c --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/GarbageFreeSortedArrayThreadContextMap.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.logging.log4j.spi; + +import org.apache.logging.log4j.util.PropertiesUtil; + +/** + * {@code SortedArrayContextData}-based implementation of the {@code ThreadContextMap} interface that attempts not to + * create temporary objects. Adding and removing key-value pairs will not create temporary objects. + * <p> + * Since the underlying data structure is modified directly it is not suitable for passing by reference to other + * threads. Instead, client code needs to copy the contents when interacting with another thread. + * </p> + * + * @since 2.7 + */ +public class GarbageFreeSortedArrayThreadContextMap extends AbstractGarbageFreeMutableThreadContext { + @Override + protected MutableContextData createMutableContextData() { + return new ArrayContextData(PropertiesUtil.getProperties().getIntegerProperty( + PROPERTY_NAME_INITIAL_CAPACITY, DEFAULT_INITIAL_CAPACITY)); + } + + @Override + protected MutableContextData createMutableContextData(final ContextData original) { + return new ArrayContextData(original); + } +}
