com.linecorp.armeria.internal.DefaultAttributeMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of armeria-shaded Show documentation
Show all versions of armeria-shaded Show documentation
Asynchronous HTTP/2 RPC/REST client/server library built on top of Java 8, Netty, Thrift and GRPC (armeria-shaded)
/*
* Copyright 2016 LINE Corporation
*
* LINE Corporation 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:
*
* https://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.
*/
/*
* Copyright 2012 The Netty Project
*
* The Netty Project 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:
*
* https://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.linecorp.armeria.internal;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.AttributeMap;
/**
* Default {@link AttributeMap} implementation which use simple synchronization per bucket to keep the memory
* overhead as low as possible.
*
* Note: This class has been forked from {@link io.netty.util.DefaultAttributeMap}, with the following
* changes:
*
*
* - Add {@link #attrs()}
* - Increase the default bucket size from 4 to 8
*
*/
public class DefaultAttributeMap implements AttributeMap {
// Forked from Netty at d0912f27091e4548466df81f545c017a25c9d256
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater updater =
AtomicReferenceFieldUpdater.newUpdater(DefaultAttributeMap.class,
AtomicReferenceArray.class,
"attributes");
private static final int BUCKET_SIZE = 8;
private static final int MASK = BUCKET_SIZE - 1;
// Initialize lazily to reduce memory consumption; updated by AtomicReferenceFieldUpdater above.
@VisibleForTesting
volatile AtomicReferenceArray> attributes;
@SuppressWarnings("unchecked")
@Override
public Attribute attr(AttributeKey key) {
if (key == null) {
throw new NullPointerException("key");
}
AtomicReferenceArray> attributes = this.attributes;
if (attributes == null) {
// Not using ConcurrentHashMap due to high memory consumption.
attributes = new AtomicReferenceArray<>(BUCKET_SIZE);
if (!updater.compareAndSet(this, null, attributes)) {
attributes = this.attributes;
}
}
int i = index(key);
DefaultAttribute> head = attributes.get(i);
if (head == null) {
// No head exists yet which means we may be able to add the attribute without synchronization and
// just use compare and set. At worst we need to fallback to synchronization and waste two
// allocations.
head = new DefaultAttribute();
DefaultAttribute attr = new DefaultAttribute<>(head, key);
head.next = attr;
attr.prev = head;
if (attributes.compareAndSet(i, null, head)) {
// we were able to add it so return the attr right away
return attr;
} else {
head = attributes.get(i);
}
}
synchronized (head) {
DefaultAttribute> curr = head;
for (;;) {
DefaultAttribute> next = curr.next;
if (next == null) {
DefaultAttribute attr = new DefaultAttribute<>(head, key);
curr.next = attr;
attr.prev = curr;
return attr;
}
if (next.key == key && !next.removed) {
return (Attribute) next;
}
curr = next;
}
}
}
@Override
public boolean hasAttr(AttributeKey key) {
if (key == null) {
throw new NullPointerException("key");
}
AtomicReferenceArray> attributes = this.attributes;
if (attributes == null) {
// no attribute exists
return false;
}
int i = index(key);
DefaultAttribute> head = attributes.get(i);
if (head == null) {
// No attribute exists which point to the bucket in which the head should be located
return false;
}
// We need to synchronize on the head.
synchronized (head) {
// Start with head.next as the head itself does not store an attribute.
DefaultAttribute> curr = head.next;
while (curr != null) {
if (curr.key == key && !curr.removed) {
return true;
}
curr = curr.next;
}
return false;
}
}
/**
* Returns all {@link Attribute}s this map contains.
*/
public Iterator> attrs() {
final AtomicReferenceArray> attributes = this.attributes;
if (attributes == null) {
return Collections.emptyIterator();
}
return new IteratorImpl(attributes);
}
private static int index(AttributeKey> key) {
return key.id() & MASK;
}
@Override
public String toString() {
final ToStringHelper helper = MoreObjects.toStringHelper("");
for (Iterator> i = attrs(); i.hasNext();) {
final Attribute> a = i.next();
helper.add(a.key().name(), a.get());
}
return helper.toString();
}
private static final class DefaultAttribute extends AtomicReference implements Attribute {
private static final long serialVersionUID = -2661411462200283011L;
// The head of the linked-list this attribute belongs to
private final DefaultAttribute> head;
private final AttributeKey key;
// Double-linked list to prev and next node to allow fast removal
private DefaultAttribute> prev;
private DefaultAttribute> next;
// Will be set to true one the attribute is removed via getAndRemove() or remove()
private volatile boolean removed;
DefaultAttribute(DefaultAttribute> head, AttributeKey key) {
this.head = head;
this.key = key;
}
// Special constructor for the head of the linked-list.
DefaultAttribute() {
head = this;
key = null;
}
@Override
public AttributeKey key() {
return key;
}
@Override
public T setIfAbsent(T value) {
while (!compareAndSet(null, value)) {
T old = get();
if (old != null) {
return old;
}
}
return null;
}
@Override
public T getAndRemove() {
removed = true;
T oldValue = getAndSet(null);
remove0();
return oldValue;
}
@Override
public void remove() {
removed = true;
set(null);
remove0();
}
private void remove0() {
synchronized (head) {
if (prev == null) {
// Removed before.
return;
}
prev.next = next;
if (next != null) {
next.prev = prev;
}
// Null out prev and next - this will guard against multiple remove0() calls which may corrupt
// the linked list for the bucket.
prev = null;
next = null;
}
}
}
private static final class IteratorImpl implements Iterator> {
private final AtomicReferenceArray> attributes;
private int idx = -1;
private DefaultAttribute> next;
IteratorImpl(AtomicReferenceArray> attributes) {
this.attributes = attributes;
next = findNext(null);
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public Attribute> next() {
final DefaultAttribute> next = this.next;
if (next == null) {
throw new NoSuchElementException();
}
this.next = findNext(next.next);
return next;
}
private DefaultAttribute> findNext(DefaultAttribute> next) {
loop: for (;;) {
if (next == null) {
for (idx++; idx < attributes.length(); idx++) {
final DefaultAttribute> head = attributes.get(idx);
if (head != null && head.next != null) {
next = head.next;
break;
}
}
if (next == null) {
return null;
}
}
while (next.removed) {
if ((next = next.next) == null) {
continue loop;
}
}
return next;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy